2018-08-01 Jan Willem Jagersma <jwjagersma@gmail.com>
[official-gcc.git] / gcc / dumpfile.c
blob10e9cabc6fa5793c91cce68735f7988149bbb74f
1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "options.h"
24 #include "tree.h"
25 #include "gimple-pretty-print.h"
26 #include "diagnostic-core.h"
27 #include "dumpfile.h"
28 #include "context.h"
29 #include "profile-count.h"
30 #include "tree-cfg.h"
31 #include "langhooks.h"
32 #include "backend.h" /* for gimple.h. */
33 #include "gimple.h" /* for dump_user_location_t ctor. */
34 #include "rtl.h" /* for dump_user_location_t ctor. */
35 #include "selftest.h"
36 #include "optinfo.h"
37 #include "dump-context.h"
38 #include "cgraph.h"
39 #include "tree-pass.h" /* for "current_pass". */
40 #include "optinfo-emit-json.h"
42 /* If non-NULL, return one past-the-end of the matching SUBPART of
43 the WHOLE string. */
44 #define skip_leading_substring(whole, part) \
45 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
47 static dump_flags_t pflags; /* current dump_flags */
49 static void dump_loc (dump_flags_t, FILE *, source_location);
51 /* Current -fopt-info output stream, if any, and flags. */
52 static FILE *alt_dump_file = NULL;
53 static dump_flags_t alt_flags;
55 static FILE *dump_open_alternate_stream (struct dump_file_info *);
57 /* These are currently used for communicating between passes.
58 However, instead of accessing them directly, the passes can use
59 dump_printf () for dumps. */
60 FILE *dump_file = NULL;
61 const char *dump_file_name;
62 dump_flags_t dump_flags;
63 bool dumps_are_enabled = false;
66 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
67 global. */
69 void
70 set_dump_file (FILE *new_dump_file)
72 dumpfile_ensure_any_optinfo_are_flushed ();
73 dump_file = new_dump_file;
74 dump_context::get ().refresh_dumps_are_enabled ();
77 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
78 global. */
80 static void
81 set_alt_dump_file (FILE *new_alt_dump_file)
83 dumpfile_ensure_any_optinfo_are_flushed ();
84 alt_dump_file = new_alt_dump_file;
85 dump_context::get ().refresh_dumps_are_enabled ();
88 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
89 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
90 OPTGROUP_NONE, 0, 0, num, false, false}
92 /* Table of tree dump switches. This must be consistent with the
93 TREE_DUMP_INDEX enumeration in dumpfile.h. */
94 static struct dump_file_info dump_files[TDI_end] =
96 DUMP_FILE_INFO (NULL, NULL, DK_none, 0),
97 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa, 0),
98 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa, 0),
99 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa, 0),
100 DUMP_FILE_INFO (".original", "tree-original", DK_tree, 0),
101 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree, 0),
102 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree, 0),
103 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa, 0),
104 #define FIRST_AUTO_NUMBERED_DUMP 1
105 #define FIRST_ME_AUTO_NUMBERED_DUMP 4
107 DUMP_FILE_INFO (NULL, "lang-all", DK_lang, 0),
108 DUMP_FILE_INFO (NULL, "tree-all", DK_tree, 0),
109 DUMP_FILE_INFO (NULL, "rtl-all", DK_rtl, 0),
110 DUMP_FILE_INFO (NULL, "ipa-all", DK_ipa, 0),
113 /* Table of dump options. This must be consistent with the TDF_* flags
114 in dumpfile.h and opt_info_options below. */
115 static const kv_pair<dump_flags_t> dump_options[] =
117 {"address", TDF_ADDRESS},
118 {"asmname", TDF_ASMNAME},
119 {"slim", TDF_SLIM},
120 {"raw", TDF_RAW},
121 {"graph", TDF_GRAPH},
122 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
123 | MSG_MISSED_OPTIMIZATION
124 | MSG_NOTE)},
125 {"cselib", TDF_CSELIB},
126 {"stats", TDF_STATS},
127 {"blocks", TDF_BLOCKS},
128 {"vops", TDF_VOPS},
129 {"lineno", TDF_LINENO},
130 {"uid", TDF_UID},
131 {"stmtaddr", TDF_STMTADDR},
132 {"memsyms", TDF_MEMSYMS},
133 {"eh", TDF_EH},
134 {"alias", TDF_ALIAS},
135 {"nouid", TDF_NOUID},
136 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
137 {"scev", TDF_SCEV},
138 {"gimple", TDF_GIMPLE},
139 {"folding", TDF_FOLDING},
140 {"optimized", MSG_OPTIMIZED_LOCATIONS},
141 {"missed", MSG_MISSED_OPTIMIZATION},
142 {"note", MSG_NOTE},
143 {"optall", MSG_ALL},
144 {"all", dump_flags_t (TDF_ALL_VALUES
145 & ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
146 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
147 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
148 {NULL, TDF_NONE}
151 /* A subset of the dump_options table which is used for -fopt-info
152 types. This must be consistent with the MSG_* flags in dumpfile.h.
154 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
156 {"optimized", MSG_OPTIMIZED_LOCATIONS},
157 {"missed", MSG_MISSED_OPTIMIZATION},
158 {"note", MSG_NOTE},
159 {"all", MSG_ALL},
160 {NULL, TDF_NONE}
163 /* Flags used for -fopt-info groups. */
164 const kv_pair<optgroup_flags_t> optgroup_options[] =
166 {"ipa", OPTGROUP_IPA},
167 {"loop", OPTGROUP_LOOP},
168 {"inline", OPTGROUP_INLINE},
169 {"omp", OPTGROUP_OMP},
170 {"vec", OPTGROUP_VEC},
171 {"optall", OPTGROUP_ALL},
172 {NULL, OPTGROUP_NONE}
175 gcc::dump_manager::dump_manager ():
176 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
177 m_extra_dump_files (NULL),
178 m_extra_dump_files_in_use (0),
179 m_extra_dump_files_alloced (0)
183 gcc::dump_manager::~dump_manager ()
185 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
187 dump_file_info *dfi = &m_extra_dump_files[i];
188 /* suffix, swtch, glob are statically allocated for the entries
189 in dump_files, and for statistics, but are dynamically allocated
190 for those for passes. */
191 if (dfi->owns_strings)
193 XDELETEVEC (const_cast <char *> (dfi->suffix));
194 XDELETEVEC (const_cast <char *> (dfi->swtch));
195 XDELETEVEC (const_cast <char *> (dfi->glob));
197 /* These, if non-NULL, are always dynamically allocated. */
198 XDELETEVEC (const_cast <char *> (dfi->pfilename));
199 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
201 XDELETEVEC (m_extra_dump_files);
204 unsigned int
205 gcc::dump_manager::
206 dump_register (const char *suffix, const char *swtch, const char *glob,
207 dump_kind dkind, optgroup_flags_t optgroup_flags,
208 bool take_ownership)
210 int num = m_next_dump++;
212 size_t count = m_extra_dump_files_in_use++;
214 if (count >= m_extra_dump_files_alloced)
216 if (m_extra_dump_files_alloced == 0)
217 m_extra_dump_files_alloced = 512;
218 else
219 m_extra_dump_files_alloced *= 2;
220 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
221 m_extra_dump_files,
222 m_extra_dump_files_alloced);
224 /* Construct a new object in the space allocated above. */
225 new (m_extra_dump_files + count) dump_file_info ();
227 else
229 /* Zero out the already constructed object. */
230 m_extra_dump_files[count] = dump_file_info ();
233 m_extra_dump_files[count].suffix = suffix;
234 m_extra_dump_files[count].swtch = swtch;
235 m_extra_dump_files[count].glob = glob;
236 m_extra_dump_files[count].dkind = dkind;
237 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
238 m_extra_dump_files[count].num = num;
239 m_extra_dump_files[count].owns_strings = take_ownership;
241 return count + TDI_end;
245 /* Allow languages and middle-end to register their dumps before the
246 optimization passes. */
248 void
249 gcc::dump_manager::
250 register_dumps ()
252 lang_hooks.register_dumps (this);
253 /* If this assert fails, some FE registered more than
254 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
255 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
256 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
257 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
258 dump_files[TDI_original].num = m_next_dump++;
259 dump_files[TDI_gimple].num = m_next_dump++;
260 dump_files[TDI_nested].num = m_next_dump++;
264 /* Return the dump_file_info for the given phase. */
266 struct dump_file_info *
267 gcc::dump_manager::
268 get_dump_file_info (int phase) const
270 if (phase < TDI_end)
271 return &dump_files[phase];
272 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
273 return NULL;
274 else
275 return m_extra_dump_files + (phase - TDI_end);
278 /* Locate the dump_file_info with swtch equal to SWTCH,
279 or return NULL if no such dump_file_info exists. */
281 struct dump_file_info *
282 gcc::dump_manager::
283 get_dump_file_info_by_switch (const char *swtch) const
285 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
286 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
287 return &m_extra_dump_files[i];
289 /* Not found. */
290 return NULL;
294 /* Return the name of the dump file for the given phase.
295 The caller is responsible for calling free on the returned
296 buffer.
297 If the dump is not enabled, returns NULL. */
299 char *
300 gcc::dump_manager::
301 get_dump_file_name (int phase, int part) const
303 struct dump_file_info *dfi;
305 if (phase == TDI_none)
306 return NULL;
308 dfi = get_dump_file_info (phase);
310 return get_dump_file_name (dfi, part);
313 /* Return the name of the dump file for the given dump_file_info.
314 The caller is responsible for calling free on the returned
315 buffer.
316 If the dump is not enabled, returns NULL. */
318 char *
319 gcc::dump_manager::
320 get_dump_file_name (struct dump_file_info *dfi, int part) const
322 char dump_id[10];
324 gcc_assert (dfi);
326 if (dfi->pstate == 0)
327 return NULL;
329 /* If available, use the command line dump filename. */
330 if (dfi->pfilename)
331 return xstrdup (dfi->pfilename);
333 if (dfi->num < 0)
334 dump_id[0] = '\0';
335 else
337 /* (null), LANG, TREE, RTL, IPA. */
338 char suffix = " ltri"[dfi->dkind];
340 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
341 dump_id[0] = '\0';
344 if (part != -1)
346 char part_id[8];
347 snprintf (part_id, sizeof (part_id), ".%i", part);
348 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
350 else
351 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
354 /* Open a dump file called FILENAME. Some filenames are special and
355 refer to the standard streams. TRUNC indicates whether this is the
356 first open (so the file should be truncated, rather than appended).
357 An error message is emitted in the event of failure. */
359 static FILE *
360 dump_open (const char *filename, bool trunc)
362 if (strcmp ("stderr", filename) == 0)
363 return stderr;
365 if (strcmp ("stdout", filename) == 0
366 || strcmp ("-", filename) == 0)
367 return stdout;
369 FILE *stream = fopen (filename, trunc ? "w" : "a");
371 if (!stream)
372 error ("could not open dump file %qs: %m", filename);
373 return stream;
376 /* For a given DFI, open an alternate dump filename (which could also
377 be a standard stream such as stdout/stderr). If the alternate dump
378 file cannot be opened, return NULL. */
380 static FILE *
381 dump_open_alternate_stream (struct dump_file_info *dfi)
383 if (!dfi->alt_filename)
384 return NULL;
386 if (dfi->alt_stream)
387 return dfi->alt_stream;
389 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
391 if (stream)
392 dfi->alt_state = 1;
394 return stream;
397 /* Construct a dump_user_location_t from STMT (using its location and
398 hotness). */
400 dump_user_location_t::dump_user_location_t (gimple *stmt)
401 : m_count (), m_loc (UNKNOWN_LOCATION)
403 if (stmt)
405 if (stmt->bb)
406 m_count = stmt->bb->count;
407 m_loc = gimple_location (stmt);
411 /* Construct a dump_user_location_t from an RTL instruction (using its
412 location and hotness). */
414 dump_user_location_t::dump_user_location_t (rtx_insn *insn)
415 : m_count (), m_loc (UNKNOWN_LOCATION)
417 if (insn)
419 basic_block bb = BLOCK_FOR_INSN (insn);
420 if (bb)
421 m_count = bb->count;
422 m_loc = INSN_LOCATION (insn);
426 /* Construct from a function declaration. This one requires spelling out
427 to avoid accidentally constructing from other kinds of tree. */
429 dump_user_location_t
430 dump_user_location_t::from_function_decl (tree fndecl)
432 gcc_assert (fndecl);
434 // FIXME: profile count for function?
435 return dump_user_location_t (profile_count (),
436 DECL_SOURCE_LOCATION (fndecl));
439 /* Print source location on DFILE if enabled. */
441 static void
442 dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
444 if (dump_kind)
446 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
447 fprintf (dfile, "%s:%d:%d: note: ", LOCATION_FILE (loc),
448 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
449 else if (current_function_decl)
450 fprintf (dfile, "%s:%d:%d: note: ",
451 DECL_SOURCE_FILE (current_function_decl),
452 DECL_SOURCE_LINE (current_function_decl),
453 DECL_SOURCE_COLUMN (current_function_decl));
454 /* Indentation based on scope depth. */
455 fprintf (dfile, "%*s", get_dump_scope_depth (), "");
459 /* Print source location to PP if enabled. */
461 static void
462 dump_loc (dump_flags_t dump_kind, pretty_printer *pp, source_location loc)
464 if (dump_kind)
466 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
467 pp_printf (pp, "%s:%d:%d: note: ", LOCATION_FILE (loc),
468 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
469 else if (current_function_decl)
470 pp_printf (pp, "%s:%d:%d: note: ",
471 DECL_SOURCE_FILE (current_function_decl),
472 DECL_SOURCE_LINE (current_function_decl),
473 DECL_SOURCE_COLUMN (current_function_decl));
474 /* Indentation based on scope depth. */
475 for (unsigned i = 0; i < get_dump_scope_depth (); i++)
476 pp_character (pp, ' ');
480 /* Implementation of dump_context member functions. */
482 /* dump_context's dtor. */
484 dump_context::~dump_context ()
486 delete m_pending;
489 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
490 or alt_dump_file change, or when changing dump_context in selftests. */
492 void
493 dump_context::refresh_dumps_are_enabled ()
495 dumps_are_enabled = (dump_file || alt_dump_file || optinfo_enabled_p ()
496 || m_test_pp);
499 /* Print LOC to the appropriate dump destinations, given DUMP_KIND.
500 If optinfos are enabled, begin a new optinfo. */
502 void
503 dump_context::dump_loc (dump_flags_t dump_kind, const dump_location_t &loc)
505 end_any_optinfo ();
507 location_t srcloc = loc.get_location_t ();
509 if (dump_file && (dump_kind & pflags))
510 ::dump_loc (dump_kind, dump_file, srcloc);
512 if (alt_dump_file && (dump_kind & alt_flags))
513 ::dump_loc (dump_kind, alt_dump_file, srcloc);
515 /* Support for temp_dump_context in selftests. */
516 if (m_test_pp && (dump_kind & m_test_pp_flags))
517 ::dump_loc (dump_kind, m_test_pp, srcloc);
519 if (optinfo_enabled_p ())
521 optinfo &info = begin_next_optinfo (loc);
522 info.handle_dump_file_kind (dump_kind);
526 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
528 static optinfo_item *
529 make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
531 pretty_printer pp;
532 pp_needs_newline (&pp) = true;
533 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
534 pp_newline (&pp);
536 optinfo_item *item
537 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
538 xstrdup (pp_formatted_text (&pp)));
539 return item;
542 /* Dump gimple statement GS with SPC indentation spaces and
543 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
545 void
546 dump_context::dump_gimple_stmt (dump_flags_t dump_kind,
547 dump_flags_t extra_dump_flags,
548 gimple *gs, int spc)
550 optinfo_item *item
551 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
552 emit_item (item, dump_kind);
554 if (optinfo_enabled_p ())
556 optinfo &info = ensure_pending_optinfo ();
557 info.handle_dump_file_kind (dump_kind);
558 info.add_item (item);
560 else
561 delete item;
564 /* Similar to dump_gimple_stmt, except additionally print source location. */
566 void
567 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind,
568 const dump_location_t &loc,
569 dump_flags_t extra_dump_flags,
570 gimple *gs, int spc)
572 dump_loc (dump_kind, loc);
573 dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
576 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
578 static optinfo_item *
579 make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
581 dump_flags |= TDF_RHS_ONLY;
582 pretty_printer pp;
583 pp_needs_newline (&pp) = true;
584 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
586 optinfo_item *item
587 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
588 xstrdup (pp_formatted_text (&pp)));
589 return item;
592 /* Dump gimple statement GS with SPC indentation spaces and
593 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
594 Do not terminate with a newline or semicolon. */
596 void
597 dump_context::dump_gimple_expr (dump_flags_t dump_kind,
598 dump_flags_t extra_dump_flags,
599 gimple *gs, int spc)
601 optinfo_item *item
602 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
603 emit_item (item, dump_kind);
605 if (optinfo_enabled_p ())
607 optinfo &info = ensure_pending_optinfo ();
608 info.handle_dump_file_kind (dump_kind);
609 info.add_item (item);
611 else
612 delete item;
615 /* Similar to dump_gimple_expr, except additionally print source location. */
617 void
618 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind,
619 const dump_location_t &loc,
620 dump_flags_t extra_dump_flags,
621 gimple *gs,
622 int spc)
624 dump_loc (dump_kind, loc);
625 dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
628 /* Make an item for the given dump call, equivalent to print_generic_expr. */
630 static optinfo_item *
631 make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
633 pretty_printer pp;
634 pp_needs_newline (&pp) = true;
635 pp_translate_identifiers (&pp) = false;
636 dump_generic_node (&pp, node, 0, dump_flags, false);
638 location_t loc = UNKNOWN_LOCATION;
639 if (EXPR_HAS_LOCATION (node))
640 loc = EXPR_LOCATION (node);
642 optinfo_item *item
643 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
644 xstrdup (pp_formatted_text (&pp)));
645 return item;
648 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
649 DUMP_KIND is enabled. */
651 void
652 dump_context::dump_generic_expr (dump_flags_t dump_kind,
653 dump_flags_t extra_dump_flags,
654 tree t)
656 optinfo_item *item
657 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
658 emit_item (item, dump_kind);
660 if (optinfo_enabled_p ())
662 optinfo &info = ensure_pending_optinfo ();
663 info.handle_dump_file_kind (dump_kind);
664 info.add_item (item);
666 else
667 delete item;
671 /* Similar to dump_generic_expr, except additionally print the source
672 location. */
674 void
675 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind,
676 const dump_location_t &loc,
677 dump_flags_t extra_dump_flags,
678 tree t)
680 dump_loc (dump_kind, loc);
681 dump_generic_expr (dump_kind, extra_dump_flags, t);
684 /* Make an item for the given dump call. */
686 static optinfo_item *
687 make_item_for_dump_printf_va (const char *format, va_list ap)
688 ATTRIBUTE_PRINTF (1, 0);
690 static optinfo_item *
691 make_item_for_dump_printf_va (const char *format, va_list ap)
693 char *formatted_text = xvasprintf (format, ap);
694 optinfo_item *item
695 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
696 formatted_text);
697 return item;
700 /* Make an item for the given dump call. */
702 static optinfo_item *
703 make_item_for_dump_printf (const char *format, ...)
704 ATTRIBUTE_PRINTF (1, 2);
706 static optinfo_item *
707 make_item_for_dump_printf (const char *format, ...)
709 va_list ap;
710 va_start (ap, format);
711 optinfo_item *item
712 = make_item_for_dump_printf_va (format, ap);
713 va_end (ap);
714 return item;
717 /* Output a formatted message using FORMAT on appropriate dump streams. */
719 void
720 dump_context::dump_printf_va (dump_flags_t dump_kind, const char *format,
721 va_list ap)
723 optinfo_item *item = make_item_for_dump_printf_va (format, ap);
724 emit_item (item, dump_kind);
726 if (optinfo_enabled_p ())
728 optinfo &info = ensure_pending_optinfo ();
729 info.handle_dump_file_kind (dump_kind);
730 info.add_item (item);
732 else
733 delete item;
736 /* Similar to dump_printf, except source location is also printed, and
737 dump location captured. */
739 void
740 dump_context::dump_printf_loc_va (dump_flags_t dump_kind,
741 const dump_location_t &loc,
742 const char *format, va_list ap)
744 dump_loc (dump_kind, loc);
745 dump_printf_va (dump_kind, format, ap);
748 /* Make an item for the given dump call, equivalent to print_dec. */
750 template<unsigned int N, typename C>
751 static optinfo_item *
752 make_item_for_dump_dec (const poly_int<N, C> &value)
754 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
755 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
757 pretty_printer pp;
759 if (value.is_constant ())
760 pp_wide_int (&pp, value.coeffs[0], sgn);
761 else
763 pp_character (&pp, '[');
764 for (unsigned int i = 0; i < N; ++i)
766 pp_wide_int (&pp, value.coeffs[i], sgn);
767 pp_character (&pp, i == N - 1 ? ']' : ',');
771 optinfo_item *item
772 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
773 xstrdup (pp_formatted_text (&pp)));
774 return item;
777 /* Output VALUE in decimal to appropriate dump streams. */
779 template<unsigned int N, typename C>
780 void
781 dump_context::dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
783 optinfo_item *item = make_item_for_dump_dec (value);
784 emit_item (item, dump_kind);
786 if (optinfo_enabled_p ())
788 optinfo &info = ensure_pending_optinfo ();
789 info.handle_dump_file_kind (dump_kind);
790 info.add_item (item);
792 else
793 delete item;
796 /* Make an item for the given dump call. */
798 static optinfo_item *
799 make_item_for_dump_symtab_node (symtab_node *node)
801 location_t loc = DECL_SOURCE_LOCATION (node->decl);
802 optinfo_item *item
803 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
804 xstrdup (node->dump_name ()));
805 return item;
808 /* Output the name of NODE on appropriate dump streams. */
810 void
811 dump_context::dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
813 optinfo_item *item = make_item_for_dump_symtab_node (node);
814 emit_item (item, dump_kind);
816 if (optinfo_enabled_p ())
818 optinfo &info = ensure_pending_optinfo ();
819 info.handle_dump_file_kind (dump_kind);
820 info.add_item (item);
822 else
823 delete item;
826 /* Get the current dump scope-nesting depth.
827 For use by -fopt-info (for showing nesting via indentation). */
829 unsigned int
830 dump_context::get_scope_depth () const
832 return m_scope_depth;
835 /* Push a nested dump scope.
836 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
837 destination, if any.
838 Emit a "scope" optinfo if optinfos are enabled.
839 Increment the scope depth. */
841 void
842 dump_context::begin_scope (const char *name, const dump_location_t &loc)
844 if (dump_file)
845 ::dump_loc (MSG_NOTE, dump_file, loc.get_location_t ());
847 if (alt_dump_file)
848 ::dump_loc (MSG_NOTE, alt_dump_file, loc.get_location_t ());
850 /* Support for temp_dump_context in selftests. */
851 if (m_test_pp)
852 ::dump_loc (MSG_NOTE, m_test_pp, loc.get_location_t ());
854 optinfo_item *item = make_item_for_dump_printf ("=== %s ===\n", name);
855 emit_item (item, MSG_NOTE);
857 if (optinfo_enabled_p ())
859 optinfo &info = begin_next_optinfo (loc);
860 info.m_kind = OPTINFO_KIND_SCOPE;
861 info.add_item (item);
862 end_any_optinfo ();
864 else
865 delete item;
867 m_scope_depth++;
870 /* Pop a nested dump scope. */
872 void
873 dump_context::end_scope ()
875 end_any_optinfo ();
876 m_scope_depth--;
877 optimization_records_maybe_pop_dump_scope ();
880 /* Return the optinfo currently being accumulated, creating one if
881 necessary. */
883 optinfo &
884 dump_context::ensure_pending_optinfo ()
886 if (!m_pending)
887 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
888 return *m_pending;
891 /* Start a new optinfo and return it, ending any optinfo that was already
892 accumulated. */
894 optinfo &
895 dump_context::begin_next_optinfo (const dump_location_t &loc)
897 end_any_optinfo ();
898 gcc_assert (m_pending == NULL);
899 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
900 return *m_pending;
903 /* End any optinfo that has been accumulated within this context; emitting
904 it to any destinations as appropriate, such as optimization records. */
906 void
907 dump_context::end_any_optinfo ()
909 if (m_pending)
910 m_pending->emit ();
911 delete m_pending;
912 m_pending = NULL;
915 /* Emit ITEM to all item destinations (those that don't require
916 consolidation into optinfo instances). */
918 void
919 dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
921 if (dump_file && (dump_kind & pflags))
922 fprintf (dump_file, "%s", item->get_text ());
924 if (alt_dump_file && (dump_kind & alt_flags))
925 fprintf (alt_dump_file, "%s", item->get_text ());
927 /* Support for temp_dump_context in selftests. */
928 if (m_test_pp && (dump_kind & m_test_pp_flags))
929 pp_string (m_test_pp, item->get_text ());
932 /* The current singleton dump_context, and its default. */
934 dump_context *dump_context::s_current = &dump_context::s_default;
935 dump_context dump_context::s_default;
937 /* Implementation of dump_* API calls, calling into dump_context
938 member functions. */
940 /* Dump gimple statement GS with SPC indentation spaces and
941 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
943 void
944 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
945 gimple *gs, int spc)
947 dump_context::get ().dump_gimple_stmt (dump_kind, extra_dump_flags, gs, spc);
950 /* Similar to dump_gimple_stmt, except additionally print source location. */
952 void
953 dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
954 dump_flags_t extra_dump_flags, gimple *gs, int spc)
956 dump_context::get ().dump_gimple_stmt_loc (dump_kind, loc, extra_dump_flags,
957 gs, spc);
960 /* Dump gimple statement GS with SPC indentation spaces and
961 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
962 Do not terminate with a newline or semicolon. */
964 void
965 dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
966 gimple *gs, int spc)
968 dump_context::get ().dump_gimple_expr (dump_kind, extra_dump_flags, gs, spc);
971 /* Similar to dump_gimple_expr, except additionally print source location. */
973 void
974 dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
975 dump_flags_t extra_dump_flags, gimple *gs, int spc)
977 dump_context::get ().dump_gimple_expr_loc (dump_kind, loc, extra_dump_flags,
978 gs, spc);
981 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
982 DUMP_KIND is enabled. */
984 void
985 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
986 tree t)
988 dump_context::get ().dump_generic_expr (dump_kind, extra_dump_flags, t);
991 /* Similar to dump_generic_expr, except additionally print the source
992 location. */
994 void
995 dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
996 dump_flags_t extra_dump_flags, tree t)
998 dump_context::get ().dump_generic_expr_loc (dump_kind, loc, extra_dump_flags,
1002 /* Output a formatted message using FORMAT on appropriate dump streams. */
1004 void
1005 dump_printf (dump_flags_t dump_kind, const char *format, ...)
1007 va_list ap;
1008 va_start (ap, format);
1009 dump_context::get ().dump_printf_va (dump_kind, format, ap);
1010 va_end (ap);
1013 /* Similar to dump_printf, except source location is also printed, and
1014 dump location captured. */
1016 void
1017 dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
1018 const char *format, ...)
1020 va_list ap;
1021 va_start (ap, format);
1022 dump_context::get ().dump_printf_loc_va (dump_kind, loc, format, ap);
1023 va_end (ap);
1026 /* Output VALUE in decimal to appropriate dump streams. */
1028 template<unsigned int N, typename C>
1029 void
1030 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
1032 dump_context::get ().dump_dec (dump_kind, value);
1035 template void dump_dec (dump_flags_t, const poly_uint16 &);
1036 template void dump_dec (dump_flags_t, const poly_int64 &);
1037 template void dump_dec (dump_flags_t, const poly_uint64 &);
1038 template void dump_dec (dump_flags_t, const poly_offset_int &);
1039 template void dump_dec (dump_flags_t, const poly_widest_int &);
1041 void
1042 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1044 if (dump_file && (dump_kind & pflags))
1045 print_dec (value, dump_file, sgn);
1047 if (alt_dump_file && (dump_kind & alt_flags))
1048 print_dec (value, alt_dump_file, sgn);
1051 /* Output VALUE in hexadecimal to appropriate dump streams. */
1053 void
1054 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1056 if (dump_file && (dump_kind & pflags))
1057 print_hex (value, dump_file);
1059 if (alt_dump_file && (dump_kind & alt_flags))
1060 print_hex (value, alt_dump_file);
1063 /* Emit and delete the currently pending optinfo, if there is one,
1064 without the caller needing to know about class dump_context. */
1066 void
1067 dumpfile_ensure_any_optinfo_are_flushed ()
1069 dump_context::get().end_any_optinfo ();
1072 /* Output the name of NODE on appropriate dump streams. */
1074 void
1075 dump_symtab_node (dump_flags_t dump_kind, symtab_node *node)
1077 dump_context::get ().dump_symtab_node (dump_kind, node);
1080 /* Get the current dump scope-nesting depth.
1081 For use by -fopt-info (for showing nesting via indentation). */
1083 unsigned int
1084 get_dump_scope_depth ()
1086 return dump_context::get ().get_scope_depth ();
1089 /* Push a nested dump scope.
1090 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1091 destination, if any.
1092 Emit a "scope" opinfo if optinfos are enabled.
1093 Increment the scope depth. */
1095 void
1096 dump_begin_scope (const char *name, const dump_location_t &loc)
1098 dump_context::get ().begin_scope (name, loc);
1101 /* Pop a nested dump scope. */
1103 void
1104 dump_end_scope ()
1106 dump_context::get ().end_scope ();
1109 /* Start a dump for PHASE. Store user-supplied dump flags in
1110 *FLAG_PTR. Return the number of streams opened. Set globals
1111 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1112 set dump_flags appropriately for both pass dump stream and
1113 -fopt-info stream. */
1116 gcc::dump_manager::
1117 dump_start (int phase, dump_flags_t *flag_ptr)
1119 int count = 0;
1120 char *name;
1121 struct dump_file_info *dfi;
1122 FILE *stream;
1123 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1124 return 0;
1126 dfi = get_dump_file_info (phase);
1127 name = get_dump_file_name (phase);
1128 if (name)
1130 stream = dump_open (name, dfi->pstate < 0);
1131 if (stream)
1133 dfi->pstate = 1;
1134 count++;
1136 free (name);
1137 dfi->pstream = stream;
1138 set_dump_file (dfi->pstream);
1139 /* Initialize current dump flags. */
1140 pflags = dfi->pflags;
1143 stream = dump_open_alternate_stream (dfi);
1144 if (stream)
1146 dfi->alt_stream = stream;
1147 count++;
1148 set_alt_dump_file (dfi->alt_stream);
1149 /* Initialize current -fopt-info flags. */
1150 alt_flags = dfi->alt_flags;
1153 if (flag_ptr)
1154 *flag_ptr = dfi->pflags;
1156 return count;
1159 /* Finish a tree dump for PHASE and close associated dump streams. Also
1160 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1162 void
1163 gcc::dump_manager::
1164 dump_finish (int phase)
1166 struct dump_file_info *dfi;
1168 if (phase < 0)
1169 return;
1170 dfi = get_dump_file_info (phase);
1171 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
1172 fclose (dfi->pstream);
1174 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
1175 fclose (dfi->alt_stream);
1177 dfi->alt_stream = NULL;
1178 dfi->pstream = NULL;
1179 set_dump_file (NULL);
1180 set_alt_dump_file (NULL);
1181 dump_flags = TDF_NONE;
1182 alt_flags = TDF_NONE;
1183 pflags = TDF_NONE;
1186 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1187 *FLAG_PTR and returns a stream to write to. If the dump is not
1188 enabled, returns NULL.
1189 PART can be used for dump files which should be split to multiple
1190 parts. PART == -1 indicates dump file with no parts.
1191 If PART is -1, multiple calls will reopen and append to the dump file. */
1193 FILE *
1194 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1196 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
1199 FILE *
1200 gcc::dump_manager::
1201 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
1203 char *name;
1204 struct dump_file_info *dfi;
1205 FILE *stream;
1207 if (phase == TDI_none || !dump_phase_enabled_p (phase))
1208 return NULL;
1210 name = get_dump_file_name (phase, part);
1211 if (!name)
1212 return NULL;
1213 dfi = get_dump_file_info (phase);
1215 /* We do not support re-opening of dump files with parts. This would require
1216 tracking pstate per part of the dump file. */
1217 stream = dump_open (name, part != -1 || dfi->pstate < 0);
1218 if (stream)
1219 dfi->pstate = 1;
1220 free (name);
1222 if (flag_ptr)
1223 *flag_ptr = dfi->pflags;
1225 /* Initialize current flags */
1226 pflags = dfi->pflags;
1227 return stream;
1230 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1231 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1232 any phase. */
1235 gcc::dump_manager::
1236 dump_phase_enabled_p (int phase) const
1238 if (phase == TDI_tree_all)
1240 size_t i;
1241 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1242 if (dump_files[i].pstate || dump_files[i].alt_state)
1243 return 1;
1244 for (i = 0; i < m_extra_dump_files_in_use; i++)
1245 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
1246 return 1;
1247 return 0;
1249 else
1251 struct dump_file_info *dfi = get_dump_file_info (phase);
1252 return dfi->pstate || dfi->alt_state;
1256 /* Returns nonzero if tree dump PHASE has been initialized. */
1259 gcc::dump_manager::
1260 dump_initialized_p (int phase) const
1262 struct dump_file_info *dfi = get_dump_file_info (phase);
1263 return dfi->pstate > 0 || dfi->alt_state > 0;
1266 /* Returns the switch name of PHASE. */
1268 const char *
1269 dump_flag_name (int phase)
1271 return g->get_dumps ()->dump_flag_name (phase);
1274 const char *
1275 gcc::dump_manager::
1276 dump_flag_name (int phase) const
1278 struct dump_file_info *dfi = get_dump_file_info (phase);
1279 return dfi->swtch;
1282 /* Finish a tree dump for PHASE. STREAM is the stream created by
1283 dump_begin. */
1285 void
1286 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1288 if (stream != stderr && stream != stdout)
1289 fclose (stream);
1292 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1293 enabled tree dumps. */
1296 gcc::dump_manager::
1297 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
1299 int n = 0;
1300 size_t i;
1302 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1304 if ((dump_files[i].dkind == dkind))
1306 const char *old_filename = dump_files[i].pfilename;
1307 dump_files[i].pstate = -1;
1308 dump_files[i].pflags |= flags;
1309 n++;
1310 /* Override the existing filename. */
1311 if (filename)
1313 dump_files[i].pfilename = xstrdup (filename);
1314 /* Since it is a command-line provided file, which is
1315 common to all the phases, use it in append mode. */
1316 dump_files[i].pstate = 1;
1318 if (old_filename && filename != old_filename)
1319 free (CONST_CAST (char *, old_filename));
1323 for (i = 0; i < m_extra_dump_files_in_use; i++)
1325 if ((m_extra_dump_files[i].dkind == dkind))
1327 const char *old_filename = m_extra_dump_files[i].pfilename;
1328 m_extra_dump_files[i].pstate = -1;
1329 m_extra_dump_files[i].pflags |= flags;
1330 n++;
1331 /* Override the existing filename. */
1332 if (filename)
1334 m_extra_dump_files[i].pfilename = xstrdup (filename);
1335 /* Since it is a command-line provided file, which is
1336 common to all the phases, use it in append mode. */
1337 m_extra_dump_files[i].pstate = 1;
1339 if (old_filename && filename != old_filename)
1340 free (CONST_CAST (char *, old_filename));
1344 return n;
1347 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1348 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1349 dumps. */
1352 gcc::dump_manager::
1353 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1354 const char *filename)
1356 int n = 0;
1357 size_t i;
1359 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1361 if ((dump_files[i].optgroup_flags & optgroup_flags))
1363 const char *old_filename = dump_files[i].alt_filename;
1364 /* Since this file is shared among different passes, it
1365 should be opened in append mode. */
1366 dump_files[i].alt_state = 1;
1367 dump_files[i].alt_flags |= flags;
1368 n++;
1369 /* Override the existing filename. */
1370 if (filename)
1371 dump_files[i].alt_filename = xstrdup (filename);
1372 if (old_filename && filename != old_filename)
1373 free (CONST_CAST (char *, old_filename));
1377 for (i = 0; i < m_extra_dump_files_in_use; i++)
1379 if ((m_extra_dump_files[i].optgroup_flags & optgroup_flags))
1381 const char *old_filename = m_extra_dump_files[i].alt_filename;
1382 /* Since this file is shared among different passes, it
1383 should be opened in append mode. */
1384 m_extra_dump_files[i].alt_state = 1;
1385 m_extra_dump_files[i].alt_flags |= flags;
1386 n++;
1387 /* Override the existing filename. */
1388 if (filename)
1389 m_extra_dump_files[i].alt_filename = xstrdup (filename);
1390 if (old_filename && filename != old_filename)
1391 free (CONST_CAST (char *, old_filename));
1395 return n;
1398 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1399 relevant details in the dump_files array. */
1402 gcc::dump_manager::
1403 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1405 const char *option_value;
1406 const char *ptr;
1407 dump_flags_t flags;
1409 if (doglob && !dfi->glob)
1410 return 0;
1412 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1413 if (!option_value)
1414 return 0;
1416 if (*option_value && *option_value != '-' && *option_value != '=')
1417 return 0;
1419 ptr = option_value;
1420 flags = TDF_NONE;
1422 while (*ptr)
1424 const struct kv_pair<dump_flags_t> *option_ptr;
1425 const char *end_ptr;
1426 const char *eq_ptr;
1427 unsigned length;
1429 while (*ptr == '-')
1430 ptr++;
1431 end_ptr = strchr (ptr, '-');
1432 eq_ptr = strchr (ptr, '=');
1434 if (eq_ptr && !end_ptr)
1435 end_ptr = eq_ptr;
1437 if (!end_ptr)
1438 end_ptr = ptr + strlen (ptr);
1439 length = end_ptr - ptr;
1441 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1442 if (strlen (option_ptr->name) == length
1443 && !memcmp (option_ptr->name, ptr, length))
1445 flags |= option_ptr->value;
1446 goto found;
1449 if (*ptr == '=')
1451 /* Interpret rest of the argument as a dump filename. This
1452 filename overrides other command line filenames. */
1453 if (dfi->pfilename)
1454 free (CONST_CAST (char *, dfi->pfilename));
1455 dfi->pfilename = xstrdup (ptr + 1);
1456 break;
1458 else
1459 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1460 length, ptr, dfi->swtch);
1461 found:;
1462 ptr = end_ptr;
1465 dfi->pstate = -1;
1466 dfi->pflags |= flags;
1468 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1469 known dumps. */
1470 if (dfi->suffix == NULL)
1471 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1473 return 1;
1477 gcc::dump_manager::
1478 dump_switch_p (const char *arg)
1480 size_t i;
1481 int any = 0;
1483 for (i = TDI_none + 1; i != TDI_end; i++)
1484 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1486 /* Don't glob if we got a hit already */
1487 if (!any)
1488 for (i = TDI_none + 1; i != TDI_end; i++)
1489 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1491 for (i = 0; i < m_extra_dump_files_in_use; i++)
1492 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1494 if (!any)
1495 for (i = 0; i < m_extra_dump_files_in_use; i++)
1496 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1499 return any;
1502 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1503 and filename. Return non-zero if it is a recognized switch. */
1505 static int
1506 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1507 optgroup_flags_t *optgroup_flags, char **filename)
1509 const char *option_value;
1510 const char *ptr;
1512 option_value = arg;
1513 ptr = option_value;
1515 *filename = NULL;
1516 *flags = TDF_NONE;
1517 *optgroup_flags = OPTGROUP_NONE;
1519 if (!ptr)
1520 return 1; /* Handle '-fopt-info' without any additional options. */
1522 while (*ptr)
1524 const char *end_ptr;
1525 const char *eq_ptr;
1526 unsigned length;
1528 while (*ptr == '-')
1529 ptr++;
1530 end_ptr = strchr (ptr, '-');
1531 eq_ptr = strchr (ptr, '=');
1533 if (eq_ptr && !end_ptr)
1534 end_ptr = eq_ptr;
1536 if (!end_ptr)
1537 end_ptr = ptr + strlen (ptr);
1538 length = end_ptr - ptr;
1540 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1541 option_ptr->name; option_ptr++)
1542 if (strlen (option_ptr->name) == length
1543 && !memcmp (option_ptr->name, ptr, length))
1545 *flags |= option_ptr->value;
1546 goto found;
1549 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1550 option_ptr->name; option_ptr++)
1551 if (strlen (option_ptr->name) == length
1552 && !memcmp (option_ptr->name, ptr, length))
1554 *optgroup_flags |= option_ptr->value;
1555 goto found;
1558 if (*ptr == '=')
1560 /* Interpret rest of the argument as a dump filename. This
1561 filename overrides other command line filenames. */
1562 *filename = xstrdup (ptr + 1);
1563 break;
1565 else
1567 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1568 length, ptr, arg);
1569 return 0;
1571 found:;
1572 ptr = end_ptr;
1575 return 1;
1578 /* Return non-zero if ARG is a recognized switch for
1579 -fopt-info. Return zero otherwise. */
1582 opt_info_switch_p (const char *arg)
1584 dump_flags_t flags;
1585 optgroup_flags_t optgroup_flags;
1586 char *filename;
1587 static char *file_seen = NULL;
1588 gcc::dump_manager *dumps = g->get_dumps ();
1590 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1591 return 0;
1593 if (!filename)
1594 filename = xstrdup ("stderr");
1596 /* Bail out if a different filename has been specified. */
1597 if (file_seen && strcmp (file_seen, filename))
1599 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1600 arg);
1601 return 1;
1604 file_seen = xstrdup (filename);
1605 if (!flags)
1606 flags = MSG_OPTIMIZED_LOCATIONS;
1607 if (!optgroup_flags)
1608 optgroup_flags = OPTGROUP_ALL;
1610 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1613 /* Print basic block on the dump streams. */
1615 void
1616 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1618 if (dump_file && (dump_kind & pflags))
1619 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1620 if (alt_dump_file && (dump_kind & alt_flags))
1621 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
1624 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1626 void
1627 dump_function (int phase, tree fn)
1629 FILE *stream;
1630 dump_flags_t flags;
1632 stream = dump_begin (phase, &flags);
1633 if (stream)
1635 dump_function_to_file (fn, stream, flags);
1636 dump_end (phase, stream);
1640 /* Print information from the combine pass on dump_file. */
1642 void
1643 print_combine_total_stats (void)
1645 if (dump_file)
1646 dump_combine_total_stats (dump_file);
1649 /* Enable RTL dump for all the RTL passes. */
1651 bool
1652 enable_rtl_dump_file (void)
1654 gcc::dump_manager *dumps = g->get_dumps ();
1655 int num_enabled =
1656 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
1657 NULL);
1658 return num_enabled > 0;
1661 #if CHECKING_P
1663 /* temp_dump_context's ctor. Temporarily override the dump_context
1664 (to forcibly enable optinfo-generation). */
1666 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
1667 dump_flags_t test_pp_flags)
1669 : m_context (),
1670 m_saved (&dump_context ().get ())
1672 dump_context::s_current = &m_context;
1673 m_context.m_forcibly_enable_optinfo = forcibly_enable_optinfo;
1674 m_context.m_test_pp = &m_pp;
1675 m_context.m_test_pp_flags = test_pp_flags;
1677 dump_context::get ().refresh_dumps_are_enabled ();
1680 /* temp_dump_context's dtor. Restore the saved dump_context. */
1682 temp_dump_context::~temp_dump_context ()
1684 dump_context::s_current = m_saved;
1686 dump_context::get ().refresh_dumps_are_enabled ();
1689 /* 0-terminate the text dumped so far, and return it. */
1691 const char *
1692 temp_dump_context::get_dumped_text ()
1694 return pp_formatted_text (&m_pp);
1697 namespace selftest {
1699 /* Verify that the dump_location_t constructors capture the source location
1700 at which they were called (provided that the build compiler is sufficiently
1701 recent). */
1703 static void
1704 test_impl_location ()
1706 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
1707 /* Default ctor. */
1709 dump_location_t loc;
1710 const int expected_line = __LINE__ - 1;
1711 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1712 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1715 /* Constructing from a gimple. */
1717 dump_location_t loc ((gimple *)NULL);
1718 const int expected_line = __LINE__ - 1;
1719 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1720 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1723 /* Constructing from an rtx_insn. */
1725 dump_location_t loc ((rtx_insn *)NULL);
1726 const int expected_line = __LINE__ - 1;
1727 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1728 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1730 #endif
1733 /* Verify that the text dumped so far in CONTEXT equals
1734 EXPECTED_TEXT, using LOC for the location of any failure.
1735 As a side-effect, the internal buffer is 0-terminated. */
1737 static void
1738 verify_dumped_text (const location &loc,
1739 temp_dump_context *context,
1740 const char *expected_text)
1742 gcc_assert (context);
1743 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
1744 expected_text);
1747 /* Verify that the text dumped so far in CONTEXT equals
1748 EXPECTED_TEXT.
1749 As a side-effect, the internal buffer is 0-terminated. */
1751 #define ASSERT_DUMPED_TEXT_EQ(CONTEXT, EXPECTED_TEXT) \
1752 SELFTEST_BEGIN_STMT \
1753 verify_dumped_text (SELFTEST_LOCATION, &(CONTEXT), (EXPECTED_TEXT)); \
1754 SELFTEST_END_STMT
1756 /* Verify that ITEM has the expected values. */
1758 static void
1759 verify_item (const location &loc,
1760 const optinfo_item *item,
1761 enum optinfo_item_kind expected_kind,
1762 location_t expected_location,
1763 const char *expected_text)
1765 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
1766 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
1767 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
1770 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
1772 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
1773 SELFTEST_BEGIN_STMT \
1774 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
1775 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
1776 SELFTEST_END_STMT
1778 /* Verify that ITEM is a tree item, with the expected values. */
1780 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1781 SELFTEST_BEGIN_STMT \
1782 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
1783 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1784 SELFTEST_END_STMT
1786 /* Verify that ITEM is a gimple item, with the expected values. */
1788 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1789 SELFTEST_BEGIN_STMT \
1790 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
1791 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1792 SELFTEST_END_STMT
1794 /* Verify that calls to the dump_* API are captured and consolidated into
1795 optimization records. */
1797 static void
1798 test_capture_of_dump_calls (const line_table_case &case_)
1800 /* Generate a location_t for testing. */
1801 line_table_test ltt (case_);
1802 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
1803 linemap_line_start (line_table, 5, 100);
1804 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1805 location_t where = linemap_position_for_column (line_table, 10);
1806 if (where > LINE_MAP_MAX_LOCATION_WITH_COLS)
1807 return;
1809 dump_location_t loc = dump_location_t::from_location_t (where);
1811 greturn *stmt = gimple_build_return (NULL);
1812 gimple_set_location (stmt, where);
1814 /* Run all tests twice, with and then without optinfo enabled, to ensure
1815 that immediate destinations vs optinfo-based destinations both
1816 work, independently of each other, with no leaks. */
1817 for (int i = 0 ; i < 2; i++)
1819 bool with_optinfo = (i == 0);
1821 /* Test of dump_printf. */
1823 temp_dump_context tmp (with_optinfo, MSG_ALL);
1824 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
1826 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
1827 if (with_optinfo)
1829 optinfo *info = tmp.get_pending_optinfo ();
1830 ASSERT_TRUE (info != NULL);
1831 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1832 ASSERT_EQ (info->num_items (), 1);
1833 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
1837 /* Tree, via dump_generic_expr. */
1839 temp_dump_context tmp (with_optinfo, MSG_ALL);
1840 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
1841 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
1843 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
1844 if (with_optinfo)
1846 optinfo *info = tmp.get_pending_optinfo ();
1847 ASSERT_TRUE (info != NULL);
1848 ASSERT_EQ (info->get_location_t (), where);
1849 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1850 ASSERT_EQ (info->num_items (), 2);
1851 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
1852 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
1856 /* Tree, via dump_generic_expr_loc. */
1858 temp_dump_context tmp (with_optinfo, MSG_ALL);
1859 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
1861 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
1862 if (with_optinfo)
1864 optinfo *info = tmp.get_pending_optinfo ();
1865 ASSERT_TRUE (info != NULL);
1866 ASSERT_EQ (info->get_location_t (), where);
1867 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
1868 ASSERT_EQ (info->num_items (), 1);
1869 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
1873 /* Gimple. */
1875 /* dump_gimple_stmt_loc. */
1877 temp_dump_context tmp (with_optinfo, MSG_ALL);
1878 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
1880 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
1881 if (with_optinfo)
1883 optinfo *info = tmp.get_pending_optinfo ();
1884 ASSERT_TRUE (info != NULL);
1885 ASSERT_EQ (info->num_items (), 1);
1886 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
1890 /* dump_gimple_stmt. */
1892 temp_dump_context tmp (with_optinfo, MSG_ALL);
1893 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
1895 ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
1896 if (with_optinfo)
1898 optinfo *info = tmp.get_pending_optinfo ();
1899 ASSERT_TRUE (info != NULL);
1900 ASSERT_EQ (info->num_items (), 1);
1901 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;\n");
1905 /* dump_gimple_expr_loc. */
1907 temp_dump_context tmp (with_optinfo, MSG_ALL);
1908 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
1910 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
1911 if (with_optinfo)
1913 optinfo *info = tmp.get_pending_optinfo ();
1914 ASSERT_TRUE (info != NULL);
1915 ASSERT_EQ (info->num_items (), 1);
1916 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
1920 /* dump_gimple_expr. */
1922 temp_dump_context tmp (with_optinfo, MSG_ALL);
1923 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
1925 ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
1926 if (with_optinfo)
1928 optinfo *info = tmp.get_pending_optinfo ();
1929 ASSERT_TRUE (info != NULL);
1930 ASSERT_EQ (info->num_items (), 1);
1931 ASSERT_IS_GIMPLE (info->get_item (0), where, "return;");
1936 /* poly_int. */
1938 temp_dump_context tmp (with_optinfo, MSG_ALL);
1939 dump_dec (MSG_NOTE, poly_int64 (42));
1941 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
1942 if (with_optinfo)
1944 optinfo *info = tmp.get_pending_optinfo ();
1945 ASSERT_TRUE (info != NULL);
1946 ASSERT_EQ (info->num_items (), 1);
1947 ASSERT_IS_TEXT (info->get_item (0), "42");
1951 /* scopes. */
1953 temp_dump_context tmp (with_optinfo, MSG_ALL);
1954 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
1956 AUTO_DUMP_SCOPE ("outer scope", stmt);
1957 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
1959 AUTO_DUMP_SCOPE ("middle scope", stmt);
1960 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
1962 AUTO_DUMP_SCOPE ("inner scope", stmt);
1963 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
1965 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
1967 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
1969 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
1971 ASSERT_DUMPED_TEXT_EQ (tmp,
1972 "test.txt:5:10: note: msg 1\n"
1973 "test.txt:5:10: note: === outer scope ===\n"
1974 "test.txt:5:10: note: msg 2\n"
1975 "test.txt:5:10: note: === middle scope ===\n"
1976 "test.txt:5:10: note: msg 3\n"
1977 "test.txt:5:10: note: === inner scope ===\n"
1978 "test.txt:5:10: note: msg 4\n"
1979 "test.txt:5:10: note: msg 5\n"
1980 "test.txt:5:10: note: msg 6\n"
1981 "test.txt:5:10: note: msg 7\n");
1982 if (with_optinfo)
1984 optinfo *info = tmp.get_pending_optinfo ();
1985 ASSERT_TRUE (info != NULL);
1986 ASSERT_EQ (info->num_items (), 1);
1987 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
1992 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
1993 above. */
1995 /* MSG_OPTIMIZED_LOCATIONS. */
1997 temp_dump_context tmp (true, MSG_ALL);
1998 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
1999 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2000 OPTINFO_KIND_SUCCESS);
2003 /* MSG_MISSED_OPTIMIZATION. */
2005 temp_dump_context tmp (true, MSG_ALL);
2006 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2007 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2008 OPTINFO_KIND_FAILURE);
2013 /* Run all of the selftests within this file. */
2015 void
2016 dumpfile_c_tests ()
2018 test_impl_location ();
2019 for_each_line_table_case (test_capture_of_dump_calls);
2022 } // namespace selftest
2024 #endif /* CHECKING_P */