Add -fopt-info-internals
[official-gcc.git] / gcc / dumpfile.c
blobe15edc7a31c73169ee44342630b71107729692d9
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 *, source_location);
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, source_location 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, source_location 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 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
519 or alt_dump_file change, or when changing dump_context in selftests. */
521 void
522 dump_context::refresh_dumps_are_enabled ()
524 dumps_are_enabled = (dump_file || alt_dump_file || optinfo_enabled_p ()
525 || m_test_pp);
528 /* Determine if a message of kind DUMP_KIND and at the current scope depth
529 should be printed.
531 Only show messages that match FILTER both on their kind *and*
532 their priority. */
534 bool
535 dump_context::apply_dump_filter_p (dump_flags_t dump_kind,
536 dump_flags_t filter) const
538 /* Few messages, if any, have an explicit MSG_PRIORITY.
539 If DUMP_KIND does, we'll use it.
540 Otherwise, generate an implicit priority value for the message based
541 on the current scope depth.
542 Messages at the top-level scope are MSG_PRIORITY_USER_FACING,
543 whereas those in nested scopes are MSG_PRIORITY_INTERNALS. */
544 if (!(dump_kind & MSG_ALL_PRIORITIES))
546 dump_flags_t implicit_priority
547 = (m_scope_depth > 0
548 ? MSG_PRIORITY_INTERNALS
549 : MSG_PRIORITY_USER_FACING);
550 dump_kind |= implicit_priority;
553 return (dump_kind & (filter & MSG_ALL_KINDS)
554 && dump_kind & (filter & MSG_ALL_PRIORITIES));
557 /* Print LOC to the appropriate dump destinations, given DUMP_KIND.
558 If optinfos are enabled, begin a new optinfo. */
560 void
561 dump_context::dump_loc (dump_flags_t dump_kind, const dump_location_t &loc)
563 end_any_optinfo ();
565 location_t srcloc = loc.get_location_t ();
567 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
568 ::dump_loc (dump_kind, dump_file, srcloc);
570 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
571 ::dump_loc (dump_kind, alt_dump_file, srcloc);
573 /* Support for temp_dump_context in selftests. */
574 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
575 ::dump_loc (dump_kind, m_test_pp, srcloc);
577 if (optinfo_enabled_p ())
579 optinfo &info = begin_next_optinfo (loc);
580 info.handle_dump_file_kind (dump_kind);
584 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
586 static optinfo_item *
587 make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
589 pretty_printer pp;
590 pp_needs_newline (&pp) = true;
591 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
592 pp_newline (&pp);
594 optinfo_item *item
595 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
596 xstrdup (pp_formatted_text (&pp)));
597 return item;
600 /* Dump gimple statement GS with SPC indentation spaces and
601 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
603 void
604 dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
605 dump_flags_t extra_dump_flags,
606 gimple *gs, int spc)
608 optinfo_item *item
609 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
610 emit_item (item, dump_kind);
612 if (optinfo_enabled_p ())
614 optinfo &info = ensure_pending_optinfo ();
615 info.handle_dump_file_kind (dump_kind);
616 info.add_item (item);
618 else
619 delete item;
622 /* Similar to dump_gimple_stmt, except additionally print source location. */
624 void
625 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind,
626 const dump_location_t &loc,
627 dump_flags_t extra_dump_flags,
628 gimple *gs, int spc)
630 dump_loc (dump_kind, loc);
631 dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
634 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
636 static optinfo_item *
637 make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
639 dump_flags |= TDF_RHS_ONLY;
640 pretty_printer pp;
641 pp_needs_newline (&pp) = true;
642 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
644 optinfo_item *item
645 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
646 xstrdup (pp_formatted_text (&pp)));
647 return item;
650 /* Dump gimple statement GS with SPC indentation spaces and
651 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
652 Do not terminate with a newline or semicolon. */
654 void
655 dump_context::dump_gimple_expr (dump_flags_t dump_kind,
656 dump_flags_t extra_dump_flags,
657 gimple *gs, int spc)
659 optinfo_item *item
660 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
661 emit_item (item, dump_kind);
663 if (optinfo_enabled_p ())
665 optinfo &info = ensure_pending_optinfo ();
666 info.handle_dump_file_kind (dump_kind);
667 info.add_item (item);
669 else
670 delete item;
673 /* Similar to dump_gimple_expr, except additionally print source location. */
675 void
676 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind,
677 const dump_location_t &loc,
678 dump_flags_t extra_dump_flags,
679 gimple *gs,
680 int spc)
682 dump_loc (dump_kind, loc);
683 dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
686 /* Make an item for the given dump call, equivalent to print_generic_expr. */
688 static optinfo_item *
689 make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
691 pretty_printer pp;
692 pp_needs_newline (&pp) = true;
693 pp_translate_identifiers (&pp) = false;
694 dump_generic_node (&pp, node, 0, dump_flags, false);
696 location_t loc = UNKNOWN_LOCATION;
697 if (EXPR_HAS_LOCATION (node))
698 loc = EXPR_LOCATION (node);
700 optinfo_item *item
701 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
702 xstrdup (pp_formatted_text (&pp)));
703 return item;
706 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
707 DUMP_KIND is enabled. */
709 void
710 dump_context::dump_generic_expr (dump_flags_t dump_kind,
711 dump_flags_t extra_dump_flags,
712 tree t)
714 optinfo_item *item
715 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
716 emit_item (item, dump_kind);
718 if (optinfo_enabled_p ())
720 optinfo &info = ensure_pending_optinfo ();
721 info.handle_dump_file_kind (dump_kind);
722 info.add_item (item);
724 else
725 delete item;
729 /* Similar to dump_generic_expr, except additionally print the source
730 location. */
732 void
733 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind,
734 const dump_location_t &loc,
735 dump_flags_t extra_dump_flags,
736 tree t)
738 dump_loc (dump_kind, loc);
739 dump_generic_expr (dump_kind, extra_dump_flags, t);
742 /* A subclass of pretty_printer for implementing dump_context::dump_printf_va.
743 In particular, the formatted chunks are captured as optinfo_item instances,
744 thus retaining metadata about the entities being dumped (e.g. source
745 locations), rather than just as plain text. */
747 class dump_pretty_printer : public pretty_printer
749 public:
750 dump_pretty_printer (dump_context *context, dump_flags_t dump_kind);
752 void emit_items (optinfo *dest);
754 private:
755 /* Information on an optinfo_item that was generated during phase 2 of
756 formatting. */
757 struct stashed_item
759 stashed_item (const char **buffer_ptr_, optinfo_item *item_)
760 : buffer_ptr (buffer_ptr_), item (item_) {}
761 const char **buffer_ptr;
762 optinfo_item *item;
765 static bool format_decoder_cb (pretty_printer *pp, text_info *text,
766 const char *spec, int /*precision*/,
767 bool /*wide*/, bool /*set_locus*/,
768 bool /*verbose*/, bool */*quoted*/,
769 const char **buffer_ptr);
771 bool decode_format (text_info *text, const char *spec,
772 const char **buffer_ptr);
774 void stash_item (const char **buffer_ptr, optinfo_item *item);
776 void emit_any_pending_textual_chunks (optinfo *dest);
778 void emit_item (optinfo_item *item, optinfo *dest);
780 dump_context *m_context;
781 dump_flags_t m_dump_kind;
782 auto_vec<stashed_item> m_stashed_items;
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 %E: gimple *:
919 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
920 %G: gimple *:
921 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
922 %T: tree:
923 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
925 FIXME: add symtab_node?
927 These format codes build optinfo_item instances, thus capturing metadata
928 about the arguments being dumped, as well as the textual output. */
930 bool
931 dump_pretty_printer::decode_format (text_info *text, const char *spec,
932 const char **buffer_ptr)
934 /* Various format codes that imply making an optinfo_item and stashed it
935 for later use (to capture metadata, rather than plain text). */
936 switch (*spec)
938 case 'E':
940 gimple *stmt = va_arg (*text->args_ptr, gimple *);
942 /* Make an item for the stmt, and stash it. */
943 optinfo_item *item = make_item_for_dump_gimple_expr (stmt, 0, TDF_SLIM);
944 stash_item (buffer_ptr, item);
945 return true;
948 case 'G':
950 gimple *stmt = va_arg (*text->args_ptr, gimple *);
952 /* Make an item for the stmt, and stash it. */
953 optinfo_item *item = make_item_for_dump_gimple_stmt (stmt, 0, TDF_SLIM);
954 stash_item (buffer_ptr, item);
955 return true;
958 case 'T':
960 tree t = va_arg (*text->args_ptr, tree);
962 /* Make an item for the tree, and stash it. */
963 optinfo_item *item = make_item_for_dump_generic_expr (t, TDF_SLIM);
964 stash_item (buffer_ptr, item);
965 return true;
968 default:
969 return false;
973 /* Output a formatted message using FORMAT on appropriate dump streams. */
975 void
976 dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
977 va_list *ap)
979 dump_pretty_printer pp (this, dump_kind);
981 text_info text;
982 text.err_no = errno;
983 text.args_ptr = ap;
984 text.format_spec = format;
986 /* Phases 1 and 2, using pp_format. */
987 pp_format (&pp, &text);
989 /* Phase 3. */
990 if (optinfo_enabled_p ())
992 optinfo &info = ensure_pending_optinfo ();
993 info.handle_dump_file_kind (dump_kind);
994 pp.emit_items (&info);
996 else
997 pp.emit_items (NULL);
1000 /* Similar to dump_printf, except source location is also printed, and
1001 dump location captured. */
1003 void
1004 dump_context::dump_printf_loc_va (dump_flags_t dump_kind,
1005 const dump_location_t &loc,
1006 const char *format, va_list *ap)
1008 dump_loc (dump_kind, loc);
1009 dump_printf_va (dump_kind, format, ap);
1012 /* Make an item for the given dump call, equivalent to print_dec. */
1014 template<unsigned int N, typename C>
1015 static optinfo_item *
1016 make_item_for_dump_dec (const poly_int<N, C> &value)
1018 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
1019 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
1021 pretty_printer pp;
1023 if (value.is_constant ())
1024 pp_wide_int (&pp, value.coeffs[0], sgn);
1025 else
1027 pp_character (&pp, '[');
1028 for (unsigned int i = 0; i < N; ++i)
1030 pp_wide_int (&pp, value.coeffs[i], sgn);
1031 pp_character (&pp, i == N - 1 ? ']' : ',');
1035 optinfo_item *item
1036 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1037 xstrdup (pp_formatted_text (&pp)));
1038 return item;
1041 /* Output VALUE in decimal to appropriate dump streams. */
1043 template<unsigned int N, typename C>
1044 void
1045 dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
1047 optinfo_item *item = make_item_for_dump_dec (value);
1048 emit_item (item, dump_kind);
1050 if (optinfo_enabled_p ())
1052 optinfo &info = ensure_pending_optinfo ();
1053 info.handle_dump_file_kind (dump_kind);
1054 info.add_item (item);
1056 else
1057 delete item;
1060 /* Make an item for the given dump call. */
1062 static optinfo_item *
1063 make_item_for_dump_symtab_node (symtab_node *node)
1065 location_t loc = DECL_SOURCE_LOCATION (node->decl);
1066 optinfo_item *item
1067 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
1068 xstrdup (node->dump_name ()));
1069 return item;
1072 /* Output the name of NODE on appropriate dump streams. */
1074 void
1075 dump_context::dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
1077 optinfo_item *item = make_item_for_dump_symtab_node (node);
1078 emit_item (item, dump_kind);
1080 if (optinfo_enabled_p ())
1082 optinfo &info = ensure_pending_optinfo ();
1083 info.handle_dump_file_kind (dump_kind);
1084 info.add_item (item);
1086 else
1087 delete item;
1090 /* Get the current dump scope-nesting depth.
1091 For use by -fopt-info (for showing nesting via indentation). */
1093 unsigned int
1094 dump_context::get_scope_depth () const
1096 return m_scope_depth;
1099 /* Push a nested dump scope.
1100 Increment the scope depth.
1101 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1102 destination, if any.
1103 Emit a "scope" optinfo if optinfos are enabled. */
1105 void
1106 dump_context::begin_scope (const char *name, const dump_location_t &loc)
1108 m_scope_depth++;
1110 if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
1111 ::dump_loc (MSG_NOTE, dump_file, loc.get_location_t ());
1113 if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
1114 ::dump_loc (MSG_NOTE, alt_dump_file, loc.get_location_t ());
1116 /* Support for temp_dump_context in selftests. */
1117 if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
1118 ::dump_loc (MSG_NOTE, m_test_pp, loc.get_location_t ());
1120 pretty_printer pp;
1121 pp_printf (&pp, "=== %s ===\n", name);
1122 optinfo_item *item
1123 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1124 xstrdup (pp_formatted_text (&pp)));
1125 emit_item (item, MSG_NOTE);
1127 if (optinfo_enabled_p ())
1129 optinfo &info = begin_next_optinfo (loc);
1130 info.m_kind = OPTINFO_KIND_SCOPE;
1131 info.add_item (item);
1133 else
1134 delete item;
1137 /* Pop a nested dump scope. */
1139 void
1140 dump_context::end_scope ()
1142 end_any_optinfo ();
1143 m_scope_depth--;
1144 optimization_records_maybe_pop_dump_scope ();
1147 /* Return the optinfo currently being accumulated, creating one if
1148 necessary. */
1150 optinfo &
1151 dump_context::ensure_pending_optinfo ()
1153 if (!m_pending)
1154 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
1155 return *m_pending;
1158 /* Start a new optinfo and return it, ending any optinfo that was already
1159 accumulated. */
1161 optinfo &
1162 dump_context::begin_next_optinfo (const dump_location_t &loc)
1164 end_any_optinfo ();
1165 gcc_assert (m_pending == NULL);
1166 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
1167 return *m_pending;
1170 /* End any optinfo that has been accumulated within this context; emitting
1171 it to any destinations as appropriate, such as optimization records. */
1173 void
1174 dump_context::end_any_optinfo ()
1176 if (m_pending)
1177 m_pending->emit ();
1178 delete m_pending;
1179 m_pending = NULL;
1182 /* Emit ITEM to all item destinations (those that don't require
1183 consolidation into optinfo instances). */
1185 void
1186 dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
1188 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
1189 fprintf (dump_file, "%s", item->get_text ());
1191 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
1192 fprintf (alt_dump_file, "%s", item->get_text ());
1194 /* Support for temp_dump_context in selftests. */
1195 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
1196 pp_string (m_test_pp, item->get_text ());
1199 /* The current singleton dump_context, and its default. */
1201 dump_context *dump_context::s_current = &dump_context::s_default;
1202 dump_context dump_context::s_default;
1204 /* Implementation of dump_* API calls, calling into dump_context
1205 member functions. */
1207 /* Dump gimple statement GS with SPC indentation spaces and
1208 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1210 void
1211 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1212 gimple *gs, int spc)
1214 dump_context::get ().dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
1217 /* Similar to dump_gimple_stmt, except additionally print source location. */
1219 void
1220 dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1221 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1223 dump_context::get ().dump_gimple_stmt_loc (dump_kind, loc, extra_dump_flags,
1224 gs, spc);
1227 /* Dump gimple statement GS with SPC indentation spaces and
1228 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1229 Do not terminate with a newline or semicolon. */
1231 void
1232 dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1233 gimple *gs, int spc)
1235 dump_context::get ().dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
1238 /* Similar to dump_gimple_expr, except additionally print source location. */
1240 void
1241 dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1242 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1244 dump_context::get ().dump_gimple_expr_loc (dump_kind, loc, extra_dump_flags,
1245 gs, spc);
1248 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1249 DUMP_KIND is enabled. */
1251 void
1252 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1253 tree t)
1255 dump_context::get ().dump_generic_expr (dump_kind, extra_dump_flags, t);
1258 /* Similar to dump_generic_expr, except additionally print the source
1259 location. */
1261 void
1262 dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1263 dump_flags_t extra_dump_flags, tree t)
1265 dump_context::get ().dump_generic_expr_loc (dump_kind, loc, extra_dump_flags,
1269 /* Output a formatted message using FORMAT on appropriate dump streams. */
1271 void
1272 dump_printf (dump_flags_t dump_kind, const char *format, ...)
1274 va_list ap;
1275 va_start (ap, format);
1276 dump_context::get ().dump_printf_va (dump_kind, format, &ap);
1277 va_end (ap);
1280 /* Similar to dump_printf, except source location is also printed, and
1281 dump location captured. */
1283 void
1284 dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1285 const char *format, ...)
1287 va_list ap;
1288 va_start (ap, format);
1289 dump_context::get ().dump_printf_loc_va (dump_kind, loc, format, &ap);
1290 va_end (ap);
1293 /* Output VALUE in decimal to appropriate dump streams. */
1295 template<unsigned int N, typename C>
1296 void
1297 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
1299 dump_context::get ().dump_dec (dump_kind, value);
1302 template void dump_dec (dump_flags_t, const poly_uint16 &);
1303 template void dump_dec (dump_flags_t, const poly_int64 &);
1304 template void dump_dec (dump_flags_t, const poly_uint64 &);
1305 template void dump_dec (dump_flags_t, const poly_offset_int &);
1306 template void dump_dec (dump_flags_t, const poly_widest_int &);
1308 void
1309 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1311 if (dump_file
1312 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1313 print_dec (value, dump_file, sgn);
1315 if (alt_dump_file
1316 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1317 print_dec (value, alt_dump_file, sgn);
1320 /* Output VALUE in hexadecimal to appropriate dump streams. */
1322 void
1323 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1325 if (dump_file
1326 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1327 print_hex (value, dump_file);
1329 if (alt_dump_file
1330 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1331 print_hex (value, alt_dump_file);
1334 /* Emit and delete the currently pending optinfo, if there is one,
1335 without the caller needing to know about class dump_context. */
1337 void
1338 dumpfile_ensure_any_optinfo_are_flushed ()
1340 dump_context::get().end_any_optinfo ();
1343 /* Output the name of NODE on appropriate dump streams. */
1345 void
1346 dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
1348 dump_context::get ().dump_symtab_node (dump_kind, node);
1351 /* Get the current dump scope-nesting depth.
1352 For use by -fopt-info (for showing nesting via indentation). */
1354 unsigned int
1355 get_dump_scope_depth ()
1357 return dump_context::get ().get_scope_depth ();
1360 /* Push a nested dump scope.
1361 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1362 destination, if any.
1363 Emit a "scope" opinfo if optinfos are enabled.
1364 Increment the scope depth. */
1366 void
1367 dump_begin_scope (const char *name, const dump_location_t &loc)
1369 dump_context::get ().begin_scope (name, loc);
1372 /* Pop a nested dump scope. */
1374 void
1375 dump_end_scope ()
1377 dump_context::get ().end_scope ();
1380 /* Start a dump for PHASE. Store user-supplied dump flags in
1381 *FLAG_PTR. Return the number of streams opened. Set globals
1382 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1383 set dump_flags appropriately for both pass dump stream and
1384 -fopt-info stream. */
1387 gcc::dump_manager::
1388 dump_start (int phase, dump_flags_t *flag_ptr)
1390 int count = 0;
1391 char *name;
1392 struct dump_file_info *dfi;
1393 FILE *stream;
1394 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1395 return 0;
1397 dfi = get_dump_file_info (phase);
1398 name = get_dump_file_name (phase);
1399 if (name)
1401 stream = dump_open (name, dfi->pstate < 0);
1402 if (stream)
1404 dfi->pstate = 1;
1405 count++;
1407 free (name);
1408 dfi->pstream = stream;
1409 set_dump_file (dfi->pstream);
1410 /* Initialize current dump flags. */
1411 pflags = dfi->pflags;
1414 stream = dump_open_alternate_stream (dfi);
1415 if (stream)
1417 dfi->alt_stream = stream;
1418 count++;
1419 set_alt_dump_file (dfi->alt_stream);
1420 /* Initialize current -fopt-info flags. */
1421 alt_flags = dfi->alt_flags;
1424 if (flag_ptr)
1425 *flag_ptr = dfi->pflags;
1427 return count;
1430 /* Finish a tree dump for PHASE and close associated dump streams. Also
1431 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1433 void
1434 gcc::dump_manager::
1435 dump_finish (int phase)
1437 struct dump_file_info *dfi;
1439 if (phase < 0)
1440 return;
1441 dfi = get_dump_file_info (phase);
1442 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1443 fclose (dfi->pstream);
1445 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1446 fclose (dfi->alt_stream);
1448 dfi->alt_stream = NULL;
1449 dfi->pstream = NULL;
1450 set_dump_file (NULL);
1451 set_alt_dump_file (NULL);
1452 dump_flags = TDF_NONE;
1453 alt_flags = TDF_NONE;
1454 pflags = TDF_NONE;
1457 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1458 *FLAG_PTR and returns a stream to write to. If the dump is not
1459 enabled, returns NULL.
1460 PART can be used for dump files which should be split to multiple
1461 parts. PART == -1 indicates dump file with no parts.
1462 If PART is -1, multiple calls will reopen and append to the dump file. */
1464 FILE *
1465 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1467 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1470 FILE *
1471 gcc::dump_manager::
1472 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1474 char *name;
1475 struct dump_file_info *dfi;
1476 FILE *stream;
1478 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1479 return NULL;
1481 name = get_dump_file_name (phase, part);
1482 if (!name)
1483 return NULL;
1484 dfi = get_dump_file_info (phase);
1486 /* We do not support re-opening of dump files with parts. This would require
1487 tracking pstate per part of the dump file. */
1488 stream = dump_open (name, part != -1 || dfi->pstate < 0);
1489 if (stream)
1490 dfi->pstate = 1;
1491 free (name);
1493 if (flag_ptr)
1494 *flag_ptr = dfi->pflags;
1496 /* Initialize current flags */
1497 pflags = dfi->pflags;
1498 return stream;
1501 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1502 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1503 any phase. */
1506 gcc::dump_manager::
1507 dump_phase_enabled_p (int phase) const
1509 if (phase == TDI_tree_all)
1511 size_t i;
1512 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1513 if (dump_files[i].pstate || dump_files[i].alt_state)
1514 return 1;
1515 for (i = 0; i < m_extra_dump_files_in_use; i++)
1516 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1517 return 1;
1518 return 0;
1520 else
1522 struct dump_file_info *dfi = get_dump_file_info (phase);
1523 return dfi->pstate || dfi->alt_state;
1527 /* Returns nonzero if tree dump PHASE has been initialized. */
1530 gcc::dump_manager::
1531 dump_initialized_p (int phase) const
1533 struct dump_file_info *dfi = get_dump_file_info (phase);
1534 return dfi->pstate > 0 || dfi->alt_state > 0;
1537 /* Returns the switch name of PHASE. */
1539 const char *
1540 dump_flag_name (int phase)
1542 return g->get_dumps ()->dump_flag_name (phase);
1545 const char *
1546 gcc::dump_manager::
1547 dump_flag_name (int phase) const
1549 struct dump_file_info *dfi = get_dump_file_info (phase);
1550 return dfi->swtch;
1553 /* Handle -fdump-* and -fopt-info for a pass added after
1554 command-line options are parsed (those from plugins and
1555 those from backends).
1557 Because the registration of plugin/backend passes happens after the
1558 command-line options are parsed, the options that specify single
1559 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1560 passes. Therefore we currently can only enable dumping of
1561 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1562 are specified. This is done here.
1564 Similarly, the saved -fopt-info options are wired up to the new pass. */
1566 void
1567 gcc::dump_manager::register_pass (opt_pass *pass)
1569 gcc_assert (pass);
1571 register_one_dump_file (pass);
1573 dump_file_info *pass_dfi = get_dump_file_info (pass->static_pass_number);
1574 gcc_assert (pass_dfi);
1576 enum tree_dump_index tdi;
1577 if (pass->type == SIMPLE_IPA_PASS
1578 || pass->type == IPA_PASS)
1579 tdi = TDI_ipa_all;
1580 else if (pass->type == GIMPLE_PASS)
1581 tdi = TDI_tree_all;
1582 else
1583 tdi = TDI_rtl_all;
1584 const dump_file_info *tdi_dfi = get_dump_file_info (tdi);
1585 gcc_assert (tdi_dfi);
1587 /* Check if dump-all flag is specified. */
1588 if (tdi_dfi->pstate)
1590 pass_dfi->pstate = tdi_dfi->pstate;
1591 pass_dfi->pflags = tdi_dfi->pflags;
1594 update_dfi_for_opt_info (pass_dfi);
1597 /* Finish a tree dump for PHASE. STREAM is the stream created by
1598 dump_begin. */
1600 void
1601 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1603 if (stream != stderr && stream != stdout)
1604 fclose (stream);
1607 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1608 enabled tree dumps. */
1611 gcc::dump_manager::
1612 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1614 int n = 0;
1615 size_t i;
1617 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1619 if (dump_files[i].dkind == dkind)
1621 const char *old_filename = dump_files[i].pfilename;
1622 dump_files[i].pstate = -1;
1623 dump_files[i].pflags |= flags;
1624 n++;
1625 /* Override the existing filename. */
1626 if (filename)
1628 dump_files[i].pfilename = xstrdup (filename);
1629 /* Since it is a command-line provided file, which is
1630 common to all the phases, use it in append mode. */
1631 dump_files[i].pstate = 1;
1633 if (old_filename && filename != old_filename)
1634 free (CONST_CAST (char *, old_filename));
1638 for (i = 0; i < m_extra_dump_files_in_use; i++)
1640 if (m_extra_dump_files[i].dkind == dkind)
1642 const char *old_filename = m_extra_dump_files[i].pfilename;
1643 m_extra_dump_files[i].pstate = -1;
1644 m_extra_dump_files[i].pflags |= flags;
1645 n++;
1646 /* Override the existing filename. */
1647 if (filename)
1649 m_extra_dump_files[i].pfilename = xstrdup (filename);
1650 /* Since it is a command-line provided file, which is
1651 common to all the phases, use it in append mode. */
1652 m_extra_dump_files[i].pstate = 1;
1654 if (old_filename && filename != old_filename)
1655 free (CONST_CAST (char *, old_filename));
1659 return n;
1662 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1663 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1664 dumps. */
1667 gcc::dump_manager::
1668 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1669 const char *filename)
1671 int n = 0;
1673 m_optgroup_flags = optgroup_flags;
1674 m_optinfo_flags = flags;
1675 m_optinfo_filename = xstrdup (filename);
1677 for (size_t i = TDI_none + 1; i < (size_t) TDI_end; i++)
1678 if (update_dfi_for_opt_info (&dump_files[i]))
1679 n++;
1681 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1682 if (update_dfi_for_opt_info (&m_extra_dump_files[i]))
1683 n++;
1685 return n;
1688 /* Use the saved -fopt-info options to update DFI.
1689 Return true if the dump is enabled. */
1691 bool
1692 gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
1694 gcc_assert (dfi);
1696 if (!(dfi->optgroup_flags & m_optgroup_flags))
1697 return false;
1699 const char *old_filename = dfi->alt_filename;
1700 /* Since this file is shared among different passes, it
1701 should be opened in append mode. */
1702 dfi->alt_state = 1;
1703 dfi->alt_flags |= m_optinfo_flags;
1704 /* Override the existing filename. */
1705 if (m_optinfo_filename)
1706 dfi->alt_filename = xstrdup (m_optinfo_filename);
1707 if (old_filename && m_optinfo_filename != old_filename)
1708 free (CONST_CAST (char *, old_filename));
1710 return true;
1713 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1714 relevant details in the dump_files array. */
1717 gcc::dump_manager::
1718 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1720 const char *option_value;
1721 const char *ptr;
1722 dump_flags_t flags;
1724 if (doglob && !dfi->glob)
1725 return 0;
1727 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1728 if (!option_value)
1729 return 0;
1731 if (*option_value && *option_value != '-' && *option_value != '=')
1732 return 0;
1734 ptr = option_value;
1735 flags = MSG_ALL_PRIORITIES;
1737 while (*ptr)
1739 const struct kv_pair<dump_flags_t> *option_ptr;
1740 const char *end_ptr;
1741 const char *eq_ptr;
1742 unsigned length;
1744 while (*ptr == '-')
1745 ptr++;
1746 end_ptr = strchr (ptr, '-');
1747 eq_ptr = strchr (ptr, '=');
1749 if (eq_ptr && !end_ptr)
1750 end_ptr = eq_ptr;
1752 if (!end_ptr)
1753 end_ptr = ptr + strlen (ptr);
1754 length = end_ptr - ptr;
1756 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1757 if (strlen (option_ptr->name) == length
1758 && !memcmp (option_ptr->name, ptr, length))
1760 flags |= option_ptr->value;
1761 goto found;
1764 if (*ptr == '=')
1766 /* Interpret rest of the argument as a dump filename. This
1767 filename overrides other command line filenames. */
1768 if (dfi->pfilename)
1769 free (CONST_CAST (char *, dfi->pfilename));
1770 dfi->pfilename = xstrdup (ptr + 1);
1771 break;
1773 else
1774 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1775 length, ptr, dfi->swtch);
1776 found:;
1777 ptr = end_ptr;
1780 dfi->pstate = -1;
1781 dfi->pflags |= flags;
1783 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1784 known dumps. */
1785 if (dfi->suffix == NULL)
1786 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1788 return 1;
1792 gcc::dump_manager::
1793 dump_switch_p (const char *arg)
1795 size_t i;
1796 int any = 0;
1798 for (i = TDI_none + 1; i != TDI_end; i++)
1799 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1801 /* Don't glob if we got a hit already */
1802 if (!any)
1803 for (i = TDI_none + 1; i != TDI_end; i++)
1804 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1806 for (i = 0; i < m_extra_dump_files_in_use; i++)
1807 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1809 if (!any)
1810 for (i = 0; i < m_extra_dump_files_in_use; i++)
1811 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1814 return any;
1817 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1818 and filename. Return non-zero if it is a recognized switch. */
1820 static int
1821 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1822 optgroup_flags_t *optgroup_flags, char **filename)
1824 const char *option_value;
1825 const char *ptr;
1827 option_value = arg;
1828 ptr = option_value;
1830 *filename = NULL;
1832 /* Default to filtering out "internals" messages, and retaining
1833 "user-facing" messages. */
1834 *flags = MSG_PRIORITY_USER_FACING;
1836 *optgroup_flags = OPTGROUP_NONE;
1838 if (!ptr)
1839 return 1; /* Handle '-fopt-info' without any additional options. */
1841 while (*ptr)
1843 const char *end_ptr;
1844 const char *eq_ptr;
1845 unsigned length;
1847 while (*ptr == '-')
1848 ptr++;
1849 end_ptr = strchr (ptr, '-');
1850 eq_ptr = strchr (ptr, '=');
1852 if (eq_ptr && !end_ptr)
1853 end_ptr = eq_ptr;
1855 if (!end_ptr)
1856 end_ptr = ptr + strlen (ptr);
1857 length = end_ptr - ptr;
1859 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1860 option_ptr->name; option_ptr++)
1861 if (strlen (option_ptr->name) == length
1862 && !memcmp (option_ptr->name, ptr, length))
1864 *flags |= option_ptr->value;
1865 goto found;
1868 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1869 option_ptr->name; option_ptr++)
1870 if (strlen (option_ptr->name) == length
1871 && !memcmp (option_ptr->name, ptr, length))
1873 *optgroup_flags |= option_ptr->value;
1874 goto found;
1877 if (*ptr == '=')
1879 /* Interpret rest of the argument as a dump filename. This
1880 filename overrides other command line filenames. */
1881 *filename = xstrdup (ptr + 1);
1882 break;
1884 else
1886 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1887 length, ptr, arg);
1888 return 0;
1890 found:;
1891 ptr = end_ptr;
1894 return 1;
1897 /* Return non-zero if ARG is a recognized switch for
1898 -fopt-info. Return zero otherwise. */
1901 opt_info_switch_p (const char *arg)
1903 dump_flags_t flags;
1904 optgroup_flags_t optgroup_flags;
1905 char *filename;
1906 static char *file_seen = NULL;
1907 gcc::dump_manager *dumps = g->get_dumps ();
1909 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1910 return 0;
1912 if (!filename)
1913 filename = xstrdup ("stderr");
1915 /* Bail out if a different filename has been specified. */
1916 if (file_seen && strcmp (file_seen, filename))
1918 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1919 arg);
1920 return 1;
1923 file_seen = xstrdup (filename);
1924 if (!(flags & MSG_ALL_KINDS))
1925 flags |= MSG_OPTIMIZED_LOCATIONS;
1926 if (!optgroup_flags)
1927 optgroup_flags = OPTGROUP_ALL;
1929 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1932 /* Print basic block on the dump streams. */
1934 void
1935 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1937 if (dump_file
1938 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1939 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1940 if (alt_dump_file
1941 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1942 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
1945 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1947 void
1948 dump_function (int phase, tree fn)
1950 FILE *stream;
1951 dump_flags_t flags;
1953 stream = dump_begin (phase, &flags);
1954 if (stream)
1956 dump_function_to_file (fn, stream, flags);
1957 dump_end (phase, stream);
1961 /* Print information from the combine pass on dump_file. */
1963 void
1964 print_combine_total_stats (void)
1966 if (dump_file)
1967 dump_combine_total_stats (dump_file);
1970 /* Enable RTL dump for all the RTL passes. */
1972 bool
1973 enable_rtl_dump_file (void)
1975 gcc::dump_manager *dumps = g->get_dumps ();
1976 int num_enabled =
1977 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
1978 NULL);
1979 return num_enabled > 0;
1982 #if CHECKING_P
1984 /* temp_dump_context's ctor. Temporarily override the dump_context
1985 (to forcibly enable optinfo-generation). */
1987 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
1988 dump_flags_t test_pp_flags)
1990 : m_context (),
1991 m_saved (&dump_context ().get ())
1993 dump_context::s_current = &m_context;
1994 m_context.m_forcibly_enable_optinfo = forcibly_enable_optinfo;
1995 m_context.m_test_pp = &m_pp;
1996 m_context.m_test_pp_flags = test_pp_flags;
1998 dump_context::get ().refresh_dumps_are_enabled ();
2001 /* temp_dump_context's dtor. Restore the saved dump_context. */
2003 temp_dump_context::~temp_dump_context ()
2005 dump_context::s_current = m_saved;
2007 dump_context::get ().refresh_dumps_are_enabled ();
2010 /* 0-terminate the text dumped so far, and return it. */
2012 const char *
2013 temp_dump_context::get_dumped_text ()
2015 return pp_formatted_text (&m_pp);
2018 namespace selftest {
2020 /* Verify that the dump_location_t constructors capture the source location
2021 at which they were called (provided that the build compiler is sufficiently
2022 recent). */
2024 static void
2025 test_impl_location ()
2027 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2028 /* Default ctor. */
2030 dump_location_t loc;
2031 const int expected_line = __LINE__ - 1;
2032 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2033 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2036 /* Constructing from a gimple. */
2038 dump_location_t loc ((gimple *)NULL);
2039 const int expected_line = __LINE__ - 1;
2040 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2041 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2044 /* Constructing from an rtx_insn. */
2046 dump_location_t loc ((rtx_insn *)NULL);
2047 const int expected_line = __LINE__ - 1;
2048 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2049 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2051 #endif
2054 /* Verify that the text dumped so far in CONTEXT equals
2055 EXPECTED_TEXT, using LOC for the location of any failure.
2056 As a side-effect, the internal buffer is 0-terminated. */
2058 static void
2059 verify_dumped_text (const location &loc,
2060 temp_dump_context *context,
2061 const char *expected_text)
2063 gcc_assert (context);
2064 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
2065 expected_text);
2068 /* Verify that the text dumped so far in CONTEXT equals
2069 EXPECTED_TEXT.
2070 As a side-effect, the internal buffer is 0-terminated. */
2072 #define ASSERT_DUMPED_TEXT_EQ(CONTEXT, EXPECTED_TEXT) \
2073 SELFTEST_BEGIN_STMT \
2074 verify_dumped_text (SELFTEST_LOCATION, &(CONTEXT), (EXPECTED_TEXT)); \
2075 SELFTEST_END_STMT
2077 /* Verify that ITEM has the expected values. */
2079 static void
2080 verify_item (const location &loc,
2081 const optinfo_item *item,
2082 enum optinfo_item_kind expected_kind,
2083 location_t expected_location,
2084 const char *expected_text)
2086 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
2087 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
2088 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
2091 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
2093 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
2094 SELFTEST_BEGIN_STMT \
2095 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
2096 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
2097 SELFTEST_END_STMT
2099 /* Verify that ITEM is a tree item, with the expected values. */
2101 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
2102 SELFTEST_BEGIN_STMT \
2103 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
2104 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
2105 SELFTEST_END_STMT
2107 /* Verify that ITEM is a gimple item, with the expected values. */
2109 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
2110 SELFTEST_BEGIN_STMT \
2111 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
2112 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
2113 SELFTEST_END_STMT
2115 /* Verify that calls to the dump_* API are captured and consolidated into
2116 optimization records. */
2118 static void
2119 test_capture_of_dump_calls (const line_table_case &case_)
2121 /* Generate a location_t for testing. */
2122 line_table_test ltt (case_);
2123 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
2124 linemap_line_start (line_table, 5, 100);
2125 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2126 location_t where = linemap_position_for_column (line_table, 10);
2127 if (where > LINE_MAP_MAX_LOCATION_WITH_COLS)
2128 return;
2130 dump_location_t loc = dump_location_t::from_location_t (where);
2132 gimple *stmt = gimple_build_return (NULL);
2133 gimple_set_location (stmt, where);
2135 tree test_decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
2136 get_identifier ("test_decl"),
2137 integer_type_node);
2138 /* Run all tests twice, with and then without optinfo enabled, to ensure
2139 that immediate destinations vs optinfo-based destinations both
2140 work, independently of each other, with no leaks. */
2141 for (int i = 0 ; i < 2; i++)
2143 bool with_optinfo = (i == 0);
2145 /* Test of dump_printf. */
2147 temp_dump_context tmp (with_optinfo,
2148 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2149 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
2151 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
2152 if (with_optinfo)
2154 optinfo *info = tmp.get_pending_optinfo ();
2155 ASSERT_TRUE (info != NULL);
2156 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2157 ASSERT_EQ (info->num_items (), 1);
2158 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
2162 /* Test of dump_printf with %T. */
2164 temp_dump_context tmp (with_optinfo,
2165 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2166 dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
2168 ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
2169 if (with_optinfo)
2171 optinfo *info = tmp.get_pending_optinfo ();
2172 ASSERT_TRUE (info != NULL);
2173 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2174 ASSERT_EQ (info->num_items (), 2);
2175 ASSERT_IS_TEXT (info->get_item (0), "tree: ");
2176 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2180 /* Test of dump_printf with %E. */
2182 temp_dump_context tmp (with_optinfo,
2183 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2184 dump_printf (MSG_NOTE, "gimple: %E", stmt);
2186 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
2187 if (with_optinfo)
2189 optinfo *info = tmp.get_pending_optinfo ();
2190 ASSERT_TRUE (info != NULL);
2191 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2192 ASSERT_EQ (info->num_items (), 2);
2193 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2194 ASSERT_IS_GIMPLE (info->get_item (1), where, "return;");
2198 /* Test of dump_printf with %G. */
2200 temp_dump_context tmp (with_optinfo,
2201 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2202 dump_printf (MSG_NOTE, "gimple: %G", stmt);
2204 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
2205 if (with_optinfo)
2207 optinfo *info = tmp.get_pending_optinfo ();
2208 ASSERT_TRUE (info != NULL);
2209 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2210 ASSERT_EQ (info->num_items (), 2);
2211 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2212 ASSERT_IS_GIMPLE (info->get_item (1), where, "return;\n");
2216 /* dump_print_loc with multiple format codes. This tests various
2217 things:
2218 - intermingling of text, format codes handled by the base
2219 pretty_printer, and dump-specific format codes
2220 - multiple dump-specific format codes: some consecutive, others
2221 separated by text, trailing text after the final one. */
2223 temp_dump_context tmp (with_optinfo,
2224 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2225 dump_printf_loc (MSG_NOTE, loc, "before %T and %T"
2226 " %i consecutive %E%E after\n",
2227 integer_zero_node, test_decl, 42, stmt, stmt);
2229 ASSERT_DUMPED_TEXT_EQ (tmp,
2230 "test.txt:5:10: note: before 0 and test_decl"
2231 " 42 consecutive return;return; after\n");
2232 if (with_optinfo)
2234 optinfo *info = tmp.get_pending_optinfo ();
2235 ASSERT_TRUE (info != NULL);
2236 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2237 ASSERT_EQ (info->num_items (), 8);
2238 ASSERT_IS_TEXT (info->get_item (0), "before ");
2239 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2240 ASSERT_IS_TEXT (info->get_item (2), " and ");
2241 ASSERT_IS_TREE (info->get_item (3), UNKNOWN_LOCATION, "test_decl");
2242 ASSERT_IS_TEXT (info->get_item (4), " 42 consecutive ");
2243 ASSERT_IS_GIMPLE (info->get_item (5), where, "return;");
2244 ASSERT_IS_GIMPLE (info->get_item (6), where, "return;");
2245 ASSERT_IS_TEXT (info->get_item (7), " after\n");
2249 /* Tree, via dump_generic_expr. */
2251 temp_dump_context tmp (with_optinfo,
2252 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2253 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
2254 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
2256 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
2257 if (with_optinfo)
2259 optinfo *info = tmp.get_pending_optinfo ();
2260 ASSERT_TRUE (info != NULL);
2261 ASSERT_EQ (info->get_location_t (), where);
2262 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2263 ASSERT_EQ (info->num_items (), 2);
2264 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
2265 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2269 /* Tree, via dump_generic_expr_loc. */
2271 temp_dump_context tmp (with_optinfo,
2272 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2273 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
2275 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
2276 if (with_optinfo)
2278 optinfo *info = tmp.get_pending_optinfo ();
2279 ASSERT_TRUE (info != NULL);
2280 ASSERT_EQ (info->get_location_t (), where);
2281 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2282 ASSERT_EQ (info->num_items (), 1);
2283 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
2287 /* Gimple. */
2289 /* dump_gimple_stmt_loc. */
2291 temp_dump_context tmp (with_optinfo,
2292 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2293 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2295 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
2296 if (with_optinfo)
2298 optinfo *info = tmp.get_pending_optinfo ();
2299 ASSERT_TRUE (info != NULL);
2300 ASSERT_EQ (info->num_items (), 1);
2301 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
2305 /* dump_gimple_stmt. */
2307 temp_dump_context tmp (with_optinfo,
2308 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2309 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
2311 ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
2312 if (with_optinfo)
2314 optinfo *info = tmp.get_pending_optinfo ();
2315 ASSERT_TRUE (info != NULL);
2316 ASSERT_EQ (info->num_items (), 1);
2317 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
2321 /* dump_gimple_expr_loc. */
2323 temp_dump_context tmp (with_optinfo,
2324 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2325 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2327 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
2328 if (with_optinfo)
2330 optinfo *info = tmp.get_pending_optinfo ();
2331 ASSERT_TRUE (info != NULL);
2332 ASSERT_EQ (info->num_items (), 1);
2333 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
2337 /* dump_gimple_expr. */
2339 temp_dump_context tmp (with_optinfo,
2340 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2341 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
2343 ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
2344 if (with_optinfo)
2346 optinfo *info = tmp.get_pending_optinfo ();
2347 ASSERT_TRUE (info != NULL);
2348 ASSERT_EQ (info->num_items (), 1);
2349 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
2354 /* poly_int. */
2356 temp_dump_context tmp (with_optinfo,
2357 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2358 dump_dec (MSG_NOTE, poly_int64 (42));
2360 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
2361 if (with_optinfo)
2363 optinfo *info = tmp.get_pending_optinfo ();
2364 ASSERT_TRUE (info != NULL);
2365 ASSERT_EQ (info->num_items (), 1);
2366 ASSERT_IS_TEXT (info->get_item (0), "42");
2370 /* Scopes. Test with all 4 combinations of
2371 filtering by MSG_PRIORITY_USER_FACING
2372 and/or filtering by MSG_PRIORITY_INTERNALS. */
2373 for (int j = 0; j < 3; j++)
2375 dump_flags_t dump_filter = MSG_ALL_KINDS;
2376 if (j % 2)
2377 dump_filter |= MSG_PRIORITY_USER_FACING;
2378 if (j / 2)
2379 dump_filter |= MSG_PRIORITY_INTERNALS;
2381 temp_dump_context tmp (with_optinfo, dump_filter);
2382 /* Emit various messages, mostly with implicit priority. */
2383 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2384 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, stmt,
2385 "explicitly internal msg\n");
2387 AUTO_DUMP_SCOPE ("outer scope", stmt);
2388 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2390 AUTO_DUMP_SCOPE ("middle scope", stmt);
2391 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2393 AUTO_DUMP_SCOPE ("inner scope", stmt);
2394 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
2395 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_USER_FACING, stmt,
2396 "explicitly user-facing msg\n");
2398 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2400 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2402 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2404 switch (dump_filter & MSG_ALL_PRIORITIES)
2406 default:
2407 gcc_unreachable ();
2408 case 0:
2409 ASSERT_DUMPED_TEXT_EQ (tmp, "");
2410 break;
2411 case MSG_PRIORITY_USER_FACING:
2412 ASSERT_DUMPED_TEXT_EQ
2413 (tmp,
2414 "test.txt:5:10: note: msg 1\n"
2415 "test.txt:5:10: note: explicitly user-facing msg\n"
2416 "test.txt:5:10: note: msg 7\n");
2417 break;
2418 case MSG_PRIORITY_INTERNALS:
2419 ASSERT_DUMPED_TEXT_EQ
2420 (tmp,
2421 "test.txt:5:10: note: explicitly internal msg\n"
2422 "test.txt:5:10: note: === outer scope ===\n"
2423 "test.txt:5:10: note: msg 2\n"
2424 "test.txt:5:10: note: === middle scope ===\n"
2425 "test.txt:5:10: note: msg 3\n"
2426 "test.txt:5:10: note: === inner scope ===\n"
2427 "test.txt:5:10: note: msg 4\n"
2428 "test.txt:5:10: note: msg 5\n"
2429 "test.txt:5:10: note: msg 6\n");
2430 break;
2431 case MSG_ALL_PRIORITIES:
2432 ASSERT_DUMPED_TEXT_EQ
2433 (tmp,
2434 "test.txt:5:10: note: msg 1\n"
2435 "test.txt:5:10: note: explicitly internal msg\n"
2436 "test.txt:5:10: note: === outer scope ===\n"
2437 "test.txt:5:10: note: msg 2\n"
2438 "test.txt:5:10: note: === middle scope ===\n"
2439 "test.txt:5:10: note: msg 3\n"
2440 "test.txt:5:10: note: === inner scope ===\n"
2441 "test.txt:5:10: note: msg 4\n"
2442 "test.txt:5:10: note: explicitly user-facing msg\n"
2443 "test.txt:5:10: note: msg 5\n"
2444 "test.txt:5:10: note: msg 6\n"
2445 "test.txt:5:10: note: msg 7\n");
2446 break;
2448 if (with_optinfo)
2450 optinfo *info = tmp.get_pending_optinfo ();
2451 ASSERT_TRUE (info != NULL);
2452 ASSERT_EQ (info->num_items (), 1);
2453 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
2458 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2459 above. */
2461 /* MSG_OPTIMIZED_LOCATIONS. */
2463 temp_dump_context tmp (true, MSG_ALL_KINDS);
2464 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
2465 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2466 OPTINFO_KIND_SUCCESS);
2469 /* MSG_MISSED_OPTIMIZATION. */
2471 temp_dump_context tmp (true, MSG_ALL_KINDS);
2472 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2473 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2474 OPTINFO_KIND_FAILURE);
2478 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2480 temp_dump_context tmp (false,
2481 MSG_OPTIMIZED_LOCATIONS | MSG_ALL_PRIORITIES);
2482 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2484 AUTO_DUMP_SCOPE ("outer scope", stmt);
2485 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2487 AUTO_DUMP_SCOPE ("middle scope", stmt);
2488 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2490 AUTO_DUMP_SCOPE ("inner scope", stmt);
2491 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt, "msg 4\n");
2493 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2495 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2497 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2499 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: optimized: msg 4\n");
2503 /* Run all of the selftests within this file. */
2505 void
2506 dumpfile_c_tests ()
2508 test_impl_location ();
2509 for_each_line_table_case (test_capture_of_dump_calls);
2512 } // namespace selftest
2514 #endif /* CHECKING_P */