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
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
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/>. */
23 #include "coretypes.h"
30 #include "optinfo-emit-json.h"
31 #include "dump-context.h"
32 #include "pretty-print.h"
33 #include "gimple-pretty-print.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 ()
53 /* Get a string from KIND. */
56 optinfo_kind_to_string (enum optinfo_kind kind
)
62 case OPTINFO_KIND_SUCCESS
:
64 case OPTINFO_KIND_FAILURE
:
66 case OPTINFO_KIND_NOTE
:
68 case OPTINFO_KIND_SCOPE
:
80 FOR_EACH_VEC_ELT (m_items
, i
, item
)
84 /* Emit the optinfo to all of the active destinations. */
89 /* -fsave-optimization-record. */
90 optimization_records_maybe_record_optinfo (this);
93 /* Update the optinfo's kind based on DUMP_KIND. */
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. */
109 optinfo::add_string (const char *str
)
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. */
120 optinfo::add_printf (const char *format
, ...)
123 va_start (ap
, format
);
124 add_printf_va (format
, ap
);
128 /* Append printf-formatted text to this optinfo. */
131 optinfo::add_printf_va (const char *format
, va_list ap
)
133 char *formatted_text
= xvasprintf (format
, ap
);
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. */
144 optinfo::add_gimple_stmt (gimple
*stmt
, int spc
, dump_flags_t dump_flags
)
147 pp_needs_newline (&pp
) = true;
148 pp_gimple_stmt_1 (&pp
, stmt
, spc
, dump_flags
);
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. */
161 optinfo::add_gimple_expr (gimple
*stmt
, int spc
, dump_flags_t dump_flags
)
163 dump_flags
|= TDF_RHS_ONLY
;
165 pp_needs_newline (&pp
) = true;
166 pp_gimple_stmt_1 (&pp
, stmt
, spc
, dump_flags
);
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. */
177 optinfo::add_tree (tree node
, dump_flags_t dump_flags
)
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
);
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. */
197 optinfo::add_symtab_node (symtab_node
*node
)
199 location_t loc
= DECL_SOURCE_LOCATION (node
->decl
);
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
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
);
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 ();