[Ada] Argument_String_To_List creates empty items from whitespace
[gcc.git] / gcc / dumpfile.c
1 /* Dump infrastructure for optimizations and intermediate representation.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
9 version.
10
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
14 for more details.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "options.h"
24 #include "tree.h"
25 #include "gimple-pretty-print.h"
26 #include "diagnostic-core.h"
27 #include "dumpfile.h"
28 #include "context.h"
29 #include "profile-count.h"
30 #include "tree-cfg.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. */
35 #include "selftest.h"
36
37 /* If non-NULL, return one past-the-end of the matching SUBPART of
38 the WHOLE string. */
39 #define skip_leading_substring(whole, part) \
40 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
41
42 static dump_flags_t pflags; /* current dump_flags */
43
44 static void dump_loc (dump_flags_t, FILE *, source_location);
45
46 /* Current -fopt-info output stream, if any, and flags. */
47 static FILE *alt_dump_file = NULL;
48 static dump_flags_t alt_flags;
49
50 static FILE *dump_open_alternate_stream (struct dump_file_info *);
51
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;
59
60
61 /* Update the "dumps_are_enabled" global; to be called whenever dump_file
62 or alt_dump_file change. */
63
64 static void
65 refresh_dumps_are_enabled ()
66 {
67 dumps_are_enabled = (dump_file || alt_dump_file);
68 }
69
70 /* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
71 global. */
72
73 void
74 set_dump_file (FILE *new_dump_file)
75 {
76 dump_file = new_dump_file;
77 refresh_dumps_are_enabled ();
78 }
79
80 /* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
81 global. */
82
83 static void
84 set_alt_dump_file (FILE *new_alt_dump_file)
85 {
86 alt_dump_file = new_alt_dump_file;
87 refresh_dumps_are_enabled ();
88 }
89
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}
93
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] =
97 {
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
108
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),
113 };
114
115 /* Define a name->number mapping for a dump flag value. */
116 template <typename ValueType>
117 struct kv_pair
118 {
119 const char *const name; /* the name of the value */
120 const ValueType value; /* the value of the name */
121 };
122
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[] =
126 {
127 {"address", TDF_ADDRESS},
128 {"asmname", TDF_ASMNAME},
129 {"slim", TDF_SLIM},
130 {"raw", TDF_RAW},
131 {"graph", TDF_GRAPH},
132 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
133 | MSG_MISSED_OPTIMIZATION
134 | MSG_NOTE)},
135 {"cselib", TDF_CSELIB},
136 {"stats", TDF_STATS},
137 {"blocks", TDF_BLOCKS},
138 {"vops", TDF_VOPS},
139 {"lineno", TDF_LINENO},
140 {"uid", TDF_UID},
141 {"stmtaddr", TDF_STMTADDR},
142 {"memsyms", TDF_MEMSYMS},
143 {"eh", TDF_EH},
144 {"alias", TDF_ALIAS},
145 {"nouid", TDF_NOUID},
146 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
147 {"scev", TDF_SCEV},
148 {"gimple", TDF_GIMPLE},
149 {"folding", TDF_FOLDING},
150 {"optimized", MSG_OPTIMIZED_LOCATIONS},
151 {"missed", MSG_MISSED_OPTIMIZATION},
152 {"note", MSG_NOTE},
153 {"optall", MSG_ALL},
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))},
157 {NULL, TDF_NONE}
158 };
159
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.
162 */
163 static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
164 {
165 {"optimized", MSG_OPTIMIZED_LOCATIONS},
166 {"missed", MSG_MISSED_OPTIMIZATION},
167 {"note", MSG_NOTE},
168 {"all", MSG_ALL},
169 {NULL, TDF_NONE}
170 };
171
172 /* Flags used for -fopt-info groups. */
173 static const kv_pair<optgroup_flags_t> optgroup_options[] =
174 {
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}
182 };
183
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)
189 {
190 }
191
192 gcc::dump_manager::~dump_manager ()
193 {
194 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
195 {
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)
201 {
202 XDELETEVEC (const_cast <char *> (dfi->suffix));
203 XDELETEVEC (const_cast <char *> (dfi->swtch));
204 XDELETEVEC (const_cast <char *> (dfi->glob));
205 }
206 /* These, if non-NULL, are always dynamically allocated. */
207 XDELETEVEC (const_cast <char *> (dfi->pfilename));
208 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
209 }
210 XDELETEVEC (m_extra_dump_files);
211 }
212
213 unsigned int
214 gcc::dump_manager::
215 dump_register (const char *suffix, const char *swtch, const char *glob,
216 dump_kind dkind, optgroup_flags_t optgroup_flags,
217 bool take_ownership)
218 {
219 int num = m_next_dump++;
220
221 size_t count = m_extra_dump_files_in_use++;
222
223 if (count >= m_extra_dump_files_alloced)
224 {
225 if (m_extra_dump_files_alloced == 0)
226 m_extra_dump_files_alloced = 512;
227 else
228 m_extra_dump_files_alloced *= 2;
229 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
230 m_extra_dump_files,
231 m_extra_dump_files_alloced);
232
233 /* Construct a new object in the space allocated above. */
234 new (m_extra_dump_files + count) dump_file_info ();
235 }
236 else
237 {
238 /* Zero out the already constructed object. */
239 m_extra_dump_files[count] = dump_file_info ();
240 }
241
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;
249
250 return count + TDI_end;
251 }
252
253
254 /* Allow languages and middle-end to register their dumps before the
255 optimization passes. */
256
257 void
258 gcc::dump_manager::
259 register_dumps ()
260 {
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++;
270 }
271
272
273 /* Return the dump_file_info for the given phase. */
274
275 struct dump_file_info *
276 gcc::dump_manager::
277 get_dump_file_info (int phase) const
278 {
279 if (phase < TDI_end)
280 return &dump_files[phase];
281 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
282 return NULL;
283 else
284 return m_extra_dump_files + (phase - TDI_end);
285 }
286
287 /* Locate the dump_file_info with swtch equal to SWTCH,
288 or return NULL if no such dump_file_info exists. */
289
290 struct dump_file_info *
291 gcc::dump_manager::
292 get_dump_file_info_by_switch (const char *swtch) const
293 {
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];
297
298 /* Not found. */
299 return NULL;
300 }
301
302
303 /* Return the name of the dump file for the given phase.
304 The caller is responsible for calling free on the returned
305 buffer.
306 If the dump is not enabled, returns NULL. */
307
308 char *
309 gcc::dump_manager::
310 get_dump_file_name (int phase, int part) const
311 {
312 struct dump_file_info *dfi;
313
314 if (phase == TDI_none)
315 return NULL;
316
317 dfi = get_dump_file_info (phase);
318
319 return get_dump_file_name (dfi, part);
320 }
321
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
324 buffer.
325 If the dump is not enabled, returns NULL. */
326
327 char *
328 gcc::dump_manager::
329 get_dump_file_name (struct dump_file_info *dfi, int part) const
330 {
331 char dump_id[10];
332
333 gcc_assert (dfi);
334
335 if (dfi->pstate == 0)
336 return NULL;
337
338 /* If available, use the command line dump filename. */
339 if (dfi->pfilename)
340 return xstrdup (dfi->pfilename);
341
342 if (dfi->num < 0)
343 dump_id[0] = '\0';
344 else
345 {
346 /* (null), LANG, TREE, RTL, IPA. */
347 char suffix = " ltri"[dfi->dkind];
348
349 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
350 dump_id[0] = '\0';
351 }
352
353 if (part != -1)
354 {
355 char part_id[8];
356 snprintf (part_id, sizeof (part_id), ".%i", part);
357 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
358 }
359 else
360 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
361 }
362
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. */
367
368 static FILE *
369 dump_open (const char *filename, bool trunc)
370 {
371 if (strcmp ("stderr", filename) == 0)
372 return stderr;
373
374 if (strcmp ("stdout", filename) == 0
375 || strcmp ("-", filename) == 0)
376 return stdout;
377
378 FILE *stream = fopen (filename, trunc ? "w" : "a");
379
380 if (!stream)
381 error ("could not open dump file %qs: %m", filename);
382 return stream;
383 }
384
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. */
388
389 static FILE *
390 dump_open_alternate_stream (struct dump_file_info *dfi)
391 {
392 if (!dfi->alt_filename)
393 return NULL;
394
395 if (dfi->alt_stream)
396 return dfi->alt_stream;
397
398 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
399
400 if (stream)
401 dfi->alt_state = 1;
402
403 return stream;
404 }
405
406 /* Construct a dump_user_location_t from STMT (using its location and
407 hotness). */
408
409 dump_user_location_t::dump_user_location_t (gimple *stmt)
410 : m_count (), m_loc (UNKNOWN_LOCATION)
411 {
412 if (stmt)
413 {
414 if (stmt->bb)
415 m_count = stmt->bb->count;
416 m_loc = gimple_location (stmt);
417 }
418 }
419
420 /* Construct a dump_user_location_t from an RTL instruction (using its
421 location and hotness). */
422
423 dump_user_location_t::dump_user_location_t (rtx_insn *insn)
424 : m_count (), m_loc (UNKNOWN_LOCATION)
425 {
426 if (insn)
427 {
428 basic_block bb = BLOCK_FOR_INSN (insn);
429 if (bb)
430 m_count = bb->count;
431 m_loc = INSN_LOCATION (insn);
432 }
433 }
434
435 /* Construct from a function declaration. This one requires spelling out
436 to avoid accidentally constructing from other kinds of tree. */
437
438 dump_user_location_t
439 dump_user_location_t::from_function_decl (tree fndecl)
440 {
441 gcc_assert (fndecl);
442
443 // FIXME: profile count for function?
444 return dump_user_location_t (profile_count (),
445 DECL_SOURCE_LOCATION (fndecl));
446 }
447
448 /* Print source location on DFILE if enabled. */
449
450 static void
451 dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
452 {
453 if (dump_kind)
454 {
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 (), "");
465 }
466 }
467
468 /* Dump gimple statement GS with SPC indentation spaces and
469 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
470
471 void
472 dump_gimple_stmt (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
473 gimple *gs, int spc)
474 {
475 if (dump_file && (dump_kind & pflags))
476 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
477
478 if (alt_dump_file && (dump_kind & alt_flags))
479 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
480 }
481
482 /* Similar to dump_gimple_stmt, except additionally print source location. */
483
484 void
485 dump_gimple_stmt_loc (dump_flags_t dump_kind, const dump_location_t &loc,
486 dump_flags_t extra_dump_flags, gimple *gs, int spc)
487 {
488 location_t srcloc = loc.get_location_t ();
489 if (dump_file && (dump_kind & pflags))
490 {
491 dump_loc (dump_kind, dump_file, srcloc);
492 print_gimple_stmt (dump_file, gs, spc, dump_flags | extra_dump_flags);
493 }
494
495 if (alt_dump_file && (dump_kind & alt_flags))
496 {
497 dump_loc (dump_kind, alt_dump_file, srcloc);
498 print_gimple_stmt (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
499 }
500 }
501
502 /* Dump gimple statement GS with SPC indentation spaces and
503 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
504 Do not terminate with a newline or semicolon. */
505
506 void
507 dump_gimple_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
508 gimple *gs, int spc)
509 {
510 if (dump_file && (dump_kind & pflags))
511 print_gimple_expr (dump_file, gs, spc, dump_flags | extra_dump_flags);
512
513 if (alt_dump_file && (dump_kind & alt_flags))
514 print_gimple_expr (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
515 }
516
517 /* Similar to dump_gimple_expr, except additionally print source location. */
518
519 void
520 dump_gimple_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
521 dump_flags_t extra_dump_flags, gimple *gs, int spc)
522 {
523 location_t srcloc = loc.get_location_t ();
524 if (dump_file && (dump_kind & pflags))
525 {
526 dump_loc (dump_kind, dump_file, srcloc);
527 print_gimple_expr (dump_file, gs, spc, dump_flags | extra_dump_flags);
528 }
529
530 if (alt_dump_file && (dump_kind & alt_flags))
531 {
532 dump_loc (dump_kind, alt_dump_file, srcloc);
533 print_gimple_expr (alt_dump_file, gs, spc, dump_flags | extra_dump_flags);
534 }
535 }
536
537
538 /* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
539 DUMP_KIND is enabled. */
540
541 void
542 dump_generic_expr (dump_flags_t dump_kind, dump_flags_t extra_dump_flags,
543 tree t)
544 {
545 if (dump_file && (dump_kind & pflags))
546 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
547
548 if (alt_dump_file && (dump_kind & alt_flags))
549 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
550 }
551
552
553 /* Similar to dump_generic_expr, except additionally print the source
554 location. */
555
556 void
557 dump_generic_expr_loc (dump_flags_t dump_kind, const dump_location_t &loc,
558 dump_flags_t extra_dump_flags, tree t)
559 {
560 location_t srcloc = loc.get_location_t ();
561 if (dump_file && (dump_kind & pflags))
562 {
563 dump_loc (dump_kind, dump_file, srcloc);
564 print_generic_expr (dump_file, t, dump_flags | extra_dump_flags);
565 }
566
567 if (alt_dump_file && (dump_kind & alt_flags))
568 {
569 dump_loc (dump_kind, alt_dump_file, srcloc);
570 print_generic_expr (alt_dump_file, t, dump_flags | extra_dump_flags);
571 }
572 }
573
574 /* Output a formatted message using FORMAT on appropriate dump streams. */
575
576 void
577 dump_printf (dump_flags_t dump_kind, const char *format, ...)
578 {
579 if (dump_file && (dump_kind & pflags))
580 {
581 va_list ap;
582 va_start (ap, format);
583 vfprintf (dump_file, format, ap);
584 va_end (ap);
585 }
586
587 if (alt_dump_file && (dump_kind & alt_flags))
588 {
589 va_list ap;
590 va_start (ap, format);
591 vfprintf (alt_dump_file, format, ap);
592 va_end (ap);
593 }
594 }
595
596 /* Similar to dump_printf, except source location is also printed. */
597
598 void
599 dump_printf_loc (dump_flags_t dump_kind, const dump_location_t &loc,
600 const char *format, ...)
601 {
602 location_t srcloc = loc.get_location_t ();
603 if (dump_file && (dump_kind & pflags))
604 {
605 va_list ap;
606 dump_loc (dump_kind, dump_file, srcloc);
607 va_start (ap, format);
608 vfprintf (dump_file, format, ap);
609 va_end (ap);
610 }
611
612 if (alt_dump_file && (dump_kind & alt_flags))
613 {
614 va_list ap;
615 dump_loc (dump_kind, alt_dump_file, srcloc);
616 va_start (ap, format);
617 vfprintf (alt_dump_file, format, ap);
618 va_end (ap);
619 }
620 }
621
622 /* Output VALUE in decimal to appropriate dump streams. */
623
624 template<unsigned int N, typename C>
625 void
626 dump_dec (dump_flags_t dump_kind, const poly_int<N, C> &value)
627 {
628 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
629 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
630 if (dump_file && (dump_kind & pflags))
631 print_dec (value, dump_file, sgn);
632
633 if (alt_dump_file && (dump_kind & alt_flags))
634 print_dec (value, alt_dump_file, sgn);
635 }
636
637 template void dump_dec (dump_flags_t, const poly_uint16 &);
638 template void dump_dec (dump_flags_t, const poly_int64 &);
639 template void dump_dec (dump_flags_t, const poly_uint64 &);
640 template void dump_dec (dump_flags_t, const poly_offset_int &);
641 template void dump_dec (dump_flags_t, const poly_widest_int &);
642
643 void
644 dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
645 {
646 if (dump_file && (dump_kind & pflags))
647 print_dec (value, dump_file, sgn);
648
649 if (alt_dump_file && (dump_kind & alt_flags))
650 print_dec (value, alt_dump_file, sgn);
651 }
652
653 /* Output VALUE in hexadecimal to appropriate dump streams. */
654
655 void
656 dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
657 {
658 if (dump_file && (dump_kind & pflags))
659 print_hex (value, dump_file);
660
661 if (alt_dump_file && (dump_kind & alt_flags))
662 print_hex (value, alt_dump_file);
663 }
664
665 /* The current dump scope-nesting depth. */
666
667 static int dump_scope_depth;
668
669 /* Get the current dump scope-nesting depth.
670 For use by dump_*_loc (for showing nesting via indentation). */
671
672 unsigned int
673 get_dump_scope_depth ()
674 {
675 return dump_scope_depth;
676 }
677
678 /* Push a nested dump scope.
679 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
680 destination, if any.
681 Increment the scope depth. */
682
683 void
684 dump_begin_scope (const char *name, const dump_location_t &loc)
685 {
686 dump_printf_loc (MSG_NOTE, loc, "=== %s ===\n", name);
687 dump_scope_depth++;
688 }
689
690 /* Pop a nested dump scope. */
691
692 void
693 dump_end_scope ()
694 {
695 dump_scope_depth--;
696 }
697
698 /* Start a dump for PHASE. Store user-supplied dump flags in
699 *FLAG_PTR. Return the number of streams opened. Set globals
700 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
701 set dump_flags appropriately for both pass dump stream and
702 -fopt-info stream. */
703
704 int
705 gcc::dump_manager::
706 dump_start (int phase, dump_flags_t *flag_ptr)
707 {
708 int count = 0;
709 char *name;
710 struct dump_file_info *dfi;
711 FILE *stream;
712 if (phase == TDI_none || !dump_phase_enabled_p (phase))
713 return 0;
714
715 dfi = get_dump_file_info (phase);
716 name = get_dump_file_name (phase);
717 if (name)
718 {
719 stream = dump_open (name, dfi->pstate < 0);
720 if (stream)
721 {
722 dfi->pstate = 1;
723 count++;
724 }
725 free (name);
726 dfi->pstream = stream;
727 set_dump_file (dfi->pstream);
728 /* Initialize current dump flags. */
729 pflags = dfi->pflags;
730 }
731
732 stream = dump_open_alternate_stream (dfi);
733 if (stream)
734 {
735 dfi->alt_stream = stream;
736 count++;
737 set_alt_dump_file (dfi->alt_stream);
738 /* Initialize current -fopt-info flags. */
739 alt_flags = dfi->alt_flags;
740 }
741
742 if (flag_ptr)
743 *flag_ptr = dfi->pflags;
744
745 return count;
746 }
747
748 /* Finish a tree dump for PHASE and close associated dump streams. Also
749 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
750
751 void
752 gcc::dump_manager::
753 dump_finish (int phase)
754 {
755 struct dump_file_info *dfi;
756
757 if (phase < 0)
758 return;
759 dfi = get_dump_file_info (phase);
760 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
761 fclose (dfi->pstream);
762
763 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
764 fclose (dfi->alt_stream);
765
766 dfi->alt_stream = NULL;
767 dfi->pstream = NULL;
768 set_dump_file (NULL);
769 set_alt_dump_file (NULL);
770 dump_flags = TDF_NONE;
771 alt_flags = TDF_NONE;
772 pflags = TDF_NONE;
773 }
774
775 /* Begin a tree dump for PHASE. Stores any user supplied flag in
776 *FLAG_PTR and returns a stream to write to. If the dump is not
777 enabled, returns NULL.
778 PART can be used for dump files which should be split to multiple
779 parts. PART == -1 indicates dump file with no parts.
780 If PART is -1, multiple calls will reopen and append to the dump file. */
781
782 FILE *
783 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
784 {
785 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
786 }
787
788 FILE *
789 gcc::dump_manager::
790 dump_begin (int phase, dump_flags_t *flag_ptr, int part)
791 {
792 char *name;
793 struct dump_file_info *dfi;
794 FILE *stream;
795
796 if (phase == TDI_none || !dump_phase_enabled_p (phase))
797 return NULL;
798
799 name = get_dump_file_name (phase, part);
800 if (!name)
801 return NULL;
802 dfi = get_dump_file_info (phase);
803
804 /* We do not support re-opening of dump files with parts. This would require
805 tracking pstate per part of the dump file. */
806 stream = dump_open (name, part != -1 || dfi->pstate < 0);
807 if (stream)
808 dfi->pstate = 1;
809 free (name);
810
811 if (flag_ptr)
812 *flag_ptr = dfi->pflags;
813
814 /* Initialize current flags */
815 pflags = dfi->pflags;
816 return stream;
817 }
818
819 /* Returns nonzero if dump PHASE is enabled for at least one stream.
820 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
821 any phase. */
822
823 int
824 gcc::dump_manager::
825 dump_phase_enabled_p (int phase) const
826 {
827 if (phase == TDI_tree_all)
828 {
829 size_t i;
830 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
831 if (dump_files[i].pstate || dump_files[i].alt_state)
832 return 1;
833 for (i = 0; i < m_extra_dump_files_in_use; i++)
834 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
835 return 1;
836 return 0;
837 }
838 else
839 {
840 struct dump_file_info *dfi = get_dump_file_info (phase);
841 return dfi->pstate || dfi->alt_state;
842 }
843 }
844
845 /* Returns nonzero if tree dump PHASE has been initialized. */
846
847 int
848 gcc::dump_manager::
849 dump_initialized_p (int phase) const
850 {
851 struct dump_file_info *dfi = get_dump_file_info (phase);
852 return dfi->pstate > 0 || dfi->alt_state > 0;
853 }
854
855 /* Returns the switch name of PHASE. */
856
857 const char *
858 dump_flag_name (int phase)
859 {
860 return g->get_dumps ()->dump_flag_name (phase);
861 }
862
863 const char *
864 gcc::dump_manager::
865 dump_flag_name (int phase) const
866 {
867 struct dump_file_info *dfi = get_dump_file_info (phase);
868 return dfi->swtch;
869 }
870
871 /* Finish a tree dump for PHASE. STREAM is the stream created by
872 dump_begin. */
873
874 void
875 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
876 {
877 if (stream != stderr && stream != stdout)
878 fclose (stream);
879 }
880
881 /* Enable all tree dumps with FLAGS on FILENAME. Return number of
882 enabled tree dumps. */
883
884 int
885 gcc::dump_manager::
886 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
887 {
888 int n = 0;
889 size_t i;
890
891 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
892 {
893 if ((dump_files[i].dkind == dkind))
894 {
895 const char *old_filename = dump_files[i].pfilename;
896 dump_files[i].pstate = -1;
897 dump_files[i].pflags |= flags;
898 n++;
899 /* Override the existing filename. */
900 if (filename)
901 {
902 dump_files[i].pfilename = xstrdup (filename);
903 /* Since it is a command-line provided file, which is
904 common to all the phases, use it in append mode. */
905 dump_files[i].pstate = 1;
906 }
907 if (old_filename && filename != old_filename)
908 free (CONST_CAST (char *, old_filename));
909 }
910 }
911
912 for (i = 0; i < m_extra_dump_files_in_use; i++)
913 {
914 if ((m_extra_dump_files[i].dkind == dkind))
915 {
916 const char *old_filename = m_extra_dump_files[i].pfilename;
917 m_extra_dump_files[i].pstate = -1;
918 m_extra_dump_files[i].pflags |= flags;
919 n++;
920 /* Override the existing filename. */
921 if (filename)
922 {
923 m_extra_dump_files[i].pfilename = xstrdup (filename);
924 /* Since it is a command-line provided file, which is
925 common to all the phases, use it in append mode. */
926 m_extra_dump_files[i].pstate = 1;
927 }
928 if (old_filename && filename != old_filename)
929 free (CONST_CAST (char *, old_filename));
930 }
931 }
932
933 return n;
934 }
935
936 /* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
937 Enable dumps with FLAGS on FILENAME. Return the number of enabled
938 dumps. */
939
940 int
941 gcc::dump_manager::
942 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
943 const char *filename)
944 {
945 int n = 0;
946 size_t i;
947
948 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
949 {
950 if ((dump_files[i].optgroup_flags & optgroup_flags))
951 {
952 const char *old_filename = dump_files[i].alt_filename;
953 /* Since this file is shared among different passes, it
954 should be opened in append mode. */
955 dump_files[i].alt_state = 1;
956 dump_files[i].alt_flags |= flags;
957 n++;
958 /* Override the existing filename. */
959 if (filename)
960 dump_files[i].alt_filename = xstrdup (filename);
961 if (old_filename && filename != old_filename)
962 free (CONST_CAST (char *, old_filename));
963 }
964 }
965
966 for (i = 0; i < m_extra_dump_files_in_use; i++)
967 {
968 if ((m_extra_dump_files[i].optgroup_flags & optgroup_flags))
969 {
970 const char *old_filename = m_extra_dump_files[i].alt_filename;
971 /* Since this file is shared among different passes, it
972 should be opened in append mode. */
973 m_extra_dump_files[i].alt_state = 1;
974 m_extra_dump_files[i].alt_flags |= flags;
975 n++;
976 /* Override the existing filename. */
977 if (filename)
978 m_extra_dump_files[i].alt_filename = xstrdup (filename);
979 if (old_filename && filename != old_filename)
980 free (CONST_CAST (char *, old_filename));
981 }
982 }
983
984 return n;
985 }
986
987 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
988 relevant details in the dump_files array. */
989
990 int
991 gcc::dump_manager::
992 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
993 {
994 const char *option_value;
995 const char *ptr;
996 dump_flags_t flags;
997
998 if (doglob && !dfi->glob)
999 return 0;
1000
1001 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1002 if (!option_value)
1003 return 0;
1004
1005 if (*option_value && *option_value != '-' && *option_value != '=')
1006 return 0;
1007
1008 ptr = option_value;
1009 flags = TDF_NONE;
1010
1011 while (*ptr)
1012 {
1013 const struct kv_pair<dump_flags_t> *option_ptr;
1014 const char *end_ptr;
1015 const char *eq_ptr;
1016 unsigned length;
1017
1018 while (*ptr == '-')
1019 ptr++;
1020 end_ptr = strchr (ptr, '-');
1021 eq_ptr = strchr (ptr, '=');
1022
1023 if (eq_ptr && !end_ptr)
1024 end_ptr = eq_ptr;
1025
1026 if (!end_ptr)
1027 end_ptr = ptr + strlen (ptr);
1028 length = end_ptr - ptr;
1029
1030 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1031 if (strlen (option_ptr->name) == length
1032 && !memcmp (option_ptr->name, ptr, length))
1033 {
1034 flags |= option_ptr->value;
1035 goto found;
1036 }
1037
1038 if (*ptr == '=')
1039 {
1040 /* Interpret rest of the argument as a dump filename. This
1041 filename overrides other command line filenames. */
1042 if (dfi->pfilename)
1043 free (CONST_CAST (char *, dfi->pfilename));
1044 dfi->pfilename = xstrdup (ptr + 1);
1045 break;
1046 }
1047 else
1048 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1049 length, ptr, dfi->swtch);
1050 found:;
1051 ptr = end_ptr;
1052 }
1053
1054 dfi->pstate = -1;
1055 dfi->pflags |= flags;
1056
1057 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1058 known dumps. */
1059 if (dfi->suffix == NULL)
1060 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
1061
1062 return 1;
1063 }
1064
1065 int
1066 gcc::dump_manager::
1067 dump_switch_p (const char *arg)
1068 {
1069 size_t i;
1070 int any = 0;
1071
1072 for (i = TDI_none + 1; i != TDI_end; i++)
1073 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1074
1075 /* Don't glob if we got a hit already */
1076 if (!any)
1077 for (i = TDI_none + 1; i != TDI_end; i++)
1078 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1079
1080 for (i = 0; i < m_extra_dump_files_in_use; i++)
1081 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
1082
1083 if (!any)
1084 for (i = 0; i < m_extra_dump_files_in_use; i++)
1085 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
1086
1087
1088 return any;
1089 }
1090
1091 /* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1092 and filename. Return non-zero if it is a recognized switch. */
1093
1094 static int
1095 opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1096 optgroup_flags_t *optgroup_flags, char **filename)
1097 {
1098 const char *option_value;
1099 const char *ptr;
1100
1101 option_value = arg;
1102 ptr = option_value;
1103
1104 *filename = NULL;
1105 *flags = TDF_NONE;
1106 *optgroup_flags = OPTGROUP_NONE;
1107
1108 if (!ptr)
1109 return 1; /* Handle '-fopt-info' without any additional options. */
1110
1111 while (*ptr)
1112 {
1113 const char *end_ptr;
1114 const char *eq_ptr;
1115 unsigned length;
1116
1117 while (*ptr == '-')
1118 ptr++;
1119 end_ptr = strchr (ptr, '-');
1120 eq_ptr = strchr (ptr, '=');
1121
1122 if (eq_ptr && !end_ptr)
1123 end_ptr = eq_ptr;
1124
1125 if (!end_ptr)
1126 end_ptr = ptr + strlen (ptr);
1127 length = end_ptr - ptr;
1128
1129 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1130 option_ptr->name; option_ptr++)
1131 if (strlen (option_ptr->name) == length
1132 && !memcmp (option_ptr->name, ptr, length))
1133 {
1134 *flags |= option_ptr->value;
1135 goto found;
1136 }
1137
1138 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1139 option_ptr->name; option_ptr++)
1140 if (strlen (option_ptr->name) == length
1141 && !memcmp (option_ptr->name, ptr, length))
1142 {
1143 *optgroup_flags |= option_ptr->value;
1144 goto found;
1145 }
1146
1147 if (*ptr == '=')
1148 {
1149 /* Interpret rest of the argument as a dump filename. This
1150 filename overrides other command line filenames. */
1151 *filename = xstrdup (ptr + 1);
1152 break;
1153 }
1154 else
1155 {
1156 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1157 length, ptr, arg);
1158 return 0;
1159 }
1160 found:;
1161 ptr = end_ptr;
1162 }
1163
1164 return 1;
1165 }
1166
1167 /* Return non-zero if ARG is a recognized switch for
1168 -fopt-info. Return zero otherwise. */
1169
1170 int
1171 opt_info_switch_p (const char *arg)
1172 {
1173 dump_flags_t flags;
1174 optgroup_flags_t optgroup_flags;
1175 char *filename;
1176 static char *file_seen = NULL;
1177 gcc::dump_manager *dumps = g->get_dumps ();
1178
1179 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1180 return 0;
1181
1182 if (!filename)
1183 filename = xstrdup ("stderr");
1184
1185 /* Bail out if a different filename has been specified. */
1186 if (file_seen && strcmp (file_seen, filename))
1187 {
1188 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1189 arg);
1190 return 1;
1191 }
1192
1193 file_seen = xstrdup (filename);
1194 if (!flags)
1195 flags = MSG_OPTIMIZED_LOCATIONS;
1196 if (!optgroup_flags)
1197 optgroup_flags = OPTGROUP_ALL;
1198
1199 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
1200 }
1201
1202 /* Print basic block on the dump streams. */
1203
1204 void
1205 dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
1206 {
1207 if (dump_file && (dump_kind & pflags))
1208 dump_bb (dump_file, bb, indent, TDF_DETAILS);
1209 if (alt_dump_file && (dump_kind & alt_flags))
1210 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
1211 }
1212
1213 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1214
1215 void
1216 dump_function (int phase, tree fn)
1217 {
1218 FILE *stream;
1219 dump_flags_t flags;
1220
1221 stream = dump_begin (phase, &flags);
1222 if (stream)
1223 {
1224 dump_function_to_file (fn, stream, flags);
1225 dump_end (phase, stream);
1226 }
1227 }
1228
1229 /* Print information from the combine pass on dump_file. */
1230
1231 void
1232 print_combine_total_stats (void)
1233 {
1234 if (dump_file)
1235 dump_combine_total_stats (dump_file);
1236 }
1237
1238 /* Enable RTL dump for all the RTL passes. */
1239
1240 bool
1241 enable_rtl_dump_file (void)
1242 {
1243 gcc::dump_manager *dumps = g->get_dumps ();
1244 int num_enabled =
1245 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
1246 NULL);
1247 return num_enabled > 0;
1248 }
1249
1250 #if CHECKING_P
1251
1252 namespace selftest {
1253
1254 /* Verify that the dump_location_t constructors capture the source location
1255 at which they were called (provided that the build compiler is sufficiently
1256 recent). */
1257
1258 static void
1259 test_impl_location ()
1260 {
1261 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
1262 /* Default ctor. */
1263 {
1264 dump_location_t loc;
1265 const int expected_line = __LINE__ - 1;
1266 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1267 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1268 }
1269
1270 /* Constructing from a gimple. */
1271 {
1272 dump_location_t loc ((gimple *)NULL);
1273 const int expected_line = __LINE__ - 1;
1274 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1275 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1276 }
1277
1278 /* Constructing from an rtx_insn. */
1279 {
1280 dump_location_t loc ((rtx_insn *)NULL);
1281 const int expected_line = __LINE__ - 1;
1282 ASSERT_STR_CONTAINS (loc.get_impl_location ().m_file, "dumpfile.c");
1283 ASSERT_EQ (loc.get_impl_location ().m_line, expected_line);
1284 }
1285 #endif
1286 }
1287
1288 /* Run all of the selftests within this file. */
1289
1290 void
1291 dumpfile_c_tests ()
1292 {
1293 test_impl_location ();
1294 }
1295
1296 } // namespace selftest
1297
1298 #endif /* CHECKING_P */