ed4d66fef4afcec0efa80b41e34ebbb172dd99d2
[gcc.git] / gcc / ipa-inline.h
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
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 #ifndef GCC_IPA_INLINE_H
22 #define GCC_IPA_INLINE_H
23
24
25 /* Representation of inline parameters that do depend on context function is
26 inlined into (i.e. known constant values of function parameters.
27
28 Conditions that are interesting for function body are collected into CONDS
29 vector. They are of simple for function_param OP VAL, where VAL is
30 IPA invariant. The conditions are then referred by predicates. */
31
32 struct GTY(()) condition
33 {
34 /* If agg_contents is set, this is the offset from which the used data was
35 loaded. */
36 HOST_WIDE_INT offset;
37 tree val;
38 int operand_num;
39 ENUM_BITFIELD(tree_code) code : 16;
40 /* Set if the used data were loaded from an aggregate parameter or from
41 data received by reference. */
42 unsigned agg_contents : 1;
43 /* If agg_contents is set, this differentiates between loads from data
44 passed by reference and by value. */
45 unsigned by_ref : 1;
46 };
47
48 /* Inline hints are reasons why inline heuristics should preffer inlining given
49 function. They are represtented as bitmap of the following values. */
50 enum inline_hints_vals {
51 /* When inlining turns indirect call into a direct call,
52 it is good idea to do so. */
53 INLINE_HINT_indirect_call = 1,
54 /* Inlining may make loop iterations or loop stride known. It is good idea
55 to do so because it enables loop optimizatoins. */
56 INLINE_HINT_loop_iterations = 2,
57 INLINE_HINT_loop_stride = 4,
58 /* Inlining within same strongly connected component of callgraph is often
59 a loss due to increased stack frame usage and prologue setup costs. */
60 INLINE_HINT_same_scc = 8,
61 /* Inlining functions in strongly connected component is not such a great
62 win. */
63 INLINE_HINT_in_scc = 16,
64 /* If function is declared inline by user, it may be good idea to inline
65 it. */
66 INLINE_HINT_declared_inline = 32,
67 /* Programs are usually still organized for non-LTO compilation and thus
68 if functions are in different modules, inlining may not be so important.
69 */
70 INLINE_HINT_cross_module = 64,
71 /* If array indexes of loads/stores become known there may be room for
72 further optimization. */
73 INLINE_HINT_array_index = 128,
74 /* We know that the callee is hot by profile. */
75 INLINE_HINT_known_hot = 256
76 };
77 typedef int inline_hints;
78
79
80 typedef vec<condition, va_gc> *conditions;
81
82 /* Representation of predicates i.e. formulas using conditions defined
83 above. Predicates are simple logical formulas in conjunctive-disjunctive
84 form.
85
86 Predicate is array of clauses terminated by 0. Every clause must be true
87 in order to make predicate true.
88 Clauses are represented as bitmaps of conditions. One of conditions
89 must be true in order for clause to be true. */
90
91 #define MAX_CLAUSES 8
92 typedef unsigned int clause_t;
93 struct GTY(()) predicate
94 {
95 clause_t clause[MAX_CLAUSES + 1];
96 };
97
98 /* Represnetation of function body size and time depending on the inline
99 context. We keep simple array of record, every containing of predicate
100 and time/size to account.
101
102 We keep values scaled up, so fractional sizes and times can be
103 accounted. */
104 #define INLINE_SIZE_SCALE 2
105 #define INLINE_TIME_SCALE (CGRAPH_FREQ_BASE * 2)
106 struct GTY(()) size_time_entry
107 {
108 struct predicate predicate;
109 int size;
110 int time;
111 };
112
113 /* Function inlining information. */
114 struct GTY(()) inline_summary
115 {
116 /* Information about the function body itself. */
117
118 /* Estimated stack frame consumption by the function. */
119 HOST_WIDE_INT estimated_self_stack_size;
120 /* Size of the function body. */
121 int self_size;
122 /* Time of the function body. */
123 int self_time;
124 /* Minimal size increase after inlining. */
125 int min_size;
126
127 /* False when there something makes inlining impossible (such as va_arg). */
128 unsigned inlinable : 1;
129 /* True when function contains cilk spawn (and thus we can not inline
130 into it). */
131 unsigned contains_cilk_spawn : 1;
132
133 /* Information about function that will result after applying all the
134 inline decisions present in the callgraph. Generally kept up to
135 date only for functions that are not inline clones. */
136
137 /* Estimated stack frame consumption by the function. */
138 HOST_WIDE_INT estimated_stack_size;
139 /* Expected offset of the stack frame of inlined function. */
140 HOST_WIDE_INT stack_frame_offset;
141 /* Estimated size of the function after inlining. */
142 int time;
143 int size;
144
145 /* Conditional size/time information. The summaries are being
146 merged during inlining. */
147 conditions conds;
148 vec<size_time_entry, va_gc> *entry;
149
150 /* Predicate on when some loop in the function becomes to have known
151 bounds. */
152 struct predicate * GTY((skip)) loop_iterations;
153 /* Predicate on when some loop in the function becomes to have known
154 stride. */
155 struct predicate * GTY((skip)) loop_stride;
156 /* Predicate on when some array indexes become constants. */
157 struct predicate * GTY((skip)) array_index;
158 /* Estimated growth for inlining all copies of the function before start
159 of small functions inlining.
160 This value will get out of date as the callers are duplicated, but
161 using up-to-date value in the badness metric mean a lot of extra
162 expenses. */
163 int growth;
164 /* Number of SCC on the beginning of inlining process. */
165 int scc_no;
166 };
167
168 class GTY((user)) inline_summary_t: public function_summary <inline_summary *>
169 {
170 public:
171 inline_summary_t (symbol_table *symtab, bool ggc):
172 function_summary <inline_summary *> (symtab, ggc) {}
173
174 static inline_summary_t *create_ggc (symbol_table *symtab)
175 {
176 struct inline_summary_t *summary = new (ggc_cleared_alloc <inline_summary_t> ())
177 inline_summary_t(symtab, true);
178 summary->disable_insertion_hook ();
179 return summary;
180 }
181
182
183 virtual void insert (cgraph_node *, inline_summary *);
184 virtual void remove (cgraph_node *node, inline_summary *);
185 virtual void duplicate (cgraph_node *src, cgraph_node *dst,
186 inline_summary *src_data, inline_summary *dst_data);
187 };
188
189 extern GTY(()) function_summary <inline_summary *> *inline_summaries;
190
191 /* Information kept about parameter of call site. */
192 struct inline_param_summary
193 {
194 /* REG_BR_PROB_BASE based probability that parameter will change in between
195 two invocation of the calls.
196 I.e. loop invariant parameters
197 REG_BR_PROB_BASE/estimated_iterations and regular
198 parameters REG_BR_PROB_BASE.
199
200 Value 0 is reserved for compile time invariants. */
201 int change_prob;
202 };
203
204 /* Information kept about callgraph edges. */
205 struct inline_edge_summary
206 {
207 /* Estimated size and time of the call statement. */
208 int call_stmt_size;
209 int call_stmt_time;
210 /* Depth of loop nest, 0 means no nesting. */
211 unsigned short int loop_depth;
212 struct predicate *predicate;
213 /* Array indexed by parameters.
214 0 means that parameter change all the time, REG_BR_PROB_BASE means
215 that parameter is constant. */
216 vec<inline_param_summary> param;
217 };
218
219 /* Need a typedef for inline_edge_summary because of inline function
220 'inline_edge_summary' below. */
221 typedef struct inline_edge_summary inline_edge_summary_t;
222 extern vec<inline_edge_summary_t> inline_edge_summary_vec;
223
224 struct edge_growth_cache_entry
225 {
226 int time, size;
227 inline_hints hints;
228 };
229
230 extern vec<edge_growth_cache_entry> edge_growth_cache;
231
232 /* In ipa-inline-analysis.c */
233 void debug_inline_summary (struct cgraph_node *);
234 void dump_inline_summaries (FILE *f);
235 void dump_inline_summary (FILE *f, struct cgraph_node *node);
236 void dump_inline_hints (FILE *f, inline_hints);
237 void inline_generate_summary (void);
238 void inline_read_summary (void);
239 void inline_write_summary (void);
240 void inline_free_summary (void);
241 void inline_analyze_function (struct cgraph_node *node);
242 void initialize_inline_failed (struct cgraph_edge *);
243 int estimate_time_after_inlining (struct cgraph_node *, struct cgraph_edge *);
244 int estimate_size_after_inlining (struct cgraph_node *, struct cgraph_edge *);
245 void estimate_ipcp_clone_size_and_time (struct cgraph_node *,
246 vec<tree>,
247 vec<ipa_polymorphic_call_context>,
248 vec<ipa_agg_jump_function_p>,
249 int *, int *, inline_hints *);
250 int estimate_growth (struct cgraph_node *);
251 bool growth_likely_positive (struct cgraph_node *, int);
252 void inline_merge_summary (struct cgraph_edge *edge);
253 void inline_update_overall_summary (struct cgraph_node *node);
254 int do_estimate_edge_size (struct cgraph_edge *edge);
255 int do_estimate_edge_time (struct cgraph_edge *edge);
256 inline_hints do_estimate_edge_hints (struct cgraph_edge *edge);
257 void initialize_growth_caches (void);
258 void free_growth_caches (void);
259 void compute_inline_parameters (struct cgraph_node *, bool);
260 bool speculation_useful_p (struct cgraph_edge *e, bool anticipate_inlining);
261 unsigned int early_inliner (function *fun);
262 bool inline_account_function_p (struct cgraph_node *node);
263
264
265 /* In ipa-inline-transform.c */
266 bool inline_call (struct cgraph_edge *, bool, vec<cgraph_edge *> *, int *, bool,
267 bool *callee_removed = NULL);
268 unsigned int inline_transform (struct cgraph_node *);
269 void clone_inlined_nodes (struct cgraph_edge *e, bool, bool, int *,
270 int freq_scale);
271
272 extern int ncalls_inlined;
273 extern int nfunctions_inlined;
274
275 static inline struct inline_edge_summary *
276 inline_edge_summary (struct cgraph_edge *edge)
277 {
278 return &inline_edge_summary_vec[edge->uid];
279 }
280
281
282 /* Return estimated size of the inline sequence of EDGE. */
283
284 static inline int
285 estimate_edge_size (struct cgraph_edge *edge)
286 {
287 int ret;
288 if ((int)edge_growth_cache.length () <= edge->uid
289 || !(ret = edge_growth_cache[edge->uid].size))
290 return do_estimate_edge_size (edge);
291 return ret - (ret > 0);
292 }
293
294 /* Return estimated callee growth after inlining EDGE. */
295
296 static inline int
297 estimate_edge_growth (struct cgraph_edge *edge)
298 {
299 #ifdef ENABLE_CHECKING
300 gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size
301 || !edge->callee->analyzed);
302 #endif
303 return (estimate_edge_size (edge)
304 - inline_edge_summary (edge)->call_stmt_size);
305 }
306
307 /* Return estimated callee runtime increase after inlning
308 EDGE. */
309
310 static inline int
311 estimate_edge_time (struct cgraph_edge *edge)
312 {
313 int ret;
314 if ((int)edge_growth_cache.length () <= edge->uid
315 || !(ret = edge_growth_cache[edge->uid].time))
316 return do_estimate_edge_time (edge);
317 return ret - (ret > 0);
318 }
319
320
321 /* Return estimated callee runtime increase after inlning
322 EDGE. */
323
324 static inline inline_hints
325 estimate_edge_hints (struct cgraph_edge *edge)
326 {
327 inline_hints ret;
328 if ((int)edge_growth_cache.length () <= edge->uid
329 || !(ret = edge_growth_cache[edge->uid].hints))
330 return do_estimate_edge_hints (edge);
331 return ret - 1;
332 }
333
334 /* Reset cached value for EDGE. */
335
336 static inline void
337 reset_edge_growth_cache (struct cgraph_edge *edge)
338 {
339 if ((int)edge_growth_cache.length () > edge->uid)
340 {
341 struct edge_growth_cache_entry zero = {0, 0, 0};
342 edge_growth_cache[edge->uid] = zero;
343 }
344 }
345
346 #endif /* GCC_IPA_INLINE_H */