2018-06-09 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / dumpfile.c
blob3dd41c42a7fa409ff312900b04ceae4f28929113
1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "options.h"
24 #include "tree.h"
25 #include "gimple-pretty-print.h"
26 #include "diagnostic-core.h"
27 #include "dumpfile.h"
28 #include "context.h"
29 #include "profile-count.h"
30 #include "tree-cfg.h"
31 #include "langhooks.h"
33 /* If non-NULL, return one past-the-end of the matching SUBPART of
34 the WHOLE string. */
35 #define skip_leading_substring(whole, part) \
36 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
38 static dump_flags_t pflags; /* current dump_flags */
39 static dump_flags_t alt_flags; /* current opt_info flags */
41 static void dump_loc (dump_flags_t, FILE *, source_location);
42 static FILE *dump_open_alternate_stream (struct dump_file_info *);
44 /* These are currently used for communicating between passes.
45 However, instead of accessing them directly, the passes can use
46 dump_printf () for dumps. */
47 FILE *dump_file = NULL;
48 FILE *alt_dump_file = NULL;
49 const char *dump_file_name;
50 dump_flags_t dump_flags;
52 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
53 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
54 OPTGROUP_NONE, 0, 0, num, false, false}
56 /* Table of tree dump switches. This must be consistent with the
57 TREE_DUMP_INDEX enumeration in dumpfile.h. */
58 static struct dump_file_info dump_files[TDI_end] =
60 DUMP_FILE_INFO (NULL, NULL, DK_none, 0),
61 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa, 0),
62 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa, 0),
63 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa, 0),
64 DUMP_FILE_INFO (".original", "tree-original", DK_tree, 0),
65 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree, 0),
66 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree, 0),
67 #define FIRST_AUTO_NUMBERED_DUMP 1
68 #define FIRST_ME_AUTO_NUMBERED_DUMP 4
70 DUMP_FILE_INFO (NULL, "lang-all", DK_lang, 0),
71 DUMP_FILE_INFO (NULL, "tree-all", DK_tree, 0),
72 DUMP_FILE_INFO (NULL, "rtl-all", DK_rtl, 0),
73 DUMP_FILE_INFO (NULL, "ipa-all", DK_ipa, 0),
76 /* Define a name->number mapping for a dump flag value. */
77 template <typename ValueType>
78 struct kv_pair
80 const char *const name; /* the name of the value */
81 const ValueType value; /* the value of the name */
84 /* Table of dump options. This must be consistent with the TDF_* flags
85 in dumpfile.h and opt_info_options below. */
86 static const kv_pair<dump_flags_t> dump_options[] =
88 {"address", TDF_ADDRESS},
89 {"asmname", TDF_ASMNAME},
90 {"slim", TDF_SLIM},
91 {"raw", TDF_RAW},
92 {"graph", TDF_GRAPH},
93 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
94 | MSG_MISSED_OPTIMIZATION
95 | MSG_NOTE)},
96 {"cselib", TDF_CSELIB},
97 {"stats", TDF_STATS},
98 {"blocks", TDF_BLOCKS},
99 {"vops", TDF_VOPS},
100 {"lineno", TDF_LINENO},
101 {"uid", TDF_UID},
102 {"stmtaddr", TDF_STMTADDR},
103 {"memsyms", TDF_MEMSYMS},
104 {"eh", TDF_EH},
105 {"alias", TDF_ALIAS},
106 {"nouid", TDF_NOUID},
107 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
108 {"scev", TDF_SCEV},
109 {"gimple", TDF_GIMPLE},
110 {"folding", TDF_FOLDING},
111 {"optimized", MSG_OPTIMIZED_LOCATIONS},
112 {"missed", MSG_MISSED_OPTIMIZATION},
113 {"note", MSG_NOTE},
114 {"optall", MSG_ALL},
115 {"all", dump_flags_t (~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
116 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
117 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
118 {NULL, TDF_NONE}
121 /* A subset of the dump_options table which is used for -fopt-info
122 types. This must be consistent with the MSG_* flags in dumpfile.h.
124 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
126 {"optimized", MSG_OPTIMIZED_LOCATIONS},
127 {"missed", MSG_MISSED_OPTIMIZATION},
128 {"note", MSG_NOTE},
129 {"all", MSG_ALL},
130 {NULL, TDF_NONE}
133 /* Flags used for -fopt-info groups. */
134 static const kv_pair<optgroup_flags_t> optgroup_options[] =
136 {"ipa", OPTGROUP_IPA},
137 {"loop", OPTGROUP_LOOP},
138 {"inline", OPTGROUP_INLINE},
139 {"omp", OPTGROUP_OMP},
140 {"vec", OPTGROUP_VEC},
141 {"optall", OPTGROUP_ALL},
142 {NULL, OPTGROUP_NONE}
145 gcc::dump_manager::dump_manager ():
146 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
147 m_extra_dump_files (NULL),
148 m_extra_dump_files_in_use (0),
149 m_extra_dump_files_alloced (0)
153 gcc::dump_manager::~dump_manager ()
155 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
157 dump_file_info *dfi = &m_extra_dump_files[i];
158 /* suffix, swtch, glob are statically allocated for the entries
159 in dump_files, and for statistics, but are dynamically allocated
160 for those for passes. */
161 if (dfi->owns_strings)
163 XDELETEVEC (const_cast <char *> (dfi->suffix));
164 XDELETEVEC (const_cast <char *> (dfi->swtch));
165 XDELETEVEC (const_cast <char *> (dfi->glob));
167 /* These, if non-NULL, are always dynamically allocated. */
168 XDELETEVEC (const_cast <char *> (dfi->pfilename));
169 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
171 XDELETEVEC (m_extra_dump_files);
174 unsigned int
175 gcc::dump_manager::
176 dump_register (const char *suffix, const char *swtch, const char *glob,
177 dump_kind dkind, optgroup_flags_t optgroup_flags,
178 bool take_ownership)
180 int num = m_next_dump++;
182 size_t count = m_extra_dump_files_in_use++;
184 if (count >= m_extra_dump_files_alloced)
186 if (m_extra_dump_files_alloced == 0)
187 m_extra_dump_files_alloced = 512;
188 else
189 m_extra_dump_files_alloced *= 2;
190 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
191 m_extra_dump_files,
192 m_extra_dump_files_alloced);
194 /* Construct a new object in the space allocated above. */
195 new (m_extra_dump_files + count) dump_file_info ();
197 else
199 /* Zero out the already constructed object. */
200 m_extra_dump_files[count] = dump_file_info ();
203 m_extra_dump_files[count].suffix = suffix;
204 m_extra_dump_files[count].swtch = swtch;
205 m_extra_dump_files[count].glob = glob;
206 m_extra_dump_files[count].dkind = dkind;
207 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
208 m_extra_dump_files[count].num = num;
209 m_extra_dump_files[count].owns_strings = take_ownership;
211 return count + TDI_end;
215 /* Allow languages and middle-end to register their dumps before the
216 optimization passes. */
218 void
219 gcc::dump_manager::
220 register_dumps ()
222 lang_hooks.register_dumps (this);
223 /* If this assert fails, some FE registered more than
224 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
225 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
226 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
227 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
228 dump_files[TDI_original].num = m_next_dump++;
229 dump_files[TDI_gimple].num = m_next_dump++;
230 dump_files[TDI_nested].num = m_next_dump++;
234 /* Return the dump_file_info for the given phase. */
236 struct dump_file_info *
237 gcc::dump_manager::
238 get_dump_file_info (int phase) const
240 if (phase < TDI_end)
241 return &dump_files[phase];
242 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
243 return NULL;
244 else
245 return m_extra_dump_files + (phase - TDI_end);
248 /* Locate the dump_file_info with swtch equal to SWTCH,
249 or return NULL if no such dump_file_info exists. */
251 struct dump_file_info *
252 gcc::dump_manager::
253 get_dump_file_info_by_switch (const char *swtch) const
255 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
256 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
257 return &m_extra_dump_files[i];
259 /* Not found. */
260 return NULL;
264 /* Return the name of the dump file for the given phase.
265 The caller is responsible for calling free on the returned
266 buffer.
267 If the dump is not enabled, returns NULL. */
269 char *
270 gcc::dump_manager::
271 get_dump_file_name (int phase) const
273 struct dump_file_info *dfi;
275 if (phase == TDI_none)
276 return NULL;
278 dfi = get_dump_file_info (phase);
280 return get_dump_file_name (dfi);
283 /* Return the name of the dump file for the given dump_file_info.
284 The caller is responsible for calling free on the returned
285 buffer.
286 If the dump is not enabled, returns NULL. */
288 char *
289 gcc::dump_manager::
290 get_dump_file_name (struct dump_file_info *dfi) const
292 char dump_id[10];
294 gcc_assert (dfi);
296 if (dfi->pstate == 0)
297 return NULL;
299 /* If available, use the command line dump filename. */
300 if (dfi->pfilename)
301 return xstrdup (dfi->pfilename);
303 if (dfi->num < 0)
304 dump_id[0] = '\0';
305 else
307 /* (null), LANG, TREE, RTL, IPA. */
308 char suffix = " ltri"[dfi->dkind];
310 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
311 dump_id[0] = '\0';
314 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
317 /* Open a dump file called FILENAME. Some filenames are special and
318 refer to the standard streams. TRUNC indicates whether this is the
319 first open (so the file should be truncated, rather than appended).
320 An error message is emitted in the event of failure. */
322 static FILE *
323 dump_open (const char *filename, bool trunc)
325 if (strcmp ("stderr", filename) == 0)
326 return stderr;
328 if (strcmp ("stdout", filename) == 0
329 || strcmp ("-", filename) == 0)
330 return stdout;
332 FILE *stream = fopen (filename, trunc ? "w" : "a");
334 if (!stream)
335 error ("could not open dump file %qs: %m", filename);
336 return stream;
339 /* For a given DFI, open an alternate dump filename (which could also
340 be a standard stream such as stdout/stderr). If the alternate dump
341 file cannot be opened, return NULL. */
343 static FILE *
344 dump_open_alternate_stream (struct dump_file_info *dfi)
346 if (!dfi->alt_filename)
347 return NULL;
349 if (dfi->alt_stream)
350 return dfi->alt_stream;
352 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
354 if (stream)
355 dfi->alt_state = 1;
357 return stream;
360 /* Print source location on DFILE if enabled. */
362 void
363 dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
365 if (dump_kind)
367 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
368 fprintf (dfile, "%s:%d:%d: note: ", LOCATION_FILE (loc),
369 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
370 else if (current_function_decl)
371 fprintf (dfile, "%s:%d:%d: note: ",
372 DECL_SOURCE_FILE (current_function_decl),
373 DECL_SOURCE_LINE (current_function_decl),
374 DECL_SOURCE_COLUMN (current_function_decl));
378 /* Dump gimple statement GS with SPC indentation spaces and
379 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
381 void
382 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
383 gimple *gs, int spc)
385 if (dump_file && (dump_kind & pflags))
386 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
388 if (alt_dump_file && (dump_kind & alt_flags))
389 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
392 /* Similar to dump_gimple_stmt, except additionally print source location. */
394 void
395 dump_gimple_stmt_loc (dump_flags_t dump_kind, source_location loc,
396 dump_flags_t extra_dump_flags, gimple *gs, int spc)
398 if (dump_file && (dump_kind & pflags))
400 dump_loc (dump_kind, dump_file, loc);
401 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
404 if (alt_dump_file && (dump_kind & alt_flags))
406 dump_loc (dump_kind, alt_dump_file, loc);
407 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
411 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
412 DUMP_KIND is enabled. */
414 void
415 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
416 tree t)
418 if (dump_file && (dump_kind & pflags))
419 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
421 if (alt_dump_file && (dump_kind & alt_flags))
422 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
426 /* Similar to dump_generic_expr, except additionally print the source
427 location. */
429 void
430 dump_generic_expr_loc (dump_flags_t dump_kind, source_location loc,
431 dump_flags_t extra_dump_flags, tree t)
433 if (dump_file && (dump_kind & pflags))
435 dump_loc (dump_kind, dump_file, loc);
436 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
439 if (alt_dump_file && (dump_kind & alt_flags))
441 dump_loc (dump_kind, alt_dump_file, loc);
442 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
446 /* Output a formatted message using FORMAT on appropriate dump streams. */
448 void
449 dump_printf (dump_flags_t dump_kind, const char *format, ...)
451 if (dump_file && (dump_kind & pflags))
453 va_list ap;
454 va_start (ap, format);
455 vfprintf (dump_file, format, ap);
456 va_end (ap);
459 if (alt_dump_file && (dump_kind & alt_flags))
461 va_list ap;
462 va_start (ap, format);
463 vfprintf (alt_dump_file, format, ap);
464 va_end (ap);
468 /* Similar to dump_printf, except source location is also printed. */
470 void
471 dump_printf_loc (dump_flags_t dump_kind, source_location loc,
472 const char *format, ...)
474 if (dump_file && (dump_kind & pflags))
476 va_list ap;
477 dump_loc (dump_kind, dump_file, loc);
478 va_start (ap, format);
479 vfprintf (dump_file, format, ap);
480 va_end (ap);
483 if (alt_dump_file && (dump_kind & alt_flags))
485 va_list ap;
486 dump_loc (dump_kind, alt_dump_file, loc);
487 va_start (ap, format);
488 vfprintf (alt_dump_file, format, ap);
489 va_end (ap);
493 /* Output VALUE in decimal to appropriate dump streams. */
495 template<unsigned int N, typename C>
496 void
497 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
499 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
500 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
501 if (dump_file && (dump_kind & pflags))
502 print_dec (value, dump_file, sgn);
504 if (alt_dump_file && (dump_kind & alt_flags))
505 print_dec (value, alt_dump_file, sgn);
508 template void dump_dec (dump_flags_t, const poly_uint16 &);
509 template void dump_dec (dump_flags_t, const poly_int64 &);
510 template void dump_dec (dump_flags_t, const poly_uint64 &);
511 template void dump_dec (dump_flags_t, const poly_offset_int &);
512 template void dump_dec (dump_flags_t, const poly_widest_int &);
514 /* Start a dump for PHASE. Store user-supplied dump flags in
515 *FLAG_PTR. Return the number of streams opened. Set globals
516 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
517 set dump_flags appropriately for both pass dump stream and
518 -fopt-info stream. */
521 gcc::dump_manager::
522 dump_start (int phase, dump_flags_t *flag_ptr)
524 int count = 0;
525 char *name;
526 struct dump_file_info *dfi;
527 FILE *stream;
528 if (phase == TDI_none || !dump_phase_enabled_p (phase))
529 return 0;
531 dfi = get_dump_file_info (phase);
532 name = get_dump_file_name (phase);
533 if (name)
535 stream = dump_open (name, dfi->pstate < 0);
536 if (stream)
538 dfi->pstate = 1;
539 count++;
541 free (name);
542 dfi->pstream = stream;
543 dump_file = dfi->pstream;
544 /* Initialize current dump flags. */
545 pflags = dfi->pflags;
548 stream = dump_open_alternate_stream (dfi);
549 if (stream)
551 dfi->alt_stream = stream;
552 count++;
553 alt_dump_file = dfi->alt_stream;
554 /* Initialize current -fopt-info flags. */
555 alt_flags = dfi->alt_flags;
558 if (flag_ptr)
559 *flag_ptr = dfi->pflags;
561 return count;
564 /* Finish a tree dump for PHASE and close associated dump streams. Also
565 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
567 void
568 gcc::dump_manager::
569 dump_finish (int phase)
571 struct dump_file_info *dfi;
573 if (phase < 0)
574 return;
575 dfi = get_dump_file_info (phase);
576 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
577 fclose (dfi->pstream);
579 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
580 fclose (dfi->alt_stream);
582 dfi->alt_stream = NULL;
583 dfi->pstream = NULL;
584 dump_file = NULL;
585 alt_dump_file = NULL;
586 dump_flags = TDF_NONE;
587 alt_flags = TDF_NONE;
588 pflags = TDF_NONE;
591 /* Begin a tree dump for PHASE. Stores any user supplied flag in
592 *FLAG_PTR and returns a stream to write to. If the dump is not
593 enabled, returns NULL.
594 Multiple calls will reopen and append to the dump file. */
596 FILE *
597 dump_begin (int phase, dump_flags_t *flag_ptr)
599 return g->get_dumps ()->dump_begin (phase, flag_ptr);
602 FILE *
603 gcc::dump_manager::
604 dump_begin (int phase, dump_flags_t *flag_ptr)
606 char *name;
607 struct dump_file_info *dfi;
608 FILE *stream;
610 if (phase == TDI_none || !dump_phase_enabled_p (phase))
611 return NULL;
613 name = get_dump_file_name (phase);
614 if (!name)
615 return NULL;
616 dfi = get_dump_file_info (phase);
618 stream = dump_open (name, dfi->pstate < 0);
619 if (stream)
620 dfi->pstate = 1;
621 free (name);
623 if (flag_ptr)
624 *flag_ptr = dfi->pflags;
626 /* Initialize current flags */
627 pflags = dfi->pflags;
628 return stream;
631 /* Returns nonzero if dump PHASE is enabled for at least one stream.
632 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
633 any phase. */
636 gcc::dump_manager::
637 dump_phase_enabled_p (int phase) const
639 if (phase == TDI_tree_all)
641 size_t i;
642 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
643 if (dump_files[i].pstate || dump_files[i].alt_state)
644 return 1;
645 for (i = 0; i < m_extra_dump_files_in_use; i++)
646 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
647 return 1;
648 return 0;
650 else
652 struct dump_file_info *dfi = get_dump_file_info (phase);
653 return dfi->pstate || dfi->alt_state;
657 /* Returns nonzero if tree dump PHASE has been initialized. */
660 gcc::dump_manager::
661 dump_initialized_p (int phase) const
663 struct dump_file_info *dfi = get_dump_file_info (phase);
664 return dfi->pstate > 0 || dfi->alt_state > 0;
667 /* Returns the switch name of PHASE. */
669 const char *
670 dump_flag_name (int phase)
672 return g->get_dumps ()->dump_flag_name (phase);
675 const char *
676 gcc::dump_manager::
677 dump_flag_name (int phase) const
679 struct dump_file_info *dfi = get_dump_file_info (phase);
680 return dfi->swtch;
683 /* Finish a tree dump for PHASE. STREAM is the stream created by
684 dump_begin. */
686 void
687 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
689 if (stream != stderr && stream != stdout)
690 fclose (stream);
693 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
694 enabled tree dumps. */
697 gcc::dump_manager::
698 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
700 int n = 0;
701 size_t i;
703 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
705 if ((dump_files[i].dkind == dkind))
707 const char *old_filename = dump_files[i].pfilename;
708 dump_files[i].pstate = -1;
709 dump_files[i].pflags |= flags;
710 n++;
711 /* Override the existing filename. */
712 if (filename)
714 dump_files[i].pfilename = xstrdup (filename);
715 /* Since it is a command-line provided file, which is
716 common to all the phases, use it in append mode. */
717 dump_files[i].pstate = 1;
719 if (old_filename && filename != old_filename)
720 free (CONST_CAST (char *, old_filename));
724 for (i = 0; i < m_extra_dump_files_in_use; i++)
726 if ((m_extra_dump_files[i].dkind == dkind))
728 const char *old_filename = m_extra_dump_files[i].pfilename;
729 m_extra_dump_files[i].pstate = -1;
730 m_extra_dump_files[i].pflags |= flags;
731 n++;
732 /* Override the existing filename. */
733 if (filename)
735 m_extra_dump_files[i].pfilename = xstrdup (filename);
736 /* Since it is a command-line provided file, which is
737 common to all the phases, use it in append mode. */
738 m_extra_dump_files[i].pstate = 1;
740 if (old_filename && filename != old_filename)
741 free (CONST_CAST (char *, old_filename));
745 return n;
748 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
749 Enable dumps with FLAGS on FILENAME. Return the number of enabled
750 dumps. */
753 gcc::dump_manager::
754 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
755 const char *filename)
757 int n = 0;
758 size_t i;
760 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
762 if ((dump_files[i].optgroup_flags & optgroup_flags))
764 const char *old_filename = dump_files[i].alt_filename;
765 /* Since this file is shared among different passes, it
766 should be opened in append mode. */
767 dump_files[i].alt_state = 1;
768 dump_files[i].alt_flags |= flags;
769 n++;
770 /* Override the existing filename. */
771 if (filename)
772 dump_files[i].alt_filename = xstrdup (filename);
773 if (old_filename && filename != old_filename)
774 free (CONST_CAST (char *, old_filename));
778 for (i = 0; i < m_extra_dump_files_in_use; i++)
780 if ((m_extra_dump_files[i].optgroup_flags & optgroup_flags))
782 const char *old_filename = m_extra_dump_files[i].alt_filename;
783 /* Since this file is shared among different passes, it
784 should be opened in append mode. */
785 m_extra_dump_files[i].alt_state = 1;
786 m_extra_dump_files[i].alt_flags |= flags;
787 n++;
788 /* Override the existing filename. */
789 if (filename)
790 m_extra_dump_files[i].alt_filename = xstrdup (filename);
791 if (old_filename && filename != old_filename)
792 free (CONST_CAST (char *, old_filename));
796 return n;
799 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
800 relevant details in the dump_files array. */
803 gcc::dump_manager::
804 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
806 const char *option_value;
807 const char *ptr;
808 dump_flags_t flags;
810 if (doglob && !dfi->glob)
811 return 0;
813 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
814 if (!option_value)
815 return 0;
817 if (*option_value && *option_value != '-' && *option_value != '=')
818 return 0;
820 ptr = option_value;
821 flags = TDF_NONE;
823 while (*ptr)
825 const struct kv_pair<dump_flags_t> *option_ptr;
826 const char *end_ptr;
827 const char *eq_ptr;
828 unsigned length;
830 while (*ptr == '-')
831 ptr++;
832 end_ptr = strchr (ptr, '-');
833 eq_ptr = strchr (ptr, '=');
835 if (eq_ptr && !end_ptr)
836 end_ptr = eq_ptr;
838 if (!end_ptr)
839 end_ptr = ptr + strlen (ptr);
840 length = end_ptr - ptr;
842 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
843 if (strlen (option_ptr->name) == length
844 && !memcmp (option_ptr->name, ptr, length))
846 flags |= option_ptr->value;
847 goto found;
850 if (*ptr == '=')
852 /* Interpret rest of the argument as a dump filename. This
853 filename overrides other command line filenames. */
854 if (dfi->pfilename)
855 free (CONST_CAST (char *, dfi->pfilename));
856 dfi->pfilename = xstrdup (ptr + 1);
857 break;
859 else
860 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
861 length, ptr, dfi->swtch);
862 found:;
863 ptr = end_ptr;
866 dfi->pstate = -1;
867 dfi->pflags |= flags;
869 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
870 known dumps. */
871 if (dfi->suffix == NULL)
872 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
874 return 1;
878 gcc::dump_manager::
879 dump_switch_p (const char *arg)
881 size_t i;
882 int any = 0;
884 for (i = TDI_none + 1; i != TDI_end; i++)
885 any |= dump_switch_p_1 (arg, &dump_files[i], false);
887 /* Don't glob if we got a hit already */
888 if (!any)
889 for (i = TDI_none + 1; i != TDI_end; i++)
890 any |= dump_switch_p_1 (arg, &dump_files[i], true);
892 for (i = 0; i < m_extra_dump_files_in_use; i++)
893 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
895 if (!any)
896 for (i = 0; i < m_extra_dump_files_in_use; i++)
897 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
900 return any;
903 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
904 and filename. Return non-zero if it is a recognized switch. */
906 static int
907 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
908 optgroup_flags_t *optgroup_flags, char **filename)
910 const char *option_value;
911 const char *ptr;
913 option_value = arg;
914 ptr = option_value;
916 *filename = NULL;
917 *flags = TDF_NONE;
918 *optgroup_flags = OPTGROUP_NONE;
920 if (!ptr)
921 return 1; /* Handle '-fopt-info' without any additional options. */
923 while (*ptr)
925 const char *end_ptr;
926 const char *eq_ptr;
927 unsigned length;
929 while (*ptr == '-')
930 ptr++;
931 end_ptr = strchr (ptr, '-');
932 eq_ptr = strchr (ptr, '=');
934 if (eq_ptr && !end_ptr)
935 end_ptr = eq_ptr;
937 if (!end_ptr)
938 end_ptr = ptr + strlen (ptr);
939 length = end_ptr - ptr;
941 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
942 option_ptr->name; option_ptr++)
943 if (strlen (option_ptr->name) == length
944 && !memcmp (option_ptr->name, ptr, length))
946 *flags |= option_ptr->value;
947 goto found;
950 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
951 option_ptr->name; option_ptr++)
952 if (strlen (option_ptr->name) == length
953 && !memcmp (option_ptr->name, ptr, length))
955 *optgroup_flags |= option_ptr->value;
956 goto found;
959 if (*ptr == '=')
961 /* Interpret rest of the argument as a dump filename. This
962 filename overrides other command line filenames. */
963 *filename = xstrdup (ptr + 1);
964 break;
966 else
968 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
969 length, ptr, arg);
970 return 0;
972 found:;
973 ptr = end_ptr;
976 return 1;
979 /* Return non-zero if ARG is a recognized switch for
980 -fopt-info. Return zero otherwise. */
983 opt_info_switch_p (const char *arg)
985 dump_flags_t flags;
986 optgroup_flags_t optgroup_flags;
987 char *filename;
988 static char *file_seen = NULL;
989 gcc::dump_manager *dumps = g->get_dumps ();
991 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
992 return 0;
994 if (!filename)
995 filename = xstrdup ("stderr");
997 /* Bail out if a different filename has been specified. */
998 if (file_seen && strcmp (file_seen, filename))
1000 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1001 arg);
1002 return 1;
1005 file_seen = xstrdup (filename);
1006 if (!flags)
1007 flags = MSG_OPTIMIZED_LOCATIONS;
1008 if (!optgroup_flags)
1009 optgroup_flags = OPTGROUP_ALL;
1011 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1014 /* Print basic block on the dump streams. */
1016 void
1017 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1019 if (dump_file && (dump_kind & pflags))
1020 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1021 if (alt_dump_file && (dump_kind & alt_flags))
1022 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
1025 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1027 void
1028 dump_function (int phase, tree fn)
1030 FILE *stream;
1031 dump_flags_t flags;
1033 stream = dump_begin (phase, &flags);
1034 if (stream)
1036 dump_function_to_file (fn, stream, flags);
1037 dump_end (phase, stream);
1041 /* Print information from the combine pass on dump_file. */
1043 void
1044 print_combine_total_stats (void)
1046 if (dump_file)
1047 dump_combine_total_stats (dump_file);
1050 /* Enable RTL dump for all the RTL passes. */
1052 bool
1053 enable_rtl_dump_file (void)
1055 gcc::dump_manager *dumps = g->get_dumps ();
1056 int num_enabled =
1057 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
1058 NULL);
1059 return num_enabled > 0;