]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dumpfile.h
Fix -fopt-info for plugin passes
[thirdparty/gcc.git] / gcc / dumpfile.h
1 /* Definitions for the shared dumpfile.
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License 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
21 #ifndef GCC_DUMPFILE_H
22 #define GCC_DUMPFILE_H 1
23
24 #include "profile-count.h"
25
26 /* An attribute for annotating formatting printing functions that use
27 the dumpfile/optinfo formatting codes. These are the pretty_printer
28 format codes (see pretty-print.c), with additional codes for middle-end
29 specific entities (see dumpfile.c). */
30
31 #if GCC_VERSION >= 9000
32 #define ATTRIBUTE_GCC_DUMP_PRINTF(m, n) \
33 __attribute__ ((__format__ (__gcc_dump_printf__, m ,n))) \
34 ATTRIBUTE_NONNULL(m)
35 #else
36 #define ATTRIBUTE_GCC_DUMP_PRINTF(m, n) ATTRIBUTE_NONNULL(m)
37 #endif
38
39 /* Different tree dump places. When you add new tree dump places,
40 extend the DUMP_FILES array in dumpfile.c. */
41 enum tree_dump_index
42 {
43 TDI_none, /* No dump */
44 TDI_cgraph, /* dump function call graph. */
45 TDI_inheritance, /* dump type inheritance graph. */
46 TDI_clones, /* dump IPA cloning decisions. */
47 TDI_original, /* dump each function before optimizing it */
48 TDI_gimple, /* dump each function after gimplifying it */
49 TDI_nested, /* dump each function after unnesting it */
50 TDI_lto_stream_out, /* dump information about lto streaming */
51
52 TDI_lang_all, /* enable all the language dumps. */
53 TDI_tree_all, /* enable all the GENERIC/GIMPLE dumps. */
54 TDI_rtl_all, /* enable all the RTL dumps. */
55 TDI_ipa_all, /* enable all the IPA dumps. */
56
57 TDI_end
58 };
59
60 /* Enum used to distinguish dump files to types. */
61
62 enum dump_kind
63 {
64 DK_none,
65 DK_lang,
66 DK_tree,
67 DK_rtl,
68 DK_ipa
69 };
70
71 /* Bit masks to control dumping. Not all values are applicable to all
72 dumps. Add new ones at the end. When you define new values, extend
73 the DUMP_OPTIONS array in dumpfile.c. The TDF_* flags coexist with
74 MSG_* flags (for -fopt-info) and the bit values must be chosen to
75 allow that. */
76 enum dump_flag
77 {
78 /* Value of TDF_NONE is used just for bits filtered by TDF_KIND_MASK. */
79 TDF_NONE = 0,
80
81 /* Dump node addresses. */
82 TDF_ADDRESS = (1 << 0),
83
84 /* Don't go wild following links. */
85 TDF_SLIM = (1 << 1),
86
87 /* Don't unparse the function. */
88 TDF_RAW = (1 << 2),
89
90 /* Show more detailed info about each pass. */
91 TDF_DETAILS = (1 << 3),
92
93 /* Dump various statistics about each pass. */
94 TDF_STATS = (1 << 4),
95
96 /* Display basic block boundaries. */
97 TDF_BLOCKS = (1 << 5),
98
99 /* Display virtual operands. */
100 TDF_VOPS = (1 << 6),
101
102 /* Display statement line numbers. */
103 TDF_LINENO = (1 << 7),
104
105 /* Display decl UIDs. */
106 TDF_UID = (1 << 8),
107
108 /* Address of stmt. */
109 TDF_STMTADDR = (1 << 9),
110
111 /* A graph dump is being emitted. */
112 TDF_GRAPH = (1 << 10),
113
114 /* Display memory symbols in expr.
115 Implies TDF_VOPS. */
116 TDF_MEMSYMS = (1 << 11),
117
118 /* A flag to only print the RHS of a gimple stmt. */
119 TDF_RHS_ONLY = (1 << 12),
120
121 /* Display asm names of decls. */
122 TDF_ASMNAME = (1 << 13),
123
124 /* Display EH region number holding this gimple statement. */
125 TDF_EH = (1 << 14),
126
127 /* Omit UIDs from dumps. */
128 TDF_NOUID = (1 << 15),
129
130 /* Display alias information. */
131 TDF_ALIAS = (1 << 16),
132
133 /* Enumerate locals by uid. */
134 TDF_ENUMERATE_LOCALS = (1 << 17),
135
136 /* Dump cselib details. */
137 TDF_CSELIB = (1 << 18),
138
139 /* Dump SCEV details. */
140 TDF_SCEV = (1 << 19),
141
142 /* Dump in GIMPLE FE syntax */
143 TDF_GIMPLE = (1 << 20),
144
145 /* Dump folding details. */
146 TDF_FOLDING = (1 << 21),
147
148 /* -fopt-info optimized sources. */
149 MSG_OPTIMIZED_LOCATIONS = (1 << 22),
150
151 /* Missed opportunities. */
152 MSG_MISSED_OPTIMIZATION = (1 << 23),
153
154 /* General optimization info. */
155 MSG_NOTE = (1 << 24),
156
157 MSG_ALL = (MSG_OPTIMIZED_LOCATIONS
158 | MSG_MISSED_OPTIMIZATION
159 | MSG_NOTE),
160
161 /* Dumping for -fcompare-debug. */
162 TDF_COMPARE_DEBUG = (1 << 25),
163
164 /* All values. */
165 TDF_ALL_VALUES = (1 << 26) - 1
166 };
167
168 /* Dump flags type. */
169
170 typedef enum dump_flag dump_flags_t;
171
172 static inline dump_flags_t
173 operator| (dump_flags_t lhs, dump_flags_t rhs)
174 {
175 return (dump_flags_t)((int)lhs | (int)rhs);
176 }
177
178 static inline dump_flags_t
179 operator& (dump_flags_t lhs, dump_flags_t rhs)
180 {
181 return (dump_flags_t)((int)lhs & (int)rhs);
182 }
183
184 static inline dump_flags_t
185 operator~ (dump_flags_t flags)
186 {
187 return (dump_flags_t)~((int)flags);
188 }
189
190 static inline dump_flags_t &
191 operator|= (dump_flags_t &lhs, dump_flags_t rhs)
192 {
193 lhs = (dump_flags_t)((int)lhs | (int)rhs);
194 return lhs;
195 }
196
197 static inline dump_flags_t &
198 operator&= (dump_flags_t &lhs, dump_flags_t rhs)
199 {
200 lhs = (dump_flags_t)((int)lhs & (int)rhs);
201 return lhs;
202 }
203
204 /* Flags to control high-level -fopt-info dumps. Usually these flags
205 define a group of passes. An optimization pass can be part of
206 multiple groups. */
207
208 enum optgroup_flag
209 {
210 OPTGROUP_NONE = 0,
211
212 /* IPA optimization passes */
213 OPTGROUP_IPA = (1 << 1),
214
215 /* Loop optimization passes */
216 OPTGROUP_LOOP = (1 << 2),
217
218 /* Inlining passes */
219 OPTGROUP_INLINE = (1 << 3),
220
221 /* OMP (Offloading and Multi Processing) transformations */
222 OPTGROUP_OMP = (1 << 4),
223
224 /* Vectorization passes */
225 OPTGROUP_VEC = (1 << 5),
226
227 /* All other passes */
228 OPTGROUP_OTHER = (1 << 6),
229
230 OPTGROUP_ALL = (OPTGROUP_IPA | OPTGROUP_LOOP | OPTGROUP_INLINE
231 | OPTGROUP_OMP | OPTGROUP_VEC | OPTGROUP_OTHER)
232 };
233
234 typedef enum optgroup_flag optgroup_flags_t;
235
236 static inline optgroup_flags_t
237 operator| (optgroup_flags_t lhs, optgroup_flags_t rhs)
238 {
239 return (optgroup_flags_t)((int)lhs | (int)rhs);
240 }
241
242 static inline optgroup_flags_t &
243 operator|= (optgroup_flags_t &lhs, optgroup_flags_t rhs)
244 {
245 lhs = (optgroup_flags_t)((int)lhs | (int)rhs);
246 return lhs;
247 }
248
249 /* Define a tree dump switch. */
250 struct dump_file_info
251 {
252 /* Suffix to give output file. */
253 const char *suffix;
254 /* Command line dump switch. */
255 const char *swtch;
256 /* Command line glob. */
257 const char *glob;
258 /* Filename for the pass-specific stream. */
259 const char *pfilename;
260 /* Filename for the -fopt-info stream. */
261 const char *alt_filename;
262 /* Pass-specific dump stream. */
263 FILE *pstream;
264 /* -fopt-info stream. */
265 FILE *alt_stream;
266 /* Dump kind. */
267 dump_kind dkind;
268 /* Dump flags. */
269 dump_flags_t pflags;
270 /* A pass flags for -fopt-info. */
271 dump_flags_t alt_flags;
272 /* Flags for -fopt-info given by a user. */
273 optgroup_flags_t optgroup_flags;
274 /* State of pass-specific stream. */
275 int pstate;
276 /* State of the -fopt-info stream. */
277 int alt_state;
278 /* Dump file number. */
279 int num;
280 /* Fields "suffix", "swtch", "glob" can be const strings,
281 or can be dynamically allocated, needing free. */
282 bool owns_strings;
283 /* When a given dump file is being initialized, this flag is set to true
284 if the corresponding TDF_graph dump file has also been initialized. */
285 bool graph_dump_initialized;
286 };
287
288 /* A class for describing where in the user's source that a dump message
289 relates to, with various constructors for convenience.
290 In particular, this lets us associate dump messages
291 with hotness information (e.g. from PGO), allowing them to
292 be prioritized by code hotness. */
293
294 class dump_user_location_t
295 {
296 public:
297 /* Default constructor, analogous to UNKNOWN_LOCATION. */
298 dump_user_location_t () : m_count (), m_loc (UNKNOWN_LOCATION) {}
299
300 /* Construct from a gimple statement (using its location and hotness). */
301 dump_user_location_t (const gimple *stmt);
302
303 /* Construct from an RTL instruction (using its location and hotness). */
304 dump_user_location_t (const rtx_insn *insn);
305
306 /* Construct from a location_t. This one is deprecated (since it doesn't
307 capture hotness information); it thus needs to be spelled out. */
308 static dump_user_location_t
309 from_location_t (location_t loc)
310 {
311 return dump_user_location_t (profile_count (), loc);
312 }
313
314 /* Construct from a function declaration. This one requires spelling out
315 to avoid accidentally constructing from other kinds of tree. */
316 static dump_user_location_t
317 from_function_decl (tree fndecl);
318
319 profile_count get_count () const { return m_count; }
320 location_t get_location_t () const { return m_loc; }
321
322 private:
323 /* Private ctor from count and location, for use by from_location_t. */
324 dump_user_location_t (profile_count count, location_t loc)
325 : m_count (count), m_loc (loc)
326 {}
327
328 profile_count m_count;
329 location_t m_loc;
330 };
331
332 /* A class for identifying where in the compiler's own source
333 (or a plugin) that a dump message is being emitted from. */
334
335 struct dump_impl_location_t
336 {
337 dump_impl_location_t (
338 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
339 const char *file = __builtin_FILE (),
340 int line = __builtin_LINE (),
341 const char *function = __builtin_FUNCTION ()
342 #else
343 const char *file = __FILE__,
344 int line = __LINE__,
345 const char *function = NULL
346 #endif
347 )
348 : m_file (file), m_line (line), m_function (function)
349 {}
350
351 const char *m_file;
352 int m_line;
353 const char *m_function;
354 };
355
356 /* A bundle of information for describing the location of a dump message:
357 (a) the source location and hotness within the user's code, together with
358 (b) the source location within the compiler/plugin.
359
360 The constructors use default parameters so that (b) gets sets up
361 automatically.
362
363 The upshot is that you can pass in e.g. a gimple * to dump_printf_loc,
364 and the dump call will automatically record where in GCC's source
365 code the dump was emitted from. */
366
367 class dump_location_t
368 {
369 public:
370 /* Default constructor, analogous to UNKNOWN_LOCATION. */
371 dump_location_t (const dump_impl_location_t &impl_location
372 = dump_impl_location_t ())
373 : m_user_location (dump_user_location_t ()),
374 m_impl_location (impl_location)
375 {
376 }
377
378 /* Construct from a gimple statement (using its location and hotness). */
379 dump_location_t (const gimple *stmt,
380 const dump_impl_location_t &impl_location
381 = dump_impl_location_t ())
382 : m_user_location (dump_user_location_t (stmt)),
383 m_impl_location (impl_location)
384 {
385 }
386
387 /* Construct from an RTL instruction (using its location and hotness). */
388 dump_location_t (const rtx_insn *insn,
389 const dump_impl_location_t &impl_location
390 = dump_impl_location_t ())
391 : m_user_location (dump_user_location_t (insn)),
392 m_impl_location (impl_location)
393 {
394 }
395
396 /* Construct from a dump_user_location_t. */
397 dump_location_t (const dump_user_location_t &user_location,
398 const dump_impl_location_t &impl_location
399 = dump_impl_location_t ())
400 : m_user_location (user_location),
401 m_impl_location (impl_location)
402 {
403 }
404
405 /* Construct from a location_t. This one is deprecated (since it doesn't
406 capture hotness information), and thus requires spelling out. */
407 static dump_location_t
408 from_location_t (location_t loc,
409 const dump_impl_location_t &impl_location
410 = dump_impl_location_t ())
411 {
412 return dump_location_t (dump_user_location_t::from_location_t (loc),
413 impl_location);
414 }
415
416 const dump_user_location_t &
417 get_user_location () const { return m_user_location; }
418
419 const dump_impl_location_t &
420 get_impl_location () const { return m_impl_location; }
421
422 location_t get_location_t () const
423 {
424 return m_user_location.get_location_t ();
425 }
426
427 profile_count get_count () const { return m_user_location.get_count (); }
428
429 private:
430 dump_user_location_t m_user_location;
431 dump_impl_location_t m_impl_location;
432 };
433
434 /* In dumpfile.c */
435 extern FILE *dump_begin (int, dump_flags_t *, int part=-1);
436 extern void dump_end (int, FILE *);
437 extern int opt_info_switch_p (const char *);
438 extern const char *dump_flag_name (int);
439 extern const kv_pair<optgroup_flags_t> optgroup_options[];
440
441 /* Global variables used to communicate with passes. */
442 extern FILE *dump_file;
443 extern dump_flags_t dump_flags;
444 extern const char *dump_file_name;
445
446 extern bool dumps_are_enabled;
447
448 extern void set_dump_file (FILE *new_dump_file);
449
450 /* Return true if any of the dumps is enabled, false otherwise. */
451 static inline bool
452 dump_enabled_p (void)
453 {
454 return dumps_are_enabled;
455 }
456
457 /* The following API calls (which *don't* take a "FILE *")
458 write the output to zero or more locations.
459
460 Some destinations are written to immediately as dump_* calls
461 are made; for others, the output is consolidated into an "optinfo"
462 instance (with its own metadata), and only emitted once the optinfo
463 is complete.
464
465 The destinations are:
466
467 (a) the "immediate" destinations:
468 (a.1) the active dump_file, if any
469 (a.2) the -fopt-info destination, if any
470 (b) the "optinfo" destinations, if any:
471 (b.1) as optimization records
472
473 dump_* (MSG_*) --> dumpfile.c --> items --> (a.1) dump_file
474 | `-> (a.2) alt_dump_file
475 |
476 `--> (b) optinfo
477 `---> optinfo destinations
478 (b.1) optimization records
479
480 For optinfos, the dump_*_loc mark the beginning of an optinfo
481 instance: all subsequent dump_* calls are consolidated into
482 that optinfo, until the next dump_*_loc call (or a change in
483 dump scope, or a call to dumpfile_ensure_any_optinfo_are_flushed).
484
485 A group of dump_* calls should be guarded by:
486
487 if (dump_enabled_p ())
488
489 to minimize the work done for the common case where dumps
490 are disabled. */
491
492 extern void dump_printf (dump_flags_t, const char *, ...)
493 ATTRIBUTE_GCC_DUMP_PRINTF (2, 3);
494
495 extern void dump_printf_loc (dump_flags_t, const dump_location_t &,
496 const char *, ...)
497 ATTRIBUTE_GCC_DUMP_PRINTF (3, 0);
498 extern void dump_function (int phase, tree fn);
499 extern void dump_basic_block (dump_flags_t, basic_block, int);
500 extern void dump_generic_expr_loc (dump_flags_t, const dump_location_t &,
501 dump_flags_t, tree);
502 extern void dump_generic_expr (dump_flags_t, dump_flags_t, tree);
503 extern void dump_gimple_stmt_loc (dump_flags_t, const dump_location_t &,
504 dump_flags_t, gimple *, int);
505 extern void dump_gimple_stmt (dump_flags_t, dump_flags_t, gimple *, int);
506 extern void dump_gimple_expr_loc (dump_flags_t, const dump_location_t &,
507 dump_flags_t, gimple *, int);
508 extern void dump_gimple_expr (dump_flags_t, dump_flags_t, gimple *, int);
509 extern void dump_symtab_node (dump_flags_t, symtab_node *);
510
511 template<unsigned int N, typename C>
512 void dump_dec (dump_flags_t, const poly_int<N, C> &);
513 extern void dump_dec (dump_flags_t, const poly_wide_int &, signop);
514 extern void dump_hex (dump_flags_t, const poly_wide_int &);
515
516 extern void dumpfile_ensure_any_optinfo_are_flushed ();
517
518 /* Managing nested scopes, so that dumps can express the call chain
519 leading to a dump message. */
520
521 extern unsigned int get_dump_scope_depth ();
522 extern void dump_begin_scope (const char *name, const dump_location_t &loc);
523 extern void dump_end_scope ();
524
525 /* Implementation detail of the AUTO_DUMP_SCOPE macro below.
526
527 A RAII-style class intended to make it easy to emit dump
528 information about entering and exiting a collection of nested
529 function calls. */
530
531 class auto_dump_scope
532 {
533 public:
534 auto_dump_scope (const char *name, dump_location_t loc)
535 {
536 if (dump_enabled_p ())
537 dump_begin_scope (name, loc);
538 }
539 ~auto_dump_scope ()
540 {
541 if (dump_enabled_p ())
542 dump_end_scope ();
543 }
544 };
545
546 /* A macro for calling:
547 dump_begin_scope (NAME, LOC);
548 via an RAII object, thus printing "=== MSG ===\n" to the dumpfile etc,
549 and then calling
550 dump_end_scope ();
551 once the object goes out of scope, thus capturing the nesting of
552 the scopes. */
553
554 #define AUTO_DUMP_SCOPE(NAME, LOC) \
555 auto_dump_scope scope (NAME, LOC)
556
557 extern void dump_function (int phase, tree fn);
558 extern void print_combine_total_stats (void);
559 extern bool enable_rtl_dump_file (void);
560
561 /* In tree-dump.c */
562 extern void dump_node (const_tree, dump_flags_t, FILE *);
563
564 /* In combine.c */
565 extern void dump_combine_total_stats (FILE *);
566 /* In cfghooks.c */
567 extern void dump_bb (FILE *, basic_block, int, dump_flags_t);
568
569 struct opt_pass;
570
571 namespace gcc {
572
573 /* A class for managing all of the various dump files used by the
574 optimization passes. */
575
576 class dump_manager
577 {
578 public:
579
580 dump_manager ();
581 ~dump_manager ();
582
583 /* Register a dumpfile.
584
585 TAKE_OWNERSHIP determines whether callee takes ownership of strings
586 SUFFIX, SWTCH, and GLOB. */
587 unsigned int
588 dump_register (const char *suffix, const char *swtch, const char *glob,
589 dump_kind dkind, optgroup_flags_t optgroup_flags,
590 bool take_ownership);
591
592 /* Allow languages and middle-end to register their dumps before the
593 optimization passes. */
594 void
595 register_dumps ();
596
597 /* Return the dump_file_info for the given phase. */
598 struct dump_file_info *
599 get_dump_file_info (int phase) const;
600
601 struct dump_file_info *
602 get_dump_file_info_by_switch (const char *swtch) const;
603
604 /* Return the name of the dump file for the given phase.
605 If the dump is not enabled, returns NULL. */
606 char *
607 get_dump_file_name (int phase, int part = -1) const;
608
609 char *
610 get_dump_file_name (struct dump_file_info *dfi, int part = -1) const;
611
612 int
613 dump_switch_p (const char *arg);
614
615 /* Start a dump for PHASE. Store user-supplied dump flags in
616 *FLAG_PTR. Return the number of streams opened. Set globals
617 DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
618 set dump_flags appropriately for both pass dump stream and
619 -fopt-info stream. */
620 int
621 dump_start (int phase, dump_flags_t *flag_ptr);
622
623 /* Finish a tree dump for PHASE and close associated dump streams. Also
624 reset the globals DUMP_FILE, ALT_DUMP_FILE, and DUMP_FLAGS. */
625 void
626 dump_finish (int phase);
627
628 FILE *
629 dump_begin (int phase, dump_flags_t *flag_ptr, int part);
630
631 /* Returns nonzero if tree dump PHASE has been initialized. */
632 int
633 dump_initialized_p (int phase) const;
634
635 /* Returns the switch name of PHASE. */
636 const char *
637 dump_flag_name (int phase) const;
638
639 void register_pass (opt_pass *pass);
640
641 private:
642
643 int
644 dump_phase_enabled_p (int phase) const;
645
646 int
647 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi, bool doglob);
648
649 int
650 dump_enable_all (dump_kind dkind, dump_flags_t flags, const char *filename);
651
652 int
653 opt_info_enable_passes (optgroup_flags_t optgroup_flags, dump_flags_t flags,
654 const char *filename);
655
656 bool update_dfi_for_opt_info (dump_file_info *dfi) const;
657
658 private:
659
660 /* Dynamically registered dump files and switches. */
661 int m_next_dump;
662 struct dump_file_info *m_extra_dump_files;
663 size_t m_extra_dump_files_in_use;
664 size_t m_extra_dump_files_alloced;
665
666 /* Stored values from -fopt-info, for handling passes created after
667 option-parsing (by backends and by plugins). */
668 optgroup_flags_t m_optgroup_flags;
669 dump_flags_t m_optinfo_flags;
670 char *m_optinfo_filename;
671
672 /* Grant access to dump_enable_all. */
673 friend bool ::enable_rtl_dump_file (void);
674
675 /* Grant access to opt_info_enable_passes. */
676 friend int ::opt_info_switch_p (const char *arg);
677
678 }; // class dump_manager
679
680 } // namespace gcc
681
682 #endif /* GCC_DUMPFILE_H */