Line data Source code
1 : /* Optimization information.
2 : Copyright (C) 2018-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 :
25 : #include "backend.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 :
29 : #include "optinfo.h"
30 : #include "optinfo-emit-json.h"
31 : #include "dump-context.h"
32 : #include "pretty-print.h"
33 : #include "gimple-pretty-print.h"
34 : #include "cgraph.h"
35 : #include "selftest.h"
36 :
37 : /* optinfo_item's ctor. Takes ownership of TEXT. */
38 :
39 16664493 : optinfo_item::optinfo_item (enum kind kind_, location_t location,
40 16664493 : char *text)
41 16664493 : : m_kind (kind_), m_location (location), m_text (text)
42 : {
43 16664493 : }
44 :
45 : /* optinfo_item's dtor. */
46 :
47 16662581 : optinfo_item::~optinfo_item ()
48 : {
49 16662581 : free (m_text);
50 16662581 : }
51 :
52 : /* Get a string from KIND. */
53 :
54 : const char *
55 42780 : optinfo::kind_to_string (enum kind kind_)
56 : {
57 42780 : switch (kind_)
58 : {
59 0 : default:
60 0 : gcc_unreachable ();
61 : case kind::success:
62 : return "success";
63 2067 : case kind::failure:
64 2067 : return "failure";
65 36458 : case kind::note:
66 36458 : return "note";
67 3916 : case kind::scope:
68 3916 : return "scope";
69 : }
70 : }
71 :
72 : /* optinfo's dtor. */
73 :
74 69453 : optinfo::~optinfo ()
75 : {
76 : /* Cleanup. */
77 69453 : unsigned i;
78 69453 : optinfo_item *item;
79 215692 : FOR_EACH_VEC_ELT (m_items, i, item)
80 146239 : delete item;
81 69453 : }
82 :
83 : /* Add ITEM to this optinfo. */
84 :
85 : void
86 148151 : optinfo::add_item (std::unique_ptr<optinfo_item> item)
87 : {
88 148151 : gcc_assert (item.get ());
89 148151 : m_items.safe_push (item.release ());
90 148151 : }
91 :
92 : /* Get MSG_* flags corresponding to KIND. */
93 :
94 : dump_flags_t
95 5932 : optinfo::kind_to_dump_flag (enum kind kind_)
96 : {
97 5932 : switch (kind_)
98 : {
99 0 : default:
100 0 : gcc_unreachable ();
101 : case kind::success:
102 : return MSG_OPTIMIZED_LOCATIONS;
103 : case kind::failure:
104 : return MSG_MISSED_OPTIMIZATION;
105 : case kind::note:
106 : case kind::scope:
107 : return MSG_NOTE;
108 : }
109 : }
110 :
111 : /* Re-emit this optinfo, both to the "non-immediate" destinations,
112 : *and* to the "immediate" destinations. */
113 :
114 : void
115 5932 : optinfo::emit_for_opt_problem () const
116 : {
117 5932 : dump_flags_t dump_kind = kind_to_dump_flag (get_kind ());
118 5932 : dump_kind |= MSG_PRIORITY_REEMITTED;
119 :
120 : /* Re-emit to "immediate" destinations, without creating a new optinfo. */
121 5932 : dump_context::get ().dump_loc_immediate (dump_kind, get_user_location ());
122 5932 : unsigned i;
123 5932 : optinfo_item *item;
124 22338 : FOR_EACH_VEC_ELT (m_items, i, item)
125 10474 : dump_context::get ().emit_item (*item, dump_kind);
126 :
127 : /* Re-emit to "non-immediate" destinations. */
128 5932 : dump_context::get ().emit_optinfo (this);
129 5932 : }
130 :
131 : /* Update the optinfo's kind based on DUMP_KIND. */
132 :
133 : void
134 43781 : optinfo::handle_dump_file_kind (dump_flags_t dump_kind)
135 : {
136 : /* Any optinfo for a "scope" should have been emitted separately. */
137 43781 : gcc_assert (m_kind != kind::scope);
138 :
139 43781 : if (dump_kind & MSG_OPTIMIZED_LOCATIONS)
140 395 : m_kind = kind::success;
141 43386 : else if (dump_kind & MSG_MISSED_OPTIMIZATION)
142 2060 : m_kind = kind::failure;
143 41326 : else if (dump_kind & MSG_NOTE)
144 41326 : m_kind = kind::note;
145 43781 : }
146 :
147 : /* Return true if any of the active optinfo destinations make use
148 : of inlining information.
149 : (if true, then the information is preserved). */
150 :
151 1700477 : bool optinfo_wants_inlining_info_p ()
152 : {
153 1700477 : return dump_context::get ().optimization_records_enabled_p ();
154 : }
|