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_ALL_VALUES
154 & ~(TDF_RAW
| TDF_SLIM
| TDF_LINENO
| TDF_GRAPH
155 | TDF_STMTADDR
| TDF_RHS_ONLY
| TDF_NOUID
156 | TDF_ENUMERATE_LOCALS
| TDF_SCEV
| TDF_GIMPLE
))},
160 /* A subset of the dump_options table which is used for -fopt-info
161 types. This must be consistent with the MSG_* flags in dumpfile.h.
163 static const kv_pair
<dump_flags_t
> optinfo_verbosity_options
[] =
165 {"optimized", MSG_OPTIMIZED_LOCATIONS
},
166 {"missed", MSG_MISSED_OPTIMIZATION
},
172 /* Flags used for -fopt-info groups. */
173 const kv_pair
<optgroup_flags_t
> optgroup_options
[] =
175 {"ipa", OPTGROUP_IPA
},
176 {"loop", OPTGROUP_LOOP
},
177 {"inline", OPTGROUP_INLINE
},
178 {"omp", OPTGROUP_OMP
},
179 {"vec", OPTGROUP_VEC
},
180 {"optall", OPTGROUP_ALL
},
181 {NULL
, OPTGROUP_NONE
}
184 gcc::dump_manager::dump_manager ():
185 m_next_dump (FIRST_AUTO_NUMBERED_DUMP
),
186 m_extra_dump_files (NULL
),
187 m_extra_dump_files_in_use (0),
188 m_extra_dump_files_alloced (0)
192 gcc::dump_manager::~dump_manager ()
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 (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 (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 /* Print source location on DFILE if enabled. */
451 dump_loc (dump_flags_t dump_kind
, FILE *dfile
, source_location loc
)
455 if (LOCATION_LOCUS (loc
) > BUILTINS_LOCATION
)
456 fprintf (dfile
, "%s:%d:%d: note: ", LOCATION_FILE (loc
),
457 LOCATION_LINE (loc
), LOCATION_COLUMN (loc
));
458 else if (current_function_decl
)
459 fprintf (dfile
, "%s:%d:%d: note: ",
460 DECL_SOURCE_FILE (current_function_decl
),
461 DECL_SOURCE_LINE (current_function_decl
),
462 DECL_SOURCE_COLUMN (current_function_decl
));
463 /* Indentation based on scope depth. */
464 fprintf (dfile
, "%*s", get_dump_scope_depth (), "");
468 /* Implementation of dump_context member functions. */
470 /* dump_context's dtor. */
472 dump_context::~dump_context ()
477 /* Dump gimple statement GS with SPC indentation spaces and
478 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
481 dump_context::dump_gimple_stmt (dump_flags_t dump_kind
,
482 dump_flags_t extra_dump_flags
,
485 if (dump_file
&& (dump_kind
& pflags
))
486 print_gimple_stmt (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
488 if (alt_dump_file
&& (dump_kind
& alt_flags
))
489 print_gimple_stmt (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
491 if (optinfo_enabled_p ())
493 optinfo
&info
= ensure_pending_optinfo ();
494 info
.handle_dump_file_kind (dump_kind
);
495 info
.add_gimple_stmt (gs
, spc
, dump_flags
| extra_dump_flags
);
499 /* Similar to dump_gimple_stmt, except additionally print source location. */
502 dump_context::dump_gimple_stmt_loc (dump_flags_t dump_kind
,
503 const dump_location_t
&loc
,
504 dump_flags_t extra_dump_flags
,
507 location_t srcloc
= loc
.get_location_t ();
508 if (dump_file
&& (dump_kind
& pflags
))
510 dump_loc (dump_kind
, dump_file
, srcloc
);
511 print_gimple_stmt (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
514 if (alt_dump_file
&& (dump_kind
& alt_flags
))
516 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
517 print_gimple_stmt (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
520 if (optinfo_enabled_p ())
522 optinfo
&info
= begin_next_optinfo (loc
);
523 info
.handle_dump_file_kind (dump_kind
);
524 info
.add_gimple_stmt (gs
, spc
, dump_flags
| extra_dump_flags
);
528 /* Dump gimple statement GS with SPC indentation spaces and
529 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
530 Do not terminate with a newline or semicolon. */
533 dump_context::dump_gimple_expr (dump_flags_t dump_kind
,
534 dump_flags_t extra_dump_flags
,
537 if (dump_file
&& (dump_kind
& pflags
))
538 print_gimple_expr (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
540 if (alt_dump_file
&& (dump_kind
& alt_flags
))
541 print_gimple_expr (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
543 if (optinfo_enabled_p ())
545 optinfo
&info
= ensure_pending_optinfo ();
546 info
.handle_dump_file_kind (dump_kind
);
547 info
.add_gimple_expr (gs
, spc
, dump_flags
| extra_dump_flags
);
551 /* Similar to dump_gimple_expr, except additionally print source location. */
554 dump_context::dump_gimple_expr_loc (dump_flags_t dump_kind
,
555 const dump_location_t
&loc
,
556 dump_flags_t extra_dump_flags
,
560 location_t srcloc
= loc
.get_location_t ();
561 if (dump_file
&& (dump_kind
& pflags
))
563 dump_loc (dump_kind
, dump_file
, srcloc
);
564 print_gimple_expr (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
567 if (alt_dump_file
&& (dump_kind
& alt_flags
))
569 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
570 print_gimple_expr (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
573 if (optinfo_enabled_p ())
575 optinfo
&info
= begin_next_optinfo (loc
);
576 info
.handle_dump_file_kind (dump_kind
);
577 info
.add_gimple_expr (gs
, spc
, dump_flags
| extra_dump_flags
);
582 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
583 DUMP_KIND is enabled. */
586 dump_context::dump_generic_expr (dump_flags_t dump_kind
,
587 dump_flags_t extra_dump_flags
,
590 if (dump_file
&& (dump_kind
& pflags
))
591 print_generic_expr (dump_file
, t
, dump_flags
| extra_dump_flags
);
593 if (alt_dump_file
&& (dump_kind
& alt_flags
))
594 print_generic_expr (alt_dump_file
, t
, dump_flags
| extra_dump_flags
);
596 if (optinfo_enabled_p ())
598 optinfo
&info
= ensure_pending_optinfo ();
599 info
.handle_dump_file_kind (dump_kind
);
600 info
.add_tree (t
, dump_flags
| extra_dump_flags
);
605 /* Similar to dump_generic_expr, except additionally print the source
609 dump_context::dump_generic_expr_loc (dump_flags_t dump_kind
,
610 const dump_location_t
&loc
,
611 dump_flags_t extra_dump_flags
,
614 location_t srcloc
= loc
.get_location_t ();
615 if (dump_file
&& (dump_kind
& pflags
))
617 dump_loc (dump_kind
, dump_file
, srcloc
);
618 print_generic_expr (dump_file
, t
, dump_flags
| extra_dump_flags
);
621 if (alt_dump_file
&& (dump_kind
& alt_flags
))
623 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
624 print_generic_expr (alt_dump_file
, t
, dump_flags
| extra_dump_flags
);
627 if (optinfo_enabled_p ())
629 optinfo
&info
= begin_next_optinfo (loc
);
630 info
.handle_dump_file_kind (dump_kind
);
631 info
.add_tree (t
, dump_flags
| extra_dump_flags
);
635 /* Output a formatted message using FORMAT on appropriate dump streams. */
638 dump_context::dump_printf_va (dump_flags_t dump_kind
, const char *format
,
641 if (dump_file
&& (dump_kind
& pflags
))
645 vfprintf (dump_file
, format
, aq
);
649 if (alt_dump_file
&& (dump_kind
& alt_flags
))
653 vfprintf (alt_dump_file
, format
, aq
);
657 if (optinfo_enabled_p ())
659 optinfo
&info
= ensure_pending_optinfo ();
662 info
.add_printf_va (format
, aq
);
667 /* Similar to dump_printf, except source location is also printed, and
668 dump location captured. */
671 dump_context::dump_printf_loc_va (dump_flags_t dump_kind
,
672 const dump_location_t
&loc
,
673 const char *format
, va_list ap
)
675 location_t srcloc
= loc
.get_location_t ();
677 if (dump_file
&& (dump_kind
& pflags
))
679 dump_loc (dump_kind
, dump_file
, srcloc
);
682 vfprintf (dump_file
, format
, aq
);
686 if (alt_dump_file
&& (dump_kind
& alt_flags
))
688 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
691 vfprintf (alt_dump_file
, format
, aq
);
695 if (optinfo_enabled_p ())
697 optinfo
&info
= begin_next_optinfo (loc
);
698 info
.handle_dump_file_kind (dump_kind
);
701 info
.add_printf_va (format
, aq
);
706 /* Output VALUE in decimal to appropriate dump streams. */
708 template<unsigned int N
, typename C
>
710 dump_context::dump_dec (dump_flags_t dump_kind
, const poly_int
<N
, C
> &value
)
712 STATIC_ASSERT (poly_coeff_traits
<C
>::signedness
>= 0);
713 signop sgn
= poly_coeff_traits
<C
>::signedness
? SIGNED
: UNSIGNED
;
714 if (dump_file
&& (dump_kind
& pflags
))
715 print_dec (value
, dump_file
, sgn
);
717 if (alt_dump_file
&& (dump_kind
& alt_flags
))
718 print_dec (value
, alt_dump_file
, sgn
);
720 if (optinfo_enabled_p ())
722 optinfo
&info
= ensure_pending_optinfo ();
723 info
.handle_dump_file_kind (dump_kind
);
724 info
.add_poly_int
<N
,C
> (value
);
728 /* Output the name of NODE on appropriate dump streams. */
731 dump_context::dump_symtab_node (dump_flags_t dump_kind
, symtab_node
*node
)
733 if (dump_file
&& (dump_kind
& pflags
))
734 fprintf (dump_file
, "%s", node
->dump_name ());
736 if (alt_dump_file
&& (dump_kind
& alt_flags
))
737 fprintf (alt_dump_file
, "%s", node
->dump_name ());
739 if (optinfo_enabled_p ())
741 optinfo
&info
= ensure_pending_optinfo ();
742 info
.handle_dump_file_kind (dump_kind
);
743 info
.add_symtab_node (node
);
747 /* Get the current dump scope-nesting depth.
748 For use by -fopt-info (for showing nesting via indentation). */
751 dump_context::get_scope_depth () const
753 return m_scope_depth
;
756 /* Push a nested dump scope.
757 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
759 Emit a "scope" optinfo if optinfos are enabled.
760 Increment the scope depth. */
763 dump_context::begin_scope (const char *name
, const dump_location_t
&loc
)
765 /* Specialcase, to avoid going through dump_printf_loc,
766 so that we can create a optinfo of kind OPTINFO_KIND_SCOPE. */
770 dump_loc (MSG_NOTE
, dump_file
, loc
.get_location_t ());
771 fprintf (dump_file
, "=== %s ===\n", name
);
776 dump_loc (MSG_NOTE
, alt_dump_file
, loc
.get_location_t ());
777 fprintf (alt_dump_file
, "=== %s ===\n", name
);
780 if (optinfo_enabled_p ())
783 optinfo
info (loc
, OPTINFO_KIND_SCOPE
, current_pass
);
784 info
.add_printf ("=== %s ===", name
);
791 /* Pop a nested dump scope. */
794 dump_context::end_scope ()
798 optimization_records_maybe_pop_dump_scope ();
801 /* Return the optinfo currently being accumulated, creating one if
805 dump_context::ensure_pending_optinfo ()
808 return begin_next_optinfo (dump_location_t (dump_user_location_t ()));
812 /* Start a new optinfo and return it, ending any optinfo that was already
816 dump_context::begin_next_optinfo (const dump_location_t
&loc
)
819 gcc_assert (m_pending
== NULL
);
820 m_pending
= new optinfo (loc
, OPTINFO_KIND_NOTE
, current_pass
);
824 /* End any optinfo that has been accumulated within this context; emitting
825 it to any destinations as appropriate, such as optimization records. */
828 dump_context::end_any_optinfo ()
836 /* The current singleton dump_context, and its default. */
838 dump_context
*dump_context::s_current
= &dump_context::s_default
;
839 dump_context
dump_context::s_default
;
841 /* Implementation of dump_* API calls, calling into dump_context
844 /* Dump gimple statement GS with SPC indentation spaces and
845 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
848 dump_gimple_stmt (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
851 dump_context::get ().dump_gimple_stmt (dump_kind
, extra_dump_flags
, gs
, spc
);
854 /* Similar to dump_gimple_stmt, except additionally print source location. */
857 dump_gimple_stmt_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
858 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
860 dump_context::get ().dump_gimple_stmt_loc (dump_kind
, loc
, extra_dump_flags
,
864 /* Dump gimple statement GS with SPC indentation spaces and
865 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
866 Do not terminate with a newline or semicolon. */
869 dump_gimple_expr (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
872 dump_context::get ().dump_gimple_expr (dump_kind
, extra_dump_flags
, gs
, spc
);
875 /* Similar to dump_gimple_expr, except additionally print source location. */
878 dump_gimple_expr_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
879 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
881 dump_context::get ().dump_gimple_expr_loc (dump_kind
, loc
, extra_dump_flags
,
885 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
886 DUMP_KIND is enabled. */
889 dump_generic_expr (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
892 dump_context::get ().dump_generic_expr (dump_kind
, extra_dump_flags
, t
);
895 /* Similar to dump_generic_expr, except additionally print the source
899 dump_generic_expr_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
900 dump_flags_t extra_dump_flags
, tree t
)
902 dump_context::get ().dump_generic_expr_loc (dump_kind
, loc
, extra_dump_flags
,
906 /* Output a formatted message using FORMAT on appropriate dump streams. */
909 dump_printf (dump_flags_t dump_kind
, const char *format
, ...)
912 va_start (ap
, format
);
913 dump_context::get ().dump_printf_va (dump_kind
, format
, ap
);
917 /* Similar to dump_printf, except source location is also printed, and
918 dump location captured. */
921 dump_printf_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
922 const char *format
, ...)
925 va_start (ap
, format
);
926 dump_context::get ().dump_printf_loc_va (dump_kind
, loc
, format
, ap
);
930 /* Output VALUE in decimal to appropriate dump streams. */
932 template<unsigned int N
, typename C
>
934 dump_dec (dump_flags_t dump_kind
, const poly_int
<N
, C
> &value
)
936 dump_context::get ().dump_dec (dump_kind
, value
);
939 template void dump_dec (dump_flags_t
, const poly_uint16
&);
940 template void dump_dec (dump_flags_t
, const poly_int64
&);
941 template void dump_dec (dump_flags_t
, const poly_uint64
&);
942 template void dump_dec (dump_flags_t
, const poly_offset_int
&);
943 template void dump_dec (dump_flags_t
, const poly_widest_int
&);
946 dump_dec (dump_flags_t dump_kind
, const poly_wide_int
&value
, signop sgn
)
948 if (dump_file
&& (dump_kind
& pflags
))
949 print_dec (value
, dump_file
, sgn
);
951 if (alt_dump_file
&& (dump_kind
& alt_flags
))
952 print_dec (value
, alt_dump_file
, sgn
);
955 /* Output VALUE in hexadecimal to appropriate dump streams. */
958 dump_hex (dump_flags_t dump_kind
, const poly_wide_int
&value
)
960 if (dump_file
&& (dump_kind
& pflags
))
961 print_hex (value
, dump_file
);
963 if (alt_dump_file
&& (dump_kind
& alt_flags
))
964 print_hex (value
, alt_dump_file
);
967 /* Emit and delete the currently pending optinfo, if there is one,
968 without the caller needing to know about class dump_context. */
971 dumpfile_ensure_any_optinfo_are_flushed ()
973 dump_context::get().end_any_optinfo ();
976 /* Output the name of NODE on appropriate dump streams. */
979 dump_symtab_node (dump_flags_t dump_kind
, symtab_node
*node
)
981 dump_context::get ().dump_symtab_node (dump_kind
, node
);
984 /* Get the current dump scope-nesting depth.
985 For use by -fopt-info (for showing nesting via indentation). */
988 get_dump_scope_depth ()
990 return dump_context::get ().get_scope_depth ();
993 /* Push a nested dump scope.
994 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
996 Emit a "scope" opinfo if optinfos are enabled.
997 Increment the scope depth. */
1000 dump_begin_scope (const char *name
, const dump_location_t
&loc
)
1002 dump_context::get ().begin_scope (name
, loc
);
1005 /* Pop a nested dump scope. */
1010 dump_context::get ().end_scope ();
1013 /* Start a dump for PHASE. Store user-supplied dump flags in
1014 *FLAG_PTR. Return the number of streams opened. Set globals
1015 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
1016 set dump_flags appropriately for both pass dump stream and
1017 -fopt-info stream. */
1021 dump_start (int phase
, dump_flags_t
*flag_ptr
)
1025 struct dump_file_info
*dfi
;
1027 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1030 dfi
= get_dump_file_info (phase
);
1031 name
= get_dump_file_name (phase
);
1034 stream
= dump_open (name
, dfi
->pstate
< 0);
1041 dfi
->pstream
= stream
;
1042 set_dump_file (dfi
->pstream
);
1043 /* Initialize current dump flags. */
1044 pflags
= dfi
->pflags
;
1047 stream
= dump_open_alternate_stream (dfi
);
1050 dfi
->alt_stream
= stream
;
1052 set_alt_dump_file (dfi
->alt_stream
);
1053 /* Initialize current -fopt-info flags. */
1054 alt_flags
= dfi
->alt_flags
;
1058 *flag_ptr
= dfi
->pflags
;
1063 /* Finish a tree dump for PHASE and close associated dump streams. Also
1064 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1068 dump_finish (int phase
)
1070 struct dump_file_info
*dfi
;
1074 dfi
= get_dump_file_info (phase
);
1075 if (dfi
->pstream
&& dfi
->pstream
!= stdout
&& dfi
->pstream
!= stderr
)
1076 fclose (dfi
->pstream
);
1078 if (dfi
->alt_stream
&& dfi
->alt_stream
!= stdout
&& dfi
->alt_stream
!= stderr
)
1079 fclose (dfi
->alt_stream
);
1081 dfi
->alt_stream
= NULL
;
1082 dfi
->pstream
= NULL
;
1083 set_dump_file (NULL
);
1084 set_alt_dump_file (NULL
);
1085 dump_flags
= TDF_NONE
;
1086 alt_flags
= TDF_NONE
;
1090 /* Begin a tree dump for PHASE. Stores any user supplied flag in
1091 *FLAG_PTR and returns a stream to write to. If the dump is not
1092 enabled, returns NULL.
1093 PART can be used for dump files which should be split to multiple
1094 parts. PART == -1 indicates dump file with no parts.
1095 If PART is -1, multiple calls will reopen and append to the dump file. */
1098 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1100 return g
->get_dumps ()->dump_begin (phase
, flag_ptr
, part
);
1105 dump_begin (int phase
, dump_flags_t
*flag_ptr
, int part
)
1108 struct dump_file_info
*dfi
;
1111 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
1114 name
= get_dump_file_name (phase
, part
);
1117 dfi
= get_dump_file_info (phase
);
1119 /* We do not support re-opening of dump files with parts. This would require
1120 tracking pstate per part of the dump file. */
1121 stream
= dump_open (name
, part
!= -1 || dfi
->pstate
< 0);
1127 *flag_ptr
= dfi
->pflags
;
1129 /* Initialize current flags */
1130 pflags
= dfi
->pflags
;
1134 /* Returns nonzero if dump PHASE is enabled for at least one stream.
1135 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1140 dump_phase_enabled_p (int phase
) const
1142 if (phase
== TDI_tree_all
)
1145 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1146 if (dump_files
[i
].pstate
|| dump_files
[i
].alt_state
)
1148 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1149 if (m_extra_dump_files
[i
].pstate
|| m_extra_dump_files
[i
].alt_state
)
1155 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1156 return dfi
->pstate
|| dfi
->alt_state
;
1160 /* Returns nonzero if tree dump PHASE has been initialized. */
1164 dump_initialized_p (int phase
) const
1166 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1167 return dfi
->pstate
> 0 || dfi
->alt_state
> 0;
1170 /* Returns the switch name of PHASE. */
1173 dump_flag_name (int phase
)
1175 return g
->get_dumps ()->dump_flag_name (phase
);
1180 dump_flag_name (int phase
) const
1182 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
1186 /* Finish a tree dump for PHASE. STREAM is the stream created by
1190 dump_end (int phase ATTRIBUTE_UNUSED
, FILE *stream
)
1192 if (stream
!= stderr
&& stream
!= stdout
)
1196 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
1197 enabled tree dumps. */
1201 dump_enable_all (dump_kind dkind
, dump_flags_t flags
, const char *filename
)
1206 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1208 if ((dump_files
[i
].dkind
== dkind
))
1210 const char *old_filename
= dump_files
[i
].pfilename
;
1211 dump_files
[i
].pstate
= -1;
1212 dump_files
[i
].pflags
|= flags
;
1214 /* Override the existing filename. */
1217 dump_files
[i
].pfilename
= xstrdup (filename
);
1218 /* Since it is a command-line provided file, which is
1219 common to all the phases, use it in append mode. */
1220 dump_files
[i
].pstate
= 1;
1222 if (old_filename
&& filename
!= old_filename
)
1223 free (CONST_CAST (char *, old_filename
));
1227 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1229 if ((m_extra_dump_files
[i
].dkind
== dkind
))
1231 const char *old_filename
= m_extra_dump_files
[i
].pfilename
;
1232 m_extra_dump_files
[i
].pstate
= -1;
1233 m_extra_dump_files
[i
].pflags
|= flags
;
1235 /* Override the existing filename. */
1238 m_extra_dump_files
[i
].pfilename
= xstrdup (filename
);
1239 /* Since it is a command-line provided file, which is
1240 common to all the phases, use it in append mode. */
1241 m_extra_dump_files
[i
].pstate
= 1;
1243 if (old_filename
&& filename
!= old_filename
)
1244 free (CONST_CAST (char *, old_filename
));
1251 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1252 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1257 opt_info_enable_passes (optgroup_flags_t optgroup_flags
, dump_flags_t flags
,
1258 const char *filename
)
1263 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
1265 if ((dump_files
[i
].optgroup_flags
& optgroup_flags
))
1267 const char *old_filename
= dump_files
[i
].alt_filename
;
1268 /* Since this file is shared among different passes, it
1269 should be opened in append mode. */
1270 dump_files
[i
].alt_state
= 1;
1271 dump_files
[i
].alt_flags
|= flags
;
1273 /* Override the existing filename. */
1275 dump_files
[i
].alt_filename
= xstrdup (filename
);
1276 if (old_filename
&& filename
!= old_filename
)
1277 free (CONST_CAST (char *, old_filename
));
1281 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1283 if ((m_extra_dump_files
[i
].optgroup_flags
& optgroup_flags
))
1285 const char *old_filename
= m_extra_dump_files
[i
].alt_filename
;
1286 /* Since this file is shared among different passes, it
1287 should be opened in append mode. */
1288 m_extra_dump_files
[i
].alt_state
= 1;
1289 m_extra_dump_files
[i
].alt_flags
|= flags
;
1291 /* Override the existing filename. */
1293 m_extra_dump_files
[i
].alt_filename
= xstrdup (filename
);
1294 if (old_filename
&& filename
!= old_filename
)
1295 free (CONST_CAST (char *, old_filename
));
1302 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
1303 relevant details in the dump_files array. */
1307 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
, bool doglob
)
1309 const char *option_value
;
1313 if (doglob
&& !dfi
->glob
)
1316 option_value
= skip_leading_substring (arg
, doglob
? dfi
->glob
: dfi
->swtch
);
1320 if (*option_value
&& *option_value
!= '-' && *option_value
!= '=')
1328 const struct kv_pair
<dump_flags_t
> *option_ptr
;
1329 const char *end_ptr
;
1335 end_ptr
= strchr (ptr
, '-');
1336 eq_ptr
= strchr (ptr
, '=');
1338 if (eq_ptr
&& !end_ptr
)
1342 end_ptr
= ptr
+ strlen (ptr
);
1343 length
= end_ptr
- ptr
;
1345 for (option_ptr
= dump_options
; option_ptr
->name
; option_ptr
++)
1346 if (strlen (option_ptr
->name
) == length
1347 && !memcmp (option_ptr
->name
, ptr
, length
))
1349 flags
|= option_ptr
->value
;
1355 /* Interpret rest of the argument as a dump filename. This
1356 filename overrides other command line filenames. */
1358 free (CONST_CAST (char *, dfi
->pfilename
));
1359 dfi
->pfilename
= xstrdup (ptr
+ 1);
1363 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1364 length
, ptr
, dfi
->swtch
);
1370 dfi
->pflags
|= flags
;
1372 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1374 if (dfi
->suffix
== NULL
)
1375 dump_enable_all (dfi
->dkind
, dfi
->pflags
, dfi
->pfilename
);
1382 dump_switch_p (const char *arg
)
1387 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1388 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], false);
1390 /* Don't glob if we got a hit already */
1392 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1393 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], true);
1395 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1396 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], false);
1399 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1400 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], true);
1406 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1407 and filename. Return non-zero if it is a recognized switch. */
1410 opt_info_switch_p_1 (const char *arg
, dump_flags_t
*flags
,
1411 optgroup_flags_t
*optgroup_flags
, char **filename
)
1413 const char *option_value
;
1421 *optgroup_flags
= OPTGROUP_NONE
;
1424 return 1; /* Handle '-fopt-info' without any additional options. */
1428 const char *end_ptr
;
1434 end_ptr
= strchr (ptr
, '-');
1435 eq_ptr
= strchr (ptr
, '=');
1437 if (eq_ptr
&& !end_ptr
)
1441 end_ptr
= ptr
+ strlen (ptr
);
1442 length
= end_ptr
- ptr
;
1444 for (const kv_pair
<dump_flags_t
> *option_ptr
= optinfo_verbosity_options
;
1445 option_ptr
->name
; option_ptr
++)
1446 if (strlen (option_ptr
->name
) == length
1447 && !memcmp (option_ptr
->name
, ptr
, length
))
1449 *flags
|= option_ptr
->value
;
1453 for (const kv_pair
<optgroup_flags_t
> *option_ptr
= optgroup_options
;
1454 option_ptr
->name
; option_ptr
++)
1455 if (strlen (option_ptr
->name
) == length
1456 && !memcmp (option_ptr
->name
, ptr
, length
))
1458 *optgroup_flags
|= option_ptr
->value
;
1464 /* Interpret rest of the argument as a dump filename. This
1465 filename overrides other command line filenames. */
1466 *filename
= xstrdup (ptr
+ 1);
1471 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1482 /* Return non-zero if ARG is a recognized switch for
1483 -fopt-info. Return zero otherwise. */
1486 opt_info_switch_p (const char *arg
)
1489 optgroup_flags_t optgroup_flags
;
1491 static char *file_seen
= NULL
;
1492 gcc::dump_manager
*dumps
= g
->get_dumps ();
1494 if (!opt_info_switch_p_1 (arg
, &flags
, &optgroup_flags
, &filename
))
1498 filename
= xstrdup ("stderr");
1500 /* Bail out if a different filename has been specified. */
1501 if (file_seen
&& strcmp (file_seen
, filename
))
1503 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1508 file_seen
= xstrdup (filename
);
1510 flags
= MSG_OPTIMIZED_LOCATIONS
;
1511 if (!optgroup_flags
)
1512 optgroup_flags
= OPTGROUP_ALL
;
1514 return dumps
->opt_info_enable_passes (optgroup_flags
, flags
, filename
);
1517 /* Print basic block on the dump streams. */
1520 dump_basic_block (dump_flags_t dump_kind
, basic_block bb
, int indent
)
1522 if (dump_file
&& (dump_kind
& pflags
))
1523 dump_bb (dump_file
, bb
, indent
, TDF_DETAILS
);
1524 if (alt_dump_file
&& (dump_kind
& alt_flags
))
1525 dump_bb (alt_dump_file
, bb
, indent
, TDF_DETAILS
);
1528 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1531 dump_function (int phase
, tree fn
)
1536 stream
= dump_begin (phase
, &flags
);
1539 dump_function_to_file (fn
, stream
, flags
);
1540 dump_end (phase
, stream
);
1544 /* Print information from the combine pass on dump_file. */
1547 print_combine_total_stats (void)
1550 dump_combine_total_stats (dump_file
);
1553 /* Enable RTL dump for all the RTL passes. */
1556 enable_rtl_dump_file (void)
1558 gcc::dump_manager
*dumps
= g
->get_dumps ();
1560 dumps
->dump_enable_all (DK_rtl
, dump_flags_t (TDF_DETAILS
) | TDF_BLOCKS
,
1562 return num_enabled
> 0;
1567 /* temp_dump_context's ctor. Temporarily override the dump_context
1568 (to forcibly enable optinfo-generation). */
1570 temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo
)
1572 m_saved (&dump_context ().get ())
1574 dump_context::s_current
= &m_context
;
1575 m_context
.m_forcibly_enable_optinfo
= forcibly_enable_optinfo
;
1578 /* temp_dump_context's dtor. Restore the saved dump_context. */
1580 temp_dump_context::~temp_dump_context ()
1582 dump_context::s_current
= m_saved
;
1585 namespace selftest
{
1587 /* Verify that the dump_location_t constructors capture the source location
1588 at which they were called (provided that the build compiler is sufficiently
1592 test_impl_location ()
1594 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
1597 dump_location_t loc
;
1598 const int expected_line
= __LINE__
- 1;
1599 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1600 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1603 /* Constructing from a gimple. */
1605 dump_location_t
loc ((gimple
*)NULL
);
1606 const int expected_line
= __LINE__
- 1;
1607 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1608 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1611 /* Constructing from an rtx_insn. */
1613 dump_location_t
loc ((rtx_insn
*)NULL
);
1614 const int expected_line
= __LINE__
- 1;
1615 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1616 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1621 /* Verify that ITEM has the expected values. */
1624 verify_item (const location
&loc
,
1625 const optinfo_item
*item
,
1626 enum optinfo_item_kind expected_kind
,
1627 location_t expected_location
,
1628 const char *expected_text
)
1630 ASSERT_EQ_AT (loc
, item
->get_kind (), expected_kind
);
1631 ASSERT_EQ_AT (loc
, item
->get_location (), expected_location
);
1632 ASSERT_STREQ_AT (loc
, item
->get_text (), expected_text
);
1635 /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
1637 #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
1638 SELFTEST_BEGIN_STMT \
1639 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
1640 UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
1643 /* Verify that ITEM is a tree item, with the expected values. */
1645 #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1646 SELFTEST_BEGIN_STMT \
1647 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
1648 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1651 /* Verify that ITEM is a gimple item, with the expected values. */
1653 #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
1654 SELFTEST_BEGIN_STMT \
1655 verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
1656 (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
1659 /* Verify that calls to the dump_* API are captured and consolidated into
1660 optimization records. */
1663 test_capture_of_dump_calls (const line_table_case
&case_
)
1665 /* Generate a location_t for testing. */
1666 line_table_test
ltt (case_
);
1667 linemap_add (line_table
, LC_ENTER
, false, "test.txt", 0);
1668 linemap_line_start (line_table
, 5, 100);
1669 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1670 location_t where
= linemap_position_for_column (line_table
, 10);
1672 dump_location_t loc
= dump_location_t::from_location_t (where
);
1674 /* Test of dump_printf. */
1676 temp_dump_context
tmp (true);
1677 dump_printf (MSG_NOTE
, "int: %i str: %s", 42, "foo");
1679 optinfo
*info
= tmp
.get_pending_optinfo ();
1680 ASSERT_TRUE (info
!= NULL
);
1681 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
1682 ASSERT_EQ (info
->num_items (), 1);
1683 ASSERT_IS_TEXT (info
->get_item (0), "int: 42 str: foo");
1686 /* Tree, via dump_generic_expr. */
1688 temp_dump_context
tmp (true);
1689 dump_printf_loc (MSG_NOTE
, loc
, "test of tree: ");
1690 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, integer_zero_node
);
1692 optinfo
*info
= tmp
.get_pending_optinfo ();
1693 ASSERT_TRUE (info
!= NULL
);
1694 ASSERT_EQ (info
->get_location_t (), where
);
1695 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
1696 ASSERT_EQ (info
->num_items (), 2);
1697 ASSERT_IS_TEXT (info
->get_item (0), "test of tree: ");
1698 ASSERT_IS_TREE (info
->get_item (1), UNKNOWN_LOCATION
, "0");
1701 /* Tree, via dump_generic_expr_loc. */
1703 temp_dump_context
tmp (true);
1704 dump_generic_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, integer_one_node
);
1706 optinfo
*info
= tmp
.get_pending_optinfo ();
1707 ASSERT_TRUE (info
!= NULL
);
1708 ASSERT_EQ (info
->get_location_t (), where
);
1709 ASSERT_EQ (info
->get_kind (), OPTINFO_KIND_NOTE
);
1710 ASSERT_EQ (info
->num_items (), 1);
1711 ASSERT_IS_TREE (info
->get_item (0), UNKNOWN_LOCATION
, "1");
1716 greturn
*stmt
= gimple_build_return (NULL
);
1717 gimple_set_location (stmt
, where
);
1719 /* dump_gimple_stmt_loc. */
1721 temp_dump_context
tmp (true);
1722 dump_gimple_stmt_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
1724 optinfo
*info
= tmp
.get_pending_optinfo ();
1725 ASSERT_TRUE (info
!= NULL
);
1726 ASSERT_EQ (info
->num_items (), 1);
1727 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;\n");
1730 /* dump_gimple_stmt. */
1732 temp_dump_context
tmp (true);
1733 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
1735 optinfo
*info
= tmp
.get_pending_optinfo ();
1736 ASSERT_TRUE (info
!= NULL
);
1737 ASSERT_EQ (info
->num_items (), 1);
1738 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;\n");
1741 /* dump_gimple_expr_loc. */
1743 temp_dump_context
tmp (true);
1744 dump_gimple_expr_loc (MSG_NOTE
, loc
, TDF_SLIM
, stmt
, 2);
1746 optinfo
*info
= tmp
.get_pending_optinfo ();
1747 ASSERT_TRUE (info
!= NULL
);
1748 ASSERT_EQ (info
->num_items (), 1);
1749 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;");
1752 /* dump_gimple_expr. */
1754 temp_dump_context
tmp (true);
1755 dump_gimple_expr (MSG_NOTE
, TDF_SLIM
, stmt
, 2);
1757 optinfo
*info
= tmp
.get_pending_optinfo ();
1758 ASSERT_TRUE (info
!= NULL
);
1759 ASSERT_EQ (info
->num_items (), 1);
1760 ASSERT_IS_GIMPLE (info
->get_item (0), where
, "return;");
1766 temp_dump_context
tmp (true);
1767 dump_dec (MSG_NOTE
, poly_int64 (42));
1769 optinfo
*info
= tmp
.get_pending_optinfo ();
1770 ASSERT_TRUE (info
!= NULL
);
1771 ASSERT_EQ (info
->num_items (), 1);
1772 ASSERT_IS_TEXT (info
->get_item (0), "42");
1775 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
1778 /* MSG_OPTIMIZED_LOCATIONS. */
1780 temp_dump_context
tmp (true);
1781 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
, "test");
1782 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
1783 OPTINFO_KIND_SUCCESS
);
1786 /* MSG_MISSED_OPTIMIZATION. */
1788 temp_dump_context
tmp (true);
1789 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, loc
, "test");
1790 ASSERT_EQ (tmp
.get_pending_optinfo ()->get_kind (),
1791 OPTINFO_KIND_FAILURE
);
1796 /* Run all of the selftests within this file. */
1801 test_impl_location ();
1802 for_each_line_table_case (test_capture_of_dump_calls
);
1805 } // namespace selftest
1807 #endif /* CHECKING_P */