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