Line data Source code
1 : /* Top-level LTO routines.
2 : Copyright (C) 2009-2026 Free Software Foundation, Inc.
3 : Contributed by CodeSourcery, Inc.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #define INCLUDE_STRING
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "tm.h"
26 : #include "function.h"
27 : #include "bitmap.h"
28 : #include "basic-block.h"
29 : #include "tree.h"
30 : #include "gimple.h"
31 : #include "cfghooks.h"
32 : #include "alloc-pool.h"
33 : #include "tree-pass.h"
34 : #include "tree-streamer.h"
35 : #include "cgraph.h"
36 : #include "opts.h"
37 : #include "toplev.h"
38 : #include "stor-layout.h"
39 : #include "symbol-summary.h"
40 : #include "tree-vrp.h"
41 : #include "sreal.h"
42 : #include "ipa-cp.h"
43 : #include "ipa-prop.h"
44 : #include "debug.h"
45 : #include "lto.h"
46 : #include "lto-section-names.h"
47 : #include "splay-tree.h"
48 : #include "lto-partition.h"
49 : #include "context.h"
50 : #include "pass_manager.h"
51 : #include "ipa-fnsummary.h"
52 : #include "ipa-utils.h"
53 : #include "gomp-constants.h"
54 : #include "lto-symtab.h"
55 : #include "stringpool.h"
56 : #include "fold-const.h"
57 : #include "attribs.h"
58 : #include "builtins.h"
59 : #include "lto-common.h"
60 : #include "opts-jobserver.h"
61 :
62 : /* Number of parallel tasks to run. */
63 : static int lto_parallelism;
64 :
65 : #ifdef HAVE_WORKING_FORK
66 : /* Number of active WPA streaming processes. */
67 : static int nruns = 0;
68 : #endif
69 :
70 : /* GNU make's jobserver info. */
71 : static jobserver_info *jinfo = NULL;
72 :
73 : /* Return true when NODE has a clone that is analyzed (i.e. we need
74 : to load its body even if the node itself is not needed). */
75 :
76 : static bool
77 53023 : has_analyzed_clone_p (struct cgraph_node *node)
78 : {
79 53023 : struct cgraph_node *orig = node;
80 53023 : node = node->clones;
81 53023 : if (node)
82 3784 : while (node != orig)
83 : {
84 3784 : if (node->analyzed)
85 : return true;
86 433 : if (node->clones)
87 : node = node->clones;
88 0 : else if (node->next_sibling_clone)
89 : node = node->next_sibling_clone;
90 : else
91 : {
92 0 : while (node != orig && !node->next_sibling_clone)
93 0 : node = node->clone_of;
94 0 : if (node != orig)
95 0 : node = node->next_sibling_clone;
96 : }
97 : }
98 : return false;
99 : }
100 :
101 : /* Read the function body for the function associated with NODE. */
102 :
103 : static void
104 194841 : lto_materialize_function (struct cgraph_node *node)
105 : {
106 194841 : tree decl;
107 :
108 194841 : decl = node->decl;
109 : /* Read in functions with body (analyzed nodes)
110 : and also functions that are needed to produce virtual clones. */
111 141323 : if ((node->has_gimple_body_p () && node->analyzed)
112 53519 : || node->used_as_abstract_origin
113 247864 : || has_analyzed_clone_p (node))
114 : {
115 : /* Clones don't need to be read. */
116 145169 : if (node->clone_of)
117 : return;
118 122010 : if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
119 670 : first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
120 : /* If the file contains a function with a language specific EH
121 : personality set or with EH enabled initialize the backend EH
122 : machinery. */
123 122010 : if (DECL_FUNCTION_PERSONALITY (decl)
124 122010 : || opt_for_fn (decl, flag_exceptions))
125 43815 : lto_init_eh ();
126 : }
127 :
128 : /* Let the middle end know about the function. */
129 171682 : rest_of_decl_compilation (decl, 1, 0);
130 : }
131 :
132 : /* Materialize all the bodies for all the nodes in the callgraph. */
133 :
134 : static void
135 21175 : materialize_cgraph (void)
136 : {
137 21175 : struct cgraph_node *node;
138 21175 : timevar_id_t lto_timer;
139 :
140 21175 : if (!quiet_flag)
141 0 : fprintf (stderr,
142 0 : flag_wpa ? "Materializing decls:" : "Reading function bodies:");
143 :
144 : /* Start the appropriate timer depending on the mode that we are
145 : operating in. */
146 25651 : lto_timer = (flag_wpa) ? TV_WHOPR_WPA
147 13018 : : (flag_ltrans) ? TV_WHOPR_LTRANS
148 : : TV_LTO;
149 21175 : timevar_push (lto_timer);
150 :
151 216016 : FOR_EACH_FUNCTION (node)
152 : {
153 194841 : if (node->lto_file_data)
154 : {
155 194841 : lto_materialize_function (node);
156 194841 : lto_stats.num_input_cgraph_nodes++;
157 : }
158 : }
159 :
160 21175 : current_function_decl = NULL;
161 21175 : set_cfun (NULL);
162 :
163 21175 : if (!quiet_flag)
164 0 : fprintf (stderr, "\n");
165 :
166 21175 : timevar_pop (lto_timer);
167 21175 : }
168 :
169 : /* Stream out all the linemap sections so they are available for LTRANS. */
170 :
171 : static void
172 8157 : stream_out_linemaps (const char *filename)
173 : {
174 8157 : const auto file = lto_obj_file_open (filename, true);
175 8157 : if (!file)
176 0 : fatal_error (input_location, "%<lto_obj_file_open()%> failed");
177 8157 : lto_set_current_out_file (file);
178 8157 : lto_copy_linemaps ();
179 8157 : free (const_cast<char *> (file->filename));
180 8157 : lto_set_current_out_file (nullptr);
181 8157 : lto_obj_file_close (file);
182 8157 : free (file);
183 8157 : }
184 :
185 : /* Actually stream out ENCODER into TEMP_FILENAME. */
186 :
187 : static void
188 8542 : stream_out (char *temp_filename, lto_symtab_encoder_t encoder, int part)
189 : {
190 8542 : lto_file *file = lto_obj_file_open (temp_filename, true);
191 8542 : if (!file)
192 0 : fatal_error (input_location, "%<lto_obj_file_open()%> failed");
193 8542 : lto_set_current_out_file (file);
194 :
195 8542 : gcc_assert (!dump_file);
196 8542 : streamer_dump_file = dump_begin (TDI_lto_stream_out, NULL, part);
197 8542 : ipa_write_optimization_summaries (encoder, part == 0);
198 :
199 8542 : free (const_cast<char *> (file->filename));
200 :
201 8542 : lto_set_current_out_file (NULL);
202 8542 : lto_obj_file_close (file);
203 8542 : free (file);
204 8542 : if (streamer_dump_file)
205 : {
206 0 : dump_end (TDI_lto_stream_out, streamer_dump_file);
207 0 : streamer_dump_file = NULL;
208 : }
209 8542 : }
210 :
211 : /* Wait for forked process and signal errors. */
212 : #ifdef HAVE_WORKING_FORK
213 : static void
214 0 : wait_for_child ()
215 : {
216 0 : int status;
217 0 : do
218 : {
219 : #ifndef WCONTINUED
220 : #define WCONTINUED 0
221 : #endif
222 0 : int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
223 0 : if (w == -1)
224 0 : fatal_error (input_location, "waitpid failed");
225 :
226 0 : if (WIFEXITED (status) && WEXITSTATUS (status))
227 0 : fatal_error (input_location, "streaming subprocess failed");
228 0 : else if (WIFSIGNALED (status))
229 0 : fatal_error (input_location,
230 : "streaming subprocess was killed by signal");
231 : }
232 0 : while (!WIFEXITED (status) && !WIFSIGNALED (status));
233 :
234 0 : --nruns;
235 :
236 : /* Return token to the jobserver if active. */
237 0 : if (jinfo != NULL && jinfo->is_connected)
238 0 : jinfo->return_token ();
239 0 : }
240 : #endif
241 :
242 : static void
243 8526 : stream_out_partitions_1 (char *temp_filename, int blen, int min, int max)
244 : {
245 : /* Write all the nodes in SET. */
246 17068 : for (int p = min; p < max; p ++)
247 : {
248 8542 : sprintf (temp_filename + blen, "%u.o", p);
249 8542 : stream_out (temp_filename, ltrans_partitions[p]->encoder, p);
250 8542 : ltrans_partitions[p]->encoder = NULL;
251 : }
252 8526 : }
253 :
254 : /* Stream out ENCODER into TEMP_FILENAME
255 : Fork if that seems to help. */
256 :
257 : static void
258 8526 : stream_out_partitions (char *temp_filename, int blen, int min, int max,
259 : bool ARG_UNUSED (last))
260 : {
261 : #ifdef HAVE_WORKING_FORK
262 8526 : if (lto_parallelism <= 1)
263 : {
264 7946 : stream_out_partitions_1 (temp_filename, blen, min, max);
265 7946 : return;
266 : }
267 :
268 580 : if (lto_parallelism > 0 && nruns >= lto_parallelism)
269 0 : wait_for_child ();
270 :
271 : /* If this is not the last parallel partition, execute new
272 : streaming process. */
273 580 : if (!last)
274 : {
275 369 : if (jinfo != NULL && jinfo->is_connected)
276 369 : while (true)
277 : {
278 369 : if (jinfo->get_token ())
279 : break;
280 369 : if (nruns > 0)
281 0 : wait_for_child ();
282 : else
283 : {
284 : /* There are no free tokens, lets do the job ourselves. */
285 369 : stream_out_partitions_1 (temp_filename, blen, min, max);
286 369 : return;
287 : }
288 : }
289 :
290 0 : pid_t cpid = fork ();
291 :
292 0 : if (!cpid)
293 : {
294 0 : setproctitle ("lto1-wpa-streaming");
295 0 : stream_out_partitions_1 (temp_filename, blen, min, max);
296 0 : exit (0);
297 : }
298 : /* Fork failed; lets do the job ourseleves. */
299 0 : else if (cpid == -1)
300 0 : stream_out_partitions_1 (temp_filename, blen, min, max);
301 : else
302 0 : nruns++;
303 : }
304 : /* Last partition; stream it and wait for all children to die. */
305 : else
306 : {
307 211 : stream_out_partitions_1 (temp_filename, blen, min, max);
308 422 : while (nruns > 0)
309 0 : wait_for_child ();
310 :
311 211 : if (jinfo != NULL && jinfo->is_connected)
312 211 : jinfo->disconnect ();
313 : }
314 : #else
315 : stream_out_partitions_1 (temp_filename, blen, min, max);
316 : #endif
317 : }
318 :
319 : /* Write all output files in WPA mode and the file with the list of
320 : LTRANS units. */
321 :
322 : static void
323 8157 : lto_wpa_write_files (void)
324 : {
325 8157 : unsigned i, n_sets;
326 8157 : ltrans_partition part;
327 8157 : FILE *ltrans_output_list_stream;
328 8157 : char *temp_filename;
329 8157 : auto_vec <char *>temp_filenames;
330 8157 : auto_vec <int>temp_priority;
331 8157 : size_t blen;
332 :
333 : /* Open the LTRANS output list. */
334 8157 : if (!ltrans_output_list)
335 0 : fatal_error (input_location, "no LTRANS output list filename provided");
336 :
337 8157 : timevar_push (TV_WHOPR_WPA);
338 :
339 24856 : FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
340 17084 : lto_stats.num_output_symtab_nodes
341 17076 : += lto_symtab_encoder_size (part->encoder);
342 :
343 8157 : timevar_pop (TV_WHOPR_WPA);
344 :
345 8157 : timevar_push (TV_WHOPR_WPA_IO);
346 :
347 8157 : cgraph_node *node;
348 : /* Do body modifications needed for streaming before we fork out
349 : worker processes. */
350 91519 : FOR_EACH_FUNCTION (node)
351 83362 : if (!node->clone_of && gimple_has_body_p (node->decl))
352 5436 : lto_prepare_function_for_streaming (node);
353 :
354 8157 : ggc_trim ();
355 8157 : report_heap_memory_use ();
356 :
357 : /* Generate a prefix for the LTRANS unit files. */
358 8157 : blen = strlen (ltrans_output_list);
359 8157 : temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
360 8157 : strcpy (temp_filename, ltrans_output_list);
361 8157 : if (blen > sizeof (".out")
362 8157 : && strcmp (temp_filename + blen - sizeof (".out") + 1,
363 : ".out") == 0)
364 8157 : temp_filename[blen - sizeof (".out") + 1] = '\0';
365 8157 : blen = strlen (temp_filename);
366 :
367 8157 : n_sets = ltrans_partitions.length ();
368 8157 : unsigned sets_per_worker = n_sets;
369 8157 : if (lto_parallelism > 1)
370 : {
371 8079 : if (lto_parallelism > (int)n_sets)
372 8075 : lto_parallelism = n_sets;
373 8079 : sets_per_worker = (n_sets + lto_parallelism - 1) / lto_parallelism;
374 : }
375 :
376 : /* Write out all the linemaps since they will be needed during LTRANS. */
377 8157 : sprintf (temp_filename + blen, "%u.o", n_sets);
378 8157 : const auto linemap_filename = xstrdup (temp_filename);
379 8157 : stream_out_linemaps (linemap_filename);
380 :
381 : /* Write out the partitions. */
382 16699 : for (i = 0; i < n_sets; i++)
383 : {
384 8542 : ltrans_partition part = ltrans_partitions[i];
385 :
386 : /* Write all the nodes in SET. */
387 8542 : sprintf (temp_filename + blen, "%u.o", i);
388 :
389 8542 : if (!quiet_flag)
390 0 : fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name,
391 : part->insns);
392 8542 : if (symtab->dump_file)
393 : {
394 1 : lto_symtab_encoder_iterator lsei;
395 :
396 1 : fprintf (symtab->dump_file,
397 : "Writing partition %s to file %s, %i insns\n",
398 : part->name, temp_filename, part->insns);
399 1 : fprintf (symtab->dump_file, " Symbols in partition: ");
400 1 : for (lsei = lsei_start_in_partition (part->encoder);
401 10 : !lsei_end_p (lsei);
402 9 : lsei_next_in_partition (&lsei))
403 : {
404 18 : symtab_node *node = dyn_cast<symtab_node*> (lsei_node (lsei));
405 9 : if (node)
406 9 : fprintf (symtab->dump_file, "%s ", node->dump_asm_name ());
407 : }
408 1 : fprintf (symtab->dump_file, "\n Symbols in boundary: ");
409 10 : for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
410 9 : lsei_next (&lsei))
411 : {
412 9 : symtab_node *node = dyn_cast<symtab_node*> (lsei_node (lsei));
413 9 : if (!node)
414 0 : continue;
415 9 : if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
416 : {
417 0 : fprintf (symtab->dump_file, "%s ", node->dump_asm_name ());
418 0 : cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
419 0 : if (cnode
420 0 : && lto_symtab_encoder_encode_body_p (part->encoder,
421 : cnode))
422 0 : fprintf (symtab->dump_file, "(body included)");
423 : else
424 : {
425 9 : varpool_node *vnode = dyn_cast <varpool_node *> (node);
426 0 : if (vnode
427 0 : && lto_symtab_encoder_encode_initializer_p (part->encoder,
428 : vnode))
429 0 : fprintf (symtab->dump_file, "(initializer included)");
430 : }
431 : }
432 : }
433 1 : fprintf (symtab->dump_file, "\n");
434 : }
435 8542 : gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
436 :
437 8542 : temp_priority.safe_push (part->insns);
438 8542 : temp_filenames.safe_push (xstrdup (temp_filename));
439 : }
440 8157 : memory_block_pool::trim (0);
441 :
442 16683 : for (int set = 0; set < MAX (lto_parallelism, 1); set++)
443 : {
444 8526 : stream_out_partitions (temp_filename, blen, set * sets_per_worker,
445 8526 : MIN ((set + 1) * sets_per_worker, n_sets),
446 8526 : set == MAX (lto_parallelism, 1) - 1);
447 : }
448 :
449 8157 : ltrans_output_list_stream = fopen (ltrans_output_list, "w");
450 8157 : if (ltrans_output_list_stream == NULL)
451 0 : fatal_error (input_location,
452 : "opening LTRANS output list %s: %m", ltrans_output_list);
453 8157 : fprintf (ltrans_output_list_stream, "0\n%s\n", linemap_filename);
454 8157 : free (linemap_filename);
455 16699 : for (i = 0; i < n_sets; i++)
456 : {
457 8542 : unsigned int len = strlen (temp_filenames[i]);
458 8542 : if (fprintf (ltrans_output_list_stream, "%i\n", temp_priority[i]) < 0
459 8542 : || fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
460 17084 : || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
461 0 : fatal_error (input_location, "writing to LTRANS output list %s: %m",
462 : ltrans_output_list);
463 8542 : free (temp_filenames[i]);
464 : }
465 :
466 8157 : lto_stats.num_output_files += n_sets;
467 :
468 : /* Close the LTRANS output list. */
469 8157 : if (fclose (ltrans_output_list_stream))
470 0 : fatal_error (input_location,
471 : "closing LTRANS output list %s: %m", ltrans_output_list);
472 :
473 8157 : free_ltrans_partitions ();
474 8157 : free (temp_filename);
475 :
476 8157 : timevar_pop (TV_WHOPR_WPA_IO);
477 8157 : }
478 :
479 : /* Create artificial pointers for "omp declare target link" vars. */
480 :
481 : static void
482 4476 : offload_handle_link_vars (void)
483 : {
484 : #ifdef ACCEL_COMPILER
485 : varpool_node *var;
486 : FOR_EACH_VARIABLE (var)
487 : if (lookup_attribute ("omp declare target link",
488 : DECL_ATTRIBUTES (var->decl)))
489 : {
490 : tree type = build_pointer_type (TREE_TYPE (var->decl));
491 : tree link_ptr_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
492 : clone_function_name (var->decl,
493 : "linkptr"), type);
494 : TREE_USED (link_ptr_var) = 1;
495 : TREE_STATIC (link_ptr_var) = 1;
496 : TREE_PUBLIC (link_ptr_var) = TREE_PUBLIC (var->decl);
497 : DECL_ARTIFICIAL (link_ptr_var) = 1;
498 : SET_DECL_ASSEMBLER_NAME (link_ptr_var, DECL_NAME (link_ptr_var));
499 : SET_DECL_VALUE_EXPR (var->decl, build_simple_mem_ref (link_ptr_var));
500 : DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
501 : }
502 : #endif
503 4476 : }
504 :
505 : /* Perform whole program analysis (WPA) on the callgraph and write out the
506 : optimization plan. */
507 :
508 : static void
509 8157 : do_whole_program_analysis (void)
510 : {
511 8157 : symtab_node *node;
512 :
513 8157 : lto_parallelism = 1;
514 :
515 8157 : if (!strcmp (flag_wpa, "jobserver"))
516 : {
517 8079 : jinfo = new jobserver_info ();
518 8079 : if (jinfo->is_active)
519 8079 : jinfo->connect ();
520 :
521 8079 : lto_parallelism = param_max_lto_streaming_parallelism;
522 : }
523 : else
524 : {
525 78 : lto_parallelism = atoi (flag_wpa);
526 78 : if (lto_parallelism <= 0)
527 78 : lto_parallelism = 0;
528 78 : if (lto_parallelism >= param_max_lto_streaming_parallelism)
529 0 : lto_parallelism = param_max_lto_streaming_parallelism;
530 : }
531 :
532 8157 : timevar_start (TV_PHASE_OPT_GEN);
533 :
534 : /* Note that since we are in WPA mode, materialize_cgraph will not
535 : actually read in all the function bodies. It only materializes
536 : the decls and cgraph nodes so that analysis can be performed. */
537 8157 : materialize_cgraph ();
538 :
539 : /* Reading in the cgraph uses different timers, start timing WPA now. */
540 8157 : timevar_push (TV_WHOPR_WPA);
541 :
542 8157 : if (pre_ipa_mem_report)
543 0 : dump_memory_report ("Memory consumption before IPA");
544 :
545 8157 : symtab->function_flags_ready = true;
546 :
547 8157 : if (symtab->dump_file)
548 1 : symtab->dump (symtab->dump_file);
549 8157 : bitmap_obstack_initialize (NULL);
550 8157 : symtab->state = IPA_SSA;
551 :
552 8157 : execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
553 :
554 : /* When WPA analysis raises errors, do not bother to output anything. */
555 8157 : if (seen_error ())
556 : return;
557 :
558 : /* We are about to launch the final LTRANS phase, stop the WPA timer. */
559 8157 : timevar_pop (TV_WHOPR_WPA);
560 :
561 : /* We are no longer going to stream in anything. Free some memory. */
562 8157 : lto_free_file_name_hash ();
563 :
564 :
565 8157 : timevar_push (TV_WHOPR_PARTITIONING);
566 :
567 8157 : gcc_assert (!dump_file);
568 8157 : dump_file = dump_begin (partition_dump_id, NULL);
569 :
570 8157 : if (dump_file)
571 0 : symtab->dump (dump_file);
572 :
573 8157 : symtab_node::checking_verify_symtab_nodes ();
574 8157 : bitmap_obstack_release (NULL);
575 8157 : if (flag_ipa_reorder_for_locality)
576 0 : lto_locality_map (param_max_locality_partition_size);
577 8157 : else if (flag_lto_partition == LTO_PARTITION_1TO1)
578 313 : lto_1_to_1_map ();
579 : else if (flag_lto_partition == LTO_PARTITION_MAX)
580 15 : lto_max_map ();
581 : else if (flag_lto_partition == LTO_PARTITION_ONE)
582 70 : lto_balanced_map (1, INT_MAX);
583 : else if (flag_lto_partition == LTO_PARTITION_BALANCED)
584 7755 : lto_balanced_map (param_lto_partitions,
585 : param_max_partition_size);
586 : else if (flag_lto_partition == LTO_PARTITION_CACHE)
587 4 : lto_cache_map (param_lto_partitions, param_max_partition_size);
588 : else
589 0 : gcc_unreachable ();
590 :
591 : /* Size summaries are needed for balanced partitioning. Free them now so
592 : the memory can be used for streamer caches. */
593 8157 : ipa_free_size_summary ();
594 :
595 : /* AUX pointers are used by partitioning code to bookkeep number of
596 : partitions symbol is in. This is no longer needed. */
597 116231 : FOR_EACH_SYMBOL (node)
598 108074 : node->aux = NULL;
599 :
600 8157 : lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
601 :
602 : /* Find out statics that need to be promoted
603 : to globals with hidden visibility because they are accessed from multiple
604 : partitions. */
605 8157 : lto_promote_cross_file_statics ();
606 8157 : offload_handle_link_vars ();
607 8157 : if (dump_file)
608 0 : dump_end (partition_dump_id, dump_file);
609 8157 : dump_file = NULL;
610 8157 : timevar_pop (TV_WHOPR_PARTITIONING);
611 :
612 8157 : timevar_stop (TV_PHASE_OPT_GEN);
613 :
614 : /* Collect a last time - in lto_wpa_write_files we may end up forking
615 : with the idea that this doesn't increase memory usage. So we
616 : absolutely do not want to collect after that. */
617 8157 : ggc_collect ();
618 :
619 8157 : timevar_start (TV_PHASE_STREAM_OUT);
620 8157 : if (!quiet_flag)
621 : {
622 0 : fprintf (stderr, "\nStreaming out");
623 0 : fflush (stderr);
624 : }
625 8157 : lto_wpa_write_files ();
626 8157 : if (!quiet_flag)
627 0 : fprintf (stderr, "\n");
628 8157 : timevar_stop (TV_PHASE_STREAM_OUT);
629 :
630 8157 : if (post_ipa_mem_report)
631 0 : dump_memory_report ("Memory consumption after IPA");
632 :
633 : /* Show the LTO report before launching LTRANS. */
634 8157 : if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
635 0 : print_lto_report_1 ();
636 8157 : if (mem_report_wpa)
637 0 : dump_memory_report ("Final");
638 : }
639 :
640 : unsigned int
641 46156 : lto_option_lang_mask (void)
642 : {
643 46156 : return CL_LTO;
644 : }
645 :
646 : /* Main entry point for the GIMPLE front end. This front end has
647 : three main personalities:
648 :
649 : - LTO (-flto). All the object files on the command line are
650 : loaded in memory and processed as a single translation unit.
651 : This is the traditional link-time optimization behavior.
652 :
653 : - WPA (-fwpa). Only the callgraph and summary information for
654 : files in the command file are loaded. A single callgraph
655 : (without function bodies) is instantiated for the whole set of
656 : files. IPA passes are only allowed to analyze the call graph
657 : and make transformation decisions. The callgraph is
658 : partitioned, each partition is written to a new object file
659 : together with the transformation decisions.
660 :
661 : - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
662 : summary files from running again. Since WPA computed summary
663 : information and decided what transformations to apply, LTRANS
664 : simply applies them. */
665 :
666 : void
667 21175 : lto_main (void)
668 : {
669 : /* LTO is called as a front end, even though it is not a front end.
670 : Because it is called as a front end, TV_PHASE_PARSING and
671 : TV_PARSE_GLOBAL are active, and we need to turn them off while
672 : doing LTO. Later we turn them back on so they are active up in
673 : toplev.cc. */
674 21175 : timevar_pop (TV_PARSE_GLOBAL);
675 21175 : timevar_stop (TV_PHASE_PARSING);
676 :
677 21175 : timevar_start (TV_PHASE_SETUP);
678 :
679 : /* Initialize the LTO front end. */
680 21175 : lto_fe_init ();
681 :
682 21175 : timevar_stop (TV_PHASE_SETUP);
683 21175 : timevar_start (TV_PHASE_STREAM_IN);
684 :
685 : /* Read all the symbols and call graph from all the files in the
686 : command line. */
687 21175 : read_cgraph_and_symbols (num_in_fnames, in_fnames);
688 :
689 21175 : timevar_stop (TV_PHASE_STREAM_IN);
690 :
691 21175 : if (!seen_error ())
692 : {
693 21175 : offload_handle_link_vars ();
694 :
695 : /* If WPA is enabled analyze the whole call graph and create an
696 : optimization plan. Otherwise, read in all the function
697 : bodies and continue with optimization. */
698 21175 : if (flag_wpa)
699 8157 : do_whole_program_analysis ();
700 : else
701 : {
702 13018 : timevar_start (TV_PHASE_OPT_GEN);
703 :
704 13018 : materialize_cgraph ();
705 13018 : if (!flag_ltrans)
706 : {
707 4476 : lto_promote_statics_nonwpa ();
708 4476 : offload_handle_link_vars ();
709 : }
710 :
711 : /* Annotate the CU DIE and mark the early debug phase as finished. */
712 13018 : debuginfo_early_start ();
713 13018 : debug_hooks->early_finish ("<artificial>");
714 13018 : debuginfo_early_stop ();
715 :
716 : /* Let the middle end know that we have read and merged all of
717 : the input files. */
718 13018 : symtab->compile ();
719 :
720 13018 : timevar_stop (TV_PHASE_OPT_GEN);
721 :
722 : /* FIXME lto, if the processes spawned by WPA fail, we miss
723 : the chance to print WPA's report, so WPA will call
724 : print_lto_report before launching LTRANS. If LTRANS was
725 : launched directly by the driver we would not need to do
726 : this. */
727 13018 : if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
728 0 : print_lto_report_1 ();
729 : }
730 : }
731 :
732 : /* Here we make LTO pretend to be a parser. */
733 21175 : timevar_start (TV_PHASE_PARSING);
734 21175 : timevar_push (TV_PARSE_GLOBAL);
735 21175 : }
|