Fix -fsave-optimization-record ICE (PR tree-optimization/87025)
[official-gcc.git] / gcc / dumpfile.c
blobe125650eed107f5e4bb2dc92771a65c027ed277f
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"
41 #include "stringpool.h" /* for get_identifier. */
43 /* If non-NULL, return one past-the-end of the matching SUBPART of
44 the WHOLE string. */
45 #define skip_leading_substring(whole, part) \
46 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
48 static dump_flags_t pflags; /* current dump_flags */
50 static void dump_loc (dump_flags_t, FILE *, location_t);
52 /* Current -fopt-info output stream, if any, and flags. */
53 static FILE *alt_dump_file = NULL;
54 static dump_flags_t alt_flags;
56 static FILE *dump_open_alternate_stream (struct dump_file_info *);
58 /* These are currently used for communicating between passes.
59 However, instead of accessing them directly, the passes can use
60 dump_printf () for dumps. */
61 FILE *dump_file = NULL;
62 const char *dump_file_name;
63 dump_flags_t dump_flags;
64 bool dumps_are_enabled = false;
67 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
68 global. */
70 void
71 set_dump_file (FILE *new_dump_file)
73 dumpfile_ensure_any_optinfo_are_flushed ();
74 dump_file = new_dump_file;
75 dump_context::get ().refresh_dumps_are_enabled ();
78 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
79 global. */
81 static void
82 set_alt_dump_file (FILE *new_alt_dump_file)
84 dumpfile_ensure_any_optinfo_are_flushed ();
85 alt_dump_file = new_alt_dump_file;
86 dump_context::get ().refresh_dumps_are_enabled ();
89 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
90 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
91 OPTGROUP_NONE, 0, 0, num, false, false}
93 /* Table of tree dump switches. This must be consistent with the
94 TREE_DUMP_INDEX enumeration in dumpfile.h. */
95 static struct dump_file_info dump_files[TDI_end] =
97 DUMP_FILE_INFO (NULL, NULL, DK_none, 0),
98 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa, 0),
99 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa, 0),
100 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa, 0),
101 DUMP_FILE_INFO (".original", "tree-original", DK_tree, 0),
102 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree, 0),
103 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree, 0),
104 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa, 0),
105 #define FIRST_AUTO_NUMBERED_DUMP 1
106 #define FIRST_ME_AUTO_NUMBERED_DUMP 4
108 DUMP_FILE_INFO (NULL, "lang-all", DK_lang, 0),
109 DUMP_FILE_INFO (NULL, "tree-all", DK_tree, 0),
110 DUMP_FILE_INFO (NULL, "rtl-all", DK_rtl, 0),
111 DUMP_FILE_INFO (NULL, "ipa-all", DK_ipa, 0),
114 /* Table of dump options. This must be consistent with the TDF_* flags
115 in dumpfile.h and opt_info_options below. */
116 static const kv_pair<dump_flags_t> dump_options[] =
118 {"address", TDF_ADDRESS},
119 {"asmname", TDF_ASMNAME},
120 {"slim", TDF_SLIM},
121 {"raw", TDF_RAW},
122 {"graph", TDF_GRAPH},
123 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
124 | MSG_MISSED_OPTIMIZATION
125 | MSG_NOTE)},
126 {"cselib", TDF_CSELIB},
127 {"stats", TDF_STATS},
128 {"blocks", TDF_BLOCKS},
129 {"vops", TDF_VOPS},
130 {"lineno", TDF_LINENO},
131 {"uid", TDF_UID},
132 {"stmtaddr", TDF_STMTADDR},
133 {"memsyms", TDF_MEMSYMS},
134 {"eh", TDF_EH},
135 {"alias", TDF_ALIAS},
136 {"nouid", TDF_NOUID},
137 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
138 {"scev", TDF_SCEV},
139 {"gimple", TDF_GIMPLE},
140 {"folding", TDF_FOLDING},
141 {"optimized", MSG_OPTIMIZED_LOCATIONS},
142 {"missed", MSG_MISSED_OPTIMIZATION},
143 {"note", MSG_NOTE},
144 {"optall", MSG_ALL_KINDS},
145 {"all", dump_flags_t (TDF_ALL_VALUES
146 & ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
147 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
148 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
149 {NULL, TDF_NONE}
152 /* A subset of the dump_options table which is used for -fopt-info
153 types. This must be consistent with the MSG_* flags in dumpfile.h.
155 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
157 {"optimized", MSG_OPTIMIZED_LOCATIONS},
158 {"missed", MSG_MISSED_OPTIMIZATION},
159 {"note", MSG_NOTE},
160 {"all", MSG_ALL_KINDS},
161 {"internals", MSG_PRIORITY_INTERNALS},
162 {NULL, TDF_NONE}
165 /* Flags used for -fopt-info groups. */
166 const kv_pair<optgroup_flags_t> optgroup_options[] =
168 {"ipa", OPTGROUP_IPA},
169 {"loop", OPTGROUP_LOOP},
170 {"inline", OPTGROUP_INLINE},
171 {"omp", OPTGROUP_OMP},
172 {"vec", OPTGROUP_VEC},
173 {"optall", OPTGROUP_ALL},
174 {NULL, OPTGROUP_NONE}
177 gcc::dump_manager::dump_manager ():
178 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
179 m_extra_dump_files (NULL),
180 m_extra_dump_files_in_use (0),
181 m_extra_dump_files_alloced (0),
182 m_optgroup_flags (OPTGROUP_NONE),
183 m_optinfo_flags (TDF_NONE),
184 m_optinfo_filename (NULL)
188 gcc::dump_manager::~dump_manager ()
190 free (m_optinfo_filename);
191 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
193 dump_file_info *dfi = &m_extra_dump_files[i];
194 /* suffix, swtch, glob are statically allocated for the entries
195 in dump_files, and for statistics, but are dynamically allocated
196 for those for passes. */
197 if (dfi->owns_strings)
199 XDELETEVEC (const_cast <char *> (dfi->suffix));
200 XDELETEVEC (const_cast <char *> (dfi->swtch));
201 XDELETEVEC (const_cast <char *> (dfi->glob));
203 /* These, if non-NULL, are always dynamically allocated. */
204 XDELETEVEC (const_cast <char *> (dfi->pfilename));
205 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
207 XDELETEVEC (m_extra_dump_files);
210 unsigned int
211 gcc::dump_manager::
212 dump_register (const char *suffix, const char *swtch, const char *glob,
213 dump_kind dkind, optgroup_flags_t optgroup_flags,
214 bool take_ownership)
216 int num = m_next_dump++;
218 size_t count = m_extra_dump_files_in_use++;
220 if (count >= m_extra_dump_files_alloced)
222 if (m_extra_dump_files_alloced == 0)
223 m_extra_dump_files_alloced = 512;
224 else
225 m_extra_dump_files_alloced *= 2;
226 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
227 m_extra_dump_files,
228 m_extra_dump_files_alloced);
230 /* Construct a new object in the space allocated above. */
231 new (m_extra_dump_files + count) dump_file_info ();
233 else
235 /* Zero out the already constructed object. */
236 m_extra_dump_files[count] = dump_file_info ();
239 m_extra_dump_files[count].suffix = suffix;
240 m_extra_dump_files[count].swtch = swtch;
241 m_extra_dump_files[count].glob = glob;
242 m_extra_dump_files[count].dkind = dkind;
243 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
244 m_extra_dump_files[count].num = num;
245 m_extra_dump_files[count].owns_strings = take_ownership;
247 return count + TDI_end;
251 /* Allow languages and middle-end to register their dumps before the
252 optimization passes. */
254 void
255 gcc::dump_manager::
256 register_dumps ()
258 lang_hooks.register_dumps (this);
259 /* If this assert fails, some FE registered more than
260 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
261 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
262 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
263 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
264 dump_files[TDI_original].num = m_next_dump++;
265 dump_files[TDI_gimple].num = m_next_dump++;
266 dump_files[TDI_nested].num = m_next_dump++;
270 /* Return the dump_file_info for the given phase. */
272 struct dump_file_info *
273 gcc::dump_manager::
274 get_dump_file_info (int phase) const
276 if (phase < TDI_end)
277 return &dump_files[phase];
278 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
279 return NULL;
280 else
281 return m_extra_dump_files + (phase - TDI_end);
284 /* Locate the dump_file_info with swtch equal to SWTCH,
285 or return NULL if no such dump_file_info exists. */
287 struct dump_file_info *
288 gcc::dump_manager::
289 get_dump_file_info_by_switch (const char *swtch) const
291 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
292 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
293 return &m_extra_dump_files[i];
295 /* Not found. */
296 return NULL;
300 /* Return the name of the dump file for the given phase.
301 The caller is responsible for calling free on the returned
302 buffer.
303 If the dump is not enabled, returns NULL. */
305 char *
306 gcc::dump_manager::
307 get_dump_file_name (int phase, int part) const
309 struct dump_file_info *dfi;
311 if (phase == TDI_none)
312 return NULL;
314 dfi = get_dump_file_info (phase);
316 return get_dump_file_name (dfi, part);
319 /* Return the name of the dump file for the given dump_file_info.
320 The caller is responsible for calling free on the returned
321 buffer.
322 If the dump is not enabled, returns NULL. */
324 char *
325 gcc::dump_manager::
326 get_dump_file_name (struct dump_file_info *dfi, int part) const
328 char dump_id[10];
330 gcc_assert (dfi);
332 if (dfi->pstate == 0)
333 return NULL;
335 /* If available, use the command line dump filename. */
336 if (dfi->pfilename)
337 return xstrdup (dfi->pfilename);
339 if (dfi->num < 0)
340 dump_id[0] = '\0';
341 else
343 /* (null), LANG, TREE, RTL, IPA. */
344 char suffix = " ltri"[dfi->dkind];
346 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
347 dump_id[0] = '\0';
350 if (part != -1)
352 char part_id[8];
353 snprintf (part_id, sizeof (part_id), ".%i", part);
354 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
356 else
357 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
360 /* Open a dump file called FILENAME. Some filenames are special and
361 refer to the standard streams. TRUNC indicates whether this is the
362 first open (so the file should be truncated, rather than appended).
363 An error message is emitted in the event of failure. */
365 static FILE *
366 dump_open (const char *filename, bool trunc)
368 if (strcmp ("stderr", filename) == 0)
369 return stderr;
371 if (strcmp ("stdout", filename) == 0
372 || strcmp ("-", filename) == 0)
373 return stdout;
375 FILE *stream = fopen (filename, trunc ? "w" : "a");
377 if (!stream)
378 error ("could not open dump file %qs: %m", filename);
379 return stream;
382 /* For a given DFI, open an alternate dump filename (which could also
383 be a standard stream such as stdout/stderr). If the alternate dump
384 file cannot be opened, return NULL. */
386 static FILE *
387 dump_open_alternate_stream (struct dump_file_info *dfi)
389 if (!dfi->alt_filename)
390 return NULL;
392 if (dfi->alt_stream)
393 return dfi->alt_stream;
395 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
397 if (stream)
398 dfi->alt_state = 1;
400 return stream;
403 /* Construct a dump_user_location_t from STMT (using its location and
404 hotness). */
406 dump_user_location_t::dump_user_location_t (const gimple *stmt)
407 : m_count (), m_loc (UNKNOWN_LOCATION)
409 if (stmt)
411 if (stmt->bb)
412 m_count = stmt->bb->count;
413 m_loc = gimple_location (stmt);
417 /* Construct a dump_user_location_t from an RTL instruction (using its
418 location and hotness). */
420 dump_user_location_t::dump_user_location_t (const rtx_insn *insn)
421 : m_count (), m_loc (UNKNOWN_LOCATION)
423 if (insn)
425 basic_block bb = BLOCK_FOR_INSN (insn);
426 if (bb)
427 m_count = bb->count;
428 m_loc = INSN_LOCATION (insn);
432 /* Construct from a function declaration. This one requires spelling out
433 to avoid accidentally constructing from other kinds of tree. */
435 dump_user_location_t
436 dump_user_location_t::from_function_decl (tree fndecl)
438 gcc_assert (fndecl);
440 // FIXME: profile count for function?
441 return dump_user_location_t (profile_count (),
442 DECL_SOURCE_LOCATION (fndecl));
445 /* Extract the MSG_* component from DUMP_KIND and return a string for use
446 as a prefix to dump messages.
447 These match the strings in optinfo_verbosity_options and thus the
448 "OPTIONS" within "-fopt-info-OPTIONS". */
450 static const char *
451 kind_as_string (dump_flags_t dump_kind)
453 switch (dump_kind & MSG_ALL_KINDS)
455 default:
456 gcc_unreachable ();
457 case MSG_OPTIMIZED_LOCATIONS:
458 return "optimized";
459 case MSG_MISSED_OPTIMIZATION:
460 return "missed";
461 case MSG_NOTE:
462 return "note";
466 /* Print source location on DFILE if enabled. */
468 static void
469 dump_loc (dump_flags_t dump_kind, FILE *dfile, location_t loc)
471 if (dump_kind)
473 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
474 fprintf (dfile, "%s:%d:%d: ", LOCATION_FILE (loc),
475 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
476 else if (current_function_decl)
477 fprintf (dfile, "%s:%d:%d: ",
478 DECL_SOURCE_FILE (current_function_decl),
479 DECL_SOURCE_LINE (current_function_decl),
480 DECL_SOURCE_COLUMN (current_function_decl));
481 fprintf (dfile, "%s: ", kind_as_string (dump_kind));
482 /* Indentation based on scope depth. */
483 fprintf (dfile, "%*s", get_dump_scope_depth (), "");
487 /* Print source location to PP if enabled. */
489 static void
490 dump_loc (dump_flags_t dump_kind, pretty_printer *pp, location_t loc)
492 if (dump_kind)
494 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
495 pp_printf (pp, "%s:%d:%d: ", LOCATION_FILE (loc),
496 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
497 else if (current_function_decl)
498 pp_printf (pp, "%s:%d:%d: ",
499 DECL_SOURCE_FILE (current_function_decl),
500 DECL_SOURCE_LINE (current_function_decl),
501 DECL_SOURCE_COLUMN (current_function_decl));
502 pp_printf (pp, "%s: ", kind_as_string (dump_kind));
503 /* Indentation based on scope depth. */
504 for (unsigned i = 0; i < get_dump_scope_depth (); i++)
505 pp_character (pp, ' ');
509 /* Implementation of dump_context member functions. */
511 /* dump_context's dtor. */
513 dump_context::~dump_context ()
515 delete m_pending;
518 void
519 dump_context::set_json_writer (optrecord_json_writer *writer)
521 delete m_json_writer;
522 m_json_writer = writer;
525 /* Perform cleanup activity for -fsave-optimization-record.
526 Currently, the file is written out here in one go, before cleaning
527 up. */
529 void
530 dump_context::finish_any_json_writer ()
532 if (!m_json_writer)
533 return;
535 m_json_writer->write ();
536 delete m_json_writer;
537 m_json_writer = NULL;
540 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
541 or alt_dump_file change, or when changing dump_context in selftests. */
543 void
544 dump_context::refresh_dumps_are_enabled ()
546 dumps_are_enabled = (dump_file || alt_dump_file || optinfo_enabled_p ()
547 || m_test_pp);
550 /* Determine if a message of kind DUMP_KIND and at the current scope depth
551 should be printed.
553 Only show messages that match FILTER both on their kind *and*
554 their priority. */
556 bool
557 dump_context::apply_dump_filter_p (dump_flags_t dump_kind,
558 dump_flags_t filter) const
560 /* Few messages, if any, have an explicit MSG_PRIORITY.
561 If DUMP_KIND does, we'll use it.
562 Otherwise, generate an implicit priority value for the message based
563 on the current scope depth.
564 Messages at the top-level scope are MSG_PRIORITY_USER_FACING,
565 whereas those in nested scopes are MSG_PRIORITY_INTERNALS. */
566 if (!(dump_kind & MSG_ALL_PRIORITIES))
568 dump_flags_t implicit_priority
569 = (m_scope_depth > 0
570 ? MSG_PRIORITY_INTERNALS
571 : MSG_PRIORITY_USER_FACING);
572 dump_kind |= implicit_priority;
575 return (dump_kind & (filter & MSG_ALL_KINDS)
576 && dump_kind & (filter & MSG_ALL_PRIORITIES));
579 /* Print LOC to the appropriate dump destinations, given DUMP_KIND.
580 If optinfos are enabled, begin a new optinfo. */
582 void
583 dump_context::dump_loc (dump_flags_t dump_kind, const dump_location_t &loc)
585 end_any_optinfo ();
587 dump_loc_immediate (dump_kind, loc);
589 if (optinfo_enabled_p ())
591 optinfo &info = begin_next_optinfo (loc);
592 info.handle_dump_file_kind (dump_kind);
596 /* As dump_loc above, but without starting a new optinfo. */
598 void
599 dump_context::dump_loc_immediate (dump_flags_t dump_kind,
600 const dump_location_t &loc)
602 location_t srcloc = loc.get_location_t ();
604 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
605 ::dump_loc (dump_kind, dump_file, srcloc);
607 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
608 ::dump_loc (dump_kind, alt_dump_file, srcloc);
610 /* Support for temp_dump_context in selftests. */
611 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
612 ::dump_loc (dump_kind, m_test_pp, srcloc);
615 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
617 static optinfo_item *
618 make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
620 pretty_printer pp;
621 pp_needs_newline (&pp) = true;
622 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
623 pp_newline (&pp);
625 optinfo_item *item
626 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
627 xstrdup (pp_formatted_text (&pp)));
628 return item;
631 /* Dump gimple statement GS with SPC indentation spaces and
632 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
634 void
635 dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
636 dump_flags_t extra_dump_flags,
637 gimple *gs, int spc)
639 optinfo_item *item
640 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
641 emit_item (item, dump_kind);
643 if (optinfo_enabled_p ())
645 optinfo &info = ensure_pending_optinfo ();
646 info.handle_dump_file_kind (dump_kind);
647 info.add_item (item);
649 else
650 delete item;
653 /* Similar to dump_gimple_stmt, except additionally print source location. */
655 void
656 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind,
657 const dump_location_t &loc,
658 dump_flags_t extra_dump_flags,
659 gimple *gs, int spc)
661 dump_loc (dump_kind, loc);
662 dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
665 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
667 static optinfo_item *
668 make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
670 dump_flags |= TDF_RHS_ONLY;
671 pretty_printer pp;
672 pp_needs_newline (&pp) = true;
673 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
675 optinfo_item *item
676 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
677 xstrdup (pp_formatted_text (&pp)));
678 return item;
681 /* Dump gimple statement GS with SPC indentation spaces and
682 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
683 Do not terminate with a newline or semicolon. */
685 void
686 dump_context::dump_gimple_expr (dump_flags_t dump_kind,
687 dump_flags_t extra_dump_flags,
688 gimple *gs, int spc)
690 optinfo_item *item
691 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
692 emit_item (item, dump_kind);
694 if (optinfo_enabled_p ())
696 optinfo &info = ensure_pending_optinfo ();
697 info.handle_dump_file_kind (dump_kind);
698 info.add_item (item);
700 else
701 delete item;
704 /* Similar to dump_gimple_expr, except additionally print source location. */
706 void
707 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind,
708 const dump_location_t &loc,
709 dump_flags_t extra_dump_flags,
710 gimple *gs,
711 int spc)
713 dump_loc (dump_kind, loc);
714 dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
717 /* Make an item for the given dump call, equivalent to print_generic_expr. */
719 static optinfo_item *
720 make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
722 pretty_printer pp;
723 pp_needs_newline (&pp) = true;
724 pp_translate_identifiers (&pp) = false;
725 dump_generic_node (&pp, node, 0, dump_flags, false);
727 location_t loc = UNKNOWN_LOCATION;
728 if (EXPR_HAS_LOCATION (node))
729 loc = EXPR_LOCATION (node);
731 optinfo_item *item
732 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
733 xstrdup (pp_formatted_text (&pp)));
734 return item;
737 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
738 DUMP_KIND is enabled. */
740 void
741 dump_context::dump_generic_expr (dump_flags_t dump_kind,
742 dump_flags_t extra_dump_flags,
743 tree t)
745 optinfo_item *item
746 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
747 emit_item (item, dump_kind);
749 if (optinfo_enabled_p ())
751 optinfo &info = ensure_pending_optinfo ();
752 info.handle_dump_file_kind (dump_kind);
753 info.add_item (item);
755 else
756 delete item;
760 /* Similar to dump_generic_expr, except additionally print the source
761 location. */
763 void
764 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind,
765 const dump_location_t &loc,
766 dump_flags_t extra_dump_flags,
767 tree t)
769 dump_loc (dump_kind, loc);
770 dump_generic_expr (dump_kind, extra_dump_flags, t);
773 /* Make an item for the given dump call. */
775 static optinfo_item *
776 make_item_for_dump_symtab_node (symtab_node *node)
778 location_t loc = DECL_SOURCE_LOCATION (node->decl);
779 optinfo_item *item
780 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
781 xstrdup (node->dump_name ()));
782 return item;
785 /* dump_pretty_printer's ctor. */
787 dump_pretty_printer::dump_pretty_printer (dump_context *context,
788 dump_flags_t dump_kind)
789 : pretty_printer (), m_context (context), m_dump_kind (dump_kind),
790 m_stashed_items ()
792 pp_format_decoder (this) = format_decoder_cb;
795 /* Phase 3 of formatting; compare with pp_output_formatted_text.
797 Emit optinfo_item instances for the various formatted chunks from phases
798 1 and 2 (i.e. pp_format).
800 Some chunks may already have had their items built (during decode_format).
801 These chunks have been stashed into m_stashed_items; we emit them here.
803 For all other purely textual chunks, they are printed into
804 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
805 This consolidates multiple adjacent text chunks into a single text
806 optinfo_item. */
808 void
809 dump_pretty_printer::emit_items (optinfo *dest)
811 output_buffer *buffer = pp_buffer (this);
812 struct chunk_info *chunk_array = buffer->cur_chunk_array;
813 const char **args = chunk_array->args;
815 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
816 gcc_assert (buffer->line_length == 0);
818 unsigned stashed_item_idx = 0;
819 for (unsigned chunk = 0; args[chunk]; chunk++)
821 if (stashed_item_idx < m_stashed_items.length ()
822 && args[chunk] == *m_stashed_items[stashed_item_idx].buffer_ptr)
824 emit_any_pending_textual_chunks (dest);
825 /* This chunk has a stashed item: use it. */
826 emit_item (m_stashed_items[stashed_item_idx++].item, dest);
828 else
829 /* This chunk is purely textual. Print it (to
830 buffer->formatted_obstack), so that we can consolidate adjacent
831 chunks into one textual optinfo_item. */
832 pp_string (this, args[chunk]);
835 emit_any_pending_textual_chunks (dest);
837 /* Ensure that we consumed all of stashed_items. */
838 gcc_assert (stashed_item_idx == m_stashed_items.length ());
840 /* Deallocate the chunk structure and everything after it (i.e. the
841 associated series of formatted strings). */
842 buffer->cur_chunk_array = chunk_array->prev;
843 obstack_free (&buffer->chunk_obstack, chunk_array);
846 /* Subroutine of dump_pretty_printer::emit_items
847 for consolidating multiple adjacent pure-text chunks into single
848 optinfo_items (in phase 3). */
850 void
851 dump_pretty_printer::emit_any_pending_textual_chunks (optinfo *dest)
853 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
855 /* Don't emit an item if the pending text is empty. */
856 if (output_buffer_last_position_in_text (buffer) == NULL)
857 return;
859 char *formatted_text = xstrdup (pp_formatted_text (this));
860 optinfo_item *item
861 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
862 formatted_text);
863 emit_item (item, dest);
865 /* Clear the pending text by unwinding formatted_text back to the start
866 of the buffer (without deallocating). */
867 obstack_free (&buffer->formatted_obstack,
868 buffer->formatted_obstack.object_base);
871 /* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
872 to DEST; otherwise delete ITEM. */
874 void
875 dump_pretty_printer::emit_item (optinfo_item *item, optinfo *dest)
877 m_context->emit_item (item, m_dump_kind);
878 if (dest)
879 dest->add_item (item);
880 else
881 delete item;
884 /* Record that ITEM (generated in phase 2 of formatting) is to be used for
885 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
887 void
888 dump_pretty_printer::stash_item (const char **buffer_ptr, optinfo_item *item)
890 gcc_assert (buffer_ptr);
891 gcc_assert (item);
893 m_stashed_items.safe_push (stashed_item (buffer_ptr, item));
896 /* pp_format_decoder callback for dump_pretty_printer, and thus for
897 dump_printf and dump_printf_loc.
899 A wrapper around decode_format, for type-safety. */
901 bool
902 dump_pretty_printer::format_decoder_cb (pretty_printer *pp, text_info *text,
903 const char *spec, int /*precision*/,
904 bool /*wide*/, bool /*set_locus*/,
905 bool /*verbose*/, bool */*quoted*/,
906 const char **buffer_ptr)
908 dump_pretty_printer *opp = static_cast <dump_pretty_printer *> (pp);
909 return opp->decode_format (text, spec, buffer_ptr);
912 /* Format decoder for dump_pretty_printer, and thus for dump_printf and
913 dump_printf_loc.
915 Supported format codes (in addition to the standard pretty_printer ones)
916 are:
918 %C: cgraph_node *:
919 Equivalent to: dump_symtab_node (MSG_*, node)
920 %E: gimple *:
921 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
922 %G: gimple *:
923 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
924 %T: tree:
925 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
927 TODO: add a format code that can handle (symtab_node*) *and* both
928 subclasses (presumably means teaching -Wformat about non-virtual
929 subclasses).
931 These format codes build optinfo_item instances, thus capturing metadata
932 about the arguments being dumped, as well as the textual output. */
934 bool
935 dump_pretty_printer::decode_format (text_info *text, const char *spec,
936 const char **buffer_ptr)
938 /* Various format codes that imply making an optinfo_item and stashed it
939 for later use (to capture metadata, rather than plain text). */
940 switch (*spec)
942 case 'C':
944 cgraph_node *node = va_arg (*text->args_ptr, cgraph_node *);
946 /* Make an item for the node, and stash it. */
947 optinfo_item *item = make_item_for_dump_symtab_node (node);
948 stash_item (buffer_ptr, item);
949 return true;
952 case 'E':
954 gimple *stmt = va_arg (*text->args_ptr, gimple *);
956 /* Make an item for the stmt, and stash it. */
957 optinfo_item *item = make_item_for_dump_gimple_expr (stmt, 0, TDF_SLIM);
958 stash_item (buffer_ptr, item);
959 return true;
962 case 'G':
964 gimple *stmt = va_arg (*text->args_ptr, gimple *);
966 /* Make an item for the stmt, and stash it. */
967 optinfo_item *item = make_item_for_dump_gimple_stmt (stmt, 0, TDF_SLIM);
968 stash_item (buffer_ptr, item);
969 return true;
972 case 'T':
974 tree t = va_arg (*text->args_ptr, tree);
976 /* Make an item for the tree, and stash it. */
977 optinfo_item *item = make_item_for_dump_generic_expr (t, TDF_SLIM);
978 stash_item (buffer_ptr, item);
979 return true;
982 default:
983 return false;
987 /* Output a formatted message using FORMAT on appropriate dump streams. */
989 void
990 dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
991 va_list *ap)
993 dump_pretty_printer pp (this, dump_kind);
995 text_info text;
996 text.err_no = errno;
997 text.args_ptr = ap;
998 text.format_spec = format;
1000 /* Phases 1 and 2, using pp_format. */
1001 pp_format (&pp, &text);
1003 /* Phase 3. */
1004 if (optinfo_enabled_p ())
1006 optinfo &info = ensure_pending_optinfo ();
1007 info.handle_dump_file_kind (dump_kind);
1008 pp.emit_items (&info);
1010 else
1011 pp.emit_items (NULL);
1014 /* Similar to dump_printf, except source location is also printed, and
1015 dump location captured. */
1017 void
1018 dump_context::dump_printf_loc_va (dump_flags_t dump_kind,
1019 const dump_location_t &loc,
1020 const char *format, va_list *ap)
1022 dump_loc (dump_kind, loc);
1023 dump_printf_va (dump_kind, format, ap);
1026 /* Make an item for the given dump call, equivalent to print_dec. */
1028 template<unsigned int N, typename C>
1029 static optinfo_item *
1030 make_item_for_dump_dec (const poly_int<N, C> &value)
1032 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
1033 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
1035 pretty_printer pp;
1037 if (value.is_constant ())
1038 pp_wide_int (&pp, value.coeffs[0], sgn);
1039 else
1041 pp_character (&pp, '[');
1042 for (unsigned int i = 0; i < N; ++i)
1044 pp_wide_int (&pp, value.coeffs[i], sgn);
1045 pp_character (&pp, i == N - 1 ? ']' : ',');
1049 optinfo_item *item
1050 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1051 xstrdup (pp_formatted_text (&pp)));
1052 return item;
1055 /* Output VALUE in decimal to appropriate dump streams. */
1057 template<unsigned int N, typename C>
1058 void
1059 dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
1061 optinfo_item *item = make_item_for_dump_dec (value);
1062 emit_item (item, dump_kind);
1064 if (optinfo_enabled_p ())
1066 optinfo &info = ensure_pending_optinfo ();
1067 info.handle_dump_file_kind (dump_kind);
1068 info.add_item (item);
1070 else
1071 delete item;
1074 /* Output the name of NODE on appropriate dump streams. */
1076 void
1077 dump_context::dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
1079 optinfo_item *item = make_item_for_dump_symtab_node (node);
1080 emit_item (item, dump_kind);
1082 if (optinfo_enabled_p ())
1084 optinfo &info = ensure_pending_optinfo ();
1085 info.handle_dump_file_kind (dump_kind);
1086 info.add_item (item);
1088 else
1089 delete item;
1092 /* Get the current dump scope-nesting depth.
1093 For use by -fopt-info (for showing nesting via indentation). */
1095 unsigned int
1096 dump_context::get_scope_depth () const
1098 return m_scope_depth;
1101 /* Push a nested dump scope.
1102 Increment the scope depth.
1103 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1104 destination, if any.
1105 Emit a "scope" optinfo if optinfos are enabled. */
1107 void
1108 dump_context::begin_scope (const char *name, const dump_location_t &loc)
1110 m_scope_depth++;
1112 if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
1113 ::dump_loc (MSG_NOTE, dump_file, loc.get_location_t ());
1115 if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
1116 ::dump_loc (MSG_NOTE, alt_dump_file, loc.get_location_t ());
1118 /* Support for temp_dump_context in selftests. */
1119 if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
1120 ::dump_loc (MSG_NOTE, m_test_pp, loc.get_location_t ());
1122 pretty_printer pp;
1123 pp_printf (&pp, "=== %s ===\n", name);
1124 optinfo_item *item
1125 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1126 xstrdup (pp_formatted_text (&pp)));
1127 emit_item (item, MSG_NOTE);
1129 if (optinfo_enabled_p ())
1131 optinfo &info = begin_next_optinfo (loc);
1132 info.m_kind = OPTINFO_KIND_SCOPE;
1133 info.add_item (item);
1134 end_any_optinfo ();
1136 else
1137 delete item;
1140 /* Pop a nested dump scope. */
1142 void
1143 dump_context::end_scope ()
1145 end_any_optinfo ();
1146 m_scope_depth--;
1148 if (m_json_writer)
1149 m_json_writer->pop_scope ();
1152 /* Should optinfo instances be created?
1153 All creation of optinfos should be guarded by this predicate.
1154 Return true if any optinfo destinations are active. */
1156 bool
1157 dump_context::optinfo_enabled_p () const
1159 return (optimization_records_enabled_p ());
1162 /* Return the optinfo currently being accumulated, creating one if
1163 necessary. */
1165 optinfo &
1166 dump_context::ensure_pending_optinfo ()
1168 if (!m_pending)
1169 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
1170 return *m_pending;
1173 /* Start a new optinfo and return it, ending any optinfo that was already
1174 accumulated. */
1176 optinfo &
1177 dump_context::begin_next_optinfo (const dump_location_t &loc)
1179 end_any_optinfo ();
1180 gcc_assert (m_pending == NULL);
1181 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
1182 return *m_pending;
1185 /* End any optinfo that has been accumulated within this context; emitting
1186 it to any destinations as appropriate, such as optimization records. */
1188 void
1189 dump_context::end_any_optinfo ()
1191 if (m_pending)
1192 emit_optinfo (m_pending);
1193 delete m_pending;
1194 m_pending = NULL;
1197 /* Emit the optinfo to all of the "non-immediate" destinations
1198 (emission to "immediate" destinations is done by
1199 dump_context::emit_item). */
1201 void
1202 dump_context::emit_optinfo (const optinfo *info)
1204 /* -fsave-optimization-record. */
1205 if (m_json_writer)
1206 m_json_writer->add_record (info);
1209 /* Emit ITEM to all item destinations (those that don't require
1210 consolidation into optinfo instances). */
1212 void
1213 dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
1215 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
1216 fprintf (dump_file, "%s", item->get_text ());
1218 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
1219 fprintf (alt_dump_file, "%s", item->get_text ());
1221 /* Support for temp_dump_context in selftests. */
1222 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
1223 pp_string (m_test_pp, item->get_text ());
1226 /* The current singleton dump_context, and its default. */
1228 dump_context *dump_context::s_current = &dump_context::s_default;
1229 dump_context dump_context::s_default;
1231 /* Implementation of dump_* API calls, calling into dump_context
1232 member functions. */
1234 /* Calls to the dump_* functions do non-trivial work, so they ought
1235 to be guarded by:
1236 if (dump_enabled_p ())
1237 Assert that they are guarded, and, if assertions are disabled,
1238 bail out if the calls weren't properly guarded. */
1240 #define VERIFY_DUMP_ENABLED_P \
1241 do { \
1242 gcc_assert (dump_enabled_p ()); \
1243 if (!dump_enabled_p ()) \
1244 return; \
1245 } while (0)
1247 /* Dump gimple statement GS with SPC indentation spaces and
1248 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1250 void
1251 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1252 gimple *gs, int spc)
1254 VERIFY_DUMP_ENABLED_P;
1255 dump_context::get ().dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
1258 /* Similar to dump_gimple_stmt, except additionally print source location. */
1260 void
1261 dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1262 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1264 VERIFY_DUMP_ENABLED_P;
1265 dump_context::get ().dump_gimple_stmt_loc (dump_kind, loc, extra_dump_flags,
1266 gs, spc);
1269 /* Dump gimple statement GS with SPC indentation spaces and
1270 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1271 Do not terminate with a newline or semicolon. */
1273 void
1274 dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1275 gimple *gs, int spc)
1277 VERIFY_DUMP_ENABLED_P;
1278 dump_context::get ().dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
1281 /* Similar to dump_gimple_expr, except additionally print source location. */
1283 void
1284 dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1285 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1287 VERIFY_DUMP_ENABLED_P;
1288 dump_context::get ().dump_gimple_expr_loc (dump_kind, loc, extra_dump_flags,
1289 gs, spc);
1292 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1293 DUMP_KIND is enabled. */
1295 void
1296 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1297 tree t)
1299 VERIFY_DUMP_ENABLED_P;
1300 dump_context::get ().dump_generic_expr (dump_kind, extra_dump_flags, t);
1303 /* Similar to dump_generic_expr, except additionally print the source
1304 location. */
1306 void
1307 dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1308 dump_flags_t extra_dump_flags, tree t)
1310 VERIFY_DUMP_ENABLED_P;
1311 dump_context::get ().dump_generic_expr_loc (dump_kind, loc, extra_dump_flags,
1315 /* Output a formatted message using FORMAT on appropriate dump streams. */
1317 void
1318 dump_printf (dump_flags_t dump_kind, const char *format, ...)
1320 VERIFY_DUMP_ENABLED_P;
1321 va_list ap;
1322 va_start (ap, format);
1323 dump_context::get ().dump_printf_va (dump_kind, format, &ap);
1324 va_end (ap);
1327 /* Similar to dump_printf, except source location is also printed, and
1328 dump location captured. */
1330 void
1331 dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1332 const char *format, ...)
1334 VERIFY_DUMP_ENABLED_P;
1335 va_list ap;
1336 va_start (ap, format);
1337 dump_context::get ().dump_printf_loc_va (dump_kind, loc, format, &ap);
1338 va_end (ap);
1341 /* Output VALUE in decimal to appropriate dump streams. */
1343 template<unsigned int N, typename C>
1344 void
1345 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
1347 VERIFY_DUMP_ENABLED_P;
1348 dump_context::get ().dump_dec (dump_kind, value);
1351 template void dump_dec (dump_flags_t, const poly_uint16 &);
1352 template void dump_dec (dump_flags_t, const poly_int64 &);
1353 template void dump_dec (dump_flags_t, const poly_uint64 &);
1354 template void dump_dec (dump_flags_t, const poly_offset_int &);
1355 template void dump_dec (dump_flags_t, const poly_widest_int &);
1357 void
1358 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1360 VERIFY_DUMP_ENABLED_P;
1361 if (dump_file
1362 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1363 print_dec (value, dump_file, sgn);
1365 if (alt_dump_file
1366 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1367 print_dec (value, alt_dump_file, sgn);
1370 /* Output VALUE in hexadecimal to appropriate dump streams. */
1372 void
1373 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1375 VERIFY_DUMP_ENABLED_P;
1376 if (dump_file
1377 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1378 print_hex (value, dump_file);
1380 if (alt_dump_file
1381 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1382 print_hex (value, alt_dump_file);
1385 /* Emit and delete the currently pending optinfo, if there is one,
1386 without the caller needing to know about class dump_context. */
1388 void
1389 dumpfile_ensure_any_optinfo_are_flushed ()
1391 dump_context::get().end_any_optinfo ();
1394 /* Output the name of NODE on appropriate dump streams. */
1396 void
1397 dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
1399 VERIFY_DUMP_ENABLED_P;
1400 dump_context::get ().dump_symtab_node (dump_kind, node);
1403 /* Get the current dump scope-nesting depth.
1404 For use by -fopt-info (for showing nesting via indentation). */
1406 unsigned int
1407 get_dump_scope_depth ()
1409 return dump_context::get ().get_scope_depth ();
1412 /* Push a nested dump scope.
1413 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1414 destination, if any.
1415 Emit a "scope" opinfo if optinfos are enabled.
1416 Increment the scope depth. */
1418 void
1419 dump_begin_scope (const char *name, const dump_location_t &loc)
1421 dump_context::get ().begin_scope (name, loc);
1424 /* Pop a nested dump scope. */
1426 void
1427 dump_end_scope ()
1429 dump_context::get ().end_scope ();
1432 /* Start a dump for PHASE. Store user-supplied dump flags in
1433 *FLAG_PTR. Return the number of streams opened. Set globals
1434 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1435 set dump_flags appropriately for both pass dump stream and
1436 -fopt-info stream. */
1439 gcc::dump_manager::
1440 dump_start (int phase, dump_flags_t *flag_ptr)
1442 int count = 0;
1443 char *name;
1444 struct dump_file_info *dfi;
1445 FILE *stream;
1446 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1447 return 0;
1449 dfi = get_dump_file_info (phase);
1450 name = get_dump_file_name (phase);
1451 if (name)
1453 stream = dump_open (name, dfi->pstate < 0);
1454 if (stream)
1456 dfi->pstate = 1;
1457 count++;
1459 free (name);
1460 dfi->pstream = stream;
1461 set_dump_file (dfi->pstream);
1462 /* Initialize current dump flags. */
1463 pflags = dfi->pflags;
1466 stream = dump_open_alternate_stream (dfi);
1467 if (stream)
1469 dfi->alt_stream = stream;
1470 count++;
1471 set_alt_dump_file (dfi->alt_stream);
1472 /* Initialize current -fopt-info flags. */
1473 alt_flags = dfi->alt_flags;
1476 if (flag_ptr)
1477 *flag_ptr = dfi->pflags;
1479 return count;
1482 /* Finish a tree dump for PHASE and close associated dump streams. Also
1483 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1485 void
1486 gcc::dump_manager::
1487 dump_finish (int phase)
1489 struct dump_file_info *dfi;
1491 if (phase < 0)
1492 return;
1493 dfi = get_dump_file_info (phase);
1494 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1495 fclose (dfi->pstream);
1497 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1498 fclose (dfi->alt_stream);
1500 dfi->alt_stream = NULL;
1501 dfi->pstream = NULL;
1502 set_dump_file (NULL);
1503 set_alt_dump_file (NULL);
1504 dump_flags = TDF_NONE;
1505 alt_flags = TDF_NONE;
1506 pflags = TDF_NONE;
1509 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1510 *FLAG_PTR and returns a stream to write to. If the dump is not
1511 enabled, returns NULL.
1512 PART can be used for dump files which should be split to multiple
1513 parts. PART == -1 indicates dump file with no parts.
1514 If PART is -1, multiple calls will reopen and append to the dump file. */
1516 FILE *
1517 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1519 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1522 FILE *
1523 gcc::dump_manager::
1524 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1526 char *name;
1527 struct dump_file_info *dfi;
1528 FILE *stream;
1530 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1531 return NULL;
1533 name = get_dump_file_name (phase, part);
1534 if (!name)
1535 return NULL;
1536 dfi = get_dump_file_info (phase);
1538 /* We do not support re-opening of dump files with parts. This would require
1539 tracking pstate per part of the dump file. */
1540 stream = dump_open (name, part != -1 || dfi->pstate < 0);
1541 if (stream)
1542 dfi->pstate = 1;
1543 free (name);
1545 if (flag_ptr)
1546 *flag_ptr = dfi->pflags;
1548 /* Initialize current flags */
1549 pflags = dfi->pflags;
1550 return stream;
1553 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1554 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1555 any phase. */
1558 gcc::dump_manager::
1559 dump_phase_enabled_p (int phase) const
1561 if (phase == TDI_tree_all)
1563 size_t i;
1564 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1565 if (dump_files[i].pstate || dump_files[i].alt_state)
1566 return 1;
1567 for (i = 0; i < m_extra_dump_files_in_use; i++)
1568 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1569 return 1;
1570 return 0;
1572 else
1574 struct dump_file_info *dfi = get_dump_file_info (phase);
1575 return dfi->pstate || dfi->alt_state;
1579 /* Returns nonzero if tree dump PHASE has been initialized. */
1582 gcc::dump_manager::
1583 dump_initialized_p (int phase) const
1585 struct dump_file_info *dfi = get_dump_file_info (phase);
1586 return dfi->pstate > 0 || dfi->alt_state > 0;
1589 /* Returns the switch name of PHASE. */
1591 const char *
1592 dump_flag_name (int phase)
1594 return g->get_dumps ()->dump_flag_name (phase);
1597 const char *
1598 gcc::dump_manager::
1599 dump_flag_name (int phase) const
1601 struct dump_file_info *dfi = get_dump_file_info (phase);
1602 return dfi->swtch;
1605 /* Handle -fdump-* and -fopt-info for a pass added after
1606 command-line options are parsed (those from plugins and
1607 those from backends).
1609 Because the registration of plugin/backend passes happens after the
1610 command-line options are parsed, the options that specify single
1611 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1612 passes. Therefore we currently can only enable dumping of
1613 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1614 are specified. This is done here.
1616 Similarly, the saved -fopt-info options are wired up to the new pass. */
1618 void
1619 gcc::dump_manager::register_pass (opt_pass *pass)
1621 gcc_assert (pass);
1623 register_one_dump_file (pass);
1625 dump_file_info *pass_dfi = get_dump_file_info (pass->static_pass_number);
1626 gcc_assert (pass_dfi);
1628 enum tree_dump_index tdi;
1629 if (pass->type == SIMPLE_IPA_PASS
1630 || pass->type == IPA_PASS)
1631 tdi = TDI_ipa_all;
1632 else if (pass->type == GIMPLE_PASS)
1633 tdi = TDI_tree_all;
1634 else
1635 tdi = TDI_rtl_all;
1636 const dump_file_info *tdi_dfi = get_dump_file_info (tdi);
1637 gcc_assert (tdi_dfi);
1639 /* Check if dump-all flag is specified. */
1640 if (tdi_dfi->pstate)
1642 pass_dfi->pstate = tdi_dfi->pstate;
1643 pass_dfi->pflags = tdi_dfi->pflags;
1646 update_dfi_for_opt_info (pass_dfi);
1649 /* Finish a tree dump for PHASE. STREAM is the stream created by
1650 dump_begin. */
1652 void
1653 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1655 if (stream != stderr && stream != stdout)
1656 fclose (stream);
1659 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1660 enabled tree dumps. */
1663 gcc::dump_manager::
1664 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1666 int n = 0;
1667 size_t i;
1669 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1671 if (dump_files[i].dkind == dkind)
1673 const char *old_filename = dump_files[i].pfilename;
1674 dump_files[i].pstate = -1;
1675 dump_files[i].pflags |= flags;
1676 n++;
1677 /* Override the existing filename. */
1678 if (filename)
1680 dump_files[i].pfilename = xstrdup (filename);
1681 /* Since it is a command-line provided file, which is
1682 common to all the phases, use it in append mode. */
1683 dump_files[i].pstate = 1;
1685 if (old_filename && filename != old_filename)
1686 free (CONST_CAST (char *, old_filename));
1690 for (i = 0; i < m_extra_dump_files_in_use; i++)
1692 if (m_extra_dump_files[i].dkind == dkind)
1694 const char *old_filename = m_extra_dump_files[i].pfilename;
1695 m_extra_dump_files[i].pstate = -1;
1696 m_extra_dump_files[i].pflags |= flags;
1697 n++;
1698 /* Override the existing filename. */
1699 if (filename)
1701 m_extra_dump_files[i].pfilename = xstrdup (filename);
1702 /* Since it is a command-line provided file, which is
1703 common to all the phases, use it in append mode. */
1704 m_extra_dump_files[i].pstate = 1;
1706 if (old_filename && filename != old_filename)
1707 free (CONST_CAST (char *, old_filename));
1711 return n;
1714 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1715 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1716 dumps. */
1719 gcc::dump_manager::
1720 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1721 const char *filename)
1723 int n = 0;
1725 m_optgroup_flags = optgroup_flags;
1726 m_optinfo_flags = flags;
1727 m_optinfo_filename = xstrdup (filename);
1729 for (size_t i = TDI_none + 1; i < (size_t) TDI_end; i++)
1730 if (update_dfi_for_opt_info (&dump_files[i]))
1731 n++;
1733 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1734 if (update_dfi_for_opt_info (&m_extra_dump_files[i]))
1735 n++;
1737 return n;
1740 /* Use the saved -fopt-info options to update DFI.
1741 Return true if the dump is enabled. */
1743 bool
1744 gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
1746 gcc_assert (dfi);
1748 if (!(dfi->optgroup_flags & m_optgroup_flags))
1749 return false;
1751 const char *old_filename = dfi->alt_filename;
1752 /* Since this file is shared among different passes, it
1753 should be opened in append mode. */
1754 dfi->alt_state = 1;
1755 dfi->alt_flags |= m_optinfo_flags;
1756 /* Override the existing filename. */
1757 if (m_optinfo_filename)
1758 dfi->alt_filename = xstrdup (m_optinfo_filename);
1759 if (old_filename && m_optinfo_filename != old_filename)
1760 free (CONST_CAST (char *, old_filename));
1762 return true;
1765 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1766 relevant details in the dump_files array. */
1769 gcc::dump_manager::
1770 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1772 const char *option_value;
1773 const char *ptr;
1774 dump_flags_t flags;
1776 if (doglob && !dfi->glob)
1777 return 0;
1779 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1780 if (!option_value)
1781 return 0;
1783 if (*option_value && *option_value != '-' && *option_value != '=')
1784 return 0;
1786 ptr = option_value;
1788 /* Retain "user-facing" and "internals" messages, but filter out
1789 those from an opt_problem being re-emitted at the top level
1790 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1791 messing up scan-tree-dump-times" in DejaGnu tests. */
1792 flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_INTERNALS;
1794 while (*ptr)
1796 const struct kv_pair<dump_flags_t> *option_ptr;
1797 const char *end_ptr;
1798 const char *eq_ptr;
1799 unsigned length;
1801 while (*ptr == '-')
1802 ptr++;
1803 end_ptr = strchr (ptr, '-');
1804 eq_ptr = strchr (ptr, '=');
1806 if (eq_ptr && !end_ptr)
1807 end_ptr = eq_ptr;
1809 if (!end_ptr)
1810 end_ptr = ptr + strlen (ptr);
1811 length = end_ptr - ptr;
1813 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1814 if (strlen (option_ptr->name) == length
1815 && !memcmp (option_ptr->name, ptr, length))
1817 flags |= option_ptr->value;
1818 goto found;
1821 if (*ptr == '=')
1823 /* Interpret rest of the argument as a dump filename. This
1824 filename overrides other command line filenames. */
1825 if (dfi->pfilename)
1826 free (CONST_CAST (char *, dfi->pfilename));
1827 dfi->pfilename = xstrdup (ptr + 1);
1828 break;
1830 else
1831 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1832 length, ptr, dfi->swtch);
1833 found:;
1834 ptr = end_ptr;
1837 dfi->pstate = -1;
1838 dfi->pflags |= flags;
1840 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1841 known dumps. */
1842 if (dfi->suffix == NULL)
1843 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1845 return 1;
1849 gcc::dump_manager::
1850 dump_switch_p (const char *arg)
1852 size_t i;
1853 int any = 0;
1855 for (i = TDI_none + 1; i != TDI_end; i++)
1856 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1858 /* Don't glob if we got a hit already */
1859 if (!any)
1860 for (i = TDI_none + 1; i != TDI_end; i++)
1861 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1863 for (i = 0; i < m_extra_dump_files_in_use; i++)
1864 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1866 if (!any)
1867 for (i = 0; i < m_extra_dump_files_in_use; i++)
1868 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1871 return any;
1874 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1875 and filename. Return non-zero if it is a recognized switch. */
1877 static int
1878 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1879 optgroup_flags_t *optgroup_flags, char **filename)
1881 const char *option_value;
1882 const char *ptr;
1884 option_value = arg;
1885 ptr = option_value;
1887 *filename = NULL;
1889 /* Default to filtering out "internals" messages, and retaining
1890 "user-facing" messages, and those from an opt_problem being
1891 re-emitted at the top level. */
1892 *flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_REEMITTED;
1894 *optgroup_flags = OPTGROUP_NONE;
1896 if (!ptr)
1897 return 1; /* Handle '-fopt-info' without any additional options. */
1899 while (*ptr)
1901 const char *end_ptr;
1902 const char *eq_ptr;
1903 unsigned length;
1905 while (*ptr == '-')
1906 ptr++;
1907 end_ptr = strchr (ptr, '-');
1908 eq_ptr = strchr (ptr, '=');
1910 if (eq_ptr && !end_ptr)
1911 end_ptr = eq_ptr;
1913 if (!end_ptr)
1914 end_ptr = ptr + strlen (ptr);
1915 length = end_ptr - ptr;
1917 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1918 option_ptr->name; option_ptr++)
1919 if (strlen (option_ptr->name) == length
1920 && !memcmp (option_ptr->name, ptr, length))
1922 *flags |= option_ptr->value;
1923 goto found;
1926 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1927 option_ptr->name; option_ptr++)
1928 if (strlen (option_ptr->name) == length
1929 && !memcmp (option_ptr->name, ptr, length))
1931 *optgroup_flags |= option_ptr->value;
1932 goto found;
1935 if (*ptr == '=')
1937 /* Interpret rest of the argument as a dump filename. This
1938 filename overrides other command line filenames. */
1939 *filename = xstrdup (ptr + 1);
1940 break;
1942 else
1944 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1945 length, ptr, arg);
1946 return 0;
1948 found:;
1949 ptr = end_ptr;
1952 return 1;
1955 /* Return non-zero if ARG is a recognized switch for
1956 -fopt-info. Return zero otherwise. */
1959 opt_info_switch_p (const char *arg)
1961 dump_flags_t flags;
1962 optgroup_flags_t optgroup_flags;
1963 char *filename;
1964 static char *file_seen = NULL;
1965 gcc::dump_manager *dumps = g->get_dumps ();
1967 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1968 return 0;
1970 if (!filename)
1971 filename = xstrdup ("stderr");
1973 /* Bail out if a different filename has been specified. */
1974 if (file_seen && strcmp (file_seen, filename))
1976 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1977 arg);
1978 return 1;
1981 file_seen = xstrdup (filename);
1982 if (!(flags & MSG_ALL_KINDS))
1983 flags |= MSG_OPTIMIZED_LOCATIONS;
1984 if (!optgroup_flags)
1985 optgroup_flags = OPTGROUP_ALL;
1987 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1990 /* Print basic block on the dump streams. */
1992 void
1993 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1995 if (dump_file
1996 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1997 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1998 if (alt_dump_file
1999 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
2000 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
2003 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
2005 void
2006 dump_function (int phase, tree fn)
2008 FILE *stream;
2009 dump_flags_t flags;
2011 stream = dump_begin (phase, &flags);
2012 if (stream)
2014 dump_function_to_file (fn, stream, flags);
2015 dump_end (phase, stream);
2019 /* Print information from the combine pass on dump_file. */
2021 void
2022 print_combine_total_stats (void)
2024 if (dump_file)
2025 dump_combine_total_stats (dump_file);
2028 /* Enable RTL dump for all the RTL passes. */
2030 bool
2031 enable_rtl_dump_file (void)
2033 gcc::dump_manager *dumps = g->get_dumps ();
2034 int num_enabled =
2035 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
2036 NULL);
2037 return num_enabled > 0;
2040 #if CHECKING_P
2042 namespace selftest {
2044 /* temp_dump_context's ctor. Temporarily override the dump_context
2045 (to forcibly enable optinfo-generation). */
2047 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
2048 bool forcibly_enable_dumping,
2049 dump_flags_t test_pp_flags)
2050 : m_context (),
2051 m_saved (&dump_context ().get ())
2053 dump_context::s_current = &m_context;
2054 if (forcibly_enable_optinfo)
2055 m_context.set_json_writer (new optrecord_json_writer ());
2056 /* Conditionally enable the test dump, so that we can verify both the
2057 dump_enabled_p and the !dump_enabled_p cases in selftests. */
2058 if (forcibly_enable_dumping)
2060 m_context.m_test_pp = &m_pp;
2061 m_context.m_test_pp_flags = test_pp_flags;
2064 dump_context::get ().refresh_dumps_are_enabled ();
2067 /* temp_dump_context's dtor. Restore the saved dump_context. */
2069 temp_dump_context::~temp_dump_context ()
2071 m_context.set_json_writer (NULL);
2073 dump_context::s_current = m_saved;
2075 dump_context::get ().refresh_dumps_are_enabled ();
2078 /* 0-terminate the text dumped so far, and return it. */
2080 const char *
2081 temp_dump_context::get_dumped_text ()
2083 return pp_formatted_text (&m_pp);
2086 /* Verify that the dump_location_t constructors capture the source location
2087 at which they were called (provided that the build compiler is sufficiently
2088 recent). */
2090 static void
2091 test_impl_location ()
2093 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2094 /* Default ctor. */
2096 dump_location_t loc;
2097 const int expected_line = __LINE__ - 1;
2098 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2099 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2102 /* Constructing from a gimple. */
2104 dump_location_t loc ((gimple *)NULL);
2105 const int expected_line = __LINE__ - 1;
2106 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2107 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2110 /* Constructing from an rtx_insn. */
2112 dump_location_t loc ((rtx_insn *)NULL);
2113 const int expected_line = __LINE__ - 1;
2114 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2115 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2117 #endif
2120 /* Verify that the text dumped so far in CONTEXT equals
2121 EXPECTED_TEXT, using LOC for the location of any failure.
2122 As a side-effect, the internal buffer is 0-terminated. */
2124 void
2125 verify_dumped_text (const location &loc,
2126 temp_dump_context *context,
2127 const char *expected_text)
2129 gcc_assert (context);
2130 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
2131 expected_text);
2134 /* Verify that ITEM has the expected values. */
2136 void
2137 verify_item (const location &loc,
2138 const optinfo_item *item,
2139 enum optinfo_item_kind expected_kind,
2140 location_t expected_location,
2141 const char *expected_text)
2143 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
2144 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
2145 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
2148 /* Verify that calls to the dump_* API are captured and consolidated into
2149 optimization records. */
2151 static void
2152 test_capture_of_dump_calls (const line_table_case &case_)
2154 /* Generate a location_t for testing. */
2155 line_table_test ltt (case_);
2156 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
2157 linemap_line_start (line_table, 5, 100);
2158 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2159 location_t decl_loc = linemap_position_for_column (line_table, 8);
2160 location_t stmt_loc = linemap_position_for_column (line_table, 10);
2161 if (stmt_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
2162 return;
2164 dump_location_t loc = dump_location_t::from_location_t (stmt_loc);
2166 gimple *stmt = gimple_build_return (NULL);
2167 gimple_set_location (stmt, stmt_loc);
2169 tree test_decl = build_decl (decl_loc, FUNCTION_DECL,
2170 get_identifier ("test_decl"),
2171 build_function_type_list (void_type_node,
2172 NULL_TREE));
2174 symbol_table_test tmp_symtab;
2176 cgraph_node *node = cgraph_node::get_create (test_decl);
2177 gcc_assert (node);
2179 /* Run all tests twice, with and then without optinfo enabled, to ensure
2180 that immediate destinations vs optinfo-based destinations both
2181 work, independently of each other, with no leaks. */
2182 for (int i = 0 ; i < 2; i++)
2184 bool with_optinfo = (i == 0);
2186 /* Test of dump_printf. */
2188 temp_dump_context tmp (with_optinfo, true,
2189 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2190 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
2192 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
2193 if (with_optinfo)
2195 optinfo *info = tmp.get_pending_optinfo ();
2196 ASSERT_TRUE (info != NULL);
2197 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2198 ASSERT_EQ (info->num_items (), 1);
2199 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
2203 /* Test of dump_printf with %T. */
2205 temp_dump_context tmp (with_optinfo, true,
2206 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2207 dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
2209 ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
2210 if (with_optinfo)
2212 optinfo *info = tmp.get_pending_optinfo ();
2213 ASSERT_TRUE (info != NULL);
2214 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2215 ASSERT_EQ (info->num_items (), 2);
2216 ASSERT_IS_TEXT (info->get_item (0), "tree: ");
2217 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2221 /* Test of dump_printf with %E. */
2223 temp_dump_context tmp (with_optinfo, true,
2224 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2225 dump_printf (MSG_NOTE, "gimple: %E", stmt);
2227 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
2228 if (with_optinfo)
2230 optinfo *info = tmp.get_pending_optinfo ();
2231 ASSERT_TRUE (info != NULL);
2232 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2233 ASSERT_EQ (info->num_items (), 2);
2234 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2235 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;");
2239 /* Test of dump_printf with %G. */
2241 temp_dump_context tmp (with_optinfo, true,
2242 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2243 dump_printf (MSG_NOTE, "gimple: %G", stmt);
2245 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
2246 if (with_optinfo)
2248 optinfo *info = tmp.get_pending_optinfo ();
2249 ASSERT_TRUE (info != NULL);
2250 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2251 ASSERT_EQ (info->num_items (), 2);
2252 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2253 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;\n");
2257 /* Test of dump_printf with %C. */
2259 temp_dump_context tmp (with_optinfo, true,
2260 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2261 dump_printf (MSG_NOTE, "node: %C", node);
2263 ASSERT_DUMPED_TEXT_EQ (tmp, "node: test_decl/0");
2264 if (with_optinfo)
2266 optinfo *info = tmp.get_pending_optinfo ();
2267 ASSERT_TRUE (info != NULL);
2268 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2269 ASSERT_EQ (info->num_items (), 2);
2270 ASSERT_IS_TEXT (info->get_item (0), "node: ");
2271 ASSERT_IS_SYMTAB_NODE (info->get_item (1), decl_loc, "test_decl/0");
2275 /* dump_print_loc with multiple format codes. This tests various
2276 things:
2277 - intermingling of text, format codes handled by the base
2278 pretty_printer, and dump-specific format codes
2279 - multiple dump-specific format codes: some consecutive, others
2280 separated by text, trailing text after the final one. */
2282 temp_dump_context tmp (with_optinfo, true,
2283 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2284 dump_printf_loc (MSG_NOTE, loc, "before %T and %T"
2285 " %i consecutive %E%E after\n",
2286 integer_zero_node, test_decl, 42, stmt, stmt);
2288 ASSERT_DUMPED_TEXT_EQ (tmp,
2289 "test.txt:5:10: note: before 0 and test_decl"
2290 " 42 consecutive return;return; after\n");
2291 if (with_optinfo)
2293 optinfo *info = tmp.get_pending_optinfo ();
2294 ASSERT_TRUE (info != NULL);
2295 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2296 ASSERT_EQ (info->num_items (), 8);
2297 ASSERT_IS_TEXT (info->get_item (0), "before ");
2298 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2299 ASSERT_IS_TEXT (info->get_item (2), " and ");
2300 ASSERT_IS_TREE (info->get_item (3), UNKNOWN_LOCATION, "test_decl");
2301 ASSERT_IS_TEXT (info->get_item (4), " 42 consecutive ");
2302 ASSERT_IS_GIMPLE (info->get_item (5), stmt_loc, "return;");
2303 ASSERT_IS_GIMPLE (info->get_item (6), stmt_loc, "return;");
2304 ASSERT_IS_TEXT (info->get_item (7), " after\n");
2308 /* Tree, via dump_generic_expr. */
2310 temp_dump_context tmp (with_optinfo, true,
2311 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2312 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
2313 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
2315 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
2316 if (with_optinfo)
2318 optinfo *info = tmp.get_pending_optinfo ();
2319 ASSERT_TRUE (info != NULL);
2320 ASSERT_EQ (info->get_location_t (), stmt_loc);
2321 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2322 ASSERT_EQ (info->num_items (), 2);
2323 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
2324 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2328 /* Tree, via dump_generic_expr_loc. */
2330 temp_dump_context tmp (with_optinfo, true,
2331 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2332 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
2334 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
2335 if (with_optinfo)
2337 optinfo *info = tmp.get_pending_optinfo ();
2338 ASSERT_TRUE (info != NULL);
2339 ASSERT_EQ (info->get_location_t (), stmt_loc);
2340 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2341 ASSERT_EQ (info->num_items (), 1);
2342 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
2346 /* Gimple. */
2348 /* dump_gimple_stmt_loc. */
2350 temp_dump_context tmp (with_optinfo, true,
2351 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2352 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2354 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
2355 if (with_optinfo)
2357 optinfo *info = tmp.get_pending_optinfo ();
2358 ASSERT_TRUE (info != NULL);
2359 ASSERT_EQ (info->num_items (), 1);
2360 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
2364 /* dump_gimple_stmt. */
2366 temp_dump_context tmp (with_optinfo, true,
2367 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2368 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
2370 ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
2371 if (with_optinfo)
2373 optinfo *info = tmp.get_pending_optinfo ();
2374 ASSERT_TRUE (info != NULL);
2375 ASSERT_EQ (info->num_items (), 1);
2376 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
2380 /* dump_gimple_expr_loc. */
2382 temp_dump_context tmp (with_optinfo, true,
2383 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2384 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2386 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
2387 if (with_optinfo)
2389 optinfo *info = tmp.get_pending_optinfo ();
2390 ASSERT_TRUE (info != NULL);
2391 ASSERT_EQ (info->num_items (), 1);
2392 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
2396 /* dump_gimple_expr. */
2398 temp_dump_context tmp (with_optinfo, true,
2399 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2400 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
2402 ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
2403 if (with_optinfo)
2405 optinfo *info = tmp.get_pending_optinfo ();
2406 ASSERT_TRUE (info != NULL);
2407 ASSERT_EQ (info->num_items (), 1);
2408 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
2413 /* symtab_node. */
2415 temp_dump_context tmp (with_optinfo, true,
2416 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2417 dump_symtab_node (MSG_NOTE, node);
2419 ASSERT_DUMPED_TEXT_EQ (tmp, "test_decl/0");
2420 if (with_optinfo)
2422 optinfo *info = tmp.get_pending_optinfo ();
2423 ASSERT_TRUE (info != NULL);
2424 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2425 ASSERT_EQ (info->num_items (), 1);
2426 ASSERT_IS_SYMTAB_NODE (info->get_item (0), decl_loc, "test_decl/0");
2430 /* poly_int. */
2432 temp_dump_context tmp (with_optinfo, true,
2433 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2434 dump_dec (MSG_NOTE, poly_int64 (42));
2436 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
2437 if (with_optinfo)
2439 optinfo *info = tmp.get_pending_optinfo ();
2440 ASSERT_TRUE (info != NULL);
2441 ASSERT_EQ (info->num_items (), 1);
2442 ASSERT_IS_TEXT (info->get_item (0), "42");
2446 /* Scopes. Test with all 4 combinations of
2447 filtering by MSG_PRIORITY_USER_FACING
2448 and/or filtering by MSG_PRIORITY_INTERNALS. */
2449 for (int j = 0; j < 3; j++)
2451 dump_flags_t dump_filter = MSG_ALL_KINDS;
2452 if (j % 2)
2453 dump_filter |= MSG_PRIORITY_USER_FACING;
2454 if (j / 2)
2455 dump_filter |= MSG_PRIORITY_INTERNALS;
2457 temp_dump_context tmp (with_optinfo, true, dump_filter);
2458 /* Emit various messages, mostly with implicit priority. */
2459 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2460 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, stmt,
2461 "explicitly internal msg\n");
2463 AUTO_DUMP_SCOPE ("outer scope", stmt);
2464 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2466 AUTO_DUMP_SCOPE ("middle scope", stmt);
2467 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2469 AUTO_DUMP_SCOPE ("inner scope", stmt);
2470 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
2471 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_USER_FACING, stmt,
2472 "explicitly user-facing msg\n");
2474 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2476 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2478 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2480 switch (dump_filter & MSG_ALL_PRIORITIES)
2482 default:
2483 gcc_unreachable ();
2484 case 0:
2485 ASSERT_DUMPED_TEXT_EQ (tmp, "");
2486 break;
2487 case MSG_PRIORITY_USER_FACING:
2488 ASSERT_DUMPED_TEXT_EQ
2489 (tmp,
2490 "test.txt:5:10: note: msg 1\n"
2491 "test.txt:5:10: note: explicitly user-facing msg\n"
2492 "test.txt:5:10: note: msg 7\n");
2493 break;
2494 case MSG_PRIORITY_INTERNALS:
2495 ASSERT_DUMPED_TEXT_EQ
2496 (tmp,
2497 "test.txt:5:10: note: explicitly internal msg\n"
2498 "test.txt:5:10: note: === outer scope ===\n"
2499 "test.txt:5:10: note: msg 2\n"
2500 "test.txt:5:10: note: === middle scope ===\n"
2501 "test.txt:5:10: note: msg 3\n"
2502 "test.txt:5:10: note: === inner scope ===\n"
2503 "test.txt:5:10: note: msg 4\n"
2504 "test.txt:5:10: note: msg 5\n"
2505 "test.txt:5:10: note: msg 6\n");
2506 break;
2507 case MSG_ALL_PRIORITIES:
2508 ASSERT_DUMPED_TEXT_EQ
2509 (tmp,
2510 "test.txt:5:10: note: msg 1\n"
2511 "test.txt:5:10: note: explicitly internal msg\n"
2512 "test.txt:5:10: note: === outer scope ===\n"
2513 "test.txt:5:10: note: msg 2\n"
2514 "test.txt:5:10: note: === middle scope ===\n"
2515 "test.txt:5:10: note: msg 3\n"
2516 "test.txt:5:10: note: === inner scope ===\n"
2517 "test.txt:5:10: note: msg 4\n"
2518 "test.txt:5:10: note: explicitly user-facing msg\n"
2519 "test.txt:5:10: note: msg 5\n"
2520 "test.txt:5:10: note: msg 6\n"
2521 "test.txt:5:10: note: msg 7\n");
2522 break;
2524 if (with_optinfo)
2526 optinfo *info = tmp.get_pending_optinfo ();
2527 ASSERT_TRUE (info != NULL);
2528 ASSERT_EQ (info->num_items (), 1);
2529 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
2534 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2535 above. */
2537 /* MSG_OPTIMIZED_LOCATIONS. */
2539 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2540 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
2541 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2542 OPTINFO_KIND_SUCCESS);
2545 /* MSG_MISSED_OPTIMIZATION. */
2547 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2548 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2549 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2550 OPTINFO_KIND_FAILURE);
2554 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2556 temp_dump_context tmp (false, true,
2557 MSG_OPTIMIZED_LOCATIONS | MSG_ALL_PRIORITIES);
2558 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2560 AUTO_DUMP_SCOPE ("outer scope", stmt);
2561 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2563 AUTO_DUMP_SCOPE ("middle scope", stmt);
2564 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2566 AUTO_DUMP_SCOPE ("inner scope", stmt);
2567 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt, "msg 4\n");
2569 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2571 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2573 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2575 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: optimized: msg 4\n");
2579 static void
2580 test_pr87025 ()
2582 dump_user_location_t loc
2583 = dump_user_location_t::from_location_t (UNKNOWN_LOCATION);
2585 temp_dump_context tmp (true, true,
2586 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2588 AUTO_DUMP_SCOPE ("outer scope", loc);
2589 dump_printf (MSG_NOTE, "msg1\n");
2593 /* Run all of the selftests within this file. */
2595 void
2596 dumpfile_c_tests ()
2598 test_impl_location ();
2599 for_each_line_table_case (test_capture_of_dump_calls);
2600 test_pr87025 ();
2603 } // namespace selftest
2605 #endif /* CHECKING_P */