lto-cgraph.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. /* Write and read the cgraph to the memory mapped representation of a
  2. .o file.
  3. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  4. Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "tm.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "fold-const.h"
  32. #include "stringpool.h"
  33. #include "predict.h"
  34. #include "hard-reg-set.h"
  35. #include "function.h"
  36. #include "basic-block.h"
  37. #include "tree-ssa-alias.h"
  38. #include "internal-fn.h"
  39. #include "gimple-expr.h"
  40. #include "is-a.h"
  41. #include "gimple.h"
  42. #include "hashtab.h"
  43. #include "rtl.h"
  44. #include "flags.h"
  45. #include "statistics.h"
  46. #include "real.h"
  47. #include "fixed-value.h"
  48. #include "insn-config.h"
  49. #include "expmed.h"
  50. #include "dojump.h"
  51. #include "explow.h"
  52. #include "calls.h"
  53. #include "emit-rtl.h"
  54. #include "varasm.h"
  55. #include "stmt.h"
  56. #include "expr.h"
  57. #include "params.h"
  58. #include "langhooks.h"
  59. #include "bitmap.h"
  60. #include "diagnostic-core.h"
  61. #include "except.h"
  62. #include "timevar.h"
  63. #include "hash-map.h"
  64. #include "plugin-api.h"
  65. #include "ipa-ref.h"
  66. #include "cgraph.h"
  67. #include "lto-streamer.h"
  68. #include "data-streamer.h"
  69. #include "tree-streamer.h"
  70. #include "gcov-io.h"
  71. #include "tree-pass.h"
  72. #include "profile.h"
  73. #include "context.h"
  74. #include "pass_manager.h"
  75. #include "ipa-utils.h"
  76. #include "omp-low.h"
  77. /* True when asm nodes has been output. */
  78. bool asm_nodes_output = false;
  79. static void output_cgraph_opt_summary (void);
  80. static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
  81. /* Number of LDPR values known to GCC. */
  82. #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
  83. /* All node orders are ofsetted by ORDER_BASE. */
  84. static int order_base;
  85. /* Cgraph streaming is organized as set of record whose type
  86. is indicated by a tag. */
  87. enum LTO_symtab_tags
  88. {
  89. /* Must leave 0 for the stopper. */
  90. /* Cgraph node without body available. */
  91. LTO_symtab_unavail_node = 1,
  92. /* Cgraph node with function body. */
  93. LTO_symtab_analyzed_node,
  94. /* Cgraph edges. */
  95. LTO_symtab_edge,
  96. LTO_symtab_indirect_edge,
  97. LTO_symtab_variable,
  98. LTO_symtab_last_tag
  99. };
  100. /* Create a new symtab encoder.
  101. if FOR_INPUT, the encoder allocate only datastructures needed
  102. to read the symtab. */
  103. lto_symtab_encoder_t
  104. lto_symtab_encoder_new (bool for_input)
  105. {
  106. lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
  107. if (!for_input)
  108. encoder->map = new hash_map<symtab_node *, size_t>;
  109. encoder->nodes.create (0);
  110. return encoder;
  111. }
  112. /* Delete ENCODER and its components. */
  113. void
  114. lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
  115. {
  116. encoder->nodes.release ();
  117. if (encoder->map)
  118. delete encoder->map;
  119. free (encoder);
  120. }
  121. /* Return the existing reference number of NODE in the symtab encoder in
  122. output block OB. Assign a new reference if this is the first time
  123. NODE is encoded. */
  124. int
  125. lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
  126. symtab_node *node)
  127. {
  128. int ref;
  129. if (!encoder->map)
  130. {
  131. lto_encoder_entry entry = {node, false, false, false};
  132. ref = encoder->nodes.length ();
  133. encoder->nodes.safe_push (entry);
  134. return ref;
  135. }
  136. size_t *slot = encoder->map->get (node);
  137. if (!slot || !*slot)
  138. {
  139. lto_encoder_entry entry = {node, false, false, false};
  140. ref = encoder->nodes.length ();
  141. if (!slot)
  142. encoder->map->put (node, ref + 1);
  143. encoder->nodes.safe_push (entry);
  144. }
  145. else
  146. ref = *slot - 1;
  147. return ref;
  148. }
  149. /* Remove NODE from encoder. */
  150. bool
  151. lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
  152. symtab_node *node)
  153. {
  154. int index;
  155. lto_encoder_entry last_node;
  156. size_t *slot = encoder->map->get (node);
  157. if (slot == NULL || !*slot)
  158. return false;
  159. index = *slot - 1;
  160. gcc_checking_assert (encoder->nodes[index].node == node);
  161. /* Remove from vector. We do this by swapping node with the last element
  162. of the vector. */
  163. last_node = encoder->nodes.pop ();
  164. if (last_node.node != node)
  165. {
  166. gcc_assert (encoder->map->put (last_node.node, index + 1));
  167. /* Move the last element to the original spot of NODE. */
  168. encoder->nodes[index] = last_node;
  169. }
  170. /* Remove element from hash table. */
  171. encoder->map->remove (node);
  172. return true;
  173. }
  174. /* Return TRUE if we should encode the body of NODE (if any). */
  175. bool
  176. lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
  177. struct cgraph_node *node)
  178. {
  179. int index = lto_symtab_encoder_lookup (encoder, node);
  180. return encoder->nodes[index].body;
  181. }
  182. /* Specify that we encode the body of NODE in this partition. */
  183. static void
  184. lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
  185. struct cgraph_node *node)
  186. {
  187. int index = lto_symtab_encoder_encode (encoder, node);
  188. gcc_checking_assert (encoder->nodes[index].node == node);
  189. encoder->nodes[index].body = true;
  190. }
  191. /* Return TRUE if we should encode initializer of NODE (if any). */
  192. bool
  193. lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
  194. varpool_node *node)
  195. {
  196. int index = lto_symtab_encoder_lookup (encoder, node);
  197. if (index == LCC_NOT_FOUND)
  198. return false;
  199. return encoder->nodes[index].initializer;
  200. }
  201. /* Specify that we should encode initializer of NODE (if any). */
  202. static void
  203. lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
  204. varpool_node *node)
  205. {
  206. int index = lto_symtab_encoder_lookup (encoder, node);
  207. encoder->nodes[index].initializer = true;
  208. }
  209. /* Return TRUE if NODE is in this partition. */
  210. bool
  211. lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
  212. symtab_node *node)
  213. {
  214. int index = lto_symtab_encoder_lookup (encoder, node);
  215. if (index == LCC_NOT_FOUND)
  216. return false;
  217. return encoder->nodes[index].in_partition;
  218. }
  219. /* Specify that NODE is in this partition. */
  220. void
  221. lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
  222. symtab_node *node)
  223. {
  224. int index = lto_symtab_encoder_encode (encoder, node);
  225. encoder->nodes[index].in_partition = true;
  226. }
  227. /* Output the cgraph EDGE to OB using ENCODER. */
  228. static void
  229. lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
  230. lto_symtab_encoder_t encoder)
  231. {
  232. unsigned int uid;
  233. intptr_t ref;
  234. struct bitpack_d bp;
  235. if (edge->indirect_unknown_callee)
  236. streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
  237. LTO_symtab_indirect_edge);
  238. else
  239. streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
  240. LTO_symtab_edge);
  241. ref = lto_symtab_encoder_lookup (encoder, edge->caller);
  242. gcc_assert (ref != LCC_NOT_FOUND);
  243. streamer_write_hwi_stream (ob->main_stream, ref);
  244. if (!edge->indirect_unknown_callee)
  245. {
  246. ref = lto_symtab_encoder_lookup (encoder, edge->callee);
  247. gcc_assert (ref != LCC_NOT_FOUND);
  248. streamer_write_hwi_stream (ob->main_stream, ref);
  249. }
  250. streamer_write_gcov_count_stream (ob->main_stream, edge->count);
  251. bp = bitpack_create (ob->main_stream);
  252. uid = (!gimple_has_body_p (edge->caller->decl)
  253. ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
  254. bp_pack_enum (&bp, cgraph_inline_failed_t,
  255. CIF_N_REASONS, edge->inline_failed);
  256. bp_pack_var_len_unsigned (&bp, uid);
  257. bp_pack_var_len_unsigned (&bp, edge->frequency);
  258. bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
  259. bp_pack_value (&bp, edge->speculative, 1);
  260. bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
  261. bp_pack_value (&bp, edge->can_throw_external, 1);
  262. bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
  263. if (edge->indirect_unknown_callee)
  264. {
  265. int flags = edge->indirect_info->ecf_flags;
  266. bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
  267. bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
  268. bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
  269. bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
  270. bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
  271. bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
  272. /* Flags that should not appear on indirect calls. */
  273. gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
  274. | ECF_MAY_BE_ALLOCA
  275. | ECF_SIBCALL
  276. | ECF_LEAF
  277. | ECF_NOVOPS)));
  278. }
  279. streamer_write_bitpack (&bp);
  280. if (edge->indirect_unknown_callee)
  281. {
  282. streamer_write_hwi_stream (ob->main_stream,
  283. edge->indirect_info->common_target_id);
  284. if (edge->indirect_info->common_target_id)
  285. streamer_write_hwi_stream
  286. (ob->main_stream, edge->indirect_info->common_target_probability);
  287. }
  288. }
  289. /* Return if NODE contain references from other partitions. */
  290. bool
  291. referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
  292. {
  293. int i;
  294. struct ipa_ref *ref = NULL;
  295. for (i = 0; node->iterate_referring (i, ref); i++)
  296. {
  297. /* Ignore references from non-offloadable nodes while streaming NODE into
  298. offload LTO section. */
  299. if (!ref->referring->need_lto_streaming)
  300. continue;
  301. if (ref->referring->in_other_partition
  302. || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
  303. return true;
  304. }
  305. return false;
  306. }
  307. /* Return true when node is reachable from other partition. */
  308. bool
  309. reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
  310. {
  311. struct cgraph_edge *e;
  312. if (!node->definition)
  313. return false;
  314. if (node->global.inlined_to)
  315. return false;
  316. for (e = node->callers; e; e = e->next_caller)
  317. {
  318. /* Ignore references from non-offloadable nodes while streaming NODE into
  319. offload LTO section. */
  320. if (!e->caller->need_lto_streaming)
  321. continue;
  322. if (e->caller->in_other_partition
  323. || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
  324. return true;
  325. }
  326. return false;
  327. }
  328. /* Return if NODE contain references from other partitions. */
  329. bool
  330. referenced_from_this_partition_p (symtab_node *node,
  331. lto_symtab_encoder_t encoder)
  332. {
  333. int i;
  334. struct ipa_ref *ref = NULL;
  335. for (i = 0; node->iterate_referring (i, ref); i++)
  336. if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
  337. return true;
  338. return false;
  339. }
  340. /* Return true when node is reachable from other partition. */
  341. bool
  342. reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
  343. {
  344. struct cgraph_edge *e;
  345. for (e = node->callers; e; e = e->next_caller)
  346. if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
  347. return true;
  348. return false;
  349. }
  350. /* Output the cgraph NODE to OB. ENCODER is used to find the
  351. reference number of NODE->inlined_to. SET is the set of nodes we
  352. are writing to the current file. If NODE is not in SET, then NODE
  353. is a boundary of a cgraph_node_set and we pretend NODE just has a
  354. decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
  355. that have had their callgraph node written so far. This is used to
  356. determine if NODE is a clone of a previously written node. */
  357. static void
  358. lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
  359. lto_symtab_encoder_t encoder)
  360. {
  361. unsigned int tag;
  362. struct bitpack_d bp;
  363. bool boundary_p;
  364. intptr_t ref;
  365. bool in_other_partition = false;
  366. struct cgraph_node *clone_of, *ultimate_clone_of;
  367. ipa_opt_pass_d *pass;
  368. int i;
  369. const char *comdat;
  370. const char *section;
  371. tree group;
  372. boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
  373. if (node->analyzed && (!boundary_p || node->alias || node->thunk.thunk_p))
  374. tag = LTO_symtab_analyzed_node;
  375. else
  376. tag = LTO_symtab_unavail_node;
  377. streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
  378. tag);
  379. streamer_write_hwi_stream (ob->main_stream, node->order);
  380. /* In WPA mode, we only output part of the call-graph. Also, we
  381. fake cgraph node attributes. There are two cases that we care.
  382. Boundary nodes: There are nodes that are not part of SET but are
  383. called from within SET. We artificially make them look like
  384. externally visible nodes with no function body.
  385. Cherry-picked nodes: These are nodes we pulled from other
  386. translation units into SET during IPA-inlining. We make them as
  387. local static nodes to prevent clashes with other local statics. */
  388. if (boundary_p && node->analyzed
  389. && node->get_partitioning_class () == SYMBOL_PARTITION)
  390. {
  391. /* Inline clones can not be part of boundary.
  392. gcc_assert (!node->global.inlined_to);
  393. FIXME: At the moment they can be, when partition contains an inline
  394. clone that is clone of inline clone from outside partition. We can
  395. reshape the clone tree and make other tree to be the root, but it
  396. needs a bit extra work and will be promplty done by cgraph_remove_node
  397. after reading back. */
  398. in_other_partition = 1;
  399. }
  400. clone_of = node->clone_of;
  401. while (clone_of
  402. && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
  403. if (clone_of->prev_sibling_clone)
  404. clone_of = clone_of->prev_sibling_clone;
  405. else
  406. clone_of = clone_of->clone_of;
  407. /* See if body of the master function is output. If not, we are seeing only
  408. an declaration and we do not need to pass down clone tree. */
  409. ultimate_clone_of = clone_of;
  410. while (ultimate_clone_of && ultimate_clone_of->clone_of)
  411. ultimate_clone_of = ultimate_clone_of->clone_of;
  412. if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
  413. clone_of = NULL;
  414. if (tag == LTO_symtab_analyzed_node)
  415. gcc_assert (clone_of || !node->clone_of);
  416. if (!clone_of)
  417. streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
  418. else
  419. streamer_write_hwi_stream (ob->main_stream, ref);
  420. lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
  421. streamer_write_gcov_count_stream (ob->main_stream, node->count);
  422. streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
  423. streamer_write_hwi_stream (ob->main_stream,
  424. node->ipa_transforms_to_apply.length ());
  425. FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
  426. streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
  427. if (tag == LTO_symtab_analyzed_node)
  428. {
  429. if (node->global.inlined_to)
  430. {
  431. ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
  432. gcc_assert (ref != LCC_NOT_FOUND);
  433. }
  434. else
  435. ref = LCC_NOT_FOUND;
  436. streamer_write_hwi_stream (ob->main_stream, ref);
  437. }
  438. group = node->get_comdat_group ();
  439. if (group)
  440. comdat = IDENTIFIER_POINTER (group);
  441. else
  442. comdat = "";
  443. streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
  444. if (group)
  445. {
  446. if (node->same_comdat_group && !boundary_p)
  447. {
  448. ref = lto_symtab_encoder_lookup (encoder,
  449. node->same_comdat_group);
  450. gcc_assert (ref != LCC_NOT_FOUND);
  451. }
  452. else
  453. ref = LCC_NOT_FOUND;
  454. streamer_write_hwi_stream (ob->main_stream, ref);
  455. }
  456. section = node->get_section ();
  457. if (!section)
  458. section = "";
  459. streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
  460. bp = bitpack_create (ob->main_stream);
  461. bp_pack_value (&bp, node->local.local, 1);
  462. bp_pack_value (&bp, node->externally_visible, 1);
  463. bp_pack_value (&bp, node->no_reorder, 1);
  464. bp_pack_value (&bp, node->definition, 1);
  465. bp_pack_value (&bp, node->local.versionable, 1);
  466. bp_pack_value (&bp, node->local.can_change_signature, 1);
  467. bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
  468. bp_pack_value (&bp, node->force_output, 1);
  469. bp_pack_value (&bp, node->forced_by_abi, 1);
  470. bp_pack_value (&bp, node->unique_name, 1);
  471. bp_pack_value (&bp, node->body_removed, 1);
  472. bp_pack_value (&bp, node->implicit_section, 1);
  473. bp_pack_value (&bp, node->address_taken, 1);
  474. bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
  475. && node->get_partitioning_class () == SYMBOL_PARTITION
  476. && (reachable_from_other_partition_p (node, encoder)
  477. || referenced_from_other_partition_p (node, encoder)), 1);
  478. bp_pack_value (&bp, node->lowered, 1);
  479. bp_pack_value (&bp, in_other_partition, 1);
  480. bp_pack_value (&bp, node->alias, 1);
  481. bp_pack_value (&bp, node->weakref, 1);
  482. bp_pack_value (&bp, node->frequency, 2);
  483. bp_pack_value (&bp, node->only_called_at_startup, 1);
  484. bp_pack_value (&bp, node->only_called_at_exit, 1);
  485. bp_pack_value (&bp, node->tm_clone, 1);
  486. bp_pack_value (&bp, node->calls_comdat_local, 1);
  487. bp_pack_value (&bp, node->icf_merged, 1);
  488. bp_pack_value (&bp, node->nonfreeing_fn, 1);
  489. bp_pack_value (&bp, node->thunk.thunk_p, 1);
  490. bp_pack_value (&bp, node->parallelized_function, 1);
  491. bp_pack_enum (&bp, ld_plugin_symbol_resolution,
  492. LDPR_NUM_KNOWN, node->resolution);
  493. bp_pack_value (&bp, node->instrumentation_clone, 1);
  494. bp_pack_value (&bp, node->split_part, 1);
  495. streamer_write_bitpack (&bp);
  496. streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
  497. if (node->thunk.thunk_p)
  498. {
  499. streamer_write_uhwi_stream
  500. (ob->main_stream,
  501. 1 + (node->thunk.this_adjusting != 0) * 2
  502. + (node->thunk.virtual_offset_p != 0) * 4
  503. + (node->thunk.add_pointer_bounds_args != 0) * 8);
  504. streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
  505. streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
  506. }
  507. streamer_write_hwi_stream (ob->main_stream, node->profile_id);
  508. if (DECL_STATIC_CONSTRUCTOR (node->decl))
  509. streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
  510. if (DECL_STATIC_DESTRUCTOR (node->decl))
  511. streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
  512. if (node->instrumentation_clone)
  513. lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
  514. }
  515. /* Output the varpool NODE to OB.
  516. If NODE is not in SET, then NODE is a boundary. */
  517. static void
  518. lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
  519. lto_symtab_encoder_t encoder)
  520. {
  521. bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
  522. bool encode_initializer_p
  523. = (node->definition
  524. && lto_symtab_encoder_encode_initializer_p (encoder, node));
  525. struct bitpack_d bp;
  526. int ref;
  527. const char *comdat;
  528. const char *section;
  529. tree group;
  530. gcc_assert (!encode_initializer_p || node->definition);
  531. gcc_assert (boundary_p || encode_initializer_p);
  532. streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
  533. LTO_symtab_variable);
  534. streamer_write_hwi_stream (ob->main_stream, node->order);
  535. lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
  536. bp = bitpack_create (ob->main_stream);
  537. bp_pack_value (&bp, node->externally_visible, 1);
  538. bp_pack_value (&bp, node->no_reorder, 1);
  539. bp_pack_value (&bp, node->force_output, 1);
  540. bp_pack_value (&bp, node->forced_by_abi, 1);
  541. bp_pack_value (&bp, node->unique_name, 1);
  542. bp_pack_value (&bp,
  543. node->body_removed
  544. || (!encode_initializer_p && !node->alias && node->definition),
  545. 1);
  546. bp_pack_value (&bp, node->implicit_section, 1);
  547. bp_pack_value (&bp, node->writeonly, 1);
  548. bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
  549. 1);
  550. bp_pack_value (&bp, node->alias, 1);
  551. bp_pack_value (&bp, node->weakref, 1);
  552. bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
  553. gcc_assert (node->definition || !node->analyzed);
  554. /* Constant pool initializers can be de-unified into individual ltrans units.
  555. FIXME: Alternatively at -Os we may want to avoid generating for them the local
  556. labels and share them across LTRANS partitions. */
  557. if (node->get_partitioning_class () != SYMBOL_PARTITION)
  558. {
  559. bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
  560. bp_pack_value (&bp, 0, 1); /* in_other_partition. */
  561. }
  562. else
  563. {
  564. bp_pack_value (&bp, node->definition
  565. && referenced_from_other_partition_p (node, encoder), 1);
  566. bp_pack_value (&bp, node->analyzed
  567. && boundary_p && !DECL_EXTERNAL (node->decl), 1);
  568. /* in_other_partition. */
  569. }
  570. bp_pack_value (&bp, node->tls_model, 3);
  571. bp_pack_value (&bp, node->used_by_single_function, 1);
  572. bp_pack_value (&bp, node->need_bounds_init, 1);
  573. streamer_write_bitpack (&bp);
  574. group = node->get_comdat_group ();
  575. if (group)
  576. comdat = IDENTIFIER_POINTER (group);
  577. else
  578. comdat = "";
  579. streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
  580. if (group)
  581. {
  582. if (node->same_comdat_group && !boundary_p)
  583. {
  584. ref = lto_symtab_encoder_lookup (encoder,
  585. node->same_comdat_group);
  586. gcc_assert (ref != LCC_NOT_FOUND);
  587. }
  588. else
  589. ref = LCC_NOT_FOUND;
  590. streamer_write_hwi_stream (ob->main_stream, ref);
  591. }
  592. section = node->get_section ();
  593. if (!section)
  594. section = "";
  595. streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
  596. streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
  597. LDPR_NUM_KNOWN, node->resolution);
  598. }
  599. /* Output the varpool NODE to OB.
  600. If NODE is not in SET, then NODE is a boundary. */
  601. static void
  602. lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
  603. lto_symtab_encoder_t encoder)
  604. {
  605. struct bitpack_d bp;
  606. int nref;
  607. int uid = ref->lto_stmt_uid;
  608. struct cgraph_node *node;
  609. bp = bitpack_create (ob->main_stream);
  610. bp_pack_value (&bp, ref->use, 3);
  611. bp_pack_value (&bp, ref->speculative, 1);
  612. streamer_write_bitpack (&bp);
  613. nref = lto_symtab_encoder_lookup (encoder, ref->referred);
  614. gcc_assert (nref != LCC_NOT_FOUND);
  615. streamer_write_hwi_stream (ob->main_stream, nref);
  616. node = dyn_cast <cgraph_node *> (ref->referring);
  617. if (node)
  618. {
  619. if (ref->stmt)
  620. uid = gimple_uid (ref->stmt) + 1;
  621. streamer_write_hwi_stream (ob->main_stream, uid);
  622. }
  623. }
  624. /* Stream out profile_summary to OB. */
  625. static void
  626. output_profile_summary (struct lto_simple_output_block *ob)
  627. {
  628. unsigned h_ix;
  629. struct bitpack_d bp;
  630. if (profile_info)
  631. {
  632. /* We do not output num and run_max, they are not used by
  633. GCC profile feedback and they are difficult to merge from multiple
  634. units. */
  635. gcc_assert (profile_info->runs);
  636. streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
  637. streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
  638. /* sum_all is needed for computing the working set with the
  639. histogram. */
  640. streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
  641. /* Create and output a bitpack of non-zero histogram entries indices. */
  642. bp = bitpack_create (ob->main_stream);
  643. for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
  644. bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
  645. streamer_write_bitpack (&bp);
  646. /* Now stream out only those non-zero entries. */
  647. for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
  648. {
  649. if (!profile_info->histogram[h_ix].num_counters)
  650. continue;
  651. streamer_write_gcov_count_stream (ob->main_stream,
  652. profile_info->histogram[h_ix].num_counters);
  653. streamer_write_gcov_count_stream (ob->main_stream,
  654. profile_info->histogram[h_ix].min_value);
  655. streamer_write_gcov_count_stream (ob->main_stream,
  656. profile_info->histogram[h_ix].cum_value);
  657. }
  658. /* IPA-profile computes hot bb threshold based on cumulated
  659. whole program profile. We need to stream it down to ltrans. */
  660. if (flag_wpa)
  661. streamer_write_gcov_count_stream (ob->main_stream,
  662. get_hot_bb_threshold ());
  663. }
  664. else
  665. streamer_write_uhwi_stream (ob->main_stream, 0);
  666. }
  667. /* Output all callees or indirect outgoing edges. EDGE must be the first such
  668. edge. */
  669. static void
  670. output_outgoing_cgraph_edges (struct cgraph_edge *edge,
  671. struct lto_simple_output_block *ob,
  672. lto_symtab_encoder_t encoder)
  673. {
  674. if (!edge)
  675. return;
  676. /* Output edges in backward direction, so the reconstructed callgraph match
  677. and it is easy to associate call sites in the IPA pass summaries. */
  678. while (edge->next_callee)
  679. edge = edge->next_callee;
  680. for (; edge; edge = edge->prev_callee)
  681. lto_output_edge (ob, edge, encoder);
  682. }
  683. /* Output the part of the cgraph in SET. */
  684. static void
  685. output_refs (lto_symtab_encoder_t encoder)
  686. {
  687. struct lto_simple_output_block *ob;
  688. int count;
  689. struct ipa_ref *ref;
  690. ob = lto_create_simple_output_block (LTO_section_refs);
  691. for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
  692. {
  693. symtab_node *node = lto_symtab_encoder_deref (encoder, i);
  694. /* IPA_REF_ALIAS and IPA_REF_CHKP references are always preserved
  695. in the boundary. Alias node can't have other references and
  696. can be always handled as if it's not in the boundary. */
  697. if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
  698. {
  699. cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
  700. /* Output IPA_REF_CHKP reference. */
  701. if (cnode
  702. && cnode->instrumented_version
  703. && !cnode->instrumentation_clone)
  704. {
  705. for (int i = 0; node->iterate_reference (i, ref); i++)
  706. if (ref->use == IPA_REF_CHKP)
  707. {
  708. if (lto_symtab_encoder_lookup (encoder, ref->referred)
  709. != LCC_NOT_FOUND)
  710. {
  711. int nref = lto_symtab_encoder_lookup (encoder, node);
  712. streamer_write_gcov_count_stream (ob->main_stream, 1);
  713. streamer_write_uhwi_stream (ob->main_stream, nref);
  714. lto_output_ref (ob, ref, encoder);
  715. }
  716. break;
  717. }
  718. }
  719. continue;
  720. }
  721. count = node->ref_list.nreferences ();
  722. if (count)
  723. {
  724. streamer_write_gcov_count_stream (ob->main_stream, count);
  725. streamer_write_uhwi_stream (ob->main_stream,
  726. lto_symtab_encoder_lookup (encoder, node));
  727. for (int i = 0; node->iterate_reference (i, ref); i++)
  728. lto_output_ref (ob, ref, encoder);
  729. }
  730. }
  731. streamer_write_uhwi_stream (ob->main_stream, 0);
  732. lto_destroy_simple_output_block (ob);
  733. }
  734. /* Add NODE into encoder as well as nodes it is cloned from.
  735. Do it in a way so clones appear first. */
  736. static void
  737. add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
  738. bool include_body)
  739. {
  740. if (node->clone_of)
  741. add_node_to (encoder, node->clone_of, include_body);
  742. else if (include_body)
  743. lto_set_symtab_encoder_encode_body (encoder, node);
  744. lto_symtab_encoder_encode (encoder, node);
  745. }
  746. /* Add all references in NODE to encoders. */
  747. static void
  748. create_references (lto_symtab_encoder_t encoder, symtab_node *node)
  749. {
  750. int i;
  751. struct ipa_ref *ref = NULL;
  752. for (i = 0; node->iterate_reference (i, ref); i++)
  753. if (is_a <cgraph_node *> (ref->referred))
  754. add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
  755. else
  756. lto_symtab_encoder_encode (encoder, ref->referred);
  757. }
  758. /* Select what needs to be streamed out. In regular lto mode stream everything.
  759. In offload lto mode stream only nodes marked as offloadable. */
  760. void
  761. select_what_to_stream (void)
  762. {
  763. struct symtab_node *snode;
  764. FOR_EACH_SYMBOL (snode)
  765. snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
  766. }
  767. /* Find all symbols we want to stream into given partition and insert them
  768. to encoders.
  769. The function actually replaces IN_ENCODER by new one. The reason is that
  770. streaming code needs clone's origin to be streamed before clone. This
  771. means that we need to insert the nodes in specific order. This order is
  772. ignored by the partitioning logic earlier. */
  773. lto_symtab_encoder_t
  774. compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
  775. {
  776. struct cgraph_edge *edge;
  777. int i;
  778. lto_symtab_encoder_t encoder;
  779. lto_symtab_encoder_iterator lsei;
  780. hash_set<void *> reachable_call_targets;
  781. encoder = lto_symtab_encoder_new (false);
  782. /* Go over all entries in the IN_ENCODER and duplicate them to
  783. ENCODER. At the same time insert masters of clones so
  784. every master appears before clone. */
  785. for (lsei = lsei_start_function_in_partition (in_encoder);
  786. !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
  787. {
  788. struct cgraph_node *node = lsei_cgraph_node (lsei);
  789. if (!node->need_lto_streaming)
  790. continue;
  791. add_node_to (encoder, node, true);
  792. lto_set_symtab_encoder_in_partition (encoder, node);
  793. create_references (encoder, node);
  794. /* For proper debug info, we need to ship the origins, too. */
  795. if (DECL_ABSTRACT_ORIGIN (node->decl))
  796. {
  797. struct cgraph_node *origin_node
  798. = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
  799. origin_node->used_as_abstract_origin = true;
  800. add_node_to (encoder, origin_node, true);
  801. }
  802. }
  803. for (lsei = lsei_start_variable_in_partition (in_encoder);
  804. !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
  805. {
  806. varpool_node *vnode = lsei_varpool_node (lsei);
  807. if (!vnode->need_lto_streaming)
  808. continue;
  809. lto_set_symtab_encoder_in_partition (encoder, vnode);
  810. lto_set_symtab_encoder_encode_initializer (encoder, vnode);
  811. create_references (encoder, vnode);
  812. /* For proper debug info, we need to ship the origins, too. */
  813. if (DECL_ABSTRACT_ORIGIN (vnode->decl))
  814. {
  815. varpool_node *origin_node
  816. = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
  817. lto_set_symtab_encoder_in_partition (encoder, origin_node);
  818. }
  819. }
  820. /* Pickle in also the initializer of all referenced readonly variables
  821. to help folding. Constant pool variables are not shared, so we must
  822. pickle those too. */
  823. for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
  824. {
  825. symtab_node *node = lto_symtab_encoder_deref (encoder, i);
  826. if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
  827. {
  828. if (!lto_symtab_encoder_encode_initializer_p (encoder,
  829. vnode)
  830. && (((vnode->ctor_useable_for_folding_p ()
  831. && (!DECL_VIRTUAL_P (vnode->decl)
  832. || !flag_wpa
  833. || flag_ltrans_devirtualize))
  834. || POINTER_BOUNDS_P (vnode->decl))))
  835. {
  836. lto_set_symtab_encoder_encode_initializer (encoder, vnode);
  837. create_references (encoder, vnode);
  838. }
  839. }
  840. }
  841. /* Go over all the nodes again to include callees that are not in
  842. SET. */
  843. for (lsei = lsei_start_function_in_partition (encoder);
  844. !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
  845. {
  846. struct cgraph_node *node = lsei_cgraph_node (lsei);
  847. for (edge = node->callees; edge; edge = edge->next_callee)
  848. {
  849. struct cgraph_node *callee = edge->callee;
  850. if (!lto_symtab_encoder_in_partition_p (encoder, callee))
  851. {
  852. /* We should have moved all the inlines. */
  853. gcc_assert (!callee->global.inlined_to);
  854. add_node_to (encoder, callee, false);
  855. }
  856. }
  857. /* Add all possible targets for late devirtualization. */
  858. if (flag_ltrans_devirtualize || !flag_wpa)
  859. for (edge = node->indirect_calls; edge; edge = edge->next_callee)
  860. if (edge->indirect_info->polymorphic)
  861. {
  862. unsigned int i;
  863. void *cache_token;
  864. bool final;
  865. vec <cgraph_node *>targets
  866. = possible_polymorphic_call_targets
  867. (edge, &final, &cache_token);
  868. if (!reachable_call_targets.add (cache_token))
  869. {
  870. for (i = 0; i < targets.length (); i++)
  871. {
  872. struct cgraph_node *callee = targets[i];
  873. /* Adding an external declarations into the unit serves
  874. no purpose and just increases its boundary. */
  875. if (callee->definition
  876. && !lto_symtab_encoder_in_partition_p
  877. (encoder, callee))
  878. {
  879. gcc_assert (!callee->global.inlined_to);
  880. add_node_to (encoder, callee, false);
  881. }
  882. }
  883. }
  884. }
  885. }
  886. /* Be sure to also insert alias targert and thunk callees. These needs
  887. to stay to aid local calling conventions. */
  888. for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
  889. {
  890. symtab_node *node = lto_symtab_encoder_deref (encoder, i);
  891. cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
  892. if (node->alias && node->analyzed)
  893. create_references (encoder, node);
  894. if (cnode
  895. && cnode->thunk.thunk_p)
  896. add_node_to (encoder, cnode->callees->callee, false);
  897. }
  898. lto_symtab_encoder_delete (in_encoder);
  899. return encoder;
  900. }
  901. /* Output the part of the symtab in SET and VSET. */
  902. void
  903. output_symtab (void)
  904. {
  905. struct cgraph_node *node;
  906. struct lto_simple_output_block *ob;
  907. int i, n_nodes;
  908. lto_symtab_encoder_t encoder;
  909. if (flag_wpa)
  910. output_cgraph_opt_summary ();
  911. ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
  912. output_profile_summary (ob);
  913. /* An encoder for cgraph nodes should have been created by
  914. ipa_write_summaries_1. */
  915. gcc_assert (ob->decl_state->symtab_node_encoder);
  916. encoder = ob->decl_state->symtab_node_encoder;
  917. /* Write out the nodes. We must first output a node and then its clones,
  918. otherwise at a time reading back the node there would be nothing to clone
  919. from. */
  920. n_nodes = lto_symtab_encoder_size (encoder);
  921. for (i = 0; i < n_nodes; i++)
  922. {
  923. symtab_node *node = lto_symtab_encoder_deref (encoder, i);
  924. if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
  925. lto_output_node (ob, cnode, encoder);
  926. else
  927. lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
  928. }
  929. /* Go over the nodes in SET again to write edges. */
  930. for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
  931. {
  932. node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
  933. if (node
  934. && (node->thunk.thunk_p
  935. || lto_symtab_encoder_in_partition_p (encoder, node)))
  936. {
  937. output_outgoing_cgraph_edges (node->callees, ob, encoder);
  938. output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
  939. }
  940. }
  941. streamer_write_uhwi_stream (ob->main_stream, 0);
  942. lto_destroy_simple_output_block (ob);
  943. /* Emit toplevel asms.
  944. When doing WPA we must output every asm just once. Since we do not partition asm
  945. nodes at all, output them to first output. This is kind of hack, but should work
  946. well. */
  947. if (!asm_nodes_output)
  948. {
  949. asm_nodes_output = true;
  950. lto_output_toplevel_asms ();
  951. }
  952. output_refs (encoder);
  953. }
  954. /* Return identifier encoded in IB as a plain string. */
  955. static tree
  956. read_identifier (struct lto_input_block *ib)
  957. {
  958. unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
  959. tree id;
  960. if (ib->data[ib->p + len])
  961. lto_section_overrun (ib);
  962. if (!len)
  963. {
  964. ib->p++;
  965. return NULL;
  966. }
  967. id = get_identifier (ib->data + ib->p);
  968. ib->p += len + 1;
  969. return id;
  970. }
  971. /* Return string encoded in IB, NULL if string is empty. */
  972. static const char *
  973. read_string (struct lto_input_block *ib)
  974. {
  975. unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
  976. const char *str;
  977. if (ib->data[ib->p + len])
  978. lto_section_overrun (ib);
  979. if (!len)
  980. {
  981. ib->p++;
  982. return NULL;
  983. }
  984. str = ib->data + ib->p;
  985. ib->p += len + 1;
  986. return str;
  987. }
  988. /* Output function/variable tables that will allow libgomp to look up offload
  989. target code.
  990. OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
  991. varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
  992. both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
  993. void
  994. output_offload_tables (void)
  995. {
  996. if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
  997. return;
  998. struct lto_simple_output_block *ob
  999. = lto_create_simple_output_block (LTO_section_offload_table);
  1000. for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
  1001. {
  1002. streamer_write_enum (ob->main_stream, LTO_symtab_tags,
  1003. LTO_symtab_last_tag, LTO_symtab_unavail_node);
  1004. lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
  1005. (*offload_funcs)[i]);
  1006. }
  1007. for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
  1008. {
  1009. streamer_write_enum (ob->main_stream, LTO_symtab_tags,
  1010. LTO_symtab_last_tag, LTO_symtab_variable);
  1011. lto_output_var_decl_index (ob->decl_state, ob->main_stream,
  1012. (*offload_vars)[i]);
  1013. }
  1014. streamer_write_uhwi_stream (ob->main_stream, 0);
  1015. lto_destroy_simple_output_block (ob);
  1016. /* In WHOPR mode during the WPA stage the joint offload tables need to be
  1017. streamed to one partition only. That's why we free offload_funcs and
  1018. offload_vars after the first call of output_offload_tables. */
  1019. if (flag_wpa)
  1020. {
  1021. vec_free (offload_funcs);
  1022. vec_free (offload_vars);
  1023. }
  1024. }
  1025. /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
  1026. STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
  1027. NODE or to replace the values in it, for instance because the first
  1028. time we saw it, the function body was not available but now it
  1029. is. BP is a bitpack with all the bitflags for NODE read from the
  1030. stream. */
  1031. static void
  1032. input_overwrite_node (struct lto_file_decl_data *file_data,
  1033. struct cgraph_node *node,
  1034. enum LTO_symtab_tags tag,
  1035. struct bitpack_d *bp)
  1036. {
  1037. node->aux = (void *) tag;
  1038. node->lto_file_data = file_data;
  1039. node->local.local = bp_unpack_value (bp, 1);
  1040. node->externally_visible = bp_unpack_value (bp, 1);
  1041. node->no_reorder = bp_unpack_value (bp, 1);
  1042. node->definition = bp_unpack_value (bp, 1);
  1043. node->local.versionable = bp_unpack_value (bp, 1);
  1044. node->local.can_change_signature = bp_unpack_value (bp, 1);
  1045. node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
  1046. node->force_output = bp_unpack_value (bp, 1);
  1047. node->forced_by_abi = bp_unpack_value (bp, 1);
  1048. node->unique_name = bp_unpack_value (bp, 1);
  1049. node->body_removed = bp_unpack_value (bp, 1);
  1050. node->implicit_section = bp_unpack_value (bp, 1);
  1051. node->address_taken = bp_unpack_value (bp, 1);
  1052. node->used_from_other_partition = bp_unpack_value (bp, 1);
  1053. node->lowered = bp_unpack_value (bp, 1);
  1054. node->analyzed = tag == LTO_symtab_analyzed_node;
  1055. node->in_other_partition = bp_unpack_value (bp, 1);
  1056. if (node->in_other_partition
  1057. /* Avoid updating decl when we are seeing just inline clone.
  1058. When inlining function that has functions already inlined into it,
  1059. we produce clones of inline clones.
  1060. WPA partitioning might put each clone into different unit and
  1061. we might end up streaming inline clone from other partition
  1062. to support clone we are interested in. */
  1063. && (!node->clone_of
  1064. || node->clone_of->decl != node->decl))
  1065. {
  1066. DECL_EXTERNAL (node->decl) = 1;
  1067. TREE_STATIC (node->decl) = 0;
  1068. }
  1069. node->alias = bp_unpack_value (bp, 1);
  1070. node->weakref = bp_unpack_value (bp, 1);
  1071. node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
  1072. node->only_called_at_startup = bp_unpack_value (bp, 1);
  1073. node->only_called_at_exit = bp_unpack_value (bp, 1);
  1074. node->tm_clone = bp_unpack_value (bp, 1);
  1075. node->calls_comdat_local = bp_unpack_value (bp, 1);
  1076. node->icf_merged = bp_unpack_value (bp, 1);
  1077. node->nonfreeing_fn = bp_unpack_value (bp, 1);
  1078. node->thunk.thunk_p = bp_unpack_value (bp, 1);
  1079. node->parallelized_function = bp_unpack_value (bp, 1);
  1080. node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
  1081. LDPR_NUM_KNOWN);
  1082. node->instrumentation_clone = bp_unpack_value (bp, 1);
  1083. node->split_part = bp_unpack_value (bp, 1);
  1084. gcc_assert (flag_ltrans
  1085. || (!node->in_other_partition
  1086. && !node->used_from_other_partition));
  1087. }
  1088. /* Return string alias is alias of. */
  1089. static tree
  1090. get_alias_symbol (tree decl)
  1091. {
  1092. tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
  1093. return get_identifier (TREE_STRING_POINTER
  1094. (TREE_VALUE (TREE_VALUE (alias))));
  1095. }
  1096. /* Read a node from input_block IB. TAG is the node's tag just read.
  1097. Return the node read or overwriten. */
  1098. static struct cgraph_node *
  1099. input_node (struct lto_file_decl_data *file_data,
  1100. struct lto_input_block *ib,
  1101. enum LTO_symtab_tags tag,
  1102. vec<symtab_node *> nodes)
  1103. {
  1104. gcc::pass_manager *passes = g->get_passes ();
  1105. tree fn_decl;
  1106. struct cgraph_node *node;
  1107. struct bitpack_d bp;
  1108. unsigned decl_index;
  1109. int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
  1110. int clone_ref;
  1111. int order;
  1112. int i, count;
  1113. tree group;
  1114. const char *section;
  1115. order = streamer_read_hwi (ib) + order_base;
  1116. clone_ref = streamer_read_hwi (ib);
  1117. decl_index = streamer_read_uhwi (ib);
  1118. fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
  1119. if (clone_ref != LCC_NOT_FOUND)
  1120. {
  1121. node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
  1122. 0, CGRAPH_FREQ_BASE, false,
  1123. vNULL, false, NULL, NULL);
  1124. }
  1125. else
  1126. {
  1127. /* Declaration of functions can be already merged with a declaration
  1128. from other input file. We keep cgraph unmerged until after streaming
  1129. of ipa passes is done. Alays forcingly create a fresh node. */
  1130. node = symtab->create_empty ();
  1131. node->decl = fn_decl;
  1132. node->register_symbol ();
  1133. }
  1134. node->order = order;
  1135. if (order >= symtab->order)
  1136. symtab->order = order + 1;
  1137. node->count = streamer_read_gcov_count (ib);
  1138. node->count_materialization_scale = streamer_read_hwi (ib);
  1139. count = streamer_read_hwi (ib);
  1140. node->ipa_transforms_to_apply = vNULL;
  1141. for (i = 0; i < count; i++)
  1142. {
  1143. opt_pass *pass;
  1144. int pid = streamer_read_hwi (ib);
  1145. gcc_assert (pid < passes->passes_by_id_size);
  1146. pass = passes->passes_by_id[pid];
  1147. node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
  1148. }
  1149. if (tag == LTO_symtab_analyzed_node)
  1150. ref = streamer_read_hwi (ib);
  1151. group = read_identifier (ib);
  1152. if (group)
  1153. ref2 = streamer_read_hwi (ib);
  1154. /* Make sure that we have not read this node before. Nodes that
  1155. have already been read will have their tag stored in the 'aux'
  1156. field. Since built-in functions can be referenced in multiple
  1157. functions, they are expected to be read more than once. */
  1158. if (node->aux && !DECL_BUILT_IN (node->decl))
  1159. internal_error ("bytecode stream: found multiple instances of cgraph "
  1160. "node with uid %d", node->uid);
  1161. node->tp_first_run = streamer_read_uhwi (ib);
  1162. bp = streamer_read_bitpack (ib);
  1163. input_overwrite_node (file_data, node, tag, &bp);
  1164. /* Store a reference for now, and fix up later to be a pointer. */
  1165. node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
  1166. if (group)
  1167. {
  1168. node->set_comdat_group (group);
  1169. /* Store a reference for now, and fix up later to be a pointer. */
  1170. node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
  1171. }
  1172. else
  1173. node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
  1174. section = read_string (ib);
  1175. if (section)
  1176. node->set_section_for_node (section);
  1177. if (node->thunk.thunk_p)
  1178. {
  1179. int type = streamer_read_uhwi (ib);
  1180. HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
  1181. HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
  1182. node->thunk.fixed_offset = fixed_offset;
  1183. node->thunk.this_adjusting = (type & 2);
  1184. node->thunk.virtual_value = virtual_value;
  1185. node->thunk.virtual_offset_p = (type & 4);
  1186. node->thunk.add_pointer_bounds_args = (type & 8);
  1187. }
  1188. if (node->alias && !node->analyzed && node->weakref)
  1189. node->alias_target = get_alias_symbol (node->decl);
  1190. node->profile_id = streamer_read_hwi (ib);
  1191. if (DECL_STATIC_CONSTRUCTOR (node->decl))
  1192. node->set_init_priority (streamer_read_hwi (ib));
  1193. if (DECL_STATIC_DESTRUCTOR (node->decl))
  1194. node->set_fini_priority (streamer_read_hwi (ib));
  1195. if (node->instrumentation_clone)
  1196. {
  1197. decl_index = streamer_read_uhwi (ib);
  1198. fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
  1199. node->orig_decl = fn_decl;
  1200. }
  1201. return node;
  1202. }
  1203. /* Read a node from input_block IB. TAG is the node's tag just read.
  1204. Return the node read or overwriten. */
  1205. static varpool_node *
  1206. input_varpool_node (struct lto_file_decl_data *file_data,
  1207. struct lto_input_block *ib)
  1208. {
  1209. int decl_index;
  1210. tree var_decl;
  1211. varpool_node *node;
  1212. struct bitpack_d bp;
  1213. int ref = LCC_NOT_FOUND;
  1214. int order;
  1215. tree group;
  1216. const char *section;
  1217. order = streamer_read_hwi (ib) + order_base;
  1218. decl_index = streamer_read_uhwi (ib);
  1219. var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
  1220. /* Declaration of functions can be already merged with a declaration
  1221. from other input file. We keep cgraph unmerged until after streaming
  1222. of ipa passes is done. Alays forcingly create a fresh node. */
  1223. node = varpool_node::create_empty ();
  1224. node->decl = var_decl;
  1225. node->register_symbol ();
  1226. node->order = order;
  1227. if (order >= symtab->order)
  1228. symtab->order = order + 1;
  1229. node->lto_file_data = file_data;
  1230. bp = streamer_read_bitpack (ib);
  1231. node->externally_visible = bp_unpack_value (&bp, 1);
  1232. node->no_reorder = bp_unpack_value (&bp, 1);
  1233. node->force_output = bp_unpack_value (&bp, 1);
  1234. node->forced_by_abi = bp_unpack_value (&bp, 1);
  1235. node->unique_name = bp_unpack_value (&bp, 1);
  1236. node->body_removed = bp_unpack_value (&bp, 1);
  1237. node->implicit_section = bp_unpack_value (&bp, 1);
  1238. node->writeonly = bp_unpack_value (&bp, 1);
  1239. node->definition = bp_unpack_value (&bp, 1);
  1240. node->alias = bp_unpack_value (&bp, 1);
  1241. node->weakref = bp_unpack_value (&bp, 1);
  1242. node->analyzed = bp_unpack_value (&bp, 1);
  1243. node->used_from_other_partition = bp_unpack_value (&bp, 1);
  1244. node->in_other_partition = bp_unpack_value (&bp, 1);
  1245. if (node->in_other_partition)
  1246. {
  1247. DECL_EXTERNAL (node->decl) = 1;
  1248. TREE_STATIC (node->decl) = 0;
  1249. }
  1250. if (node->alias && !node->analyzed && node->weakref)
  1251. node->alias_target = get_alias_symbol (node->decl);
  1252. node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
  1253. node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
  1254. node->need_bounds_init = bp_unpack_value (&bp, 1);
  1255. group = read_identifier (ib);
  1256. if (group)
  1257. {
  1258. node->set_comdat_group (group);
  1259. ref = streamer_read_hwi (ib);
  1260. /* Store a reference for now, and fix up later to be a pointer. */
  1261. node->same_comdat_group = (symtab_node *) (intptr_t) ref;
  1262. }
  1263. else
  1264. node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
  1265. section = read_string (ib);
  1266. if (section)
  1267. node->set_section_for_node (section);
  1268. node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
  1269. LDPR_NUM_KNOWN);
  1270. gcc_assert (flag_ltrans
  1271. || (!node->in_other_partition
  1272. && !node->used_from_other_partition));
  1273. return node;
  1274. }
  1275. /* Read a node from input_block IB. TAG is the node's tag just read.
  1276. Return the node read or overwriten. */
  1277. static void
  1278. input_ref (struct lto_input_block *ib,
  1279. symtab_node *referring_node,
  1280. vec<symtab_node *> nodes)
  1281. {
  1282. symtab_node *node = NULL;
  1283. struct bitpack_d bp;
  1284. enum ipa_ref_use use;
  1285. bool speculative;
  1286. struct ipa_ref *ref;
  1287. bp = streamer_read_bitpack (ib);
  1288. use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
  1289. speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
  1290. node = nodes[streamer_read_hwi (ib)];
  1291. ref = referring_node->create_reference (node, use);
  1292. ref->speculative = speculative;
  1293. if (is_a <cgraph_node *> (referring_node))
  1294. ref->lto_stmt_uid = streamer_read_hwi (ib);
  1295. }
  1296. /* Read an edge from IB. NODES points to a vector of previously read nodes for
  1297. decoding caller and callee of the edge to be read. If INDIRECT is true, the
  1298. edge being read is indirect (in the sense that it has
  1299. indirect_unknown_callee set). */
  1300. static void
  1301. input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
  1302. bool indirect)
  1303. {
  1304. struct cgraph_node *caller, *callee;
  1305. struct cgraph_edge *edge;
  1306. unsigned int stmt_id;
  1307. gcov_type count;
  1308. int freq;
  1309. cgraph_inline_failed_t inline_failed;
  1310. struct bitpack_d bp;
  1311. int ecf_flags = 0;
  1312. caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
  1313. if (caller == NULL || caller->decl == NULL_TREE)
  1314. internal_error ("bytecode stream: no caller found while reading edge");
  1315. if (!indirect)
  1316. {
  1317. callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
  1318. if (callee == NULL || callee->decl == NULL_TREE)
  1319. internal_error ("bytecode stream: no callee found while reading edge");
  1320. }
  1321. else
  1322. callee = NULL;
  1323. count = streamer_read_gcov_count (ib);
  1324. bp = streamer_read_bitpack (ib);
  1325. inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
  1326. stmt_id = bp_unpack_var_len_unsigned (&bp);
  1327. freq = (int) bp_unpack_var_len_unsigned (&bp);
  1328. if (indirect)
  1329. edge = caller->create_indirect_edge (NULL, 0, count, freq);
  1330. else
  1331. edge = caller->create_edge (callee, NULL, count, freq);
  1332. edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
  1333. edge->speculative = bp_unpack_value (&bp, 1);
  1334. edge->lto_stmt_uid = stmt_id;
  1335. edge->inline_failed = inline_failed;
  1336. edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
  1337. edge->can_throw_external = bp_unpack_value (&bp, 1);
  1338. edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
  1339. if (indirect)
  1340. {
  1341. if (bp_unpack_value (&bp, 1))
  1342. ecf_flags |= ECF_CONST;
  1343. if (bp_unpack_value (&bp, 1))
  1344. ecf_flags |= ECF_PURE;
  1345. if (bp_unpack_value (&bp, 1))
  1346. ecf_flags |= ECF_NORETURN;
  1347. if (bp_unpack_value (&bp, 1))
  1348. ecf_flags |= ECF_MALLOC;
  1349. if (bp_unpack_value (&bp, 1))
  1350. ecf_flags |= ECF_NOTHROW;
  1351. if (bp_unpack_value (&bp, 1))
  1352. ecf_flags |= ECF_RETURNS_TWICE;
  1353. edge->indirect_info->ecf_flags = ecf_flags;
  1354. edge->indirect_info->common_target_id = streamer_read_hwi (ib);
  1355. if (edge->indirect_info->common_target_id)
  1356. edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
  1357. }
  1358. }
  1359. /* Read a cgraph from IB using the info in FILE_DATA. */
  1360. static vec<symtab_node *>
  1361. input_cgraph_1 (struct lto_file_decl_data *file_data,
  1362. struct lto_input_block *ib)
  1363. {
  1364. enum LTO_symtab_tags tag;
  1365. vec<symtab_node *> nodes = vNULL;
  1366. symtab_node *node;
  1367. unsigned i;
  1368. tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
  1369. order_base = symtab->order;
  1370. while (tag)
  1371. {
  1372. if (tag == LTO_symtab_edge)
  1373. input_edge (ib, nodes, false);
  1374. else if (tag == LTO_symtab_indirect_edge)
  1375. input_edge (ib, nodes, true);
  1376. else if (tag == LTO_symtab_variable)
  1377. {
  1378. node = input_varpool_node (file_data, ib);
  1379. nodes.safe_push (node);
  1380. lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
  1381. }
  1382. else
  1383. {
  1384. node = input_node (file_data, ib, tag, nodes);
  1385. if (node == NULL || node->decl == NULL_TREE)
  1386. internal_error ("bytecode stream: found empty cgraph node");
  1387. nodes.safe_push (node);
  1388. lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
  1389. }
  1390. tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
  1391. }
  1392. lto_input_toplevel_asms (file_data, order_base);
  1393. /* AUX pointers should be all non-zero for function nodes read from the stream. */
  1394. #ifdef ENABLE_CHECKING
  1395. FOR_EACH_VEC_ELT (nodes, i, node)
  1396. gcc_assert (node->aux || !is_a <cgraph_node *> (node));
  1397. #endif
  1398. FOR_EACH_VEC_ELT (nodes, i, node)
  1399. {
  1400. int ref;
  1401. if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
  1402. {
  1403. ref = (int) (intptr_t) cnode->global.inlined_to;
  1404. /* We share declaration of builtins, so we may read same node twice. */
  1405. if (!node->aux)
  1406. continue;
  1407. node->aux = NULL;
  1408. /* Fixup inlined_to from reference to pointer. */
  1409. if (ref != LCC_NOT_FOUND)
  1410. dyn_cast<cgraph_node *> (node)->global.inlined_to
  1411. = dyn_cast<cgraph_node *> (nodes[ref]);
  1412. else
  1413. cnode->global.inlined_to = NULL;
  1414. /* Compute instrumented_version. */
  1415. if (cnode->instrumentation_clone)
  1416. {
  1417. gcc_assert (cnode->orig_decl);
  1418. cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
  1419. if (cnode->instrumented_version)
  1420. {
  1421. /* We may have multiple nodes for a single function which
  1422. will be merged later. To have a proper merge we need
  1423. to keep instrumentation_version reference between nodes
  1424. consistent: each instrumented_version reference should
  1425. have proper reverse reference. Thus don't break existing
  1426. instrumented_version reference if it already exists. */
  1427. if (cnode->instrumented_version->instrumented_version)
  1428. cnode->instrumented_version = NULL;
  1429. else
  1430. cnode->instrumented_version->instrumented_version = cnode;
  1431. }
  1432. /* Restore decl names reference. */
  1433. IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (cnode->decl)) = 1;
  1434. TREE_CHAIN (DECL_ASSEMBLER_NAME (cnode->decl))
  1435. = DECL_ASSEMBLER_NAME (cnode->orig_decl);
  1436. }
  1437. }
  1438. ref = (int) (intptr_t) node->same_comdat_group;
  1439. /* Fixup same_comdat_group from reference to pointer. */
  1440. if (ref != LCC_NOT_FOUND)
  1441. node->same_comdat_group = nodes[ref];
  1442. else
  1443. node->same_comdat_group = NULL;
  1444. }
  1445. FOR_EACH_VEC_ELT (nodes, i, node)
  1446. node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
  1447. return nodes;
  1448. }
  1449. /* Input ipa_refs. */
  1450. static void
  1451. input_refs (struct lto_input_block *ib,
  1452. vec<symtab_node *> nodes)
  1453. {
  1454. int count;
  1455. int idx;
  1456. while (true)
  1457. {
  1458. symtab_node *node;
  1459. count = streamer_read_uhwi (ib);
  1460. if (!count)
  1461. break;
  1462. idx = streamer_read_uhwi (ib);
  1463. node = nodes[idx];
  1464. while (count)
  1465. {
  1466. input_ref (ib, node, nodes);
  1467. count--;
  1468. }
  1469. }
  1470. }
  1471. static struct gcov_ctr_summary lto_gcov_summary;
  1472. /* Input profile_info from IB. */
  1473. static void
  1474. input_profile_summary (struct lto_input_block *ib,
  1475. struct lto_file_decl_data *file_data)
  1476. {
  1477. unsigned h_ix;
  1478. struct bitpack_d bp;
  1479. unsigned int runs = streamer_read_uhwi (ib);
  1480. if (runs)
  1481. {
  1482. file_data->profile_info.runs = runs;
  1483. file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
  1484. file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
  1485. memset (file_data->profile_info.histogram, 0,
  1486. sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
  1487. /* Input the bitpack of non-zero histogram indices. */
  1488. bp = streamer_read_bitpack (ib);
  1489. /* Read in and unpack the full bitpack, flagging non-zero
  1490. histogram entries by setting the num_counters non-zero. */
  1491. for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
  1492. {
  1493. file_data->profile_info.histogram[h_ix].num_counters
  1494. = bp_unpack_value (&bp, 1);
  1495. }
  1496. for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
  1497. {
  1498. if (!file_data->profile_info.histogram[h_ix].num_counters)
  1499. continue;
  1500. file_data->profile_info.histogram[h_ix].num_counters
  1501. = streamer_read_gcov_count (ib);
  1502. file_data->profile_info.histogram[h_ix].min_value
  1503. = streamer_read_gcov_count (ib);
  1504. file_data->profile_info.histogram[h_ix].cum_value
  1505. = streamer_read_gcov_count (ib);
  1506. }
  1507. /* IPA-profile computes hot bb threshold based on cumulated
  1508. whole program profile. We need to stream it down to ltrans. */
  1509. if (flag_ltrans)
  1510. set_hot_bb_threshold (streamer_read_gcov_count (ib));
  1511. }
  1512. }
  1513. /* Rescale profile summaries to the same number of runs in the whole unit. */
  1514. static void
  1515. merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
  1516. {
  1517. struct lto_file_decl_data *file_data;
  1518. unsigned int j, h_ix;
  1519. gcov_unsigned_t max_runs = 0;
  1520. struct cgraph_node *node;
  1521. struct cgraph_edge *edge;
  1522. gcov_type saved_sum_all = 0;
  1523. gcov_ctr_summary *saved_profile_info = 0;
  1524. int saved_scale = 0;
  1525. /* Find unit with maximal number of runs. If we ever get serious about
  1526. roundoff errors, we might also consider computing smallest common
  1527. multiply. */
  1528. for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
  1529. if (max_runs < file_data->profile_info.runs)
  1530. max_runs = file_data->profile_info.runs;
  1531. if (!max_runs)
  1532. return;
  1533. /* Simple overflow check. We probably don't need to support that many train
  1534. runs. Such a large value probably imply data corruption anyway. */
  1535. if (max_runs > INT_MAX / REG_BR_PROB_BASE)
  1536. {
  1537. sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
  1538. INT_MAX / REG_BR_PROB_BASE);
  1539. return;
  1540. }
  1541. profile_info = &lto_gcov_summary;
  1542. lto_gcov_summary.runs = max_runs;
  1543. lto_gcov_summary.sum_max = 0;
  1544. memset (lto_gcov_summary.histogram, 0,
  1545. sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
  1546. /* Rescale all units to the maximal number of runs.
  1547. sum_max can not be easily merged, as we have no idea what files come from
  1548. the same run. We do not use the info anyway, so leave it 0. */
  1549. for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
  1550. if (file_data->profile_info.runs)
  1551. {
  1552. int scale = GCOV_COMPUTE_SCALE (max_runs,
  1553. file_data->profile_info.runs);
  1554. lto_gcov_summary.sum_max
  1555. = MAX (lto_gcov_summary.sum_max,
  1556. apply_scale (file_data->profile_info.sum_max, scale));
  1557. lto_gcov_summary.sum_all
  1558. = MAX (lto_gcov_summary.sum_all,
  1559. apply_scale (file_data->profile_info.sum_all, scale));
  1560. /* Save a pointer to the profile_info with the largest
  1561. scaled sum_all and the scale for use in merging the
  1562. histogram. */
  1563. if (!saved_profile_info
  1564. || lto_gcov_summary.sum_all > saved_sum_all)
  1565. {
  1566. saved_profile_info = &file_data->profile_info;
  1567. saved_sum_all = lto_gcov_summary.sum_all;
  1568. saved_scale = scale;
  1569. }
  1570. }
  1571. gcc_assert (saved_profile_info);
  1572. /* Scale up the histogram from the profile that had the largest
  1573. scaled sum_all above. */
  1574. for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
  1575. {
  1576. /* Scale up the min value as we did the corresponding sum_all
  1577. above. Use that to find the new histogram index. */
  1578. gcov_type scaled_min
  1579. = apply_scale (saved_profile_info->histogram[h_ix].min_value,
  1580. saved_scale);
  1581. /* The new index may be shared with another scaled histogram entry,
  1582. so we need to account for a non-zero histogram entry at new_ix. */
  1583. unsigned new_ix = gcov_histo_index (scaled_min);
  1584. lto_gcov_summary.histogram[new_ix].min_value
  1585. = (lto_gcov_summary.histogram[new_ix].num_counters
  1586. ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
  1587. : scaled_min);
  1588. /* Some of the scaled counter values would ostensibly need to be placed
  1589. into different (larger) histogram buckets, but we keep things simple
  1590. here and place the scaled cumulative counter value in the bucket
  1591. corresponding to the scaled minimum counter value. */
  1592. lto_gcov_summary.histogram[new_ix].cum_value
  1593. += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
  1594. saved_scale);
  1595. lto_gcov_summary.histogram[new_ix].num_counters
  1596. += saved_profile_info->histogram[h_ix].num_counters;
  1597. }
  1598. /* Watch roundoff errors. */
  1599. if (lto_gcov_summary.sum_max < max_runs)
  1600. lto_gcov_summary.sum_max = max_runs;
  1601. /* If merging already happent at WPA time, we are done. */
  1602. if (flag_ltrans)
  1603. return;
  1604. /* Now compute count_materialization_scale of each node.
  1605. During LTRANS we already have values of count_materialization_scale
  1606. computed, so just update them. */
  1607. FOR_EACH_FUNCTION (node)
  1608. if (node->lto_file_data
  1609. && node->lto_file_data->profile_info.runs)
  1610. {
  1611. int scale;
  1612. scale = RDIV (node->count_materialization_scale * max_runs,
  1613. node->lto_file_data->profile_info.runs);
  1614. node->count_materialization_scale = scale;
  1615. if (scale < 0)
  1616. fatal_error (input_location, "Profile information in %s corrupted",
  1617. file_data->file_name);
  1618. if (scale == REG_BR_PROB_BASE)
  1619. continue;
  1620. for (edge = node->callees; edge; edge = edge->next_callee)
  1621. edge->count = apply_scale (edge->count, scale);
  1622. node->count = apply_scale (node->count, scale);
  1623. }
  1624. }
  1625. /* Input and merge the symtab from each of the .o files passed to
  1626. lto1. */
  1627. void
  1628. input_symtab (void)
  1629. {
  1630. struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
  1631. struct lto_file_decl_data *file_data;
  1632. unsigned int j = 0;
  1633. struct cgraph_node *node;
  1634. while ((file_data = file_data_vec[j++]))
  1635. {
  1636. const char *data;
  1637. size_t len;
  1638. struct lto_input_block *ib;
  1639. vec<symtab_node *> nodes;
  1640. ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
  1641. &data, &len);
  1642. if (!ib)
  1643. fatal_error (input_location,
  1644. "cannot find LTO cgraph in %s", file_data->file_name);
  1645. input_profile_summary (ib, file_data);
  1646. file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
  1647. nodes = input_cgraph_1 (file_data, ib);
  1648. lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
  1649. ib, data, len);
  1650. ib = lto_create_simple_input_block (file_data, LTO_section_refs,
  1651. &data, &len);
  1652. if (!ib)
  1653. fatal_error (input_location, "cannot find LTO section refs in %s",
  1654. file_data->file_name);
  1655. input_refs (ib, nodes);
  1656. lto_destroy_simple_input_block (file_data, LTO_section_refs,
  1657. ib, data, len);
  1658. if (flag_ltrans)
  1659. input_cgraph_opt_summary (nodes);
  1660. nodes.release ();
  1661. }
  1662. merge_profile_summaries (file_data_vec);
  1663. get_working_sets ();
  1664. /* Clear out the aux field that was used to store enough state to
  1665. tell which nodes should be overwritten. */
  1666. FOR_EACH_FUNCTION (node)
  1667. {
  1668. /* Some nodes may have been created by cgraph_node. This
  1669. happens when the callgraph contains nested functions. If the
  1670. node for the parent function was never emitted to the gimple
  1671. file, cgraph_node will create a node for it when setting the
  1672. context of the nested function. */
  1673. if (node->lto_file_data)
  1674. node->aux = NULL;
  1675. }
  1676. }
  1677. /* Input function/variable tables that will allow libgomp to look up offload
  1678. target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
  1679. void
  1680. input_offload_tables (void)
  1681. {
  1682. struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
  1683. struct lto_file_decl_data *file_data;
  1684. unsigned int j = 0;
  1685. while ((file_data = file_data_vec[j++]))
  1686. {
  1687. const char *data;
  1688. size_t len;
  1689. struct lto_input_block *ib
  1690. = lto_create_simple_input_block (file_data, LTO_section_offload_table,
  1691. &data, &len);
  1692. if (!ib)
  1693. continue;
  1694. enum LTO_symtab_tags tag
  1695. = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
  1696. while (tag)
  1697. {
  1698. if (tag == LTO_symtab_unavail_node)
  1699. {
  1700. int decl_index = streamer_read_uhwi (ib);
  1701. tree fn_decl
  1702. = lto_file_decl_data_get_fn_decl (file_data, decl_index);
  1703. vec_safe_push (offload_funcs, fn_decl);
  1704. }
  1705. else if (tag == LTO_symtab_variable)
  1706. {
  1707. int decl_index = streamer_read_uhwi (ib);
  1708. tree var_decl
  1709. = lto_file_decl_data_get_var_decl (file_data, decl_index);
  1710. vec_safe_push (offload_vars, var_decl);
  1711. }
  1712. else
  1713. fatal_error (input_location,
  1714. "invalid offload table in %s", file_data->file_name);
  1715. tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
  1716. }
  1717. lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
  1718. ib, data, len);
  1719. }
  1720. }
  1721. /* True when we need optimization summary for NODE. */
  1722. static int
  1723. output_cgraph_opt_summary_p (struct cgraph_node *node)
  1724. {
  1725. return (node->clone_of
  1726. && (node->clone.tree_map
  1727. || node->clone.args_to_skip
  1728. || node->clone.combined_args_to_skip));
  1729. }
  1730. /* Output optimization summary for EDGE to OB. */
  1731. static void
  1732. output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
  1733. struct cgraph_edge *edge ATTRIBUTE_UNUSED)
  1734. {
  1735. }
  1736. /* Output optimization summary for NODE to OB. */
  1737. static void
  1738. output_node_opt_summary (struct output_block *ob,
  1739. struct cgraph_node *node,
  1740. lto_symtab_encoder_t encoder)
  1741. {
  1742. unsigned int index;
  1743. bitmap_iterator bi;
  1744. struct ipa_replace_map *map;
  1745. struct bitpack_d bp;
  1746. int i;
  1747. struct cgraph_edge *e;
  1748. if (node->clone.args_to_skip)
  1749. {
  1750. streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
  1751. EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
  1752. streamer_write_uhwi (ob, index);
  1753. }
  1754. else
  1755. streamer_write_uhwi (ob, 0);
  1756. if (node->clone.combined_args_to_skip)
  1757. {
  1758. streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
  1759. EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
  1760. streamer_write_uhwi (ob, index);
  1761. }
  1762. else
  1763. streamer_write_uhwi (ob, 0);
  1764. streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
  1765. FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
  1766. {
  1767. /* At the moment we assume all old trees to be PARM_DECLs, because we have no
  1768. mechanism to store function local declarations into summaries. */
  1769. gcc_assert (!map->old_tree);
  1770. streamer_write_uhwi (ob, map->parm_num);
  1771. gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
  1772. stream_write_tree (ob, map->new_tree, true);
  1773. bp = bitpack_create (ob->main_stream);
  1774. bp_pack_value (&bp, map->replace_p, 1);
  1775. bp_pack_value (&bp, map->ref_p, 1);
  1776. streamer_write_bitpack (&bp);
  1777. }
  1778. if (lto_symtab_encoder_in_partition_p (encoder, node))
  1779. {
  1780. for (e = node->callees; e; e = e->next_callee)
  1781. output_edge_opt_summary (ob, e);
  1782. for (e = node->indirect_calls; e; e = e->next_callee)
  1783. output_edge_opt_summary (ob, e);
  1784. }
  1785. }
  1786. /* Output optimization summaries stored in callgraph.
  1787. At the moment it is the clone info structure. */
  1788. static void
  1789. output_cgraph_opt_summary (void)
  1790. {
  1791. int i, n_nodes;
  1792. lto_symtab_encoder_t encoder;
  1793. struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
  1794. unsigned count = 0;
  1795. ob->symbol = NULL;
  1796. encoder = ob->decl_state->symtab_node_encoder;
  1797. n_nodes = lto_symtab_encoder_size (encoder);
  1798. for (i = 0; i < n_nodes; i++)
  1799. {
  1800. symtab_node *node = lto_symtab_encoder_deref (encoder, i);
  1801. cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
  1802. if (cnode && output_cgraph_opt_summary_p (cnode))
  1803. count++;
  1804. }
  1805. streamer_write_uhwi (ob, count);
  1806. for (i = 0; i < n_nodes; i++)
  1807. {
  1808. symtab_node *node = lto_symtab_encoder_deref (encoder, i);
  1809. cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
  1810. if (cnode && output_cgraph_opt_summary_p (cnode))
  1811. {
  1812. streamer_write_uhwi (ob, i);
  1813. output_node_opt_summary (ob, cnode, encoder);
  1814. }
  1815. }
  1816. produce_asm (ob, NULL);
  1817. destroy_output_block (ob);
  1818. }
  1819. /* Input optimisation summary of EDGE. */
  1820. static void
  1821. input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
  1822. struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
  1823. {
  1824. }
  1825. /* Input optimisation summary of NODE. */
  1826. static void
  1827. input_node_opt_summary (struct cgraph_node *node,
  1828. struct lto_input_block *ib_main,
  1829. struct data_in *data_in)
  1830. {
  1831. int i;
  1832. int count;
  1833. int bit;
  1834. struct bitpack_d bp;
  1835. struct cgraph_edge *e;
  1836. count = streamer_read_uhwi (ib_main);
  1837. if (count)
  1838. node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
  1839. for (i = 0; i < count; i++)
  1840. {
  1841. bit = streamer_read_uhwi (ib_main);
  1842. bitmap_set_bit (node->clone.args_to_skip, bit);
  1843. }
  1844. count = streamer_read_uhwi (ib_main);
  1845. if (count)
  1846. node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
  1847. for (i = 0; i < count; i++)
  1848. {
  1849. bit = streamer_read_uhwi (ib_main);
  1850. bitmap_set_bit (node->clone.combined_args_to_skip, bit);
  1851. }
  1852. count = streamer_read_uhwi (ib_main);
  1853. for (i = 0; i < count; i++)
  1854. {
  1855. struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
  1856. vec_safe_push (node->clone.tree_map, map);
  1857. map->parm_num = streamer_read_uhwi (ib_main);
  1858. map->old_tree = NULL;
  1859. map->new_tree = stream_read_tree (ib_main, data_in);
  1860. bp = streamer_read_bitpack (ib_main);
  1861. map->replace_p = bp_unpack_value (&bp, 1);
  1862. map->ref_p = bp_unpack_value (&bp, 1);
  1863. }
  1864. for (e = node->callees; e; e = e->next_callee)
  1865. input_edge_opt_summary (e, ib_main);
  1866. for (e = node->indirect_calls; e; e = e->next_callee)
  1867. input_edge_opt_summary (e, ib_main);
  1868. }
  1869. /* Read section in file FILE_DATA of length LEN with data DATA. */
  1870. static void
  1871. input_cgraph_opt_section (struct lto_file_decl_data *file_data,
  1872. const char *data, size_t len,
  1873. vec<symtab_node *> nodes)
  1874. {
  1875. const struct lto_function_header *header =
  1876. (const struct lto_function_header *) data;
  1877. const int cfg_offset = sizeof (struct lto_function_header);
  1878. const int main_offset = cfg_offset + header->cfg_size;
  1879. const int string_offset = main_offset + header->main_size;
  1880. struct data_in *data_in;
  1881. unsigned int i;
  1882. unsigned int count;
  1883. lto_input_block ib_main ((const char *) data + main_offset,
  1884. header->main_size, file_data->mode_table);
  1885. data_in =
  1886. lto_data_in_create (file_data, (const char *) data + string_offset,
  1887. header->string_size, vNULL);
  1888. count = streamer_read_uhwi (&ib_main);
  1889. for (i = 0; i < count; i++)
  1890. {
  1891. int ref = streamer_read_uhwi (&ib_main);
  1892. input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
  1893. &ib_main, data_in);
  1894. }
  1895. lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
  1896. len);
  1897. lto_data_in_delete (data_in);
  1898. }
  1899. /* Input optimization summary of cgraph. */
  1900. static void
  1901. input_cgraph_opt_summary (vec<symtab_node *> nodes)
  1902. {
  1903. struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
  1904. struct lto_file_decl_data *file_data;
  1905. unsigned int j = 0;
  1906. while ((file_data = file_data_vec[j++]))
  1907. {
  1908. size_t len;
  1909. const char *data =
  1910. lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
  1911. &len);
  1912. if (data)
  1913. input_cgraph_opt_section (file_data, data, len, nodes);
  1914. }
  1915. }