]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dumpfile.c
Update copyright years.
[thirdparty/gcc.git] / gcc / dumpfile.c
CommitLineData
b5f47924 1/* Dump infrastructure for optimizations and intermediate representation.
a5544970 2 Copyright (C) 2012-2019 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
3da39f52
DM
583dump_context::dump_loc (const dump_metadata_t &metadata,
584 const dump_user_location_t &loc)
1d8693a0 585{
b84e3bde
DM
586 end_any_optinfo ();
587
3da39f52 588 dump_loc_immediate (metadata.get_dump_flags (), loc);
f4ebbd24
DM
589
590 if (optinfo_enabled_p ())
3da39f52 591 begin_next_optinfo (metadata, loc);
f4ebbd24
DM
592}
593
594/* As dump_loc above, but without starting a new optinfo. */
595
596void
597dump_context::dump_loc_immediate (dump_flags_t dump_kind,
3da39f52 598 const dump_user_location_t &loc)
f4ebbd24 599{
1d8693a0
DM
600 location_t srcloc = loc.get_location_t ();
601
7db960c5 602 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
1d8693a0
DM
603 ::dump_loc (dump_kind, dump_file, srcloc);
604
7db960c5 605 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
1d8693a0
DM
606 ::dump_loc (dump_kind, alt_dump_file, srcloc);
607
b84e3bde 608 /* Support for temp_dump_context in selftests. */
7db960c5 609 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
b84e3bde 610 ::dump_loc (dump_kind, m_test_pp, srcloc);
1d8693a0
DM
611}
612
b84e3bde
DM
613/* Make an item for the given dump call, equivalent to print_gimple_stmt. */
614
615static optinfo_item *
616make_item_for_dump_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
617{
618 pretty_printer pp;
619 pp_needs_newline (&pp) = true;
620 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
621 pp_newline (&pp);
622
623 optinfo_item *item
624 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
625 xstrdup (pp_formatted_text (&pp)));
626 return item;
627}
628
b5f47924
SS
629/* Dump gimple statement GS with SPC indentation spaces and
630 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
631
632void
3da39f52 633dump_context::dump_gimple_stmt (const dump_metadata_t &metadata,
4df3629e
DM
634 dump_flags_t extra_dump_flags,
635 gimple *gs, int spc)
b5f47924 636{
b84e3bde
DM
637 optinfo_item *item
638 = make_item_for_dump_gimple_stmt (gs, spc, dump_flags | extra_dump_flags);
3da39f52 639 emit_item (item, metadata.get_dump_flags ());
4df3629e
DM
640
641 if (optinfo_enabled_p ())
642 {
3da39f52 643 optinfo &info = ensure_pending_optinfo (metadata);
b84e3bde 644 info.add_item (item);
4df3629e 645 }
b84e3bde
DM
646 else
647 delete item;
b5f47924
SS
648}
649
650/* Similar to dump_gimple_stmt, except additionally print source location. */
651
652void
3da39f52
DM
653dump_context::dump_gimple_stmt_loc (const dump_metadata_t &metadata,
654 const dump_user_location_t &loc,
4df3629e
DM
655 dump_flags_t extra_dump_flags,
656 gimple *gs, int spc)
b5f47924 657{
3da39f52
DM
658 dump_loc (metadata, loc);
659 dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
b5f47924
SS
660}
661
b84e3bde
DM
662/* Make an item for the given dump call, equivalent to print_gimple_expr. */
663
664static optinfo_item *
665make_item_for_dump_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
666{
667 dump_flags |= TDF_RHS_ONLY;
668 pretty_printer pp;
669 pp_needs_newline (&pp) = true;
670 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
671
672 optinfo_item *item
673 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
674 xstrdup (pp_formatted_text (&pp)));
675 return item;
676}
677
30f502ed
RB
678/* Dump gimple statement GS with SPC indentation spaces and
679 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
680 Do not terminate with a newline or semicolon. */
681
682void
3da39f52 683dump_context::dump_gimple_expr (const dump_metadata_t &metadata,
4df3629e
DM
684 dump_flags_t extra_dump_flags,
685 gimple *gs, int spc)
30f502ed 686{
b84e3bde
DM
687 optinfo_item *item
688 = make_item_for_dump_gimple_expr (gs, spc, dump_flags | extra_dump_flags);
3da39f52 689 emit_item (item, metadata.get_dump_flags ());
4df3629e
DM
690
691 if (optinfo_enabled_p ())
692 {
3da39f52 693 optinfo &info = ensure_pending_optinfo (metadata);
b84e3bde 694 info.add_item (item);
4df3629e 695 }
b84e3bde
DM
696 else
697 delete item;
30f502ed
RB
698}
699
700/* Similar to dump_gimple_expr, except additionally print source location. */
701
702void
3da39f52
DM
703dump_context::dump_gimple_expr_loc (const dump_metadata_t &metadata,
704 const dump_user_location_t &loc,
4df3629e
DM
705 dump_flags_t extra_dump_flags,
706 gimple *gs,
707 int spc)
30f502ed 708{
3da39f52
DM
709 dump_loc (metadata, loc);
710 dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
30f502ed
RB
711}
712
b84e3bde
DM
713/* Make an item for the given dump call, equivalent to print_generic_expr. */
714
715static optinfo_item *
716make_item_for_dump_generic_expr (tree node, dump_flags_t dump_flags)
717{
718 pretty_printer pp;
719 pp_needs_newline (&pp) = true;
720 pp_translate_identifiers (&pp) = false;
721 dump_generic_node (&pp, node, 0, dump_flags, false);
722
723 location_t loc = UNKNOWN_LOCATION;
724 if (EXPR_HAS_LOCATION (node))
725 loc = EXPR_LOCATION (node);
726
727 optinfo_item *item
728 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
729 xstrdup (pp_formatted_text (&pp)));
730 return item;
731}
30f502ed 732
b5f47924
SS
733/* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
734 DUMP_KIND is enabled. */
735
736void
3da39f52 737dump_context::dump_generic_expr (const dump_metadata_t &metadata,
4df3629e
DM
738 dump_flags_t extra_dump_flags,
739 tree t)
b5f47924 740{
b84e3bde
DM
741 optinfo_item *item
742 = make_item_for_dump_generic_expr (t, dump_flags | extra_dump_flags);
3da39f52 743 emit_item (item, metadata.get_dump_flags ());
4df3629e
DM
744
745 if (optinfo_enabled_p ())
746 {
3da39f52 747 optinfo &info = ensure_pending_optinfo (metadata);
b84e3bde 748 info.add_item (item);
4df3629e 749 }
b84e3bde
DM
750 else
751 delete item;
b5f47924
SS
752}
753
d7259673
DM
754
755/* Similar to dump_generic_expr, except additionally print the source
756 location. */
757
758void
3da39f52
DM
759dump_context::dump_generic_expr_loc (const dump_metadata_t &metadata,
760 const dump_user_location_t &loc,
4df3629e
DM
761 dump_flags_t extra_dump_flags,
762 tree t)
d7259673 763{
3da39f52
DM
764 dump_loc (metadata, loc);
765 dump_generic_expr (metadata, extra_dump_flags, t);
d7259673
DM
766}
767
d8010ee4
DM
768/* Make an item for the given dump call. */
769
770static optinfo_item *
771make_item_for_dump_symtab_node (symtab_node *node)
772{
773 location_t loc = DECL_SOURCE_LOCATION (node->decl);
774 optinfo_item *item
775 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
776 xstrdup (node->dump_name ()));
777 return item;
778}
779
6f795a92
DM
780/* dump_pretty_printer's ctor. */
781
782dump_pretty_printer::dump_pretty_printer (dump_context *context,
783 dump_flags_t dump_kind)
784: pretty_printer (), m_context (context), m_dump_kind (dump_kind),
785 m_stashed_items ()
786{
787 pp_format_decoder (this) = format_decoder_cb;
788}
789
790/* Phase 3 of formatting; compare with pp_output_formatted_text.
791
792 Emit optinfo_item instances for the various formatted chunks from phases
793 1 and 2 (i.e. pp_format).
794
795 Some chunks may already have had their items built (during decode_format).
796 These chunks have been stashed into m_stashed_items; we emit them here.
797
798 For all other purely textual chunks, they are printed into
799 buffer->formatted_obstack, and then emitted as a textual optinfo_item.
800 This consolidates multiple adjacent text chunks into a single text
801 optinfo_item. */
802
803void
804dump_pretty_printer::emit_items (optinfo *dest)
b84e3bde 805{
6f795a92
DM
806 output_buffer *buffer = pp_buffer (this);
807 struct chunk_info *chunk_array = buffer->cur_chunk_array;
808 const char **args = chunk_array->args;
809
810 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
811 gcc_assert (buffer->line_length == 0);
812
813 unsigned stashed_item_idx = 0;
814 for (unsigned chunk = 0; args[chunk]; chunk++)
815 {
816 if (stashed_item_idx < m_stashed_items.length ()
817 && args[chunk] == *m_stashed_items[stashed_item_idx].buffer_ptr)
818 {
819 emit_any_pending_textual_chunks (dest);
820 /* This chunk has a stashed item: use it. */
821 emit_item (m_stashed_items[stashed_item_idx++].item, dest);
822 }
823 else
824 /* This chunk is purely textual. Print it (to
825 buffer->formatted_obstack), so that we can consolidate adjacent
826 chunks into one textual optinfo_item. */
827 pp_string (this, args[chunk]);
828 }
829
830 emit_any_pending_textual_chunks (dest);
831
832 /* Ensure that we consumed all of stashed_items. */
833 gcc_assert (stashed_item_idx == m_stashed_items.length ());
834
835 /* Deallocate the chunk structure and everything after it (i.e. the
836 associated series of formatted strings). */
837 buffer->cur_chunk_array = chunk_array->prev;
838 obstack_free (&buffer->chunk_obstack, chunk_array);
839}
840
841/* Subroutine of dump_pretty_printer::emit_items
842 for consolidating multiple adjacent pure-text chunks into single
843 optinfo_items (in phase 3). */
844
845void
846dump_pretty_printer::emit_any_pending_textual_chunks (optinfo *dest)
847{
848 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
849
850 /* Don't emit an item if the pending text is empty. */
851 if (output_buffer_last_position_in_text (buffer) == NULL)
852 return;
853
854 char *formatted_text = xstrdup (pp_formatted_text (this));
b84e3bde
DM
855 optinfo_item *item
856 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
857 formatted_text);
6f795a92
DM
858 emit_item (item, dest);
859
860 /* Clear the pending text by unwinding formatted_text back to the start
861 of the buffer (without deallocating). */
862 obstack_free (&buffer->formatted_obstack,
863 buffer->formatted_obstack.object_base);
b84e3bde
DM
864}
865
6f795a92
DM
866/* Emit ITEM and take ownership of it. If DEST is non-NULL, add ITEM
867 to DEST; otherwise delete ITEM. */
b84e3bde 868
6f795a92
DM
869void
870dump_pretty_printer::emit_item (optinfo_item *item, optinfo *dest)
871{
872 m_context->emit_item (item, m_dump_kind);
873 if (dest)
874 dest->add_item (item);
875 else
876 delete item;
877}
b84e3bde 878
6f795a92
DM
879/* Record that ITEM (generated in phase 2 of formatting) is to be used for
880 the chunk at BUFFER_PTR in phase 3 (by emit_items). */
881
882void
883dump_pretty_printer::stash_item (const char **buffer_ptr, optinfo_item *item)
b84e3bde 884{
6f795a92
DM
885 gcc_assert (buffer_ptr);
886 gcc_assert (item);
887
888 m_stashed_items.safe_push (stashed_item (buffer_ptr, item));
889}
890
891/* pp_format_decoder callback for dump_pretty_printer, and thus for
892 dump_printf and dump_printf_loc.
893
894 A wrapper around decode_format, for type-safety. */
895
896bool
897dump_pretty_printer::format_decoder_cb (pretty_printer *pp, text_info *text,
898 const char *spec, int /*precision*/,
899 bool /*wide*/, bool /*set_locus*/,
900 bool /*verbose*/, bool */*quoted*/,
901 const char **buffer_ptr)
902{
903 dump_pretty_printer *opp = static_cast <dump_pretty_printer *> (pp);
904 return opp->decode_format (text, spec, buffer_ptr);
905}
906
907/* Format decoder for dump_pretty_printer, and thus for dump_printf and
908 dump_printf_loc.
909
910 Supported format codes (in addition to the standard pretty_printer ones)
911 are:
912
d8010ee4
DM
913 %C: cgraph_node *:
914 Equivalent to: dump_symtab_node (MSG_*, node)
6f795a92
DM
915 %E: gimple *:
916 Equivalent to: dump_gimple_expr (MSG_*, TDF_SLIM, stmt, 0)
917 %G: gimple *:
918 Equivalent to: dump_gimple_stmt (MSG_*, TDF_SLIM, stmt, 0)
919 %T: tree:
920 Equivalent to: dump_generic_expr (MSG_*, arg, TDF_SLIM).
921
d8010ee4
DM
922 TODO: add a format code that can handle (symtab_node*) *and* both
923 subclasses (presumably means teaching -Wformat about non-virtual
924 subclasses).
6f795a92
DM
925
926 These format codes build optinfo_item instances, thus capturing metadata
927 about the arguments being dumped, as well as the textual output. */
928
929bool
930dump_pretty_printer::decode_format (text_info *text, const char *spec,
931 const char **buffer_ptr)
932{
933 /* Various format codes that imply making an optinfo_item and stashed it
934 for later use (to capture metadata, rather than plain text). */
935 switch (*spec)
936 {
d8010ee4
DM
937 case 'C':
938 {
939 cgraph_node *node = va_arg (*text->args_ptr, cgraph_node *);
940
941 /* Make an item for the node, and stash it. */
942 optinfo_item *item = make_item_for_dump_symtab_node (node);
943 stash_item (buffer_ptr, item);
944 return true;
945 }
946
6f795a92
DM
947 case 'E':
948 {
949 gimple *stmt = va_arg (*text->args_ptr, gimple *);
950
951 /* Make an item for the stmt, and stash it. */
952 optinfo_item *item = make_item_for_dump_gimple_expr (stmt, 0, TDF_SLIM);
953 stash_item (buffer_ptr, item);
954 return true;
955 }
956
957 case 'G':
958 {
959 gimple *stmt = va_arg (*text->args_ptr, gimple *);
960
961 /* Make an item for the stmt, and stash it. */
962 optinfo_item *item = make_item_for_dump_gimple_stmt (stmt, 0, TDF_SLIM);
963 stash_item (buffer_ptr, item);
964 return true;
965 }
966
967 case 'T':
968 {
969 tree t = va_arg (*text->args_ptr, tree);
970
971 /* Make an item for the tree, and stash it. */
972 optinfo_item *item = make_item_for_dump_generic_expr (t, TDF_SLIM);
973 stash_item (buffer_ptr, item);
974 return true;
975 }
976
977 default:
978 return false;
979 }
b84e3bde
DM
980}
981
b5f47924
SS
982/* Output a formatted message using FORMAT on appropriate dump streams. */
983
984void
3da39f52 985dump_context::dump_printf_va (const dump_metadata_t &metadata, const char *format,
6f795a92 986 va_list *ap)
b5f47924 987{
3da39f52 988 dump_pretty_printer pp (this, metadata.get_dump_flags ());
4df3629e 989
6f795a92
DM
990 text_info text;
991 text.err_no = errno;
992 text.args_ptr = ap;
993 text.format_spec = format;
994
995 /* Phases 1 and 2, using pp_format. */
996 pp_format (&pp, &text);
997
998 /* Phase 3. */
4df3629e
DM
999 if (optinfo_enabled_p ())
1000 {
3da39f52 1001 optinfo &info = ensure_pending_optinfo (metadata);
6f795a92 1002 pp.emit_items (&info);
b5f47924 1003 }
b84e3bde 1004 else
6f795a92 1005 pp.emit_items (NULL);
b5f47924
SS
1006}
1007
4df3629e
DM
1008/* Similar to dump_printf, except source location is also printed, and
1009 dump location captured. */
b5f47924
SS
1010
1011void
3da39f52
DM
1012dump_context::dump_printf_loc_va (const dump_metadata_t &metadata,
1013 const dump_user_location_t &loc,
6f795a92 1014 const char *format, va_list *ap)
b5f47924 1015{
3da39f52
DM
1016 dump_loc (metadata, loc);
1017 dump_printf_va (metadata, format, ap);
b5f47924
SS
1018}
1019
b84e3bde 1020/* Make an item for the given dump call, equivalent to print_dec. */
dc3f3805
RS
1021
1022template<unsigned int N, typename C>
b84e3bde
DM
1023static optinfo_item *
1024make_item_for_dump_dec (const poly_int<N, C> &value)
dc3f3805
RS
1025{
1026 STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
1027 signop sgn = poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED;
dc3f3805 1028
b84e3bde
DM
1029 pretty_printer pp;
1030
1031 if (value.is_constant ())
1032 pp_wide_int (&pp, value.coeffs[0], sgn);
1033 else
1034 {
1035 pp_character (&pp, '[');
1036 for (unsigned int i = 0; i < N; ++i)
1037 {
1038 pp_wide_int (&pp, value.coeffs[i], sgn);
1039 pp_character (&pp, i == N - 1 ? ']' : ',');
1040 }
1041 }
1042
1043 optinfo_item *item
1044 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1045 xstrdup (pp_formatted_text (&pp)));
1046 return item;
1047}
1048
1049/* Output VALUE in decimal to appropriate dump streams. */
1050
1051template<unsigned int N, typename C>
1052void
3da39f52 1053dump_context::dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
b84e3bde
DM
1054{
1055 optinfo_item *item = make_item_for_dump_dec (value);
3da39f52 1056 emit_item (item, metadata.get_dump_flags ());
4df3629e
DM
1057
1058 if (optinfo_enabled_p ())
1059 {
3da39f52 1060 optinfo &info = ensure_pending_optinfo (metadata);
b84e3bde 1061 info.add_item (item);
4df3629e 1062 }
b84e3bde
DM
1063 else
1064 delete item;
1065}
1066
4df3629e
DM
1067/* Output the name of NODE on appropriate dump streams. */
1068
1069void
3da39f52 1070dump_context::dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
4df3629e 1071{
b84e3bde 1072 optinfo_item *item = make_item_for_dump_symtab_node (node);
3da39f52 1073 emit_item (item, metadata.get_dump_flags ());
4df3629e
DM
1074
1075 if (optinfo_enabled_p ())
1076 {
3da39f52 1077 optinfo &info = ensure_pending_optinfo (metadata);
b84e3bde 1078 info.add_item (item);
4df3629e 1079 }
b84e3bde
DM
1080 else
1081 delete item;
4df3629e
DM
1082}
1083
1084/* Get the current dump scope-nesting depth.
1085 For use by -fopt-info (for showing nesting via indentation). */
1086
1087unsigned int
1088dump_context::get_scope_depth () const
1089{
1090 return m_scope_depth;
1091}
1092
1093/* Push a nested dump scope.
7db960c5 1094 Increment the scope depth.
4df3629e
DM
1095 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1096 destination, if any.
7db960c5 1097 Emit a "scope" optinfo if optinfos are enabled. */
4df3629e
DM
1098
1099void
3da39f52
DM
1100dump_context::begin_scope (const char *name,
1101 const dump_user_location_t &user_location,
1102 const dump_impl_location_t &impl_location)
4df3629e 1103{
7db960c5
DM
1104 m_scope_depth++;
1105
3da39f52
DM
1106 location_t src_loc = user_location.get_location_t ();
1107
7db960c5 1108 if (dump_file && apply_dump_filter_p (MSG_NOTE, pflags))
3da39f52 1109 ::dump_loc (MSG_NOTE, dump_file, src_loc);
4df3629e 1110
7db960c5 1111 if (alt_dump_file && apply_dump_filter_p (MSG_NOTE, alt_flags))
3da39f52 1112 ::dump_loc (MSG_NOTE, alt_dump_file, src_loc);
b84e3bde
DM
1113
1114 /* Support for temp_dump_context in selftests. */
7db960c5 1115 if (m_test_pp && apply_dump_filter_p (MSG_NOTE, m_test_pp_flags))
3da39f52 1116 ::dump_loc (MSG_NOTE, m_test_pp, src_loc);
b84e3bde 1117
6f795a92
DM
1118 pretty_printer pp;
1119 pp_printf (&pp, "=== %s ===\n", name);
1120 optinfo_item *item
1121 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
1122 xstrdup (pp_formatted_text (&pp)));
b84e3bde 1123 emit_item (item, MSG_NOTE);
4df3629e
DM
1124
1125 if (optinfo_enabled_p ())
1126 {
3da39f52
DM
1127 optinfo &info
1128 = begin_next_optinfo (dump_metadata_t (MSG_NOTE, impl_location),
1129 user_location);
b84e3bde
DM
1130 info.m_kind = OPTINFO_KIND_SCOPE;
1131 info.add_item (item);
c95c5528 1132 end_any_optinfo ();
4df3629e 1133 }
b84e3bde
DM
1134 else
1135 delete item;
4df3629e
DM
1136}
1137
1138/* Pop a nested dump scope. */
1139
1140void
1141dump_context::end_scope ()
1142{
1143 end_any_optinfo ();
1144 m_scope_depth--;
8d1af516
DM
1145
1146 if (m_json_writer)
1147 m_json_writer->pop_scope ();
1148}
1149
1150/* Should optinfo instances be created?
1151 All creation of optinfos should be guarded by this predicate.
1152 Return true if any optinfo destinations are active. */
1153
1154bool
1155dump_context::optinfo_enabled_p () const
1156{
1157 return (optimization_records_enabled_p ());
4df3629e
DM
1158}
1159
1160/* Return the optinfo currently being accumulated, creating one if
1161 necessary. */
1162
1163optinfo &
3da39f52 1164dump_context::ensure_pending_optinfo (const dump_metadata_t &metadata)
4df3629e
DM
1165{
1166 if (!m_pending)
3da39f52 1167 return begin_next_optinfo (metadata, dump_user_location_t ());
4df3629e
DM
1168 return *m_pending;
1169}
1170
1171/* Start a new optinfo and return it, ending any optinfo that was already
1172 accumulated. */
1173
1174optinfo &
3da39f52
DM
1175dump_context::begin_next_optinfo (const dump_metadata_t &metadata,
1176 const dump_user_location_t &user_loc)
4df3629e
DM
1177{
1178 end_any_optinfo ();
1179 gcc_assert (m_pending == NULL);
3da39f52 1180 dump_location_t loc (user_loc, metadata.get_impl_location ());
4df3629e 1181 m_pending = new optinfo (loc, OPTINFO_KIND_NOTE, current_pass);
3da39f52 1182 m_pending->handle_dump_file_kind (metadata.get_dump_flags ());
4df3629e
DM
1183 return *m_pending;
1184}
1185
1186/* End any optinfo that has been accumulated within this context; emitting
4a4412b9 1187 it to any destinations as appropriate, such as optimization records. */
4df3629e
DM
1188
1189void
1190dump_context::end_any_optinfo ()
1191{
1192 if (m_pending)
8d1af516 1193 emit_optinfo (m_pending);
4df3629e
DM
1194 delete m_pending;
1195 m_pending = NULL;
1196}
1197
8d1af516
DM
1198/* Emit the optinfo to all of the "non-immediate" destinations
1199 (emission to "immediate" destinations is done by
1200 dump_context::emit_item). */
1201
1202void
1203dump_context::emit_optinfo (const optinfo *info)
1204{
1205 /* -fsave-optimization-record. */
1206 if (m_json_writer)
1207 m_json_writer->add_record (info);
1208}
1209
b84e3bde
DM
1210/* Emit ITEM to all item destinations (those that don't require
1211 consolidation into optinfo instances). */
1212
1213void
1214dump_context::emit_item (optinfo_item *item, dump_flags_t dump_kind)
1215{
7db960c5 1216 if (dump_file && apply_dump_filter_p (dump_kind, pflags))
b84e3bde
DM
1217 fprintf (dump_file, "%s", item->get_text ());
1218
7db960c5 1219 if (alt_dump_file && apply_dump_filter_p (dump_kind, alt_flags))
b84e3bde
DM
1220 fprintf (alt_dump_file, "%s", item->get_text ());
1221
1222 /* Support for temp_dump_context in selftests. */
7db960c5 1223 if (m_test_pp && apply_dump_filter_p (dump_kind, m_test_pp_flags))
b84e3bde
DM
1224 pp_string (m_test_pp, item->get_text ());
1225}
1226
4df3629e
DM
1227/* The current singleton dump_context, and its default. */
1228
1229dump_context *dump_context::s_current = &dump_context::s_default;
1230dump_context dump_context::s_default;
1231
1232/* Implementation of dump_* API calls, calling into dump_context
1233 member functions. */
1234
bbeeac91
DM
1235/* Calls to the dump_* functions do non-trivial work, so they ought
1236 to be guarded by:
1237 if (dump_enabled_p ())
1238 Assert that they are guarded, and, if assertions are disabled,
1239 bail out if the calls weren't properly guarded. */
1240
1241#define VERIFY_DUMP_ENABLED_P \
1242 do { \
1243 gcc_assert (dump_enabled_p ()); \
1244 if (!dump_enabled_p ()) \
1245 return; \
1246 } while (0)
1247
4df3629e
DM
1248/* Dump gimple statement GS with SPC indentation spaces and
1249 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled. */
1250
1251void
3da39f52 1252dump_gimple_stmt (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
4df3629e
DM
1253 gimple *gs, int spc)
1254{
bbeeac91 1255 VERIFY_DUMP_ENABLED_P;
3da39f52 1256 dump_context::get ().dump_gimple_stmt (metadata, extra_dump_flags, gs, spc);
4df3629e
DM
1257}
1258
1259/* Similar to dump_gimple_stmt, except additionally print source location. */
1260
1261void
3da39f52
DM
1262dump_gimple_stmt_loc (const dump_metadata_t &metadata,
1263 const dump_user_location_t &loc,
4df3629e
DM
1264 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1265{
bbeeac91 1266 VERIFY_DUMP_ENABLED_P;
3da39f52 1267 dump_context::get ().dump_gimple_stmt_loc (metadata, loc, extra_dump_flags,
4df3629e
DM
1268 gs, spc);
1269}
1270
1271/* Dump gimple statement GS with SPC indentation spaces and
1272 EXTRA_DUMP_FLAGS on the dump streams if DUMP_KIND is enabled.
1273 Do not terminate with a newline or semicolon. */
1274
1275void
3da39f52
DM
1276dump_gimple_expr (const dump_metadata_t &metadata,
1277 dump_flags_t extra_dump_flags,
4df3629e
DM
1278 gimple *gs, int spc)
1279{
bbeeac91 1280 VERIFY_DUMP_ENABLED_P;
3da39f52 1281 dump_context::get ().dump_gimple_expr (metadata, extra_dump_flags, gs, spc);
4df3629e
DM
1282}
1283
1284/* Similar to dump_gimple_expr, except additionally print source location. */
1285
1286void
3da39f52
DM
1287dump_gimple_expr_loc (const dump_metadata_t &metadata,
1288 const dump_user_location_t &loc,
4df3629e
DM
1289 dump_flags_t extra_dump_flags, gimple *gs, int spc)
1290{
bbeeac91 1291 VERIFY_DUMP_ENABLED_P;
3da39f52 1292 dump_context::get ().dump_gimple_expr_loc (metadata, loc, extra_dump_flags,
4df3629e
DM
1293 gs, spc);
1294}
1295
1296/* Dump expression tree T using EXTRA_DUMP_FLAGS on dump streams if
1297 DUMP_KIND is enabled. */
1298
1299void
3da39f52 1300dump_generic_expr (const dump_metadata_t &metadata, dump_flags_t extra_dump_flags,
4df3629e
DM
1301 tree t)
1302{
bbeeac91 1303 VERIFY_DUMP_ENABLED_P;
3da39f52 1304 dump_context::get ().dump_generic_expr (metadata, extra_dump_flags, t);
4df3629e
DM
1305}
1306
1307/* Similar to dump_generic_expr, except additionally print the source
1308 location. */
1309
1310void
3da39f52
DM
1311dump_generic_expr_loc (const dump_metadata_t &metadata,
1312 const dump_user_location_t &loc,
4df3629e
DM
1313 dump_flags_t extra_dump_flags, tree t)
1314{
bbeeac91 1315 VERIFY_DUMP_ENABLED_P;
3da39f52 1316 dump_context::get ().dump_generic_expr_loc (metadata, loc, extra_dump_flags,
4df3629e
DM
1317 t);
1318}
1319
1320/* Output a formatted message using FORMAT on appropriate dump streams. */
1321
1322void
3da39f52 1323dump_printf (const dump_metadata_t &metadata, const char *format, ...)
4df3629e 1324{
bbeeac91 1325 VERIFY_DUMP_ENABLED_P;
4df3629e
DM
1326 va_list ap;
1327 va_start (ap, format);
3da39f52 1328 dump_context::get ().dump_printf_va (metadata, format, &ap);
4df3629e
DM
1329 va_end (ap);
1330}
1331
1332/* Similar to dump_printf, except source location is also printed, and
1333 dump location captured. */
1334
1335void
3da39f52
DM
1336dump_printf_loc (const dump_metadata_t &metadata,
1337 const dump_user_location_t &loc,
4df3629e
DM
1338 const char *format, ...)
1339{
bbeeac91 1340 VERIFY_DUMP_ENABLED_P;
4df3629e
DM
1341 va_list ap;
1342 va_start (ap, format);
3da39f52 1343 dump_context::get ().dump_printf_loc_va (metadata, loc, format, &ap);
4df3629e
DM
1344 va_end (ap);
1345}
1346
1347/* Output VALUE in decimal to appropriate dump streams. */
1348
1349template<unsigned int N, typename C>
1350void
3da39f52 1351dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value)
4df3629e 1352{
bbeeac91 1353 VERIFY_DUMP_ENABLED_P;
3da39f52 1354 dump_context::get ().dump_dec (metadata, value);
dc3f3805
RS
1355}
1356
3da39f52
DM
1357template void dump_dec (const dump_metadata_t &metadata, const poly_uint16 &);
1358template void dump_dec (const dump_metadata_t &metadata, const poly_int64 &);
1359template void dump_dec (const dump_metadata_t &metadata, const poly_uint64 &);
1360template void dump_dec (const dump_metadata_t &metadata, const poly_offset_int &);
1361template void dump_dec (const dump_metadata_t &metadata, const poly_widest_int &);
dc3f3805 1362
370c2ebe
RS
1363void
1364dump_dec (dump_flags_t dump_kind, const poly_wide_int &value, signop sgn)
1365{
bbeeac91 1366 VERIFY_DUMP_ENABLED_P;
7db960c5
DM
1367 if (dump_file
1368 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
370c2ebe
RS
1369 print_dec (value, dump_file, sgn);
1370
7db960c5
DM
1371 if (alt_dump_file
1372 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
370c2ebe
RS
1373 print_dec (value, alt_dump_file, sgn);
1374}
1375
1376/* Output VALUE in hexadecimal to appropriate dump streams. */
1377
1378void
1379dump_hex (dump_flags_t dump_kind, const poly_wide_int &value)
1380{
bbeeac91 1381 VERIFY_DUMP_ENABLED_P;
7db960c5
DM
1382 if (dump_file
1383 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
370c2ebe
RS
1384 print_hex (value, dump_file);
1385
7db960c5
DM
1386 if (alt_dump_file
1387 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
370c2ebe
RS
1388 print_hex (value, alt_dump_file);
1389}
1390
4df3629e
DM
1391/* Emit and delete the currently pending optinfo, if there is one,
1392 without the caller needing to know about class dump_context. */
1393
1394void
1395dumpfile_ensure_any_optinfo_are_flushed ()
1396{
1397 dump_context::get().end_any_optinfo ();
1398}
1399
1400/* Output the name of NODE on appropriate dump streams. */
6475e077 1401
4df3629e 1402void
3da39f52 1403dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node)
4df3629e 1404{
bbeeac91 1405 VERIFY_DUMP_ENABLED_P;
3da39f52 1406 dump_context::get ().dump_symtab_node (metadata, node);
4df3629e 1407}
6475e077
DM
1408
1409/* Get the current dump scope-nesting depth.
4df3629e 1410 For use by -fopt-info (for showing nesting via indentation). */
6475e077
DM
1411
1412unsigned int
1413get_dump_scope_depth ()
1414{
4df3629e 1415 return dump_context::get ().get_scope_depth ();
6475e077
DM
1416}
1417
1418/* Push a nested dump scope.
1419 Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
1420 destination, if any.
4df3629e 1421 Emit a "scope" opinfo if optinfos are enabled.
6475e077
DM
1422 Increment the scope depth. */
1423
1424void
3da39f52
DM
1425dump_begin_scope (const char *name,
1426 const dump_user_location_t &user_location,
1427 const dump_impl_location_t &impl_location)
6475e077 1428{
3da39f52 1429 dump_context::get ().begin_scope (name, user_location, impl_location);
6475e077
DM
1430}
1431
1432/* Pop a nested dump scope. */
1433
1434void
1435dump_end_scope ()
1436{
4df3629e 1437 dump_context::get ().end_scope ();
6475e077
DM
1438}
1439
b5f47924
SS
1440/* Start a dump for PHASE. Store user-supplied dump flags in
1441 *FLAG_PTR. Return the number of streams opened. Set globals
1442 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
2b4e6bf1
SS
1443 set dump_flags appropriately for both pass dump stream and
1444 -fopt-info stream. */
b5f47924
SS
1445
1446int
47e0da37 1447gcc::dump_manager::
1a817418 1448dump_start (int phase, dump_flags_t *flag_ptr)
b5f47924
SS
1449{
1450 int count = 0;
1451 char *name;
1452 struct dump_file_info *dfi;
1453 FILE *stream;
74911c3c 1454 if (phase == TDI_none || !dump_phase_enabled_p (phase))
b5f47924
SS
1455 return 0;
1456
1457 dfi = get_dump_file_info (phase);
1458 name = get_dump_file_name (phase);
1459 if (name)
1460 {
5d8b352a
NS
1461 stream = dump_open (name, dfi->pstate < 0);
1462 if (stream)
b5f47924
SS
1463 {
1464 dfi->pstate = 1;
1465 count++;
1466 }
1467 free (name);
1468 dfi->pstream = stream;
29b54a9d 1469 set_dump_file (dfi->pstream);
b5f47924 1470 /* Initialize current dump flags. */
8264c84d 1471 pflags = dfi->pflags;
b5f47924
SS
1472 }
1473
1474 stream = dump_open_alternate_stream (dfi);
1475 if (stream)
1476 {
1477 dfi->alt_stream = stream;
1478 count++;
29b54a9d 1479 set_alt_dump_file (dfi->alt_stream);
2b4e6bf1 1480 /* Initialize current -fopt-info flags. */
8264c84d 1481 alt_flags = dfi->alt_flags;
b5f47924
SS
1482 }
1483
1484 if (flag_ptr)
1485 *flag_ptr = dfi->pflags;
1486
1487 return count;
1488}
1489
1490/* Finish a tree dump for PHASE and close associated dump streams. Also
1491 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
1492
1493void
47e0da37 1494gcc::dump_manager::
b5f47924
SS
1495dump_finish (int phase)
1496{
1497 struct dump_file_info *dfi;
1498
1499 if (phase < 0)
1500 return;
1501 dfi = get_dump_file_info (phase);
5d8b352a 1502 if (dfi->pstream && dfi->pstream != stdout && dfi->pstream != stderr)
b5f47924
SS
1503 fclose (dfi->pstream);
1504
5d8b352a 1505 if (dfi->alt_stream && dfi->alt_stream != stdout && dfi->alt_stream != stderr)
b5f47924
SS
1506 fclose (dfi->alt_stream);
1507
1508 dfi->alt_stream = NULL;
1509 dfi->pstream = NULL;
29b54a9d
DM
1510 set_dump_file (NULL);
1511 set_alt_dump_file (NULL);
4af78ef8
DM
1512 dump_flags = TDF_NONE;
1513 alt_flags = TDF_NONE;
1514 pflags = TDF_NONE;
b5f47924
SS
1515}
1516
1517/* Begin a tree dump for PHASE. Stores any user supplied flag in
1518 *FLAG_PTR and returns a stream to write to. If the dump is not
1519 enabled, returns NULL.
75fdb07e
JH
1520 PART can be used for dump files which should be split to multiple
1521 parts. PART == -1 indicates dump file with no parts.
1522 If PART is -1, multiple calls will reopen and append to the dump file. */
b5f47924
SS
1523
1524FILE *
75fdb07e 1525dump_begin (int phase, dump_flags_t *flag_ptr, int part)
47e0da37 1526{
75fdb07e 1527 return g->get_dumps ()->dump_begin (phase, flag_ptr, part);
47e0da37
DM
1528}
1529
1530FILE *
1531gcc::dump_manager::
75fdb07e 1532dump_begin (int phase, dump_flags_t *flag_ptr, int part)
b5f47924
SS
1533{
1534 char *name;
1535 struct dump_file_info *dfi;
1536 FILE *stream;
1537
74911c3c 1538 if (phase == TDI_none || !dump_phase_enabled_p (phase))
b5f47924
SS
1539 return NULL;
1540
75fdb07e 1541 name = get_dump_file_name (phase, part);
b5f47924
SS
1542 if (!name)
1543 return NULL;
1544 dfi = get_dump_file_info (phase);
1545
75fdb07e
JH
1546 /* We do not support re-opening of dump files with parts. This would require
1547 tracking pstate per part of the dump file. */
1548 stream = dump_open (name, part != -1 || dfi->pstate < 0);
5d8b352a 1549 if (stream)
b5f47924
SS
1550 dfi->pstate = 1;
1551 free (name);
1552
1553 if (flag_ptr)
1554 *flag_ptr = dfi->pflags;
1555
1556 /* Initialize current flags */
1557 pflags = dfi->pflags;
1558 return stream;
1559}
1560
1561/* Returns nonzero if dump PHASE is enabled for at least one stream.
1562 If PHASE is TDI_tree_all, return nonzero if any dump is enabled for
1563 any phase. */
1564
47e0da37
DM
1565int
1566gcc::dump_manager::
1567dump_phase_enabled_p (int phase) const
b5f47924
SS
1568{
1569 if (phase == TDI_tree_all)
1570 {
1571 size_t i;
1572 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1573 if (dump_files[i].pstate || dump_files[i].alt_state)
1574 return 1;
47e0da37
DM
1575 for (i = 0; i < m_extra_dump_files_in_use; i++)
1576 if (m_extra_dump_files[i].pstate || m_extra_dump_files[i].alt_state)
b5f47924
SS
1577 return 1;
1578 return 0;
1579 }
1580 else
1581 {
1582 struct dump_file_info *dfi = get_dump_file_info (phase);
1583 return dfi->pstate || dfi->alt_state;
1584 }
1585}
1586
1587/* Returns nonzero if tree dump PHASE has been initialized. */
1588
1589int
47e0da37
DM
1590gcc::dump_manager::
1591dump_initialized_p (int phase) const
b5f47924
SS
1592{
1593 struct dump_file_info *dfi = get_dump_file_info (phase);
1594 return dfi->pstate > 0 || dfi->alt_state > 0;
1595}
1596
1597/* Returns the switch name of PHASE. */
1598
1599const char *
1600dump_flag_name (int phase)
47e0da37
DM
1601{
1602 return g->get_dumps ()->dump_flag_name (phase);
1603}
1604
1605const char *
1606gcc::dump_manager::
1607dump_flag_name (int phase) const
b5f47924
SS
1608{
1609 struct dump_file_info *dfi = get_dump_file_info (phase);
1610 return dfi->swtch;
1611}
1612
5d98e5a6
DM
1613/* Handle -fdump-* and -fopt-info for a pass added after
1614 command-line options are parsed (those from plugins and
1615 those from backends).
1616
1617 Because the registration of plugin/backend passes happens after the
1618 command-line options are parsed, the options that specify single
1619 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1620 passes. Therefore we currently can only enable dumping of
1621 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1622 are specified. This is done here.
1623
1624 Similarly, the saved -fopt-info options are wired up to the new pass. */
1625
1626void
1627gcc::dump_manager::register_pass (opt_pass *pass)
1628{
1629 gcc_assert (pass);
1630
1631 register_one_dump_file (pass);
1632
1633 dump_file_info *pass_dfi = get_dump_file_info (pass->static_pass_number);
1634 gcc_assert (pass_dfi);
1635
1636 enum tree_dump_index tdi;
1637 if (pass->type == SIMPLE_IPA_PASS
1638 || pass->type == IPA_PASS)
1639 tdi = TDI_ipa_all;
1640 else if (pass->type == GIMPLE_PASS)
1641 tdi = TDI_tree_all;
1642 else
1643 tdi = TDI_rtl_all;
1644 const dump_file_info *tdi_dfi = get_dump_file_info (tdi);
1645 gcc_assert (tdi_dfi);
1646
1647 /* Check if dump-all flag is specified. */
1648 if (tdi_dfi->pstate)
1649 {
1650 pass_dfi->pstate = tdi_dfi->pstate;
1651 pass_dfi->pflags = tdi_dfi->pflags;
1652 }
1653
1654 update_dfi_for_opt_info (pass_dfi);
1655}
1656
b5f47924
SS
1657/* Finish a tree dump for PHASE. STREAM is the stream created by
1658 dump_begin. */
1659
1660void
1661dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
1662{
1663 if (stream != stderr && stream != stdout)
1664 fclose (stream);
1665}
1666
1667/* Enable all tree dumps with FLAGS on FILENAME. Return number of
1668 enabled tree dumps. */
1669
47e0da37
DM
1670int
1671gcc::dump_manager::
8264c84d 1672dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename)
b5f47924 1673{
b5f47924
SS
1674 int n = 0;
1675 size_t i;
1676
1677 for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
1678 {
5433e401 1679 if (dump_files[i].dkind == dkind)
b5f47924
SS
1680 {
1681 const char *old_filename = dump_files[i].pfilename;
1682 dump_files[i].pstate = -1;
1683 dump_files[i].pflags |= flags;
1684 n++;
1685 /* Override the existing filename. */
1686 if (filename)
1687 {
1688 dump_files[i].pfilename = xstrdup (filename);
1689 /* Since it is a command-line provided file, which is
1690 common to all the phases, use it in append mode. */
1691 dump_files[i].pstate = 1;
1692 }
1693 if (old_filename && filename != old_filename)
1694 free (CONST_CAST (char *, old_filename));
1695 }
1696 }
1697
47e0da37 1698 for (i = 0; i < m_extra_dump_files_in_use; i++)
b5f47924 1699 {
5433e401 1700 if (m_extra_dump_files[i].dkind == dkind)
b5f47924 1701 {
47e0da37
DM
1702 const char *old_filename = m_extra_dump_files[i].pfilename;
1703 m_extra_dump_files[i].pstate = -1;
1704 m_extra_dump_files[i].pflags |= flags;
b5f47924
SS
1705 n++;
1706 /* Override the existing filename. */
1707 if (filename)
1708 {
47e0da37 1709 m_extra_dump_files[i].pfilename = xstrdup (filename);
b5f47924
SS
1710 /* Since it is a command-line provided file, which is
1711 common to all the phases, use it in append mode. */
47e0da37 1712 m_extra_dump_files[i].pstate = 1;
b5f47924
SS
1713 }
1714 if (old_filename && filename != old_filename)
1715 free (CONST_CAST (char *, old_filename));
1716 }
1717 }
1718
1719 return n;
1720}
1721
2b4e6bf1
SS
1722/* Enable -fopt-info dumps on all dump files matching OPTGROUP_FLAGS.
1723 Enable dumps with FLAGS on FILENAME. Return the number of enabled
1724 dumps. */
b5f47924 1725
47e0da37
DM
1726int
1727gcc::dump_manager::
4af78ef8 1728opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
1a817418 1729 const char *filename)
b5f47924
SS
1730{
1731 int n = 0;
b5f47924 1732
5d98e5a6
DM
1733 m_optgroup_flags = optgroup_flags;
1734 m_optinfo_flags = flags;
1735 m_optinfo_filename = xstrdup (filename);
b5f47924 1736
5d98e5a6
DM
1737 for (size_t i = TDI_none + 1; i < (size_t) TDI_end; i++)
1738 if (update_dfi_for_opt_info (&dump_files[i]))
1739 n++;
1740
1741 for (size_t i = 0; i < m_extra_dump_files_in_use; i++)
1742 if (update_dfi_for_opt_info (&m_extra_dump_files[i]))
1743 n++;
b5f47924
SS
1744
1745 return n;
1746}
1747
5d98e5a6
DM
1748/* Use the saved -fopt-info options to update DFI.
1749 Return true if the dump is enabled. */
1750
1751bool
1752gcc::dump_manager::update_dfi_for_opt_info (dump_file_info *dfi) const
1753{
1754 gcc_assert (dfi);
1755
1756 if (!(dfi->optgroup_flags & m_optgroup_flags))
1757 return false;
1758
1759 const char *old_filename = dfi->alt_filename;
1760 /* Since this file is shared among different passes, it
1761 should be opened in append mode. */
1762 dfi->alt_state = 1;
1763 dfi->alt_flags |= m_optinfo_flags;
1764 /* Override the existing filename. */
1765 if (m_optinfo_filename)
1766 dfi->alt_filename = xstrdup (m_optinfo_filename);
1767 if (old_filename && m_optinfo_filename != old_filename)
1768 free (CONST_CAST (char *, old_filename));
1769
1770 return true;
1771}
1772
b5f47924
SS
1773/* Parse ARG as a dump switch. Return nonzero if it is, and store the
1774 relevant details in the dump_files array. */
1775
47e0da37
DM
1776int
1777gcc::dump_manager::
b5f47924
SS
1778dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob)
1779{
1780 const char *option_value;
1781 const char *ptr;
1a817418 1782 dump_flags_t flags;
b5f47924
SS
1783
1784 if (doglob && !dfi->glob)
1785 return 0;
1786
1787 option_value = skip_leading_substring (arg, doglob ? dfi->glob : dfi->swtch);
1788 if (!option_value)
1789 return 0;
1790
1791 if (*option_value && *option_value != '-' && *option_value != '=')
1792 return 0;
1793
1794 ptr = option_value;
f4ebbd24
DM
1795
1796 /* Retain "user-facing" and "internals" messages, but filter out
1797 those from an opt_problem being re-emitted at the top level
1798 (MSG_PRIORITY_REEMITTED), so as to avoid duplicate messages
1799 messing up scan-tree-dump-times" in DejaGnu tests. */
1800 flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_INTERNALS;
b5f47924
SS
1801
1802 while (*ptr)
1803 {
4af78ef8 1804 const struct kv_pair<dump_flags_t> *option_ptr;
b5f47924
SS
1805 const char *end_ptr;
1806 const char *eq_ptr;
1807 unsigned length;
1808
1809 while (*ptr == '-')
1810 ptr++;
1811 end_ptr = strchr (ptr, '-');
1812 eq_ptr = strchr (ptr, '=');
1813
1814 if (eq_ptr && !end_ptr)
1815 end_ptr = eq_ptr;
1816
1817 if (!end_ptr)
1818 end_ptr = ptr + strlen (ptr);
1819 length = end_ptr - ptr;
1820
1821 for (option_ptr = dump_options; option_ptr->name; option_ptr++)
1822 if (strlen (option_ptr->name) == length
1823 && !memcmp (option_ptr->name, ptr, length))
1824 {
1825 flags |= option_ptr->value;
1826 goto found;
1827 }
1828
1829 if (*ptr == '=')
1830 {
1831 /* Interpret rest of the argument as a dump filename. This
1832 filename overrides other command line filenames. */
1833 if (dfi->pfilename)
1834 free (CONST_CAST (char *, dfi->pfilename));
1835 dfi->pfilename = xstrdup (ptr + 1);
1836 break;
1837 }
1838 else
1839 warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
1840 length, ptr, dfi->swtch);
1841 found:;
1842 ptr = end_ptr;
1843 }
1844
1845 dfi->pstate = -1;
1846 dfi->pflags |= flags;
1847
1848 /* Process -fdump-tree-all and -fdump-rtl-all, by enabling all the
1849 known dumps. */
1850 if (dfi->suffix == NULL)
8264c84d 1851 dump_enable_all (dfi->dkind, dfi->pflags, dfi->pfilename);
b5f47924
SS
1852
1853 return 1;
1854}
1855
1856int
47e0da37 1857gcc::dump_manager::
b5f47924
SS
1858dump_switch_p (const char *arg)
1859{
1860 size_t i;
1861 int any = 0;
1862
1863 for (i = TDI_none + 1; i != TDI_end; i++)
1864 any |= dump_switch_p_1 (arg, &dump_files[i], false);
1865
1866 /* Don't glob if we got a hit already */
1867 if (!any)
1868 for (i = TDI_none + 1; i != TDI_end; i++)
1869 any |= dump_switch_p_1 (arg, &dump_files[i], true);
1870
47e0da37
DM
1871 for (i = 0; i < m_extra_dump_files_in_use; i++)
1872 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], false);
b5f47924
SS
1873
1874 if (!any)
47e0da37
DM
1875 for (i = 0; i < m_extra_dump_files_in_use; i++)
1876 any |= dump_switch_p_1 (arg, &m_extra_dump_files[i], true);
b5f47924
SS
1877
1878
1879 return any;
1880}
1881
2b4e6bf1
SS
1882/* Parse ARG as a -fopt-info switch and store flags, optgroup_flags
1883 and filename. Return non-zero if it is a recognized switch. */
b5f47924
SS
1884
1885static int
4af78ef8
DM
1886opt_info_switch_p_1 (const char *arg, dump_flags_t *flags,
1887 optgroup_flags_t *optgroup_flags, char **filename)
b5f47924
SS
1888{
1889 const char *option_value;
1890 const char *ptr;
1891
1892 option_value = arg;
1893 ptr = option_value;
1894
1895 *filename = NULL;
7db960c5
DM
1896
1897 /* Default to filtering out "internals" messages, and retaining
f4ebbd24
DM
1898 "user-facing" messages, and those from an opt_problem being
1899 re-emitted at the top level. */
1900 *flags = MSG_PRIORITY_USER_FACING | MSG_PRIORITY_REEMITTED;
7db960c5 1901
4af78ef8 1902 *optgroup_flags = OPTGROUP_NONE;
b5f47924
SS
1903
1904 if (!ptr)
2b4e6bf1 1905 return 1; /* Handle '-fopt-info' without any additional options. */
b5f47924
SS
1906
1907 while (*ptr)
1908 {
b5f47924
SS
1909 const char *end_ptr;
1910 const char *eq_ptr;
1911 unsigned length;
1912
1913 while (*ptr == '-')
1914 ptr++;
1915 end_ptr = strchr (ptr, '-');
1916 eq_ptr = strchr (ptr, '=');
1917
1918 if (eq_ptr && !end_ptr)
1919 end_ptr = eq_ptr;
1920
1921 if (!end_ptr)
1922 end_ptr = ptr + strlen (ptr);
1923 length = end_ptr - ptr;
1924
4af78ef8
DM
1925 for (const kv_pair<dump_flags_t> *option_ptr = optinfo_verbosity_options;
1926 option_ptr->name; option_ptr++)
b5f47924
SS
1927 if (strlen (option_ptr->name) == length
1928 && !memcmp (option_ptr->name, ptr, length))
1929 {
1930 *flags |= option_ptr->value;
1931 goto found;
1932 }
1933
4af78ef8
DM
1934 for (const kv_pair<optgroup_flags_t> *option_ptr = optgroup_options;
1935 option_ptr->name; option_ptr++)
2b4e6bf1
SS
1936 if (strlen (option_ptr->name) == length
1937 && !memcmp (option_ptr->name, ptr, length))
1938 {
1939 *optgroup_flags |= option_ptr->value;
1940 goto found;
1941 }
1942
b5f47924
SS
1943 if (*ptr == '=')
1944 {
1945 /* Interpret rest of the argument as a dump filename. This
1946 filename overrides other command line filenames. */
1947 *filename = xstrdup (ptr + 1);
1948 break;
1949 }
1950 else
2b4e6bf1
SS
1951 {
1952 warning (0, "unknown option %q.*s in %<-fopt-info-%s%>",
1953 length, ptr, arg);
1954 return 0;
1955 }
b5f47924
SS
1956 found:;
1957 ptr = end_ptr;
1958 }
1959
1960 return 1;
1961}
1962
1963/* Return non-zero if ARG is a recognized switch for
1964 -fopt-info. Return zero otherwise. */
1965
1966int
1967opt_info_switch_p (const char *arg)
1968{
1a817418 1969 dump_flags_t flags;
4af78ef8 1970 optgroup_flags_t optgroup_flags;
b5f47924 1971 char *filename;
2b4e6bf1 1972 static char *file_seen = NULL;
47e0da37 1973 gcc::dump_manager *dumps = g->get_dumps ();
b5f47924 1974
2b4e6bf1
SS
1975 if (!opt_info_switch_p_1 (arg, &flags, &optgroup_flags, &filename))
1976 return 0;
b5f47924
SS
1977
1978 if (!filename)
1979 filename = xstrdup ("stderr");
2b4e6bf1
SS
1980
1981 /* Bail out if a different filename has been specified. */
1982 if (file_seen && strcmp (file_seen, filename))
1983 {
1984 warning (0, "ignoring possibly conflicting option %<-fopt-info-%s%>",
1985 arg);
1986 return 1;
1987 }
1988
1989 file_seen = xstrdup (filename);
7db960c5
DM
1990 if (!(flags & MSG_ALL_KINDS))
1991 flags |= MSG_OPTIMIZED_LOCATIONS;
2b4e6bf1
SS
1992 if (!optgroup_flags)
1993 optgroup_flags = OPTGROUP_ALL;
b5f47924 1994
47e0da37 1995 return dumps->opt_info_enable_passes (optgroup_flags, flags, filename);
b5f47924
SS
1996}
1997
b5f47924
SS
1998/* Print basic block on the dump streams. */
1999
2000void
4af78ef8 2001dump_basic_block (dump_flags_t dump_kind, basic_block bb, int indent)
b5f47924 2002{
7db960c5
DM
2003 if (dump_file
2004 && dump_context::get ().apply_dump_filter_p (dump_kind, pflags))
b5f47924 2005 dump_bb (dump_file, bb, indent, TDF_DETAILS);
7db960c5
DM
2006 if (alt_dump_file
2007 && dump_context::get ().apply_dump_filter_p (dump_kind, alt_flags))
b5f47924
SS
2008 dump_bb (alt_dump_file, bb, indent, TDF_DETAILS);
2009}
2010
c24e924f
NS
2011/* Dump FUNCTION_DECL FN as tree dump PHASE. */
2012
2013void
2014dump_function (int phase, tree fn)
2015{
2016 FILE *stream;
1a817418 2017 dump_flags_t flags;
c24e924f
NS
2018
2019 stream = dump_begin (phase, &flags);
2020 if (stream)
2021 {
2022 dump_function_to_file (fn, stream, flags);
2023 dump_end (phase, stream);
2024 }
2025}
2026
b5f47924
SS
2027/* Print information from the combine pass on dump_file. */
2028
2029void
2030print_combine_total_stats (void)
2031{
2032 if (dump_file)
2033 dump_combine_total_stats (dump_file);
2034}
2035
2036/* Enable RTL dump for all the RTL passes. */
2037
2038bool
2039enable_rtl_dump_file (void)
2040{
47e0da37
DM
2041 gcc::dump_manager *dumps = g->get_dumps ();
2042 int num_enabled =
8264c84d
ML
2043 dumps->dump_enable_all (DK_rtl, dump_flags_t (TDF_DETAILS) | TDF_BLOCKS,
2044 NULL);
47e0da37 2045 return num_enabled > 0;
b5f47924 2046}
4f5b9c80
DM
2047
2048#if CHECKING_P
2049
f4ebbd24
DM
2050namespace selftest {
2051
4df3629e
DM
2052/* temp_dump_context's ctor. Temporarily override the dump_context
2053 (to forcibly enable optinfo-generation). */
2054
b84e3bde 2055temp_dump_context::temp_dump_context (bool forcibly_enable_optinfo,
f4ebbd24 2056 bool forcibly_enable_dumping,
b84e3bde 2057 dump_flags_t test_pp_flags)
4df3629e
DM
2058: m_context (),
2059 m_saved (&dump_context ().get ())
2060{
2061 dump_context::s_current = &m_context;
8d1af516
DM
2062 if (forcibly_enable_optinfo)
2063 m_context.set_json_writer (new optrecord_json_writer ());
f4ebbd24
DM
2064 /* Conditionally enable the test dump, so that we can verify both the
2065 dump_enabled_p and the !dump_enabled_p cases in selftests. */
2066 if (forcibly_enable_dumping)
2067 {
2068 m_context.m_test_pp = &m_pp;
2069 m_context.m_test_pp_flags = test_pp_flags;
2070 }
b84e3bde
DM
2071
2072 dump_context::get ().refresh_dumps_are_enabled ();
4df3629e
DM
2073}
2074
2075/* temp_dump_context's dtor. Restore the saved dump_context. */
2076
2077temp_dump_context::~temp_dump_context ()
2078{
8d1af516
DM
2079 m_context.set_json_writer (NULL);
2080
4df3629e 2081 dump_context::s_current = m_saved;
b84e3bde
DM
2082
2083 dump_context::get ().refresh_dumps_are_enabled ();
2084}
2085
2086/* 0-terminate the text dumped so far, and return it. */
2087
2088const char *
2089temp_dump_context::get_dumped_text ()
2090{
2091 return pp_formatted_text (&m_pp);
4df3629e
DM
2092}
2093
3da39f52
DM
2094/* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2095 from EXPECTED_FUNCTION, using LOC for the location of any failure,
2096 provided that the build compiler is sufficiently recent. */
2097
2098static void
2099assert_impl_location_eq (const location &loc ATTRIBUTE_UNUSED,
2100 const dump_impl_location_t &impl_loc ATTRIBUTE_UNUSED,
2101 const char *expected_file ATTRIBUTE_UNUSED,
2102 int expected_line ATTRIBUTE_UNUSED,
2103 const char *expected_function ATTRIBUTE_UNUSED)
2104{
2105#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
2106 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_file, expected_file);
2107 ASSERT_EQ_AT (loc, impl_loc.m_line, expected_line);
2108 ASSERT_STR_CONTAINS_AT (loc, impl_loc.m_function, expected_function);
2109#endif
2110}
2111
2112/* Verify that IMPL_LOC is within EXPECTED_FILE at EXPECTED_LINE,
2113 from EXPECTED_FUNCTION, provided that the build compiler is
2114 sufficiently recent. */
2115
2116#define ASSERT_IMPL_LOCATION_EQ(IMPL_LOC, EXPECTED_FILE, EXPECTED_LINE, \
2117 EXPECTED_FUNCTION) \
2118 SELFTEST_BEGIN_STMT \
2119 assert_impl_location_eq (SELFTEST_LOCATION, IMPL_LOC, \
2120 EXPECTED_FILE, EXPECTED_LINE, \
2121 EXPECTED_FUNCTION); \
2122 SELFTEST_END_STMT
2123
4f5b9c80
DM
2124/* Verify that the dump_location_t constructors capture the source location
2125 at which they were called (provided that the build compiler is sufficiently
2126 recent). */
2127
2128static void
2129test_impl_location ()
2130{
4f5b9c80
DM
2131 /* Default ctor. */
2132 {
2133 dump_location_t loc;
2134 const int expected_line = __LINE__ - 1;
3da39f52
DM
2135 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2136 "dumpfile.c", expected_line, "test_impl_location");
4f5b9c80
DM
2137 }
2138
2139 /* Constructing from a gimple. */
2140 {
2141 dump_location_t loc ((gimple *)NULL);
2142 const int expected_line = __LINE__ - 1;
3da39f52
DM
2143 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2144 "dumpfile.c", expected_line, "test_impl_location");
4f5b9c80
DM
2145 }
2146
2147 /* Constructing from an rtx_insn. */
2148 {
2149 dump_location_t loc ((rtx_insn *)NULL);
2150 const int expected_line = __LINE__ - 1;
3da39f52
DM
2151 ASSERT_IMPL_LOCATION_EQ (loc.get_impl_location (),
2152 "dumpfile.c", expected_line, "test_impl_location");
4f5b9c80 2153 }
4f5b9c80
DM
2154}
2155
b84e3bde
DM
2156/* Verify that the text dumped so far in CONTEXT equals
2157 EXPECTED_TEXT, using LOC for the location of any failure.
2158 As a side-effect, the internal buffer is 0-terminated. */
2159
f4ebbd24 2160void
b84e3bde
DM
2161verify_dumped_text (const location &loc,
2162 temp_dump_context *context,
2163 const char *expected_text)
2164{
2165 gcc_assert (context);
2166 ASSERT_STREQ_AT (loc, context->get_dumped_text (),
2167 expected_text);
2168}
2169
4df3629e
DM
2170/* Verify that ITEM has the expected values. */
2171
f4ebbd24 2172void
4df3629e
DM
2173verify_item (const location &loc,
2174 const optinfo_item *item,
2175 enum optinfo_item_kind expected_kind,
2176 location_t expected_location,
2177 const char *expected_text)
2178{
2179 ASSERT_EQ_AT (loc, item->get_kind (), expected_kind);
2180 ASSERT_EQ_AT (loc, item->get_location (), expected_location);
2181 ASSERT_STREQ_AT (loc, item->get_text (), expected_text);
2182}
2183
4df3629e
DM
2184/* Verify that calls to the dump_* API are captured and consolidated into
2185 optimization records. */
2186
2187static void
2188test_capture_of_dump_calls (const line_table_case &case_)
2189{
2190 /* Generate a location_t for testing. */
2191 line_table_test ltt (case_);
2192 linemap_add (line_table, LC_ENTER, false, "test.txt", 0);
2193 linemap_line_start (line_table, 5, 100);
2194 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
d8010ee4
DM
2195 location_t decl_loc = linemap_position_for_column (line_table, 8);
2196 location_t stmt_loc = linemap_position_for_column (line_table, 10);
2197 if (stmt_loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
b84e3bde 2198 return;
4df3629e 2199
3da39f52 2200 dump_user_location_t loc = dump_user_location_t::from_location_t (stmt_loc);
4df3629e 2201
6f795a92 2202 gimple *stmt = gimple_build_return (NULL);
d8010ee4 2203 gimple_set_location (stmt, stmt_loc);
4df3629e 2204
d8010ee4 2205 tree test_decl = build_decl (decl_loc, FUNCTION_DECL,
6f795a92 2206 get_identifier ("test_decl"),
d8010ee4
DM
2207 build_function_type_list (void_type_node,
2208 NULL_TREE));
2209
2210 symbol_table_test tmp_symtab;
2211
2212 cgraph_node *node = cgraph_node::get_create (test_decl);
2213 gcc_assert (node);
2214
b84e3bde
DM
2215 /* Run all tests twice, with and then without optinfo enabled, to ensure
2216 that immediate destinations vs optinfo-based destinations both
2217 work, independently of each other, with no leaks. */
2218 for (int i = 0 ; i < 2; i++)
4df3629e 2219 {
b84e3bde
DM
2220 bool with_optinfo = (i == 0);
2221
2222 /* Test of dump_printf. */
2223 {
f4ebbd24 2224 temp_dump_context tmp (with_optinfo, true,
7db960c5 2225 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2226 dump_printf (MSG_NOTE, "int: %i str: %s", 42, "foo");
3da39f52 2227 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2228
2229 ASSERT_DUMPED_TEXT_EQ (tmp, "int: 42 str: foo");
2230 if (with_optinfo)
2231 {
2232 optinfo *info = tmp.get_pending_optinfo ();
2233 ASSERT_TRUE (info != NULL);
2234 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2235 ASSERT_EQ (info->num_items (), 1);
2236 ASSERT_IS_TEXT (info->get_item (0), "int: 42 str: foo");
3da39f52
DM
2237 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2238 "dumpfile.c", expected_impl_line,
2239 "test_capture_of_dump_calls");
b84e3bde
DM
2240 }
2241 }
2242
6f795a92
DM
2243 /* Test of dump_printf with %T. */
2244 {
f4ebbd24 2245 temp_dump_context tmp (with_optinfo, true,
7db960c5 2246 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
6f795a92 2247 dump_printf (MSG_NOTE, "tree: %T", integer_zero_node);
3da39f52 2248 const int expected_impl_line = __LINE__ - 1;
6f795a92
DM
2249
2250 ASSERT_DUMPED_TEXT_EQ (tmp, "tree: 0");
2251 if (with_optinfo)
2252 {
2253 optinfo *info = tmp.get_pending_optinfo ();
2254 ASSERT_TRUE (info != NULL);
2255 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2256 ASSERT_EQ (info->num_items (), 2);
2257 ASSERT_IS_TEXT (info->get_item (0), "tree: ");
2258 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
3da39f52
DM
2259 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2260 "dumpfile.c", expected_impl_line,
2261 "test_capture_of_dump_calls");
6f795a92
DM
2262 }
2263 }
2264
2265 /* Test of dump_printf with %E. */
2266 {
f4ebbd24 2267 temp_dump_context tmp (with_optinfo, true,
7db960c5 2268 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
6f795a92 2269 dump_printf (MSG_NOTE, "gimple: %E", stmt);
3da39f52 2270 const int expected_impl_line = __LINE__ - 1;
6f795a92
DM
2271
2272 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;");
2273 if (with_optinfo)
2274 {
2275 optinfo *info = tmp.get_pending_optinfo ();
2276 ASSERT_TRUE (info != NULL);
2277 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2278 ASSERT_EQ (info->num_items (), 2);
2279 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
d8010ee4 2280 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;");
3da39f52
DM
2281 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2282 "dumpfile.c", expected_impl_line,
2283 "test_capture_of_dump_calls");
6f795a92
DM
2284 }
2285 }
2286
2287 /* Test of dump_printf with %G. */
2288 {
f4ebbd24 2289 temp_dump_context tmp (with_optinfo, true,
7db960c5 2290 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
6f795a92 2291 dump_printf (MSG_NOTE, "gimple: %G", stmt);
3da39f52 2292 const int expected_impl_line = __LINE__ - 1;
6f795a92
DM
2293
2294 ASSERT_DUMPED_TEXT_EQ (tmp, "gimple: return;\n");
2295 if (with_optinfo)
2296 {
2297 optinfo *info = tmp.get_pending_optinfo ();
2298 ASSERT_TRUE (info != NULL);
2299 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2300 ASSERT_EQ (info->num_items (), 2);
2301 ASSERT_IS_TEXT (info->get_item (0), "gimple: ");
d8010ee4 2302 ASSERT_IS_GIMPLE (info->get_item (1), stmt_loc, "return;\n");
3da39f52
DM
2303 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2304 "dumpfile.c", expected_impl_line,
2305 "test_capture_of_dump_calls");
d8010ee4
DM
2306 }
2307 }
2308
2309 /* Test of dump_printf with %C. */
2310 {
2311 temp_dump_context tmp (with_optinfo, true,
2312 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2313 dump_printf (MSG_NOTE, "node: %C", node);
3da39f52 2314 const int expected_impl_line = __LINE__ - 1;
d8010ee4
DM
2315
2316 ASSERT_DUMPED_TEXT_EQ (tmp, "node: test_decl/0");
2317 if (with_optinfo)
2318 {
2319 optinfo *info = tmp.get_pending_optinfo ();
2320 ASSERT_TRUE (info != NULL);
2321 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2322 ASSERT_EQ (info->num_items (), 2);
2323 ASSERT_IS_TEXT (info->get_item (0), "node: ");
2324 ASSERT_IS_SYMTAB_NODE (info->get_item (1), decl_loc, "test_decl/0");
3da39f52
DM
2325 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2326 "dumpfile.c", expected_impl_line,
2327 "test_capture_of_dump_calls");
6f795a92
DM
2328 }
2329 }
2330
2331 /* dump_print_loc with multiple format codes. This tests various
2332 things:
2333 - intermingling of text, format codes handled by the base
2334 pretty_printer, and dump-specific format codes
2335 - multiple dump-specific format codes: some consecutive, others
2336 separated by text, trailing text after the final one. */
2337 {
f4ebbd24 2338 temp_dump_context tmp (with_optinfo, true,
7db960c5 2339 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
6f795a92
DM
2340 dump_printf_loc (MSG_NOTE, loc, "before %T and %T"
2341 " %i consecutive %E%E after\n",
2342 integer_zero_node, test_decl, 42, stmt, stmt);
2343
2344 ASSERT_DUMPED_TEXT_EQ (tmp,
2345 "test.txt:5:10: note: before 0 and test_decl"
2346 " 42 consecutive return;return; after\n");
2347 if (with_optinfo)
2348 {
2349 optinfo *info = tmp.get_pending_optinfo ();
2350 ASSERT_TRUE (info != NULL);
2351 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2352 ASSERT_EQ (info->num_items (), 8);
2353 ASSERT_IS_TEXT (info->get_item (0), "before ");
2354 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
2355 ASSERT_IS_TEXT (info->get_item (2), " and ");
2356 ASSERT_IS_TREE (info->get_item (3), UNKNOWN_LOCATION, "test_decl");
2357 ASSERT_IS_TEXT (info->get_item (4), " 42 consecutive ");
d8010ee4
DM
2358 ASSERT_IS_GIMPLE (info->get_item (5), stmt_loc, "return;");
2359 ASSERT_IS_GIMPLE (info->get_item (6), stmt_loc, "return;");
6f795a92 2360 ASSERT_IS_TEXT (info->get_item (7), " after\n");
3da39f52
DM
2361 /* We don't ASSERT_IMPL_LOCATION_EQ here, to avoid having to
2362 enforce at which exact line the multiline dump_printf_loc
2363 occurred. */
6f795a92
DM
2364 }
2365 }
2366
b84e3bde
DM
2367 /* Tree, via dump_generic_expr. */
2368 {
f4ebbd24 2369 temp_dump_context tmp (with_optinfo, true,
7db960c5 2370 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2371 dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
3da39f52 2372 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2373 dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
2374
2375 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: test of tree: 0");
2376 if (with_optinfo)
2377 {
2378 optinfo *info = tmp.get_pending_optinfo ();
2379 ASSERT_TRUE (info != NULL);
d8010ee4 2380 ASSERT_EQ (info->get_location_t (), stmt_loc);
b84e3bde
DM
2381 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2382 ASSERT_EQ (info->num_items (), 2);
2383 ASSERT_IS_TEXT (info->get_item (0), "test of tree: ");
2384 ASSERT_IS_TREE (info->get_item (1), UNKNOWN_LOCATION, "0");
3da39f52
DM
2385 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2386 "dumpfile.c", expected_impl_line,
2387 "test_capture_of_dump_calls");
b84e3bde
DM
2388 }
2389 }
2390
2391 /* Tree, via dump_generic_expr_loc. */
2392 {
f4ebbd24 2393 temp_dump_context tmp (with_optinfo, true,
7db960c5 2394 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2395 dump_generic_expr_loc (MSG_NOTE, loc, TDF_SLIM, integer_one_node);
3da39f52 2396 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2397
2398 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: 1");
2399 if (with_optinfo)
2400 {
2401 optinfo *info = tmp.get_pending_optinfo ();
2402 ASSERT_TRUE (info != NULL);
d8010ee4 2403 ASSERT_EQ (info->get_location_t (), stmt_loc);
b84e3bde
DM
2404 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2405 ASSERT_EQ (info->num_items (), 1);
2406 ASSERT_IS_TREE (info->get_item (0), UNKNOWN_LOCATION, "1");
3da39f52
DM
2407 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2408 "dumpfile.c", expected_impl_line,
2409 "test_capture_of_dump_calls");
b84e3bde
DM
2410 }
2411 }
2412
2413 /* Gimple. */
2414 {
2415 /* dump_gimple_stmt_loc. */
2416 {
f4ebbd24 2417 temp_dump_context tmp (with_optinfo, true,
7db960c5 2418 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2419 dump_gimple_stmt_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
3da39f52 2420 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2421
2422 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;\n");
2423 if (with_optinfo)
2424 {
2425 optinfo *info = tmp.get_pending_optinfo ();
2426 ASSERT_TRUE (info != NULL);
2427 ASSERT_EQ (info->num_items (), 1);
d8010ee4 2428 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
3da39f52
DM
2429 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2430 "dumpfile.c", expected_impl_line,
2431 "test_capture_of_dump_calls");
b84e3bde
DM
2432 }
2433 }
4df3629e 2434
b84e3bde
DM
2435 /* dump_gimple_stmt. */
2436 {
f4ebbd24 2437 temp_dump_context tmp (with_optinfo, true,
7db960c5 2438 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2439 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 2);
3da39f52 2440 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2441
2442 ASSERT_DUMPED_TEXT_EQ (tmp, "return;\n");
2443 if (with_optinfo)
2444 {
2445 optinfo *info = tmp.get_pending_optinfo ();
2446 ASSERT_TRUE (info != NULL);
2447 ASSERT_EQ (info->num_items (), 1);
d8010ee4 2448 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;\n");
3da39f52
DM
2449 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2450 "dumpfile.c", expected_impl_line,
2451 "test_capture_of_dump_calls");
b84e3bde
DM
2452 }
2453 }
4df3629e 2454
b84e3bde
DM
2455 /* dump_gimple_expr_loc. */
2456 {
f4ebbd24 2457 temp_dump_context tmp (with_optinfo, true,
7db960c5 2458 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2459 dump_gimple_expr_loc (MSG_NOTE, loc, TDF_SLIM, stmt, 2);
3da39f52 2460 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2461
2462 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: note: return;");
2463 if (with_optinfo)
2464 {
2465 optinfo *info = tmp.get_pending_optinfo ();
2466 ASSERT_TRUE (info != NULL);
2467 ASSERT_EQ (info->num_items (), 1);
d8010ee4 2468 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
3da39f52
DM
2469 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2470 "dumpfile.c", expected_impl_line,
2471 "test_capture_of_dump_calls");
b84e3bde
DM
2472 }
2473 }
4df3629e 2474
b84e3bde
DM
2475 /* dump_gimple_expr. */
2476 {
f4ebbd24 2477 temp_dump_context tmp (with_optinfo, true,
7db960c5 2478 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2479 dump_gimple_expr (MSG_NOTE, TDF_SLIM, stmt, 2);
3da39f52 2480 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2481
2482 ASSERT_DUMPED_TEXT_EQ (tmp, "return;");
2483 if (with_optinfo)
2484 {
2485 optinfo *info = tmp.get_pending_optinfo ();
2486 ASSERT_TRUE (info != NULL);
2487 ASSERT_EQ (info->num_items (), 1);
d8010ee4 2488 ASSERT_IS_GIMPLE (info->get_item (0), stmt_loc, "return;");
3da39f52
DM
2489 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2490 "dumpfile.c", expected_impl_line,
2491 "test_capture_of_dump_calls");
b84e3bde
DM
2492 }
2493 }
2494 }
2495
d8010ee4
DM
2496 /* symtab_node. */
2497 {
2498 temp_dump_context tmp (with_optinfo, true,
2499 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2500 dump_symtab_node (MSG_NOTE, node);
3da39f52 2501 const int expected_impl_line = __LINE__ - 1;
d8010ee4
DM
2502
2503 ASSERT_DUMPED_TEXT_EQ (tmp, "test_decl/0");
2504 if (with_optinfo)
2505 {
2506 optinfo *info = tmp.get_pending_optinfo ();
2507 ASSERT_TRUE (info != NULL);
2508 ASSERT_EQ (info->get_kind (), OPTINFO_KIND_NOTE);
2509 ASSERT_EQ (info->num_items (), 1);
2510 ASSERT_IS_SYMTAB_NODE (info->get_item (0), decl_loc, "test_decl/0");
3da39f52
DM
2511 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2512 "dumpfile.c", expected_impl_line,
2513 "test_capture_of_dump_calls");
d8010ee4
DM
2514 }
2515 }
2516
b84e3bde
DM
2517 /* poly_int. */
2518 {
f4ebbd24 2519 temp_dump_context tmp (with_optinfo, true,
7db960c5 2520 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
b84e3bde 2521 dump_dec (MSG_NOTE, poly_int64 (42));
3da39f52 2522 const int expected_impl_line = __LINE__ - 1;
b84e3bde
DM
2523
2524 ASSERT_DUMPED_TEXT_EQ (tmp, "42");
2525 if (with_optinfo)
2526 {
2527 optinfo *info = tmp.get_pending_optinfo ();
2528 ASSERT_TRUE (info != NULL);
2529 ASSERT_EQ (info->num_items (), 1);
2530 ASSERT_IS_TEXT (info->get_item (0), "42");
3da39f52
DM
2531 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2532 "dumpfile.c", expected_impl_line,
2533 "test_capture_of_dump_calls");
b84e3bde
DM
2534 }
2535 }
2536
7db960c5
DM
2537 /* Scopes. Test with all 4 combinations of
2538 filtering by MSG_PRIORITY_USER_FACING
2539 and/or filtering by MSG_PRIORITY_INTERNALS. */
2540 for (int j = 0; j < 3; j++)
b84e3bde 2541 {
7db960c5
DM
2542 dump_flags_t dump_filter = MSG_ALL_KINDS;
2543 if (j % 2)
2544 dump_filter |= MSG_PRIORITY_USER_FACING;
2545 if (j / 2)
2546 dump_filter |= MSG_PRIORITY_INTERNALS;
2547
f4ebbd24 2548 temp_dump_context tmp (with_optinfo, true, dump_filter);
7db960c5
DM
2549 /* Emit various messages, mostly with implicit priority. */
2550 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2551 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, stmt,
2552 "explicitly internal msg\n");
b84e3bde 2553 {
7db960c5
DM
2554 AUTO_DUMP_SCOPE ("outer scope", stmt);
2555 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
b84e3bde 2556 {
7db960c5
DM
2557 AUTO_DUMP_SCOPE ("middle scope", stmt);
2558 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2559 {
2560 AUTO_DUMP_SCOPE ("inner scope", stmt);
2561 dump_printf_loc (MSG_NOTE, stmt, "msg 4\n");
2562 dump_printf_loc (MSG_NOTE | MSG_PRIORITY_USER_FACING, stmt,
2563 "explicitly user-facing msg\n");
2564 }
2565 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
b84e3bde 2566 }
7db960c5 2567 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
b84e3bde 2568 }
7db960c5 2569 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
3da39f52 2570 const int expected_impl_line = __LINE__ - 1;
b84e3bde 2571
7db960c5
DM
2572 switch (dump_filter & MSG_ALL_PRIORITIES)
2573 {
2574 default:
2575 gcc_unreachable ();
2576 case 0:
2577 ASSERT_DUMPED_TEXT_EQ (tmp, "");
2578 break;
2579 case MSG_PRIORITY_USER_FACING:
2580 ASSERT_DUMPED_TEXT_EQ
2581 (tmp,
2582 "test.txt:5:10: note: msg 1\n"
2583 "test.txt:5:10: note: explicitly user-facing msg\n"
2584 "test.txt:5:10: note: msg 7\n");
2585 break;
2586 case MSG_PRIORITY_INTERNALS:
2587 ASSERT_DUMPED_TEXT_EQ
2588 (tmp,
2589 "test.txt:5:10: note: explicitly internal msg\n"
2590 "test.txt:5:10: note: === outer scope ===\n"
2591 "test.txt:5:10: note: msg 2\n"
2592 "test.txt:5:10: note: === middle scope ===\n"
2593 "test.txt:5:10: note: msg 3\n"
2594 "test.txt:5:10: note: === inner scope ===\n"
2595 "test.txt:5:10: note: msg 4\n"
2596 "test.txt:5:10: note: msg 5\n"
2597 "test.txt:5:10: note: msg 6\n");
2598 break;
2599 case MSG_ALL_PRIORITIES:
2600 ASSERT_DUMPED_TEXT_EQ
2601 (tmp,
2602 "test.txt:5:10: note: msg 1\n"
2603 "test.txt:5:10: note: explicitly internal msg\n"
2604 "test.txt:5:10: note: === outer scope ===\n"
2605 "test.txt:5:10: note: msg 2\n"
2606 "test.txt:5:10: note: === middle scope ===\n"
2607 "test.txt:5:10: note: msg 3\n"
2608 "test.txt:5:10: note: === inner scope ===\n"
2609 "test.txt:5:10: note: msg 4\n"
2610 "test.txt:5:10: note: explicitly user-facing msg\n"
2611 "test.txt:5:10: note: msg 5\n"
2612 "test.txt:5:10: note: msg 6\n"
2613 "test.txt:5:10: note: msg 7\n");
2614 break;
2615 }
2616 if (with_optinfo)
2617 {
2618 optinfo *info = tmp.get_pending_optinfo ();
2619 ASSERT_TRUE (info != NULL);
2620 ASSERT_EQ (info->num_items (), 1);
2621 ASSERT_IS_TEXT (info->get_item (0), "msg 7\n");
3da39f52
DM
2622 ASSERT_IMPL_LOCATION_EQ (info->get_impl_location (),
2623 "dumpfile.c", expected_impl_line,
2624 "test_capture_of_dump_calls");
7db960c5
DM
2625 }
2626 }
4df3629e 2627 }
4df3629e
DM
2628
2629 /* Verify that MSG_* affects optinfo->get_kind (); we tested MSG_NOTE
2630 above. */
2631 {
2632 /* MSG_OPTIMIZED_LOCATIONS. */
2633 {
f4ebbd24 2634 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
4df3629e
DM
2635 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, "test");
2636 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2637 OPTINFO_KIND_SUCCESS);
2638 }
2639
2640 /* MSG_MISSED_OPTIMIZATION. */
2641 {
f4ebbd24 2642 temp_dump_context tmp (true, true, MSG_ALL_KINDS);
4df3629e
DM
2643 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, "test");
2644 ASSERT_EQ (tmp.get_pending_optinfo ()->get_kind (),
2645 OPTINFO_KIND_FAILURE);
2646 }
2647 }
c8d1534e
DM
2648
2649 /* Verify that MSG_* affect AUTO_DUMP_SCOPE and the dump calls. */
2650 {
f4ebbd24 2651 temp_dump_context tmp (false, true,
7db960c5 2652 MSG_OPTIMIZED_LOCATIONS | MSG_ALL_PRIORITIES);
c8d1534e
DM
2653 dump_printf_loc (MSG_NOTE, stmt, "msg 1\n");
2654 {
2655 AUTO_DUMP_SCOPE ("outer scope", stmt);
2656 dump_printf_loc (MSG_NOTE, stmt, "msg 2\n");
2657 {
2658 AUTO_DUMP_SCOPE ("middle scope", stmt);
2659 dump_printf_loc (MSG_NOTE, stmt, "msg 3\n");
2660 {
2661 AUTO_DUMP_SCOPE ("inner scope", stmt);
2662 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt, "msg 4\n");
2663 }
2664 dump_printf_loc (MSG_NOTE, stmt, "msg 5\n");
2665 }
2666 dump_printf_loc (MSG_NOTE, stmt, "msg 6\n");
2667 }
2668 dump_printf_loc (MSG_NOTE, stmt, "msg 7\n");
2669
ed2d9d37 2670 ASSERT_DUMPED_TEXT_EQ (tmp, "test.txt:5:10: optimized: msg 4\n");
c8d1534e 2671 }
4df3629e
DM
2672}
2673
c95c5528
DM
2674static void
2675test_pr87025 ()
2676{
2677 dump_user_location_t loc
2678 = dump_user_location_t::from_location_t (UNKNOWN_LOCATION);
2679
2680 temp_dump_context tmp (true, true,
2681 MSG_ALL_KINDS | MSG_PRIORITY_USER_FACING);
2682 {
2683 AUTO_DUMP_SCOPE ("outer scope", loc);
2684 dump_printf (MSG_NOTE, "msg1\n");
2685 }
2686}
2687
4f5b9c80
DM
2688/* Run all of the selftests within this file. */
2689
2690void
2691dumpfile_c_tests ()
2692{
2693 test_impl_location ();
4df3629e 2694 for_each_line_table_case (test_capture_of_dump_calls);
c95c5528 2695 test_pr87025 ();
4f5b9c80
DM
2696}
2697
2698} // namespace selftest
2699
2700#endif /* CHECKING_P */