ipa-fnsummary.h revision 1.1.1.3 1 1.1 mrg /* IPA function body analysis.
2 1.1.1.3 mrg Copyright (C) 2003-2020 Free Software Foundation, Inc.
3 1.1 mrg Contributed by Jan Hubicka
4 1.1 mrg
5 1.1 mrg This file is part of GCC.
6 1.1 mrg
7 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
8 1.1 mrg the terms of the GNU General Public License as published by the Free
9 1.1 mrg Software Foundation; either version 3, or (at your option) any later
10 1.1 mrg version.
11 1.1 mrg
12 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 1.1 mrg for more details.
16 1.1 mrg
17 1.1 mrg You should have received a copy of the GNU General Public License
18 1.1 mrg along with GCC; see the file COPYING3. If not see
19 1.1 mrg <http://www.gnu.org/licenses/>. */
20 1.1 mrg
21 1.1 mrg #ifndef GCC_IPA_SUMMARY_H
22 1.1 mrg #define GCC_IPA_SUMMARY_H
23 1.1 mrg
24 1.1 mrg #include "sreal.h"
25 1.1 mrg #include "ipa-predicate.h"
26 1.1 mrg
27 1.1 mrg
28 1.1.1.3 mrg /* Hints are reasons why IPA heuristics should prefer specializing given
29 1.1.1.3 mrg function. They are represented as bitmap of the following values. */
30 1.1 mrg enum ipa_hints_vals {
31 1.1 mrg /* When specialization turns indirect call into a direct call,
32 1.1 mrg it is good idea to do so. */
33 1.1 mrg INLINE_HINT_indirect_call = 1,
34 1.1 mrg /* Inlining may make loop iterations or loop stride known. It is good idea
35 1.1.1.3 mrg to do so because it enables loop optimizations. */
36 1.1 mrg INLINE_HINT_loop_iterations = 2,
37 1.1 mrg INLINE_HINT_loop_stride = 4,
38 1.1 mrg /* Inlining within same strongly connected component of callgraph is often
39 1.1 mrg a loss due to increased stack frame usage and prologue setup costs. */
40 1.1 mrg INLINE_HINT_same_scc = 8,
41 1.1 mrg /* Inlining functions in strongly connected component is not such a great
42 1.1 mrg win. */
43 1.1 mrg INLINE_HINT_in_scc = 16,
44 1.1 mrg /* If function is declared inline by user, it may be good idea to inline
45 1.1 mrg it. Set by simple_edge_hints in ipa-inline-analysis.c. */
46 1.1 mrg INLINE_HINT_declared_inline = 32,
47 1.1 mrg /* Programs are usually still organized for non-LTO compilation and thus
48 1.1 mrg if functions are in different modules, inlining may not be so important.
49 1.1 mrg Set by simple_edge_hints in ipa-inline-analysis.c. */
50 1.1 mrg INLINE_HINT_cross_module = 64,
51 1.1 mrg /* We know that the callee is hot by profile. */
52 1.1.1.3 mrg INLINE_HINT_known_hot = 128
53 1.1 mrg };
54 1.1 mrg
55 1.1 mrg typedef int ipa_hints;
56 1.1 mrg
57 1.1 mrg /* Simple description of whether a memory load or a condition refers to a load
58 1.1 mrg from an aggregate and if so, how and where from in the aggregate.
59 1.1 mrg Individual fields have the same meaning like fields with the same name in
60 1.1 mrg struct condition. */
61 1.1 mrg
62 1.1 mrg struct agg_position_info
63 1.1 mrg {
64 1.1 mrg HOST_WIDE_INT offset;
65 1.1 mrg bool agg_contents;
66 1.1 mrg bool by_ref;
67 1.1 mrg };
68 1.1 mrg
69 1.1 mrg /* Representation of function body size and time depending on the call
70 1.1 mrg context. We keep simple array of record, every containing of predicate
71 1.1 mrg and time/size to account. */
72 1.1.1.3 mrg class GTY(()) size_time_entry
73 1.1 mrg {
74 1.1.1.3 mrg public:
75 1.1 mrg /* Predicate for code to be executed. */
76 1.1 mrg predicate exec_predicate;
77 1.1 mrg /* Predicate for value to be constant and optimized out in a specialized copy.
78 1.1 mrg When deciding on specialization this makes it possible to see how much
79 1.1 mrg the executed code paths will simplify. */
80 1.1 mrg predicate nonconst_predicate;
81 1.1 mrg int size;
82 1.1 mrg sreal GTY((skip)) time;
83 1.1 mrg };
84 1.1 mrg
85 1.1.1.3 mrg /* Summary about function and stack frame sizes. We keep this info
86 1.1.1.3 mrg for inline clones and also for WPA streaming. For this reason this is not
87 1.1.1.3 mrg part of ipa_fn_summary which exists only for offline functions. */
88 1.1.1.3 mrg class ipa_size_summary
89 1.1.1.3 mrg {
90 1.1.1.3 mrg public:
91 1.1.1.3 mrg /* Estimated stack frame consumption by the function. */
92 1.1.1.3 mrg HOST_WIDE_INT estimated_self_stack_size;
93 1.1.1.3 mrg /* Size of the function body. */
94 1.1.1.3 mrg int self_size;
95 1.1.1.3 mrg /* Estimated size of the function after inlining. */
96 1.1.1.3 mrg int size;
97 1.1.1.3 mrg
98 1.1.1.3 mrg ipa_size_summary ()
99 1.1.1.3 mrg : estimated_self_stack_size (0), self_size (0), size (0)
100 1.1.1.3 mrg {
101 1.1.1.3 mrg }
102 1.1.1.3 mrg };
103 1.1.1.3 mrg
104 1.1 mrg /* Function inlining information. */
105 1.1.1.3 mrg class GTY(()) ipa_fn_summary
106 1.1 mrg {
107 1.1.1.3 mrg public:
108 1.1.1.2 mrg /* Keep all field empty so summary dumping works during its computation.
109 1.1.1.2 mrg This is useful for debugging. */
110 1.1.1.2 mrg ipa_fn_summary ()
111 1.1.1.3 mrg : min_size (0),
112 1.1.1.2 mrg inlinable (false), single_caller (false),
113 1.1.1.2 mrg fp_expressions (false), estimated_stack_size (false),
114 1.1.1.3 mrg time (0), conds (NULL),
115 1.1.1.3 mrg size_time_table (NULL), call_size_time_table (NULL), loop_iterations (NULL),
116 1.1.1.3 mrg loop_stride (NULL), growth (0), scc_no (0)
117 1.1.1.2 mrg {
118 1.1.1.2 mrg }
119 1.1.1.2 mrg
120 1.1.1.2 mrg /* Copy constructor. */
121 1.1.1.2 mrg ipa_fn_summary (const ipa_fn_summary &s)
122 1.1.1.3 mrg : min_size (s.min_size),
123 1.1.1.2 mrg inlinable (s.inlinable), single_caller (s.single_caller),
124 1.1.1.2 mrg fp_expressions (s.fp_expressions),
125 1.1.1.2 mrg estimated_stack_size (s.estimated_stack_size),
126 1.1.1.3 mrg time (s.time), conds (s.conds), size_time_table (s.size_time_table),
127 1.1.1.3 mrg call_size_time_table (NULL),
128 1.1.1.2 mrg loop_iterations (s.loop_iterations), loop_stride (s.loop_stride),
129 1.1.1.3 mrg growth (s.growth), scc_no (s.scc_no)
130 1.1.1.2 mrg {}
131 1.1.1.2 mrg
132 1.1.1.2 mrg /* Default constructor. */
133 1.1.1.2 mrg ~ipa_fn_summary ();
134 1.1.1.2 mrg
135 1.1 mrg /* Information about the function body itself. */
136 1.1 mrg
137 1.1 mrg /* Minimal size increase after inlining. */
138 1.1 mrg int min_size;
139 1.1 mrg
140 1.1 mrg /* False when there something makes inlining impossible (such as va_arg). */
141 1.1 mrg unsigned inlinable : 1;
142 1.1 mrg /* True wen there is only one caller of the function before small function
143 1.1 mrg inlining. */
144 1.1 mrg unsigned int single_caller : 1;
145 1.1 mrg /* True if function contains any floating point expressions. */
146 1.1 mrg unsigned int fp_expressions : 1;
147 1.1 mrg
148 1.1 mrg /* Information about function that will result after applying all the
149 1.1 mrg inline decisions present in the callgraph. Generally kept up to
150 1.1 mrg date only for functions that are not inline clones. */
151 1.1 mrg
152 1.1 mrg /* Estimated stack frame consumption by the function. */
153 1.1 mrg HOST_WIDE_INT estimated_stack_size;
154 1.1.1.3 mrg /* Estimated runtime of function after inlining. */
155 1.1 mrg sreal GTY((skip)) time;
156 1.1 mrg
157 1.1 mrg /* Conditional size/time information. The summaries are being
158 1.1 mrg merged during inlining. */
159 1.1 mrg conditions conds;
160 1.1.1.3 mrg /* Normal code is accounted in size_time_table, while calls are
161 1.1.1.3 mrg accounted in call_size_time_table. This is because calls
162 1.1.1.3 mrg are often adjusted by IPA optimizations and thus this summary
163 1.1.1.3 mrg is generated from call summary information when needed. */
164 1.1 mrg vec<size_time_entry, va_gc> *size_time_table;
165 1.1.1.3 mrg vec<size_time_entry, va_gc> *call_size_time_table;
166 1.1 mrg
167 1.1 mrg /* Predicate on when some loop in the function becomes to have known
168 1.1 mrg bounds. */
169 1.1 mrg predicate * GTY((skip)) loop_iterations;
170 1.1 mrg /* Predicate on when some loop in the function becomes to have known
171 1.1 mrg stride. */
172 1.1 mrg predicate * GTY((skip)) loop_stride;
173 1.1 mrg /* Estimated growth for inlining all copies of the function before start
174 1.1 mrg of small functions inlining.
175 1.1 mrg This value will get out of date as the callers are duplicated, but
176 1.1 mrg using up-to-date value in the badness metric mean a lot of extra
177 1.1 mrg expenses. */
178 1.1 mrg int growth;
179 1.1 mrg /* Number of SCC on the beginning of inlining process. */
180 1.1 mrg int scc_no;
181 1.1 mrg
182 1.1 mrg /* Record time and size under given predicates. */
183 1.1.1.3 mrg void account_size_time (int, sreal, const predicate &, const predicate &,
184 1.1.1.3 mrg bool call = false);
185 1.1 mrg
186 1.1 mrg /* We keep values scaled up, so fractional sizes can be accounted. */
187 1.1 mrg static const int size_scale = 2;
188 1.1.1.3 mrg /* Maximal size of size_time_table before we start to be conservative. */
189 1.1.1.3 mrg static const int max_size_time_table_size = 256;
190 1.1 mrg };
191 1.1 mrg
192 1.1.1.2 mrg class GTY((user)) ipa_fn_summary_t:
193 1.1.1.2 mrg public fast_function_summary <ipa_fn_summary *, va_gc>
194 1.1 mrg {
195 1.1 mrg public:
196 1.1.1.2 mrg ipa_fn_summary_t (symbol_table *symtab):
197 1.1.1.2 mrg fast_function_summary <ipa_fn_summary *, va_gc> (symtab) {}
198 1.1 mrg
199 1.1 mrg static ipa_fn_summary_t *create_ggc (symbol_table *symtab)
200 1.1 mrg {
201 1.1.1.3 mrg class ipa_fn_summary_t *summary
202 1.1.1.3 mrg = new (ggc_alloc_no_dtor<ipa_fn_summary_t> ()) ipa_fn_summary_t (symtab);
203 1.1 mrg summary->disable_insertion_hook ();
204 1.1 mrg return summary;
205 1.1 mrg }
206 1.1 mrg
207 1.1.1.2 mrg /* Remove ipa_fn_summary for all callees of NODE. */
208 1.1.1.2 mrg void remove_callees (cgraph_node *node);
209 1.1 mrg
210 1.1 mrg virtual void insert (cgraph_node *, ipa_fn_summary *);
211 1.1.1.2 mrg virtual void remove (cgraph_node *node, ipa_fn_summary *)
212 1.1.1.2 mrg {
213 1.1.1.2 mrg remove_callees (node);
214 1.1.1.2 mrg }
215 1.1.1.2 mrg
216 1.1 mrg virtual void duplicate (cgraph_node *src, cgraph_node *dst,
217 1.1 mrg ipa_fn_summary *src_data, ipa_fn_summary *dst_data);
218 1.1 mrg };
219 1.1 mrg
220 1.1.1.2 mrg extern GTY(()) fast_function_summary <ipa_fn_summary *, va_gc>
221 1.1.1.2 mrg *ipa_fn_summaries;
222 1.1 mrg
223 1.1.1.3 mrg class ipa_size_summary_t:
224 1.1.1.3 mrg public fast_function_summary <ipa_size_summary *, va_heap>
225 1.1.1.3 mrg {
226 1.1.1.3 mrg public:
227 1.1.1.3 mrg ipa_size_summary_t (symbol_table *symtab):
228 1.1.1.3 mrg fast_function_summary <ipa_size_summary *, va_heap> (symtab)
229 1.1.1.3 mrg {
230 1.1.1.3 mrg disable_insertion_hook ();
231 1.1.1.3 mrg }
232 1.1.1.3 mrg
233 1.1.1.3 mrg virtual void duplicate (cgraph_node *, cgraph_node *,
234 1.1.1.3 mrg ipa_size_summary *src_data,
235 1.1.1.3 mrg ipa_size_summary *dst_data)
236 1.1.1.3 mrg {
237 1.1.1.3 mrg *dst_data = *src_data;
238 1.1.1.3 mrg }
239 1.1.1.3 mrg };
240 1.1.1.3 mrg extern fast_function_summary <ipa_size_summary *, va_heap>
241 1.1.1.3 mrg *ipa_size_summaries;
242 1.1.1.3 mrg
243 1.1 mrg /* Information kept about callgraph edges. */
244 1.1.1.3 mrg class ipa_call_summary
245 1.1 mrg {
246 1.1.1.3 mrg public:
247 1.1.1.2 mrg /* Keep all field empty so summary dumping works during its computation.
248 1.1.1.2 mrg This is useful for debugging. */
249 1.1.1.2 mrg ipa_call_summary ()
250 1.1.1.2 mrg : predicate (NULL), param (vNULL), call_stmt_size (0), call_stmt_time (0),
251 1.1.1.2 mrg loop_depth (0), is_return_callee_uncaptured (false)
252 1.1.1.2 mrg {
253 1.1.1.2 mrg }
254 1.1.1.2 mrg
255 1.1.1.2 mrg /* Copy constructor. */
256 1.1.1.2 mrg ipa_call_summary (const ipa_call_summary &s):
257 1.1.1.2 mrg predicate (s.predicate), param (s.param), call_stmt_size (s.call_stmt_size),
258 1.1.1.2 mrg call_stmt_time (s.call_stmt_time), loop_depth (s.loop_depth),
259 1.1.1.2 mrg is_return_callee_uncaptured (s.is_return_callee_uncaptured)
260 1.1.1.2 mrg {
261 1.1.1.2 mrg }
262 1.1.1.2 mrg
263 1.1.1.2 mrg /* Default destructor. */
264 1.1.1.2 mrg ~ipa_call_summary ();
265 1.1.1.2 mrg
266 1.1 mrg class predicate *predicate;
267 1.1 mrg /* Vector indexed by parameters. */
268 1.1 mrg vec<inline_param_summary> param;
269 1.1 mrg /* Estimated size and time of the call statement. */
270 1.1 mrg int call_stmt_size;
271 1.1 mrg int call_stmt_time;
272 1.1 mrg /* Depth of loop nest, 0 means no nesting. */
273 1.1 mrg unsigned int loop_depth;
274 1.1 mrg /* Indicates whether the caller returns the value of it's callee. */
275 1.1 mrg bool is_return_callee_uncaptured;
276 1.1 mrg };
277 1.1 mrg
278 1.1.1.2 mrg class ipa_call_summary_t: public fast_call_summary <ipa_call_summary *, va_heap>
279 1.1 mrg {
280 1.1 mrg public:
281 1.1.1.2 mrg ipa_call_summary_t (symbol_table *symtab):
282 1.1.1.2 mrg fast_call_summary <ipa_call_summary *, va_heap> (symtab) {}
283 1.1 mrg
284 1.1 mrg /* Hook that is called by summary when an edge is duplicated. */
285 1.1 mrg virtual void duplicate (cgraph_edge *src, cgraph_edge *dst,
286 1.1 mrg ipa_call_summary *src_data,
287 1.1 mrg ipa_call_summary *dst_data);
288 1.1 mrg };
289 1.1 mrg
290 1.1.1.3 mrg /* This object describe a context of call. That is a summary of known
291 1.1.1.3 mrg information about its parameters. Main purpose of this context is
292 1.1.1.3 mrg to give more realistic estimations of function runtime, size and
293 1.1.1.3 mrg inline hints. */
294 1.1.1.3 mrg class ipa_call_context
295 1.1.1.3 mrg {
296 1.1.1.3 mrg public:
297 1.1.1.3 mrg ipa_call_context (cgraph_node *node,
298 1.1.1.3 mrg clause_t possible_truths,
299 1.1.1.3 mrg clause_t nonspec_possible_truths,
300 1.1.1.3 mrg vec<tree> known_vals,
301 1.1.1.3 mrg vec<ipa_polymorphic_call_context> known_contexts,
302 1.1.1.3 mrg vec<ipa_agg_value_set> known_aggs,
303 1.1.1.3 mrg vec<inline_param_summary> m_inline_param_summary);
304 1.1.1.3 mrg ipa_call_context ()
305 1.1.1.3 mrg : m_node(NULL)
306 1.1.1.3 mrg {
307 1.1.1.3 mrg }
308 1.1.1.3 mrg void estimate_size_and_time (int *ret_size, int *ret_min_size,
309 1.1.1.3 mrg sreal *ret_time,
310 1.1.1.3 mrg sreal *ret_nonspecialized_time,
311 1.1.1.3 mrg ipa_hints *ret_hints);
312 1.1.1.3 mrg void duplicate_from (const ipa_call_context &ctx);
313 1.1.1.3 mrg void release (bool all = false);
314 1.1.1.3 mrg bool equal_to (const ipa_call_context &);
315 1.1.1.3 mrg bool exists_p ()
316 1.1.1.3 mrg {
317 1.1.1.3 mrg return m_node != NULL;
318 1.1.1.3 mrg }
319 1.1.1.3 mrg private:
320 1.1.1.3 mrg /* Called function. */
321 1.1.1.3 mrg cgraph_node *m_node;
322 1.1.1.3 mrg /* Clause describing what predicate conditionals can be satisfied
323 1.1.1.3 mrg in this context if function is inlined/specialized. */
324 1.1.1.3 mrg clause_t m_possible_truths;
325 1.1.1.3 mrg /* Clause describing what predicate conditionals can be satisfied
326 1.1.1.3 mrg in this context if function is kept offline. */
327 1.1.1.3 mrg clause_t m_nonspec_possible_truths;
328 1.1.1.3 mrg /* Inline summary maintains info about change probabilities. */
329 1.1.1.3 mrg vec<inline_param_summary> m_inline_param_summary;
330 1.1.1.3 mrg
331 1.1.1.3 mrg /* The following is used only to resolve indirect calls. */
332 1.1.1.3 mrg
333 1.1.1.3 mrg /* Vector describing known values of parameters. */
334 1.1.1.3 mrg vec<tree> m_known_vals;
335 1.1.1.3 mrg /* Vector describing known polymorphic call contexts. */
336 1.1.1.3 mrg vec<ipa_polymorphic_call_context> m_known_contexts;
337 1.1.1.3 mrg /* Vector describing known aggregate values. */
338 1.1.1.3 mrg vec<ipa_agg_value_set> m_known_aggs;
339 1.1.1.3 mrg };
340 1.1.1.3 mrg
341 1.1.1.2 mrg extern fast_call_summary <ipa_call_summary *, va_heap> *ipa_call_summaries;
342 1.1 mrg
343 1.1 mrg /* In ipa-fnsummary.c */
344 1.1 mrg void ipa_debug_fn_summary (struct cgraph_node *);
345 1.1 mrg void ipa_dump_fn_summaries (FILE *f);
346 1.1 mrg void ipa_dump_fn_summary (FILE *f, struct cgraph_node *node);
347 1.1 mrg void ipa_dump_hints (FILE *f, ipa_hints);
348 1.1 mrg void ipa_free_fn_summary (void);
349 1.1.1.3 mrg void ipa_free_size_summary (void);
350 1.1 mrg void inline_analyze_function (struct cgraph_node *node);
351 1.1 mrg void estimate_ipcp_clone_size_and_time (struct cgraph_node *,
352 1.1 mrg vec<tree>,
353 1.1 mrg vec<ipa_polymorphic_call_context>,
354 1.1.1.3 mrg vec<ipa_agg_value_set>,
355 1.1 mrg int *, sreal *, sreal *,
356 1.1 mrg ipa_hints *);
357 1.1 mrg void ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge);
358 1.1.1.3 mrg void ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset = true);
359 1.1 mrg void compute_fn_summary (struct cgraph_node *, bool);
360 1.1 mrg
361 1.1 mrg
362 1.1.1.3 mrg void evaluate_properties_for_edge (struct cgraph_edge *e,
363 1.1.1.3 mrg bool inline_p,
364 1.1 mrg clause_t *clause_ptr,
365 1.1 mrg clause_t *nonspec_clause_ptr,
366 1.1 mrg vec<tree> *known_vals_ptr,
367 1.1 mrg vec<ipa_polymorphic_call_context>
368 1.1 mrg *known_contexts_ptr,
369 1.1.1.3 mrg vec<ipa_agg_value_set> *);
370 1.1 mrg
371 1.1 mrg void ipa_fnsummary_c_finalize (void);
372 1.1.1.3 mrg HOST_WIDE_INT ipa_get_stack_frame_offset (struct cgraph_node *node);
373 1.1.1.3 mrg void ipa_remove_from_growth_caches (struct cgraph_edge *edge);
374 1.1.1.3 mrg
375 1.1.1.3 mrg /* Return true if EDGE is a cross module call. */
376 1.1.1.3 mrg
377 1.1.1.3 mrg static inline bool
378 1.1.1.3 mrg cross_module_call_p (struct cgraph_edge *edge)
379 1.1.1.3 mrg {
380 1.1.1.3 mrg /* Here we do not want to walk to alias target becuase ICF may create
381 1.1.1.3 mrg cross-unit aliases. */
382 1.1.1.3 mrg if (edge->caller->unit_id == edge->callee->unit_id)
383 1.1.1.3 mrg return false;
384 1.1.1.3 mrg /* If the call is to a (former) comdat function or s symbol with mutiple
385 1.1.1.3 mrg extern inline definitions then treat is as in-module call. */
386 1.1.1.3 mrg if (edge->callee->merged_extern_inline || edge->callee->merged_comdat
387 1.1.1.3 mrg || DECL_COMDAT (edge->callee->decl))
388 1.1.1.3 mrg return false;
389 1.1.1.3 mrg return true;
390 1.1.1.3 mrg }
391 1.1 mrg
392 1.1 mrg #endif /* GCC_IPA_FNSUMMARY_H */
393