PR middle-end/82063 - issues with arguments enabled by -Wall
[official-gcc.git] / gcc / dumpfile.c
blob6c9920c6bd2ffb02297cc15754f2055fcc2bef1e
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_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
154 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
155 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
156 {NULL, TDF_NONE}
159 /* A subset of the dump_options table which is used for -fopt-info
160 types. This must be consistent with the MSG_* flags in dumpfile.h.
162 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
164 {"optimized", MSG_OPTIMIZED_LOCATIONS},
165 {"missed", MSG_MISSED_OPTIMIZATION},
166 {"note", MSG_NOTE},
167 {"all", MSG_ALL},
168 {NULL, TDF_NONE}
171 /* Flags used for -fopt-info groups. */
172 const kv_pair<optgroup_flags_t> optgroup_options[] =
174 {"ipa", OPTGROUP_IPA},
175 {"loop", OPTGROUP_LOOP},
176 {"inline", OPTGROUP_INLINE},
177 {"omp", OPTGROUP_OMP},
178 {"vec", OPTGROUP_VEC},
179 {"optall", OPTGROUP_ALL},
180 {NULL, OPTGROUP_NONE}
183 gcc::dump_manager::dump_manager ():
184 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
185 m_extra_dump_files (NULL),
186 m_extra_dump_files_in_use (0),
187 m_extra_dump_files_alloced (0)
191 gcc::dump_manager::~dump_manager ()
193 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
195 dump_file_info *dfi = &m_extra_dump_files[i];
196 /* suffix, swtch, glob are statically allocated for the entries
197 in dump_files, and for statistics, but are dynamically allocated
198 for those for passes. */
199 if (dfi->owns_strings)
201 XDELETEVEC (const_cast <char *> (dfi->suffix));
202 XDELETEVEC (const_cast <char *> (dfi->swtch));
203 XDELETEVEC (const_cast <char *> (dfi->glob));
205 /* These, if non-NULL, are always dynamically allocated. */
206 XDELETEVEC (const_cast <char *> (dfi->pfilename));
207 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
209 XDELETEVEC (m_extra_dump_files);
212 unsigned int
213 gcc::dump_manager::
214 dump_register (const char *suffix, const char *swtch, const char *glob,
215 dump_kind dkind, optgroup_flags_t optgroup_flags,
216 bool take_ownership)
218 int num = m_next_dump++;
220 size_t count = m_extra_dump_files_in_use++;
222 if (count >= m_extra_dump_files_alloced)
224 if (m_extra_dump_files_alloced == 0)
225 m_extra_dump_files_alloced = 512;
226 else
227 m_extra_dump_files_alloced *= 2;
228 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
229 m_extra_dump_files,
230 m_extra_dump_files_alloced);
232 /* Construct a new object in the space allocated above. */
233 new (m_extra_dump_files + count) dump_file_info ();
235 else
237 /* Zero out the already constructed object. */
238 m_extra_dump_files[count] = dump_file_info ();
241 m_extra_dump_files[count].suffix = suffix;
242 m_extra_dump_files[count].swtch = swtch;
243 m_extra_dump_files[count].glob = glob;
244 m_extra_dump_files[count].dkind = dkind;
245 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
246 m_extra_dump_files[count].num = num;
247 m_extra_dump_files[count].owns_strings = take_ownership;
249 return count + TDI_end;
253 /* Allow languages and middle-end to register their dumps before the
254 optimization passes. */
256 void
257 gcc::dump_manager::
258 register_dumps ()
260 lang_hooks.register_dumps (this);
261 /* If this assert fails, some FE registered more than
262 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
263 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
264 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
265 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
266 dump_files[TDI_original].num = m_next_dump++;
267 dump_files[TDI_gimple].num = m_next_dump++;
268 dump_files[TDI_nested].num = m_next_dump++;
272 /* Return the dump_file_info for the given phase. */
274 struct dump_file_info *
275 gcc::dump_manager::
276 get_dump_file_info (int phase) const
278 if (phase < TDI_end)
279 return &dump_files[phase];
280 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
281 return NULL;
282 else
283 return m_extra_dump_files + (phase - TDI_end);
286 /* Locate the dump_file_info with swtch equal to SWTCH,
287 or return NULL if no such dump_file_info exists. */
289 struct dump_file_info *
290 gcc::dump_manager::
291 get_dump_file_info_by_switch (const char *swtch) const
293 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
294 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
295 return &m_extra_dump_files[i];
297 /* Not found. */
298 return NULL;
302 /* Return the name of the dump file for the given phase.
303 The caller is responsible for calling free on the returned
304 buffer.
305 If the dump is not enabled, returns NULL. */
307 char *
308 gcc::dump_manager::
309 get_dump_file_name (int phase, int part) const
311 struct dump_file_info *dfi;
313 if (phase == TDI_none)
314 return NULL;
316 dfi = get_dump_file_info (phase);
318 return get_dump_file_name (dfi, part);
321 /* Return the name of the dump file for the given dump_file_info.
322 The caller is responsible for calling free on the returned
323 buffer.
324 If the dump is not enabled, returns NULL. */
326 char *
327 gcc::dump_manager::
328 get_dump_file_name (struct dump_file_info *dfi, int part) const
330 char dump_id[10];
332 gcc_assert (dfi);
334 if (dfi->pstate == 0)
335 return NULL;
337 /* If available, use the command line dump filename. */
338 if (dfi->pfilename)
339 return xstrdup (dfi->pfilename);
341 if (dfi->num < 0)
342 dump_id[0] = '\0';
343 else
345 /* (null), LANG, TREE, RTL, IPA. */
346 char suffix = " ltri"[dfi->dkind];
348 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
349 dump_id[0] = '\0';
352 if (part != -1)
354 char part_id[8];
355 snprintf (part_id, sizeof (part_id), ".%i", part);
356 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
358 else
359 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
362 /* Open a dump file called FILENAME. Some filenames are special and
363 refer to the standard streams. TRUNC indicates whether this is the
364 first open (so the file should be truncated, rather than appended).
365 An error message is emitted in the event of failure. */
367 static FILE *
368 dump_open (const char *filename, bool trunc)
370 if (strcmp ("stderr", filename) == 0)
371 return stderr;
373 if (strcmp ("stdout", filename) == 0
374 || strcmp ("-", filename) == 0)
375 return stdout;
377 FILE *stream = fopen (filename, trunc ? "w" : "a");
379 if (!stream)
380 error ("could not open dump file %qs: %m", filename);
381 return stream;
384 /* For a given DFI, open an alternate dump filename (which could also
385 be a standard stream such as stdout/stderr). If the alternate dump
386 file cannot be opened, return NULL. */
388 static FILE *
389 dump_open_alternate_stream (struct dump_file_info *dfi)
391 if (!dfi->alt_filename)
392 return NULL;
394 if (dfi->alt_stream)
395 return dfi->alt_stream;
397 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
399 if (stream)
400 dfi->alt_state = 1;
402 return stream;
405 /* Construct a dump_user_location_t from STMT (using its location and
406 hotness). */
408 dump_user_location_t::dump_user_location_t (gimple *stmt)
409 : m_count (), m_loc (UNKNOWN_LOCATION)
411 if (stmt)
413 if (stmt->bb)
414 m_count = stmt->bb->count;
415 m_loc = gimple_location (stmt);
419 /* Construct a dump_user_location_t from an RTL instruction (using its
420 location and hotness). */
422 dump_user_location_t::dump_user_location_t (rtx_insn *insn)
423 : m_count (), m_loc (UNKNOWN_LOCATION)
425 if (insn)
427 basic_block bb = BLOCK_FOR_INSN (insn);
428 if (bb)
429 m_count = bb->count;
430 m_loc = INSN_LOCATION (insn);
434 /* Construct from a function declaration. This one requires spelling out
435 to avoid accidentally constructing from other kinds of tree. */
437 dump_user_location_t
438 dump_user_location_t::from_function_decl (tree fndecl)
440 gcc_assert (fndecl);
442 // FIXME: profile count for function?
443 return dump_user_location_t (profile_count (),
444 DECL_SOURCE_LOCATION (fndecl));
447 /* Print source location on DFILE if enabled. */
449 static void
450 dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
452 if (dump_kind)
454 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
455 fprintf (dfile, "%s:%d:%d: note: ", LOCATION_FILE (loc),
456 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
457 else if (current_function_decl)
458 fprintf (dfile, "%s:%d:%d: note: ",
459 DECL_SOURCE_FILE (current_function_decl),
460 DECL_SOURCE_LINE (current_function_decl),
461 DECL_SOURCE_COLUMN (current_function_decl));
462 /* Indentation based on scope depth. */
463 fprintf (dfile, "%*s", get_dump_scope_depth (), "");
467 /* Implementation of dump_context member functions. */
469 /* dump_context's dtor. */
471 dump_context::~dump_context ()
473 delete m_pending;
476 /* Dump gimple statement GS with SPC indentation spaces and
477 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
479 void
480 dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
481 dump_flags_t extra_dump_flags,
482 gimple *gs, int spc)
484 if (dump_file && (dump_kind & pflags))
485 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
487 if (alt_dump_file && (dump_kind & alt_flags))
488 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
490 if (optinfo_enabled_p ())
492 optinfo &info = ensure_pending_optinfo ();
493 info.handle_dump_file_kind (dump_kind);
494 info.add_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
498 /* Similar to dump_gimple_stmt, except additionally print source location. */
500 void
501 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind,
502 const dump_location_t &loc,
503 dump_flags_t extra_dump_flags,
504 gimple *gs, int spc)
506 location_t srcloc = loc.get_location_t ();
507 if (dump_file && (dump_kind & pflags))
509 dump_loc (dump_kind, dump_file, srcloc);
510 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
513 if (alt_dump_file && (dump_kind & alt_flags))
515 dump_loc (dump_kind, alt_dump_file, srcloc);
516 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
519 if (optinfo_enabled_p ())
521 optinfo &info = begin_next_optinfo (loc);
522 info.handle_dump_file_kind (dump_kind);
523 info.add_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
527 /* Dump gimple statement GS with SPC indentation spaces and
528 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
529 Do not terminate with a newline or semicolon. */
531 void
532 dump_context::dump_gimple_expr (dump_flags_t dump_kind,
533 dump_flags_t extra_dump_flags,
534 gimple *gs, int spc)
536 if (dump_file && (dump_kind & pflags))
537 print_gimple_expr (dump_file, gs, spc, dump_flags | extra_dump_flags);
539 if (alt_dump_file && (dump_kind & alt_flags))
540 print_gimple_expr (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
542 if (optinfo_enabled_p ())
544 optinfo &info = ensure_pending_optinfo ();
545 info.handle_dump_file_kind (dump_kind);
546 info.add_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
550 /* Similar to dump_gimple_expr, except additionally print source location. */
552 void
553 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind,
554 const dump_location_t &loc,
555 dump_flags_t extra_dump_flags,
556 gimple *gs,
557 int spc)
559 location_t srcloc = loc.get_location_t ();
560 if (dump_file && (dump_kind & pflags))
562 dump_loc (dump_kind, dump_file, srcloc);
563 print_gimple_expr (dump_file, gs, spc, dump_flags | extra_dump_flags);
566 if (alt_dump_file && (dump_kind & alt_flags))
568 dump_loc (dump_kind, alt_dump_file, srcloc);
569 print_gimple_expr (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
572 if (optinfo_enabled_p ())
574 optinfo &info = begin_next_optinfo (loc);
575 info.handle_dump_file_kind (dump_kind);
576 info.add_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
581 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
582 DUMP_KIND is enabled. */
584 void
585 dump_context::dump_generic_expr (dump_flags_t dump_kind,
586 dump_flags_t extra_dump_flags,
587 tree t)
589 if (dump_file && (dump_kind & pflags))
590 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
592 if (alt_dump_file && (dump_kind & alt_flags))
593 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
595 if (optinfo_enabled_p ())
597 optinfo &info = ensure_pending_optinfo ();
598 info.handle_dump_file_kind (dump_kind);
599 info.add_tree (t, dump_flags | extra_dump_flags);
604 /* Similar to dump_generic_expr, except additionally print the source
605 location. */
607 void
608 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind,
609 const dump_location_t &loc,
610 dump_flags_t extra_dump_flags,
611 tree t)
613 location_t srcloc = loc.get_location_t ();
614 if (dump_file && (dump_kind & pflags))
616 dump_loc (dump_kind, dump_file, srcloc);
617 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
620 if (alt_dump_file && (dump_kind & alt_flags))
622 dump_loc (dump_kind, alt_dump_file, srcloc);
623 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
626 if (optinfo_enabled_p ())
628 optinfo &info = begin_next_optinfo (loc);
629 info.handle_dump_file_kind (dump_kind);
630 info.add_tree (t, dump_flags | extra_dump_flags);
634 /* Output a formatted message using FORMAT on appropriate dump streams. */
636 void
637 dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
638 va_list ap)
640 if (dump_file && (dump_kind & pflags))
642 va_list aq;
643 va_copy (aq, ap);
644 vfprintf (dump_file, format, aq);
645 va_end (aq);
648 if (alt_dump_file && (dump_kind & alt_flags))
650 va_list aq;
651 va_copy (aq, ap);
652 vfprintf (alt_dump_file, format, aq);
653 va_end (aq);
656 if (optinfo_enabled_p ())
658 optinfo &info = ensure_pending_optinfo ();
659 va_list aq;
660 va_copy (aq, ap);
661 info.add_printf_va (format, aq);
662 va_end (aq);
666 /* Similar to dump_printf, except source location is also printed, and
667 dump location captured. */
669 void
670 dump_context::dump_printf_loc_va (dump_flags_t dump_kind,
671 const dump_location_t &loc,
672 const char *format, va_list ap)
674 location_t srcloc = loc.get_location_t ();
676 if (dump_file && (dump_kind & pflags))
678 dump_loc (dump_kind, dump_file, srcloc);
679 va_list aq;
680 va_copy (aq, ap);
681 vfprintf (dump_file, format, aq);
682 va_end (aq);
685 if (alt_dump_file && (dump_kind & alt_flags))
687 dump_loc (dump_kind, alt_dump_file, srcloc);
688 va_list aq;
689 va_copy (aq, ap);
690 vfprintf (alt_dump_file, format, aq);
691 va_end (aq);
694 if (optinfo_enabled_p ())
696 optinfo &info = begin_next_optinfo (loc);
697 info.handle_dump_file_kind (dump_kind);
698 va_list aq;
699 va_copy (aq, ap);
700 info.add_printf_va (format, aq);
701 va_end (aq);
705 /* Output VALUE in decimal to appropriate dump streams. */
707 template<unsigned int N, typename C>
708 void
709 dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
711 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
712 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
713 if (dump_file && (dump_kind & pflags))
714 print_dec (value, dump_file, sgn);
716 if (alt_dump_file && (dump_kind & alt_flags))
717 print_dec (value, alt_dump_file, sgn);
719 if (optinfo_enabled_p ())
721 optinfo &info = ensure_pending_optinfo ();
722 info.handle_dump_file_kind (dump_kind);
723 info.add_poly_int<N,C> (value);
727 /* Output the name of NODE on appropriate dump streams. */
729 void
730 dump_context::dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
732 if (dump_file && (dump_kind & pflags))
733 fprintf (dump_file, "%s", node->dump_name ());
735 if (alt_dump_file && (dump_kind & alt_flags))
736 fprintf (alt_dump_file, "%s", node->dump_name ());
738 if (optinfo_enabled_p ())
740 optinfo &info = ensure_pending_optinfo ();
741 info.handle_dump_file_kind (dump_kind);
742 info.add_symtab_node (node);
746 /* Get the current dump scope-nesting depth.
747 For use by -fopt-info (for showing nesting via indentation). */
749 unsigned int
750 dump_context::get_scope_depth () const
752 return m_scope_depth;
755 /* Push a nested dump scope.
756 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
757 destination, if any.
758 Emit a "scope" optinfo if optinfos are enabled.
759 Increment the scope depth. */
761 void
762 dump_context::begin_scope (const char *name, const dump_location_t &loc)
764 /* Specialcase, to avoid going through dump_printf_loc,
765 so that we can create a optinfo of kind OPTINFO_KIND_SCOPE. */
767 if (dump_file)
769 dump_loc (MSG_NOTE, dump_file, loc.get_location_t ());
770 fprintf (dump_file, "=== %s ===\n", name);
773 if (alt_dump_file)
775 dump_loc (MSG_NOTE, alt_dump_file, loc.get_location_t ());
776 fprintf (alt_dump_file, "=== %s ===\n", name);
779 if (optinfo_enabled_p ())
781 end_any_optinfo ();
782 optinfo info (loc, OPTINFO_KIND_SCOPE, current_pass);
783 info.add_printf ("=== %s ===", name);
784 info.emit ();
787 m_scope_depth++;
790 /* Pop a nested dump scope. */
792 void
793 dump_context::end_scope ()
795 end_any_optinfo ();
796 m_scope_depth--;
797 optimization_records_maybe_pop_dump_scope ();
800 /* Return the optinfo currently being accumulated, creating one if
801 necessary. */
803 optinfo &
804 dump_context::ensure_pending_optinfo ()
806 if (!m_pending)
807 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
808 return *m_pending;
811 /* Start a new optinfo and return it, ending any optinfo that was already
812 accumulated. */
814 optinfo &
815 dump_context::begin_next_optinfo (const dump_location_t &loc)
817 end_any_optinfo ();
818 gcc_assert (m_pending == NULL);
819 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
820 return *m_pending;
823 /* End any optinfo that has been accumulated within this context; emitting
824 it to any destinations as appropriate, such as optimization records. */
826 void
827 dump_context::end_any_optinfo ()
829 if (m_pending)
830 m_pending->emit ();
831 delete m_pending;
832 m_pending = NULL;
835 /* The current singleton dump_context, and its default. */
837 dump_context *dump_context::s_current = &dump_context::s_default;
838 dump_context dump_context::s_default;
840 /* Implementation of dump_* API calls, calling into dump_context
841 member functions. */
843 /* Dump gimple statement GS with SPC indentation spaces and
844 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
846 void
847 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
848 gimple *gs, int spc)
850 dump_context::get ().dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
853 /* Similar to dump_gimple_stmt, except additionally print source location. */
855 void
856 dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
857 dump_flags_t extra_dump_flags, gimple *gs, int spc)
859 dump_context::get ().dump_gimple_stmt_loc (dump_kind, loc, extra_dump_flags,
860 gs, spc);
863 /* Dump gimple statement GS with SPC indentation spaces and
864 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
865 Do not terminate with a newline or semicolon. */
867 void
868 dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
869 gimple *gs, int spc)
871 dump_context::get ().dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
874 /* Similar to dump_gimple_expr, except additionally print source location. */
876 void
877 dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
878 dump_flags_t extra_dump_flags, gimple *gs, int spc)
880 dump_context::get ().dump_gimple_expr_loc (dump_kind, loc, extra_dump_flags,
881 gs, spc);
884 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
885 DUMP_KIND is enabled. */
887 void
888 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
889 tree t)
891 dump_context::get ().dump_generic_expr (dump_kind, extra_dump_flags, t);
894 /* Similar to dump_generic_expr, except additionally print the source
895 location. */
897 void
898 dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
899 dump_flags_t extra_dump_flags, tree t)
901 dump_context::get ().dump_generic_expr_loc (dump_kind, loc, extra_dump_flags,
905 /* Output a formatted message using FORMAT on appropriate dump streams. */
907 void
908 dump_printf (dump_flags_t dump_kind, const char *format, ...)
910 va_list ap;
911 va_start (ap, format);
912 dump_context::get ().dump_printf_va (dump_kind, format, ap);
913 va_end (ap);
916 /* Similar to dump_printf, except source location is also printed, and
917 dump location captured. */
919 void
920 dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
921 const char *format, ...)
923 va_list ap;
924 va_start (ap, format);
925 dump_context::get ().dump_printf_loc_va (dump_kind, loc, format, ap);
926 va_end (ap);
929 /* Output VALUE in decimal to appropriate dump streams. */
931 template<unsigned int N, typename C>
932 void
933 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
935 dump_context::get ().dump_dec (dump_kind, value);
938 template void dump_dec (dump_flags_t, const poly_uint16 &);
939 template void dump_dec (dump_flags_t, const poly_int64 &);
940 template void dump_dec (dump_flags_t, const poly_uint64 &);
941 template void dump_dec (dump_flags_t, const poly_offset_int &);
942 template void dump_dec (dump_flags_t, const poly_widest_int &);
944 void
945 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
947 if (dump_file && (dump_kind & pflags))
948 print_dec (value, dump_file, sgn);
950 if (alt_dump_file && (dump_kind & alt_flags))
951 print_dec (value, alt_dump_file, sgn);
954 /* Output VALUE in hexadecimal to appropriate dump streams. */
956 void
957 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
959 if (dump_file && (dump_kind & pflags))
960 print_hex (value, dump_file);
962 if (alt_dump_file && (dump_kind & alt_flags))
963 print_hex (value, alt_dump_file);
966 /* Emit and delete the currently pending optinfo, if there is one,
967 without the caller needing to know about class dump_context. */
969 void
970 dumpfile_ensure_any_optinfo_are_flushed ()
972 dump_context::get().end_any_optinfo ();
975 /* Output the name of NODE on appropriate dump streams. */
977 void
978 dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
980 dump_context::get ().dump_symtab_node (dump_kind, node);
983 /* Get the current dump scope-nesting depth.
984 For use by -fopt-info (for showing nesting via indentation). */
986 unsigned int
987 get_dump_scope_depth ()
989 return dump_context::get ().get_scope_depth ();
992 /* Push a nested dump scope.
993 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
994 destination, if any.
995 Emit a "scope" opinfo if optinfos are enabled.
996 Increment the scope depth. */
998 void
999 dump_begin_scope (const char *name, const dump_location_t &loc)
1001 dump_context::get ().begin_scope (name, loc);
1004 /* Pop a nested dump scope. */
1006 void
1007 dump_end_scope ()
1009 dump_context::get ().end_scope ();
1012 /* Start a dump for PHASE. Store user-supplied dump flags in
1013 *FLAG_PTR. Return the number of streams opened. Set globals
1014 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1015 set dump_flags appropriately for both pass dump stream and
1016 -fopt-info stream. */
1019 gcc::dump_manager::
1020 dump_start (int phase, dump_flags_t *flag_ptr)
1022 int count = 0;
1023 char *name;
1024 struct dump_file_info *dfi;
1025 FILE *stream;
1026 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1027 return 0;
1029 dfi = get_dump_file_info (phase);
1030 name = get_dump_file_name (phase);
1031 if (name)
1033 stream = dump_open (name, dfi->pstate < 0);
1034 if (stream)
1036 dfi->pstate = 1;
1037 count++;
1039 free (name);
1040 dfi->pstream = stream;
1041 set_dump_file (dfi->pstream);
1042 /* Initialize current dump flags. */
1043 pflags = dfi->pflags;
1046 stream = dump_open_alternate_stream (dfi);
1047 if (stream)
1049 dfi->alt_stream = stream;
1050 count++;
1051 set_alt_dump_file (dfi->alt_stream);
1052 /* Initialize current -fopt-info flags. */
1053 alt_flags = dfi->alt_flags;
1056 if (flag_ptr)
1057 *flag_ptr = dfi->pflags;
1059 return count;
1062 /* Finish a tree dump for PHASE and close associated dump streams. Also
1063 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1065 void
1066 gcc::dump_manager::
1067 dump_finish (int phase)
1069 struct dump_file_info *dfi;
1071 if (phase < 0)
1072 return;
1073 dfi = get_dump_file_info (phase);
1074 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1075 fclose (dfi->pstream);
1077 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1078 fclose (dfi->alt_stream);
1080 dfi->alt_stream = NULL;
1081 dfi->pstream = NULL;
1082 set_dump_file (NULL);
1083 set_alt_dump_file (NULL);
1084 dump_flags = TDF_NONE;
1085 alt_flags = TDF_NONE;
1086 pflags = TDF_NONE;
1089 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1090 *FLAG_PTR and returns a stream to write to. If the dump is not
1091 enabled, returns NULL.
1092 PART can be used for dump files which should be split to multiple
1093 parts. PART == -1 indicates dump file with no parts.
1094 If PART is -1, multiple calls will reopen and append to the dump file. */
1096 FILE *
1097 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1099 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1102 FILE *
1103 gcc::dump_manager::
1104 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1106 char *name;
1107 struct dump_file_info *dfi;
1108 FILE *stream;
1110 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1111 return NULL;
1113 name = get_dump_file_name (phase, part);
1114 if (!name)
1115 return NULL;
1116 dfi = get_dump_file_info (phase);
1118 /* We do not support re-opening of dump files with parts. This would require
1119 tracking pstate per part of the dump file. */
1120 stream = dump_open (name, part != -1 || dfi->pstate < 0);
1121 if (stream)
1122 dfi->pstate = 1;
1123 free (name);
1125 if (flag_ptr)
1126 *flag_ptr = dfi->pflags;
1128 /* Initialize current flags */
1129 pflags = dfi->pflags;
1130 return stream;
1133 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1134 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1135 any phase. */
1138 gcc::dump_manager::
1139 dump_phase_enabled_p (int phase) const
1141 if (phase == TDI_tree_all)
1143 size_t i;
1144 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1145 if (dump_files[i].pstate || dump_files[i].alt_state)
1146 return 1;
1147 for (i = 0; i < m_extra_dump_files_in_use; i++)
1148 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1149 return 1;
1150 return 0;
1152 else
1154 struct dump_file_info *dfi = get_dump_file_info (phase);
1155 return dfi->pstate || dfi->alt_state;
1159 /* Returns nonzero if tree dump PHASE has been initialized. */
1162 gcc::dump_manager::
1163 dump_initialized_p (int phase) const
1165 struct dump_file_info *dfi = get_dump_file_info (phase);
1166 return dfi->pstate > 0 || dfi->alt_state > 0;
1169 /* Returns the switch name of PHASE. */
1171 const char *
1172 dump_flag_name (int phase)
1174 return g->get_dumps ()->dump_flag_name (phase);
1177 const char *
1178 gcc::dump_manager::
1179 dump_flag_name (int phase) const
1181 struct dump_file_info *dfi = get_dump_file_info (phase);
1182 return dfi->swtch;
1185 /* Finish a tree dump for PHASE. STREAM is the stream created by
1186 dump_begin. */
1188 void
1189 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1191 if (stream != stderr && stream != stdout)
1192 fclose (stream);
1195 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1196 enabled tree dumps. */
1199 gcc::dump_manager::
1200 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1202 int n = 0;
1203 size_t i;
1205 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1207 if ((dump_files[i].dkind == dkind))
1209 const char *old_filename = dump_files[i].pfilename;
1210 dump_files[i].pstate = -1;
1211 dump_files[i].pflags |= flags;
1212 n++;
1213 /* Override the existing filename. */
1214 if (filename)
1216 dump_files[i].pfilename = xstrdup (filename);
1217 /* Since it is a command-line provided file, which is
1218 common to all the phases, use it in append mode. */
1219 dump_files[i].pstate = 1;
1221 if (old_filename && filename != old_filename)
1222 free (CONST_CAST (char *, old_filename));
1226 for (i = 0; i < m_extra_dump_files_in_use; i++)
1228 if ((m_extra_dump_files[i].dkind == dkind))
1230 const char *old_filename = m_extra_dump_files[i].pfilename;
1231 m_extra_dump_files[i].pstate = -1;
1232 m_extra_dump_files[i].pflags |= flags;
1233 n++;
1234 /* Override the existing filename. */
1235 if (filename)
1237 m_extra_dump_files[i].pfilename = xstrdup (filename);
1238 /* Since it is a command-line provided file, which is
1239 common to all the phases, use it in append mode. */
1240 m_extra_dump_files[i].pstate = 1;
1242 if (old_filename && filename != old_filename)
1243 free (CONST_CAST (char *, old_filename));
1247 return n;
1250 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1251 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1252 dumps. */
1255 gcc::dump_manager::
1256 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1257 const char *filename)
1259 int n = 0;
1260 size_t i;
1262 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1264 if ((dump_files[i].optgroup_flags & optgroup_flags))
1266 const char *old_filename = dump_files[i].alt_filename;
1267 /* Since this file is shared among different passes, it
1268 should be opened in append mode. */
1269 dump_files[i].alt_state = 1;
1270 dump_files[i].alt_flags |= flags;
1271 n++;
1272 /* Override the existing filename. */
1273 if (filename)
1274 dump_files[i].alt_filename = xstrdup (filename);
1275 if (old_filename && filename != old_filename)
1276 free (CONST_CAST (char *, old_filename));
1280 for (i = 0; i < m_extra_dump_files_in_use; i++)
1282 if ((m_extra_dump_files[i].optgroup_flags & optgroup_flags))
1284 const char *old_filename = m_extra_dump_files[i].alt_filename;
1285 /* Since this file is shared among different passes, it
1286 should be opened in append mode. */
1287 m_extra_dump_files[i].alt_state = 1;
1288 m_extra_dump_files[i].alt_flags |= flags;
1289 n++;
1290 /* Override the existing filename. */
1291 if (filename)
1292 m_extra_dump_files[i].alt_filename = xstrdup (filename);
1293 if (old_filename && filename != old_filename)
1294 free (CONST_CAST (char *, old_filename));
1298 return n;
1301 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1302 relevant details in the dump_files array. */
1305 gcc::dump_manager::
1306 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1308 const char *option_value;
1309 const char *ptr;
1310 dump_flags_t flags;
1312 if (doglob && !dfi->glob)
1313 return 0;
1315 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1316 if (!option_value)
1317 return 0;
1319 if (*option_value && *option_value != '-' && *option_value != '=')
1320 return 0;
1322 ptr = option_value;
1323 flags = TDF_NONE;
1325 while (*ptr)
1327 const struct kv_pair<dump_flags_t> *option_ptr;
1328 const char *end_ptr;
1329 const char *eq_ptr;
1330 unsigned length;
1332 while (*ptr == '-')
1333 ptr++;
1334 end_ptr = strchr (ptr, '-');
1335 eq_ptr = strchr (ptr, '=');
1337 if (eq_ptr && !end_ptr)
1338 end_ptr = eq_ptr;
1340 if (!end_ptr)
1341 end_ptr = ptr + strlen (ptr);
1342 length = end_ptr - ptr;
1344 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1345 if (strlen (option_ptr->name) == length
1346 && !memcmp (option_ptr->name, ptr, length))
1348 flags |= option_ptr->value;
1349 goto found;
1352 if (*ptr == '=')
1354 /* Interpret rest of the argument as a dump filename. This
1355 filename overrides other command line filenames. */
1356 if (dfi->pfilename)
1357 free (CONST_CAST (char *, dfi->pfilename));
1358 dfi->pfilename = xstrdup (ptr + 1);
1359 break;
1361 else
1362 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1363 length, ptr, dfi->swtch);
1364 found:;
1365 ptr = end_ptr;
1368 dfi->pstate = -1;
1369 dfi->pflags |= flags;
1371 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1372 known dumps. */
1373 if (dfi->suffix == NULL)
1374 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1376 return 1;
1380 gcc::dump_manager::
1381 dump_switch_p (const char *arg)
1383 size_t i;
1384 int any = 0;
1386 for (i = TDI_none + 1; i != TDI_end; i++)
1387 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1389 /* Don't glob if we got a hit already */
1390 if (!any)
1391 for (i = TDI_none + 1; i != TDI_end; i++)
1392 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1394 for (i = 0; i < m_extra_dump_files_in_use; i++)
1395 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1397 if (!any)
1398 for (i = 0; i < m_extra_dump_files_in_use; i++)
1399 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1402 return any;
1405 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1406 and filename. Return non-zero if it is a recognized switch. */
1408 static int
1409 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1410 optgroup_flags_t *optgroup_flags, char **filename)
1412 const char *option_value;
1413 const char *ptr;
1415 option_value = arg;
1416 ptr = option_value;
1418 *filename = NULL;
1419 *flags = TDF_NONE;
1420 *optgroup_flags = OPTGROUP_NONE;
1422 if (!ptr)
1423 return 1; /* Handle '-fopt-info' without any additional options. */
1425 while (*ptr)
1427 const char *end_ptr;
1428 const char *eq_ptr;
1429 unsigned length;
1431 while (*ptr == '-')
1432 ptr++;
1433 end_ptr = strchr (ptr, '-');
1434 eq_ptr = strchr (ptr, '=');
1436 if (eq_ptr && !end_ptr)
1437 end_ptr = eq_ptr;
1439 if (!end_ptr)
1440 end_ptr = ptr + strlen (ptr);
1441 length = end_ptr - ptr;
1443 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1444 option_ptr->name; option_ptr++)
1445 if (strlen (option_ptr->name) == length
1446 && !memcmp (option_ptr->name, ptr, length))
1448 *flags |= option_ptr->value;
1449 goto found;
1452 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1453 option_ptr->name; option_ptr++)
1454 if (strlen (option_ptr->name) == length
1455 && !memcmp (option_ptr->name, ptr, length))
1457 *optgroup_flags |= option_ptr->value;
1458 goto found;
1461 if (*ptr == '=')
1463 /* Interpret rest of the argument as a dump filename. This
1464 filename overrides other command line filenames. */
1465 *filename = xstrdup (ptr + 1);
1466 break;
1468 else
1470 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1471 length, ptr, arg);
1472 return 0;
1474 found:;
1475 ptr = end_ptr;
1478 return 1;
1481 /* Return non-zero if ARG is a recognized switch for
1482 -fopt-info. Return zero otherwise. */
1485 opt_info_switch_p (const char *arg)
1487 dump_flags_t flags;
1488 optgroup_flags_t optgroup_flags;
1489 char *filename;
1490 static char *file_seen = NULL;
1491 gcc::dump_manager *dumps = g->get_dumps ();
1493 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1494 return 0;
1496 if (!filename)
1497 filename = xstrdup ("stderr");
1499 /* Bail out if a different filename has been specified. */
1500 if (file_seen && strcmp (file_seen, filename))
1502 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1503 arg);
1504 return 1;
1507 file_seen = xstrdup (filename);
1508 if (!flags)
1509 flags = MSG_OPTIMIZED_LOCATIONS;
1510 if (!optgroup_flags)
1511 optgroup_flags = OPTGROUP_ALL;
1513 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1516 /* Print basic block on the dump streams. */
1518 void
1519 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1521 if (dump_file && (dump_kind & pflags))
1522 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1523 if (alt_dump_file && (dump_kind & alt_flags))
1524 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
1527 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1529 void
1530 dump_function (int phase, tree fn)
1532 FILE *stream;
1533 dump_flags_t flags;
1535 stream = dump_begin (phase, &flags);
1536 if (stream)
1538 dump_function_to_file (fn, stream, flags);
1539 dump_end (phase, stream);
1543 /* Print information from the combine pass on dump_file. */
1545 void
1546 print_combine_total_stats (void)
1548 if (dump_file)
1549 dump_combine_total_stats (dump_file);
1552 /* Enable RTL dump for all the RTL passes. */
1554 bool
1555 enable_rtl_dump_file (void)
1557 gcc::dump_manager *dumps = g->get_dumps ();
1558 int num_enabled =
1559 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
1560 NULL);
1561 return num_enabled > 0;
1564 #if CHECKING_P
1566 /* temp_dump_context's ctor. Temporarily override the dump_context
1567 (to forcibly enable optinfo-generation). */
1569 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo)
1570 : m_context (),
1571 m_saved (&dump_context ().get ())
1573 dump_context::s_current = &m_context;
1574 m_context.m_forcibly_enable_optinfo = forcibly_enable_optinfo;
1577 /* temp_dump_context's dtor. Restore the saved dump_context. */
1579 temp_dump_context::~temp_dump_context ()
1581 dump_context::s_current = m_saved;
1584 namespace selftest {
1586 /* Verify that the dump_location_t constructors capture the source location
1587 at which they were called (provided that the build compiler is sufficiently
1588 recent). */
1590 static void
1591 test_impl_location ()
1593 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
1594 /* Default ctor. */
1596 dump_location_t loc;
1597 const int expected_line = __LINE__ - 1;
1598 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1599 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1602 /* Constructing from a gimple. */
1604 dump_location_t loc ((gimple *)NULL);
1605 const int expected_line = __LINE__ - 1;
1606 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1607 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1610 /* Constructing from an rtx_insn. */
1612 dump_location_t loc ((rtx_insn *)NULL);
1613 const int expected_line = __LINE__ - 1;
1614 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1615 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1617 #endif
1620 /* Verify that ITEM has the expected values. */
1622 static void
1623 verify_item (const location &loc,
1624 const optinfo_item *item,
1625 enum optinfo_item_kind expected_kind,
1626 location_t expected_location,
1627 const char *expected_text)
1629 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
1630 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
1631 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
1634 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
1636 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
1637 SELFTEST_BEGIN_STMT \
1638 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
1639 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
1640 SELFTEST_END_STMT
1642 /* Verify that ITEM is a tree item, with the expected values. */
1644 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1645 SELFTEST_BEGIN_STMT \
1646 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
1647 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1648 SELFTEST_END_STMT
1650 /* Verify that ITEM is a gimple item, with the expected values. */
1652 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1653 SELFTEST_BEGIN_STMT \
1654 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
1655 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1656 SELFTEST_END_STMT
1658 /* Verify that calls to the dump_* API are captured and consolidated into
1659 optimization records. */
1661 static void
1662 test_capture_of_dump_calls (const line_table_case &case_)
1664 /* Generate a location_t for testing. */
1665 line_table_test ltt (case_);
1666 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
1667 linemap_line_start (line_table, 5, 100);
1668 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1669 location_t where = linemap_position_for_column (line_table, 10);
1671 dump_location_t loc = dump_location_t::from_location_t (where);
1673 /* Test of dump_printf. */
1675 temp_dump_context tmp (true);
1676 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
1678 optinfo *info = tmp.get_pending_optinfo ();
1679 ASSERT_TRUE (info != NULL);
1680 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1681 ASSERT_EQ (info->num_items (), 1);
1682 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
1685 /* Tree, via dump_generic_expr. */
1687 temp_dump_context tmp (true);
1688 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
1689 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
1691 optinfo *info = tmp.get_pending_optinfo ();
1692 ASSERT_TRUE (info != NULL);
1693 ASSERT_EQ (info->get_location_t (), where);
1694 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1695 ASSERT_EQ (info->num_items (), 2);
1696 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
1697 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
1700 /* Tree, via dump_generic_expr_loc. */
1702 temp_dump_context tmp (true);
1703 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
1705 optinfo *info = tmp.get_pending_optinfo ();
1706 ASSERT_TRUE (info != NULL);
1707 ASSERT_EQ (info->get_location_t (), where);
1708 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1709 ASSERT_EQ (info->num_items (), 1);
1710 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
1713 /* Gimple. */
1715 greturn *stmt = gimple_build_return (NULL);
1716 gimple_set_location (stmt, where);
1718 /* dump_gimple_stmt_loc. */
1720 temp_dump_context tmp (true);
1721 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
1723 optinfo *info = tmp.get_pending_optinfo ();
1724 ASSERT_TRUE (info != NULL);
1725 ASSERT_EQ (info->num_items (), 1);
1726 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
1729 /* dump_gimple_stmt. */
1731 temp_dump_context tmp (true);
1732 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
1734 optinfo *info = tmp.get_pending_optinfo ();
1735 ASSERT_TRUE (info != NULL);
1736 ASSERT_EQ (info->num_items (), 1);
1737 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
1740 /* dump_gimple_expr_loc. */
1742 temp_dump_context tmp (true);
1743 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
1745 optinfo *info = tmp.get_pending_optinfo ();
1746 ASSERT_TRUE (info != NULL);
1747 ASSERT_EQ (info->num_items (), 1);
1748 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
1751 /* dump_gimple_expr. */
1753 temp_dump_context tmp (true);
1754 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
1756 optinfo *info = tmp.get_pending_optinfo ();
1757 ASSERT_TRUE (info != NULL);
1758 ASSERT_EQ (info->num_items (), 1);
1759 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
1763 /* poly_int. */
1765 temp_dump_context tmp (true);
1766 dump_dec (MSG_NOTE, poly_int64 (42));
1768 optinfo *info = tmp.get_pending_optinfo ();
1769 ASSERT_TRUE (info != NULL);
1770 ASSERT_EQ (info->num_items (), 1);
1771 ASSERT_IS_TEXT (info->get_item (0), "42");
1774 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
1775 above. */
1777 /* MSG_OPTIMIZED_LOCATIONS. */
1779 temp_dump_context tmp (true);
1780 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
1781 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
1782 OPTINFO_KIND_SUCCESS);
1785 /* MSG_MISSED_OPTIMIZATION. */
1787 temp_dump_context tmp (true);
1788 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
1789 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
1790 OPTINFO_KIND_FAILURE);
1795 /* Run all of the selftests within this file. */
1797 void
1798 dumpfile_c_tests ()
1800 test_impl_location ();
1801 for_each_line_table_case (test_capture_of_dump_calls);
1804 } // namespace selftest
1806 #endif /* CHECKING_P */