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