2018-11-11 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dumpfile.c
blob09c24905f52a76afee56d50dc63a1e8c22ae51c4
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 dump_loc_immediate (dump_kind, loc);
567 if (optinfo_enabled_p ())
569 optinfo &info = begin_next_optinfo (loc);
570 info.handle_dump_file_kind (dump_kind);
574 /* As dump_loc above, but without starting a new optinfo. */
576 void
577 dump_context::dump_loc_immediate (dump_flags_t dump_kind,
578 const dump_location_t &loc)
580 location_t srcloc = loc.get_location_t ();
582 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
583 ::dump_loc (dump_kind, dump_file, srcloc);
585 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
586 ::dump_loc (dump_kind, alt_dump_file, srcloc);
588 /* Support for temp_dump_context in selftests. */
589 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
590 ::dump_loc (dump_kind, m_test_pp, srcloc);
593 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
595 static optinfo_item *
596 make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
598 pretty_printer pp;
599 pp_needs_newline (&pp) = true;
600 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
601 pp_newline (&pp);
603 optinfo_item *item
604 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
605 xstrdup (pp_formatted_text (&pp)));
606 return item;
609 /* Dump gimple statement GS with SPC indentation spaces and
610 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
612 void
613 dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
614 dump_flags_t extra_dump_flags,
615 gimple *gs, int spc)
617 optinfo_item *item
618 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
619 emit_item (item, dump_kind);
621 if (optinfo_enabled_p ())
623 optinfo &info = ensure_pending_optinfo ();
624 info.handle_dump_file_kind (dump_kind);
625 info.add_item (item);
627 else
628 delete item;
631 /* Similar to dump_gimple_stmt, except additionally print source location. */
633 void
634 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind,
635 const dump_location_t &loc,
636 dump_flags_t extra_dump_flags,
637 gimple *gs, int spc)
639 dump_loc (dump_kind, loc);
640 dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
643 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
645 static optinfo_item *
646 make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
648 dump_flags |= TDF_RHS_ONLY;
649 pretty_printer pp;
650 pp_needs_newline (&pp) = true;
651 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
653 optinfo_item *item
654 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
655 xstrdup (pp_formatted_text (&pp)));
656 return item;
659 /* Dump gimple statement GS with SPC indentation spaces and
660 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
661 Do not terminate with a newline or semicolon. */
663 void
664 dump_context::dump_gimple_expr (dump_flags_t dump_kind,
665 dump_flags_t extra_dump_flags,
666 gimple *gs, int spc)
668 optinfo_item *item
669 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
670 emit_item (item, dump_kind);
672 if (optinfo_enabled_p ())
674 optinfo &info = ensure_pending_optinfo ();
675 info.handle_dump_file_kind (dump_kind);
676 info.add_item (item);
678 else
679 delete item;
682 /* Similar to dump_gimple_expr, except additionally print source location. */
684 void
685 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind,
686 const dump_location_t &loc,
687 dump_flags_t extra_dump_flags,
688 gimple *gs,
689 int spc)
691 dump_loc (dump_kind, loc);
692 dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
695 /* Make an item for the given dump call, equivalent to print_generic_expr. */
697 static optinfo_item *
698 make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
700 pretty_printer pp;
701 pp_needs_newline (&pp) = true;
702 pp_translate_identifiers (&pp) = false;
703 dump_generic_node (&pp, node, 0, dump_flags, false);
705 location_t loc = UNKNOWN_LOCATION;
706 if (EXPR_HAS_LOCATION (node))
707 loc = EXPR_LOCATION (node);
709 optinfo_item *item
710 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
711 xstrdup (pp_formatted_text (&pp)));
712 return item;
715 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
716 DUMP_KIND is enabled. */
718 void
719 dump_context::dump_generic_expr (dump_flags_t dump_kind,
720 dump_flags_t extra_dump_flags,
721 tree t)
723 optinfo_item *item
724 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
725 emit_item (item, dump_kind);
727 if (optinfo_enabled_p ())
729 optinfo &info = ensure_pending_optinfo ();
730 info.handle_dump_file_kind (dump_kind);
731 info.add_item (item);
733 else
734 delete item;
738 /* Similar to dump_generic_expr, except additionally print the source
739 location. */
741 void
742 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind,
743 const dump_location_t &loc,
744 dump_flags_t extra_dump_flags,
745 tree t)
747 dump_loc (dump_kind, loc);
748 dump_generic_expr (dump_kind, extra_dump_flags, t);
751 /* Make an item for the given dump call. */
753 static optinfo_item *
754 make_item_for_dump_symtab_node (symtab_node *node)
756 location_t loc = DECL_SOURCE_LOCATION (node->decl);
757 optinfo_item *item
758 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
759 xstrdup (node->dump_name ()));
760 return item;
763 /* dump_pretty_printer's ctor. */
765 dump_pretty_printer::dump_pretty_printer (dump_context *context,
766 dump_flags_t dump_kind)
767 : pretty_printer (), m_context (context), m_dump_kind (dump_kind),
768 m_stashed_items ()
770 pp_format_decoder (this) = format_decoder_cb;
773 /* Phase 3 of formatting; compare with pp_output_formatted_text.
775 Emit optinfo_item instances for the various formatted chunks from phases
776 1 and 2 (i.e. pp_format).
778 Some chunks may already have had their items built (during decode_format).
779 These chunks have been stashed into m_stashed_items; we emit them here.
781 For all other purely textual chunks, they are printed into
782 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
783 This consolidates multiple adjacent text chunks into a single text
784 optinfo_item. */
786 void
787 dump_pretty_printer::emit_items (optinfo *dest)
789 output_buffer *buffer = pp_buffer (this);
790 struct chunk_info *chunk_array = buffer->cur_chunk_array;
791 const char **args = chunk_array->args;
793 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
794 gcc_assert (buffer->line_length == 0);
796 unsigned stashed_item_idx = 0;
797 for (unsigned chunk = 0; args[chunk]; chunk++)
799 if (stashed_item_idx < m_stashed_items.length ()
800 && args[chunk] == *m_stashed_items[stashed_item_idx].buffer_ptr)
802 emit_any_pending_textual_chunks (dest);
803 /* This chunk has a stashed item: use it. */
804 emit_item (m_stashed_items[stashed_item_idx++].item, dest);
806 else
807 /* This chunk is purely textual. Print it (to
808 buffer->formatted_obstack), so that we can consolidate adjacent
809 chunks into one textual optinfo_item. */
810 pp_string (this, args[chunk]);
813 emit_any_pending_textual_chunks (dest);
815 /* Ensure that we consumed all of stashed_items. */
816 gcc_assert (stashed_item_idx == m_stashed_items.length ());
818 /* Deallocate the chunk structure and everything after it (i.e. the
819 associated series of formatted strings). */
820 buffer->cur_chunk_array = chunk_array->prev;
821 obstack_free (&buffer->chunk_obstack, chunk_array);
824 /* Subroutine of dump_pretty_printer::emit_items
825 for consolidating multiple adjacent pure-text chunks into single
826 optinfo_items (in phase 3). */
828 void
829 dump_pretty_printer::emit_any_pending_textual_chunks (optinfo *dest)
831 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
833 /* Don't emit an item if the pending text is empty. */
834 if (output_buffer_last_position_in_text (buffer) == NULL)
835 return;
837 char *formatted_text = xstrdup (pp_formatted_text (this));
838 optinfo_item *item
839 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
840 formatted_text);
841 emit_item (item, dest);
843 /* Clear the pending text by unwinding formatted_text back to the start
844 of the buffer (without deallocating). */
845 obstack_free (&buffer->formatted_obstack,
846 buffer->formatted_obstack.object_base);
849 /* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
850 to DEST; otherwise delete ITEM. */
852 void
853 dump_pretty_printer::emit_item (optinfo_item *item, optinfo *dest)
855 m_context->emit_item (item, m_dump_kind);
856 if (dest)
857 dest->add_item (item);
858 else
859 delete item;
862 /* Record that ITEM (generated in phase 2 of formatting) is to be used for
863 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
865 void
866 dump_pretty_printer::stash_item (const char **buffer_ptr, optinfo_item *item)
868 gcc_assert (buffer_ptr);
869 gcc_assert (item);
871 m_stashed_items.safe_push (stashed_item (buffer_ptr, item));
874 /* pp_format_decoder callback for dump_pretty_printer, and thus for
875 dump_printf and dump_printf_loc.
877 A wrapper around decode_format, for type-safety. */
879 bool
880 dump_pretty_printer::format_decoder_cb (pretty_printer *pp, text_info *text,
881 const char *spec, int /*precision*/,
882 bool /*wide*/, bool /*set_locus*/,
883 bool /*verbose*/, bool */*quoted*/,
884 const char **buffer_ptr)
886 dump_pretty_printer *opp = static_cast <dump_pretty_printer *> (pp);
887 return opp->decode_format (text, spec, buffer_ptr);
890 /* Format decoder for dump_pretty_printer, and thus for dump_printf and
891 dump_printf_loc.
893 Supported format codes (in addition to the standard pretty_printer ones)
894 are:
896 %C: cgraph_node *:
897 Equivalent to: dump_symtab_node (MSG_*, node)
898 %E: gimple *:
899 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
900 %G: gimple *:
901 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
902 %T: tree:
903 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
905 TODO: add a format code that can handle (symtab_node*) *and* both
906 subclasses (presumably means teaching -Wformat about non-virtual
907 subclasses).
909 These format codes build optinfo_item instances, thus capturing metadata
910 about the arguments being dumped, as well as the textual output. */
912 bool
913 dump_pretty_printer::decode_format (text_info *text, const char *spec,
914 const char **buffer_ptr)
916 /* Various format codes that imply making an optinfo_item and stashed it
917 for later use (to capture metadata, rather than plain text). */
918 switch (*spec)
920 case 'C':
922 cgraph_node *node = va_arg (*text->args_ptr, cgraph_node *);
924 /* Make an item for the node, and stash it. */
925 optinfo_item *item = make_item_for_dump_symtab_node (node);
926 stash_item (buffer_ptr, item);
927 return true;
930 case 'E':
932 gimple *stmt = va_arg (*text->args_ptr, gimple *);
934 /* Make an item for the stmt, and stash it. */
935 optinfo_item *item = make_item_for_dump_gimple_expr (stmt, 0, TDF_SLIM);
936 stash_item (buffer_ptr, item);
937 return true;
940 case 'G':
942 gimple *stmt = va_arg (*text->args_ptr, gimple *);
944 /* Make an item for the stmt, and stash it. */
945 optinfo_item *item = make_item_for_dump_gimple_stmt (stmt, 0, TDF_SLIM);
946 stash_item (buffer_ptr, item);
947 return true;
950 case 'T':
952 tree t = va_arg (*text->args_ptr, tree);
954 /* Make an item for the tree, and stash it. */
955 optinfo_item *item = make_item_for_dump_generic_expr (t, TDF_SLIM);
956 stash_item (buffer_ptr, item);
957 return true;
960 default:
961 return false;
965 /* Output a formatted message using FORMAT on appropriate dump streams. */
967 void
968 dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
969 va_list *ap)
971 dump_pretty_printer pp (this, dump_kind);
973 text_info text;
974 text.err_no = errno;
975 text.args_ptr = ap;
976 text.format_spec = format;
978 /* Phases 1 and 2, using pp_format. */
979 pp_format (&pp, &text);
981 /* Phase 3. */
982 if (optinfo_enabled_p ())
984 optinfo &info = ensure_pending_optinfo ();
985 info.handle_dump_file_kind (dump_kind);
986 pp.emit_items (&info);
988 else
989 pp.emit_items (NULL);
992 /* Similar to dump_printf, except source location is also printed, and
993 dump location captured. */
995 void
996 dump_context::dump_printf_loc_va (dump_flags_t dump_kind,
997 const dump_location_t &loc,
998 const char *format, va_list *ap)
1000 dump_loc (dump_kind, loc);
1001 dump_printf_va (dump_kind, format, ap);
1004 /* Make an item for the given dump call, equivalent to print_dec. */
1006 template<unsigned int N, typename C>
1007 static optinfo_item *
1008 make_item_for_dump_dec (const poly_int<N, C> &value)
1010 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
1011 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
1013 pretty_printer pp;
1015 if (value.is_constant ())
1016 pp_wide_int (&pp, value.coeffs[0], sgn);
1017 else
1019 pp_character (&pp, '[');
1020 for (unsigned int i = 0; i < N; ++i)
1022 pp_wide_int (&pp, value.coeffs[i], sgn);
1023 pp_character (&pp, i == N - 1 ? ']' : ',');
1027 optinfo_item *item
1028 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1029 xstrdup (pp_formatted_text (&pp)));
1030 return item;
1033 /* Output VALUE in decimal to appropriate dump streams. */
1035 template<unsigned int N, typename C>
1036 void
1037 dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
1039 optinfo_item *item = make_item_for_dump_dec (value);
1040 emit_item (item, dump_kind);
1042 if (optinfo_enabled_p ())
1044 optinfo &info = ensure_pending_optinfo ();
1045 info.handle_dump_file_kind (dump_kind);
1046 info.add_item (item);
1048 else
1049 delete item;
1052 /* Output the name of NODE on appropriate dump streams. */
1054 void
1055 dump_context::dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
1057 optinfo_item *item = make_item_for_dump_symtab_node (node);
1058 emit_item (item, dump_kind);
1060 if (optinfo_enabled_p ())
1062 optinfo &info = ensure_pending_optinfo ();
1063 info.handle_dump_file_kind (dump_kind);
1064 info.add_item (item);
1066 else
1067 delete item;
1070 /* Get the current dump scope-nesting depth.
1071 For use by -fopt-info (for showing nesting via indentation). */
1073 unsigned int
1074 dump_context::get_scope_depth () const
1076 return m_scope_depth;
1079 /* Push a nested dump scope.
1080 Increment the scope depth.
1081 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1082 destination, if any.
1083 Emit a "scope" optinfo if optinfos are enabled. */
1085 void
1086 dump_context::begin_scope (const char *name, const dump_location_t &loc)
1088 m_scope_depth++;
1090 if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
1091 ::dump_loc (MSG_NOTE, dump_file, loc.get_location_t ());
1093 if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
1094 ::dump_loc (MSG_NOTE, alt_dump_file, loc.get_location_t ());
1096 /* Support for temp_dump_context in selftests. */
1097 if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
1098 ::dump_loc (MSG_NOTE, m_test_pp, loc.get_location_t ());
1100 pretty_printer pp;
1101 pp_printf (&pp, "=== %s ===\n", name);
1102 optinfo_item *item
1103 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1104 xstrdup (pp_formatted_text (&pp)));
1105 emit_item (item, MSG_NOTE);
1107 if (optinfo_enabled_p ())
1109 optinfo &info = begin_next_optinfo (loc);
1110 info.m_kind = OPTINFO_KIND_SCOPE;
1111 info.add_item (item);
1113 else
1114 delete item;
1117 /* Pop a nested dump scope. */
1119 void
1120 dump_context::end_scope ()
1122 end_any_optinfo ();
1123 m_scope_depth--;
1124 optimization_records_maybe_pop_dump_scope ();
1127 /* Return the optinfo currently being accumulated, creating one if
1128 necessary. */
1130 optinfo &
1131 dump_context::ensure_pending_optinfo ()
1133 if (!m_pending)
1134 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
1135 return *m_pending;
1138 /* Start a new optinfo and return it, ending any optinfo that was already
1139 accumulated. */
1141 optinfo &
1142 dump_context::begin_next_optinfo (const dump_location_t &loc)
1144 end_any_optinfo ();
1145 gcc_assert (m_pending == NULL);
1146 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
1147 return *m_pending;
1150 /* End any optinfo that has been accumulated within this context; emitting
1151 it to any destinations as appropriate, such as optimization records. */
1153 void
1154 dump_context::end_any_optinfo ()
1156 if (m_pending)
1157 m_pending->emit ();
1158 delete m_pending;
1159 m_pending = NULL;
1162 /* Emit ITEM to all item destinations (those that don't require
1163 consolidation into optinfo instances). */
1165 void
1166 dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
1168 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
1169 fprintf (dump_file, "%s", item->get_text ());
1171 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
1172 fprintf (alt_dump_file, "%s", item->get_text ());
1174 /* Support for temp_dump_context in selftests. */
1175 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
1176 pp_string (m_test_pp, item->get_text ());
1179 /* The current singleton dump_context, and its default. */
1181 dump_context *dump_context::s_current = &dump_context::s_default;
1182 dump_context dump_context::s_default;
1184 /* Implementation of dump_* API calls, calling into dump_context
1185 member functions. */
1187 /* Dump gimple statement GS with SPC indentation spaces and
1188 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1190 void
1191 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1192 gimple *gs, int spc)
1194 dump_context::get ().dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
1197 /* Similar to dump_gimple_stmt, except additionally print source location. */
1199 void
1200 dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1201 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1203 dump_context::get ().dump_gimple_stmt_loc (dump_kind, loc, extra_dump_flags,
1204 gs, spc);
1207 /* Dump gimple statement GS with SPC indentation spaces and
1208 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1209 Do not terminate with a newline or semicolon. */
1211 void
1212 dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1213 gimple *gs, int spc)
1215 dump_context::get ().dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
1218 /* Similar to dump_gimple_expr, except additionally print source location. */
1220 void
1221 dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1222 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1224 dump_context::get ().dump_gimple_expr_loc (dump_kind, loc, extra_dump_flags,
1225 gs, spc);
1228 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1229 DUMP_KIND is enabled. */
1231 void
1232 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
1233 tree t)
1235 dump_context::get ().dump_generic_expr (dump_kind, extra_dump_flags, t);
1238 /* Similar to dump_generic_expr, except additionally print the source
1239 location. */
1241 void
1242 dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1243 dump_flags_t extra_dump_flags, tree t)
1245 dump_context::get ().dump_generic_expr_loc (dump_kind, loc, extra_dump_flags,
1249 /* Output a formatted message using FORMAT on appropriate dump streams. */
1251 void
1252 dump_printf (dump_flags_t dump_kind, const char *format, ...)
1254 va_list ap;
1255 va_start (ap, format);
1256 dump_context::get ().dump_printf_va (dump_kind, format, &ap);
1257 va_end (ap);
1260 /* Similar to dump_printf, except source location is also printed, and
1261 dump location captured. */
1263 void
1264 dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1265 const char *format, ...)
1267 va_list ap;
1268 va_start (ap, format);
1269 dump_context::get ().dump_printf_loc_va (dump_kind, loc, format, &ap);
1270 va_end (ap);
1273 /* Output VALUE in decimal to appropriate dump streams. */
1275 template<unsigned int N, typename C>
1276 void
1277 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
1279 dump_context::get ().dump_dec (dump_kind, value);
1282 template void dump_dec (dump_flags_t, const poly_uint16 &);
1283 template void dump_dec (dump_flags_t, const poly_int64 &);
1284 template void dump_dec (dump_flags_t, const poly_uint64 &);
1285 template void dump_dec (dump_flags_t, const poly_offset_int &);
1286 template void dump_dec (dump_flags_t, const poly_widest_int &);
1288 void
1289 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1291 if (dump_file
1292 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1293 print_dec (value, dump_file, sgn);
1295 if (alt_dump_file
1296 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1297 print_dec (value, alt_dump_file, sgn);
1300 /* Output VALUE in hexadecimal to appropriate dump streams. */
1302 void
1303 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1305 if (dump_file
1306 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1307 print_hex (value, dump_file);
1309 if (alt_dump_file
1310 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1311 print_hex (value, alt_dump_file);
1314 /* Emit and delete the currently pending optinfo, if there is one,
1315 without the caller needing to know about class dump_context. */
1317 void
1318 dumpfile_ensure_any_optinfo_are_flushed ()
1320 dump_context::get().end_any_optinfo ();
1323 /* Output the name of NODE on appropriate dump streams. */
1325 void
1326 dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
1328 dump_context::get ().dump_symtab_node (dump_kind, node);
1331 /* Get the current dump scope-nesting depth.
1332 For use by -fopt-info (for showing nesting via indentation). */
1334 unsigned int
1335 get_dump_scope_depth ()
1337 return dump_context::get ().get_scope_depth ();
1340 /* Push a nested dump scope.
1341 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1342 destination, if any.
1343 Emit a "scope" opinfo if optinfos are enabled.
1344 Increment the scope depth. */
1346 void
1347 dump_begin_scope (const char *name, const dump_location_t &loc)
1349 dump_context::get ().begin_scope (name, loc);
1352 /* Pop a nested dump scope. */
1354 void
1355 dump_end_scope ()
1357 dump_context::get ().end_scope ();
1360 /* Start a dump for PHASE. Store user-supplied dump flags in
1361 *FLAG_PTR. Return the number of streams opened. Set globals
1362 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1363 set dump_flags appropriately for both pass dump stream and
1364 -fopt-info stream. */
1367 gcc::dump_manager::
1368 dump_start (int phase, dump_flags_t *flag_ptr)
1370 int count = 0;
1371 char *name;
1372 struct dump_file_info *dfi;
1373 FILE *stream;
1374 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1375 return 0;
1377 dfi = get_dump_file_info (phase);
1378 name = get_dump_file_name (phase);
1379 if (name)
1381 stream = dump_open (name, dfi->pstate < 0);
1382 if (stream)
1384 dfi->pstate = 1;
1385 count++;
1387 free (name);
1388 dfi->pstream = stream;
1389 set_dump_file (dfi->pstream);
1390 /* Initialize current dump flags. */
1391 pflags = dfi->pflags;
1394 stream = dump_open_alternate_stream (dfi);
1395 if (stream)
1397 dfi->alt_stream = stream;
1398 count++;
1399 set_alt_dump_file (dfi->alt_stream);
1400 /* Initialize current -fopt-info flags. */
1401 alt_flags = dfi->alt_flags;
1404 if (flag_ptr)
1405 *flag_ptr = dfi->pflags;
1407 return count;
1410 /* Finish a tree dump for PHASE and close associated dump streams. Also
1411 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1413 void
1414 gcc::dump_manager::
1415 dump_finish (int phase)
1417 struct dump_file_info *dfi;
1419 if (phase < 0)
1420 return;
1421 dfi = get_dump_file_info (phase);
1422 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1423 fclose (dfi->pstream);
1425 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1426 fclose (dfi->alt_stream);
1428 dfi->alt_stream = NULL;
1429 dfi->pstream = NULL;
1430 set_dump_file (NULL);
1431 set_alt_dump_file (NULL);
1432 dump_flags = TDF_NONE;
1433 alt_flags = TDF_NONE;
1434 pflags = TDF_NONE;
1437 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1438 *FLAG_PTR and returns a stream to write to. If the dump is not
1439 enabled, returns NULL.
1440 PART can be used for dump files which should be split to multiple
1441 parts. PART == -1 indicates dump file with no parts.
1442 If PART is -1, multiple calls will reopen and append to the dump file. */
1444 FILE *
1445 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1447 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1450 FILE *
1451 gcc::dump_manager::
1452 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1454 char *name;
1455 struct dump_file_info *dfi;
1456 FILE *stream;
1458 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1459 return NULL;
1461 name = get_dump_file_name (phase, part);
1462 if (!name)
1463 return NULL;
1464 dfi = get_dump_file_info (phase);
1466 /* We do not support re-opening of dump files with parts. This would require
1467 tracking pstate per part of the dump file. */
1468 stream = dump_open (name, part != -1 || dfi->pstate < 0);
1469 if (stream)
1470 dfi->pstate = 1;
1471 free (name);
1473 if (flag_ptr)
1474 *flag_ptr = dfi->pflags;
1476 /* Initialize current flags */
1477 pflags = dfi->pflags;
1478 return stream;
1481 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1482 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1483 any phase. */
1486 gcc::dump_manager::
1487 dump_phase_enabled_p (int phase) const
1489 if (phase == TDI_tree_all)
1491 size_t i;
1492 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1493 if (dump_files[i].pstate || dump_files[i].alt_state)
1494 return 1;
1495 for (i = 0; i < m_extra_dump_files_in_use; i++)
1496 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1497 return 1;
1498 return 0;
1500 else
1502 struct dump_file_info *dfi = get_dump_file_info (phase);
1503 return dfi->pstate || dfi->alt_state;
1507 /* Returns nonzero if tree dump PHASE has been initialized. */
1510 gcc::dump_manager::
1511 dump_initialized_p (int phase) const
1513 struct dump_file_info *dfi = get_dump_file_info (phase);
1514 return dfi->pstate > 0 || dfi->alt_state > 0;
1517 /* Returns the switch name of PHASE. */
1519 const char *
1520 dump_flag_name (int phase)
1522 return g->get_dumps ()->dump_flag_name (phase);
1525 const char *
1526 gcc::dump_manager::
1527 dump_flag_name (int phase) const
1529 struct dump_file_info *dfi = get_dump_file_info (phase);
1530 return dfi->swtch;
1533 /* Handle -fdump-* and -fopt-info for a pass added after
1534 command-line options are parsed (those from plugins and
1535 those from backends).
1537 Because the registration of plugin/backend passes happens after the
1538 command-line options are parsed, the options that specify single
1539 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1540 passes. Therefore we currently can only enable dumping of
1541 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1542 are specified. This is done here.
1544 Similarly, the saved -fopt-info options are wired up to the new pass. */
1546 void
1547 gcc::dump_manager::register_pass (opt_pass *pass)
1549 gcc_assert (pass);
1551 register_one_dump_file (pass);
1553 dump_file_info *pass_dfi = get_dump_file_info (pass->static_pass_number);
1554 gcc_assert (pass_dfi);
1556 enum tree_dump_index tdi;
1557 if (pass->type == SIMPLE_IPA_PASS
1558 || pass->type == IPA_PASS)
1559 tdi = TDI_ipa_all;
1560 else if (pass->type == GIMPLE_PASS)
1561 tdi = TDI_tree_all;
1562 else
1563 tdi = TDI_rtl_all;
1564 const dump_file_info *tdi_dfi = get_dump_file_info (tdi);
1565 gcc_assert (tdi_dfi);
1567 /* Check if dump-all flag is specified. */
1568 if (tdi_dfi->pstate)
1570 pass_dfi->pstate = tdi_dfi->pstate;
1571 pass_dfi->pflags = tdi_dfi->pflags;
1574 update_dfi_for_opt_info (pass_dfi);
1577 /* Finish a tree dump for PHASE. STREAM is the stream created by
1578 dump_begin. */
1580 void
1581 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1583 if (stream != stderr && stream != stdout)
1584 fclose (stream);
1587 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1588 enabled tree dumps. */
1591 gcc::dump_manager::
1592 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1594 int n = 0;
1595 size_t i;
1597 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1599 if (dump_files[i].dkind == dkind)
1601 const char *old_filename = dump_files[i].pfilename;
1602 dump_files[i].pstate = -1;
1603 dump_files[i].pflags |= flags;
1604 n++;
1605 /* Override the existing filename. */
1606 if (filename)
1608 dump_files[i].pfilename = xstrdup (filename);
1609 /* Since it is a command-line provided file, which is
1610 common to all the phases, use it in append mode. */
1611 dump_files[i].pstate = 1;
1613 if (old_filename && filename != old_filename)
1614 free (CONST_CAST (char *, old_filename));
1618 for (i = 0; i < m_extra_dump_files_in_use; i++)
1620 if (m_extra_dump_files[i].dkind == dkind)
1622 const char *old_filename = m_extra_dump_files[i].pfilename;
1623 m_extra_dump_files[i].pstate = -1;
1624 m_extra_dump_files[i].pflags |= flags;
1625 n++;
1626 /* Override the existing filename. */
1627 if (filename)
1629 m_extra_dump_files[i].pfilename = xstrdup (filename);
1630 /* Since it is a command-line provided file, which is
1631 common to all the phases, use it in append mode. */
1632 m_extra_dump_files[i].pstate = 1;
1634 if (old_filename && filename != old_filename)
1635 free (CONST_CAST (char *, old_filename));
1639 return n;
1642 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1643 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1644 dumps. */
1647 gcc::dump_manager::
1648 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1649 const char *filename)
1651 int n = 0;
1653 m_optgroup_flags = optgroup_flags;
1654 m_optinfo_flags = flags;
1655 m_optinfo_filename = xstrdup (filename);
1657 for (size_t i = TDI_none + 1; i < (size_t) TDI_end; i++)
1658 if (update_dfi_for_opt_info (&dump_files[i]))
1659 n++;
1661 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1662 if (update_dfi_for_opt_info (&m_extra_dump_files[i]))
1663 n++;
1665 return n;
1668 /* Use the saved -fopt-info options to update DFI.
1669 Return true if the dump is enabled. */
1671 bool
1672 gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
1674 gcc_assert (dfi);
1676 if (!(dfi->optgroup_flags & m_optgroup_flags))
1677 return false;
1679 const char *old_filename = dfi->alt_filename;
1680 /* Since this file is shared among different passes, it
1681 should be opened in append mode. */
1682 dfi->alt_state = 1;
1683 dfi->alt_flags |= m_optinfo_flags;
1684 /* Override the existing filename. */
1685 if (m_optinfo_filename)
1686 dfi->alt_filename = xstrdup (m_optinfo_filename);
1687 if (old_filename && m_optinfo_filename != old_filename)
1688 free (CONST_CAST (char *, old_filename));
1690 return true;
1693 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1694 relevant details in the dump_files array. */
1697 gcc::dump_manager::
1698 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1700 const char *option_value;
1701 const char *ptr;
1702 dump_flags_t flags;
1704 if (doglob && !dfi->glob)
1705 return 0;
1707 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1708 if (!option_value)
1709 return 0;
1711 if (*option_value && *option_value != '-' && *option_value != '=')
1712 return 0;
1714 ptr = option_value;
1716 /* Retain "user-facing" and "internals" messages, but filter out
1717 those from an opt_problem being re-emitted at the top level
1718 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1719 messing up scan-tree-dump-times" in DejaGnu tests. */
1720 flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_INTERNALS;
1722 while (*ptr)
1724 const struct kv_pair<dump_flags_t> *option_ptr;
1725 const char *end_ptr;
1726 const char *eq_ptr;
1727 unsigned length;
1729 while (*ptr == '-')
1730 ptr++;
1731 end_ptr = strchr (ptr, '-');
1732 eq_ptr = strchr (ptr, '=');
1734 if (eq_ptr && !end_ptr)
1735 end_ptr = eq_ptr;
1737 if (!end_ptr)
1738 end_ptr = ptr + strlen (ptr);
1739 length = end_ptr - ptr;
1741 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1742 if (strlen (option_ptr->name) == length
1743 && !memcmp (option_ptr->name, ptr, length))
1745 flags |= option_ptr->value;
1746 goto found;
1749 if (*ptr == '=')
1751 /* Interpret rest of the argument as a dump filename. This
1752 filename overrides other command line filenames. */
1753 if (dfi->pfilename)
1754 free (CONST_CAST (char *, dfi->pfilename));
1755 dfi->pfilename = xstrdup (ptr + 1);
1756 break;
1758 else
1759 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1760 length, ptr, dfi->swtch);
1761 found:;
1762 ptr = end_ptr;
1765 dfi->pstate = -1;
1766 dfi->pflags |= flags;
1768 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1769 known dumps. */
1770 if (dfi->suffix == NULL)
1771 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1773 return 1;
1777 gcc::dump_manager::
1778 dump_switch_p (const char *arg)
1780 size_t i;
1781 int any = 0;
1783 for (i = TDI_none + 1; i != TDI_end; i++)
1784 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1786 /* Don't glob if we got a hit already */
1787 if (!any)
1788 for (i = TDI_none + 1; i != TDI_end; i++)
1789 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1791 for (i = 0; i < m_extra_dump_files_in_use; i++)
1792 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1794 if (!any)
1795 for (i = 0; i < m_extra_dump_files_in_use; i++)
1796 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1799 return any;
1802 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1803 and filename. Return non-zero if it is a recognized switch. */
1805 static int
1806 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1807 optgroup_flags_t *optgroup_flags, char **filename)
1809 const char *option_value;
1810 const char *ptr;
1812 option_value = arg;
1813 ptr = option_value;
1815 *filename = NULL;
1817 /* Default to filtering out "internals" messages, and retaining
1818 "user-facing" messages, and those from an opt_problem being
1819 re-emitted at the top level. */
1820 *flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_REEMITTED;
1822 *optgroup_flags = OPTGROUP_NONE;
1824 if (!ptr)
1825 return 1; /* Handle '-fopt-info' without any additional options. */
1827 while (*ptr)
1829 const char *end_ptr;
1830 const char *eq_ptr;
1831 unsigned length;
1833 while (*ptr == '-')
1834 ptr++;
1835 end_ptr = strchr (ptr, '-');
1836 eq_ptr = strchr (ptr, '=');
1838 if (eq_ptr && !end_ptr)
1839 end_ptr = eq_ptr;
1841 if (!end_ptr)
1842 end_ptr = ptr + strlen (ptr);
1843 length = end_ptr - ptr;
1845 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1846 option_ptr->name; option_ptr++)
1847 if (strlen (option_ptr->name) == length
1848 && !memcmp (option_ptr->name, ptr, length))
1850 *flags |= option_ptr->value;
1851 goto found;
1854 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1855 option_ptr->name; option_ptr++)
1856 if (strlen (option_ptr->name) == length
1857 && !memcmp (option_ptr->name, ptr, length))
1859 *optgroup_flags |= option_ptr->value;
1860 goto found;
1863 if (*ptr == '=')
1865 /* Interpret rest of the argument as a dump filename. This
1866 filename overrides other command line filenames. */
1867 *filename = xstrdup (ptr + 1);
1868 break;
1870 else
1872 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1873 length, ptr, arg);
1874 return 0;
1876 found:;
1877 ptr = end_ptr;
1880 return 1;
1883 /* Return non-zero if ARG is a recognized switch for
1884 -fopt-info. Return zero otherwise. */
1887 opt_info_switch_p (const char *arg)
1889 dump_flags_t flags;
1890 optgroup_flags_t optgroup_flags;
1891 char *filename;
1892 static char *file_seen = NULL;
1893 gcc::dump_manager *dumps = g->get_dumps ();
1895 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1896 return 0;
1898 if (!filename)
1899 filename = xstrdup ("stderr");
1901 /* Bail out if a different filename has been specified. */
1902 if (file_seen && strcmp (file_seen, filename))
1904 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1905 arg);
1906 return 1;
1909 file_seen = xstrdup (filename);
1910 if (!(flags & MSG_ALL_KINDS))
1911 flags |= MSG_OPTIMIZED_LOCATIONS;
1912 if (!optgroup_flags)
1913 optgroup_flags = OPTGROUP_ALL;
1915 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1918 /* Print basic block on the dump streams. */
1920 void
1921 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1923 if (dump_file
1924 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1925 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1926 if (alt_dump_file
1927 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1928 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
1931 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1933 void
1934 dump_function (int phase, tree fn)
1936 FILE *stream;
1937 dump_flags_t flags;
1939 stream = dump_begin (phase, &flags);
1940 if (stream)
1942 dump_function_to_file (fn, stream, flags);
1943 dump_end (phase, stream);
1947 /* Print information from the combine pass on dump_file. */
1949 void
1950 print_combine_total_stats (void)
1952 if (dump_file)
1953 dump_combine_total_stats (dump_file);
1956 /* Enable RTL dump for all the RTL passes. */
1958 bool
1959 enable_rtl_dump_file (void)
1961 gcc::dump_manager *dumps = g->get_dumps ();
1962 int num_enabled =
1963 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
1964 NULL);
1965 return num_enabled > 0;
1968 #if CHECKING_P
1970 namespace selftest {
1972 /* temp_dump_context's ctor. Temporarily override the dump_context
1973 (to forcibly enable optinfo-generation). */
1975 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
1976 bool forcibly_enable_dumping,
1977 dump_flags_t test_pp_flags)
1978 : m_context (),
1979 m_saved (&dump_context ().get ())
1981 dump_context::s_current = &m_context;
1982 m_context.m_forcibly_enable_optinfo = forcibly_enable_optinfo;
1983 /* Conditionally enable the test dump, so that we can verify both the
1984 dump_enabled_p and the !dump_enabled_p cases in selftests. */
1985 if (forcibly_enable_dumping)
1987 m_context.m_test_pp = &m_pp;
1988 m_context.m_test_pp_flags = test_pp_flags;
1991 dump_context::get ().refresh_dumps_are_enabled ();
1994 /* temp_dump_context's dtor. Restore the saved dump_context. */
1996 temp_dump_context::~temp_dump_context ()
1998 dump_context::s_current = m_saved;
2000 dump_context::get ().refresh_dumps_are_enabled ();
2003 /* 0-terminate the text dumped so far, and return it. */
2005 const char *
2006 temp_dump_context::get_dumped_text ()
2008 return pp_formatted_text (&m_pp);
2011 /* Verify that the dump_location_t constructors capture the source location
2012 at which they were called (provided that the build compiler is sufficiently
2013 recent). */
2015 static void
2016 test_impl_location ()
2018 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2019 /* Default ctor. */
2021 dump_location_t loc;
2022 const int expected_line = __LINE__ - 1;
2023 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2024 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2027 /* Constructing from a gimple. */
2029 dump_location_t loc ((gimple *)NULL);
2030 const int expected_line = __LINE__ - 1;
2031 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2032 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2035 /* Constructing from an rtx_insn. */
2037 dump_location_t loc ((rtx_insn *)NULL);
2038 const int expected_line = __LINE__ - 1;
2039 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
2040 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
2042 #endif
2045 /* Verify that the text dumped so far in CONTEXT equals
2046 EXPECTED_TEXT, using LOC for the location of any failure.
2047 As a side-effect, the internal buffer is 0-terminated. */
2049 void
2050 verify_dumped_text (const location &loc,
2051 temp_dump_context *context,
2052 const char *expected_text)
2054 gcc_assert (context);
2055 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
2056 expected_text);
2059 /* Verify that ITEM has the expected values. */
2061 void
2062 verify_item (const location &loc,
2063 const optinfo_item *item,
2064 enum optinfo_item_kind expected_kind,
2065 location_t expected_location,
2066 const char *expected_text)
2068 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
2069 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
2070 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
2073 /* Verify that calls to the dump_* API are captured and consolidated into
2074 optimization records. */
2076 static void
2077 test_capture_of_dump_calls (const line_table_case &case_)
2079 /* Generate a location_t for testing. */
2080 line_table_test ltt (case_);
2081 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
2082 linemap_line_start (line_table, 5, 100);
2083 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2084 location_t decl_loc = linemap_position_for_column (line_table, 8);
2085 location_t stmt_loc = linemap_position_for_column (line_table, 10);
2086 if (stmt_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
2087 return;
2089 dump_location_t loc = dump_location_t::from_location_t (stmt_loc);
2091 gimple *stmt = gimple_build_return (NULL);
2092 gimple_set_location (stmt, stmt_loc);
2094 tree test_decl = build_decl (decl_loc, FUNCTION_DECL,
2095 get_identifier ("test_decl"),
2096 build_function_type_list (void_type_node,
2097 NULL_TREE));
2099 symbol_table_test tmp_symtab;
2101 cgraph_node *node = cgraph_node::get_create (test_decl);
2102 gcc_assert (node);
2104 /* Run all tests twice, with and then without optinfo enabled, to ensure
2105 that immediate destinations vs optinfo-based destinations both
2106 work, independently of each other, with no leaks. */
2107 for (int i = 0 ; i < 2; i++)
2109 bool with_optinfo = (i == 0);
2111 /* Test of dump_printf. */
2113 temp_dump_context tmp (with_optinfo, true,
2114 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2115 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
2117 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
2118 if (with_optinfo)
2120 optinfo *info = tmp.get_pending_optinfo ();
2121 ASSERT_TRUE (info != NULL);
2122 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2123 ASSERT_EQ (info->num_items (), 1);
2124 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
2128 /* Test of dump_printf with %T. */
2130 temp_dump_context tmp (with_optinfo, true,
2131 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2132 dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
2134 ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
2135 if (with_optinfo)
2137 optinfo *info = tmp.get_pending_optinfo ();
2138 ASSERT_TRUE (info != NULL);
2139 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2140 ASSERT_EQ (info->num_items (), 2);
2141 ASSERT_IS_TEXT (info->get_item (0), "tree: ");
2142 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2146 /* Test of dump_printf with %E. */
2148 temp_dump_context tmp (with_optinfo, true,
2149 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2150 dump_printf (MSG_NOTE, "gimple: %E", stmt);
2152 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
2153 if (with_optinfo)
2155 optinfo *info = tmp.get_pending_optinfo ();
2156 ASSERT_TRUE (info != NULL);
2157 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2158 ASSERT_EQ (info->num_items (), 2);
2159 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2160 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;");
2164 /* Test of dump_printf with %G. */
2166 temp_dump_context tmp (with_optinfo, true,
2167 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2168 dump_printf (MSG_NOTE, "gimple: %G", stmt);
2170 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
2171 if (with_optinfo)
2173 optinfo *info = tmp.get_pending_optinfo ();
2174 ASSERT_TRUE (info != NULL);
2175 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2176 ASSERT_EQ (info->num_items (), 2);
2177 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2178 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;\n");
2182 /* Test of dump_printf with %C. */
2184 temp_dump_context tmp (with_optinfo, true,
2185 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2186 dump_printf (MSG_NOTE, "node: %C", node);
2188 ASSERT_DUMPED_TEXT_EQ (tmp, "node: test_decl/0");
2189 if (with_optinfo)
2191 optinfo *info = tmp.get_pending_optinfo ();
2192 ASSERT_TRUE (info != NULL);
2193 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2194 ASSERT_EQ (info->num_items (), 2);
2195 ASSERT_IS_TEXT (info->get_item (0), "node: ");
2196 ASSERT_IS_SYMTAB_NODE (info->get_item (1), decl_loc, "test_decl/0");
2200 /* dump_print_loc with multiple format codes. This tests various
2201 things:
2202 - intermingling of text, format codes handled by the base
2203 pretty_printer, and dump-specific format codes
2204 - multiple dump-specific format codes: some consecutive, others
2205 separated by text, trailing text after the final one. */
2207 temp_dump_context tmp (with_optinfo, true,
2208 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2209 dump_printf_loc (MSG_NOTE, loc, "before %T and %T"
2210 " %i consecutive %E%E after\n",
2211 integer_zero_node, test_decl, 42, stmt, stmt);
2213 ASSERT_DUMPED_TEXT_EQ (tmp,
2214 "test.txt:5:10: note: before 0 and test_decl"
2215 " 42 consecutive return;return; after\n");
2216 if (with_optinfo)
2218 optinfo *info = tmp.get_pending_optinfo ();
2219 ASSERT_TRUE (info != NULL);
2220 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2221 ASSERT_EQ (info->num_items (), 8);
2222 ASSERT_IS_TEXT (info->get_item (0), "before ");
2223 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2224 ASSERT_IS_TEXT (info->get_item (2), " and ");
2225 ASSERT_IS_TREE (info->get_item (3), UNKNOWN_LOCATION, "test_decl");
2226 ASSERT_IS_TEXT (info->get_item (4), " 42 consecutive ");
2227 ASSERT_IS_GIMPLE (info->get_item (5), stmt_loc, "return;");
2228 ASSERT_IS_GIMPLE (info->get_item (6), stmt_loc, "return;");
2229 ASSERT_IS_TEXT (info->get_item (7), " after\n");
2233 /* Tree, via dump_generic_expr. */
2235 temp_dump_context tmp (with_optinfo, true,
2236 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2237 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
2238 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
2240 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
2241 if (with_optinfo)
2243 optinfo *info = tmp.get_pending_optinfo ();
2244 ASSERT_TRUE (info != NULL);
2245 ASSERT_EQ (info->get_location_t (), stmt_loc);
2246 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2247 ASSERT_EQ (info->num_items (), 2);
2248 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
2249 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2253 /* Tree, via dump_generic_expr_loc. */
2255 temp_dump_context tmp (with_optinfo, true,
2256 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2257 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
2259 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
2260 if (with_optinfo)
2262 optinfo *info = tmp.get_pending_optinfo ();
2263 ASSERT_TRUE (info != NULL);
2264 ASSERT_EQ (info->get_location_t (), stmt_loc);
2265 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2266 ASSERT_EQ (info->num_items (), 1);
2267 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
2271 /* Gimple. */
2273 /* dump_gimple_stmt_loc. */
2275 temp_dump_context tmp (with_optinfo, true,
2276 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2277 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2279 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
2280 if (with_optinfo)
2282 optinfo *info = tmp.get_pending_optinfo ();
2283 ASSERT_TRUE (info != NULL);
2284 ASSERT_EQ (info->num_items (), 1);
2285 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
2289 /* dump_gimple_stmt. */
2291 temp_dump_context tmp (with_optinfo, true,
2292 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2293 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
2295 ASSERT_DUMPED_TEXT_EQ (tmp, "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), stmt_loc, "return;\n");
2305 /* dump_gimple_expr_loc. */
2307 temp_dump_context tmp (with_optinfo, true,
2308 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2309 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2311 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
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), stmt_loc, "return;");
2321 /* dump_gimple_expr. */
2323 temp_dump_context tmp (with_optinfo, true,
2324 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2325 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
2327 ASSERT_DUMPED_TEXT_EQ (tmp, "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), stmt_loc, "return;");
2338 /* symtab_node. */
2340 temp_dump_context tmp (with_optinfo, true,
2341 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2342 dump_symtab_node (MSG_NOTE, node);
2344 ASSERT_DUMPED_TEXT_EQ (tmp, "test_decl/0");
2345 if (with_optinfo)
2347 optinfo *info = tmp.get_pending_optinfo ();
2348 ASSERT_TRUE (info != NULL);
2349 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2350 ASSERT_EQ (info->num_items (), 1);
2351 ASSERT_IS_SYMTAB_NODE (info->get_item (0), decl_loc, "test_decl/0");
2355 /* poly_int. */
2357 temp_dump_context tmp (with_optinfo, true,
2358 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2359 dump_dec (MSG_NOTE, poly_int64 (42));
2361 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
2362 if (with_optinfo)
2364 optinfo *info = tmp.get_pending_optinfo ();
2365 ASSERT_TRUE (info != NULL);
2366 ASSERT_EQ (info->num_items (), 1);
2367 ASSERT_IS_TEXT (info->get_item (0), "42");
2371 /* Scopes. Test with all 4 combinations of
2372 filtering by MSG_PRIORITY_USER_FACING
2373 and/or filtering by MSG_PRIORITY_INTERNALS. */
2374 for (int j = 0; j < 3; j++)
2376 dump_flags_t dump_filter = MSG_ALL_KINDS;
2377 if (j % 2)
2378 dump_filter |= MSG_PRIORITY_USER_FACING;
2379 if (j / 2)
2380 dump_filter |= MSG_PRIORITY_INTERNALS;
2382 temp_dump_context tmp (with_optinfo, true, dump_filter);
2383 /* Emit various messages, mostly with implicit priority. */
2384 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2385 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, stmt,
2386 "explicitly internal msg\n");
2388 AUTO_DUMP_SCOPE ("outer scope", stmt);
2389 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2391 AUTO_DUMP_SCOPE ("middle scope", stmt);
2392 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2394 AUTO_DUMP_SCOPE ("inner scope", stmt);
2395 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
2396 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_USER_FACING, stmt,
2397 "explicitly user-facing msg\n");
2399 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2401 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2403 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2405 switch (dump_filter & MSG_ALL_PRIORITIES)
2407 default:
2408 gcc_unreachable ();
2409 case 0:
2410 ASSERT_DUMPED_TEXT_EQ (tmp, "");
2411 break;
2412 case MSG_PRIORITY_USER_FACING:
2413 ASSERT_DUMPED_TEXT_EQ
2414 (tmp,
2415 "test.txt:5:10: note: msg 1\n"
2416 "test.txt:5:10: note: explicitly user-facing msg\n"
2417 "test.txt:5:10: note: msg 7\n");
2418 break;
2419 case MSG_PRIORITY_INTERNALS:
2420 ASSERT_DUMPED_TEXT_EQ
2421 (tmp,
2422 "test.txt:5:10: note: explicitly internal msg\n"
2423 "test.txt:5:10: note: === outer scope ===\n"
2424 "test.txt:5:10: note: msg 2\n"
2425 "test.txt:5:10: note: === middle scope ===\n"
2426 "test.txt:5:10: note: msg 3\n"
2427 "test.txt:5:10: note: === inner scope ===\n"
2428 "test.txt:5:10: note: msg 4\n"
2429 "test.txt:5:10: note: msg 5\n"
2430 "test.txt:5:10: note: msg 6\n");
2431 break;
2432 case MSG_ALL_PRIORITIES:
2433 ASSERT_DUMPED_TEXT_EQ
2434 (tmp,
2435 "test.txt:5:10: note: msg 1\n"
2436 "test.txt:5:10: note: explicitly internal msg\n"
2437 "test.txt:5:10: note: === outer scope ===\n"
2438 "test.txt:5:10: note: msg 2\n"
2439 "test.txt:5:10: note: === middle scope ===\n"
2440 "test.txt:5:10: note: msg 3\n"
2441 "test.txt:5:10: note: === inner scope ===\n"
2442 "test.txt:5:10: note: msg 4\n"
2443 "test.txt:5:10: note: explicitly user-facing msg\n"
2444 "test.txt:5:10: note: msg 5\n"
2445 "test.txt:5:10: note: msg 6\n"
2446 "test.txt:5:10: note: msg 7\n");
2447 break;
2449 if (with_optinfo)
2451 optinfo *info = tmp.get_pending_optinfo ();
2452 ASSERT_TRUE (info != NULL);
2453 ASSERT_EQ (info->num_items (), 1);
2454 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
2459 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2460 above. */
2462 /* MSG_OPTIMIZED_LOCATIONS. */
2464 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2465 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
2466 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2467 OPTINFO_KIND_SUCCESS);
2470 /* MSG_MISSED_OPTIMIZATION. */
2472 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2473 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2474 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2475 OPTINFO_KIND_FAILURE);
2479 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2481 temp_dump_context tmp (false, true,
2482 MSG_OPTIMIZED_LOCATIONS | MSG_ALL_PRIORITIES);
2483 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2485 AUTO_DUMP_SCOPE ("outer scope", stmt);
2486 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2488 AUTO_DUMP_SCOPE ("middle scope", stmt);
2489 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2491 AUTO_DUMP_SCOPE ("inner scope", stmt);
2492 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt, "msg 4\n");
2494 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2496 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2498 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2500 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: optimized: msg 4\n");
2504 /* Run all of the selftests within this file. */
2506 void
2507 dumpfile_c_tests ()
2509 test_impl_location ();
2510 for_each_line_table_case (test_capture_of_dump_calls);
2513 } // namespace selftest
2515 #endif /* CHECKING_P */