1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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"
42 /* If non-NULL, return one past-the-end of the matching SUBPART of
44 #define skip_leading_substring(whole, part) \
45 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
47 static dump_flags_t pflags
; /* current dump_flags */
49 static void dump_loc (dump_flags_t
, FILE *, source_location
);
51 /* Current -fopt-info output stream, if any, and flags. */
52 static FILE *alt_dump_file
= NULL
;
53 static dump_flags_t alt_flags
;
55 static FILE *dump_open_alternate_stream (struct dump_file_info
*);
57 /* These are currently used for communicating between passes.
58 However, instead of accessing them directly, the passes can use
59 dump_printf () for dumps. */
60 FILE *dump_file
= NULL
;
61 const char *dump_file_name
;
62 dump_flags_t dump_flags
;
63 bool dumps_are_enabled
= false;
66 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
67 or alt_dump_file change. */
70 refresh_dumps_are_enabled ()
72 dumps_are_enabled
= (dump_file
|| alt_dump_file
|| optinfo_enabled_p ());
75 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
79 set_dump_file (FILE *new_dump_file
)
81 dumpfile_ensure_any_optinfo_are_flushed ();
82 dump_file
= new_dump_file
;
83 refresh_dumps_are_enabled ();
86 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
90 set_alt_dump_file (FILE *new_alt_dump_file
)
92 dumpfile_ensure_any_optinfo_are_flushed ();
93 alt_dump_file
= new_alt_dump_file
;
94 refresh_dumps_are_enabled ();
97 #define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
98 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
99 OPTGROUP_NONE, 0, 0, num, false, false}
101 /* Table of tree dump switches. This must be consistent with the
102 TREE_DUMP_INDEX enumeration in dumpfile.h. */
103 static struct dump_file_info dump_files
[TDI_end
] =
105 DUMP_FILE_INFO (NULL
, NULL
, DK_none
, 0),
106 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa
, 0),
107 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa
, 0),
108 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa
, 0),
109 DUMP_FILE_INFO (".original", "tree-original", DK_tree
, 0),
110 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree
, 0),
111 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree
, 0),
112 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa
, 0),
113 #define FIRST_AUTO_NUMBERED_DUMP 1
114 #define FIRST_ME_AUTO_NUMBERED_DUMP 4
116 DUMP_FILE_INFO (NULL
, "lang-all", DK_lang
, 0),
117 DUMP_FILE_INFO (NULL
, "tree-all", DK_tree
, 0),
118 DUMP_FILE_INFO (NULL
, "rtl-all", DK_rtl
, 0),
119 DUMP_FILE_INFO (NULL
, "ipa-all", DK_ipa
, 0),
122 /* Table of dump options. This must be consistent with the TDF_* flags
123 in dumpfile.h and opt_info_options below. */
124 static const kv_pair
<dump_flags_t
> dump_options
[] =
126 {"address", TDF_ADDRESS
},
127 {"asmname", TDF_ASMNAME
},
130 {"graph", TDF_GRAPH
},
131 {"details", (TDF_DETAILS
| MSG_OPTIMIZED_LOCATIONS
132 | MSG_MISSED_OPTIMIZATION
134 {"cselib", TDF_CSELIB
},
135 {"stats", TDF_STATS
},
136 {"blocks", TDF_BLOCKS
},
138 {"lineno", TDF_LINENO
},
140 {"stmtaddr", TDF_STMTADDR
},
141 {"memsyms", TDF_MEMSYMS
},
143 {"alias", TDF_ALIAS
},
144 {"nouid", TDF_NOUID
},
145 {"enumerate_locals", TDF_ENUMERATE_LOCALS
},
147 {"gimple", TDF_GIMPLE
},
148 {"folding", TDF_FOLDING
},
149 {"optimized", MSG_OPTIMIZED_LOCATIONS
},
150 {"missed", MSG_MISSED_OPTIMIZATION
},
153 {"all", dump_flags_t (~(TDF_RAW
| TDF_SLIM
| TDF_LINENO
| TDF_GRAPH
154 | TDF_STMTADDR
| TDF_RHS_ONLY
| TDF_NOUID
155 | TDF_ENUMERATE_LOCALS
| TDF_SCEV
| TDF_GIMPLE
))},
159 /* A subset of the dump_options table which is used for -fopt-info
160 types. This must be consistent with the MSG_* flags in dumpfile.h.
162 static const kv_pair
<dump_flags_t
> optinfo_verbosity_options
[] =
164 {"optimized", MSG_OPTIMIZED_LOCATIONS
},
165 {"missed", MSG_MISSED_OPTIMIZATION
},
171 /* Flags used for -fopt-info groups. */
172 const kv_pair
<optgroup_flags_t
> optgroup_options
[] =
174 {"ipa", OPTGROUP_IPA
},
175 {"loop", OPTGROUP_LOOP
},
176 {"inline", OPTGROUP_INLINE
},
177 {"omp", OPTGROUP_OMP
},
178 {"vec", OPTGROUP_VEC
},
179 {"optall", OPTGROUP_ALL
},
180 {NULL
, OPTGROUP_NONE
}
183 gcc::dump_manager::dump_manager ():
184 m_next_dump (FIRST_AUTO_NUMBERED_DUMP
),
185 m_extra_dump_files (NULL
),
186 m_extra_dump_files_in_use (0),
187 m_extra_dump_files_alloced (0)
191 gcc::dump_manager::~dump_manager ()
193 for (size_t i
= 0; i
< m_extra_dump_files_in_use
; i
++)
195 dump_file_info
*dfi
= &m_extra_dump_files
[i
];
196 /* suffix, swtch, glob are statically allocated for the entries
197 in dump_files, and for statistics, but are dynamically allocated
198 for those for passes. */
199 if (dfi
->owns_strings
)
201 XDELETEVEC (const_cast <char *> (dfi
->suffix
));
202 XDELETEVEC (const_cast <char *> (dfi
->swtch
));
203 XDELETEVEC (const_cast <char *> (dfi
->glob
));
205 /* These, if non-NULL, are always dynamically allocated. */
206 XDELETEVEC (const_cast <char *> (dfi
->pfilename
));
207 XDELETEVEC (const_cast <char *> (dfi
->alt_filename
));
209 XDELETEVEC (m_extra_dump_files
);
214 dump_register (const char *suffix
, const char *swtch
, const char *glob
,
215 dump_kind dkind
, optgroup_flags_t optgroup_flags
,
218 int num
= m_next_dump
++;
220 size_t count
= m_extra_dump_files_in_use
++;
222 if (count
>= m_extra_dump_files_alloced
)
224 if (m_extra_dump_files_alloced
== 0)
225 m_extra_dump_files_alloced
= 512;
227 m_extra_dump_files_alloced
*= 2;
228 m_extra_dump_files
= XRESIZEVEC (struct dump_file_info
,
230 m_extra_dump_files_alloced
);
232 /* Construct a new object in the space allocated above. */
233 new (m_extra_dump_files
+ count
) dump_file_info ();
237 /* Zero out the already constructed object. */
238 m_extra_dump_files
[count
] = dump_file_info ();
241 m_extra_dump_files
[count
].suffix
= suffix
;
242 m_extra_dump_files
[count
].swtch
= swtch
;
243 m_extra_dump_files
[count
].glob
= glob
;
244 m_extra_dump_files
[count
].dkind
= dkind
;
245 m_extra_dump_files
[count
].optgroup_flags
= optgroup_flags
;
246 m_extra_dump_files
[count
].num
= num
;
247 m_extra_dump_files
[count
].owns_strings
= take_ownership
;
249 return count
+ TDI_end
;
253 /* Allow languages and middle-end to register their dumps before the
254 optimization passes. */
260 lang_hooks
.register_dumps (this);
261 /* If this assert fails, some FE registered more than
262 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
263 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
264 gcc_assert (m_next_dump
<= FIRST_ME_AUTO_NUMBERED_DUMP
);
265 m_next_dump
= FIRST_ME_AUTO_NUMBERED_DUMP
;
266 dump_files
[TDI_original
].num
= m_next_dump
++;
267 dump_files
[TDI_gimple
].num
= m_next_dump
++;
268 dump_files
[TDI_nested
].num
= m_next_dump
++;
272 /* Return the dump_file_info for the given phase. */
274 struct dump_file_info
*
276 get_dump_file_info (int phase
) const
279 return &dump_files
[phase
];
280 else if ((size_t) (phase
- TDI_end
) >= m_extra_dump_files_in_use
)
283 return m_extra_dump_files
+ (phase
- TDI_end
);
286 /* Locate the dump_file_info with swtch equal to SWTCH,
287 or return NULL if no such dump_file_info exists. */
289 struct dump_file_info
*
291 get_dump_file_info_by_switch (const char *swtch
) const
293 for (unsigned i
= 0; i
< m_extra_dump_files_in_use
; i
++)
294 if (strcmp (m_extra_dump_files
[i
].swtch
, swtch
) == 0)
295 return &m_extra_dump_files
[i
];
302 /* Return the name of the dump file for the given phase.
303 The caller is responsible for calling free on the returned
305 If the dump is not enabled, returns NULL. */
309 get_dump_file_name (int phase
, int part
) const
311 struct dump_file_info
*dfi
;
313 if (phase
== TDI_none
)
316 dfi
= get_dump_file_info (phase
);
318 return get_dump_file_name (dfi
, part
);
321 /* Return the name of the dump file for the given dump_file_info.
322 The caller is responsible for calling free on the returned
324 If the dump is not enabled, returns NULL. */
328 get_dump_file_name (struct dump_file_info
*dfi
, int part
) const
334 if (dfi
->pstate
== 0)
337 /* If available, use the command line dump filename. */
339 return xstrdup (dfi
->pfilename
);
345 /* (null), LANG, TREE, RTL, IPA. */
346 char suffix
= " ltri"[dfi
->dkind
];
348 if (snprintf (dump_id
, sizeof (dump_id
), ".%03d%c", dfi
->num
, suffix
) < 0)
355 snprintf (part_id
, sizeof (part_id
), ".%i", part
);
356 return concat (dump_base_name
, dump_id
, part_id
, dfi
->suffix
, NULL
);
359 return concat (dump_base_name
, dump_id
, dfi
->suffix
, NULL
);
362 /* Open a dump file called FILENAME. Some filenames are special and
363 refer to the standard streams. TRUNC indicates whether this is the
364 first open (so the file should be truncated, rather than appended).
365 An error message is emitted in the event of failure. */
368 dump_open (const char *filename
, bool trunc
)
370 if (strcmp ("stderr", filename
) == 0)
373 if (strcmp ("stdout", filename
) == 0
374 || strcmp ("-", filename
) == 0)
377 FILE *stream
= fopen (filename
, trunc
? "w" : "a");
380 error ("could not open dump file %qs: %m", filename
);
384 /* For a given DFI, open an alternate dump filename (which could also
385 be a standard stream such as stdout/stderr). If the alternate dump
386 file cannot be opened, return NULL. */
389 dump_open_alternate_stream (struct dump_file_info
*dfi
)
391 if (!dfi
->alt_filename
)
395 return dfi
->alt_stream
;
397 FILE *stream
= dump_open (dfi
->alt_filename
, dfi
->alt_state
< 0);
405 /* Construct a dump_user_location_t from STMT (using its location and
408 dump_user_location_t::dump_user_location_t (gimple
*stmt
)
409 : m_count (), m_loc (UNKNOWN_LOCATION
)
414 m_count
= stmt
->bb
->count
;
415 m_loc
= gimple_location (stmt
);
419 /* Construct a dump_user_location_t from an RTL instruction (using its
420 location and hotness). */
422 dump_user_location_t::dump_user_location_t (rtx_insn
*insn
)
423 : m_count (), m_loc (UNKNOWN_LOCATION
)
427 basic_block bb
= BLOCK_FOR_INSN (insn
);
430 m_loc
= INSN_LOCATION (insn
);
434 /* Construct from a function declaration. This one requires spelling out
435 to avoid accidentally constructing from other kinds of tree. */
438 dump_user_location_t::from_function_decl (tree fndecl
)
442 // FIXME: profile count for function?
443 return dump_user_location_t (profile_count (),
444 DECL_SOURCE_LOCATION (fndecl
));
447 /* Print source location on DFILE if enabled. */
450 dump_loc (dump_flags_t dump_kind
, FILE *dfile
, source_location loc
)
454 if (LOCATION_LOCUS (loc
) > BUILTINS_LOCATION
)
455 fprintf (dfile
, "%s:%d:%d: note: ", LOCATION_FILE (loc
),
456 LOCATION_LINE (loc
), LOCATION_COLUMN (loc
));
457 else if (current_function_decl
)
458 fprintf (dfile
, "%s:%d:%d: note: ",
459 DECL_SOURCE_FILE (current_function_decl
),
460 DECL_SOURCE_LINE (current_function_decl
),
461 DECL_SOURCE_COLUMN (current_function_decl
));
462 /* Indentation based on scope depth. */
463 fprintf (dfile
, "%*s", get_dump_scope_depth (), "");
467 /* Implementation of dump_context member functions. */
469 /* dump_context's dtor. */
471 dump_context::~dump_context ()
476 /* Dump gimple statement GS with SPC indentation spaces and
477 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
480 dump_context::dump_gimple_stmt (dump_flags_t dump_kind
,
481 dump_flags_t extra_dump_flags
,
484 if (dump_file
&& (dump_kind
& pflags
))
485 print_gimple_stmt (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
487 if (alt_dump_file
&& (dump_kind
& alt_flags
))
488 print_gimple_stmt (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
490 if (optinfo_enabled_p ())
492 optinfo
&info
= ensure_pending_optinfo ();
493 info
.handle_dump_file_kind (dump_kind
);
494 info
.add_gimple_stmt (gs
, spc
, dump_flags
| extra_dump_flags
);
498 /* Similar to dump_gimple_stmt, except additionally print source location. */
501 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind
,
502 const dump_location_t
&loc
,
503 dump_flags_t extra_dump_flags
,
506 location_t srcloc
= loc
.get_location_t ();
507 if (dump_file
&& (dump_kind
& pflags
))
509 dump_loc (dump_kind
, dump_file
, srcloc
);
510 print_gimple_stmt (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
513 if (alt_dump_file
&& (dump_kind
& alt_flags
))
515 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
516 print_gimple_stmt (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
519 if (optinfo_enabled_p ())
521 optinfo
&info
= begin_next_optinfo (loc
);
522 info
.handle_dump_file_kind (dump_kind
);
523 info
.add_gimple_stmt (gs
, spc
, dump_flags
| extra_dump_flags
);
527 /* Dump gimple statement GS with SPC indentation spaces and
528 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
529 Do not terminate with a newline or semicolon. */
532 dump_context::dump_gimple_expr (dump_flags_t dump_kind
,
533 dump_flags_t extra_dump_flags
,
536 if (dump_file
&& (dump_kind
& pflags
))
537 print_gimple_expr (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
539 if (alt_dump_file
&& (dump_kind
& alt_flags
))
540 print_gimple_expr (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
542 if (optinfo_enabled_p ())
544 optinfo
&info
= ensure_pending_optinfo ();
545 info
.handle_dump_file_kind (dump_kind
);
546 info
.add_gimple_expr (gs
, spc
, dump_flags
| extra_dump_flags
);
550 /* Similar to dump_gimple_expr, except additionally print source location. */
553 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind
,
554 const dump_location_t
&loc
,
555 dump_flags_t extra_dump_flags
,
559 location_t srcloc
= loc
.get_location_t ();
560 if (dump_file
&& (dump_kind
& pflags
))
562 dump_loc (dump_kind
, dump_file
, srcloc
);
563 print_gimple_expr (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
566 if (alt_dump_file
&& (dump_kind
& alt_flags
))
568 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
569 print_gimple_expr (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
572 if (optinfo_enabled_p ())
574 optinfo
&info
= begin_next_optinfo (loc
);
575 info
.handle_dump_file_kind (dump_kind
);
576 info
.add_gimple_expr (gs
, spc
, dump_flags
| extra_dump_flags
);
581 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
582 DUMP_KIND is enabled. */
585 dump_context::dump_generic_expr (dump_flags_t dump_kind
,
586 dump_flags_t extra_dump_flags
,
589 if (dump_file
&& (dump_kind
& pflags
))
590 print_generic_expr (dump_file
, t
, dump_flags
| extra_dump_flags
);
592 if (alt_dump_file
&& (dump_kind
& alt_flags
))
593 print_generic_expr (alt_dump_file
, t
, dump_flags
| extra_dump_flags
);
595 if (optinfo_enabled_p ())
597 optinfo
&info
= ensure_pending_optinfo ();
598 info
.handle_dump_file_kind (dump_kind
);
599 info
.add_tree (t
, dump_flags
| extra_dump_flags
);
604 /* Similar to dump_generic_expr, except additionally print the source
608 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind
,
609 const dump_location_t
&loc
,
610 dump_flags_t extra_dump_flags
,
613 location_t srcloc
= loc
.get_location_t ();
614 if (dump_file
&& (dump_kind
& pflags
))
616 dump_loc (dump_kind
, dump_file
, srcloc
);
617 print_generic_expr (dump_file
, t
, dump_flags
| extra_dump_flags
);
620 if (alt_dump_file
&& (dump_kind
& alt_flags
))
622 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
623 print_generic_expr (alt_dump_file
, t
, dump_flags
| extra_dump_flags
);
626 if (optinfo_enabled_p ())
628 optinfo
&info
= begin_next_optinfo (loc
);
629 info
.handle_dump_file_kind (dump_kind
);
630 info
.add_tree (t
, dump_flags
| extra_dump_flags
);
634 /* Output a formatted message using FORMAT on appropriate dump streams. */
637 dump_context::dump_printf_va (dump_flags_t dump_kind
, const char *format
,
640 if (dump_file
&& (dump_kind
& pflags
))
644 vfprintf (dump_file
, format
, aq
);
648 if (alt_dump_file
&& (dump_kind
& alt_flags
))
652 vfprintf (alt_dump_file
, format
, aq
);
656 if (optinfo_enabled_p ())
658 optinfo
&info
= ensure_pending_optinfo ();
661 info
.add_printf_va (format
, aq
);
666 /* Similar to dump_printf, except source location is also printed, and
667 dump location captured. */
670 dump_context::dump_printf_loc_va (dump_flags_t dump_kind
,
671 const dump_location_t
&loc
,
672 const char *format
, va_list ap
)
674 location_t srcloc
= loc
.get_location_t ();
676 if (dump_file
&& (dump_kind
& pflags
))
678 dump_loc (dump_kind
, dump_file
, srcloc
);
681 vfprintf (dump_file
, format
, aq
);
685 if (alt_dump_file
&& (dump_kind
& alt_flags
))
687 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
690 vfprintf (alt_dump_file
, format
, aq
);
694 if (optinfo_enabled_p ())
696 optinfo
&info
= begin_next_optinfo (loc
);
697 info
.handle_dump_file_kind (dump_kind
);
700 info
.add_printf_va (format
, aq
);
705 /* Output VALUE in decimal to appropriate dump streams. */
707 template<unsigned int N
, typename C
>
709 dump_context::dump_dec (dump_flags_t dump_kind
, const poly_int
<N
, C
> &value
)
711 STATIC_ASSERT (poly_coeff_traits
<C
>::signedness
>= 0);
712 signop sgn
= poly_coeff_traits
<C
>::signedness
? SIGNED
: UNSIGNED
;
713 if (dump_file
&& (dump_kind
& pflags
))
714 print_dec (value
, dump_file
, sgn
);
716 if (alt_dump_file
&& (dump_kind
& alt_flags
))
717 print_dec (value
, alt_dump_file
, sgn
);
719 if (optinfo_enabled_p ())
721 optinfo
&info
= ensure_pending_optinfo ();
722 info
.handle_dump_file_kind (dump_kind
);
723 info
.add_poly_int
<N
,C
> (value
);
727 /* Output the name of NODE on appropriate dump streams. */
730 dump_context::dump_symtab_node (dump_flags_t dump_kind
, symtab_node
*node
)
732 if (dump_file
&& (dump_kind
& pflags
))
733 fprintf (dump_file
, "%s", node
->dump_name ());
735 if (alt_dump_file
&& (dump_kind
& alt_flags
))
736 fprintf (alt_dump_file
, "%s", node
->dump_name ());
738 if (optinfo_enabled_p ())
740 optinfo
&info
= ensure_pending_optinfo ();
741 info
.handle_dump_file_kind (dump_kind
);
742 info
.add_symtab_node (node
);
746 /* Get the current dump scope-nesting depth.
747 For use by -fopt-info (for showing nesting via indentation). */
750 dump_context::get_scope_depth () const
752 return m_scope_depth
;
755 /* Push a nested dump scope.
756 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
758 Emit a "scope" optinfo if optinfos are enabled.
759 Increment the scope depth. */
762 dump_context::begin_scope (const char *name
, const dump_location_t
&loc
)
764 /* Specialcase, to avoid going through dump_printf_loc,
765 so that we can create a optinfo of kind OPTINFO_KIND_SCOPE. */
769 dump_loc (MSG_NOTE
, dump_file
, loc
.get_location_t ());
770 fprintf (dump_file
, "=== %s ===\n", name
);
775 dump_loc (MSG_NOTE
, alt_dump_file
, loc
.get_location_t ());
776 fprintf (alt_dump_file
, "=== %s ===\n", name
);
779 if (optinfo_enabled_p ())
782 optinfo
info (loc
, OPTINFO_KIND_SCOPE
, current_pass
);
783 info
.add_printf ("=== %s ===", name
);
790 /* Pop a nested dump scope. */
793 dump_context::end_scope ()
797 optimization_records_maybe_pop_dump_scope ();
800 /* Return the optinfo currently being accumulated, creating one if
804 dump_context::ensure_pending_optinfo ()
807 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
811 /* Start a new optinfo and return it, ending any optinfo that was already
815 dump_context::begin_next_optinfo (const dump_location_t
&loc
)
818 gcc_assert (m_pending
== NULL
);
819 m_pending
= new optinfo (loc
, OPTINFO_KIND_NOTE
, current_pass
);
823 /* End any optinfo that has been accumulated within this context; emitting
824 it to any destinations as appropriate, such as optimization records. */
827 dump_context::end_any_optinfo ()
835 /* The current singleton dump_context, and its default. */
837 dump_context
*dump_context::s_current
= &dump_context::s_default
;
838 dump_context
dump_context::s_default
;
840 /* Implementation of dump_* API calls, calling into dump_context
843 /* Dump gimple statement GS with SPC indentation spaces and
844 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
847 dump_gimple_stmt (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
850 dump_context::get ().dump_gimple_stmt (dump_kind
, extra_dump_flags
, gs
, spc
);
853 /* Similar to dump_gimple_stmt, except additionally print source location. */
856 dump_gimple_stmt_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
857 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
859 dump_context::get ().dump_gimple_stmt_loc (dump_kind
, loc
, extra_dump_flags
,
863 /* Dump gimple statement GS with SPC indentation spaces and
864 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
865 Do not terminate with a newline or semicolon. */
868 dump_gimple_expr (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
871 dump_context::get ().dump_gimple_expr (dump_kind
, extra_dump_flags
, gs
, spc
);
874 /* Similar to dump_gimple_expr, except additionally print source location. */
877 dump_gimple_expr_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
878 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
880 dump_context::get ().dump_gimple_expr_loc (dump_kind
, loc
, extra_dump_flags
,
884 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
885 DUMP_KIND is enabled. */
888 dump_generic_expr (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
891 dump_context::get ().dump_generic_expr (dump_kind
, extra_dump_flags
, t
);
894 /* Similar to dump_generic_expr, except additionally print the source
898 dump_generic_expr_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
899 dump_flags_t extra_dump_flags
, tree t
)
901 dump_context::get ().dump_generic_expr_loc (dump_kind
, loc
, extra_dump_flags
,
905 /* Output a formatted message using FORMAT on appropriate dump streams. */
908 dump_printf (dump_flags_t dump_kind
, const char *format
, ...)
911 va_start (ap
, format
);
912 dump_context::get ().dump_printf_va (dump_kind
, format
, ap
);
916 /* Similar to dump_printf, except source location is also printed, and
917 dump location captured. */
920 dump_printf_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
921 const char *format
, ...)
924 va_start (ap
, format
);
925 dump_context::get ().dump_printf_loc_va (dump_kind
, loc
, format
, ap
);
929 /* Output VALUE in decimal to appropriate dump streams. */
931 template<unsigned int N
, typename C
>
933 dump_dec (dump_flags_t dump_kind
, const poly_int
<N
, C
> &value
)
935 dump_context::get ().dump_dec (dump_kind
, value
);
938 template void dump_dec (dump_flags_t
, const poly_uint16
&);
939 template void dump_dec (dump_flags_t
, const poly_int64
&);
940 template void dump_dec (dump_flags_t
, const poly_uint64
&);
941 template void dump_dec (dump_flags_t
, const poly_offset_int
&);
942 template void dump_dec (dump_flags_t
, const poly_widest_int
&);
945 dump_dec (dump_flags_t dump_kind
, const poly_wide_int
&value
, signop sgn
)
947 if (dump_file
&& (dump_kind
& pflags
))
948 print_dec (value
, dump_file
, sgn
);
950 if (alt_dump_file
&& (dump_kind
& alt_flags
))
951 print_dec (value
, alt_dump_file
, sgn
);
954 /* Output VALUE in hexadecimal to appropriate dump streams. */
957 dump_hex (dump_flags_t dump_kind
, const poly_wide_int
&value
)
959 if (dump_file
&& (dump_kind
& pflags
))
960 print_hex (value
, dump_file
);
962 if (alt_dump_file
&& (dump_kind
& alt_flags
))
963 print_hex (value
, alt_dump_file
);
966 /* Emit and delete the currently pending optinfo, if there is one,
967 without the caller needing to know about class dump_context. */
970 dumpfile_ensure_any_optinfo_are_flushed ()
972 dump_context::get().end_any_optinfo ();
975 /* Output the name of NODE on appropriate dump streams. */
978 dump_symtab_node (dump_flags_t dump_kind
, symtab_node
*node
)
980 dump_context::get ().dump_symtab_node (dump_kind
, node
);
983 /* Get the current dump scope-nesting depth.
984 For use by -fopt-info (for showing nesting via indentation). */
987 get_dump_scope_depth ()
989 return dump_context::get ().get_scope_depth ();
992 /* Push a nested dump scope.
993 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
995 Emit a "scope" opinfo if optinfos are enabled.
996 Increment the scope depth. */
999 dump_begin_scope (const char *name
, const dump_location_t
&loc
)
1001 dump_context::get ().begin_scope (name
, loc
);
1004 /* Pop a nested dump scope. */
1009 dump_context::get ().end_scope ();
1012 /* Start a dump for PHASE. Store user-supplied dump flags in
1013 *FLAG_PTR. Return the number of streams opened. Set globals
1014 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1015 set dump_flags appropriately for both pass dump stream and
1016 -fopt-info stream. */
1020 dump_start (int phase
, dump_flags_t
*flag_ptr
)
1024 struct dump_file_info
*dfi
;
1026 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1029 dfi
= get_dump_file_info (phase
);
1030 name
= get_dump_file_name (phase
);
1033 stream
= dump_open (name
, dfi
->pstate
< 0);
1040 dfi
->pstream
= stream
;
1041 set_dump_file (dfi
->pstream
);
1042 /* Initialize current dump flags. */
1043 pflags
= dfi
->pflags
;
1046 stream
= dump_open_alternate_stream (dfi
);
1049 dfi
->alt_stream
= stream
;
1051 set_alt_dump_file (dfi
->alt_stream
);
1052 /* Initialize current -fopt-info flags. */
1053 alt_flags
= dfi
->alt_flags
;
1057 *flag_ptr
= dfi
->pflags
;
1062 /* Finish a tree dump for PHASE and close associated dump streams. Also
1063 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1067 dump_finish (int phase
)
1069 struct dump_file_info
*dfi
;
1073 dfi
= get_dump_file_info (phase
);
1074 if (dfi
->pstream
&& dfi
->pstream
!= stdout
&& dfi
->pstream
!= stderr
)
1075 fclose (dfi
->pstream
);
1077 if (dfi
->alt_stream
&& dfi
->alt_stream
!= stdout
&& dfi
->alt_stream
!= stderr
)
1078 fclose (dfi
->alt_stream
);
1080 dfi
->alt_stream
= NULL
;
1081 dfi
->pstream
= NULL
;
1082 set_dump_file (NULL
);
1083 set_alt_dump_file (NULL
);
1084 dump_flags
= TDF_NONE
;
1085 alt_flags
= TDF_NONE
;
1089 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1090 *FLAG_PTR and returns a stream to write to. If the dump is not
1091 enabled, returns NULL.
1092 PART can be used for dump files which should be split to multiple
1093 parts. PART == -1 indicates dump file with no parts.
1094 If PART is -1, multiple calls will reopen and append to the dump file. */
1097 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1099 return g
->get_dumps ()->dump_begin (phase
, flag_ptr
, part
);
1104 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1107 struct dump_file_info
*dfi
;
1110 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1113 name
= get_dump_file_name (phase
, part
);
1116 dfi
= get_dump_file_info (phase
);
1118 /* We do not support re-opening of dump files with parts. This would require
1119 tracking pstate per part of the dump file. */
1120 stream
= dump_open (name
, part
!= -1 || dfi
->pstate
< 0);
1126 *flag_ptr
= dfi
->pflags
;
1128 /* Initialize current flags */
1129 pflags
= dfi
->pflags
;
1133 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1134 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1139 dump_phase_enabled_p (int phase
) const
1141 if (phase
== TDI_tree_all
)
1144 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1145 if (dump_files
[i
].pstate
|| dump_files
[i
].alt_state
)
1147 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1148 if (m_extra_dump_files
[i
].pstate
|| m_extra_dump_files
[i
].alt_state
)
1154 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1155 return dfi
->pstate
|| dfi
->alt_state
;
1159 /* Returns nonzero if tree dump PHASE has been initialized. */
1163 dump_initialized_p (int phase
) const
1165 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1166 return dfi
->pstate
> 0 || dfi
->alt_state
> 0;
1169 /* Returns the switch name of PHASE. */
1172 dump_flag_name (int phase
)
1174 return g
->get_dumps ()->dump_flag_name (phase
);
1179 dump_flag_name (int phase
) const
1181 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1185 /* Finish a tree dump for PHASE. STREAM is the stream created by
1189 dump_end (int phase ATTRIBUTE_UNUSED
, FILE *stream
)
1191 if (stream
!= stderr
&& stream
!= stdout
)
1195 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1196 enabled tree dumps. */
1200 dump_enable_all (dump_kind dkind
, dump_flags_t flags
, const char *filename
)
1205 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1207 if ((dump_files
[i
].dkind
== dkind
))
1209 const char *old_filename
= dump_files
[i
].pfilename
;
1210 dump_files
[i
].pstate
= -1;
1211 dump_files
[i
].pflags
|= flags
;
1213 /* Override the existing filename. */
1216 dump_files
[i
].pfilename
= xstrdup (filename
);
1217 /* Since it is a command-line provided file, which is
1218 common to all the phases, use it in append mode. */
1219 dump_files
[i
].pstate
= 1;
1221 if (old_filename
&& filename
!= old_filename
)
1222 free (CONST_CAST (char *, old_filename
));
1226 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1228 if ((m_extra_dump_files
[i
].dkind
== dkind
))
1230 const char *old_filename
= m_extra_dump_files
[i
].pfilename
;
1231 m_extra_dump_files
[i
].pstate
= -1;
1232 m_extra_dump_files
[i
].pflags
|= flags
;
1234 /* Override the existing filename. */
1237 m_extra_dump_files
[i
].pfilename
= xstrdup (filename
);
1238 /* Since it is a command-line provided file, which is
1239 common to all the phases, use it in append mode. */
1240 m_extra_dump_files
[i
].pstate
= 1;
1242 if (old_filename
&& filename
!= old_filename
)
1243 free (CONST_CAST (char *, old_filename
));
1250 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1251 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1256 opt_info_enable_passes (optgroup_flags_t optgroup_flags
, dump_flags_t flags
,
1257 const char *filename
)
1262 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1264 if ((dump_files
[i
].optgroup_flags
& optgroup_flags
))
1266 const char *old_filename
= dump_files
[i
].alt_filename
;
1267 /* Since this file is shared among different passes, it
1268 should be opened in append mode. */
1269 dump_files
[i
].alt_state
= 1;
1270 dump_files
[i
].alt_flags
|= flags
;
1272 /* Override the existing filename. */
1274 dump_files
[i
].alt_filename
= xstrdup (filename
);
1275 if (old_filename
&& filename
!= old_filename
)
1276 free (CONST_CAST (char *, old_filename
));
1280 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1282 if ((m_extra_dump_files
[i
].optgroup_flags
& optgroup_flags
))
1284 const char *old_filename
= m_extra_dump_files
[i
].alt_filename
;
1285 /* Since this file is shared among different passes, it
1286 should be opened in append mode. */
1287 m_extra_dump_files
[i
].alt_state
= 1;
1288 m_extra_dump_files
[i
].alt_flags
|= flags
;
1290 /* Override the existing filename. */
1292 m_extra_dump_files
[i
].alt_filename
= xstrdup (filename
);
1293 if (old_filename
&& filename
!= old_filename
)
1294 free (CONST_CAST (char *, old_filename
));
1301 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1302 relevant details in the dump_files array. */
1306 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
, bool doglob
)
1308 const char *option_value
;
1312 if (doglob
&& !dfi
->glob
)
1315 option_value
= skip_leading_substring (arg
, doglob
? dfi
->glob
: dfi
->swtch
);
1319 if (*option_value
&& *option_value
!= '-' && *option_value
!= '=')
1327 const struct kv_pair
<dump_flags_t
> *option_ptr
;
1328 const char *end_ptr
;
1334 end_ptr
= strchr (ptr
, '-');
1335 eq_ptr
= strchr (ptr
, '=');
1337 if (eq_ptr
&& !end_ptr
)
1341 end_ptr
= ptr
+ strlen (ptr
);
1342 length
= end_ptr
- ptr
;
1344 for (option_ptr
= dump_options
; option_ptr
->name
; option_ptr
++)
1345 if (strlen (option_ptr
->name
) == length
1346 && !memcmp (option_ptr
->name
, ptr
, length
))
1348 flags
|= option_ptr
->value
;
1354 /* Interpret rest of the argument as a dump filename. This
1355 filename overrides other command line filenames. */
1357 free (CONST_CAST (char *, dfi
->pfilename
));
1358 dfi
->pfilename
= xstrdup (ptr
+ 1);
1362 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1363 length
, ptr
, dfi
->swtch
);
1369 dfi
->pflags
|= flags
;
1371 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1373 if (dfi
->suffix
== NULL
)
1374 dump_enable_all (dfi
->dkind
, dfi
->pflags
, dfi
->pfilename
);
1381 dump_switch_p (const char *arg
)
1386 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1387 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], false);
1389 /* Don't glob if we got a hit already */
1391 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1392 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], true);
1394 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1395 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], false);
1398 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1399 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], true);
1405 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1406 and filename. Return non-zero if it is a recognized switch. */
1409 opt_info_switch_p_1 (const char *arg
, dump_flags_t
*flags
,
1410 optgroup_flags_t
*optgroup_flags
, char **filename
)
1412 const char *option_value
;
1420 *optgroup_flags
= OPTGROUP_NONE
;
1423 return 1; /* Handle '-fopt-info' without any additional options. */
1427 const char *end_ptr
;
1433 end_ptr
= strchr (ptr
, '-');
1434 eq_ptr
= strchr (ptr
, '=');
1436 if (eq_ptr
&& !end_ptr
)
1440 end_ptr
= ptr
+ strlen (ptr
);
1441 length
= end_ptr
- ptr
;
1443 for (const kv_pair
<dump_flags_t
> *option_ptr
= optinfo_verbosity_options
;
1444 option_ptr
->name
; option_ptr
++)
1445 if (strlen (option_ptr
->name
) == length
1446 && !memcmp (option_ptr
->name
, ptr
, length
))
1448 *flags
|= option_ptr
->value
;
1452 for (const kv_pair
<optgroup_flags_t
> *option_ptr
= optgroup_options
;
1453 option_ptr
->name
; option_ptr
++)
1454 if (strlen (option_ptr
->name
) == length
1455 && !memcmp (option_ptr
->name
, ptr
, length
))
1457 *optgroup_flags
|= option_ptr
->value
;
1463 /* Interpret rest of the argument as a dump filename. This
1464 filename overrides other command line filenames. */
1465 *filename
= xstrdup (ptr
+ 1);
1470 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1481 /* Return non-zero if ARG is a recognized switch for
1482 -fopt-info. Return zero otherwise. */
1485 opt_info_switch_p (const char *arg
)
1488 optgroup_flags_t optgroup_flags
;
1490 static char *file_seen
= NULL
;
1491 gcc::dump_manager
*dumps
= g
->get_dumps ();
1493 if (!opt_info_switch_p_1 (arg
, &flags
, &optgroup_flags
, &filename
))
1497 filename
= xstrdup ("stderr");
1499 /* Bail out if a different filename has been specified. */
1500 if (file_seen
&& strcmp (file_seen
, filename
))
1502 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1507 file_seen
= xstrdup (filename
);
1509 flags
= MSG_OPTIMIZED_LOCATIONS
;
1510 if (!optgroup_flags
)
1511 optgroup_flags
= OPTGROUP_ALL
;
1513 return dumps
->opt_info_enable_passes (optgroup_flags
, flags
, filename
);
1516 /* Print basic block on the dump streams. */
1519 dump_basic_block (dump_flags_t dump_kind
, basic_block bb
, int indent
)
1521 if (dump_file
&& (dump_kind
& pflags
))
1522 dump_bb (dump_file
, bb
, indent
, TDF_DETAILS
);
1523 if (alt_dump_file
&& (dump_kind
& alt_flags
))
1524 dump_bb (alt_dump_file
, bb
, indent
, TDF_DETAILS
);
1527 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1530 dump_function (int phase
, tree fn
)
1535 stream
= dump_begin (phase
, &flags
);
1538 dump_function_to_file (fn
, stream
, flags
);
1539 dump_end (phase
, stream
);
1543 /* Print information from the combine pass on dump_file. */
1546 print_combine_total_stats (void)
1549 dump_combine_total_stats (dump_file
);
1552 /* Enable RTL dump for all the RTL passes. */
1555 enable_rtl_dump_file (void)
1557 gcc::dump_manager
*dumps
= g
->get_dumps ();
1559 dumps
->dump_enable_all (DK_rtl
, dump_flags_t (TDF_DETAILS
) | TDF_BLOCKS
,
1561 return num_enabled
> 0;
1566 /* temp_dump_context's ctor. Temporarily override the dump_context
1567 (to forcibly enable optinfo-generation). */
1569 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo
)
1571 m_saved (&dump_context ().get ())
1573 dump_context::s_current
= &m_context
;
1574 m_context
.m_forcibly_enable_optinfo
= forcibly_enable_optinfo
;
1577 /* temp_dump_context's dtor. Restore the saved dump_context. */
1579 temp_dump_context::~temp_dump_context ()
1581 dump_context::s_current
= m_saved
;
1584 namespace selftest
{
1586 /* Verify that the dump_location_t constructors capture the source location
1587 at which they were called (provided that the build compiler is sufficiently
1591 test_impl_location ()
1593 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
1596 dump_location_t loc
;
1597 const int expected_line
= __LINE__
- 1;
1598 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1599 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1602 /* Constructing from a gimple. */
1604 dump_location_t
loc ((gimple
*)NULL
);
1605 const int expected_line
= __LINE__
- 1;
1606 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1607 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1610 /* Constructing from an rtx_insn. */
1612 dump_location_t
loc ((rtx_insn
*)NULL
);
1613 const int expected_line
= __LINE__
- 1;
1614 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1615 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1620 /* Verify that ITEM has the expected values. */
1623 verify_item (const location
&loc
,
1624 const optinfo_item
*item
,
1625 enum optinfo_item_kind expected_kind
,
1626 location_t expected_location
,
1627 const char *expected_text
)
1629 ASSERT_EQ_AT (loc
, item
->get_kind (), expected_kind
);
1630 ASSERT_EQ_AT (loc
, item
->get_location (), expected_location
);
1631 ASSERT_STREQ_AT (loc
, item
->get_text (), expected_text
);
1634 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
1636 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
1637 SELFTEST_BEGIN_STMT \
1638 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
1639 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
1642 /* Verify that ITEM is a tree item, with the expected values. */
1644 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1645 SELFTEST_BEGIN_STMT \
1646 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
1647 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1650 /* Verify that ITEM is a gimple item, with the expected values. */
1652 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1653 SELFTEST_BEGIN_STMT \
1654 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
1655 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1658 /* Verify that calls to the dump_* API are captured and consolidated into
1659 optimization records. */
1662 test_capture_of_dump_calls (const line_table_case
&case_
)
1664 /* Generate a location_t for testing. */
1665 line_table_test
ltt (case_
);
1666 linemap_add (line_table
, LC_ENTER
, false, "test.txt", 0);
1667 linemap_line_start (line_table
, 5, 100);
1668 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1669 location_t where
= linemap_position_for_column (line_table
, 10);
1671 dump_location_t loc
= dump_location_t::from_location_t (where
);
1673 /* Test of dump_printf. */
1675 temp_dump_context
tmp (true);
1676 dump_printf (MSG_NOTE
, "int: %i str: %s", 42, "foo");
1678 optinfo
*info
= tmp
.get_pending_optinfo ();
1679 ASSERT_TRUE (info
!= NULL
);
1680 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
1681 ASSERT_EQ (info
->num_items (), 1);
1682 ASSERT_IS_TEXT (info
->get_item (0), "int: 42 str: foo");
1685 /* Tree, via dump_generic_expr. */
1687 temp_dump_context
tmp (true);
1688 dump_printf_loc (MSG_NOTE
, loc
, "test of tree: ");
1689 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, integer_zero_node
);
1691 optinfo
*info
= tmp
.get_pending_optinfo ();
1692 ASSERT_TRUE (info
!= NULL
);
1693 ASSERT_EQ (info
->get_location_t (), where
);
1694 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
1695 ASSERT_EQ (info
->num_items (), 2);
1696 ASSERT_IS_TEXT (info
->get_item (0), "test of tree: ");
1697 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
1700 /* Tree, via dump_generic_expr_loc. */
1702 temp_dump_context
tmp (true);
1703 dump_generic_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, integer_one_node
);
1705 optinfo
*info
= tmp
.get_pending_optinfo ();
1706 ASSERT_TRUE (info
!= NULL
);
1707 ASSERT_EQ (info
->get_location_t (), where
);
1708 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
1709 ASSERT_EQ (info
->num_items (), 1);
1710 ASSERT_IS_TREE (info
->get_item (0), UNKNOWN_LOCATION
, "1");
1715 greturn
*stmt
= gimple_build_return (NULL
);
1716 gimple_set_location (stmt
, where
);
1718 /* dump_gimple_stmt_loc. */
1720 temp_dump_context
tmp (true);
1721 dump_gimple_stmt_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
1723 optinfo
*info
= tmp
.get_pending_optinfo ();
1724 ASSERT_TRUE (info
!= NULL
);
1725 ASSERT_EQ (info
->num_items (), 1);
1726 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;\n");
1729 /* dump_gimple_stmt. */
1731 temp_dump_context
tmp (true);
1732 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
1734 optinfo
*info
= tmp
.get_pending_optinfo ();
1735 ASSERT_TRUE (info
!= NULL
);
1736 ASSERT_EQ (info
->num_items (), 1);
1737 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;\n");
1740 /* dump_gimple_expr_loc. */
1742 temp_dump_context
tmp (true);
1743 dump_gimple_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
1745 optinfo
*info
= tmp
.get_pending_optinfo ();
1746 ASSERT_TRUE (info
!= NULL
);
1747 ASSERT_EQ (info
->num_items (), 1);
1748 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;");
1751 /* dump_gimple_expr. */
1753 temp_dump_context
tmp (true);
1754 dump_gimple_expr (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
1756 optinfo
*info
= tmp
.get_pending_optinfo ();
1757 ASSERT_TRUE (info
!= NULL
);
1758 ASSERT_EQ (info
->num_items (), 1);
1759 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;");
1765 temp_dump_context
tmp (true);
1766 dump_dec (MSG_NOTE
, poly_int64 (42));
1768 optinfo
*info
= tmp
.get_pending_optinfo ();
1769 ASSERT_TRUE (info
!= NULL
);
1770 ASSERT_EQ (info
->num_items (), 1);
1771 ASSERT_IS_TEXT (info
->get_item (0), "42");
1774 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
1777 /* MSG_OPTIMIZED_LOCATIONS. */
1779 temp_dump_context
tmp (true);
1780 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
, "test");
1781 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
1782 OPTINFO_KIND_SUCCESS
);
1785 /* MSG_MISSED_OPTIMIZATION. */
1787 temp_dump_context
tmp (true);
1788 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
, "test");
1789 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
1790 OPTINFO_KIND_FAILURE
);
1795 /* Run all of the selftests within this file. */
1800 test_impl_location ();
1801 for_each_line_table_case (test_capture_of_dump_calls
);
1804 } // namespace selftest
1806 #endif /* CHECKING_P */