]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dumpfile.c
generalized IPA predicate on parameter
[thirdparty/gcc.git] / gcc / dumpfile.c
CommitLineData
8ae8ae1b 1/* Dump infrastructure for optimizations and intermediate representation.
fbd26352 2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
8ae8ae1b 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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"
7c29e30e 23#include "options.h"
24#include "tree.h"
25#include "gimple-pretty-print.h"
8ae8ae1b 26#include "diagnostic-core.h"
27#include "dumpfile.h"
41142c53 28#include "context.h"
a7dca604 29#include "profile-count.h"
930c75f4 30#include "tree-cfg.h"
73714718 31#include "langhooks.h"
c309657f 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"
38cf91e5 36#include "optinfo.h"
37#include "dump-context.h"
38#include "cgraph.h"
39#include "tree-pass.h" /* for "current_pass". */
9dcf2a11 40#include "optinfo-emit-json.h"
bffe1cb4 41#include "stringpool.h" /* for get_identifier. */
8ae8ae1b 42
43/* If non-NULL, return one past-the-end of the matching SUBPART of
44 the WHOLE string. */
45#define skip_leading_substring(whole, part) \
46 (strncmp (whole, part, strlen (part)) ? NULL : whole + strlen (part))
47
3f6e5ced 48static dump_flags_t pflags; /* current dump_flags */
8ae8ae1b 49
be1e7283 50static void dump_loc (dump_flags_t, FILE *, location_t);
7fa3c46f 51
52/* Current -fopt-info output stream, if any, and flags. */
53static FILE *alt_dump_file = NULL;
54static dump_flags_t alt_flags;
55
8ae8ae1b 56static FILE *dump_open_alternate_stream (struct dump_file_info *);
57
6d8fb6cf 58/* These are currently used for communicating between passes.
59 However, instead of accessing them directly, the passes can use
60 dump_printf () for dumps. */
61FILE *dump_file = NULL;
6d8fb6cf 62const char *dump_file_name;
3f6e5ced 63dump_flags_t dump_flags;
7fa3c46f 64bool dumps_are_enabled = false;
65
66
7fa3c46f 67/* Set global "dump_file" to NEW_DUMP_FILE, refreshing the "dumps_are_enabled"
68 global. */
69
70void
71set_dump_file (FILE *new_dump_file)
72{
38cf91e5 73 dumpfile_ensure_any_optinfo_are_flushed ();
7fa3c46f 74 dump_file = new_dump_file;
71e711b7 75 dump_context::get ().refresh_dumps_are_enabled ();
7fa3c46f 76}
77
78/* Set "alt_dump_file" to NEW_ALT_DUMP_FILE, refreshing the "dumps_are_enabled"
79 global. */
80
81static void
82set_alt_dump_file (FILE *new_alt_dump_file)
83{
38cf91e5 84 dumpfile_ensure_any_optinfo_are_flushed ();
7fa3c46f 85 alt_dump_file = new_alt_dump_file;
71e711b7 86 dump_context::get ().refresh_dumps_are_enabled ();
7fa3c46f 87}
6d8fb6cf 88
eb37fcdc 89#define DUMP_FILE_INFO(suffix, swtch, dkind, num) \
54e7de93 90 {suffix, swtch, NULL, NULL, NULL, NULL, NULL, dkind, TDF_NONE, TDF_NONE, \
91 OPTGROUP_NONE, 0, 0, num, false, false}
ffdaf8f1 92
8ae8ae1b 93/* Table of tree dump switches. This must be consistent with the
94 TREE_DUMP_INDEX enumeration in dumpfile.h. */
95static struct dump_file_info dump_files[TDI_end] =
96{
eb37fcdc 97 DUMP_FILE_INFO (NULL, NULL, DK_none, 0),
98 DUMP_FILE_INFO (".cgraph", "ipa-cgraph", DK_ipa, 0),
99 DUMP_FILE_INFO (".type-inheritance", "ipa-type-inheritance", DK_ipa, 0),
100 DUMP_FILE_INFO (".ipa-clones", "ipa-clones", DK_ipa, 0),
73714718 101 DUMP_FILE_INFO (".original", "tree-original", DK_tree, 0),
102 DUMP_FILE_INFO (".gimple", "tree-gimple", DK_tree, 0),
103 DUMP_FILE_INFO (".nested", "tree-nested", DK_tree, 0),
7858a084 104 DUMP_FILE_INFO (".lto-stream-out", "ipa-lto-stream-out", DK_ipa, 0),
73714718 105#define FIRST_AUTO_NUMBERED_DUMP 1
025fe22b 106#define FIRST_ME_AUTO_NUMBERED_DUMP 4
8ae8ae1b 107
eb37fcdc 108 DUMP_FILE_INFO (NULL, "lang-all", DK_lang, 0),
109 DUMP_FILE_INFO (NULL, "tree-all", DK_tree, 0),
110 DUMP_FILE_INFO (NULL, "rtl-all", DK_rtl, 0),
111 DUMP_FILE_INFO (NULL, "ipa-all", DK_ipa, 0),
8ae8ae1b 112};
113
8ae8ae1b 114/* Table of dump options. This must be consistent with the TDF_* flags
115 in dumpfile.h and opt_info_options below. */
54e7de93 116static const kv_pair<dump_flags_t> dump_options[] =
8ae8ae1b 117{
b340d73f 118 {"none", TDF_NONE},
8ae8ae1b 119 {"address", TDF_ADDRESS},
120 {"asmname", TDF_ASMNAME},
121 {"slim", TDF_SLIM},
122 {"raw", TDF_RAW},
123 {"graph", TDF_GRAPH},
124 {"details", (TDF_DETAILS | MSG_OPTIMIZED_LOCATIONS
125 | MSG_MISSED_OPTIMIZATION
126 | MSG_NOTE)},
127 {"cselib", TDF_CSELIB},
128 {"stats", TDF_STATS},
129 {"blocks", TDF_BLOCKS},
130 {"vops", TDF_VOPS},
131 {"lineno", TDF_LINENO},
132 {"uid", TDF_UID},
133 {"stmtaddr", TDF_STMTADDR},
134 {"memsyms", TDF_MEMSYMS},
8ae8ae1b 135 {"eh", TDF_EH},
136 {"alias", TDF_ALIAS},
137 {"nouid", TDF_NOUID},
138 {"enumerate_locals", TDF_ENUMERATE_LOCALS},
139 {"scev", TDF_SCEV},
b1f04d34 140 {"gimple", TDF_GIMPLE},
3a068f26 141 {"folding", TDF_FOLDING},
633ed9f7 142 {"optimized", MSG_OPTIMIZED_LOCATIONS},
143 {"missed", MSG_MISSED_OPTIMIZATION},
144 {"note", MSG_NOTE},
9ddd8fa7 145 {"optall", MSG_ALL_KINDS},
0c413628 146 {"all", dump_flags_t (TDF_ALL_VALUES
147 & ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_GRAPH
148 | TDF_STMTADDR | TDF_RHS_ONLY | TDF_NOUID
149 | TDF_ENUMERATE_LOCALS | TDF_SCEV | TDF_GIMPLE))},
54e7de93 150 {NULL, TDF_NONE}
8ae8ae1b 151};
152
c7875731 153/* A subset of the dump_options table which is used for -fopt-info
154 types. This must be consistent with the MSG_* flags in dumpfile.h.
8ae8ae1b 155 */
54e7de93 156static const kv_pair<dump_flags_t> optinfo_verbosity_options[] =
8ae8ae1b 157{
158 {"optimized", MSG_OPTIMIZED_LOCATIONS},
159 {"missed", MSG_MISSED_OPTIMIZATION},
160 {"note", MSG_NOTE},
9ddd8fa7 161 {"all", MSG_ALL_KINDS},
162 {"internals", MSG_PRIORITY_INTERNALS},
54e7de93 163 {NULL, TDF_NONE}
c7875731 164};
165
166/* Flags used for -fopt-info groups. */
9dcf2a11 167const kv_pair<optgroup_flags_t> optgroup_options[] =
c7875731 168{
169 {"ipa", OPTGROUP_IPA},
170 {"loop", OPTGROUP_LOOP},
171 {"inline", OPTGROUP_INLINE},
f57c8178 172 {"omp", OPTGROUP_OMP},
c7875731 173 {"vec", OPTGROUP_VEC},
174 {"optall", OPTGROUP_ALL},
54e7de93 175 {NULL, OPTGROUP_NONE}
8ae8ae1b 176};
177
41142c53 178gcc::dump_manager::dump_manager ():
179 m_next_dump (FIRST_AUTO_NUMBERED_DUMP),
180 m_extra_dump_files (NULL),
181 m_extra_dump_files_in_use (0),
e81a6963 182 m_extra_dump_files_alloced (0),
183 m_optgroup_flags (OPTGROUP_NONE),
184 m_optinfo_flags (TDF_NONE),
185 m_optinfo_filename (NULL)
41142c53 186{
187}
188
33c6d8ad 189gcc::dump_manager::~dump_manager ()
190{
e81a6963 191 free (m_optinfo_filename);
33c6d8ad 192 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
193 {
194 dump_file_info *dfi = &m_extra_dump_files[i];
195 /* suffix, swtch, glob are statically allocated for the entries
196 in dump_files, and for statistics, but are dynamically allocated
197 for those for passes. */
198 if (dfi->owns_strings)
199 {
200 XDELETEVEC (const_cast <char *> (dfi->suffix));
201 XDELETEVEC (const_cast <char *> (dfi->swtch));
202 XDELETEVEC (const_cast <char *> (dfi->glob));
203 }
204 /* These, if non-NULL, are always dynamically allocated. */
205 XDELETEVEC (const_cast <char *> (dfi->pfilename));
206 XDELETEVEC (const_cast <char *> (dfi->alt_filename));
207 }
208 XDELETEVEC (m_extra_dump_files);
209}
210
8ae8ae1b 211unsigned int
41142c53 212gcc::dump_manager::
8ae8ae1b 213dump_register (const char *suffix, const char *swtch, const char *glob,
54e7de93 214 dump_kind dkind, optgroup_flags_t optgroup_flags,
215 bool take_ownership)
8ae8ae1b 216{
41142c53 217 int num = m_next_dump++;
8ae8ae1b 218
41142c53 219 size_t count = m_extra_dump_files_in_use++;
8ae8ae1b 220
41142c53 221 if (count >= m_extra_dump_files_alloced)
8ae8ae1b 222 {
41142c53 223 if (m_extra_dump_files_alloced == 0)
73714718 224 m_extra_dump_files_alloced = 512;
8ae8ae1b 225 else
41142c53 226 m_extra_dump_files_alloced *= 2;
227 m_extra_dump_files = XRESIZEVEC (struct dump_file_info,
228 m_extra_dump_files,
229 m_extra_dump_files_alloced);
a324786b 230
231 /* Construct a new object in the space allocated above. */
232 new (m_extra_dump_files + count) dump_file_info ();
233 }
234 else
235 {
236 /* Zero out the already constructed object. */
237 m_extra_dump_files[count] = dump_file_info ();
8ae8ae1b 238 }
239
41142c53 240 m_extra_dump_files[count].suffix = suffix;
241 m_extra_dump_files[count].swtch = swtch;
242 m_extra_dump_files[count].glob = glob;
ffdaf8f1 243 m_extra_dump_files[count].dkind = dkind;
41142c53 244 m_extra_dump_files[count].optgroup_flags = optgroup_flags;
245 m_extra_dump_files[count].num = num;
33c6d8ad 246 m_extra_dump_files[count].owns_strings = take_ownership;
8ae8ae1b 247
248 return count + TDI_end;
249}
250
251
73714718 252/* Allow languages and middle-end to register their dumps before the
253 optimization passes. */
254
255void
256gcc::dump_manager::
257register_dumps ()
258{
259 lang_hooks.register_dumps (this);
260 /* If this assert fails, some FE registered more than
261 FIRST_ME_AUTO_NUMBERED_DUMP - FIRST_AUTO_NUMBERED_DUMP
262 dump files. Bump FIRST_ME_AUTO_NUMBERED_DUMP accordingly. */
263 gcc_assert (m_next_dump <= FIRST_ME_AUTO_NUMBERED_DUMP);
264 m_next_dump = FIRST_ME_AUTO_NUMBERED_DUMP;
265 dump_files[TDI_original].num = m_next_dump++;
266 dump_files[TDI_gimple].num = m_next_dump++;
267 dump_files[TDI_nested].num = m_next_dump++;
268}
269
270
8ae8ae1b 271/* Return the dump_file_info for the given phase. */
272
273struct dump_file_info *
41142c53 274gcc::dump_manager::
275get_dump_file_info (int phase) const
8ae8ae1b 276{
277 if (phase < TDI_end)
278 return &dump_files[phase];
41142c53 279 else if ((size_t) (phase - TDI_end) >= m_extra_dump_files_in_use)
8ae8ae1b 280 return NULL;
281 else
41142c53 282 return m_extra_dump_files + (phase - TDI_end);
8ae8ae1b 283}
284
f9e004fe 285/* Locate the dump_file_info with swtch equal to SWTCH,
286 or return NULL if no such dump_file_info exists. */
287
288struct dump_file_info *
289gcc::dump_manager::
290get_dump_file_info_by_switch (const char *swtch) const
291{
292 for (unsigned i = 0; i < m_extra_dump_files_in_use; i++)
c9281ef8 293 if (strcmp (m_extra_dump_files[i].swtch, swtch) == 0)
f9e004fe 294 return &m_extra_dump_files[i];
295
296 /* Not found. */
297 return NULL;
298}
299
8ae8ae1b 300
301/* Return the name of the dump file for the given phase.
f9e004fe 302 The caller is responsible for calling free on the returned
303 buffer.
8ae8ae1b 304 If the dump is not enabled, returns NULL. */
305
306char *
41142c53 307gcc::dump_manager::
96854265 308get_dump_file_name (int phase, int part) const
8ae8ae1b 309{
8ae8ae1b 310 struct dump_file_info *dfi;
311
312 if (phase == TDI_none)
313 return NULL;
314
315 dfi = get_dump_file_info (phase);
f9e004fe 316
96854265 317 return get_dump_file_name (dfi, part);
f9e004fe 318}
319
320/* Return the name of the dump file for the given dump_file_info.
321 The caller is responsible for calling free on the returned
322 buffer.
323 If the dump is not enabled, returns NULL. */
324
325char *
326gcc::dump_manager::
96854265 327get_dump_file_name (struct dump_file_info *dfi, int part) const
f9e004fe 328{
329 char dump_id[10];
330
331 gcc_assert (dfi);
332
8ae8ae1b 333 if (dfi->pstate == 0)
334 return NULL;
335
336 /* If available, use the command line dump filename. */
337 if (dfi->pfilename)
338 return xstrdup (dfi->pfilename);
339
340 if (dfi->num < 0)
341 dump_id[0] = '\0';
342 else
343 {
ffdaf8f1 344 /* (null), LANG, TREE, RTL, IPA. */
345 char suffix = " ltri"[dfi->dkind];
75e7644a 346
ffdaf8f1 347 if (snprintf (dump_id, sizeof (dump_id), ".%03d%c", dfi->num, suffix) < 0)
8ae8ae1b 348 dump_id[0] = '\0';
349 }
350
96854265 351 if (part != -1)
352 {
353 char part_id[8];
354 snprintf (part_id, sizeof (part_id), ".%i", part);
355 return concat (dump_base_name, dump_id, part_id, dfi->suffix, NULL);
356 }
357 else
358 return concat (dump_base_name, dump_id, dfi->suffix, NULL);
8ae8ae1b 359}
360
0c124b17 361/* Open a dump file called FILENAME. Some filenames are special and
362 refer to the standard streams. TRUNC indicates whether this is the
363 first open (so the file should be truncated, rather than appended).
364 An error message is emitted in the event of failure. */
365
366static FILE *
367dump_open (const char *filename, bool trunc)
368{
369 if (strcmp ("stderr", filename) == 0)
370 return stderr;
371
6268bc0f 372 if (strcmp ("stdout", filename) == 0
373 || strcmp ("-", filename) == 0)
0c124b17 374 return stdout;
375
376 FILE *stream = fopen (filename, trunc ? "w" : "a");
377
378 if (!stream)
379 error ("could not open dump file %qs: %m", filename);
380 return stream;
381}
382
8ae8ae1b 383/* For a given DFI, open an alternate dump filename (which could also
384 be a standard stream such as stdout/stderr). If the alternate dump
385 file cannot be opened, return NULL. */
386
387static FILE *
388dump_open_alternate_stream (struct dump_file_info *dfi)
389{
8ae8ae1b 390 if (!dfi->alt_filename)
391 return NULL;
392
393 if (dfi->alt_stream)
394 return dfi->alt_stream;
395
0c124b17 396 FILE *stream = dump_open (dfi->alt_filename, dfi->alt_state < 0);
8ae8ae1b 397
0c124b17 398 if (stream)
8ae8ae1b 399 dfi->alt_state = 1;
400
401 return stream;
402}
403
c309657f 404/* Construct a dump_user_location_t from STMT (using its location and
405 hotness). */
406
0d4b5198 407dump_user_location_t::dump_user_location_t (const gimple *stmt)
c309657f 408: m_count (), m_loc (UNKNOWN_LOCATION)
409{
410 if (stmt)
411 {
412 if (stmt->bb)
413 m_count = stmt->bb->count;
414 m_loc = gimple_location (stmt);
415 }
416}
417
418/* Construct a dump_user_location_t from an RTL instruction (using its
419 location and hotness). */
420
0d4b5198 421dump_user_location_t::dump_user_location_t (const rtx_insn *insn)
c309657f 422: m_count (), m_loc (UNKNOWN_LOCATION)
423{
424 if (insn)
425 {
426 basic_block bb = BLOCK_FOR_INSN (insn);
427 if (bb)
428 m_count = bb->count;
429 m_loc = INSN_LOCATION (insn);
430 }
431}
432
433/* Construct from a function declaration. This one requires spelling out
434 to avoid accidentally constructing from other kinds of tree. */
435
436dump_user_location_t
437dump_user_location_t::from_function_decl (tree fndecl)
438{
439 gcc_assert (fndecl);
440
441 // FIXME: profile count for function?
442 return dump_user_location_t (profile_count (),
443 DECL_SOURCE_LOCATION (fndecl));
444}
445
a333f643 446/* Extract the MSG_* component from DUMP_KIND and return a string for use
447 as a prefix to dump messages.
448 These match the strings in optinfo_verbosity_options and thus the
449 "OPTIONS" within "-fopt-info-OPTIONS". */
450
451static const char *
452kind_as_string (dump_flags_t dump_kind)
453{
9ddd8fa7 454 switch (dump_kind & MSG_ALL_KINDS)
a333f643 455 {
456 default:
457 gcc_unreachable ();
458 case MSG_OPTIMIZED_LOCATIONS:
459 return "optimized";
460 case MSG_MISSED_OPTIMIZATION:
461 return "missed";
462 case MSG_NOTE:
463 return "note";
464 }
465}
466
8ae8ae1b 467/* Print source location on DFILE if enabled. */
468
c309657f 469static void
be1e7283 470dump_loc (dump_flags_t dump_kind, FILE *dfile, location_t loc)
8ae8ae1b 471{
8ae8ae1b 472 if (dump_kind)
473 {
b2d978a6 474 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
a333f643 475 fprintf (dfile, "%s:%d:%d: ", LOCATION_FILE (loc),
202c2bd9 476 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
c4c46233 477 else if (current_function_decl)
a333f643 478 fprintf (dfile, "%s:%d:%d: ",
8ae8ae1b 479 DECL_SOURCE_FILE (current_function_decl),
202c2bd9 480 DECL_SOURCE_LINE (current_function_decl),
481 DECL_SOURCE_COLUMN (current_function_decl));
a333f643 482 fprintf (dfile, "%s: ", kind_as_string (dump_kind));
b18ceb23 483 /* Indentation based on scope depth. */
484 fprintf (dfile, "%*s", get_dump_scope_depth (), "");
8ae8ae1b 485 }
486}
487
71e711b7 488/* Print source location to PP if enabled. */
489
490static void
be1e7283 491dump_loc (dump_flags_t dump_kind, pretty_printer *pp, location_t loc)
71e711b7 492{
493 if (dump_kind)
494 {
495 if (LOCATION_LOCUS (loc) > BUILTINS_LOCATION)
a333f643 496 pp_printf (pp, "%s:%d:%d: ", LOCATION_FILE (loc),
71e711b7 497 LOCATION_LINE (loc), LOCATION_COLUMN (loc));
498 else if (current_function_decl)
a333f643 499 pp_printf (pp, "%s:%d:%d: ",
71e711b7 500 DECL_SOURCE_FILE (current_function_decl),
501 DECL_SOURCE_LINE (current_function_decl),
502 DECL_SOURCE_COLUMN (current_function_decl));
a333f643 503 pp_printf (pp, "%s: ", kind_as_string (dump_kind));
71e711b7 504 /* Indentation based on scope depth. */
505 for (unsigned i = 0; i < get_dump_scope_depth (); i++)
506 pp_character (pp, ' ');
507 }
508}
509
38cf91e5 510/* Implementation of dump_context member functions. */
511
512/* dump_context's dtor. */
513
514dump_context::~dump_context ()
515{
516 delete m_pending;
517}
518
80d492fc 519void
520dump_context::set_json_writer (optrecord_json_writer *writer)
521{
522 delete m_json_writer;
523 m_json_writer = writer;
524}
525
526/* Perform cleanup activity for -fsave-optimization-record.
527 Currently, the file is written out here in one go, before cleaning
528 up. */
529
530void
531dump_context::finish_any_json_writer ()
532{
533 if (!m_json_writer)
534 return;
535
536 m_json_writer->write ();
537 delete m_json_writer;
538 m_json_writer = NULL;
539}
540
71e711b7 541/* Update the "dumps_are_enabled" global; to be called whenever dump_file
542 or alt_dump_file change, or when changing dump_context in selftests. */
543
544void
545dump_context::refresh_dumps_are_enabled ()
546{
547 dumps_are_enabled = (dump_file || alt_dump_file || optinfo_enabled_p ()
548 || m_test_pp);
549}
550
9ddd8fa7 551/* Determine if a message of kind DUMP_KIND and at the current scope depth
552 should be printed.
553
554 Only show messages that match FILTER both on their kind *and*
555 their priority. */
556
557bool
558dump_context::apply_dump_filter_p (dump_flags_t dump_kind,
559 dump_flags_t filter) const
560{
561 /* Few messages, if any, have an explicit MSG_PRIORITY.
562 If DUMP_KIND does, we'll use it.
563 Otherwise, generate an implicit priority value for the message based
564 on the current scope depth.
565 Messages at the top-level scope are MSG_PRIORITY_USER_FACING,
566 whereas those in nested scopes are MSG_PRIORITY_INTERNALS. */
567 if (!(dump_kind & MSG_ALL_PRIORITIES))
568 {
569 dump_flags_t implicit_priority
570 = (m_scope_depth > 0
571 ? MSG_PRIORITY_INTERNALS
572 : MSG_PRIORITY_USER_FACING);
573 dump_kind |= implicit_priority;
574 }
575
576 return (dump_kind & (filter & MSG_ALL_KINDS)
577 && dump_kind & (filter & MSG_ALL_PRIORITIES));
578}
579
291c13cb 580/* Print LOC to the appropriate dump destinations, given DUMP_KIND.
581 If optinfos are enabled, begin a new optinfo. */
582
583void
e43345c1 584dump_context::dump_loc (const dump_metadata_t &metadata,
585 const dump_user_location_t &loc)
291c13cb 586{
71e711b7 587 end_any_optinfo ();
588
e43345c1 589 dump_loc_immediate (metadata.get_dump_flags (), loc);
ed9370cc 590
591 if (optinfo_enabled_p ())
e43345c1 592 begin_next_optinfo (metadata, loc);
ed9370cc 593}
594
595/* As dump_loc above, but without starting a new optinfo. */
596
597void
598dump_context::dump_loc_immediate (dump_flags_t dump_kind,
e43345c1 599 const dump_user_location_t &loc)
ed9370cc 600{
291c13cb 601 location_t srcloc = loc.get_location_t ();
602
9ddd8fa7 603 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
291c13cb 604 ::dump_loc (dump_kind, dump_file, srcloc);
605
9ddd8fa7 606 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
291c13cb 607 ::dump_loc (dump_kind, alt_dump_file, srcloc);
608
71e711b7 609 /* Support for temp_dump_context in selftests. */
9ddd8fa7 610 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
71e711b7 611 ::dump_loc (dump_kind, m_test_pp, srcloc);
291c13cb 612}
613
71e711b7 614/* Make an item for the given dump call, equivalent to print_gimple_stmt. */
615
616static optinfo_item *
617make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
618{
619 pretty_printer pp;
620 pp_needs_newline (&pp) = true;
621 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
622 pp_newline (&pp);
623
624 optinfo_item *item
625 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
626 xstrdup (pp_formatted_text (&pp)));
627 return item;
628}
629
8ae8ae1b 630/* Dump gimple statement GS with SPC indentation spaces and
631 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
632
633void
e43345c1 634dump_context::dump_gimple_stmt (const dump_metadata_t &metadata,
38cf91e5 635 dump_flags_t extra_dump_flags,
636 gimple *gs, int spc)
8ae8ae1b 637{
71e711b7 638 optinfo_item *item
639 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
e43345c1 640 emit_item (item, metadata.get_dump_flags ());
38cf91e5 641
642 if (optinfo_enabled_p ())
643 {
e43345c1 644 optinfo &info = ensure_pending_optinfo (metadata);
71e711b7 645 info.add_item (item);
38cf91e5 646 }
71e711b7 647 else
648 delete item;
8ae8ae1b 649}
650
651/* Similar to dump_gimple_stmt, except additionally print source location. */
652
653void
e43345c1 654dump_context::dump_gimple_stmt_loc (const dump_metadata_t &metadata,
655 const dump_user_location_t &loc,
38cf91e5 656 dump_flags_t extra_dump_flags,
657 gimple *gs, int spc)
8ae8ae1b 658{
e43345c1 659 dump_loc (metadata, loc);
660 dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
8ae8ae1b 661}
662
71e711b7 663/* Make an item for the given dump call, equivalent to print_gimple_expr. */
664
665static optinfo_item *
666make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
667{
668 dump_flags |= TDF_RHS_ONLY;
669 pretty_printer pp;
670 pp_needs_newline (&pp) = true;
671 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
672
673 optinfo_item *item
674 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
675 xstrdup (pp_formatted_text (&pp)));
676 return item;
677}
678
94982cd5 679/* Dump gimple statement GS with SPC indentation spaces and
680 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
681 Do not terminate with a newline or semicolon. */
682
683void
e43345c1 684dump_context::dump_gimple_expr (const dump_metadata_t &metadata,
38cf91e5 685 dump_flags_t extra_dump_flags,
686 gimple *gs, int spc)
94982cd5 687{
71e711b7 688 optinfo_item *item
689 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
e43345c1 690 emit_item (item, metadata.get_dump_flags ());
38cf91e5 691
692 if (optinfo_enabled_p ())
693 {
e43345c1 694 optinfo &info = ensure_pending_optinfo (metadata);
71e711b7 695 info.add_item (item);
38cf91e5 696 }
71e711b7 697 else
698 delete item;
94982cd5 699}
700
701/* Similar to dump_gimple_expr, except additionally print source location. */
702
703void
e43345c1 704dump_context::dump_gimple_expr_loc (const dump_metadata_t &metadata,
705 const dump_user_location_t &loc,
38cf91e5 706 dump_flags_t extra_dump_flags,
707 gimple *gs,
708 int spc)
94982cd5 709{
e43345c1 710 dump_loc (metadata, loc);
711 dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
94982cd5 712}
713
71e711b7 714/* Make an item for the given dump call, equivalent to print_generic_expr. */
715
716static optinfo_item *
717make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
718{
719 pretty_printer pp;
720 pp_needs_newline (&pp) = true;
721 pp_translate_identifiers (&pp) = false;
722 dump_generic_node (&pp, node, 0, dump_flags, false);
723
724 location_t loc = UNKNOWN_LOCATION;
725 if (EXPR_HAS_LOCATION (node))
726 loc = EXPR_LOCATION (node);
727
728 optinfo_item *item
729 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
730 xstrdup (pp_formatted_text (&pp)));
731 return item;
732}
94982cd5 733
8ae8ae1b 734/* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
735 DUMP_KIND is enabled. */
736
737void
e43345c1 738dump_context::dump_generic_expr (const dump_metadata_t &metadata,
38cf91e5 739 dump_flags_t extra_dump_flags,
740 tree t)
8ae8ae1b 741{
71e711b7 742 optinfo_item *item
743 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
e43345c1 744 emit_item (item, metadata.get_dump_flags ());
38cf91e5 745
746 if (optinfo_enabled_p ())
747 {
e43345c1 748 optinfo &info = ensure_pending_optinfo (metadata);
71e711b7 749 info.add_item (item);
38cf91e5 750 }
71e711b7 751 else
752 delete item;
8ae8ae1b 753}
754
f2ebf2c6 755
756/* Similar to dump_generic_expr, except additionally print the source
757 location. */
758
759void
e43345c1 760dump_context::dump_generic_expr_loc (const dump_metadata_t &metadata,
761 const dump_user_location_t &loc,
38cf91e5 762 dump_flags_t extra_dump_flags,
763 tree t)
f2ebf2c6 764{
e43345c1 765 dump_loc (metadata, loc);
766 dump_generic_expr (metadata, extra_dump_flags, t);
f2ebf2c6 767}
768
95b14393 769/* Make an item for the given dump call. */
770
771static optinfo_item *
772make_item_for_dump_symtab_node (symtab_node *node)
773{
774 location_t loc = DECL_SOURCE_LOCATION (node->decl);
775 optinfo_item *item
776 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
777 xstrdup (node->dump_name ()));
778 return item;
779}
780
bffe1cb4 781/* dump_pretty_printer's ctor. */
782
783dump_pretty_printer::dump_pretty_printer (dump_context *context,
784 dump_flags_t dump_kind)
785: pretty_printer (), m_context (context), m_dump_kind (dump_kind),
786 m_stashed_items ()
787{
788 pp_format_decoder (this) = format_decoder_cb;
789}
790
791/* Phase 3 of formatting; compare with pp_output_formatted_text.
792
793 Emit optinfo_item instances for the various formatted chunks from phases
794 1 and 2 (i.e. pp_format).
795
796 Some chunks may already have had their items built (during decode_format).
797 These chunks have been stashed into m_stashed_items; we emit them here.
798
799 For all other purely textual chunks, they are printed into
800 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
801 This consolidates multiple adjacent text chunks into a single text
802 optinfo_item. */
803
804void
805dump_pretty_printer::emit_items (optinfo *dest)
71e711b7 806{
bffe1cb4 807 output_buffer *buffer = pp_buffer (this);
808 struct chunk_info *chunk_array = buffer->cur_chunk_array;
809 const char **args = chunk_array->args;
810
811 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
812 gcc_assert (buffer->line_length == 0);
813
814 unsigned stashed_item_idx = 0;
815 for (unsigned chunk = 0; args[chunk]; chunk++)
816 {
817 if (stashed_item_idx < m_stashed_items.length ()
818 && args[chunk] == *m_stashed_items[stashed_item_idx].buffer_ptr)
819 {
820 emit_any_pending_textual_chunks (dest);
821 /* This chunk has a stashed item: use it. */
822 emit_item (m_stashed_items[stashed_item_idx++].item, dest);
823 }
824 else
825 /* This chunk is purely textual. Print it (to
826 buffer->formatted_obstack), so that we can consolidate adjacent
827 chunks into one textual optinfo_item. */
828 pp_string (this, args[chunk]);
829 }
830
831 emit_any_pending_textual_chunks (dest);
832
833 /* Ensure that we consumed all of stashed_items. */
834 gcc_assert (stashed_item_idx == m_stashed_items.length ());
835
836 /* Deallocate the chunk structure and everything after it (i.e. the
837 associated series of formatted strings). */
838 buffer->cur_chunk_array = chunk_array->prev;
839 obstack_free (&buffer->chunk_obstack, chunk_array);
840}
841
842/* Subroutine of dump_pretty_printer::emit_items
843 for consolidating multiple adjacent pure-text chunks into single
844 optinfo_items (in phase 3). */
845
846void
847dump_pretty_printer::emit_any_pending_textual_chunks (optinfo *dest)
848{
849 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
850
851 /* Don't emit an item if the pending text is empty. */
852 if (output_buffer_last_position_in_text (buffer) == NULL)
853 return;
854
855 char *formatted_text = xstrdup (pp_formatted_text (this));
71e711b7 856 optinfo_item *item
857 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
858 formatted_text);
bffe1cb4 859 emit_item (item, dest);
860
861 /* Clear the pending text by unwinding formatted_text back to the start
862 of the buffer (without deallocating). */
863 obstack_free (&buffer->formatted_obstack,
864 buffer->formatted_obstack.object_base);
71e711b7 865}
866
bffe1cb4 867/* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
868 to DEST; otherwise delete ITEM. */
71e711b7 869
bffe1cb4 870void
871dump_pretty_printer::emit_item (optinfo_item *item, optinfo *dest)
872{
873 m_context->emit_item (item, m_dump_kind);
874 if (dest)
875 dest->add_item (item);
876 else
877 delete item;
878}
71e711b7 879
bffe1cb4 880/* Record that ITEM (generated in phase 2 of formatting) is to be used for
881 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
882
883void
884dump_pretty_printer::stash_item (const char **buffer_ptr, optinfo_item *item)
71e711b7 885{
bffe1cb4 886 gcc_assert (buffer_ptr);
887 gcc_assert (item);
888
889 m_stashed_items.safe_push (stashed_item (buffer_ptr, item));
890}
891
892/* pp_format_decoder callback for dump_pretty_printer, and thus for
893 dump_printf and dump_printf_loc.
894
895 A wrapper around decode_format, for type-safety. */
896
897bool
898dump_pretty_printer::format_decoder_cb (pretty_printer *pp, text_info *text,
899 const char *spec, int /*precision*/,
900 bool /*wide*/, bool /*set_locus*/,
901 bool /*verbose*/, bool */*quoted*/,
902 const char **buffer_ptr)
903{
904 dump_pretty_printer *opp = static_cast <dump_pretty_printer *> (pp);
905 return opp->decode_format (text, spec, buffer_ptr);
906}
907
908/* Format decoder for dump_pretty_printer, and thus for dump_printf and
909 dump_printf_loc.
910
911 Supported format codes (in addition to the standard pretty_printer ones)
912 are:
913
95b14393 914 %C: cgraph_node *:
915 Equivalent to: dump_symtab_node (MSG_*, node)
bffe1cb4 916 %E: gimple *:
917 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
918 %G: gimple *:
919 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
920 %T: tree:
921 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
922
95b14393 923 TODO: add a format code that can handle (symtab_node*) *and* both
924 subclasses (presumably means teaching -Wformat about non-virtual
925 subclasses).
bffe1cb4 926
927 These format codes build optinfo_item instances, thus capturing metadata
928 about the arguments being dumped, as well as the textual output. */
929
930bool
931dump_pretty_printer::decode_format (text_info *text, const char *spec,
932 const char **buffer_ptr)
933{
934 /* Various format codes that imply making an optinfo_item and stashed it
935 for later use (to capture metadata, rather than plain text). */
936 switch (*spec)
937 {
95b14393 938 case 'C':
939 {
940 cgraph_node *node = va_arg (*text->args_ptr, cgraph_node *);
941
942 /* Make an item for the node, and stash it. */
943 optinfo_item *item = make_item_for_dump_symtab_node (node);
944 stash_item (buffer_ptr, item);
945 return true;
946 }
947
bffe1cb4 948 case 'E':
949 {
950 gimple *stmt = va_arg (*text->args_ptr, gimple *);
951
952 /* Make an item for the stmt, and stash it. */
953 optinfo_item *item = make_item_for_dump_gimple_expr (stmt, 0, TDF_SLIM);
954 stash_item (buffer_ptr, item);
955 return true;
956 }
957
958 case 'G':
959 {
960 gimple *stmt = va_arg (*text->args_ptr, gimple *);
961
962 /* Make an item for the stmt, and stash it. */
963 optinfo_item *item = make_item_for_dump_gimple_stmt (stmt, 0, TDF_SLIM);
964 stash_item (buffer_ptr, item);
965 return true;
966 }
967
968 case 'T':
969 {
970 tree t = va_arg (*text->args_ptr, tree);
971
972 /* Make an item for the tree, and stash it. */
973 optinfo_item *item = make_item_for_dump_generic_expr (t, TDF_SLIM);
974 stash_item (buffer_ptr, item);
975 return true;
976 }
977
978 default:
979 return false;
980 }
71e711b7 981}
982
8ae8ae1b 983/* Output a formatted message using FORMAT on appropriate dump streams. */
984
985void
e43345c1 986dump_context::dump_printf_va (const dump_metadata_t &metadata, const char *format,
bffe1cb4 987 va_list *ap)
8ae8ae1b 988{
e43345c1 989 dump_pretty_printer pp (this, metadata.get_dump_flags ());
38cf91e5 990
bffe1cb4 991 text_info text;
992 text.err_no = errno;
993 text.args_ptr = ap;
994 text.format_spec = format;
995
996 /* Phases 1 and 2, using pp_format. */
997 pp_format (&pp, &text);
998
999 /* Phase 3. */
38cf91e5 1000 if (optinfo_enabled_p ())
1001 {
e43345c1 1002 optinfo &info = ensure_pending_optinfo (metadata);
bffe1cb4 1003 pp.emit_items (&info);
8ae8ae1b 1004 }
71e711b7 1005 else
bffe1cb4 1006 pp.emit_items (NULL);
8ae8ae1b 1007}
1008
38cf91e5 1009/* Similar to dump_printf, except source location is also printed, and
1010 dump location captured. */
8ae8ae1b 1011
1012void
e43345c1 1013dump_context::dump_printf_loc_va (const dump_metadata_t &metadata,
1014 const dump_user_location_t &loc,
bffe1cb4 1015 const char *format, va_list *ap)
8ae8ae1b 1016{
e43345c1 1017 dump_loc (metadata, loc);
1018 dump_printf_va (metadata, format, ap);
8ae8ae1b 1019}
1020
71e711b7 1021/* Make an item for the given dump call, equivalent to print_dec. */
28eeb147 1022
1023template<unsigned int N, typename C>
71e711b7 1024static optinfo_item *
1025make_item_for_dump_dec (const poly_int<N, C> &value)
28eeb147 1026{
1027 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
1028 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
28eeb147 1029
71e711b7 1030 pretty_printer pp;
1031
1032 if (value.is_constant ())
1033 pp_wide_int (&pp, value.coeffs[0], sgn);
1034 else
1035 {
1036 pp_character (&pp, '[');
1037 for (unsigned int i = 0; i < N; ++i)
1038 {
1039 pp_wide_int (&pp, value.coeffs[i], sgn);
1040 pp_character (&pp, i == N - 1 ? ']' : ',');
1041 }
1042 }
1043
1044 optinfo_item *item
1045 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1046 xstrdup (pp_formatted_text (&pp)));
1047 return item;
1048}
1049
1050/* Output VALUE in decimal to appropriate dump streams. */
1051
1052template<unsigned int N, typename C>
1053void
e43345c1 1054dump_context::dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
71e711b7 1055{
1056 optinfo_item *item = make_item_for_dump_dec (value);
e43345c1 1057 emit_item (item, metadata.get_dump_flags ());
38cf91e5 1058
1059 if (optinfo_enabled_p ())
1060 {
e43345c1 1061 optinfo &info = ensure_pending_optinfo (metadata);
71e711b7 1062 info.add_item (item);
38cf91e5 1063 }
71e711b7 1064 else
1065 delete item;
1066}
1067
38cf91e5 1068/* Output the name of NODE on appropriate dump streams. */
1069
1070void
e43345c1 1071dump_context::dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
38cf91e5 1072{
71e711b7 1073 optinfo_item *item = make_item_for_dump_symtab_node (node);
e43345c1 1074 emit_item (item, metadata.get_dump_flags ());
38cf91e5 1075
1076 if (optinfo_enabled_p ())
1077 {
e43345c1 1078 optinfo &info = ensure_pending_optinfo (metadata);
71e711b7 1079 info.add_item (item);
38cf91e5 1080 }
71e711b7 1081 else
1082 delete item;
38cf91e5 1083}
1084
1085/* Get the current dump scope-nesting depth.
1086 For use by -fopt-info (for showing nesting via indentation). */
1087
1088unsigned int
1089dump_context::get_scope_depth () const
1090{
1091 return m_scope_depth;
1092}
1093
1094/* Push a nested dump scope.
9ddd8fa7 1095 Increment the scope depth.
38cf91e5 1096 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1097 destination, if any.
9ddd8fa7 1098 Emit a "scope" optinfo if optinfos are enabled. */
38cf91e5 1099
1100void
e43345c1 1101dump_context::begin_scope (const char *name,
1102 const dump_user_location_t &user_location,
1103 const dump_impl_location_t &impl_location)
38cf91e5 1104{
9ddd8fa7 1105 m_scope_depth++;
1106
e43345c1 1107 location_t src_loc = user_location.get_location_t ();
1108
9ddd8fa7 1109 if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
e43345c1 1110 ::dump_loc (MSG_NOTE, dump_file, src_loc);
38cf91e5 1111
9ddd8fa7 1112 if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
e43345c1 1113 ::dump_loc (MSG_NOTE, alt_dump_file, src_loc);
71e711b7 1114
1115 /* Support for temp_dump_context in selftests. */
9ddd8fa7 1116 if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
e43345c1 1117 ::dump_loc (MSG_NOTE, m_test_pp, src_loc);
71e711b7 1118
bffe1cb4 1119 pretty_printer pp;
1120 pp_printf (&pp, "=== %s ===\n", name);
1121 optinfo_item *item
1122 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1123 xstrdup (pp_formatted_text (&pp)));
71e711b7 1124 emit_item (item, MSG_NOTE);
38cf91e5 1125
1126 if (optinfo_enabled_p ())
1127 {
e43345c1 1128 optinfo &info
1129 = begin_next_optinfo (dump_metadata_t (MSG_NOTE, impl_location),
1130 user_location);
71e711b7 1131 info.m_kind = OPTINFO_KIND_SCOPE;
1132 info.add_item (item);
f2e20cc0 1133 end_any_optinfo ();
38cf91e5 1134 }
71e711b7 1135 else
1136 delete item;
38cf91e5 1137}
1138
1139/* Pop a nested dump scope. */
1140
1141void
1142dump_context::end_scope ()
1143{
1144 end_any_optinfo ();
1145 m_scope_depth--;
80d492fc 1146
1147 if (m_json_writer)
1148 m_json_writer->pop_scope ();
1149}
1150
1151/* Should optinfo instances be created?
1152 All creation of optinfos should be guarded by this predicate.
1153 Return true if any optinfo destinations are active. */
1154
1155bool
1156dump_context::optinfo_enabled_p () const
1157{
1158 return (optimization_records_enabled_p ());
38cf91e5 1159}
1160
1161/* Return the optinfo currently being accumulated, creating one if
1162 necessary. */
1163
1164optinfo &
e43345c1 1165dump_context::ensure_pending_optinfo (const dump_metadata_t &metadata)
38cf91e5 1166{
1167 if (!m_pending)
e43345c1 1168 return begin_next_optinfo (metadata, dump_user_location_t ());
38cf91e5 1169 return *m_pending;
1170}
1171
1172/* Start a new optinfo and return it, ending any optinfo that was already
1173 accumulated. */
1174
1175optinfo &
e43345c1 1176dump_context::begin_next_optinfo (const dump_metadata_t &metadata,
1177 const dump_user_location_t &user_loc)
38cf91e5 1178{
1179 end_any_optinfo ();
1180 gcc_assert (m_pending == NULL);
e43345c1 1181 dump_location_t loc (user_loc, metadata.get_impl_location ());
38cf91e5 1182 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
e43345c1 1183 m_pending->handle_dump_file_kind (metadata.get_dump_flags ());
38cf91e5 1184 return *m_pending;
1185}
1186
1187/* End any optinfo that has been accumulated within this context; emitting
9dcf2a11 1188 it to any destinations as appropriate, such as optimization records. */
38cf91e5 1189
1190void
1191dump_context::end_any_optinfo ()
1192{
1193 if (m_pending)
80d492fc 1194 emit_optinfo (m_pending);
38cf91e5 1195 delete m_pending;
1196 m_pending = NULL;
1197}
1198
80d492fc 1199/* Emit the optinfo to all of the "non-immediate" destinations
1200 (emission to "immediate" destinations is done by
1201 dump_context::emit_item). */
1202
1203void
1204dump_context::emit_optinfo (const optinfo *info)
1205{
1206 /* -fsave-optimization-record. */
1207 if (m_json_writer)
1208 m_json_writer->add_record (info);
1209}
1210
71e711b7 1211/* Emit ITEM to all item destinations (those that don't require
1212 consolidation into optinfo instances). */
1213
1214void
1215dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
1216{
9ddd8fa7 1217 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
71e711b7 1218 fprintf (dump_file, "%s", item->get_text ());
1219
9ddd8fa7 1220 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
71e711b7 1221 fprintf (alt_dump_file, "%s", item->get_text ());
1222
1223 /* Support for temp_dump_context in selftests. */
9ddd8fa7 1224 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
71e711b7 1225 pp_string (m_test_pp, item->get_text ());
1226}
1227
38cf91e5 1228/* The current singleton dump_context, and its default. */
1229
1230dump_context *dump_context::s_current = &dump_context::s_default;
1231dump_context dump_context::s_default;
1232
1233/* Implementation of dump_* API calls, calling into dump_context
1234 member functions. */
1235
91f42adc 1236/* Calls to the dump_* functions do non-trivial work, so they ought
1237 to be guarded by:
1238 if (dump_enabled_p ())
1239 Assert that they are guarded, and, if assertions are disabled,
1240 bail out if the calls weren't properly guarded. */
1241
1242#define VERIFY_DUMP_ENABLED_P \
1243 do { \
1244 gcc_assert (dump_enabled_p ()); \
1245 if (!dump_enabled_p ()) \
1246 return; \
1247 } while (0)
1248
38cf91e5 1249/* Dump gimple statement GS with SPC indentation spaces and
1250 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1251
1252void
e43345c1 1253dump_gimple_stmt (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
38cf91e5 1254 gimple *gs, int spc)
1255{
91f42adc 1256 VERIFY_DUMP_ENABLED_P;
e43345c1 1257 dump_context::get ().dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
38cf91e5 1258}
1259
1260/* Similar to dump_gimple_stmt, except additionally print source location. */
1261
1262void
e43345c1 1263dump_gimple_stmt_loc (const dump_metadata_t &metadata,
1264 const dump_user_location_t &loc,
38cf91e5 1265 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1266{
91f42adc 1267 VERIFY_DUMP_ENABLED_P;
e43345c1 1268 dump_context::get ().dump_gimple_stmt_loc (metadata, loc, extra_dump_flags,
38cf91e5 1269 gs, spc);
1270}
1271
1272/* Dump gimple statement GS with SPC indentation spaces and
1273 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1274 Do not terminate with a newline or semicolon. */
1275
1276void
e43345c1 1277dump_gimple_expr (const dump_metadata_t &metadata,
1278 dump_flags_t extra_dump_flags,
38cf91e5 1279 gimple *gs, int spc)
1280{
91f42adc 1281 VERIFY_DUMP_ENABLED_P;
e43345c1 1282 dump_context::get ().dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
38cf91e5 1283}
1284
1285/* Similar to dump_gimple_expr, except additionally print source location. */
1286
1287void
e43345c1 1288dump_gimple_expr_loc (const dump_metadata_t &metadata,
1289 const dump_user_location_t &loc,
38cf91e5 1290 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1291{
91f42adc 1292 VERIFY_DUMP_ENABLED_P;
e43345c1 1293 dump_context::get ().dump_gimple_expr_loc (metadata, loc, extra_dump_flags,
38cf91e5 1294 gs, spc);
1295}
1296
1297/* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1298 DUMP_KIND is enabled. */
1299
1300void
e43345c1 1301dump_generic_expr (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
38cf91e5 1302 tree t)
1303{
91f42adc 1304 VERIFY_DUMP_ENABLED_P;
e43345c1 1305 dump_context::get ().dump_generic_expr (metadata, extra_dump_flags, t);
38cf91e5 1306}
1307
1308/* Similar to dump_generic_expr, except additionally print the source
1309 location. */
1310
1311void
e43345c1 1312dump_generic_expr_loc (const dump_metadata_t &metadata,
1313 const dump_user_location_t &loc,
38cf91e5 1314 dump_flags_t extra_dump_flags, tree t)
1315{
91f42adc 1316 VERIFY_DUMP_ENABLED_P;
e43345c1 1317 dump_context::get ().dump_generic_expr_loc (metadata, loc, extra_dump_flags,
38cf91e5 1318 t);
1319}
1320
1321/* Output a formatted message using FORMAT on appropriate dump streams. */
1322
1323void
e43345c1 1324dump_printf (const dump_metadata_t &metadata, const char *format, ...)
38cf91e5 1325{
91f42adc 1326 VERIFY_DUMP_ENABLED_P;
38cf91e5 1327 va_list ap;
1328 va_start (ap, format);
e43345c1 1329 dump_context::get ().dump_printf_va (metadata, format, &ap);
38cf91e5 1330 va_end (ap);
1331}
1332
1333/* Similar to dump_printf, except source location is also printed, and
1334 dump location captured. */
1335
1336void
e43345c1 1337dump_printf_loc (const dump_metadata_t &metadata,
1338 const dump_user_location_t &loc,
38cf91e5 1339 const char *format, ...)
1340{
91f42adc 1341 VERIFY_DUMP_ENABLED_P;
38cf91e5 1342 va_list ap;
1343 va_start (ap, format);
e43345c1 1344 dump_context::get ().dump_printf_loc_va (metadata, loc, format, &ap);
38cf91e5 1345 va_end (ap);
1346}
1347
1348/* Output VALUE in decimal to appropriate dump streams. */
1349
1350template<unsigned int N, typename C>
1351void
e43345c1 1352dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
38cf91e5 1353{
91f42adc 1354 VERIFY_DUMP_ENABLED_P;
e43345c1 1355 dump_context::get ().dump_dec (metadata, value);
28eeb147 1356}
1357
e43345c1 1358template void dump_dec (const dump_metadata_t &metadata, const poly_uint16 &);
1359template void dump_dec (const dump_metadata_t &metadata, const poly_int64 &);
1360template void dump_dec (const dump_metadata_t &metadata, const poly_uint64 &);
1361template void dump_dec (const dump_metadata_t &metadata, const poly_offset_int &);
1362template void dump_dec (const dump_metadata_t &metadata, const poly_widest_int &);
28eeb147 1363
18bbd2f1 1364void
1365dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1366{
91f42adc 1367 VERIFY_DUMP_ENABLED_P;
9ddd8fa7 1368 if (dump_file
1369 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
18bbd2f1 1370 print_dec (value, dump_file, sgn);
1371
9ddd8fa7 1372 if (alt_dump_file
1373 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
18bbd2f1 1374 print_dec (value, alt_dump_file, sgn);
1375}
1376
1377/* Output VALUE in hexadecimal to appropriate dump streams. */
1378
1379void
1380dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1381{
91f42adc 1382 VERIFY_DUMP_ENABLED_P;
9ddd8fa7 1383 if (dump_file
1384 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
18bbd2f1 1385 print_hex (value, dump_file);
1386
9ddd8fa7 1387 if (alt_dump_file
1388 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
18bbd2f1 1389 print_hex (value, alt_dump_file);
1390}
1391
38cf91e5 1392/* Emit and delete the currently pending optinfo, if there is one,
1393 without the caller needing to know about class dump_context. */
1394
1395void
1396dumpfile_ensure_any_optinfo_are_flushed ()
1397{
1398 dump_context::get().end_any_optinfo ();
1399}
1400
1401/* Output the name of NODE on appropriate dump streams. */
b18ceb23 1402
38cf91e5 1403void
e43345c1 1404dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
38cf91e5 1405{
91f42adc 1406 VERIFY_DUMP_ENABLED_P;
e43345c1 1407 dump_context::get ().dump_symtab_node (metadata, node);
38cf91e5 1408}
b18ceb23 1409
1410/* Get the current dump scope-nesting depth.
38cf91e5 1411 For use by -fopt-info (for showing nesting via indentation). */
b18ceb23 1412
1413unsigned int
1414get_dump_scope_depth ()
1415{
38cf91e5 1416 return dump_context::get ().get_scope_depth ();
b18ceb23 1417}
1418
1419/* Push a nested dump scope.
1420 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1421 destination, if any.
38cf91e5 1422 Emit a "scope" opinfo if optinfos are enabled.
b18ceb23 1423 Increment the scope depth. */
1424
1425void
e43345c1 1426dump_begin_scope (const char *name,
1427 const dump_user_location_t &user_location,
1428 const dump_impl_location_t &impl_location)
b18ceb23 1429{
e43345c1 1430 dump_context::get ().begin_scope (name, user_location, impl_location);
b18ceb23 1431}
1432
1433/* Pop a nested dump scope. */
1434
1435void
1436dump_end_scope ()
1437{
38cf91e5 1438 dump_context::get ().end_scope ();
b18ceb23 1439}
1440
8ae8ae1b 1441/* Start a dump for PHASE. Store user-supplied dump flags in
1442 *FLAG_PTR. Return the number of streams opened. Set globals
1443 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
c7875731 1444 set dump_flags appropriately for both pass dump stream and
1445 -fopt-info stream. */
8ae8ae1b 1446
1447int
41142c53 1448gcc::dump_manager::
3f6e5ced 1449dump_start (int phase, dump_flags_t *flag_ptr)
8ae8ae1b 1450{
1451 int count = 0;
1452 char *name;
1453 struct dump_file_info *dfi;
1454 FILE *stream;
e739f874 1455 if (phase == TDI_none || !dump_phase_enabled_p (phase))
8ae8ae1b 1456 return 0;
1457
1458 dfi = get_dump_file_info (phase);
1459 name = get_dump_file_name (phase);
1460 if (name)
1461 {
0c124b17 1462 stream = dump_open (name, dfi->pstate < 0);
1463 if (stream)
8ae8ae1b 1464 {
1465 dfi->pstate = 1;
1466 count++;
1467 }
1468 free (name);
1469 dfi->pstream = stream;
7fa3c46f 1470 set_dump_file (dfi->pstream);
8ae8ae1b 1471 /* Initialize current dump flags. */
ffdaf8f1 1472 pflags = dfi->pflags;
8ae8ae1b 1473 }
1474
1475 stream = dump_open_alternate_stream (dfi);
1476 if (stream)
1477 {
1478 dfi->alt_stream = stream;
1479 count++;
7fa3c46f 1480 set_alt_dump_file (dfi->alt_stream);
c7875731 1481 /* Initialize current -fopt-info flags. */
ffdaf8f1 1482 alt_flags = dfi->alt_flags;
8ae8ae1b 1483 }
1484
1485 if (flag_ptr)
1486 *flag_ptr = dfi->pflags;
1487
1488 return count;
1489}
1490
1491/* Finish a tree dump for PHASE and close associated dump streams. Also
1492 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1493
1494void
41142c53 1495gcc::dump_manager::
8ae8ae1b 1496dump_finish (int phase)
1497{
1498 struct dump_file_info *dfi;
1499
1500 if (phase < 0)
1501 return;
1502 dfi = get_dump_file_info (phase);
0c124b17 1503 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
8ae8ae1b 1504 fclose (dfi->pstream);
1505
0c124b17 1506 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
8ae8ae1b 1507 fclose (dfi->alt_stream);
1508
1509 dfi->alt_stream = NULL;
1510 dfi->pstream = NULL;
7fa3c46f 1511 set_dump_file (NULL);
1512 set_alt_dump_file (NULL);
54e7de93 1513 dump_flags = TDF_NONE;
1514 alt_flags = TDF_NONE;
1515 pflags = TDF_NONE;
8ae8ae1b 1516}
1517
1518/* Begin a tree dump for PHASE. Stores any user supplied flag in
1519 *FLAG_PTR and returns a stream to write to. If the dump is not
1520 enabled, returns NULL.
96854265 1521 PART can be used for dump files which should be split to multiple
1522 parts. PART == -1 indicates dump file with no parts.
1523 If PART is -1, multiple calls will reopen and append to the dump file. */
8ae8ae1b 1524
1525FILE *
96854265 1526dump_begin (int phase, dump_flags_t *flag_ptr, int part)
41142c53 1527{
96854265 1528 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
41142c53 1529}
1530
1531FILE *
1532gcc::dump_manager::
96854265 1533dump_begin (int phase, dump_flags_t *flag_ptr, int part)
8ae8ae1b 1534{
1535 char *name;
1536 struct dump_file_info *dfi;
1537 FILE *stream;
1538
e739f874 1539 if (phase == TDI_none || !dump_phase_enabled_p (phase))
8ae8ae1b 1540 return NULL;
1541
96854265 1542 name = get_dump_file_name (phase, part);
8ae8ae1b 1543 if (!name)
1544 return NULL;
1545 dfi = get_dump_file_info (phase);
1546
96854265 1547 /* We do not support re-opening of dump files with parts. This would require
1548 tracking pstate per part of the dump file. */
1549 stream = dump_open (name, part != -1 || dfi->pstate < 0);
0c124b17 1550 if (stream)
8ae8ae1b 1551 dfi->pstate = 1;
1552 free (name);
1553
1554 if (flag_ptr)
1555 *flag_ptr = dfi->pflags;
1556
1557 /* Initialize current flags */
1558 pflags = dfi->pflags;
1559 return stream;
1560}
1561
1562/* Returns nonzero if dump PHASE is enabled for at least one stream.
1563 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1564 any phase. */
1565
41142c53 1566int
1567gcc::dump_manager::
1568dump_phase_enabled_p (int phase) const
8ae8ae1b 1569{
1570 if (phase == TDI_tree_all)
1571 {
1572 size_t i;
1573 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1574 if (dump_files[i].pstate || dump_files[i].alt_state)
1575 return 1;
41142c53 1576 for (i = 0; i < m_extra_dump_files_in_use; i++)
1577 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
8ae8ae1b 1578 return 1;
1579 return 0;
1580 }
1581 else
1582 {
1583 struct dump_file_info *dfi = get_dump_file_info (phase);
1584 return dfi->pstate || dfi->alt_state;
1585 }
1586}
1587
1588/* Returns nonzero if tree dump PHASE has been initialized. */
1589
1590int
41142c53 1591gcc::dump_manager::
1592dump_initialized_p (int phase) const
8ae8ae1b 1593{
1594 struct dump_file_info *dfi = get_dump_file_info (phase);
1595 return dfi->pstate > 0 || dfi->alt_state > 0;
1596}
1597
1598/* Returns the switch name of PHASE. */
1599
1600const char *
1601dump_flag_name (int phase)
41142c53 1602{
1603 return g->get_dumps ()->dump_flag_name (phase);
1604}
1605
1606const char *
1607gcc::dump_manager::
1608dump_flag_name (int phase) const
8ae8ae1b 1609{
1610 struct dump_file_info *dfi = get_dump_file_info (phase);
1611 return dfi->swtch;
1612}
1613
e81a6963 1614/* Handle -fdump-* and -fopt-info for a pass added after
1615 command-line options are parsed (those from plugins and
1616 those from backends).
1617
1618 Because the registration of plugin/backend passes happens after the
1619 command-line options are parsed, the options that specify single
1620 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1621 passes. Therefore we currently can only enable dumping of
1622 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1623 are specified. This is done here.
1624
1625 Similarly, the saved -fopt-info options are wired up to the new pass. */
1626
1627void
1628gcc::dump_manager::register_pass (opt_pass *pass)
1629{
1630 gcc_assert (pass);
1631
1632 register_one_dump_file (pass);
1633
1634 dump_file_info *pass_dfi = get_dump_file_info (pass->static_pass_number);
1635 gcc_assert (pass_dfi);
1636
1637 enum tree_dump_index tdi;
1638 if (pass->type == SIMPLE_IPA_PASS
1639 || pass->type == IPA_PASS)
1640 tdi = TDI_ipa_all;
1641 else if (pass->type == GIMPLE_PASS)
1642 tdi = TDI_tree_all;
1643 else
1644 tdi = TDI_rtl_all;
1645 const dump_file_info *tdi_dfi = get_dump_file_info (tdi);
1646 gcc_assert (tdi_dfi);
1647
1648 /* Check if dump-all flag is specified. */
1649 if (tdi_dfi->pstate)
1650 {
1651 pass_dfi->pstate = tdi_dfi->pstate;
1652 pass_dfi->pflags = tdi_dfi->pflags;
1653 }
1654
1655 update_dfi_for_opt_info (pass_dfi);
1656}
1657
8ae8ae1b 1658/* Finish a tree dump for PHASE. STREAM is the stream created by
1659 dump_begin. */
1660
1661void
1662dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1663{
1664 if (stream != stderr && stream != stdout)
1665 fclose (stream);
1666}
1667
1668/* Enable all tree dumps with FLAGS on FILENAME. Return number of
1669 enabled tree dumps. */
1670
41142c53 1671int
1672gcc::dump_manager::
ffdaf8f1 1673dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
8ae8ae1b 1674{
8ae8ae1b 1675 int n = 0;
1676 size_t i;
1677
1678 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1679 {
f8e5c21b 1680 if (dump_files[i].dkind == dkind)
8ae8ae1b 1681 {
1682 const char *old_filename = dump_files[i].pfilename;
1683 dump_files[i].pstate = -1;
1684 dump_files[i].pflags |= flags;
1685 n++;
1686 /* Override the existing filename. */
1687 if (filename)
1688 {
1689 dump_files[i].pfilename = xstrdup (filename);
1690 /* Since it is a command-line provided file, which is
1691 common to all the phases, use it in append mode. */
1692 dump_files[i].pstate = 1;
1693 }
1694 if (old_filename && filename != old_filename)
1695 free (CONST_CAST (char *, old_filename));
1696 }
1697 }
1698
41142c53 1699 for (i = 0; i < m_extra_dump_files_in_use; i++)
8ae8ae1b 1700 {
f8e5c21b 1701 if (m_extra_dump_files[i].dkind == dkind)
8ae8ae1b 1702 {
41142c53 1703 const char *old_filename = m_extra_dump_files[i].pfilename;
1704 m_extra_dump_files[i].pstate = -1;
1705 m_extra_dump_files[i].pflags |= flags;
8ae8ae1b 1706 n++;
1707 /* Override the existing filename. */
1708 if (filename)
1709 {
41142c53 1710 m_extra_dump_files[i].pfilename = xstrdup (filename);
8ae8ae1b 1711 /* Since it is a command-line provided file, which is
1712 common to all the phases, use it in append mode. */
41142c53 1713 m_extra_dump_files[i].pstate = 1;
8ae8ae1b 1714 }
1715 if (old_filename && filename != old_filename)
1716 free (CONST_CAST (char *, old_filename));
1717 }
1718 }
1719
1720 return n;
1721}
1722
c7875731 1723/* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1724 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1725 dumps. */
8ae8ae1b 1726
41142c53 1727int
1728gcc::dump_manager::
54e7de93 1729opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
3f6e5ced 1730 const char *filename)
8ae8ae1b 1731{
1732 int n = 0;
8ae8ae1b 1733
e81a6963 1734 m_optgroup_flags = optgroup_flags;
1735 m_optinfo_flags = flags;
1736 m_optinfo_filename = xstrdup (filename);
8ae8ae1b 1737
e81a6963 1738 for (size_t i = TDI_none + 1; i < (size_t) TDI_end; i++)
1739 if (update_dfi_for_opt_info (&dump_files[i]))
1740 n++;
1741
1742 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1743 if (update_dfi_for_opt_info (&m_extra_dump_files[i]))
1744 n++;
8ae8ae1b 1745
1746 return n;
1747}
1748
e81a6963 1749/* Use the saved -fopt-info options to update DFI.
1750 Return true if the dump is enabled. */
1751
1752bool
1753gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
1754{
1755 gcc_assert (dfi);
1756
1757 if (!(dfi->optgroup_flags & m_optgroup_flags))
1758 return false;
1759
1760 const char *old_filename = dfi->alt_filename;
1761 /* Since this file is shared among different passes, it
1762 should be opened in append mode. */
1763 dfi->alt_state = 1;
1764 dfi->alt_flags |= m_optinfo_flags;
1765 /* Override the existing filename. */
1766 if (m_optinfo_filename)
1767 dfi->alt_filename = xstrdup (m_optinfo_filename);
1768 if (old_filename && m_optinfo_filename != old_filename)
1769 free (CONST_CAST (char *, old_filename));
1770
1771 return true;
1772}
1773
b340d73f 1774/* Helper routine to parse -<dump format>[=filename]
1775 and return the corresponding dump flag. If POS_P is non-NULL,
1776 assign start of filename into *POS_P. */
8ae8ae1b 1777
b340d73f 1778dump_flags_t
1779parse_dump_option (const char *option_value, const char **pos_p)
8ae8ae1b 1780{
8ae8ae1b 1781 const char *ptr;
3f6e5ced 1782 dump_flags_t flags;
8ae8ae1b 1783
8ae8ae1b 1784 ptr = option_value;
b340d73f 1785 if (pos_p)
1786 *pos_p = NULL;
ed9370cc 1787
1788 /* Retain "user-facing" and "internals" messages, but filter out
1789 those from an opt_problem being re-emitted at the top level
1790 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1791 messing up scan-tree-dump-times" in DejaGnu tests. */
1792 flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_INTERNALS;
8ae8ae1b 1793
1794 while (*ptr)
1795 {
54e7de93 1796 const struct kv_pair<dump_flags_t> *option_ptr;
8ae8ae1b 1797 const char *end_ptr;
1798 const char *eq_ptr;
1799 unsigned length;
8ae8ae1b 1800 while (*ptr == '-')
1801 ptr++;
1802 end_ptr = strchr (ptr, '-');
1803 eq_ptr = strchr (ptr, '=');
1804
1805 if (eq_ptr && !end_ptr)
b340d73f 1806 end_ptr = eq_ptr;
8ae8ae1b 1807
1808 if (!end_ptr)
1809 end_ptr = ptr + strlen (ptr);
1810 length = end_ptr - ptr;
1811
1812 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1813 if (strlen (option_ptr->name) == length
1814 && !memcmp (option_ptr->name, ptr, length))
b340d73f 1815 {
1816 flags |= option_ptr->value;
8ae8ae1b 1817 goto found;
b340d73f 1818 }
8ae8ae1b 1819
1820 if (*ptr == '=')
b340d73f 1821 {
8ae8ae1b 1822 /* Interpret rest of the argument as a dump filename. This
1823 filename overrides other command line filenames. */
b340d73f 1824 if (pos_p)
1825 *pos_p = ptr + 1;
1826 break;
1827 }
8ae8ae1b 1828 else
b340d73f 1829 {
1830 warning (0, "ignoring unknown option %q.*s",
1831 length, ptr);
1832 flags = TDF_ERROR;
1833 }
1834 found:
8ae8ae1b 1835 ptr = end_ptr;
b340d73f 1836 }
1837
1838 return flags;
1839}
1840
1841/* Parse ARG as a dump switch. Return nonzero if it is, and store the
1842 relevant details in the dump_files array. */
1843
1844int
1845gcc::dump_manager::
1846dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1847{
1848 const char *option_value;
1849 dump_flags_t flags = TDF_NONE;
1850
1851 if (doglob && !dfi->glob)
1852 return 0;
1853
1854 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1855 if (!option_value)
1856 return 0;
1857
1858 if (*option_value && *option_value != '-' && *option_value != '=')
1859 return 0;
1860
1861 const char *filename;
1862 flags = parse_dump_option (option_value, &filename);
1863 if (filename)
1864 {
1865 if (dfi->pfilename)
1866 free (CONST_CAST (char *, dfi->pfilename));
1867 dfi->pfilename = xstrdup (filename);
8ae8ae1b 1868 }
1869
1870 dfi->pstate = -1;
1871 dfi->pflags |= flags;
1872
1873 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1874 known dumps. */
1875 if (dfi->suffix == NULL)
ffdaf8f1 1876 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
8ae8ae1b 1877
1878 return 1;
1879}
1880
1881int
41142c53 1882gcc::dump_manager::
8ae8ae1b 1883dump_switch_p (const char *arg)
1884{
1885 size_t i;
1886 int any = 0;
1887
1888 for (i = TDI_none + 1; i != TDI_end; i++)
1889 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1890
1891 /* Don't glob if we got a hit already */
1892 if (!any)
1893 for (i = TDI_none + 1; i != TDI_end; i++)
1894 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1895
41142c53 1896 for (i = 0; i < m_extra_dump_files_in_use; i++)
1897 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
8ae8ae1b 1898
1899 if (!any)
41142c53 1900 for (i = 0; i < m_extra_dump_files_in_use; i++)
1901 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
8ae8ae1b 1902
1903
1904 return any;
1905}
1906
c7875731 1907/* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1908 and filename. Return non-zero if it is a recognized switch. */
8ae8ae1b 1909
1910static int
54e7de93 1911opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1912 optgroup_flags_t *optgroup_flags, char **filename)
8ae8ae1b 1913{
1914 const char *option_value;
1915 const char *ptr;
1916
1917 option_value = arg;
1918 ptr = option_value;
1919
1920 *filename = NULL;
9ddd8fa7 1921
1922 /* Default to filtering out "internals" messages, and retaining
ed9370cc 1923 "user-facing" messages, and those from an opt_problem being
1924 re-emitted at the top level. */
1925 *flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_REEMITTED;
9ddd8fa7 1926
54e7de93 1927 *optgroup_flags = OPTGROUP_NONE;
8ae8ae1b 1928
1929 if (!ptr)
c7875731 1930 return 1; /* Handle '-fopt-info' without any additional options. */
8ae8ae1b 1931
1932 while (*ptr)
1933 {
8ae8ae1b 1934 const char *end_ptr;
1935 const char *eq_ptr;
1936 unsigned length;
1937
1938 while (*ptr == '-')
1939 ptr++;
1940 end_ptr = strchr (ptr, '-');
1941 eq_ptr = strchr (ptr, '=');
1942
99e4092d 1943 if (eq_ptr && (!end_ptr || eq_ptr < end_ptr))
8ae8ae1b 1944 end_ptr = eq_ptr;
99e4092d 1945 else if (!end_ptr)
8ae8ae1b 1946 end_ptr = ptr + strlen (ptr);
1947 length = end_ptr - ptr;
1948
54e7de93 1949 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1950 option_ptr->name; option_ptr++)
8ae8ae1b 1951 if (strlen (option_ptr->name) == length
1952 && !memcmp (option_ptr->name, ptr, length))
1953 {
1954 *flags |= option_ptr->value;
1955 goto found;
1956 }
1957
54e7de93 1958 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1959 option_ptr->name; option_ptr++)
c7875731 1960 if (strlen (option_ptr->name) == length
1961 && !memcmp (option_ptr->name, ptr, length))
1962 {
1963 *optgroup_flags |= option_ptr->value;
1964 goto found;
1965 }
1966
8ae8ae1b 1967 if (*ptr == '=')
1968 {
1969 /* Interpret rest of the argument as a dump filename. This
1970 filename overrides other command line filenames. */
1971 *filename = xstrdup (ptr + 1);
1972 break;
1973 }
1974 else
c7875731 1975 {
1976 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1977 length, ptr, arg);
1978 return 0;
1979 }
8ae8ae1b 1980 found:;
1981 ptr = end_ptr;
1982 }
1983
1984 return 1;
1985}
1986
1987/* Return non-zero if ARG is a recognized switch for
1988 -fopt-info. Return zero otherwise. */
1989
1990int
1991opt_info_switch_p (const char *arg)
1992{
3f6e5ced 1993 dump_flags_t flags;
54e7de93 1994 optgroup_flags_t optgroup_flags;
8ae8ae1b 1995 char *filename;
c7875731 1996 static char *file_seen = NULL;
41142c53 1997 gcc::dump_manager *dumps = g->get_dumps ();
8ae8ae1b 1998
c7875731 1999 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
2000 return 0;
8ae8ae1b 2001
2002 if (!filename)
2003 filename = xstrdup ("stderr");
c7875731 2004
2005 /* Bail out if a different filename has been specified. */
2006 if (file_seen && strcmp (file_seen, filename))
2007 {
2008 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
2009 arg);
2010 return 1;
2011 }
2012
2013 file_seen = xstrdup (filename);
9ddd8fa7 2014 if (!(flags & MSG_ALL_KINDS))
2015 flags |= MSG_OPTIMIZED_LOCATIONS;
c7875731 2016 if (!optgroup_flags)
2017 optgroup_flags = OPTGROUP_ALL;
8ae8ae1b 2018
41142c53 2019 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
8ae8ae1b 2020}
2021
8ae8ae1b 2022/* Print basic block on the dump streams. */
2023
2024void
54e7de93 2025dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
8ae8ae1b 2026{
9ddd8fa7 2027 if (dump_file
2028 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
8ae8ae1b 2029 dump_bb (dump_file, bb, indent, TDF_DETAILS);
9ddd8fa7 2030 if (alt_dump_file
2031 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
8ae8ae1b 2032 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
2033}
2034
930c75f4 2035/* Dump FUNCTION_DECL FN as tree dump PHASE. */
2036
2037void
2038dump_function (int phase, tree fn)
2039{
2040 FILE *stream;
3f6e5ced 2041 dump_flags_t flags;
930c75f4 2042
2043 stream = dump_begin (phase, &flags);
2044 if (stream)
2045 {
2046 dump_function_to_file (fn, stream, flags);
2047 dump_end (phase, stream);
2048 }
2049}
2050
8ae8ae1b 2051/* Print information from the combine pass on dump_file. */
2052
2053void
2054print_combine_total_stats (void)
2055{
2056 if (dump_file)
2057 dump_combine_total_stats (dump_file);
2058}
2059
2060/* Enable RTL dump for all the RTL passes. */
2061
2062bool
2063enable_rtl_dump_file (void)
2064{
41142c53 2065 gcc::dump_manager *dumps = g->get_dumps ();
2066 int num_enabled =
ffdaf8f1 2067 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
2068 NULL);
41142c53 2069 return num_enabled > 0;
8ae8ae1b 2070}
c309657f 2071
2072#if CHECKING_P
2073
ed9370cc 2074namespace selftest {
2075
38cf91e5 2076/* temp_dump_context's ctor. Temporarily override the dump_context
2077 (to forcibly enable optinfo-generation). */
2078
71e711b7 2079temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
ed9370cc 2080 bool forcibly_enable_dumping,
71e711b7 2081 dump_flags_t test_pp_flags)
38cf91e5 2082: m_context (),
2083 m_saved (&dump_context ().get ())
2084{
2085 dump_context::s_current = &m_context;
80d492fc 2086 if (forcibly_enable_optinfo)
2087 m_context.set_json_writer (new optrecord_json_writer ());
ed9370cc 2088 /* Conditionally enable the test dump, so that we can verify both the
2089 dump_enabled_p and the !dump_enabled_p cases in selftests. */
2090 if (forcibly_enable_dumping)
2091 {
2092 m_context.m_test_pp = &m_pp;
2093 m_context.m_test_pp_flags = test_pp_flags;
2094 }
71e711b7 2095
2096 dump_context::get ().refresh_dumps_are_enabled ();
38cf91e5 2097}
2098
2099/* temp_dump_context's dtor. Restore the saved dump_context. */
2100
2101temp_dump_context::~temp_dump_context ()
2102{
80d492fc 2103 m_context.set_json_writer (NULL);
2104
38cf91e5 2105 dump_context::s_current = m_saved;
71e711b7 2106
2107 dump_context::get ().refresh_dumps_are_enabled ();
2108}
2109
2110/* 0-terminate the text dumped so far, and return it. */
2111
2112const char *
2113temp_dump_context::get_dumped_text ()
2114{
2115 return pp_formatted_text (&m_pp);
38cf91e5 2116}
2117
e43345c1 2118/* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2119 from EXPECTED_FUNCTION, using LOC for the location of any failure,
2120 provided that the build compiler is sufficiently recent. */
2121
2122static void
2123assert_impl_location_eq (const location &loc ATTRIBUTE_UNUSED,
2124 const dump_impl_location_t &impl_loc ATTRIBUTE_UNUSED,
2125 const char *expected_file ATTRIBUTE_UNUSED,
2126 int expected_line ATTRIBUTE_UNUSED,
2127 const char *expected_function ATTRIBUTE_UNUSED)
2128{
2129#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2130 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_file, expected_file);
2131 ASSERT_EQ_AT (loc, impl_loc.m_line, expected_line);
2132 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_function, expected_function);
2133#endif
2134}
2135
2136/* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2137 from EXPECTED_FUNCTION, provided that the build compiler is
2138 sufficiently recent. */
2139
2140#define ASSERT_IMPL_LOCATION_EQ(IMPL_LOC, EXPECTED_FILE, EXPECTED_LINE, \
2141 EXPECTED_FUNCTION) \
2142 SELFTEST_BEGIN_STMT \
2143 assert_impl_location_eq (SELFTEST_LOCATION, IMPL_LOC, \
2144 EXPECTED_FILE, EXPECTED_LINE, \
2145 EXPECTED_FUNCTION); \
2146 SELFTEST_END_STMT
2147
c309657f 2148/* Verify that the dump_location_t constructors capture the source location
2149 at which they were called (provided that the build compiler is sufficiently
2150 recent). */
2151
2152static void
2153test_impl_location ()
2154{
c309657f 2155 /* Default ctor. */
2156 {
2157 dump_location_t loc;
2158 const int expected_line = __LINE__ - 1;
e43345c1 2159 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2160 "dumpfile.c", expected_line, "test_impl_location");
c309657f 2161 }
2162
2163 /* Constructing from a gimple. */
2164 {
2165 dump_location_t loc ((gimple *)NULL);
2166 const int expected_line = __LINE__ - 1;
e43345c1 2167 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2168 "dumpfile.c", expected_line, "test_impl_location");
c309657f 2169 }
2170
2171 /* Constructing from an rtx_insn. */
2172 {
2173 dump_location_t loc ((rtx_insn *)NULL);
2174 const int expected_line = __LINE__ - 1;
e43345c1 2175 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2176 "dumpfile.c", expected_line, "test_impl_location");
c309657f 2177 }
c309657f 2178}
2179
71e711b7 2180/* Verify that the text dumped so far in CONTEXT equals
2181 EXPECTED_TEXT, using LOC for the location of any failure.
2182 As a side-effect, the internal buffer is 0-terminated. */
2183
ed9370cc 2184void
71e711b7 2185verify_dumped_text (const location &loc,
2186 temp_dump_context *context,
2187 const char *expected_text)
2188{
2189 gcc_assert (context);
2190 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
2191 expected_text);
2192}
2193
38cf91e5 2194/* Verify that ITEM has the expected values. */
2195
ed9370cc 2196void
38cf91e5 2197verify_item (const location &loc,
2198 const optinfo_item *item,
2199 enum optinfo_item_kind expected_kind,
2200 location_t expected_location,
2201 const char *expected_text)
2202{
2203 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
2204 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
2205 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
2206}
2207
38cf91e5 2208/* Verify that calls to the dump_* API are captured and consolidated into
2209 optimization records. */
2210
2211static void
2212test_capture_of_dump_calls (const line_table_case &case_)
2213{
2214 /* Generate a location_t for testing. */
2215 line_table_test ltt (case_);
2216 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
2217 linemap_line_start (line_table, 5, 100);
2218 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
95b14393 2219 location_t decl_loc = linemap_position_for_column (line_table, 8);
2220 location_t stmt_loc = linemap_position_for_column (line_table, 10);
2221 if (stmt_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
71e711b7 2222 return;
38cf91e5 2223
e43345c1 2224 dump_user_location_t loc = dump_user_location_t::from_location_t (stmt_loc);
38cf91e5 2225
bffe1cb4 2226 gimple *stmt = gimple_build_return (NULL);
95b14393 2227 gimple_set_location (stmt, stmt_loc);
38cf91e5 2228
95b14393 2229 tree test_decl = build_decl (decl_loc, FUNCTION_DECL,
bffe1cb4 2230 get_identifier ("test_decl"),
95b14393 2231 build_function_type_list (void_type_node,
2232 NULL_TREE));
2233
2234 symbol_table_test tmp_symtab;
2235
2236 cgraph_node *node = cgraph_node::get_create (test_decl);
2237 gcc_assert (node);
2238
71e711b7 2239 /* Run all tests twice, with and then without optinfo enabled, to ensure
2240 that immediate destinations vs optinfo-based destinations both
2241 work, independently of each other, with no leaks. */
2242 for (int i = 0 ; i < 2; i++)
38cf91e5 2243 {
71e711b7 2244 bool with_optinfo = (i == 0);
2245
2246 /* Test of dump_printf. */
2247 {
ed9370cc 2248 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2249 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2250 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
e43345c1 2251 const int expected_impl_line = __LINE__ - 1;
71e711b7 2252
2253 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
2254 if (with_optinfo)
2255 {
2256 optinfo *info = tmp.get_pending_optinfo ();
2257 ASSERT_TRUE (info != NULL);
2258 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2259 ASSERT_EQ (info->num_items (), 1);
2260 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
e43345c1 2261 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2262 "dumpfile.c", expected_impl_line,
2263 "test_capture_of_dump_calls");
71e711b7 2264 }
2265 }
2266
bffe1cb4 2267 /* Test of dump_printf with %T. */
2268 {
ed9370cc 2269 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2270 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
bffe1cb4 2271 dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
e43345c1 2272 const int expected_impl_line = __LINE__ - 1;
bffe1cb4 2273
2274 ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
2275 if (with_optinfo)
2276 {
2277 optinfo *info = tmp.get_pending_optinfo ();
2278 ASSERT_TRUE (info != NULL);
2279 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2280 ASSERT_EQ (info->num_items (), 2);
2281 ASSERT_IS_TEXT (info->get_item (0), "tree: ");
2282 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
e43345c1 2283 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2284 "dumpfile.c", expected_impl_line,
2285 "test_capture_of_dump_calls");
bffe1cb4 2286 }
2287 }
2288
2289 /* Test of dump_printf with %E. */
2290 {
ed9370cc 2291 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2292 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
bffe1cb4 2293 dump_printf (MSG_NOTE, "gimple: %E", stmt);
e43345c1 2294 const int expected_impl_line = __LINE__ - 1;
bffe1cb4 2295
2296 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
2297 if (with_optinfo)
2298 {
2299 optinfo *info = tmp.get_pending_optinfo ();
2300 ASSERT_TRUE (info != NULL);
2301 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2302 ASSERT_EQ (info->num_items (), 2);
2303 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
95b14393 2304 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;");
e43345c1 2305 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2306 "dumpfile.c", expected_impl_line,
2307 "test_capture_of_dump_calls");
bffe1cb4 2308 }
2309 }
2310
2311 /* Test of dump_printf with %G. */
2312 {
ed9370cc 2313 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2314 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
bffe1cb4 2315 dump_printf (MSG_NOTE, "gimple: %G", stmt);
e43345c1 2316 const int expected_impl_line = __LINE__ - 1;
bffe1cb4 2317
2318 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
2319 if (with_optinfo)
2320 {
2321 optinfo *info = tmp.get_pending_optinfo ();
2322 ASSERT_TRUE (info != NULL);
2323 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2324 ASSERT_EQ (info->num_items (), 2);
2325 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
95b14393 2326 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;\n");
e43345c1 2327 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2328 "dumpfile.c", expected_impl_line,
2329 "test_capture_of_dump_calls");
95b14393 2330 }
2331 }
2332
2333 /* Test of dump_printf with %C. */
2334 {
2335 temp_dump_context tmp (with_optinfo, true,
2336 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2337 dump_printf (MSG_NOTE, "node: %C", node);
e43345c1 2338 const int expected_impl_line = __LINE__ - 1;
95b14393 2339
2340 ASSERT_DUMPED_TEXT_EQ (tmp, "node: test_decl/0");
2341 if (with_optinfo)
2342 {
2343 optinfo *info = tmp.get_pending_optinfo ();
2344 ASSERT_TRUE (info != NULL);
2345 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2346 ASSERT_EQ (info->num_items (), 2);
2347 ASSERT_IS_TEXT (info->get_item (0), "node: ");
2348 ASSERT_IS_SYMTAB_NODE (info->get_item (1), decl_loc, "test_decl/0");
e43345c1 2349 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2350 "dumpfile.c", expected_impl_line,
2351 "test_capture_of_dump_calls");
bffe1cb4 2352 }
2353 }
2354
2355 /* dump_print_loc with multiple format codes. This tests various
2356 things:
2357 - intermingling of text, format codes handled by the base
2358 pretty_printer, and dump-specific format codes
2359 - multiple dump-specific format codes: some consecutive, others
2360 separated by text, trailing text after the final one. */
2361 {
ed9370cc 2362 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2363 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
bffe1cb4 2364 dump_printf_loc (MSG_NOTE, loc, "before %T and %T"
2365 " %i consecutive %E%E after\n",
2366 integer_zero_node, test_decl, 42, stmt, stmt);
2367
2368 ASSERT_DUMPED_TEXT_EQ (tmp,
2369 "test.txt:5:10: note: before 0 and test_decl"
2370 " 42 consecutive return;return; after\n");
2371 if (with_optinfo)
2372 {
2373 optinfo *info = tmp.get_pending_optinfo ();
2374 ASSERT_TRUE (info != NULL);
2375 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2376 ASSERT_EQ (info->num_items (), 8);
2377 ASSERT_IS_TEXT (info->get_item (0), "before ");
2378 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2379 ASSERT_IS_TEXT (info->get_item (2), " and ");
2380 ASSERT_IS_TREE (info->get_item (3), UNKNOWN_LOCATION, "test_decl");
2381 ASSERT_IS_TEXT (info->get_item (4), " 42 consecutive ");
95b14393 2382 ASSERT_IS_GIMPLE (info->get_item (5), stmt_loc, "return;");
2383 ASSERT_IS_GIMPLE (info->get_item (6), stmt_loc, "return;");
bffe1cb4 2384 ASSERT_IS_TEXT (info->get_item (7), " after\n");
e43345c1 2385 /* We don't ASSERT_IMPL_LOCATION_EQ here, to avoid having to
2386 enforce at which exact line the multiline dump_printf_loc
2387 occurred. */
bffe1cb4 2388 }
2389 }
2390
71e711b7 2391 /* Tree, via dump_generic_expr. */
2392 {
ed9370cc 2393 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2394 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2395 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
e43345c1 2396 const int expected_impl_line = __LINE__ - 1;
71e711b7 2397 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
2398
2399 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
2400 if (with_optinfo)
2401 {
2402 optinfo *info = tmp.get_pending_optinfo ();
2403 ASSERT_TRUE (info != NULL);
95b14393 2404 ASSERT_EQ (info->get_location_t (), stmt_loc);
71e711b7 2405 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2406 ASSERT_EQ (info->num_items (), 2);
2407 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
2408 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
e43345c1 2409 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2410 "dumpfile.c", expected_impl_line,
2411 "test_capture_of_dump_calls");
71e711b7 2412 }
2413 }
2414
2415 /* Tree, via dump_generic_expr_loc. */
2416 {
ed9370cc 2417 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2418 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2419 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
e43345c1 2420 const int expected_impl_line = __LINE__ - 1;
71e711b7 2421
2422 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
2423 if (with_optinfo)
2424 {
2425 optinfo *info = tmp.get_pending_optinfo ();
2426 ASSERT_TRUE (info != NULL);
95b14393 2427 ASSERT_EQ (info->get_location_t (), stmt_loc);
71e711b7 2428 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2429 ASSERT_EQ (info->num_items (), 1);
2430 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
e43345c1 2431 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2432 "dumpfile.c", expected_impl_line,
2433 "test_capture_of_dump_calls");
71e711b7 2434 }
2435 }
2436
2437 /* Gimple. */
2438 {
2439 /* dump_gimple_stmt_loc. */
2440 {
ed9370cc 2441 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2442 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2443 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
e43345c1 2444 const int expected_impl_line = __LINE__ - 1;
71e711b7 2445
2446 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
2447 if (with_optinfo)
2448 {
2449 optinfo *info = tmp.get_pending_optinfo ();
2450 ASSERT_TRUE (info != NULL);
2451 ASSERT_EQ (info->num_items (), 1);
95b14393 2452 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
e43345c1 2453 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2454 "dumpfile.c", expected_impl_line,
2455 "test_capture_of_dump_calls");
71e711b7 2456 }
2457 }
38cf91e5 2458
71e711b7 2459 /* dump_gimple_stmt. */
2460 {
ed9370cc 2461 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2462 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2463 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
e43345c1 2464 const int expected_impl_line = __LINE__ - 1;
71e711b7 2465
2466 ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
2467 if (with_optinfo)
2468 {
2469 optinfo *info = tmp.get_pending_optinfo ();
2470 ASSERT_TRUE (info != NULL);
2471 ASSERT_EQ (info->num_items (), 1);
95b14393 2472 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
e43345c1 2473 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2474 "dumpfile.c", expected_impl_line,
2475 "test_capture_of_dump_calls");
71e711b7 2476 }
2477 }
38cf91e5 2478
71e711b7 2479 /* dump_gimple_expr_loc. */
2480 {
ed9370cc 2481 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2482 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2483 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
e43345c1 2484 const int expected_impl_line = __LINE__ - 1;
71e711b7 2485
2486 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
2487 if (with_optinfo)
2488 {
2489 optinfo *info = tmp.get_pending_optinfo ();
2490 ASSERT_TRUE (info != NULL);
2491 ASSERT_EQ (info->num_items (), 1);
95b14393 2492 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
e43345c1 2493 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2494 "dumpfile.c", expected_impl_line,
2495 "test_capture_of_dump_calls");
71e711b7 2496 }
2497 }
38cf91e5 2498
71e711b7 2499 /* dump_gimple_expr. */
2500 {
ed9370cc 2501 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2502 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2503 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
e43345c1 2504 const int expected_impl_line = __LINE__ - 1;
71e711b7 2505
2506 ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
2507 if (with_optinfo)
2508 {
2509 optinfo *info = tmp.get_pending_optinfo ();
2510 ASSERT_TRUE (info != NULL);
2511 ASSERT_EQ (info->num_items (), 1);
95b14393 2512 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
e43345c1 2513 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2514 "dumpfile.c", expected_impl_line,
2515 "test_capture_of_dump_calls");
71e711b7 2516 }
2517 }
2518 }
2519
95b14393 2520 /* symtab_node. */
2521 {
2522 temp_dump_context tmp (with_optinfo, true,
2523 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2524 dump_symtab_node (MSG_NOTE, node);
e43345c1 2525 const int expected_impl_line = __LINE__ - 1;
95b14393 2526
2527 ASSERT_DUMPED_TEXT_EQ (tmp, "test_decl/0");
2528 if (with_optinfo)
2529 {
2530 optinfo *info = tmp.get_pending_optinfo ();
2531 ASSERT_TRUE (info != NULL);
2532 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2533 ASSERT_EQ (info->num_items (), 1);
2534 ASSERT_IS_SYMTAB_NODE (info->get_item (0), decl_loc, "test_decl/0");
e43345c1 2535 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2536 "dumpfile.c", expected_impl_line,
2537 "test_capture_of_dump_calls");
95b14393 2538 }
2539 }
2540
71e711b7 2541 /* poly_int. */
2542 {
ed9370cc 2543 temp_dump_context tmp (with_optinfo, true,
9ddd8fa7 2544 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
71e711b7 2545 dump_dec (MSG_NOTE, poly_int64 (42));
e43345c1 2546 const int expected_impl_line = __LINE__ - 1;
71e711b7 2547
2548 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
2549 if (with_optinfo)
2550 {
2551 optinfo *info = tmp.get_pending_optinfo ();
2552 ASSERT_TRUE (info != NULL);
2553 ASSERT_EQ (info->num_items (), 1);
2554 ASSERT_IS_TEXT (info->get_item (0), "42");
e43345c1 2555 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2556 "dumpfile.c", expected_impl_line,
2557 "test_capture_of_dump_calls");
71e711b7 2558 }
2559 }
2560
9ddd8fa7 2561 /* Scopes. Test with all 4 combinations of
2562 filtering by MSG_PRIORITY_USER_FACING
2563 and/or filtering by MSG_PRIORITY_INTERNALS. */
2564 for (int j = 0; j < 3; j++)
71e711b7 2565 {
9ddd8fa7 2566 dump_flags_t dump_filter = MSG_ALL_KINDS;
2567 if (j % 2)
2568 dump_filter |= MSG_PRIORITY_USER_FACING;
2569 if (j / 2)
2570 dump_filter |= MSG_PRIORITY_INTERNALS;
2571
ed9370cc 2572 temp_dump_context tmp (with_optinfo, true, dump_filter);
9ddd8fa7 2573 /* Emit various messages, mostly with implicit priority. */
2574 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2575 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, stmt,
2576 "explicitly internal msg\n");
71e711b7 2577 {
9ddd8fa7 2578 AUTO_DUMP_SCOPE ("outer scope", stmt);
2579 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
71e711b7 2580 {
9ddd8fa7 2581 AUTO_DUMP_SCOPE ("middle scope", stmt);
2582 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2583 {
2584 AUTO_DUMP_SCOPE ("inner scope", stmt);
2585 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
2586 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_USER_FACING, stmt,
2587 "explicitly user-facing msg\n");
2588 }
2589 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
71e711b7 2590 }
9ddd8fa7 2591 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
71e711b7 2592 }
9ddd8fa7 2593 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
e43345c1 2594 const int expected_impl_line = __LINE__ - 1;
71e711b7 2595
9ddd8fa7 2596 switch (dump_filter & MSG_ALL_PRIORITIES)
2597 {
2598 default:
2599 gcc_unreachable ();
2600 case 0:
2601 ASSERT_DUMPED_TEXT_EQ (tmp, "");
2602 break;
2603 case MSG_PRIORITY_USER_FACING:
2604 ASSERT_DUMPED_TEXT_EQ
2605 (tmp,
2606 "test.txt:5:10: note: msg 1\n"
2607 "test.txt:5:10: note: explicitly user-facing msg\n"
2608 "test.txt:5:10: note: msg 7\n");
2609 break;
2610 case MSG_PRIORITY_INTERNALS:
2611 ASSERT_DUMPED_TEXT_EQ
2612 (tmp,
2613 "test.txt:5:10: note: explicitly internal msg\n"
2614 "test.txt:5:10: note: === outer scope ===\n"
2615 "test.txt:5:10: note: msg 2\n"
2616 "test.txt:5:10: note: === middle scope ===\n"
2617 "test.txt:5:10: note: msg 3\n"
2618 "test.txt:5:10: note: === inner scope ===\n"
2619 "test.txt:5:10: note: msg 4\n"
2620 "test.txt:5:10: note: msg 5\n"
2621 "test.txt:5:10: note: msg 6\n");
2622 break;
2623 case MSG_ALL_PRIORITIES:
2624 ASSERT_DUMPED_TEXT_EQ
2625 (tmp,
2626 "test.txt:5:10: note: msg 1\n"
2627 "test.txt:5:10: note: explicitly internal msg\n"
2628 "test.txt:5:10: note: === outer scope ===\n"
2629 "test.txt:5:10: note: msg 2\n"
2630 "test.txt:5:10: note: === middle scope ===\n"
2631 "test.txt:5:10: note: msg 3\n"
2632 "test.txt:5:10: note: === inner scope ===\n"
2633 "test.txt:5:10: note: msg 4\n"
2634 "test.txt:5:10: note: explicitly user-facing msg\n"
2635 "test.txt:5:10: note: msg 5\n"
2636 "test.txt:5:10: note: msg 6\n"
2637 "test.txt:5:10: note: msg 7\n");
2638 break;
2639 }
2640 if (with_optinfo)
2641 {
2642 optinfo *info = tmp.get_pending_optinfo ();
2643 ASSERT_TRUE (info != NULL);
2644 ASSERT_EQ (info->num_items (), 1);
2645 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
e43345c1 2646 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2647 "dumpfile.c", expected_impl_line,
2648 "test_capture_of_dump_calls");
9ddd8fa7 2649 }
2650 }
38cf91e5 2651 }
38cf91e5 2652
2653 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2654 above. */
2655 {
2656 /* MSG_OPTIMIZED_LOCATIONS. */
2657 {
ed9370cc 2658 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
38cf91e5 2659 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
2660 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2661 OPTINFO_KIND_SUCCESS);
2662 }
2663
2664 /* MSG_MISSED_OPTIMIZATION. */
2665 {
ed9370cc 2666 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
38cf91e5 2667 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2668 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2669 OPTINFO_KIND_FAILURE);
2670 }
2671 }
cd35a24a 2672
2673 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2674 {
ed9370cc 2675 temp_dump_context tmp (false, true,
9ddd8fa7 2676 MSG_OPTIMIZED_LOCATIONS | MSG_ALL_PRIORITIES);
cd35a24a 2677 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2678 {
2679 AUTO_DUMP_SCOPE ("outer scope", stmt);
2680 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2681 {
2682 AUTO_DUMP_SCOPE ("middle scope", stmt);
2683 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2684 {
2685 AUTO_DUMP_SCOPE ("inner scope", stmt);
2686 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt, "msg 4\n");
2687 }
2688 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2689 }
2690 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2691 }
2692 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2693
a333f643 2694 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: optimized: msg 4\n");
cd35a24a 2695 }
38cf91e5 2696}
2697
f2e20cc0 2698static void
2699test_pr87025 ()
2700{
2701 dump_user_location_t loc
2702 = dump_user_location_t::from_location_t (UNKNOWN_LOCATION);
2703
2704 temp_dump_context tmp (true, true,
2705 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2706 {
2707 AUTO_DUMP_SCOPE ("outer scope", loc);
2708 dump_printf (MSG_NOTE, "msg1\n");
2709 }
2710}
2711
c309657f 2712/* Run all of the selftests within this file. */
2713
2714void
2715dumpfile_c_tests ()
2716{
2717 test_impl_location ();
38cf91e5 2718 for_each_line_table_case (test_capture_of_dump_calls);
f2e20cc0 2719 test_pr87025 ();
c309657f 2720}
2721
2722} // namespace selftest
2723
2724#endif /* CHECKING_P */