PR rtl-optimization/88470
[official-gcc.git] / gcc / dumpfile.c
blob5005f5e96b6cf660c9a5cfc7f6aa11d883a7da2e
1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "options.h"
24 #include "tree.h"
25 #include "gimple-pretty-print.h"
26 #include "diagnostic-core.h"
27 #include "dumpfile.h"
28 #include "context.h"
29 #include "profile-count.h"
30 #include "tree-cfg.h"
31 #include "langhooks.h"
32 #include "backend.h" /* for gimple.h. */
33 #include "gimple.h" /* for dump_user_location_t ctor. */
34 #include "rtl.h" /* for dump_user_location_t ctor. */
35 #include "selftest.h"
36 #include "optinfo.h"
37 #include "dump-context.h"
38 #include "cgraph.h"
39 #include "tree-pass.h" /* for "current_pass". */
40 #include "optinfo-emit-json.h"
41 #include "stringpool.h" /* for get_identifier. */
43 /* If non-NULL, return one past-the-end of the matching SUBPART of
44 the WHOLE string. */
45 #define skip_leading_substring(whole, part) \
46 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
48 static dump_flags_t pflags; /* current dump_flags */
50 static void dump_loc (dump_flags_t, FILE *, location_t);
52 /* Current -fopt-info output stream, if any, and flags. */
53 static FILE *alt_dump_file = NULL;
54 static dump_flags_t alt_flags;
56 static FILE *dump_open_alternate_stream (struct dump_file_info *);
58 /* These are currently used for communicating between passes.
59 However, instead of accessing them directly, the passes can use
60 dump_printf () for dumps. */
61 FILE *dump_file = NULL;
62 const char *dump_file_name;
63 dump_flags_t dump_flags;
64 bool dumps_are_enabled = false;
67 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
68 global. */
70 void
71 set_dump_file (FILE *new_dump_file)
73 dumpfile_ensure_any_optinfo_are_flushed ();
74 dump_file = new_dump_file;
75 dump_context::get ().refresh_dumps_are_enabled ();
78 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
79 global. */
81 static void
82 set_alt_dump_file (FILE *new_alt_dump_file)
84 dumpfile_ensure_any_optinfo_are_flushed ();
85 alt_dump_file = new_alt_dump_file;
86 dump_context::get ().refresh_dumps_are_enabled ();
89 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
90 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
91 OPTGROUP_NONE, 0, 0, num, false, false}
93 /* Table of tree dump switches. This must be consistent with the
94 TREE_DUMP_INDEX enumeration in dumpfile.h. */
95 static struct dump_file_info dump_files[TDI_end] =
97 DUMP_FILE_INFO (NULL, NULL, DK_none, 0),
98 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa, 0),
99 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa, 0),
100 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa, 0),
101 DUMP_FILE_INFO (".original", "tree-original", DK_tree, 0),
102 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree, 0),
103 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree, 0),
104 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa, 0),
105 #define FIRST_AUTO_NUMBERED_DUMP 1
106 #define FIRST_ME_AUTO_NUMBERED_DUMP 4
108 DUMP_FILE_INFO (NULL, "lang-all", DK_lang, 0),
109 DUMP_FILE_INFO (NULL, "tree-all", DK_tree, 0),
110 DUMP_FILE_INFO (NULL, "rtl-all", DK_rtl, 0),
111 DUMP_FILE_INFO (NULL, "ipa-all", DK_ipa, 0),
114 /* Table of dump options. This must be consistent with the TDF_* flags
115 in dumpfile.h and opt_info_options below. */
116 static const kv_pair<dump_flags_t> dump_options[] =
118 {"address", TDF_ADDRESS},
119 {"asmname", TDF_ASMNAME},
120 {"slim", TDF_SLIM},
121 {"raw", TDF_RAW},
122 {"graph", TDF_GRAPH},
123 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
124 | MSG_MISSED_OPTIMIZATION
125 | MSG_NOTE)},
126 {"cselib", TDF_CSELIB},
127 {"stats", TDF_STATS},
128 {"blocks", TDF_BLOCKS},
129 {"vops", TDF_VOPS},
130 {"lineno", TDF_LINENO},
131 {"uid", TDF_UID},
132 {"stmtaddr", TDF_STMTADDR},
133 {"memsyms", TDF_MEMSYMS},
134 {"eh", TDF_EH},
135 {"alias", TDF_ALIAS},
136 {"nouid", TDF_NOUID},
137 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
138 {"scev", TDF_SCEV},
139 {"gimple", TDF_GIMPLE},
140 {"folding", TDF_FOLDING},
141 {"optimized", MSG_OPTIMIZED_LOCATIONS},
142 {"missed", MSG_MISSED_OPTIMIZATION},
143 {"note", MSG_NOTE},
144 {"optall", MSG_ALL_KINDS},
145 {"all", dump_flags_t (TDF_ALL_VALUES
146 & ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
147 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
148 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
149 {NULL, TDF_NONE}
152 /* A subset of the dump_options table which is used for -fopt-info
153 types. This must be consistent with the MSG_* flags in dumpfile.h.
155 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
157 {"optimized", MSG_OPTIMIZED_LOCATIONS},
158 {"missed", MSG_MISSED_OPTIMIZATION},
159 {"note", MSG_NOTE},
160 {"all", MSG_ALL_KINDS},
161 {"internals", MSG_PRIORITY_INTERNALS},
162 {NULL, TDF_NONE}
165 /* Flags used for -fopt-info groups. */
166 const kv_pair<optgroup_flags_t> optgroup_options[] =
168 {"ipa", OPTGROUP_IPA},
169 {"loop", OPTGROUP_LOOP},
170 {"inline", OPTGROUP_INLINE},
171 {"omp", OPTGROUP_OMP},
172 {"vec", OPTGROUP_VEC},
173 {"optall", OPTGROUP_ALL},
174 {NULL, OPTGROUP_NONE}
177 gcc::dump_manager::dump_manager ():
178 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
179 m_extra_dump_files (NULL),
180 m_extra_dump_files_in_use (0),
181 m_extra_dump_files_alloced (0),
182 m_optgroup_flags (OPTGROUP_NONE),
183 m_optinfo_flags (TDF_NONE),
184 m_optinfo_filename (NULL)
188 gcc::dump_manager::~dump_manager ()
190 free (m_optinfo_filename);
191 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
193 dump_file_info *dfi = &m_extra_dump_files[i];
194 /* suffix, swtch, glob are statically allocated for the entries
195 in dump_files, and for statistics, but are dynamically allocated
196 for those for passes. */
197 if (dfi->owns_strings)
199 XDELETEVEC (const_cast <char *> (dfi->suffix));
200 XDELETEVEC (const_cast <char *> (dfi->swtch));
201 XDELETEVEC (const_cast <char *> (dfi->glob));
203 /* These, if non-NULL, are always dynamically allocated. */
204 XDELETEVEC (const_cast <char *> (dfi->pfilename));
205 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
207 XDELETEVEC (m_extra_dump_files);
210 unsigned int
211 gcc::dump_manager::
212 dump_register (const char *suffix, const char *swtch, const char *glob,
213 dump_kind dkind, optgroup_flags_t optgroup_flags,
214 bool take_ownership)
216 int num = m_next_dump++;
218 size_t count = m_extra_dump_files_in_use++;
220 if (count >= m_extra_dump_files_alloced)
222 if (m_extra_dump_files_alloced == 0)
223 m_extra_dump_files_alloced = 512;
224 else
225 m_extra_dump_files_alloced *= 2;
226 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
227 m_extra_dump_files,
228 m_extra_dump_files_alloced);
230 /* Construct a new object in the space allocated above. */
231 new (m_extra_dump_files + count) dump_file_info ();
233 else
235 /* Zero out the already constructed object. */
236 m_extra_dump_files[count] = dump_file_info ();
239 m_extra_dump_files[count].suffix = suffix;
240 m_extra_dump_files[count].swtch = swtch;
241 m_extra_dump_files[count].glob = glob;
242 m_extra_dump_files[count].dkind = dkind;
243 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
244 m_extra_dump_files[count].num = num;
245 m_extra_dump_files[count].owns_strings = take_ownership;
247 return count + TDI_end;
251 /* Allow languages and middle-end to register their dumps before the
252 optimization passes. */
254 void
255 gcc::dump_manager::
256 register_dumps ()
258 lang_hooks.register_dumps (this);
259 /* If this assert fails, some FE registered more than
260 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
261 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
262 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
263 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
264 dump_files[TDI_original].num = m_next_dump++;
265 dump_files[TDI_gimple].num = m_next_dump++;
266 dump_files[TDI_nested].num = m_next_dump++;
270 /* Return the dump_file_info for the given phase. */
272 struct dump_file_info *
273 gcc::dump_manager::
274 get_dump_file_info (int phase) const
276 if (phase < TDI_end)
277 return &dump_files[phase];
278 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
279 return NULL;
280 else
281 return m_extra_dump_files + (phase - TDI_end);
284 /* Locate the dump_file_info with swtch equal to SWTCH,
285 or return NULL if no such dump_file_info exists. */
287 struct dump_file_info *
288 gcc::dump_manager::
289 get_dump_file_info_by_switch (const char *swtch) const
291 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
292 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
293 return &m_extra_dump_files[i];
295 /* Not found. */
296 return NULL;
300 /* Return the name of the dump file for the given phase.
301 The caller is responsible for calling free on the returned
302 buffer.
303 If the dump is not enabled, returns NULL. */
305 char *
306 gcc::dump_manager::
307 get_dump_file_name (int phase, int part) const
309 struct dump_file_info *dfi;
311 if (phase == TDI_none)
312 return NULL;
314 dfi = get_dump_file_info (phase);
316 return get_dump_file_name (dfi, part);
319 /* Return the name of the dump file for the given dump_file_info.
320 The caller is responsible for calling free on the returned
321 buffer.
322 If the dump is not enabled, returns NULL. */
324 char *
325 gcc::dump_manager::
326 get_dump_file_name (struct dump_file_info *dfi, int part) const
328 char dump_id[10];
330 gcc_assert (dfi);
332 if (dfi->pstate == 0)
333 return NULL;
335 /* If available, use the command line dump filename. */
336 if (dfi->pfilename)
337 return xstrdup (dfi->pfilename);
339 if (dfi->num < 0)
340 dump_id[0] = '\0';
341 else
343 /* (null), LANG, TREE, RTL, IPA. */
344 char suffix = " ltri"[dfi->dkind];
346 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
347 dump_id[0] = '\0';
350 if (part != -1)
352 char part_id[8];
353 snprintf (part_id, sizeof (part_id), ".%i", part);
354 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
356 else
357 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
360 /* Open a dump file called FILENAME. Some filenames are special and
361 refer to the standard streams. TRUNC indicates whether this is the
362 first open (so the file should be truncated, rather than appended).
363 An error message is emitted in the event of failure. */
365 static FILE *
366 dump_open (const char *filename, bool trunc)
368 if (strcmp ("stderr", filename) == 0)
369 return stderr;
371 if (strcmp ("stdout", filename) == 0
372 || strcmp ("-", filename) == 0)
373 return stdout;
375 FILE *stream = fopen (filename, trunc ? "w" : "a");
377 if (!stream)
378 error ("could not open dump file %qs: %m", filename);
379 return stream;
382 /* For a given DFI, open an alternate dump filename (which could also
383 be a standard stream such as stdout/stderr). If the alternate dump
384 file cannot be opened, return NULL. */
386 static FILE *
387 dump_open_alternate_stream (struct dump_file_info *dfi)
389 if (!dfi->alt_filename)
390 return NULL;
392 if (dfi->alt_stream)
393 return dfi->alt_stream;
395 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
397 if (stream)
398 dfi->alt_state = 1;
400 return stream;
403 /* Construct a dump_user_location_t from STMT (using its location and
404 hotness). */
406 dump_user_location_t::dump_user_location_t (const gimple *stmt)
407 : m_count (), m_loc (UNKNOWN_LOCATION)
409 if (stmt)
411 if (stmt->bb)
412 m_count = stmt->bb->count;
413 m_loc = gimple_location (stmt);
417 /* Construct a dump_user_location_t from an RTL instruction (using its
418 location and hotness). */
420 dump_user_location_t::dump_user_location_t (const rtx_insn *insn)
421 : m_count (), m_loc (UNKNOWN_LOCATION)
423 if (insn)
425 basic_block bb = BLOCK_FOR_INSN (insn);
426 if (bb)
427 m_count = bb->count;
428 m_loc = INSN_LOCATION (insn);
432 /* Construct from a function declaration. This one requires spelling out
433 to avoid accidentally constructing from other kinds of tree. */
435 dump_user_location_t
436 dump_user_location_t::from_function_decl (tree fndecl)
438 gcc_assert (fndecl);
440 // FIXME: profile count for function?
441 return dump_user_location_t (profile_count (),
442 DECL_SOURCE_LOCATION (fndecl));
445 /* Extract the MSG_* component from DUMP_KIND and return a string for use
446 as a prefix to dump messages.
447 These match the strings in optinfo_verbosity_options and thus the
448 "OPTIONS" within "-fopt-info-OPTIONS". */
450 static const char *
451 kind_as_string (dump_flags_t dump_kind)
453 switch (dump_kind & MSG_ALL_KINDS)
455 default:
456 gcc_unreachable ();
457 case MSG_OPTIMIZED_LOCATIONS:
458 return "optimized";
459 case MSG_MISSED_OPTIMIZATION:
460 return "missed";
461 case MSG_NOTE:
462 return "note";
466 /* Print source location on DFILE if enabled. */
468 static void
469 dump_loc (dump_flags_t dump_kind, FILE *dfile, location_t loc)
471 if (dump_kind)
473 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
474 fprintf (dfile, "%s:%d:%d: ", LOCATION_FILE (loc),
475 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
476 else if (current_function_decl)
477 fprintf (dfile, "%s:%d:%d: ",
478 DECL_SOURCE_FILE (current_function_decl),
479 DECL_SOURCE_LINE (current_function_decl),
480 DECL_SOURCE_COLUMN (current_function_decl));
481 fprintf (dfile, "%s: ", kind_as_string (dump_kind));
482 /* Indentation based on scope depth. */
483 fprintf (dfile, "%*s", get_dump_scope_depth (), "");
487 /* Print source location to PP if enabled. */
489 static void
490 dump_loc (dump_flags_t dump_kind, pretty_printer *pp, location_t loc)
492 if (dump_kind)
494 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
495 pp_printf (pp, "%s:%d:%d: ", LOCATION_FILE (loc),
496 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
497 else if (current_function_decl)
498 pp_printf (pp, "%s:%d:%d: ",
499 DECL_SOURCE_FILE (current_function_decl),
500 DECL_SOURCE_LINE (current_function_decl),
501 DECL_SOURCE_COLUMN (current_function_decl));
502 pp_printf (pp, "%s: ", kind_as_string (dump_kind));
503 /* Indentation based on scope depth. */
504 for (unsigned i = 0; i < get_dump_scope_depth (); i++)
505 pp_character (pp, ' ');
509 /* Implementation of dump_context member functions. */
511 /* dump_context's dtor. */
513 dump_context::~dump_context ()
515 delete m_pending;
518 void
519 dump_context::set_json_writer (optrecord_json_writer *writer)
521 delete m_json_writer;
522 m_json_writer = writer;
525 /* Perform cleanup activity for -fsave-optimization-record.
526 Currently, the file is written out here in one go, before cleaning
527 up. */
529 void
530 dump_context::finish_any_json_writer ()
532 if (!m_json_writer)
533 return;
535 m_json_writer->write ();
536 delete m_json_writer;
537 m_json_writer = NULL;
540 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
541 or alt_dump_file change, or when changing dump_context in selftests. */
543 void
544 dump_context::refresh_dumps_are_enabled ()
546 dumps_are_enabled = (dump_file || alt_dump_file || optinfo_enabled_p ()
547 || m_test_pp);
550 /* Determine if a message of kind DUMP_KIND and at the current scope depth
551 should be printed.
553 Only show messages that match FILTER both on their kind *and*
554 their priority. */
556 bool
557 dump_context::apply_dump_filter_p (dump_flags_t dump_kind,
558 dump_flags_t filter) const
560 /* Few messages, if any, have an explicit MSG_PRIORITY.
561 If DUMP_KIND does, we'll use it.
562 Otherwise, generate an implicit priority value for the message based
563 on the current scope depth.
564 Messages at the top-level scope are MSG_PRIORITY_USER_FACING,
565 whereas those in nested scopes are MSG_PRIORITY_INTERNALS. */
566 if (!(dump_kind & MSG_ALL_PRIORITIES))
568 dump_flags_t implicit_priority
569 = (m_scope_depth > 0
570 ? MSG_PRIORITY_INTERNALS
571 : MSG_PRIORITY_USER_FACING);
572 dump_kind |= implicit_priority;
575 return (dump_kind & (filter & MSG_ALL_KINDS)
576 && dump_kind & (filter & MSG_ALL_PRIORITIES));
579 /* Print LOC to the appropriate dump destinations, given DUMP_KIND.
580 If optinfos are enabled, begin a new optinfo. */
582 void
583 dump_context::dump_loc (const dump_metadata_t &metadata,
584 const dump_user_location_t &loc)
586 end_any_optinfo ();
588 dump_loc_immediate (metadata.get_dump_flags (), loc);
590 if (optinfo_enabled_p ())
591 begin_next_optinfo (metadata, loc);
594 /* As dump_loc above, but without starting a new optinfo. */
596 void
597 dump_context::dump_loc_immediate (dump_flags_t dump_kind,
598 const dump_user_location_t &loc)
600 location_t srcloc = loc.get_location_t ();
602 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
603 ::dump_loc (dump_kind, dump_file, srcloc);
605 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
606 ::dump_loc (dump_kind, alt_dump_file, srcloc);
608 /* Support for temp_dump_context in selftests. */
609 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
610 ::dump_loc (dump_kind, m_test_pp, srcloc);
613 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
615 static optinfo_item *
616 make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
618 pretty_printer pp;
619 pp_needs_newline (&pp) = true;
620 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
621 pp_newline (&pp);
623 optinfo_item *item
624 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
625 xstrdup (pp_formatted_text (&pp)));
626 return item;
629 /* Dump gimple statement GS with SPC indentation spaces and
630 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
632 void
633 dump_context::dump_gimple_stmt (const dump_metadata_t &metadata,
634 dump_flags_t extra_dump_flags,
635 gimple *gs, int spc)
637 optinfo_item *item
638 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
639 emit_item (item, metadata.get_dump_flags ());
641 if (optinfo_enabled_p ())
643 optinfo &info = ensure_pending_optinfo (metadata);
644 info.add_item (item);
646 else
647 delete item;
650 /* Similar to dump_gimple_stmt, except additionally print source location. */
652 void
653 dump_context::dump_gimple_stmt_loc (const dump_metadata_t &metadata,
654 const dump_user_location_t &loc,
655 dump_flags_t extra_dump_flags,
656 gimple *gs, int spc)
658 dump_loc (metadata, loc);
659 dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
662 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
664 static optinfo_item *
665 make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
667 dump_flags |= TDF_RHS_ONLY;
668 pretty_printer pp;
669 pp_needs_newline (&pp) = true;
670 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
672 optinfo_item *item
673 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
674 xstrdup (pp_formatted_text (&pp)));
675 return item;
678 /* Dump gimple statement GS with SPC indentation spaces and
679 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
680 Do not terminate with a newline or semicolon. */
682 void
683 dump_context::dump_gimple_expr (const dump_metadata_t &metadata,
684 dump_flags_t extra_dump_flags,
685 gimple *gs, int spc)
687 optinfo_item *item
688 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
689 emit_item (item, metadata.get_dump_flags ());
691 if (optinfo_enabled_p ())
693 optinfo &info = ensure_pending_optinfo (metadata);
694 info.add_item (item);
696 else
697 delete item;
700 /* Similar to dump_gimple_expr, except additionally print source location. */
702 void
703 dump_context::dump_gimple_expr_loc (const dump_metadata_t &metadata,
704 const dump_user_location_t &loc,
705 dump_flags_t extra_dump_flags,
706 gimple *gs,
707 int spc)
709 dump_loc (metadata, loc);
710 dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
713 /* Make an item for the given dump call, equivalent to print_generic_expr. */
715 static optinfo_item *
716 make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
718 pretty_printer pp;
719 pp_needs_newline (&pp) = true;
720 pp_translate_identifiers (&pp) = false;
721 dump_generic_node (&pp, node, 0, dump_flags, false);
723 location_t loc = UNKNOWN_LOCATION;
724 if (EXPR_HAS_LOCATION (node))
725 loc = EXPR_LOCATION (node);
727 optinfo_item *item
728 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
729 xstrdup (pp_formatted_text (&pp)));
730 return item;
733 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
734 DUMP_KIND is enabled. */
736 void
737 dump_context::dump_generic_expr (const dump_metadata_t &metadata,
738 dump_flags_t extra_dump_flags,
739 tree t)
741 optinfo_item *item
742 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
743 emit_item (item, metadata.get_dump_flags ());
745 if (optinfo_enabled_p ())
747 optinfo &info = ensure_pending_optinfo (metadata);
748 info.add_item (item);
750 else
751 delete item;
755 /* Similar to dump_generic_expr, except additionally print the source
756 location. */
758 void
759 dump_context::dump_generic_expr_loc (const dump_metadata_t &metadata,
760 const dump_user_location_t &loc,
761 dump_flags_t extra_dump_flags,
762 tree t)
764 dump_loc (metadata, loc);
765 dump_generic_expr (metadata, extra_dump_flags, t);
768 /* Make an item for the given dump call. */
770 static optinfo_item *
771 make_item_for_dump_symtab_node (symtab_node *node)
773 location_t loc = DECL_SOURCE_LOCATION (node->decl);
774 optinfo_item *item
775 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
776 xstrdup (node->dump_name ()));
777 return item;
780 /* dump_pretty_printer's ctor. */
782 dump_pretty_printer::dump_pretty_printer (dump_context *context,
783 dump_flags_t dump_kind)
784 : pretty_printer (), m_context (context), m_dump_kind (dump_kind),
785 m_stashed_items ()
787 pp_format_decoder (this) = format_decoder_cb;
790 /* Phase 3 of formatting; compare with pp_output_formatted_text.
792 Emit optinfo_item instances for the various formatted chunks from phases
793 1 and 2 (i.e. pp_format).
795 Some chunks may already have had their items built (during decode_format).
796 These chunks have been stashed into m_stashed_items; we emit them here.
798 For all other purely textual chunks, they are printed into
799 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
800 This consolidates multiple adjacent text chunks into a single text
801 optinfo_item. */
803 void
804 dump_pretty_printer::emit_items (optinfo *dest)
806 output_buffer *buffer = pp_buffer (this);
807 struct chunk_info *chunk_array = buffer->cur_chunk_array;
808 const char **args = chunk_array->args;
810 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
811 gcc_assert (buffer->line_length == 0);
813 unsigned stashed_item_idx = 0;
814 for (unsigned chunk = 0; args[chunk]; chunk++)
816 if (stashed_item_idx < m_stashed_items.length ()
817 && args[chunk] == *m_stashed_items[stashed_item_idx].buffer_ptr)
819 emit_any_pending_textual_chunks (dest);
820 /* This chunk has a stashed item: use it. */
821 emit_item (m_stashed_items[stashed_item_idx++].item, dest);
823 else
824 /* This chunk is purely textual. Print it (to
825 buffer->formatted_obstack), so that we can consolidate adjacent
826 chunks into one textual optinfo_item. */
827 pp_string (this, args[chunk]);
830 emit_any_pending_textual_chunks (dest);
832 /* Ensure that we consumed all of stashed_items. */
833 gcc_assert (stashed_item_idx == m_stashed_items.length ());
835 /* Deallocate the chunk structure and everything after it (i.e. the
836 associated series of formatted strings). */
837 buffer->cur_chunk_array = chunk_array->prev;
838 obstack_free (&buffer->chunk_obstack, chunk_array);
841 /* Subroutine of dump_pretty_printer::emit_items
842 for consolidating multiple adjacent pure-text chunks into single
843 optinfo_items (in phase 3). */
845 void
846 dump_pretty_printer::emit_any_pending_textual_chunks (optinfo *dest)
848 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
850 /* Don't emit an item if the pending text is empty. */
851 if (output_buffer_last_position_in_text (buffer) == NULL)
852 return;
854 char *formatted_text = xstrdup (pp_formatted_text (this));
855 optinfo_item *item
856 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
857 formatted_text);
858 emit_item (item, dest);
860 /* Clear the pending text by unwinding formatted_text back to the start
861 of the buffer (without deallocating). */
862 obstack_free (&buffer->formatted_obstack,
863 buffer->formatted_obstack.object_base);
866 /* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
867 to DEST; otherwise delete ITEM. */
869 void
870 dump_pretty_printer::emit_item (optinfo_item *item, optinfo *dest)
872 m_context->emit_item (item, m_dump_kind);
873 if (dest)
874 dest->add_item (item);
875 else
876 delete item;
879 /* Record that ITEM (generated in phase 2 of formatting) is to be used for
880 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
882 void
883 dump_pretty_printer::stash_item (const char **buffer_ptr, optinfo_item *item)
885 gcc_assert (buffer_ptr);
886 gcc_assert (item);
888 m_stashed_items.safe_push (stashed_item (buffer_ptr, item));
891 /* pp_format_decoder callback for dump_pretty_printer, and thus for
892 dump_printf and dump_printf_loc.
894 A wrapper around decode_format, for type-safety. */
896 bool
897 dump_pretty_printer::format_decoder_cb (pretty_printer *pp, text_info *text,
898 const char *spec, int /*precision*/,
899 bool /*wide*/, bool /*set_locus*/,
900 bool /*verbose*/, bool */*quoted*/,
901 const char **buffer_ptr)
903 dump_pretty_printer *opp = static_cast <dump_pretty_printer *> (pp);
904 return opp->decode_format (text, spec, buffer_ptr);
907 /* Format decoder for dump_pretty_printer, and thus for dump_printf and
908 dump_printf_loc.
910 Supported format codes (in addition to the standard pretty_printer ones)
911 are:
913 %C: cgraph_node *:
914 Equivalent to: dump_symtab_node (MSG_*, node)
915 %E: gimple *:
916 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
917 %G: gimple *:
918 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
919 %T: tree:
920 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
922 TODO: add a format code that can handle (symtab_node*) *and* both
923 subclasses (presumably means teaching -Wformat about non-virtual
924 subclasses).
926 These format codes build optinfo_item instances, thus capturing metadata
927 about the arguments being dumped, as well as the textual output. */
929 bool
930 dump_pretty_printer::decode_format (text_info *text, const char *spec,
931 const char **buffer_ptr)
933 /* Various format codes that imply making an optinfo_item and stashed it
934 for later use (to capture metadata, rather than plain text). */
935 switch (*spec)
937 case 'C':
939 cgraph_node *node = va_arg (*text->args_ptr, cgraph_node *);
941 /* Make an item for the node, and stash it. */
942 optinfo_item *item = make_item_for_dump_symtab_node (node);
943 stash_item (buffer_ptr, item);
944 return true;
947 case 'E':
949 gimple *stmt = va_arg (*text->args_ptr, gimple *);
951 /* Make an item for the stmt, and stash it. */
952 optinfo_item *item = make_item_for_dump_gimple_expr (stmt, 0, TDF_SLIM);
953 stash_item (buffer_ptr, item);
954 return true;
957 case 'G':
959 gimple *stmt = va_arg (*text->args_ptr, gimple *);
961 /* Make an item for the stmt, and stash it. */
962 optinfo_item *item = make_item_for_dump_gimple_stmt (stmt, 0, TDF_SLIM);
963 stash_item (buffer_ptr, item);
964 return true;
967 case 'T':
969 tree t = va_arg (*text->args_ptr, tree);
971 /* Make an item for the tree, and stash it. */
972 optinfo_item *item = make_item_for_dump_generic_expr (t, TDF_SLIM);
973 stash_item (buffer_ptr, item);
974 return true;
977 default:
978 return false;
982 /* Output a formatted message using FORMAT on appropriate dump streams. */
984 void
985 dump_context::dump_printf_va (const dump_metadata_t &metadata, const char *format,
986 va_list *ap)
988 dump_pretty_printer pp (this, metadata.get_dump_flags ());
990 text_info text;
991 text.err_no = errno;
992 text.args_ptr = ap;
993 text.format_spec = format;
995 /* Phases 1 and 2, using pp_format. */
996 pp_format (&pp, &text);
998 /* Phase 3. */
999 if (optinfo_enabled_p ())
1001 optinfo &info = ensure_pending_optinfo (metadata);
1002 pp.emit_items (&info);
1004 else
1005 pp.emit_items (NULL);
1008 /* Similar to dump_printf, except source location is also printed, and
1009 dump location captured. */
1011 void
1012 dump_context::dump_printf_loc_va (const dump_metadata_t &metadata,
1013 const dump_user_location_t &loc,
1014 const char *format, va_list *ap)
1016 dump_loc (metadata, loc);
1017 dump_printf_va (metadata, format, ap);
1020 /* Make an item for the given dump call, equivalent to print_dec. */
1022 template<unsigned int N, typename C>
1023 static optinfo_item *
1024 make_item_for_dump_dec (const poly_int<N, C> &value)
1026 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
1027 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
1029 pretty_printer pp;
1031 if (value.is_constant ())
1032 pp_wide_int (&pp, value.coeffs[0], sgn);
1033 else
1035 pp_character (&pp, '[');
1036 for (unsigned int i = 0; i < N; ++i)
1038 pp_wide_int (&pp, value.coeffs[i], sgn);
1039 pp_character (&pp, i == N - 1 ? ']' : ',');
1043 optinfo_item *item
1044 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1045 xstrdup (pp_formatted_text (&pp)));
1046 return item;
1049 /* Output VALUE in decimal to appropriate dump streams. */
1051 template<unsigned int N, typename C>
1052 void
1053 dump_context::dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
1055 optinfo_item *item = make_item_for_dump_dec (value);
1056 emit_item (item, metadata.get_dump_flags ());
1058 if (optinfo_enabled_p ())
1060 optinfo &info = ensure_pending_optinfo (metadata);
1061 info.add_item (item);
1063 else
1064 delete item;
1067 /* Output the name of NODE on appropriate dump streams. */
1069 void
1070 dump_context::dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
1072 optinfo_item *item = make_item_for_dump_symtab_node (node);
1073 emit_item (item, metadata.get_dump_flags ());
1075 if (optinfo_enabled_p ())
1077 optinfo &info = ensure_pending_optinfo (metadata);
1078 info.add_item (item);
1080 else
1081 delete item;
1084 /* Get the current dump scope-nesting depth.
1085 For use by -fopt-info (for showing nesting via indentation). */
1087 unsigned int
1088 dump_context::get_scope_depth () const
1090 return m_scope_depth;
1093 /* Push a nested dump scope.
1094 Increment the scope depth.
1095 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1096 destination, if any.
1097 Emit a "scope" optinfo if optinfos are enabled. */
1099 void
1100 dump_context::begin_scope (const char *name,
1101 const dump_user_location_t &user_location,
1102 const dump_impl_location_t &impl_location)
1104 m_scope_depth++;
1106 location_t src_loc = user_location.get_location_t ();
1108 if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
1109 ::dump_loc (MSG_NOTE, dump_file, src_loc);
1111 if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
1112 ::dump_loc (MSG_NOTE, alt_dump_file, src_loc);
1114 /* Support for temp_dump_context in selftests. */
1115 if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
1116 ::dump_loc (MSG_NOTE, m_test_pp, src_loc);
1118 pretty_printer pp;
1119 pp_printf (&pp, "=== %s ===\n", name);
1120 optinfo_item *item
1121 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1122 xstrdup (pp_formatted_text (&pp)));
1123 emit_item (item, MSG_NOTE);
1125 if (optinfo_enabled_p ())
1127 optinfo &info
1128 = begin_next_optinfo (dump_metadata_t (MSG_NOTE, impl_location),
1129 user_location);
1130 info.m_kind = OPTINFO_KIND_SCOPE;
1131 info.add_item (item);
1132 end_any_optinfo ();
1134 else
1135 delete item;
1138 /* Pop a nested dump scope. */
1140 void
1141 dump_context::end_scope ()
1143 end_any_optinfo ();
1144 m_scope_depth--;
1146 if (m_json_writer)
1147 m_json_writer->pop_scope ();
1150 /* Should optinfo instances be created?
1151 All creation of optinfos should be guarded by this predicate.
1152 Return true if any optinfo destinations are active. */
1154 bool
1155 dump_context::optinfo_enabled_p () const
1157 return (optimization_records_enabled_p ());
1160 /* Return the optinfo currently being accumulated, creating one if
1161 necessary. */
1163 optinfo &
1164 dump_context::ensure_pending_optinfo (const dump_metadata_t &metadata)
1166 if (!m_pending)
1167 return begin_next_optinfo (metadata, dump_user_location_t ());
1168 return *m_pending;
1171 /* Start a new optinfo and return it, ending any optinfo that was already
1172 accumulated. */
1174 optinfo &
1175 dump_context::begin_next_optinfo (const dump_metadata_t &metadata,
1176 const dump_user_location_t &user_loc)
1178 end_any_optinfo ();
1179 gcc_assert (m_pending == NULL);
1180 dump_location_t loc (user_loc, metadata.get_impl_location ());
1181 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
1182 m_pending->handle_dump_file_kind (metadata.get_dump_flags ());
1183 return *m_pending;
1186 /* End any optinfo that has been accumulated within this context; emitting
1187 it to any destinations as appropriate, such as optimization records. */
1189 void
1190 dump_context::end_any_optinfo ()
1192 if (m_pending)
1193 emit_optinfo (m_pending);
1194 delete m_pending;
1195 m_pending = NULL;
1198 /* Emit the optinfo to all of the "non-immediate" destinations
1199 (emission to "immediate" destinations is done by
1200 dump_context::emit_item). */
1202 void
1203 dump_context::emit_optinfo (const optinfo *info)
1205 /* -fsave-optimization-record. */
1206 if (m_json_writer)
1207 m_json_writer->add_record (info);
1210 /* Emit ITEM to all item destinations (those that don't require
1211 consolidation into optinfo instances). */
1213 void
1214 dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
1216 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
1217 fprintf (dump_file, "%s", item->get_text ());
1219 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
1220 fprintf (alt_dump_file, "%s", item->get_text ());
1222 /* Support for temp_dump_context in selftests. */
1223 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
1224 pp_string (m_test_pp, item->get_text ());
1227 /* The current singleton dump_context, and its default. */
1229 dump_context *dump_context::s_current = &dump_context::s_default;
1230 dump_context dump_context::s_default;
1232 /* Implementation of dump_* API calls, calling into dump_context
1233 member functions. */
1235 /* Calls to the dump_* functions do non-trivial work, so they ought
1236 to be guarded by:
1237 if (dump_enabled_p ())
1238 Assert that they are guarded, and, if assertions are disabled,
1239 bail out if the calls weren't properly guarded. */
1241 #define VERIFY_DUMP_ENABLED_P \
1242 do { \
1243 gcc_assert (dump_enabled_p ()); \
1244 if (!dump_enabled_p ()) \
1245 return; \
1246 } while (0)
1248 /* Dump gimple statement GS with SPC indentation spaces and
1249 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1251 void
1252 dump_gimple_stmt (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
1253 gimple *gs, int spc)
1255 VERIFY_DUMP_ENABLED_P;
1256 dump_context::get ().dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
1259 /* Similar to dump_gimple_stmt, except additionally print source location. */
1261 void
1262 dump_gimple_stmt_loc (const dump_metadata_t &metadata,
1263 const dump_user_location_t &loc,
1264 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1266 VERIFY_DUMP_ENABLED_P;
1267 dump_context::get ().dump_gimple_stmt_loc (metadata, loc, extra_dump_flags,
1268 gs, spc);
1271 /* Dump gimple statement GS with SPC indentation spaces and
1272 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1273 Do not terminate with a newline or semicolon. */
1275 void
1276 dump_gimple_expr (const dump_metadata_t &metadata,
1277 dump_flags_t extra_dump_flags,
1278 gimple *gs, int spc)
1280 VERIFY_DUMP_ENABLED_P;
1281 dump_context::get ().dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
1284 /* Similar to dump_gimple_expr, except additionally print source location. */
1286 void
1287 dump_gimple_expr_loc (const dump_metadata_t &metadata,
1288 const dump_user_location_t &loc,
1289 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1291 VERIFY_DUMP_ENABLED_P;
1292 dump_context::get ().dump_gimple_expr_loc (metadata, loc, extra_dump_flags,
1293 gs, spc);
1296 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1297 DUMP_KIND is enabled. */
1299 void
1300 dump_generic_expr (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
1301 tree t)
1303 VERIFY_DUMP_ENABLED_P;
1304 dump_context::get ().dump_generic_expr (metadata, extra_dump_flags, t);
1307 /* Similar to dump_generic_expr, except additionally print the source
1308 location. */
1310 void
1311 dump_generic_expr_loc (const dump_metadata_t &metadata,
1312 const dump_user_location_t &loc,
1313 dump_flags_t extra_dump_flags, tree t)
1315 VERIFY_DUMP_ENABLED_P;
1316 dump_context::get ().dump_generic_expr_loc (metadata, loc, extra_dump_flags,
1320 /* Output a formatted message using FORMAT on appropriate dump streams. */
1322 void
1323 dump_printf (const dump_metadata_t &metadata, const char *format, ...)
1325 VERIFY_DUMP_ENABLED_P;
1326 va_list ap;
1327 va_start (ap, format);
1328 dump_context::get ().dump_printf_va (metadata, format, &ap);
1329 va_end (ap);
1332 /* Similar to dump_printf, except source location is also printed, and
1333 dump location captured. */
1335 void
1336 dump_printf_loc (const dump_metadata_t &metadata,
1337 const dump_user_location_t &loc,
1338 const char *format, ...)
1340 VERIFY_DUMP_ENABLED_P;
1341 va_list ap;
1342 va_start (ap, format);
1343 dump_context::get ().dump_printf_loc_va (metadata, loc, format, &ap);
1344 va_end (ap);
1347 /* Output VALUE in decimal to appropriate dump streams. */
1349 template<unsigned int N, typename C>
1350 void
1351 dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
1353 VERIFY_DUMP_ENABLED_P;
1354 dump_context::get ().dump_dec (metadata, value);
1357 template void dump_dec (const dump_metadata_t &metadata, const poly_uint16 &);
1358 template void dump_dec (const dump_metadata_t &metadata, const poly_int64 &);
1359 template void dump_dec (const dump_metadata_t &metadata, const poly_uint64 &);
1360 template void dump_dec (const dump_metadata_t &metadata, const poly_offset_int &);
1361 template void dump_dec (const dump_metadata_t &metadata, const poly_widest_int &);
1363 void
1364 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1366 VERIFY_DUMP_ENABLED_P;
1367 if (dump_file
1368 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1369 print_dec (value, dump_file, sgn);
1371 if (alt_dump_file
1372 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1373 print_dec (value, alt_dump_file, sgn);
1376 /* Output VALUE in hexadecimal to appropriate dump streams. */
1378 void
1379 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1381 VERIFY_DUMP_ENABLED_P;
1382 if (dump_file
1383 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
1384 print_hex (value, dump_file);
1386 if (alt_dump_file
1387 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
1388 print_hex (value, alt_dump_file);
1391 /* Emit and delete the currently pending optinfo, if there is one,
1392 without the caller needing to know about class dump_context. */
1394 void
1395 dumpfile_ensure_any_optinfo_are_flushed ()
1397 dump_context::get().end_any_optinfo ();
1400 /* Output the name of NODE on appropriate dump streams. */
1402 void
1403 dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
1405 VERIFY_DUMP_ENABLED_P;
1406 dump_context::get ().dump_symtab_node (metadata, node);
1409 /* Get the current dump scope-nesting depth.
1410 For use by -fopt-info (for showing nesting via indentation). */
1412 unsigned int
1413 get_dump_scope_depth ()
1415 return dump_context::get ().get_scope_depth ();
1418 /* Push a nested dump scope.
1419 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1420 destination, if any.
1421 Emit a "scope" opinfo if optinfos are enabled.
1422 Increment the scope depth. */
1424 void
1425 dump_begin_scope (const char *name,
1426 const dump_user_location_t &user_location,
1427 const dump_impl_location_t &impl_location)
1429 dump_context::get ().begin_scope (name, user_location, impl_location);
1432 /* Pop a nested dump scope. */
1434 void
1435 dump_end_scope ()
1437 dump_context::get ().end_scope ();
1440 /* Start a dump for PHASE. Store user-supplied dump flags in
1441 *FLAG_PTR. Return the number of streams opened. Set globals
1442 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1443 set dump_flags appropriately for both pass dump stream and
1444 -fopt-info stream. */
1447 gcc::dump_manager::
1448 dump_start (int phase, dump_flags_t *flag_ptr)
1450 int count = 0;
1451 char *name;
1452 struct dump_file_info *dfi;
1453 FILE *stream;
1454 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1455 return 0;
1457 dfi = get_dump_file_info (phase);
1458 name = get_dump_file_name (phase);
1459 if (name)
1461 stream = dump_open (name, dfi->pstate < 0);
1462 if (stream)
1464 dfi->pstate = 1;
1465 count++;
1467 free (name);
1468 dfi->pstream = stream;
1469 set_dump_file (dfi->pstream);
1470 /* Initialize current dump flags. */
1471 pflags = dfi->pflags;
1474 stream = dump_open_alternate_stream (dfi);
1475 if (stream)
1477 dfi->alt_stream = stream;
1478 count++;
1479 set_alt_dump_file (dfi->alt_stream);
1480 /* Initialize current -fopt-info flags. */
1481 alt_flags = dfi->alt_flags;
1484 if (flag_ptr)
1485 *flag_ptr = dfi->pflags;
1487 return count;
1490 /* Finish a tree dump for PHASE and close associated dump streams. Also
1491 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1493 void
1494 gcc::dump_manager::
1495 dump_finish (int phase)
1497 struct dump_file_info *dfi;
1499 if (phase < 0)
1500 return;
1501 dfi = get_dump_file_info (phase);
1502 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1503 fclose (dfi->pstream);
1505 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1506 fclose (dfi->alt_stream);
1508 dfi->alt_stream = NULL;
1509 dfi->pstream = NULL;
1510 set_dump_file (NULL);
1511 set_alt_dump_file (NULL);
1512 dump_flags = TDF_NONE;
1513 alt_flags = TDF_NONE;
1514 pflags = TDF_NONE;
1517 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1518 *FLAG_PTR and returns a stream to write to. If the dump is not
1519 enabled, returns NULL.
1520 PART can be used for dump files which should be split to multiple
1521 parts. PART == -1 indicates dump file with no parts.
1522 If PART is -1, multiple calls will reopen and append to the dump file. */
1524 FILE *
1525 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1527 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1530 FILE *
1531 gcc::dump_manager::
1532 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1534 char *name;
1535 struct dump_file_info *dfi;
1536 FILE *stream;
1538 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1539 return NULL;
1541 name = get_dump_file_name (phase, part);
1542 if (!name)
1543 return NULL;
1544 dfi = get_dump_file_info (phase);
1546 /* We do not support re-opening of dump files with parts. This would require
1547 tracking pstate per part of the dump file. */
1548 stream = dump_open (name, part != -1 || dfi->pstate < 0);
1549 if (stream)
1550 dfi->pstate = 1;
1551 free (name);
1553 if (flag_ptr)
1554 *flag_ptr = dfi->pflags;
1556 /* Initialize current flags */
1557 pflags = dfi->pflags;
1558 return stream;
1561 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1562 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1563 any phase. */
1566 gcc::dump_manager::
1567 dump_phase_enabled_p (int phase) const
1569 if (phase == TDI_tree_all)
1571 size_t i;
1572 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1573 if (dump_files[i].pstate || dump_files[i].alt_state)
1574 return 1;
1575 for (i = 0; i < m_extra_dump_files_in_use; i++)
1576 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1577 return 1;
1578 return 0;
1580 else
1582 struct dump_file_info *dfi = get_dump_file_info (phase);
1583 return dfi->pstate || dfi->alt_state;
1587 /* Returns nonzero if tree dump PHASE has been initialized. */
1590 gcc::dump_manager::
1591 dump_initialized_p (int phase) const
1593 struct dump_file_info *dfi = get_dump_file_info (phase);
1594 return dfi->pstate > 0 || dfi->alt_state > 0;
1597 /* Returns the switch name of PHASE. */
1599 const char *
1600 dump_flag_name (int phase)
1602 return g->get_dumps ()->dump_flag_name (phase);
1605 const char *
1606 gcc::dump_manager::
1607 dump_flag_name (int phase) const
1609 struct dump_file_info *dfi = get_dump_file_info (phase);
1610 return dfi->swtch;
1613 /* Handle -fdump-* and -fopt-info for a pass added after
1614 command-line options are parsed (those from plugins and
1615 those from backends).
1617 Because the registration of plugin/backend passes happens after the
1618 command-line options are parsed, the options that specify single
1619 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1620 passes. Therefore we currently can only enable dumping of
1621 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1622 are specified. This is done here.
1624 Similarly, the saved -fopt-info options are wired up to the new pass. */
1626 void
1627 gcc::dump_manager::register_pass (opt_pass *pass)
1629 gcc_assert (pass);
1631 register_one_dump_file (pass);
1633 dump_file_info *pass_dfi = get_dump_file_info (pass->static_pass_number);
1634 gcc_assert (pass_dfi);
1636 enum tree_dump_index tdi;
1637 if (pass->type == SIMPLE_IPA_PASS
1638 || pass->type == IPA_PASS)
1639 tdi = TDI_ipa_all;
1640 else if (pass->type == GIMPLE_PASS)
1641 tdi = TDI_tree_all;
1642 else
1643 tdi = TDI_rtl_all;
1644 const dump_file_info *tdi_dfi = get_dump_file_info (tdi);
1645 gcc_assert (tdi_dfi);
1647 /* Check if dump-all flag is specified. */
1648 if (tdi_dfi->pstate)
1650 pass_dfi->pstate = tdi_dfi->pstate;
1651 pass_dfi->pflags = tdi_dfi->pflags;
1654 update_dfi_for_opt_info (pass_dfi);
1657 /* Finish a tree dump for PHASE. STREAM is the stream created by
1658 dump_begin. */
1660 void
1661 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1663 if (stream != stderr && stream != stdout)
1664 fclose (stream);
1667 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1668 enabled tree dumps. */
1671 gcc::dump_manager::
1672 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1674 int n = 0;
1675 size_t i;
1677 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1679 if (dump_files[i].dkind == dkind)
1681 const char *old_filename = dump_files[i].pfilename;
1682 dump_files[i].pstate = -1;
1683 dump_files[i].pflags |= flags;
1684 n++;
1685 /* Override the existing filename. */
1686 if (filename)
1688 dump_files[i].pfilename = xstrdup (filename);
1689 /* Since it is a command-line provided file, which is
1690 common to all the phases, use it in append mode. */
1691 dump_files[i].pstate = 1;
1693 if (old_filename && filename != old_filename)
1694 free (CONST_CAST (char *, old_filename));
1698 for (i = 0; i < m_extra_dump_files_in_use; i++)
1700 if (m_extra_dump_files[i].dkind == dkind)
1702 const char *old_filename = m_extra_dump_files[i].pfilename;
1703 m_extra_dump_files[i].pstate = -1;
1704 m_extra_dump_files[i].pflags |= flags;
1705 n++;
1706 /* Override the existing filename. */
1707 if (filename)
1709 m_extra_dump_files[i].pfilename = xstrdup (filename);
1710 /* Since it is a command-line provided file, which is
1711 common to all the phases, use it in append mode. */
1712 m_extra_dump_files[i].pstate = 1;
1714 if (old_filename && filename != old_filename)
1715 free (CONST_CAST (char *, old_filename));
1719 return n;
1722 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1723 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1724 dumps. */
1727 gcc::dump_manager::
1728 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1729 const char *filename)
1731 int n = 0;
1733 m_optgroup_flags = optgroup_flags;
1734 m_optinfo_flags = flags;
1735 m_optinfo_filename = xstrdup (filename);
1737 for (size_t i = TDI_none + 1; i < (size_t) TDI_end; i++)
1738 if (update_dfi_for_opt_info (&dump_files[i]))
1739 n++;
1741 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1742 if (update_dfi_for_opt_info (&m_extra_dump_files[i]))
1743 n++;
1745 return n;
1748 /* Use the saved -fopt-info options to update DFI.
1749 Return true if the dump is enabled. */
1751 bool
1752 gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
1754 gcc_assert (dfi);
1756 if (!(dfi->optgroup_flags & m_optgroup_flags))
1757 return false;
1759 const char *old_filename = dfi->alt_filename;
1760 /* Since this file is shared among different passes, it
1761 should be opened in append mode. */
1762 dfi->alt_state = 1;
1763 dfi->alt_flags |= m_optinfo_flags;
1764 /* Override the existing filename. */
1765 if (m_optinfo_filename)
1766 dfi->alt_filename = xstrdup (m_optinfo_filename);
1767 if (old_filename && m_optinfo_filename != old_filename)
1768 free (CONST_CAST (char *, old_filename));
1770 return true;
1773 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1774 relevant details in the dump_files array. */
1777 gcc::dump_manager::
1778 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1780 const char *option_value;
1781 const char *ptr;
1782 dump_flags_t flags;
1784 if (doglob && !dfi->glob)
1785 return 0;
1787 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1788 if (!option_value)
1789 return 0;
1791 if (*option_value && *option_value != '-' && *option_value != '=')
1792 return 0;
1794 ptr = option_value;
1796 /* Retain "user-facing" and "internals" messages, but filter out
1797 those from an opt_problem being re-emitted at the top level
1798 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1799 messing up scan-tree-dump-times" in DejaGnu tests. */
1800 flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_INTERNALS;
1802 while (*ptr)
1804 const struct kv_pair<dump_flags_t> *option_ptr;
1805 const char *end_ptr;
1806 const char *eq_ptr;
1807 unsigned length;
1809 while (*ptr == '-')
1810 ptr++;
1811 end_ptr = strchr (ptr, '-');
1812 eq_ptr = strchr (ptr, '=');
1814 if (eq_ptr && !end_ptr)
1815 end_ptr = eq_ptr;
1817 if (!end_ptr)
1818 end_ptr = ptr + strlen (ptr);
1819 length = end_ptr - ptr;
1821 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1822 if (strlen (option_ptr->name) == length
1823 && !memcmp (option_ptr->name, ptr, length))
1825 flags |= option_ptr->value;
1826 goto found;
1829 if (*ptr == '=')
1831 /* Interpret rest of the argument as a dump filename. This
1832 filename overrides other command line filenames. */
1833 if (dfi->pfilename)
1834 free (CONST_CAST (char *, dfi->pfilename));
1835 dfi->pfilename = xstrdup (ptr + 1);
1836 break;
1838 else
1839 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1840 length, ptr, dfi->swtch);
1841 found:;
1842 ptr = end_ptr;
1845 dfi->pstate = -1;
1846 dfi->pflags |= flags;
1848 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1849 known dumps. */
1850 if (dfi->suffix == NULL)
1851 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1853 return 1;
1857 gcc::dump_manager::
1858 dump_switch_p (const char *arg)
1860 size_t i;
1861 int any = 0;
1863 for (i = TDI_none + 1; i != TDI_end; i++)
1864 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1866 /* Don't glob if we got a hit already */
1867 if (!any)
1868 for (i = TDI_none + 1; i != TDI_end; i++)
1869 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1871 for (i = 0; i < m_extra_dump_files_in_use; i++)
1872 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1874 if (!any)
1875 for (i = 0; i < m_extra_dump_files_in_use; i++)
1876 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1879 return any;
1882 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1883 and filename. Return non-zero if it is a recognized switch. */
1885 static int
1886 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1887 optgroup_flags_t *optgroup_flags, char **filename)
1889 const char *option_value;
1890 const char *ptr;
1892 option_value = arg;
1893 ptr = option_value;
1895 *filename = NULL;
1897 /* Default to filtering out "internals" messages, and retaining
1898 "user-facing" messages, and those from an opt_problem being
1899 re-emitted at the top level. */
1900 *flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_REEMITTED;
1902 *optgroup_flags = OPTGROUP_NONE;
1904 if (!ptr)
1905 return 1; /* Handle '-fopt-info' without any additional options. */
1907 while (*ptr)
1909 const char *end_ptr;
1910 const char *eq_ptr;
1911 unsigned length;
1913 while (*ptr == '-')
1914 ptr++;
1915 end_ptr = strchr (ptr, '-');
1916 eq_ptr = strchr (ptr, '=');
1918 if (eq_ptr && !end_ptr)
1919 end_ptr = eq_ptr;
1921 if (!end_ptr)
1922 end_ptr = ptr + strlen (ptr);
1923 length = end_ptr - ptr;
1925 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1926 option_ptr->name; option_ptr++)
1927 if (strlen (option_ptr->name) == length
1928 && !memcmp (option_ptr->name, ptr, length))
1930 *flags |= option_ptr->value;
1931 goto found;
1934 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1935 option_ptr->name; option_ptr++)
1936 if (strlen (option_ptr->name) == length
1937 && !memcmp (option_ptr->name, ptr, length))
1939 *optgroup_flags |= option_ptr->value;
1940 goto found;
1943 if (*ptr == '=')
1945 /* Interpret rest of the argument as a dump filename. This
1946 filename overrides other command line filenames. */
1947 *filename = xstrdup (ptr + 1);
1948 break;
1950 else
1952 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1953 length, ptr, arg);
1954 return 0;
1956 found:;
1957 ptr = end_ptr;
1960 return 1;
1963 /* Return non-zero if ARG is a recognized switch for
1964 -fopt-info. Return zero otherwise. */
1967 opt_info_switch_p (const char *arg)
1969 dump_flags_t flags;
1970 optgroup_flags_t optgroup_flags;
1971 char *filename;
1972 static char *file_seen = NULL;
1973 gcc::dump_manager *dumps = g->get_dumps ();
1975 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1976 return 0;
1978 if (!filename)
1979 filename = xstrdup ("stderr");
1981 /* Bail out if a different filename has been specified. */
1982 if (file_seen && strcmp (file_seen, filename))
1984 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1985 arg);
1986 return 1;
1989 file_seen = xstrdup (filename);
1990 if (!(flags & MSG_ALL_KINDS))
1991 flags |= MSG_OPTIMIZED_LOCATIONS;
1992 if (!optgroup_flags)
1993 optgroup_flags = OPTGROUP_ALL;
1995 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1998 /* Print basic block on the dump streams. */
2000 void
2001 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
2003 if (dump_file
2004 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
2005 dump_bb (dump_file, bb, indent, TDF_DETAILS);
2006 if (alt_dump_file
2007 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
2008 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
2011 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
2013 void
2014 dump_function (int phase, tree fn)
2016 FILE *stream;
2017 dump_flags_t flags;
2019 stream = dump_begin (phase, &flags);
2020 if (stream)
2022 dump_function_to_file (fn, stream, flags);
2023 dump_end (phase, stream);
2027 /* Print information from the combine pass on dump_file. */
2029 void
2030 print_combine_total_stats (void)
2032 if (dump_file)
2033 dump_combine_total_stats (dump_file);
2036 /* Enable RTL dump for all the RTL passes. */
2038 bool
2039 enable_rtl_dump_file (void)
2041 gcc::dump_manager *dumps = g->get_dumps ();
2042 int num_enabled =
2043 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
2044 NULL);
2045 return num_enabled > 0;
2048 #if CHECKING_P
2050 namespace selftest {
2052 /* temp_dump_context's ctor. Temporarily override the dump_context
2053 (to forcibly enable optinfo-generation). */
2055 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
2056 bool forcibly_enable_dumping,
2057 dump_flags_t test_pp_flags)
2058 : m_context (),
2059 m_saved (&dump_context ().get ())
2061 dump_context::s_current = &m_context;
2062 if (forcibly_enable_optinfo)
2063 m_context.set_json_writer (new optrecord_json_writer ());
2064 /* Conditionally enable the test dump, so that we can verify both the
2065 dump_enabled_p and the !dump_enabled_p cases in selftests. */
2066 if (forcibly_enable_dumping)
2068 m_context.m_test_pp = &m_pp;
2069 m_context.m_test_pp_flags = test_pp_flags;
2072 dump_context::get ().refresh_dumps_are_enabled ();
2075 /* temp_dump_context's dtor. Restore the saved dump_context. */
2077 temp_dump_context::~temp_dump_context ()
2079 m_context.set_json_writer (NULL);
2081 dump_context::s_current = m_saved;
2083 dump_context::get ().refresh_dumps_are_enabled ();
2086 /* 0-terminate the text dumped so far, and return it. */
2088 const char *
2089 temp_dump_context::get_dumped_text ()
2091 return pp_formatted_text (&m_pp);
2094 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2095 from EXPECTED_FUNCTION, using LOC for the location of any failure,
2096 provided that the build compiler is sufficiently recent. */
2098 static void
2099 assert_impl_location_eq (const location &loc ATTRIBUTE_UNUSED,
2100 const dump_impl_location_t &impl_loc ATTRIBUTE_UNUSED,
2101 const char *expected_file ATTRIBUTE_UNUSED,
2102 int expected_line ATTRIBUTE_UNUSED,
2103 const char *expected_function ATTRIBUTE_UNUSED)
2105 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2106 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_file, expected_file);
2107 ASSERT_EQ_AT (loc, impl_loc.m_line, expected_line);
2108 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_function, expected_function);
2109 #endif
2112 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2113 from EXPECTED_FUNCTION, provided that the build compiler is
2114 sufficiently recent. */
2116 #define ASSERT_IMPL_LOCATION_EQ(IMPL_LOC, EXPECTED_FILE, EXPECTED_LINE, \
2117 EXPECTED_FUNCTION) \
2118 SELFTEST_BEGIN_STMT \
2119 assert_impl_location_eq (SELFTEST_LOCATION, IMPL_LOC, \
2120 EXPECTED_FILE, EXPECTED_LINE, \
2121 EXPECTED_FUNCTION); \
2122 SELFTEST_END_STMT
2124 /* Verify that the dump_location_t constructors capture the source location
2125 at which they were called (provided that the build compiler is sufficiently
2126 recent). */
2128 static void
2129 test_impl_location ()
2131 /* Default ctor. */
2133 dump_location_t loc;
2134 const int expected_line = __LINE__ - 1;
2135 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2136 "dumpfile.c", expected_line, "test_impl_location");
2139 /* Constructing from a gimple. */
2141 dump_location_t loc ((gimple *)NULL);
2142 const int expected_line = __LINE__ - 1;
2143 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2144 "dumpfile.c", expected_line, "test_impl_location");
2147 /* Constructing from an rtx_insn. */
2149 dump_location_t loc ((rtx_insn *)NULL);
2150 const int expected_line = __LINE__ - 1;
2151 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2152 "dumpfile.c", expected_line, "test_impl_location");
2156 /* Verify that the text dumped so far in CONTEXT equals
2157 EXPECTED_TEXT, using LOC for the location of any failure.
2158 As a side-effect, the internal buffer is 0-terminated. */
2160 void
2161 verify_dumped_text (const location &loc,
2162 temp_dump_context *context,
2163 const char *expected_text)
2165 gcc_assert (context);
2166 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
2167 expected_text);
2170 /* Verify that ITEM has the expected values. */
2172 void
2173 verify_item (const location &loc,
2174 const optinfo_item *item,
2175 enum optinfo_item_kind expected_kind,
2176 location_t expected_location,
2177 const char *expected_text)
2179 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
2180 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
2181 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
2184 /* Verify that calls to the dump_* API are captured and consolidated into
2185 optimization records. */
2187 static void
2188 test_capture_of_dump_calls (const line_table_case &case_)
2190 /* Generate a location_t for testing. */
2191 line_table_test ltt (case_);
2192 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
2193 linemap_line_start (line_table, 5, 100);
2194 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
2195 location_t decl_loc = linemap_position_for_column (line_table, 8);
2196 location_t stmt_loc = linemap_position_for_column (line_table, 10);
2197 if (stmt_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
2198 return;
2200 dump_user_location_t loc = dump_user_location_t::from_location_t (stmt_loc);
2202 gimple *stmt = gimple_build_return (NULL);
2203 gimple_set_location (stmt, stmt_loc);
2205 tree test_decl = build_decl (decl_loc, FUNCTION_DECL,
2206 get_identifier ("test_decl"),
2207 build_function_type_list (void_type_node,
2208 NULL_TREE));
2210 symbol_table_test tmp_symtab;
2212 cgraph_node *node = cgraph_node::get_create (test_decl);
2213 gcc_assert (node);
2215 /* Run all tests twice, with and then without optinfo enabled, to ensure
2216 that immediate destinations vs optinfo-based destinations both
2217 work, independently of each other, with no leaks. */
2218 for (int i = 0 ; i < 2; i++)
2220 bool with_optinfo = (i == 0);
2222 /* Test of dump_printf. */
2224 temp_dump_context tmp (with_optinfo, true,
2225 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2226 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
2227 const int expected_impl_line = __LINE__ - 1;
2229 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
2230 if (with_optinfo)
2232 optinfo *info = tmp.get_pending_optinfo ();
2233 ASSERT_TRUE (info != NULL);
2234 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2235 ASSERT_EQ (info->num_items (), 1);
2236 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
2237 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2238 "dumpfile.c", expected_impl_line,
2239 "test_capture_of_dump_calls");
2243 /* Test of dump_printf with %T. */
2245 temp_dump_context tmp (with_optinfo, true,
2246 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2247 dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
2248 const int expected_impl_line = __LINE__ - 1;
2250 ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
2251 if (with_optinfo)
2253 optinfo *info = tmp.get_pending_optinfo ();
2254 ASSERT_TRUE (info != NULL);
2255 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2256 ASSERT_EQ (info->num_items (), 2);
2257 ASSERT_IS_TEXT (info->get_item (0), "tree: ");
2258 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2259 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2260 "dumpfile.c", expected_impl_line,
2261 "test_capture_of_dump_calls");
2265 /* Test of dump_printf with %E. */
2267 temp_dump_context tmp (with_optinfo, true,
2268 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2269 dump_printf (MSG_NOTE, "gimple: %E", stmt);
2270 const int expected_impl_line = __LINE__ - 1;
2272 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
2273 if (with_optinfo)
2275 optinfo *info = tmp.get_pending_optinfo ();
2276 ASSERT_TRUE (info != NULL);
2277 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2278 ASSERT_EQ (info->num_items (), 2);
2279 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2280 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;");
2281 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2282 "dumpfile.c", expected_impl_line,
2283 "test_capture_of_dump_calls");
2287 /* Test of dump_printf with %G. */
2289 temp_dump_context tmp (with_optinfo, true,
2290 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2291 dump_printf (MSG_NOTE, "gimple: %G", stmt);
2292 const int expected_impl_line = __LINE__ - 1;
2294 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
2295 if (with_optinfo)
2297 optinfo *info = tmp.get_pending_optinfo ();
2298 ASSERT_TRUE (info != NULL);
2299 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2300 ASSERT_EQ (info->num_items (), 2);
2301 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
2302 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;\n");
2303 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2304 "dumpfile.c", expected_impl_line,
2305 "test_capture_of_dump_calls");
2309 /* Test of dump_printf with %C. */
2311 temp_dump_context tmp (with_optinfo, true,
2312 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2313 dump_printf (MSG_NOTE, "node: %C", node);
2314 const int expected_impl_line = __LINE__ - 1;
2316 ASSERT_DUMPED_TEXT_EQ (tmp, "node: test_decl/0");
2317 if (with_optinfo)
2319 optinfo *info = tmp.get_pending_optinfo ();
2320 ASSERT_TRUE (info != NULL);
2321 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2322 ASSERT_EQ (info->num_items (), 2);
2323 ASSERT_IS_TEXT (info->get_item (0), "node: ");
2324 ASSERT_IS_SYMTAB_NODE (info->get_item (1), decl_loc, "test_decl/0");
2325 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2326 "dumpfile.c", expected_impl_line,
2327 "test_capture_of_dump_calls");
2331 /* dump_print_loc with multiple format codes. This tests various
2332 things:
2333 - intermingling of text, format codes handled by the base
2334 pretty_printer, and dump-specific format codes
2335 - multiple dump-specific format codes: some consecutive, others
2336 separated by text, trailing text after the final one. */
2338 temp_dump_context tmp (with_optinfo, true,
2339 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2340 dump_printf_loc (MSG_NOTE, loc, "before %T and %T"
2341 " %i consecutive %E%E after\n",
2342 integer_zero_node, test_decl, 42, stmt, stmt);
2344 ASSERT_DUMPED_TEXT_EQ (tmp,
2345 "test.txt:5:10: note: before 0 and test_decl"
2346 " 42 consecutive return;return; after\n");
2347 if (with_optinfo)
2349 optinfo *info = tmp.get_pending_optinfo ();
2350 ASSERT_TRUE (info != NULL);
2351 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2352 ASSERT_EQ (info->num_items (), 8);
2353 ASSERT_IS_TEXT (info->get_item (0), "before ");
2354 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2355 ASSERT_IS_TEXT (info->get_item (2), " and ");
2356 ASSERT_IS_TREE (info->get_item (3), UNKNOWN_LOCATION, "test_decl");
2357 ASSERT_IS_TEXT (info->get_item (4), " 42 consecutive ");
2358 ASSERT_IS_GIMPLE (info->get_item (5), stmt_loc, "return;");
2359 ASSERT_IS_GIMPLE (info->get_item (6), stmt_loc, "return;");
2360 ASSERT_IS_TEXT (info->get_item (7), " after\n");
2361 /* We don't ASSERT_IMPL_LOCATION_EQ here, to avoid having to
2362 enforce at which exact line the multiline dump_printf_loc
2363 occurred. */
2367 /* Tree, via dump_generic_expr. */
2369 temp_dump_context tmp (with_optinfo, true,
2370 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2371 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
2372 const int expected_impl_line = __LINE__ - 1;
2373 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
2375 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
2376 if (with_optinfo)
2378 optinfo *info = tmp.get_pending_optinfo ();
2379 ASSERT_TRUE (info != NULL);
2380 ASSERT_EQ (info->get_location_t (), stmt_loc);
2381 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2382 ASSERT_EQ (info->num_items (), 2);
2383 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
2384 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2385 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2386 "dumpfile.c", expected_impl_line,
2387 "test_capture_of_dump_calls");
2391 /* Tree, via dump_generic_expr_loc. */
2393 temp_dump_context tmp (with_optinfo, true,
2394 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2395 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
2396 const int expected_impl_line = __LINE__ - 1;
2398 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
2399 if (with_optinfo)
2401 optinfo *info = tmp.get_pending_optinfo ();
2402 ASSERT_TRUE (info != NULL);
2403 ASSERT_EQ (info->get_location_t (), stmt_loc);
2404 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2405 ASSERT_EQ (info->num_items (), 1);
2406 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
2407 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2408 "dumpfile.c", expected_impl_line,
2409 "test_capture_of_dump_calls");
2413 /* Gimple. */
2415 /* dump_gimple_stmt_loc. */
2417 temp_dump_context tmp (with_optinfo, true,
2418 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2419 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2420 const int expected_impl_line = __LINE__ - 1;
2422 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
2423 if (with_optinfo)
2425 optinfo *info = tmp.get_pending_optinfo ();
2426 ASSERT_TRUE (info != NULL);
2427 ASSERT_EQ (info->num_items (), 1);
2428 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
2429 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2430 "dumpfile.c", expected_impl_line,
2431 "test_capture_of_dump_calls");
2435 /* dump_gimple_stmt. */
2437 temp_dump_context tmp (with_optinfo, true,
2438 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2439 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
2440 const int expected_impl_line = __LINE__ - 1;
2442 ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
2443 if (with_optinfo)
2445 optinfo *info = tmp.get_pending_optinfo ();
2446 ASSERT_TRUE (info != NULL);
2447 ASSERT_EQ (info->num_items (), 1);
2448 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
2449 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2450 "dumpfile.c", expected_impl_line,
2451 "test_capture_of_dump_calls");
2455 /* dump_gimple_expr_loc. */
2457 temp_dump_context tmp (with_optinfo, true,
2458 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2459 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
2460 const int expected_impl_line = __LINE__ - 1;
2462 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
2463 if (with_optinfo)
2465 optinfo *info = tmp.get_pending_optinfo ();
2466 ASSERT_TRUE (info != NULL);
2467 ASSERT_EQ (info->num_items (), 1);
2468 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
2469 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2470 "dumpfile.c", expected_impl_line,
2471 "test_capture_of_dump_calls");
2475 /* dump_gimple_expr. */
2477 temp_dump_context tmp (with_optinfo, true,
2478 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2479 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
2480 const int expected_impl_line = __LINE__ - 1;
2482 ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
2483 if (with_optinfo)
2485 optinfo *info = tmp.get_pending_optinfo ();
2486 ASSERT_TRUE (info != NULL);
2487 ASSERT_EQ (info->num_items (), 1);
2488 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
2489 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2490 "dumpfile.c", expected_impl_line,
2491 "test_capture_of_dump_calls");
2496 /* symtab_node. */
2498 temp_dump_context tmp (with_optinfo, true,
2499 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2500 dump_symtab_node (MSG_NOTE, node);
2501 const int expected_impl_line = __LINE__ - 1;
2503 ASSERT_DUMPED_TEXT_EQ (tmp, "test_decl/0");
2504 if (with_optinfo)
2506 optinfo *info = tmp.get_pending_optinfo ();
2507 ASSERT_TRUE (info != NULL);
2508 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2509 ASSERT_EQ (info->num_items (), 1);
2510 ASSERT_IS_SYMTAB_NODE (info->get_item (0), decl_loc, "test_decl/0");
2511 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2512 "dumpfile.c", expected_impl_line,
2513 "test_capture_of_dump_calls");
2517 /* poly_int. */
2519 temp_dump_context tmp (with_optinfo, true,
2520 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2521 dump_dec (MSG_NOTE, poly_int64 (42));
2522 const int expected_impl_line = __LINE__ - 1;
2524 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
2525 if (with_optinfo)
2527 optinfo *info = tmp.get_pending_optinfo ();
2528 ASSERT_TRUE (info != NULL);
2529 ASSERT_EQ (info->num_items (), 1);
2530 ASSERT_IS_TEXT (info->get_item (0), "42");
2531 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2532 "dumpfile.c", expected_impl_line,
2533 "test_capture_of_dump_calls");
2537 /* Scopes. Test with all 4 combinations of
2538 filtering by MSG_PRIORITY_USER_FACING
2539 and/or filtering by MSG_PRIORITY_INTERNALS. */
2540 for (int j = 0; j < 3; j++)
2542 dump_flags_t dump_filter = MSG_ALL_KINDS;
2543 if (j % 2)
2544 dump_filter |= MSG_PRIORITY_USER_FACING;
2545 if (j / 2)
2546 dump_filter |= MSG_PRIORITY_INTERNALS;
2548 temp_dump_context tmp (with_optinfo, true, dump_filter);
2549 /* Emit various messages, mostly with implicit priority. */
2550 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2551 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, stmt,
2552 "explicitly internal msg\n");
2554 AUTO_DUMP_SCOPE ("outer scope", stmt);
2555 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2557 AUTO_DUMP_SCOPE ("middle scope", stmt);
2558 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2560 AUTO_DUMP_SCOPE ("inner scope", stmt);
2561 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
2562 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_USER_FACING, stmt,
2563 "explicitly user-facing msg\n");
2565 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2567 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2569 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2570 const int expected_impl_line = __LINE__ - 1;
2572 switch (dump_filter & MSG_ALL_PRIORITIES)
2574 default:
2575 gcc_unreachable ();
2576 case 0:
2577 ASSERT_DUMPED_TEXT_EQ (tmp, "");
2578 break;
2579 case MSG_PRIORITY_USER_FACING:
2580 ASSERT_DUMPED_TEXT_EQ
2581 (tmp,
2582 "test.txt:5:10: note: msg 1\n"
2583 "test.txt:5:10: note: explicitly user-facing msg\n"
2584 "test.txt:5:10: note: msg 7\n");
2585 break;
2586 case MSG_PRIORITY_INTERNALS:
2587 ASSERT_DUMPED_TEXT_EQ
2588 (tmp,
2589 "test.txt:5:10: note: explicitly internal msg\n"
2590 "test.txt:5:10: note: === outer scope ===\n"
2591 "test.txt:5:10: note: msg 2\n"
2592 "test.txt:5:10: note: === middle scope ===\n"
2593 "test.txt:5:10: note: msg 3\n"
2594 "test.txt:5:10: note: === inner scope ===\n"
2595 "test.txt:5:10: note: msg 4\n"
2596 "test.txt:5:10: note: msg 5\n"
2597 "test.txt:5:10: note: msg 6\n");
2598 break;
2599 case MSG_ALL_PRIORITIES:
2600 ASSERT_DUMPED_TEXT_EQ
2601 (tmp,
2602 "test.txt:5:10: note: msg 1\n"
2603 "test.txt:5:10: note: explicitly internal msg\n"
2604 "test.txt:5:10: note: === outer scope ===\n"
2605 "test.txt:5:10: note: msg 2\n"
2606 "test.txt:5:10: note: === middle scope ===\n"
2607 "test.txt:5:10: note: msg 3\n"
2608 "test.txt:5:10: note: === inner scope ===\n"
2609 "test.txt:5:10: note: msg 4\n"
2610 "test.txt:5:10: note: explicitly user-facing msg\n"
2611 "test.txt:5:10: note: msg 5\n"
2612 "test.txt:5:10: note: msg 6\n"
2613 "test.txt:5:10: note: msg 7\n");
2614 break;
2616 if (with_optinfo)
2618 optinfo *info = tmp.get_pending_optinfo ();
2619 ASSERT_TRUE (info != NULL);
2620 ASSERT_EQ (info->num_items (), 1);
2621 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
2622 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2623 "dumpfile.c", expected_impl_line,
2624 "test_capture_of_dump_calls");
2629 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2630 above. */
2632 /* MSG_OPTIMIZED_LOCATIONS. */
2634 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2635 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
2636 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2637 OPTINFO_KIND_SUCCESS);
2640 /* MSG_MISSED_OPTIMIZATION. */
2642 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
2643 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2644 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2645 OPTINFO_KIND_FAILURE);
2649 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2651 temp_dump_context tmp (false, true,
2652 MSG_OPTIMIZED_LOCATIONS | MSG_ALL_PRIORITIES);
2653 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2655 AUTO_DUMP_SCOPE ("outer scope", stmt);
2656 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2658 AUTO_DUMP_SCOPE ("middle scope", stmt);
2659 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2661 AUTO_DUMP_SCOPE ("inner scope", stmt);
2662 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt, "msg 4\n");
2664 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2666 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2668 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2670 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: optimized: msg 4\n");
2674 static void
2675 test_pr87025 ()
2677 dump_user_location_t loc
2678 = dump_user_location_t::from_location_t (UNKNOWN_LOCATION);
2680 temp_dump_context tmp (true, true,
2681 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2683 AUTO_DUMP_SCOPE ("outer scope", loc);
2684 dump_printf (MSG_NOTE, "msg1\n");
2688 /* Run all of the selftests within this file. */
2690 void
2691 dumpfile_c_tests ()
2693 test_impl_location ();
2694 for_each_line_table_case (test_capture_of_dump_calls);
2695 test_pr87025 ();
2698 } // namespace selftest
2700 #endif /* CHECKING_P */