]>
Commit | Line | Data |
---|---|---|
4df3629e DM |
1 | /* Optimization information. |
2 | Copyright (C) 2018 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 "dump-context.h" | |
31 | #include "pretty-print.h" | |
32 | #include "gimple-pretty-print.h" | |
33 | #include "cgraph.h" | |
34 | #include "selftest.h" | |
35 | ||
36 | /* optinfo_item's ctor. */ | |
37 | ||
38 | optinfo_item::optinfo_item (enum optinfo_item_kind kind, location_t location, | |
39 | char *text, bool owned) | |
40 | : m_kind (kind), m_location (location), m_text (text), m_owned (owned) | |
41 | { | |
42 | } | |
43 | ||
44 | /* optinfo_item's dtor. */ | |
45 | ||
46 | optinfo_item::~optinfo_item () | |
47 | { | |
48 | if (m_owned) | |
49 | free (m_text); | |
50 | } | |
51 | ||
52 | /* Get a string from KIND. */ | |
53 | ||
54 | const char * | |
55 | optinfo_kind_to_string (enum optinfo_kind kind) | |
56 | { | |
57 | switch (kind) | |
58 | { | |
59 | default: | |
60 | gcc_unreachable (); | |
61 | case OPTINFO_KIND_SUCCESS: | |
62 | return "success"; | |
63 | case OPTINFO_KIND_FAILURE: | |
64 | return "failure"; | |
65 | case OPTINFO_KIND_NOTE: | |
66 | return "note"; | |
67 | case OPTINFO_KIND_SCOPE: | |
68 | return "scope"; | |
69 | } | |
70 | } | |
71 | ||
72 | /* optinfo's dtor. */ | |
73 | ||
74 | optinfo::~optinfo () | |
75 | { | |
76 | /* Cleanup. */ | |
77 | unsigned i; | |
78 | optinfo_item *item; | |
79 | FOR_EACH_VEC_ELT (m_items, i, item) | |
80 | delete item; | |
81 | } | |
82 | ||
83 | /* Emit the optinfo to all of the active destinations. */ | |
84 | ||
85 | void | |
86 | optinfo::emit () | |
87 | { | |
88 | /* currently this is a no-op. */ | |
89 | } | |
90 | ||
91 | /* Update the optinfo's kind based on DUMP_KIND. */ | |
92 | ||
93 | void | |
94 | optinfo::handle_dump_file_kind (dump_flags_t dump_kind) | |
95 | { | |
96 | if (dump_kind & MSG_OPTIMIZED_LOCATIONS) | |
97 | m_kind = OPTINFO_KIND_SUCCESS; | |
98 | else if (dump_kind & MSG_MISSED_OPTIMIZATION) | |
99 | m_kind = OPTINFO_KIND_FAILURE; | |
100 | else if (dump_kind & MSG_NOTE) | |
101 | m_kind = OPTINFO_KIND_NOTE; | |
102 | } | |
103 | ||
104 | /* Append a string literal to this optinfo. */ | |
105 | ||
106 | void | |
107 | optinfo::add_string (const char *str) | |
108 | { | |
109 | optinfo_item *item | |
110 | = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION, | |
111 | const_cast <char *> (str), false); | |
112 | m_items.safe_push (item); | |
113 | } | |
114 | ||
115 | /* Append printf-formatted text to this optinfo. */ | |
116 | ||
117 | void | |
118 | optinfo::add_printf (const char *format, ...) | |
119 | { | |
120 | va_list ap; | |
121 | va_start (ap, format); | |
122 | add_printf_va (format, ap); | |
123 | va_end (ap); | |
124 | } | |
125 | ||
126 | /* Append printf-formatted text to this optinfo. */ | |
127 | ||
128 | void | |
129 | optinfo::add_printf_va (const char *format, va_list ap) | |
130 | { | |
131 | char *formatted_text = xvasprintf (format, ap); | |
132 | optinfo_item *item | |
133 | = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION, | |
134 | formatted_text, true); | |
135 | m_items.safe_push (item); | |
136 | } | |
137 | ||
138 | /* Append a gimple statement to this optinfo, equivalent to | |
139 | print_gimple_stmt. */ | |
140 | ||
141 | void | |
142 | optinfo::add_gimple_stmt (gimple *stmt, int spc, dump_flags_t dump_flags) | |
143 | { | |
144 | pretty_printer pp; | |
145 | pp_needs_newline (&pp) = true; | |
146 | pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags); | |
147 | pp_newline (&pp); | |
148 | ||
149 | optinfo_item *item | |
150 | = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt), | |
151 | xstrdup (pp_formatted_text (&pp)), true); | |
152 | m_items.safe_push (item); | |
153 | } | |
154 | ||
155 | /* Append a gimple statement to this optinfo, equivalent to | |
156 | print_gimple_expr. */ | |
157 | ||
158 | void | |
159 | optinfo::add_gimple_expr (gimple *stmt, int spc, dump_flags_t dump_flags) | |
160 | { | |
161 | dump_flags |= TDF_RHS_ONLY; | |
162 | pretty_printer pp; | |
163 | pp_needs_newline (&pp) = true; | |
164 | pp_gimple_stmt_1 (&pp, stmt, spc, dump_flags); | |
165 | ||
166 | optinfo_item *item | |
167 | = new optinfo_item (OPTINFO_ITEM_KIND_GIMPLE, gimple_location (stmt), | |
168 | xstrdup (pp_formatted_text (&pp)), true); | |
169 | m_items.safe_push (item); | |
170 | } | |
171 | ||
172 | /* Append a tree node to this optinfo, equivalent to print_generic_expr. */ | |
173 | ||
174 | void | |
175 | optinfo::add_tree (tree node, dump_flags_t dump_flags) | |
176 | { | |
177 | pretty_printer pp; | |
178 | pp_needs_newline (&pp) = true; | |
179 | pp_translate_identifiers (&pp) = false; | |
180 | dump_generic_node (&pp, node, 0, dump_flags, false); | |
181 | ||
182 | location_t loc = UNKNOWN_LOCATION; | |
183 | if (EXPR_HAS_LOCATION (node)) | |
184 | loc = EXPR_LOCATION (node); | |
185 | ||
186 | optinfo_item *item | |
187 | = new optinfo_item (OPTINFO_ITEM_KIND_TREE, loc, | |
188 | xstrdup (pp_formatted_text (&pp)), true); | |
189 | m_items.safe_push (item); | |
190 | } | |
191 | ||
192 | /* Append a symbol table node to this optinfo. */ | |
193 | ||
194 | void | |
195 | optinfo::add_symtab_node (symtab_node *node) | |
196 | { | |
197 | location_t loc = DECL_SOURCE_LOCATION (node->decl); | |
198 | optinfo_item *item | |
199 | = new optinfo_item (OPTINFO_ITEM_KIND_SYMTAB_NODE, loc, | |
200 | xstrdup (node->dump_name ()), true); | |
201 | m_items.safe_push (item); | |
202 | } | |
203 | ||
204 | /* Append the decimal represenation of a wide_int_ref to this | |
205 | optinfo. */ | |
206 | ||
207 | void | |
208 | optinfo::add_dec (const wide_int_ref &wi, signop sgn) | |
209 | { | |
210 | char buf[WIDE_INT_PRINT_BUFFER_SIZE]; | |
211 | print_dec (wi, buf, sgn); | |
212 | optinfo_item *item | |
213 | = new optinfo_item (OPTINFO_ITEM_KIND_TEXT, UNKNOWN_LOCATION, | |
214 | xstrdup (buf), true); | |
215 | m_items.safe_push (item); | |
216 | } | |
217 | ||
218 | /* Should optinfo instances be created? | |
219 | All creation of optinfos should be guarded by this predicate. | |
220 | Return true if any optinfo destinations are active. */ | |
221 | ||
222 | bool optinfo_enabled_p () | |
223 | { | |
224 | /* Currently no destinations are implemented, just a hook for | |
225 | selftests. */ | |
226 | return dump_context::get ().forcibly_enable_optinfo_p (); | |
227 | } | |
228 | ||
229 | /* Return true if any of the active optinfo destinations make use | |
230 | of inlining information. | |
231 | (if true, then the information is preserved). */ | |
232 | ||
233 | bool optinfo_wants_inlining_info_p () | |
234 | { | |
235 | return false; | |
236 | } |