1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2021 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
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
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/>. */
22 #include "coretypes.h"
25 #include "gimple-pretty-print.h"
26 #include "diagnostic-core.h"
29 #include "profile-count.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. */
37 #include "dump-context.h"
39 #include "tree-pass.h" /* for "current_pass". */
40 #include "optinfo-emit-json.h"
41 #include "stringpool.h" /* for get_identifier. */
42 #include "spellcheck.h"
44 /* If non-NULL, return one past-the-end of the matching SUBPART of
46 #define skip_leading_substring(whole, part) \
47 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
49 static dump_flags_t pflags
; /* current dump_flags */
51 static void dump_loc (dump_flags_t
, FILE *, location_t
);
53 /* Current -fopt-info output stream, if any, and flags. */
54 static FILE *alt_dump_file
= NULL
;
55 static dump_flags_t alt_flags
;
57 static FILE *dump_open_alternate_stream (struct dump_file_info
*);
59 /* These are currently used for communicating between passes.
60 However, instead of accessing them directly, the passes can use
61 dump_printf () for dumps. */
62 FILE *dump_file
= NULL
;
63 const char *dump_file_name
;
64 dump_flags_t dump_flags
;
65 bool dumps_are_enabled
= false;
68 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
72 set_dump_file (FILE *new_dump_file
)
74 dumpfile_ensure_any_optinfo_are_flushed ();
75 dump_file
= new_dump_file
;
76 dump_context::get ().refresh_dumps_are_enabled ();
79 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
83 set_alt_dump_file (FILE *new_alt_dump_file
)
85 dumpfile_ensure_any_optinfo_are_flushed ();
86 alt_dump_file
= new_alt_dump_file
;
87 dump_context::get ().refresh_dumps_are_enabled ();
90 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
91 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
92 OPTGROUP_NONE, 0, 0, num, false, false}
94 /* Table of tree dump switches. This must be consistent with the
95 TREE_DUMP_INDEX enumeration in dumpfile.h. */
96 static struct dump_file_info dump_files
[TDI_end
] =
98 DUMP_FILE_INFO (NULL
, NULL
, DK_none
, 0),
99 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa
, 0),
100 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa
, 0),
101 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa
, 0),
102 DUMP_FILE_INFO (".original", "tree-original", DK_tree
, 0),
103 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree
, 0),
104 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree
, 0),
105 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa
, 0),
106 DUMP_FILE_INFO (".profile-report", "profile-report", DK_ipa
, 0),
107 #define FIRST_AUTO_NUMBERED_DUMP 1
108 #define FIRST_ME_AUTO_NUMBERED_DUMP 5
110 DUMP_FILE_INFO (NULL
, "lang-all", DK_lang
, 0),
111 DUMP_FILE_INFO (NULL
, "tree-all", DK_tree
, 0),
112 DUMP_FILE_INFO (NULL
, "rtl-all", DK_rtl
, 0),
113 DUMP_FILE_INFO (NULL
, "ipa-all", DK_ipa
, 0),
116 /* Table of dump options. This must be consistent with the TDF_* flags
117 in dumpfile.h and opt_info_options below. */
118 static const kv_pair
<dump_flags_t
> dump_options
[] =
121 {"address", TDF_ADDRESS
},
122 {"asmname", TDF_ASMNAME
},
125 {"graph", TDF_GRAPH
},
126 {"details", (TDF_DETAILS
| MSG_OPTIMIZED_LOCATIONS
127 | MSG_MISSED_OPTIMIZATION
129 {"cselib", TDF_CSELIB
},
130 {"stats", TDF_STATS
},
131 {"blocks", TDF_BLOCKS
},
133 {"lineno", TDF_LINENO
},
135 {"stmtaddr", TDF_STMTADDR
},
136 {"memsyms", TDF_MEMSYMS
},
138 {"alias", TDF_ALIAS
},
139 {"nouid", TDF_NOUID
},
140 {"enumerate_locals", TDF_ENUMERATE_LOCALS
},
142 {"gimple", TDF_GIMPLE
},
143 {"folding", TDF_FOLDING
},
144 {"optimized", MSG_OPTIMIZED_LOCATIONS
},
145 {"missed", MSG_MISSED_OPTIMIZATION
},
147 {"optall", MSG_ALL_KINDS
},
148 {"threading", TDF_THREADING
},
149 {"all", dump_flags_t (TDF_ALL_VALUES
150 & ~(TDF_RAW
| TDF_SLIM
| TDF_LINENO
| TDF_GRAPH
151 | TDF_STMTADDR
| TDF_RHS_ONLY
| TDF_NOUID
152 | TDF_ENUMERATE_LOCALS
| TDF_SCEV
| TDF_GIMPLE
))},
156 /* A subset of the dump_options table which is used for -fopt-info
157 types. This must be consistent with the MSG_* flags in dumpfile.h.
159 static const kv_pair
<dump_flags_t
> optinfo_verbosity_options
[] =
161 {"optimized", MSG_OPTIMIZED_LOCATIONS
},
162 {"missed", MSG_MISSED_OPTIMIZATION
},
164 {"all", MSG_ALL_KINDS
},
165 {"internals", MSG_PRIORITY_INTERNALS
},
169 /* Flags used for -fopt-info groups. */
170 const kv_pair
<optgroup_flags_t
> optgroup_options
[] =
172 {"ipa", OPTGROUP_IPA
},
173 {"loop", OPTGROUP_LOOP
},
174 {"inline", OPTGROUP_INLINE
},
175 {"omp", OPTGROUP_OMP
},
176 {"vec", OPTGROUP_VEC
},
177 {"optall", OPTGROUP_ALL
},
178 {NULL
, OPTGROUP_NONE
}
181 gcc::dump_manager::dump_manager ():
182 m_next_dump (FIRST_AUTO_NUMBERED_DUMP
),
183 m_extra_dump_files (NULL
),
184 m_extra_dump_files_in_use (0),
185 m_extra_dump_files_alloced (0),
186 m_optgroup_flags (OPTGROUP_NONE
),
187 m_optinfo_flags (TDF_NONE
),
188 m_optinfo_filename (NULL
)
192 gcc::dump_manager::~dump_manager ()
194 free (m_optinfo_filename
);
195 for (size_t i
= 0; i
< m_extra_dump_files_in_use
; i
++)
197 dump_file_info
*dfi
= &m_extra_dump_files
[i
];
198 /* suffix, swtch, glob are statically allocated for the entries
199 in dump_files, and for statistics, but are dynamically allocated
200 for those for passes. */
201 if (dfi
->owns_strings
)
203 XDELETEVEC (const_cast <char *> (dfi
->suffix
));
204 XDELETEVEC (const_cast <char *> (dfi
->swtch
));
205 XDELETEVEC (const_cast <char *> (dfi
->glob
));
207 /* These, if non-NULL, are always dynamically allocated. */
208 XDELETEVEC (const_cast <char *> (dfi
->pfilename
));
209 XDELETEVEC (const_cast <char *> (dfi
->alt_filename
));
211 XDELETEVEC (m_extra_dump_files
);
216 dump_register (const char *suffix
, const char *swtch
, const char *glob
,
217 dump_kind dkind
, optgroup_flags_t optgroup_flags
,
220 int num
= m_next_dump
++;
222 size_t count
= m_extra_dump_files_in_use
++;
224 if (count
>= m_extra_dump_files_alloced
)
226 if (m_extra_dump_files_alloced
== 0)
227 m_extra_dump_files_alloced
= 512;
229 m_extra_dump_files_alloced
*= 2;
230 m_extra_dump_files
= XRESIZEVEC (struct dump_file_info
,
232 m_extra_dump_files_alloced
);
234 /* Construct a new object in the space allocated above. */
235 new (m_extra_dump_files
+ count
) dump_file_info ();
239 /* Zero out the already constructed object. */
240 m_extra_dump_files
[count
] = dump_file_info ();
243 m_extra_dump_files
[count
].suffix
= suffix
;
244 m_extra_dump_files
[count
].swtch
= swtch
;
245 m_extra_dump_files
[count
].glob
= glob
;
246 m_extra_dump_files
[count
].dkind
= dkind
;
247 m_extra_dump_files
[count
].optgroup_flags
= optgroup_flags
;
248 m_extra_dump_files
[count
].num
= num
;
249 m_extra_dump_files
[count
].owns_strings
= take_ownership
;
251 return count
+ TDI_end
;
255 /* Allow languages and middle-end to register their dumps before the
256 optimization passes. */
262 lang_hooks
.register_dumps (this);
263 /* If this assert fails, some FE registered more than
264 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
265 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
266 gcc_assert (m_next_dump
<= FIRST_ME_AUTO_NUMBERED_DUMP
);
267 m_next_dump
= FIRST_ME_AUTO_NUMBERED_DUMP
;
268 dump_files
[TDI_original
].num
= m_next_dump
++;
269 dump_files
[TDI_gimple
].num
= m_next_dump
++;
270 dump_files
[TDI_nested
].num
= m_next_dump
++;
274 /* Return the dump_file_info for the given phase. */
276 struct dump_file_info
*
278 get_dump_file_info (int phase
) const
281 return &dump_files
[phase
];
282 else if ((size_t) (phase
- TDI_end
) >= m_extra_dump_files_in_use
)
285 return m_extra_dump_files
+ (phase
- TDI_end
);
288 /* Locate the dump_file_info with swtch equal to SWTCH,
289 or return NULL if no such dump_file_info exists. */
291 struct dump_file_info
*
293 get_dump_file_info_by_switch (const char *swtch
) const
295 for (unsigned i
= 0; i
< m_extra_dump_files_in_use
; i
++)
296 if (strcmp (m_extra_dump_files
[i
].swtch
, swtch
) == 0)
297 return &m_extra_dump_files
[i
];
304 /* Return the name of the dump file for the given phase.
305 The caller is responsible for calling free on the returned
307 If the dump is not enabled, returns NULL. */
311 get_dump_file_name (int phase
, int part
) const
313 struct dump_file_info
*dfi
;
315 if (phase
== TDI_none
)
318 dfi
= get_dump_file_info (phase
);
320 return get_dump_file_name (dfi
, part
);
323 /* Return the name of the dump file for the given dump_file_info.
324 The caller is responsible for calling free on the returned
326 If the dump is not enabled, returns NULL. */
330 get_dump_file_name (struct dump_file_info
*dfi
, int part
) const
336 if (dfi
->pstate
== 0)
339 /* If available, use the command line dump filename. */
341 return xstrdup (dfi
->pfilename
);
347 /* (null), LANG, TREE, RTL, IPA. */
348 char suffix
= " ltri"[dfi
->dkind
];
350 if (snprintf (dump_id
, sizeof (dump_id
), ".%03d%c", dfi
->num
, suffix
) < 0)
357 snprintf (part_id
, sizeof (part_id
), ".%i", part
);
358 return concat (dump_base_name
, dump_id
, part_id
, dfi
->suffix
, NULL
);
361 return concat (dump_base_name
, dump_id
, dfi
->suffix
, NULL
);
364 /* Open a dump file called FILENAME. Some filenames are special and
365 refer to the standard streams. TRUNC indicates whether this is the
366 first open (so the file should be truncated, rather than appended).
367 An error message is emitted in the event of failure. */
370 dump_open (const char *filename
, bool trunc
)
372 if (strcmp ("stderr", filename
) == 0)
375 if (strcmp ("stdout", filename
) == 0
376 || strcmp ("-", filename
) == 0)
379 FILE *stream
= fopen (filename
, trunc
? "w" : "a");
382 error ("could not open dump file %qs: %m", filename
);
386 /* For a given DFI, open an alternate dump filename (which could also
387 be a standard stream such as stdout/stderr). If the alternate dump
388 file cannot be opened, return NULL. */
391 dump_open_alternate_stream (struct dump_file_info
*dfi
)
393 if (!dfi
->alt_filename
)
397 return dfi
->alt_stream
;
399 FILE *stream
= dump_open (dfi
->alt_filename
, dfi
->alt_state
< 0);
407 /* Construct a dump_user_location_t from STMT (using its location and
410 dump_user_location_t::dump_user_location_t (const gimple
*stmt
)
411 : m_count (), m_loc (UNKNOWN_LOCATION
)
416 m_count
= stmt
->bb
->count
;
417 m_loc
= gimple_location (stmt
);
421 /* Construct a dump_user_location_t from an RTL instruction (using its
422 location and hotness). */
424 dump_user_location_t::dump_user_location_t (const rtx_insn
*insn
)
425 : m_count (), m_loc (UNKNOWN_LOCATION
)
429 basic_block bb
= BLOCK_FOR_INSN (insn
);
432 m_loc
= INSN_LOCATION (insn
);
436 /* Construct from a function declaration. This one requires spelling out
437 to avoid accidentally constructing from other kinds of tree. */
440 dump_user_location_t::from_function_decl (tree fndecl
)
444 // FIXME: profile count for function?
445 return dump_user_location_t (profile_count (),
446 DECL_SOURCE_LOCATION (fndecl
));
449 /* Extract the MSG_* component from DUMP_KIND and return a string for use
450 as a prefix to dump messages.
451 These match the strings in optinfo_verbosity_options and thus the
452 "OPTIONS" within "-fopt-info-OPTIONS". */
455 kind_as_string (dump_flags_t dump_kind
)
457 switch (dump_kind
& MSG_ALL_KINDS
)
461 case MSG_OPTIMIZED_LOCATIONS
:
463 case MSG_MISSED_OPTIMIZATION
:
470 /* Print source location on DFILE if enabled. */
473 dump_loc (dump_flags_t dump_kind
, FILE *dfile
, location_t loc
)
477 if (LOCATION_LOCUS (loc
) > BUILTINS_LOCATION
)
478 fprintf (dfile
, "%s:%d:%d: ", LOCATION_FILE (loc
),
479 LOCATION_LINE (loc
), LOCATION_COLUMN (loc
));
480 else if (current_function_decl
)
481 fprintf (dfile
, "%s:%d:%d: ",
482 DECL_SOURCE_FILE (current_function_decl
),
483 DECL_SOURCE_LINE (current_function_decl
),
484 DECL_SOURCE_COLUMN (current_function_decl
));
485 fprintf (dfile
, "%s: ", kind_as_string (dump_kind
));
486 /* Indentation based on scope depth. */
487 fprintf (dfile
, "%*s", get_dump_scope_depth (), "");
491 /* Print source location to PP if enabled. */
494 dump_loc (dump_flags_t dump_kind
, pretty_printer
*pp
, location_t loc
)
496 /* Disable warnings about missing quoting in GCC diagnostics for
497 the pp_printf calls. Their format strings aren't used to format
498 diagnostics so don't need to follow GCC diagnostic conventions. */
500 # pragma GCC diagnostic push
501 # pragma GCC diagnostic ignored "-Wformat-diag"
506 if (LOCATION_LOCUS (loc
) > BUILTINS_LOCATION
)
507 pp_printf (pp
, "%s:%d:%d: ", LOCATION_FILE (loc
),
508 LOCATION_LINE (loc
), LOCATION_COLUMN (loc
));
509 else if (current_function_decl
)
510 pp_printf (pp
, "%s:%d:%d: ",
511 DECL_SOURCE_FILE (current_function_decl
),
512 DECL_SOURCE_LINE (current_function_decl
),
513 DECL_SOURCE_COLUMN (current_function_decl
));
514 pp_printf (pp
, "%s: ", kind_as_string (dump_kind
));
515 /* Indentation based on scope depth. */
516 for (unsigned i
= 0; i
< get_dump_scope_depth (); i
++)
517 pp_character (pp
, ' ');
521 # pragma GCC diagnostic pop
525 /* Implementation of dump_context member functions. */
527 /* dump_context's dtor. */
529 dump_context::~dump_context ()
535 dump_context::set_json_writer (optrecord_json_writer
*writer
)
537 delete m_json_writer
;
538 m_json_writer
= writer
;
541 /* Perform cleanup activity for -fsave-optimization-record.
542 Currently, the file is written out here in one go, before cleaning
546 dump_context::finish_any_json_writer ()
551 m_json_writer
->write ();
552 delete m_json_writer
;
553 m_json_writer
= NULL
;
556 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
557 or alt_dump_file change, or when changing dump_context in selftests. */
560 dump_context::refresh_dumps_are_enabled ()
562 dumps_are_enabled
= (dump_file
|| alt_dump_file
|| optinfo_enabled_p ()
566 /* Determine if a message of kind DUMP_KIND and at the current scope depth
569 Only show messages that match FILTER both on their kind *and*
573 dump_context::apply_dump_filter_p (dump_flags_t dump_kind
,
574 dump_flags_t filter
) const
576 /* Few messages, if any, have an explicit MSG_PRIORITY.
577 If DUMP_KIND does, we'll use it.
578 Otherwise, generate an implicit priority value for the message based
579 on the current scope depth.
580 Messages at the top-level scope are MSG_PRIORITY_USER_FACING,
581 whereas those in nested scopes are MSG_PRIORITY_INTERNALS. */
582 if (!(dump_kind
& MSG_ALL_PRIORITIES
))
584 dump_flags_t implicit_priority
586 ? MSG_PRIORITY_INTERNALS
587 : MSG_PRIORITY_USER_FACING
);
588 dump_kind
|= implicit_priority
;
591 return (dump_kind
& (filter
& MSG_ALL_KINDS
)
592 && dump_kind
& (filter
& MSG_ALL_PRIORITIES
));
595 /* Print LOC to the appropriate dump destinations, given DUMP_KIND.
596 If optinfos are enabled, begin a new optinfo. */
599 dump_context::dump_loc (const dump_metadata_t
&metadata
,
600 const dump_user_location_t
&loc
)
604 dump_loc_immediate (metadata
.get_dump_flags (), loc
);
606 if (optinfo_enabled_p ())
607 begin_next_optinfo (metadata
, loc
);
610 /* As dump_loc above, but without starting a new optinfo. */
613 dump_context::dump_loc_immediate (dump_flags_t dump_kind
,
614 const dump_user_location_t
&loc
)
616 location_t srcloc
= loc
.get_location_t ();
618 if (dump_file
&& apply_dump_filter_p (dump_kind
, pflags
))
619 ::dump_loc (dump_kind
, dump_file
, srcloc
);
621 if (alt_dump_file
&& apply_dump_filter_p (dump_kind
, alt_flags
))
622 ::dump_loc (dump_kind
, alt_dump_file
, srcloc
);
624 /* Support for temp_dump_context in selftests. */
625 if (m_test_pp
&& apply_dump_filter_p (dump_kind
, m_test_pp_flags
))
626 ::dump_loc (dump_kind
, m_test_pp
, srcloc
);
629 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
631 static optinfo_item
*
632 make_item_for_dump_gimple_stmt (gimple
*stmt
, int spc
, dump_flags_t dump_flags
)
635 pp_needs_newline (&pp
) = true;
636 pp_gimple_stmt_1 (&pp
, stmt
, spc
, dump_flags
);
640 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE
, gimple_location (stmt
),
641 xstrdup (pp_formatted_text (&pp
)));
645 /* Dump gimple statement GS with SPC indentation spaces and
646 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
649 dump_context::dump_gimple_stmt (const dump_metadata_t
&metadata
,
650 dump_flags_t extra_dump_flags
,
654 = make_item_for_dump_gimple_stmt (gs
, spc
, dump_flags
| extra_dump_flags
);
655 emit_item (item
, metadata
.get_dump_flags ());
657 if (optinfo_enabled_p ())
659 optinfo
&info
= ensure_pending_optinfo (metadata
);
660 info
.add_item (item
);
666 /* Similar to dump_gimple_stmt, except additionally print source location. */
669 dump_context::dump_gimple_stmt_loc (const dump_metadata_t
&metadata
,
670 const dump_user_location_t
&loc
,
671 dump_flags_t extra_dump_flags
,
674 dump_loc (metadata
, loc
);
675 dump_gimple_stmt (metadata
, extra_dump_flags
, gs
, spc
);
678 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
680 static optinfo_item
*
681 make_item_for_dump_gimple_expr (gimple
*stmt
, int spc
, dump_flags_t dump_flags
)
683 dump_flags
|= TDF_RHS_ONLY
;
685 pp_needs_newline (&pp
) = true;
686 pp_gimple_stmt_1 (&pp
, stmt
, spc
, dump_flags
);
689 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE
, gimple_location (stmt
),
690 xstrdup (pp_formatted_text (&pp
)));
694 /* Dump gimple statement GS with SPC indentation spaces and
695 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
696 Do not terminate with a newline or semicolon. */
699 dump_context::dump_gimple_expr (const dump_metadata_t
&metadata
,
700 dump_flags_t extra_dump_flags
,
704 = make_item_for_dump_gimple_expr (gs
, spc
, dump_flags
| extra_dump_flags
);
705 emit_item (item
, metadata
.get_dump_flags ());
707 if (optinfo_enabled_p ())
709 optinfo
&info
= ensure_pending_optinfo (metadata
);
710 info
.add_item (item
);
716 /* Similar to dump_gimple_expr, except additionally print source location. */
719 dump_context::dump_gimple_expr_loc (const dump_metadata_t
&metadata
,
720 const dump_user_location_t
&loc
,
721 dump_flags_t extra_dump_flags
,
725 dump_loc (metadata
, loc
);
726 dump_gimple_expr (metadata
, extra_dump_flags
, gs
, spc
);
729 /* Make an item for the given dump call, equivalent to print_generic_expr. */
731 static optinfo_item
*
732 make_item_for_dump_generic_expr (tree node
, dump_flags_t dump_flags
)
735 pp_needs_newline (&pp
) = true;
736 pp_translate_identifiers (&pp
) = false;
737 dump_generic_node (&pp
, node
, 0, dump_flags
, false);
739 location_t loc
= UNKNOWN_LOCATION
;
740 if (EXPR_HAS_LOCATION (node
))
741 loc
= EXPR_LOCATION (node
);
744 = new optinfo_item (OPTINFO_ITEM_KIND_TREE
, loc
,
745 xstrdup (pp_formatted_text (&pp
)));
749 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
750 DUMP_KIND is enabled. */
753 dump_context::dump_generic_expr (const dump_metadata_t
&metadata
,
754 dump_flags_t extra_dump_flags
,
758 = make_item_for_dump_generic_expr (t
, dump_flags
| extra_dump_flags
);
759 emit_item (item
, metadata
.get_dump_flags ());
761 if (optinfo_enabled_p ())
763 optinfo
&info
= ensure_pending_optinfo (metadata
);
764 info
.add_item (item
);
771 /* Similar to dump_generic_expr, except additionally print the source
775 dump_context::dump_generic_expr_loc (const dump_metadata_t
&metadata
,
776 const dump_user_location_t
&loc
,
777 dump_flags_t extra_dump_flags
,
780 dump_loc (metadata
, loc
);
781 dump_generic_expr (metadata
, extra_dump_flags
, t
);
784 /* Make an item for the given dump call. */
786 static optinfo_item
*
787 make_item_for_dump_symtab_node (symtab_node
*node
)
789 location_t loc
= DECL_SOURCE_LOCATION (node
->decl
);
791 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE
, loc
,
792 xstrdup (node
->dump_name ()));
796 /* dump_pretty_printer's ctor. */
798 dump_pretty_printer::dump_pretty_printer (dump_context
*context
,
799 dump_flags_t dump_kind
)
800 : pretty_printer (), m_context (context
), m_dump_kind (dump_kind
),
803 pp_format_decoder (this) = format_decoder_cb
;
806 /* Phase 3 of formatting; compare with pp_output_formatted_text.
808 Emit optinfo_item instances for the various formatted chunks from phases
809 1 and 2 (i.e. pp_format).
811 Some chunks may already have had their items built (during decode_format).
812 These chunks have been stashed into m_stashed_items; we emit them here.
814 For all other purely textual chunks, they are printed into
815 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
816 This consolidates multiple adjacent text chunks into a single text
820 dump_pretty_printer::emit_items (optinfo
*dest
)
822 output_buffer
*buffer
= pp_buffer (this);
823 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
824 const char **args
= chunk_array
->args
;
826 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
827 gcc_assert (buffer
->line_length
== 0);
829 unsigned stashed_item_idx
= 0;
830 for (unsigned chunk
= 0; args
[chunk
]; chunk
++)
832 if (stashed_item_idx
< m_stashed_items
.length ()
833 && args
[chunk
] == *m_stashed_items
[stashed_item_idx
].buffer_ptr
)
835 emit_any_pending_textual_chunks (dest
);
836 /* This chunk has a stashed item: use it. */
837 emit_item (m_stashed_items
[stashed_item_idx
++].item
, dest
);
840 /* This chunk is purely textual. Print it (to
841 buffer->formatted_obstack), so that we can consolidate adjacent
842 chunks into one textual optinfo_item. */
843 pp_string (this, args
[chunk
]);
846 emit_any_pending_textual_chunks (dest
);
848 /* Ensure that we consumed all of stashed_items. */
849 gcc_assert (stashed_item_idx
== m_stashed_items
.length ());
851 /* Deallocate the chunk structure and everything after it (i.e. the
852 associated series of formatted strings). */
853 buffer
->cur_chunk_array
= chunk_array
->prev
;
854 obstack_free (&buffer
->chunk_obstack
, chunk_array
);
857 /* Subroutine of dump_pretty_printer::emit_items
858 for consolidating multiple adjacent pure-text chunks into single
859 optinfo_items (in phase 3). */
862 dump_pretty_printer::emit_any_pending_textual_chunks (optinfo
*dest
)
864 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
866 /* Don't emit an item if the pending text is empty. */
867 if (output_buffer_last_position_in_text (buffer
) == NULL
)
870 char *formatted_text
= xstrdup (pp_formatted_text (this));
872 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT
, UNKNOWN_LOCATION
,
874 emit_item (item
, dest
);
876 /* Clear the pending text by unwinding formatted_text back to the start
877 of the buffer (without deallocating). */
878 obstack_free (&buffer
->formatted_obstack
,
879 buffer
->formatted_obstack
.object_base
);
882 /* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
883 to DEST; otherwise delete ITEM. */
886 dump_pretty_printer::emit_item (optinfo_item
*item
, optinfo
*dest
)
888 m_context
->emit_item (item
, m_dump_kind
);
890 dest
->add_item (item
);
895 /* Record that ITEM (generated in phase 2 of formatting) is to be used for
896 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
899 dump_pretty_printer::stash_item (const char **buffer_ptr
, optinfo_item
*item
)
901 gcc_assert (buffer_ptr
);
904 m_stashed_items
.safe_push (stashed_item (buffer_ptr
, item
));
907 /* pp_format_decoder callback for dump_pretty_printer, and thus for
908 dump_printf and dump_printf_loc.
910 A wrapper around decode_format, for type-safety. */
913 dump_pretty_printer::format_decoder_cb (pretty_printer
*pp
, text_info
*text
,
914 const char *spec
, int /*precision*/,
915 bool /*wide*/, bool /*set_locus*/,
916 bool /*verbose*/, bool */
*quoted*/
,
917 const char **buffer_ptr
)
919 dump_pretty_printer
*opp
= static_cast <dump_pretty_printer
*> (pp
);
920 return opp
->decode_format (text
, spec
, buffer_ptr
);
923 /* Format decoder for dump_pretty_printer, and thus for dump_printf and
926 Supported format codes (in addition to the standard pretty_printer ones)
930 Equivalent to: dump_symtab_node (MSG_*, node)
932 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
934 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
936 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
938 TODO: add a format code that can handle (symtab_node*) *and* both
939 subclasses (presumably means teaching -Wformat about non-virtual
942 These format codes build optinfo_item instances, thus capturing metadata
943 about the arguments being dumped, as well as the textual output. */
946 dump_pretty_printer::decode_format (text_info
*text
, const char *spec
,
947 const char **buffer_ptr
)
949 /* Various format codes that imply making an optinfo_item and stashed it
950 for later use (to capture metadata, rather than plain text). */
955 cgraph_node
*node
= va_arg (*text
->args_ptr
, cgraph_node
*);
957 /* Make an item for the node, and stash it. */
958 optinfo_item
*item
= make_item_for_dump_symtab_node (node
);
959 stash_item (buffer_ptr
, item
);
965 gimple
*stmt
= va_arg (*text
->args_ptr
, gimple
*);
967 /* Make an item for the stmt, and stash it. */
968 optinfo_item
*item
= make_item_for_dump_gimple_expr (stmt
, 0, TDF_SLIM
);
969 stash_item (buffer_ptr
, item
);
975 gimple
*stmt
= va_arg (*text
->args_ptr
, gimple
*);
977 /* Make an item for the stmt, and stash it. */
978 optinfo_item
*item
= make_item_for_dump_gimple_stmt (stmt
, 0, TDF_SLIM
);
979 stash_item (buffer_ptr
, item
);
985 tree t
= va_arg (*text
->args_ptr
, tree
);
987 /* Make an item for the tree, and stash it. */
988 optinfo_item
*item
= make_item_for_dump_generic_expr (t
, TDF_SLIM
);
989 stash_item (buffer_ptr
, item
);
998 /* Output a formatted message using FORMAT on appropriate dump streams. */
1001 dump_context::dump_printf_va (const dump_metadata_t
&metadata
, const char *format
,
1004 dump_pretty_printer
pp (this, metadata
.get_dump_flags ());
1007 text
.err_no
= errno
;
1009 text
.format_spec
= format
;
1011 /* Phases 1 and 2, using pp_format. */
1012 pp_format (&pp
, &text
);
1015 if (optinfo_enabled_p ())
1017 optinfo
&info
= ensure_pending_optinfo (metadata
);
1018 pp
.emit_items (&info
);
1021 pp
.emit_items (NULL
);
1024 /* Similar to dump_printf, except source location is also printed, and
1025 dump location captured. */
1028 dump_context::dump_printf_loc_va (const dump_metadata_t
&metadata
,
1029 const dump_user_location_t
&loc
,
1030 const char *format
, va_list *ap
)
1032 dump_loc (metadata
, loc
);
1033 dump_printf_va (metadata
, format
, ap
);
1036 /* Make an item for the given dump call, equivalent to print_dec. */
1038 template<unsigned int N
, typename C
>
1039 static optinfo_item
*
1040 make_item_for_dump_dec (const poly_int
<N
, C
> &value
)
1042 STATIC_ASSERT (poly_coeff_traits
<C
>::signedness
>= 0);
1043 signop sgn
= poly_coeff_traits
<C
>::signedness
? SIGNED
: UNSIGNED
;
1047 if (value
.is_constant ())
1048 pp_wide_int (&pp
, value
.coeffs
[0], sgn
);
1051 pp_character (&pp
, '[');
1052 for (unsigned int i
= 0; i
< N
; ++i
)
1054 pp_wide_int (&pp
, value
.coeffs
[i
], sgn
);
1055 pp_character (&pp
, i
== N
- 1 ? ']' : ',');
1060 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT
, UNKNOWN_LOCATION
,
1061 xstrdup (pp_formatted_text (&pp
)));
1065 /* Output VALUE in decimal to appropriate dump streams. */
1067 template<unsigned int N
, typename C
>
1069 dump_context::dump_dec (const dump_metadata_t
&metadata
, const poly_int
<N
, C
> &value
)
1071 optinfo_item
*item
= make_item_for_dump_dec (value
);
1072 emit_item (item
, metadata
.get_dump_flags ());
1074 if (optinfo_enabled_p ())
1076 optinfo
&info
= ensure_pending_optinfo (metadata
);
1077 info
.add_item (item
);
1083 /* Output the name of NODE on appropriate dump streams. */
1086 dump_context::dump_symtab_node (const dump_metadata_t
&metadata
, symtab_node
*node
)
1088 optinfo_item
*item
= make_item_for_dump_symtab_node (node
);
1089 emit_item (item
, metadata
.get_dump_flags ());
1091 if (optinfo_enabled_p ())
1093 optinfo
&info
= ensure_pending_optinfo (metadata
);
1094 info
.add_item (item
);
1100 /* Get the current dump scope-nesting depth.
1101 For use by -fopt-info (for showing nesting via indentation). */
1104 dump_context::get_scope_depth () const
1106 return m_scope_depth
;
1109 /* Push a nested dump scope.
1110 Increment the scope depth.
1111 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1112 destination, if any.
1113 Emit a "scope" optinfo if optinfos are enabled. */
1116 dump_context::begin_scope (const char *name
,
1117 const dump_user_location_t
&user_location
,
1118 const dump_impl_location_t
&impl_location
)
1122 location_t src_loc
= user_location
.get_location_t ();
1124 if (dump_file
&& apply_dump_filter_p (MSG_NOTE
, pflags
))
1125 ::dump_loc (MSG_NOTE
, dump_file
, src_loc
);
1127 if (alt_dump_file
&& apply_dump_filter_p (MSG_NOTE
, alt_flags
))
1128 ::dump_loc (MSG_NOTE
, alt_dump_file
, src_loc
);
1130 /* Support for temp_dump_context in selftests. */
1131 if (m_test_pp
&& apply_dump_filter_p (MSG_NOTE
, m_test_pp_flags
))
1132 ::dump_loc (MSG_NOTE
, m_test_pp
, src_loc
);
1134 /* Format multiple consecutive punctuation characters via %s to
1135 avoid -Wformat-diag in the pp_printf call below whose output
1136 isn't used for diagnostic output. */
1138 pp_printf (&pp
, "%s %s %s", "===", name
, "===");
1141 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT
, UNKNOWN_LOCATION
,
1142 xstrdup (pp_formatted_text (&pp
)));
1143 emit_item (item
, MSG_NOTE
);
1145 if (optinfo_enabled_p ())
1148 = begin_next_optinfo (dump_metadata_t (MSG_NOTE
, impl_location
),
1150 info
.m_kind
= OPTINFO_KIND_SCOPE
;
1151 info
.add_item (item
);
1158 /* Pop a nested dump scope. */
1161 dump_context::end_scope ()
1167 m_json_writer
->pop_scope ();
1170 /* Should optinfo instances be created?
1171 All creation of optinfos should be guarded by this predicate.
1172 Return true if any optinfo destinations are active. */
1175 dump_context::optinfo_enabled_p () const
1177 return (optimization_records_enabled_p ());
1180 /* Return the optinfo currently being accumulated, creating one if
1184 dump_context::ensure_pending_optinfo (const dump_metadata_t
&metadata
)
1187 return begin_next_optinfo (metadata
, dump_user_location_t ());
1191 /* Start a new optinfo and return it, ending any optinfo that was already
1195 dump_context::begin_next_optinfo (const dump_metadata_t
&metadata
,
1196 const dump_user_location_t
&user_loc
)
1199 gcc_assert (m_pending
== NULL
);
1200 dump_location_t
loc (user_loc
, metadata
.get_impl_location ());
1201 m_pending
= new optinfo (loc
, OPTINFO_KIND_NOTE
, current_pass
);
1202 m_pending
->handle_dump_file_kind (metadata
.get_dump_flags ());
1206 /* End any optinfo that has been accumulated within this context; emitting
1207 it to any destinations as appropriate, such as optimization records. */
1210 dump_context::end_any_optinfo ()
1213 emit_optinfo (m_pending
);
1218 /* Emit the optinfo to all of the "non-immediate" destinations
1219 (emission to "immediate" destinations is done by
1220 dump_context::emit_item). */
1223 dump_context::emit_optinfo (const optinfo
*info
)
1225 /* -fsave-optimization-record. */
1227 m_json_writer
->add_record (info
);
1230 /* Emit ITEM to all item destinations (those that don't require
1231 consolidation into optinfo instances). */
1234 dump_context::emit_item (optinfo_item
*item
, dump_flags_t dump_kind
)
1236 if (dump_file
&& apply_dump_filter_p (dump_kind
, pflags
))
1237 fprintf (dump_file
, "%s", item
->get_text ());
1239 if (alt_dump_file
&& apply_dump_filter_p (dump_kind
, alt_flags
))
1240 fprintf (alt_dump_file
, "%s", item
->get_text ());
1242 /* Support for temp_dump_context in selftests. */
1243 if (m_test_pp
&& apply_dump_filter_p (dump_kind
, m_test_pp_flags
))
1244 pp_string (m_test_pp
, item
->get_text ());
1247 /* The current singleton dump_context, and its default. */
1249 dump_context
*dump_context::s_current
= &dump_context::s_default
;
1250 dump_context
dump_context::s_default
;
1252 /* Implementation of dump_* API calls, calling into dump_context
1253 member functions. */
1255 /* Calls to the dump_* functions do non-trivial work, so they ought
1257 if (dump_enabled_p ())
1258 Assert that they are guarded, and, if assertions are disabled,
1259 bail out if the calls weren't properly guarded. */
1261 #define VERIFY_DUMP_ENABLED_P \
1263 gcc_assert (dump_enabled_p ()); \
1264 if (!dump_enabled_p ()) \
1268 /* Dump gimple statement GS with SPC indentation spaces and
1269 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1272 dump_gimple_stmt (const dump_metadata_t
&metadata
, dump_flags_t extra_dump_flags
,
1273 gimple
*gs
, int spc
)
1275 VERIFY_DUMP_ENABLED_P
;
1276 dump_context::get ().dump_gimple_stmt (metadata
, extra_dump_flags
, gs
, spc
);
1279 /* Similar to dump_gimple_stmt, except additionally print source location. */
1282 dump_gimple_stmt_loc (const dump_metadata_t
&metadata
,
1283 const dump_user_location_t
&loc
,
1284 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
1286 VERIFY_DUMP_ENABLED_P
;
1287 dump_context::get ().dump_gimple_stmt_loc (metadata
, loc
, extra_dump_flags
,
1291 /* Dump gimple statement GS with SPC indentation spaces and
1292 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1293 Do not terminate with a newline or semicolon. */
1296 dump_gimple_expr (const dump_metadata_t
&metadata
,
1297 dump_flags_t extra_dump_flags
,
1298 gimple
*gs
, int spc
)
1300 VERIFY_DUMP_ENABLED_P
;
1301 dump_context::get ().dump_gimple_expr (metadata
, extra_dump_flags
, gs
, spc
);
1304 /* Similar to dump_gimple_expr, except additionally print source location. */
1307 dump_gimple_expr_loc (const dump_metadata_t
&metadata
,
1308 const dump_user_location_t
&loc
,
1309 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
1311 VERIFY_DUMP_ENABLED_P
;
1312 dump_context::get ().dump_gimple_expr_loc (metadata
, loc
, extra_dump_flags
,
1316 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1317 DUMP_KIND is enabled. */
1320 dump_generic_expr (const dump_metadata_t
&metadata
, dump_flags_t extra_dump_flags
,
1323 VERIFY_DUMP_ENABLED_P
;
1324 dump_context::get ().dump_generic_expr (metadata
, extra_dump_flags
, t
);
1327 /* Similar to dump_generic_expr, except additionally print the source
1331 dump_generic_expr_loc (const dump_metadata_t
&metadata
,
1332 const dump_user_location_t
&loc
,
1333 dump_flags_t extra_dump_flags
, tree t
)
1335 VERIFY_DUMP_ENABLED_P
;
1336 dump_context::get ().dump_generic_expr_loc (metadata
, loc
, extra_dump_flags
,
1340 /* Output a formatted message using FORMAT on appropriate dump streams. */
1343 dump_printf (const dump_metadata_t
&metadata
, const char *format
, ...)
1345 VERIFY_DUMP_ENABLED_P
;
1347 va_start (ap
, format
);
1348 dump_context::get ().dump_printf_va (metadata
, format
, &ap
);
1352 /* Similar to dump_printf, except source location is also printed, and
1353 dump location captured. */
1356 dump_printf_loc (const dump_metadata_t
&metadata
,
1357 const dump_user_location_t
&loc
,
1358 const char *format
, ...)
1360 VERIFY_DUMP_ENABLED_P
;
1362 va_start (ap
, format
);
1363 dump_context::get ().dump_printf_loc_va (metadata
, loc
, format
, &ap
);
1367 /* Output VALUE in decimal to appropriate dump streams. */
1369 template<unsigned int N
, typename C
>
1371 dump_dec (const dump_metadata_t
&metadata
, const poly_int
<N
, C
> &value
)
1373 VERIFY_DUMP_ENABLED_P
;
1374 dump_context::get ().dump_dec (metadata
, value
);
1377 template void dump_dec (const dump_metadata_t
&metadata
, const poly_uint16
&);
1378 template void dump_dec (const dump_metadata_t
&metadata
, const poly_int64
&);
1379 template void dump_dec (const dump_metadata_t
&metadata
, const poly_uint64
&);
1380 template void dump_dec (const dump_metadata_t
&metadata
, const poly_offset_int
&);
1381 template void dump_dec (const dump_metadata_t
&metadata
, const poly_widest_int
&);
1384 dump_dec (dump_flags_t dump_kind
, const poly_wide_int
&value
, signop sgn
)
1386 VERIFY_DUMP_ENABLED_P
;
1388 && dump_context::get ().apply_dump_filter_p (dump_kind
, pflags
))
1389 print_dec (value
, dump_file
, sgn
);
1392 && dump_context::get ().apply_dump_filter_p (dump_kind
, alt_flags
))
1393 print_dec (value
, alt_dump_file
, sgn
);
1396 /* Output VALUE in hexadecimal to appropriate dump streams. */
1399 dump_hex (dump_flags_t dump_kind
, const poly_wide_int
&value
)
1401 VERIFY_DUMP_ENABLED_P
;
1403 && dump_context::get ().apply_dump_filter_p (dump_kind
, pflags
))
1404 print_hex (value
, dump_file
);
1407 && dump_context::get ().apply_dump_filter_p (dump_kind
, alt_flags
))
1408 print_hex (value
, alt_dump_file
);
1411 /* Emit and delete the currently pending optinfo, if there is one,
1412 without the caller needing to know about class dump_context. */
1415 dumpfile_ensure_any_optinfo_are_flushed ()
1417 dump_context::get().end_any_optinfo ();
1420 /* Output the name of NODE on appropriate dump streams. */
1423 dump_symtab_node (const dump_metadata_t
&metadata
, symtab_node
*node
)
1425 VERIFY_DUMP_ENABLED_P
;
1426 dump_context::get ().dump_symtab_node (metadata
, node
);
1429 /* Get the current dump scope-nesting depth.
1430 For use by -fopt-info (for showing nesting via indentation). */
1433 get_dump_scope_depth ()
1435 return dump_context::get ().get_scope_depth ();
1438 /* Push a nested dump scope.
1439 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1440 destination, if any.
1441 Emit a "scope" opinfo if optinfos are enabled.
1442 Increment the scope depth. */
1445 dump_begin_scope (const char *name
,
1446 const dump_user_location_t
&user_location
,
1447 const dump_impl_location_t
&impl_location
)
1449 dump_context::get ().begin_scope (name
, user_location
, impl_location
);
1452 /* Pop a nested dump scope. */
1457 dump_context::get ().end_scope ();
1460 /* Start a dump for PHASE. Store user-supplied dump flags in
1461 *FLAG_PTR. Return the number of streams opened. Set globals
1462 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1463 set dump_flags appropriately for both pass dump stream and
1464 -fopt-info stream. */
1468 dump_start (int phase
, dump_flags_t
*flag_ptr
)
1472 struct dump_file_info
*dfi
;
1474 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1477 dfi
= get_dump_file_info (phase
);
1478 name
= get_dump_file_name (phase
);
1481 stream
= dump_open (name
, dfi
->pstate
< 0);
1488 dfi
->pstream
= stream
;
1489 set_dump_file (dfi
->pstream
);
1490 /* Initialize current dump flags. */
1491 pflags
= dfi
->pflags
;
1494 stream
= dump_open_alternate_stream (dfi
);
1497 dfi
->alt_stream
= stream
;
1499 set_alt_dump_file (dfi
->alt_stream
);
1500 /* Initialize current -fopt-info flags. */
1501 alt_flags
= dfi
->alt_flags
;
1505 *flag_ptr
= dfi
->pflags
;
1510 /* Finish a tree dump for PHASE and close associated dump streams. Also
1511 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1515 dump_finish (int phase
)
1517 struct dump_file_info
*dfi
;
1521 dfi
= get_dump_file_info (phase
);
1522 if (dfi
->pstream
&& dfi
->pstream
!= stdout
&& dfi
->pstream
!= stderr
)
1523 fclose (dfi
->pstream
);
1525 if (dfi
->alt_stream
&& dfi
->alt_stream
!= stdout
&& dfi
->alt_stream
!= stderr
)
1526 fclose (dfi
->alt_stream
);
1528 dfi
->alt_stream
= NULL
;
1529 dfi
->pstream
= NULL
;
1530 set_dump_file (NULL
);
1531 set_alt_dump_file (NULL
);
1532 dump_flags
= TDF_NONE
;
1533 alt_flags
= TDF_NONE
;
1537 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1538 *FLAG_PTR and returns a stream to write to. If the dump is not
1539 enabled, returns NULL.
1540 PART can be used for dump files which should be split to multiple
1541 parts. PART == -1 indicates dump file with no parts.
1542 If PART is -1, multiple calls will reopen and append to the dump file. */
1545 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1547 return g
->get_dumps ()->dump_begin (phase
, flag_ptr
, part
);
1552 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1554 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1557 char *name
= get_dump_file_name (phase
, part
);
1560 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1562 /* We do not support re-opening of dump files with parts. This would require
1563 tracking pstate per part of the dump file. */
1564 FILE *stream
= dump_open (name
, part
!= -1 || dfi
->pstate
< 0);
1570 *flag_ptr
= dfi
->pflags
;
1572 /* Initialize current flags */
1573 pflags
= dfi
->pflags
;
1577 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1578 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1583 dump_phase_enabled_p (int phase
) const
1585 if (phase
== TDI_tree_all
)
1588 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1589 if (dump_files
[i
].pstate
|| dump_files
[i
].alt_state
)
1591 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1592 if (m_extra_dump_files
[i
].pstate
|| m_extra_dump_files
[i
].alt_state
)
1598 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1599 return dfi
->pstate
|| dfi
->alt_state
;
1603 /* Returns nonzero if tree dump PHASE has been initialized. */
1607 dump_initialized_p (int phase
) const
1609 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1610 return dfi
->pstate
> 0 || dfi
->alt_state
> 0;
1613 /* Returns the switch name of PHASE. */
1616 dump_flag_name (int phase
)
1618 return g
->get_dumps ()->dump_flag_name (phase
);
1623 dump_flag_name (int phase
) const
1625 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1629 /* Handle -fdump-* and -fopt-info for a pass added after
1630 command-line options are parsed (those from plugins and
1631 those from backends).
1633 Because the registration of plugin/backend passes happens after the
1634 command-line options are parsed, the options that specify single
1635 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1636 passes. Therefore we currently can only enable dumping of
1637 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1638 are specified. This is done here.
1640 Similarly, the saved -fopt-info options are wired up to the new pass. */
1643 gcc::dump_manager::register_pass (opt_pass
*pass
)
1647 register_one_dump_file (pass
);
1649 dump_file_info
*pass_dfi
= get_dump_file_info (pass
->static_pass_number
);
1650 gcc_assert (pass_dfi
);
1652 enum tree_dump_index tdi
;
1653 if (pass
->type
== SIMPLE_IPA_PASS
1654 || pass
->type
== IPA_PASS
)
1656 else if (pass
->type
== GIMPLE_PASS
)
1660 const dump_file_info
*tdi_dfi
= get_dump_file_info (tdi
);
1661 gcc_assert (tdi_dfi
);
1663 /* Check if dump-all flag is specified. */
1664 if (tdi_dfi
->pstate
)
1666 pass_dfi
->pstate
= tdi_dfi
->pstate
;
1667 pass_dfi
->pflags
= tdi_dfi
->pflags
;
1670 update_dfi_for_opt_info (pass_dfi
);
1673 /* Finish a tree dump for PHASE. STREAM is the stream created by
1677 dump_end (int phase ATTRIBUTE_UNUSED
, FILE *stream
)
1679 if (stream
!= stderr
&& stream
!= stdout
)
1683 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1684 enabled tree dumps. */
1688 dump_enable_all (dump_kind dkind
, dump_flags_t flags
, const char *filename
)
1693 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1695 if (dump_files
[i
].dkind
== dkind
)
1697 const char *old_filename
= dump_files
[i
].pfilename
;
1698 dump_files
[i
].pstate
= -1;
1699 dump_files
[i
].pflags
|= flags
;
1701 /* Override the existing filename. */
1704 dump_files
[i
].pfilename
= xstrdup (filename
);
1705 /* Since it is a command-line provided file, which is
1706 common to all the phases, use it in append mode. */
1707 dump_files
[i
].pstate
= 1;
1709 if (old_filename
&& filename
!= old_filename
)
1710 free (CONST_CAST (char *, old_filename
));
1714 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1716 if (m_extra_dump_files
[i
].dkind
== dkind
)
1718 const char *old_filename
= m_extra_dump_files
[i
].pfilename
;
1719 m_extra_dump_files
[i
].pstate
= -1;
1720 m_extra_dump_files
[i
].pflags
|= flags
;
1722 /* Override the existing filename. */
1725 m_extra_dump_files
[i
].pfilename
= xstrdup (filename
);
1726 /* Since it is a command-line provided file, which is
1727 common to all the phases, use it in append mode. */
1728 m_extra_dump_files
[i
].pstate
= 1;
1730 if (old_filename
&& filename
!= old_filename
)
1731 free (CONST_CAST (char *, old_filename
));
1738 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1739 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1744 opt_info_enable_passes (optgroup_flags_t optgroup_flags
, dump_flags_t flags
,
1745 const char *filename
)
1749 m_optgroup_flags
= optgroup_flags
;
1750 m_optinfo_flags
= flags
;
1751 m_optinfo_filename
= xstrdup (filename
);
1753 for (size_t i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1754 if (update_dfi_for_opt_info (&dump_files
[i
]))
1757 for (size_t i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1758 if (update_dfi_for_opt_info (&m_extra_dump_files
[i
]))
1764 /* Use the saved -fopt-info options to update DFI.
1765 Return true if the dump is enabled. */
1768 gcc::dump_manager::update_dfi_for_opt_info (dump_file_info
*dfi
) const
1772 if (!(dfi
->optgroup_flags
& m_optgroup_flags
))
1775 const char *old_filename
= dfi
->alt_filename
;
1776 /* Since this file is shared among different passes, it
1777 should be opened in append mode. */
1779 dfi
->alt_flags
|= m_optinfo_flags
;
1780 /* Override the existing filename. */
1781 if (m_optinfo_filename
)
1782 dfi
->alt_filename
= xstrdup (m_optinfo_filename
);
1783 if (old_filename
&& m_optinfo_filename
!= old_filename
)
1784 free (CONST_CAST (char *, old_filename
));
1789 /* Helper routine to parse -<dump format>[=filename]
1790 and return the corresponding dump flag. If POS_P is non-NULL,
1791 assign start of filename into *POS_P. */
1794 parse_dump_option (const char *option_value
, const char **pos_p
)
1803 /* Retain "user-facing" and "internals" messages, but filter out
1804 those from an opt_problem being re-emitted at the top level
1805 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1806 messing up scan-tree-dump-times" in DejaGnu tests. */
1807 flags
= MSG_PRIORITY_USER_FACING
| MSG_PRIORITY_INTERNALS
;
1811 const struct kv_pair
<dump_flags_t
> *option_ptr
;
1812 const char *end_ptr
;
1817 end_ptr
= strchr (ptr
, '-');
1818 eq_ptr
= strchr (ptr
, '=');
1820 if (eq_ptr
&& (!end_ptr
|| end_ptr
> eq_ptr
))
1824 end_ptr
= ptr
+ strlen (ptr
);
1825 length
= end_ptr
- ptr
;
1827 for (option_ptr
= dump_options
; option_ptr
->name
; option_ptr
++)
1828 if (strlen (option_ptr
->name
) == length
1829 && !memcmp (option_ptr
->name
, ptr
, length
))
1831 flags
|= option_ptr
->value
;
1837 /* Interpret rest of the argument as a dump filename. This
1838 filename overrides other command line filenames. */
1845 warning (0, "ignoring unknown option %q.*s",
1856 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1857 relevant details in the dump_files array. */
1861 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
, bool doglob
)
1863 const char *option_value
;
1864 dump_flags_t flags
= TDF_NONE
;
1866 if (doglob
&& !dfi
->glob
)
1869 option_value
= skip_leading_substring (arg
, doglob
? dfi
->glob
: dfi
->swtch
);
1873 if (*option_value
&& *option_value
!= '-' && *option_value
!= '=')
1876 const char *filename
;
1877 flags
= parse_dump_option (option_value
, &filename
);
1881 free (CONST_CAST (char *, dfi
->pfilename
));
1882 dfi
->pfilename
= xstrdup (filename
);
1886 dfi
->pflags
|= flags
;
1888 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1890 if (dfi
->suffix
== NULL
)
1891 dump_enable_all (dfi
->dkind
, dfi
->pflags
, dfi
->pfilename
);
1898 dump_switch_p (const char *arg
)
1903 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1904 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], false);
1906 /* Don't glob if we got a hit already */
1908 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1909 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], true);
1911 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1912 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], false);
1915 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1916 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], true);
1920 auto_vec
<const char *> candidates
;
1921 for (size_t i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1922 candidates
.safe_push (dump_files
[i
].swtch
);
1923 for (size_t i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1924 candidates
.safe_push (m_extra_dump_files
[i
].swtch
);
1925 const char *hint
= find_closest_string (arg
, &candidates
);
1927 error ("unrecognized command-line option %<-fdump-%s%>; "
1928 "did you mean %<-fdump-%s%>?", arg
, hint
);
1930 error ("unrecognized command-line option %<-fdump-%s%>", arg
);
1934 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1935 and filename. Return non-zero if it is a recognized switch. */
1938 opt_info_switch_p_1 (const char *arg
, dump_flags_t
*flags
,
1939 optgroup_flags_t
*optgroup_flags
, char **filename
)
1941 const char *option_value
;
1949 /* Default to filtering out "internals" messages, and retaining
1950 "user-facing" messages, and those from an opt_problem being
1951 re-emitted at the top level. */
1952 *flags
= MSG_PRIORITY_USER_FACING
| MSG_PRIORITY_REEMITTED
;
1954 *optgroup_flags
= OPTGROUP_NONE
;
1957 return 1; /* Handle '-fopt-info' without any additional options. */
1961 const char *end_ptr
;
1967 end_ptr
= strchr (ptr
, '-');
1968 eq_ptr
= strchr (ptr
, '=');
1970 if (eq_ptr
&& (!end_ptr
|| eq_ptr
< end_ptr
))
1973 end_ptr
= ptr
+ strlen (ptr
);
1974 length
= end_ptr
- ptr
;
1976 for (const kv_pair
<dump_flags_t
> *option_ptr
= optinfo_verbosity_options
;
1977 option_ptr
->name
; option_ptr
++)
1978 if (strlen (option_ptr
->name
) == length
1979 && !memcmp (option_ptr
->name
, ptr
, length
))
1981 *flags
|= option_ptr
->value
;
1985 for (const kv_pair
<optgroup_flags_t
> *option_ptr
= optgroup_options
;
1986 option_ptr
->name
; option_ptr
++)
1987 if (strlen (option_ptr
->name
) == length
1988 && !memcmp (option_ptr
->name
, ptr
, length
))
1990 *optgroup_flags
|= option_ptr
->value
;
1996 /* Interpret rest of the argument as a dump filename. This
1997 filename overrides other command line filenames. */
1998 *filename
= xstrdup (ptr
+ 1);
2003 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
2014 /* Return non-zero if ARG is a recognized switch for
2015 -fopt-info. Return zero otherwise. */
2018 opt_info_switch_p (const char *arg
)
2021 optgroup_flags_t optgroup_flags
;
2023 static char *file_seen
= NULL
;
2024 gcc::dump_manager
*dumps
= g
->get_dumps ();
2026 if (!opt_info_switch_p_1 (arg
, &flags
, &optgroup_flags
, &filename
))
2030 filename
= xstrdup ("stderr");
2032 /* Bail out if a different filename has been specified. */
2033 if (file_seen
&& strcmp (file_seen
, filename
))
2035 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
2040 file_seen
= xstrdup (filename
);
2041 if (!(flags
& MSG_ALL_KINDS
))
2042 flags
|= MSG_OPTIMIZED_LOCATIONS
;
2043 if (!optgroup_flags
)
2044 optgroup_flags
= OPTGROUP_ALL
;
2046 return dumps
->opt_info_enable_passes (optgroup_flags
, flags
, filename
);
2049 /* Print basic block on the dump streams. */
2052 dump_basic_block (dump_flags_t dump_kind
, basic_block bb
, int indent
)
2055 && dump_context::get ().apply_dump_filter_p (dump_kind
, pflags
))
2056 dump_bb (dump_file
, bb
, indent
, TDF_DETAILS
);
2058 && dump_context::get ().apply_dump_filter_p (dump_kind
, alt_flags
))
2059 dump_bb (alt_dump_file
, bb
, indent
, TDF_DETAILS
);
2062 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
2065 dump_function (int phase
, tree fn
)
2070 stream
= dump_begin (phase
, &flags
);
2073 dump_function_to_file (fn
, stream
, flags
);
2074 dump_end (phase
, stream
);
2078 /* Print information from the combine pass on dump_file. */
2081 print_combine_total_stats (void)
2084 dump_combine_total_stats (dump_file
);
2087 /* Enable RTL dump for all the RTL passes. */
2090 enable_rtl_dump_file (void)
2092 gcc::dump_manager
*dumps
= g
->get_dumps ();
2094 dumps
->dump_enable_all (DK_rtl
, dump_flags_t (TDF_DETAILS
) | TDF_BLOCKS
,
2096 return num_enabled
> 0;
2099 /* debug_dump_context's ctor. Temporarily override the dump_context
2100 (to forcibly enable output to stderr). */
2102 debug_dump_context::debug_dump_context (FILE *f
)
2104 m_saved (&dump_context::get ()),
2105 m_saved_flags (dump_flags
),
2106 m_saved_pflags (pflags
),
2107 m_saved_file (dump_file
)
2110 dump_context::s_current
= &m_context
;
2111 pflags
= dump_flags
= MSG_ALL_KINDS
| MSG_ALL_PRIORITIES
;
2112 dump_context::get ().refresh_dumps_are_enabled ();
2115 /* debug_dump_context's dtor. Restore the saved dump_context. */
2117 debug_dump_context::~debug_dump_context ()
2119 set_dump_file (m_saved_file
);
2120 dump_context::s_current
= m_saved
;
2121 dump_flags
= m_saved_flags
;
2122 pflags
= m_saved_pflags
;
2123 dump_context::get ().refresh_dumps_are_enabled ();
2129 namespace selftest
{
2131 /* temp_dump_context's ctor. Temporarily override the dump_context
2132 (to forcibly enable optinfo-generation). */
2134 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo
,
2135 bool forcibly_enable_dumping
,
2136 dump_flags_t test_pp_flags
)
2138 m_saved (&dump_context::get ())
2140 dump_context::s_current
= &m_context
;
2141 if (forcibly_enable_optinfo
)
2142 m_context
.set_json_writer (new optrecord_json_writer ());
2143 /* Conditionally enable the test dump, so that we can verify both the
2144 dump_enabled_p and the !dump_enabled_p cases in selftests. */
2145 if (forcibly_enable_dumping
)
2147 m_context
.m_test_pp
= &m_pp
;
2148 m_context
.m_test_pp_flags
= test_pp_flags
;
2151 dump_context::get ().refresh_dumps_are_enabled ();
2154 /* temp_dump_context's dtor. Restore the saved dump_context. */
2156 temp_dump_context::~temp_dump_context ()
2158 m_context
.set_json_writer (NULL
);
2160 dump_context::s_current
= m_saved
;
2162 dump_context::get ().refresh_dumps_are_enabled ();
2165 /* 0-terminate the text dumped so far, and return it. */
2168 temp_dump_context::get_dumped_text ()
2170 return pp_formatted_text (&m_pp
);
2173 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2174 from EXPECTED_FUNCTION, using LOC for the location of any failure,
2175 provided that the build compiler is sufficiently recent. */
2178 assert_impl_location_eq (const location
&loc ATTRIBUTE_UNUSED
,
2179 const dump_impl_location_t
&impl_loc ATTRIBUTE_UNUSED
,
2180 const char *expected_file ATTRIBUTE_UNUSED
,
2181 int expected_line ATTRIBUTE_UNUSED
,
2182 const char *expected_function ATTRIBUTE_UNUSED
)
2184 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2185 ASSERT_STR_CONTAINS_AT (loc
, impl_loc
.m_file
, expected_file
);
2186 ASSERT_EQ_AT (loc
, impl_loc
.m_line
, expected_line
);
2187 ASSERT_STR_CONTAINS_AT (loc
, impl_loc
.m_function
, expected_function
);
2191 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2192 from EXPECTED_FUNCTION, provided that the build compiler is
2193 sufficiently recent. */
2195 #define ASSERT_IMPL_LOCATION_EQ(IMPL_LOC, EXPECTED_FILE, EXPECTED_LINE, \
2196 EXPECTED_FUNCTION) \
2197 SELFTEST_BEGIN_STMT \
2198 assert_impl_location_eq (SELFTEST_LOCATION, IMPL_LOC, \
2199 EXPECTED_FILE, EXPECTED_LINE, \
2200 EXPECTED_FUNCTION); \
2203 /* Verify that the dump_location_t constructors capture the source location
2204 at which they were called (provided that the build compiler is sufficiently
2208 test_impl_location ()
2212 dump_location_t loc
;
2213 const int expected_line
= __LINE__
- 1;
2214 ASSERT_IMPL_LOCATION_EQ (loc
.get_impl_location (),
2215 "dumpfile.c", expected_line
, "test_impl_location");
2218 /* Constructing from a gimple. */
2220 dump_location_t
loc ((gimple
*)NULL
);
2221 const int expected_line
= __LINE__
- 1;
2222 ASSERT_IMPL_LOCATION_EQ (loc
.get_impl_location (),
2223 "dumpfile.c", expected_line
, "test_impl_location");
2226 /* Constructing from an rtx_insn. */
2228 dump_location_t
loc ((rtx_insn
*)NULL
);
2229 const int expected_line
= __LINE__
- 1;
2230 ASSERT_IMPL_LOCATION_EQ (loc
.get_impl_location (),
2231 "dumpfile.c", expected_line
, "test_impl_location");
2235 /* Verify that the text dumped so far in CONTEXT equals
2236 EXPECTED_TEXT, using LOC for the location of any failure.
2237 As a side-effect, the internal buffer is 0-terminated. */
2240 verify_dumped_text (const location
&loc
,
2241 temp_dump_context
*context
,
2242 const char *expected_text
)
2244 gcc_assert (context
);
2245 ASSERT_STREQ_AT (loc
, context
->get_dumped_text (),
2249 /* Verify that ITEM has the expected values. */
2252 verify_item (const location
&loc
,
2253 const optinfo_item
*item
,
2254 enum optinfo_item_kind expected_kind
,
2255 location_t expected_location
,
2256 const char *expected_text
)
2258 ASSERT_EQ_AT (loc
, item
->get_kind (), expected_kind
);
2259 ASSERT_EQ_AT (loc
, item
->get_location (), expected_location
);
2260 ASSERT_STREQ_AT (loc
, item
->get_text (), expected_text
);
2263 /* Verify that calls to the dump_* API are captured and consolidated into
2264 optimization records. */
2267 test_capture_of_dump_calls (const line_table_case
&case_
)
2269 /* Generate a location_t for testing. */
2270 line_table_test
ltt (case_
);
2271 linemap_add (line_table
, LC_ENTER
, false, "test.txt", 0);
2272 linemap_line_start (line_table
, 5, 100);
2273 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
2274 location_t decl_loc
= linemap_position_for_column (line_table
, 8);
2275 location_t stmt_loc
= linemap_position_for_column (line_table
, 10);
2276 if (stmt_loc
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
2279 dump_user_location_t loc
= dump_user_location_t::from_location_t (stmt_loc
);
2281 gimple
*stmt
= gimple_build_return (NULL
);
2282 gimple_set_location (stmt
, stmt_loc
);
2284 tree test_decl
= build_decl (decl_loc
, FUNCTION_DECL
,
2285 get_identifier ("test_decl"),
2286 build_function_type_list (void_type_node
,
2289 symbol_table_test tmp_symtab
;
2291 cgraph_node
*node
= cgraph_node::get_create (test_decl
);
2294 /* Run all tests twice, with and then without optinfo enabled, to ensure
2295 that immediate destinations vs optinfo-based destinations both
2296 work, independently of each other, with no leaks. */
2297 for (int i
= 0 ; i
< 2; i
++)
2299 bool with_optinfo
= (i
== 0);
2301 /* Test of dump_printf. */
2303 temp_dump_context
tmp (with_optinfo
, true,
2304 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2305 dump_printf (MSG_NOTE
, "int: %i str: %s", 42, "foo");
2306 const int expected_impl_line
= __LINE__
- 1;
2308 ASSERT_DUMPED_TEXT_EQ (tmp
, "int: 42 str: foo");
2311 optinfo
*info
= tmp
.get_pending_optinfo ();
2312 ASSERT_TRUE (info
!= NULL
);
2313 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2314 ASSERT_EQ (info
->num_items (), 1);
2315 ASSERT_IS_TEXT (info
->get_item (0), "int: 42 str: foo");
2316 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2317 "dumpfile.c", expected_impl_line
,
2318 "test_capture_of_dump_calls");
2322 /* Test of dump_printf with %T. */
2324 temp_dump_context
tmp (with_optinfo
, true,
2325 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2326 dump_printf (MSG_NOTE
, "tree: %T", integer_zero_node
);
2327 const int expected_impl_line
= __LINE__
- 1;
2329 ASSERT_DUMPED_TEXT_EQ (tmp
, "tree: 0");
2332 optinfo
*info
= tmp
.get_pending_optinfo ();
2333 ASSERT_TRUE (info
!= NULL
);
2334 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2335 ASSERT_EQ (info
->num_items (), 2);
2336 ASSERT_IS_TEXT (info
->get_item (0), "tree: ");
2337 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
2338 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2339 "dumpfile.c", expected_impl_line
,
2340 "test_capture_of_dump_calls");
2344 /* Test of dump_printf with %E. */
2346 temp_dump_context
tmp (with_optinfo
, true,
2347 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2348 dump_printf (MSG_NOTE
, "gimple: %E", stmt
);
2349 const int expected_impl_line
= __LINE__
- 1;
2351 ASSERT_DUMPED_TEXT_EQ (tmp
, "gimple: return;");
2354 optinfo
*info
= tmp
.get_pending_optinfo ();
2355 ASSERT_TRUE (info
!= NULL
);
2356 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2357 ASSERT_EQ (info
->num_items (), 2);
2358 ASSERT_IS_TEXT (info
->get_item (0), "gimple: ");
2359 ASSERT_IS_GIMPLE (info
->get_item (1), stmt_loc
, "return;");
2360 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2361 "dumpfile.c", expected_impl_line
,
2362 "test_capture_of_dump_calls");
2366 /* Test of dump_printf with %G. */
2368 temp_dump_context
tmp (with_optinfo
, true,
2369 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2370 dump_printf (MSG_NOTE
, "gimple: %G", stmt
);
2371 const int expected_impl_line
= __LINE__
- 1;
2373 ASSERT_DUMPED_TEXT_EQ (tmp
, "gimple: return;\n");
2376 optinfo
*info
= tmp
.get_pending_optinfo ();
2377 ASSERT_TRUE (info
!= NULL
);
2378 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2379 ASSERT_EQ (info
->num_items (), 2);
2380 ASSERT_IS_TEXT (info
->get_item (0), "gimple: ");
2381 ASSERT_IS_GIMPLE (info
->get_item (1), stmt_loc
, "return;\n");
2382 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2383 "dumpfile.c", expected_impl_line
,
2384 "test_capture_of_dump_calls");
2388 /* Test of dump_printf with %C. */
2390 temp_dump_context
tmp (with_optinfo
, true,
2391 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2392 dump_printf (MSG_NOTE
, "node: %C", node
);
2393 const int expected_impl_line
= __LINE__
- 1;
2395 ASSERT_DUMPED_TEXT_EQ (tmp
, "node: test_decl/0");
2398 optinfo
*info
= tmp
.get_pending_optinfo ();
2399 ASSERT_TRUE (info
!= NULL
);
2400 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2401 ASSERT_EQ (info
->num_items (), 2);
2402 ASSERT_IS_TEXT (info
->get_item (0), "node: ");
2403 ASSERT_IS_SYMTAB_NODE (info
->get_item (1), decl_loc
, "test_decl/0");
2404 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2405 "dumpfile.c", expected_impl_line
,
2406 "test_capture_of_dump_calls");
2410 /* dump_print_loc with multiple format codes. This tests various
2412 - intermingling of text, format codes handled by the base
2413 pretty_printer, and dump-specific format codes
2414 - multiple dump-specific format codes: some consecutive, others
2415 separated by text, trailing text after the final one. */
2417 temp_dump_context
tmp (with_optinfo
, true,
2418 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2419 dump_printf_loc (MSG_NOTE
, loc
, "before %T and %T"
2420 " %i consecutive %E%E after\n",
2421 integer_zero_node
, test_decl
, 42, stmt
, stmt
);
2423 ASSERT_DUMPED_TEXT_EQ (tmp
,
2424 "test.txt:5:10: note: before 0 and test_decl"
2425 " 42 consecutive return;return; after\n");
2428 optinfo
*info
= tmp
.get_pending_optinfo ();
2429 ASSERT_TRUE (info
!= NULL
);
2430 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2431 ASSERT_EQ (info
->num_items (), 8);
2432 ASSERT_IS_TEXT (info
->get_item (0), "before ");
2433 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
2434 ASSERT_IS_TEXT (info
->get_item (2), " and ");
2435 ASSERT_IS_TREE (info
->get_item (3), UNKNOWN_LOCATION
, "test_decl");
2436 ASSERT_IS_TEXT (info
->get_item (4), " 42 consecutive ");
2437 ASSERT_IS_GIMPLE (info
->get_item (5), stmt_loc
, "return;");
2438 ASSERT_IS_GIMPLE (info
->get_item (6), stmt_loc
, "return;");
2439 ASSERT_IS_TEXT (info
->get_item (7), " after\n");
2440 /* We don't ASSERT_IMPL_LOCATION_EQ here, to avoid having to
2441 enforce at which exact line the multiline dump_printf_loc
2446 /* Tree, via dump_generic_expr. */
2448 temp_dump_context
tmp (with_optinfo
, true,
2449 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2450 dump_printf_loc (MSG_NOTE
, loc
, "test of tree: ");
2451 const int expected_impl_line
= __LINE__
- 1;
2452 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, integer_zero_node
);
2454 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: test of tree: 0");
2457 optinfo
*info
= tmp
.get_pending_optinfo ();
2458 ASSERT_TRUE (info
!= NULL
);
2459 ASSERT_EQ (info
->get_location_t (), stmt_loc
);
2460 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2461 ASSERT_EQ (info
->num_items (), 2);
2462 ASSERT_IS_TEXT (info
->get_item (0), "test of tree: ");
2463 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
2464 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2465 "dumpfile.c", expected_impl_line
,
2466 "test_capture_of_dump_calls");
2470 /* Tree, via dump_generic_expr_loc. */
2472 temp_dump_context
tmp (with_optinfo
, true,
2473 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2474 dump_generic_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, integer_one_node
);
2475 const int expected_impl_line
= __LINE__
- 1;
2477 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: 1");
2480 optinfo
*info
= tmp
.get_pending_optinfo ();
2481 ASSERT_TRUE (info
!= NULL
);
2482 ASSERT_EQ (info
->get_location_t (), stmt_loc
);
2483 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2484 ASSERT_EQ (info
->num_items (), 1);
2485 ASSERT_IS_TREE (info
->get_item (0), UNKNOWN_LOCATION
, "1");
2486 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2487 "dumpfile.c", expected_impl_line
,
2488 "test_capture_of_dump_calls");
2494 /* dump_gimple_stmt_loc. */
2496 temp_dump_context
tmp (with_optinfo
, true,
2497 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2498 dump_gimple_stmt_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
2499 const int expected_impl_line
= __LINE__
- 1;
2501 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: return;\n");
2504 optinfo
*info
= tmp
.get_pending_optinfo ();
2505 ASSERT_TRUE (info
!= NULL
);
2506 ASSERT_EQ (info
->num_items (), 1);
2507 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;\n");
2508 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2509 "dumpfile.c", expected_impl_line
,
2510 "test_capture_of_dump_calls");
2514 /* dump_gimple_stmt. */
2516 temp_dump_context
tmp (with_optinfo
, true,
2517 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2518 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
2519 const int expected_impl_line
= __LINE__
- 1;
2521 ASSERT_DUMPED_TEXT_EQ (tmp
, "return;\n");
2524 optinfo
*info
= tmp
.get_pending_optinfo ();
2525 ASSERT_TRUE (info
!= NULL
);
2526 ASSERT_EQ (info
->num_items (), 1);
2527 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;\n");
2528 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2529 "dumpfile.c", expected_impl_line
,
2530 "test_capture_of_dump_calls");
2534 /* dump_gimple_expr_loc. */
2536 temp_dump_context
tmp (with_optinfo
, true,
2537 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2538 dump_gimple_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
2539 const int expected_impl_line
= __LINE__
- 1;
2541 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: return;");
2544 optinfo
*info
= tmp
.get_pending_optinfo ();
2545 ASSERT_TRUE (info
!= NULL
);
2546 ASSERT_EQ (info
->num_items (), 1);
2547 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;");
2548 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2549 "dumpfile.c", expected_impl_line
,
2550 "test_capture_of_dump_calls");
2554 /* dump_gimple_expr. */
2556 temp_dump_context
tmp (with_optinfo
, true,
2557 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2558 dump_gimple_expr (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
2559 const int expected_impl_line
= __LINE__
- 1;
2561 ASSERT_DUMPED_TEXT_EQ (tmp
, "return;");
2564 optinfo
*info
= tmp
.get_pending_optinfo ();
2565 ASSERT_TRUE (info
!= NULL
);
2566 ASSERT_EQ (info
->num_items (), 1);
2567 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;");
2568 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2569 "dumpfile.c", expected_impl_line
,
2570 "test_capture_of_dump_calls");
2577 temp_dump_context
tmp (with_optinfo
, true,
2578 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2579 dump_symtab_node (MSG_NOTE
, node
);
2580 const int expected_impl_line
= __LINE__
- 1;
2582 ASSERT_DUMPED_TEXT_EQ (tmp
, "test_decl/0");
2585 optinfo
*info
= tmp
.get_pending_optinfo ();
2586 ASSERT_TRUE (info
!= NULL
);
2587 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2588 ASSERT_EQ (info
->num_items (), 1);
2589 ASSERT_IS_SYMTAB_NODE (info
->get_item (0), decl_loc
, "test_decl/0");
2590 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2591 "dumpfile.c", expected_impl_line
,
2592 "test_capture_of_dump_calls");
2598 temp_dump_context
tmp (with_optinfo
, true,
2599 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2600 dump_dec (MSG_NOTE
, poly_int64 (42));
2601 const int expected_impl_line
= __LINE__
- 1;
2603 ASSERT_DUMPED_TEXT_EQ (tmp
, "42");
2606 optinfo
*info
= tmp
.get_pending_optinfo ();
2607 ASSERT_TRUE (info
!= NULL
);
2608 ASSERT_EQ (info
->num_items (), 1);
2609 ASSERT_IS_TEXT (info
->get_item (0), "42");
2610 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2611 "dumpfile.c", expected_impl_line
,
2612 "test_capture_of_dump_calls");
2616 /* Scopes. Test with all 4 combinations of
2617 filtering by MSG_PRIORITY_USER_FACING
2618 and/or filtering by MSG_PRIORITY_INTERNALS. */
2619 for (int j
= 0; j
< 3; j
++)
2621 dump_flags_t dump_filter
= MSG_ALL_KINDS
;
2623 dump_filter
|= MSG_PRIORITY_USER_FACING
;
2625 dump_filter
|= MSG_PRIORITY_INTERNALS
;
2627 temp_dump_context
tmp (with_optinfo
, true, dump_filter
);
2628 /* Emit various messages, mostly with implicit priority. */
2629 dump_printf_loc (MSG_NOTE
, stmt
, "msg 1\n");
2630 dump_printf_loc (MSG_NOTE
| MSG_PRIORITY_INTERNALS
, stmt
,
2631 "explicitly internal msg\n");
2633 AUTO_DUMP_SCOPE ("outer scope", stmt
);
2634 dump_printf_loc (MSG_NOTE
, stmt
, "msg 2\n");
2636 AUTO_DUMP_SCOPE ("middle scope", stmt
);
2637 dump_printf_loc (MSG_NOTE
, stmt
, "msg 3\n");
2639 AUTO_DUMP_SCOPE ("inner scope", stmt
);
2640 dump_printf_loc (MSG_NOTE
, stmt
, "msg 4\n");
2641 dump_printf_loc (MSG_NOTE
| MSG_PRIORITY_USER_FACING
, stmt
,
2642 "explicitly user-facing msg\n");
2644 dump_printf_loc (MSG_NOTE
, stmt
, "msg 5\n");
2646 dump_printf_loc (MSG_NOTE
, stmt
, "msg 6\n");
2648 dump_printf_loc (MSG_NOTE
, stmt
, "msg 7\n");
2649 const int expected_impl_line
= __LINE__
- 1;
2651 switch (dump_filter
& MSG_ALL_PRIORITIES
)
2656 ASSERT_DUMPED_TEXT_EQ (tmp
, "");
2658 case MSG_PRIORITY_USER_FACING
:
2659 ASSERT_DUMPED_TEXT_EQ
2661 "test.txt:5:10: note: msg 1\n"
2662 "test.txt:5:10: note: explicitly user-facing msg\n"
2663 "test.txt:5:10: note: msg 7\n");
2665 case MSG_PRIORITY_INTERNALS
:
2666 ASSERT_DUMPED_TEXT_EQ
2668 "test.txt:5:10: note: explicitly internal msg\n"
2669 "test.txt:5:10: note: === outer scope ===\n"
2670 "test.txt:5:10: note: msg 2\n"
2671 "test.txt:5:10: note: === middle scope ===\n"
2672 "test.txt:5:10: note: msg 3\n"
2673 "test.txt:5:10: note: === inner scope ===\n"
2674 "test.txt:5:10: note: msg 4\n"
2675 "test.txt:5:10: note: msg 5\n"
2676 "test.txt:5:10: note: msg 6\n");
2678 case MSG_ALL_PRIORITIES
:
2679 ASSERT_DUMPED_TEXT_EQ
2681 "test.txt:5:10: note: msg 1\n"
2682 "test.txt:5:10: note: explicitly internal msg\n"
2683 "test.txt:5:10: note: === outer scope ===\n"
2684 "test.txt:5:10: note: msg 2\n"
2685 "test.txt:5:10: note: === middle scope ===\n"
2686 "test.txt:5:10: note: msg 3\n"
2687 "test.txt:5:10: note: === inner scope ===\n"
2688 "test.txt:5:10: note: msg 4\n"
2689 "test.txt:5:10: note: explicitly user-facing msg\n"
2690 "test.txt:5:10: note: msg 5\n"
2691 "test.txt:5:10: note: msg 6\n"
2692 "test.txt:5:10: note: msg 7\n");
2697 optinfo
*info
= tmp
.get_pending_optinfo ();
2698 ASSERT_TRUE (info
!= NULL
);
2699 ASSERT_EQ (info
->num_items (), 1);
2700 ASSERT_IS_TEXT (info
->get_item (0), "msg 7\n");
2701 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2702 "dumpfile.c", expected_impl_line
,
2703 "test_capture_of_dump_calls");
2708 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2711 /* MSG_OPTIMIZED_LOCATIONS. */
2713 temp_dump_context
tmp (true, true, MSG_ALL_KINDS
);
2714 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
, "test");
2715 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
2716 OPTINFO_KIND_SUCCESS
);
2719 /* MSG_MISSED_OPTIMIZATION. */
2721 temp_dump_context
tmp (true, true, MSG_ALL_KINDS
);
2722 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
, "test");
2723 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
2724 OPTINFO_KIND_FAILURE
);
2728 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2730 temp_dump_context
tmp (false, true,
2731 MSG_OPTIMIZED_LOCATIONS
| MSG_ALL_PRIORITIES
);
2732 dump_printf_loc (MSG_NOTE
, stmt
, "msg 1\n");
2734 AUTO_DUMP_SCOPE ("outer scope", stmt
);
2735 dump_printf_loc (MSG_NOTE
, stmt
, "msg 2\n");
2737 AUTO_DUMP_SCOPE ("middle scope", stmt
);
2738 dump_printf_loc (MSG_NOTE
, stmt
, "msg 3\n");
2740 AUTO_DUMP_SCOPE ("inner scope", stmt
);
2741 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
, "msg 4\n");
2743 dump_printf_loc (MSG_NOTE
, stmt
, "msg 5\n");
2745 dump_printf_loc (MSG_NOTE
, stmt
, "msg 6\n");
2747 dump_printf_loc (MSG_NOTE
, stmt
, "msg 7\n");
2749 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: optimized: msg 4\n");
2756 dump_user_location_t loc
2757 = dump_user_location_t::from_location_t (UNKNOWN_LOCATION
);
2759 temp_dump_context
tmp (true, true,
2760 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2762 AUTO_DUMP_SCOPE ("outer scope", loc
);
2763 dump_printf (MSG_NOTE
, "msg1\n");
2767 /* Run all of the selftests within this file. */
2772 test_impl_location ();
2773 for_each_line_table_case (test_capture_of_dump_calls
);
2777 } // namespace selftest
2779 #endif /* CHECKING_P */