Limit dump_flag enum values range (PR middle-end/86645).
[official-gcc.git] / gcc / dumpfile.c
blob176c9b846d7dc5ebbbdb1a88753b1fac663443e6
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"
32 #include "backend.h" /* for gimple.h. */
33 #include "gimple.h" /* for dump_user_location_t ctor. */
34 #include "rtl.h" /* for dump_user_location_t ctor. */
35 #include "selftest.h"
36 #include "optinfo.h"
37 #include "dump-context.h"
38 #include "cgraph.h"
39 #include "tree-pass.h" /* for "current_pass". */
40 #include "optinfo-emit-json.h"
42 /* If non-NULL, return one past-the-end of the matching SUBPART of
43 the WHOLE string. */
44 #define skip_leading_substring(whole, part) \
45 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
47 static dump_flags_t pflags; /* current dump_flags */
49 static void dump_loc (dump_flags_t, FILE *, source_location);
51 /* Current -fopt-info output stream, if any, and flags. */
52 static FILE *alt_dump_file = NULL;
53 static dump_flags_t alt_flags;
55 static FILE *dump_open_alternate_stream (struct dump_file_info *);
57 /* These are currently used for communicating between passes.
58 However, instead of accessing them directly, the passes can use
59 dump_printf () for dumps. */
60 FILE *dump_file = NULL;
61 const char *dump_file_name;
62 dump_flags_t dump_flags;
63 bool dumps_are_enabled = false;
66 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
67 or alt_dump_file change. */
69 static void
70 refresh_dumps_are_enabled ()
72 dumps_are_enabled = (dump_file || alt_dump_file || optinfo_enabled_p ());
75 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
76 global. */
78 void
79 set_dump_file (FILE *new_dump_file)
81 dumpfile_ensure_any_optinfo_are_flushed ();
82 dump_file = new_dump_file;
83 refresh_dumps_are_enabled ();
86 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
87 global. */
89 static void
90 set_alt_dump_file (FILE *new_alt_dump_file)
92 dumpfile_ensure_any_optinfo_are_flushed ();
93 alt_dump_file = new_alt_dump_file;
94 refresh_dumps_are_enabled ();
97 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
98 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
99 OPTGROUP_NONE, 0, 0, num, false, false}
101 /* Table of tree dump switches. This must be consistent with the
102 TREE_DUMP_INDEX enumeration in dumpfile.h. */
103 static struct dump_file_info dump_files[TDI_end] =
105 DUMP_FILE_INFO (NULL, NULL, DK_none, 0),
106 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa, 0),
107 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa, 0),
108 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa, 0),
109 DUMP_FILE_INFO (".original", "tree-original", DK_tree, 0),
110 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree, 0),
111 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree, 0),
112 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa, 0),
113 #define FIRST_AUTO_NUMBERED_DUMP 1
114 #define FIRST_ME_AUTO_NUMBERED_DUMP 4
116 DUMP_FILE_INFO (NULL, "lang-all", DK_lang, 0),
117 DUMP_FILE_INFO (NULL, "tree-all", DK_tree, 0),
118 DUMP_FILE_INFO (NULL, "rtl-all", DK_rtl, 0),
119 DUMP_FILE_INFO (NULL, "ipa-all", DK_ipa, 0),
122 /* Table of dump options. This must be consistent with the TDF_* flags
123 in dumpfile.h and opt_info_options below. */
124 static const kv_pair<dump_flags_t> dump_options[] =
126 {"address", TDF_ADDRESS},
127 {"asmname", TDF_ASMNAME},
128 {"slim", TDF_SLIM},
129 {"raw", TDF_RAW},
130 {"graph", TDF_GRAPH},
131 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
132 | MSG_MISSED_OPTIMIZATION
133 | MSG_NOTE)},
134 {"cselib", TDF_CSELIB},
135 {"stats", TDF_STATS},
136 {"blocks", TDF_BLOCKS},
137 {"vops", TDF_VOPS},
138 {"lineno", TDF_LINENO},
139 {"uid", TDF_UID},
140 {"stmtaddr", TDF_STMTADDR},
141 {"memsyms", TDF_MEMSYMS},
142 {"eh", TDF_EH},
143 {"alias", TDF_ALIAS},
144 {"nouid", TDF_NOUID},
145 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
146 {"scev", TDF_SCEV},
147 {"gimple", TDF_GIMPLE},
148 {"folding", TDF_FOLDING},
149 {"optimized", MSG_OPTIMIZED_LOCATIONS},
150 {"missed", MSG_MISSED_OPTIMIZATION},
151 {"note", MSG_NOTE},
152 {"optall", MSG_ALL},
153 {"all", dump_flags_t (TDF_ALL_VALUES
154 & ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
155 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
156 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
157 {NULL, TDF_NONE}
160 /* A subset of the dump_options table which is used for -fopt-info
161 types. This must be consistent with the MSG_* flags in dumpfile.h.
163 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
165 {"optimized", MSG_OPTIMIZED_LOCATIONS},
166 {"missed", MSG_MISSED_OPTIMIZATION},
167 {"note", MSG_NOTE},
168 {"all", MSG_ALL},
169 {NULL, TDF_NONE}
172 /* Flags used for -fopt-info groups. */
173 const kv_pair<optgroup_flags_t> optgroup_options[] =
175 {"ipa", OPTGROUP_IPA},
176 {"loop", OPTGROUP_LOOP},
177 {"inline", OPTGROUP_INLINE},
178 {"omp", OPTGROUP_OMP},
179 {"vec", OPTGROUP_VEC},
180 {"optall", OPTGROUP_ALL},
181 {NULL, OPTGROUP_NONE}
184 gcc::dump_manager::dump_manager ():
185 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
186 m_extra_dump_files (NULL),
187 m_extra_dump_files_in_use (0),
188 m_extra_dump_files_alloced (0)
192 gcc::dump_manager::~dump_manager ()
194 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
196 dump_file_info *dfi = &m_extra_dump_files[i];
197 /* suffix, swtch, glob are statically allocated for the entries
198 in dump_files, and for statistics, but are dynamically allocated
199 for those for passes. */
200 if (dfi->owns_strings)
202 XDELETEVEC (const_cast <char *> (dfi->suffix));
203 XDELETEVEC (const_cast <char *> (dfi->swtch));
204 XDELETEVEC (const_cast <char *> (dfi->glob));
206 /* These, if non-NULL, are always dynamically allocated. */
207 XDELETEVEC (const_cast <char *> (dfi->pfilename));
208 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
210 XDELETEVEC (m_extra_dump_files);
213 unsigned int
214 gcc::dump_manager::
215 dump_register (const char *suffix, const char *swtch, const char *glob,
216 dump_kind dkind, optgroup_flags_t optgroup_flags,
217 bool take_ownership)
219 int num = m_next_dump++;
221 size_t count = m_extra_dump_files_in_use++;
223 if (count >= m_extra_dump_files_alloced)
225 if (m_extra_dump_files_alloced == 0)
226 m_extra_dump_files_alloced = 512;
227 else
228 m_extra_dump_files_alloced *= 2;
229 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
230 m_extra_dump_files,
231 m_extra_dump_files_alloced);
233 /* Construct a new object in the space allocated above. */
234 new (m_extra_dump_files + count) dump_file_info ();
236 else
238 /* Zero out the already constructed object. */
239 m_extra_dump_files[count] = dump_file_info ();
242 m_extra_dump_files[count].suffix = suffix;
243 m_extra_dump_files[count].swtch = swtch;
244 m_extra_dump_files[count].glob = glob;
245 m_extra_dump_files[count].dkind = dkind;
246 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
247 m_extra_dump_files[count].num = num;
248 m_extra_dump_files[count].owns_strings = take_ownership;
250 return count + TDI_end;
254 /* Allow languages and middle-end to register their dumps before the
255 optimization passes. */
257 void
258 gcc::dump_manager::
259 register_dumps ()
261 lang_hooks.register_dumps (this);
262 /* If this assert fails, some FE registered more than
263 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
264 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
265 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
266 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
267 dump_files[TDI_original].num = m_next_dump++;
268 dump_files[TDI_gimple].num = m_next_dump++;
269 dump_files[TDI_nested].num = m_next_dump++;
273 /* Return the dump_file_info for the given phase. */
275 struct dump_file_info *
276 gcc::dump_manager::
277 get_dump_file_info (int phase) const
279 if (phase < TDI_end)
280 return &dump_files[phase];
281 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
282 return NULL;
283 else
284 return m_extra_dump_files + (phase - TDI_end);
287 /* Locate the dump_file_info with swtch equal to SWTCH,
288 or return NULL if no such dump_file_info exists. */
290 struct dump_file_info *
291 gcc::dump_manager::
292 get_dump_file_info_by_switch (const char *swtch) const
294 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
295 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
296 return &m_extra_dump_files[i];
298 /* Not found. */
299 return NULL;
303 /* Return the name of the dump file for the given phase.
304 The caller is responsible for calling free on the returned
305 buffer.
306 If the dump is not enabled, returns NULL. */
308 char *
309 gcc::dump_manager::
310 get_dump_file_name (int phase, int part) const
312 struct dump_file_info *dfi;
314 if (phase == TDI_none)
315 return NULL;
317 dfi = get_dump_file_info (phase);
319 return get_dump_file_name (dfi, part);
322 /* Return the name of the dump file for the given dump_file_info.
323 The caller is responsible for calling free on the returned
324 buffer.
325 If the dump is not enabled, returns NULL. */
327 char *
328 gcc::dump_manager::
329 get_dump_file_name (struct dump_file_info *dfi, int part) const
331 char dump_id[10];
333 gcc_assert (dfi);
335 if (dfi->pstate == 0)
336 return NULL;
338 /* If available, use the command line dump filename. */
339 if (dfi->pfilename)
340 return xstrdup (dfi->pfilename);
342 if (dfi->num < 0)
343 dump_id[0] = '\0';
344 else
346 /* (null), LANG, TREE, RTL, IPA. */
347 char suffix = " ltri"[dfi->dkind];
349 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
350 dump_id[0] = '\0';
353 if (part != -1)
355 char part_id[8];
356 snprintf (part_id, sizeof (part_id), ".%i", part);
357 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
359 else
360 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
363 /* Open a dump file called FILENAME. Some filenames are special and
364 refer to the standard streams. TRUNC indicates whether this is the
365 first open (so the file should be truncated, rather than appended).
366 An error message is emitted in the event of failure. */
368 static FILE *
369 dump_open (const char *filename, bool trunc)
371 if (strcmp ("stderr", filename) == 0)
372 return stderr;
374 if (strcmp ("stdout", filename) == 0
375 || strcmp ("-", filename) == 0)
376 return stdout;
378 FILE *stream = fopen (filename, trunc ? "w" : "a");
380 if (!stream)
381 error ("could not open dump file %qs: %m", filename);
382 return stream;
385 /* For a given DFI, open an alternate dump filename (which could also
386 be a standard stream such as stdout/stderr). If the alternate dump
387 file cannot be opened, return NULL. */
389 static FILE *
390 dump_open_alternate_stream (struct dump_file_info *dfi)
392 if (!dfi->alt_filename)
393 return NULL;
395 if (dfi->alt_stream)
396 return dfi->alt_stream;
398 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
400 if (stream)
401 dfi->alt_state = 1;
403 return stream;
406 /* Construct a dump_user_location_t from STMT (using its location and
407 hotness). */
409 dump_user_location_t::dump_user_location_t (gimple *stmt)
410 : m_count (), m_loc (UNKNOWN_LOCATION)
412 if (stmt)
414 if (stmt->bb)
415 m_count = stmt->bb->count;
416 m_loc = gimple_location (stmt);
420 /* Construct a dump_user_location_t from an RTL instruction (using its
421 location and hotness). */
423 dump_user_location_t::dump_user_location_t (rtx_insn *insn)
424 : m_count (), m_loc (UNKNOWN_LOCATION)
426 if (insn)
428 basic_block bb = BLOCK_FOR_INSN (insn);
429 if (bb)
430 m_count = bb->count;
431 m_loc = INSN_LOCATION (insn);
435 /* Construct from a function declaration. This one requires spelling out
436 to avoid accidentally constructing from other kinds of tree. */
438 dump_user_location_t
439 dump_user_location_t::from_function_decl (tree fndecl)
441 gcc_assert (fndecl);
443 // FIXME: profile count for function?
444 return dump_user_location_t (profile_count (),
445 DECL_SOURCE_LOCATION (fndecl));
448 /* Print source location on DFILE if enabled. */
450 static void
451 dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
453 if (dump_kind)
455 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
456 fprintf (dfile, "%s:%d:%d: note: ", LOCATION_FILE (loc),
457 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
458 else if (current_function_decl)
459 fprintf (dfile, "%s:%d:%d: note: ",
460 DECL_SOURCE_FILE (current_function_decl),
461 DECL_SOURCE_LINE (current_function_decl),
462 DECL_SOURCE_COLUMN (current_function_decl));
463 /* Indentation based on scope depth. */
464 fprintf (dfile, "%*s", get_dump_scope_depth (), "");
468 /* Implementation of dump_context member functions. */
470 /* dump_context's dtor. */
472 dump_context::~dump_context ()
474 delete m_pending;
477 /* Dump gimple statement GS with SPC indentation spaces and
478 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
480 void
481 dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
482 dump_flags_t extra_dump_flags,
483 gimple *gs, int spc)
485 if (dump_file && (dump_kind & pflags))
486 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
488 if (alt_dump_file && (dump_kind & alt_flags))
489 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
491 if (optinfo_enabled_p ())
493 optinfo &info = ensure_pending_optinfo ();
494 info.handle_dump_file_kind (dump_kind);
495 info.add_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
499 /* Similar to dump_gimple_stmt, except additionally print source location. */
501 void
502 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind,
503 const dump_location_t &loc,
504 dump_flags_t extra_dump_flags,
505 gimple *gs, int spc)
507 location_t srcloc = loc.get_location_t ();
508 if (dump_file && (dump_kind & pflags))
510 dump_loc (dump_kind, dump_file, srcloc);
511 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
514 if (alt_dump_file && (dump_kind & alt_flags))
516 dump_loc (dump_kind, alt_dump_file, srcloc);
517 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
520 if (optinfo_enabled_p ())
522 optinfo &info = begin_next_optinfo (loc);
523 info.handle_dump_file_kind (dump_kind);
524 info.add_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
528 /* Dump gimple statement GS with SPC indentation spaces and
529 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
530 Do not terminate with a newline or semicolon. */
532 void
533 dump_context::dump_gimple_expr (dump_flags_t dump_kind,
534 dump_flags_t extra_dump_flags,
535 gimple *gs, int spc)
537 if (dump_file && (dump_kind & pflags))
538 print_gimple_expr (dump_file, gs, spc, dump_flags | extra_dump_flags);
540 if (alt_dump_file && (dump_kind & alt_flags))
541 print_gimple_expr (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
543 if (optinfo_enabled_p ())
545 optinfo &info = ensure_pending_optinfo ();
546 info.handle_dump_file_kind (dump_kind);
547 info.add_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
551 /* Similar to dump_gimple_expr, except additionally print source location. */
553 void
554 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind,
555 const dump_location_t &loc,
556 dump_flags_t extra_dump_flags,
557 gimple *gs,
558 int spc)
560 location_t srcloc = loc.get_location_t ();
561 if (dump_file && (dump_kind & pflags))
563 dump_loc (dump_kind, dump_file, srcloc);
564 print_gimple_expr (dump_file, gs, spc, dump_flags | extra_dump_flags);
567 if (alt_dump_file && (dump_kind & alt_flags))
569 dump_loc (dump_kind, alt_dump_file, srcloc);
570 print_gimple_expr (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
573 if (optinfo_enabled_p ())
575 optinfo &info = begin_next_optinfo (loc);
576 info.handle_dump_file_kind (dump_kind);
577 info.add_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
582 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
583 DUMP_KIND is enabled. */
585 void
586 dump_context::dump_generic_expr (dump_flags_t dump_kind,
587 dump_flags_t extra_dump_flags,
588 tree t)
590 if (dump_file && (dump_kind & pflags))
591 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
593 if (alt_dump_file && (dump_kind & alt_flags))
594 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
596 if (optinfo_enabled_p ())
598 optinfo &info = ensure_pending_optinfo ();
599 info.handle_dump_file_kind (dump_kind);
600 info.add_tree (t, dump_flags | extra_dump_flags);
605 /* Similar to dump_generic_expr, except additionally print the source
606 location. */
608 void
609 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind,
610 const dump_location_t &loc,
611 dump_flags_t extra_dump_flags,
612 tree t)
614 location_t srcloc = loc.get_location_t ();
615 if (dump_file && (dump_kind & pflags))
617 dump_loc (dump_kind, dump_file, srcloc);
618 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
621 if (alt_dump_file && (dump_kind & alt_flags))
623 dump_loc (dump_kind, alt_dump_file, srcloc);
624 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
627 if (optinfo_enabled_p ())
629 optinfo &info = begin_next_optinfo (loc);
630 info.handle_dump_file_kind (dump_kind);
631 info.add_tree (t, dump_flags | extra_dump_flags);
635 /* Output a formatted message using FORMAT on appropriate dump streams. */
637 void
638 dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
639 va_list ap)
641 if (dump_file && (dump_kind & pflags))
643 va_list aq;
644 va_copy (aq, ap);
645 vfprintf (dump_file, format, aq);
646 va_end (aq);
649 if (alt_dump_file && (dump_kind & alt_flags))
651 va_list aq;
652 va_copy (aq, ap);
653 vfprintf (alt_dump_file, format, aq);
654 va_end (aq);
657 if (optinfo_enabled_p ())
659 optinfo &info = ensure_pending_optinfo ();
660 va_list aq;
661 va_copy (aq, ap);
662 info.add_printf_va (format, aq);
663 va_end (aq);
667 /* Similar to dump_printf, except source location is also printed, and
668 dump location captured. */
670 void
671 dump_context::dump_printf_loc_va (dump_flags_t dump_kind,
672 const dump_location_t &loc,
673 const char *format, va_list ap)
675 location_t srcloc = loc.get_location_t ();
677 if (dump_file && (dump_kind & pflags))
679 dump_loc (dump_kind, dump_file, srcloc);
680 va_list aq;
681 va_copy (aq, ap);
682 vfprintf (dump_file, format, aq);
683 va_end (aq);
686 if (alt_dump_file && (dump_kind & alt_flags))
688 dump_loc (dump_kind, alt_dump_file, srcloc);
689 va_list aq;
690 va_copy (aq, ap);
691 vfprintf (alt_dump_file, format, aq);
692 va_end (aq);
695 if (optinfo_enabled_p ())
697 optinfo &info = begin_next_optinfo (loc);
698 info.handle_dump_file_kind (dump_kind);
699 va_list aq;
700 va_copy (aq, ap);
701 info.add_printf_va (format, aq);
702 va_end (aq);
706 /* Output VALUE in decimal to appropriate dump streams. */
708 template<unsigned int N, typename C>
709 void
710 dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
712 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
713 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
714 if (dump_file && (dump_kind & pflags))
715 print_dec (value, dump_file, sgn);
717 if (alt_dump_file && (dump_kind & alt_flags))
718 print_dec (value, alt_dump_file, sgn);
720 if (optinfo_enabled_p ())
722 optinfo &info = ensure_pending_optinfo ();
723 info.handle_dump_file_kind (dump_kind);
724 info.add_poly_int<N,C> (value);
728 /* Output the name of NODE on appropriate dump streams. */
730 void
731 dump_context::dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
733 if (dump_file && (dump_kind & pflags))
734 fprintf (dump_file, "%s", node->dump_name ());
736 if (alt_dump_file && (dump_kind & alt_flags))
737 fprintf (alt_dump_file, "%s", node->dump_name ());
739 if (optinfo_enabled_p ())
741 optinfo &info = ensure_pending_optinfo ();
742 info.handle_dump_file_kind (dump_kind);
743 info.add_symtab_node (node);
747 /* Get the current dump scope-nesting depth.
748 For use by -fopt-info (for showing nesting via indentation). */
750 unsigned int
751 dump_context::get_scope_depth () const
753 return m_scope_depth;
756 /* Push a nested dump scope.
757 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
758 destination, if any.
759 Emit a "scope" optinfo if optinfos are enabled.
760 Increment the scope depth. */
762 void
763 dump_context::begin_scope (const char *name, const dump_location_t &loc)
765 /* Specialcase, to avoid going through dump_printf_loc,
766 so that we can create a optinfo of kind OPTINFO_KIND_SCOPE. */
768 if (dump_file)
770 dump_loc (MSG_NOTE, dump_file, loc.get_location_t ());
771 fprintf (dump_file, "=== %s ===\n", name);
774 if (alt_dump_file)
776 dump_loc (MSG_NOTE, alt_dump_file, loc.get_location_t ());
777 fprintf (alt_dump_file, "=== %s ===\n", name);
780 if (optinfo_enabled_p ())
782 end_any_optinfo ();
783 optinfo info (loc, OPTINFO_KIND_SCOPE, current_pass);
784 info.add_printf ("=== %s ===", name);
785 info.emit ();
788 m_scope_depth++;
791 /* Pop a nested dump scope. */
793 void
794 dump_context::end_scope ()
796 end_any_optinfo ();
797 m_scope_depth--;
798 optimization_records_maybe_pop_dump_scope ();
801 /* Return the optinfo currently being accumulated, creating one if
802 necessary. */
804 optinfo &
805 dump_context::ensure_pending_optinfo ()
807 if (!m_pending)
808 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
809 return *m_pending;
812 /* Start a new optinfo and return it, ending any optinfo that was already
813 accumulated. */
815 optinfo &
816 dump_context::begin_next_optinfo (const dump_location_t &loc)
818 end_any_optinfo ();
819 gcc_assert (m_pending == NULL);
820 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
821 return *m_pending;
824 /* End any optinfo that has been accumulated within this context; emitting
825 it to any destinations as appropriate, such as optimization records. */
827 void
828 dump_context::end_any_optinfo ()
830 if (m_pending)
831 m_pending->emit ();
832 delete m_pending;
833 m_pending = NULL;
836 /* The current singleton dump_context, and its default. */
838 dump_context *dump_context::s_current = &dump_context::s_default;
839 dump_context dump_context::s_default;
841 /* Implementation of dump_* API calls, calling into dump_context
842 member functions. */
844 /* Dump gimple statement GS with SPC indentation spaces and
845 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
847 void
848 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
849 gimple *gs, int spc)
851 dump_context::get ().dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
854 /* Similar to dump_gimple_stmt, except additionally print source location. */
856 void
857 dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
858 dump_flags_t extra_dump_flags, gimple *gs, int spc)
860 dump_context::get ().dump_gimple_stmt_loc (dump_kind, loc, extra_dump_flags,
861 gs, spc);
864 /* Dump gimple statement GS with SPC indentation spaces and
865 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
866 Do not terminate with a newline or semicolon. */
868 void
869 dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
870 gimple *gs, int spc)
872 dump_context::get ().dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
875 /* Similar to dump_gimple_expr, except additionally print source location. */
877 void
878 dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
879 dump_flags_t extra_dump_flags, gimple *gs, int spc)
881 dump_context::get ().dump_gimple_expr_loc (dump_kind, loc, extra_dump_flags,
882 gs, spc);
885 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
886 DUMP_KIND is enabled. */
888 void
889 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
890 tree t)
892 dump_context::get ().dump_generic_expr (dump_kind, extra_dump_flags, t);
895 /* Similar to dump_generic_expr, except additionally print the source
896 location. */
898 void
899 dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
900 dump_flags_t extra_dump_flags, tree t)
902 dump_context::get ().dump_generic_expr_loc (dump_kind, loc, extra_dump_flags,
906 /* Output a formatted message using FORMAT on appropriate dump streams. */
908 void
909 dump_printf (dump_flags_t dump_kind, const char *format, ...)
911 va_list ap;
912 va_start (ap, format);
913 dump_context::get ().dump_printf_va (dump_kind, format, ap);
914 va_end (ap);
917 /* Similar to dump_printf, except source location is also printed, and
918 dump location captured. */
920 void
921 dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
922 const char *format, ...)
924 va_list ap;
925 va_start (ap, format);
926 dump_context::get ().dump_printf_loc_va (dump_kind, loc, format, ap);
927 va_end (ap);
930 /* Output VALUE in decimal to appropriate dump streams. */
932 template<unsigned int N, typename C>
933 void
934 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
936 dump_context::get ().dump_dec (dump_kind, value);
939 template void dump_dec (dump_flags_t, const poly_uint16 &);
940 template void dump_dec (dump_flags_t, const poly_int64 &);
941 template void dump_dec (dump_flags_t, const poly_uint64 &);
942 template void dump_dec (dump_flags_t, const poly_offset_int &);
943 template void dump_dec (dump_flags_t, const poly_widest_int &);
945 void
946 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
948 if (dump_file && (dump_kind & pflags))
949 print_dec (value, dump_file, sgn);
951 if (alt_dump_file && (dump_kind & alt_flags))
952 print_dec (value, alt_dump_file, sgn);
955 /* Output VALUE in hexadecimal to appropriate dump streams. */
957 void
958 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
960 if (dump_file && (dump_kind & pflags))
961 print_hex (value, dump_file);
963 if (alt_dump_file && (dump_kind & alt_flags))
964 print_hex (value, alt_dump_file);
967 /* Emit and delete the currently pending optinfo, if there is one,
968 without the caller needing to know about class dump_context. */
970 void
971 dumpfile_ensure_any_optinfo_are_flushed ()
973 dump_context::get().end_any_optinfo ();
976 /* Output the name of NODE on appropriate dump streams. */
978 void
979 dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
981 dump_context::get ().dump_symtab_node (dump_kind, node);
984 /* Get the current dump scope-nesting depth.
985 For use by -fopt-info (for showing nesting via indentation). */
987 unsigned int
988 get_dump_scope_depth ()
990 return dump_context::get ().get_scope_depth ();
993 /* Push a nested dump scope.
994 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
995 destination, if any.
996 Emit a "scope" opinfo if optinfos are enabled.
997 Increment the scope depth. */
999 void
1000 dump_begin_scope (const char *name, const dump_location_t &loc)
1002 dump_context::get ().begin_scope (name, loc);
1005 /* Pop a nested dump scope. */
1007 void
1008 dump_end_scope ()
1010 dump_context::get ().end_scope ();
1013 /* Start a dump for PHASE. Store user-supplied dump flags in
1014 *FLAG_PTR. Return the number of streams opened. Set globals
1015 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1016 set dump_flags appropriately for both pass dump stream and
1017 -fopt-info stream. */
1020 gcc::dump_manager::
1021 dump_start (int phase, dump_flags_t *flag_ptr)
1023 int count = 0;
1024 char *name;
1025 struct dump_file_info *dfi;
1026 FILE *stream;
1027 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1028 return 0;
1030 dfi = get_dump_file_info (phase);
1031 name = get_dump_file_name (phase);
1032 if (name)
1034 stream = dump_open (name, dfi->pstate < 0);
1035 if (stream)
1037 dfi->pstate = 1;
1038 count++;
1040 free (name);
1041 dfi->pstream = stream;
1042 set_dump_file (dfi->pstream);
1043 /* Initialize current dump flags. */
1044 pflags = dfi->pflags;
1047 stream = dump_open_alternate_stream (dfi);
1048 if (stream)
1050 dfi->alt_stream = stream;
1051 count++;
1052 set_alt_dump_file (dfi->alt_stream);
1053 /* Initialize current -fopt-info flags. */
1054 alt_flags = dfi->alt_flags;
1057 if (flag_ptr)
1058 *flag_ptr = dfi->pflags;
1060 return count;
1063 /* Finish a tree dump for PHASE and close associated dump streams. Also
1064 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1066 void
1067 gcc::dump_manager::
1068 dump_finish (int phase)
1070 struct dump_file_info *dfi;
1072 if (phase < 0)
1073 return;
1074 dfi = get_dump_file_info (phase);
1075 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1076 fclose (dfi->pstream);
1078 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1079 fclose (dfi->alt_stream);
1081 dfi->alt_stream = NULL;
1082 dfi->pstream = NULL;
1083 set_dump_file (NULL);
1084 set_alt_dump_file (NULL);
1085 dump_flags = TDF_NONE;
1086 alt_flags = TDF_NONE;
1087 pflags = TDF_NONE;
1090 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1091 *FLAG_PTR and returns a stream to write to. If the dump is not
1092 enabled, returns NULL.
1093 PART can be used for dump files which should be split to multiple
1094 parts. PART == -1 indicates dump file with no parts.
1095 If PART is -1, multiple calls will reopen and append to the dump file. */
1097 FILE *
1098 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1100 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1103 FILE *
1104 gcc::dump_manager::
1105 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1107 char *name;
1108 struct dump_file_info *dfi;
1109 FILE *stream;
1111 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1112 return NULL;
1114 name = get_dump_file_name (phase, part);
1115 if (!name)
1116 return NULL;
1117 dfi = get_dump_file_info (phase);
1119 /* We do not support re-opening of dump files with parts. This would require
1120 tracking pstate per part of the dump file. */
1121 stream = dump_open (name, part != -1 || dfi->pstate < 0);
1122 if (stream)
1123 dfi->pstate = 1;
1124 free (name);
1126 if (flag_ptr)
1127 *flag_ptr = dfi->pflags;
1129 /* Initialize current flags */
1130 pflags = dfi->pflags;
1131 return stream;
1134 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1135 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1136 any phase. */
1139 gcc::dump_manager::
1140 dump_phase_enabled_p (int phase) const
1142 if (phase == TDI_tree_all)
1144 size_t i;
1145 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1146 if (dump_files[i].pstate || dump_files[i].alt_state)
1147 return 1;
1148 for (i = 0; i < m_extra_dump_files_in_use; i++)
1149 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1150 return 1;
1151 return 0;
1153 else
1155 struct dump_file_info *dfi = get_dump_file_info (phase);
1156 return dfi->pstate || dfi->alt_state;
1160 /* Returns nonzero if tree dump PHASE has been initialized. */
1163 gcc::dump_manager::
1164 dump_initialized_p (int phase) const
1166 struct dump_file_info *dfi = get_dump_file_info (phase);
1167 return dfi->pstate > 0 || dfi->alt_state > 0;
1170 /* Returns the switch name of PHASE. */
1172 const char *
1173 dump_flag_name (int phase)
1175 return g->get_dumps ()->dump_flag_name (phase);
1178 const char *
1179 gcc::dump_manager::
1180 dump_flag_name (int phase) const
1182 struct dump_file_info *dfi = get_dump_file_info (phase);
1183 return dfi->swtch;
1186 /* Finish a tree dump for PHASE. STREAM is the stream created by
1187 dump_begin. */
1189 void
1190 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1192 if (stream != stderr && stream != stdout)
1193 fclose (stream);
1196 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1197 enabled tree dumps. */
1200 gcc::dump_manager::
1201 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1203 int n = 0;
1204 size_t i;
1206 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1208 if ((dump_files[i].dkind == dkind))
1210 const char *old_filename = dump_files[i].pfilename;
1211 dump_files[i].pstate = -1;
1212 dump_files[i].pflags |= flags;
1213 n++;
1214 /* Override the existing filename. */
1215 if (filename)
1217 dump_files[i].pfilename = xstrdup (filename);
1218 /* Since it is a command-line provided file, which is
1219 common to all the phases, use it in append mode. */
1220 dump_files[i].pstate = 1;
1222 if (old_filename && filename != old_filename)
1223 free (CONST_CAST (char *, old_filename));
1227 for (i = 0; i < m_extra_dump_files_in_use; i++)
1229 if ((m_extra_dump_files[i].dkind == dkind))
1231 const char *old_filename = m_extra_dump_files[i].pfilename;
1232 m_extra_dump_files[i].pstate = -1;
1233 m_extra_dump_files[i].pflags |= flags;
1234 n++;
1235 /* Override the existing filename. */
1236 if (filename)
1238 m_extra_dump_files[i].pfilename = xstrdup (filename);
1239 /* Since it is a command-line provided file, which is
1240 common to all the phases, use it in append mode. */
1241 m_extra_dump_files[i].pstate = 1;
1243 if (old_filename && filename != old_filename)
1244 free (CONST_CAST (char *, old_filename));
1248 return n;
1251 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1252 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1253 dumps. */
1256 gcc::dump_manager::
1257 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1258 const char *filename)
1260 int n = 0;
1261 size_t i;
1263 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1265 if ((dump_files[i].optgroup_flags & optgroup_flags))
1267 const char *old_filename = dump_files[i].alt_filename;
1268 /* Since this file is shared among different passes, it
1269 should be opened in append mode. */
1270 dump_files[i].alt_state = 1;
1271 dump_files[i].alt_flags |= flags;
1272 n++;
1273 /* Override the existing filename. */
1274 if (filename)
1275 dump_files[i].alt_filename = xstrdup (filename);
1276 if (old_filename && filename != old_filename)
1277 free (CONST_CAST (char *, old_filename));
1281 for (i = 0; i < m_extra_dump_files_in_use; i++)
1283 if ((m_extra_dump_files[i].optgroup_flags & optgroup_flags))
1285 const char *old_filename = m_extra_dump_files[i].alt_filename;
1286 /* Since this file is shared among different passes, it
1287 should be opened in append mode. */
1288 m_extra_dump_files[i].alt_state = 1;
1289 m_extra_dump_files[i].alt_flags |= flags;
1290 n++;
1291 /* Override the existing filename. */
1292 if (filename)
1293 m_extra_dump_files[i].alt_filename = xstrdup (filename);
1294 if (old_filename && filename != old_filename)
1295 free (CONST_CAST (char *, old_filename));
1299 return n;
1302 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1303 relevant details in the dump_files array. */
1306 gcc::dump_manager::
1307 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1309 const char *option_value;
1310 const char *ptr;
1311 dump_flags_t flags;
1313 if (doglob && !dfi->glob)
1314 return 0;
1316 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1317 if (!option_value)
1318 return 0;
1320 if (*option_value && *option_value != '-' && *option_value != '=')
1321 return 0;
1323 ptr = option_value;
1324 flags = TDF_NONE;
1326 while (*ptr)
1328 const struct kv_pair<dump_flags_t> *option_ptr;
1329 const char *end_ptr;
1330 const char *eq_ptr;
1331 unsigned length;
1333 while (*ptr == '-')
1334 ptr++;
1335 end_ptr = strchr (ptr, '-');
1336 eq_ptr = strchr (ptr, '=');
1338 if (eq_ptr && !end_ptr)
1339 end_ptr = eq_ptr;
1341 if (!end_ptr)
1342 end_ptr = ptr + strlen (ptr);
1343 length = end_ptr - ptr;
1345 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1346 if (strlen (option_ptr->name) == length
1347 && !memcmp (option_ptr->name, ptr, length))
1349 flags |= option_ptr->value;
1350 goto found;
1353 if (*ptr == '=')
1355 /* Interpret rest of the argument as a dump filename. This
1356 filename overrides other command line filenames. */
1357 if (dfi->pfilename)
1358 free (CONST_CAST (char *, dfi->pfilename));
1359 dfi->pfilename = xstrdup (ptr + 1);
1360 break;
1362 else
1363 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1364 length, ptr, dfi->swtch);
1365 found:;
1366 ptr = end_ptr;
1369 dfi->pstate = -1;
1370 dfi->pflags |= flags;
1372 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1373 known dumps. */
1374 if (dfi->suffix == NULL)
1375 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1377 return 1;
1381 gcc::dump_manager::
1382 dump_switch_p (const char *arg)
1384 size_t i;
1385 int any = 0;
1387 for (i = TDI_none + 1; i != TDI_end; i++)
1388 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1390 /* Don't glob if we got a hit already */
1391 if (!any)
1392 for (i = TDI_none + 1; i != TDI_end; i++)
1393 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1395 for (i = 0; i < m_extra_dump_files_in_use; i++)
1396 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1398 if (!any)
1399 for (i = 0; i < m_extra_dump_files_in_use; i++)
1400 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1403 return any;
1406 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1407 and filename. Return non-zero if it is a recognized switch. */
1409 static int
1410 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1411 optgroup_flags_t *optgroup_flags, char **filename)
1413 const char *option_value;
1414 const char *ptr;
1416 option_value = arg;
1417 ptr = option_value;
1419 *filename = NULL;
1420 *flags = TDF_NONE;
1421 *optgroup_flags = OPTGROUP_NONE;
1423 if (!ptr)
1424 return 1; /* Handle '-fopt-info' without any additional options. */
1426 while (*ptr)
1428 const char *end_ptr;
1429 const char *eq_ptr;
1430 unsigned length;
1432 while (*ptr == '-')
1433 ptr++;
1434 end_ptr = strchr (ptr, '-');
1435 eq_ptr = strchr (ptr, '=');
1437 if (eq_ptr && !end_ptr)
1438 end_ptr = eq_ptr;
1440 if (!end_ptr)
1441 end_ptr = ptr + strlen (ptr);
1442 length = end_ptr - ptr;
1444 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1445 option_ptr->name; option_ptr++)
1446 if (strlen (option_ptr->name) == length
1447 && !memcmp (option_ptr->name, ptr, length))
1449 *flags |= option_ptr->value;
1450 goto found;
1453 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1454 option_ptr->name; option_ptr++)
1455 if (strlen (option_ptr->name) == length
1456 && !memcmp (option_ptr->name, ptr, length))
1458 *optgroup_flags |= option_ptr->value;
1459 goto found;
1462 if (*ptr == '=')
1464 /* Interpret rest of the argument as a dump filename. This
1465 filename overrides other command line filenames. */
1466 *filename = xstrdup (ptr + 1);
1467 break;
1469 else
1471 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1472 length, ptr, arg);
1473 return 0;
1475 found:;
1476 ptr = end_ptr;
1479 return 1;
1482 /* Return non-zero if ARG is a recognized switch for
1483 -fopt-info. Return zero otherwise. */
1486 opt_info_switch_p (const char *arg)
1488 dump_flags_t flags;
1489 optgroup_flags_t optgroup_flags;
1490 char *filename;
1491 static char *file_seen = NULL;
1492 gcc::dump_manager *dumps = g->get_dumps ();
1494 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1495 return 0;
1497 if (!filename)
1498 filename = xstrdup ("stderr");
1500 /* Bail out if a different filename has been specified. */
1501 if (file_seen && strcmp (file_seen, filename))
1503 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1504 arg);
1505 return 1;
1508 file_seen = xstrdup (filename);
1509 if (!flags)
1510 flags = MSG_OPTIMIZED_LOCATIONS;
1511 if (!optgroup_flags)
1512 optgroup_flags = OPTGROUP_ALL;
1514 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1517 /* Print basic block on the dump streams. */
1519 void
1520 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1522 if (dump_file && (dump_kind & pflags))
1523 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1524 if (alt_dump_file && (dump_kind & alt_flags))
1525 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
1528 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1530 void
1531 dump_function (int phase, tree fn)
1533 FILE *stream;
1534 dump_flags_t flags;
1536 stream = dump_begin (phase, &flags);
1537 if (stream)
1539 dump_function_to_file (fn, stream, flags);
1540 dump_end (phase, stream);
1544 /* Print information from the combine pass on dump_file. */
1546 void
1547 print_combine_total_stats (void)
1549 if (dump_file)
1550 dump_combine_total_stats (dump_file);
1553 /* Enable RTL dump for all the RTL passes. */
1555 bool
1556 enable_rtl_dump_file (void)
1558 gcc::dump_manager *dumps = g->get_dumps ();
1559 int num_enabled =
1560 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
1561 NULL);
1562 return num_enabled > 0;
1565 #if CHECKING_P
1567 /* temp_dump_context's ctor. Temporarily override the dump_context
1568 (to forcibly enable optinfo-generation). */
1570 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo)
1571 : m_context (),
1572 m_saved (&dump_context ().get ())
1574 dump_context::s_current = &m_context;
1575 m_context.m_forcibly_enable_optinfo = forcibly_enable_optinfo;
1578 /* temp_dump_context's dtor. Restore the saved dump_context. */
1580 temp_dump_context::~temp_dump_context ()
1582 dump_context::s_current = m_saved;
1585 namespace selftest {
1587 /* Verify that the dump_location_t constructors capture the source location
1588 at which they were called (provided that the build compiler is sufficiently
1589 recent). */
1591 static void
1592 test_impl_location ()
1594 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
1595 /* Default ctor. */
1597 dump_location_t loc;
1598 const int expected_line = __LINE__ - 1;
1599 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1600 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1603 /* Constructing from a gimple. */
1605 dump_location_t loc ((gimple *)NULL);
1606 const int expected_line = __LINE__ - 1;
1607 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1608 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1611 /* Constructing from an rtx_insn. */
1613 dump_location_t loc ((rtx_insn *)NULL);
1614 const int expected_line = __LINE__ - 1;
1615 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1616 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1618 #endif
1621 /* Verify that ITEM has the expected values. */
1623 static void
1624 verify_item (const location &loc,
1625 const optinfo_item *item,
1626 enum optinfo_item_kind expected_kind,
1627 location_t expected_location,
1628 const char *expected_text)
1630 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
1631 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
1632 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
1635 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
1637 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
1638 SELFTEST_BEGIN_STMT \
1639 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
1640 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
1641 SELFTEST_END_STMT
1643 /* Verify that ITEM is a tree item, with the expected values. */
1645 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1646 SELFTEST_BEGIN_STMT \
1647 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
1648 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1649 SELFTEST_END_STMT
1651 /* Verify that ITEM is a gimple item, with the expected values. */
1653 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1654 SELFTEST_BEGIN_STMT \
1655 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
1656 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1657 SELFTEST_END_STMT
1659 /* Verify that calls to the dump_* API are captured and consolidated into
1660 optimization records. */
1662 static void
1663 test_capture_of_dump_calls (const line_table_case &case_)
1665 /* Generate a location_t for testing. */
1666 line_table_test ltt (case_);
1667 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
1668 linemap_line_start (line_table, 5, 100);
1669 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1670 location_t where = linemap_position_for_column (line_table, 10);
1672 dump_location_t loc = dump_location_t::from_location_t (where);
1674 /* Test of dump_printf. */
1676 temp_dump_context tmp (true);
1677 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
1679 optinfo *info = tmp.get_pending_optinfo ();
1680 ASSERT_TRUE (info != NULL);
1681 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1682 ASSERT_EQ (info->num_items (), 1);
1683 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
1686 /* Tree, via dump_generic_expr. */
1688 temp_dump_context tmp (true);
1689 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
1690 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
1692 optinfo *info = tmp.get_pending_optinfo ();
1693 ASSERT_TRUE (info != NULL);
1694 ASSERT_EQ (info->get_location_t (), where);
1695 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1696 ASSERT_EQ (info->num_items (), 2);
1697 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
1698 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
1701 /* Tree, via dump_generic_expr_loc. */
1703 temp_dump_context tmp (true);
1704 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
1706 optinfo *info = tmp.get_pending_optinfo ();
1707 ASSERT_TRUE (info != NULL);
1708 ASSERT_EQ (info->get_location_t (), where);
1709 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1710 ASSERT_EQ (info->num_items (), 1);
1711 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
1714 /* Gimple. */
1716 greturn *stmt = gimple_build_return (NULL);
1717 gimple_set_location (stmt, where);
1719 /* dump_gimple_stmt_loc. */
1721 temp_dump_context tmp (true);
1722 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
1724 optinfo *info = tmp.get_pending_optinfo ();
1725 ASSERT_TRUE (info != NULL);
1726 ASSERT_EQ (info->num_items (), 1);
1727 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
1730 /* dump_gimple_stmt. */
1732 temp_dump_context tmp (true);
1733 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
1735 optinfo *info = tmp.get_pending_optinfo ();
1736 ASSERT_TRUE (info != NULL);
1737 ASSERT_EQ (info->num_items (), 1);
1738 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
1741 /* dump_gimple_expr_loc. */
1743 temp_dump_context tmp (true);
1744 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
1746 optinfo *info = tmp.get_pending_optinfo ();
1747 ASSERT_TRUE (info != NULL);
1748 ASSERT_EQ (info->num_items (), 1);
1749 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
1752 /* dump_gimple_expr. */
1754 temp_dump_context tmp (true);
1755 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
1757 optinfo *info = tmp.get_pending_optinfo ();
1758 ASSERT_TRUE (info != NULL);
1759 ASSERT_EQ (info->num_items (), 1);
1760 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
1764 /* poly_int. */
1766 temp_dump_context tmp (true);
1767 dump_dec (MSG_NOTE, poly_int64 (42));
1769 optinfo *info = tmp.get_pending_optinfo ();
1770 ASSERT_TRUE (info != NULL);
1771 ASSERT_EQ (info->num_items (), 1);
1772 ASSERT_IS_TEXT (info->get_item (0), "42");
1775 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
1776 above. */
1778 /* MSG_OPTIMIZED_LOCATIONS. */
1780 temp_dump_context tmp (true);
1781 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
1782 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
1783 OPTINFO_KIND_SUCCESS);
1786 /* MSG_MISSED_OPTIMIZATION. */
1788 temp_dump_context tmp (true);
1789 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
1790 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
1791 OPTINFO_KIND_FAILURE);
1796 /* Run all of the selftests within this file. */
1798 void
1799 dumpfile_c_tests ()
1801 test_impl_location ();
1802 for_each_line_table_case (test_capture_of_dump_calls);
1805 } // namespace selftest
1807 #endif /* CHECKING_P */