libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / optinfo.cc
blob48a270cbfea83685f300fe3b9f39e670ffdafcef
1 /* Optimization information.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
26 #include "backend.h"
27 #include "tree.h"
28 #include "gimple.h"
30 #include "optinfo.h"
31 #include "optinfo-emit-json.h"
32 #include "dump-context.h"
33 #include "pretty-print.h"
34 #include "gimple-pretty-print.h"
35 #include "cgraph.h"
36 #include "selftest.h"
38 /* optinfo_item's ctor. Takes ownership of TEXT. */
40 optinfo_item::optinfo_item (enum optinfo_item_kind kind, location_t location,
41 char *text)
42 : m_kind (kind), m_location (location), m_text (text)
46 /* optinfo_item's dtor. */
48 optinfo_item::~optinfo_item ()
50 free (m_text);
53 /* Get a string from KIND. */
55 const char *
56 optinfo_kind_to_string (enum optinfo_kind kind)
58 switch (kind)
60 default:
61 gcc_unreachable ();
62 case OPTINFO_KIND_SUCCESS:
63 return "success";
64 case OPTINFO_KIND_FAILURE:
65 return "failure";
66 case OPTINFO_KIND_NOTE:
67 return "note";
68 case OPTINFO_KIND_SCOPE:
69 return "scope";
73 /* optinfo's dtor. */
75 optinfo::~optinfo ()
77 /* Cleanup. */
78 unsigned i;
79 optinfo_item *item;
80 FOR_EACH_VEC_ELT (m_items, i, item)
81 delete item;
84 /* Add ITEM to this optinfo. */
86 void
87 optinfo::add_item (std::unique_ptr<optinfo_item> item)
89 gcc_assert (item.get ());
90 m_items.safe_push (item.release ());
93 /* Get MSG_* flags corresponding to KIND. */
95 static dump_flags_t
96 optinfo_kind_to_dump_flag (enum optinfo_kind kind)
98 switch (kind)
100 default:
101 gcc_unreachable ();
102 case OPTINFO_KIND_SUCCESS:
103 return MSG_OPTIMIZED_LOCATIONS;
104 case OPTINFO_KIND_FAILURE:
105 return MSG_MISSED_OPTIMIZATION;
106 case OPTINFO_KIND_NOTE:
107 case OPTINFO_KIND_SCOPE:
108 return MSG_NOTE;
112 /* Re-emit this optinfo, both to the "non-immediate" destinations,
113 *and* to the "immediate" destinations. */
115 void
116 optinfo::emit_for_opt_problem () const
118 dump_flags_t dump_kind = optinfo_kind_to_dump_flag (get_kind ());
119 dump_kind |= MSG_PRIORITY_REEMITTED;
121 /* Re-emit to "immediate" destinations, without creating a new optinfo. */
122 dump_context::get ().dump_loc_immediate (dump_kind, get_user_location ());
123 unsigned i;
124 optinfo_item *item;
125 FOR_EACH_VEC_ELT (m_items, i, item)
126 dump_context::get ().emit_item (*item, dump_kind);
128 /* Re-emit to "non-immediate" destinations. */
129 dump_context::get ().emit_optinfo (this);
132 /* Update the optinfo's kind based on DUMP_KIND. */
134 void
135 optinfo::handle_dump_file_kind (dump_flags_t dump_kind)
137 /* Any optinfo for a "scope" should have been emitted separately. */
138 gcc_assert (m_kind != OPTINFO_KIND_SCOPE);
140 if (dump_kind & MSG_OPTIMIZED_LOCATIONS)
141 m_kind = OPTINFO_KIND_SUCCESS;
142 else if (dump_kind & MSG_MISSED_OPTIMIZATION)
143 m_kind = OPTINFO_KIND_FAILURE;
144 else if (dump_kind & MSG_NOTE)
145 m_kind = OPTINFO_KIND_NOTE;
148 /* Return true if any of the active optinfo destinations make use
149 of inlining information.
150 (if true, then the information is preserved). */
152 bool optinfo_wants_inlining_info_p ()
154 return dump_context::get ().optimization_records_enabled_p ();