1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2    Copyright (C) 1987-2021 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
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 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 /* This is the top level of cc1/c++.
21    It parses command args, opens files, invokes the various passes
22    in the proper order, and counts the time used by each.
23    Error messages and low-level interface to malloc also handled here.  */
24 
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "alloc-pool.h"
34 #include "timevar.h"
35 #include "memmodel.h"
36 #include "tm_p.h"
37 #include "optabs-libfuncs.h"
38 #include "insn-config.h"
39 #include "ira.h"
40 #include "recog.h"
41 #include "cgraph.h"
42 #include "coverage.h"
43 #include "diagnostic.h"
44 #include "varasm.h"
45 #include "tree-inline.h"
46 #include "realmpfr.h"	/* For GMP/MPFR/MPC versions, in print_version.  */
47 #include "version.h"
48 #include "flags.h"
49 #include "insn-attr.h"
50 #include "output.h"
51 #include "toplev.h"
52 #include "expr.h"
53 #include "intl.h"
54 #include "tree-diagnostic.h"
55 #include "reload.h"
56 #include "lra.h"
57 #include "dwarf2asm.h"
58 #include "debug.h"
59 #include "common/common-target.h"
60 #include "langhooks.h"
61 #include "cfgloop.h" /* for init_set_costs */
62 #include "hosthooks.h"
63 #include "opts.h"
64 #include "opts-diagnostic.h"
65 #include "stringpool.h"
66 #include "attribs.h"
67 #include "asan.h"
68 #include "tsan.h"
69 #include "plugin.h"
70 #include "context.h"
71 #include "pass_manager.h"
72 #include "auto-profile.h"
73 #include "dwarf2out.h"
74 #include "ipa-reference.h"
75 #include "symbol-summary.h"
76 #include "tree-vrp.h"
77 #include "ipa-prop.h"
78 #include "gcse.h"
79 #include "omp-offload.h"
80 #include "edit-context.h"
81 #include "tree-pass.h"
82 #include "dumpfile.h"
83 #include "ipa-fnsummary.h"
84 #include "dump-context.h"
85 #include "print-tree.h"
86 #include "optinfo-emit-json.h"
87 #include "ipa-modref-tree.h"
88 #include "ipa-modref.h"
89 #include "dbgcnt.h"
90 
91 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
92 #include "dbxout.h"
93 #endif
94 
95 #ifdef XCOFF_DEBUGGING_INFO
96 #include "xcoffout.h"		/* Needed for external data declarations. */
97 #endif
98 
99 #include "selftest.h"
100 
101 #ifdef HAVE_isl
102 #include <isl/version.h>
103 #endif
104 
105 static void general_init (const char *, bool);
106 static void do_compile ();
107 static void process_options (void);
108 static void backend_init (void);
109 static int lang_dependent_init (const char *);
110 static void init_asm_output (const char *);
111 static void finalize (bool);
112 
113 static void crash_signal (int) ATTRIBUTE_NORETURN;
114 static void compile_file (void);
115 
116 /* True if we don't need a backend (e.g. preprocessing only).  */
117 static bool no_backend;
118 
119 /* Decoded options, and number of such options.  */
120 struct cl_decoded_option *save_decoded_options;
121 unsigned int save_decoded_options_count;
122 
123 /* Used to enable -fvar-tracking, -fweb and -frename-registers according
124    to optimize in process_options ().  */
125 #define AUTODETECT_VALUE 2
126 
127 /* Debug hooks - dependent upon command line options.  */
128 
129 const struct gcc_debug_hooks *debug_hooks;
130 
131 /* The FUNCTION_DECL for the function currently being compiled,
132    or 0 if between functions.  */
133 tree current_function_decl;
134 
135 /* Set to the FUNC_BEGIN label of the current function, or NULL
136    if none.  */
137 const char * current_function_func_begin_label;
138 
139 /* A random sequence of characters, unless overridden by user.  */
140 static const char *flag_random_seed;
141 
142 /* A local time stamp derived from the time of compilation. It will be
143    zero if the system cannot provide a time.  It will be -1u, if the
144    user has specified a particular random seed.  */
145 unsigned local_tick;
146 
147 /* Random number for this compilation */
148 HOST_WIDE_INT random_seed;
149 
150 /* -f flags.  */
151 
152 /* When non-NULL, indicates that whenever space is allocated on the
153    stack, the resulting stack pointer must not pass this
154    address---that is, for stacks that grow downward, the stack pointer
155    must always be greater than or equal to this address; for stacks
156    that grow upward, the stack pointer must be less than this address.
157    At present, the rtx may be either a REG or a SYMBOL_REF, although
158    the support provided depends on the backend.  */
159 rtx stack_limit_rtx;
160 
161 class target_flag_state default_target_flag_state;
162 #if SWITCHABLE_TARGET
163 class target_flag_state *this_target_flag_state = &default_target_flag_state;
164 #else
165 #define this_target_flag_state (&default_target_flag_state)
166 #endif
167 
168 /* The user symbol prefix after having resolved same.  */
169 const char *user_label_prefix;
170 
171 /* Output files for assembler code (real compiler output)
172    and debugging dumps.  */
173 
174 FILE *asm_out_file;
175 FILE *aux_info_file;
176 FILE *callgraph_info_file = NULL;
177 static bitmap callgraph_info_external_printed;
178 FILE *stack_usage_file = NULL;
179 
180 /* The current working directory of a translation.  It's generally the
181    directory from which compilation was initiated, but a preprocessed
182    file may specify the original directory in which it was
183    created.  */
184 
185 static const char *src_pwd;
186 
187 /* Initialize src_pwd with the given string, and return true.  If it
188    was already initialized, return false.  As a special case, it may
189    be called with a NULL argument to test whether src_pwd has NOT been
190    initialized yet.  */
191 
192 bool
set_src_pwd(const char * pwd)193 set_src_pwd (const char *pwd)
194 {
195   if (src_pwd)
196     {
197       if (strcmp (src_pwd, pwd) == 0)
198 	return true;
199       else
200 	return false;
201     }
202 
203   src_pwd = xstrdup (pwd);
204   return true;
205 }
206 
207 /* Return the directory from which the translation unit was initiated,
208    in case set_src_pwd() was not called before to assign it a
209    different value.  */
210 
211 const char *
get_src_pwd(void)212 get_src_pwd (void)
213 {
214   if (! src_pwd)
215     {
216       src_pwd = getpwd ();
217       if (!src_pwd)
218 	src_pwd = ".";
219     }
220 
221    return src_pwd;
222 }
223 
224 /* Called when the start of a function definition is parsed,
225    this function prints on stderr the name of the function.  */
226 void
announce_function(tree decl)227 announce_function (tree decl)
228 {
229   if (!quiet_flag)
230     {
231       if (rtl_dump_and_exit)
232 	fprintf (stderr, "%s ",
233 		 identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))));
234       else
235 	fprintf (stderr, " %s",
236 		 identifier_to_locale (lang_hooks.decl_printable_name (decl, 2)));
237       fflush (stderr);
238       pp_needs_newline (global_dc->printer) = true;
239       diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
240     }
241 }
242 
243 /* Initialize local_tick with the time of day, or -1 if
244    flag_random_seed is set.  */
245 
246 static void
init_local_tick(void)247 init_local_tick (void)
248 {
249   if (!flag_random_seed)
250     {
251 #ifdef HAVE_GETTIMEOFDAY
252       {
253 	struct timeval tv;
254 
255 	gettimeofday (&tv, NULL);
256 	local_tick = (unsigned) tv.tv_sec * 1000 + tv.tv_usec / 1000;
257       }
258 #else
259       {
260 	time_t now = time (NULL);
261 
262 	if (now != (time_t)-1)
263 	  local_tick = (unsigned) now;
264       }
265 #endif
266     }
267   else
268     local_tick = -1;
269 }
270 
271 /* Obtain the random_seed.  Unless NOINIT, initialize it if
272    it's not provided in the command line.  */
273 
274 HOST_WIDE_INT
get_random_seed(bool noinit)275 get_random_seed (bool noinit)
276 {
277   if (!random_seed && !noinit)
278     {
279       int fd = open ("/dev/urandom", O_RDONLY);
280       if (fd >= 0)
281         {
282           if (read (fd, &random_seed, sizeof (random_seed))
283               != sizeof (random_seed))
284             random_seed = 0;
285           close (fd);
286         }
287       if (!random_seed)
288 	random_seed = local_tick ^ getpid ();
289     }
290   return random_seed;
291 }
292 
293 /* Set flag_random_seed to VAL, and if non-null, reinitialize random_seed.  */
294 
295 void
set_random_seed(const char * val)296 set_random_seed (const char *val)
297 {
298   flag_random_seed = val;
299   if (flag_random_seed)
300     {
301       char *endp;
302 
303       /* When the driver passed in a hex number don't crc it again */
304       random_seed = strtoul (flag_random_seed, &endp, 0);
305       if (!(endp > flag_random_seed && *endp == 0))
306         random_seed = crc32_string (0, flag_random_seed);
307     }
308 }
309 
310 /* Handler for fatal signals, such as SIGSEGV.  These are transformed
311    into ICE messages, which is much more user friendly.  In case the
312    error printer crashes, reset the signal to prevent infinite recursion.  */
313 
314 static void
crash_signal(int signo)315 crash_signal (int signo)
316 {
317   signal (signo, SIG_DFL);
318 
319   /* If we crashed while processing an ASM statement, then be a little more
320      graceful.  It's most likely the user's fault.  */
321   if (this_is_asm_operands)
322     {
323       output_operand_lossage ("unrecoverable error");
324       exit (FATAL_EXIT_CODE);
325     }
326 
327   internal_error ("%s", strsignal (signo));
328 }
329 
330 /* A subroutine of wrapup_global_declarations.  We've come to the end of
331    the compilation unit.  All deferred variables should be undeferred,
332    and all incomplete decls should be finalized.  */
333 
334 void
wrapup_global_declaration_1(tree decl)335 wrapup_global_declaration_1 (tree decl)
336 {
337   /* We're not deferring this any longer.  Assignment is conditional to
338      avoid needlessly dirtying PCH pages.  */
339   if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
340       && DECL_DEFER_OUTPUT (decl) != 0)
341     DECL_DEFER_OUTPUT (decl) = 0;
342 
343   if (VAR_P (decl) && DECL_SIZE (decl) == 0)
344     lang_hooks.finish_incomplete_decl (decl);
345 }
346 
347 /* A subroutine of wrapup_global_declarations.  Decide whether or not DECL
348    needs to be output.  Return true if it is output.  */
349 
350 bool
wrapup_global_declaration_2(tree decl)351 wrapup_global_declaration_2 (tree decl)
352 {
353   if (TREE_ASM_WRITTEN (decl) || DECL_EXTERNAL (decl)
354       || (VAR_P (decl) && DECL_HAS_VALUE_EXPR_P (decl)))
355     return false;
356 
357   /* Don't write out static consts, unless we still need them.
358 
359      We also keep static consts if not optimizing (for debugging),
360      unless the user specified -fno-keep-static-consts.
361      ??? They might be better written into the debug information.
362      This is possible when using DWARF.
363 
364      A language processor that wants static constants to be always
365      written out (even if it is not used) is responsible for
366      calling rest_of_decl_compilation itself.  E.g. the C front-end
367      calls rest_of_decl_compilation from finish_decl.
368      One motivation for this is that is conventional in some
369      environments to write things like:
370      static const char rcsid[] = "... version string ...";
371      intending to force the string to be in the executable.
372 
373      A language processor that would prefer to have unneeded
374      static constants "optimized away" would just defer writing
375      them out until here.  E.g. C++ does this, because static
376      constants are often defined in header files.
377 
378      ??? A tempting alternative (for both C and C++) would be
379      to force a constant to be written if and only if it is
380      defined in a main file, as opposed to an include file.  */
381 
382   if (VAR_P (decl) && TREE_STATIC (decl))
383     {
384       varpool_node *node;
385       bool needed = true;
386       node = varpool_node::get (decl);
387 
388       if (!node && flag_ltrans)
389 	needed = false;
390       else if (node && node->definition)
391 	needed = false;
392       else if (node && node->alias)
393 	needed = false;
394       else if (!symtab->global_info_ready
395 	       && (TREE_USED (decl)
396 		   || TREE_USED (DECL_ASSEMBLER_NAME (decl))))
397 	/* needed */;
398       else if (node && node->analyzed)
399 	/* needed */;
400       else if (DECL_COMDAT (decl))
401 	needed = false;
402       else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
403 	       && (optimize || !flag_keep_static_consts
404 		   || DECL_ARTIFICIAL (decl)))
405 	needed = false;
406 
407       if (needed)
408 	{
409 	  rest_of_decl_compilation (decl, 1, 1);
410 	  return true;
411 	}
412     }
413 
414   return false;
415 }
416 
417 /* Do any final processing required for the declarations in VEC, of
418    which there are LEN.  We write out inline functions and variables
419    that have been deferred until this point, but which are required.
420    Returns nonzero if anything was put out.  */
421 
422 bool
wrapup_global_declarations(tree * vec,int len)423 wrapup_global_declarations (tree *vec, int len)
424 {
425   bool reconsider, output_something = false;
426   int i;
427 
428   for (i = 0; i < len; i++)
429     wrapup_global_declaration_1 (vec[i]);
430 
431   /* Now emit any global variables or functions that we have been
432      putting off.  We need to loop in case one of the things emitted
433      here references another one which comes earlier in the list.  */
434   do
435     {
436       reconsider = false;
437       for (i = 0; i < len; i++)
438 	reconsider |= wrapup_global_declaration_2 (vec[i]);
439       if (reconsider)
440 	output_something = true;
441     }
442   while (reconsider);
443 
444   return output_something;
445 }
446 
447 /* Compile an entire translation unit.  Write a file of assembly
448    output and various debugging dumps.  */
449 
450 static void
compile_file(void)451 compile_file (void)
452 {
453   timevar_start (TV_PHASE_PARSING);
454   timevar_push (TV_PARSE_GLOBAL);
455 
456   /* Parse entire file and generate initial debug information.  */
457   lang_hooks.parse_file ();
458 
459   timevar_pop (TV_PARSE_GLOBAL);
460   timevar_stop (TV_PHASE_PARSING);
461 
462   if (flag_dump_locations)
463     dump_location_info (stderr);
464 
465   /* Compilation is now finished except for writing
466      what's left of the symbol table output.  */
467 
468   if (flag_syntax_only || flag_wpa)
469     return;
470 
471   /* Reset maximum_field_alignment, it can be adjusted by #pragma pack
472      and this shouldn't influence any types built by the middle-end
473      from now on (like gcov_info_type).  */
474   maximum_field_alignment = initial_max_fld_align * BITS_PER_UNIT;
475 
476   ggc_protect_identifiers = false;
477 
478   /* Run the actual compilation process.  */
479   if (!in_lto_p)
480     {
481       timevar_start (TV_PHASE_OPT_GEN);
482       symtab->finalize_compilation_unit ();
483       timevar_stop (TV_PHASE_OPT_GEN);
484     }
485 
486   /* Perform any post compilation-proper parser cleanups and
487      processing.  This is currently only needed for the C++ parser,
488      which can be hopefully cleaned up so this hook is no longer
489      necessary.  */
490   if (lang_hooks.decls.post_compilation_parsing_cleanups)
491     lang_hooks.decls.post_compilation_parsing_cleanups ();
492 
493   dump_context::get ().finish_any_json_writer ();
494 
495   if (seen_error ())
496     return;
497 
498   timevar_start (TV_PHASE_LATE_ASM);
499 
500   /* Compilation unit is finalized.  When producing non-fat LTO object, we are
501      basically finished.  */
502   if ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
503       || !flag_lto || flag_fat_lto_objects)
504     {
505       /* File-scope initialization for AddressSanitizer.  */
506       if (flag_sanitize & SANITIZE_ADDRESS)
507         asan_finish_file ();
508 
509       if (flag_sanitize & SANITIZE_THREAD)
510 	tsan_finish_file ();
511 
512       if (gate_hwasan ())
513 	hwasan_finish_file ();
514 
515       omp_finish_file ();
516 
517       output_shared_constant_pool ();
518       output_object_blocks ();
519       finish_tm_clone_pairs ();
520 
521       /* Write out any pending weak symbol declarations.  */
522       weak_finish ();
523 
524       /* This must be at the end before unwind and debug info.
525 	 Some target ports emit PIC setup thunks here.  */
526       targetm.asm_out.code_end ();
527 
528       /* Do dbx symbols.  */
529       timevar_push (TV_SYMOUT);
530 
531 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
532       dwarf2out_frame_finish ();
533 #endif
534 
535       debuginfo_start ();
536       (*debug_hooks->finish) (main_input_filename);
537       debuginfo_stop ();
538       timevar_pop (TV_SYMOUT);
539 
540       /* Output some stuff at end of file if nec.  */
541 
542       dw2_output_indirect_constants ();
543 
544       /* Flush any pending external directives.  */
545       process_pending_assemble_externals ();
546    }
547 
548   /* Let linker plugin know that this is a slim object and must be LTOed
549      even when user did not ask for it.  */
550   if (flag_generate_lto && !flag_fat_lto_objects)
551     {
552 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
553       ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, NULL_TREE, "__gnu_lto_slim",
554 				      HOST_WIDE_INT_1U, 8);
555 #elif defined ASM_OUTPUT_ALIGNED_COMMON
556       ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, "__gnu_lto_slim",
557 				 HOST_WIDE_INT_1U, 8);
558 #else
559       ASM_OUTPUT_COMMON (asm_out_file, "__gnu_lto_slim",
560 			 HOST_WIDE_INT_1U,
561 			 HOST_WIDE_INT_1U);
562 #endif
563     }
564 
565   /* Attach a special .ident directive to the end of the file to identify
566      the version of GCC which compiled this code.  The format of the .ident
567      string is patterned after the ones produced by native SVR4 compilers.  */
568   if (!flag_no_ident)
569     {
570       const char *pkg_version = "(GNU) ";
571       char *ident_str;
572 
573       if (strcmp ("(GCC) ", pkgversion_string))
574 	pkg_version = pkgversion_string;
575 
576       ident_str = ACONCAT (("GCC: ", pkg_version, version_string, NULL));
577       targetm.asm_out.output_ident (ident_str);
578     }
579 
580   /* Auto profile finalization. */
581   if (flag_auto_profile)
582     end_auto_profile ();
583 
584   /* Invoke registered plugin callbacks.  */
585   invoke_plugin_callbacks (PLUGIN_FINISH_UNIT, NULL);
586 
587   /* This must be at the end.  Some target ports emit end of file directives
588      into the assembly file here, and hence we cannot output anything to the
589      assembly file after this point.  */
590   targetm.asm_out.file_end ();
591 
592   timevar_stop (TV_PHASE_LATE_ASM);
593 }
594 
595 /* Print version information to FILE.
596    Each line begins with INDENT (for the case where FILE is the
597    assembler output file).
598 
599    If SHOW_GLOBAL_STATE is true (for cc1 etc), we are within the compiler
600    proper and can print pertinent state (e.g. params and plugins).
601 
602    If SHOW_GLOBAL_STATE is false (for use by libgccjit), we are outside the
603    compiler, and we don't hold the mutex on the compiler's global state:
604    we can't print params and plugins, since they might not be initialized,
605    or might be being manipulated by a compile running in another
606    thread.  */
607 
608 void
print_version(FILE * file,const char * indent,bool show_global_state)609 print_version (FILE *file, const char *indent, bool show_global_state)
610 {
611   static const char fmt1[] =
612 #ifdef __GNUC__
613     N_("%s%s%s %sversion %s (%s)\n%s\tcompiled by GNU C version %s, ")
614 #else
615     N_("%s%s%s %sversion %s (%s) compiled by CC, ")
616 #endif
617     ;
618   static const char fmt2[] =
619     N_("GMP version %s, MPFR version %s, MPC version %s, isl version %s\n");
620   static const char fmt3[] =
621     N_("%s%swarning: %s header version %s differs from library version %s.\n");
622   static const char fmt4[] =
623     N_("%s%sGGC heuristics: --param ggc-min-expand=%d --param ggc-min-heapsize=%d\n");
624 #ifndef __VERSION__
625 #define __VERSION__ "[?]"
626 #endif
627   fprintf (file,
628 	   file == stderr ? _(fmt1) : fmt1,
629 	   indent, *indent != 0 ? " " : "",
630 	   lang_hooks.name, pkgversion_string, version_string, TARGET_NAME,
631 	   indent, __VERSION__);
632 
633   /* We need to stringify the GMP macro values.  Ugh, gmp_version has
634      two string formats, "i.j.k" and "i.j" when k is zero.  As of
635      gmp-4.3.0, GMP always uses the 3 number format.  */
636 #define GCC_GMP_STRINGIFY_VERSION3(X) #X
637 #define GCC_GMP_STRINGIFY_VERSION2(X) GCC_GMP_STRINGIFY_VERSION3 (X)
638 #define GCC_GMP_VERSION_NUM(X,Y,Z) (((X) << 16L) | ((Y) << 8) | (Z))
639 #define GCC_GMP_VERSION \
640   GCC_GMP_VERSION_NUM(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL)
641 #if GCC_GMP_VERSION < GCC_GMP_VERSION_NUM(4,3,0) && __GNU_MP_VERSION_PATCHLEVEL == 0
642 #define GCC_GMP_STRINGIFY_VERSION \
643   GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION) "." \
644   GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION_MINOR)
645 #else
646 #define GCC_GMP_STRINGIFY_VERSION \
647   GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION) "." \
648   GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION_MINOR) "." \
649   GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION_PATCHLEVEL)
650 #endif
651   fprintf (file,
652 	   file == stderr ? _(fmt2) : fmt2,
653 	   GCC_GMP_STRINGIFY_VERSION, MPFR_VERSION_STRING, MPC_VERSION_STRING,
654 #ifndef HAVE_isl
655 	   "none"
656 #else
657 	   isl_version ()
658 #endif
659 	   );
660   if (strcmp (GCC_GMP_STRINGIFY_VERSION, gmp_version))
661     fprintf (file,
662 	     file == stderr ? _(fmt3) : fmt3,
663 	     indent, *indent != 0 ? " " : "",
664 	     "GMP", GCC_GMP_STRINGIFY_VERSION, gmp_version);
665   if (strcmp (MPFR_VERSION_STRING, mpfr_get_version ()))
666     fprintf (file,
667 	     file == stderr ? _(fmt3) : fmt3,
668 	     indent, *indent != 0 ? " " : "",
669 	     "MPFR", MPFR_VERSION_STRING, mpfr_get_version ());
670   if (strcmp (MPC_VERSION_STRING, mpc_get_version ()))
671     fprintf (file,
672 	     file == stderr ? _(fmt3) : fmt3,
673 	     indent, *indent != 0 ? " " : "",
674 	     "MPC", MPC_VERSION_STRING, mpc_get_version ());
675 
676   if (show_global_state)
677     {
678       fprintf (file,
679 	       file == stderr ? _(fmt4) : fmt4,
680 	       indent, *indent != 0 ? " " : "",
681 	       param_ggc_min_expand, param_ggc_min_heapsize);
682 
683       print_plugins_versions (file, indent);
684     }
685 }
686 
687 
688 
689 /* Open assembly code output file.  Do this even if -fsyntax-only is
690    on, because then the driver will have provided the name of a
691    temporary file or bit bucket for us.  NAME is the file specified on
692    the command line, possibly NULL.  */
693 static void
init_asm_output(const char * name)694 init_asm_output (const char *name)
695 {
696   if (name == NULL && asm_file_name == 0)
697     asm_out_file = stdout;
698   else
699     {
700       if (asm_file_name == 0)
701 	{
702 	  int len = strlen (dump_base_name);
703 	  char *dumpname = XNEWVEC (char, len + 6);
704 
705 	  memcpy (dumpname, dump_base_name, len + 1);
706 	  strip_off_ending (dumpname, len);
707 	  strcat (dumpname, ".s");
708 	  asm_file_name = dumpname;
709 	}
710       if (!strcmp (asm_file_name, "-"))
711 	asm_out_file = stdout;
712       else if (!canonical_filename_eq (asm_file_name, name)
713 	       || !strcmp (asm_file_name, HOST_BIT_BUCKET))
714 	asm_out_file = fopen (asm_file_name, "w");
715       else
716 	/* Use UNKOWN_LOCATION to prevent gcc from printing the first
717 	   line in the current file. */
718 	fatal_error (UNKNOWN_LOCATION,
719 		     "input file %qs is the same as output file",
720 		     asm_file_name);
721       if (asm_out_file == 0)
722 	fatal_error (UNKNOWN_LOCATION,
723 		     "cannot open %qs for writing: %m", asm_file_name);
724     }
725 
726   if (!flag_syntax_only)
727     {
728       targetm.asm_out.file_start ();
729 
730       if (flag_record_gcc_switches)
731 	{
732 	  if (targetm.asm_out.record_gcc_switches)
733 	    {
734 	      const char *str
735 		= gen_producer_string (lang_hooks.name,
736 				       save_decoded_options,
737 				       save_decoded_options_count);
738 	      targetm.asm_out.record_gcc_switches (str);
739 	    }
740 	  else
741 	    inform (UNKNOWN_LOCATION,
742 		    "%<-frecord-gcc-switches%> is not supported by "
743 		    "the current target");
744 	}
745 
746       if (flag_verbose_asm)
747 	{
748 	  print_version (asm_out_file, ASM_COMMENT_START, true);
749 	  fputs (ASM_COMMENT_START, asm_out_file);
750 	  fputs (" options passed: ", asm_out_file);
751 	  char *cmdline = gen_command_line_string (save_decoded_options,
752 						   save_decoded_options_count);
753 	  fputs (cmdline, asm_out_file);
754 	  free (cmdline);
755 	  fputc ('\n', asm_out_file);
756 	}
757     }
758 }
759 
760 /* A helper function; used as the reallocator function for cpp's line
761    table.  */
762 static void *
realloc_for_line_map(void * ptr,size_t len)763 realloc_for_line_map (void *ptr, size_t len)
764 {
765   return ggc_realloc (ptr, len);
766 }
767 
768 /* A helper function: used as the allocator function for
769    identifier_to_locale.  */
770 static void *
alloc_for_identifier_to_locale(size_t len)771 alloc_for_identifier_to_locale (size_t len)
772 {
773   return ggc_alloc_atomic (len);
774 }
775 
776 /* Output stack usage information.  */
777 static void
output_stack_usage_1(FILE * cf)778 output_stack_usage_1 (FILE *cf)
779 {
780   static bool warning_issued = false;
781   enum stack_usage_kind_type { STATIC = 0, DYNAMIC, DYNAMIC_BOUNDED };
782   const char *stack_usage_kind_str[] = {
783     "static",
784     "dynamic",
785     "dynamic,bounded"
786   };
787   HOST_WIDE_INT stack_usage = current_function_static_stack_size;
788   enum stack_usage_kind_type stack_usage_kind;
789 
790   if (stack_usage < 0)
791     {
792       if (!warning_issued)
793 	{
794 	  warning (0, "stack usage computation not supported for this target");
795 	  warning_issued = true;
796 	}
797       return;
798     }
799 
800   stack_usage_kind = STATIC;
801 
802   /* Add the maximum amount of space pushed onto the stack.  */
803   if (maybe_ne (current_function_pushed_stack_size, 0))
804     {
805       HOST_WIDE_INT extra;
806       if (current_function_pushed_stack_size.is_constant (&extra))
807 	{
808 	  stack_usage += extra;
809 	  stack_usage_kind = DYNAMIC_BOUNDED;
810 	}
811       else
812 	{
813 	  extra = constant_lower_bound (current_function_pushed_stack_size);
814 	  stack_usage += extra;
815 	  stack_usage_kind = DYNAMIC;
816 	}
817     }
818 
819   /* Now on to the tricky part: dynamic stack allocation.  */
820   if (current_function_allocates_dynamic_stack_space)
821     {
822       if (stack_usage_kind != DYNAMIC)
823 	{
824 	  if (current_function_has_unbounded_dynamic_stack_size)
825 	    stack_usage_kind = DYNAMIC;
826 	  else
827 	    stack_usage_kind = DYNAMIC_BOUNDED;
828 	}
829 
830       /* Add the size even in the unbounded case, this can't hurt.  */
831       stack_usage += current_function_dynamic_stack_size;
832     }
833 
834   if (cf && flag_callgraph_info & CALLGRAPH_INFO_STACK_USAGE)
835     fprintf (cf, "\\n" HOST_WIDE_INT_PRINT_DEC " bytes (%s)",
836 	     stack_usage,
837 	     stack_usage_kind_str[stack_usage_kind]);
838 
839   if (stack_usage_file)
840     {
841       print_decl_identifier (stack_usage_file, current_function_decl,
842 			     PRINT_DECL_ORIGIN | PRINT_DECL_NAME);
843       fprintf (stack_usage_file, "\t" HOST_WIDE_INT_PRINT_DEC"\t%s\n",
844 	       stack_usage, stack_usage_kind_str[stack_usage_kind]);
845     }
846 
847   if (warn_stack_usage >= 0 && warn_stack_usage < HOST_WIDE_INT_MAX)
848     {
849       const location_t loc = DECL_SOURCE_LOCATION (current_function_decl);
850 
851       if (stack_usage_kind == DYNAMIC)
852 	warning_at (loc, OPT_Wstack_usage_, "stack usage might be unbounded");
853       else if (stack_usage > warn_stack_usage)
854 	{
855 	  if (stack_usage_kind == DYNAMIC_BOUNDED)
856 	    warning_at (loc,
857 			OPT_Wstack_usage_, "stack usage might be %wu bytes",
858 			stack_usage);
859 	  else
860 	    warning_at (loc, OPT_Wstack_usage_, "stack usage is %wu bytes",
861 			stack_usage);
862 	}
863     }
864 }
865 
866 /* Dump placeholder node for indirect calls in VCG format.  */
867 
868 #define INDIRECT_CALL_NAME  "__indirect_call"
869 
870 static void
dump_final_node_vcg_start(FILE * f,tree decl)871 dump_final_node_vcg_start (FILE *f, tree decl)
872 {
873   fputs ("node: { title: \"", f);
874   if (decl)
875     print_decl_identifier (f, decl, PRINT_DECL_UNIQUE_NAME);
876   else
877     fputs (INDIRECT_CALL_NAME, f);
878   fputs ("\" label: \"", f);
879   if (decl)
880     {
881       print_decl_identifier (f, decl, PRINT_DECL_NAME);
882       fputs ("\\n", f);
883       print_decl_identifier (f, decl, PRINT_DECL_ORIGIN);
884     }
885   else
886     fputs ("Indirect Call Placeholder", f);
887 }
888 
889 /* Dump final cgraph edge in VCG format.  */
890 
891 static void
dump_final_callee_vcg(FILE * f,location_t location,tree callee)892 dump_final_callee_vcg (FILE *f, location_t location, tree callee)
893 {
894   if ((!callee || DECL_EXTERNAL (callee))
895       && bitmap_set_bit (callgraph_info_external_printed,
896 			 callee ? DECL_UID (callee) + 1 : 0))
897     {
898       dump_final_node_vcg_start (f, callee);
899       fputs ("\" shape : ellipse }\n", f);
900     }
901 
902   fputs ("edge: { sourcename: \"", f);
903   print_decl_identifier (f, current_function_decl, PRINT_DECL_UNIQUE_NAME);
904   fputs ("\" targetname: \"", f);
905   if (callee)
906     print_decl_identifier (f, callee, PRINT_DECL_UNIQUE_NAME);
907   else
908     fputs (INDIRECT_CALL_NAME, f);
909   if (LOCATION_LOCUS (location) != UNKNOWN_LOCATION)
910     {
911       expanded_location loc;
912       fputs ("\" label: \"", f);
913       loc = expand_location (location);
914       fprintf (f, "%s:%d:%d", loc.file, loc.line, loc.column);
915     }
916   fputs ("\" }\n", f);
917 }
918 
919 /* Dump final cgraph node in VCG format.  */
920 
921 static void
dump_final_node_vcg(FILE * f)922 dump_final_node_vcg (FILE *f)
923 {
924   dump_final_node_vcg_start (f, current_function_decl);
925 
926   if (flag_stack_usage_info
927       || (flag_callgraph_info & CALLGRAPH_INFO_STACK_USAGE))
928     output_stack_usage_1 (f);
929 
930   if (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC)
931     {
932       fprintf (f, "\\n%u dynamic objects", vec_safe_length (cfun->su->dallocs));
933 
934       unsigned i;
935       callinfo_dalloc *cda;
936       FOR_EACH_VEC_SAFE_ELT (cfun->su->dallocs, i, cda)
937 	{
938 	  expanded_location loc = expand_location (cda->location);
939 	  fprintf (f, "\\n %s", cda->name);
940 	  fprintf (f, " %s:%d:%d", loc.file, loc.line, loc.column);
941 	}
942 
943       vec_free (cfun->su->dallocs);
944       cfun->su->dallocs = NULL;
945     }
946 
947   fputs ("\" }\n", f);
948 
949   unsigned i;
950   callinfo_callee *c;
951   FOR_EACH_VEC_SAFE_ELT (cfun->su->callees, i, c)
952     dump_final_callee_vcg (f, c->location, c->decl);
953   vec_free (cfun->su->callees);
954   cfun->su->callees = NULL;
955 }
956 
957 /* Output stack usage and callgraph info, as requested.  */
958 void
output_stack_usage(void)959 output_stack_usage (void)
960 {
961   if (flag_callgraph_info)
962     dump_final_node_vcg (callgraph_info_file);
963   else
964     output_stack_usage_1 (NULL);
965 }
966 
967 /* Open an auxiliary output file.  */
968 static FILE *
open_auxiliary_file(const char * ext)969 open_auxiliary_file (const char *ext)
970 {
971   char *filename;
972   FILE *file;
973 
974   filename = concat (aux_base_name, ".", ext, NULL);
975   file = fopen (filename, "w");
976   if (!file)
977     fatal_error (input_location, "cannot open %s for writing: %m", filename);
978   free (filename);
979   return file;
980 }
981 
982 /* Alternative diagnostics callback for reentered ICE reporting.  */
983 
984 static void
internal_error_reentered(diagnostic_context *,const char *,va_list *)985 internal_error_reentered (diagnostic_context *, const char *, va_list *)
986 {
987   /* Flush the dump file if emergency_dump_function itself caused an ICE.  */
988   if (dump_file)
989     fflush (dump_file);
990 }
991 
992 /* Auxiliary callback for the diagnostics code.  */
993 
994 static void
internal_error_function(diagnostic_context *,const char *,va_list *)995 internal_error_function (diagnostic_context *, const char *, va_list *)
996 {
997   global_dc->internal_error = internal_error_reentered;
998   warn_if_plugins ();
999   emergency_dump_function ();
1000 }
1001 
1002 /* Initialization of the front end environment, before command line
1003    options are parsed.  Signal handlers, internationalization etc.
1004    ARGV0 is main's argv[0].  */
1005 static void
general_init(const char * argv0,bool init_signals)1006 general_init (const char *argv0, bool init_signals)
1007 {
1008   const char *p;
1009 
1010   p = argv0 + strlen (argv0);
1011   while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
1012     --p;
1013   progname = p;
1014 
1015   xmalloc_set_program_name (progname);
1016 
1017   hex_init ();
1018 
1019   /* Unlock the stdio streams.  */
1020   unlock_std_streams ();
1021 
1022   gcc_init_libintl ();
1023 
1024   identifier_to_locale_alloc = alloc_for_identifier_to_locale;
1025   identifier_to_locale_free = ggc_free;
1026 
1027   /* Initialize the diagnostics reporting machinery, so option parsing
1028      can give warnings and errors.  */
1029   diagnostic_initialize (global_dc, N_OPTS);
1030   global_dc->lang_mask = lang_hooks.option_lang_mask ();
1031   /* Set a default printer.  Language specific initializations will
1032      override it later.  */
1033   tree_diagnostics_defaults (global_dc);
1034 
1035   global_dc->show_caret
1036     = global_options_init.x_flag_diagnostics_show_caret;
1037   global_dc->show_labels_p
1038     = global_options_init.x_flag_diagnostics_show_labels;
1039   global_dc->show_line_numbers_p
1040     = global_options_init.x_flag_diagnostics_show_line_numbers;
1041   global_dc->show_cwe
1042     = global_options_init.x_flag_diagnostics_show_cwe;
1043   global_dc->path_format
1044     = (enum diagnostic_path_format)global_options_init.x_flag_diagnostics_path_format;
1045   global_dc->show_path_depths
1046     = global_options_init.x_flag_diagnostics_show_path_depths;
1047   global_dc->show_option_requested
1048     = global_options_init.x_flag_diagnostics_show_option;
1049   global_dc->min_margin_width
1050     = global_options_init.x_diagnostics_minimum_margin_width;
1051   global_dc->show_column
1052     = global_options_init.x_flag_show_column;
1053   global_dc->internal_error = internal_error_function;
1054   global_dc->option_enabled = option_enabled;
1055   global_dc->option_state = &global_options;
1056   global_dc->option_name = option_name;
1057   global_dc->get_option_url = get_option_url;
1058 
1059   if (init_signals)
1060     {
1061       /* Trap fatal signals, e.g. SIGSEGV, and convert them to ICE messages.  */
1062 #ifdef SIGSEGV
1063       signal (SIGSEGV, crash_signal);
1064 #endif
1065 #ifdef SIGILL
1066       signal (SIGILL, crash_signal);
1067 #endif
1068 #ifdef SIGBUS
1069       signal (SIGBUS, crash_signal);
1070 #endif
1071 #ifdef SIGABRT
1072       signal (SIGABRT, crash_signal);
1073 #endif
1074 #if defined SIGIOT && (!defined SIGABRT || SIGABRT != SIGIOT)
1075       signal (SIGIOT, crash_signal);
1076 #endif
1077 #ifdef SIGFPE
1078       signal (SIGFPE, crash_signal);
1079 #endif
1080 
1081       /* Other host-specific signal setup.  */
1082       (*host_hooks.extra_signals)();
1083   }
1084 
1085   /* Initialize the garbage-collector, string pools and tree type hash
1086      table.  */
1087   init_ggc ();
1088   init_stringpool ();
1089   input_location = UNKNOWN_LOCATION;
1090   line_table = ggc_alloc<line_maps> ();
1091   linemap_init (line_table, BUILTINS_LOCATION);
1092   line_table->reallocator = realloc_for_line_map;
1093   line_table->round_alloc_size = ggc_round_alloc_size;
1094   line_table->default_range_bits = 5;
1095   init_ttree ();
1096 
1097   /* Initialize register usage now so switches may override.  */
1098   init_reg_sets ();
1099 
1100   /* Create the singleton holder for global state.  This creates the
1101      dump manager.  */
1102   g = new gcc::context ();
1103 
1104   /* Allow languages and middle-end to register their dumps before the
1105      optimization passes.  */
1106   g->get_dumps ()->register_dumps ();
1107 
1108   /* Create the passes.  */
1109   g->set_passes (new gcc::pass_manager (g));
1110 
1111   symtab = new (ggc_alloc <symbol_table> ()) symbol_table ();
1112 
1113   statistics_early_init ();
1114   debuginfo_early_init ();
1115 }
1116 
1117 /* Return true if the current target supports -fsection-anchors.  */
1118 
1119 static bool
target_supports_section_anchors_p(void)1120 target_supports_section_anchors_p (void)
1121 {
1122   if (targetm.min_anchor_offset == 0 && targetm.max_anchor_offset == 0)
1123     return false;
1124 
1125   if (targetm.asm_out.output_anchor == NULL)
1126     return false;
1127 
1128   return true;
1129 }
1130 
1131 /* Parse "N[:M][:...]" into struct align_flags A.
1132    VALUES contains parsed values (in reverse order), all processed
1133    values are popped.  */
1134 
1135 static void
read_log_maxskip(auto_vec<unsigned> & values,align_flags_tuple * a)1136 read_log_maxskip (auto_vec<unsigned> &values, align_flags_tuple *a)
1137 {
1138   unsigned n = values.pop ();
1139   if (n != 0)
1140     a->log = floor_log2 (n * 2 - 1);
1141 
1142   if (values.is_empty ())
1143     a->maxskip = n ? n - 1 : 0;
1144   else
1145     {
1146       unsigned m = values.pop ();
1147       /* -falign-foo=N:M means M-1 max bytes of padding, not M.  */
1148       if (m > 0)
1149 	m--;
1150       a->maxskip = m;
1151     }
1152 
1153   /* Normalize the tuple.  */
1154   a->normalize ();
1155 }
1156 
1157 /* Parse "N[:M[:N2[:M2]]]" string FLAG into a pair of struct align_flags.  */
1158 
1159 static void
parse_N_M(const char * flag,align_flags & a)1160 parse_N_M (const char *flag, align_flags &a)
1161 {
1162   if (flag)
1163     {
1164       static hash_map <nofree_string_hash, align_flags> cache;
1165       align_flags *entry = cache.get (flag);
1166       if (entry)
1167 	{
1168 	  a = *entry;
1169 	  return;
1170 	}
1171 
1172       auto_vec<unsigned> result_values;
1173       bool r = parse_and_check_align_values (flag, NULL, result_values, false,
1174 					     UNKNOWN_LOCATION);
1175       if (!r)
1176 	return;
1177 
1178       /* Reverse values for easier manipulation.  */
1179       result_values.reverse ();
1180 
1181       read_log_maxskip (result_values, &a.levels[0]);
1182       if (!result_values.is_empty ())
1183 	read_log_maxskip (result_values, &a.levels[1]);
1184 #ifdef SUBALIGN_LOG
1185       else
1186 	{
1187 	  /* N2[:M2] is not specified.  This arch has a default for N2.
1188 	     Before -falign-foo=N:M:N2:M2 was introduced, x86 had a tweak.
1189 	     -falign-functions=N with N > 8 was adding secondary alignment.
1190 	     -falign-functions=10 was emitting this before every function:
1191 			.p2align 4,,9
1192 			.p2align 3
1193 	     Now this behavior (and more) can be explicitly requested:
1194 	     -falign-functions=16:10:8
1195 	     Retain old behavior if N2 is missing: */
1196 
1197 	  int align = 1 << a.levels[0].log;
1198 	  int subalign = 1 << SUBALIGN_LOG;
1199 
1200 	  if (a.levels[0].log > SUBALIGN_LOG
1201 	      && a.levels[0].maxskip >= subalign - 1)
1202 	    {
1203 	      /* Set N2 unless subalign can never have any effect.  */
1204 	      if (align > a.levels[0].maxskip + 1)
1205 		{
1206 		  a.levels[1].log = SUBALIGN_LOG;
1207 		  a.levels[1].normalize ();
1208 		}
1209 	    }
1210 	}
1211 #endif
1212 
1213       /* Cache seen value.  */
1214       cache.put (flag, a);
1215     }
1216 }
1217 
1218 /* Process -falign-foo=N[:M[:N2[:M2]]] options.  */
1219 
1220 void
parse_alignment_opts(void)1221 parse_alignment_opts (void)
1222 {
1223   parse_N_M (str_align_loops, align_loops);
1224   parse_N_M (str_align_jumps, align_jumps);
1225   parse_N_M (str_align_labels, align_labels);
1226   parse_N_M (str_align_functions, align_functions);
1227 }
1228 
1229 /* Process the options that have been parsed.  */
1230 static void
process_options(void)1231 process_options (void)
1232 {
1233   /* Just in case lang_hooks.post_options ends up calling a debug_hook.
1234      This can happen with incorrect pre-processed input. */
1235   debug_hooks = &do_nothing_debug_hooks;
1236 
1237   maximum_field_alignment = initial_max_fld_align * BITS_PER_UNIT;
1238 
1239   /* Allow the front end to perform consistency checks and do further
1240      initialization based on the command line options.  This hook also
1241      sets the original filename if appropriate (e.g. foo.i -> foo.c)
1242      so we can correctly initialize debug output.  */
1243   no_backend = lang_hooks.post_options (&main_input_filename);
1244 
1245   /* Some machines may reject certain combinations of options.  */
1246   location_t saved_location = input_location;
1247   input_location = UNKNOWN_LOCATION;
1248   targetm.target_option.override ();
1249   input_location = saved_location;
1250 
1251   if (flag_diagnostics_generate_patch)
1252       global_dc->edit_context_ptr = new edit_context ();
1253 
1254   /* Avoid any informative notes in the second run of -fcompare-debug.  */
1255   if (flag_compare_debug)
1256     diagnostic_inhibit_notes (global_dc);
1257 
1258   if (flag_section_anchors && !target_supports_section_anchors_p ())
1259     {
1260       warning_at (UNKNOWN_LOCATION, OPT_fsection_anchors,
1261 		  "this target does not support %qs",
1262 		  "-fsection-anchors");
1263       flag_section_anchors = 0;
1264     }
1265 
1266   if (flag_short_enums == 2)
1267     flag_short_enums = targetm.default_short_enums ();
1268 
1269   /* Set aux_base_name if not already set.  */
1270   if (aux_base_name)
1271     ;
1272   else if (dump_base_name)
1273     {
1274       const char *name = dump_base_name;
1275       int nlen, len;
1276 
1277       if (dump_base_ext && (len = strlen (dump_base_ext))
1278 	  && (nlen = strlen (name)) && nlen > len
1279 	  && strcmp (name + nlen - len, dump_base_ext) == 0)
1280 	{
1281 	  char *p = xstrndup (name, nlen - len);
1282 	  name = p;
1283 	}
1284 
1285       aux_base_name = name;
1286     }
1287   else
1288     aux_base_name = "gccaux";
1289 
1290 #ifndef HAVE_isl
1291   if (flag_graphite
1292       || flag_loop_nest_optimize
1293       || flag_graphite_identity
1294       || flag_loop_parallelize_all)
1295     sorry ("Graphite loop optimizations cannot be used (isl is not available) "
1296 	   "(%<-fgraphite%>, %<-fgraphite-identity%>, "
1297 	   "%<-floop-nest-optimize%>, %<-floop-parallelize-all%>)");
1298 #endif
1299 
1300   if (flag_cf_protection != CF_NONE
1301       && !(flag_cf_protection & CF_SET))
1302     {
1303       if (flag_cf_protection == CF_FULL)
1304 	{
1305 	  error_at (UNKNOWN_LOCATION,
1306 		    "%<-fcf-protection=full%> is not supported for this "
1307 		    "target");
1308 	  flag_cf_protection = CF_NONE;
1309 	}
1310       if (flag_cf_protection == CF_BRANCH)
1311 	{
1312 	  error_at (UNKNOWN_LOCATION,
1313 		    "%<-fcf-protection=branch%> is not supported for this "
1314 		    "target");
1315 	  flag_cf_protection = CF_NONE;
1316 	}
1317       if (flag_cf_protection == CF_RETURN)
1318 	{
1319 	  error_at (UNKNOWN_LOCATION,
1320 		    "%<-fcf-protection=return%> is not supported for this "
1321 		    "target");
1322 	  flag_cf_protection = CF_NONE;
1323 	}
1324     }
1325 
1326   /* One region RA really helps to decrease the code size.  */
1327   if (flag_ira_region == IRA_REGION_AUTODETECT)
1328     flag_ira_region
1329       = optimize_size || !optimize ? IRA_REGION_ONE : IRA_REGION_MIXED;
1330 
1331   if (!abi_version_at_least (2))
1332     {
1333       /* -fabi-version=1 support was removed after GCC 4.9.  */
1334       error_at (UNKNOWN_LOCATION,
1335 		"%<-fabi-version=1%> is no longer supported");
1336       flag_abi_version = 2;
1337     }
1338 
1339   /* web and rename-registers help when run after loop unrolling.  */
1340   if (flag_web == AUTODETECT_VALUE)
1341     flag_web = flag_unroll_loops;
1342 
1343   if (flag_rename_registers == AUTODETECT_VALUE)
1344     flag_rename_registers = flag_unroll_loops;
1345 
1346   if (flag_non_call_exceptions)
1347     flag_asynchronous_unwind_tables = 1;
1348   if (flag_asynchronous_unwind_tables)
1349     flag_unwind_tables = 1;
1350 
1351   if (flag_value_profile_transformations)
1352     flag_profile_values = 1;
1353 
1354   /* Warn about options that are not supported on this machine.  */
1355 #ifndef INSN_SCHEDULING
1356   if (flag_schedule_insns || flag_schedule_insns_after_reload)
1357     warning_at (UNKNOWN_LOCATION, 0,
1358 		"instruction scheduling not supported on this target machine");
1359 #endif
1360   if (!DELAY_SLOTS && flag_delayed_branch)
1361     warning_at (UNKNOWN_LOCATION, 0,
1362 		"this target machine does not have delayed branches");
1363 
1364   user_label_prefix = USER_LABEL_PREFIX;
1365   if (flag_leading_underscore != -1)
1366     {
1367       /* If the default prefix is more complicated than "" or "_",
1368 	 issue a warning and ignore this option.  */
1369       if (user_label_prefix[0] == 0 ||
1370 	  (user_label_prefix[0] == '_' && user_label_prefix[1] == 0))
1371 	{
1372 	  user_label_prefix = flag_leading_underscore ? "_" : "";
1373 	}
1374       else
1375 	warning_at (UNKNOWN_LOCATION, 0,
1376 		    "%<-f%sleading-underscore%> not supported on this "
1377 		    "target machine", flag_leading_underscore ? "" : "no-");
1378     }
1379 
1380   /* If we are in verbose mode, write out the version and maybe all the
1381      option flags in use.  */
1382   if (version_flag)
1383     {
1384       print_version (stderr, "", true);
1385       if (!quiet_flag)
1386 	{
1387 	  fputs ("options passed: ", stderr);
1388 	  char *cmdline = gen_command_line_string (save_decoded_options,
1389 						   save_decoded_options_count);
1390 
1391 	  fputs (cmdline, stderr);
1392 	  free (cmdline);
1393 	  fputc ('\n', stderr);
1394 	}
1395     }
1396 
1397   if (flag_syntax_only)
1398     {
1399       write_symbols = NO_DEBUG;
1400       profile_flag = 0;
1401     }
1402 
1403   if (flag_gtoggle)
1404     {
1405       if (debug_info_level == DINFO_LEVEL_NONE)
1406 	{
1407 	  debug_info_level = DINFO_LEVEL_NORMAL;
1408 
1409 	  if (write_symbols == NO_DEBUG)
1410 	    write_symbols = PREFERRED_DEBUGGING_TYPE;
1411 	}
1412       else
1413 	debug_info_level = DINFO_LEVEL_NONE;
1414     }
1415 
1416   if (flag_dump_final_insns && !flag_syntax_only && !no_backend)
1417     {
1418       FILE *final_output = fopen (flag_dump_final_insns, "w");
1419       if (!final_output)
1420 	{
1421 	  error_at (UNKNOWN_LOCATION,
1422 		    "could not open final insn dump file %qs: %m",
1423 		    flag_dump_final_insns);
1424 	  flag_dump_final_insns = NULL;
1425 	}
1426       else if (fclose (final_output))
1427 	{
1428 	  error_at (UNKNOWN_LOCATION,
1429 		    "could not close zeroed insn dump file %qs: %m",
1430 		    flag_dump_final_insns);
1431 	  flag_dump_final_insns = NULL;
1432 	}
1433     }
1434 
1435   /* A lot of code assumes write_symbols == NO_DEBUG if the debugging
1436      level is 0.  */
1437   if (debug_info_level == DINFO_LEVEL_NONE)
1438     write_symbols = NO_DEBUG;
1439 
1440   if (write_symbols == NO_DEBUG)
1441     ;
1442 #if defined(DBX_DEBUGGING_INFO)
1443   else if (write_symbols == DBX_DEBUG)
1444     debug_hooks = &dbx_debug_hooks;
1445 #endif
1446 #if defined(XCOFF_DEBUGGING_INFO)
1447   else if (write_symbols == XCOFF_DEBUG)
1448     debug_hooks = &xcoff_debug_hooks;
1449 #endif
1450 #ifdef DWARF2_DEBUGGING_INFO
1451   else if (write_symbols == DWARF2_DEBUG)
1452     debug_hooks = &dwarf2_debug_hooks;
1453 #endif
1454 #ifdef VMS_DEBUGGING_INFO
1455   else if (write_symbols == VMS_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
1456     debug_hooks = &vmsdbg_debug_hooks;
1457 #endif
1458 #ifdef DWARF2_LINENO_DEBUGGING_INFO
1459   else if (write_symbols == DWARF2_DEBUG)
1460     debug_hooks = &dwarf2_lineno_debug_hooks;
1461 #endif
1462   else
1463     error_at (UNKNOWN_LOCATION,
1464 	      "target system does not support the %qs debug format",
1465 	      debug_type_names[write_symbols]);
1466 
1467   /* We know which debug output will be used so we can set flag_var_tracking
1468      and flag_var_tracking_uninit if the user has not specified them.  */
1469   if (debug_info_level < DINFO_LEVEL_NORMAL
1470       || debug_hooks->var_location == do_nothing_debug_hooks.var_location)
1471     {
1472       if (flag_var_tracking == 1
1473 	  || flag_var_tracking_uninit == 1)
1474         {
1475 	  if (debug_info_level < DINFO_LEVEL_NORMAL)
1476 	    warning_at (UNKNOWN_LOCATION, 0,
1477 			"variable tracking requested, but useless unless "
1478 			"producing debug info");
1479 	  else
1480 	    warning_at (UNKNOWN_LOCATION, 0,
1481 			"variable tracking requested, but not supported "
1482 			"by this debug format");
1483 	}
1484       flag_var_tracking = 0;
1485       flag_var_tracking_uninit = 0;
1486     }
1487 
1488   /* The debug hooks are used to implement -fdump-go-spec because it
1489      gives a simple and stable API for all the information we need to
1490      dump.  */
1491   if (flag_dump_go_spec != NULL)
1492     debug_hooks = dump_go_spec_init (flag_dump_go_spec, debug_hooks);
1493 
1494   /* If the user specifically requested variable tracking with tagging
1495      uninitialized variables, we need to turn on variable tracking.
1496      (We already determined above that variable tracking is feasible.)  */
1497   if (flag_var_tracking_uninit == 1)
1498     flag_var_tracking = 1;
1499 
1500   if (flag_var_tracking == AUTODETECT_VALUE)
1501     flag_var_tracking = optimize >= 1;
1502 
1503   if (flag_var_tracking_uninit == AUTODETECT_VALUE)
1504     flag_var_tracking_uninit = flag_var_tracking;
1505 
1506   if (flag_var_tracking_assignments == AUTODETECT_VALUE)
1507     flag_var_tracking_assignments
1508       = (flag_var_tracking
1509 	 && !(flag_selective_scheduling || flag_selective_scheduling2));
1510 
1511   if (flag_var_tracking_assignments_toggle)
1512     flag_var_tracking_assignments = !flag_var_tracking_assignments;
1513 
1514   if (flag_var_tracking_assignments && !flag_var_tracking)
1515     flag_var_tracking = flag_var_tracking_assignments = -1;
1516 
1517   if (flag_var_tracking_assignments
1518       && (flag_selective_scheduling || flag_selective_scheduling2))
1519     warning_at (UNKNOWN_LOCATION, 0,
1520 		"var-tracking-assignments changes selective scheduling");
1521 
1522   if (debug_nonbind_markers_p == AUTODETECT_VALUE)
1523     debug_nonbind_markers_p
1524       = (optimize
1525 	 && debug_info_level >= DINFO_LEVEL_NORMAL
1526 	 && (write_symbols == DWARF2_DEBUG
1527 	     || write_symbols == VMS_AND_DWARF2_DEBUG)
1528 	 && !(flag_selective_scheduling || flag_selective_scheduling2));
1529 
1530   if (dwarf2out_as_loc_support == AUTODETECT_VALUE)
1531     dwarf2out_as_loc_support
1532       = dwarf2out_default_as_loc_support ();
1533   if (dwarf2out_as_locview_support == AUTODETECT_VALUE)
1534     dwarf2out_as_locview_support
1535       = dwarf2out_default_as_locview_support ();
1536 
1537   if (debug_variable_location_views == AUTODETECT_VALUE)
1538     {
1539       debug_variable_location_views
1540 	= (flag_var_tracking
1541 	   && debug_info_level >= DINFO_LEVEL_NORMAL
1542 	   && (write_symbols == DWARF2_DEBUG
1543 	       || write_symbols == VMS_AND_DWARF2_DEBUG)
1544 	   && !dwarf_strict
1545 	   && dwarf2out_as_loc_support
1546 	   && dwarf2out_as_locview_support);
1547     }
1548   else if (debug_variable_location_views == -1 && dwarf_version != 5)
1549     {
1550       warning_at (UNKNOWN_LOCATION, 0,
1551 		  "without %<-gdwarf-5%>, "
1552 		  "%<-gvariable-location-views=incompat5%> "
1553 		  "is equivalent to %<-gvariable-location-views%>");
1554       debug_variable_location_views = 1;
1555     }
1556 
1557   if (debug_internal_reset_location_views == 2)
1558     {
1559       debug_internal_reset_location_views
1560 	= (debug_variable_location_views
1561 	   && targetm.reset_location_view);
1562     }
1563   else if (debug_internal_reset_location_views
1564 	   && !debug_variable_location_views)
1565     {
1566       warning_at (UNKNOWN_LOCATION, 0,
1567 		  "%<-ginternal-reset-location-views%> is forced disabled "
1568 		  "without %<-gvariable-location-views%>");
1569       debug_internal_reset_location_views = 0;
1570     }
1571 
1572   if (debug_inline_points == AUTODETECT_VALUE)
1573     debug_inline_points = debug_variable_location_views;
1574   else if (debug_inline_points && !debug_nonbind_markers_p)
1575     {
1576       warning_at (UNKNOWN_LOCATION, 0,
1577 		  "%<-ginline-points%> is forced disabled without "
1578 		  "%<-gstatement-frontiers%>");
1579       debug_inline_points = 0;
1580     }
1581 
1582   if (flag_tree_cselim == AUTODETECT_VALUE)
1583     {
1584       if (HAVE_conditional_move)
1585 	flag_tree_cselim = 1;
1586       else
1587 	flag_tree_cselim = 0;
1588     }
1589 
1590   /* If auxiliary info generation is desired, open the output file.
1591      This goes in the same directory as the source file--unlike
1592      all the other output files.  */
1593   if (flag_gen_aux_info)
1594     {
1595       aux_info_file = fopen (aux_info_file_name, "w");
1596       if (aux_info_file == 0)
1597 	fatal_error (UNKNOWN_LOCATION,
1598 		     "cannot open %s: %m", aux_info_file_name);
1599     }
1600 
1601   if (!targetm_common.have_named_sections)
1602     {
1603       if (flag_function_sections)
1604 	{
1605 	  warning_at (UNKNOWN_LOCATION, 0,
1606 		      "%<-ffunction-sections%> not supported for this target");
1607 	  flag_function_sections = 0;
1608 	}
1609       if (flag_data_sections)
1610 	{
1611 	  warning_at (UNKNOWN_LOCATION, 0,
1612 		      "%<-fdata-sections%> not supported for this target");
1613 	  flag_data_sections = 0;
1614 	}
1615     }
1616 
1617   if (flag_prefetch_loop_arrays > 0 && !targetm.code_for_prefetch)
1618     {
1619       warning_at (UNKNOWN_LOCATION, 0,
1620 		  "%<-fprefetch-loop-arrays%> not supported for this target");
1621       flag_prefetch_loop_arrays = 0;
1622     }
1623   else if (flag_prefetch_loop_arrays > 0 && !targetm.have_prefetch ())
1624     {
1625       warning_at (UNKNOWN_LOCATION, 0,
1626 		  "%<-fprefetch-loop-arrays%> not supported for this target "
1627 		  "(try %<-march%> switches)");
1628       flag_prefetch_loop_arrays = 0;
1629     }
1630 
1631   /* This combination of options isn't handled for i386 targets and doesn't
1632      make much sense anyway, so don't allow it.  */
1633   if (flag_prefetch_loop_arrays > 0 && optimize_size)
1634     {
1635       warning_at (UNKNOWN_LOCATION, 0,
1636 		  "%<-fprefetch-loop-arrays%> is not supported with %<-Os%>");
1637       flag_prefetch_loop_arrays = 0;
1638     }
1639 
1640   /* The presence of IEEE signaling NaNs, implies all math can trap.  */
1641   if (flag_signaling_nans)
1642     flag_trapping_math = 1;
1643 
1644   /* We cannot reassociate if we want traps or signed zeros.  */
1645   if (flag_associative_math && (flag_trapping_math || flag_signed_zeros))
1646     {
1647       warning_at (UNKNOWN_LOCATION, 0,
1648 		  "%<-fassociative-math%> disabled; other options take "
1649 		  "precedence");
1650       flag_associative_math = 0;
1651     }
1652 
1653   /* -fstack-clash-protection is not currently supported on targets
1654      where the stack grows up.  */
1655   if (flag_stack_clash_protection && !STACK_GROWS_DOWNWARD)
1656     {
1657       warning_at (UNKNOWN_LOCATION, 0,
1658 		  "%<-fstack-clash-protection%> is not supported on targets "
1659 		  "where the stack grows from lower to higher addresses");
1660       flag_stack_clash_protection = 0;
1661     }
1662 
1663   /* We cannot support -fstack-check= and -fstack-clash-protection at
1664      the same time.  */
1665   if (flag_stack_check != NO_STACK_CHECK && flag_stack_clash_protection)
1666     {
1667       warning_at (UNKNOWN_LOCATION, 0,
1668 		  "%<-fstack-check=%> and %<-fstack-clash_protection%> are "
1669 		  "mutually exclusive; disabling %<-fstack-check=%>");
1670       flag_stack_check = NO_STACK_CHECK;
1671     }
1672 
1673   /* With -fcx-limited-range, we do cheap and quick complex arithmetic.  */
1674   if (flag_cx_limited_range)
1675     flag_complex_method = 0;
1676 
1677   /* With -fcx-fortran-rules, we do something in-between cheap and C99.  */
1678   if (flag_cx_fortran_rules)
1679     flag_complex_method = 1;
1680 
1681   /* Targets must be able to place spill slots at lower addresses.  If the
1682      target already uses a soft frame pointer, the transition is trivial.  */
1683   if (!FRAME_GROWS_DOWNWARD && flag_stack_protect)
1684     {
1685       warning_at (UNKNOWN_LOCATION, 0,
1686 		  "%<-fstack-protector%> not supported for this target");
1687       flag_stack_protect = 0;
1688     }
1689   if (!flag_stack_protect)
1690     warn_stack_protect = 0;
1691 
1692   /* Address Sanitizer needs porting to each target architecture.  */
1693 
1694   if ((flag_sanitize & SANITIZE_ADDRESS)
1695       && !FRAME_GROWS_DOWNWARD)
1696     {
1697       warning_at (UNKNOWN_LOCATION, 0,
1698 		  "%<-fsanitize=address%> and %<-fsanitize=kernel-address%> "
1699 		  "are not supported for this target");
1700       flag_sanitize &= ~SANITIZE_ADDRESS;
1701     }
1702 
1703   if ((flag_sanitize & SANITIZE_USER_ADDRESS)
1704       && ((targetm.asan_shadow_offset == NULL)
1705 	  || (targetm.asan_shadow_offset () == 0)))
1706     {
1707       warning_at (UNKNOWN_LOCATION, 0,
1708 		  "%<-fsanitize=address%> not supported for this target");
1709       flag_sanitize &= ~SANITIZE_ADDRESS;
1710     }
1711 
1712   if ((flag_sanitize & SANITIZE_KERNEL_ADDRESS)
1713       && (targetm.asan_shadow_offset == NULL
1714 	  && !asan_shadow_offset_set_p ()))
1715     {
1716       warning_at (UNKNOWN_LOCATION, 0,
1717 		  "%<-fsanitize=kernel-address%> with stack protection "
1718 		  "is not supported without %<-fasan-shadow-offset=%> "
1719 		  "for this target");
1720       flag_sanitize &= ~SANITIZE_ADDRESS;
1721     }
1722 
1723   /* HWAsan requires top byte ignore feature in the backend.  */
1724   if (flag_sanitize & SANITIZE_HWADDRESS
1725       && ! targetm.memtag.can_tag_addresses ())
1726     {
1727       warning_at (UNKNOWN_LOCATION, 0, "%qs is not supported for this target",
1728 		  "-fsanitize=hwaddress");
1729       flag_sanitize &= ~SANITIZE_HWADDRESS;
1730     }
1731 
1732   HOST_WIDE_INT patch_area_size, patch_area_start;
1733   parse_and_check_patch_area (flag_patchable_function_entry, false,
1734 			      &patch_area_size, &patch_area_start);
1735 
1736  /* Do not use IPA optimizations for register allocation if profiler is active
1737     or patchable function entries are inserted for run-time instrumentation
1738     or port does not emit prologue and epilogue as RTL.  */
1739   if (profile_flag || patch_area_size
1740       || !targetm.have_prologue () || !targetm.have_epilogue ())
1741     flag_ipa_ra = 0;
1742 
1743   /* Enable -Werror=coverage-mismatch when -Werror and -Wno-error
1744      have not been set.  */
1745   if (!global_options_set.x_warnings_are_errors
1746       && warn_coverage_mismatch
1747       && (global_dc->classify_diagnostic[OPT_Wcoverage_mismatch] ==
1748           DK_UNSPECIFIED))
1749     diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_mismatch,
1750                                     DK_ERROR, UNKNOWN_LOCATION);
1751 
1752   /* Save the current optimization options.  */
1753   optimization_default_node
1754     = build_optimization_node (&global_options, &global_options_set);
1755   optimization_current_node = optimization_default_node;
1756 
1757   if (flag_checking >= 2)
1758     hash_table_sanitize_eq_limit
1759       = param_hash_table_verification_limit;
1760 
1761   if (flag_large_source_files)
1762     line_table->default_range_bits = 0;
1763 
1764   /* Please don't change global_options after this point, those changes won't
1765      be reflected in optimization_{default,current}_node.  */
1766 }
1767 
1768 /* This function can be called multiple times to reinitialize the compiler
1769    back end when register classes or instruction sets have changed,
1770    before each function.  */
1771 static void
backend_init_target(void)1772 backend_init_target (void)
1773 {
1774   /* This depends on stack_pointer_rtx.  */
1775   init_fake_stack_mems ();
1776 
1777   /* Sets static_base_value[HARD_FRAME_POINTER_REGNUM], which is
1778      mode-dependent.  */
1779   init_alias_target ();
1780 
1781   /* Depends on HARD_FRAME_POINTER_REGNUM.  */
1782   if (!ira_use_lra_p)
1783     init_reload ();
1784 
1785   /* Depends on the enabled attribute.  */
1786   recog_init ();
1787 
1788   /* The following initialization functions need to generate rtl, so
1789      provide a dummy function context for them.  */
1790   init_dummy_function_start ();
1791 
1792   /* rtx_cost is mode-dependent, so cached values need to be recomputed
1793      on a mode change.  */
1794   init_expmed ();
1795   init_lower_subreg ();
1796   init_set_costs ();
1797 
1798   init_expr_target ();
1799   ira_init ();
1800 
1801   /* We may need to recompute regno_save_code[] and regno_restore_code[]
1802      after a mode change as well.  */
1803   caller_save_initialized_p = false;
1804 
1805   expand_dummy_function_end ();
1806 }
1807 
1808 /* Initialize the compiler back end.  This function is called only once,
1809    when starting the compiler.  */
1810 static void
backend_init(void)1811 backend_init (void)
1812 {
1813   init_emit_once ();
1814 
1815   init_rtlanal ();
1816   init_inline_once ();
1817   init_varasm_once ();
1818   save_register_info ();
1819 
1820   /* Middle end needs this initialization for default mem attributes
1821      used by early calls to make_decl_rtl.  */
1822   init_emit_regs ();
1823 
1824   /* Middle end needs this initialization for mode tables used to assign
1825      modes to vector variables.  */
1826   init_regs ();
1827 }
1828 
1829 /* Initialize things that are both lang-dependent and target-dependent.
1830    This function can be called more than once if target parameters change.  */
1831 static void
lang_dependent_init_target(void)1832 lang_dependent_init_target (void)
1833 {
1834   /* This creates various _DECL nodes, so needs to be called after the
1835      front end is initialized.  It also depends on the HAVE_xxx macros
1836      generated from the target machine description.  */
1837   init_optabs ();
1838 
1839   gcc_assert (!this_target_rtl->target_specific_initialized);
1840 }
1841 
1842 /* Perform initializations that are lang-dependent or target-dependent.
1843    but matters only for late optimizations and RTL generation.  */
1844 
1845 static int rtl_initialized;
1846 
1847 void
initialize_rtl(void)1848 initialize_rtl (void)
1849 {
1850   auto_timevar tv (g_timer, TV_INITIALIZE_RTL);
1851 
1852   /* Initialization done just once per compilation, but delayed
1853      till code generation.  */
1854   if (!rtl_initialized)
1855     ira_init_once ();
1856   rtl_initialized = true;
1857 
1858   /* Target specific RTL backend initialization.  */
1859   if (!this_target_rtl->target_specific_initialized)
1860     {
1861       backend_init_target ();
1862       this_target_rtl->target_specific_initialized = true;
1863     }
1864 }
1865 
1866 /* Language-dependent initialization.  Returns nonzero on success.  */
1867 static int
lang_dependent_init(const char * name)1868 lang_dependent_init (const char *name)
1869 {
1870   location_t save_loc = input_location;
1871   if (!dump_base_name)
1872     {
1873       dump_base_name = name && name[0] ? name : "gccdump";
1874 
1875       /* We do not want to derive a non-empty dumpbase-ext from an
1876 	 explicit -dumpbase argument, only from a defaulted
1877 	 dumpbase.  */
1878       if (!dump_base_ext)
1879 	{
1880 	  const char *base = lbasename (dump_base_name);
1881 	  const char *ext = strrchr (base, '.');
1882 	  if (ext)
1883 	    dump_base_ext = ext;
1884 	}
1885     }
1886 
1887   /* Other front-end initialization.  */
1888   input_location = BUILTINS_LOCATION;
1889   if (lang_hooks.init () == 0)
1890     return 0;
1891   input_location = save_loc;
1892 
1893   if (!flag_wpa)
1894     {
1895       init_asm_output (name);
1896 
1897       if (!flag_generate_lto && !flag_compare_debug)
1898 	{
1899 	  /* If stack usage information is desired, open the output file.  */
1900 	  if (flag_stack_usage)
1901 	    stack_usage_file = open_auxiliary_file ("su");
1902 
1903 	  /* If call graph information is desired, open the output file.  */
1904 	  if (flag_callgraph_info)
1905 	    {
1906 	      callgraph_info_file = open_auxiliary_file ("ci");
1907 	      /* Write the file header.  */
1908 	      fprintf (callgraph_info_file,
1909 		       "graph: { title: \"%s\"\n", main_input_filename);
1910 	      bitmap_obstack_initialize (NULL);
1911 	      callgraph_info_external_printed = BITMAP_ALLOC (NULL);
1912 	    }
1913 	}
1914       else
1915 	flag_stack_usage = flag_callgraph_info = false;
1916     }
1917 
1918   /* This creates various _DECL nodes, so needs to be called after the
1919      front end is initialized.  */
1920   init_eh ();
1921 
1922   /* Do the target-specific parts of the initialization.  */
1923   lang_dependent_init_target ();
1924 
1925   if (!flag_wpa)
1926     {
1927       /* If dbx symbol table desired, initialize writing it and output the
1928 	 predefined types.  */
1929       timevar_push (TV_SYMOUT);
1930 
1931       /* Now we have the correct original filename, we can initialize
1932 	 debug output.  */
1933       (*debug_hooks->init) (name);
1934 
1935       timevar_pop (TV_SYMOUT);
1936     }
1937 
1938   return 1;
1939 }
1940 
1941 
1942 /* Reinitialize everything when target parameters, such as register usage,
1943    have changed.  */
1944 void
target_reinit(void)1945 target_reinit (void)
1946 {
1947   struct rtl_data saved_x_rtl;
1948   rtx *saved_regno_reg_rtx;
1949   tree saved_optimization_current_node;
1950   struct target_optabs *saved_this_fn_optabs;
1951 
1952   /* Temporarily switch to the default optimization node, so that
1953      *this_target_optabs is set to the default, not reflecting
1954      whatever a previous function used for the optimize
1955      attribute.  */
1956   saved_optimization_current_node = optimization_current_node;
1957   saved_this_fn_optabs = this_fn_optabs;
1958   if (saved_optimization_current_node != optimization_default_node)
1959     {
1960       optimization_current_node = optimization_default_node;
1961       cl_optimization_restore
1962 	(&global_options, &global_options_set,
1963 	 TREE_OPTIMIZATION (optimization_default_node));
1964     }
1965   this_fn_optabs = this_target_optabs;
1966 
1967   /* Save *crtl and regno_reg_rtx around the reinitialization
1968      to allow target_reinit being called even after prepare_function_start.  */
1969   saved_regno_reg_rtx = regno_reg_rtx;
1970   if (saved_regno_reg_rtx)
1971     {
1972       saved_x_rtl = *crtl;
1973       memset (crtl, '\0', sizeof (*crtl));
1974       regno_reg_rtx = NULL;
1975     }
1976 
1977   this_target_rtl->target_specific_initialized = false;
1978 
1979   /* This initializes hard_frame_pointer, and calls init_reg_modes_target()
1980      to initialize reg_raw_mode[].  */
1981   init_emit_regs ();
1982 
1983   /* This invokes target hooks to set fixed_reg[] etc, which is
1984      mode-dependent.  */
1985   init_regs ();
1986 
1987   /* Reinitialize lang-dependent parts.  */
1988   lang_dependent_init_target ();
1989 
1990   /* Restore the original optimization node.  */
1991   if (saved_optimization_current_node != optimization_default_node)
1992     {
1993       optimization_current_node = saved_optimization_current_node;
1994       cl_optimization_restore (&global_options, &global_options_set,
1995 			       TREE_OPTIMIZATION (optimization_current_node));
1996     }
1997   this_fn_optabs = saved_this_fn_optabs;
1998 
1999   /* Restore regno_reg_rtx at the end, as free_after_compilation from
2000      expand_dummy_function_end clears it.  */
2001   if (saved_regno_reg_rtx)
2002     {
2003       *crtl = saved_x_rtl;
2004       regno_reg_rtx = saved_regno_reg_rtx;
2005       saved_regno_reg_rtx = NULL;
2006     }
2007 }
2008 
2009 void
dump_memory_report(const char * header)2010 dump_memory_report (const char *header)
2011 {
2012   /* Print significant header.  */
2013   fputc ('\n', stderr);
2014   for (unsigned i = 0; i < 80; i++)
2015     fputc ('#', stderr);
2016   fprintf (stderr, "\n# %-77s#\n", header);
2017   for (unsigned i = 0; i < 80; i++)
2018     fputc ('#', stderr);
2019   fputs ("\n\n", stderr);
2020 
2021   dump_line_table_statistics ();
2022   ggc_print_statistics ();
2023   stringpool_statistics ();
2024   dump_tree_statistics ();
2025   dump_gimple_statistics ();
2026   dump_rtx_statistics ();
2027   dump_alloc_pool_statistics ();
2028   dump_bitmap_statistics ();
2029   dump_hash_table_loc_statistics ();
2030   dump_vec_loc_statistics ();
2031   dump_ggc_loc_statistics ();
2032   dump_alias_stats (stderr);
2033   dump_pta_stats (stderr);
2034 }
2035 
2036 /* Clean up: close opened files, etc.  */
2037 
2038 static void
finalize(bool no_backend)2039 finalize (bool no_backend)
2040 {
2041   /* Close the dump files.  */
2042   if (flag_gen_aux_info)
2043     {
2044       fclose (aux_info_file);
2045       aux_info_file = NULL;
2046       if (seen_error ())
2047 	unlink (aux_info_file_name);
2048     }
2049 
2050   /* Close non-debugging input and output files.  Take special care to note
2051      whether fclose returns an error, since the pages might still be on the
2052      buffer chain while the file is open.  */
2053 
2054   if (asm_out_file)
2055     {
2056       if (ferror (asm_out_file) != 0)
2057 	fatal_error (input_location, "error writing to %s: %m", asm_file_name);
2058       if (fclose (asm_out_file) != 0)
2059 	fatal_error (input_location, "error closing %s: %m", asm_file_name);
2060       asm_out_file = NULL;
2061     }
2062 
2063   if (stack_usage_file)
2064     {
2065       fclose (stack_usage_file);
2066       stack_usage_file = NULL;
2067     }
2068 
2069   if (callgraph_info_file)
2070     {
2071       fputs ("}\n", callgraph_info_file);
2072       fclose (callgraph_info_file);
2073       callgraph_info_file = NULL;
2074       BITMAP_FREE (callgraph_info_external_printed);
2075       bitmap_obstack_release (NULL);
2076     }
2077 
2078   if (seen_error ())
2079     coverage_remove_note_file ();
2080 
2081   if (!no_backend)
2082     {
2083       statistics_fini ();
2084       debuginfo_fini ();
2085 
2086       g->get_passes ()->finish_optimization_passes ();
2087 
2088       lra_finish_once ();
2089     }
2090 
2091   if (mem_report)
2092     dump_memory_report ("Final");
2093 
2094   if (profile_report)
2095     dump_profile_report ();
2096 
2097   if (flag_dbg_cnt_list)
2098     dbg_cnt_list_all_counters ();
2099 
2100   /* Language-specific end of compilation actions.  */
2101   lang_hooks.finish ();
2102 }
2103 
2104 static bool
standard_type_bitsize(int bitsize)2105 standard_type_bitsize (int bitsize)
2106 {
2107   /* As a special exception, we always want __int128 enabled if possible.  */
2108   if (bitsize == 128)
2109     return false;
2110   if (bitsize == CHAR_TYPE_SIZE
2111       || bitsize == SHORT_TYPE_SIZE
2112       || bitsize == INT_TYPE_SIZE
2113       || bitsize == LONG_TYPE_SIZE
2114       || bitsize == LONG_LONG_TYPE_SIZE)
2115     return true;
2116   return false;
2117 }
2118 
2119 /* Initialize the compiler, and compile the input file.  */
2120 static void
do_compile()2121 do_compile ()
2122 {
2123   process_options ();
2124 
2125   /* Don't do any more if an error has already occurred.  */
2126   if (!seen_error ())
2127     {
2128       int i;
2129 
2130       timevar_start (TV_PHASE_SETUP);
2131 
2132       if (flag_save_optimization_record)
2133 	{
2134 	  dump_context::get ().set_json_writer (new optrecord_json_writer ());
2135 	}
2136 
2137       /* This must be run always, because it is needed to compute the FP
2138 	 predefined macros, such as __LDBL_MAX__, for targets using non
2139 	 default FP formats.  */
2140       init_adjust_machine_modes ();
2141       init_derived_machine_modes ();
2142 
2143       /* This must happen after the backend has a chance to process
2144 	 command line options, but before the parsers are
2145 	 initialized.  */
2146       for (i = 0; i < NUM_INT_N_ENTS; i ++)
2147 	if (targetm.scalar_mode_supported_p (int_n_data[i].m)
2148 	    && ! standard_type_bitsize (int_n_data[i].bitsize))
2149 	  int_n_enabled_p[i] = true;
2150 	else
2151 	  int_n_enabled_p[i] = false;
2152 
2153       /* Initialize mpfrs exponent range.  This is important to get
2154          underflow/overflow in a reasonable timeframe.  */
2155       machine_mode mode;
2156       int min_exp = -1;
2157       int max_exp = 1;
2158       FOR_EACH_MODE_IN_CLASS (mode, MODE_FLOAT)
2159 	if (SCALAR_FLOAT_MODE_P (mode))
2160 	  {
2161 	    const real_format *fmt = REAL_MODE_FORMAT (mode);
2162 	    if (fmt)
2163 	      {
2164 		/* fmt->emin - fmt->p + 1 should be enough but the
2165 		   back-and-forth dance in real_to_decimal_for_mode we
2166 		   do for checking fails due to rounding effects then.  */
2167 		if ((fmt->emin - fmt->p) < min_exp)
2168 		  min_exp = fmt->emin - fmt->p;
2169 		if (fmt->emax > max_exp)
2170 		  max_exp = fmt->emax;
2171 	      }
2172 	  }
2173       /* E.g. mpc_norm assumes it can square a number without bothering with
2174 	 with range scaling, so until that is fixed, double the minimum
2175 	 and maximum exponents, plus add some buffer for arithmetics
2176 	 on the squared numbers.  */
2177       if (mpfr_set_emin (2 * (min_exp - 1))
2178 	  || mpfr_set_emax (2 * (max_exp + 1)))
2179 	sorry ("mpfr not configured to handle all floating modes");
2180 
2181       /* Set up the back-end if requested.  */
2182       if (!no_backend)
2183 	backend_init ();
2184 
2185       /* Language-dependent initialization.  Returns true on success.  */
2186       if (lang_dependent_init (main_input_filename))
2187         {
2188           /* Initialize yet another pass.  */
2189 
2190           ggc_protect_identifiers = true;
2191 
2192 	  symtab->initialize ();
2193           init_final (main_input_filename);
2194           coverage_init (aux_base_name);
2195           statistics_init ();
2196           debuginfo_init ();
2197           invoke_plugin_callbacks (PLUGIN_START_UNIT, NULL);
2198 
2199           timevar_stop (TV_PHASE_SETUP);
2200 
2201           compile_file ();
2202         }
2203       else
2204         {
2205           timevar_stop (TV_PHASE_SETUP);
2206         }
2207 
2208       timevar_start (TV_PHASE_FINALIZE);
2209 
2210       finalize (no_backend);
2211 
2212       timevar_stop (TV_PHASE_FINALIZE);
2213     }
2214 }
2215 
toplev(timer * external_timer,bool init_signals)2216 toplev::toplev (timer *external_timer,
2217 		bool init_signals)
2218   : m_use_TV_TOTAL (external_timer == NULL),
2219     m_init_signals (init_signals)
2220 {
2221   if (external_timer)
2222     g_timer = external_timer;
2223 }
2224 
~toplev()2225 toplev::~toplev ()
2226 {
2227   if (g_timer && m_use_TV_TOTAL)
2228     {
2229       g_timer->stop (TV_TOTAL);
2230       g_timer->print (stderr);
2231       delete g_timer;
2232       g_timer = NULL;
2233     }
2234 }
2235 
2236 /* Potentially call timevar_init (which will create g_timevars if it
2237    doesn't already exist).  */
2238 
2239 void
start_timevars()2240 toplev::start_timevars ()
2241 {
2242   if (time_report || !quiet_flag  || flag_detailed_statistics)
2243     timevar_init ();
2244 
2245   timevar_start (TV_TOTAL);
2246 }
2247 
2248 /* Handle -fself-test.   */
2249 
2250 void
run_self_tests()2251 toplev::run_self_tests ()
2252 {
2253   if (no_backend)
2254     {
2255       error_at (UNKNOWN_LOCATION, "self-tests incompatible with %<-E%>");
2256       return;
2257     }
2258 #if CHECKING_P
2259   /* Reset some state.  */
2260   input_location = UNKNOWN_LOCATION;
2261   bitmap_obstack_initialize (NULL);
2262 
2263   /* Run the tests; any failures will lead to an abort of the process.
2264      Use "make selftests-gdb" to run under the debugger.  */
2265   ::selftest::run_tests ();
2266 
2267   /* Cleanup.  */
2268   bitmap_obstack_release (NULL);
2269 #else
2270   inform (UNKNOWN_LOCATION, "self-tests are not enabled in this build");
2271 #endif /* #if CHECKING_P */
2272 }
2273 
2274 /* Entry point of cc1, cc1plus, jc1, f771, etc.
2275    Exit code is FATAL_EXIT_CODE if can't open files or if there were
2276    any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
2277 
2278    It is not safe to call this function more than once.  */
2279 
2280 int
main(int argc,char ** argv)2281 toplev::main (int argc, char **argv)
2282 {
2283   /* Parsing and gimplification sometimes need quite large stack.
2284      Increase stack size limits if possible.  */
2285   stack_limit_increase (64 * 1024 * 1024);
2286 
2287   expandargv (&argc, &argv);
2288 
2289   /* Initialization of GCC's environment, and diagnostics.  */
2290   general_init (argv[0], m_init_signals);
2291 
2292   /* One-off initialization of options that does not need to be
2293      repeated when options are added for particular functions.  */
2294   init_options_once ();
2295   init_opts_obstack ();
2296 
2297   /* Initialize global options structures; this must be repeated for
2298      each structure used for parsing options.  */
2299   init_options_struct (&global_options, &global_options_set);
2300   lang_hooks.init_options_struct (&global_options);
2301 
2302   /* Init GGC heuristics must be caller after we initialize
2303      options.  */
2304   init_ggc_heuristics ();
2305 
2306   /* Convert the options to an array.  */
2307   decode_cmdline_options_to_array_default_mask (argc,
2308 						CONST_CAST2 (const char **,
2309 							     char **, argv),
2310 						&save_decoded_options,
2311 						&save_decoded_options_count);
2312 
2313   /* Perform language-specific options initialization.  */
2314   lang_hooks.init_options (save_decoded_options_count, save_decoded_options);
2315 
2316   /* Parse the options and do minimal processing; basically just
2317      enough to default flags appropriately.  */
2318   decode_options (&global_options, &global_options_set,
2319 		  save_decoded_options, save_decoded_options_count,
2320 		  UNKNOWN_LOCATION, global_dc,
2321 		  targetm.target_option.override);
2322 
2323   handle_common_deferred_options ();
2324 
2325   init_local_tick ();
2326 
2327   initialize_plugins ();
2328 
2329   if (version_flag)
2330     print_version (stderr, "", true);
2331 
2332   if (help_flag)
2333     print_plugins_help (stderr, "");
2334 
2335   /* Exit early if we can (e.g. -help).  */
2336   if (!exit_after_options)
2337     {
2338       if (m_use_TV_TOTAL)
2339 	start_timevars ();
2340       do_compile ();
2341     }
2342 
2343   if (warningcount || errorcount || werrorcount)
2344     print_ignored_options ();
2345 
2346   if (flag_self_test)
2347     run_self_tests ();
2348 
2349   /* Invoke registered plugin callbacks if any.  Some plugins could
2350      emit some diagnostics here.  */
2351   invoke_plugin_callbacks (PLUGIN_FINISH, NULL);
2352 
2353   if (flag_diagnostics_generate_patch)
2354     {
2355       gcc_assert (global_dc->edit_context_ptr);
2356 
2357       pretty_printer pp;
2358       pp_show_color (&pp) = pp_show_color (global_dc->printer);
2359       global_dc->edit_context_ptr->print_diff (&pp, true);
2360       pp_flush (&pp);
2361     }
2362 
2363   diagnostic_finish (global_dc);
2364 
2365   finalize_plugins ();
2366 
2367   after_memory_report = true;
2368 
2369   if (seen_error () || werrorcount)
2370     return (FATAL_EXIT_CODE);
2371 
2372   return (SUCCESS_EXIT_CODE);
2373 }
2374 
2375 /* For those that want to, this function aims to clean up enough state that
2376    you can call toplev::main again. */
2377 void
finalize(void)2378 toplev::finalize (void)
2379 {
2380   rtl_initialized = false;
2381   this_target_rtl->target_specific_initialized = false;
2382 
2383   /* Needs to be called before cgraph_c_finalize since it uses symtab.  */
2384   ipa_reference_c_finalize ();
2385   ipa_fnsummary_c_finalize ();
2386   ipa_modref_c_finalize ();
2387 
2388   cgraph_c_finalize ();
2389   cgraphunit_c_finalize ();
2390   symtab_thunks_cc_finalize ();
2391   dwarf2out_c_finalize ();
2392   gcse_c_finalize ();
2393   ipa_cp_c_finalize ();
2394   ira_costs_c_finalize ();
2395 
2396   /* save_decoded_options uses opts_obstack, so these must
2397      be cleaned up together.  */
2398   obstack_free (&opts_obstack, NULL);
2399   XDELETEVEC (save_decoded_options);
2400   save_decoded_options = NULL;
2401   save_decoded_options_count = 0;
2402 
2403   /* Clean up the context (and pass_manager etc). */
2404   delete g;
2405   g = NULL;
2406 
2407 }
2408