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 /* If non-NULL, return one past-the-end of the matching SUBPART of
39 #define skip_leading_substring(whole, part) \
40 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
42 static dump_flags_t pflags
; /* current dump_flags */
44 static void dump_loc (dump_flags_t
, FILE *, source_location
);
46 /* Current -fopt-info output stream, if any, and flags. */
47 static FILE *alt_dump_file
= NULL
;
48 static dump_flags_t alt_flags
;
50 static FILE *dump_open_alternate_stream (struct dump_file_info
*);
52 /* These are currently used for communicating between passes.
53 However, instead of accessing them directly, the passes can use
54 dump_printf () for dumps. */
55 FILE *dump_file
= NULL
;
56 const char *dump_file_name
;
57 dump_flags_t dump_flags
;
58 bool dumps_are_enabled
= false;
61 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
62 or alt_dump_file change. */
65 refresh_dumps_are_enabled ()
67 dumps_are_enabled
= (dump_file
|| alt_dump_file
);
70 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
74 set_dump_file (FILE *new_dump_file
)
76 dump_file
= new_dump_file
;
77 refresh_dumps_are_enabled ();
80 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
84 set_alt_dump_file (FILE *new_alt_dump_file
)
86 alt_dump_file
= new_alt_dump_file
;
87 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 #define FIRST_AUTO_NUMBERED_DUMP 1
107 #define FIRST_ME_AUTO_NUMBERED_DUMP 4
109 DUMP_FILE_INFO (NULL
, "lang-all", DK_lang
, 0),
110 DUMP_FILE_INFO (NULL
, "tree-all", DK_tree
, 0),
111 DUMP_FILE_INFO (NULL
, "rtl-all", DK_rtl
, 0),
112 DUMP_FILE_INFO (NULL
, "ipa-all", DK_ipa
, 0),
115 /* Define a name->number mapping for a dump flag value. */
116 template <typename ValueType
>
119 const char *const name
; /* the name of the value */
120 const ValueType value
; /* the value of the name */
123 /* Table of dump options. This must be consistent with the TDF_* flags
124 in dumpfile.h and opt_info_options below. */
125 static const kv_pair
<dump_flags_t
> dump_options
[] =
127 {"address", TDF_ADDRESS
},
128 {"asmname", TDF_ASMNAME
},
131 {"graph", TDF_GRAPH
},
132 {"details", (TDF_DETAILS
| MSG_OPTIMIZED_LOCATIONS
133 | MSG_MISSED_OPTIMIZATION
135 {"cselib", TDF_CSELIB
},
136 {"stats", TDF_STATS
},
137 {"blocks", TDF_BLOCKS
},
139 {"lineno", TDF_LINENO
},
141 {"stmtaddr", TDF_STMTADDR
},
142 {"memsyms", TDF_MEMSYMS
},
144 {"alias", TDF_ALIAS
},
145 {"nouid", TDF_NOUID
},
146 {"enumerate_locals", TDF_ENUMERATE_LOCALS
},
148 {"gimple", TDF_GIMPLE
},
149 {"folding", TDF_FOLDING
},
150 {"optimized", MSG_OPTIMIZED_LOCATIONS
},
151 {"missed", MSG_MISSED_OPTIMIZATION
},
154 {"all", dump_flags_t (~(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 static 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
) 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
);
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
) 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)
353 return concat (dump_base_name
, dump_id
, dfi
->suffix
, NULL
);
356 /* Open a dump file called FILENAME. Some filenames are special and
357 refer to the standard streams. TRUNC indicates whether this is the
358 first open (so the file should be truncated, rather than appended).
359 An error message is emitted in the event of failure. */
362 dump_open (const char *filename
, bool trunc
)
364 if (strcmp ("stderr", filename
) == 0)
367 if (strcmp ("stdout", filename
) == 0
368 || strcmp ("-", filename
) == 0)
371 FILE *stream
= fopen (filename
, trunc
? "w" : "a");
374 error ("could not open dump file %qs: %m", filename
);
378 /* For a given DFI, open an alternate dump filename (which could also
379 be a standard stream such as stdout/stderr). If the alternate dump
380 file cannot be opened, return NULL. */
383 dump_open_alternate_stream (struct dump_file_info
*dfi
)
385 if (!dfi
->alt_filename
)
389 return dfi
->alt_stream
;
391 FILE *stream
= dump_open (dfi
->alt_filename
, dfi
->alt_state
< 0);
399 /* Construct a dump_user_location_t from STMT (using its location and
402 dump_user_location_t::dump_user_location_t (gimple
*stmt
)
403 : m_count (), m_loc (UNKNOWN_LOCATION
)
408 m_count
= stmt
->bb
->count
;
409 m_loc
= gimple_location (stmt
);
413 /* Construct a dump_user_location_t from an RTL instruction (using its
414 location and hotness). */
416 dump_user_location_t::dump_user_location_t (rtx_insn
*insn
)
417 : m_count (), m_loc (UNKNOWN_LOCATION
)
421 basic_block bb
= BLOCK_FOR_INSN (insn
);
424 m_loc
= INSN_LOCATION (insn
);
428 /* Construct from a function declaration. This one requires spelling out
429 to avoid accidentally constructing from other kinds of tree. */
432 dump_user_location_t::from_function_decl (tree fndecl
)
436 // FIXME: profile count for function?
437 return dump_user_location_t (profile_count (),
438 DECL_SOURCE_LOCATION (fndecl
));
441 /* Print source location on DFILE if enabled. */
444 dump_loc (dump_flags_t dump_kind
, FILE *dfile
, source_location loc
)
448 if (LOCATION_LOCUS (loc
) > BUILTINS_LOCATION
)
449 fprintf (dfile
, "%s:%d:%d: note: ", LOCATION_FILE (loc
),
450 LOCATION_LINE (loc
), LOCATION_COLUMN (loc
));
451 else if (current_function_decl
)
452 fprintf (dfile
, "%s:%d:%d: note: ",
453 DECL_SOURCE_FILE (current_function_decl
),
454 DECL_SOURCE_LINE (current_function_decl
),
455 DECL_SOURCE_COLUMN (current_function_decl
));
456 /* Indentation based on scope depth. */
457 fprintf (dfile
, "%*s", get_dump_scope_depth (), "");
461 /* Dump gimple statement GS with SPC indentation spaces and
462 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
465 dump_gimple_stmt (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
468 if (dump_file
&& (dump_kind
& pflags
))
469 print_gimple_stmt (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
471 if (alt_dump_file
&& (dump_kind
& alt_flags
))
472 print_gimple_stmt (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
475 /* Similar to dump_gimple_stmt, except additionally print source location. */
478 dump_gimple_stmt_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
479 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
481 location_t srcloc
= loc
.get_location_t ();
482 if (dump_file
&& (dump_kind
& pflags
))
484 dump_loc (dump_kind
, dump_file
, srcloc
);
485 print_gimple_stmt (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
488 if (alt_dump_file
&& (dump_kind
& alt_flags
))
490 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
491 print_gimple_stmt (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
495 /* Dump gimple statement GS with SPC indentation spaces and
496 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
497 Do not terminate with a newline or semicolon. */
500 dump_gimple_expr (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
503 if (dump_file
&& (dump_kind
& pflags
))
504 print_gimple_expr (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
506 if (alt_dump_file
&& (dump_kind
& alt_flags
))
507 print_gimple_expr (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
510 /* Similar to dump_gimple_expr, except additionally print source location. */
513 dump_gimple_expr_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
514 dump_flags_t extra_dump_flags
, gimple
*gs
, int spc
)
516 location_t srcloc
= loc
.get_location_t ();
517 if (dump_file
&& (dump_kind
& pflags
))
519 dump_loc (dump_kind
, dump_file
, srcloc
);
520 print_gimple_expr (dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
523 if (alt_dump_file
&& (dump_kind
& alt_flags
))
525 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
526 print_gimple_expr (alt_dump_file
, gs
, spc
, dump_flags
| extra_dump_flags
);
531 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
532 DUMP_KIND is enabled. */
535 dump_generic_expr (dump_flags_t dump_kind
, dump_flags_t extra_dump_flags
,
538 if (dump_file
&& (dump_kind
& pflags
))
539 print_generic_expr (dump_file
, t
, dump_flags
| extra_dump_flags
);
541 if (alt_dump_file
&& (dump_kind
& alt_flags
))
542 print_generic_expr (alt_dump_file
, t
, dump_flags
| extra_dump_flags
);
546 /* Similar to dump_generic_expr, except additionally print the source
550 dump_generic_expr_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
551 dump_flags_t extra_dump_flags
, tree t
)
553 location_t srcloc
= loc
.get_location_t ();
554 if (dump_file
&& (dump_kind
& pflags
))
556 dump_loc (dump_kind
, dump_file
, srcloc
);
557 print_generic_expr (dump_file
, t
, dump_flags
| extra_dump_flags
);
560 if (alt_dump_file
&& (dump_kind
& alt_flags
))
562 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
563 print_generic_expr (alt_dump_file
, t
, dump_flags
| extra_dump_flags
);
567 /* Output a formatted message using FORMAT on appropriate dump streams. */
570 dump_printf (dump_flags_t dump_kind
, const char *format
, ...)
572 if (dump_file
&& (dump_kind
& pflags
))
575 va_start (ap
, format
);
576 vfprintf (dump_file
, format
, ap
);
580 if (alt_dump_file
&& (dump_kind
& alt_flags
))
583 va_start (ap
, format
);
584 vfprintf (alt_dump_file
, format
, ap
);
589 /* Similar to dump_printf, except source location is also printed. */
592 dump_printf_loc (dump_flags_t dump_kind
, const dump_location_t
&loc
,
593 const char *format
, ...)
595 location_t srcloc
= loc
.get_location_t ();
596 if (dump_file
&& (dump_kind
& pflags
))
599 dump_loc (dump_kind
, dump_file
, srcloc
);
600 va_start (ap
, format
);
601 vfprintf (dump_file
, format
, ap
);
605 if (alt_dump_file
&& (dump_kind
& alt_flags
))
608 dump_loc (dump_kind
, alt_dump_file
, srcloc
);
609 va_start (ap
, format
);
610 vfprintf (alt_dump_file
, format
, ap
);
615 /* Output VALUE in decimal to appropriate dump streams. */
617 template<unsigned int N
, typename C
>
619 dump_dec (dump_flags_t dump_kind
, const poly_int
<N
, C
> &value
)
621 STATIC_ASSERT (poly_coeff_traits
<C
>::signedness
>= 0);
622 signop sgn
= poly_coeff_traits
<C
>::signedness
? SIGNED
: UNSIGNED
;
623 if (dump_file
&& (dump_kind
& pflags
))
624 print_dec (value
, dump_file
, sgn
);
626 if (alt_dump_file
&& (dump_kind
& alt_flags
))
627 print_dec (value
, alt_dump_file
, sgn
);
630 template void dump_dec (dump_flags_t
, const poly_uint16
&);
631 template void dump_dec (dump_flags_t
, const poly_int64
&);
632 template void dump_dec (dump_flags_t
, const poly_uint64
&);
633 template void dump_dec (dump_flags_t
, const poly_offset_int
&);
634 template void dump_dec (dump_flags_t
, const poly_widest_int
&);
637 dump_dec (dump_flags_t dump_kind
, const poly_wide_int
&value
, signop sgn
)
639 if (dump_file
&& (dump_kind
& pflags
))
640 print_dec (value
, dump_file
, sgn
);
642 if (alt_dump_file
&& (dump_kind
& alt_flags
))
643 print_dec (value
, alt_dump_file
, sgn
);
646 /* Output VALUE in hexadecimal to appropriate dump streams. */
649 dump_hex (dump_flags_t dump_kind
, const poly_wide_int
&value
)
651 if (dump_file
&& (dump_kind
& pflags
))
652 print_hex (value
, dump_file
);
654 if (alt_dump_file
&& (dump_kind
& alt_flags
))
655 print_hex (value
, alt_dump_file
);
658 /* The current dump scope-nesting depth. */
660 static int dump_scope_depth
;
662 /* Get the current dump scope-nesting depth.
663 For use by dump_*_loc (for showing nesting via indentation). */
666 get_dump_scope_depth ()
668 return dump_scope_depth
;
671 /* Push a nested dump scope.
672 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
674 Increment the scope depth. */
677 dump_begin_scope (const char *name
, const dump_location_t
&loc
)
679 dump_printf_loc (MSG_NOTE
, loc
, "=== %s ===\n", name
);
683 /* Pop a nested dump scope. */
691 /* Start a dump for PHASE. Store user-supplied dump flags in
692 *FLAG_PTR. Return the number of streams opened. Set globals
693 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
694 set dump_flags appropriately for both pass dump stream and
695 -fopt-info stream. */
699 dump_start (int phase
, dump_flags_t
*flag_ptr
)
703 struct dump_file_info
*dfi
;
705 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
708 dfi
= get_dump_file_info (phase
);
709 name
= get_dump_file_name (phase
);
712 stream
= dump_open (name
, dfi
->pstate
< 0);
719 dfi
->pstream
= stream
;
720 set_dump_file (dfi
->pstream
);
721 /* Initialize current dump flags. */
722 pflags
= dfi
->pflags
;
725 stream
= dump_open_alternate_stream (dfi
);
728 dfi
->alt_stream
= stream
;
730 set_alt_dump_file (dfi
->alt_stream
);
731 /* Initialize current -fopt-info flags. */
732 alt_flags
= dfi
->alt_flags
;
736 *flag_ptr
= dfi
->pflags
;
741 /* Finish a tree dump for PHASE and close associated dump streams. Also
742 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
746 dump_finish (int phase
)
748 struct dump_file_info
*dfi
;
752 dfi
= get_dump_file_info (phase
);
753 if (dfi
->pstream
&& dfi
->pstream
!= stdout
&& dfi
->pstream
!= stderr
)
754 fclose (dfi
->pstream
);
756 if (dfi
->alt_stream
&& dfi
->alt_stream
!= stdout
&& dfi
->alt_stream
!= stderr
)
757 fclose (dfi
->alt_stream
);
759 dfi
->alt_stream
= NULL
;
761 set_dump_file (NULL
);
762 set_alt_dump_file (NULL
);
763 dump_flags
= TDF_NONE
;
764 alt_flags
= TDF_NONE
;
768 /* Begin a tree dump for PHASE. Stores any user supplied flag in
769 *FLAG_PTR and returns a stream to write to. If the dump is not
770 enabled, returns NULL.
771 Multiple calls will reopen and append to the dump file. */
774 dump_begin (int phase
, dump_flags_t
*flag_ptr
)
776 return g
->get_dumps ()->dump_begin (phase
, flag_ptr
);
781 dump_begin (int phase
, dump_flags_t
*flag_ptr
)
784 struct dump_file_info
*dfi
;
787 if (phase
== TDI_none
|| !dump_phase_enabled_p (phase
))
790 name
= get_dump_file_name (phase
);
793 dfi
= get_dump_file_info (phase
);
795 stream
= dump_open (name
, dfi
->pstate
< 0);
801 *flag_ptr
= dfi
->pflags
;
803 /* Initialize current flags */
804 pflags
= dfi
->pflags
;
808 /* Returns nonzero if dump PHASE is enabled for at least one stream.
809 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
814 dump_phase_enabled_p (int phase
) const
816 if (phase
== TDI_tree_all
)
819 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
820 if (dump_files
[i
].pstate
|| dump_files
[i
].alt_state
)
822 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
823 if (m_extra_dump_files
[i
].pstate
|| m_extra_dump_files
[i
].alt_state
)
829 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
830 return dfi
->pstate
|| dfi
->alt_state
;
834 /* Returns nonzero if tree dump PHASE has been initialized. */
838 dump_initialized_p (int phase
) const
840 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
841 return dfi
->pstate
> 0 || dfi
->alt_state
> 0;
844 /* Returns the switch name of PHASE. */
847 dump_flag_name (int phase
)
849 return g
->get_dumps ()->dump_flag_name (phase
);
854 dump_flag_name (int phase
) const
856 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
860 /* Finish a tree dump for PHASE. STREAM is the stream created by
864 dump_end (int phase ATTRIBUTE_UNUSED
, FILE *stream
)
866 if (stream
!= stderr
&& stream
!= stdout
)
870 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
871 enabled tree dumps. */
875 dump_enable_all (dump_kind dkind
, dump_flags_t flags
, const char *filename
)
880 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
882 if ((dump_files
[i
].dkind
== dkind
))
884 const char *old_filename
= dump_files
[i
].pfilename
;
885 dump_files
[i
].pstate
= -1;
886 dump_files
[i
].pflags
|= flags
;
888 /* Override the existing filename. */
891 dump_files
[i
].pfilename
= xstrdup (filename
);
892 /* Since it is a command-line provided file, which is
893 common to all the phases, use it in append mode. */
894 dump_files
[i
].pstate
= 1;
896 if (old_filename
&& filename
!= old_filename
)
897 free (CONST_CAST (char *, old_filename
));
901 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
903 if ((m_extra_dump_files
[i
].dkind
== dkind
))
905 const char *old_filename
= m_extra_dump_files
[i
].pfilename
;
906 m_extra_dump_files
[i
].pstate
= -1;
907 m_extra_dump_files
[i
].pflags
|= flags
;
909 /* Override the existing filename. */
912 m_extra_dump_files
[i
].pfilename
= xstrdup (filename
);
913 /* Since it is a command-line provided file, which is
914 common to all the phases, use it in append mode. */
915 m_extra_dump_files
[i
].pstate
= 1;
917 if (old_filename
&& filename
!= old_filename
)
918 free (CONST_CAST (char *, old_filename
));
925 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
926 Enable dumps with FLAGS on FILENAME. Return the number of enabled
931 opt_info_enable_passes (optgroup_flags_t optgroup_flags
, dump_flags_t flags
,
932 const char *filename
)
937 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
939 if ((dump_files
[i
].optgroup_flags
& optgroup_flags
))
941 const char *old_filename
= dump_files
[i
].alt_filename
;
942 /* Since this file is shared among different passes, it
943 should be opened in append mode. */
944 dump_files
[i
].alt_state
= 1;
945 dump_files
[i
].alt_flags
|= flags
;
947 /* Override the existing filename. */
949 dump_files
[i
].alt_filename
= xstrdup (filename
);
950 if (old_filename
&& filename
!= old_filename
)
951 free (CONST_CAST (char *, old_filename
));
955 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
957 if ((m_extra_dump_files
[i
].optgroup_flags
& optgroup_flags
))
959 const char *old_filename
= m_extra_dump_files
[i
].alt_filename
;
960 /* Since this file is shared among different passes, it
961 should be opened in append mode. */
962 m_extra_dump_files
[i
].alt_state
= 1;
963 m_extra_dump_files
[i
].alt_flags
|= flags
;
965 /* Override the existing filename. */
967 m_extra_dump_files
[i
].alt_filename
= xstrdup (filename
);
968 if (old_filename
&& filename
!= old_filename
)
969 free (CONST_CAST (char *, old_filename
));
976 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
977 relevant details in the dump_files array. */
981 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
, bool doglob
)
983 const char *option_value
;
987 if (doglob
&& !dfi
->glob
)
990 option_value
= skip_leading_substring (arg
, doglob
? dfi
->glob
: dfi
->swtch
);
994 if (*option_value
&& *option_value
!= '-' && *option_value
!= '=')
1002 const struct kv_pair
<dump_flags_t
> *option_ptr
;
1003 const char *end_ptr
;
1009 end_ptr
= strchr (ptr
, '-');
1010 eq_ptr
= strchr (ptr
, '=');
1012 if (eq_ptr
&& !end_ptr
)
1016 end_ptr
= ptr
+ strlen (ptr
);
1017 length
= end_ptr
- ptr
;
1019 for (option_ptr
= dump_options
; option_ptr
->name
; option_ptr
++)
1020 if (strlen (option_ptr
->name
) == length
1021 && !memcmp (option_ptr
->name
, ptr
, length
))
1023 flags
|= option_ptr
->value
;
1029 /* Interpret rest of the argument as a dump filename. This
1030 filename overrides other command line filenames. */
1032 free (CONST_CAST (char *, dfi
->pfilename
));
1033 dfi
->pfilename
= xstrdup (ptr
+ 1);
1037 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1038 length
, ptr
, dfi
->swtch
);
1044 dfi
->pflags
|= flags
;
1046 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1048 if (dfi
->suffix
== NULL
)
1049 dump_enable_all (dfi
->dkind
, dfi
->pflags
, dfi
->pfilename
);
1056 dump_switch_p (const char *arg
)
1061 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1062 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], false);
1064 /* Don't glob if we got a hit already */
1066 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
1067 any
|= dump_switch_p_1 (arg
, &dump_files
[i
], true);
1069 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1070 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], false);
1073 for (i
= 0; i
< m_extra_dump_files_in_use
; i
++)
1074 any
|= dump_switch_p_1 (arg
, &m_extra_dump_files
[i
], true);
1080 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1081 and filename. Return non-zero if it is a recognized switch. */
1084 opt_info_switch_p_1 (const char *arg
, dump_flags_t
*flags
,
1085 optgroup_flags_t
*optgroup_flags
, char **filename
)
1087 const char *option_value
;
1095 *optgroup_flags
= OPTGROUP_NONE
;
1098 return 1; /* Handle '-fopt-info' without any additional options. */
1102 const char *end_ptr
;
1108 end_ptr
= strchr (ptr
, '-');
1109 eq_ptr
= strchr (ptr
, '=');
1111 if (eq_ptr
&& !end_ptr
)
1115 end_ptr
= ptr
+ strlen (ptr
);
1116 length
= end_ptr
- ptr
;
1118 for (const kv_pair
<dump_flags_t
> *option_ptr
= optinfo_verbosity_options
;
1119 option_ptr
->name
; option_ptr
++)
1120 if (strlen (option_ptr
->name
) == length
1121 && !memcmp (option_ptr
->name
, ptr
, length
))
1123 *flags
|= option_ptr
->value
;
1127 for (const kv_pair
<optgroup_flags_t
> *option_ptr
= optgroup_options
;
1128 option_ptr
->name
; option_ptr
++)
1129 if (strlen (option_ptr
->name
) == length
1130 && !memcmp (option_ptr
->name
, ptr
, length
))
1132 *optgroup_flags
|= option_ptr
->value
;
1138 /* Interpret rest of the argument as a dump filename. This
1139 filename overrides other command line filenames. */
1140 *filename
= xstrdup (ptr
+ 1);
1145 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1156 /* Return non-zero if ARG is a recognized switch for
1157 -fopt-info. Return zero otherwise. */
1160 opt_info_switch_p (const char *arg
)
1163 optgroup_flags_t optgroup_flags
;
1165 static char *file_seen
= NULL
;
1166 gcc::dump_manager
*dumps
= g
->get_dumps ();
1168 if (!opt_info_switch_p_1 (arg
, &flags
, &optgroup_flags
, &filename
))
1172 filename
= xstrdup ("stderr");
1174 /* Bail out if a different filename has been specified. */
1175 if (file_seen
&& strcmp (file_seen
, filename
))
1177 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1182 file_seen
= xstrdup (filename
);
1184 flags
= MSG_OPTIMIZED_LOCATIONS
;
1185 if (!optgroup_flags
)
1186 optgroup_flags
= OPTGROUP_ALL
;
1188 return dumps
->opt_info_enable_passes (optgroup_flags
, flags
, filename
);
1191 /* Print basic block on the dump streams. */
1194 dump_basic_block (dump_flags_t dump_kind
, basic_block bb
, int indent
)
1196 if (dump_file
&& (dump_kind
& pflags
))
1197 dump_bb (dump_file
, bb
, indent
, TDF_DETAILS
);
1198 if (alt_dump_file
&& (dump_kind
& alt_flags
))
1199 dump_bb (alt_dump_file
, bb
, indent
, TDF_DETAILS
);
1202 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1205 dump_function (int phase
, tree fn
)
1210 stream
= dump_begin (phase
, &flags
);
1213 dump_function_to_file (fn
, stream
, flags
);
1214 dump_end (phase
, stream
);
1218 /* Print information from the combine pass on dump_file. */
1221 print_combine_total_stats (void)
1224 dump_combine_total_stats (dump_file
);
1227 /* Enable RTL dump for all the RTL passes. */
1230 enable_rtl_dump_file (void)
1232 gcc::dump_manager
*dumps
= g
->get_dumps ();
1234 dumps
->dump_enable_all (DK_rtl
, dump_flags_t (TDF_DETAILS
) | TDF_BLOCKS
,
1236 return num_enabled
> 0;
1241 namespace selftest
{
1243 /* Verify that the dump_location_t constructors capture the source location
1244 at which they were called (provided that the build compiler is sufficiently
1248 test_impl_location ()
1250 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
1253 dump_location_t loc
;
1254 const int expected_line
= __LINE__
- 1;
1255 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1256 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1259 /* Constructing from a gimple. */
1261 dump_location_t
loc ((gimple
*)NULL
);
1262 const int expected_line
= __LINE__
- 1;
1263 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1264 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1267 /* Constructing from an rtx_insn. */
1269 dump_location_t
loc ((rtx_insn
*)NULL
);
1270 const int expected_line
= __LINE__
- 1;
1271 ASSERT_STR_CONTAINS (loc
.get_impl_location ().m_file
, "dumpfile.c");
1272 ASSERT_EQ (loc
.get_impl_location ().m_line
, expected_line
);
1277 /* Run all of the selftests within this file. */
1282 test_impl_location ();
1285 } // namespace selftest
1287 #endif /* CHECKING_P */