1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2020 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 {"all", dump_flags_t (TDF_ALL_VALUES
149 & ~(TDF_RAW
| TDF_SLIM
| TDF_LINENO
| TDF_GRAPH
150 | TDF_STMTADDR
| TDF_RHS_ONLY
| TDF_NOUID
151 | TDF_ENUMERATE_LOCALS
| TDF_SCEV
| TDF_GIMPLE
))},
155 /* A subset of the dump_options table which is used for -fopt-info
156 types. This must be consistent with the MSG_* flags in dumpfile.h.
158 static const kv_pair
<dump_flags_t
> optinfo_verbosity_options
[] =
160 {"optimized", MSG_OPTIMIZED_LOCATIONS
},
161 {"missed", MSG_MISSED_OPTIMIZATION
},
163 {"all", MSG_ALL_KINDS
},
164 {"internals", MSG_PRIORITY_INTERNALS
},
168 /* Flags used for -fopt-info groups. */
169 const kv_pair
<optgroup_flags_t
> optgroup_options
[] =
171 {"ipa", OPTGROUP_IPA
},
172 {"loop", OPTGROUP_LOOP
},
173 {"inline", OPTGROUP_INLINE
},
174 {"omp", OPTGROUP_OMP
},
175 {"vec", OPTGROUP_VEC
},
176 {"optall", OPTGROUP_ALL
},
177 {NULL
, OPTGROUP_NONE
}
180 gcc::dump_manager::dump_manager ():
181 m_next_dump (FIRST_AUTO_NUMBERED_DUMP
),
182 m_extra_dump_files (NULL
),
183 m_extra_dump_files_in_use (0),
184 m_extra_dump_files_alloced (0),
185 m_optgroup_flags (OPTGROUP_NONE
),
186 m_optinfo_flags (TDF_NONE
),
187 m_optinfo_filename (NULL
)
191 gcc::dump_manager::~dump_manager ()
193 free (m_optinfo_filename
);
194 for (size_t i
= 0; i
< m_extra_dump_files_in_use
; i
++)
196 dump_file_info
*dfi
= &m_extra_dump_files
[i
];
197 /* suffix, swtch, glob are statically allocated for the entries
198 in dump_files, and for statistics, but are dynamically allocated
199 for those for passes. */
200 if (dfi
->owns_strings
)
202 XDELETEVEC (const_cast <char *> (dfi
->suffix
));
203 XDELETEVEC (const_cast <char *> (dfi
->swtch
));
204 XDELETEVEC (const_cast <char *> (dfi
->glob
));
206 /* These, if non-NULL, are always dynamically allocated. */
207 XDELETEVEC (const_cast <char *> (dfi
->pfilename
));
208 XDELETEVEC (const_cast <char *> (dfi
->alt_filename
));
210 XDELETEVEC (m_extra_dump_files
);
215 dump_register (const char *suffix
, const char *swtch
, const char *glob
,
216 dump_kind dkind
, optgroup_flags_t optgroup_flags
,
219 int num
= m_next_dump
++;
221 size_t count
= m_extra_dump_files_in_use
++;
223 if (count
>= m_extra_dump_files_alloced
)
225 if (m_extra_dump_files_alloced
== 0)
226 m_extra_dump_files_alloced
= 512;
228 m_extra_dump_files_alloced
*= 2;
229 m_extra_dump_files
= XRESIZEVEC (struct dump_file_info
,
231 m_extra_dump_files_alloced
);
233 /* Construct a new object in the space allocated above. */
234 new (m_extra_dump_files
+ count
) dump_file_info ();
238 /* Zero out the already constructed object. */
239 m_extra_dump_files
[count
] = dump_file_info ();
242 m_extra_dump_files
[count
].suffix
= suffix
;
243 m_extra_dump_files
[count
].swtch
= swtch
;
244 m_extra_dump_files
[count
].glob
= glob
;
245 m_extra_dump_files
[count
].dkind
= dkind
;
246 m_extra_dump_files
[count
].optgroup_flags
= optgroup_flags
;
247 m_extra_dump_files
[count
].num
= num
;
248 m_extra_dump_files
[count
].owns_strings
= take_ownership
;
250 return count
+ TDI_end
;
254 /* Allow languages and middle-end to register their dumps before the
255 optimization passes. */
261 lang_hooks
.register_dumps (this);
262 /* If this assert fails, some FE registered more than
263 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
264 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
265 gcc_assert (m_next_dump
<= FIRST_ME_AUTO_NUMBERED_DUMP
);
266 m_next_dump
= FIRST_ME_AUTO_NUMBERED_DUMP
;
267 dump_files
[TDI_original
].num
= m_next_dump
++;
268 dump_files
[TDI_gimple
].num
= m_next_dump
++;
269 dump_files
[TDI_nested
].num
= m_next_dump
++;
273 /* Return the dump_file_info for the given phase. */
275 struct dump_file_info
*
277 get_dump_file_info (int phase
) const
280 return &dump_files
[phase
];
281 else if ((size_t) (phase
- TDI_end
) >= m_extra_dump_files_in_use
)
284 return m_extra_dump_files
+ (phase
- TDI_end
);
287 /* Locate the dump_file_info with swtch equal to SWTCH,
288 or return NULL if no such dump_file_info exists. */
290 struct dump_file_info
*
292 get_dump_file_info_by_switch (const char *swtch
) const
294 for (unsigned i
= 0; i
< m_extra_dump_files_in_use
; i
++)
295 if (strcmp (m_extra_dump_files
[i
].swtch
, swtch
) == 0)
296 return &m_extra_dump_files
[i
];
303 /* Return the name of the dump file for the given phase.
304 The caller is responsible for calling free on the returned
306 If the dump is not enabled, returns NULL. */
310 get_dump_file_name (int phase
, int part
) const
312 struct dump_file_info
*dfi
;
314 if (phase
== TDI_none
)
317 dfi
= get_dump_file_info (phase
);
319 return get_dump_file_name (dfi
, part
);
322 /* Return the name of the dump file for the given dump_file_info.
323 The caller is responsible for calling free on the returned
325 If the dump is not enabled, returns NULL. */
329 get_dump_file_name (struct dump_file_info
*dfi
, int part
) const
335 if (dfi
->pstate
== 0)
338 /* If available, use the command line dump filename. */
340 return xstrdup (dfi
->pfilename
);
346 /* (null), LANG, TREE, RTL, IPA. */
347 char suffix
= " ltri"[dfi
->dkind
];
349 if (snprintf (dump_id
, sizeof (dump_id
), ".%03d%c", dfi
->num
, suffix
) < 0)
356 snprintf (part_id
, sizeof (part_id
), ".%i", part
);
357 return concat (dump_base_name
, dump_id
, part_id
, dfi
->suffix
, NULL
);
360 return concat (dump_base_name
, dump_id
, dfi
->suffix
, NULL
);
363 /* Open a dump file called FILENAME. Some filenames are special and
364 refer to the standard streams. TRUNC indicates whether this is the
365 first open (so the file should be truncated, rather than appended).
366 An error message is emitted in the event of failure. */
369 dump_open (const char *filename
, bool trunc
)
371 if (strcmp ("stderr", filename
) == 0)
374 if (strcmp ("stdout", filename
) == 0
375 || strcmp ("-", filename
) == 0)
378 FILE *stream
= fopen (filename
, trunc
? "w" : "a");
381 error ("could not open dump file %qs: %m", filename
);
385 /* For a given DFI, open an alternate dump filename (which could also
386 be a standard stream such as stdout/stderr). If the alternate dump
387 file cannot be opened, return NULL. */
390 dump_open_alternate_stream (struct dump_file_info
*dfi
)
392 if (!dfi
->alt_filename
)
396 return dfi
->alt_stream
;
398 FILE *stream
= dump_open (dfi
->alt_filename
, dfi
->alt_state
< 0);
406 /* Construct a dump_user_location_t from STMT (using its location and
409 dump_user_location_t::dump_user_location_t (const gimple
*stmt
)
410 : m_count (), m_loc (UNKNOWN_LOCATION
)
415 m_count
= stmt
->bb
->count
;
416 m_loc
= gimple_location (stmt
);
420 /* Construct a dump_user_location_t from an RTL instruction (using its
421 location and hotness). */
423 dump_user_location_t::dump_user_location_t (const rtx_insn
*insn
)
424 : m_count (), m_loc (UNKNOWN_LOCATION
)
428 basic_block bb
= BLOCK_FOR_INSN (insn
);
431 m_loc
= INSN_LOCATION (insn
);
435 /* Construct from a function declaration. This one requires spelling out
436 to avoid accidentally constructing from other kinds of tree. */
439 dump_user_location_t::from_function_decl (tree fndecl
)
443 // FIXME: profile count for function?
444 return dump_user_location_t (profile_count (),
445 DECL_SOURCE_LOCATION (fndecl
));
448 /* Extract the MSG_* component from DUMP_KIND and return a string for use
449 as a prefix to dump messages.
450 These match the strings in optinfo_verbosity_options and thus the
451 "OPTIONS" within "-fopt-info-OPTIONS". */
454 kind_as_string (dump_flags_t dump_kind
)
456 switch (dump_kind
& MSG_ALL_KINDS
)
460 case MSG_OPTIMIZED_LOCATIONS
:
462 case MSG_MISSED_OPTIMIZATION
:
469 /* Print source location on DFILE if enabled. */
472 dump_loc (dump_flags_t dump_kind
, FILE *dfile
, location_t loc
)
476 if (LOCATION_LOCUS (loc
) > BUILTINS_LOCATION
)
477 fprintf (dfile
, "%s:%d:%d: ", LOCATION_FILE (loc
),
478 LOCATION_LINE (loc
), LOCATION_COLUMN (loc
));
479 else if (current_function_decl
)
480 fprintf (dfile
, "%s:%d:%d: ",
481 DECL_SOURCE_FILE (current_function_decl
),
482 DECL_SOURCE_LINE (current_function_decl
),
483 DECL_SOURCE_COLUMN (current_function_decl
));
484 fprintf (dfile
, "%s: ", kind_as_string (dump_kind
));
485 /* Indentation based on scope depth. */
486 fprintf (dfile
, "%*s", get_dump_scope_depth (), "");
490 /* Print source location to PP if enabled. */
493 dump_loc (dump_flags_t dump_kind
, pretty_printer
*pp
, location_t loc
)
497 if (LOCATION_LOCUS (loc
) > BUILTINS_LOCATION
)
498 pp_printf (pp
, "%s:%d:%d: ", LOCATION_FILE (loc
),
499 LOCATION_LINE (loc
), LOCATION_COLUMN (loc
));
500 else if (current_function_decl
)
501 pp_printf (pp
, "%s:%d:%d: ",
502 DECL_SOURCE_FILE (current_function_decl
),
503 DECL_SOURCE_LINE (current_function_decl
),
504 DECL_SOURCE_COLUMN (current_function_decl
));
505 pp_printf (pp
, "%s: ", kind_as_string (dump_kind
));
506 /* Indentation based on scope depth. */
507 for (unsigned i
= 0; i
< get_dump_scope_depth (); i
++)
508 pp_character (pp
, ' ');
512 /* Implementation of dump_context member functions. */
514 /* dump_context's dtor. */
516 dump_context::~dump_context ()
522 dump_context::set_json_writer (optrecord_json_writer
*writer
)
524 delete m_json_writer
;
525 m_json_writer
= writer
;
528 /* Perform cleanup activity for -fsave-optimization-record.
529 Currently, the file is written out here in one go, before cleaning
533 dump_context::finish_any_json_writer ()
538 m_json_writer
->write ();
539 delete m_json_writer
;
540 m_json_writer
= NULL
;
543 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
544 or alt_dump_file change, or when changing dump_context in selftests. */
547 dump_context::refresh_dumps_are_enabled ()
549 dumps_are_enabled
= (dump_file
|| alt_dump_file
|| optinfo_enabled_p ()
553 /* Determine if a message of kind DUMP_KIND and at the current scope depth
556 Only show messages that match FILTER both on their kind *and*
560 dump_context::apply_dump_filter_p (dump_flags_t dump_kind
,
561 dump_flags_t filter
) const
563 /* Few messages, if any, have an explicit MSG_PRIORITY.
564 If DUMP_KIND does, we'll use it.
565 Otherwise, generate an implicit priority value for the message based
566 on the current scope depth.
567 Messages at the top-level scope are MSG_PRIORITY_USER_FACING,
568 whereas those in nested scopes are MSG_PRIORITY_INTERNALS. */
569 if (!(dump_kind
& MSG_ALL_PRIORITIES
))
571 dump_flags_t implicit_priority
573 ? MSG_PRIORITY_INTERNALS
574 : MSG_PRIORITY_USER_FACING
);
575 dump_kind
|= implicit_priority
;
578 return (dump_kind
& (filter
& MSG_ALL_KINDS
)
579 && dump_kind
& (filter
& MSG_ALL_PRIORITIES
));
582 /* Print LOC to the appropriate dump destinations, given DUMP_KIND.
583 If optinfos are enabled, begin a new optinfo. */
586 dump_context::dump_loc (const dump_metadata_t
&metadata
,
587 const dump_user_location_t
&loc
)
591 dump_loc_immediate (metadata
.get_dump_flags (), loc
);
593 if (optinfo_enabled_p ())
594 begin_next_optinfo (metadata
, loc
);
597 /* As dump_loc above, but without starting a new optinfo. */
600 dump_context::dump_loc_immediate (dump_flags_t dump_kind
,
601 const dump_user_location_t
&loc
)
603 location_t srcloc
= loc
.get_location_t ();
605 if (dump_file
&& apply_dump_filter_p (dump_kind
, pflags
))
606 ::dump_loc (dump_kind
, dump_file
, srcloc
);
608 if (alt_dump_file
&& apply_dump_filter_p (dump_kind
, alt_flags
))
609 ::dump_loc (dump_kind
, alt_dump_file
, srcloc
);
611 /* Support for temp_dump_context in selftests. */
612 if (m_test_pp
&& apply_dump_filter_p (dump_kind
, m_test_pp_flags
))
613 ::dump_loc (dump_kind
, m_test_pp
, srcloc
);
616 /* Make an item for the given dump call, equivalent to print_gimple_stmt. */
618 static optinfo_item
*
619 make_item_for_dump_gimple_stmt (gimple
*stmt
, int spc
, dump_flags_t dump_flags
)
622 pp_needs_newline (&pp
) = true;
623 pp_gimple_stmt_1 (&pp
, stmt
, spc
, dump_flags
);
627 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE
, gimple_location (stmt
),
628 xstrdup (pp_formatted_text (&pp
)));
632 /* Dump gimple statement GS with SPC indentation spaces and
633 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
636 dump_context::dump_gimple_stmt (const dump_metadata_t
&metadata
,
637 dump_flags_t extra_dump_flags
,
641 = make_item_for_dump_gimple_stmt (gs
, spc
, dump_flags
| extra_dump_flags
);
642 emit_item (item
, metadata
.get_dump_flags ());
644 if (optinfo_enabled_p ())
646 optinfo
&info
= ensure_pending_optinfo (metadata
);
647 info
.add_item (item
);
653 /* Similar to dump_gimple_stmt, except additionally print source location. */
656 dump_context::dump_gimple_stmt_loc (const dump_metadata_t
&metadata
,
657 const dump_user_location_t
&loc
,
658 dump_flags_t extra_dump_flags
,
661 dump_loc (metadata
, loc
);
662 dump_gimple_stmt (metadata
, extra_dump_flags
, gs
, spc
);
665 /* Make an item for the given dump call, equivalent to print_gimple_expr. */
667 static optinfo_item
*
668 make_item_for_dump_gimple_expr (gimple
*stmt
, int spc
, dump_flags_t dump_flags
)
670 dump_flags
|= TDF_RHS_ONLY
;
672 pp_needs_newline (&pp
) = true;
673 pp_gimple_stmt_1 (&pp
, stmt
, spc
, dump_flags
);
676 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE
, gimple_location (stmt
),
677 xstrdup (pp_formatted_text (&pp
)));
681 /* Dump gimple statement GS with SPC indentation spaces and
682 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
683 Do not terminate with a newline or semicolon. */
686 dump_context::dump_gimple_expr (const dump_metadata_t
&metadata
,
687 dump_flags_t extra_dump_flags
,
691 = make_item_for_dump_gimple_expr (gs
, spc
, dump_flags
| extra_dump_flags
);
692 emit_item (item
, metadata
.get_dump_flags ());
694 if (optinfo_enabled_p ())
696 optinfo
&info
= ensure_pending_optinfo (metadata
);
697 info
.add_item (item
);
703 /* Similar to dump_gimple_expr, except additionally print source location. */
706 dump_context::dump_gimple_expr_loc (const dump_metadata_t
&metadata
,
707 const dump_user_location_t
&loc
,
708 dump_flags_t extra_dump_flags
,
712 dump_loc (metadata
, loc
);
713 dump_gimple_expr (metadata
, extra_dump_flags
, gs
, spc
);
716 /* Make an item for the given dump call, equivalent to print_generic_expr. */
718 static optinfo_item
*
719 make_item_for_dump_generic_expr (tree node
, dump_flags_t dump_flags
)
722 pp_needs_newline (&pp
) = true;
723 pp_translate_identifiers (&pp
) = false;
724 dump_generic_node (&pp
, node
, 0, dump_flags
, false);
726 location_t loc
= UNKNOWN_LOCATION
;
727 if (EXPR_HAS_LOCATION (node
))
728 loc
= EXPR_LOCATION (node
);
731 = new optinfo_item (OPTINFO_ITEM_KIND_TREE
, loc
,
732 xstrdup (pp_formatted_text (&pp
)));
736 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
737 DUMP_KIND is enabled. */
740 dump_context::dump_generic_expr (const dump_metadata_t
&metadata
,
741 dump_flags_t extra_dump_flags
,
745 = make_item_for_dump_generic_expr (t
, dump_flags
| extra_dump_flags
);
746 emit_item (item
, metadata
.get_dump_flags ());
748 if (optinfo_enabled_p ())
750 optinfo
&info
= ensure_pending_optinfo (metadata
);
751 info
.add_item (item
);
758 /* Similar to dump_generic_expr, except additionally print the source
762 dump_context::dump_generic_expr_loc (const dump_metadata_t
&metadata
,
763 const dump_user_location_t
&loc
,
764 dump_flags_t extra_dump_flags
,
767 dump_loc (metadata
, loc
);
768 dump_generic_expr (metadata
, extra_dump_flags
, t
);
771 /* Make an item for the given dump call. */
773 static optinfo_item
*
774 make_item_for_dump_symtab_node (symtab_node
*node
)
776 location_t loc
= DECL_SOURCE_LOCATION (node
->decl
);
778 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE
, loc
,
779 xstrdup (node
->dump_name ()));
783 /* dump_pretty_printer's ctor. */
785 dump_pretty_printer::dump_pretty_printer (dump_context
*context
,
786 dump_flags_t dump_kind
)
787 : pretty_printer (), m_context (context
), m_dump_kind (dump_kind
),
790 pp_format_decoder (this) = format_decoder_cb
;
793 /* Phase 3 of formatting; compare with pp_output_formatted_text.
795 Emit optinfo_item instances for the various formatted chunks from phases
796 1 and 2 (i.e. pp_format).
798 Some chunks may already have had their items built (during decode_format).
799 These chunks have been stashed into m_stashed_items; we emit them here.
801 For all other purely textual chunks, they are printed into
802 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
803 This consolidates multiple adjacent text chunks into a single text
807 dump_pretty_printer::emit_items (optinfo
*dest
)
809 output_buffer
*buffer
= pp_buffer (this);
810 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
811 const char **args
= chunk_array
->args
;
813 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
814 gcc_assert (buffer
->line_length
== 0);
816 unsigned stashed_item_idx
= 0;
817 for (unsigned chunk
= 0; args
[chunk
]; chunk
++)
819 if (stashed_item_idx
< m_stashed_items
.length ()
820 && args
[chunk
] == *m_stashed_items
[stashed_item_idx
].buffer_ptr
)
822 emit_any_pending_textual_chunks (dest
);
823 /* This chunk has a stashed item: use it. */
824 emit_item (m_stashed_items
[stashed_item_idx
++].item
, dest
);
827 /* This chunk is purely textual. Print it (to
828 buffer->formatted_obstack), so that we can consolidate adjacent
829 chunks into one textual optinfo_item. */
830 pp_string (this, args
[chunk
]);
833 emit_any_pending_textual_chunks (dest
);
835 /* Ensure that we consumed all of stashed_items. */
836 gcc_assert (stashed_item_idx
== m_stashed_items
.length ());
838 /* Deallocate the chunk structure and everything after it (i.e. the
839 associated series of formatted strings). */
840 buffer
->cur_chunk_array
= chunk_array
->prev
;
841 obstack_free (&buffer
->chunk_obstack
, chunk_array
);
844 /* Subroutine of dump_pretty_printer::emit_items
845 for consolidating multiple adjacent pure-text chunks into single
846 optinfo_items (in phase 3). */
849 dump_pretty_printer::emit_any_pending_textual_chunks (optinfo
*dest
)
851 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
853 /* Don't emit an item if the pending text is empty. */
854 if (output_buffer_last_position_in_text (buffer
) == NULL
)
857 char *formatted_text
= xstrdup (pp_formatted_text (this));
859 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT
, UNKNOWN_LOCATION
,
861 emit_item (item
, dest
);
863 /* Clear the pending text by unwinding formatted_text back to the start
864 of the buffer (without deallocating). */
865 obstack_free (&buffer
->formatted_obstack
,
866 buffer
->formatted_obstack
.object_base
);
869 /* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
870 to DEST; otherwise delete ITEM. */
873 dump_pretty_printer::emit_item (optinfo_item
*item
, optinfo
*dest
)
875 m_context
->emit_item (item
, m_dump_kind
);
877 dest
->add_item (item
);
882 /* Record that ITEM (generated in phase 2 of formatting) is to be used for
883 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
886 dump_pretty_printer::stash_item (const char **buffer_ptr
, optinfo_item
*item
)
888 gcc_assert (buffer_ptr
);
891 m_stashed_items
.safe_push (stashed_item (buffer_ptr
, item
));
894 /* pp_format_decoder callback for dump_pretty_printer, and thus for
895 dump_printf and dump_printf_loc.
897 A wrapper around decode_format, for type-safety. */
900 dump_pretty_printer::format_decoder_cb (pretty_printer
*pp
, text_info
*text
,
901 const char *spec
, int /*precision*/,
902 bool /*wide*/, bool /*set_locus*/,
903 bool /*verbose*/, bool */
*quoted*/
,
904 const char **buffer_ptr
)
906 dump_pretty_printer
*opp
= static_cast <dump_pretty_printer
*> (pp
);
907 return opp
->decode_format (text
, spec
, buffer_ptr
);
910 /* Format decoder for dump_pretty_printer, and thus for dump_printf and
913 Supported format codes (in addition to the standard pretty_printer ones)
917 Equivalent to: dump_symtab_node (MSG_*, node)
919 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
921 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
923 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
925 TODO: add a format code that can handle (symtab_node*) *and* both
926 subclasses (presumably means teaching -Wformat about non-virtual
929 These format codes build optinfo_item instances, thus capturing metadata
930 about the arguments being dumped, as well as the textual output. */
933 dump_pretty_printer::decode_format (text_info
*text
, const char *spec
,
934 const char **buffer_ptr
)
936 /* Various format codes that imply making an optinfo_item and stashed it
937 for later use (to capture metadata, rather than plain text). */
942 cgraph_node
*node
= va_arg (*text
->args_ptr
, cgraph_node
*);
944 /* Make an item for the node, and stash it. */
945 optinfo_item
*item
= make_item_for_dump_symtab_node (node
);
946 stash_item (buffer_ptr
, item
);
952 gimple
*stmt
= va_arg (*text
->args_ptr
, gimple
*);
954 /* Make an item for the stmt, and stash it. */
955 optinfo_item
*item
= make_item_for_dump_gimple_expr (stmt
, 0, TDF_SLIM
);
956 stash_item (buffer_ptr
, item
);
962 gimple
*stmt
= va_arg (*text
->args_ptr
, gimple
*);
964 /* Make an item for the stmt, and stash it. */
965 optinfo_item
*item
= make_item_for_dump_gimple_stmt (stmt
, 0, TDF_SLIM
);
966 stash_item (buffer_ptr
, item
);
972 tree t
= va_arg (*text
->args_ptr
, tree
);
974 /* Make an item for the tree, and stash it. */
975 optinfo_item
*item
= make_item_for_dump_generic_expr (t
, TDF_SLIM
);
976 stash_item (buffer_ptr
, item
);
985 /* Output a formatted message using FORMAT on appropriate dump streams. */
988 dump_context::dump_printf_va (const dump_metadata_t
&metadata
, const char *format
,
991 dump_pretty_printer
pp (this, metadata
.get_dump_flags ());
996 text
.format_spec
= format
;
998 /* Phases 1 and 2, using pp_format. */
999 pp_format (&pp
, &text
);
1002 if (optinfo_enabled_p ())
1004 optinfo
&info
= ensure_pending_optinfo (metadata
);
1005 pp
.emit_items (&info
);
1008 pp
.emit_items (NULL
);
1011 /* Similar to dump_printf, except source location is also printed, and
1012 dump location captured. */
1015 dump_context::dump_printf_loc_va (const dump_metadata_t
&metadata
,
1016 const dump_user_location_t
&loc
,
1017 const char *format
, va_list *ap
)
1019 dump_loc (metadata
, loc
);
1020 dump_printf_va (metadata
, format
, ap
);
1023 /* Make an item for the given dump call, equivalent to print_dec. */
1025 template<unsigned int N
, typename C
>
1026 static optinfo_item
*
1027 make_item_for_dump_dec (const poly_int
<N
, C
> &value
)
1029 STATIC_ASSERT (poly_coeff_traits
<C
>::signedness
>= 0);
1030 signop sgn
= poly_coeff_traits
<C
>::signedness
? SIGNED
: UNSIGNED
;
1034 if (value
.is_constant ())
1035 pp_wide_int (&pp
, value
.coeffs
[0], sgn
);
1038 pp_character (&pp
, '[');
1039 for (unsigned int i
= 0; i
< N
; ++i
)
1041 pp_wide_int (&pp
, value
.coeffs
[i
], sgn
);
1042 pp_character (&pp
, i
== N
- 1 ? ']' : ',');
1047 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT
, UNKNOWN_LOCATION
,
1048 xstrdup (pp_formatted_text (&pp
)));
1052 /* Output VALUE in decimal to appropriate dump streams. */
1054 template<unsigned int N
, typename C
>
1056 dump_context::dump_dec (const dump_metadata_t
&metadata
, const poly_int
<N
, C
> &value
)
1058 optinfo_item
*item
= make_item_for_dump_dec (value
);
1059 emit_item (item
, metadata
.get_dump_flags ());
1061 if (optinfo_enabled_p ())
1063 optinfo
&info
= ensure_pending_optinfo (metadata
);
1064 info
.add_item (item
);
1070 /* Output the name of NODE on appropriate dump streams. */
1073 dump_context::dump_symtab_node (const dump_metadata_t
&metadata
, symtab_node
*node
)
1075 optinfo_item
*item
= make_item_for_dump_symtab_node (node
);
1076 emit_item (item
, metadata
.get_dump_flags ());
1078 if (optinfo_enabled_p ())
1080 optinfo
&info
= ensure_pending_optinfo (metadata
);
1081 info
.add_item (item
);
1087 /* Get the current dump scope-nesting depth.
1088 For use by -fopt-info (for showing nesting via indentation). */
1091 dump_context::get_scope_depth () const
1093 return m_scope_depth
;
1096 /* Push a nested dump scope.
1097 Increment the scope depth.
1098 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1099 destination, if any.
1100 Emit a "scope" optinfo if optinfos are enabled. */
1103 dump_context::begin_scope (const char *name
,
1104 const dump_user_location_t
&user_location
,
1105 const dump_impl_location_t
&impl_location
)
1109 location_t src_loc
= user_location
.get_location_t ();
1111 if (dump_file
&& apply_dump_filter_p (MSG_NOTE
, pflags
))
1112 ::dump_loc (MSG_NOTE
, dump_file
, src_loc
);
1114 if (alt_dump_file
&& apply_dump_filter_p (MSG_NOTE
, alt_flags
))
1115 ::dump_loc (MSG_NOTE
, alt_dump_file
, src_loc
);
1117 /* Support for temp_dump_context in selftests. */
1118 if (m_test_pp
&& apply_dump_filter_p (MSG_NOTE
, m_test_pp_flags
))
1119 ::dump_loc (MSG_NOTE
, m_test_pp
, src_loc
);
1122 pp_printf (&pp
, "=== %s ===\n", name
);
1124 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT
, UNKNOWN_LOCATION
,
1125 xstrdup (pp_formatted_text (&pp
)));
1126 emit_item (item
, MSG_NOTE
);
1128 if (optinfo_enabled_p ())
1131 = begin_next_optinfo (dump_metadata_t (MSG_NOTE
, impl_location
),
1133 info
.m_kind
= OPTINFO_KIND_SCOPE
;
1134 info
.add_item (item
);
1141 /* Pop a nested dump scope. */
1144 dump_context::end_scope ()
1150 m_json_writer
->pop_scope ();
1153 /* Should optinfo instances be created?
1154 All creation of optinfos should be guarded by this predicate.
1155 Return true if any optinfo destinations are active. */
1158 dump_context::optinfo_enabled_p () const
1160 return (optimization_records_enabled_p ());
1163 /* Return the optinfo currently being accumulated, creating one if
1167 dump_context::ensure_pending_optinfo (const dump_metadata_t
&metadata
)
1170 return begin_next_optinfo (metadata
, dump_user_location_t ());
1174 /* Start a new optinfo and return it, ending any optinfo that was already
1178 dump_context::begin_next_optinfo (const dump_metadata_t
&metadata
,
1179 const dump_user_location_t
&user_loc
)
1182 gcc_assert (m_pending
== NULL
);
1183 dump_location_t
loc (user_loc
, metadata
.get_impl_location ());
1184 m_pending
= new optinfo (loc
, OPTINFO_KIND_NOTE
, current_pass
);
1185 m_pending
->handle_dump_file_kind (metadata
.get_dump_flags ());
1189 /* End any optinfo that has been accumulated within this context; emitting
1190 it to any destinations as appropriate, such as optimization records. */
1193 dump_context::end_any_optinfo ()
1196 emit_optinfo (m_pending
);
1201 /* Emit the optinfo to all of the "non-immediate" destinations
1202 (emission to "immediate" destinations is done by
1203 dump_context::emit_item). */
1206 dump_context::emit_optinfo (const optinfo
*info
)
1208 /* -fsave-optimization-record. */
1210 m_json_writer
->add_record (info
);
1213 /* Emit ITEM to all item destinations (those that don't require
1214 consolidation into optinfo instances). */
1217 dump_context::emit_item (optinfo_item
*item
, dump_flags_t dump_kind
)
1219 if (dump_file
&& apply_dump_filter_p (dump_kind
, pflags
))
1220 fprintf (dump_file
, "%s", item
->get_text ());
1222 if (alt_dump_file
&& apply_dump_filter_p (dump_kind
, alt_flags
))
1223 fprintf (alt_dump_file
, "%s", item
->get_text ());
1225 /* Support for temp_dump_context in selftests. */
1226 if (m_test_pp
&& apply_dump_filter_p (dump_kind
, m_test_pp_flags
))
1227 pp_string (m_test_pp
, item
->get_text ());
1230 /* The current singleton dump_context, and its default. */
1232 dump_context
*dump_context::s_current
= &dump_context::s_default
;
1233 dump_context
dump_context::s_default
;
1235 /* Implementation of dump_* API calls, calling into dump_context
1236 member functions. */
1238 /* Calls to the dump_* functions do non-trivial work, so they ought
1240 if (dump_enabled_p ())
1241 Assert that they are guarded, and, if assertions are disabled,
1242 bail out if the calls weren't properly guarded. */
1244 #define VERIFY_DUMP_ENABLED_P \
1246 gcc_assert (dump_enabled_p ()); \
1247 if (!dump_enabled_p ()) \
1251 /* Dump gimple statement GS with SPC indentation spaces and
1252 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1255 dump_gimple_stmt (const dump_metadata_t
&metadata
, dump_flags_t extra_dump_flags
,
1256 gimple
*gs
, int spc
)
1258 VERIFY_DUMP_ENABLED_P
;
1259 dump_context::get ().dump_gimple_stmt (metadata
, extra_dump_flags
, gs
, spc
);
1262 /* Similar to dump_gimple_stmt, except additionally print source location. */
1265 dump_gimple_stmt_loc (const dump_metadata_t
&metadata
,
1266 const dump_user_location_t
&loc
,
1267 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
1269 VERIFY_DUMP_ENABLED_P
;
1270 dump_context::get ().dump_gimple_stmt_loc (metadata
, loc
, extra_dump_flags
,
1274 /* Dump gimple statement GS with SPC indentation spaces and
1275 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1276 Do not terminate with a newline or semicolon. */
1279 dump_gimple_expr (const dump_metadata_t
&metadata
,
1280 dump_flags_t extra_dump_flags
,
1281 gimple
*gs
, int spc
)
1283 VERIFY_DUMP_ENABLED_P
;
1284 dump_context::get ().dump_gimple_expr (metadata
, extra_dump_flags
, gs
, spc
);
1287 /* Similar to dump_gimple_expr, except additionally print source location. */
1290 dump_gimple_expr_loc (const dump_metadata_t
&metadata
,
1291 const dump_user_location_t
&loc
,
1292 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
1294 VERIFY_DUMP_ENABLED_P
;
1295 dump_context::get ().dump_gimple_expr_loc (metadata
, loc
, extra_dump_flags
,
1299 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1300 DUMP_KIND is enabled. */
1303 dump_generic_expr (const dump_metadata_t
&metadata
, dump_flags_t extra_dump_flags
,
1306 VERIFY_DUMP_ENABLED_P
;
1307 dump_context::get ().dump_generic_expr (metadata
, extra_dump_flags
, t
);
1310 /* Similar to dump_generic_expr, except additionally print the source
1314 dump_generic_expr_loc (const dump_metadata_t
&metadata
,
1315 const dump_user_location_t
&loc
,
1316 dump_flags_t extra_dump_flags
, tree t
)
1318 VERIFY_DUMP_ENABLED_P
;
1319 dump_context::get ().dump_generic_expr_loc (metadata
, loc
, extra_dump_flags
,
1323 /* Output a formatted message using FORMAT on appropriate dump streams. */
1326 dump_printf (const dump_metadata_t
&metadata
, const char *format
, ...)
1328 VERIFY_DUMP_ENABLED_P
;
1330 va_start (ap
, format
);
1331 dump_context::get ().dump_printf_va (metadata
, format
, &ap
);
1335 /* Similar to dump_printf, except source location is also printed, and
1336 dump location captured. */
1339 dump_printf_loc (const dump_metadata_t
&metadata
,
1340 const dump_user_location_t
&loc
,
1341 const char *format
, ...)
1343 VERIFY_DUMP_ENABLED_P
;
1345 va_start (ap
, format
);
1346 dump_context::get ().dump_printf_loc_va (metadata
, loc
, format
, &ap
);
1350 /* Output VALUE in decimal to appropriate dump streams. */
1352 template<unsigned int N
, typename C
>
1354 dump_dec (const dump_metadata_t
&metadata
, const poly_int
<N
, C
> &value
)
1356 VERIFY_DUMP_ENABLED_P
;
1357 dump_context::get ().dump_dec (metadata
, value
);
1360 template void dump_dec (const dump_metadata_t
&metadata
, const poly_uint16
&);
1361 template void dump_dec (const dump_metadata_t
&metadata
, const poly_int64
&);
1362 template void dump_dec (const dump_metadata_t
&metadata
, const poly_uint64
&);
1363 template void dump_dec (const dump_metadata_t
&metadata
, const poly_offset_int
&);
1364 template void dump_dec (const dump_metadata_t
&metadata
, const poly_widest_int
&);
1367 dump_dec (dump_flags_t dump_kind
, const poly_wide_int
&value
, signop sgn
)
1369 VERIFY_DUMP_ENABLED_P
;
1371 && dump_context::get ().apply_dump_filter_p (dump_kind
, pflags
))
1372 print_dec (value
, dump_file
, sgn
);
1375 && dump_context::get ().apply_dump_filter_p (dump_kind
, alt_flags
))
1376 print_dec (value
, alt_dump_file
, sgn
);
1379 /* Output VALUE in hexadecimal to appropriate dump streams. */
1382 dump_hex (dump_flags_t dump_kind
, const poly_wide_int
&value
)
1384 VERIFY_DUMP_ENABLED_P
;
1386 && dump_context::get ().apply_dump_filter_p (dump_kind
, pflags
))
1387 print_hex (value
, dump_file
);
1390 && dump_context::get ().apply_dump_filter_p (dump_kind
, alt_flags
))
1391 print_hex (value
, alt_dump_file
);
1394 /* Emit and delete the currently pending optinfo, if there is one,
1395 without the caller needing to know about class dump_context. */
1398 dumpfile_ensure_any_optinfo_are_flushed ()
1400 dump_context::get().end_any_optinfo ();
1403 /* Output the name of NODE on appropriate dump streams. */
1406 dump_symtab_node (const dump_metadata_t
&metadata
, symtab_node
*node
)
1408 VERIFY_DUMP_ENABLED_P
;
1409 dump_context::get ().dump_symtab_node (metadata
, node
);
1412 /* Get the current dump scope-nesting depth.
1413 For use by -fopt-info (for showing nesting via indentation). */
1416 get_dump_scope_depth ()
1418 return dump_context::get ().get_scope_depth ();
1421 /* Push a nested dump scope.
1422 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1423 destination, if any.
1424 Emit a "scope" opinfo if optinfos are enabled.
1425 Increment the scope depth. */
1428 dump_begin_scope (const char *name
,
1429 const dump_user_location_t
&user_location
,
1430 const dump_impl_location_t
&impl_location
)
1432 dump_context::get ().begin_scope (name
, user_location
, impl_location
);
1435 /* Pop a nested dump scope. */
1440 dump_context::get ().end_scope ();
1443 /* Start a dump for PHASE. Store user-supplied dump flags in
1444 *FLAG_PTR. Return the number of streams opened. Set globals
1445 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1446 set dump_flags appropriately for both pass dump stream and
1447 -fopt-info stream. */
1451 dump_start (int phase
, dump_flags_t
*flag_ptr
)
1455 struct dump_file_info
*dfi
;
1457 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1460 dfi
= get_dump_file_info (phase
);
1461 name
= get_dump_file_name (phase
);
1464 stream
= dump_open (name
, dfi
->pstate
< 0);
1471 dfi
->pstream
= stream
;
1472 set_dump_file (dfi
->pstream
);
1473 /* Initialize current dump flags. */
1474 pflags
= dfi
->pflags
;
1477 stream
= dump_open_alternate_stream (dfi
);
1480 dfi
->alt_stream
= stream
;
1482 set_alt_dump_file (dfi
->alt_stream
);
1483 /* Initialize current -fopt-info flags. */
1484 alt_flags
= dfi
->alt_flags
;
1488 *flag_ptr
= dfi
->pflags
;
1493 /* Finish a tree dump for PHASE and close associated dump streams. Also
1494 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1498 dump_finish (int phase
)
1500 struct dump_file_info
*dfi
;
1504 dfi
= get_dump_file_info (phase
);
1505 if (dfi
->pstream
&& dfi
->pstream
!= stdout
&& dfi
->pstream
!= stderr
)
1506 fclose (dfi
->pstream
);
1508 if (dfi
->alt_stream
&& dfi
->alt_stream
!= stdout
&& dfi
->alt_stream
!= stderr
)
1509 fclose (dfi
->alt_stream
);
1511 dfi
->alt_stream
= NULL
;
1512 dfi
->pstream
= NULL
;
1513 set_dump_file (NULL
);
1514 set_alt_dump_file (NULL
);
1515 dump_flags
= TDF_NONE
;
1516 alt_flags
= TDF_NONE
;
1520 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1521 *FLAG_PTR and returns a stream to write to. If the dump is not
1522 enabled, returns NULL.
1523 PART can be used for dump files which should be split to multiple
1524 parts. PART == -1 indicates dump file with no parts.
1525 If PART is -1, multiple calls will reopen and append to the dump file. */
1528 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1530 return g
->get_dumps ()->dump_begin (phase
, flag_ptr
, part
);
1535 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1537 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1540 char *name
= get_dump_file_name (phase
, part
);
1543 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1545 /* We do not support re-opening of dump files with parts. This would require
1546 tracking pstate per part of the dump file. */
1547 FILE *stream
= dump_open (name
, part
!= -1 || dfi
->pstate
< 0);
1553 *flag_ptr
= dfi
->pflags
;
1555 /* Initialize current flags */
1556 pflags
= dfi
->pflags
;
1560 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1561 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1566 dump_phase_enabled_p (int phase
) const
1568 if (phase
== TDI_tree_all
)
1571 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1572 if (dump_files
[i
].pstate
|| dump_files
[i
].alt_state
)
1574 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1575 if (m_extra_dump_files
[i
].pstate
|| m_extra_dump_files
[i
].alt_state
)
1581 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1582 return dfi
->pstate
|| dfi
->alt_state
;
1586 /* Returns nonzero if tree dump PHASE has been initialized. */
1590 dump_initialized_p (int phase
) const
1592 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1593 return dfi
->pstate
> 0 || dfi
->alt_state
> 0;
1596 /* Returns the switch name of PHASE. */
1599 dump_flag_name (int phase
)
1601 return g
->get_dumps ()->dump_flag_name (phase
);
1606 dump_flag_name (int phase
) const
1608 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1612 /* Handle -fdump-* and -fopt-info for a pass added after
1613 command-line options are parsed (those from plugins and
1614 those from backends).
1616 Because the registration of plugin/backend passes happens after the
1617 command-line options are parsed, the options that specify single
1618 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1619 passes. Therefore we currently can only enable dumping of
1620 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1621 are specified. This is done here.
1623 Similarly, the saved -fopt-info options are wired up to the new pass. */
1626 gcc::dump_manager::register_pass (opt_pass
*pass
)
1630 register_one_dump_file (pass
);
1632 dump_file_info
*pass_dfi
= get_dump_file_info (pass
->static_pass_number
);
1633 gcc_assert (pass_dfi
);
1635 enum tree_dump_index tdi
;
1636 if (pass
->type
== SIMPLE_IPA_PASS
1637 || pass
->type
== IPA_PASS
)
1639 else if (pass
->type
== GIMPLE_PASS
)
1643 const dump_file_info
*tdi_dfi
= get_dump_file_info (tdi
);
1644 gcc_assert (tdi_dfi
);
1646 /* Check if dump-all flag is specified. */
1647 if (tdi_dfi
->pstate
)
1649 pass_dfi
->pstate
= tdi_dfi
->pstate
;
1650 pass_dfi
->pflags
= tdi_dfi
->pflags
;
1653 update_dfi_for_opt_info (pass_dfi
);
1656 /* Finish a tree dump for PHASE. STREAM is the stream created by
1660 dump_end (int phase ATTRIBUTE_UNUSED
, FILE *stream
)
1662 if (stream
!= stderr
&& stream
!= stdout
)
1666 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1667 enabled tree dumps. */
1671 dump_enable_all (dump_kind dkind
, dump_flags_t flags
, const char *filename
)
1676 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1678 if (dump_files
[i
].dkind
== dkind
)
1680 const char *old_filename
= dump_files
[i
].pfilename
;
1681 dump_files
[i
].pstate
= -1;
1682 dump_files
[i
].pflags
|= flags
;
1684 /* Override the existing filename. */
1687 dump_files
[i
].pfilename
= xstrdup (filename
);
1688 /* Since it is a command-line provided file, which is
1689 common to all the phases, use it in append mode. */
1690 dump_files
[i
].pstate
= 1;
1692 if (old_filename
&& filename
!= old_filename
)
1693 free (CONST_CAST (char *, old_filename
));
1697 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1699 if (m_extra_dump_files
[i
].dkind
== dkind
)
1701 const char *old_filename
= m_extra_dump_files
[i
].pfilename
;
1702 m_extra_dump_files
[i
].pstate
= -1;
1703 m_extra_dump_files
[i
].pflags
|= flags
;
1705 /* Override the existing filename. */
1708 m_extra_dump_files
[i
].pfilename
= xstrdup (filename
);
1709 /* Since it is a command-line provided file, which is
1710 common to all the phases, use it in append mode. */
1711 m_extra_dump_files
[i
].pstate
= 1;
1713 if (old_filename
&& filename
!= old_filename
)
1714 free (CONST_CAST (char *, old_filename
));
1721 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1722 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1727 opt_info_enable_passes (optgroup_flags_t optgroup_flags
, dump_flags_t flags
,
1728 const char *filename
)
1732 m_optgroup_flags
= optgroup_flags
;
1733 m_optinfo_flags
= flags
;
1734 m_optinfo_filename
= xstrdup (filename
);
1736 for (size_t i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1737 if (update_dfi_for_opt_info (&dump_files
[i
]))
1740 for (size_t i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1741 if (update_dfi_for_opt_info (&m_extra_dump_files
[i
]))
1747 /* Use the saved -fopt-info options to update DFI.
1748 Return true if the dump is enabled. */
1751 gcc::dump_manager::update_dfi_for_opt_info (dump_file_info
*dfi
) const
1755 if (!(dfi
->optgroup_flags
& m_optgroup_flags
))
1758 const char *old_filename
= dfi
->alt_filename
;
1759 /* Since this file is shared among different passes, it
1760 should be opened in append mode. */
1762 dfi
->alt_flags
|= m_optinfo_flags
;
1763 /* Override the existing filename. */
1764 if (m_optinfo_filename
)
1765 dfi
->alt_filename
= xstrdup (m_optinfo_filename
);
1766 if (old_filename
&& m_optinfo_filename
!= old_filename
)
1767 free (CONST_CAST (char *, old_filename
));
1772 /* Helper routine to parse -<dump format>[=filename]
1773 and return the corresponding dump flag. If POS_P is non-NULL,
1774 assign start of filename into *POS_P. */
1777 parse_dump_option (const char *option_value
, const char **pos_p
)
1786 /* Retain "user-facing" and "internals" messages, but filter out
1787 those from an opt_problem being re-emitted at the top level
1788 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1789 messing up scan-tree-dump-times" in DejaGnu tests. */
1790 flags
= MSG_PRIORITY_USER_FACING
| MSG_PRIORITY_INTERNALS
;
1794 const struct kv_pair
<dump_flags_t
> *option_ptr
;
1795 const char *end_ptr
;
1800 end_ptr
= strchr (ptr
, '-');
1801 eq_ptr
= strchr (ptr
, '=');
1803 if (eq_ptr
&& !end_ptr
)
1807 end_ptr
= ptr
+ strlen (ptr
);
1808 length
= end_ptr
- ptr
;
1810 for (option_ptr
= dump_options
; option_ptr
->name
; option_ptr
++)
1811 if (strlen (option_ptr
->name
) == length
1812 && !memcmp (option_ptr
->name
, ptr
, length
))
1814 flags
|= option_ptr
->value
;
1820 /* Interpret rest of the argument as a dump filename. This
1821 filename overrides other command line filenames. */
1828 warning (0, "ignoring unknown option %q.*s",
1839 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1840 relevant details in the dump_files array. */
1844 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
, bool doglob
)
1846 const char *option_value
;
1847 dump_flags_t flags
= TDF_NONE
;
1849 if (doglob
&& !dfi
->glob
)
1852 option_value
= skip_leading_substring (arg
, doglob
? dfi
->glob
: dfi
->swtch
);
1856 if (*option_value
&& *option_value
!= '-' && *option_value
!= '=')
1859 const char *filename
;
1860 flags
= parse_dump_option (option_value
, &filename
);
1864 free (CONST_CAST (char *, dfi
->pfilename
));
1865 dfi
->pfilename
= xstrdup (filename
);
1869 dfi
->pflags
|= flags
;
1871 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1873 if (dfi
->suffix
== NULL
)
1874 dump_enable_all (dfi
->dkind
, dfi
->pflags
, dfi
->pfilename
);
1881 dump_switch_p (const char *arg
)
1886 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1887 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], false);
1889 /* Don't glob if we got a hit already */
1891 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1892 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], true);
1894 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1895 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], false);
1898 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1899 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], true);
1903 auto_vec
<const char *> candidates
;
1904 for (size_t i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1905 candidates
.safe_push (dump_files
[i
].swtch
);
1906 for (size_t i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1907 candidates
.safe_push (m_extra_dump_files
[i
].swtch
);
1908 const char *hint
= find_closest_string (arg
, &candidates
);
1910 error ("unrecognized command-line option %<-fdump-%s%>; "
1911 "did you mean %<-fdump-%s%>?", arg
, hint
);
1913 error ("unrecognized command-line option %<-fdump-%s%>", arg
);
1917 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1918 and filename. Return non-zero if it is a recognized switch. */
1921 opt_info_switch_p_1 (const char *arg
, dump_flags_t
*flags
,
1922 optgroup_flags_t
*optgroup_flags
, char **filename
)
1924 const char *option_value
;
1932 /* Default to filtering out "internals" messages, and retaining
1933 "user-facing" messages, and those from an opt_problem being
1934 re-emitted at the top level. */
1935 *flags
= MSG_PRIORITY_USER_FACING
| MSG_PRIORITY_REEMITTED
;
1937 *optgroup_flags
= OPTGROUP_NONE
;
1940 return 1; /* Handle '-fopt-info' without any additional options. */
1944 const char *end_ptr
;
1950 end_ptr
= strchr (ptr
, '-');
1951 eq_ptr
= strchr (ptr
, '=');
1953 if (eq_ptr
&& (!end_ptr
|| eq_ptr
< end_ptr
))
1956 end_ptr
= ptr
+ strlen (ptr
);
1957 length
= end_ptr
- ptr
;
1959 for (const kv_pair
<dump_flags_t
> *option_ptr
= optinfo_verbosity_options
;
1960 option_ptr
->name
; option_ptr
++)
1961 if (strlen (option_ptr
->name
) == length
1962 && !memcmp (option_ptr
->name
, ptr
, length
))
1964 *flags
|= option_ptr
->value
;
1968 for (const kv_pair
<optgroup_flags_t
> *option_ptr
= optgroup_options
;
1969 option_ptr
->name
; option_ptr
++)
1970 if (strlen (option_ptr
->name
) == length
1971 && !memcmp (option_ptr
->name
, ptr
, length
))
1973 *optgroup_flags
|= option_ptr
->value
;
1979 /* Interpret rest of the argument as a dump filename. This
1980 filename overrides other command line filenames. */
1981 *filename
= xstrdup (ptr
+ 1);
1986 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1997 /* Return non-zero if ARG is a recognized switch for
1998 -fopt-info. Return zero otherwise. */
2001 opt_info_switch_p (const char *arg
)
2004 optgroup_flags_t optgroup_flags
;
2006 static char *file_seen
= NULL
;
2007 gcc::dump_manager
*dumps
= g
->get_dumps ();
2009 if (!opt_info_switch_p_1 (arg
, &flags
, &optgroup_flags
, &filename
))
2013 filename
= xstrdup ("stderr");
2015 /* Bail out if a different filename has been specified. */
2016 if (file_seen
&& strcmp (file_seen
, filename
))
2018 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
2023 file_seen
= xstrdup (filename
);
2024 if (!(flags
& MSG_ALL_KINDS
))
2025 flags
|= MSG_OPTIMIZED_LOCATIONS
;
2026 if (!optgroup_flags
)
2027 optgroup_flags
= OPTGROUP_ALL
;
2029 return dumps
->opt_info_enable_passes (optgroup_flags
, flags
, filename
);
2032 /* Print basic block on the dump streams. */
2035 dump_basic_block (dump_flags_t dump_kind
, basic_block bb
, int indent
)
2038 && dump_context::get ().apply_dump_filter_p (dump_kind
, pflags
))
2039 dump_bb (dump_file
, bb
, indent
, TDF_DETAILS
);
2041 && dump_context::get ().apply_dump_filter_p (dump_kind
, alt_flags
))
2042 dump_bb (alt_dump_file
, bb
, indent
, TDF_DETAILS
);
2045 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
2048 dump_function (int phase
, tree fn
)
2053 stream
= dump_begin (phase
, &flags
);
2056 dump_function_to_file (fn
, stream
, flags
);
2057 dump_end (phase
, stream
);
2061 /* Print information from the combine pass on dump_file. */
2064 print_combine_total_stats (void)
2067 dump_combine_total_stats (dump_file
);
2070 /* Enable RTL dump for all the RTL passes. */
2073 enable_rtl_dump_file (void)
2075 gcc::dump_manager
*dumps
= g
->get_dumps ();
2077 dumps
->dump_enable_all (DK_rtl
, dump_flags_t (TDF_DETAILS
) | TDF_BLOCKS
,
2079 return num_enabled
> 0;
2082 /* debug_dump_context's ctor. Temporarily override the dump_context
2083 (to forcibly enable output to stderr). */
2085 debug_dump_context::debug_dump_context ()
2087 m_saved (&dump_context::get ()),
2088 m_saved_flags (dump_flags
),
2089 m_saved_pflags (pflags
),
2090 m_saved_file (dump_file
)
2092 set_dump_file (stderr
);
2093 dump_context::s_current
= &m_context
;
2094 pflags
= dump_flags
= MSG_ALL_KINDS
| MSG_ALL_PRIORITIES
;
2095 dump_context::get ().refresh_dumps_are_enabled ();
2098 /* debug_dump_context's dtor. Restore the saved dump_context. */
2100 debug_dump_context::~debug_dump_context ()
2102 set_dump_file (m_saved_file
);
2103 dump_context::s_current
= m_saved
;
2104 dump_flags
= m_saved_flags
;
2105 pflags
= m_saved_pflags
;
2106 dump_context::get ().refresh_dumps_are_enabled ();
2112 namespace selftest
{
2114 /* temp_dump_context's ctor. Temporarily override the dump_context
2115 (to forcibly enable optinfo-generation). */
2117 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo
,
2118 bool forcibly_enable_dumping
,
2119 dump_flags_t test_pp_flags
)
2121 m_saved (&dump_context::get ())
2123 dump_context::s_current
= &m_context
;
2124 if (forcibly_enable_optinfo
)
2125 m_context
.set_json_writer (new optrecord_json_writer ());
2126 /* Conditionally enable the test dump, so that we can verify both the
2127 dump_enabled_p and the !dump_enabled_p cases in selftests. */
2128 if (forcibly_enable_dumping
)
2130 m_context
.m_test_pp
= &m_pp
;
2131 m_context
.m_test_pp_flags
= test_pp_flags
;
2134 dump_context::get ().refresh_dumps_are_enabled ();
2137 /* temp_dump_context's dtor. Restore the saved dump_context. */
2139 temp_dump_context::~temp_dump_context ()
2141 m_context
.set_json_writer (NULL
);
2143 dump_context::s_current
= m_saved
;
2145 dump_context::get ().refresh_dumps_are_enabled ();
2148 /* 0-terminate the text dumped so far, and return it. */
2151 temp_dump_context::get_dumped_text ()
2153 return pp_formatted_text (&m_pp
);
2156 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2157 from EXPECTED_FUNCTION, using LOC for the location of any failure,
2158 provided that the build compiler is sufficiently recent. */
2161 assert_impl_location_eq (const location
&loc ATTRIBUTE_UNUSED
,
2162 const dump_impl_location_t
&impl_loc ATTRIBUTE_UNUSED
,
2163 const char *expected_file ATTRIBUTE_UNUSED
,
2164 int expected_line ATTRIBUTE_UNUSED
,
2165 const char *expected_function ATTRIBUTE_UNUSED
)
2167 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2168 ASSERT_STR_CONTAINS_AT (loc
, impl_loc
.m_file
, expected_file
);
2169 ASSERT_EQ_AT (loc
, impl_loc
.m_line
, expected_line
);
2170 ASSERT_STR_CONTAINS_AT (loc
, impl_loc
.m_function
, expected_function
);
2174 /* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2175 from EXPECTED_FUNCTION, provided that the build compiler is
2176 sufficiently recent. */
2178 #define ASSERT_IMPL_LOCATION_EQ(IMPL_LOC, EXPECTED_FILE, EXPECTED_LINE, \
2179 EXPECTED_FUNCTION) \
2180 SELFTEST_BEGIN_STMT \
2181 assert_impl_location_eq (SELFTEST_LOCATION, IMPL_LOC, \
2182 EXPECTED_FILE, EXPECTED_LINE, \
2183 EXPECTED_FUNCTION); \
2186 /* Verify that the dump_location_t constructors capture the source location
2187 at which they were called (provided that the build compiler is sufficiently
2191 test_impl_location ()
2195 dump_location_t loc
;
2196 const int expected_line
= __LINE__
- 1;
2197 ASSERT_IMPL_LOCATION_EQ (loc
.get_impl_location (),
2198 "dumpfile.c", expected_line
, "test_impl_location");
2201 /* Constructing from a gimple. */
2203 dump_location_t
loc ((gimple
*)NULL
);
2204 const int expected_line
= __LINE__
- 1;
2205 ASSERT_IMPL_LOCATION_EQ (loc
.get_impl_location (),
2206 "dumpfile.c", expected_line
, "test_impl_location");
2209 /* Constructing from an rtx_insn. */
2211 dump_location_t
loc ((rtx_insn
*)NULL
);
2212 const int expected_line
= __LINE__
- 1;
2213 ASSERT_IMPL_LOCATION_EQ (loc
.get_impl_location (),
2214 "dumpfile.c", expected_line
, "test_impl_location");
2218 /* Verify that the text dumped so far in CONTEXT equals
2219 EXPECTED_TEXT, using LOC for the location of any failure.
2220 As a side-effect, the internal buffer is 0-terminated. */
2223 verify_dumped_text (const location
&loc
,
2224 temp_dump_context
*context
,
2225 const char *expected_text
)
2227 gcc_assert (context
);
2228 ASSERT_STREQ_AT (loc
, context
->get_dumped_text (),
2232 /* Verify that ITEM has the expected values. */
2235 verify_item (const location
&loc
,
2236 const optinfo_item
*item
,
2237 enum optinfo_item_kind expected_kind
,
2238 location_t expected_location
,
2239 const char *expected_text
)
2241 ASSERT_EQ_AT (loc
, item
->get_kind (), expected_kind
);
2242 ASSERT_EQ_AT (loc
, item
->get_location (), expected_location
);
2243 ASSERT_STREQ_AT (loc
, item
->get_text (), expected_text
);
2246 /* Verify that calls to the dump_* API are captured and consolidated into
2247 optimization records. */
2250 test_capture_of_dump_calls (const line_table_case
&case_
)
2252 /* Generate a location_t for testing. */
2253 line_table_test
ltt (case_
);
2254 linemap_add (line_table
, LC_ENTER
, false, "test.txt", 0);
2255 linemap_line_start (line_table
, 5, 100);
2256 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
2257 location_t decl_loc
= linemap_position_for_column (line_table
, 8);
2258 location_t stmt_loc
= linemap_position_for_column (line_table
, 10);
2259 if (stmt_loc
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
2262 dump_user_location_t loc
= dump_user_location_t::from_location_t (stmt_loc
);
2264 gimple
*stmt
= gimple_build_return (NULL
);
2265 gimple_set_location (stmt
, stmt_loc
);
2267 tree test_decl
= build_decl (decl_loc
, FUNCTION_DECL
,
2268 get_identifier ("test_decl"),
2269 build_function_type_list (void_type_node
,
2272 symbol_table_test tmp_symtab
;
2274 cgraph_node
*node
= cgraph_node::get_create (test_decl
);
2277 /* Run all tests twice, with and then without optinfo enabled, to ensure
2278 that immediate destinations vs optinfo-based destinations both
2279 work, independently of each other, with no leaks. */
2280 for (int i
= 0 ; i
< 2; i
++)
2282 bool with_optinfo
= (i
== 0);
2284 /* Test of dump_printf. */
2286 temp_dump_context
tmp (with_optinfo
, true,
2287 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2288 dump_printf (MSG_NOTE
, "int: %i str: %s", 42, "foo");
2289 const int expected_impl_line
= __LINE__
- 1;
2291 ASSERT_DUMPED_TEXT_EQ (tmp
, "int: 42 str: foo");
2294 optinfo
*info
= tmp
.get_pending_optinfo ();
2295 ASSERT_TRUE (info
!= NULL
);
2296 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2297 ASSERT_EQ (info
->num_items (), 1);
2298 ASSERT_IS_TEXT (info
->get_item (0), "int: 42 str: foo");
2299 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2300 "dumpfile.c", expected_impl_line
,
2301 "test_capture_of_dump_calls");
2305 /* Test of dump_printf with %T. */
2307 temp_dump_context
tmp (with_optinfo
, true,
2308 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2309 dump_printf (MSG_NOTE
, "tree: %T", integer_zero_node
);
2310 const int expected_impl_line
= __LINE__
- 1;
2312 ASSERT_DUMPED_TEXT_EQ (tmp
, "tree: 0");
2315 optinfo
*info
= tmp
.get_pending_optinfo ();
2316 ASSERT_TRUE (info
!= NULL
);
2317 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2318 ASSERT_EQ (info
->num_items (), 2);
2319 ASSERT_IS_TEXT (info
->get_item (0), "tree: ");
2320 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
2321 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2322 "dumpfile.c", expected_impl_line
,
2323 "test_capture_of_dump_calls");
2327 /* Test of dump_printf with %E. */
2329 temp_dump_context
tmp (with_optinfo
, true,
2330 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2331 dump_printf (MSG_NOTE
, "gimple: %E", stmt
);
2332 const int expected_impl_line
= __LINE__
- 1;
2334 ASSERT_DUMPED_TEXT_EQ (tmp
, "gimple: return;");
2337 optinfo
*info
= tmp
.get_pending_optinfo ();
2338 ASSERT_TRUE (info
!= NULL
);
2339 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2340 ASSERT_EQ (info
->num_items (), 2);
2341 ASSERT_IS_TEXT (info
->get_item (0), "gimple: ");
2342 ASSERT_IS_GIMPLE (info
->get_item (1), stmt_loc
, "return;");
2343 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2344 "dumpfile.c", expected_impl_line
,
2345 "test_capture_of_dump_calls");
2349 /* Test of dump_printf with %G. */
2351 temp_dump_context
tmp (with_optinfo
, true,
2352 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2353 dump_printf (MSG_NOTE
, "gimple: %G", stmt
);
2354 const int expected_impl_line
= __LINE__
- 1;
2356 ASSERT_DUMPED_TEXT_EQ (tmp
, "gimple: return;\n");
2359 optinfo
*info
= tmp
.get_pending_optinfo ();
2360 ASSERT_TRUE (info
!= NULL
);
2361 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2362 ASSERT_EQ (info
->num_items (), 2);
2363 ASSERT_IS_TEXT (info
->get_item (0), "gimple: ");
2364 ASSERT_IS_GIMPLE (info
->get_item (1), stmt_loc
, "return;\n");
2365 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2366 "dumpfile.c", expected_impl_line
,
2367 "test_capture_of_dump_calls");
2371 /* Test of dump_printf with %C. */
2373 temp_dump_context
tmp (with_optinfo
, true,
2374 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2375 dump_printf (MSG_NOTE
, "node: %C", node
);
2376 const int expected_impl_line
= __LINE__
- 1;
2378 ASSERT_DUMPED_TEXT_EQ (tmp
, "node: test_decl/0");
2381 optinfo
*info
= tmp
.get_pending_optinfo ();
2382 ASSERT_TRUE (info
!= NULL
);
2383 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2384 ASSERT_EQ (info
->num_items (), 2);
2385 ASSERT_IS_TEXT (info
->get_item (0), "node: ");
2386 ASSERT_IS_SYMTAB_NODE (info
->get_item (1), decl_loc
, "test_decl/0");
2387 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2388 "dumpfile.c", expected_impl_line
,
2389 "test_capture_of_dump_calls");
2393 /* dump_print_loc with multiple format codes. This tests various
2395 - intermingling of text, format codes handled by the base
2396 pretty_printer, and dump-specific format codes
2397 - multiple dump-specific format codes: some consecutive, others
2398 separated by text, trailing text after the final one. */
2400 temp_dump_context
tmp (with_optinfo
, true,
2401 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2402 dump_printf_loc (MSG_NOTE
, loc
, "before %T and %T"
2403 " %i consecutive %E%E after\n",
2404 integer_zero_node
, test_decl
, 42, stmt
, stmt
);
2406 ASSERT_DUMPED_TEXT_EQ (tmp
,
2407 "test.txt:5:10: note: before 0 and test_decl"
2408 " 42 consecutive return;return; after\n");
2411 optinfo
*info
= tmp
.get_pending_optinfo ();
2412 ASSERT_TRUE (info
!= NULL
);
2413 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2414 ASSERT_EQ (info
->num_items (), 8);
2415 ASSERT_IS_TEXT (info
->get_item (0), "before ");
2416 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
2417 ASSERT_IS_TEXT (info
->get_item (2), " and ");
2418 ASSERT_IS_TREE (info
->get_item (3), UNKNOWN_LOCATION
, "test_decl");
2419 ASSERT_IS_TEXT (info
->get_item (4), " 42 consecutive ");
2420 ASSERT_IS_GIMPLE (info
->get_item (5), stmt_loc
, "return;");
2421 ASSERT_IS_GIMPLE (info
->get_item (6), stmt_loc
, "return;");
2422 ASSERT_IS_TEXT (info
->get_item (7), " after\n");
2423 /* We don't ASSERT_IMPL_LOCATION_EQ here, to avoid having to
2424 enforce at which exact line the multiline dump_printf_loc
2429 /* Tree, via dump_generic_expr. */
2431 temp_dump_context
tmp (with_optinfo
, true,
2432 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2433 dump_printf_loc (MSG_NOTE
, loc
, "test of tree: ");
2434 const int expected_impl_line
= __LINE__
- 1;
2435 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, integer_zero_node
);
2437 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: test of tree: 0");
2440 optinfo
*info
= tmp
.get_pending_optinfo ();
2441 ASSERT_TRUE (info
!= NULL
);
2442 ASSERT_EQ (info
->get_location_t (), stmt_loc
);
2443 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2444 ASSERT_EQ (info
->num_items (), 2);
2445 ASSERT_IS_TEXT (info
->get_item (0), "test of tree: ");
2446 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
2447 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2448 "dumpfile.c", expected_impl_line
,
2449 "test_capture_of_dump_calls");
2453 /* Tree, via dump_generic_expr_loc. */
2455 temp_dump_context
tmp (with_optinfo
, true,
2456 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2457 dump_generic_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, integer_one_node
);
2458 const int expected_impl_line
= __LINE__
- 1;
2460 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: 1");
2463 optinfo
*info
= tmp
.get_pending_optinfo ();
2464 ASSERT_TRUE (info
!= NULL
);
2465 ASSERT_EQ (info
->get_location_t (), stmt_loc
);
2466 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2467 ASSERT_EQ (info
->num_items (), 1);
2468 ASSERT_IS_TREE (info
->get_item (0), UNKNOWN_LOCATION
, "1");
2469 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2470 "dumpfile.c", expected_impl_line
,
2471 "test_capture_of_dump_calls");
2477 /* dump_gimple_stmt_loc. */
2479 temp_dump_context
tmp (with_optinfo
, true,
2480 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2481 dump_gimple_stmt_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
2482 const int expected_impl_line
= __LINE__
- 1;
2484 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: return;\n");
2487 optinfo
*info
= tmp
.get_pending_optinfo ();
2488 ASSERT_TRUE (info
!= NULL
);
2489 ASSERT_EQ (info
->num_items (), 1);
2490 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;\n");
2491 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2492 "dumpfile.c", expected_impl_line
,
2493 "test_capture_of_dump_calls");
2497 /* dump_gimple_stmt. */
2499 temp_dump_context
tmp (with_optinfo
, true,
2500 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2501 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
2502 const int expected_impl_line
= __LINE__
- 1;
2504 ASSERT_DUMPED_TEXT_EQ (tmp
, "return;\n");
2507 optinfo
*info
= tmp
.get_pending_optinfo ();
2508 ASSERT_TRUE (info
!= NULL
);
2509 ASSERT_EQ (info
->num_items (), 1);
2510 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;\n");
2511 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2512 "dumpfile.c", expected_impl_line
,
2513 "test_capture_of_dump_calls");
2517 /* dump_gimple_expr_loc. */
2519 temp_dump_context
tmp (with_optinfo
, true,
2520 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2521 dump_gimple_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
2522 const int expected_impl_line
= __LINE__
- 1;
2524 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: note: return;");
2527 optinfo
*info
= tmp
.get_pending_optinfo ();
2528 ASSERT_TRUE (info
!= NULL
);
2529 ASSERT_EQ (info
->num_items (), 1);
2530 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;");
2531 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2532 "dumpfile.c", expected_impl_line
,
2533 "test_capture_of_dump_calls");
2537 /* dump_gimple_expr. */
2539 temp_dump_context
tmp (with_optinfo
, true,
2540 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2541 dump_gimple_expr (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
2542 const int expected_impl_line
= __LINE__
- 1;
2544 ASSERT_DUMPED_TEXT_EQ (tmp
, "return;");
2547 optinfo
*info
= tmp
.get_pending_optinfo ();
2548 ASSERT_TRUE (info
!= NULL
);
2549 ASSERT_EQ (info
->num_items (), 1);
2550 ASSERT_IS_GIMPLE (info
->get_item (0), stmt_loc
, "return;");
2551 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2552 "dumpfile.c", expected_impl_line
,
2553 "test_capture_of_dump_calls");
2560 temp_dump_context
tmp (with_optinfo
, true,
2561 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2562 dump_symtab_node (MSG_NOTE
, node
);
2563 const int expected_impl_line
= __LINE__
- 1;
2565 ASSERT_DUMPED_TEXT_EQ (tmp
, "test_decl/0");
2568 optinfo
*info
= tmp
.get_pending_optinfo ();
2569 ASSERT_TRUE (info
!= NULL
);
2570 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
2571 ASSERT_EQ (info
->num_items (), 1);
2572 ASSERT_IS_SYMTAB_NODE (info
->get_item (0), decl_loc
, "test_decl/0");
2573 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2574 "dumpfile.c", expected_impl_line
,
2575 "test_capture_of_dump_calls");
2581 temp_dump_context
tmp (with_optinfo
, true,
2582 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2583 dump_dec (MSG_NOTE
, poly_int64 (42));
2584 const int expected_impl_line
= __LINE__
- 1;
2586 ASSERT_DUMPED_TEXT_EQ (tmp
, "42");
2589 optinfo
*info
= tmp
.get_pending_optinfo ();
2590 ASSERT_TRUE (info
!= NULL
);
2591 ASSERT_EQ (info
->num_items (), 1);
2592 ASSERT_IS_TEXT (info
->get_item (0), "42");
2593 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2594 "dumpfile.c", expected_impl_line
,
2595 "test_capture_of_dump_calls");
2599 /* Scopes. Test with all 4 combinations of
2600 filtering by MSG_PRIORITY_USER_FACING
2601 and/or filtering by MSG_PRIORITY_INTERNALS. */
2602 for (int j
= 0; j
< 3; j
++)
2604 dump_flags_t dump_filter
= MSG_ALL_KINDS
;
2606 dump_filter
|= MSG_PRIORITY_USER_FACING
;
2608 dump_filter
|= MSG_PRIORITY_INTERNALS
;
2610 temp_dump_context
tmp (with_optinfo
, true, dump_filter
);
2611 /* Emit various messages, mostly with implicit priority. */
2612 dump_printf_loc (MSG_NOTE
, stmt
, "msg 1\n");
2613 dump_printf_loc (MSG_NOTE
| MSG_PRIORITY_INTERNALS
, stmt
,
2614 "explicitly internal msg\n");
2616 AUTO_DUMP_SCOPE ("outer scope", stmt
);
2617 dump_printf_loc (MSG_NOTE
, stmt
, "msg 2\n");
2619 AUTO_DUMP_SCOPE ("middle scope", stmt
);
2620 dump_printf_loc (MSG_NOTE
, stmt
, "msg 3\n");
2622 AUTO_DUMP_SCOPE ("inner scope", stmt
);
2623 dump_printf_loc (MSG_NOTE
, stmt
, "msg 4\n");
2624 dump_printf_loc (MSG_NOTE
| MSG_PRIORITY_USER_FACING
, stmt
,
2625 "explicitly user-facing msg\n");
2627 dump_printf_loc (MSG_NOTE
, stmt
, "msg 5\n");
2629 dump_printf_loc (MSG_NOTE
, stmt
, "msg 6\n");
2631 dump_printf_loc (MSG_NOTE
, stmt
, "msg 7\n");
2632 const int expected_impl_line
= __LINE__
- 1;
2634 switch (dump_filter
& MSG_ALL_PRIORITIES
)
2639 ASSERT_DUMPED_TEXT_EQ (tmp
, "");
2641 case MSG_PRIORITY_USER_FACING
:
2642 ASSERT_DUMPED_TEXT_EQ
2644 "test.txt:5:10: note: msg 1\n"
2645 "test.txt:5:10: note: explicitly user-facing msg\n"
2646 "test.txt:5:10: note: msg 7\n");
2648 case MSG_PRIORITY_INTERNALS
:
2649 ASSERT_DUMPED_TEXT_EQ
2651 "test.txt:5:10: note: explicitly internal msg\n"
2652 "test.txt:5:10: note: === outer scope ===\n"
2653 "test.txt:5:10: note: msg 2\n"
2654 "test.txt:5:10: note: === middle scope ===\n"
2655 "test.txt:5:10: note: msg 3\n"
2656 "test.txt:5:10: note: === inner scope ===\n"
2657 "test.txt:5:10: note: msg 4\n"
2658 "test.txt:5:10: note: msg 5\n"
2659 "test.txt:5:10: note: msg 6\n");
2661 case MSG_ALL_PRIORITIES
:
2662 ASSERT_DUMPED_TEXT_EQ
2664 "test.txt:5:10: note: msg 1\n"
2665 "test.txt:5:10: note: explicitly internal msg\n"
2666 "test.txt:5:10: note: === outer scope ===\n"
2667 "test.txt:5:10: note: msg 2\n"
2668 "test.txt:5:10: note: === middle scope ===\n"
2669 "test.txt:5:10: note: msg 3\n"
2670 "test.txt:5:10: note: === inner scope ===\n"
2671 "test.txt:5:10: note: msg 4\n"
2672 "test.txt:5:10: note: explicitly user-facing msg\n"
2673 "test.txt:5:10: note: msg 5\n"
2674 "test.txt:5:10: note: msg 6\n"
2675 "test.txt:5:10: note: msg 7\n");
2680 optinfo
*info
= tmp
.get_pending_optinfo ();
2681 ASSERT_TRUE (info
!= NULL
);
2682 ASSERT_EQ (info
->num_items (), 1);
2683 ASSERT_IS_TEXT (info
->get_item (0), "msg 7\n");
2684 ASSERT_IMPL_LOCATION_EQ (info
->get_impl_location (),
2685 "dumpfile.c", expected_impl_line
,
2686 "test_capture_of_dump_calls");
2691 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2694 /* MSG_OPTIMIZED_LOCATIONS. */
2696 temp_dump_context
tmp (true, true, MSG_ALL_KINDS
);
2697 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
, "test");
2698 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
2699 OPTINFO_KIND_SUCCESS
);
2702 /* MSG_MISSED_OPTIMIZATION. */
2704 temp_dump_context
tmp (true, true, MSG_ALL_KINDS
);
2705 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
, "test");
2706 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
2707 OPTINFO_KIND_FAILURE
);
2711 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2713 temp_dump_context
tmp (false, true,
2714 MSG_OPTIMIZED_LOCATIONS
| MSG_ALL_PRIORITIES
);
2715 dump_printf_loc (MSG_NOTE
, stmt
, "msg 1\n");
2717 AUTO_DUMP_SCOPE ("outer scope", stmt
);
2718 dump_printf_loc (MSG_NOTE
, stmt
, "msg 2\n");
2720 AUTO_DUMP_SCOPE ("middle scope", stmt
);
2721 dump_printf_loc (MSG_NOTE
, stmt
, "msg 3\n");
2723 AUTO_DUMP_SCOPE ("inner scope", stmt
);
2724 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, stmt
, "msg 4\n");
2726 dump_printf_loc (MSG_NOTE
, stmt
, "msg 5\n");
2728 dump_printf_loc (MSG_NOTE
, stmt
, "msg 6\n");
2730 dump_printf_loc (MSG_NOTE
, stmt
, "msg 7\n");
2732 ASSERT_DUMPED_TEXT_EQ (tmp
, "test.txt:5:10: optimized: msg 4\n");
2739 dump_user_location_t loc
2740 = dump_user_location_t::from_location_t (UNKNOWN_LOCATION
);
2742 temp_dump_context
tmp (true, true,
2743 MSG_ALL_KINDS
| MSG_PRIORITY_USER_FACING
);
2745 AUTO_DUMP_SCOPE ("outer scope", loc
);
2746 dump_printf (MSG_NOTE
, "msg1\n");
2750 /* Run all of the selftests within this file. */
2755 test_impl_location ();
2756 for_each_line_table_case (test_capture_of_dump_calls
);
2760 } // namespace selftest
2762 #endif /* CHECKING_P */