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