[13/46] Make STMT_VINFO_RELATED_STMT a stmt_vec_info
[official-gcc.git] / gcc / optinfo.cc
blob93de9d9388a0d5d95cbb5e6bb3c6db8f2ebdc0a8
1 /* Optimization information.
2 Copyright (C) 2018 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 #include "system.h"
23 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
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"
37 /* optinfo_item's ctor. */
39 optinfo_item::optinfo_item (enum optinfo_item_kind kind, location_t location,
40 char *text, bool owned)
41 : m_kind (kind), m_location (location), m_text (text), m_owned (owned)
45 /* optinfo_item's dtor. */
47 optinfo_item::~optinfo_item ()
49 if (m_owned)
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 /* Emit the optinfo to all of the active destinations. */
86 void
87 optinfo::emit ()
89 /* -fsave-optimization-record. */
90 optimization_records_maybe_record_optinfo (this);
93 /* Update the optinfo's kind based on DUMP_KIND. */
95 void
96 optinfo::handle_dump_file_kind (dump_flags_t dump_kind)
98 if (dump_kind & MSG_OPTIMIZED_LOCATIONS)
99 m_kind = OPTINFO_KIND_SUCCESS;
100 else if (dump_kind & MSG_MISSED_OPTIMIZATION)
101 m_kind = OPTINFO_KIND_FAILURE;
102 else if (dump_kind & MSG_NOTE)
103 m_kind = OPTINFO_KIND_NOTE;
106 /* Append a string literal to this optinfo. */
108 void
109 optinfo::add_string (const char *str)
111 optinfo_item *item
112 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
113 const_cast <char *> (str), false);
114 m_items.safe_push (item);
117 /* Append printf-formatted text to this optinfo. */
119 void
120 optinfo::add_printf (const char *format, ...)
122 va_list ap;
123 va_start (ap, format);
124 add_printf_va (format, ap);
125 va_end (ap);
128 /* Append printf-formatted text to this optinfo. */
130 void
131 optinfo::add_printf_va (const char *format, va_list ap)
133 char *formatted_text = xvasprintf (format, ap);
134 optinfo_item *item
135 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
136 formatted_text, true);
137 m_items.safe_push (item);
140 /* Append a gimple statement to this optinfo, equivalent to
141 print_gimple_stmt. */
143 void
144 optinfo::add_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags)
146 pretty_printer pp;
147 pp_needs_newline (&pp) = true;
148 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
149 pp_newline (&pp);
151 optinfo_item *item
152 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
153 xstrdup (pp_formatted_text (&pp)), true);
154 m_items.safe_push (item);
157 /* Append a gimple statement to this optinfo, equivalent to
158 print_gimple_expr. */
160 void
161 optinfo::add_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags)
163 dump_flags |= TDF_RHS_ONLY;
164 pretty_printer pp;
165 pp_needs_newline (&pp) = true;
166 pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags);
168 optinfo_item *item
169 = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt),
170 xstrdup (pp_formatted_text (&pp)), true);
171 m_items.safe_push (item);
174 /* Append a tree node to this optinfo, equivalent to print_generic_expr. */
176 void
177 optinfo::add_tree (tree node, dump_flags_t dump_flags)
179 pretty_printer pp;
180 pp_needs_newline (&pp) = true;
181 pp_translate_identifiers (&pp) = false;
182 dump_generic_node (&pp, node, 0, dump_flags, false);
184 location_t loc = UNKNOWN_LOCATION;
185 if (EXPR_HAS_LOCATION (node))
186 loc = EXPR_LOCATION (node);
188 optinfo_item *item
189 = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc,
190 xstrdup (pp_formatted_text (&pp)), true);
191 m_items.safe_push (item);
194 /* Append a symbol table node to this optinfo. */
196 void
197 optinfo::add_symtab_node (symtab_node *node)
199 location_t loc = DECL_SOURCE_LOCATION (node->decl);
200 optinfo_item *item
201 = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc,
202 xstrdup (node->dump_name ()), true);
203 m_items.safe_push (item);
206 /* Append the decimal represenation of a wide_int_ref to this
207 optinfo. */
209 void
210 optinfo::add_dec (const wide_int_ref &wi, signop sgn)
212 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
213 print_dec (wi, buf, sgn);
214 optinfo_item *item
215 = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION,
216 xstrdup (buf), true);
217 m_items.safe_push (item);
220 /* Should optinfo instances be created?
221 All creation of optinfos should be guarded by this predicate.
222 Return true if any optinfo destinations are active. */
224 bool optinfo_enabled_p ()
226 return (dump_context::get ().forcibly_enable_optinfo_p ()
227 || optimization_records_enabled_p ());
230 /* Return true if any of the active optinfo destinations make use
231 of inlining information.
232 (if true, then the information is preserved). */
234 bool optinfo_wants_inlining_info_p ()
236 return optimization_records_enabled_p ();