Branch data Line data Source code
1 : : /* Function summary pass.
2 : : Copyright (C) 2003-2025 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 : : /* Analysis of function bodies used by inter-procedural passes
22 : :
23 : : We estimate for each function
24 : : - function body size and size after specializing into given context
25 : : - average function execution time in a given context
26 : : - function frame size
27 : : For each call
28 : : - call statement size, time and how often the parameters change
29 : :
30 : : ipa_fn_summary data structures store above information locally (i.e.
31 : : parameters of the function itself) and globally (i.e. parameters of
32 : : the function created by applying all the inline decisions already
33 : : present in the callgraph).
34 : :
35 : : We provide access to the ipa_fn_summary data structure and
36 : : basic logic updating the parameters when inlining is performed.
37 : :
38 : : The summaries are context sensitive. Context means
39 : : 1) partial assignment of known constant values of operands
40 : : 2) whether function is inlined into the call or not.
41 : : It is easy to add more variants. To represent function size and time
42 : : that depends on context (i.e. it is known to be optimized away when
43 : : context is known either by inlining or from IP-CP and cloning),
44 : : we use predicates.
45 : :
46 : : estimate_edge_size_and_time can be used to query
47 : : function size/time in the given context. ipa_merge_fn_summary_after_inlining merges
48 : : properties of caller and callee after inlining.
49 : :
50 : : Finally pass_inline_parameters is exported. This is used to drive
51 : : computation of function parameters used by the early inliner. IPA
52 : : inlined performs analysis via its analyze_function method. */
53 : :
54 : : #include "config.h"
55 : : #define INCLUDE_VECTOR
56 : : #include "system.h"
57 : : #include "coretypes.h"
58 : : #include "backend.h"
59 : : #include "target.h"
60 : : #include "tree.h"
61 : : #include "gimple.h"
62 : : #include "alloc-pool.h"
63 : : #include "tree-pass.h"
64 : : #include "ssa.h"
65 : : #include "tree-streamer.h"
66 : : #include "cgraph.h"
67 : : #include "diagnostic.h"
68 : : #include "fold-const.h"
69 : : #include "print-tree.h"
70 : : #include "tree-inline.h"
71 : : #include "gimple-pretty-print.h"
72 : : #include "cfganal.h"
73 : : #include "gimple-iterator.h"
74 : : #include "tree-cfg.h"
75 : : #include "tree-ssa-loop-niter.h"
76 : : #include "tree-ssa-loop.h"
77 : : #include "symbol-summary.h"
78 : : #include "sreal.h"
79 : : #include "ipa-cp.h"
80 : : #include "ipa-prop.h"
81 : : #include "ipa-fnsummary.h"
82 : : #include "cfgloop.h"
83 : : #include "tree-scalar-evolution.h"
84 : : #include "ipa-utils.h"
85 : : #include "cfgexpand.h"
86 : : #include "gimplify.h"
87 : : #include "stringpool.h"
88 : : #include "attribs.h"
89 : : #include "tree-into-ssa.h"
90 : : #include "symtab-clones.h"
91 : : #include "gimple-range.h"
92 : : #include "tree-dfa.h"
93 : :
94 : : /* Summaries. */
95 : : fast_function_summary <ipa_fn_summary *, va_gc> *ipa_fn_summaries;
96 : : fast_function_summary <ipa_size_summary *, va_heap> *ipa_size_summaries;
97 : : fast_call_summary <ipa_call_summary *, va_heap> *ipa_call_summaries;
98 : :
99 : : /* Edge predicates goes here. */
100 : : static object_allocator<ipa_predicate> edge_predicate_pool ("edge predicates");
101 : :
102 : :
103 : : /* Dump IPA hints. */
104 : : void
105 : 164 : ipa_dump_hints (FILE *f, ipa_hints hints)
106 : : {
107 : 164 : if (!hints)
108 : : return;
109 : 127 : fprintf (f, "IPA hints:");
110 : 127 : if (hints & INLINE_HINT_indirect_call)
111 : : {
112 : 23 : hints &= ~INLINE_HINT_indirect_call;
113 : 23 : fprintf (f, " indirect_call");
114 : : }
115 : 127 : if (hints & INLINE_HINT_loop_iterations)
116 : : {
117 : 3 : hints &= ~INLINE_HINT_loop_iterations;
118 : 3 : fprintf (f, " loop_iterations");
119 : : }
120 : 127 : if (hints & INLINE_HINT_loop_stride)
121 : : {
122 : 3 : hints &= ~INLINE_HINT_loop_stride;
123 : 3 : fprintf (f, " loop_stride");
124 : : }
125 : 127 : if (hints & INLINE_HINT_same_scc)
126 : : {
127 : 4 : hints &= ~INLINE_HINT_same_scc;
128 : 4 : fprintf (f, " same_scc");
129 : : }
130 : 127 : if (hints & INLINE_HINT_in_scc)
131 : : {
132 : 11 : hints &= ~INLINE_HINT_in_scc;
133 : 11 : fprintf (f, " in_scc");
134 : : }
135 : 127 : if (hints & INLINE_HINT_cross_module)
136 : : {
137 : 2 : hints &= ~INLINE_HINT_cross_module;
138 : 2 : fprintf (f, " cross_module");
139 : : }
140 : 127 : if (hints & INLINE_HINT_declared_inline)
141 : : {
142 : 101 : hints &= ~INLINE_HINT_declared_inline;
143 : 101 : fprintf (f, " declared_inline");
144 : : }
145 : 127 : if (hints & INLINE_HINT_known_hot)
146 : : {
147 : 1 : hints &= ~INLINE_HINT_known_hot;
148 : 1 : fprintf (f, " known_hot");
149 : : }
150 : 127 : if (hints & INLINE_HINT_builtin_constant_p)
151 : : {
152 : 4 : hints &= ~INLINE_HINT_builtin_constant_p;
153 : 4 : fprintf (f, " builtin_constant_p");
154 : : }
155 : 127 : gcc_assert (!hints);
156 : : }
157 : :
158 : :
159 : : /* Record SIZE and TIME to SUMMARY.
160 : : The accounted code will be executed when EXEC_PRED is true.
161 : : When NONCONST_PRED is false the code will evaluate to constant and
162 : : will get optimized out in specialized clones of the function.
163 : : If CALL is true account to call_size_time_table rather than
164 : : size_time_table. */
165 : :
166 : : void
167 : 128969544 : ipa_fn_summary::account_size_time (int size, sreal time,
168 : : const ipa_predicate &exec_pred,
169 : : const ipa_predicate &nonconst_pred_in,
170 : : bool call)
171 : : {
172 : 128969544 : size_time_entry *e;
173 : 128969544 : bool found = false;
174 : 128969544 : int i;
175 : 128969544 : ipa_predicate nonconst_pred;
176 : 128969544 : vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table;
177 : :
178 : 128969544 : if (exec_pred == false)
179 : 4070247 : return;
180 : :
181 : 128804494 : nonconst_pred = nonconst_pred_in & exec_pred;
182 : :
183 : 128804494 : if (nonconst_pred == false)
184 : : return;
185 : :
186 : : /* We need to create initial empty unconditional clause, but otherwise
187 : : we don't need to account empty times and sizes. */
188 : 150808245 : if (!size && time == 0 && table->length ())
189 : 3227090 : return;
190 : :
191 : : /* Only for calls we are unaccounting what we previously recorded. */
192 : 124899297 : gcc_checking_assert (time >= 0 || call);
193 : :
194 : 449190728 : for (i = 0; table->iterate (i, &e); i++)
195 : 421203760 : if (e->exec_predicate == exec_pred
196 : 421203760 : && e->nonconst_predicate == nonconst_pred)
197 : : {
198 : : found = true;
199 : : break;
200 : : }
201 : 124899297 : if (i == max_size_time_table_size)
202 : : {
203 : 0 : i = 0;
204 : 0 : found = true;
205 : 0 : e = &(*table)[0];
206 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
207 : 0 : fprintf (dump_file,
208 : : "\t\tReached limit on number of entries, "
209 : : "ignoring the predicate.");
210 : : }
211 : 124903614 : if (dump_file && (dump_flags & TDF_DETAILS) && (time != 0 || size))
212 : : {
213 : 4013 : fprintf (dump_file,
214 : : "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate exec:",
215 : 3786 : ((double) size) / ipa_fn_summary::size_scale,
216 : : (time.to_double ()), found ? "" : "new ");
217 : 3786 : exec_pred.dump (dump_file, conds, 0);
218 : 3786 : if (exec_pred != nonconst_pred)
219 : : {
220 : 81 : fprintf (dump_file, " nonconst:");
221 : 81 : nonconst_pred.dump (dump_file, conds);
222 : : }
223 : : else
224 : 3705 : fprintf (dump_file, "\n");
225 : : }
226 : 124899297 : if (!found)
227 : : {
228 : 27986968 : class size_time_entry new_entry;
229 : 27986968 : new_entry.size = size;
230 : 27986968 : new_entry.time = time;
231 : 27986968 : new_entry.exec_predicate = exec_pred;
232 : 27986968 : new_entry.nonconst_predicate = nonconst_pred;
233 : 27986968 : if (call)
234 : 1666614 : call_size_time_table.safe_push (new_entry);
235 : : else
236 : 26320354 : size_time_table.safe_push (new_entry);
237 : : }
238 : : else
239 : : {
240 : 96912329 : e->size += size;
241 : 96912329 : e->time += time;
242 : : /* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */
243 : : /* Tolerate small roundoff issues. */
244 : 96912329 : if (e->time < 0)
245 : 46 : e->time = 0;
246 : : }
247 : : }
248 : :
249 : : /* We proved E to be unreachable, redirect it to __builtin_unreachable. */
250 : :
251 : : static struct cgraph_edge *
252 : 183711 : redirect_to_unreachable (struct cgraph_edge *e)
253 : : {
254 : 183711 : struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
255 : 183711 : struct cgraph_node *target
256 : 183711 : = cgraph_node::get_create (builtin_decl_unreachable ());
257 : :
258 : 183711 : gcc_checking_assert (lookup_attribute ("cold",
259 : : DECL_ATTRIBUTES (target->decl)));
260 : :
261 : 183711 : if (e->speculative)
262 : 229 : e = cgraph_edge::resolve_speculation (e, target->decl);
263 : 183482 : else if (!e->callee)
264 : 681 : e = cgraph_edge::make_direct (e, target);
265 : : else
266 : 182801 : e->redirect_callee (target);
267 : 183711 : class ipa_call_summary *es = ipa_call_summaries->get (e);
268 : 183711 : e->inline_failed = CIF_UNREACHABLE;
269 : 183711 : e->count = profile_count::zero ();
270 : 183711 : es->call_stmt_size = 0;
271 : 183711 : es->call_stmt_time = 0;
272 : 183711 : if (callee)
273 : 0 : callee->remove_symbol_and_inline_clones ();
274 : 183711 : return e;
275 : : }
276 : :
277 : : /* Set predicate for edge E. */
278 : :
279 : : static void
280 : 28282829 : edge_set_predicate (struct cgraph_edge *e, ipa_predicate *predicate)
281 : : {
282 : : /* If the edge is determined to be never executed, redirect it
283 : : to BUILTIN_UNREACHABLE to make it clear to IPA passes the call will
284 : : be optimized out. */
285 : 25974445 : if (predicate && *predicate == false
286 : : /* When handling speculative edges, we need to do the redirection
287 : : just once. Do it always on the direct edge, so we do not
288 : : attempt to resolve speculation while duplicating the edge. */
289 : 28466590 : && (!e->speculative || e->callee))
290 : 183711 : e = redirect_to_unreachable (e);
291 : :
292 : 28282829 : class ipa_call_summary *es = ipa_call_summaries->get (e);
293 : 54257274 : if (predicate && *predicate != true)
294 : : {
295 : 3923821 : if (!es->predicate)
296 : 3608345 : es->predicate = edge_predicate_pool.allocate ();
297 : 3923821 : *es->predicate = *predicate;
298 : : }
299 : : else
300 : : {
301 : 24359008 : if (es->predicate)
302 : 593349 : edge_predicate_pool.remove (es->predicate);
303 : 24359008 : es->predicate = NULL;
304 : : }
305 : 28282829 : }
306 : :
307 : : /* Set predicate for hint *P. */
308 : :
309 : : static void
310 : 144892 : set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate)
311 : : {
312 : 144892 : if (new_predicate == false || new_predicate == true)
313 : : {
314 : 1804 : if (*p)
315 : 0 : edge_predicate_pool.remove (*p);
316 : 1804 : *p = NULL;
317 : : }
318 : : else
319 : : {
320 : 143088 : if (!*p)
321 : 143088 : *p = edge_predicate_pool.allocate ();
322 : 143088 : **p = new_predicate;
323 : : }
324 : 144892 : }
325 : :
326 : : /* Find if NEW_PREDICATE is already in V and if so, increment its freq.
327 : : Otherwise add a new item to the vector with this predicate and frerq equal
328 : : to add_freq, unless the number of predicates would exceed MAX_NUM_PREDICATES
329 : : in which case the function does nothing. */
330 : :
331 : : static void
332 : 1062316 : add_freqcounting_predicate (vec<ipa_freqcounting_predicate, va_gc> **v,
333 : : const ipa_predicate &new_predicate, sreal add_freq,
334 : : unsigned max_num_predicates)
335 : : {
336 : 1062316 : if (new_predicate == false || new_predicate == true)
337 : : return;
338 : : ipa_freqcounting_predicate *f;
339 : 135181 : for (int i = 0; vec_safe_iterate (*v, i, &f); i++)
340 : 70027 : if (new_predicate == f->predicate)
341 : : {
342 : 0 : f->freq += add_freq;
343 : 0 : return;
344 : : }
345 : 90179 : if (vec_safe_length (*v) >= max_num_predicates)
346 : : /* Too many different predicates to account for. */
347 : : return;
348 : :
349 : 64882 : ipa_freqcounting_predicate fcp;
350 : 64882 : fcp.predicate = NULL;
351 : 64882 : set_hint_predicate (&fcp.predicate, new_predicate);
352 : 64882 : fcp.freq = add_freq;
353 : 64882 : vec_safe_push (*v, fcp);
354 : 64882 : return;
355 : : }
356 : :
357 : : /* Compute what conditions may or may not hold given information about
358 : : parameters. RET_CLAUSE returns truths that may hold in a specialized copy,
359 : : while RET_NONSPEC_CLAUSE returns truths that may hold in an nonspecialized
360 : : copy when called in a given context. It is a bitmask of conditions. Bit
361 : : 0 means that condition is known to be false, while bit 1 means that condition
362 : : may or may not be true. These differs - for example NOT_INLINED condition
363 : : is always false in the second and also builtin_constant_p tests cannot use
364 : : the fact that parameter is indeed a constant.
365 : :
366 : : When INLINE_P is true, assume that we are inlining. AVAL contains known
367 : : information about argument values. The function does not modify its content
368 : : and so AVALs could also be of type ipa_call_arg_values but so far all
369 : : callers work with the auto version and so we avoid the conversion for
370 : : convenience.
371 : :
372 : : ERROR_MARK value of an argument means compile time invariant. */
373 : :
374 : : static void
375 : 19620041 : evaluate_conditions_for_known_args (struct cgraph_node *node,
376 : : bool inline_p,
377 : : ipa_auto_call_arg_values *avals,
378 : : clause_t *ret_clause,
379 : : clause_t *ret_nonspec_clause,
380 : : ipa_call_summary *es)
381 : : {
382 : 19620041 : clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
383 : 19620041 : clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition;
384 : 19620041 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
385 : 19620041 : int i;
386 : 19620041 : struct condition *c;
387 : :
388 : 77818607 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
389 : : {
390 : 58198566 : tree val = NULL;
391 : 58198566 : tree res;
392 : 58198566 : int j;
393 : 58198566 : struct expr_eval_op *op;
394 : :
395 : 58198566 : if (c->code == ipa_predicate::not_sra_candidate)
396 : : {
397 : 13891283 : if (!inline_p
398 : 13891283 : || !es
399 : 13306373 : || (int)es->param.length () <= c->operand_num
400 : 20544468 : || !es->param[c->operand_num].points_to_possible_sra_candidate)
401 : 10516295 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
402 : 13891283 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
403 : 58198566 : continue;
404 : : }
405 : :
406 : 44307283 : if (c->agg_contents)
407 : : {
408 : 22407848 : if (c->code == ipa_predicate::changed
409 : 22403430 : && !c->by_ref
410 : 22403430 : && (avals->safe_sval_at(c->operand_num) == error_mark_node))
411 : 4418 : continue;
412 : :
413 : 22399012 : if (tree sval = avals->safe_sval_at (c->operand_num))
414 : 10441054 : val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref);
415 : 10441054 : if (!val)
416 : : {
417 : 22388519 : ipa_argagg_value_list avs (avals);
418 : 22388519 : val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
419 : 22388519 : c->by_ref);
420 : : }
421 : : }
422 : : else
423 : : {
424 : 21903853 : val = avals->safe_sval_at (c->operand_num);
425 : 21903853 : if (val && val == error_mark_node
426 : 1909684 : && c->code != ipa_predicate::changed)
427 : : val = NULL_TREE;
428 : : }
429 : :
430 : 33166420 : if (!val
431 : 32428196 : && (c->code == ipa_predicate::changed
432 : : || c->code == ipa_predicate::is_not_constant))
433 : : {
434 : 21419608 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
435 : 21419608 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
436 : 21419608 : continue;
437 : : }
438 : 22883257 : if (c->code == ipa_predicate::changed)
439 : : {
440 : 6543573 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
441 : 6543573 : continue;
442 : : }
443 : :
444 : 16339684 : if (c->code == ipa_predicate::is_not_constant)
445 : : {
446 : 5191 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
447 : 5191 : continue;
448 : : }
449 : :
450 : 16334493 : if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val)))
451 : : {
452 : 5108568 : if (c->type != TREE_TYPE (val))
453 : 968984 : val = fold_unary (VIEW_CONVERT_EXPR, c->type, val);
454 : 5392383 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
455 : : {
456 : 284206 : if (!val)
457 : : break;
458 : 283815 : if (!op->val[0])
459 : 102809 : val = fold_unary (op->code, op->type, val);
460 : 181006 : else if (!op->val[1])
461 : 362012 : val = fold_binary (op->code, op->type,
462 : : op->index ? op->val[0] : val,
463 : : op->index ? val : op->val[0]);
464 : 0 : else if (op->index == 0)
465 : 0 : val = fold_ternary (op->code, op->type,
466 : : val, op->val[0], op->val[1]);
467 : 0 : else if (op->index == 1)
468 : 0 : val = fold_ternary (op->code, op->type,
469 : : op->val[0], val, op->val[1]);
470 : 0 : else if (op->index == 2)
471 : 0 : val = fold_ternary (op->code, op->type,
472 : : op->val[0], op->val[1], val);
473 : : else
474 : : val = NULL_TREE;
475 : : }
476 : :
477 : 12727329 : res = val
478 : 5108568 : ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
479 : : : NULL;
480 : :
481 : 5108157 : if (res && integer_zerop (res))
482 : 2597964 : continue;
483 : 2510604 : if (res && integer_onep (res))
484 : : {
485 : 2489897 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
486 : 2489897 : nonspec_clause
487 : 2489897 : |= 1 << (i + ipa_predicate::first_dynamic_condition);
488 : 2489897 : continue;
489 : : }
490 : : }
491 : 11246632 : if (c->operand_num < (int) avals->m_known_value_ranges.length ()
492 : 6074953 : && !c->agg_contents
493 : 13031007 : && (!val || TREE_CODE (val) != INTEGER_CST))
494 : : {
495 : 1668128 : value_range vr (avals->m_known_value_ranges[c->operand_num]);
496 : 1668128 : if (!vr.undefined_p ()
497 : 973154 : && !vr.varying_p ()
498 : 2641282 : && (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ())))
499 : : {
500 : 972625 : if (!useless_type_conversion_p (c->type, vr.type ()))
501 : 301 : range_cast (vr, c->type);
502 : :
503 : 1251359 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
504 : : {
505 : 314728 : if (vr.varying_p () || vr.undefined_p ())
506 : : break;
507 : :
508 : 278734 : value_range res (op->type);
509 : 278734 : if (!op->val[0])
510 : : {
511 : 106002 : value_range varying (op->type);
512 : 106002 : varying.set_varying (op->type);
513 : 106002 : range_op_handler handler (op->code);
514 : 106002 : if (!handler
515 : 105994 : || !res.supports_type_p (op->type)
516 : 211996 : || !handler.fold_range (res, op->type, vr, varying))
517 : 8 : res.set_varying (op->type);
518 : 106002 : }
519 : 172732 : else if (!op->val[1])
520 : : {
521 : 172560 : value_range op0 (TREE_TYPE (op->val[0]));
522 : 172560 : range_op_handler handler (op->code);
523 : :
524 : 172560 : ipa_get_range_from_ip_invariant (op0, op->val[0], node);
525 : :
526 : 172560 : if (!handler
527 : 172560 : || !res.supports_type_p (op->type)
528 : 517144 : || !handler.fold_range (res, op->type,
529 : : op->index ? op0 : vr,
530 : 172560 : op->index ? vr : op0))
531 : 0 : res.set_varying (op->type);
532 : 172560 : }
533 : : else
534 : 172 : res.set_varying (op->type);
535 : 278734 : vr = res;
536 : 278734 : }
537 : 972625 : if (!vr.varying_p () && !vr.undefined_p ())
538 : : {
539 : 925833 : int_range<2> res;
540 : 925833 : value_range val_vr (TREE_TYPE (c->val));
541 : 925833 : range_op_handler handler (c->code);
542 : :
543 : 925833 : ipa_get_range_from_ip_invariant (val_vr, c->val, node);
544 : :
545 : 925833 : if (!handler
546 : 925833 : || !val_vr.supports_type_p (TREE_TYPE (c->val))
547 : 1851666 : || !handler.fold_range (res, boolean_type_node, vr, val_vr))
548 : 0 : res.set_varying (boolean_type_node);
549 : :
550 : 925833 : if (res.zero_p ())
551 : 188754 : continue;
552 : 925833 : }
553 : : }
554 : 1668128 : }
555 : :
556 : 11057878 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
557 : 11057878 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
558 : : }
559 : 19620041 : *ret_clause = clause;
560 : 19620041 : if (ret_nonspec_clause)
561 : 17033200 : *ret_nonspec_clause = nonspec_clause;
562 : 19620041 : }
563 : :
564 : : /* Return true if VRP will be exectued on the function.
565 : : We do not want to anticipate optimizations that will not happen.
566 : :
567 : : FIXME: This can be confused with -fdisable and debug counters and thus
568 : : it should not be used for correctness (only to make heuristics work).
569 : : This means that inliner should do its own optimizations of expressions
570 : : that it predicts to be constant so wrong code can not be triggered by
571 : : builtin_constant_p. */
572 : :
573 : : static bool
574 : 10209059 : vrp_will_run_p (struct cgraph_node *node)
575 : : {
576 : 10209059 : return (opt_for_fn (node->decl, optimize)
577 : 10209059 : && !opt_for_fn (node->decl, optimize_debug)
578 : 20416866 : && opt_for_fn (node->decl, flag_tree_vrp));
579 : : }
580 : :
581 : : /* Similarly about FRE. */
582 : :
583 : : static bool
584 : 12483336 : fre_will_run_p (struct cgraph_node *node)
585 : : {
586 : 12483336 : return (opt_for_fn (node->decl, optimize)
587 : 12483336 : && !opt_for_fn (node->decl, optimize_debug)
588 : 24964356 : && opt_for_fn (node->decl, flag_tree_fre));
589 : : }
590 : :
591 : : /* Work out what conditions might be true at invocation of E.
592 : : Compute costs for inlined edge if INLINE_P is true.
593 : :
594 : : Return in CLAUSE_PTR the evaluated conditions and in NONSPEC_CLAUSE_PTR
595 : : (if non-NULL) conditions evaluated for nonspecialized clone called
596 : : in a given context.
597 : :
598 : : Vectors in AVALS will be populated with useful known information about
599 : : argument values - information not known to have any uses will be omitted -
600 : : except for m_known_contexts which will only be calculated if
601 : : COMPUTE_CONTEXTS is true. */
602 : :
603 : : void
604 : 19448656 : evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
605 : : clause_t *clause_ptr,
606 : : clause_t *nonspec_clause_ptr,
607 : : ipa_auto_call_arg_values *avals,
608 : : bool compute_contexts)
609 : : {
610 : 19448656 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
611 : 19448656 : class ipa_fn_summary *info = ipa_fn_summaries->get (callee);
612 : 19448656 : class ipa_edge_args *args;
613 : 19448656 : class ipa_call_summary *es = NULL;
614 : :
615 : 19448656 : if (clause_ptr)
616 : 19448656 : *clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
617 : :
618 : 19448656 : if (ipa_node_params_sum
619 : 8673622 : && !e->call_stmt_cannot_inline_p
620 : 8673622 : && (info->conds || compute_contexts)
621 : 28122278 : && (args = ipa_edge_args_sum->get (e)) != NULL)
622 : : {
623 : 8620006 : struct cgraph_node *caller;
624 : 8620006 : class ipa_node_params *caller_parms_info, *callee_pi = NULL;
625 : 8620006 : int i, count = ipa_get_cs_argument_count (args);
626 : 8620006 : es = ipa_call_summaries->get (e);
627 : :
628 : 8620006 : if (count)
629 : : {
630 : 7959167 : if (e->caller->inlined_to)
631 : : caller = e->caller->inlined_to;
632 : : else
633 : 6529927 : caller = e->caller;
634 : 7959167 : caller_parms_info = ipa_node_params_sum->get (caller);
635 : 7959167 : callee_pi = ipa_node_params_sum->get (callee);
636 : :
637 : : /* Watch for thunks. */
638 : 7959167 : if (callee_pi)
639 : : /* Watch for variadic functions. */
640 : 7958880 : count = MIN (count, ipa_get_param_count (callee_pi));
641 : : }
642 : :
643 : 7958880 : if (callee_pi)
644 : 26445143 : for (i = 0; i < count; i++)
645 : : {
646 : 18486263 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
647 : :
648 : 18486263 : if (ipa_is_param_used_by_indirect_call (callee_pi, i)
649 : 18486263 : || ipa_is_param_used_by_ipa_predicates (callee_pi, i))
650 : : {
651 : : /* Determine if we know constant value of the parameter. */
652 : 12483336 : tree type = ipa_get_type (callee_pi, i);
653 : 12483336 : tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type);
654 : :
655 : 9605452 : if (!cst && e->call_stmt
656 : 22053584 : && i < (int)gimple_call_num_args (e->call_stmt))
657 : : {
658 : 9570248 : cst = gimple_call_arg (e->call_stmt, i);
659 : 9570248 : if (!is_gimple_min_invariant (cst))
660 : : cst = NULL;
661 : : }
662 : 3056200 : if (cst)
663 : : {
664 : 3020996 : gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
665 : 3020996 : if (!avals->m_known_vals.length ())
666 : 1572328 : avals->m_known_vals.safe_grow_cleared (count, true);
667 : 3020996 : avals->m_known_vals[i] = cst;
668 : : }
669 : 9462340 : else if (inline_p && !es->param[i].change_prob)
670 : : {
671 : 3418062 : if (!avals->m_known_vals.length ())
672 : 2873396 : avals->m_known_vals.safe_grow_cleared (count, true);
673 : 3418062 : avals->m_known_vals[i] = error_mark_node;
674 : : }
675 : :
676 : : /* If we failed to get simple constant, try value range. */
677 : 3020996 : if ((!cst || TREE_CODE (cst) != INTEGER_CST)
678 : 10209059 : && vrp_will_run_p (caller)
679 : 22548754 : && ipa_is_param_used_by_ipa_predicates (callee_pi, i))
680 : : {
681 : 10035918 : value_range vr (type);
682 : :
683 : 10035918 : ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type);
684 : 10035918 : if (!vr.undefined_p () && !vr.varying_p ())
685 : : {
686 : 6855966 : if (!avals->m_known_value_ranges.length ())
687 : : {
688 : 4979375 : avals->m_known_value_ranges.safe_grow_cleared (count,
689 : : true);
690 : 15938857 : for (int i = 0; i < count; ++i)
691 : 10959482 : avals->m_known_value_ranges[i].set_type (void_type_node);
692 : : }
693 : 6855966 : avals->m_known_value_ranges[i] = vr;
694 : : }
695 : 10035918 : }
696 : :
697 : : /* Determine known aggregate values. */
698 : 12483336 : if (fre_will_run_p (caller))
699 : 12480660 : ipa_push_agg_values_from_jfunc (caller_parms_info,
700 : : caller, &jf->agg, i,
701 : : &avals->m_known_aggs);
702 : : }
703 : :
704 : : /* For calls used in polymorphic calls we further determine
705 : : polymorphic call context. */
706 : 18486263 : if (compute_contexts
707 : 18486263 : && ipa_is_param_used_by_polymorphic_call (callee_pi, i))
708 : : {
709 : 89745 : ipa_polymorphic_call_context
710 : 89745 : ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
711 : 179490 : if (!ctx.useless_p ())
712 : : {
713 : 88529 : if (!avals->m_known_contexts.length ())
714 : 88392 : avals->m_known_contexts.safe_grow_cleared (count, true);
715 : 88529 : avals->m_known_contexts[i]
716 : 177058 : = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
717 : : }
718 : : }
719 : : }
720 : : else
721 : 661126 : gcc_assert (!count || callee->thunk);
722 : : }
723 : 10828650 : else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds)
724 : : {
725 : 8476390 : int i, count = (int)gimple_call_num_args (e->call_stmt);
726 : :
727 : 24056856 : for (i = 0; i < count; i++)
728 : : {
729 : 15580466 : tree cst = gimple_call_arg (e->call_stmt, i);
730 : 15580466 : if (!is_gimple_min_invariant (cst))
731 : : cst = NULL;
732 : 5609043 : if (cst)
733 : : {
734 : 5609043 : if (!avals->m_known_vals.length ())
735 : 3887808 : avals->m_known_vals.safe_grow_cleared (count, true);
736 : 5609043 : avals->m_known_vals[i] = cst;
737 : : }
738 : : }
739 : : }
740 : :
741 : 19448656 : evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr,
742 : : nonspec_clause_ptr, es);
743 : 19448656 : }
744 : :
745 : :
746 : : /* Allocate the function summary. */
747 : :
748 : : static void
749 : 449565 : ipa_fn_summary_alloc (void)
750 : : {
751 : 449565 : gcc_checking_assert (!ipa_fn_summaries);
752 : 449565 : ipa_size_summaries = new ipa_size_summary_t (symtab);
753 : 449565 : ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab);
754 : 449565 : ipa_call_summaries = new ipa_call_summary_t (symtab);
755 : 449565 : }
756 : :
757 : 25208929 : ipa_call_summary::~ipa_call_summary ()
758 : : {
759 : 25208929 : if (predicate)
760 : 3014996 : edge_predicate_pool.remove (predicate);
761 : :
762 : 25208929 : param.release ();
763 : 25208929 : }
764 : :
765 : 9320921 : ipa_fn_summary::~ipa_fn_summary ()
766 : : {
767 : 9320921 : unsigned len = vec_safe_length (loop_iterations);
768 : 9428125 : for (unsigned i = 0; i < len; i++)
769 : 107204 : edge_predicate_pool.remove ((*loop_iterations)[i].predicate);
770 : 9320921 : len = vec_safe_length (loop_strides);
771 : 9356805 : for (unsigned i = 0; i < len; i++)
772 : 35884 : edge_predicate_pool.remove ((*loop_strides)[i].predicate);
773 : 9320921 : vec_free (conds);
774 : 9320921 : call_size_time_table.release ();
775 : 9320921 : vec_free (loop_iterations);
776 : 9320921 : vec_free (loop_strides);
777 : 9320921 : builtin_constant_p_parms.release ();
778 : 9320921 : }
779 : :
780 : : void
781 : 6867671 : ipa_fn_summary_t::remove_callees (cgraph_node *node)
782 : : {
783 : 6867671 : cgraph_edge *e;
784 : 28125447 : for (e = node->callees; e; e = e->next_callee)
785 : 21257776 : ipa_call_summaries->remove (e);
786 : 7339870 : for (e = node->indirect_calls; e; e = e->next_callee)
787 : 472199 : ipa_call_summaries->remove (e);
788 : 6867671 : }
789 : :
790 : : /* Duplicate predicates in loop hint vector, allocating memory for them and
791 : : remove and deallocate any uninteresting (true or false) ones. Return the
792 : : result. */
793 : :
794 : : static vec<ipa_freqcounting_predicate, va_gc> *
795 : 22164 : remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v,
796 : : clause_t possible_truths)
797 : : {
798 : 22164 : if (vec_safe_length (v) == 0)
799 : : return NULL;
800 : :
801 : 2902 : vec<ipa_freqcounting_predicate, va_gc> *res = v->copy ();
802 : 2902 : int len = res->length();
803 : 6751 : for (int i = len - 1; i >= 0; i--)
804 : : {
805 : 3849 : ipa_predicate new_predicate
806 : 3849 : = (*res)[i].predicate->remap_after_duplication (possible_truths);
807 : : /* We do not want to free previous predicate; it is used by node
808 : : origin. */
809 : 3849 : (*res)[i].predicate = NULL;
810 : 3849 : set_hint_predicate (&(*res)[i].predicate, new_predicate);
811 : :
812 : 3849 : if (!(*res)[i].predicate)
813 : 1804 : res->unordered_remove (i);
814 : : }
815 : :
816 : : return res;
817 : : }
818 : :
819 : :
820 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
821 : : void
822 : 2367377 : ipa_fn_summary_t::duplicate (cgraph_node *src,
823 : : cgraph_node *dst,
824 : : ipa_fn_summary *src_info,
825 : : ipa_fn_summary *info)
826 : : {
827 : 2367377 : new (info) ipa_fn_summary (*src_info);
828 : : /* TODO: as an optimization, we may avoid copying conditions
829 : : that are known to be false or true. */
830 : 2367377 : info->conds = vec_safe_copy (info->conds);
831 : :
832 : 2367377 : clone_info *cinfo = clone_info::get (dst);
833 : : /* When there are any replacements in the function body, see if we can figure
834 : : out that something was optimized out. */
835 : 2367377 : if (ipa_node_params_sum && cinfo && cinfo->tree_map)
836 : : {
837 : : /* Use SRC parm info since it may not be copied yet. */
838 : 11082 : ipa_node_params *parms_info = ipa_node_params_sum->get (src);
839 : 11082 : ipa_auto_call_arg_values avals;
840 : 11082 : int count = ipa_get_param_count (parms_info);
841 : 11082 : int i, j;
842 : 11082 : clause_t possible_truths;
843 : 11082 : ipa_predicate true_pred = true;
844 : 11082 : size_time_entry *e;
845 : 11082 : int optimized_out_size = 0;
846 : 11082 : bool inlined_to_p = false;
847 : 11082 : struct cgraph_edge *edge, *next;
848 : :
849 : 11082 : info->size_time_table.release ();
850 : 11082 : avals.m_known_vals.safe_grow_cleared (count, true);
851 : 44246 : for (i = 0; i < count; i++)
852 : : {
853 : : struct ipa_replace_map *r;
854 : :
855 : 108552 : for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++)
856 : : {
857 : 60410 : if (r->parm_num == i)
858 : : {
859 : 18186 : avals.m_known_vals[i] = r->new_tree;
860 : 18186 : break;
861 : : }
862 : : }
863 : : }
864 : 11082 : evaluate_conditions_for_known_args (dst, false,
865 : : &avals,
866 : : &possible_truths,
867 : : /* We are going to specialize,
868 : : so ignore nonspec truths. */
869 : : NULL,
870 : : NULL);
871 : :
872 : 11082 : info->account_size_time (0, 0, true_pred, true_pred);
873 : :
874 : : /* Remap size_time vectors.
875 : : Simplify the predicate by pruning out alternatives that are known
876 : : to be false.
877 : : TODO: as on optimization, we can also eliminate conditions known
878 : : to be true. */
879 : 77129 : for (i = 0; src_info->size_time_table.iterate (i, &e); i++)
880 : : {
881 : 66047 : ipa_predicate new_exec_pred;
882 : 66047 : ipa_predicate new_nonconst_pred;
883 : 66047 : new_exec_pred = e->exec_predicate.remap_after_duplication
884 : 66047 : (possible_truths);
885 : 66047 : new_nonconst_pred = e->nonconst_predicate.remap_after_duplication
886 : 66047 : (possible_truths);
887 : 66047 : if (new_exec_pred == false || new_nonconst_pred == false)
888 : 7434 : optimized_out_size += e->size;
889 : : else
890 : 58613 : info->account_size_time (e->size, e->time, new_exec_pred,
891 : : new_nonconst_pred);
892 : : }
893 : :
894 : : /* Remap edge predicates with the same simplification as above.
895 : : Also copy constantness arrays. */
896 : 176030 : for (edge = dst->callees; edge; edge = next)
897 : : {
898 : 164948 : ipa_predicate new_predicate;
899 : 164948 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
900 : 164948 : next = edge->next_callee;
901 : :
902 : 164948 : if (!edge->inline_failed)
903 : 0 : inlined_to_p = true;
904 : 164948 : if (!es->predicate)
905 : 152941 : continue;
906 : 12007 : new_predicate = es->predicate->remap_after_duplication
907 : 12007 : (possible_truths);
908 : 12007 : if (new_predicate == false && *es->predicate != false)
909 : 2196 : optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale;
910 : 12007 : edge_set_predicate (edge, &new_predicate);
911 : : }
912 : :
913 : : /* Remap indirect edge predicates with the same simplification as above.
914 : : Also copy constantness arrays. */
915 : 11897 : for (edge = dst->indirect_calls; edge; edge = next)
916 : : {
917 : 815 : ipa_predicate new_predicate;
918 : 815 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
919 : 815 : next = edge->next_callee;
920 : :
921 : 815 : gcc_checking_assert (edge->inline_failed);
922 : 815 : if (!es->predicate)
923 : 703 : continue;
924 : 112 : new_predicate = es->predicate->remap_after_duplication
925 : 112 : (possible_truths);
926 : 112 : if (new_predicate == false && *es->predicate != false)
927 : 22 : optimized_out_size
928 : 22 : += es->call_stmt_size * ipa_fn_summary::size_scale;
929 : 112 : edge_set_predicate (edge, &new_predicate);
930 : : }
931 : 11082 : info->loop_iterations
932 : 11082 : = remap_freqcounting_preds_after_dup (info->loop_iterations,
933 : : possible_truths);
934 : 11082 : info->loop_strides
935 : 11082 : = remap_freqcounting_preds_after_dup (info->loop_strides,
936 : : possible_truths);
937 : 11082 : if (info->builtin_constant_p_parms.length())
938 : : {
939 : 25 : vec <int, va_heap, vl_ptr> parms = info->builtin_constant_p_parms;
940 : 25 : int ip;
941 : 25 : info->builtin_constant_p_parms = vNULL;
942 : 50 : for (i = 0; parms.iterate (i, &ip); i++)
943 : 25 : if (!avals.m_known_vals[ip])
944 : 4 : info->builtin_constant_p_parms.safe_push (ip);
945 : : }
946 : :
947 : : /* If inliner or someone after inliner will ever start producing
948 : : non-trivial clones, we will get trouble with lack of information
949 : : about updating self sizes, because size vectors already contains
950 : : sizes of the callees. */
951 : 11082 : gcc_assert (!inlined_to_p || !optimized_out_size);
952 : 11082 : }
953 : : else
954 : : {
955 : 2356295 : info->size_time_table = src_info->size_time_table.copy ();
956 : 2356295 : info->loop_iterations = vec_safe_copy (src_info->loop_iterations);
957 : 2356295 : info->loop_strides = vec_safe_copy (info->loop_strides);
958 : :
959 : 2356295 : info->builtin_constant_p_parms
960 : 2356295 : = info->builtin_constant_p_parms.copy ();
961 : :
962 : 2356295 : ipa_freqcounting_predicate *f;
963 : 2403856 : for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++)
964 : : {
965 : 47561 : ipa_predicate p = *f->predicate;
966 : 47561 : f->predicate = NULL;
967 : 47561 : set_hint_predicate (&f->predicate, p);
968 : : }
969 : 2383375 : for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++)
970 : : {
971 : 27080 : ipa_predicate p = *f->predicate;
972 : 27080 : f->predicate = NULL;
973 : 27080 : set_hint_predicate (&f->predicate, p);
974 : : }
975 : : }
976 : 2367377 : if (!dst->inlined_to)
977 : 117928 : ipa_update_overall_fn_summary (dst);
978 : 2367377 : }
979 : :
980 : :
981 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
982 : :
983 : : void
984 : 3116305 : ipa_call_summary_t::duplicate (struct cgraph_edge *src,
985 : : struct cgraph_edge *dst,
986 : : class ipa_call_summary *srcinfo,
987 : : class ipa_call_summary *info)
988 : : {
989 : 3116305 : new (info) ipa_call_summary (*srcinfo);
990 : 3116305 : info->predicate = NULL;
991 : 3116305 : edge_set_predicate (dst, srcinfo->predicate);
992 : 3116305 : info->param = srcinfo->param.copy ();
993 : 3116305 : if (!dst->indirect_unknown_callee && src->indirect_unknown_callee)
994 : : {
995 : 10299 : info->call_stmt_size -= (eni_size_weights.indirect_call_cost
996 : 10299 : - eni_size_weights.call_cost);
997 : 10299 : info->call_stmt_time -= (eni_time_weights.indirect_call_cost
998 : 10299 : - eni_time_weights.call_cost);
999 : : }
1000 : 3116305 : }
1001 : :
1002 : : /* Dump edge summaries associated to NODE and recursively to all clones.
1003 : : Indent by INDENT. */
1004 : :
1005 : : static void
1006 : 1899 : dump_ipa_call_summary (FILE *f, int indent, struct cgraph_node *node,
1007 : : class ipa_fn_summary *info)
1008 : : {
1009 : 1899 : struct cgraph_edge *edge;
1010 : 9548 : for (edge = node->callees; edge; edge = edge->next_callee)
1011 : : {
1012 : 7649 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1013 : 7649 : struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1014 : 7649 : int i;
1015 : :
1016 : 15298 : fprintf (f,
1017 : : "%*s%s %s\n%*s freq:%4.2f",
1018 : : indent, "", callee->dump_name (),
1019 : 7649 : !edge->inline_failed
1020 : 7150 : ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1021 : 7649 : indent, "", edge->sreal_frequency ().to_double ());
1022 : :
1023 : 7649 : if (cross_module_call_p (edge))
1024 : 4 : fprintf (f, " cross module");
1025 : :
1026 : 7649 : if (es)
1027 : 7150 : fprintf (f, " loop depth:%2i size:%2i time: %2i",
1028 : : es->loop_depth, es->call_stmt_size, es->call_stmt_time);
1029 : :
1030 : 7649 : ipa_fn_summary *s = ipa_fn_summaries->get (callee);
1031 : 7649 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1032 : 7649 : if (s != NULL)
1033 : 505 : fprintf (f, " callee size:%2i stack:%2i",
1034 : 505 : (int) (ss->size / ipa_fn_summary::size_scale),
1035 : 505 : (int) s->estimated_stack_size);
1036 : :
1037 : 7649 : if (es && es->predicate)
1038 : : {
1039 : 4627 : fprintf (f, " predicate: ");
1040 : 4627 : es->predicate->dump (f, info->conds);
1041 : : }
1042 : : else
1043 : 3022 : fprintf (f, "\n");
1044 : 7649 : if (es && es->param.exists ())
1045 : 4782 : for (i = 0; i < (int) es->param.length (); i++)
1046 : : {
1047 : 1406 : int prob = es->param[i].change_prob;
1048 : :
1049 : 1406 : if (!prob)
1050 : 612 : fprintf (f, "%*s op%i is compile time invariant\n",
1051 : : indent + 2, "", i);
1052 : 794 : else if (prob != REG_BR_PROB_BASE)
1053 : 35 : fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1054 : 35 : prob * 100.0 / REG_BR_PROB_BASE);
1055 : 1406 : if (es->param[i].points_to_local_or_readonly_memory)
1056 : 400 : fprintf (f, "%*s op%i points to local or readonly memory\n",
1057 : : indent + 2, "", i);
1058 : 1406 : if (es->param[i].points_to_possible_sra_candidate)
1059 : 227 : fprintf (f, "%*s op%i points to possible sra candidate\n",
1060 : : indent + 2, "", i);
1061 : : }
1062 : 7649 : if (!edge->inline_failed)
1063 : : {
1064 : 499 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1065 : 499 : fprintf (f, "%*sStack frame offset %i, callee self size %i\n",
1066 : : indent + 2, "",
1067 : 499 : (int) ipa_get_stack_frame_offset (callee),
1068 : 499 : (int) ss->estimated_self_stack_size);
1069 : 499 : dump_ipa_call_summary (f, indent + 2, callee, info);
1070 : : }
1071 : : }
1072 : 2191 : for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1073 : : {
1074 : 292 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1075 : 292 : fprintf (f, "%*sindirect call loop depth:%2i freq:%4.2f size:%2i"
1076 : : " time: %2i",
1077 : : indent, "",
1078 : : es->loop_depth,
1079 : 292 : edge->sreal_frequency ().to_double (), es->call_stmt_size,
1080 : : es->call_stmt_time);
1081 : 292 : if (es->predicate)
1082 : : {
1083 : 6 : fprintf (f, "predicate: ");
1084 : 6 : es->predicate->dump (f, info->conds);
1085 : : }
1086 : : else
1087 : 286 : fprintf (f, "\n");
1088 : : }
1089 : 1899 : }
1090 : :
1091 : :
1092 : : void
1093 : 1606 : ipa_dump_fn_summary (FILE *f, struct cgraph_node *node)
1094 : : {
1095 : 1606 : if (node->definition)
1096 : : {
1097 : 1606 : class ipa_fn_summary *s = ipa_fn_summaries->get (node);
1098 : 1606 : class ipa_size_summary *ss = ipa_size_summaries->get (node);
1099 : 1606 : if (s != NULL)
1100 : : {
1101 : 1400 : size_time_entry *e;
1102 : 1400 : int i;
1103 : 1400 : fprintf (f, "IPA function summary for %s", node->dump_name ());
1104 : 1400 : if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1105 : 0 : fprintf (f, " always_inline");
1106 : 1400 : if (s->inlinable)
1107 : 1231 : fprintf (f, " inlinable");
1108 : 1400 : if (s->fp_expressions)
1109 : 44 : fprintf (f, " fp_expression");
1110 : 1400 : if (s->builtin_constant_p_parms.length ())
1111 : : {
1112 : 2 : fprintf (f, " builtin_constant_p_parms");
1113 : 4 : for (unsigned int i = 0;
1114 : 4 : i < s->builtin_constant_p_parms.length (); i++)
1115 : 2 : fprintf (f, " %i", s->builtin_constant_p_parms[i]);
1116 : : }
1117 : 1400 : fprintf (f, "\n global time: %f\n", s->time.to_double ());
1118 : 1400 : fprintf (f, " self size: %i\n", ss->self_size);
1119 : 1400 : fprintf (f, " global size: %i\n", ss->size);
1120 : 1400 : fprintf (f, " min size: %i\n", s->min_size);
1121 : 1400 : fprintf (f, " self stack: %i\n",
1122 : 1400 : (int) ss->estimated_self_stack_size);
1123 : 1400 : fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1124 : 1400 : if (s->growth)
1125 : 84 : fprintf (f, " estimated growth:%i\n", (int) s->growth);
1126 : 1400 : if (s->scc_no)
1127 : 5 : fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1128 : 5881 : for (i = 0; s->size_time_table.iterate (i, &e); i++)
1129 : : {
1130 : 4481 : fprintf (f, " size:%f, time:%f",
1131 : 4481 : (double) e->size / ipa_fn_summary::size_scale,
1132 : : e->time.to_double ());
1133 : 4481 : if (e->exec_predicate != true)
1134 : : {
1135 : 2641 : fprintf (f, ", executed if:");
1136 : 2641 : e->exec_predicate.dump (f, s->conds, 0);
1137 : : }
1138 : 4481 : if (e->exec_predicate != e->nonconst_predicate)
1139 : : {
1140 : 1154 : fprintf (f, ", nonconst if:");
1141 : 1154 : e->nonconst_predicate.dump (f, s->conds, 0);
1142 : : }
1143 : 4481 : fprintf (f, "\n");
1144 : : }
1145 : : ipa_freqcounting_predicate *fcp;
1146 : : bool first_fcp = true;
1147 : 1506 : for (int i = 0; vec_safe_iterate (s->loop_iterations, i, &fcp); i++)
1148 : : {
1149 : 106 : if (first_fcp)
1150 : : {
1151 : 78 : fprintf (f, " loop iterations:");
1152 : 78 : first_fcp = false;
1153 : : }
1154 : 106 : fprintf (f, " %3.2f for ", fcp->freq.to_double ());
1155 : 106 : fcp->predicate->dump (f, s->conds);
1156 : : }
1157 : : first_fcp = true;
1158 : 1418 : for (int i = 0; vec_safe_iterate (s->loop_strides, i, &fcp); i++)
1159 : : {
1160 : 18 : if (first_fcp)
1161 : : {
1162 : 10 : fprintf (f, " loop strides:");
1163 : 10 : first_fcp = false;
1164 : : }
1165 : 18 : fprintf (f, " %3.2f for :", fcp->freq.to_double ());
1166 : 18 : fcp->predicate->dump (f, s->conds);
1167 : : }
1168 : 1400 : fprintf (f, " calls:\n");
1169 : 1400 : dump_ipa_call_summary (f, 4, node, s);
1170 : 1400 : fprintf (f, "\n");
1171 : 1400 : if (s->target_info)
1172 : 0 : fprintf (f, " target_info: %x\n", s->target_info);
1173 : : }
1174 : : else
1175 : 206 : fprintf (f, "IPA summary for %s is missing.\n", node->dump_name ());
1176 : : }
1177 : 1606 : }
1178 : :
1179 : : DEBUG_FUNCTION void
1180 : 0 : ipa_debug_fn_summary (struct cgraph_node *node)
1181 : : {
1182 : 0 : ipa_dump_fn_summary (stderr, node);
1183 : 0 : }
1184 : :
1185 : : void
1186 : 356 : ipa_dump_fn_summaries (FILE *f)
1187 : : {
1188 : 356 : struct cgraph_node *node;
1189 : :
1190 : 2234 : FOR_EACH_DEFINED_FUNCTION (node)
1191 : 1878 : if (!node->inlined_to)
1192 : 1379 : ipa_dump_fn_summary (f, node);
1193 : 356 : }
1194 : :
1195 : : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1196 : : boolean variable pointed to by DATA. */
1197 : :
1198 : : static bool
1199 : 758966 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1200 : : void *data)
1201 : : {
1202 : 758966 : bool *b = (bool *) data;
1203 : 758966 : *b = true;
1204 : 758966 : return true;
1205 : : }
1206 : :
1207 : : /* If OP refers to value of function parameter, return the corresponding
1208 : : parameter. If non-NULL, the size of the memory load (or the SSA_NAME of the
1209 : : PARM_DECL) will be stored to *SIZE_P in that case too. */
1210 : :
1211 : : static tree
1212 : 176599171 : unmodified_parm_1 (ipa_func_body_info *fbi, gimple *stmt, tree op,
1213 : : poly_int64 *size_p)
1214 : : {
1215 : : /* SSA_NAME referring to parm default def? */
1216 : 176599171 : if (TREE_CODE (op) == SSA_NAME
1217 : 103235513 : && SSA_NAME_IS_DEFAULT_DEF (op)
1218 : 200584929 : && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1219 : : {
1220 : 23805765 : if (size_p)
1221 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1222 : 23805765 : return SSA_NAME_VAR (op);
1223 : : }
1224 : : /* Non-SSA parm reference? */
1225 : 152793406 : if (TREE_CODE (op) == PARM_DECL
1226 : 1341287 : && fbi->aa_walk_budget > 0)
1227 : : {
1228 : 1338799 : bool modified = false;
1229 : :
1230 : 1338799 : ao_ref refd;
1231 : 1338799 : ao_ref_init (&refd, op);
1232 : 2677598 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
1233 : : mark_modified, &modified, NULL, NULL,
1234 : : fbi->aa_walk_budget);
1235 : 1338799 : if (walked < 0)
1236 : : {
1237 : 6 : fbi->aa_walk_budget = 0;
1238 : 863620 : return NULL_TREE;
1239 : : }
1240 : 1338793 : fbi->aa_walk_budget -= walked;
1241 : 1338793 : if (!modified)
1242 : : {
1243 : 863614 : if (size_p)
1244 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1245 : 863614 : return op;
1246 : : }
1247 : : }
1248 : : return NULL_TREE;
1249 : : }
1250 : :
1251 : : /* If OP refers to value of function parameter, return the corresponding
1252 : : parameter. Also traverse chains of SSA register assignments. If non-NULL,
1253 : : the size of the memory load (or the SSA_NAME of the PARM_DECL) will be
1254 : : stored to *SIZE_P in that case too. */
1255 : :
1256 : : static tree
1257 : 115024195 : unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op,
1258 : : poly_int64 *size_p)
1259 : : {
1260 : 143482176 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1261 : 143482176 : if (res)
1262 : : return res;
1263 : :
1264 : 119553991 : if (TREE_CODE (op) == SSA_NAME
1265 : 67974310 : && !SSA_NAME_IS_DEFAULT_DEF (op)
1266 : 187352887 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1267 : 28457981 : return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op),
1268 : 28457981 : gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)),
1269 : 28457981 : size_p);
1270 : : return NULL_TREE;
1271 : : }
1272 : :
1273 : : /* If OP refers to a value of a function parameter or value loaded from an
1274 : : aggregate passed to a parameter (either by value or reference), return TRUE
1275 : : and store the number of the parameter to *INDEX_P, the access size into
1276 : : *SIZE_P, and information whether and how it has been loaded from an
1277 : : aggregate into *AGGPOS. INFO describes the function parameters, STMT is the
1278 : : statement in which OP is used or loaded. */
1279 : :
1280 : : static bool
1281 : 31662380 : unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi,
1282 : : gimple *stmt, tree op, int *index_p,
1283 : : poly_int64 *size_p,
1284 : : struct agg_position_info *aggpos)
1285 : : {
1286 : 33116995 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1287 : :
1288 : 33116995 : gcc_checking_assert (aggpos);
1289 : 33116995 : if (res)
1290 : : {
1291 : 741194 : *index_p = ipa_get_param_decl_index (fbi->info, res);
1292 : 741194 : if (*index_p < 0)
1293 : : return false;
1294 : 741065 : aggpos->agg_contents = false;
1295 : 741065 : aggpos->by_ref = false;
1296 : 741065 : return true;
1297 : : }
1298 : :
1299 : 32375801 : if (TREE_CODE (op) == SSA_NAME)
1300 : : {
1301 : 11455438 : if (SSA_NAME_IS_DEFAULT_DEF (op)
1302 : 11455438 : || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1303 : : return false;
1304 : 4661186 : stmt = SSA_NAME_DEF_STMT (op);
1305 : 4661186 : op = gimple_assign_rhs1 (stmt);
1306 : 4661186 : if (!REFERENCE_CLASS_P (op))
1307 : : return unmodified_parm_or_parm_agg_item (fbi, stmt, op, index_p, size_p,
1308 : : aggpos);
1309 : : }
1310 : :
1311 : 24126934 : aggpos->agg_contents = true;
1312 : 24126934 : return ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
1313 : : stmt, op, index_p, &aggpos->offset,
1314 : 24126934 : size_p, &aggpos->by_ref);
1315 : : }
1316 : :
1317 : : /* If stmt is simple load or store of value pointed to by a function parmaeter,
1318 : : return its index. */
1319 : :
1320 : : static int
1321 : 106917108 : load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt)
1322 : : {
1323 : 106917108 : if (!optimize)
1324 : : return -1;
1325 : 153401930 : gassign *assign = dyn_cast <gassign *> (stmt);
1326 : 53272817 : if (!assign)
1327 : : return -1;
1328 : 53272817 : tree param;
1329 : 53272817 : if (gimple_assign_load_p (stmt))
1330 : 19723810 : param = gimple_assign_rhs1 (stmt);
1331 : 33549007 : else if (gimple_store_p (stmt))
1332 : 17763821 : param = gimple_assign_lhs (stmt);
1333 : : else
1334 : : return -1;
1335 : 37487631 : tree base = get_base_address (param);
1336 : 37487631 : if (TREE_CODE (base) != MEM_REF
1337 : 12756745 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1338 : 50240354 : || !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1339 : : return -1;
1340 : 6865851 : tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1341 : 6865851 : if (TREE_CODE (p) != PARM_DECL)
1342 : : return -1;
1343 : 6787995 : return ipa_get_param_decl_index (fbi->info, p);
1344 : : }
1345 : :
1346 : : /* See if statement might disappear after inlining.
1347 : : 0 - means not eliminated
1348 : : 1 - half of statements goes away
1349 : : 2 - for sure it is eliminated.
1350 : : We are not terribly sophisticated, basically looking for simple abstraction
1351 : : penalty wrappers. */
1352 : :
1353 : : static int
1354 : 106917108 : eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt)
1355 : : {
1356 : 106917108 : enum gimple_code code = gimple_code (stmt);
1357 : 106917108 : enum tree_code rhs_code;
1358 : :
1359 : 106917108 : if (!optimize)
1360 : : return 0;
1361 : :
1362 : 89114676 : switch (code)
1363 : : {
1364 : : case GIMPLE_RETURN:
1365 : : return 2;
1366 : 53272817 : case GIMPLE_ASSIGN:
1367 : 53272817 : if (gimple_num_ops (stmt) != 2)
1368 : : return 0;
1369 : :
1370 : 38300408 : rhs_code = gimple_assign_rhs_code (stmt);
1371 : :
1372 : : /* Casts of parameters, loads from parameters passed by reference
1373 : : and stores to return value or parameters are often free after
1374 : : inlining due to SRA and further combining.
1375 : : Assume that half of statements goes away. */
1376 : 38300408 : if (CONVERT_EXPR_CODE_P (rhs_code)
1377 : : || rhs_code == VIEW_CONVERT_EXPR
1378 : : || rhs_code == ADDR_EXPR
1379 : 35677810 : || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1380 : : {
1381 : 37487631 : tree rhs = gimple_assign_rhs1 (stmt);
1382 : 37487631 : tree lhs = gimple_assign_lhs (stmt);
1383 : 37487631 : tree inner_rhs = get_base_address (rhs);
1384 : 37487631 : tree inner_lhs = get_base_address (lhs);
1385 : 37487631 : bool rhs_free = false;
1386 : 37487631 : bool lhs_free = false;
1387 : :
1388 : 37487631 : if (!inner_rhs)
1389 : 0 : inner_rhs = rhs;
1390 : 37487631 : if (!inner_lhs)
1391 : 0 : inner_lhs = lhs;
1392 : :
1393 : : /* Reads of parameter are expected to be free. */
1394 : 37487631 : if (unmodified_parm (fbi, stmt, inner_rhs, NULL))
1395 : : rhs_free = true;
1396 : : /* Match expressions of form &this->field. Those will most likely
1397 : : combine with something upstream after inlining. */
1398 : 36292661 : else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1399 : : {
1400 : 2327908 : tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1401 : 2327908 : if (TREE_CODE (op) == PARM_DECL)
1402 : : rhs_free = true;
1403 : 2295389 : else if (TREE_CODE (op) == MEM_REF
1404 : 2295389 : && unmodified_parm (fbi, stmt, TREE_OPERAND (op, 0),
1405 : : NULL))
1406 : : rhs_free = true;
1407 : : }
1408 : :
1409 : : /* When parameter is not SSA register because its address is taken
1410 : : and it is just copied into one, the statement will be completely
1411 : : free after inlining (we will copy propagate backward). */
1412 : 1227489 : if (rhs_free && is_gimple_reg (lhs))
1413 : : return 2;
1414 : :
1415 : : /* Reads of parameters passed by reference
1416 : : expected to be free (i.e. optimized out after inlining). */
1417 : 36844744 : if (TREE_CODE (inner_rhs) == MEM_REF
1418 : 36844744 : && unmodified_parm (fbi, stmt, TREE_OPERAND (inner_rhs, 0), NULL))
1419 : : rhs_free = true;
1420 : :
1421 : : /* Copying parameter passed by reference into gimple register is
1422 : : probably also going to copy propagate, but we can't be quite
1423 : : sure. */
1424 : 36844744 : if (rhs_free && is_gimple_reg (lhs))
1425 : : lhs_free = true;
1426 : :
1427 : : /* Writes to parameters, parameters passed by value and return value
1428 : : (either directly or passed via invisible reference) are free.
1429 : :
1430 : : TODO: We ought to handle testcase like
1431 : : struct a {int a,b;};
1432 : : struct a
1433 : : returnstruct (void)
1434 : : {
1435 : : struct a a ={1,2};
1436 : : return a;
1437 : : }
1438 : :
1439 : : This translate into:
1440 : :
1441 : : returnstruct ()
1442 : : {
1443 : : int a$b;
1444 : : int a$a;
1445 : : struct a a;
1446 : : struct a D.2739;
1447 : :
1448 : : <bb 2>:
1449 : : D.2739.a = 1;
1450 : : D.2739.b = 2;
1451 : : return D.2739;
1452 : :
1453 : : }
1454 : : For that we either need to copy ipa-split logic detecting writes
1455 : : to return value. */
1456 : 36844744 : if (TREE_CODE (inner_lhs) == PARM_DECL
1457 : 36760270 : || TREE_CODE (inner_lhs) == RESULT_DECL
1458 : 72749240 : || (TREE_CODE (inner_lhs) == MEM_REF
1459 : 4708882 : && (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0),
1460 : : NULL)
1461 : 2406762 : || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1462 : 2405614 : && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1463 : 344217 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1464 : : (inner_lhs,
1465 : : 0))) == RESULT_DECL))))
1466 : : lhs_free = true;
1467 : 33528813 : if (lhs_free
1468 : 36844744 : && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1469 : : rhs_free = true;
1470 : 36844744 : if (lhs_free && rhs_free)
1471 : : return 1;
1472 : : }
1473 : : return 0;
1474 : : default:
1475 : : return 0;
1476 : : }
1477 : : }
1478 : :
1479 : : /* Analyze EXPR if it represents a series of simple operations performed on
1480 : : a function parameter and return true if so. FBI, STMT, EXPR, INDEX_P and
1481 : : AGGPOS have the same meaning like in unmodified_parm_or_parm_agg_item.
1482 : : Type of the parameter or load from an aggregate via the parameter is
1483 : : stored in *TYPE_P. Operations on the parameter are recorded to
1484 : : PARAM_OPS_P if it is not NULL. */
1485 : :
1486 : : static bool
1487 : 26057286 : decompose_param_expr (struct ipa_func_body_info *fbi,
1488 : : gimple *stmt, tree expr,
1489 : : int *index_p, tree *type_p,
1490 : : struct agg_position_info *aggpos,
1491 : : expr_eval_ops *param_ops_p = NULL)
1492 : : {
1493 : 26057286 : int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops);
1494 : 26057286 : int op_count = 0;
1495 : :
1496 : 26057286 : if (param_ops_p)
1497 : 8803585 : *param_ops_p = NULL;
1498 : :
1499 : 31662380 : while (true)
1500 : : {
1501 : 31662380 : expr_eval_op eval_op;
1502 : 31662380 : unsigned rhs_count;
1503 : 31662380 : unsigned cst_count = 0;
1504 : :
1505 : 31662380 : if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL,
1506 : : aggpos))
1507 : : {
1508 : 4283474 : tree type = TREE_TYPE (expr);
1509 : :
1510 : 4283474 : if (aggpos->agg_contents)
1511 : : {
1512 : : /* Stop if containing bit-field. */
1513 : 3542409 : if (TREE_CODE (expr) == BIT_FIELD_REF
1514 : 3542409 : || contains_bitfld_component_ref_p (expr))
1515 : : break;
1516 : : }
1517 : :
1518 : 4247714 : *type_p = type;
1519 : 4247714 : return true;
1520 : : }
1521 : :
1522 : 27378906 : if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr))
1523 : : break;
1524 : 10145426 : stmt = SSA_NAME_DEF_STMT (expr);
1525 : :
1526 : 10145426 : if (gcall *call = dyn_cast <gcall *> (stmt))
1527 : : {
1528 : 2374753 : int flags = gimple_call_return_flags (call);
1529 : 2374753 : if (!(flags & ERF_RETURNS_ARG))
1530 : 2925140 : goto fail;
1531 : 188267 : int arg = flags & ERF_RETURN_ARG_MASK;
1532 : 188267 : if (arg >= (int)gimple_call_num_args (call))
1533 : 0 : goto fail;
1534 : 188267 : expr = gimple_call_arg (stmt, arg);
1535 : 3886826 : continue;
1536 : 188267 : }
1537 : :
1538 : 7770673 : if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr)))
1539 : : break;
1540 : :
1541 : 6155586 : switch (gimple_assign_rhs_class (stmt))
1542 : : {
1543 : 3698559 : case GIMPLE_SINGLE_RHS:
1544 : 3698559 : expr = gimple_assign_rhs1 (stmt);
1545 : 3698559 : continue;
1546 : :
1547 : : case GIMPLE_UNARY_RHS:
1548 : : rhs_count = 1;
1549 : : break;
1550 : :
1551 : 1472572 : case GIMPLE_BINARY_RHS:
1552 : 1472572 : rhs_count = 2;
1553 : 1472572 : break;
1554 : :
1555 : 642 : case GIMPLE_TERNARY_RHS:
1556 : 642 : rhs_count = 3;
1557 : 642 : break;
1558 : :
1559 : 0 : default:
1560 : 0 : goto fail;
1561 : : }
1562 : :
1563 : : /* Stop if expression is too complex. */
1564 : 2457027 : if (op_count++ == op_limit)
1565 : : break;
1566 : :
1567 : 2456922 : if (param_ops_p)
1568 : : {
1569 : 2455349 : eval_op.code = gimple_assign_rhs_code (stmt);
1570 : 2455349 : eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt));
1571 : 2455349 : eval_op.val[0] = NULL_TREE;
1572 : 2455349 : eval_op.val[1] = NULL_TREE;
1573 : : }
1574 : :
1575 : : expr = NULL_TREE;
1576 : 5648388 : for (unsigned i = 0; i < rhs_count; i++)
1577 : : {
1578 : 3930120 : tree op = gimple_op (stmt, i + 1);
1579 : :
1580 : 3930120 : gcc_assert (op && !TYPE_P (op));
1581 : 3930120 : if (is_gimple_ip_invariant (op))
1582 : : {
1583 : 736756 : if (++cst_count == rhs_count)
1584 : 1106 : goto fail;
1585 : :
1586 : 735650 : eval_op.val[cst_count - 1] = op;
1587 : : }
1588 : 3193364 : else if (!expr)
1589 : : {
1590 : : /* Found a non-constant operand, and record its index in rhs
1591 : : operands. */
1592 : 2455816 : eval_op.index = i;
1593 : 2455816 : expr = op;
1594 : : }
1595 : : else
1596 : : {
1597 : : /* Found more than one non-constant operands. */
1598 : 737548 : goto fail;
1599 : : }
1600 : : }
1601 : :
1602 : 1718268 : if (param_ops_p)
1603 : 1716989 : vec_safe_insert (*param_ops_p, 0, eval_op);
1604 : : }
1605 : :
1606 : : /* Failed to decompose, free resource and return. */
1607 : 21809572 : fail:
1608 : 21809572 : if (param_ops_p)
1609 : 8719962 : vec_free (*param_ops_p);
1610 : :
1611 : : return false;
1612 : : }
1613 : :
1614 : : /* Record to SUMMARY that PARM is used by builtin_constant_p. */
1615 : :
1616 : : static void
1617 : 10232 : add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm)
1618 : : {
1619 : 10232 : int ip;
1620 : :
1621 : : /* Avoid duplicates. */
1622 : 10244 : for (unsigned int i = 0;
1623 : 10244 : summary->builtin_constant_p_parms.iterate (i, &ip); i++)
1624 : 4957 : if (ip == parm)
1625 : 10232 : return;
1626 : 5287 : summary->builtin_constant_p_parms.safe_push (parm);
1627 : : }
1628 : :
1629 : : /* If BB ends by a conditional we can turn into predicates, attach corresponding
1630 : : predicates to the CFG edges. */
1631 : :
1632 : : static void
1633 : 33538031 : set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1634 : : class ipa_fn_summary *summary,
1635 : : class ipa_node_params *params_summary,
1636 : : basic_block bb)
1637 : : {
1638 : 33538031 : tree op, op2;
1639 : 33538031 : int index;
1640 : 33538031 : struct agg_position_info aggpos;
1641 : 33538031 : enum tree_code code, inverted_code;
1642 : 33538031 : edge e;
1643 : 33538031 : edge_iterator ei;
1644 : 33538031 : gimple *set_stmt;
1645 : 33538031 : tree param_type;
1646 : 33538031 : expr_eval_ops param_ops;
1647 : :
1648 : 67076062 : gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1649 : 11004265 : if (!last)
1650 : 33527799 : return;
1651 : 11004265 : if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1652 : : return;
1653 : 8724880 : op = gimple_cond_lhs (last);
1654 : :
1655 : 8724880 : if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1656 : : ¶m_ops))
1657 : : {
1658 : 1182281 : code = gimple_cond_code (last);
1659 : 1182281 : inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1660 : :
1661 : 3546843 : FOR_EACH_EDGE (e, ei, bb->succs)
1662 : : {
1663 : 4729124 : enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1664 : 2364562 : ? code : inverted_code);
1665 : : /* invert_tree_comparison will return ERROR_MARK on FP
1666 : : comparisons that are not EQ/NE instead of returning proper
1667 : : unordered one. Be sure it is not confused with NON_CONSTANT.
1668 : :
1669 : : And if the edge's target is the final block of diamond CFG graph
1670 : : of this conditional statement, we do not need to compute
1671 : : predicate for the edge because the final block's predicate must
1672 : : be at least as that of the first block of the statement. */
1673 : 2364562 : if (this_code != ERROR_MARK
1674 : 2364562 : && !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1675 : : {
1676 : 2005257 : ipa_predicate p
1677 : 2005257 : = add_condition (summary, params_summary, index,
1678 : : param_type, &aggpos,
1679 : : this_code, gimple_cond_rhs (last), param_ops);
1680 : 2005257 : e->aux = edge_predicate_pool.allocate ();
1681 : 2005257 : *(ipa_predicate *) e->aux = p;
1682 : : }
1683 : : }
1684 : 1182281 : vec_free (param_ops);
1685 : 1182281 : return;
1686 : : }
1687 : :
1688 : 7542599 : if (TREE_CODE (op) != SSA_NAME)
1689 : : return;
1690 : : /* Special case
1691 : : if (builtin_constant_p (op))
1692 : : constant_code
1693 : : else
1694 : : nonconstant_code.
1695 : : Here we can predicate nonconstant_code. We can't
1696 : : really handle constant_code since we have no predicate
1697 : : for this and also the constant code is not known to be
1698 : : optimized away when inliner doesn't see operand is constant.
1699 : : Other optimizers might think otherwise. */
1700 : 7541533 : if (gimple_cond_code (last) != NE_EXPR
1701 : 7541533 : || !integer_zerop (gimple_cond_rhs (last)))
1702 : 4288990 : return;
1703 : 3252543 : set_stmt = SSA_NAME_DEF_STMT (op);
1704 : 3252543 : if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1705 : 3252543 : || gimple_call_num_args (set_stmt) != 1)
1706 : : return;
1707 : 11418 : op2 = gimple_call_arg (set_stmt, 0);
1708 : 11418 : if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos))
1709 : : return;
1710 : 10232 : if (!aggpos.by_ref)
1711 : 10199 : add_builtin_constant_p_parm (summary, index);
1712 : 30696 : FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1713 : : {
1714 : 10232 : ipa_predicate p = add_condition (summary, params_summary, index,
1715 : : param_type, &aggpos,
1716 : : ipa_predicate::is_not_constant, NULL_TREE);
1717 : 10232 : e->aux = edge_predicate_pool.allocate ();
1718 : 10232 : *(ipa_predicate *) e->aux = p;
1719 : : }
1720 : : }
1721 : :
1722 : :
1723 : : /* If BB ends by a switch we can turn into predicates, attach corresponding
1724 : : predicates to the CFG edges. */
1725 : :
1726 : : static void
1727 : 33538031 : set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1728 : : class ipa_fn_summary *summary,
1729 : : class ipa_node_params *params_summary,
1730 : : basic_block bb)
1731 : : {
1732 : 33538031 : tree op;
1733 : 33538031 : int index;
1734 : 33538031 : struct agg_position_info aggpos;
1735 : 33538031 : edge e;
1736 : 33538031 : edge_iterator ei;
1737 : 33538031 : size_t n;
1738 : 33538031 : size_t case_idx;
1739 : 33538031 : tree param_type;
1740 : 33538031 : expr_eval_ops param_ops;
1741 : :
1742 : 67076062 : gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
1743 : 78705 : if (!last)
1744 : 33507234 : return;
1745 : 78705 : op = gimple_switch_index (last);
1746 : 78705 : if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1747 : : ¶m_ops))
1748 : : return;
1749 : :
1750 : 44688 : auto_vec<std::pair<tree, tree> > ranges;
1751 : 44688 : tree type = TREE_TYPE (op);
1752 : 44688 : int bound_limit = opt_for_fn (fbi->node->decl,
1753 : : param_ipa_max_switch_predicate_bounds);
1754 : 44688 : int bound_count = 0;
1755 : : // This can safely be an integer range, as switches can only hold
1756 : : // integers.
1757 : 44688 : int_range<2> vr;
1758 : :
1759 : 89376 : get_range_query (cfun)->range_of_expr (vr, op);
1760 : 44688 : if (vr.undefined_p ())
1761 : 0 : vr.set_varying (TREE_TYPE (op));
1762 : 44688 : tree vr_min, vr_max;
1763 : : // TODO: This entire function could use a rewrite to use the irange
1764 : : // API, instead of trying to recreate its intersection/union logic.
1765 : : // Any use of get_legacy_range() is a serious code smell.
1766 : 44688 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
1767 : 44688 : wide_int vr_wmin = wi::to_wide (vr_min);
1768 : 44688 : wide_int vr_wmax = wi::to_wide (vr_max);
1769 : :
1770 : 311255 : FOR_EACH_EDGE (e, ei, bb->succs)
1771 : : {
1772 : 266567 : e->aux = edge_predicate_pool.allocate ();
1773 : 266567 : *(ipa_predicate *) e->aux = false;
1774 : : }
1775 : :
1776 : 44688 : e = gimple_switch_edge (cfun, last, 0);
1777 : : /* Set BOUND_COUNT to maximum count to bypass computing predicate for
1778 : : default case if its target basic block is in convergence point of all
1779 : : switch cases, which can be determined by checking whether it
1780 : : post-dominates the switch statement. */
1781 : 44688 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1782 : 12266 : bound_count = INT_MAX;
1783 : :
1784 : 44688 : n = gimple_switch_num_labels (last);
1785 : 291238 : for (case_idx = 1; case_idx < n; ++case_idx)
1786 : : {
1787 : 246550 : tree cl = gimple_switch_label (last, case_idx);
1788 : 246550 : tree min = CASE_LOW (cl);
1789 : 246550 : tree max = CASE_HIGH (cl);
1790 : 246550 : ipa_predicate p;
1791 : :
1792 : 246550 : e = gimple_switch_edge (cfun, last, case_idx);
1793 : :
1794 : : /* The case value might not have same type as switch expression,
1795 : : extend the value based on the expression type. */
1796 : 246550 : if (TREE_TYPE (min) != type)
1797 : 45437 : min = wide_int_to_tree (type, wi::to_wide (min));
1798 : :
1799 : 246550 : if (!max)
1800 : : max = min;
1801 : 17318 : else if (TREE_TYPE (max) != type)
1802 : 3280 : max = wide_int_to_tree (type, wi::to_wide (max));
1803 : :
1804 : : /* The case's target basic block is in convergence point of all switch
1805 : : cases, its predicate should be at least as that of the switch
1806 : : statement. */
1807 : 246550 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1808 : 2696 : p = true;
1809 : 243854 : else if (min == max)
1810 : 227305 : p = add_condition (summary, params_summary, index, param_type,
1811 : : &aggpos, EQ_EXPR, min, param_ops);
1812 : : else
1813 : : {
1814 : 16549 : ipa_predicate p1, p2;
1815 : 16549 : p1 = add_condition (summary, params_summary, index, param_type,
1816 : : &aggpos, GE_EXPR, min, param_ops);
1817 : 16549 : p2 = add_condition (summary, params_summary,index, param_type,
1818 : : &aggpos, LE_EXPR, max, param_ops);
1819 : 16549 : p = p1 & p2;
1820 : : }
1821 : 246550 : *(ipa_predicate *) e->aux
1822 : 246550 : = p.or_with (summary->conds, *(ipa_predicate *) e->aux);
1823 : :
1824 : : /* If there are too many disjoint case ranges, predicate for default
1825 : : case might become too complicated. So add a limit here. */
1826 : 246550 : if (bound_count > bound_limit)
1827 : 93742 : continue;
1828 : :
1829 : 152808 : bool new_range = true;
1830 : :
1831 : 152808 : if (!ranges.is_empty ())
1832 : : {
1833 : 120386 : wide_int curr_wmin = wi::to_wide (min);
1834 : 120386 : wide_int last_wmax = wi::to_wide (ranges.last ().second);
1835 : :
1836 : : /* Merge case ranges if they are continuous. */
1837 : 120386 : if (curr_wmin == last_wmax + 1)
1838 : : new_range = false;
1839 : 38891 : else if (vr_type == VR_ANTI_RANGE)
1840 : : {
1841 : : /* If two disjoint case ranges can be connected by anti-range
1842 : : of switch index, combine them to one range. */
1843 : 79 : if (wi::lt_p (vr_wmax, curr_wmin - 1, TYPE_SIGN (type)))
1844 : : vr_type = VR_UNDEFINED;
1845 : 0 : else if (wi::le_p (vr_wmin, last_wmax + 1, TYPE_SIGN (type)))
1846 : 0 : new_range = false;
1847 : : }
1848 : 120386 : }
1849 : :
1850 : : /* Create/extend a case range. And we count endpoints of range set,
1851 : : this number nearly equals to number of conditions that we will create
1852 : : for predicate of default case. */
1853 : 120386 : if (new_range)
1854 : : {
1855 : 71313 : bound_count += (min == max) ? 1 : 2;
1856 : 71313 : ranges.safe_push (std::make_pair (min, max));
1857 : : }
1858 : : else
1859 : : {
1860 : 81495 : bound_count += (ranges.last ().first == ranges.last ().second);
1861 : 81495 : ranges.last ().second = max;
1862 : : }
1863 : : }
1864 : :
1865 : 44688 : e = gimple_switch_edge (cfun, last, 0);
1866 : 44688 : if (bound_count > bound_limit)
1867 : : {
1868 : 13891 : *(ipa_predicate *) e->aux = true;
1869 : 13891 : vec_free (param_ops);
1870 : 13891 : return;
1871 : : }
1872 : :
1873 : 30797 : ipa_predicate p_seg = true;
1874 : 30797 : ipa_predicate p_all = false;
1875 : :
1876 : 30797 : if (vr_type != VR_RANGE)
1877 : : {
1878 : 30336 : vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type));
1879 : 30336 : vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type));
1880 : : }
1881 : :
1882 : : /* Construct predicate to represent default range set that is negation of
1883 : : all case ranges. Case range is classified as containing single/non-single
1884 : : values. Suppose a piece of case ranges in the following.
1885 : :
1886 : : [D1...D2] [S1] ... [Sn] [D3...D4]
1887 : :
1888 : : To represent default case's range sets between two non-single value
1889 : : case ranges (From D2 to D3), we construct predicate as:
1890 : :
1891 : : D2 < x < D3 && x != S1 && ... && x != Sn
1892 : : */
1893 : 93946 : for (size_t i = 0; i < ranges.length (); i++)
1894 : : {
1895 : 63244 : tree min = ranges[i].first;
1896 : 63244 : tree max = ranges[i].second;
1897 : :
1898 : 63244 : if (min == max)
1899 : 80088 : p_seg &= add_condition (summary, params_summary, index,
1900 : : param_type, &aggpos, NE_EXPR,
1901 : 40044 : min, param_ops);
1902 : : else
1903 : : {
1904 : : /* Do not create sub-predicate for range that is beyond low bound
1905 : : of switch index. */
1906 : 23200 : if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type)))
1907 : : {
1908 : 14407 : p_seg &= add_condition (summary, params_summary, index,
1909 : : param_type, &aggpos,
1910 : 14407 : LT_EXPR, min, param_ops);
1911 : 14407 : p_all = p_all.or_with (summary->conds, p_seg);
1912 : : }
1913 : :
1914 : : /* Do not create sub-predicate for range that is beyond up bound
1915 : : of switch index. */
1916 : 23200 : if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type)))
1917 : : {
1918 : 95 : p_seg = false;
1919 : 95 : break;
1920 : : }
1921 : :
1922 : 23105 : p_seg = add_condition (summary, params_summary, index,
1923 : : param_type, &aggpos, GT_EXPR,
1924 : : max, param_ops);
1925 : : }
1926 : : }
1927 : :
1928 : 30797 : p_all = p_all.or_with (summary->conds, p_seg);
1929 : 30797 : *(ipa_predicate *) e->aux
1930 : 30797 : = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux);
1931 : :
1932 : 38342 : vec_free (param_ops);
1933 : 44688 : }
1934 : :
1935 : :
1936 : : /* For each BB in NODE attach to its AUX pointer predicate under
1937 : : which it is executable. */
1938 : :
1939 : : static void
1940 : 5982588 : compute_bb_predicates (struct ipa_func_body_info *fbi,
1941 : : struct cgraph_node *node,
1942 : : class ipa_fn_summary *summary,
1943 : : class ipa_node_params *params_summary)
1944 : : {
1945 : 5982588 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1946 : 5982588 : bool done = false;
1947 : 5982588 : basic_block bb;
1948 : :
1949 : 39520619 : FOR_EACH_BB_FN (bb, my_function)
1950 : : {
1951 : 33538031 : set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb);
1952 : 33538031 : set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb);
1953 : : }
1954 : :
1955 : : /* Entry block is always executable. */
1956 : 5982588 : ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1957 : 5982588 : = edge_predicate_pool.allocate ();
1958 : 5982588 : *(ipa_predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux = true;
1959 : :
1960 : : /* A simple dataflow propagation of predicates forward in the CFG.
1961 : : TODO: work in reverse postorder. */
1962 : 18066159 : while (!done)
1963 : : {
1964 : 12083571 : done = true;
1965 : 84445542 : FOR_EACH_BB_FN (bb, my_function)
1966 : : {
1967 : 72361971 : ipa_predicate p = false;
1968 : 72361971 : edge e;
1969 : 72361971 : edge_iterator ei;
1970 : 89814505 : FOR_EACH_EDGE (e, ei, bb->preds)
1971 : : {
1972 : 77009856 : if (e->src->aux)
1973 : : {
1974 : 75203993 : ipa_predicate this_bb_predicate
1975 : : = *(ipa_predicate *) e->src->aux;
1976 : 75203993 : if (e->aux)
1977 : 4769780 : this_bb_predicate &= (*(ipa_predicate *) e->aux);
1978 : 75203993 : p = p.or_with (summary->conds, this_bb_predicate);
1979 : 75203993 : if (p == true)
1980 : : break;
1981 : : }
1982 : : }
1983 : 72361971 : if (p != false)
1984 : : {
1985 : 71210122 : basic_block pdom_bb;
1986 : :
1987 : 71210122 : if (!bb->aux)
1988 : : {
1989 : 26229393 : done = false;
1990 : 26229393 : bb->aux = edge_predicate_pool.allocate ();
1991 : 26229393 : *((ipa_predicate *) bb->aux) = p;
1992 : : }
1993 : 44980729 : else if (p != *(ipa_predicate *) bb->aux)
1994 : : {
1995 : : /* This OR operation is needed to ensure monotonous data flow
1996 : : in the case we hit the limit on number of clauses and the
1997 : : and/or operations above give approximate answers. */
1998 : 142466 : p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux);
1999 : 142466 : if (p != *(ipa_predicate *)bb->aux)
2000 : : {
2001 : 117085 : done = false;
2002 : 117085 : *((ipa_predicate *)bb->aux) = p;
2003 : : }
2004 : : }
2005 : :
2006 : : /* For switch/if statement, we can OR-combine predicates of all
2007 : : its cases/branches to get predicate for basic block in their
2008 : : convergence point, but sometimes this will generate very
2009 : : complicated predicate. Actually, we can get simplified
2010 : : predicate in another way by using the fact that predicate
2011 : : for a basic block must also hold true for its post dominators.
2012 : : To be specific, basic block in convergence point of
2013 : : conditional statement should include predicate of the
2014 : : statement. */
2015 : 71210122 : pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
2016 : 71210122 : if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb)
2017 : : ;
2018 : 36572387 : else if (!pdom_bb->aux)
2019 : : {
2020 : 7308624 : done = false;
2021 : 7308624 : pdom_bb->aux = edge_predicate_pool.allocate ();
2022 : 7308624 : *((ipa_predicate *)pdom_bb->aux) = p;
2023 : : }
2024 : 29263763 : else if (p != *(ipa_predicate *)pdom_bb->aux)
2025 : : {
2026 : 3161244 : p = p.or_with (summary->conds,
2027 : : *(ipa_predicate *)pdom_bb->aux);
2028 : 3161244 : if (p != *(ipa_predicate *)pdom_bb->aux)
2029 : : {
2030 : 160811 : done = false;
2031 : 160811 : *((ipa_predicate *)pdom_bb->aux) = p;
2032 : : }
2033 : : }
2034 : : }
2035 : : }
2036 : : }
2037 : 5982588 : }
2038 : :
2039 : :
2040 : : /* Return predicate specifying when the STMT might have result that is not
2041 : : a compile time constant. */
2042 : :
2043 : : static ipa_predicate
2044 : 4068474 : will_be_nonconstant_expr_predicate (ipa_func_body_info *fbi,
2045 : : class ipa_fn_summary *summary,
2046 : : class ipa_node_params *params_summary,
2047 : : tree expr,
2048 : : vec<ipa_predicate> nonconstant_names)
2049 : : {
2050 : 4068474 : tree parm;
2051 : 4068474 : int index;
2052 : :
2053 : 4305329 : while (UNARY_CLASS_P (expr))
2054 : 236855 : expr = TREE_OPERAND (expr, 0);
2055 : :
2056 : 4068474 : parm = unmodified_parm (fbi, NULL, expr, NULL);
2057 : 4068474 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2058 : 201233 : return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL,
2059 : 201233 : ipa_predicate::changed, NULL_TREE);
2060 : 3867241 : if (is_gimple_min_invariant (expr))
2061 : 60205 : return false;
2062 : 3807036 : if (TREE_CODE (expr) == SSA_NAME)
2063 : 3636182 : return nonconstant_names[SSA_NAME_VERSION (expr)];
2064 : 170854 : if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2065 : : {
2066 : 170245 : ipa_predicate p1
2067 : 170245 : = will_be_nonconstant_expr_predicate (fbi, summary,
2068 : : params_summary,
2069 : 170245 : TREE_OPERAND (expr, 0),
2070 : : nonconstant_names);
2071 : 170245 : if (p1 == true)
2072 : 97268 : return p1;
2073 : :
2074 : 72977 : ipa_predicate p2
2075 : 72977 : = will_be_nonconstant_expr_predicate (fbi, summary,
2076 : : params_summary,
2077 : 72977 : TREE_OPERAND (expr, 1),
2078 : : nonconstant_names);
2079 : 72977 : return p1.or_with (summary->conds, p2);
2080 : : }
2081 : 609 : else if (TREE_CODE (expr) == COND_EXPR)
2082 : : {
2083 : 232 : ipa_predicate p1
2084 : 232 : = will_be_nonconstant_expr_predicate (fbi, summary,
2085 : : params_summary,
2086 : 232 : TREE_OPERAND (expr, 0),
2087 : : nonconstant_names);
2088 : 232 : if (p1 == true)
2089 : 81 : return p1;
2090 : :
2091 : 151 : ipa_predicate p2
2092 : 151 : = will_be_nonconstant_expr_predicate (fbi, summary,
2093 : : params_summary,
2094 : 151 : TREE_OPERAND (expr, 1),
2095 : : nonconstant_names);
2096 : 151 : if (p2 == true)
2097 : 151 : return p2;
2098 : 0 : p1 = p1.or_with (summary->conds, p2);
2099 : 0 : p2 = will_be_nonconstant_expr_predicate (fbi, summary,
2100 : : params_summary,
2101 : 0 : TREE_OPERAND (expr, 2),
2102 : : nonconstant_names);
2103 : 0 : return p2.or_with (summary->conds, p1);
2104 : : }
2105 : 377 : else if (TREE_CODE (expr) == CALL_EXPR)
2106 : 377 : return true;
2107 : : else
2108 : : {
2109 : 0 : debug_tree (expr);
2110 : 0 : gcc_unreachable ();
2111 : : }
2112 : : }
2113 : :
2114 : :
2115 : : /* Return predicate specifying when the STMT might have result that is not
2116 : : a compile time constant. */
2117 : :
2118 : : static ipa_predicate
2119 : 109159059 : will_be_nonconstant_predicate (struct ipa_func_body_info *fbi,
2120 : : class ipa_fn_summary *summary,
2121 : : class ipa_node_params *params_summary,
2122 : : gimple *stmt,
2123 : : vec<ipa_predicate> nonconstant_names)
2124 : : {
2125 : 109159059 : ipa_predicate p = true;
2126 : 109159059 : ssa_op_iter iter;
2127 : 109159059 : tree use;
2128 : 109159059 : tree param_type = NULL_TREE;
2129 : 109159059 : ipa_predicate op_non_const;
2130 : 109159059 : bool is_load;
2131 : 109159059 : int base_index;
2132 : 109159059 : struct agg_position_info aggpos;
2133 : :
2134 : : /* What statements might be optimized away
2135 : : when their arguments are constant. */
2136 : 109159059 : if (gimple_code (stmt) != GIMPLE_ASSIGN
2137 : 36975281 : && gimple_code (stmt) != GIMPLE_COND
2138 : 26149402 : && gimple_code (stmt) != GIMPLE_SWITCH
2139 : 135229756 : && (gimple_code (stmt) != GIMPLE_CALL
2140 : 19537829 : || !(gimple_call_flags (stmt) & ECF_CONST)))
2141 : 24668429 : return p;
2142 : :
2143 : : /* Stores will stay anyway. */
2144 : 84490630 : if (gimple_store_p (stmt))
2145 : 24704169 : return p;
2146 : :
2147 : 59786461 : is_load = gimple_assign_load_p (stmt);
2148 : :
2149 : : /* Loads can be optimized when the value is known. */
2150 : 59786461 : if (is_load)
2151 : : {
2152 : 17242283 : tree op = gimple_assign_rhs1 (stmt);
2153 : 17242283 : if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type,
2154 : : &aggpos))
2155 : 14231770 : return p;
2156 : : }
2157 : : else
2158 : 42544178 : base_index = -1;
2159 : :
2160 : : /* See if we understand all operands before we start
2161 : : adding conditionals. */
2162 : 61444020 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2163 : : {
2164 : 45259878 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2165 : : /* For arguments we can build a condition. */
2166 : 45259878 : if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0)
2167 : 7969730 : continue;
2168 : 37290148 : if (TREE_CODE (use) != SSA_NAME)
2169 : 0 : return p;
2170 : : /* If we know when operand is constant,
2171 : : we still can say something useful. */
2172 : 37290148 : if (nonconstant_names[SSA_NAME_VERSION (use)] != true)
2173 : 7919599 : continue;
2174 : 29370549 : return p;
2175 : : }
2176 : :
2177 : 16184142 : if (is_load)
2178 : 3010439 : op_non_const =
2179 : 3010439 : add_condition (summary, params_summary,
2180 : : base_index, param_type, &aggpos,
2181 : : ipa_predicate::changed, NULL_TREE);
2182 : : else
2183 : 13173703 : op_non_const = false;
2184 : 31090892 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2185 : : {
2186 : 14906750 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2187 : 14906750 : int index;
2188 : :
2189 : 14906750 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2190 : : {
2191 : 7504342 : if (index != base_index)
2192 : 5156787 : p = add_condition (summary, params_summary, index,
2193 : 5156787 : TREE_TYPE (parm), NULL,
2194 : : ipa_predicate::changed, NULL_TREE);
2195 : : else
2196 : 2347555 : continue;
2197 : : }
2198 : : else
2199 : 7402408 : p = nonconstant_names[SSA_NAME_VERSION (use)];
2200 : 12559195 : op_non_const = p.or_with (summary->conds, op_non_const);
2201 : : }
2202 : 16184142 : if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2203 : 14199550 : && gimple_op (stmt, 0)
2204 : 30157831 : && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2205 : 13973689 : nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2206 : 13973689 : = op_non_const;
2207 : 16184142 : return op_non_const;
2208 : : }
2209 : :
2210 : : struct record_modified_bb_info
2211 : : {
2212 : : tree op;
2213 : : bitmap bb_set;
2214 : : gimple *stmt;
2215 : : };
2216 : :
2217 : : /* Value is initialized in INIT_BB and used in USE_BB. We want to compute
2218 : : probability how often it changes between USE_BB.
2219 : : INIT_BB->count/USE_BB->count is an estimate, but if INIT_BB
2220 : : is in different loop nest, we can do better.
2221 : : This is all just estimate. In theory we look for minimal cut separating
2222 : : INIT_BB and USE_BB, but we only want to anticipate loop invariant motion
2223 : : anyway. */
2224 : :
2225 : : static basic_block
2226 : 10103121 : get_minimal_bb (basic_block init_bb, basic_block use_bb)
2227 : : {
2228 : 10103121 : class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father);
2229 : 10103121 : if (l && l->header->count < init_bb->count)
2230 : 358093 : return l->header;
2231 : : return init_bb;
2232 : : }
2233 : :
2234 : : /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2235 : : set except for info->stmt. */
2236 : :
2237 : : static bool
2238 : 4801404 : record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2239 : : {
2240 : 4801404 : struct record_modified_bb_info *info =
2241 : : (struct record_modified_bb_info *) data;
2242 : 4801404 : if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2243 : : return false;
2244 : 4771644 : if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef)))
2245 : : return false;
2246 : 4686218 : bitmap_set_bit (info->bb_set,
2247 : 4686218 : SSA_NAME_IS_DEFAULT_DEF (vdef)
2248 : 0 : ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2249 : : : get_minimal_bb
2250 : 4686218 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2251 : 4686218 : gimple_bb (info->stmt))->index);
2252 : 4686218 : if (dump_file)
2253 : : {
2254 : 0 : fprintf (dump_file, " Param ");
2255 : 0 : print_generic_expr (dump_file, info->op, TDF_SLIM);
2256 : 0 : fprintf (dump_file, " changed at bb %i, minimal: %i stmt: ",
2257 : 0 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index,
2258 : : get_minimal_bb
2259 : 0 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2260 : 0 : gimple_bb (info->stmt))->index);
2261 : 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (vdef), 0);
2262 : : }
2263 : : return false;
2264 : : }
2265 : :
2266 : : /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2267 : : will change since last invocation of STMT.
2268 : :
2269 : : Value 0 is reserved for compile time invariants.
2270 : : For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2271 : : ought to be REG_BR_PROB_BASE / estimated_iters. */
2272 : :
2273 : : static int
2274 : 36425463 : param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i)
2275 : : {
2276 : 36425463 : tree op = gimple_call_arg (stmt, i);
2277 : 36425463 : basic_block bb = gimple_bb (stmt);
2278 : :
2279 : 36425463 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
2280 : 299 : op = TREE_OPERAND (op, 0);
2281 : :
2282 : 36425463 : tree base = get_base_address (op);
2283 : :
2284 : : /* Global invariants never change. */
2285 : 36425463 : if (is_gimple_min_invariant (base))
2286 : : return 0;
2287 : :
2288 : : /* We would have to do non-trivial analysis to really work out what
2289 : : is the probability of value to change (i.e. when init statement
2290 : : is in a sibling loop of the call).
2291 : :
2292 : : We do an conservative estimate: when call is executed N times more often
2293 : : than the statement defining value, we take the frequency 1/N. */
2294 : 18217089 : if (TREE_CODE (base) == SSA_NAME)
2295 : : {
2296 : 16011523 : profile_count init_count;
2297 : :
2298 : 24388056 : if (!bb->count.nonzero_p ())
2299 : : return REG_BR_PROB_BASE;
2300 : :
2301 : 8239841 : if (SSA_NAME_IS_DEFAULT_DEF (base))
2302 : 2822938 : init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2303 : : else
2304 : 5416903 : init_count = get_minimal_bb
2305 : 5416903 : (gimple_bb (SSA_NAME_DEF_STMT (base)),
2306 : : gimple_bb (stmt))->count;
2307 : :
2308 : 8239841 : if (init_count < bb->count)
2309 : 424367 : return MAX ((init_count.to_sreal_scale (bb->count)
2310 : : * REG_BR_PROB_BASE).to_int (), 1);
2311 : : return REG_BR_PROB_BASE;
2312 : : }
2313 : : else
2314 : : {
2315 : 2205566 : ao_ref refd;
2316 : 2205566 : profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2317 : 2205566 : struct record_modified_bb_info info;
2318 : 2205566 : tree init = ctor_for_folding (base);
2319 : :
2320 : 2205566 : if (init != error_mark_node)
2321 : : return 0;
2322 : 4886772 : if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0)
2323 : : return REG_BR_PROB_BASE;
2324 : 1353389 : if (dump_file)
2325 : : {
2326 : 0 : fprintf (dump_file, " Analyzing param change probability of ");
2327 : 0 : print_generic_expr (dump_file, op, TDF_SLIM);
2328 : 0 : fprintf (dump_file, "\n");
2329 : : }
2330 : 1353389 : ao_ref_init (&refd, op);
2331 : 1353389 : info.op = op;
2332 : 1353389 : info.stmt = stmt;
2333 : 1353389 : info.bb_set = BITMAP_ALLOC (NULL);
2334 : 1353389 : int walked
2335 : 2706778 : = walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2336 : : NULL, NULL, fbi->aa_walk_budget);
2337 : 1353389 : if (walked > 0)
2338 : 1233606 : fbi->aa_walk_budget -= walked;
2339 : 1353389 : if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index))
2340 : : {
2341 : 839461 : if (walked < 0)
2342 : 204 : fbi->aa_walk_budget = 0;
2343 : 839461 : if (dump_file)
2344 : : {
2345 : 0 : if (walked < 0)
2346 : 0 : fprintf (dump_file, " Ran out of AA walking budget.\n");
2347 : : else
2348 : 0 : fprintf (dump_file, " Set in same BB as used.\n");
2349 : : }
2350 : 839461 : BITMAP_FREE (info.bb_set);
2351 : 839461 : return REG_BR_PROB_BASE;
2352 : : }
2353 : :
2354 : 513928 : bitmap_iterator bi;
2355 : 513928 : unsigned index;
2356 : : /* Lookup the most frequent update of the value and believe that
2357 : : it dominates all the other; precise analysis here is difficult. */
2358 : 1556036 : EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2359 : 1042108 : max = max.max (BASIC_BLOCK_FOR_FN (cfun, index)->count);
2360 : 513928 : if (dump_file)
2361 : : {
2362 : 0 : fprintf (dump_file, " Set with count ");
2363 : 0 : max.dump (dump_file);
2364 : 0 : fprintf (dump_file, " and used with count ");
2365 : 0 : bb->count.dump (dump_file);
2366 : 0 : fprintf (dump_file, " freq %f\n",
2367 : 0 : max.to_sreal_scale (bb->count).to_double ());
2368 : : }
2369 : :
2370 : 513928 : BITMAP_FREE (info.bb_set);
2371 : 513928 : if (max < bb->count)
2372 : 86246 : return MAX ((max.to_sreal_scale (bb->count)
2373 : : * REG_BR_PROB_BASE).to_int (), 1);
2374 : : return REG_BR_PROB_BASE;
2375 : : }
2376 : : }
2377 : :
2378 : : /* Find whether a basic block BB is the final block of a (half) diamond CFG
2379 : : sub-graph and if the predicate the condition depends on is known. If so,
2380 : : return true and store the pointer the predicate in *P. */
2381 : :
2382 : : static bool
2383 : 6193225 : phi_result_unknown_predicate (ipa_func_body_info *fbi,
2384 : : ipa_fn_summary *summary,
2385 : : class ipa_node_params *params_summary,
2386 : : basic_block bb,
2387 : : ipa_predicate *p,
2388 : : vec<ipa_predicate> nonconstant_names)
2389 : : {
2390 : 6193225 : edge e;
2391 : 6193225 : edge_iterator ei;
2392 : 6193225 : basic_block first_bb = NULL;
2393 : :
2394 : 6193225 : if (single_pred_p (bb))
2395 : : {
2396 : 20907 : *p = false;
2397 : 20907 : return true;
2398 : : }
2399 : :
2400 : 14134869 : FOR_EACH_EDGE (e, ei, bb->preds)
2401 : : {
2402 : 12079174 : if (single_succ_p (e->src))
2403 : : {
2404 : 12623866 : if (!single_pred_p (e->src))
2405 : : return false;
2406 : 7014624 : if (!first_bb)
2407 : 3065121 : first_bb = single_pred (e->src);
2408 : 3949503 : else if (single_pred (e->src) != first_bb)
2409 : : return false;
2410 : : }
2411 : : else
2412 : : {
2413 : 4038037 : if (!first_bb)
2414 : : first_bb = e->src;
2415 : 1400523 : else if (e->src != first_bb)
2416 : : return false;
2417 : : }
2418 : : }
2419 : :
2420 : 2055695 : if (!first_bb)
2421 : : return false;
2422 : :
2423 : 4111390 : gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb));
2424 : 2010499 : if (!stmt
2425 : 2010499 : || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2426 : 302698 : return false;
2427 : :
2428 : 1752997 : *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary,
2429 : : gimple_cond_lhs (stmt),
2430 : : nonconstant_names);
2431 : 1752997 : if (*p == true)
2432 : : return false;
2433 : : else
2434 : : return true;
2435 : : }
2436 : :
2437 : : /* Given a PHI statement in a function described by inline properties SUMMARY
2438 : : and *P being the predicate describing whether the selected PHI argument is
2439 : : known, store a predicate for the result of the PHI statement into
2440 : : NONCONSTANT_NAMES, if possible. */
2441 : :
2442 : : static void
2443 : 655037 : predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi,
2444 : : ipa_predicate *p,
2445 : : vec<ipa_predicate> nonconstant_names)
2446 : : {
2447 : 655037 : unsigned i;
2448 : :
2449 : 933497 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2450 : : {
2451 : 817400 : tree arg = gimple_phi_arg (phi, i)->def;
2452 : 817400 : if (!is_gimple_min_invariant (arg))
2453 : : {
2454 : 701901 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
2455 : 1403802 : *p = p->or_with (summary->conds,
2456 : 701901 : nonconstant_names[SSA_NAME_VERSION (arg)]);
2457 : 701901 : if (*p == true)
2458 : : return;
2459 : : }
2460 : : }
2461 : :
2462 : 116097 : if (dump_file && (dump_flags & TDF_DETAILS))
2463 : : {
2464 : 3 : fprintf (dump_file, "\t\tphi predicate: ");
2465 : 3 : p->dump (dump_file, summary->conds);
2466 : : }
2467 : 116097 : nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2468 : : }
2469 : :
2470 : : /* For a typical usage of __builtin_expect (a<b, 1), we
2471 : : may introduce an extra relation stmt:
2472 : : With the builtin, we have
2473 : : t1 = a <= b;
2474 : : t2 = (long int) t1;
2475 : : t3 = __builtin_expect (t2, 1);
2476 : : if (t3 != 0)
2477 : : goto ...
2478 : : Without the builtin, we have
2479 : : if (a<=b)
2480 : : goto...
2481 : : This affects the size/time estimation and may have
2482 : : an impact on the earlier inlining.
2483 : : Here find this pattern and fix it up later. */
2484 : :
2485 : : static gimple *
2486 : 39475693 : find_foldable_builtin_expect (basic_block bb)
2487 : : {
2488 : 39475693 : gimple_stmt_iterator bsi;
2489 : :
2490 : 268890925 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2491 : : {
2492 : 190084729 : gimple *stmt = gsi_stmt (bsi);
2493 : 190084729 : if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2494 : 189894446 : || gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY)
2495 : 379979110 : || gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT))
2496 : : {
2497 : 308520 : tree var = gimple_call_lhs (stmt);
2498 : 308520 : tree arg = gimple_call_arg (stmt, 0);
2499 : 308520 : use_operand_p use_p;
2500 : 308520 : gimple *use_stmt;
2501 : 308520 : bool match = false;
2502 : 308520 : bool done = false;
2503 : :
2504 : 308520 : if (!var || !arg)
2505 : 3 : continue;
2506 : 308517 : gcc_assert (TREE_CODE (var) == SSA_NAME);
2507 : :
2508 : 615791 : while (TREE_CODE (arg) == SSA_NAME)
2509 : : {
2510 : 615791 : gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg);
2511 : 615791 : if (!is_gimple_assign (stmt_tmp))
2512 : : break;
2513 : 609234 : switch (gimple_assign_rhs_code (stmt_tmp))
2514 : : {
2515 : : case LT_EXPR:
2516 : : case LE_EXPR:
2517 : : case GT_EXPR:
2518 : : case GE_EXPR:
2519 : : case EQ_EXPR:
2520 : : case NE_EXPR:
2521 : : match = true;
2522 : : done = true;
2523 : : break;
2524 : : CASE_CONVERT:
2525 : : break;
2526 : : default:
2527 : : done = true;
2528 : : break;
2529 : : }
2530 : 307274 : if (done)
2531 : : break;
2532 : 307274 : arg = gimple_assign_rhs1 (stmt_tmp);
2533 : : }
2534 : :
2535 : 275116 : if (match && single_imm_use (var, &use_p, &use_stmt)
2536 : 583236 : && gimple_code (use_stmt) == GIMPLE_COND)
2537 : 145190 : return use_stmt;
2538 : : }
2539 : : }
2540 : : return NULL;
2541 : : }
2542 : :
2543 : : /* Return true when the basic blocks contains only clobbers followed by RESX.
2544 : : Such BBs are kept around to make removal of dead stores possible with
2545 : : presence of EH and will be optimized out by optimize_clobbers later in the
2546 : : game.
2547 : :
2548 : : NEED_EH is used to recurse in case the clobber has non-EH predecessors
2549 : : that can be clobber only, too.. When it is false, the RESX is not necessary
2550 : : on the end of basic block. */
2551 : :
2552 : : static bool
2553 : 40019187 : clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2554 : : {
2555 : 40019187 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2556 : 40019187 : edge_iterator ei;
2557 : 40019187 : edge e;
2558 : :
2559 : 40019187 : if (need_eh)
2560 : : {
2561 : 39943564 : if (gsi_end_p (gsi))
2562 : : return false;
2563 : 38756144 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2564 : : return false;
2565 : 929753 : gsi_prev (&gsi);
2566 : : }
2567 : 39552265 : else if (!single_succ_p (bb))
2568 : : return false;
2569 : :
2570 : 4597426 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2571 : : {
2572 : 2715100 : gimple *stmt = gsi_stmt (gsi);
2573 : 2715100 : if (is_gimple_debug (stmt))
2574 : 1045795 : continue;
2575 : 1669305 : if (gimple_clobber_p (stmt))
2576 : 780470 : continue;
2577 : 888835 : if (gimple_code (stmt) == GIMPLE_LABEL)
2578 : : break;
2579 : : return false;
2580 : : }
2581 : :
2582 : : /* See if all predecessors are either throws or clobber only BBs. */
2583 : 3020310 : FOR_EACH_EDGE (e, ei, bb->preds)
2584 : 2538245 : if (!(e->flags & EDGE_EH)
2585 : 2538245 : && !clobber_only_eh_bb_p (e->src, false))
2586 : : return false;
2587 : :
2588 : : return true;
2589 : : }
2590 : :
2591 : : /* Return true if STMT compute a floating point expression that may be affected
2592 : : by -ffast-math and similar flags. */
2593 : :
2594 : : static bool
2595 : 89862984 : fp_expression_p (gimple *stmt)
2596 : : {
2597 : 89862984 : ssa_op_iter i;
2598 : 89862984 : tree op;
2599 : :
2600 : 201492672 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE)
2601 : 112219872 : if (FLOAT_TYPE_P (TREE_TYPE (op)))
2602 : : return true;
2603 : : return false;
2604 : : }
2605 : :
2606 : : /* Return true if T references memory location that is local
2607 : : for the function (that means, dead after return) or read-only. */
2608 : :
2609 : : bool
2610 : 55312017 : refs_local_or_readonly_memory_p (tree t)
2611 : : {
2612 : : /* Non-escaping memory is fine. */
2613 : 55312017 : t = get_base_address (t);
2614 : 55312017 : if ((TREE_CODE (t) == MEM_REF
2615 : 55312017 : || TREE_CODE (t) == TARGET_MEM_REF))
2616 : 23699892 : return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2617 : :
2618 : : /* Automatic variables are fine. */
2619 : 31612125 : if (DECL_P (t)
2620 : 31612125 : && auto_var_in_fn_p (t, current_function_decl))
2621 : : return true;
2622 : :
2623 : : /* Read-only variables are fine. */
2624 : 10441834 : if (DECL_P (t) && TREE_READONLY (t))
2625 : : return true;
2626 : :
2627 : : return false;
2628 : : }
2629 : :
2630 : : /* Return true if T is a pointer pointing to memory location that is local
2631 : : for the function (that means, dead after return) or read-only. */
2632 : :
2633 : : bool
2634 : 68303169 : points_to_local_or_readonly_memory_p (tree t)
2635 : : {
2636 : : /* See if memory location is clearly invalid. */
2637 : 68303169 : if (integer_zerop (t))
2638 : 2284630 : return flag_delete_null_pointer_checks;
2639 : 66018539 : if (TREE_CODE (t) == SSA_NAME)
2640 : : {
2641 : : /* For IPA passes we can consinder accesses to return slot local
2642 : : even if it is not local in the sense that memory is dead by
2643 : : the end of founction.
2644 : : The outer function will see a store in the call assignment
2645 : : and thus this will do right thing for all uses of this
2646 : : function in the current IPA passes (modref, pure/const discovery
2647 : : and inlining heuristics). */
2648 : 41694037 : if (DECL_RESULT (current_function_decl)
2649 : 41694037 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
2650 : 42620144 : && t == ssa_default_def (cfun, DECL_RESULT (current_function_decl)))
2651 : : return true;
2652 : 41412329 : return !ptr_deref_may_alias_global_p (t, false);
2653 : : }
2654 : 24324502 : if (TREE_CODE (t) == ADDR_EXPR
2655 : 24324502 : && (TREE_CODE (TREE_OPERAND (t, 0)) != TARGET_MEM_REF
2656 : 4536 : || TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) != INTEGER_CST))
2657 : 12355150 : return refs_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2658 : : return false;
2659 : : }
2660 : :
2661 : : /* Return true if T is a pointer pointing to memory location that is possible
2662 : : sra candidate if all functions it is passed to are inlined. */
2663 : :
2664 : : static bool
2665 : 36425463 : points_to_possible_sra_candidate_p (tree t)
2666 : : {
2667 : 36425463 : if (TREE_CODE (t) != ADDR_EXPR)
2668 : : return false;
2669 : :
2670 : 10748093 : t = get_base_address (TREE_OPERAND (t, 0));
2671 : :
2672 : : /* Automatic variables are fine. */
2673 : 10748093 : if (DECL_P (t)
2674 : 10748093 : && auto_var_in_fn_p (t, current_function_decl))
2675 : : return true;
2676 : : return false;
2677 : : }
2678 : :
2679 : : /* Return true if BB only calls builtin_unreachable.
2680 : : We skip empty basic blocks, debug statements, clobbers and predicts.
2681 : : CACHE is used to memoize already analyzed blocks. */
2682 : :
2683 : : static bool
2684 : 26017481 : builtin_unreachable_bb_p (basic_block bb, vec<unsigned char> &cache)
2685 : : {
2686 : 26017481 : if (cache[bb->index])
2687 : 2424743 : return cache[bb->index] - 1;
2688 : 23592738 : gimple_stmt_iterator si;
2689 : 23592738 : auto_vec <basic_block, 4> visited_bbs;
2690 : 23592738 : bool ret = false;
2691 : 24413497 : while (true)
2692 : : {
2693 : 24413497 : bool empty_bb = true;
2694 : 24413497 : visited_bbs.safe_push (bb);
2695 : 24413497 : cache[bb->index] = 3;
2696 : 24413497 : for (si = gsi_start_nondebug_bb (bb);
2697 : 25764764 : !gsi_end_p (si) && empty_bb;
2698 : 1351267 : gsi_next_nondebug (&si))
2699 : : {
2700 : 24370208 : if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT
2701 : 24009615 : && !gimple_clobber_p (gsi_stmt (si))
2702 : 47422152 : && !gimple_nop_p (gsi_stmt (si)))
2703 : : {
2704 : : empty_bb = false;
2705 : : break;
2706 : : }
2707 : : }
2708 : 24413497 : if (!empty_bb)
2709 : : break;
2710 : : else
2711 : 1394556 : bb = single_succ_edge (bb)->dest;
2712 : 1394556 : if (cache[bb->index])
2713 : : {
2714 : 573797 : ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1;
2715 : 573797 : goto done;
2716 : : }
2717 : : }
2718 : 23018941 : if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE)
2719 : 23018941 : || gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP))
2720 : : ret = true;
2721 : 23592738 : done:
2722 : 95191711 : for (basic_block vbb:visited_bbs)
2723 : 24413497 : cache[vbb->index] = (unsigned char)ret + 1;
2724 : 23592738 : return ret;
2725 : 23592738 : }
2726 : :
2727 : : static bool
2728 : 13092161 : guards_builtin_unreachable (basic_block bb, vec<unsigned char> &cache)
2729 : : {
2730 : 13092161 : edge_iterator ei;
2731 : 13092161 : edge e;
2732 : 38929816 : FOR_EACH_EDGE (e, ei, bb->succs)
2733 : 26017481 : if (builtin_unreachable_bb_p (e->dest, cache))
2734 : : {
2735 : 179826 : if (dump_file && (dump_flags & TDF_DETAILS))
2736 : 1 : fprintf (dump_file,
2737 : : "BB %i ends with conditional guarding __builtin_unreachable;"
2738 : : " conditinal is unnecesary\n", bb->index);
2739 : 179826 : return true;
2740 : : }
2741 : : return false;
2742 : : }
2743 : :
2744 : : #define STMT_NECESSARY GF_PLF_1
2745 : :
2746 : : /* If STMT is not already marked necessary, mark it, and add it to the
2747 : : worklist if ADD_TO_WORKLIST is true. */
2748 : :
2749 : : static inline void
2750 : 164776203 : mark_stmt_necessary (gimple *stmt, auto_vec<gimple *> &worklist)
2751 : : {
2752 : 164776203 : gcc_assert (stmt);
2753 : :
2754 : 164776203 : if (gimple_plf (stmt, STMT_NECESSARY))
2755 : : return;
2756 : :
2757 : 136862978 : if (dump_file && (dump_flags & TDF_DETAILS))
2758 : : {
2759 : 207 : fprintf (dump_file, "Marking useful stmt: ");
2760 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2761 : 207 : fprintf (dump_file, "\n");
2762 : : }
2763 : :
2764 : 136862978 : gimple_set_plf (stmt, STMT_NECESSARY, true);
2765 : 136862978 : worklist.safe_push (stmt);
2766 : : }
2767 : :
2768 : : /* Mark the statement defining operand OP as necessary. */
2769 : :
2770 : : static inline void
2771 : 116525483 : mark_operand_necessary (tree op, auto_vec<gimple *> &worklist)
2772 : : {
2773 : 116525483 : gimple *stmt = SSA_NAME_DEF_STMT (op);
2774 : 116525483 : if (gimple_nop_p (stmt))
2775 : : return;
2776 : 94532083 : mark_stmt_necessary (stmt, worklist);
2777 : : }
2778 : :
2779 : : /* Mark all statements that will remain in the body after optimizing out
2780 : : conditionals guarding __builtin_unreachable which we keep to preserve
2781 : : value ranges. */
2782 : :
2783 : : static void
2784 : 6866378 : find_necessary_statements (struct cgraph_node *node)
2785 : : {
2786 : 6866378 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2787 : 6866378 : auto_vec<unsigned char, 10> cache;
2788 : 6866378 : basic_block bb;
2789 : 6866378 : auto_vec<gimple *> worklist;
2790 : :
2791 : 6866378 : cache.safe_grow_cleared (last_basic_block_for_fn (cfun));
2792 : : /* Mark all obviously necessary statements. */
2793 : 46809942 : FOR_EACH_BB_FN (bb, my_function)
2794 : : {
2795 : 39943564 : for (gimple_stmt_iterator gsi = gsi_start_phis (bb);
2796 : 51265935 : !gsi_end_p (gsi); gsi_next (&gsi))
2797 : 11322371 : gimple_set_plf (gsi_stmt (gsi), STMT_NECESSARY, false);
2798 : :
2799 : 223391242 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2800 : 143504114 : gsi_next_nondebug (&bsi))
2801 : : {
2802 : 143504114 : gimple *stmt = gsi_stmt (bsi);
2803 : :
2804 : 143504114 : gimple_set_plf (stmt, STMT_NECESSARY, false);
2805 : 143504114 : if (gimple_has_side_effects (stmt)
2806 : 117525438 : || (is_ctrl_stmt (stmt)
2807 : 20872645 : && (gimple_code (stmt) != GIMPLE_COND
2808 : 13092161 : || !guards_builtin_unreachable (bb, cache)))
2809 : 96832619 : || gimple_store_p (stmt)
2810 : 216785276 : || gimple_code (stmt) == GIMPLE_ASM)
2811 : 70244120 : mark_stmt_necessary (stmt, worklist);
2812 : : }
2813 : : }
2814 : 143729356 : while (worklist.length () > 0)
2815 : : {
2816 : 136862978 : gimple *stmt = worklist.pop ();
2817 : :
2818 : 136862978 : if (dump_file && (dump_flags & TDF_DETAILS))
2819 : : {
2820 : 207 : fprintf (dump_file, "processing: ");
2821 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2822 : 207 : fprintf (dump_file, "\n");
2823 : : }
2824 : 136862978 : if (gimple_code (stmt) == GIMPLE_PHI)
2825 : 17507464 : for (unsigned int k = 0; k < gimple_phi_num_args (stmt); k++)
2826 : : {
2827 : 12354433 : tree arg = PHI_ARG_DEF (stmt, k);
2828 : :
2829 : 12354433 : if (TREE_CODE (arg) == SSA_NAME)
2830 : 9612519 : mark_operand_necessary (arg, worklist);
2831 : : }
2832 : : else
2833 : : {
2834 : 131709947 : ssa_op_iter iter;
2835 : 131709947 : tree use;
2836 : :
2837 : 238622911 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2838 : 106912964 : mark_operand_necessary (use, worklist);
2839 : : }
2840 : : }
2841 : 6866378 : }
2842 : :
2843 : : /* Analyze function body for NODE.
2844 : : EARLY indicates run from early optimization pipeline. */
2845 : :
2846 : : static void
2847 : 6866378 : analyze_function_body (struct cgraph_node *node, bool early)
2848 : : {
2849 : 6866378 : sreal time = opt_for_fn (node->decl, param_uninlined_function_time);
2850 : : /* Estimate static overhead for function prologue/epilogue and alignment. */
2851 : 6866378 : int size = opt_for_fn (node->decl, param_uninlined_function_insns);
2852 : : /* Benefits are scaled by probability of elimination that is in range
2853 : : <0,2>. */
2854 : 6866378 : basic_block bb;
2855 : 6866378 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2856 : 6866378 : sreal freq;
2857 : 6866378 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
2858 : 13732756 : ipa_node_params *params_summary
2859 : 6866378 : = early ? NULL : ipa_node_params_sum->get (node);
2860 : 6866378 : ipa_predicate bb_predicate;
2861 : 6866378 : struct ipa_func_body_info fbi;
2862 : 6866378 : vec<ipa_predicate> nonconstant_names = vNULL;
2863 : 6866378 : int nblocks, n;
2864 : 6866378 : int *order;
2865 : 6866378 : gimple *fix_builtin_expect_stmt;
2866 : :
2867 : 6866378 : gcc_assert (my_function && my_function->cfg);
2868 : 6866378 : gcc_assert (cfun == my_function);
2869 : :
2870 : 6866378 : memset(&fbi, 0, sizeof(fbi));
2871 : 6866378 : vec_free (info->conds);
2872 : 6866378 : info->conds = NULL;
2873 : 6866378 : info->size_time_table.release ();
2874 : 6866378 : info->call_size_time_table.release ();
2875 : :
2876 : : /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2877 : : so we can produce proper inline hints.
2878 : :
2879 : : When optimizing and analyzing for early inliner, initialize node params
2880 : : so we can produce correct BB predicates. */
2881 : :
2882 : 6866378 : if (opt_for_fn (node->decl, optimize))
2883 : : {
2884 : 5982588 : calculate_dominance_info (CDI_DOMINATORS);
2885 : 5982588 : calculate_dominance_info (CDI_POST_DOMINATORS);
2886 : 5982588 : if (!early)
2887 : 1318908 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2888 : : else
2889 : : {
2890 : 4663680 : ipa_check_create_node_params ();
2891 : 4663680 : ipa_initialize_node_params (node);
2892 : : }
2893 : :
2894 : 5982588 : if (ipa_node_params_sum)
2895 : : {
2896 : 5982588 : fbi.node = node;
2897 : 5982588 : fbi.info = ipa_node_params_sum->get (node);
2898 : 5982588 : fbi.bb_infos = vNULL;
2899 : 5982588 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
2900 : 5982588 : fbi.param_count = count_formal_params (node->decl);
2901 : 5982588 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
2902 : :
2903 : 5982588 : nonconstant_names.safe_grow_cleared
2904 : 5982588 : (SSANAMES (my_function)->length (), true);
2905 : : }
2906 : : }
2907 : :
2908 : 6866378 : if (dump_file)
2909 : 227 : fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2910 : : node->dump_name ());
2911 : :
2912 : : /* When we run into maximal number of entries, we assign everything to the
2913 : : constant truth case. Be sure to have it in list. */
2914 : 6866378 : bb_predicate = true;
2915 : 6866378 : info->account_size_time (0, 0, bb_predicate, bb_predicate);
2916 : :
2917 : 6866378 : bb_predicate = ipa_predicate::not_inlined ();
2918 : 6866378 : info->account_size_time (opt_for_fn (node->decl,
2919 : : param_uninlined_function_insns)
2920 : : * ipa_fn_summary::size_scale,
2921 : 6866378 : opt_for_fn (node->decl,
2922 : : param_uninlined_function_time),
2923 : : bb_predicate,
2924 : : bb_predicate);
2925 : :
2926 : : /* Only look for target information for inlinable functions. */
2927 : 6866378 : bool scan_for_target_info =
2928 : 6866378 : info->inlinable
2929 : 12306485 : && targetm.target_option.need_ipa_fn_target_info (node->decl,
2930 : 5440107 : info->target_info);
2931 : :
2932 : 6866378 : if (fbi.info)
2933 : 5982588 : compute_bb_predicates (&fbi, node, info, params_summary);
2934 : 6866378 : find_necessary_statements (node);
2935 : 6866378 : const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2936 : 6866378 : order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2937 : 6866378 : nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2938 : 46809942 : for (n = 0; n < nblocks; n++)
2939 : : {
2940 : 39943564 : bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2941 : 39943564 : freq = bb->count.to_sreal_scale (entry_count);
2942 : 39943564 : if (clobber_only_eh_bb_p (bb))
2943 : : {
2944 : 467871 : if (dump_file && (dump_flags & TDF_DETAILS))
2945 : 0 : fprintf (dump_file, "\n Ignoring BB %i;"
2946 : : " it will be optimized away by cleanup_clobbers\n",
2947 : : bb->index);
2948 : 467871 : continue;
2949 : : }
2950 : :
2951 : : /* TODO: Obviously predicates can be propagated down across CFG. */
2952 : 39475693 : if (fbi.info)
2953 : : {
2954 : 33128761 : if (bb->aux)
2955 : 33128747 : bb_predicate = *(ipa_predicate *)bb->aux;
2956 : : else
2957 : 14 : bb_predicate = false;
2958 : : }
2959 : : else
2960 : 6346932 : bb_predicate = true;
2961 : :
2962 : 39475693 : if (dump_file && (dump_flags & TDF_DETAILS))
2963 : : {
2964 : 67 : fprintf (dump_file, "\n BB %i predicate:", bb->index);
2965 : 67 : bb_predicate.dump (dump_file, info->conds);
2966 : : }
2967 : :
2968 : 39475693 : if (fbi.info && nonconstant_names.exists ())
2969 : : {
2970 : 33128761 : ipa_predicate phi_predicate;
2971 : 33128761 : bool first_phi = true;
2972 : :
2973 : 33783798 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2974 : 655037 : gsi_next (&bsi))
2975 : : {
2976 : 6264279 : if (first_phi
2977 : 6264279 : && !phi_result_unknown_predicate (&fbi, info,
2978 : : params_summary,
2979 : : bb,
2980 : : &phi_predicate,
2981 : : nonconstant_names))
2982 : : break;
2983 : 655037 : first_phi = false;
2984 : 655037 : if (dump_file && (dump_flags & TDF_DETAILS))
2985 : : {
2986 : 3 : fprintf (dump_file, " ");
2987 : 3 : print_gimple_stmt (dump_file, gsi_stmt (bsi), 0);
2988 : : }
2989 : 655037 : predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2990 : : nonconstant_names);
2991 : : }
2992 : : }
2993 : :
2994 : 39475693 : fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2995 : :
2996 : 39475693 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
2997 : 175297330 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
2998 : : {
2999 : 135821637 : gimple *stmt = gsi_stmt (bsi);
3000 : 135821637 : if (!gimple_plf (stmt, STMT_NECESSARY))
3001 : : {
3002 : 5237013 : if (dump_file && (dump_flags & TDF_DETAILS))
3003 : : {
3004 : 19 : fprintf (dump_file, " skipping unnecesary stmt ");
3005 : 19 : print_gimple_stmt (dump_file, stmt, 0);
3006 : : }
3007 : : /* TODO: const calls used only to produce values for
3008 : : builtion_unreachable guards should not be accounted. However
3009 : : we still want to inline them and this does does not work well
3010 : : with the cost model. For now account them as usual. */
3011 : 5237013 : if (!is_gimple_call (stmt)
3012 : 5237013 : || gimple_call_internal_p (stmt))
3013 : 4961631 : continue;
3014 : : }
3015 : 130860006 : int this_size = estimate_num_insns (stmt, &eni_size_weights);
3016 : 130860006 : int this_time = estimate_num_insns (stmt, &eni_time_weights);
3017 : 130860006 : int prob;
3018 : 130860006 : ipa_predicate will_be_nonconstant;
3019 : :
3020 : : /* This relation stmt should be folded after we remove
3021 : : __builtin_expect call. Adjust the cost here. */
3022 : 130860006 : if (stmt == fix_builtin_expect_stmt)
3023 : : {
3024 : 144750 : this_size--;
3025 : 144750 : this_time--;
3026 : : }
3027 : :
3028 : 130860006 : if (dump_file && (dump_flags & TDF_DETAILS))
3029 : : {
3030 : 201 : fprintf (dump_file, " ");
3031 : 201 : print_gimple_stmt (dump_file, stmt, 0);
3032 : 201 : fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
3033 : : freq.to_double (), this_size,
3034 : : this_time);
3035 : : }
3036 : :
3037 : 130860006 : if (is_gimple_call (stmt)
3038 : 130860006 : && !gimple_call_internal_p (stmt))
3039 : : {
3040 : 21728682 : struct cgraph_edge *edge = node->get_edge (stmt);
3041 : 21728682 : ipa_call_summary *es = ipa_call_summaries->get_create (edge);
3042 : :
3043 : : /* Special case: results of BUILT_IN_CONSTANT_P will be always
3044 : : resolved as constant. We however don't want to optimize
3045 : : out the cgraph edges. */
3046 : 21728682 : if (nonconstant_names.exists ()
3047 : 18911285 : && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
3048 : 8534 : && gimple_call_lhs (stmt)
3049 : 21737216 : && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3050 : : {
3051 : 8534 : ipa_predicate false_p = false;
3052 : 8534 : nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
3053 : 8534 : = false_p;
3054 : : }
3055 : 21728682 : if (ipa_node_params_sum)
3056 : : {
3057 : 18924346 : int count = gimple_call_num_args (stmt);
3058 : 18924346 : int i;
3059 : :
3060 : 18924346 : if (count)
3061 : 16185457 : es->param.safe_grow_cleared (count, true);
3062 : 55349809 : for (i = 0; i < count; i++)
3063 : : {
3064 : 36425463 : int prob = param_change_prob (&fbi, stmt, i);
3065 : 36425463 : gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
3066 : 36425463 : es->param[i].change_prob = prob;
3067 : 36425463 : es->param[i].points_to_local_or_readonly_memory
3068 : 36425463 : = points_to_local_or_readonly_memory_p
3069 : 36425463 : (gimple_call_arg (stmt, i));
3070 : 36425463 : es->param[i].points_to_possible_sra_candidate
3071 : 36425463 : = points_to_possible_sra_candidate_p
3072 : 36425463 : (gimple_call_arg (stmt, i));
3073 : : }
3074 : : }
3075 : : /* We cannot setup VLA parameters during inlining. */
3076 : 63881810 : for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i)
3077 : 42153524 : if (TREE_CODE (gimple_call_arg (stmt, i)) == WITH_SIZE_EXPR)
3078 : : {
3079 : 396 : edge->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
3080 : 396 : break;
3081 : : }
3082 : 21728682 : es->call_stmt_size = this_size;
3083 : 21728682 : es->call_stmt_time = this_time;
3084 : 21728682 : es->loop_depth = bb_loop_depth (bb);
3085 : 21728682 : edge_set_predicate (edge, &bb_predicate);
3086 : 21728682 : if (edge->speculative)
3087 : : {
3088 : 0 : cgraph_edge *indirect
3089 : 0 : = edge->speculative_call_indirect_edge ();
3090 : 0 : ipa_call_summary *es2
3091 : 0 : = ipa_call_summaries->get_create (indirect);
3092 : 0 : ipa_call_summaries->duplicate (edge, indirect,
3093 : : es, es2);
3094 : :
3095 : : /* Edge is the first direct call.
3096 : : create and duplicate call summaries for multiple
3097 : : speculative call targets. */
3098 : 0 : for (cgraph_edge *direct
3099 : 0 : = edge->next_speculative_call_target ();
3100 : 0 : direct;
3101 : 0 : direct = direct->next_speculative_call_target ())
3102 : : {
3103 : 0 : ipa_call_summary *es3
3104 : 0 : = ipa_call_summaries->get_create (direct);
3105 : 0 : ipa_call_summaries->duplicate (edge, direct,
3106 : : es, es3);
3107 : : }
3108 : : }
3109 : : }
3110 : :
3111 : : /* TODO: When conditional jump or switch is known to be constant, but
3112 : : we did not translate it into the predicates, we really can account
3113 : : just maximum of the possible paths. */
3114 : 130860006 : if (fbi.info)
3115 : 109159059 : will_be_nonconstant
3116 : 109159059 : = will_be_nonconstant_predicate (&fbi, info, params_summary,
3117 : : stmt, nonconstant_names);
3118 : : else
3119 : 21700947 : will_be_nonconstant = true;
3120 : 130860006 : if (this_time || this_size)
3121 : : {
3122 : 106917108 : sreal final_time = (sreal)this_time * freq;
3123 : 106917108 : prob = eliminated_by_inlining_prob (&fbi, stmt);
3124 : 106917108 : if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
3125 : 11 : fprintf (dump_file,
3126 : : "\t\t50%% will be eliminated by inlining\n");
3127 : 106917108 : if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
3128 : 18 : fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
3129 : :
3130 : 106917108 : ipa_predicate p = bb_predicate & will_be_nonconstant;
3131 : 106917108 : int parm = load_or_store_of_ptr_parameter (&fbi, stmt);
3132 : 106917108 : ipa_predicate sra_predicate = true;
3133 : 106917108 : if (parm != -1)
3134 : 13260218 : sra_predicate &= add_condition (info, params_summary, parm,
3135 : : ptr_type_node, NULL,
3136 : 6630109 : ipa_predicate::not_sra_candidate, NULL, 0);
3137 : :
3138 : : /* We can ignore statement when we proved it is never going
3139 : : to happen, but we cannot do that for call statements
3140 : : because edges are accounted specially. */
3141 : :
3142 : 213834216 : if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false)
3143 : : {
3144 : 106239001 : time += final_time;
3145 : 106239001 : size += this_size;
3146 : : }
3147 : :
3148 : : /* We account everything but the calls. Calls have their own
3149 : : size/time info attached to cgraph edges. This is necessary
3150 : : in order to make the cost disappear after inlining. */
3151 : 106917108 : if (!is_gimple_call (stmt))
3152 : : {
3153 : 85743354 : if (prob)
3154 : : {
3155 : 14125760 : ipa_predicate ip
3156 : 14125760 : = bb_predicate & ipa_predicate::not_inlined () & sra_predicate;
3157 : 28251520 : info->account_size_time (this_size * prob,
3158 : 14125760 : (final_time * prob) / 2, ip,
3159 : : p);
3160 : : }
3161 : 14125760 : if (prob != 2)
3162 : 158456824 : info->account_size_time (this_size * (2 - prob),
3163 : 79228412 : (final_time * (2 - prob) / 2),
3164 : 158456824 : bb_predicate & sra_predicate,
3165 : : p);
3166 : : }
3167 : :
3168 : 106917108 : if (!info->fp_expressions && fp_expression_p (stmt))
3169 : : {
3170 : 590184 : info->fp_expressions = true;
3171 : 590184 : if (dump_file)
3172 : 9 : fprintf (dump_file, " fp_expression set\n");
3173 : : }
3174 : : }
3175 : :
3176 : : /* For target specific information, we want to scan all statements
3177 : : rather than those statements with non-zero weights, to avoid
3178 : : missing to scan something interesting for target information,
3179 : : such as: internal function calls. */
3180 : 130860006 : if (scan_for_target_info)
3181 : 0 : scan_for_target_info =
3182 : 0 : targetm.target_option.update_ipa_fn_target_info
3183 : 0 : (info->target_info, stmt);
3184 : :
3185 : : /* Account cost of address calculations in the statements. */
3186 : 504680855 : for (unsigned int i = 0; i < gimple_num_ops (stmt); i++)
3187 : : {
3188 : 373820849 : for (tree op = gimple_op (stmt, i);
3189 : 721658751 : op && handled_component_p (op);
3190 : 46391509 : op = TREE_OPERAND (op, 0))
3191 : 46391509 : if ((TREE_CODE (op) == ARRAY_REF
3192 : 46391509 : || TREE_CODE (op) == ARRAY_RANGE_REF)
3193 : 46391509 : && TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
3194 : : {
3195 : 2288081 : ipa_predicate p = bb_predicate;
3196 : 2288081 : if (fbi.info)
3197 : 1778548 : p = p & will_be_nonconstant_expr_predicate
3198 : 1778548 : (&fbi, info, params_summary,
3199 : 1778548 : TREE_OPERAND (op, 1),
3200 : 1778548 : nonconstant_names);
3201 : 2288081 : if (p != false)
3202 : : {
3203 : 2283092 : time += freq;
3204 : 2283092 : size += 1;
3205 : 2283092 : if (dump_file)
3206 : 24 : fprintf (dump_file,
3207 : : "\t\tAccounting address calculation.\n");
3208 : 2283092 : info->account_size_time (ipa_fn_summary::size_scale,
3209 : : freq,
3210 : : bb_predicate,
3211 : : p);
3212 : : }
3213 : : }
3214 : : }
3215 : :
3216 : : }
3217 : : }
3218 : 6866378 : free (order);
3219 : :
3220 : 6866378 : if (nonconstant_names.exists () && !early)
3221 : : {
3222 : 1318908 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3223 : 1318908 : unsigned max_loop_predicates = opt_for_fn (node->decl,
3224 : : param_ipa_max_loop_predicates);
3225 : :
3226 : 1318908 : if (dump_file && (dump_flags & TDF_DETAILS))
3227 : 14 : flow_loops_dump (dump_file, NULL, 0);
3228 : 1318908 : scev_initialize ();
3229 : 4552013 : for (auto loop : loops_list (cfun, 0))
3230 : : {
3231 : 595289 : ipa_predicate loop_iterations = true;
3232 : 595289 : sreal header_freq;
3233 : 595289 : edge ex;
3234 : 595289 : unsigned int j;
3235 : 595289 : class tree_niter_desc niter_desc;
3236 : 595289 : if (!loop->header->aux)
3237 : 0 : continue;
3238 : :
3239 : 595289 : profile_count phdr_count = loop_preheader_edge (loop)->count ();
3240 : 595289 : sreal phdr_freq = phdr_count.to_sreal_scale (entry_count);
3241 : :
3242 : 595289 : bb_predicate = *(ipa_predicate *)loop->header->aux;
3243 : 595289 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3244 : 1624542 : FOR_EACH_VEC_ELT (exits, j, ex)
3245 : 1029253 : if (number_of_iterations_exit (loop, ex, &niter_desc, false)
3246 : 1029253 : && !is_gimple_min_invariant (niter_desc.niter))
3247 : : {
3248 : 145003 : ipa_predicate will_be_nonconstant
3249 : 145003 : = will_be_nonconstant_expr_predicate (&fbi, info,
3250 : : params_summary,
3251 : : niter_desc.niter,
3252 : : nonconstant_names);
3253 : 145003 : if (will_be_nonconstant != true)
3254 : 58529 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3255 : 145003 : if (will_be_nonconstant != true
3256 : 203532 : && will_be_nonconstant != false)
3257 : 57985 : loop_iterations &= will_be_nonconstant;
3258 : : }
3259 : 595289 : add_freqcounting_predicate (&s->loop_iterations, loop_iterations,
3260 : : phdr_freq, max_loop_predicates);
3261 : 595289 : }
3262 : :
3263 : : /* To avoid quadratic behavior we analyze stride predicates only
3264 : : with respect to the containing loop. Thus we simply iterate
3265 : : over all defs in the outermost loop body. */
3266 : 1318908 : for (class loop *loop = loops_for_fn (cfun)->tree_root->inner;
3267 : 1785935 : loop != NULL; loop = loop->next)
3268 : : {
3269 : 467027 : ipa_predicate loop_stride = true;
3270 : 467027 : basic_block *body = get_loop_body (loop);
3271 : 467027 : profile_count phdr_count = loop_preheader_edge (loop)->count ();
3272 : 467027 : sreal phdr_freq = phdr_count.to_sreal_scale (entry_count);
3273 : 2815700 : for (unsigned i = 0; i < loop->num_nodes; i++)
3274 : : {
3275 : 2348673 : gimple_stmt_iterator gsi;
3276 : 2348673 : if (!body[i]->aux)
3277 : 7 : continue;
3278 : :
3279 : 2348666 : bb_predicate = *(ipa_predicate *)body[i]->aux;
3280 : 15132569 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
3281 : 10435237 : gsi_next (&gsi))
3282 : : {
3283 : 10435237 : gimple *stmt = gsi_stmt (gsi);
3284 : :
3285 : 10435237 : if (!is_gimple_assign (stmt))
3286 : 10286916 : continue;
3287 : :
3288 : 5193837 : tree def = gimple_assign_lhs (stmt);
3289 : 5193837 : if (TREE_CODE (def) != SSA_NAME)
3290 : 993992 : continue;
3291 : :
3292 : 4199845 : affine_iv iv;
3293 : 8399690 : if (!simple_iv (loop_containing_stmt (stmt),
3294 : : loop_containing_stmt (stmt),
3295 : : def, &iv, true)
3296 : 4199845 : || is_gimple_min_invariant (iv.step))
3297 : 4051524 : continue;
3298 : :
3299 : 148321 : ipa_predicate will_be_nonconstant
3300 : 148321 : = will_be_nonconstant_expr_predicate (&fbi, info,
3301 : : params_summary,
3302 : : iv.step,
3303 : : nonconstant_names);
3304 : 148321 : if (will_be_nonconstant != true)
3305 : 52591 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3306 : 148321 : if (will_be_nonconstant != true
3307 : 200912 : && will_be_nonconstant != false)
3308 : 33719 : loop_stride = loop_stride & will_be_nonconstant;
3309 : : }
3310 : : }
3311 : 467027 : add_freqcounting_predicate (&s->loop_strides, loop_stride,
3312 : : phdr_freq, max_loop_predicates);
3313 : 467027 : free (body);
3314 : : }
3315 : 1318908 : scev_finalize ();
3316 : : }
3317 : 60542698 : FOR_ALL_BB_FN (bb, my_function)
3318 : : {
3319 : 53676320 : edge e;
3320 : 53676320 : edge_iterator ei;
3321 : :
3322 : 53676320 : if (bb->aux)
3323 : 39520605 : edge_predicate_pool.remove ((ipa_predicate *)bb->aux);
3324 : 53676320 : bb->aux = NULL;
3325 : 114463986 : FOR_EACH_EDGE (e, ei, bb->succs)
3326 : : {
3327 : 60787666 : if (e->aux)
3328 : 2282056 : edge_predicate_pool.remove ((ipa_predicate *)e->aux);
3329 : 60787666 : e->aux = NULL;
3330 : : }
3331 : : }
3332 : 6866378 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3333 : 6866378 : ipa_size_summary *ss = ipa_size_summaries->get (node);
3334 : 6866378 : s->time = time;
3335 : 6866378 : ss->self_size = size;
3336 : 6866378 : nonconstant_names.release ();
3337 : 6866378 : ipa_release_body_info (&fbi);
3338 : 6866378 : if (opt_for_fn (node->decl, optimize))
3339 : : {
3340 : 5982588 : if (!early)
3341 : 1318908 : loop_optimizer_finalize ();
3342 : 4663680 : else if (!ipa_edge_args_sum)
3343 : 4663666 : ipa_free_all_node_params ();
3344 : 5982588 : free_dominance_info (CDI_DOMINATORS);
3345 : 5982588 : free_dominance_info (CDI_POST_DOMINATORS);
3346 : : }
3347 : 6866378 : if (dump_file)
3348 : : {
3349 : 227 : fprintf (dump_file, "\n");
3350 : 227 : ipa_dump_fn_summary (dump_file, node);
3351 : : }
3352 : 6866378 : }
3353 : :
3354 : :
3355 : : /* Compute function summary.
3356 : : EARLY is true when we compute parameters during early opts. */
3357 : :
3358 : : void
3359 : 6867671 : compute_fn_summary (struct cgraph_node *node, bool early)
3360 : : {
3361 : 6867671 : HOST_WIDE_INT self_stack_size;
3362 : 6867671 : struct cgraph_edge *e;
3363 : :
3364 : 6867671 : gcc_assert (!node->inlined_to);
3365 : :
3366 : 6867671 : if (!ipa_fn_summaries)
3367 : 209502 : ipa_fn_summary_alloc ();
3368 : :
3369 : : /* Create a new ipa_fn_summary. */
3370 : 6867671 : ((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node);
3371 : 6867671 : ipa_fn_summaries->remove (node);
3372 : 6867671 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
3373 : 6867671 : class ipa_size_summary *size_info = ipa_size_summaries->get_create (node);
3374 : :
3375 : : /* Estimate the stack size for the function if we're optimizing. */
3376 : 5983865 : self_stack_size = optimize && !node->thunk
3377 : 12850259 : ? estimated_stack_frame_size (node) : 0;
3378 : 6867671 : size_info->estimated_self_stack_size = self_stack_size;
3379 : 6867671 : info->estimated_stack_size = self_stack_size;
3380 : :
3381 : 6867671 : if (node->thunk)
3382 : : {
3383 : 1293 : ipa_call_summary *es = ipa_call_summaries->get_create (node->callees);
3384 : 1293 : ipa_predicate t = true;
3385 : :
3386 : 1293 : node->can_change_signature = false;
3387 : 1293 : es->call_stmt_size = eni_size_weights.call_cost;
3388 : 1293 : es->call_stmt_time = eni_time_weights.call_cost;
3389 : 6465 : info->account_size_time (ipa_fn_summary::size_scale
3390 : 1293 : * opt_for_fn (node->decl,
3391 : : param_uninlined_function_thunk_insns),
3392 : 1293 : opt_for_fn (node->decl,
3393 : : param_uninlined_function_thunk_time), t, t);
3394 : 1293 : t = ipa_predicate::not_inlined ();
3395 : 1293 : info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t);
3396 : 1293 : ipa_update_overall_fn_summary (node);
3397 : 1293 : size_info->self_size = size_info->size;
3398 : 1293 : if (stdarg_p (TREE_TYPE (node->decl)))
3399 : : {
3400 : 9 : info->inlinable = false;
3401 : 9 : node->callees->inline_failed = CIF_VARIADIC_THUNK;
3402 : : }
3403 : : else
3404 : 1284 : info->inlinable = true;
3405 : : }
3406 : : else
3407 : : {
3408 : : /* Even is_gimple_min_invariant rely on current_function_decl. */
3409 : 6866378 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3410 : :
3411 : : /* During IPA profile merging we may be called w/o virtual SSA form
3412 : : built. */
3413 : 6866378 : update_ssa (TODO_update_ssa_only_virtuals);
3414 : :
3415 : : /* Can this function be inlined at all? */
3416 : 6866378 : if (!opt_for_fn (node->decl, optimize)
3417 : 7750168 : && !lookup_attribute ("always_inline",
3418 : 883790 : DECL_ATTRIBUTES (node->decl)))
3419 : 815908 : info->inlinable = false;
3420 : : else
3421 : 6050470 : info->inlinable = tree_inlinable_function_p (node->decl);
3422 : :
3423 : 6866378 : bool no_signature = false;
3424 : : /* Type attributes can use parameter indices to describe them.
3425 : : Special case fn spec since we can safely preserve them in
3426 : : modref summaries. */
3427 : 6866378 : for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl));
3428 : 7245722 : list && !no_signature; list = TREE_CHAIN (list))
3429 : 379344 : if (!ipa_param_adjustments::type_attribute_allowed_p
3430 : 379344 : (get_attribute_name (list)))
3431 : : {
3432 : 155823 : if (dump_file)
3433 : : {
3434 : 0 : fprintf (dump_file, "No signature change:"
3435 : : " function type has unhandled attribute %s.\n",
3436 : 0 : IDENTIFIER_POINTER (get_attribute_name (list)));
3437 : : }
3438 : : no_signature = true;
3439 : : }
3440 : 6866378 : for (tree parm = DECL_ARGUMENTS (node->decl);
3441 : 20544988 : parm && !no_signature; parm = DECL_CHAIN (parm))
3442 : 13678610 : if (variably_modified_type_p (TREE_TYPE (parm), node->decl))
3443 : : {
3444 : 14932 : if (dump_file)
3445 : : {
3446 : 0 : fprintf (dump_file, "No signature change:"
3447 : : " has parameter with variably modified type.\n");
3448 : : }
3449 : : no_signature = true;
3450 : : }
3451 : :
3452 : : /* Likewise for #pragma omp declare simd functions or functions
3453 : : with simd attribute. */
3454 : 6866378 : if (no_signature
3455 : 13562001 : || lookup_attribute ("omp declare simd",
3456 : 6695623 : DECL_ATTRIBUTES (node->decl)))
3457 : 172440 : node->can_change_signature = false;
3458 : : else
3459 : : {
3460 : : /* Otherwise, inlinable functions always can change signature. */
3461 : 6693938 : if (info->inlinable)
3462 : 5363067 : node->can_change_signature = true;
3463 : : else
3464 : : {
3465 : : /* Functions calling builtin_apply cannot change signature. */
3466 : 5216523 : for (e = node->callees; e; e = e->next_callee)
3467 : : {
3468 : 3917862 : tree cdecl = e->callee->decl;
3469 : 3917862 : if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS,
3470 : : BUILT_IN_VA_START))
3471 : : break;
3472 : : }
3473 : 1330871 : node->can_change_signature = !e;
3474 : : }
3475 : : }
3476 : 6866378 : analyze_function_body (node, early);
3477 : 6866378 : pop_cfun ();
3478 : : }
3479 : :
3480 : : /* Inlining characteristics are maintained by the cgraph_mark_inline. */
3481 : 6867671 : size_info->size = size_info->self_size;
3482 : 6867671 : info->estimated_stack_size = size_info->estimated_self_stack_size;
3483 : :
3484 : : /* Code above should compute exactly the same result as
3485 : : ipa_update_overall_fn_summary except for case when speculative
3486 : : edges are present since these are accounted to size but not
3487 : : self_size. Do not compare time since different order the roundoff
3488 : : errors result in slight changes. */
3489 : 6867671 : ipa_update_overall_fn_summary (node);
3490 : 6867671 : if (flag_checking)
3491 : : {
3492 : 7339788 : for (e = node->indirect_calls; e; e = e->next_callee)
3493 : 472199 : if (e->speculative)
3494 : : break;
3495 : 6867589 : gcc_assert (e || size_info->size == size_info->self_size);
3496 : : }
3497 : 6867671 : }
3498 : :
3499 : :
3500 : : /* Compute parameters of functions used by inliner using
3501 : : current_function_decl. */
3502 : :
3503 : : static unsigned int
3504 : 5492009 : compute_fn_summary_for_current (void)
3505 : : {
3506 : 5492009 : compute_fn_summary (cgraph_node::get (current_function_decl), true);
3507 : 5492009 : return 0;
3508 : : }
3509 : :
3510 : : /* Estimate benefit devirtualizing indirect edge IE and return true if it can
3511 : : be devirtualized and inlined, provided m_known_vals, m_known_contexts and
3512 : : m_known_aggs in AVALS. Return false straight away if AVALS is NULL. */
3513 : :
3514 : : static bool
3515 : 533033 : estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3516 : : int *size, int *time,
3517 : : ipa_call_arg_values *avals)
3518 : : {
3519 : 533033 : tree target;
3520 : 533033 : struct cgraph_node *callee;
3521 : 533033 : class ipa_fn_summary *isummary;
3522 : 533033 : enum availability avail;
3523 : 533033 : bool speculative;
3524 : :
3525 : 533033 : if (!avals
3526 : 851122 : || (!avals->m_known_vals.length() && !avals->m_known_contexts.length ()))
3527 : : return false;
3528 : 509100 : if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3529 : : return false;
3530 : :
3531 : 508252 : target = ipa_get_indirect_edge_target (ie, avals, &speculative);
3532 : 508252 : if (!target || speculative)
3533 : : return false;
3534 : :
3535 : : /* Account for difference in cost between indirect and direct calls. */
3536 : 204010 : *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3537 : 204010 : *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3538 : 204010 : gcc_checking_assert (*time >= 0);
3539 : 204010 : gcc_checking_assert (*size >= 0);
3540 : :
3541 : 204010 : callee = cgraph_node::get (target);
3542 : 204010 : if (!callee || !callee->definition)
3543 : : return false;
3544 : 191015 : callee = callee->function_symbol (&avail);
3545 : 191015 : if (avail < AVAIL_AVAILABLE)
3546 : : return false;
3547 : 191015 : isummary = ipa_fn_summaries->get (callee);
3548 : 191015 : if (isummary == NULL)
3549 : : return false;
3550 : :
3551 : 191011 : return isummary->inlinable;
3552 : : }
3553 : :
3554 : : /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3555 : : handle edge E with probability PROB. Set HINTS accordingly if edge may be
3556 : : devirtualized. AVALS, if non-NULL, describes the context of the call site
3557 : : as far as values of parameters are concerened. */
3558 : :
3559 : : static inline void
3560 : 180100524 : estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3561 : : sreal *time, ipa_call_arg_values *avals,
3562 : : ipa_hints *hints)
3563 : : {
3564 : 180100524 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3565 : 180100524 : int call_size = es->call_stmt_size;
3566 : 180100524 : int call_time = es->call_stmt_time;
3567 : 180100524 : int cur_size;
3568 : :
3569 : 3610443 : if (!e->callee && hints && e->maybe_hot_p ()
3570 : 180633557 : && estimate_edge_devirt_benefit (e, &call_size, &call_time, avals))
3571 : 190895 : *hints |= INLINE_HINT_indirect_call;
3572 : 180100524 : cur_size = call_size * ipa_fn_summary::size_scale;
3573 : 180100524 : *size += cur_size;
3574 : 180100524 : if (min_size)
3575 : 24316157 : *min_size += cur_size;
3576 : 180100524 : if (time)
3577 : 171943034 : *time += ((sreal)call_time) * e->sreal_frequency ();
3578 : 180100524 : }
3579 : :
3580 : :
3581 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3582 : : calls in NODE. POSSIBLE_TRUTHS and AVALS describe the context of the call
3583 : : site.
3584 : :
3585 : : Helper for estimate_calls_size_and_time which does the same but
3586 : : (in most cases) faster. */
3587 : :
3588 : : static void
3589 : 59818978 : estimate_calls_size_and_time_1 (struct cgraph_node *node, int *size,
3590 : : int *min_size, sreal *time,
3591 : : ipa_hints *hints,
3592 : : clause_t possible_truths,
3593 : : ipa_call_arg_values *avals)
3594 : : {
3595 : 59818978 : struct cgraph_edge *e;
3596 : 286000266 : for (e = node->callees; e; e = e->next_callee)
3597 : : {
3598 : 226181288 : if (!e->inline_failed)
3599 : : {
3600 : 42081438 : gcc_checking_assert (!ipa_call_summaries->get (e));
3601 : 42081438 : estimate_calls_size_and_time_1 (e->callee, size, min_size, time,
3602 : : hints, possible_truths, avals);
3603 : :
3604 : 42081438 : continue;
3605 : : }
3606 : 184099850 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3607 : :
3608 : : /* Do not care about zero sized builtins. */
3609 : 184099850 : if (!es->call_stmt_size)
3610 : : {
3611 : 16383563 : gcc_checking_assert (!es->call_stmt_time);
3612 : 16383563 : continue;
3613 : : }
3614 : 167716287 : if (!es->predicate
3615 : 167716287 : || es->predicate->evaluate (possible_truths))
3616 : : {
3617 : : /* Predicates of calls shall not use NOT_CHANGED codes,
3618 : : so we do not need to compute probabilities. */
3619 : 166074257 : estimate_edge_size_and_time (e, size,
3620 : 166074257 : es->predicate ? NULL : min_size,
3621 : : time, avals, hints);
3622 : : }
3623 : : }
3624 : 63211991 : for (e = node->indirect_calls; e; e = e->next_callee)
3625 : : {
3626 : 3393013 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3627 : 3393013 : if (!es->predicate
3628 : 3393013 : || es->predicate->evaluate (possible_truths))
3629 : 3378328 : estimate_edge_size_and_time (e, size,
3630 : 3378328 : es->predicate ? NULL : min_size,
3631 : : time, avals, hints);
3632 : : }
3633 : 59818978 : }
3634 : :
3635 : : /* Populate sum->call_size_time_table for edges from NODE. */
3636 : :
3637 : : static void
3638 : 3020350 : summarize_calls_size_and_time (struct cgraph_node *node,
3639 : : ipa_fn_summary *sum)
3640 : : {
3641 : 3020350 : struct cgraph_edge *e;
3642 : 13580741 : for (e = node->callees; e; e = e->next_callee)
3643 : : {
3644 : 10560391 : if (!e->inline_failed)
3645 : : {
3646 : 964269 : gcc_checking_assert (!ipa_call_summaries->get (e));
3647 : 964269 : summarize_calls_size_and_time (e->callee, sum);
3648 : 964269 : continue;
3649 : : }
3650 : 9596122 : int size = 0;
3651 : 9596122 : sreal time = 0;
3652 : :
3653 : 9596122 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3654 : :
3655 : 9596122 : ipa_predicate pred = true;
3656 : 9596122 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3657 : :
3658 : 9596122 : if (es->predicate)
3659 : 1549059 : pred = *es->predicate;
3660 : 9596122 : sum->account_size_time (size, time, pred, pred, true);
3661 : : }
3662 : 3252465 : for (e = node->indirect_calls; e; e = e->next_callee)
3663 : : {
3664 : 232115 : int size = 0;
3665 : 232115 : sreal time = 0;
3666 : :
3667 : 232115 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3668 : 232115 : ipa_predicate pred = true;
3669 : 232115 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3670 : :
3671 : 232115 : if (es->predicate)
3672 : 35777 : pred = *es->predicate;
3673 : 232115 : sum->account_size_time (size, time, pred, pred, true);
3674 : : }
3675 : 3020350 : }
3676 : :
3677 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3678 : : calls in NODE. POSSIBLE_TRUTHS and AVALS (the latter if non-NULL) describe
3679 : : context of the call site. */
3680 : :
3681 : : static void
3682 : 17737573 : estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3683 : : int *min_size, sreal *time,
3684 : : ipa_hints *hints,
3685 : : clause_t possible_truths,
3686 : : ipa_call_arg_values *avals)
3687 : : {
3688 : 17737573 : class ipa_fn_summary *sum = ipa_fn_summaries->get (node);
3689 : 17737573 : bool use_table = true;
3690 : :
3691 : 17737573 : gcc_assert (node->callees || node->indirect_calls);
3692 : :
3693 : : /* During early inlining we do not calculate info for very
3694 : : large functions and thus there is no need for producing
3695 : : summaries. */
3696 : 17737573 : if (!ipa_node_params_sum)
3697 : : use_table = false;
3698 : : /* Do not calculate summaries for simple wrappers; it is waste
3699 : : of memory. */
3700 : 9395382 : else if (node->callees && node->indirect_calls
3701 : 506443 : && node->callees->inline_failed && !node->callees->next_callee)
3702 : : use_table = false;
3703 : : /* If there is an indirect edge that may be optimized, we need
3704 : : to go the slow way. */
3705 : 9338290 : else if (avals && hints
3706 : 9338290 : && (avals->m_known_vals.length ()
3707 : 2422979 : || avals->m_known_contexts.length ()
3708 : 2376808 : || avals->m_known_aggs.length ()))
3709 : : {
3710 : 3180789 : ipa_node_params *params_summary = ipa_node_params_sum->get (node);
3711 : 3180789 : unsigned int nargs = params_summary
3712 : 3180789 : ? ipa_get_param_count (params_summary) : 0;
3713 : :
3714 : 10972783 : for (unsigned int i = 0; i < nargs && use_table; i++)
3715 : : {
3716 : 7791994 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3717 : 7791994 : && (avals->safe_sval_at (i)
3718 : 139189 : || (ipa_argagg_value_list (avals).value_for_index_p (i))))
3719 : : use_table = false;
3720 : 7724544 : else if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3721 : 7724544 : && (avals->m_known_contexts.length () > i
3722 : 7860204 : && !avals->m_known_contexts[i].useless_p ()))
3723 : : use_table = false;
3724 : : }
3725 : : }
3726 : :
3727 : : /* Fast path is via the call size time table. */
3728 : 3180789 : if (use_table)
3729 : : {
3730 : : /* Build summary if it is absent. */
3731 : 9202847 : if (!sum->call_size_time_table.length ())
3732 : : {
3733 : 1236379 : ipa_predicate true_pred = true;
3734 : 1236379 : sum->account_size_time (0, 0, true_pred, true_pred, true);
3735 : 1236379 : summarize_calls_size_and_time (node, sum);
3736 : : }
3737 : :
3738 : 9202847 : int old_size = *size;
3739 : 9202847 : sreal old_time = time ? *time : 0;
3740 : :
3741 : 9202847 : if (min_size)
3742 : 9202847 : *min_size += sum->call_size_time_table[0].size;
3743 : :
3744 : : unsigned int i;
3745 : : size_time_entry *e;
3746 : :
3747 : : /* Walk the table and account sizes and times. */
3748 : 23874787 : for (i = 0; sum->call_size_time_table.iterate (i, &e);
3749 : : i++)
3750 : 14671940 : if (e->exec_predicate.evaluate (possible_truths))
3751 : : {
3752 : 13313640 : *size += e->size;
3753 : 13313640 : if (time)
3754 : 10642252 : *time += e->time;
3755 : : }
3756 : :
3757 : : /* Be careful and see if both methods agree. */
3758 : 33 : if ((flag_checking || dump_file)
3759 : : /* Do not try to sanity check when we know we lost some
3760 : : precision. */
3761 : 9202847 : && sum->call_size_time_table.length ()
3762 : : < ipa_fn_summary::max_size_time_table_size)
3763 : : {
3764 : 9202814 : estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL,
3765 : : possible_truths, avals);
3766 : 9202814 : gcc_assert (*size == old_size);
3767 : 16498981 : if (time && (*time - old_time > 1 || *time - old_time < -1)
3768 : 9202835 : && dump_file)
3769 : 0 : fprintf (dump_file, "Time mismatch in call summary %f!=%f\n",
3770 : : old_time.to_double (),
3771 : : time->to_double ());
3772 : : }
3773 : : }
3774 : : /* Slow path by walking all edges. */
3775 : : else
3776 : 8534726 : estimate_calls_size_and_time_1 (node, size, min_size, time, hints,
3777 : : possible_truths, avals);
3778 : 17737573 : }
3779 : :
3780 : : /* Main constructor for ipa call context. Memory allocation of ARG_VALUES
3781 : : is owned by the caller. INLINE_PARAM_SUMMARY is also owned by the
3782 : : caller. */
3783 : :
3784 : 17033200 : ipa_call_context::ipa_call_context (cgraph_node *node, clause_t possible_truths,
3785 : : clause_t nonspec_possible_truths,
3786 : : vec<inline_param_summary>
3787 : : inline_param_summary,
3788 : 17033200 : ipa_auto_call_arg_values *arg_values)
3789 : 17033200 : : m_node (node), m_possible_truths (possible_truths),
3790 : 17033200 : m_nonspec_possible_truths (nonspec_possible_truths),
3791 : 17033200 : m_inline_param_summary (inline_param_summary),
3792 : 17033200 : m_avals (arg_values)
3793 : : {
3794 : 17033200 : }
3795 : :
3796 : : /* Set THIS to be a duplicate of CTX. Copy all relevant info. */
3797 : :
3798 : : void
3799 : 1706164 : ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx)
3800 : : {
3801 : 1706164 : m_node = ctx.m_node;
3802 : 1706164 : m_possible_truths = ctx.m_possible_truths;
3803 : 1706164 : m_nonspec_possible_truths = ctx.m_nonspec_possible_truths;
3804 : 1706164 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3805 : 1706164 : unsigned int nargs = params_summary
3806 : 1706164 : ? ipa_get_param_count (params_summary) : 0;
3807 : :
3808 : 1706164 : m_inline_param_summary = vNULL;
3809 : : /* Copy the info only if there is at least one useful entry. */
3810 : 1706164 : if (ctx.m_inline_param_summary.exists ())
3811 : : {
3812 : 1520493 : unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs);
3813 : :
3814 : 3505182 : for (unsigned int i = 0; i < n; i++)
3815 : 2911098 : if (ipa_is_param_used_by_ipa_predicates (params_summary, i)
3816 : 2911098 : && !ctx.m_inline_param_summary[i].useless_p ())
3817 : : {
3818 : 926409 : m_inline_param_summary
3819 : 926409 : = ctx.m_inline_param_summary.copy ();
3820 : 926409 : break;
3821 : : }
3822 : : }
3823 : 1706164 : m_avals.m_known_vals = vNULL;
3824 : 1706164 : if (ctx.m_avals.m_known_vals.exists ())
3825 : : {
3826 : 1706164 : unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs);
3827 : :
3828 : 4128182 : for (unsigned int i = 0; i < n; i++)
3829 : 2443832 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3830 : 2443832 : && ctx.m_avals.m_known_vals[i])
3831 : : {
3832 : 21814 : m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy ();
3833 : 21814 : break;
3834 : : }
3835 : : }
3836 : :
3837 : 1706164 : m_avals.m_known_contexts = vNULL;
3838 : 1706164 : if (ctx.m_avals.m_known_contexts.exists ())
3839 : : {
3840 : 1706164 : unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs);
3841 : :
3842 : 1707219 : for (unsigned int i = 0; i < n; i++)
3843 : 19831 : if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3844 : 19831 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3845 : : {
3846 : 18776 : m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy ();
3847 : 18776 : break;
3848 : : }
3849 : : }
3850 : :
3851 : 1706164 : m_avals.m_known_aggs = vNULL;
3852 : 1706164 : if (ctx.m_avals.m_known_aggs.exists ())
3853 : : {
3854 : 1706164 : const ipa_argagg_value_list avl (&ctx.m_avals);
3855 : 5881423 : for (unsigned int i = 0; i < nargs; i++)
3856 : 4178548 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3857 : 4178548 : && avl.value_for_index_p (i))
3858 : : {
3859 : 3289 : m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy ();
3860 : 3289 : break;
3861 : : }
3862 : : }
3863 : :
3864 : 1706164 : m_avals.m_known_value_ranges = vNULL;
3865 : 1706164 : }
3866 : :
3867 : : /* Release memory used by known_vals/contexts/aggs vectors. and
3868 : : inline_param_summary. */
3869 : :
3870 : : void
3871 : 2772844 : ipa_cached_call_context::release ()
3872 : : {
3873 : : /* See if context is initialized at first place. */
3874 : 2772844 : if (!m_node)
3875 : : return;
3876 : 1706164 : m_avals.m_known_aggs.release ();
3877 : 1706164 : m_avals.m_known_vals.release ();
3878 : 1706164 : m_avals.m_known_contexts.release ();
3879 : 1706164 : m_inline_param_summary.release ();
3880 : : }
3881 : :
3882 : : /* Return true if CTX describes the same call context as THIS. */
3883 : :
3884 : : bool
3885 : 5796329 : ipa_call_context::equal_to (const ipa_call_context &ctx)
3886 : : {
3887 : 5796329 : if (m_node != ctx.m_node
3888 : : || m_possible_truths != ctx.m_possible_truths
3889 : 4729649 : || m_nonspec_possible_truths != ctx.m_nonspec_possible_truths)
3890 : : return false;
3891 : :
3892 : 4213851 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3893 : 4213851 : unsigned int nargs = params_summary
3894 : 4213851 : ? ipa_get_param_count (params_summary) : 0;
3895 : :
3896 : 4213851 : if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ())
3897 : : {
3898 : 12637822 : for (unsigned int i = 0; i < nargs; i++)
3899 : : {
3900 : 8749034 : if (!ipa_is_param_used_by_ipa_predicates (params_summary, i))
3901 : 2742736 : continue;
3902 : 6006298 : if (i >= m_inline_param_summary.length ()
3903 : 4309523 : || m_inline_param_summary[i].useless_p ())
3904 : : {
3905 : 2520614 : if (i < ctx.m_inline_param_summary.length ()
3906 : 2520614 : && !ctx.m_inline_param_summary[i].useless_p ())
3907 : : return false;
3908 : 2482690 : continue;
3909 : : }
3910 : 3485684 : if (i >= ctx.m_inline_param_summary.length ()
3911 : 3485681 : || ctx.m_inline_param_summary[i].useless_p ())
3912 : : {
3913 : : if (i < m_inline_param_summary.length ()
3914 : : && !m_inline_param_summary[i].useless_p ())
3915 : : return false;
3916 : : continue;
3917 : : }
3918 : 3450279 : if (!m_inline_param_summary[i].equal_to
3919 : 3450279 : (ctx.m_inline_param_summary[i]))
3920 : : return false;
3921 : : }
3922 : : }
3923 : 4096372 : if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ())
3924 : : {
3925 : 12607919 : for (unsigned int i = 0; i < nargs; i++)
3926 : : {
3927 : 8516545 : if (!ipa_is_param_used_by_indirect_call (params_summary, i))
3928 : 8389813 : continue;
3929 : 126732 : if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i])
3930 : : {
3931 : 74070 : if (i < ctx.m_avals.m_known_vals.length ()
3932 : 74070 : && ctx.m_avals.m_known_vals[i])
3933 : : return false;
3934 : 74048 : continue;
3935 : : }
3936 : 52662 : if (i >= ctx.m_avals.m_known_vals.length ()
3937 : 52662 : || !ctx.m_avals.m_known_vals[i])
3938 : : {
3939 : : if (i < m_avals.m_known_vals.length () && m_avals.m_known_vals[i])
3940 : : return false;
3941 : : continue;
3942 : : }
3943 : 52639 : if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i])
3944 : : return false;
3945 : : }
3946 : : }
3947 : 4091374 : if (m_avals.m_known_contexts.exists ()
3948 : 4091374 : || ctx.m_avals.m_known_contexts.exists ())
3949 : : {
3950 : 12601017 : for (unsigned int i = 0; i < nargs; i++)
3951 : : {
3952 : 8510113 : if (!ipa_is_param_used_by_polymorphic_call (params_summary, i))
3953 : 8448520 : continue;
3954 : 61593 : if (i >= m_avals.m_known_contexts.length ()
3955 : 60920 : || m_avals.m_known_contexts[i].useless_p ())
3956 : : {
3957 : 673 : if (i < ctx.m_avals.m_known_contexts.length ()
3958 : 673 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3959 : : return false;
3960 : 661 : continue;
3961 : : }
3962 : 60920 : if (i >= ctx.m_avals.m_known_contexts.length ()
3963 : 60920 : || ctx.m_avals.m_known_contexts[i].useless_p ())
3964 : : {
3965 : 12 : if (i < m_avals.m_known_contexts.length ()
3966 : 1706176 : && !m_avals.m_known_contexts[i].useless_p ())
3967 : : return false;
3968 : 0 : continue;
3969 : : }
3970 : 60908 : if (!m_avals.m_known_contexts[i].equal_to
3971 : 60908 : (ctx.m_avals.m_known_contexts[i]))
3972 : : return false;
3973 : : }
3974 : : }
3975 : 4090904 : if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ())
3976 : : {
3977 : : unsigned i = 0, j = 0;
3978 : 9158543 : while (i < m_avals.m_known_aggs.length ()
3979 : 4578060 : || j < ctx.m_avals.m_known_aggs.length ())
3980 : : {
3981 : 487895 : if (i >= m_avals.m_known_aggs.length ())
3982 : : {
3983 : 478181 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
3984 : 478181 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
3985 : : return false;
3986 : 477876 : j++;
3987 : 477876 : continue;
3988 : 477876 : }
3989 : 9714 : if (j >= ctx.m_avals.m_known_aggs.length ())
3990 : : {
3991 : 317 : int idx1 = m_avals.m_known_aggs[i].index;
3992 : 317 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
3993 : : return false;
3994 : 4 : i++;
3995 : 4 : continue;
3996 : 4 : }
3997 : :
3998 : 9397 : int idx1 = m_avals.m_known_aggs[i].index;
3999 : 9397 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4000 : 9397 : if (idx1 < idx2)
4001 : : {
4002 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4003 : : return false;
4004 : 0 : i++;
4005 : 0 : continue;
4006 : : }
4007 : 9397 : if (idx1 > idx2)
4008 : : {
4009 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4010 : : return false;
4011 : 0 : j++;
4012 : 0 : continue;
4013 : : }
4014 : 9397 : if (!ipa_is_param_used_by_indirect_call (params_summary, idx1))
4015 : : {
4016 : 248 : i++;
4017 : 248 : j++;
4018 : 248 : continue;
4019 : : }
4020 : :
4021 : 9149 : if ((m_avals.m_known_aggs[i].unit_offset
4022 : 9149 : != ctx.m_avals.m_known_aggs[j].unit_offset)
4023 : 9141 : || (m_avals.m_known_aggs[i].by_ref
4024 : 9141 : != ctx.m_avals.m_known_aggs[j].by_ref)
4025 : 18290 : || !operand_equal_p (m_avals.m_known_aggs[i].value,
4026 : 9141 : ctx.m_avals.m_known_aggs[j].value))
4027 : 121 : return false;
4028 : 9028 : i++;
4029 : 9028 : j++;
4030 : : }
4031 : : }
4032 : : return true;
4033 : : }
4034 : :
4035 : : /* Fill in the selected fields in ESTIMATES with value estimated for call in
4036 : : this context. Always compute size and min_size. Only compute time and
4037 : : nonspecialized_time if EST_TIMES is true. Only compute hints if EST_HINTS
4038 : : is true. */
4039 : :
4040 : : void
4041 : 16998196 : ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates,
4042 : : bool est_times, bool est_hints)
4043 : : {
4044 : 16998196 : class ipa_fn_summary *info = ipa_fn_summaries->get (m_node);
4045 : 16998196 : size_time_entry *e;
4046 : 16998196 : int size = 0;
4047 : 16998196 : sreal time = 0;
4048 : 16998196 : int min_size = 0;
4049 : 16998196 : ipa_hints hints = 0;
4050 : 16998196 : sreal loops_with_known_iterations = 0;
4051 : 16998196 : sreal loops_with_known_strides = 0;
4052 : 16998196 : int i;
4053 : :
4054 : 16998196 : if (dump_file && (dump_flags & TDF_DETAILS))
4055 : : {
4056 : 1012 : bool found = false;
4057 : 1012 : fprintf (dump_file, " Estimating body: %s\n"
4058 : : " Known to be false: ", m_node->dump_name ());
4059 : :
4060 : 3334 : for (i = ipa_predicate::not_inlined_condition;
4061 : 6668 : i < (ipa_predicate::first_dynamic_condition
4062 : 5570 : + (int) vec_safe_length (info->conds)); i++)
4063 : 2322 : if (!(m_possible_truths & (1 << i)))
4064 : : {
4065 : 1389 : if (found)
4066 : 423 : fprintf (dump_file, ", ");
4067 : 1389 : found = true;
4068 : 1389 : dump_condition (dump_file, info->conds, i);
4069 : : }
4070 : : }
4071 : :
4072 : 16998196 : if (m_node->callees || m_node->indirect_calls)
4073 : 22208642 : estimate_calls_size_and_time (m_node, &size, &min_size,
4074 : : est_times ? &time : NULL,
4075 : : est_hints ? &hints : NULL, m_possible_truths,
4076 : : &m_avals);
4077 : :
4078 : 16998196 : sreal nonspecialized_time = time;
4079 : :
4080 : 16998196 : min_size += info->size_time_table[0].size;
4081 : 112082321 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4082 : : {
4083 : 95084125 : bool exec = e->exec_predicate.evaluate (m_nonspec_possible_truths);
4084 : :
4085 : : /* Because predicates are conservative, it can happen that nonconst is 1
4086 : : but exec is 0. */
4087 : 95084125 : if (exec)
4088 : : {
4089 : 92772877 : bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths);
4090 : :
4091 : 92772877 : gcc_checking_assert (e->time >= 0);
4092 : 92772877 : gcc_checking_assert (time >= 0);
4093 : :
4094 : : /* We compute specialized size only because size of nonspecialized
4095 : : copy is context independent.
4096 : :
4097 : : The difference between nonspecialized execution and specialized is
4098 : : that nonspecialized is not going to have optimized out computations
4099 : : known to be constant in a specialized setting. */
4100 : 92772877 : if (nonconst)
4101 : 46278561 : size += e->size;
4102 : 92772877 : if (!est_times)
4103 : 50891742 : continue;
4104 : 41881135 : nonspecialized_time += e->time;
4105 : 41881135 : if (!nonconst)
4106 : : ;
4107 : 19652356 : else if (!m_inline_param_summary.exists ())
4108 : : {
4109 : 1379064 : if (nonconst)
4110 : 1379064 : time += e->time;
4111 : : }
4112 : : else
4113 : : {
4114 : 18273292 : int prob = e->nonconst_predicate.probability
4115 : 18273292 : (info->conds, m_possible_truths,
4116 : : m_inline_param_summary);
4117 : 18273292 : gcc_checking_assert (prob >= 0);
4118 : 18273292 : gcc_checking_assert (prob <= REG_BR_PROB_BASE);
4119 : 18273292 : if (prob == REG_BR_PROB_BASE)
4120 : 15796535 : time += e->time;
4121 : : else
4122 : 2476757 : time += e->time * prob / REG_BR_PROB_BASE;
4123 : : }
4124 : 41881135 : gcc_checking_assert (time >= 0);
4125 : : }
4126 : : }
4127 : 16998196 : gcc_checking_assert (info->size_time_table[0].exec_predicate == true);
4128 : 16998196 : gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true);
4129 : 16998196 : gcc_checking_assert (min_size >= 0);
4130 : 16998196 : gcc_checking_assert (size >= 0);
4131 : 16998196 : gcc_checking_assert (time >= 0);
4132 : : /* nonspecialized_time should be always bigger than specialized time.
4133 : : Roundoff issues however may get into the way. */
4134 : 16998196 : gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1);
4135 : :
4136 : : /* Roundoff issues may make specialized time bigger than nonspecialized
4137 : : time. We do not really want that to happen because some heuristics
4138 : : may get confused by seeing negative speedups. */
4139 : 16998196 : if (time > nonspecialized_time)
4140 : 0 : time = nonspecialized_time;
4141 : :
4142 : 16998196 : if (est_hints)
4143 : : {
4144 : 5925440 : if (info->scc_no)
4145 : 187633 : hints |= INLINE_HINT_in_scc;
4146 : 5925440 : if (DECL_DECLARED_INLINE_P (m_node->decl))
4147 : 3455614 : hints |= INLINE_HINT_declared_inline;
4148 : 5925440 : if (info->builtin_constant_p_parms.length ()
4149 : 11141 : && DECL_DECLARED_INLINE_P (m_node->decl))
4150 : 11085 : hints |= INLINE_HINT_builtin_constant_p;
4151 : :
4152 : : ipa_freqcounting_predicate *fcp;
4153 : 6519942 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
4154 : 594502 : if (!fcp->predicate->evaluate (m_possible_truths))
4155 : : {
4156 : 434677 : hints |= INLINE_HINT_loop_iterations;
4157 : 434677 : loops_with_known_iterations += fcp->freq;
4158 : : }
4159 : 5925440 : estimates->loops_with_known_iterations = loops_with_known_iterations;
4160 : :
4161 : 6289476 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
4162 : 364036 : if (!fcp->predicate->evaluate (m_possible_truths))
4163 : : {
4164 : 353532 : hints |= INLINE_HINT_loop_stride;
4165 : 353532 : loops_with_known_strides += fcp->freq;
4166 : : }
4167 : 5925440 : estimates->loops_with_known_strides = loops_with_known_strides;
4168 : : }
4169 : :
4170 : 16998196 : size = RDIV (size, ipa_fn_summary::size_scale);
4171 : 16998196 : min_size = RDIV (min_size, ipa_fn_summary::size_scale);
4172 : :
4173 : 16998196 : if (dump_file && (dump_flags & TDF_DETAILS))
4174 : : {
4175 : 1012 : fprintf (dump_file, "\n size:%i", (int) size);
4176 : 1012 : if (est_times)
4177 : 811 : fprintf (dump_file, " time:%f nonspec time:%f",
4178 : : time.to_double (), nonspecialized_time.to_double ());
4179 : 1012 : if (est_hints)
4180 : 811 : fprintf (dump_file, " loops with known iterations:%f "
4181 : : "known strides:%f", loops_with_known_iterations.to_double (),
4182 : : loops_with_known_strides.to_double ());
4183 : 1012 : fprintf (dump_file, "\n");
4184 : : }
4185 : 16998196 : if (est_times)
4186 : : {
4187 : 5925440 : estimates->time = time;
4188 : 5925440 : estimates->nonspecialized_time = nonspecialized_time;
4189 : : }
4190 : 16998196 : estimates->size = size;
4191 : 16998196 : estimates->min_size = min_size;
4192 : 16998196 : if (est_hints)
4193 : 5925440 : estimates->hints = hints;
4194 : 16998196 : return;
4195 : : }
4196 : :
4197 : :
4198 : : /* Estimate size and time needed to execute callee of EDGE assuming that
4199 : : parameters known to be constant at caller of EDGE are propagated.
4200 : : KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
4201 : : and types for parameters. */
4202 : :
4203 : : void
4204 : 160303 : estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
4205 : : ipa_auto_call_arg_values *avals,
4206 : : ipa_call_estimates *estimates)
4207 : : {
4208 : 160303 : clause_t clause, nonspec_clause;
4209 : :
4210 : 160303 : evaluate_conditions_for_known_args (node, false, avals, &clause,
4211 : : &nonspec_clause, NULL);
4212 : 160303 : ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals);
4213 : 160303 : ctx.estimate_size_and_time (estimates);
4214 : 160303 : }
4215 : :
4216 : : /* Return stack frame offset where frame of NODE is supposed to start inside
4217 : : of the function it is inlined to.
4218 : : Return 0 for functions that are not inlined. */
4219 : :
4220 : : HOST_WIDE_INT
4221 : 4528064 : ipa_get_stack_frame_offset (struct cgraph_node *node)
4222 : : {
4223 : 4528064 : HOST_WIDE_INT offset = 0;
4224 : 4528064 : if (!node->inlined_to)
4225 : : return 0;
4226 : 3581618 : node = node->callers->caller;
4227 : 4443372 : while (true)
4228 : : {
4229 : 4443372 : offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
4230 : 4012495 : if (!node->inlined_to)
4231 : : return offset;
4232 : 430877 : node = node->callers->caller;
4233 : : }
4234 : : }
4235 : :
4236 : :
4237 : : /* Update summary information of inline clones after inlining.
4238 : : Compute peak stack usage. */
4239 : :
4240 : : static void
4241 : 4116444 : inline_update_callee_summaries (struct cgraph_node *node, int depth)
4242 : : {
4243 : 4116444 : struct cgraph_edge *e;
4244 : :
4245 : 4116444 : ipa_propagate_frequency (node);
4246 : 7637586 : for (e = node->callees; e; e = e->next_callee)
4247 : : {
4248 : 3521142 : if (!e->inline_failed)
4249 : 535325 : inline_update_callee_summaries (e->callee, depth);
4250 : : else
4251 : 2985817 : ipa_call_summaries->get (e)->loop_depth += depth;
4252 : : }
4253 : 4212397 : for (e = node->indirect_calls; e; e = e->next_callee)
4254 : 95953 : ipa_call_summaries->get (e)->loop_depth += depth;
4255 : 4116444 : }
4256 : :
4257 : : /* Update change_prob and points_to_local_or_readonly_memory of EDGE after
4258 : : INLINED_EDGE has been inlined.
4259 : :
4260 : : When function A is inlined in B and A calls C with parameter that
4261 : : changes with probability PROB1 and C is known to be passthrough
4262 : : of argument if B that change with probability PROB2, the probability
4263 : : of change is now PROB1*PROB2. */
4264 : :
4265 : : static void
4266 : 3081998 : remap_edge_params (struct cgraph_edge *inlined_edge,
4267 : : struct cgraph_edge *edge)
4268 : : {
4269 : 3081998 : if (ipa_node_params_sum)
4270 : : {
4271 : 2220739 : int i;
4272 : 2220739 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4273 : 2220739 : if (!args)
4274 : : return;
4275 : 1213100 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4276 : 1213100 : class ipa_call_summary *inlined_es
4277 : 1213100 : = ipa_call_summaries->get (inlined_edge);
4278 : :
4279 : 1213100 : if (es->param.length () == 0)
4280 : : return;
4281 : :
4282 : 7142454 : for (i = 0; i < ipa_get_cs_argument_count (args); i++)
4283 : : {
4284 : 2415043 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4285 : 2415043 : if (jfunc->type == IPA_JF_PASS_THROUGH
4286 : 1758000 : || jfunc->type == IPA_JF_ANCESTOR)
4287 : : {
4288 : 791765 : int id = jfunc->type == IPA_JF_PASS_THROUGH
4289 : 791765 : ? ipa_get_jf_pass_through_formal_id (jfunc)
4290 : 134722 : : ipa_get_jf_ancestor_formal_id (jfunc);
4291 : 1583448 : if (id < (int) inlined_es->param.length ())
4292 : : {
4293 : 791675 : int prob1 = es->param[i].change_prob;
4294 : 791675 : int prob2 = inlined_es->param[id].change_prob;
4295 : 791675 : int prob = combine_probabilities (prob1, prob2);
4296 : :
4297 : 791675 : if (prob1 && prob2 && !prob)
4298 : 791675 : prob = 1;
4299 : :
4300 : 791675 : es->param[i].change_prob = prob;
4301 : :
4302 : 1583350 : if (inlined_es
4303 : 791675 : ->param[id].points_to_local_or_readonly_memory)
4304 : 205650 : es->param[i].points_to_local_or_readonly_memory = true;
4305 : 1583350 : if (inlined_es
4306 : 791675 : ->param[id].points_to_possible_sra_candidate)
4307 : 168411 : es->param[i].points_to_possible_sra_candidate = true;
4308 : : }
4309 : 791765 : if (!es->param[i].points_to_local_or_readonly_memory
4310 : : && jfunc->type == IPA_JF_CONST
4311 : : && points_to_local_or_readonly_memory_p
4312 : : (ipa_get_jf_constant (jfunc)))
4313 : : es->param[i].points_to_local_or_readonly_memory = true;
4314 : : }
4315 : : }
4316 : : }
4317 : : }
4318 : :
4319 : : /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
4320 : :
4321 : : Remap predicates of callees of NODE. Rest of arguments match
4322 : : remap_predicate.
4323 : :
4324 : : Also update change probabilities. */
4325 : :
4326 : : static void
4327 : 4116444 : remap_edge_summaries (struct cgraph_edge *inlined_edge,
4328 : : struct cgraph_node *node,
4329 : : class ipa_fn_summary *info,
4330 : : class ipa_node_params *params_summary,
4331 : : class ipa_fn_summary *callee_info,
4332 : : const vec<int> &operand_map,
4333 : : const vec<HOST_WIDE_INT> &offset_map,
4334 : : clause_t possible_truths,
4335 : : ipa_predicate *toplev_predicate)
4336 : : {
4337 : 4116444 : struct cgraph_edge *e, *next;
4338 : 7637156 : for (e = node->callees; e; e = next)
4339 : : {
4340 : 3520712 : ipa_predicate p;
4341 : 3520712 : next = e->next_callee;
4342 : :
4343 : 3520712 : if (e->inline_failed)
4344 : : {
4345 : 2985387 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4346 : 2985387 : remap_edge_params (inlined_edge, e);
4347 : :
4348 : 2985387 : if (es->predicate)
4349 : : {
4350 : 872473 : p = es->predicate->remap_after_inlining
4351 : 872473 : (info, params_summary,
4352 : : callee_info, operand_map,
4353 : : offset_map, possible_truths,
4354 : : *toplev_predicate);
4355 : 872473 : edge_set_predicate (e, &p);
4356 : : }
4357 : : else
4358 : 2112914 : edge_set_predicate (e, toplev_predicate);
4359 : : }
4360 : : else
4361 : 535325 : remap_edge_summaries (inlined_edge, e->callee, info,
4362 : : params_summary, callee_info,
4363 : : operand_map, offset_map, possible_truths,
4364 : : toplev_predicate);
4365 : : }
4366 : 4213055 : for (e = node->indirect_calls; e; e = next)
4367 : : {
4368 : 96611 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4369 : 96611 : ipa_predicate p;
4370 : 96611 : next = e->next_callee;
4371 : :
4372 : 96611 : remap_edge_params (inlined_edge, e);
4373 : 96611 : if (es->predicate)
4374 : : {
4375 : 24233 : p = es->predicate->remap_after_inlining
4376 : 24233 : (info, params_summary,
4377 : : callee_info, operand_map, offset_map,
4378 : : possible_truths, *toplev_predicate);
4379 : 24233 : edge_set_predicate (e, &p);
4380 : : }
4381 : : else
4382 : 72378 : edge_set_predicate (e, toplev_predicate);
4383 : : }
4384 : 4116444 : }
4385 : :
4386 : : /* Run remap_after_inlining on each predicate in V. */
4387 : :
4388 : : static void
4389 : 7162238 : remap_freqcounting_predicate (class ipa_fn_summary *info,
4390 : : class ipa_node_params *params_summary,
4391 : : class ipa_fn_summary *callee_info,
4392 : : vec<ipa_freqcounting_predicate, va_gc> *v,
4393 : : const vec<int> &operand_map,
4394 : : const vec<HOST_WIDE_INT> &offset_map,
4395 : : clause_t possible_truths,
4396 : : ipa_predicate *toplev_predicate)
4397 : :
4398 : : {
4399 : 7162238 : ipa_freqcounting_predicate *fcp;
4400 : 7189271 : for (int i = 0; vec_safe_iterate (v, i, &fcp); i++)
4401 : : {
4402 : 27033 : ipa_predicate p
4403 : 27033 : = fcp->predicate->remap_after_inlining (info, params_summary,
4404 : : callee_info, operand_map,
4405 : : offset_map, possible_truths,
4406 : : *toplev_predicate);
4407 : 37061 : if (p != false && p != true)
4408 : 2802 : *fcp->predicate &= p;
4409 : : }
4410 : 7162238 : }
4411 : :
4412 : : /* We inlined EDGE. Update summary of the function we inlined into. */
4413 : :
4414 : : void
4415 : 3581119 : ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
4416 : : {
4417 : 3581119 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
4418 : 7162238 : struct cgraph_node *to = (edge->caller->inlined_to
4419 : 3581119 : ? edge->caller->inlined_to : edge->caller);
4420 : 3581119 : class ipa_fn_summary *info = ipa_fn_summaries->get (to);
4421 : 3581119 : clause_t clause = 0; /* not_inline is known to be false. */
4422 : 3581119 : size_time_entry *e;
4423 : 3581119 : auto_vec<int, 8> operand_map;
4424 : 3581119 : auto_vec<HOST_WIDE_INT, 8> offset_map;
4425 : 3581119 : int i;
4426 : 3581119 : ipa_predicate toplev_predicate;
4427 : 3581119 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4428 : 7162238 : ipa_node_params *params_summary = (ipa_node_params_sum
4429 : 3581119 : ? ipa_node_params_sum->get (to) : NULL);
4430 : :
4431 : 3581119 : if (es->predicate)
4432 : 302515 : toplev_predicate = *es->predicate;
4433 : : else
4434 : 3278604 : toplev_predicate = true;
4435 : :
4436 : 3581119 : info->fp_expressions |= callee_info->fp_expressions;
4437 : 3581119 : info->target_info |= callee_info->target_info;
4438 : :
4439 : 3581119 : if (callee_info->conds)
4440 : : {
4441 : 2575759 : ipa_auto_call_arg_values avals;
4442 : 2575759 : evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
4443 : 2575759 : }
4444 : 3581119 : if (ipa_node_params_sum && callee_info->conds)
4445 : : {
4446 : 681144 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4447 : 681144 : int count = args ? ipa_get_cs_argument_count (args) : 0;
4448 : 681007 : int i;
4449 : :
4450 : 681007 : if (count)
4451 : : {
4452 : 681007 : operand_map.safe_grow_cleared (count, true);
4453 : 681007 : offset_map.safe_grow_cleared (count, true);
4454 : : }
4455 : 2138963 : for (i = 0; i < count; i++)
4456 : : {
4457 : 1457819 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4458 : 1457819 : int map = -1;
4459 : :
4460 : : /* TODO: handle non-NOPs when merging. */
4461 : 1457819 : if (jfunc->type == IPA_JF_PASS_THROUGH)
4462 : : {
4463 : 254761 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4464 : 251678 : map = ipa_get_jf_pass_through_formal_id (jfunc);
4465 : 254761 : if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
4466 : 172737 : offset_map[i] = -1;
4467 : : }
4468 : 1203058 : else if (jfunc->type == IPA_JF_ANCESTOR)
4469 : : {
4470 : 77101 : HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
4471 : 77101 : if (offset >= 0 && offset < INT_MAX)
4472 : : {
4473 : 77101 : map = ipa_get_jf_ancestor_formal_id (jfunc);
4474 : 77101 : if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
4475 : 50488 : offset = -1;
4476 : 77101 : offset_map[i] = offset;
4477 : : }
4478 : : }
4479 : 1457819 : operand_map[i] = map;
4480 : 2653195 : gcc_assert (map < ipa_get_param_count (params_summary));
4481 : : }
4482 : :
4483 : : int ip;
4484 : 684075 : for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++)
4485 : 2931 : if (ip < count && operand_map[ip] >= 0)
4486 : 33 : add_builtin_constant_p_parm (info, operand_map[ip]);
4487 : : }
4488 : 3581119 : sreal freq = edge->sreal_frequency ();
4489 : 19278289 : for (i = 0; callee_info->size_time_table.iterate (i, &e); i++)
4490 : : {
4491 : 15697170 : ipa_predicate p;
4492 : 15697170 : p = e->exec_predicate.remap_after_inlining
4493 : 15697170 : (info, params_summary,
4494 : : callee_info, operand_map,
4495 : : offset_map, clause,
4496 : : toplev_predicate);
4497 : 15697170 : ipa_predicate nonconstp;
4498 : 15697170 : nonconstp = e->nonconst_predicate.remap_after_inlining
4499 : 15697170 : (info, params_summary,
4500 : : callee_info, operand_map,
4501 : : offset_map, clause,
4502 : : toplev_predicate);
4503 : 23627437 : if (p != false && nonconstp != false)
4504 : : {
4505 : 7642925 : sreal add_time = ((sreal)e->time * freq);
4506 : 7642925 : int prob = e->nonconst_predicate.probability (callee_info->conds,
4507 : : clause, es->param);
4508 : 7642925 : if (prob != REG_BR_PROB_BASE)
4509 : 974048 : add_time = add_time * prob / REG_BR_PROB_BASE;
4510 : 974048 : if (prob != REG_BR_PROB_BASE
4511 : 974048 : && dump_file && (dump_flags & TDF_DETAILS))
4512 : : {
4513 : 3 : fprintf (dump_file, "\t\tScaling time by probability:%f\n",
4514 : 3 : (double) prob / REG_BR_PROB_BASE);
4515 : : }
4516 : 7642925 : info->account_size_time (e->size, add_time, p, nonconstp);
4517 : : }
4518 : : }
4519 : 3581119 : remap_edge_summaries (edge, edge->callee, info, params_summary,
4520 : : callee_info, operand_map,
4521 : : offset_map, clause, &toplev_predicate);
4522 : 3581119 : remap_freqcounting_predicate (info, params_summary, callee_info,
4523 : : info->loop_iterations, operand_map,
4524 : : offset_map, clause, &toplev_predicate);
4525 : 3581119 : remap_freqcounting_predicate (info, params_summary, callee_info,
4526 : : info->loop_strides, operand_map,
4527 : : offset_map, clause, &toplev_predicate);
4528 : :
4529 : 3581119 : HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
4530 : 3581119 : HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
4531 : :
4532 : 3581119 : if (info->estimated_stack_size < peak)
4533 : 109255 : info->estimated_stack_size = peak;
4534 : :
4535 : 3581119 : inline_update_callee_summaries (edge->callee, es->loop_depth);
4536 : 3581119 : if (info->call_size_time_table.length ())
4537 : : {
4538 : 819702 : int edge_size = 0;
4539 : 819702 : sreal edge_time = 0;
4540 : :
4541 : 819702 : estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0);
4542 : : /* Unaccount size and time of the optimized out call. */
4543 : 819702 : info->account_size_time (-edge_size, -edge_time,
4544 : 819702 : es->predicate ? *es->predicate : true,
4545 : 819702 : es->predicate ? *es->predicate : true,
4546 : : true);
4547 : : /* Account new calls. */
4548 : 819702 : summarize_calls_size_and_time (edge->callee, info);
4549 : : }
4550 : :
4551 : : /* Free summaries that are not maintained for inline clones/edges. */
4552 : 3581119 : ipa_call_summaries->remove (edge);
4553 : 3581119 : ipa_fn_summaries->remove (edge->callee);
4554 : 3581119 : ipa_remove_from_growth_caches (edge);
4555 : 3581119 : }
4556 : :
4557 : : /* For performance reasons ipa_merge_fn_summary_after_inlining is not updating
4558 : : overall size and time. Recompute it.
4559 : : If RESET is true also recompute call_time_size_table. */
4560 : :
4561 : : void
4562 : 8847752 : ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset)
4563 : : {
4564 : 8847752 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
4565 : 8847752 : class ipa_size_summary *size_info = ipa_size_summaries->get (node);
4566 : 8847752 : size_time_entry *e;
4567 : 8847752 : int i;
4568 : :
4569 : 8847752 : size_info->size = 0;
4570 : 8847752 : info->time = 0;
4571 : 44309794 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4572 : : {
4573 : 35462042 : size_info->size += e->size;
4574 : 35462042 : info->time += e->time;
4575 : : }
4576 : 8847752 : info->min_size = info->size_time_table[0].size;
4577 : 8847752 : if (reset)
4578 : 8058467 : info->call_size_time_table.release ();
4579 : 8847752 : if (node->callees || node->indirect_calls)
4580 : 6683039 : estimate_calls_size_and_time (node, &size_info->size, &info->min_size,
4581 : : &info->time, NULL,
4582 : : ~(clause_t) (1 << ipa_predicate::false_condition),
4583 : : NULL);
4584 : 8847752 : size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale);
4585 : 8847752 : info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale);
4586 : 8847752 : }
4587 : :
4588 : :
4589 : : /* This function performs intraprocedural analysis in NODE that is required to
4590 : : inline indirect calls. */
4591 : :
4592 : : static void
4593 : 1318904 : inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4594 : : {
4595 : 1318904 : ipa_analyze_node (node);
4596 : 1318904 : if (dump_file && (dump_flags & TDF_DETAILS))
4597 : : {
4598 : 14 : ipa_print_node_params (dump_file, node);
4599 : 14 : ipa_print_node_jump_functions (dump_file, node);
4600 : : }
4601 : 1318904 : }
4602 : :
4603 : :
4604 : : /* Note function body size. */
4605 : :
4606 : : void
4607 : 1331180 : inline_analyze_function (struct cgraph_node *node)
4608 : : {
4609 : 1331180 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4610 : :
4611 : 1331180 : if (dump_file)
4612 : 92 : fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ());
4613 : 1331180 : if (opt_for_fn (node->decl, optimize) && !node->thunk)
4614 : 1318904 : inline_indirect_intraprocedural_analysis (node);
4615 : 1331180 : compute_fn_summary (node, false);
4616 : 1331180 : if (!optimize)
4617 : : {
4618 : 10999 : struct cgraph_edge *e;
4619 : 24001 : for (e = node->callees; e; e = e->next_callee)
4620 : 13002 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4621 : 11070 : for (e = node->indirect_calls; e; e = e->next_callee)
4622 : 71 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4623 : : }
4624 : :
4625 : 1331180 : pop_cfun ();
4626 : 1331180 : }
4627 : :
4628 : :
4629 : : /* Called when new function is inserted to callgraph late. */
4630 : :
4631 : : void
4632 : 17985 : ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
4633 : : {
4634 : 17985 : inline_analyze_function (node);
4635 : 17985 : }
4636 : :
4637 : : /* Note function body size. */
4638 : :
4639 : : static void
4640 : 227249 : ipa_fn_summary_generate (void)
4641 : : {
4642 : 227249 : struct cgraph_node *node;
4643 : :
4644 : 2029489 : FOR_EACH_DEFINED_FUNCTION (node)
4645 : 1802240 : if (DECL_STRUCT_FUNCTION (node->decl))
4646 : 1785848 : node->versionable = tree_versionable_function_p (node->decl);
4647 : :
4648 : 227249 : ipa_fn_summary_alloc ();
4649 : :
4650 : 227249 : ipa_fn_summaries->enable_insertion_hook ();
4651 : :
4652 : 227249 : ipa_register_cgraph_hooks ();
4653 : :
4654 : 2029489 : FOR_EACH_DEFINED_FUNCTION (node)
4655 : 1802240 : if (!node->alias
4656 : 1802240 : && (flag_generate_lto || flag_generate_offload|| flag_wpa
4657 : 1612181 : || opt_for_fn (node->decl, optimize)))
4658 : 1296344 : inline_analyze_function (node);
4659 : 227249 : }
4660 : :
4661 : :
4662 : : /* Write inline summary for edge E to OB. */
4663 : :
4664 : : static void
4665 : 343774 : read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
4666 : : bool prevails)
4667 : : {
4668 : 343774 : class ipa_call_summary *es = prevails
4669 : 343774 : ? ipa_call_summaries->get_create (e) : NULL;
4670 : 343774 : ipa_predicate p;
4671 : 343774 : int length, i;
4672 : :
4673 : 343774 : int size = streamer_read_uhwi (ib);
4674 : 343774 : int time = streamer_read_uhwi (ib);
4675 : 343774 : int depth = streamer_read_uhwi (ib);
4676 : :
4677 : 343774 : if (es)
4678 : : {
4679 : 343725 : es->call_stmt_size = size;
4680 : 343725 : es->call_stmt_time = time;
4681 : 343725 : es->loop_depth = depth;
4682 : : }
4683 : :
4684 : 343774 : bitpack_d bp = streamer_read_bitpack (ib);
4685 : 343774 : if (es)
4686 : 343725 : es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
4687 : : else
4688 : 49 : bp_unpack_value (&bp, 1);
4689 : :
4690 : 343774 : p.stream_in (ib);
4691 : 343774 : if (es)
4692 : 343725 : edge_set_predicate (e, &p);
4693 : 343774 : length = streamer_read_uhwi (ib);
4694 : 343774 : if (length && es
4695 : 343774 : && (e->possibly_call_in_translation_unit_p ()
4696 : : /* Also stream in jump functions to builtins in hope that they
4697 : : will get fnspecs. */
4698 : 117322 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
4699 : : {
4700 : 231038 : es->param.safe_grow_cleared (length, true);
4701 : 796649 : for (i = 0; i < length; i++)
4702 : : {
4703 : 565611 : es->param[i].change_prob = streamer_read_uhwi (ib);
4704 : 565611 : bitpack_d bp = streamer_read_bitpack (ib);
4705 : 1696833 : es->param[i].points_to_local_or_readonly_memory
4706 : 565611 : = bp_unpack_value (&bp, 1);
4707 : 1696833 : es->param[i].points_to_possible_sra_candidate
4708 : 565611 : = bp_unpack_value (&bp, 1);
4709 : : }
4710 : : }
4711 : : else
4712 : : {
4713 : 133451 : for (i = 0; i < length; i++)
4714 : : {
4715 : 20715 : streamer_read_uhwi (ib);
4716 : 20715 : streamer_read_uhwi (ib);
4717 : : }
4718 : : }
4719 : 343774 : }
4720 : :
4721 : :
4722 : : /* Stream in inline summaries from the section. */
4723 : :
4724 : : static void
4725 : 13839 : inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4726 : : size_t len)
4727 : : {
4728 : 13839 : const struct lto_function_header *header =
4729 : : (const struct lto_function_header *) data;
4730 : 13839 : const int cfg_offset = sizeof (struct lto_function_header);
4731 : 13839 : const int main_offset = cfg_offset + header->cfg_size;
4732 : 13839 : const int string_offset = main_offset + header->main_size;
4733 : 13839 : class data_in *data_in;
4734 : 13839 : unsigned int i, count2, j;
4735 : 13839 : unsigned int f_count;
4736 : :
4737 : 13839 : lto_input_block ib ((const char *) data + main_offset, header->main_size,
4738 : 13839 : file_data);
4739 : :
4740 : 13839 : data_in =
4741 : 27678 : lto_data_in_create (file_data, (const char *) data + string_offset,
4742 : 13839 : header->string_size, vNULL);
4743 : 13839 : f_count = streamer_read_uhwi (&ib);
4744 : 98642 : for (i = 0; i < f_count; i++)
4745 : : {
4746 : 84803 : unsigned int index;
4747 : 84803 : struct cgraph_node *node;
4748 : 84803 : class ipa_fn_summary *info;
4749 : 84803 : class ipa_node_params *params_summary;
4750 : 84803 : class ipa_size_summary *size_info;
4751 : 84803 : lto_symtab_encoder_t encoder;
4752 : 84803 : struct bitpack_d bp;
4753 : 84803 : struct cgraph_edge *e;
4754 : 84803 : ipa_predicate p;
4755 : :
4756 : 84803 : index = streamer_read_uhwi (&ib);
4757 : 84803 : encoder = file_data->symtab_node_encoder;
4758 : 84803 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4759 : : index));
4760 : 84803 : info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
4761 : 84803 : params_summary = node->prevailing_p ()
4762 : 84803 : ? ipa_node_params_sum->get (node) : NULL;
4763 : 169606 : size_info = node->prevailing_p ()
4764 : 84803 : ? ipa_size_summaries->get_create (node) : NULL;
4765 : :
4766 : 84803 : int stack_size = streamer_read_uhwi (&ib);
4767 : 84803 : int size = streamer_read_uhwi (&ib);
4768 : 84803 : sreal time = sreal::stream_in (&ib);
4769 : :
4770 : 84803 : if (info)
4771 : : {
4772 : 84748 : info->estimated_stack_size
4773 : 84748 : = size_info->estimated_self_stack_size = stack_size;
4774 : 84748 : size_info->size = size_info->self_size = size;
4775 : 84748 : info->time = time;
4776 : : }
4777 : :
4778 : 84803 : bp = streamer_read_bitpack (&ib);
4779 : 84803 : if (info)
4780 : : {
4781 : 84748 : info->inlinable = bp_unpack_value (&bp, 1);
4782 : 84748 : info->fp_expressions = bp_unpack_value (&bp, 1);
4783 : 84748 : if (!lto_stream_offload_p)
4784 : 84748 : info->target_info = streamer_read_uhwi (&ib);
4785 : : }
4786 : : else
4787 : : {
4788 : 55 : bp_unpack_value (&bp, 1);
4789 : 55 : bp_unpack_value (&bp, 1);
4790 : 55 : if (!lto_stream_offload_p)
4791 : 55 : streamer_read_uhwi (&ib);
4792 : : }
4793 : :
4794 : 84803 : count2 = streamer_read_uhwi (&ib);
4795 : 84803 : gcc_assert (!info || !info->conds);
4796 : 84748 : if (info)
4797 : 84748 : vec_safe_reserve_exact (info->conds, count2);
4798 : 157725 : for (j = 0; j < count2; j++)
4799 : : {
4800 : 72922 : struct condition c;
4801 : 72922 : unsigned int k, count3;
4802 : 72922 : c.operand_num = streamer_read_uhwi (&ib);
4803 : 72922 : c.code = (enum tree_code) streamer_read_uhwi (&ib);
4804 : 72922 : c.type = stream_read_tree (&ib, data_in);
4805 : 72922 : c.val = stream_read_tree (&ib, data_in);
4806 : 72922 : bp = streamer_read_bitpack (&ib);
4807 : 72922 : c.agg_contents = bp_unpack_value (&bp, 1);
4808 : 72922 : c.by_ref = bp_unpack_value (&bp, 1);
4809 : 72922 : if (c.agg_contents)
4810 : 10505 : c.offset = streamer_read_uhwi (&ib);
4811 : 72922 : count3 = streamer_read_uhwi (&ib);
4812 : 72922 : c.param_ops = NULL;
4813 : 72922 : if (info)
4814 : 72922 : vec_safe_reserve_exact (c.param_ops, count3);
4815 : 72922 : if (params_summary)
4816 : 72922 : ipa_set_param_used_by_ipa_predicates
4817 : 72922 : (params_summary, c.operand_num, true);
4818 : 76138 : for (k = 0; k < count3; k++)
4819 : : {
4820 : 3216 : struct expr_eval_op op;
4821 : 3216 : enum gimple_rhs_class rhs_class;
4822 : 3216 : op.code = (enum tree_code) streamer_read_uhwi (&ib);
4823 : 3216 : op.type = stream_read_tree (&ib, data_in);
4824 : 3216 : switch (rhs_class = get_gimple_rhs_class (op.code))
4825 : : {
4826 : 1391 : case GIMPLE_UNARY_RHS:
4827 : 1391 : op.index = 0;
4828 : 1391 : op.val[0] = NULL_TREE;
4829 : 1391 : op.val[1] = NULL_TREE;
4830 : 1391 : break;
4831 : :
4832 : 1825 : case GIMPLE_BINARY_RHS:
4833 : 1825 : case GIMPLE_TERNARY_RHS:
4834 : 1825 : bp = streamer_read_bitpack (&ib);
4835 : 1825 : op.index = bp_unpack_value (&bp, 2);
4836 : 1825 : op.val[0] = stream_read_tree (&ib, data_in);
4837 : 1825 : if (rhs_class == GIMPLE_BINARY_RHS)
4838 : 1825 : op.val[1] = NULL_TREE;
4839 : : else
4840 : 0 : op.val[1] = stream_read_tree (&ib, data_in);
4841 : : break;
4842 : :
4843 : 0 : default:
4844 : 0 : fatal_error (UNKNOWN_LOCATION,
4845 : : "invalid fnsummary in LTO stream");
4846 : : }
4847 : 3216 : if (info)
4848 : 3216 : c.param_ops->quick_push (op);
4849 : : }
4850 : 72922 : if (info)
4851 : 72922 : info->conds->quick_push (c);
4852 : : }
4853 : 84803 : count2 = streamer_read_uhwi (&ib);
4854 : 84803 : gcc_assert (!info || !info->size_time_table.length ());
4855 : 84803 : if (info && count2)
4856 : 84748 : info->size_time_table.reserve_exact (count2);
4857 : 317531 : for (j = 0; j < count2; j++)
4858 : : {
4859 : 232728 : class size_time_entry e;
4860 : :
4861 : 232728 : e.size = streamer_read_uhwi (&ib);
4862 : 232728 : e.time = sreal::stream_in (&ib);
4863 : 232728 : e.exec_predicate.stream_in (&ib);
4864 : 232728 : e.nonconst_predicate.stream_in (&ib);
4865 : :
4866 : 232728 : if (info)
4867 : 232618 : info->size_time_table.quick_push (e);
4868 : : }
4869 : :
4870 : 84803 : count2 = streamer_read_uhwi (&ib);
4871 : 86039 : for (j = 0; j < count2; j++)
4872 : : {
4873 : 1236 : p.stream_in (&ib);
4874 : 1236 : sreal fcp_freq = sreal::stream_in (&ib);
4875 : 1236 : if (info)
4876 : : {
4877 : 1236 : ipa_freqcounting_predicate fcp;
4878 : 1236 : fcp.predicate = NULL;
4879 : 1236 : set_hint_predicate (&fcp.predicate, p);
4880 : 1236 : fcp.freq = fcp_freq;
4881 : 1236 : vec_safe_push (info->loop_iterations, fcp);
4882 : : }
4883 : : }
4884 : 84803 : count2 = streamer_read_uhwi (&ib);
4885 : 85087 : for (j = 0; j < count2; j++)
4886 : : {
4887 : 284 : p.stream_in (&ib);
4888 : 284 : sreal fcp_freq = sreal::stream_in (&ib);
4889 : 284 : if (info)
4890 : : {
4891 : 284 : ipa_freqcounting_predicate fcp;
4892 : 284 : fcp.predicate = NULL;
4893 : 284 : set_hint_predicate (&fcp.predicate, p);
4894 : 284 : fcp.freq = fcp_freq;
4895 : 284 : vec_safe_push (info->loop_strides, fcp);
4896 : : }
4897 : : }
4898 : 84803 : count2 = streamer_read_uhwi (&ib);
4899 : 84803 : if (info && count2)
4900 : 6 : info->builtin_constant_p_parms.reserve_exact (count2);
4901 : 84809 : for (j = 0; j < count2; j++)
4902 : : {
4903 : 6 : int parm = streamer_read_uhwi (&ib);
4904 : 6 : if (info)
4905 : 6 : info->builtin_constant_p_parms.quick_push (parm);
4906 : : }
4907 : 427162 : for (e = node->callees; e; e = e->next_callee)
4908 : 342359 : read_ipa_call_summary (&ib, e, info != NULL);
4909 : 86218 : for (e = node->indirect_calls; e; e = e->next_callee)
4910 : 1415 : read_ipa_call_summary (&ib, e, info != NULL);
4911 : : }
4912 : :
4913 : 13839 : lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4914 : : len);
4915 : 13839 : lto_data_in_delete (data_in);
4916 : 13839 : }
4917 : :
4918 : :
4919 : : /* Read inline summary. Jump functions are shared among ipa-cp
4920 : : and inliner, so when ipa-cp is active, we don't need to write them
4921 : : twice. */
4922 : :
4923 : : static void
4924 : 12814 : ipa_fn_summary_read (void)
4925 : : {
4926 : 12814 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4927 : 12814 : struct lto_file_decl_data *file_data;
4928 : 12814 : unsigned int j = 0;
4929 : :
4930 : 12814 : ipa_prop_read_jump_functions ();
4931 : 12814 : ipa_fn_summary_alloc ();
4932 : :
4933 : 39467 : while ((file_data = file_data_vec[j++]))
4934 : : {
4935 : 13839 : size_t len;
4936 : 13839 : const char *data
4937 : 13839 : = lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary,
4938 : : &len);
4939 : 13839 : if (data)
4940 : 13839 : inline_read_section (file_data, data, len);
4941 : : else
4942 : : /* Fatal error here. We do not want to support compiling ltrans units
4943 : : with different version of compiler or different flags than the WPA
4944 : : unit, so this should never happen. */
4945 : 0 : fatal_error (input_location,
4946 : : "ipa inline summary is missing in input file");
4947 : : }
4948 : 12814 : ipa_register_cgraph_hooks ();
4949 : :
4950 : 12814 : gcc_assert (ipa_fn_summaries);
4951 : 12814 : ipa_fn_summaries->enable_insertion_hook ();
4952 : 12814 : }
4953 : :
4954 : :
4955 : : /* Write inline summary for edge E to OB. */
4956 : :
4957 : : static void
4958 : 373616 : write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
4959 : : {
4960 : 373616 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4961 : 373616 : int i;
4962 : :
4963 : 373616 : streamer_write_uhwi (ob, es->call_stmt_size);
4964 : 373616 : streamer_write_uhwi (ob, es->call_stmt_time);
4965 : 373616 : streamer_write_uhwi (ob, es->loop_depth);
4966 : :
4967 : 373616 : bitpack_d bp = bitpack_create (ob->main_stream);
4968 : 373616 : bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
4969 : 373616 : streamer_write_bitpack (&bp);
4970 : :
4971 : 373616 : if (es->predicate)
4972 : 9826 : es->predicate->stream_out (ob);
4973 : : else
4974 : 363790 : streamer_write_uhwi (ob, 0);
4975 : 373616 : streamer_write_uhwi (ob, es->param.length ());
4976 : 2260915 : for (i = 0; i < (int) es->param.length (); i++)
4977 : : {
4978 : 624787 : streamer_write_uhwi (ob, es->param[i].change_prob);
4979 : 624787 : bp = bitpack_create (ob->main_stream);
4980 : 624787 : bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1);
4981 : 624787 : bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1);
4982 : 624787 : streamer_write_bitpack (&bp);
4983 : : }
4984 : 373616 : }
4985 : :
4986 : :
4987 : : /* Write inline summary for node in SET.
4988 : : Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4989 : : active, we don't need to write them twice. */
4990 : :
4991 : : static void
4992 : 23763 : ipa_fn_summary_write (void)
4993 : : {
4994 : 23763 : struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
4995 : 23763 : lto_symtab_encoder_iterator lsei;
4996 : 23763 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4997 : 23763 : unsigned int count = 0;
4998 : :
4999 : 127291 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5000 : 103528 : lsei_next_function_in_partition (&lsei))
5001 : : {
5002 : 103528 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5003 : 103528 : if (cnode->definition && !cnode->alias)
5004 : 101018 : count++;
5005 : : }
5006 : 23763 : streamer_write_uhwi (ob, count);
5007 : :
5008 : 127291 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5009 : 103528 : lsei_next_function_in_partition (&lsei))
5010 : : {
5011 : 103528 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5012 : 103528 : if (cnode->definition && !cnode->alias)
5013 : : {
5014 : 101018 : class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
5015 : 101018 : class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
5016 : 101018 : struct bitpack_d bp;
5017 : 101018 : struct cgraph_edge *edge;
5018 : 101018 : int i;
5019 : 101018 : size_time_entry *e;
5020 : 101018 : struct condition *c;
5021 : :
5022 : 101018 : streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
5023 : 101018 : streamer_write_hwi (ob, size_info->estimated_self_stack_size);
5024 : 101018 : streamer_write_hwi (ob, size_info->self_size);
5025 : 101018 : info->time.stream_out (ob);
5026 : 101018 : bp = bitpack_create (ob->main_stream);
5027 : 101018 : bp_pack_value (&bp, info->inlinable, 1);
5028 : 101018 : bp_pack_value (&bp, info->fp_expressions, 1);
5029 : 101018 : streamer_write_bitpack (&bp);
5030 : 101018 : if (!lto_stream_offload_p)
5031 : 101018 : streamer_write_uhwi (ob, info->target_info);
5032 : 101018 : streamer_write_uhwi (ob, vec_safe_length (info->conds));
5033 : 188877 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
5034 : : {
5035 : 87859 : int j;
5036 : 87859 : struct expr_eval_op *op;
5037 : :
5038 : 87859 : streamer_write_uhwi (ob, c->operand_num);
5039 : 87859 : streamer_write_uhwi (ob, c->code);
5040 : 87859 : stream_write_tree (ob, c->type, true);
5041 : 87859 : stream_write_tree (ob, c->val, true);
5042 : 87859 : bp = bitpack_create (ob->main_stream);
5043 : 87859 : bp_pack_value (&bp, c->agg_contents, 1);
5044 : 87859 : bp_pack_value (&bp, c->by_ref, 1);
5045 : 87859 : streamer_write_bitpack (&bp);
5046 : 87859 : if (c->agg_contents)
5047 : 13450 : streamer_write_uhwi (ob, c->offset);
5048 : 87859 : streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
5049 : 182013 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
5050 : : {
5051 : 3739 : streamer_write_uhwi (ob, op->code);
5052 : 3739 : stream_write_tree (ob, op->type, true);
5053 : 3739 : if (op->val[0])
5054 : : {
5055 : 2145 : bp = bitpack_create (ob->main_stream);
5056 : 2145 : bp_pack_value (&bp, op->index, 2);
5057 : 2145 : streamer_write_bitpack (&bp);
5058 : 2145 : stream_write_tree (ob, op->val[0], true);
5059 : 2145 : if (op->val[1])
5060 : 4 : stream_write_tree (ob, op->val[1], true);
5061 : : }
5062 : : }
5063 : : }
5064 : 101018 : streamer_write_uhwi (ob, info->size_time_table.length ());
5065 : 482894 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
5066 : : {
5067 : 280858 : streamer_write_uhwi (ob, e->size);
5068 : 280858 : e->time.stream_out (ob);
5069 : 280858 : e->exec_predicate.stream_out (ob);
5070 : 280858 : e->nonconst_predicate.stream_out (ob);
5071 : : }
5072 : 101018 : ipa_freqcounting_predicate *fcp;
5073 : 101018 : streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations));
5074 : 203835 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
5075 : : {
5076 : 1799 : fcp->predicate->stream_out (ob);
5077 : 1799 : fcp->freq.stream_out (ob);
5078 : : }
5079 : 101018 : streamer_write_uhwi (ob, vec_safe_length (info->loop_strides));
5080 : 202368 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
5081 : : {
5082 : 332 : fcp->predicate->stream_out (ob);
5083 : 332 : fcp->freq.stream_out (ob);
5084 : : }
5085 : 101018 : streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ());
5086 : 101018 : int ip;
5087 : 202050 : for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip);
5088 : : i++)
5089 : 14 : streamer_write_uhwi (ob, ip);
5090 : 472802 : for (edge = cnode->callees; edge; edge = edge->next_callee)
5091 : 371784 : write_ipa_call_summary (ob, edge);
5092 : 102850 : for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
5093 : 1832 : write_ipa_call_summary (ob, edge);
5094 : : }
5095 : : }
5096 : 23763 : streamer_write_char_stream (ob->main_stream, 0);
5097 : 23763 : produce_asm (ob);
5098 : 23763 : destroy_output_block (ob);
5099 : :
5100 : 23763 : ipa_prop_write_jump_functions ();
5101 : 23763 : }
5102 : :
5103 : :
5104 : : /* Release function summary. */
5105 : :
5106 : : void
5107 : 709832 : ipa_free_fn_summary (void)
5108 : : {
5109 : 709832 : if (!ipa_call_summaries)
5110 : : return;
5111 : 449560 : ggc_delete (ipa_fn_summaries);
5112 : 449560 : ipa_fn_summaries = NULL;
5113 : 449560 : delete ipa_call_summaries;
5114 : 449560 : ipa_call_summaries = NULL;
5115 : 449560 : edge_predicate_pool.release ();
5116 : : /* During IPA this is one of largest datastructures to release. */
5117 : 449560 : if (flag_wpa)
5118 : 8586 : ggc_trim ();
5119 : : }
5120 : :
5121 : : /* Release function summary. */
5122 : :
5123 : : void
5124 : 709832 : ipa_free_size_summary (void)
5125 : : {
5126 : 709832 : if (!ipa_size_summaries)
5127 : : return;
5128 : 449560 : delete ipa_size_summaries;
5129 : 449560 : ipa_size_summaries = NULL;
5130 : : }
5131 : :
5132 : : namespace {
5133 : :
5134 : : const pass_data pass_data_local_fn_summary =
5135 : : {
5136 : : GIMPLE_PASS, /* type */
5137 : : "local-fnsummary", /* name */
5138 : : OPTGROUP_INLINE, /* optinfo_flags */
5139 : : TV_INLINE_PARAMETERS, /* tv_id */
5140 : : 0, /* properties_required */
5141 : : 0, /* properties_provided */
5142 : : 0, /* properties_destroyed */
5143 : : 0, /* todo_flags_start */
5144 : : 0, /* todo_flags_finish */
5145 : : };
5146 : :
5147 : : class pass_local_fn_summary : public gimple_opt_pass
5148 : : {
5149 : : public:
5150 : 566314 : pass_local_fn_summary (gcc::context *ctxt)
5151 : 1132628 : : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
5152 : : {}
5153 : :
5154 : : /* opt_pass methods: */
5155 : 283157 : opt_pass * clone () final override
5156 : : {
5157 : 283157 : return new pass_local_fn_summary (m_ctxt);
5158 : : }
5159 : 5492009 : unsigned int execute (function *) final override
5160 : : {
5161 : 5492009 : return compute_fn_summary_for_current ();
5162 : : }
5163 : :
5164 : : }; // class pass_local_fn_summary
5165 : :
5166 : : } // anon namespace
5167 : :
5168 : : gimple_opt_pass *
5169 : 283157 : make_pass_local_fn_summary (gcc::context *ctxt)
5170 : : {
5171 : 283157 : return new pass_local_fn_summary (ctxt);
5172 : : }
5173 : :
5174 : :
5175 : : /* Free inline summary. */
5176 : :
5177 : : namespace {
5178 : :
5179 : : const pass_data pass_data_ipa_free_fn_summary =
5180 : : {
5181 : : SIMPLE_IPA_PASS, /* type */
5182 : : "free-fnsummary", /* name */
5183 : : OPTGROUP_NONE, /* optinfo_flags */
5184 : : TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
5185 : : 0, /* properties_required */
5186 : : 0, /* properties_provided */
5187 : : 0, /* properties_destroyed */
5188 : : 0, /* todo_flags_start */
5189 : : 0, /* todo_flags_finish */
5190 : : };
5191 : :
5192 : : class pass_ipa_free_fn_summary : public simple_ipa_opt_pass
5193 : : {
5194 : : public:
5195 : 566314 : pass_ipa_free_fn_summary (gcc::context *ctxt)
5196 : 566314 : : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
5197 : 1132628 : small_p (false)
5198 : : {}
5199 : :
5200 : : /* opt_pass methods: */
5201 : 283157 : opt_pass *clone () final override
5202 : : {
5203 : 283157 : return new pass_ipa_free_fn_summary (m_ctxt);
5204 : : }
5205 : 566314 : void set_pass_param (unsigned int n, bool param) final override
5206 : : {
5207 : 566314 : gcc_assert (n == 0);
5208 : 566314 : small_p = param;
5209 : 566314 : }
5210 : 476011 : bool gate (function *) final override { return true; }
5211 : 454244 : unsigned int execute (function *) final override
5212 : : {
5213 : 454244 : ipa_free_fn_summary ();
5214 : : /* Free ipa-prop structures if they are no longer needed. */
5215 : 454244 : ipa_free_all_structures_after_iinln ();
5216 : 454244 : if (!flag_wpa)
5217 : 445658 : ipa_free_size_summary ();
5218 : 454244 : return 0;
5219 : : }
5220 : :
5221 : : private:
5222 : : bool small_p;
5223 : : }; // class pass_ipa_free_fn_summary
5224 : :
5225 : : } // anon namespace
5226 : :
5227 : : simple_ipa_opt_pass *
5228 : 283157 : make_pass_ipa_free_fn_summary (gcc::context *ctxt)
5229 : : {
5230 : 283157 : return new pass_ipa_free_fn_summary (ctxt);
5231 : : }
5232 : :
5233 : : namespace {
5234 : :
5235 : : const pass_data pass_data_ipa_fn_summary =
5236 : : {
5237 : : IPA_PASS, /* type */
5238 : : "fnsummary", /* name */
5239 : : OPTGROUP_INLINE, /* optinfo_flags */
5240 : : TV_IPA_FNSUMMARY, /* tv_id */
5241 : : 0, /* properties_required */
5242 : : 0, /* properties_provided */
5243 : : 0, /* properties_destroyed */
5244 : : 0, /* todo_flags_start */
5245 : : ( TODO_dump_symtab ), /* todo_flags_finish */
5246 : : };
5247 : :
5248 : : class pass_ipa_fn_summary : public ipa_opt_pass_d
5249 : : {
5250 : : public:
5251 : 283157 : pass_ipa_fn_summary (gcc::context *ctxt)
5252 : : : ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt,
5253 : : ipa_fn_summary_generate, /* generate_summary */
5254 : : ipa_fn_summary_write, /* write_summary */
5255 : : ipa_fn_summary_read, /* read_summary */
5256 : : NULL, /* write_optimization_summary */
5257 : : NULL, /* read_optimization_summary */
5258 : : NULL, /* stmt_fixup */
5259 : : 0, /* function_transform_todo_flags_start */
5260 : : NULL, /* function_transform */
5261 : 283157 : NULL) /* variable_transform */
5262 : 283157 : {}
5263 : :
5264 : : /* opt_pass methods: */
5265 : 226853 : unsigned int execute (function *) final override { return 0; }
5266 : :
5267 : : }; // class pass_ipa_fn_summary
5268 : :
5269 : : } // anon namespace
5270 : :
5271 : : ipa_opt_pass_d *
5272 : 283157 : make_pass_ipa_fn_summary (gcc::context *ctxt)
5273 : : {
5274 : 283157 : return new pass_ipa_fn_summary (ctxt);
5275 : : }
5276 : :
5277 : : /* Reset all state within ipa-fnsummary.cc so that we can rerun the compiler
5278 : : within the same process. For use by toplev::finalize. */
5279 : :
5280 : : void
5281 : 254814 : ipa_fnsummary_cc_finalize (void)
5282 : : {
5283 : 254814 : ipa_free_fn_summary ();
5284 : 254814 : ipa_free_size_summary ();
5285 : 254814 : }
|