Line data Source code
1 : /* Function summary pass.
2 : Copyright (C) 2003-2026 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 185 : ipa_dump_hints (FILE *f, ipa_hints hints)
106 : {
107 185 : if (!hints)
108 : return;
109 148 : fprintf (f, "IPA hints:");
110 148 : if (hints & INLINE_HINT_indirect_call)
111 : {
112 23 : hints &= ~INLINE_HINT_indirect_call;
113 23 : fprintf (f, " indirect_call");
114 : }
115 148 : if (hints & INLINE_HINT_loop_iterations)
116 : {
117 3 : hints &= ~INLINE_HINT_loop_iterations;
118 3 : fprintf (f, " loop_iterations");
119 : }
120 148 : if (hints & INLINE_HINT_loop_stride)
121 : {
122 3 : hints &= ~INLINE_HINT_loop_stride;
123 3 : fprintf (f, " loop_stride");
124 : }
125 148 : if (hints & INLINE_HINT_same_scc)
126 : {
127 4 : hints &= ~INLINE_HINT_same_scc;
128 4 : fprintf (f, " same_scc");
129 : }
130 148 : if (hints & INLINE_HINT_in_scc)
131 : {
132 11 : hints &= ~INLINE_HINT_in_scc;
133 11 : fprintf (f, " in_scc");
134 : }
135 148 : if (hints & INLINE_HINT_cross_module)
136 : {
137 2 : hints &= ~INLINE_HINT_cross_module;
138 2 : fprintf (f, " cross_module");
139 : }
140 148 : if (hints & INLINE_HINT_declared_inline)
141 : {
142 122 : hints &= ~INLINE_HINT_declared_inline;
143 122 : fprintf (f, " declared_inline");
144 : }
145 148 : if (hints & INLINE_HINT_known_hot)
146 : {
147 1 : hints &= ~INLINE_HINT_known_hot;
148 1 : fprintf (f, " known_hot");
149 : }
150 148 : 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 148 : 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 134668013 : 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 134668013 : size_time_entry *e;
173 134668013 : bool found = false;
174 134668013 : int i;
175 134668013 : ipa_predicate nonconst_pred;
176 134668013 : vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table;
177 :
178 134668013 : if (exec_pred == false)
179 4757899 : return;
180 :
181 134409414 : nonconst_pred = nonconst_pred_in & exec_pred;
182 :
183 134409414 : 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 157411985 : if (!size && time == 0 && table->length ())
189 3785097 : return;
190 :
191 : /* Only for calls we are unaccounting what we previously recorded. */
192 129910114 : gcc_checking_assert (time >= 0 || call);
193 :
194 468942045 : for (i = 0; table->iterate (i, &e); i++)
195 439805146 : if (e->exec_predicate == exec_pred
196 439805146 : && e->nonconst_predicate == nonconst_pred)
197 : {
198 : found = true;
199 : break;
200 : }
201 129910114 : 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 129914388 : if (dump_file && (dump_flags & TDF_DETAILS) && (time != 0 || size))
212 : {
213 3997 : fprintf (dump_file,
214 : "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate exec:",
215 3766 : ((double) size) / ipa_fn_summary::size_scale,
216 : (time.to_double ()), found ? "" : "new ");
217 3766 : exec_pred.dump (dump_file, conds, 0);
218 3766 : if (exec_pred != nonconst_pred)
219 : {
220 84 : fprintf (dump_file, " nonconst:");
221 84 : nonconst_pred.dump (dump_file, conds);
222 : }
223 : else
224 3682 : fprintf (dump_file, "\n");
225 : }
226 129910114 : if (!found)
227 : {
228 29136899 : class size_time_entry new_entry;
229 29136899 : new_entry.size = size;
230 29136899 : new_entry.time = time;
231 29136899 : new_entry.exec_predicate = exec_pred;
232 29136899 : new_entry.nonconst_predicate = nonconst_pred;
233 29136899 : if (call)
234 1314023 : call_size_time_table.safe_push (new_entry);
235 : else
236 27822876 : size_time_table.safe_push (new_entry);
237 : }
238 : else
239 : {
240 100773215 : e->size += size;
241 100773215 : e->time += time;
242 : /* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */
243 : /* Tolerate small roundoff issues. */
244 100773215 : if (e->time < 0)
245 210 : 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 272409 : redirect_to_unreachable (struct cgraph_edge *e)
253 : {
254 272409 : struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
255 272409 : struct cgraph_node *target
256 272409 : = cgraph_node::get_create (builtin_decl_unreachable ());
257 :
258 272409 : gcc_checking_assert (lookup_attribute ("cold",
259 : DECL_ATTRIBUTES (target->decl)));
260 :
261 272409 : if (e->speculative)
262 314 : e = cgraph_edge::resolve_speculation (e, target->decl);
263 272095 : else if (!e->callee)
264 762 : e = cgraph_edge::make_direct (e, target);
265 : else
266 271333 : e->redirect_callee (target);
267 272409 : class ipa_call_summary *es = ipa_call_summaries->get (e);
268 272409 : e->inline_failed = CIF_UNREACHABLE;
269 272409 : e->count = profile_count::zero ();
270 272409 : es->call_stmt_size = 0;
271 272409 : es->call_stmt_time = 0;
272 272409 : if (callee)
273 0 : callee->remove_symbol_and_inline_clones ();
274 272409 : if (e->has_callback)
275 4 : for (cgraph_edge *cbe = e->first_callback_edge (); cbe;
276 1 : cbe = cbe->next_callback_edge ())
277 : /* If the carrying edge is unreachable, so are the callback calls. */
278 1 : redirect_to_unreachable (cbe);
279 272409 : return e;
280 : }
281 :
282 : /* Set predicate for edge E. */
283 :
284 : static void
285 30724806 : edge_set_predicate (struct cgraph_edge *e, ipa_predicate *predicate)
286 : {
287 : /* If the edge is determined to be never executed, redirect it
288 : to BUILTIN_UNREACHABLE to make it clear to IPA passes the call will
289 : be optimized out. */
290 28034082 : if (predicate && *predicate == false
291 : /* When handling speculative edges, we need to do the redirection
292 : just once. Do it always on the direct edge, so we do not
293 : attempt to resolve speculation while duplicating the edge. */
294 30997290 : && (!e->speculative || e->callee))
295 272408 : e = redirect_to_unreachable (e);
296 :
297 30724806 : class ipa_call_summary *es = ipa_call_summaries->get (e);
298 58758888 : if (predicate && *predicate != true)
299 : {
300 4556087 : if (!es->predicate)
301 4084041 : es->predicate = edge_predicate_pool.allocate ();
302 4556087 : *es->predicate = *predicate;
303 : }
304 : else
305 : {
306 26168719 : if (es->predicate)
307 838999 : edge_predicate_pool.remove (es->predicate);
308 26168719 : es->predicate = NULL;
309 : }
310 30724806 : }
311 :
312 : /* Set predicate for hint *P. */
313 :
314 : static void
315 148035 : set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate)
316 : {
317 148035 : if (new_predicate == false || new_predicate == true)
318 : {
319 3015 : if (*p)
320 0 : edge_predicate_pool.remove (*p);
321 3015 : *p = NULL;
322 : }
323 : else
324 : {
325 145020 : if (!*p)
326 145020 : *p = edge_predicate_pool.allocate ();
327 145020 : **p = new_predicate;
328 : }
329 148035 : }
330 :
331 : /* Find if NEW_PREDICATE is already in V and if so, increment its freq.
332 : Otherwise add a new item to the vector with this predicate and frerq equal
333 : to add_freq, unless the number of predicates would exceed MAX_NUM_PREDICATES
334 : in which case the function does nothing. */
335 :
336 : static void
337 1107864 : add_freqcounting_predicate (vec<ipa_freqcounting_predicate, va_gc> **v,
338 : const ipa_predicate &new_predicate, sreal add_freq,
339 : unsigned max_num_predicates)
340 : {
341 1107864 : if (new_predicate == false || new_predicate == true)
342 : return;
343 : ipa_freqcounting_predicate *f;
344 139570 : for (int i = 0; vec_safe_iterate (*v, i, &f); i++)
345 71385 : if (new_predicate == f->predicate)
346 : {
347 0 : f->freq += add_freq;
348 0 : return;
349 : }
350 94045 : if (vec_safe_length (*v) >= max_num_predicates)
351 : /* Too many different predicates to account for. */
352 : return;
353 :
354 67913 : ipa_freqcounting_predicate fcp;
355 67913 : fcp.predicate = NULL;
356 67913 : set_hint_predicate (&fcp.predicate, new_predicate);
357 67913 : fcp.freq = add_freq;
358 67913 : vec_safe_push (*v, fcp);
359 67913 : return;
360 : }
361 :
362 : /* Compute what conditions may or may not hold given information about
363 : parameters. RET_CLAUSE returns truths that may hold in a specialized copy,
364 : while RET_NONSPEC_CLAUSE returns truths that may hold in an nonspecialized
365 : copy when called in a given context. It is a bitmask of conditions. Bit
366 : 0 means that condition is known to be false, while bit 1 means that condition
367 : may or may not be true. These differs - for example NOT_INLINED condition
368 : is always false in the second and also builtin_constant_p tests cannot use
369 : the fact that parameter is indeed a constant.
370 :
371 : When INLINE_P is true, assume that we are inlining. AVAL contains known
372 : information about argument values. The function does not modify its content
373 : and so AVALs could also be of type ipa_call_arg_values but so far all
374 : callers work with the auto version and so we avoid the conversion for
375 : convenience.
376 :
377 : ERROR_MARK value of an argument means compile time invariant. */
378 :
379 : static void
380 22116263 : evaluate_conditions_for_known_args (struct cgraph_node *node,
381 : bool inline_p,
382 : ipa_auto_call_arg_values *avals,
383 : clause_t *ret_clause,
384 : clause_t *ret_nonspec_clause,
385 : ipa_call_summary *es)
386 : {
387 22116263 : clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
388 22116263 : clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition;
389 22116263 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
390 22116263 : int i;
391 22116263 : struct condition *c;
392 :
393 91905439 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
394 : {
395 69789176 : tree val = NULL;
396 69789176 : tree res;
397 69789176 : int j;
398 69789176 : struct expr_eval_op *op;
399 :
400 69789176 : if (c->code == ipa_predicate::not_sra_candidate)
401 : {
402 16402598 : if (!inline_p
403 16402598 : || !es
404 16642281 : || (int)es->param.length () <= c->operand_num
405 24723707 : || !es->param[c->operand_num].points_to_possible_sra_candidate)
406 12174875 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
407 16402598 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
408 69789176 : continue;
409 : }
410 :
411 53386578 : if (c->agg_contents)
412 : {
413 28954580 : if (c->code == ipa_predicate::changed
414 18487157 : && !c->by_ref
415 31468721 : && (avals->safe_sval_at(c->operand_num) == error_mark_node))
416 5208 : continue;
417 :
418 28944164 : if (tree sval = avals->safe_sval_at (c->operand_num))
419 13580559 : val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref);
420 13580559 : if (!val)
421 : {
422 28933881 : ipa_argagg_value_list avs (avals);
423 28933881 : val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
424 28933881 : c->by_ref);
425 : }
426 : }
427 : else
428 : {
429 24437206 : val = avals->safe_sval_at (c->operand_num);
430 24437206 : if (val && val == error_mark_node
431 2384377 : && c->code != ipa_predicate::changed)
432 : val = NULL_TREE;
433 : }
434 :
435 40886440 : if (!val
436 39593352 : && (c->code == ipa_predicate::changed
437 : || c->code == ipa_predicate::is_not_constant))
438 : {
439 25166602 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
440 25166602 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
441 25166602 : continue;
442 : }
443 28214768 : if (c->code == ipa_predicate::changed)
444 : {
445 7785540 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
446 7785540 : continue;
447 : }
448 :
449 20429228 : if (c->code == ipa_predicate::is_not_constant)
450 : {
451 5539 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
452 5539 : continue;
453 : }
454 :
455 20423689 : if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val)))
456 : {
457 5920446 : if (c->type != TREE_TYPE (val))
458 1342439 : val = fold_unary (VIEW_CONVERT_EXPR, c->type, val);
459 6337061 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
460 : {
461 417059 : if (!val)
462 : break;
463 416615 : if (!op->val[0])
464 155843 : val = fold_unary (op->code, op->type, val);
465 260772 : else if (!op->val[1])
466 521544 : val = fold_binary (op->code, op->type,
467 : op->index ? op->val[0] : val,
468 : op->index ? val : op->val[0]);
469 0 : else if (op->index == 0)
470 0 : val = fold_ternary (op->code, op->type,
471 : val, op->val[0], op->val[1]);
472 0 : else if (op->index == 1)
473 0 : val = fold_ternary (op->code, op->type,
474 : op->val[0], val, op->val[1]);
475 0 : else if (op->index == 2)
476 0 : val = fold_ternary (op->code, op->type,
477 : op->val[0], op->val[1], val);
478 : else
479 : val = NULL_TREE;
480 : }
481 :
482 5920446 : res = val
483 5920446 : ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
484 : : NULL;
485 :
486 5919982 : if (res && integer_zerop (res))
487 3051152 : continue;
488 2869294 : if (res && integer_onep (res))
489 : {
490 2846652 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
491 2846652 : nonspec_clause
492 2846652 : |= 1 << (i + ipa_predicate::first_dynamic_condition);
493 2846652 : continue;
494 : }
495 : }
496 14525885 : if (c->operand_num < (int) avals->m_known_value_ranges.length ()
497 8434199 : && !c->agg_contents
498 16451757 : && (!val || TREE_CODE (val) != INTEGER_CST))
499 : {
500 1925783 : value_range vr (avals->m_known_value_ranges[c->operand_num]);
501 1925783 : if (!vr.undefined_p ()
502 1254362 : && !vr.varying_p ()
503 3180145 : && (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ())))
504 : {
505 1253930 : if (!useless_type_conversion_p (c->type, vr.type ()))
506 638 : range_cast (vr, c->type);
507 :
508 1571918 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
509 : {
510 356184 : if (vr.varying_p () || vr.undefined_p ())
511 : break;
512 :
513 317988 : value_range res (op->type);
514 317988 : if (!op->val[0])
515 : {
516 103755 : value_range varying (op->type);
517 103755 : varying.set_varying (op->type);
518 103755 : range_op_handler handler (op->code);
519 103755 : if (!handler
520 103755 : || !res.supports_type_p (op->type)
521 207510 : || !handler.fold_range (res, op->type, vr, varying))
522 0 : res.set_varying (op->type);
523 103755 : }
524 214233 : else if (!op->val[1])
525 : {
526 214061 : value_range op0 (TREE_TYPE (op->val[0]));
527 214061 : range_op_handler handler (op->code);
528 :
529 214061 : ipa_get_range_from_ip_invariant (op0, op->val[0], node);
530 :
531 214061 : if (!handler
532 214061 : || !res.supports_type_p (op->type)
533 428122 : || !handler.fold_range (res, op->type,
534 214061 : op->index ? op0 : vr,
535 427534 : op->index ? vr : op0))
536 0 : res.set_varying (op->type);
537 214061 : }
538 : else
539 172 : res.set_varying (op->type);
540 317988 : vr = res;
541 317988 : }
542 1253930 : if (!vr.varying_p () && !vr.undefined_p ())
543 : {
544 1210261 : int_range<2> res;
545 1210261 : value_range val_vr (TREE_TYPE (c->val));
546 1210261 : range_op_handler handler (c->code);
547 :
548 1210261 : ipa_get_range_from_ip_invariant (val_vr, c->val, node);
549 :
550 1210261 : if (!handler
551 1210261 : || !val_vr.supports_type_p (TREE_TYPE (c->val))
552 2420522 : || !handler.fold_range (res, boolean_type_node, vr, val_vr))
553 0 : res.set_varying (boolean_type_node);
554 :
555 1210261 : if (res.zero_p ())
556 204895 : continue;
557 1210261 : }
558 : }
559 1925783 : }
560 :
561 14320990 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
562 14320990 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
563 : }
564 22116263 : *ret_clause = clause;
565 22116263 : if (ret_nonspec_clause)
566 19231438 : *ret_nonspec_clause = nonspec_clause;
567 22116263 : }
568 :
569 : /* Return true if VRP will be exectued on the function.
570 : We do not want to anticipate optimizations that will not happen.
571 :
572 : FIXME: This can be confused with -fdisable and debug counters and thus
573 : it should not be used for correctness (only to make heuristics work).
574 : This means that inliner should do its own optimizations of expressions
575 : that it predicts to be constant so wrong code can not be triggered by
576 : builtin_constant_p. */
577 :
578 : static bool
579 12194028 : vrp_will_run_p (struct cgraph_node *node)
580 : {
581 12194028 : return (opt_for_fn (node->decl, optimize)
582 12194028 : && !opt_for_fn (node->decl, optimize_debug)
583 24386839 : && opt_for_fn (node->decl, flag_tree_vrp));
584 : }
585 :
586 : /* Similarly about FRE. */
587 :
588 : static bool
589 14492918 : fre_will_run_p (struct cgraph_node *node)
590 : {
591 14492918 : return (opt_for_fn (node->decl, optimize)
592 14492918 : && !opt_for_fn (node->decl, optimize_debug)
593 28983555 : && opt_for_fn (node->decl, flag_tree_fre));
594 : }
595 :
596 : /* Work out what conditions might be true at invocation of E.
597 : Compute costs for inlined edge if INLINE_P is true.
598 :
599 : Return in CLAUSE_PTR the evaluated conditions and in NONSPEC_CLAUSE_PTR
600 : (if non-NULL) conditions evaluated for nonspecialized clone called
601 : in a given context.
602 :
603 : Vectors in AVALS will be populated with useful known information about
604 : argument values - information not known to have any uses will be omitted -
605 : except for m_known_contexts which will only be calculated if
606 : COMPUTE_CONTEXTS is true. */
607 :
608 : void
609 21819322 : evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
610 : clause_t *clause_ptr,
611 : clause_t *nonspec_clause_ptr,
612 : ipa_auto_call_arg_values *avals,
613 : bool compute_contexts)
614 : {
615 21819322 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
616 21819322 : class ipa_fn_summary *info = ipa_fn_summaries->get (callee);
617 21819322 : class ipa_edge_args *args;
618 21819322 : class ipa_call_summary *es = NULL;
619 :
620 21819322 : if (clause_ptr)
621 21819322 : *clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
622 :
623 21819322 : if (ipa_node_params_sum
624 10127748 : && !e->call_stmt_cannot_inline_p
625 10127748 : && (info->conds || compute_contexts)
626 31947070 : && (args = ipa_edge_args_sum->get (e)) != NULL)
627 : {
628 10073277 : struct cgraph_node *caller;
629 10073277 : class ipa_node_params *caller_parms_info, *callee_pi = NULL;
630 10073277 : int i, count = ipa_get_cs_argument_count (args);
631 10073277 : es = ipa_call_summaries->get (e);
632 :
633 10073277 : if (count)
634 : {
635 9378175 : if (e->caller->inlined_to)
636 : caller = e->caller->inlined_to;
637 : else
638 7561483 : caller = e->caller;
639 9378175 : caller_parms_info = ipa_node_params_sum->get (caller);
640 9378175 : callee_pi = ipa_node_params_sum->get (callee);
641 :
642 : /* Watch for thunks. */
643 9378175 : if (callee_pi)
644 : /* Watch for variadic functions. */
645 9377834 : count = MIN (count, ipa_get_param_count (callee_pi));
646 : }
647 :
648 9377834 : if (callee_pi)
649 30675909 : for (i = 0; i < count; i++)
650 : {
651 21298075 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
652 :
653 21298075 : if (ipa_is_param_used_by_indirect_call (callee_pi, i)
654 21298075 : || ipa_is_param_used_by_ipa_predicates (callee_pi, i))
655 : {
656 : /* Determine if we know constant value of the parameter. */
657 14492918 : tree type = ipa_get_type (callee_pi, i);
658 14492918 : tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type);
659 :
660 11265043 : if (!cst && e->call_stmt
661 25707325 : && i < (int)gimple_call_num_args (e->call_stmt))
662 : {
663 11214407 : cst = gimple_call_arg (e->call_stmt, i);
664 11214407 : if (!is_gimple_min_invariant (cst))
665 : cst = NULL;
666 : }
667 3456963 : if (cst)
668 : {
669 3406327 : gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
670 3406327 : if (!avals->m_known_vals.length ())
671 1834138 : avals->m_known_vals.safe_grow_cleared (count, true);
672 3406327 : avals->m_known_vals[i] = cst;
673 : }
674 11086591 : else if (inline_p && !es->param[i].change_prob)
675 : {
676 4257439 : if (!avals->m_known_vals.length ())
677 3548718 : avals->m_known_vals.safe_grow_cleared (count, true);
678 4257439 : avals->m_known_vals[i] = error_mark_node;
679 : }
680 :
681 : /* If we failed to get simple constant, try value range. */
682 3406327 : if ((!cst || TREE_CODE (cst) != INTEGER_CST)
683 12194028 : && vrp_will_run_p (caller)
684 26523124 : && ipa_is_param_used_by_ipa_predicates (callee_pi, i))
685 : {
686 11989833 : value_range vr (type);
687 :
688 11989833 : ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type);
689 11989833 : if (!vr.undefined_p () && !vr.varying_p ())
690 : {
691 8595796 : if (!avals->m_known_value_ranges.length ())
692 : {
693 6212125 : avals->m_known_value_ranges.safe_grow_cleared (count,
694 : true);
695 19680793 : for (int i = 0; i < count; ++i)
696 13468668 : avals->m_known_value_ranges[i].set_type (void_type_node);
697 : }
698 8595796 : avals->m_known_value_ranges[i] = vr;
699 : }
700 11989833 : }
701 :
702 : /* Determine known aggregate values. */
703 14492918 : if (fre_will_run_p (caller))
704 14490233 : ipa_push_agg_values_from_jfunc (caller_parms_info,
705 : caller, &jf->agg, i,
706 : &avals->m_known_aggs);
707 : }
708 :
709 : /* For calls used in polymorphic calls we further determine
710 : polymorphic call context. */
711 21298075 : if (compute_contexts
712 21298075 : && ipa_is_param_used_by_polymorphic_call (callee_pi, i))
713 : {
714 161466 : ipa_polymorphic_call_context
715 161466 : ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
716 322932 : if (!ctx.useless_p ())
717 : {
718 159518 : if (!avals->m_known_contexts.length ())
719 159381 : avals->m_known_contexts.safe_grow_cleared (count, true);
720 159518 : avals->m_known_contexts[i]
721 319036 : = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
722 : }
723 : }
724 : }
725 : else
726 695443 : gcc_assert (!count || callee->thunk);
727 : }
728 11746045 : else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds)
729 : {
730 9180196 : int i, count = (int)gimple_call_num_args (e->call_stmt);
731 :
732 26113530 : for (i = 0; i < count; i++)
733 : {
734 16933334 : tree cst = gimple_call_arg (e->call_stmt, i);
735 16933334 : if (!is_gimple_min_invariant (cst))
736 : cst = NULL;
737 6021289 : if (cst)
738 : {
739 6021289 : if (!avals->m_known_vals.length ())
740 4145881 : avals->m_known_vals.safe_grow_cleared (count, true);
741 6021289 : avals->m_known_vals[i] = cst;
742 : }
743 : }
744 : }
745 :
746 21819322 : evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr,
747 : nonspec_clause_ptr, es);
748 21819322 : }
749 :
750 :
751 : /* Allocate the function summary. */
752 :
753 : static void
754 459112 : ipa_fn_summary_alloc (void)
755 : {
756 459112 : gcc_checking_assert (!ipa_fn_summaries);
757 459112 : ipa_size_summaries = new ipa_size_summary_t (symtab);
758 459112 : ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab);
759 459112 : ipa_call_summaries = new ipa_call_summary_t (symtab);
760 459112 : }
761 :
762 26989651 : ipa_call_summary::~ipa_call_summary ()
763 : {
764 26989651 : if (predicate)
765 3245042 : edge_predicate_pool.remove (predicate);
766 :
767 26989651 : param.release ();
768 26989651 : }
769 :
770 9942926 : ipa_fn_summary::~ipa_fn_summary ()
771 : {
772 9942926 : unsigned len = vec_safe_length (loop_iterations);
773 10054491 : for (unsigned i = 0; i < len; i++)
774 111565 : edge_predicate_pool.remove ((*loop_iterations)[i].predicate);
775 9942926 : len = vec_safe_length (loop_strides);
776 9976381 : for (unsigned i = 0; i < len; i++)
777 33455 : edge_predicate_pool.remove ((*loop_strides)[i].predicate);
778 9942926 : vec_free (conds);
779 9942926 : call_size_time_table.release ();
780 9942926 : vec_free (loop_iterations);
781 9942926 : vec_free (loop_strides);
782 9942926 : builtin_constant_p_parms.release ();
783 9942926 : }
784 :
785 : void
786 7194017 : ipa_fn_summary_t::remove_callees (cgraph_node *node)
787 : {
788 7194017 : cgraph_edge *e;
789 29524724 : for (e = node->callees; e; e = e->next_callee)
790 22330707 : ipa_call_summaries->remove (e);
791 7671240 : for (e = node->indirect_calls; e; e = e->next_callee)
792 477223 : ipa_call_summaries->remove (e);
793 7194017 : }
794 :
795 : /* Duplicate predicates in loop hint vector, allocating memory for them and
796 : remove and deallocate any uninteresting (true or false) ones. Return the
797 : result. */
798 :
799 : static vec<ipa_freqcounting_predicate, va_gc> *
800 29450 : remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v,
801 : clause_t possible_truths)
802 : {
803 29450 : if (vec_safe_length (v) == 0)
804 : return NULL;
805 :
806 4699 : vec<ipa_freqcounting_predicate, va_gc> *res = v->copy ();
807 4699 : int len = res->length();
808 11001 : for (int i = len - 1; i >= 0; i--)
809 : {
810 6302 : ipa_predicate new_predicate
811 6302 : = (*res)[i].predicate->remap_after_duplication (possible_truths);
812 : /* We do not want to free previous predicate; it is used by node
813 : origin. */
814 6302 : (*res)[i].predicate = NULL;
815 6302 : set_hint_predicate (&(*res)[i].predicate, new_predicate);
816 :
817 6302 : if (!(*res)[i].predicate)
818 3015 : res->unordered_remove (i);
819 : }
820 :
821 : return res;
822 : }
823 :
824 :
825 : /* Hook that is called by cgraph.cc when a node is duplicated. */
826 : void
827 2661602 : ipa_fn_summary_t::duplicate (cgraph_node *src,
828 : cgraph_node *dst,
829 : ipa_fn_summary *src_info,
830 : ipa_fn_summary *info)
831 : {
832 2661602 : new (info) ipa_fn_summary (*src_info);
833 : /* TODO: as an optimization, we may avoid copying conditions
834 : that are known to be false or true. */
835 2661602 : info->conds = vec_safe_copy (info->conds);
836 :
837 2661602 : clone_info *cinfo = clone_info::get (dst);
838 : /* When there are any replacements in the function body, see if we can figure
839 : out that something was optimized out. */
840 2661602 : if (ipa_node_params_sum && cinfo && cinfo->tree_map)
841 : {
842 : /* Use SRC parm info since it may not be copied yet. */
843 14725 : ipa_node_params *parms_info = ipa_node_params_sum->get (src);
844 14725 : ipa_auto_call_arg_values avals;
845 14725 : int count = ipa_get_param_count (parms_info);
846 14725 : int i, j;
847 14725 : clause_t possible_truths;
848 14725 : ipa_predicate true_pred = true;
849 14725 : size_time_entry *e;
850 14725 : int optimized_out_size = 0;
851 14725 : bool inlined_to_p = false;
852 14725 : struct cgraph_edge *edge, *next;
853 :
854 14725 : info->size_time_table.release ();
855 14725 : avals.m_known_vals.safe_grow_cleared (count, true);
856 60047 : for (i = 0; i < count; i++)
857 : {
858 : struct ipa_replace_map *r;
859 :
860 148954 : for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++)
861 : {
862 83984 : if (r->parm_num == i)
863 : {
864 25674 : avals.m_known_vals[i] = r->new_tree;
865 25674 : break;
866 : }
867 : }
868 : }
869 14725 : evaluate_conditions_for_known_args (dst, false,
870 : &avals,
871 : &possible_truths,
872 : /* We are going to specialize,
873 : so ignore nonspec truths. */
874 : NULL,
875 : NULL);
876 :
877 14725 : info->account_size_time (0, 0, true_pred, true_pred);
878 :
879 : /* Remap size_time vectors.
880 : Simplify the predicate by pruning out alternatives that are known
881 : to be false.
882 : TODO: as on optimization, we can also eliminate conditions known
883 : to be true. */
884 104809 : for (i = 0; src_info->size_time_table.iterate (i, &e); i++)
885 : {
886 90084 : ipa_predicate new_exec_pred;
887 90084 : ipa_predicate new_nonconst_pred;
888 90084 : new_exec_pred = e->exec_predicate.remap_after_duplication
889 90084 : (possible_truths);
890 90084 : new_nonconst_pred = e->nonconst_predicate.remap_after_duplication
891 90084 : (possible_truths);
892 90084 : if (new_exec_pred == false || new_nonconst_pred == false)
893 13016 : optimized_out_size += e->size;
894 : else
895 77068 : info->account_size_time (e->size, e->time, new_exec_pred,
896 : new_nonconst_pred);
897 : }
898 :
899 : /* Remap edge predicates with the same simplification as above.
900 : Also copy constantness arrays. */
901 190429 : for (edge = dst->callees; edge; edge = next)
902 : {
903 175704 : ipa_predicate new_predicate;
904 175704 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
905 175704 : next = edge->next_callee;
906 :
907 175704 : if (!edge->inline_failed)
908 0 : inlined_to_p = true;
909 175704 : if (!es->predicate)
910 160190 : continue;
911 15514 : new_predicate = es->predicate->remap_after_duplication
912 15514 : (possible_truths);
913 15514 : if (new_predicate == false && *es->predicate != false)
914 4498 : optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale;
915 15514 : edge_set_predicate (edge, &new_predicate);
916 : }
917 :
918 : /* Remap indirect edge predicates with the same simplification as above.
919 : Also copy constantness arrays. */
920 15629 : for (edge = dst->indirect_calls; edge; edge = next)
921 : {
922 904 : ipa_predicate new_predicate;
923 904 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
924 904 : next = edge->next_callee;
925 :
926 904 : gcc_checking_assert (edge->inline_failed);
927 904 : if (!es->predicate)
928 799 : continue;
929 105 : new_predicate = es->predicate->remap_after_duplication
930 105 : (possible_truths);
931 105 : if (new_predicate == false && *es->predicate != false)
932 24 : optimized_out_size
933 24 : += es->call_stmt_size * ipa_fn_summary::size_scale;
934 105 : edge_set_predicate (edge, &new_predicate);
935 : }
936 14725 : info->loop_iterations
937 14725 : = remap_freqcounting_preds_after_dup (info->loop_iterations,
938 : possible_truths);
939 14725 : info->loop_strides
940 14725 : = remap_freqcounting_preds_after_dup (info->loop_strides,
941 : possible_truths);
942 14725 : if (info->builtin_constant_p_parms.length())
943 : {
944 22 : vec <int, va_heap, vl_ptr> parms = info->builtin_constant_p_parms;
945 22 : int ip;
946 22 : info->builtin_constant_p_parms = vNULL;
947 44 : for (i = 0; parms.iterate (i, &ip); i++)
948 22 : if (!avals.m_known_vals[ip])
949 4 : info->builtin_constant_p_parms.safe_push (ip);
950 : }
951 :
952 : /* If inliner or someone after inliner will ever start producing
953 : non-trivial clones, we will get trouble with lack of information
954 : about updating self sizes, because size vectors already contains
955 : sizes of the callees. */
956 14725 : gcc_assert (!inlined_to_p || !optimized_out_size);
957 14725 : }
958 : else
959 : {
960 2646877 : info->size_time_table = src_info->size_time_table.copy ();
961 2646877 : info->loop_iterations = vec_safe_copy (src_info->loop_iterations);
962 2646877 : info->loop_strides = vec_safe_copy (info->loop_strides);
963 :
964 2646877 : info->builtin_constant_p_parms
965 2646877 : = info->builtin_constant_p_parms.copy ();
966 :
967 2646877 : ipa_freqcounting_predicate *f;
968 2695133 : for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++)
969 : {
970 48256 : ipa_predicate p = *f->predicate;
971 48256 : f->predicate = NULL;
972 48256 : set_hint_predicate (&f->predicate, p);
973 : }
974 2670806 : for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++)
975 : {
976 23929 : ipa_predicate p = *f->predicate;
977 23929 : f->predicate = NULL;
978 23929 : set_hint_predicate (&f->predicate, p);
979 : }
980 : }
981 2661602 : if (!dst->inlined_to)
982 137534 : ipa_update_overall_fn_summary (dst);
983 2661602 : }
984 :
985 :
986 : /* Hook that is called by cgraph.cc when a node is duplicated. */
987 :
988 : void
989 3829012 : ipa_call_summary_t::duplicate (struct cgraph_edge *src,
990 : struct cgraph_edge *dst,
991 : class ipa_call_summary *srcinfo,
992 : class ipa_call_summary *info)
993 : {
994 3829012 : new (info) ipa_call_summary (*srcinfo);
995 3829012 : info->predicate = NULL;
996 3829012 : edge_set_predicate (dst, srcinfo->predicate);
997 3829012 : info->param = srcinfo->param.copy ();
998 3829012 : if (!dst->indirect_unknown_callee && src->indirect_unknown_callee
999 : /* Don't subtract the size when dealing with callback pairs, since the
1000 : edge has no real size. */
1001 22596 : && !src->has_callback && !dst->callback)
1002 : {
1003 22596 : info->call_stmt_size -= (eni_size_weights.indirect_call_cost
1004 22596 : - eni_size_weights.call_cost);
1005 22596 : info->call_stmt_time -= (eni_time_weights.indirect_call_cost
1006 22596 : - eni_time_weights.call_cost);
1007 : }
1008 3829012 : }
1009 :
1010 : /* Dump edge summaries associated to NODE and recursively to all clones.
1011 : Indent by INDENT. */
1012 :
1013 : static void
1014 1984 : dump_ipa_call_summary (FILE *f, int indent, struct cgraph_node *node,
1015 : class ipa_fn_summary *info)
1016 : {
1017 1984 : struct cgraph_edge *edge;
1018 9822 : for (edge = node->callees; edge; edge = edge->next_callee)
1019 : {
1020 7838 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1021 7838 : struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1022 7838 : int i;
1023 :
1024 15676 : fprintf (f,
1025 : "%*s%s %s\n%*s freq:%4.2f",
1026 : indent, "", callee->dump_name (),
1027 7838 : !edge->inline_failed
1028 7284 : ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1029 7838 : indent, "", edge->sreal_frequency ().to_double ());
1030 :
1031 7838 : if (cross_module_call_p (edge))
1032 4 : fprintf (f, " cross module");
1033 :
1034 7838 : if (es)
1035 7284 : fprintf (f, " loop depth:%2i size:%2i time: %2i",
1036 : es->loop_depth, es->call_stmt_size, es->call_stmt_time);
1037 :
1038 7838 : ipa_fn_summary *s = ipa_fn_summaries->get (callee);
1039 7838 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1040 7838 : if (s != NULL)
1041 623 : fprintf (f, " callee size:%2i stack:%2i",
1042 623 : (int) (ss->size / ipa_fn_summary::size_scale),
1043 623 : (int) s->estimated_stack_size);
1044 :
1045 7838 : if (es && es->predicate)
1046 : {
1047 4627 : fprintf (f, " predicate: ");
1048 4627 : es->predicate->dump (f, info->conds);
1049 : }
1050 : else
1051 3211 : fprintf (f, "\n");
1052 7838 : if (es && es->param.exists ())
1053 5390 : for (i = 0; i < (int) es->param.length (); i++)
1054 : {
1055 1590 : int prob = es->param[i].change_prob;
1056 :
1057 1590 : if (!prob)
1058 606 : fprintf (f, "%*s op%i is compile time invariant\n",
1059 : indent + 2, "", i);
1060 984 : else if (prob != REG_BR_PROB_BASE)
1061 35 : fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1062 35 : prob * 100.0 / REG_BR_PROB_BASE);
1063 1590 : if (es->param[i].points_to_local_or_readonly_memory)
1064 406 : fprintf (f, "%*s op%i points to local or readonly memory\n",
1065 : indent + 2, "", i);
1066 1590 : if (es->param[i].points_to_possible_sra_candidate)
1067 225 : fprintf (f, "%*s op%i points to possible sra candidate\n",
1068 : indent + 2, "", i);
1069 : }
1070 7838 : if (!edge->inline_failed)
1071 : {
1072 554 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1073 554 : fprintf (f, "%*sStack frame offset %i, callee self size %i\n",
1074 : indent + 2, "",
1075 554 : (int) ipa_get_stack_frame_offset (callee),
1076 554 : (int) ss->estimated_self_stack_size);
1077 554 : dump_ipa_call_summary (f, indent + 2, callee, info);
1078 : }
1079 : }
1080 2282 : for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1081 : {
1082 298 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1083 298 : fprintf (f, "%*sindirect call loop depth:%2i freq:%4.2f size:%2i"
1084 : " time: %2i",
1085 : indent, "",
1086 : es->loop_depth,
1087 298 : edge->sreal_frequency ().to_double (), es->call_stmt_size,
1088 : es->call_stmt_time);
1089 298 : if (es->predicate)
1090 : {
1091 6 : fprintf (f, "predicate: ");
1092 6 : es->predicate->dump (f, info->conds);
1093 : }
1094 : else
1095 292 : fprintf (f, "\n");
1096 : }
1097 1984 : }
1098 :
1099 :
1100 : void
1101 1633 : ipa_dump_fn_summary (FILE *f, struct cgraph_node *node)
1102 : {
1103 1633 : if (node->definition)
1104 : {
1105 1633 : class ipa_fn_summary *s = ipa_fn_summaries->get (node);
1106 1633 : class ipa_size_summary *ss = ipa_size_summaries->get (node);
1107 1633 : if (s != NULL)
1108 : {
1109 1430 : size_time_entry *e;
1110 1430 : int i;
1111 1430 : fprintf (f, "IPA function summary for %s", node->dump_name ());
1112 1430 : if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1113 0 : fprintf (f, " always_inline");
1114 1430 : if (s->inlinable)
1115 1243 : fprintf (f, " inlinable");
1116 1430 : if (s->fp_expressions)
1117 45 : fprintf (f, " fp_expression");
1118 1430 : if (s->builtin_constant_p_parms.length ())
1119 : {
1120 2 : fprintf (f, " builtin_constant_p_parms");
1121 4 : for (unsigned int i = 0;
1122 4 : i < s->builtin_constant_p_parms.length (); i++)
1123 2 : fprintf (f, " %i", s->builtin_constant_p_parms[i]);
1124 : }
1125 1430 : fprintf (f, "\n global time: %f\n", s->time.to_double ());
1126 1430 : fprintf (f, " self size: %i\n", ss->self_size);
1127 1430 : fprintf (f, " global size: %i\n", ss->size);
1128 1430 : fprintf (f, " min size: %i\n", s->min_size);
1129 1430 : fprintf (f, " self stack: %i\n",
1130 1430 : (int) ss->estimated_self_stack_size);
1131 1430 : fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1132 1430 : if (s->growth)
1133 161 : fprintf (f, " estimated growth:%i\n", (int) s->growth);
1134 1430 : if (s->scc_no)
1135 5 : fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1136 5972 : for (i = 0; s->size_time_table.iterate (i, &e); i++)
1137 : {
1138 4542 : fprintf (f, " size:%f, time:%f",
1139 4542 : (double) e->size / ipa_fn_summary::size_scale,
1140 : e->time.to_double ());
1141 4542 : if (e->exec_predicate != true)
1142 : {
1143 2655 : fprintf (f, ", executed if:");
1144 2655 : e->exec_predicate.dump (f, s->conds, 0);
1145 : }
1146 4542 : if (e->exec_predicate != e->nonconst_predicate)
1147 : {
1148 1187 : fprintf (f, ", nonconst if:");
1149 1187 : e->nonconst_predicate.dump (f, s->conds, 0);
1150 : }
1151 4542 : fprintf (f, "\n");
1152 : }
1153 : ipa_freqcounting_predicate *fcp;
1154 : bool first_fcp = true;
1155 1542 : for (int i = 0; vec_safe_iterate (s->loop_iterations, i, &fcp); i++)
1156 : {
1157 112 : if (first_fcp)
1158 : {
1159 79 : fprintf (f, " loop iterations:");
1160 79 : first_fcp = false;
1161 : }
1162 112 : fprintf (f, " %3.2f for ", fcp->freq.to_double ());
1163 112 : fcp->predicate->dump (f, s->conds);
1164 : }
1165 : first_fcp = true;
1166 1450 : for (int i = 0; vec_safe_iterate (s->loop_strides, i, &fcp); i++)
1167 : {
1168 20 : if (first_fcp)
1169 : {
1170 11 : fprintf (f, " loop strides:");
1171 11 : first_fcp = false;
1172 : }
1173 20 : fprintf (f, " %3.2f for :", fcp->freq.to_double ());
1174 20 : fcp->predicate->dump (f, s->conds);
1175 : }
1176 1430 : fprintf (f, " calls:\n");
1177 1430 : dump_ipa_call_summary (f, 4, node, s);
1178 1430 : fprintf (f, "\n");
1179 1430 : if (s->target_info)
1180 0 : fprintf (f, " target_info: %x\n", s->target_info);
1181 : }
1182 : else
1183 203 : fprintf (f, "IPA summary for %s is missing.\n", node->dump_name ());
1184 : }
1185 1633 : }
1186 :
1187 : DEBUG_FUNCTION void
1188 0 : ipa_debug_fn_summary (struct cgraph_node *node)
1189 : {
1190 0 : ipa_dump_fn_summary (stderr, node);
1191 0 : }
1192 :
1193 : void
1194 356 : ipa_dump_fn_summaries (FILE *f)
1195 : {
1196 356 : struct cgraph_node *node;
1197 :
1198 2296 : FOR_EACH_DEFINED_FUNCTION (node)
1199 1940 : if (!node->inlined_to)
1200 1386 : ipa_dump_fn_summary (f, node);
1201 356 : }
1202 :
1203 : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1204 : boolean variable pointed to by DATA. */
1205 :
1206 : static bool
1207 797836 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1208 : void *data)
1209 : {
1210 797836 : bool *b = (bool *) data;
1211 797836 : *b = true;
1212 797836 : return true;
1213 : }
1214 :
1215 : /* If OP refers to value of function parameter, return the corresponding
1216 : parameter. If non-NULL, the size of the memory load (or the SSA_NAME of the
1217 : PARM_DECL) will be stored to *SIZE_P in that case too. */
1218 :
1219 : static tree
1220 183165730 : unmodified_parm_1 (ipa_func_body_info *fbi, gimple *stmt, tree op,
1221 : poly_int64 *size_p)
1222 : {
1223 : /* SSA_NAME referring to parm default def? */
1224 183165730 : if (TREE_CODE (op) == SSA_NAME
1225 107188036 : && SSA_NAME_IS_DEFAULT_DEF (op)
1226 208423063 : && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1227 : {
1228 25057688 : if (size_p)
1229 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1230 25057688 : return SSA_NAME_VAR (op);
1231 : }
1232 : /* Non-SSA parm reference? */
1233 158108042 : if (TREE_CODE (op) == PARM_DECL
1234 1447556 : && fbi->aa_walk_budget > 0)
1235 : {
1236 1445010 : bool modified = false;
1237 :
1238 1445010 : ao_ref refd;
1239 1445010 : ao_ref_init (&refd, op);
1240 2890020 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
1241 : mark_modified, &modified, NULL, NULL,
1242 : fbi->aa_walk_budget);
1243 1445010 : if (walked < 0)
1244 : {
1245 8 : fbi->aa_walk_budget = 0;
1246 931192 : return NULL_TREE;
1247 : }
1248 1445002 : fbi->aa_walk_budget -= walked;
1249 1445002 : if (!modified)
1250 : {
1251 931184 : if (size_p)
1252 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1253 931184 : return op;
1254 : }
1255 : }
1256 : return NULL_TREE;
1257 : }
1258 :
1259 : /* If OP refers to value of function parameter, return the corresponding
1260 : parameter. Also traverse chains of SSA register assignments. If non-NULL,
1261 : the size of the memory load (or the SSA_NAME of the PARM_DECL) will be
1262 : stored to *SIZE_P in that case too. */
1263 :
1264 : static tree
1265 119623056 : unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op,
1266 : poly_int64 *size_p)
1267 : {
1268 148893784 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1269 148893784 : if (res)
1270 : return res;
1271 :
1272 123714404 : if (TREE_CODE (op) == SSA_NAME
1273 70241145 : && !SSA_NAME_IS_DEFAULT_DEF (op)
1274 193760405 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1275 29270728 : return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op),
1276 29270728 : gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)),
1277 29270728 : size_p);
1278 : return NULL_TREE;
1279 : }
1280 :
1281 : /* If OP refers to a value of a function parameter or value loaded from an
1282 : aggregate passed to a parameter (either by value or reference), return TRUE
1283 : and store the number of the parameter to *INDEX_P, the access size into
1284 : *SIZE_P, and information whether and how it has been loaded from an
1285 : aggregate into *AGGPOS. INFO describes the function parameters, STMT is the
1286 : statement in which OP is used or loaded. */
1287 :
1288 : static bool
1289 32823819 : unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi,
1290 : gimple *stmt, tree op, int *index_p,
1291 : poly_int64 *size_p,
1292 : struct agg_position_info *aggpos)
1293 : {
1294 34271946 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1295 :
1296 34271946 : gcc_checking_assert (aggpos);
1297 34271946 : if (res)
1298 : {
1299 809492 : *index_p = ipa_get_param_decl_index (fbi->info, res);
1300 809492 : if (*index_p < 0)
1301 : return false;
1302 809362 : aggpos->agg_contents = false;
1303 809362 : aggpos->by_ref = false;
1304 809362 : return true;
1305 : }
1306 :
1307 33462454 : if (TREE_CODE (op) == SSA_NAME)
1308 : {
1309 11889203 : if (SSA_NAME_IS_DEFAULT_DEF (op)
1310 11889203 : || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1311 : return false;
1312 4797542 : stmt = SSA_NAME_DEF_STMT (op);
1313 4797542 : op = gimple_assign_rhs1 (stmt);
1314 4797542 : if (!REFERENCE_CLASS_P (op))
1315 : return unmodified_parm_or_parm_agg_item (fbi, stmt, op, index_p, size_p,
1316 : aggpos);
1317 : }
1318 :
1319 24922666 : aggpos->agg_contents = true;
1320 24922666 : return ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
1321 : stmt, op, index_p, &aggpos->offset,
1322 24922666 : size_p, &aggpos->by_ref);
1323 : }
1324 :
1325 : /* If stmt is simple load or store of value pointed to by a function parmaeter,
1326 : return its index. */
1327 :
1328 : static int
1329 110972374 : load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt)
1330 : {
1331 110972374 : if (!optimize)
1332 : return -1;
1333 159031823 : gassign *assign = dyn_cast <gassign *> (stmt);
1334 55245067 : if (!assign)
1335 : return -1;
1336 55245067 : tree param;
1337 55245067 : if (gimple_assign_load_p (stmt))
1338 20299204 : param = gimple_assign_rhs1 (stmt);
1339 34945863 : else if (gimple_store_p (stmt))
1340 18546491 : param = gimple_assign_lhs (stmt);
1341 : else
1342 : return -1;
1343 38845695 : tree base = get_base_address (param);
1344 38845695 : if (TREE_CODE (base) != MEM_REF
1345 13467602 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1346 52309193 : || !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1347 : return -1;
1348 7270450 : tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1349 7270450 : if (TREE_CODE (p) != PARM_DECL)
1350 : return -1;
1351 7185618 : return ipa_get_param_decl_index (fbi->info, p);
1352 : }
1353 :
1354 : /* See if statement might disappear after inlining.
1355 : 0 - means not eliminated
1356 : 1 - half of statements goes away
1357 : 2 - for sure it is eliminated.
1358 : We are not terribly sophisticated, basically looking for simple abstraction
1359 : penalty wrappers. */
1360 :
1361 : static int
1362 110972374 : eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt)
1363 : {
1364 110972374 : enum gimple_code code = gimple_code (stmt);
1365 110972374 : enum tree_code rhs_code;
1366 :
1367 110972374 : if (!optimize)
1368 : return 0;
1369 :
1370 92862313 : switch (code)
1371 : {
1372 : case GIMPLE_RETURN:
1373 : return 2;
1374 55245067 : case GIMPLE_ASSIGN:
1375 55245067 : if (gimple_num_ops (stmt) != 2)
1376 : return 0;
1377 :
1378 39648881 : rhs_code = gimple_assign_rhs_code (stmt);
1379 :
1380 : /* Casts of parameters, loads from parameters passed by reference
1381 : and stores to return value or parameters are often free after
1382 : inlining due to SRA and further combining.
1383 : Assume that half of statements goes away. */
1384 39648881 : if (CONVERT_EXPR_CODE_P (rhs_code)
1385 : || rhs_code == VIEW_CONVERT_EXPR
1386 : || rhs_code == ADDR_EXPR
1387 36868888 : || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1388 : {
1389 38845695 : tree rhs = gimple_assign_rhs1 (stmt);
1390 38845695 : tree lhs = gimple_assign_lhs (stmt);
1391 38845695 : tree inner_rhs = get_base_address (rhs);
1392 38845695 : tree inner_lhs = get_base_address (lhs);
1393 38845695 : bool rhs_free = false;
1394 38845695 : bool lhs_free = false;
1395 :
1396 38845695 : if (!inner_rhs)
1397 0 : inner_rhs = rhs;
1398 38845695 : if (!inner_lhs)
1399 0 : inner_lhs = lhs;
1400 :
1401 : /* Reads of parameter are expected to be free. */
1402 38845695 : if (unmodified_parm (fbi, stmt, inner_rhs, NULL))
1403 : rhs_free = true;
1404 : /* Match expressions of form &this->field. Those will most likely
1405 : combine with something upstream after inlining. */
1406 37580783 : else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1407 : {
1408 2481544 : tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1409 2481544 : if (TREE_CODE (op) == PARM_DECL)
1410 : rhs_free = true;
1411 2443474 : else if (TREE_CODE (op) == MEM_REF
1412 2443474 : && unmodified_parm (fbi, stmt, TREE_OPERAND (op, 0),
1413 : NULL))
1414 : rhs_free = true;
1415 : }
1416 :
1417 : /* When parameter is not SSA register because its address is taken
1418 : and it is just copied into one, the statement will be completely
1419 : free after inlining (we will copy propagate backward). */
1420 1302982 : if (rhs_free && is_gimple_reg (lhs))
1421 : return 2;
1422 :
1423 : /* Reads of parameters passed by reference
1424 : expected to be free (i.e. optimized out after inlining). */
1425 38170903 : if (TREE_CODE (inner_rhs) == MEM_REF
1426 38170903 : && unmodified_parm (fbi, stmt, TREE_OPERAND (inner_rhs, 0), NULL))
1427 : rhs_free = true;
1428 :
1429 : /* Copying parameter passed by reference into gimple register is
1430 : probably also going to copy propagate, but we can't be quite
1431 : sure. */
1432 38170903 : if (rhs_free && is_gimple_reg (lhs))
1433 : lhs_free = true;
1434 :
1435 : /* Writes to parameters, parameters passed by value and return value
1436 : (either directly or passed via invisible reference) are free.
1437 :
1438 : TODO: We ought to handle testcase like
1439 : struct a {int a,b;};
1440 : struct a
1441 : returnstruct (void)
1442 : {
1443 : struct a a ={1,2};
1444 : return a;
1445 : }
1446 :
1447 : This translate into:
1448 :
1449 : returnstruct ()
1450 : {
1451 : int a$b;
1452 : int a$a;
1453 : struct a a;
1454 : struct a D.2739;
1455 :
1456 : <bb 2>:
1457 : D.2739.a = 1;
1458 : D.2739.b = 2;
1459 : return D.2739;
1460 :
1461 : }
1462 : For that we either need to copy ipa-split logic detecting writes
1463 : to return value. */
1464 38170903 : if (TREE_CODE (inner_lhs) == PARM_DECL
1465 38081134 : || TREE_CODE (inner_lhs) == RESULT_DECL
1466 75414525 : || (TREE_CODE (inner_lhs) == MEM_REF
1467 5038437 : && (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0),
1468 : NULL)
1469 2591285 : || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1470 2590096 : && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1471 356908 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1472 : (inner_lhs,
1473 : 0))) == RESULT_DECL))))
1474 : lhs_free = true;
1475 34714881 : if (lhs_free
1476 38170903 : && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1477 : rhs_free = true;
1478 38170903 : if (lhs_free && rhs_free)
1479 : return 1;
1480 : }
1481 : return 0;
1482 : default:
1483 : return 0;
1484 : }
1485 : }
1486 :
1487 : /* Analyze EXPR if it represents a series of simple operations performed on
1488 : a function parameter and return true if so. FBI, STMT, EXPR, INDEX_P and
1489 : AGGPOS have the same meaning like in unmodified_parm_or_parm_agg_item.
1490 : Type of the parameter or load from an aggregate via the parameter is
1491 : stored in *TYPE_P. Operations on the parameter are recorded to
1492 : PARAM_OPS_P if it is not NULL. */
1493 :
1494 : static bool
1495 27033493 : decompose_param_expr (struct ipa_func_body_info *fbi,
1496 : gimple *stmt, tree expr,
1497 : int *index_p, tree *type_p,
1498 : struct agg_position_info *aggpos,
1499 : expr_eval_ops *param_ops_p = NULL)
1500 : {
1501 27033493 : int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops);
1502 27033493 : int op_count = 0;
1503 :
1504 27033493 : if (param_ops_p)
1505 9251383 : *param_ops_p = NULL;
1506 :
1507 32823819 : while (true)
1508 : {
1509 32823819 : expr_eval_op eval_op;
1510 32823819 : unsigned rhs_count;
1511 32823819 : unsigned cst_count = 0;
1512 :
1513 32823819 : if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL,
1514 : aggpos))
1515 : {
1516 4569383 : tree type = TREE_TYPE (expr);
1517 :
1518 4569383 : if (aggpos->agg_contents)
1519 : {
1520 : /* Stop if containing bit-field. */
1521 3760021 : if (TREE_CODE (expr) == BIT_FIELD_REF
1522 3760021 : || contains_bitfld_component_ref_p (expr))
1523 : break;
1524 : }
1525 :
1526 4546543 : *type_p = type;
1527 4546543 : return true;
1528 : }
1529 :
1530 28254436 : if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr))
1531 : break;
1532 10556630 : stmt = SSA_NAME_DEF_STMT (expr);
1533 :
1534 10556630 : if (gcall *call = dyn_cast <gcall *> (stmt))
1535 : {
1536 2490758 : int flags = gimple_call_return_flags (call);
1537 2490758 : if (!(flags & ERF_RETURNS_ARG))
1538 3086628 : goto fail;
1539 209428 : int arg = flags & ERF_RETURN_ARG_MASK;
1540 209428 : if (arg >= (int)gimple_call_num_args (call))
1541 0 : goto fail;
1542 209428 : expr = gimple_call_arg (stmt, arg);
1543 4016499 : continue;
1544 209428 : }
1545 :
1546 8065872 : if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr)))
1547 : break;
1548 :
1549 6386271 : switch (gimple_assign_rhs_class (stmt))
1550 : {
1551 3807071 : case GIMPLE_SINGLE_RHS:
1552 3807071 : expr = gimple_assign_rhs1 (stmt);
1553 3807071 : continue;
1554 :
1555 : case GIMPLE_UNARY_RHS:
1556 : rhs_count = 1;
1557 : break;
1558 :
1559 1554787 : case GIMPLE_BINARY_RHS:
1560 1554787 : rhs_count = 2;
1561 1554787 : break;
1562 :
1563 642 : case GIMPLE_TERNARY_RHS:
1564 642 : rhs_count = 3;
1565 642 : break;
1566 :
1567 0 : default:
1568 0 : goto fail;
1569 : }
1570 :
1571 : /* Stop if expression is too complex. */
1572 2579200 : if (op_count++ == op_limit)
1573 : break;
1574 :
1575 2579125 : if (param_ops_p)
1576 : {
1577 2562473 : eval_op.code = gimple_assign_rhs_code (stmt);
1578 2562473 : eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt));
1579 2562473 : eval_op.val[0] = NULL_TREE;
1580 2562473 : eval_op.val[1] = NULL_TREE;
1581 : }
1582 :
1583 : expr = NULL_TREE;
1584 5908395 : for (unsigned i = 0; i < rhs_count; i++)
1585 : {
1586 4134568 : tree op = gimple_op (stmt, i + 1);
1587 :
1588 4134568 : gcc_assert (op && !TYPE_P (op));
1589 4134568 : if (is_gimple_ip_invariant (op))
1590 : {
1591 752439 : if (++cst_count == rhs_count)
1592 1147 : goto fail;
1593 :
1594 751292 : eval_op.val[cst_count - 1] = op;
1595 : }
1596 3382129 : else if (!expr)
1597 : {
1598 : /* Found a non-constant operand, and record its index in rhs
1599 : operands. */
1600 2577978 : eval_op.index = i;
1601 2577978 : expr = op;
1602 : }
1603 : else
1604 : {
1605 : /* Found more than one non-constant operands. */
1606 804151 : goto fail;
1607 : }
1608 : }
1609 :
1610 1773827 : if (param_ops_p)
1611 1757620 : vec_safe_insert (*param_ops_p, 0, eval_op);
1612 : }
1613 :
1614 : /* Failed to decompose, free resource and return. */
1615 22486950 : fail:
1616 22486950 : if (param_ops_p)
1617 9069830 : vec_free (*param_ops_p);
1618 :
1619 : return false;
1620 : }
1621 :
1622 : /* Record to SUMMARY that PARM is used by builtin_constant_p. */
1623 :
1624 : static void
1625 10700 : add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm)
1626 : {
1627 10700 : int ip;
1628 :
1629 : /* Avoid duplicates. */
1630 10738 : for (unsigned int i = 0;
1631 10738 : summary->builtin_constant_p_parms.iterate (i, &ip); i++)
1632 5160 : if (ip == parm)
1633 10700 : return;
1634 5578 : summary->builtin_constant_p_parms.safe_push (parm);
1635 : }
1636 :
1637 : /* If BB ends by a conditional we can turn into predicates, attach corresponding
1638 : predicates to the CFG edges. */
1639 :
1640 : static void
1641 34951224 : set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1642 : class ipa_fn_summary *summary,
1643 : class ipa_node_params *params_summary,
1644 : basic_block bb)
1645 : {
1646 34951224 : tree op, op2;
1647 34951224 : int index;
1648 34951224 : struct agg_position_info aggpos;
1649 34951224 : enum tree_code code, inverted_code;
1650 34951224 : edge e;
1651 34951224 : edge_iterator ei;
1652 34951224 : gimple *set_stmt;
1653 34951224 : tree param_type;
1654 34951224 : expr_eval_ops param_ops;
1655 :
1656 69902448 : gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1657 11586796 : if (!last)
1658 34940069 : return;
1659 11586796 : if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1660 : return;
1661 9172854 : op = gimple_cond_lhs (last);
1662 :
1663 9172854 : if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1664 : ¶m_ops))
1665 : {
1666 1295673 : code = gimple_cond_code (last);
1667 1295673 : inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1668 :
1669 3887019 : FOR_EACH_EDGE (e, ei, bb->succs)
1670 : {
1671 1295673 : enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1672 2591346 : ? code : inverted_code);
1673 : /* invert_tree_comparison will return ERROR_MARK on FP
1674 : comparisons that are not EQ/NE instead of returning proper
1675 : unordered one. Be sure it is not confused with NON_CONSTANT.
1676 :
1677 : And if the edge's target is the final block of diamond CFG graph
1678 : of this conditional statement, we do not need to compute
1679 : predicate for the edge because the final block's predicate must
1680 : be at least as that of the first block of the statement. */
1681 2591346 : if (this_code != ERROR_MARK
1682 2591346 : && !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1683 : {
1684 2183532 : ipa_predicate p
1685 2183532 : = add_condition (summary, params_summary, index,
1686 : param_type, &aggpos,
1687 : this_code, gimple_cond_rhs (last), param_ops);
1688 2183532 : e->aux = edge_predicate_pool.allocate ();
1689 2183532 : *(ipa_predicate *) e->aux = p;
1690 : }
1691 : }
1692 1295673 : vec_free (param_ops);
1693 1295673 : return;
1694 : }
1695 :
1696 7877181 : if (TREE_CODE (op) != SSA_NAME)
1697 : return;
1698 : /* Special case
1699 : if (builtin_constant_p (op))
1700 : constant_code
1701 : else
1702 : nonconstant_code.
1703 : Here we can predicate nonconstant_code. We can't
1704 : really handle constant_code since we have no predicate
1705 : for this and also the constant code is not known to be
1706 : optimized away when inliner doesn't see operand is constant.
1707 : Other optimizers might think otherwise. */
1708 7876110 : if (gimple_cond_code (last) != NE_EXPR
1709 7876110 : || !integer_zerop (gimple_cond_rhs (last)))
1710 4458149 : return;
1711 3417961 : set_stmt = SSA_NAME_DEF_STMT (op);
1712 3417961 : if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1713 3417961 : || gimple_call_num_args (set_stmt) != 1)
1714 : return;
1715 27664 : op2 = gimple_call_arg (set_stmt, 0);
1716 27664 : if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos))
1717 : return;
1718 11155 : if (!aggpos.by_ref)
1719 10664 : add_builtin_constant_p_parm (summary, index);
1720 33465 : FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1721 : {
1722 11155 : ipa_predicate p = add_condition (summary, params_summary, index,
1723 : param_type, &aggpos,
1724 : ipa_predicate::is_not_constant, NULL_TREE);
1725 11155 : e->aux = edge_predicate_pool.allocate ();
1726 11155 : *(ipa_predicate *) e->aux = p;
1727 : }
1728 : }
1729 :
1730 :
1731 : /* If BB ends by a switch we can turn into predicates, attach corresponding
1732 : predicates to the CFG edges. */
1733 :
1734 : static void
1735 34951224 : set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1736 : class ipa_fn_summary *summary,
1737 : class ipa_node_params *params_summary,
1738 : basic_block bb)
1739 : {
1740 34951224 : tree op;
1741 34951224 : int index;
1742 34951224 : struct agg_position_info aggpos;
1743 34951224 : edge e;
1744 34951224 : edge_iterator ei;
1745 34951224 : size_t n;
1746 34951224 : size_t case_idx;
1747 34951224 : tree param_type;
1748 34951224 : expr_eval_ops param_ops;
1749 :
1750 69902448 : gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
1751 78529 : if (!last)
1752 34922224 : return;
1753 78529 : op = gimple_switch_index (last);
1754 78529 : if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1755 : ¶m_ops))
1756 : return;
1757 :
1758 43971 : auto_vec<std::pair<tree, tree> > ranges;
1759 43971 : tree type = TREE_TYPE (op);
1760 43971 : int bound_limit = opt_for_fn (fbi->node->decl,
1761 : param_ipa_max_switch_predicate_bounds);
1762 43971 : int bound_count = 0;
1763 : // This can safely be an integer range, as switches can only hold
1764 : // integers.
1765 43971 : int_range<2> vr;
1766 :
1767 87942 : get_range_query (cfun)->range_of_expr (vr, op);
1768 43971 : if (vr.undefined_p ())
1769 0 : vr.set_varying (TREE_TYPE (op));
1770 43971 : tree vr_min, vr_max;
1771 : // TODO: This entire function could use a rewrite to use the irange
1772 : // API, instead of trying to recreate its intersection/union logic.
1773 : // Any use of get_legacy_range() is a serious code smell.
1774 43971 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
1775 43971 : wide_int vr_wmin = wi::to_wide (vr_min);
1776 43971 : wide_int vr_wmax = wi::to_wide (vr_max);
1777 :
1778 303816 : FOR_EACH_EDGE (e, ei, bb->succs)
1779 : {
1780 259845 : e->aux = edge_predicate_pool.allocate ();
1781 259845 : *(ipa_predicate *) e->aux = false;
1782 : }
1783 :
1784 43971 : e = gimple_switch_edge (cfun, last, 0);
1785 : /* Set BOUND_COUNT to maximum count to bypass computing predicate for
1786 : default case if its target basic block is in convergence point of all
1787 : switch cases, which can be determined by checking whether it
1788 : post-dominates the switch statement. */
1789 43971 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1790 12599 : bound_count = INT_MAX;
1791 :
1792 43971 : n = gimple_switch_num_labels (last);
1793 290286 : for (case_idx = 1; case_idx < n; ++case_idx)
1794 : {
1795 246315 : tree cl = gimple_switch_label (last, case_idx);
1796 246315 : tree min = CASE_LOW (cl);
1797 246315 : tree max = CASE_HIGH (cl);
1798 246315 : ipa_predicate p;
1799 :
1800 246315 : e = gimple_switch_edge (cfun, last, case_idx);
1801 :
1802 : /* The case value might not have same type as switch expression,
1803 : extend the value based on the expression type. */
1804 246315 : if (TREE_TYPE (min) != type)
1805 43891 : min = wide_int_to_tree (type, wi::to_wide (min));
1806 :
1807 246315 : if (!max)
1808 : max = min;
1809 11950 : else if (TREE_TYPE (max) != type)
1810 3297 : max = wide_int_to_tree (type, wi::to_wide (max));
1811 :
1812 : /* The case's target basic block is in convergence point of all switch
1813 : cases, its predicate should be at least as that of the switch
1814 : statement. */
1815 246315 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1816 4626 : p = true;
1817 241689 : else if (min == max)
1818 231275 : p = add_condition (summary, params_summary, index, param_type,
1819 : &aggpos, EQ_EXPR, min, param_ops);
1820 : else
1821 : {
1822 10414 : ipa_predicate p1, p2;
1823 10414 : p1 = add_condition (summary, params_summary, index, param_type,
1824 : &aggpos, GE_EXPR, min, param_ops);
1825 10414 : p2 = add_condition (summary, params_summary,index, param_type,
1826 : &aggpos, LE_EXPR, max, param_ops);
1827 10414 : p = p1 & p2;
1828 : }
1829 246315 : *(ipa_predicate *) e->aux
1830 246315 : = p.or_with (summary->conds, *(ipa_predicate *) e->aux);
1831 :
1832 : /* If there are too many disjoint case ranges, predicate for default
1833 : case might become too complicated. So add a limit here. */
1834 246315 : if (bound_count > bound_limit)
1835 96236 : continue;
1836 :
1837 150079 : bool new_range = true;
1838 :
1839 150079 : if (!ranges.is_empty ())
1840 : {
1841 118707 : wide_int curr_wmin = wi::to_wide (min);
1842 118707 : wide_int last_wmax = wi::to_wide (ranges.last ().second);
1843 :
1844 : /* Merge case ranges if they are continuous. */
1845 118707 : if (curr_wmin == last_wmax + 1)
1846 : new_range = false;
1847 40680 : else if (vr_type == VR_ANTI_RANGE)
1848 : {
1849 : /* If two disjoint case ranges can be connected by anti-range
1850 : of switch index, combine them to one range. */
1851 79 : if (wi::lt_p (vr_wmax, curr_wmin - 1, TYPE_SIGN (type)))
1852 : vr_type = VR_UNDEFINED;
1853 0 : else if (wi::le_p (vr_wmin, last_wmax + 1, TYPE_SIGN (type)))
1854 0 : new_range = false;
1855 : }
1856 118707 : }
1857 :
1858 : /* Create/extend a case range. And we count endpoints of range set,
1859 : this number nearly equals to number of conditions that we will create
1860 : for predicate of default case. */
1861 118707 : if (new_range)
1862 : {
1863 72052 : bound_count += (min == max) ? 1 : 2;
1864 72052 : ranges.safe_push (std::make_pair (min, max));
1865 : }
1866 : else
1867 : {
1868 78027 : bound_count += (ranges.last ().first == ranges.last ().second);
1869 78027 : ranges.last ().second = max;
1870 : }
1871 : }
1872 :
1873 43971 : e = gimple_switch_edge (cfun, last, 0);
1874 43971 : if (bound_count > bound_limit)
1875 : {
1876 14971 : *(ipa_predicate *) e->aux = true;
1877 14971 : vec_free (param_ops);
1878 14971 : return;
1879 : }
1880 :
1881 29000 : ipa_predicate p_seg = true;
1882 29000 : ipa_predicate p_all = false;
1883 :
1884 29000 : if (vr_type != VR_RANGE)
1885 : {
1886 28479 : vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type));
1887 28479 : vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type));
1888 : }
1889 :
1890 : /* Construct predicate to represent default range set that is negation of
1891 : all case ranges. Case range is classified as containing single/non-single
1892 : values. Suppose a piece of case ranges in the following.
1893 :
1894 : [D1...D2] [S1] ... [Sn] [D3...D4]
1895 :
1896 : To represent default case's range sets between two non-single value
1897 : case ranges (From D2 to D3), we construct predicate as:
1898 :
1899 : D2 < x < D3 && x != S1 && ... && x != Sn
1900 : */
1901 89294 : for (size_t i = 0; i < ranges.length (); i++)
1902 : {
1903 60389 : tree min = ranges[i].first;
1904 60389 : tree max = ranges[i].second;
1905 :
1906 60389 : if (min == max)
1907 74756 : p_seg &= add_condition (summary, params_summary, index,
1908 : param_type, &aggpos, NE_EXPR,
1909 37378 : min, param_ops);
1910 : else
1911 : {
1912 : /* Do not create sub-predicate for range that is beyond low bound
1913 : of switch index. */
1914 23011 : if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type)))
1915 : {
1916 16073 : p_seg &= add_condition (summary, params_summary, index,
1917 : param_type, &aggpos,
1918 16073 : LT_EXPR, min, param_ops);
1919 16073 : p_all = p_all.or_with (summary->conds, p_seg);
1920 : }
1921 :
1922 : /* Do not create sub-predicate for range that is beyond up bound
1923 : of switch index. */
1924 23011 : if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type)))
1925 : {
1926 95 : p_seg = false;
1927 95 : break;
1928 : }
1929 :
1930 22916 : p_seg = add_condition (summary, params_summary, index,
1931 : param_type, &aggpos, GT_EXPR,
1932 : max, param_ops);
1933 : }
1934 : }
1935 :
1936 29000 : p_all = p_all.or_with (summary->conds, p_seg);
1937 29000 : *(ipa_predicate *) e->aux
1938 29000 : = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux);
1939 :
1940 35400 : vec_free (param_ops);
1941 43971 : }
1942 :
1943 :
1944 : /* For each BB in NODE attach to its AUX pointer predicate under
1945 : which it is executable. */
1946 :
1947 : static void
1948 6296988 : compute_bb_predicates (struct ipa_func_body_info *fbi,
1949 : struct cgraph_node *node,
1950 : class ipa_fn_summary *summary,
1951 : class ipa_node_params *params_summary)
1952 : {
1953 6296988 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1954 6296988 : bool done = false;
1955 6296988 : basic_block bb;
1956 :
1957 41248212 : FOR_EACH_BB_FN (bb, my_function)
1958 : {
1959 34951224 : set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb);
1960 34951224 : set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb);
1961 : }
1962 :
1963 : /* Entry block is always executable. */
1964 6296988 : ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1965 6296988 : = edge_predicate_pool.allocate ();
1966 6296988 : *(ipa_predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux = true;
1967 :
1968 : /* A simple dataflow propagation of predicates forward in the CFG.
1969 : TODO: work in reverse postorder. */
1970 19009955 : while (!done)
1971 : {
1972 12712967 : done = true;
1973 87721318 : FOR_EACH_BB_FN (bb, my_function)
1974 : {
1975 75008351 : ipa_predicate p = false;
1976 75008351 : edge e;
1977 75008351 : edge_iterator ei;
1978 92917340 : FOR_EACH_EDGE (e, ei, bb->preds)
1979 : {
1980 79687403 : if (e->src->aux)
1981 : {
1982 77939319 : ipa_predicate this_bb_predicate
1983 : = *(ipa_predicate *) e->src->aux;
1984 77939319 : if (e->aux)
1985 5116388 : this_bb_predicate &= (*(ipa_predicate *) e->aux);
1986 77939319 : p = p.or_with (summary->conds, this_bb_predicate);
1987 77939319 : if (p == true)
1988 : break;
1989 : }
1990 : }
1991 75008351 : if (p != false)
1992 : {
1993 73866552 : basic_block pdom_bb;
1994 :
1995 73866552 : if (!bb->aux)
1996 : {
1997 27370100 : done = false;
1998 27370100 : bb->aux = edge_predicate_pool.allocate ();
1999 27370100 : *((ipa_predicate *) bb->aux) = p;
2000 : }
2001 46496452 : else if (p != *(ipa_predicate *) bb->aux)
2002 : {
2003 : /* This OR operation is needed to ensure monotonous data flow
2004 : in the case we hit the limit on number of clauses and the
2005 : and/or operations above give approximate answers. */
2006 126815 : p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux);
2007 126815 : if (p != *(ipa_predicate *)bb->aux)
2008 : {
2009 104086 : done = false;
2010 104086 : *((ipa_predicate *)bb->aux) = p;
2011 : }
2012 : }
2013 :
2014 : /* For switch/if statement, we can OR-combine predicates of all
2015 : its cases/branches to get predicate for basic block in their
2016 : convergence point, but sometimes this will generate very
2017 : complicated predicate. Actually, we can get simplified
2018 : predicate in another way by using the fact that predicate
2019 : for a basic block must also hold true for its post dominators.
2020 : To be specific, basic block in convergence point of
2021 : conditional statement should include predicate of the
2022 : statement. */
2023 73866552 : pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
2024 73866552 : if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb)
2025 : ;
2026 38034451 : else if (!pdom_bb->aux)
2027 : {
2028 7581110 : done = false;
2029 7581110 : pdom_bb->aux = edge_predicate_pool.allocate ();
2030 7581110 : *((ipa_predicate *)pdom_bb->aux) = p;
2031 : }
2032 30453341 : else if (p != *(ipa_predicate *)pdom_bb->aux)
2033 : {
2034 3334961 : p = p.or_with (summary->conds,
2035 : *(ipa_predicate *)pdom_bb->aux);
2036 3334961 : if (p != *(ipa_predicate *)pdom_bb->aux)
2037 : {
2038 141111 : done = false;
2039 141111 : *((ipa_predicate *)pdom_bb->aux) = p;
2040 : }
2041 : }
2042 : }
2043 : }
2044 : }
2045 6296988 : }
2046 :
2047 :
2048 : /* Return predicate specifying when the STMT might have result that is not
2049 : a compile time constant. */
2050 :
2051 : static ipa_predicate
2052 4243890 : will_be_nonconstant_expr_predicate (ipa_func_body_info *fbi,
2053 : class ipa_fn_summary *summary,
2054 : class ipa_node_params *params_summary,
2055 : tree expr,
2056 : vec<ipa_predicate> nonconstant_names)
2057 : {
2058 4243890 : tree parm;
2059 4243890 : int index;
2060 :
2061 4502934 : while (UNARY_CLASS_P (expr))
2062 259044 : expr = TREE_OPERAND (expr, 0);
2063 :
2064 4243890 : parm = unmodified_parm (fbi, NULL, expr, NULL);
2065 4243890 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2066 229120 : return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL,
2067 229120 : ipa_predicate::changed, NULL_TREE);
2068 4014770 : if (is_gimple_min_invariant (expr))
2069 62478 : return false;
2070 3952292 : if (TREE_CODE (expr) == SSA_NAME)
2071 3770831 : return nonconstant_names[SSA_NAME_VERSION (expr)];
2072 181461 : if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2073 : {
2074 180835 : ipa_predicate p1
2075 180835 : = will_be_nonconstant_expr_predicate (fbi, summary,
2076 : params_summary,
2077 180835 : TREE_OPERAND (expr, 0),
2078 : nonconstant_names);
2079 180835 : if (p1 == true)
2080 105486 : return p1;
2081 :
2082 75349 : ipa_predicate p2
2083 75349 : = will_be_nonconstant_expr_predicate (fbi, summary,
2084 : params_summary,
2085 75349 : TREE_OPERAND (expr, 1),
2086 : nonconstant_names);
2087 75349 : return p1.or_with (summary->conds, p2);
2088 : }
2089 626 : else if (TREE_CODE (expr) == COND_EXPR)
2090 : {
2091 184 : ipa_predicate p1
2092 184 : = will_be_nonconstant_expr_predicate (fbi, summary,
2093 : params_summary,
2094 184 : TREE_OPERAND (expr, 0),
2095 : nonconstant_names);
2096 184 : if (p1 == true)
2097 33 : return p1;
2098 :
2099 151 : ipa_predicate p2
2100 151 : = will_be_nonconstant_expr_predicate (fbi, summary,
2101 : params_summary,
2102 151 : TREE_OPERAND (expr, 1),
2103 : nonconstant_names);
2104 151 : if (p2 == true)
2105 151 : return p2;
2106 0 : p1 = p1.or_with (summary->conds, p2);
2107 0 : p2 = will_be_nonconstant_expr_predicate (fbi, summary,
2108 : params_summary,
2109 0 : TREE_OPERAND (expr, 2),
2110 : nonconstant_names);
2111 0 : return p2.or_with (summary->conds, p1);
2112 : }
2113 442 : else if (TREE_CODE (expr) == CALL_EXPR)
2114 442 : return true;
2115 : else
2116 : {
2117 0 : debug_tree (expr);
2118 0 : gcc_unreachable ();
2119 : }
2120 : }
2121 :
2122 :
2123 : /* Return predicate specifying when the STMT might have result that is not
2124 : a compile time constant. */
2125 :
2126 : static ipa_predicate
2127 113325568 : will_be_nonconstant_predicate (struct ipa_func_body_info *fbi,
2128 : class ipa_fn_summary *summary,
2129 : class ipa_node_params *params_summary,
2130 : gimple *stmt,
2131 : vec<ipa_predicate> nonconstant_names)
2132 : {
2133 113325568 : ipa_predicate p = true;
2134 113325568 : ssa_op_iter iter;
2135 113325568 : tree use;
2136 113325568 : tree param_type = NULL_TREE;
2137 113325568 : ipa_predicate op_non_const;
2138 113325568 : bool is_load;
2139 113325568 : int base_index;
2140 113325568 : struct agg_position_info aggpos;
2141 :
2142 : /* What statements might be optimized away
2143 : when their arguments are constant. */
2144 113325568 : if (gimple_code (stmt) != GIMPLE_ASSIGN
2145 39004677 : && gimple_code (stmt) != GIMPLE_COND
2146 27672871 : && gimple_code (stmt) != GIMPLE_SWITCH
2147 140919910 : && (gimple_code (stmt) != GIMPLE_CALL
2148 20694023 : || !(gimple_call_flags (stmt) & ECF_CONST)))
2149 25951374 : return p;
2150 :
2151 : /* Stores will stay anyway. */
2152 87374194 : if (gimple_store_p (stmt))
2153 25570280 : return p;
2154 :
2155 61803914 : is_load = gimple_assign_load_p (stmt);
2156 :
2157 : /* Loads can be optimized when the value is known. */
2158 61803914 : if (is_load)
2159 : {
2160 17754446 : tree op = gimple_assign_rhs1 (stmt);
2161 17754446 : if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type,
2162 : &aggpos))
2163 14558702 : return p;
2164 : }
2165 : else
2166 44049468 : base_index = -1;
2167 :
2168 : /* See if we understand all operands before we start
2169 : adding conditionals. */
2170 63886418 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2171 : {
2172 46922004 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2173 : /* For arguments we can build a condition. */
2174 46922004 : if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0)
2175 8356893 : continue;
2176 38565111 : if (TREE_CODE (use) != SSA_NAME)
2177 0 : return p;
2178 : /* If we know when operand is constant,
2179 : we still can say something useful. */
2180 38565111 : if (nonconstant_names[SSA_NAME_VERSION (use)] != true)
2181 8284313 : continue;
2182 30280798 : return p;
2183 : }
2184 :
2185 16964414 : if (is_load)
2186 3195663 : op_non_const =
2187 3195663 : add_condition (summary, params_summary,
2188 : base_index, param_type, &aggpos,
2189 : ipa_predicate::changed, NULL_TREE);
2190 : else
2191 13768751 : op_non_const = false;
2192 32577184 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2193 : {
2194 15612770 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2195 15612770 : int index;
2196 :
2197 15612770 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2198 : {
2199 7866868 : if (index != base_index)
2200 5367568 : p = add_condition (summary, params_summary, index,
2201 5367568 : TREE_TYPE (parm), NULL,
2202 : ipa_predicate::changed, NULL_TREE);
2203 : else
2204 2499300 : continue;
2205 : }
2206 : else
2207 7745902 : p = nonconstant_names[SSA_NAME_VERSION (use)];
2208 13113470 : op_non_const = p.or_with (summary->conds, op_non_const);
2209 : }
2210 16964414 : if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2211 14769643 : && gimple_op (stmt, 0)
2212 31445286 : && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2213 14480872 : nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2214 14480872 : = op_non_const;
2215 16964414 : return op_non_const;
2216 : }
2217 :
2218 : struct record_modified_bb_info
2219 : {
2220 : tree op;
2221 : bitmap bb_set;
2222 : gimple *stmt;
2223 : };
2224 :
2225 : /* Value is initialized in INIT_BB and used in USE_BB. We want to compute
2226 : probability how often it changes between USE_BB.
2227 : INIT_BB->count/USE_BB->count is an estimate, but if INIT_BB
2228 : is in different loop nest, we can do better.
2229 : This is all just estimate. In theory we look for minimal cut separating
2230 : INIT_BB and USE_BB, but we only want to anticipate loop invariant motion
2231 : anyway. */
2232 :
2233 : static basic_block
2234 10361400 : get_minimal_bb (basic_block init_bb, basic_block use_bb)
2235 : {
2236 10361400 : class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father);
2237 10361400 : if (l && l->header->count < init_bb->count)
2238 369487 : return l->header;
2239 : return init_bb;
2240 : }
2241 :
2242 : /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2243 : set except for info->stmt. */
2244 :
2245 : static bool
2246 4814214 : record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2247 : {
2248 4814214 : struct record_modified_bb_info *info =
2249 : (struct record_modified_bb_info *) data;
2250 4814214 : if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2251 : return false;
2252 4786118 : if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef)))
2253 : return false;
2254 4739333 : bitmap_set_bit (info->bb_set,
2255 4739333 : SSA_NAME_IS_DEFAULT_DEF (vdef)
2256 0 : ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2257 : : get_minimal_bb
2258 4739333 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2259 4739333 : gimple_bb (info->stmt))->index);
2260 4739333 : if (dump_file)
2261 : {
2262 0 : fprintf (dump_file, " Param ");
2263 0 : print_generic_expr (dump_file, info->op, TDF_SLIM);
2264 0 : fprintf (dump_file, " changed at bb %i, minimal: %i stmt: ",
2265 0 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index,
2266 : get_minimal_bb
2267 0 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2268 0 : gimple_bb (info->stmt))->index);
2269 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (vdef), 0);
2270 : }
2271 : return false;
2272 : }
2273 :
2274 : /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2275 : will change since last invocation of STMT.
2276 :
2277 : Value 0 is reserved for compile time invariants.
2278 : For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2279 : ought to be REG_BR_PROB_BASE / estimated_iters. */
2280 :
2281 : static int
2282 38247656 : param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i)
2283 : {
2284 38247656 : tree op = gimple_call_arg (stmt, i);
2285 38247656 : basic_block bb = gimple_bb (stmt);
2286 :
2287 38247656 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
2288 299 : op = TREE_OPERAND (op, 0);
2289 :
2290 38247656 : tree base = get_base_address (op);
2291 :
2292 : /* Global invariants never change. */
2293 38247656 : if (is_gimple_min_invariant (base))
2294 : return 0;
2295 :
2296 : /* We would have to do non-trivial analysis to really work out what
2297 : is the probability of value to change (i.e. when init statement
2298 : is in a sibling loop of the call).
2299 :
2300 : We do an conservative estimate: when call is executed N times more often
2301 : than the statement defining value, we take the frequency 1/N. */
2302 19164659 : if (TREE_CODE (base) == SSA_NAME)
2303 : {
2304 16872457 : profile_count init_count;
2305 :
2306 25666061 : if (!bb->count.nonzero_p ())
2307 : return REG_BR_PROB_BASE;
2308 :
2309 8636256 : if (SSA_NAME_IS_DEFAULT_DEF (base))
2310 3014189 : init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2311 : else
2312 5622067 : init_count = get_minimal_bb
2313 5622067 : (gimple_bb (SSA_NAME_DEF_STMT (base)),
2314 : gimple_bb (stmt))->count;
2315 :
2316 8636256 : if (init_count < bb->count)
2317 449655 : return MAX ((init_count.to_sreal_scale (bb->count)
2318 : * REG_BR_PROB_BASE).to_int (), 1);
2319 : return REG_BR_PROB_BASE;
2320 : }
2321 : else
2322 : {
2323 2292202 : ao_ref refd;
2324 2292202 : profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2325 2292202 : struct record_modified_bb_info info;
2326 2292202 : tree init = ctor_for_folding (base);
2327 :
2328 2292202 : if (init != error_mark_node)
2329 : return 0;
2330 5088091 : if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0)
2331 : return REG_BR_PROB_BASE;
2332 1410527 : if (dump_file)
2333 : {
2334 0 : fprintf (dump_file, " Analyzing param change probability of ");
2335 0 : print_generic_expr (dump_file, op, TDF_SLIM);
2336 0 : fprintf (dump_file, "\n");
2337 : }
2338 1410527 : ao_ref_init (&refd, op);
2339 1410527 : info.op = op;
2340 1410527 : info.stmt = stmt;
2341 1410527 : info.bb_set = BITMAP_ALLOC (NULL);
2342 1410527 : int walked
2343 2821054 : = walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2344 : NULL, NULL, fbi->aa_walk_budget);
2345 1410527 : if (walked > 0)
2346 1281483 : fbi->aa_walk_budget -= walked;
2347 1410527 : if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index))
2348 : {
2349 869283 : if (walked < 0)
2350 206 : fbi->aa_walk_budget = 0;
2351 869283 : if (dump_file)
2352 : {
2353 0 : if (walked < 0)
2354 0 : fprintf (dump_file, " Ran out of AA walking budget.\n");
2355 : else
2356 0 : fprintf (dump_file, " Set in same BB as used.\n");
2357 : }
2358 869283 : BITMAP_FREE (info.bb_set);
2359 869283 : return REG_BR_PROB_BASE;
2360 : }
2361 :
2362 541244 : bitmap_iterator bi;
2363 541244 : unsigned index;
2364 : /* Lookup the most frequent update of the value and believe that
2365 : it dominates all the other; precise analysis here is difficult. */
2366 1528250 : EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2367 987006 : max = profile_count::max_prefer_initialized
2368 987006 : (max, BASIC_BLOCK_FOR_FN (cfun, index)->count);
2369 541244 : if (dump_file)
2370 : {
2371 0 : fprintf (dump_file, " Set with count ");
2372 0 : max.dump (dump_file);
2373 0 : fprintf (dump_file, " and used with count ");
2374 0 : bb->count.dump (dump_file);
2375 0 : fprintf (dump_file, " freq %f\n",
2376 0 : max.to_sreal_scale (bb->count).to_double ());
2377 : }
2378 :
2379 541244 : BITMAP_FREE (info.bb_set);
2380 541244 : if (max < bb->count)
2381 90410 : return MAX ((max.to_sreal_scale (bb->count)
2382 : * REG_BR_PROB_BASE).to_int (), 1);
2383 : return REG_BR_PROB_BASE;
2384 : }
2385 : }
2386 :
2387 : /* Find whether a basic block BB is the final block of a (half) diamond CFG
2388 : sub-graph and if the predicate the condition depends on is known. If so,
2389 : return true and store the pointer the predicate in *P. */
2390 :
2391 : static bool
2392 6320417 : phi_result_unknown_predicate (ipa_func_body_info *fbi,
2393 : ipa_fn_summary *summary,
2394 : class ipa_node_params *params_summary,
2395 : basic_block bb,
2396 : ipa_predicate *p,
2397 : vec<ipa_predicate> nonconstant_names)
2398 : {
2399 6320417 : edge e;
2400 6320417 : edge_iterator ei;
2401 6320417 : basic_block first_bb = NULL;
2402 :
2403 6320417 : if (single_pred_p (bb))
2404 : {
2405 20059 : *p = false;
2406 20059 : return true;
2407 : }
2408 :
2409 14528576 : FOR_EACH_EDGE (e, ei, bb->preds)
2410 : {
2411 12396652 : if (single_succ_p (e->src))
2412 : {
2413 13002035 : if (!single_pred_p (e->src))
2414 : return false;
2415 7302504 : if (!first_bb)
2416 3194664 : first_bb = single_pred (e->src);
2417 4107840 : else if (single_pred (e->src) != first_bb)
2418 : return false;
2419 : }
2420 : else
2421 : {
2422 4105131 : if (!first_bb)
2423 : first_bb = e->src;
2424 1413150 : else if (e->src != first_bb)
2425 : return false;
2426 : }
2427 : }
2428 :
2429 2131924 : if (!first_bb)
2430 : return false;
2431 :
2432 4263848 : gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb));
2433 2089763 : if (!stmt
2434 2089763 : || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2435 310485 : return false;
2436 :
2437 1821439 : *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary,
2438 : gimple_cond_lhs (stmt),
2439 : nonconstant_names);
2440 1821439 : if (*p == true)
2441 : return false;
2442 : else
2443 : return true;
2444 : }
2445 :
2446 : /* Given a PHI statement in a function described by inline properties SUMMARY
2447 : and *P being the predicate describing whether the selected PHI argument is
2448 : known, store a predicate for the result of the PHI statement into
2449 : NONCONSTANT_NAMES, if possible. */
2450 :
2451 : static void
2452 703629 : predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi,
2453 : ipa_predicate *p,
2454 : vec<ipa_predicate> nonconstant_names)
2455 : {
2456 703629 : unsigned i;
2457 :
2458 997070 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2459 : {
2460 877843 : tree arg = gimple_phi_arg (phi, i)->def;
2461 877843 : if (!is_gimple_min_invariant (arg))
2462 : {
2463 757282 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
2464 1514564 : *p = p->or_with (summary->conds,
2465 757282 : nonconstant_names[SSA_NAME_VERSION (arg)]);
2466 757282 : if (*p == true)
2467 : return;
2468 : }
2469 : }
2470 :
2471 119227 : if (dump_file && (dump_flags & TDF_DETAILS))
2472 : {
2473 3 : fprintf (dump_file, "\t\tphi predicate: ");
2474 3 : p->dump (dump_file, summary->conds);
2475 : }
2476 119227 : nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2477 : }
2478 :
2479 : /* For a typical usage of __builtin_expect (a<b, 1), we
2480 : may introduce an extra relation stmt:
2481 : With the builtin, we have
2482 : t1 = a <= b;
2483 : t2 = (long int) t1;
2484 : t3 = __builtin_expect (t2, 1);
2485 : if (t3 != 0)
2486 : goto ...
2487 : Without the builtin, we have
2488 : if (a<=b)
2489 : goto...
2490 : This affects the size/time estimation and may have
2491 : an impact on the earlier inlining.
2492 : Here find this pattern and fix it up later. */
2493 :
2494 : static gimple *
2495 40859473 : find_foldable_builtin_expect (basic_block bb)
2496 : {
2497 40859473 : gimple_stmt_iterator bsi;
2498 :
2499 281630978 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2500 : {
2501 200073657 : gimple *stmt = gsi_stmt (bsi);
2502 200073657 : if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2503 199861955 : || gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY)
2504 399935547 : || gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT))
2505 : {
2506 343303 : tree var = gimple_call_lhs (stmt);
2507 343303 : tree arg = gimple_call_arg (stmt, 0);
2508 343303 : use_operand_p use_p;
2509 343303 : gimple *use_stmt;
2510 343303 : bool match = false;
2511 343303 : bool done = false;
2512 :
2513 343303 : if (!var || !arg)
2514 3 : continue;
2515 343300 : gcc_assert (TREE_CODE (var) == SSA_NAME);
2516 :
2517 685294 : while (TREE_CODE (arg) == SSA_NAME)
2518 : {
2519 685294 : gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg);
2520 685294 : if (!is_gimple_assign (stmt_tmp))
2521 : break;
2522 677978 : switch (gimple_assign_rhs_code (stmt_tmp))
2523 : {
2524 : case LT_EXPR:
2525 : case LE_EXPR:
2526 : case GT_EXPR:
2527 : case GE_EXPR:
2528 : case EQ_EXPR:
2529 : case NE_EXPR:
2530 : match = true;
2531 : done = true;
2532 : break;
2533 : CASE_CONVERT:
2534 : break;
2535 : default:
2536 : done = true;
2537 : break;
2538 : }
2539 341994 : if (done)
2540 : break;
2541 341994 : arg = gimple_assign_rhs1 (stmt_tmp);
2542 : }
2543 :
2544 308159 : if (match && single_imm_use (var, &use_p, &use_stmt)
2545 651038 : && gimple_code (use_stmt) == GIMPLE_COND)
2546 161625 : return use_stmt;
2547 : }
2548 : }
2549 : return NULL;
2550 : }
2551 :
2552 : /* Return true when the basic blocks contains only clobbers followed by RESX.
2553 : Such BBs are kept around to make removal of dead stores possible with
2554 : presence of EH and will be optimized out by optimize_clobbers later in the
2555 : game.
2556 :
2557 : NEED_EH is used to recurse in case the clobber has non-EH predecessors
2558 : that can be clobber only, too.. When it is false, the RESX is not necessary
2559 : on the end of basic block. */
2560 :
2561 : static bool
2562 41454680 : clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2563 : {
2564 41454680 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2565 41454680 : edge_iterator ei;
2566 41454680 : edge e;
2567 :
2568 41454680 : if (need_eh)
2569 : {
2570 41386519 : if (gsi_end_p (gsi))
2571 : return false;
2572 40258729 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2573 : return false;
2574 1027170 : gsi_prev (&gsi);
2575 : }
2576 40928104 : else if (!single_succ_p (bb))
2577 : return false;
2578 :
2579 4596348 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2580 : {
2581 2751314 : gimple *stmt = gsi_stmt (gsi);
2582 2751314 : if (is_gimple_debug (stmt))
2583 918437 : continue;
2584 1832877 : if (gimple_clobber_p (stmt))
2585 865187 : continue;
2586 967690 : if (gimple_code (stmt) == GIMPLE_LABEL)
2587 : break;
2588 : return false;
2589 : }
2590 :
2591 : /* See if all predecessors are either throws or clobber only BBs. */
2592 2985879 : FOR_EACH_EDGE (e, ei, bb->preds)
2593 2457373 : if (!(e->flags & EDGE_EH)
2594 2457373 : && !clobber_only_eh_bb_p (e->src, false))
2595 : return false;
2596 :
2597 : return true;
2598 : }
2599 :
2600 : /* Return true if STMT compute a floating point expression that may be affected
2601 : by -ffast-math and similar flags. */
2602 :
2603 : static bool
2604 93805746 : fp_expression_p (gimple *stmt)
2605 : {
2606 93805746 : ssa_op_iter i;
2607 93805746 : tree op;
2608 :
2609 210511404 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE)
2610 117300239 : if (FLOAT_TYPE_P (TREE_TYPE (op)))
2611 : return true;
2612 : return false;
2613 : }
2614 :
2615 : /* Return true if T references memory location that is local
2616 : for the function (that means, dead after return) or read-only. */
2617 :
2618 : bool
2619 58425894 : refs_local_or_readonly_memory_p (tree t)
2620 : {
2621 : /* Non-escaping memory is fine. */
2622 58425894 : t = get_base_address (t);
2623 58425894 : if ((TREE_CODE (t) == MEM_REF
2624 58425894 : || TREE_CODE (t) == TARGET_MEM_REF))
2625 24676269 : return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2626 :
2627 : /* Automatic variables are fine. */
2628 33749625 : if (DECL_P (t)
2629 33749625 : && auto_var_in_fn_p (t, current_function_decl))
2630 : return true;
2631 :
2632 : /* Read-only variables are fine. */
2633 11066400 : if (DECL_P (t) && TREE_READONLY (t))
2634 : return true;
2635 :
2636 : return false;
2637 : }
2638 :
2639 : /* Return true if T is a pointer pointing to memory location that is local
2640 : for the function (that means, dead after return) or read-only. */
2641 :
2642 : bool
2643 71445068 : points_to_local_or_readonly_memory_p (tree t)
2644 : {
2645 : /* See if memory location is clearly invalid. */
2646 71445068 : if (integer_zerop (t))
2647 2429065 : return flag_delete_null_pointer_checks;
2648 69016003 : if (TREE_CODE (t) == SSA_NAME)
2649 : {
2650 : /* For IPA passes we can consinder accesses to return slot local
2651 : even if it is not local in the sense that memory is dead by
2652 : the end of founction.
2653 : The outer function will see a store in the call assignment
2654 : and thus this will do right thing for all uses of this
2655 : function in the current IPA passes (modref, pure/const discovery
2656 : and inlining heuristics). */
2657 43678075 : if (DECL_RESULT (current_function_decl)
2658 43678075 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
2659 44711225 : && t == ssa_default_def (cfun, DECL_RESULT (current_function_decl)))
2660 : return true;
2661 43370099 : return !ptr_deref_may_alias_global_p (t, false);
2662 : }
2663 25337928 : if (TREE_CODE (t) == ADDR_EXPR
2664 25337928 : && (TREE_CODE (TREE_OPERAND (t, 0)) != TARGET_MEM_REF
2665 4527 : || TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) != INTEGER_CST))
2666 13085691 : return refs_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2667 : return false;
2668 : }
2669 :
2670 : /* Return true if T is a pointer pointing to memory location that is possible
2671 : sra candidate if all functions it is passed to are inlined. */
2672 :
2673 : static bool
2674 38247656 : points_to_possible_sra_candidate_p (tree t)
2675 : {
2676 38247656 : if (TREE_CODE (t) != ADDR_EXPR)
2677 : return false;
2678 :
2679 11384595 : t = get_base_address (TREE_OPERAND (t, 0));
2680 :
2681 : /* Automatic variables are fine. */
2682 11384595 : if (DECL_P (t)
2683 11384595 : && auto_var_in_fn_p (t, current_function_decl))
2684 : return true;
2685 : return false;
2686 : }
2687 :
2688 : /* Return true if BB only calls builtin_unreachable.
2689 : We skip empty basic blocks, debug statements, clobbers and predicts.
2690 : CACHE is used to memoize already analyzed blocks. */
2691 :
2692 : static bool
2693 27149325 : builtin_unreachable_bb_p (basic_block bb, vec<unsigned char> &cache)
2694 : {
2695 27149325 : if (cache[bb->index])
2696 2561407 : return cache[bb->index] - 1;
2697 24587918 : gimple_stmt_iterator si;
2698 24587918 : auto_vec <basic_block, 4> visited_bbs;
2699 24587918 : bool ret = false;
2700 25385862 : while (true)
2701 : {
2702 25385862 : bool empty_bb = true;
2703 25385862 : visited_bbs.safe_push (bb);
2704 25385862 : cache[bb->index] = 3;
2705 25385862 : for (si = gsi_start_nondebug_bb (bb);
2706 26854536 : !gsi_end_p (si) && empty_bb;
2707 1468674 : gsi_next_nondebug (&si))
2708 : {
2709 25497233 : if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT
2710 25130824 : && !gimple_clobber_p (gsi_stmt (si))
2711 49559052 : && !gimple_nop_p (gsi_stmt (si)))
2712 : {
2713 : empty_bb = false;
2714 : break;
2715 : }
2716 : }
2717 25385862 : if (!empty_bb)
2718 : break;
2719 : else
2720 1357303 : bb = single_succ_edge (bb)->dest;
2721 1357303 : if (cache[bb->index])
2722 : {
2723 559359 : ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1;
2724 559359 : goto done;
2725 : }
2726 : }
2727 24028559 : if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE)
2728 24028559 : || gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP))
2729 : ret = true;
2730 24587918 : done:
2731 99149616 : for (basic_block vbb:visited_bbs)
2732 25385862 : cache[vbb->index] = (unsigned char)ret + 1;
2733 24587918 : return ret;
2734 24587918 : }
2735 :
2736 : static bool
2737 13695915 : guards_builtin_unreachable (basic_block bb, vec<unsigned char> &cache)
2738 : {
2739 13695915 : edge_iterator ei;
2740 13695915 : edge e;
2741 40588452 : FOR_EACH_EDGE (e, ei, bb->succs)
2742 27149325 : if (builtin_unreachable_bb_p (e->dest, cache))
2743 : {
2744 256788 : if (dump_file && (dump_flags & TDF_DETAILS))
2745 1 : fprintf (dump_file,
2746 : "BB %i ends with conditional guarding __builtin_unreachable;"
2747 : " conditinal is unnecesary\n", bb->index);
2748 256788 : return true;
2749 : }
2750 : return false;
2751 : }
2752 :
2753 : #define STMT_NECESSARY GF_PLF_1
2754 :
2755 : /* If STMT is not already marked necessary, mark it, and add it to the
2756 : worklist if ADD_TO_WORKLIST is true. */
2757 :
2758 : static inline void
2759 170785897 : mark_stmt_necessary (gimple *stmt, auto_vec<gimple *> &worklist)
2760 : {
2761 170785897 : gcc_assert (stmt);
2762 :
2763 170785897 : if (gimple_plf (stmt, STMT_NECESSARY))
2764 : return;
2765 :
2766 141670125 : if (dump_file && (dump_flags & TDF_DETAILS))
2767 : {
2768 207 : fprintf (dump_file, "Marking useful stmt: ");
2769 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2770 207 : fprintf (dump_file, "\n");
2771 : }
2772 :
2773 141670125 : gimple_set_plf (stmt, STMT_NECESSARY, true);
2774 141670125 : worklist.safe_push (stmt);
2775 : }
2776 :
2777 : /* Mark the statement defining operand OP as necessary. */
2778 :
2779 : static inline void
2780 120263742 : mark_operand_necessary (tree op, auto_vec<gimple *> &worklist)
2781 : {
2782 120263742 : gimple *stmt = SSA_NAME_DEF_STMT (op);
2783 120263742 : if (gimple_nop_p (stmt))
2784 : return;
2785 97588519 : mark_stmt_necessary (stmt, worklist);
2786 : }
2787 :
2788 : /* Mark all statements that will remain in the body after optimizing out
2789 : conditionals guarding __builtin_unreachable which we keep to preserve
2790 : value ranges. */
2791 :
2792 : static void
2793 7192729 : find_necessary_statements (struct cgraph_node *node)
2794 : {
2795 7192729 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2796 7192729 : auto_vec<unsigned char, 10> cache;
2797 7192729 : basic_block bb;
2798 7192729 : auto_vec<gimple *> worklist;
2799 :
2800 7192729 : cache.safe_grow_cleared (last_basic_block_for_fn (cfun));
2801 : /* Mark all obviously necessary statements. */
2802 48579248 : FOR_EACH_BB_FN (bb, my_function)
2803 : {
2804 41386519 : for (gimple_stmt_iterator gsi = gsi_start_phis (bb);
2805 52903171 : !gsi_end_p (gsi); gsi_next (&gsi))
2806 11516652 : gimple_set_plf (gsi_stmt (gsi), STMT_NECESSARY, false);
2807 :
2808 231361411 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2809 148588373 : gsi_next_nondebug (&bsi))
2810 : {
2811 148588373 : gimple *stmt = gsi_stmt (bsi);
2812 :
2813 148588373 : gimple_set_plf (stmt, STMT_NECESSARY, false);
2814 148588373 : if (gimple_has_side_effects (stmt)
2815 121745018 : || (is_ctrl_stmt (stmt)
2816 21906408 : && (gimple_code (stmt) != GIMPLE_COND
2817 13695915 : || !guards_builtin_unreachable (bb, cache)))
2818 100095398 : || gimple_store_p (stmt)
2819 224001060 : || gimple_code (stmt) == GIMPLE_ASM)
2820 73197378 : mark_stmt_necessary (stmt, worklist);
2821 : }
2822 : }
2823 148862854 : while (worklist.length () > 0)
2824 : {
2825 141670125 : gimple *stmt = worklist.pop ();
2826 :
2827 141670125 : if (dump_file && (dump_flags & TDF_DETAILS))
2828 : {
2829 207 : fprintf (dump_file, "processing: ");
2830 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2831 207 : fprintf (dump_file, "\n");
2832 : }
2833 141670125 : if (gimple_code (stmt) == GIMPLE_PHI)
2834 18163265 : for (unsigned int k = 0; k < gimple_phi_num_args (stmt); k++)
2835 : {
2836 12853311 : tree arg = PHI_ARG_DEF (stmt, k);
2837 :
2838 12853311 : if (TREE_CODE (arg) == SSA_NAME)
2839 9865252 : mark_operand_necessary (arg, worklist);
2840 : }
2841 : else
2842 : {
2843 136360171 : ssa_op_iter iter;
2844 136360171 : tree use;
2845 :
2846 246758661 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2847 110398490 : mark_operand_necessary (use, worklist);
2848 : }
2849 : }
2850 7192729 : }
2851 :
2852 : /* Analyze function body for NODE.
2853 : EARLY indicates run from early optimization pipeline. */
2854 :
2855 : static void
2856 7192729 : analyze_function_body (struct cgraph_node *node, bool early)
2857 : {
2858 7192729 : sreal time = opt_for_fn (node->decl, param_uninlined_function_time);
2859 : /* Estimate static overhead for function prologue/epilogue and alignment. */
2860 7192729 : int size = opt_for_fn (node->decl, param_uninlined_function_insns);
2861 : /* Benefits are scaled by probability of elimination that is in range
2862 : <0,2>. */
2863 7192729 : basic_block bb;
2864 7192729 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2865 7192729 : sreal freq;
2866 7192729 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
2867 7192729 : ipa_node_params *params_summary
2868 7192729 : = early ? NULL : ipa_node_params_sum->get (node);
2869 7192729 : ipa_predicate bb_predicate;
2870 7192729 : struct ipa_func_body_info fbi;
2871 7192729 : vec<ipa_predicate> nonconstant_names = vNULL;
2872 7192729 : int nblocks, n;
2873 7192729 : int *order;
2874 7192729 : gimple *fix_builtin_expect_stmt;
2875 :
2876 7192729 : gcc_assert (my_function && my_function->cfg);
2877 7192729 : gcc_assert (cfun == my_function);
2878 :
2879 7192729 : memset(&fbi, 0, sizeof(fbi));
2880 7192729 : vec_free (info->conds);
2881 7192729 : info->conds = NULL;
2882 7192729 : info->size_time_table.release ();
2883 7192729 : info->call_size_time_table.release ();
2884 :
2885 : /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2886 : so we can produce proper inline hints.
2887 :
2888 : When optimizing and analyzing for early inliner, initialize node params
2889 : so we can produce correct BB predicates. */
2890 :
2891 7192729 : if (opt_for_fn (node->decl, optimize))
2892 : {
2893 6296988 : calculate_dominance_info (CDI_DOMINATORS);
2894 6296988 : calculate_dominance_info (CDI_POST_DOMINATORS);
2895 6296988 : if (!early)
2896 1375986 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2897 : else
2898 : {
2899 4921002 : ipa_check_create_node_params ();
2900 4921002 : ipa_initialize_node_params (node);
2901 : }
2902 :
2903 6296988 : if (ipa_node_params_sum)
2904 : {
2905 6296988 : fbi.node = node;
2906 6296988 : fbi.info = ipa_node_params_sum->get (node);
2907 6296988 : fbi.bb_infos = vNULL;
2908 6296988 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
2909 6296988 : fbi.param_count = count_formal_params (node->decl);
2910 6296988 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
2911 :
2912 6296988 : nonconstant_names.safe_grow_cleared
2913 6296988 : (SSANAMES (my_function)->length (), true);
2914 : }
2915 : }
2916 :
2917 7192729 : if (dump_file)
2918 247 : fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2919 : node->dump_name ());
2920 :
2921 : /* When we run into maximal number of entries, we assign everything to the
2922 : constant truth case. Be sure to have it in list. */
2923 7192729 : bb_predicate = true;
2924 7192729 : info->account_size_time (0, 0, bb_predicate, bb_predicate);
2925 :
2926 7192729 : bb_predicate = ipa_predicate::not_inlined ();
2927 7192729 : info->account_size_time (opt_for_fn (node->decl,
2928 : param_uninlined_function_insns)
2929 : * ipa_fn_summary::size_scale,
2930 7192729 : opt_for_fn (node->decl,
2931 : param_uninlined_function_time),
2932 : bb_predicate,
2933 : bb_predicate);
2934 :
2935 : /* Only look for target information for inlinable functions. */
2936 7192729 : bool scan_for_target_info =
2937 7192729 : info->inlinable
2938 12906380 : && targetm.target_option.need_ipa_fn_target_info (node->decl,
2939 5713651 : info->target_info);
2940 :
2941 7192729 : if (fbi.info)
2942 6296988 : compute_bb_predicates (&fbi, node, info, params_summary);
2943 7192729 : find_necessary_statements (node);
2944 7192729 : const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2945 7192729 : order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2946 7192729 : nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2947 48579248 : for (n = 0; n < nblocks; n++)
2948 : {
2949 41386519 : bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2950 41386519 : freq = bb->count.to_sreal_scale (entry_count);
2951 41386519 : if (clobber_only_eh_bb_p (bb))
2952 : {
2953 527046 : if (dump_file && (dump_flags & TDF_DETAILS))
2954 0 : fprintf (dump_file, "\n Ignoring BB %i;"
2955 : " it will be optimized away by cleanup_clobbers\n",
2956 : bb->index);
2957 527046 : continue;
2958 : }
2959 :
2960 : /* TODO: Obviously predicates can be propagated down across CFG. */
2961 40859473 : if (fbi.info)
2962 : {
2963 34485222 : if (bb->aux)
2964 34485208 : bb_predicate = *(ipa_predicate *)bb->aux;
2965 : else
2966 14 : bb_predicate = false;
2967 : }
2968 : else
2969 6374251 : bb_predicate = true;
2970 :
2971 40859473 : if (dump_file && (dump_flags & TDF_DETAILS))
2972 : {
2973 67 : fprintf (dump_file, "\n BB %i predicate:", bb->index);
2974 67 : bb_predicate.dump (dump_file, info->conds);
2975 : }
2976 :
2977 40859473 : if (fbi.info && nonconstant_names.exists ())
2978 : {
2979 34485222 : ipa_predicate phi_predicate;
2980 34485222 : bool first_phi = true;
2981 :
2982 35188851 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2983 703629 : gsi_next (&bsi))
2984 : {
2985 6403160 : if (first_phi
2986 6403160 : && !phi_result_unknown_predicate (&fbi, info,
2987 : params_summary,
2988 : bb,
2989 : &phi_predicate,
2990 : nonconstant_names))
2991 : break;
2992 703629 : first_phi = false;
2993 703629 : if (dump_file && (dump_flags & TDF_DETAILS))
2994 : {
2995 3 : fprintf (dump_file, " ");
2996 3 : print_gimple_stmt (dump_file, gsi_stmt (bsi), 0);
2997 : }
2998 703629 : predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2999 : nonconstant_names);
3000 : }
3001 : }
3002 :
3003 40859473 : fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
3004 :
3005 40859473 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
3006 181487963 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
3007 : {
3008 140628490 : gimple *stmt = gsi_stmt (bsi);
3009 140628490 : if (!gimple_plf (stmt, STMT_NECESSARY))
3010 : {
3011 5519766 : if (dump_file && (dump_flags & TDF_DETAILS))
3012 : {
3013 19 : fprintf (dump_file, " skipping unnecesary stmt ");
3014 19 : print_gimple_stmt (dump_file, stmt, 0);
3015 : }
3016 : /* TODO: const calls used only to produce values for
3017 : builtion_unreachable guards should not be accounted. However
3018 : we still want to inline them and this does does not work well
3019 : with the cost model. For now account them as usual. */
3020 5519766 : if (!is_gimple_call (stmt)
3021 5519766 : || gimple_call_internal_p (stmt))
3022 5240161 : continue;
3023 : }
3024 135388329 : int this_size = estimate_num_insns (stmt, &eni_size_weights);
3025 135388329 : int this_time = estimate_num_insns (stmt, &eni_time_weights);
3026 135388329 : int prob;
3027 135388329 : ipa_predicate will_be_nonconstant;
3028 :
3029 : /* This relation stmt should be folded after we remove
3030 : __builtin_expect call. Adjust the cost here. */
3031 135388329 : if (stmt == fix_builtin_expect_stmt)
3032 : {
3033 161187 : this_size--;
3034 161187 : this_time--;
3035 : }
3036 :
3037 135388329 : if (dump_file && (dump_flags & TDF_DETAILS))
3038 : {
3039 201 : fprintf (dump_file, " ");
3040 201 : print_gimple_stmt (dump_file, stmt, 0);
3041 201 : fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
3042 : freq.to_double (), this_size,
3043 : this_time);
3044 : }
3045 :
3046 135388329 : if (is_gimple_call (stmt)
3047 135388329 : && !gimple_call_internal_p (stmt))
3048 : {
3049 22791561 : struct cgraph_edge *edge = node->get_edge (stmt);
3050 22791561 : ipa_call_summary *es = ipa_call_summaries->get_create (edge);
3051 :
3052 : /* Special case: results of BUILT_IN_CONSTANT_P will be always
3053 : resolved as constant. We however don't want to optimize
3054 : out the cgraph edges. */
3055 22791561 : if (nonconstant_names.exists ()
3056 19922441 : && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
3057 28317 : && gimple_call_lhs (stmt)
3058 22819878 : && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3059 : {
3060 28317 : ipa_predicate false_p = false;
3061 28317 : nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
3062 28317 : = false_p;
3063 : }
3064 22791561 : if (ipa_node_params_sum)
3065 : {
3066 19935241 : int count = gimple_call_num_args (stmt);
3067 19935241 : int i;
3068 :
3069 19935241 : if (count)
3070 17035004 : es->param.safe_grow_cleared (count, true);
3071 58182897 : for (i = 0; i < count; i++)
3072 : {
3073 38247656 : int prob = param_change_prob (&fbi, stmt, i);
3074 38247656 : gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
3075 38247656 : es->param[i].change_prob = prob;
3076 38247656 : es->param[i].points_to_local_or_readonly_memory
3077 38247656 : = points_to_local_or_readonly_memory_p
3078 38247656 : (gimple_call_arg (stmt, i));
3079 38247656 : es->param[i].points_to_possible_sra_candidate
3080 38247656 : = points_to_possible_sra_candidate_p
3081 38247656 : (gimple_call_arg (stmt, i));
3082 : }
3083 : }
3084 : /* We cannot setup VLA parameters during inlining. */
3085 66851474 : for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i)
3086 44060309 : if (TREE_CODE (gimple_call_arg (stmt, i)) == WITH_SIZE_EXPR)
3087 : {
3088 396 : edge->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
3089 396 : break;
3090 : }
3091 22791561 : es->call_stmt_size = this_size;
3092 22791561 : es->call_stmt_time = this_time;
3093 22791561 : es->loop_depth = bb_loop_depth (bb);
3094 22791561 : edge_set_predicate (edge, &bb_predicate);
3095 22791561 : if (edge->speculative)
3096 : {
3097 0 : cgraph_edge *indirect
3098 0 : = edge->speculative_call_indirect_edge ();
3099 0 : ipa_call_summary *es2
3100 0 : = ipa_call_summaries->get_create (indirect);
3101 0 : ipa_call_summaries->duplicate (edge, indirect,
3102 : es, es2);
3103 :
3104 : /* Edge is the first direct call.
3105 : create and duplicate call summaries for multiple
3106 : speculative call targets. */
3107 0 : for (cgraph_edge *direct
3108 0 : = edge->next_speculative_call_target ();
3109 0 : direct;
3110 0 : direct = direct->next_speculative_call_target ())
3111 : {
3112 0 : ipa_call_summary *es3
3113 0 : = ipa_call_summaries->get_create (direct);
3114 0 : ipa_call_summaries->duplicate (edge, direct,
3115 : es, es3);
3116 : }
3117 : }
3118 :
3119 : /* If dealing with a carrying edge, copy its summary over to its
3120 : attached edges as well. */
3121 22791561 : if (edge->has_callback)
3122 : {
3123 15081 : cgraph_edge *cbe;
3124 30162 : for (cbe = edge->first_callback_edge (); cbe;
3125 15081 : cbe = cbe->next_callback_edge ())
3126 : {
3127 15081 : ipa_call_summary *es2 = ipa_call_summaries->get_create (cbe);
3128 15081 : ipa_call_summaries->duplicate (edge, cbe, es, es2);
3129 : /* Unlike speculative edges, callback edges have no real
3130 : size or time; the call doesn't exist. Reflect that in
3131 : their summaries. */
3132 15081 : es2->call_stmt_size = 0;
3133 15081 : es2->call_stmt_time = 0;
3134 : }
3135 : }
3136 : }
3137 :
3138 : /* TODO: When conditional jump or switch is known to be constant, but
3139 : we did not translate it into the predicates, we really can account
3140 : just maximum of the possible paths. */
3141 135388329 : if (fbi.info)
3142 113325568 : will_be_nonconstant
3143 113325568 : = will_be_nonconstant_predicate (&fbi, info, params_summary,
3144 : stmt, nonconstant_names);
3145 : else
3146 22062761 : will_be_nonconstant = true;
3147 135388329 : if (this_time || this_size)
3148 : {
3149 110972374 : sreal final_time = (sreal)this_time * freq;
3150 110972374 : prob = eliminated_by_inlining_prob (&fbi, stmt);
3151 110972374 : if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
3152 11 : fprintf (dump_file,
3153 : "\t\t50%% will be eliminated by inlining\n");
3154 110972374 : if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
3155 18 : fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
3156 :
3157 110972374 : ipa_predicate p = bb_predicate & will_be_nonconstant;
3158 110972374 : int parm = load_or_store_of_ptr_parameter (&fbi, stmt);
3159 110972374 : ipa_predicate sra_predicate = true;
3160 110972374 : if (parm != -1)
3161 14057844 : sra_predicate &= add_condition (info, params_summary, parm,
3162 : ptr_type_node, NULL,
3163 7028922 : ipa_predicate::not_sra_candidate, NULL, 0);
3164 :
3165 : /* We can ignore statement when we proved it is never going
3166 : to happen, but we cannot do that for call statements
3167 : because edges are accounted specially. */
3168 :
3169 221944748 : if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false)
3170 : {
3171 110258171 : time += final_time;
3172 110258171 : size += this_size;
3173 : }
3174 :
3175 : /* We account everything but the calls. Calls have their own
3176 : size/time info attached to cgraph edges. This is necessary
3177 : in order to make the cost disappear after inlining. */
3178 110972374 : if (!is_gimple_call (stmt))
3179 : {
3180 88845893 : if (prob)
3181 : {
3182 14829665 : ipa_predicate ip
3183 14829665 : = bb_predicate & ipa_predicate::not_inlined () & sra_predicate;
3184 29659330 : info->account_size_time (this_size * prob,
3185 14829665 : (final_time * prob) / 2, ip,
3186 : p);
3187 : }
3188 14829665 : if (prob != 2)
3189 163955842 : info->account_size_time (this_size * (2 - prob),
3190 81977921 : (final_time * (2 - prob) / 2),
3191 163955842 : bb_predicate & sra_predicate,
3192 : p);
3193 : }
3194 :
3195 110972374 : if (!info->fp_expressions && fp_expression_p (stmt))
3196 : {
3197 594581 : info->fp_expressions = true;
3198 594581 : if (dump_file)
3199 9 : fprintf (dump_file, " fp_expression set\n");
3200 : }
3201 : }
3202 :
3203 : /* For target specific information, we want to scan all statements
3204 : rather than those statements with non-zero weights, to avoid
3205 : missing to scan something interesting for target information,
3206 : such as: internal function calls. */
3207 135388329 : if (scan_for_target_info)
3208 0 : scan_for_target_info =
3209 0 : targetm.target_option.update_ipa_fn_target_info
3210 0 : (info->target_info, stmt);
3211 :
3212 : /* Account cost of address calculations in the statements. */
3213 523483715 : for (unsigned int i = 0; i < gimple_num_ops (stmt); i++)
3214 : {
3215 388095386 : for (tree op = gimple_op (stmt, i);
3216 746991068 : op && handled_component_p (op);
3217 46733514 : op = TREE_OPERAND (op, 0))
3218 46733514 : if ((TREE_CODE (op) == ARRAY_REF
3219 46733514 : || TREE_CODE (op) == ARRAY_RANGE_REF)
3220 46733514 : && TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
3221 : {
3222 2379483 : ipa_predicate p = bb_predicate;
3223 2379483 : if (fbi.info)
3224 1861051 : p = p & will_be_nonconstant_expr_predicate
3225 1861051 : (&fbi, info, params_summary,
3226 1861051 : TREE_OPERAND (op, 1),
3227 1861051 : nonconstant_names);
3228 2379483 : if (p != false)
3229 : {
3230 2373781 : time += freq;
3231 2373781 : size += 1;
3232 2373781 : if (dump_file)
3233 24 : fprintf (dump_file,
3234 : "\t\tAccounting address calculation.\n");
3235 2373781 : info->account_size_time (ipa_fn_summary::size_scale,
3236 : freq,
3237 : bb_predicate,
3238 : p);
3239 : }
3240 : }
3241 : }
3242 :
3243 : }
3244 : }
3245 7192729 : free (order);
3246 :
3247 7192729 : if (nonconstant_names.exists () && !early)
3248 : {
3249 1375986 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3250 1375986 : unsigned max_loop_predicates = opt_for_fn (node->decl,
3251 : param_ipa_max_loop_predicates);
3252 :
3253 1375986 : if (dump_file && (dump_flags & TDF_DETAILS))
3254 14 : flow_loops_dump (dump_file, NULL, 0);
3255 1375986 : scev_initialize ();
3256 4748500 : for (auto loop : loops_list (cfun, 0))
3257 : {
3258 620542 : ipa_predicate loop_iterations = true;
3259 620542 : sreal header_freq;
3260 620542 : edge ex;
3261 620542 : unsigned int j;
3262 620542 : class tree_niter_desc niter_desc;
3263 620542 : if (!loop->header->aux)
3264 0 : continue;
3265 :
3266 620542 : profile_count hdr_count = loop->header->count;
3267 620542 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3268 :
3269 620542 : bb_predicate = *(ipa_predicate *)loop->header->aux;
3270 620542 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3271 1621445 : FOR_EACH_VEC_ELT (exits, j, ex)
3272 1000903 : if (number_of_iterations_exit (loop, ex, &niter_desc, false)
3273 1000903 : && !is_gimple_min_invariant (niter_desc.niter))
3274 : {
3275 152824 : ipa_predicate will_be_nonconstant
3276 152824 : = will_be_nonconstant_expr_predicate (&fbi, info,
3277 : params_summary,
3278 : niter_desc.niter,
3279 : nonconstant_names);
3280 152824 : if (will_be_nonconstant != true)
3281 61372 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3282 152824 : if (will_be_nonconstant != true
3283 214196 : && will_be_nonconstant != false)
3284 60817 : loop_iterations &= will_be_nonconstant;
3285 : }
3286 620542 : add_freqcounting_predicate (&s->loop_iterations, loop_iterations,
3287 : hdr_freq, max_loop_predicates);
3288 620542 : }
3289 :
3290 : /* To avoid quadratic behavior we analyze stride predicates only
3291 : with respect to the containing loop. Thus we simply iterate
3292 : over all defs in the outermost loop body. */
3293 1375986 : for (class loop *loop = loops_for_fn (cfun)->tree_root->inner;
3294 1863308 : loop != NULL; loop = loop->next)
3295 : {
3296 487322 : ipa_predicate loop_stride = true;
3297 487322 : basic_block *body = get_loop_body (loop);
3298 487322 : profile_count hdr_count = loop->header->count;
3299 487322 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3300 2874280 : for (unsigned i = 0; i < loop->num_nodes; i++)
3301 : {
3302 2386958 : gimple_stmt_iterator gsi;
3303 2386958 : if (!body[i]->aux)
3304 7 : continue;
3305 :
3306 2386951 : bb_predicate = *(ipa_predicate *)body[i]->aux;
3307 15753405 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
3308 10979503 : gsi_next (&gsi))
3309 : {
3310 10979503 : gimple *stmt = gsi_stmt (gsi);
3311 :
3312 10979503 : if (!is_gimple_assign (stmt))
3313 10827446 : continue;
3314 :
3315 5362299 : tree def = gimple_assign_lhs (stmt);
3316 5362299 : if (TREE_CODE (def) != SSA_NAME)
3317 1021912 : continue;
3318 :
3319 4340387 : affine_iv iv;
3320 8680774 : if (!simple_iv (loop_containing_stmt (stmt),
3321 : loop_containing_stmt (stmt),
3322 : def, &iv, true)
3323 4340387 : || is_gimple_min_invariant (iv.step))
3324 4188330 : continue;
3325 :
3326 152057 : ipa_predicate will_be_nonconstant
3327 152057 : = will_be_nonconstant_expr_predicate (&fbi, info,
3328 : params_summary,
3329 : iv.step,
3330 : nonconstant_names);
3331 152057 : if (will_be_nonconstant != true)
3332 53721 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3333 152057 : if (will_be_nonconstant != true
3334 205778 : && will_be_nonconstant != false)
3335 34849 : loop_stride = loop_stride & will_be_nonconstant;
3336 : }
3337 : }
3338 487322 : add_freqcounting_predicate (&s->loop_strides, loop_stride,
3339 : hdr_freq, max_loop_predicates);
3340 487322 : free (body);
3341 : }
3342 1375986 : scev_finalize ();
3343 : }
3344 62964706 : FOR_ALL_BB_FN (bb, my_function)
3345 : {
3346 55771977 : edge e;
3347 55771977 : edge_iterator ei;
3348 :
3349 55771977 : if (bb->aux)
3350 41248198 : edge_predicate_pool.remove ((ipa_predicate *)bb->aux);
3351 55771977 : bb->aux = NULL;
3352 118619076 : FOR_EACH_EDGE (e, ei, bb->succs)
3353 : {
3354 62847099 : if (e->aux)
3355 2454532 : edge_predicate_pool.remove ((ipa_predicate *)e->aux);
3356 62847099 : e->aux = NULL;
3357 : }
3358 : }
3359 7192729 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3360 7192729 : ipa_size_summary *ss = ipa_size_summaries->get (node);
3361 7192729 : s->time = time;
3362 7192729 : ss->self_size = size;
3363 7192729 : nonconstant_names.release ();
3364 7192729 : ipa_release_body_info (&fbi);
3365 7192729 : if (opt_for_fn (node->decl, optimize))
3366 : {
3367 6296988 : if (!early)
3368 1375986 : loop_optimizer_finalize ();
3369 4921002 : else if (!ipa_edge_args_sum)
3370 4920988 : ipa_free_all_node_params ();
3371 6296988 : free_dominance_info (CDI_DOMINATORS);
3372 6296988 : free_dominance_info (CDI_POST_DOMINATORS);
3373 : }
3374 7192729 : if (dump_file)
3375 : {
3376 247 : fprintf (dump_file, "\n");
3377 247 : ipa_dump_fn_summary (dump_file, node);
3378 : }
3379 7192729 : }
3380 :
3381 :
3382 : /* Compute function summary.
3383 : EARLY is true when we compute parameters during early opts. */
3384 :
3385 : void
3386 7194017 : compute_fn_summary (struct cgraph_node *node, bool early)
3387 : {
3388 7194017 : HOST_WIDE_INT self_stack_size;
3389 7194017 : struct cgraph_edge *e;
3390 :
3391 7194017 : gcc_assert (!node->inlined_to);
3392 :
3393 7194017 : if (!ipa_fn_summaries)
3394 214418 : ipa_fn_summary_alloc ();
3395 :
3396 : /* Create a new ipa_fn_summary. */
3397 7194017 : ((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node);
3398 7194017 : ipa_fn_summaries->remove (node);
3399 7194017 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
3400 7194017 : class ipa_size_summary *size_info = ipa_size_summaries->get_create (node);
3401 :
3402 : /* Estimate the stack size for the function if we're optimizing. */
3403 12595248 : self_stack_size = optimize && !node->thunk
3404 13491005 : ? estimated_stack_frame_size (node) : 0;
3405 7194017 : size_info->estimated_self_stack_size = self_stack_size;
3406 7194017 : info->estimated_stack_size = self_stack_size;
3407 :
3408 7194017 : if (node->thunk)
3409 : {
3410 1288 : ipa_call_summary *es = ipa_call_summaries->get_create (node->callees);
3411 1288 : ipa_predicate t = true;
3412 :
3413 1288 : node->can_change_signature = false;
3414 1288 : es->call_stmt_size = eni_size_weights.call_cost;
3415 1288 : es->call_stmt_time = eni_time_weights.call_cost;
3416 3864 : info->account_size_time (ipa_fn_summary::size_scale
3417 1288 : * opt_for_fn (node->decl,
3418 : param_uninlined_function_thunk_insns),
3419 1288 : opt_for_fn (node->decl,
3420 : param_uninlined_function_thunk_time), t, t);
3421 1288 : t = ipa_predicate::not_inlined ();
3422 1288 : info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t);
3423 1288 : ipa_update_overall_fn_summary (node);
3424 1288 : size_info->self_size = size_info->size;
3425 1288 : if (stdarg_p (TREE_TYPE (node->decl)))
3426 : {
3427 9 : info->inlinable = false;
3428 9 : node->callees->inline_failed = CIF_VARIADIC_THUNK;
3429 : }
3430 : else
3431 1279 : info->inlinable = true;
3432 : }
3433 : else
3434 : {
3435 : /* Even is_gimple_min_invariant rely on current_function_decl. */
3436 7192729 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3437 :
3438 : /* During IPA profile merging we may be called w/o virtual SSA form
3439 : built. */
3440 7192729 : update_ssa (TODO_update_ssa_only_virtuals);
3441 :
3442 : /* Can this function be inlined at all? */
3443 7192729 : if (!opt_for_fn (node->decl, optimize)
3444 8088470 : && !lookup_attribute ("always_inline",
3445 895741 : DECL_ATTRIBUTES (node->decl)))
3446 828161 : info->inlinable = false;
3447 : else
3448 6364568 : info->inlinable = tree_inlinable_function_p (node->decl);
3449 :
3450 7192729 : bool no_signature = false;
3451 :
3452 : /* Don't allow signature changes for functions which have
3453 : [[gnu::musttail]] or [[clang::musttail]] calls. Sometimes
3454 : (more often on targets which pass everything on the stack)
3455 : signature changes can result in tail calls being impossible
3456 : even when without the signature changes they would be ok.
3457 : See PR121023. */
3458 7192729 : if (cfun->has_musttail)
3459 : {
3460 1412 : if (dump_file)
3461 0 : fprintf (dump_file, "No signature change:"
3462 : " function has calls with musttail attribute.\n");
3463 : no_signature = true;
3464 : }
3465 :
3466 : /* Type attributes can use parameter indices to describe them.
3467 : Special case fn spec since we can safely preserve them in
3468 : modref summaries. */
3469 7192729 : for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl));
3470 7583040 : list && !no_signature; list = TREE_CHAIN (list))
3471 390311 : if (!ipa_param_adjustments::type_attribute_allowed_p
3472 390311 : (get_attribute_name (list)))
3473 : {
3474 157713 : if (dump_file)
3475 : {
3476 0 : fprintf (dump_file, "No signature change:"
3477 : " function type has unhandled attribute %s.\n",
3478 0 : IDENTIFIER_POINTER (get_attribute_name (list)));
3479 : }
3480 : no_signature = true;
3481 : }
3482 7192729 : for (tree parm = DECL_ARGUMENTS (node->decl);
3483 21464137 : parm && !no_signature; parm = DECL_CHAIN (parm))
3484 14271408 : if (variably_modified_type_p (TREE_TYPE (parm), node->decl))
3485 : {
3486 15631 : if (dump_file)
3487 : {
3488 0 : fprintf (dump_file, "No signature change:"
3489 : " has parameter with variably modified type.\n");
3490 : }
3491 : no_signature = true;
3492 : }
3493 :
3494 : /* Likewise for #pragma omp declare simd functions or functions
3495 : with simd attribute. */
3496 7192729 : if (no_signature
3497 14210702 : || lookup_attribute ("omp declare simd",
3498 7017973 : DECL_ATTRIBUTES (node->decl)))
3499 176444 : node->can_change_signature = false;
3500 : else
3501 : {
3502 : /* Otherwise, inlinable functions always can change signature. */
3503 7016285 : if (info->inlinable)
3504 5634655 : node->can_change_signature = true;
3505 : else
3506 : {
3507 : /* Functions calling builtin_apply cannot change signature. */
3508 5403345 : for (e = node->callees; e; e = e->next_callee)
3509 : {
3510 4054249 : tree cdecl = e->callee->decl;
3511 4054249 : if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS,
3512 : BUILT_IN_VA_START))
3513 : break;
3514 : }
3515 1381630 : node->can_change_signature = !e;
3516 : }
3517 : }
3518 7192729 : analyze_function_body (node, early);
3519 7192729 : pop_cfun ();
3520 : }
3521 :
3522 : /* Inlining characteristics are maintained by the cgraph_mark_inline. */
3523 7194017 : size_info->size = size_info->self_size;
3524 7194017 : info->estimated_stack_size = size_info->estimated_self_stack_size;
3525 :
3526 : /* Code above should compute exactly the same result as
3527 : ipa_update_overall_fn_summary except for case when speculative
3528 : edges are present since these are accounted to size but not
3529 : self_size. Do not compare time since different order the roundoff
3530 : errors result in slight changes. */
3531 7194017 : ipa_update_overall_fn_summary (node);
3532 7194017 : if (flag_checking)
3533 : {
3534 7671158 : for (e = node->indirect_calls; e; e = e->next_callee)
3535 477223 : if (e->speculative)
3536 : break;
3537 7193935 : gcc_assert (e || size_info->size == size_info->self_size);
3538 : }
3539 7194017 : }
3540 :
3541 :
3542 : /* Compute parameters of functions used by inliner using
3543 : current_function_decl. */
3544 :
3545 : static unsigned int
3546 5757227 : compute_fn_summary_for_current (void)
3547 : {
3548 5757227 : compute_fn_summary (cgraph_node::get (current_function_decl), true);
3549 5757227 : return 0;
3550 : }
3551 :
3552 : /* Estimate benefit devirtualizing indirect edge IE and return true if it can
3553 : be devirtualized and inlined, provided m_known_vals, m_known_contexts and
3554 : m_known_aggs in AVALS. Return false straight away if AVALS is NULL. */
3555 :
3556 : static bool
3557 4668191 : estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3558 : int *size, int *time,
3559 : ipa_call_arg_values *avals)
3560 : {
3561 4668191 : tree target;
3562 4668191 : struct cgraph_node *callee;
3563 4668191 : class ipa_fn_summary *isummary;
3564 4668191 : enum availability avail;
3565 4668191 : bool speculative;
3566 :
3567 4668191 : if (!avals
3568 5807093 : || (!avals->m_known_vals.length() && !avals->m_known_contexts.length ()))
3569 : return false;
3570 1335413 : if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3571 : return false;
3572 :
3573 1329046 : target = ipa_get_indirect_edge_target
3574 1329046 : (ie->callee ? ie->speculative_call_indirect_edge () : ie,
3575 : avals, &speculative);
3576 1329046 : if (!target || speculative)
3577 : return false;
3578 :
3579 : /* If this is speculative call, turn its cost into 0; we will account
3580 : the call when processing the indirect call. */
3581 217158 : if (ie->callee)
3582 : {
3583 4081 : gcc_checking_assert (ie->speculative && *size > 0);
3584 4081 : *size = 0;
3585 4081 : *time = 0;
3586 : }
3587 : else
3588 : {
3589 : /* Account for difference in cost between indirect and direct calls. */
3590 213077 : *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3591 213077 : *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3592 : }
3593 217158 : gcc_checking_assert (*time >= 0);
3594 217158 : gcc_checking_assert (*size >= 0);
3595 :
3596 217158 : callee = cgraph_node::get (target);
3597 217158 : if (!callee || !callee->definition)
3598 : return false;
3599 196549 : callee = callee->function_symbol (&avail);
3600 196549 : if (avail < AVAIL_AVAILABLE)
3601 : return false;
3602 196523 : isummary = ipa_fn_summaries->get (callee);
3603 196523 : if (isummary == NULL)
3604 : return false;
3605 :
3606 196511 : return isummary->inlinable;
3607 : }
3608 :
3609 : /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3610 : handle edge E with probability PROB. Set HINTS accordingly if edge may be
3611 : devirtualized. AVALS, if non-NULL, describes the context of the call site
3612 : as far as values of parameters are concerened. */
3613 :
3614 : static inline void
3615 215142915 : estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3616 : sreal *time, ipa_call_arg_values *avals,
3617 : ipa_hints *hints)
3618 : {
3619 215142915 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3620 215142915 : int call_size = es->call_stmt_size;
3621 215142915 : int call_time = es->call_stmt_time;
3622 215142915 : int cur_size;
3623 :
3624 211034743 : if ((!e->callee || e->speculative)
3625 215702934 : && estimate_edge_devirt_benefit (e, &call_size, &call_time, avals))
3626 : {
3627 196169 : if (hints && e->maybe_hot_p ())
3628 186923 : *hints |= INLINE_HINT_indirect_call;
3629 : }
3630 215142915 : cur_size = call_size * ipa_fn_summary::size_scale;
3631 215142915 : *size += cur_size;
3632 215142915 : if (min_size)
3633 27824686 : *min_size += cur_size;
3634 215142915 : if (time)
3635 205691568 : *time += ((sreal)call_time) * e->sreal_frequency ();
3636 215142915 : }
3637 :
3638 :
3639 : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3640 : calls in NODE. POSSIBLE_TRUTHS and AVALS describe the context of the call
3641 : site.
3642 :
3643 : Helper for estimate_calls_size_and_time which does the same but
3644 : (in most cases) faster. */
3645 :
3646 : static void
3647 76614705 : estimate_calls_size_and_time_1 (struct cgraph_node *node, int *size,
3648 : int *min_size, sreal *time,
3649 : ipa_hints *hints,
3650 : clause_t possible_truths,
3651 : ipa_call_arg_values *avals)
3652 : {
3653 76614705 : struct cgraph_edge *e;
3654 366021330 : for (e = node->callees; e; e = e->next_callee)
3655 : {
3656 289406625 : if (!e->inline_failed)
3657 : {
3658 56580383 : gcc_checking_assert (!ipa_call_summaries->get (e));
3659 56580383 : estimate_calls_size_and_time_1 (e->callee, size, min_size, time,
3660 : hints, possible_truths, avals);
3661 :
3662 56580383 : continue;
3663 : }
3664 232826242 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3665 :
3666 : /* Do not care about zero sized builtins. */
3667 232826242 : if (!es->call_stmt_size)
3668 : {
3669 30176056 : gcc_checking_assert (!es->call_stmt_time);
3670 30176056 : continue;
3671 : }
3672 202650186 : if (!es->predicate
3673 202650186 : || es->predicate->evaluate (possible_truths))
3674 : {
3675 : /* Predicates of calls shall not use NOT_CHANGED codes,
3676 : so we do not need to compute probabilities. */
3677 200009064 : estimate_edge_size_and_time (e, size,
3678 200009064 : es->predicate ? NULL : min_size,
3679 : time, avals, hints);
3680 : }
3681 : }
3682 80443520 : for (e = node->indirect_calls; e; e = e->next_callee)
3683 : {
3684 3828815 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3685 3828815 : if (!es->predicate
3686 3828815 : || es->predicate->evaluate (possible_truths))
3687 3797356 : estimate_edge_size_and_time (e, size,
3688 3797356 : es->predicate ? NULL : min_size,
3689 : time, avals, hints);
3690 : }
3691 76614705 : }
3692 :
3693 : /* Populate sum->call_size_time_table for edges from NODE. */
3694 :
3695 : static void
3696 3117945 : summarize_calls_size_and_time (struct cgraph_node *node,
3697 : ipa_fn_summary *sum)
3698 : {
3699 3117945 : struct cgraph_edge *e;
3700 14682021 : for (e = node->callees; e; e = e->next_callee)
3701 : {
3702 11564076 : if (!e->inline_failed)
3703 : {
3704 1395253 : gcc_checking_assert (!ipa_call_summaries->get (e));
3705 1395253 : summarize_calls_size_and_time (e->callee, sum);
3706 1395253 : continue;
3707 : }
3708 10168823 : int size = 0;
3709 10168823 : sreal time = 0;
3710 :
3711 10168823 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3712 :
3713 10168823 : ipa_predicate pred = true;
3714 10168823 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3715 :
3716 10168823 : if (es->predicate)
3717 1834399 : pred = *es->predicate;
3718 10168823 : sum->account_size_time (size, time, pred, pred, true);
3719 : }
3720 3428761 : for (e = node->indirect_calls; e; e = e->next_callee)
3721 : {
3722 310816 : int size = 0;
3723 310816 : sreal time = 0;
3724 :
3725 310816 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3726 310816 : ipa_predicate pred = true;
3727 310816 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3728 :
3729 310816 : if (es->predicate)
3730 78991 : pred = *es->predicate;
3731 310816 : sum->account_size_time (size, time, pred, pred, true);
3732 : }
3733 3117945 : }
3734 :
3735 : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3736 : calls in NODE. POSSIBLE_TRUTHS and AVALS (the latter if non-NULL) describe
3737 : context of the call site. */
3738 :
3739 : static void
3740 20034343 : estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3741 : int *min_size, sreal *time,
3742 : ipa_hints *hints,
3743 : clause_t possible_truths,
3744 : ipa_call_arg_values *avals)
3745 : {
3746 20034343 : class ipa_fn_summary *sum = ipa_fn_summaries->get (node);
3747 20034343 : bool use_table = true;
3748 :
3749 20034343 : gcc_assert (node->callees || node->indirect_calls);
3750 :
3751 : /* During early inlining we do not calculate info for very
3752 : large functions and thus there is no need for producing
3753 : summaries. */
3754 20034343 : if (!ipa_node_params_sum)
3755 : use_table = false;
3756 : /* Do not calculate summaries for simple wrappers; it is waste
3757 : of memory. */
3758 10974488 : else if (node->callees && !node->indirect_calls
3759 10242161 : && node->callees->inline_failed && !node->callees->next_callee)
3760 : use_table = false;
3761 : /* If there is an indirect edge that may be optimized, we need
3762 : to go the slow way. */
3763 8835084 : else if (avals
3764 8835084 : && (avals->m_known_vals.length ()
3765 3187271 : || avals->m_known_contexts.length ()
3766 3047142 : || avals->m_known_aggs.length ()))
3767 : {
3768 4272744 : ipa_node_params *params_summary = ipa_node_params_sum->get (node);
3769 4272744 : unsigned int nargs = params_summary
3770 4272744 : ? ipa_get_param_count (params_summary) : 0;
3771 :
3772 14286352 : for (unsigned int i = 0; i < nargs && use_table; i++)
3773 : {
3774 10013608 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3775 10013608 : && (avals->safe_sval_at (i)
3776 255008 : || (ipa_argagg_value_list (avals).value_for_index_p (i))))
3777 : use_table = false;
3778 9918224 : else if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3779 9918224 : && (avals->m_known_contexts.length () > i
3780 10165117 : && !avals->m_known_contexts[i].useless_p ()))
3781 : use_table = false;
3782 : }
3783 : }
3784 :
3785 : /* Fast path is via the call size time table. */
3786 4272744 : if (use_table)
3787 : {
3788 : /* Build summary if it is absent. */
3789 8588362 : if (!sum->call_size_time_table.length ())
3790 : {
3791 865836 : ipa_predicate true_pred = true;
3792 865836 : sum->account_size_time (0, 0, true_pred, true_pred, true);
3793 865836 : summarize_calls_size_and_time (node, sum);
3794 : }
3795 :
3796 8588362 : int old_size = *size;
3797 8588362 : sreal old_time = time ? *time : 0;
3798 :
3799 8588362 : if (min_size)
3800 8588362 : *min_size += sum->call_size_time_table[0].size;
3801 :
3802 : unsigned int i;
3803 : size_time_entry *e;
3804 :
3805 : /* Walk the table and account sizes and times. */
3806 22902391 : for (i = 0; sum->call_size_time_table.iterate (i, &e);
3807 : i++)
3808 14314029 : if (e->exec_predicate.evaluate (possible_truths))
3809 : {
3810 13192937 : *size += e->size;
3811 13192937 : if (time)
3812 11127471 : *time += e->time;
3813 : }
3814 :
3815 : /* Be careful and see if both methods agree. */
3816 21 : if ((flag_checking || dump_file)
3817 : /* Do not try to sanity check when we know we lost some
3818 : precision. */
3819 8588362 : && sum->call_size_time_table.length ()
3820 : < ipa_fn_summary::max_size_time_table_size)
3821 : {
3822 8588341 : estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL,
3823 : possible_truths, avals);
3824 8588341 : gcc_assert (*size == old_size);
3825 15813271 : if (time && (*time - old_time > 1 || *time - old_time < -1)
3826 8588370 : && dump_file)
3827 0 : fprintf (dump_file, "Time mismatch in call summary %f!=%f\n",
3828 : old_time.to_double (),
3829 : time->to_double ());
3830 : }
3831 : }
3832 : /* Slow path by walking all edges. */
3833 : else
3834 11445981 : estimate_calls_size_and_time_1 (node, size, min_size, time, hints,
3835 : possible_truths, avals);
3836 20034343 : }
3837 :
3838 : /* Main constructor for ipa call context. Memory allocation of ARG_VALUES
3839 : is owned by the caller. INLINE_PARAM_SUMMARY is also owned by the
3840 : caller. */
3841 :
3842 19231438 : ipa_call_context::ipa_call_context (cgraph_node *node, clause_t possible_truths,
3843 : clause_t nonspec_possible_truths,
3844 : vec<inline_param_summary>
3845 : inline_param_summary,
3846 19231438 : ipa_auto_call_arg_values *arg_values)
3847 19231438 : : m_node (node), m_possible_truths (possible_truths),
3848 19231438 : m_nonspec_possible_truths (nonspec_possible_truths),
3849 19231438 : m_inline_param_summary (inline_param_summary),
3850 19231438 : m_avals (arg_values)
3851 : {
3852 19231438 : }
3853 :
3854 : /* Set THIS to be a duplicate of CTX. Copy all relevant info. */
3855 :
3856 : void
3857 1896270 : ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx)
3858 : {
3859 1896270 : m_node = ctx.m_node;
3860 1896270 : m_possible_truths = ctx.m_possible_truths;
3861 1896270 : m_nonspec_possible_truths = ctx.m_nonspec_possible_truths;
3862 1896270 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3863 1896270 : unsigned int nargs = params_summary
3864 1896270 : ? ipa_get_param_count (params_summary) : 0;
3865 :
3866 1896270 : m_inline_param_summary = vNULL;
3867 : /* Copy the info only if there is at least one useful entry. */
3868 1896270 : if (ctx.m_inline_param_summary.exists ())
3869 : {
3870 1685369 : unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs);
3871 :
3872 3821366 : for (unsigned int i = 0; i < n; i++)
3873 3128423 : if (ipa_is_param_used_by_ipa_predicates (params_summary, i)
3874 4434591 : && !ctx.m_inline_param_summary[i].useless_p ())
3875 : {
3876 992426 : m_inline_param_summary
3877 992426 : = ctx.m_inline_param_summary.copy ();
3878 992426 : break;
3879 : }
3880 : }
3881 1896270 : m_avals.m_known_vals = vNULL;
3882 1896270 : if (ctx.m_avals.m_known_vals.exists ())
3883 : {
3884 1896270 : unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs);
3885 :
3886 4387054 : for (unsigned int i = 0; i < n; i++)
3887 2517183 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3888 2517183 : && ctx.m_avals.m_known_vals[i])
3889 : {
3890 26399 : m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy ();
3891 26399 : break;
3892 : }
3893 : }
3894 :
3895 1896270 : m_avals.m_known_contexts = vNULL;
3896 1896270 : if (ctx.m_avals.m_known_contexts.exists ())
3897 : {
3898 1896270 : unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs);
3899 :
3900 1898217 : for (unsigned int i = 0; i < n; i++)
3901 26202 : if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3902 26202 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3903 : {
3904 24255 : m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy ();
3905 24255 : break;
3906 : }
3907 : }
3908 :
3909 1896270 : m_avals.m_known_aggs = vNULL;
3910 1896270 : if (ctx.m_avals.m_known_aggs.exists ())
3911 : {
3912 1896270 : const ipa_argagg_value_list avl (&ctx.m_avals);
3913 6257185 : for (unsigned int i = 0; i < nargs; i++)
3914 4363831 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3915 4363831 : && avl.value_for_index_p (i))
3916 : {
3917 2916 : m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy ();
3918 2916 : break;
3919 : }
3920 : }
3921 :
3922 1896270 : m_avals.m_known_value_ranges = vNULL;
3923 1896270 : }
3924 :
3925 : /* Release memory used by known_vals/contexts/aggs vectors. and
3926 : inline_param_summary. */
3927 :
3928 : void
3929 3080386 : ipa_cached_call_context::release ()
3930 : {
3931 : /* See if context is initialized at first place. */
3932 3080386 : if (!m_node)
3933 : return;
3934 1896270 : m_avals.m_known_aggs.release ();
3935 1896270 : m_avals.m_known_vals.release ();
3936 1896270 : m_avals.m_known_contexts.release ();
3937 1896270 : m_inline_param_summary.release ();
3938 : }
3939 :
3940 : /* Return true if CTX describes the same call context as THIS. */
3941 :
3942 : bool
3943 6983445 : ipa_call_context::equal_to (const ipa_call_context &ctx)
3944 : {
3945 6983445 : if (m_node != ctx.m_node
3946 5799329 : || m_possible_truths != ctx.m_possible_truths
3947 5219366 : || m_nonspec_possible_truths != ctx.m_nonspec_possible_truths)
3948 : return false;
3949 :
3950 5219366 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3951 5219366 : unsigned int nargs = params_summary
3952 5219366 : ? ipa_get_param_count (params_summary) : 0;
3953 :
3954 5219366 : if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ())
3955 : {
3956 15636563 : for (unsigned int i = 0; i < nargs; i++)
3957 : {
3958 10747895 : if (!ipa_is_param_used_by_ipa_predicates (params_summary, i))
3959 3381445 : continue;
3960 7366450 : if (i >= m_inline_param_summary.length ()
3961 5345422 : || m_inline_param_summary[i].useless_p ())
3962 : {
3963 2950839 : if (i < ctx.m_inline_param_summary.length ()
3964 2950839 : && !ctx.m_inline_param_summary[i].useless_p ())
3965 : return false;
3966 2904410 : continue;
3967 : }
3968 4415611 : if (i >= ctx.m_inline_param_summary.length ()
3969 4415608 : || ctx.m_inline_param_summary[i].useless_p ())
3970 : {
3971 45937 : if (i < m_inline_param_summary.length ()
3972 45937 : && !m_inline_param_summary[i].useless_p ())
3973 : return false;
3974 0 : continue;
3975 : }
3976 4369674 : if (!m_inline_param_summary[i].equal_to
3977 4369674 : (ctx.m_inline_param_summary[i]))
3978 : return false;
3979 : }
3980 : }
3981 5100469 : if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ())
3982 : {
3983 15643373 : for (unsigned int i = 0; i < nargs; i++)
3984 : {
3985 10553351 : if (!ipa_is_param_used_by_indirect_call (params_summary, i))
3986 10358452 : continue;
3987 194899 : if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i])
3988 : {
3989 138795 : if (i < ctx.m_avals.m_known_vals.length ()
3990 138795 : && ctx.m_avals.m_known_vals[i])
3991 : return false;
3992 138714 : continue;
3993 : }
3994 56104 : if (i >= ctx.m_avals.m_known_vals.length ()
3995 56104 : || !ctx.m_avals.m_known_vals[i])
3996 : {
3997 : if (i < m_avals.m_known_vals.length () && m_avals.m_known_vals[i])
3998 : return false;
3999 : continue;
4000 : }
4001 56020 : if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i])
4002 : return false;
4003 : }
4004 : }
4005 5090022 : if (m_avals.m_known_contexts.exists ()
4006 5090022 : || ctx.m_avals.m_known_contexts.exists ())
4007 : {
4008 15628472 : for (unsigned int i = 0; i < nargs; i++)
4009 : {
4010 10539827 : if (!ipa_is_param_used_by_polymorphic_call (params_summary, i))
4011 10416634 : continue;
4012 123193 : if (i >= m_avals.m_known_contexts.length ()
4013 122026 : || m_avals.m_known_contexts[i].useless_p ())
4014 : {
4015 1167 : if (i < ctx.m_avals.m_known_contexts.length ()
4016 1167 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
4017 : return false;
4018 1158 : continue;
4019 : }
4020 122026 : if (i >= ctx.m_avals.m_known_contexts.length ()
4021 122026 : || ctx.m_avals.m_known_contexts[i].useless_p ())
4022 : {
4023 8 : if (i < m_avals.m_known_contexts.length ()
4024 1896278 : && !m_avals.m_known_contexts[i].useless_p ())
4025 : return false;
4026 0 : continue;
4027 : }
4028 122018 : if (!m_avals.m_known_contexts[i].equal_to
4029 122018 : (ctx.m_avals.m_known_contexts[i]))
4030 : return false;
4031 : }
4032 : }
4033 5088645 : if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ())
4034 : {
4035 : unsigned i = 0, j = 0;
4036 11716186 : while (i < m_avals.m_known_aggs.length ()
4037 5857393 : || j < ctx.m_avals.m_known_aggs.length ())
4038 : {
4039 770218 : if (i >= m_avals.m_known_aggs.length ())
4040 : {
4041 765734 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4042 765734 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4043 : return false;
4044 765134 : j++;
4045 765134 : continue;
4046 765134 : }
4047 4484 : if (j >= ctx.m_avals.m_known_aggs.length ())
4048 : {
4049 631 : int idx1 = m_avals.m_known_aggs[i].index;
4050 631 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4051 : return false;
4052 8 : i++;
4053 8 : continue;
4054 8 : }
4055 :
4056 3853 : int idx1 = m_avals.m_known_aggs[i].index;
4057 3853 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4058 3853 : if (idx1 < idx2)
4059 : {
4060 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4061 : return false;
4062 0 : i++;
4063 0 : continue;
4064 : }
4065 3853 : if (idx1 > idx2)
4066 : {
4067 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4068 : return false;
4069 0 : j++;
4070 0 : continue;
4071 : }
4072 3853 : if (!ipa_is_param_used_by_indirect_call (params_summary, idx1))
4073 : {
4074 476 : i++;
4075 476 : j++;
4076 476 : continue;
4077 : }
4078 :
4079 3377 : if ((m_avals.m_known_aggs[i].unit_offset
4080 3377 : != ctx.m_avals.m_known_aggs[j].unit_offset)
4081 3369 : || (m_avals.m_known_aggs[i].by_ref
4082 3369 : != ctx.m_avals.m_known_aggs[j].by_ref)
4083 6746 : || !operand_equal_p (m_avals.m_known_aggs[i].value,
4084 3369 : ctx.m_avals.m_known_aggs[j].value))
4085 247 : return false;
4086 3130 : i++;
4087 3130 : j++;
4088 : }
4089 : }
4090 : return true;
4091 : }
4092 :
4093 : /* Fill in the selected fields in ESTIMATES with value estimated for call in
4094 : this context. Always compute size and min_size. Only compute time and
4095 : nonspecialized_time if EST_TIMES is true. Only compute hints if EST_HINTS
4096 : is true. */
4097 :
4098 : void
4099 19203735 : ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates,
4100 : bool est_times, bool est_hints)
4101 : {
4102 19203735 : class ipa_fn_summary *info = ipa_fn_summaries->get (m_node);
4103 19203735 : size_time_entry *e;
4104 19203735 : int size = 0;
4105 19203735 : sreal time = 0;
4106 19203735 : int min_size = 0;
4107 19203735 : ipa_hints hints = 0;
4108 19203735 : sreal loops_with_known_iterations = 0;
4109 19203735 : sreal loops_with_known_strides = 0;
4110 19203735 : int i;
4111 :
4112 19203735 : if (dump_file && (dump_flags & TDF_DETAILS))
4113 : {
4114 1332 : bool found = false;
4115 1332 : fprintf (dump_file, " Estimating body: %s\n"
4116 : " Known to be false: ", m_node->dump_name ());
4117 :
4118 4287 : for (i = ipa_predicate::not_inlined_condition;
4119 8574 : i < (ipa_predicate::first_dynamic_condition
4120 7002 : + (int) vec_safe_length (info->conds)); i++)
4121 2955 : if (!(m_possible_truths & (1 << i)))
4122 : {
4123 1769 : if (found)
4124 504 : fprintf (dump_file, ", ");
4125 1769 : found = true;
4126 1769 : dump_condition (dump_file, info->conds, i);
4127 : }
4128 : }
4129 :
4130 19203735 : if (m_node->callees || m_node->indirect_calls)
4131 25156727 : estimate_calls_size_and_time (m_node, &size, &min_size,
4132 : est_times ? &time : NULL,
4133 : est_hints ? &hints : NULL, m_possible_truths,
4134 : &m_avals);
4135 :
4136 19203735 : sreal nonspecialized_time = time;
4137 :
4138 19203735 : min_size += info->size_time_table[0].size;
4139 132462877 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4140 : {
4141 113259142 : bool exec = e->exec_predicate.evaluate (m_nonspec_possible_truths);
4142 :
4143 : /* Because predicates are conservative, it can happen that nonconst is 1
4144 : but exec is 0. */
4145 113259142 : if (exec)
4146 : {
4147 110788794 : bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths);
4148 :
4149 110788794 : gcc_checking_assert (e->time >= 0);
4150 110788794 : gcc_checking_assert (time >= 0);
4151 :
4152 : /* We compute specialized size only because size of nonspecialized
4153 : copy is context independent.
4154 :
4155 : The difference between nonspecialized execution and specialized is
4156 : that nonspecialized is not going to have optimized out computations
4157 : known to be constant in a specialized setting. */
4158 110788794 : if (nonconst)
4159 55610235 : size += e->size;
4160 110788794 : if (!est_times)
4161 55999727 : continue;
4162 54789067 : nonspecialized_time += e->time;
4163 54789067 : if (!nonconst)
4164 : ;
4165 26241386 : else if (!m_inline_param_summary.exists ())
4166 : {
4167 2515623 : if (nonconst)
4168 2515623 : time += e->time;
4169 : }
4170 : else
4171 : {
4172 23725763 : int prob = e->nonconst_predicate.probability
4173 23725763 : (info->conds, m_possible_truths,
4174 : m_inline_param_summary);
4175 23725763 : gcc_checking_assert (prob >= 0);
4176 23725763 : gcc_checking_assert (prob <= REG_BR_PROB_BASE);
4177 23725763 : if (prob == REG_BR_PROB_BASE)
4178 19829447 : time += e->time;
4179 : else
4180 3896316 : time += e->time * prob / REG_BR_PROB_BASE;
4181 : }
4182 54789067 : gcc_checking_assert (time >= 0);
4183 : }
4184 : }
4185 19203735 : gcc_checking_assert (info->size_time_table[0].exec_predicate == true);
4186 19203735 : gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true);
4187 19203735 : gcc_checking_assert (min_size >= 0);
4188 19203735 : gcc_checking_assert (size >= 0);
4189 19203735 : gcc_checking_assert (time >= 0);
4190 : /* nonspecialized_time should be always bigger than specialized time.
4191 : Roundoff issues however may get into the way. */
4192 19203735 : gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1);
4193 :
4194 : /* Roundoff issues may make specialized time bigger than nonspecialized
4195 : time. We do not really want that to happen because some heuristics
4196 : may get confused by seeing negative speedups. */
4197 19203735 : if (time > nonspecialized_time)
4198 0 : time = nonspecialized_time;
4199 :
4200 19203735 : if (est_hints)
4201 : {
4202 7241759 : if (info->scc_no)
4203 227145 : hints |= INLINE_HINT_in_scc;
4204 7241759 : if (DECL_DECLARED_INLINE_P (m_node->decl))
4205 4588674 : hints |= INLINE_HINT_declared_inline;
4206 7241759 : if (info->builtin_constant_p_parms.length ()
4207 8895 : && DECL_DECLARED_INLINE_P (m_node->decl))
4208 8837 : hints |= INLINE_HINT_builtin_constant_p;
4209 :
4210 : ipa_freqcounting_predicate *fcp;
4211 7790063 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
4212 548304 : if (!fcp->predicate->evaluate (m_possible_truths))
4213 : {
4214 369899 : hints |= INLINE_HINT_loop_iterations;
4215 369899 : loops_with_known_iterations += fcp->freq;
4216 : }
4217 7241759 : estimates->loops_with_known_iterations = loops_with_known_iterations;
4218 :
4219 7523871 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
4220 282112 : if (!fcp->predicate->evaluate (m_possible_truths))
4221 : {
4222 261446 : hints |= INLINE_HINT_loop_stride;
4223 261446 : loops_with_known_strides += fcp->freq;
4224 : }
4225 7241759 : estimates->loops_with_known_strides = loops_with_known_strides;
4226 : }
4227 :
4228 19203735 : size = RDIV (size, ipa_fn_summary::size_scale);
4229 19203735 : min_size = RDIV (min_size, ipa_fn_summary::size_scale);
4230 :
4231 19203735 : if (dump_file && (dump_flags & TDF_DETAILS))
4232 : {
4233 1332 : fprintf (dump_file, "\n size:%i", (int) size);
4234 1332 : if (est_times)
4235 1090 : fprintf (dump_file, " time:%f nonspec time:%f",
4236 : time.to_double (), nonspecialized_time.to_double ());
4237 1332 : if (est_hints)
4238 1090 : fprintf (dump_file, " loops with known iterations:%f "
4239 : "known strides:%f", loops_with_known_iterations.to_double (),
4240 : loops_with_known_strides.to_double ());
4241 1332 : fprintf (dump_file, "\n");
4242 : }
4243 19203735 : if (est_times)
4244 : {
4245 7241759 : estimates->time = time;
4246 7241759 : estimates->nonspecialized_time = nonspecialized_time;
4247 : }
4248 19203735 : estimates->size = size;
4249 19203735 : estimates->min_size = min_size;
4250 19203735 : if (est_hints)
4251 7241759 : estimates->hints = hints;
4252 19203735 : return;
4253 : }
4254 :
4255 :
4256 : /* Estimate size and time needed to execute callee of EDGE assuming that
4257 : parameters known to be constant at caller of EDGE are propagated.
4258 : KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
4259 : and types for parameters. */
4260 :
4261 : void
4262 282216 : estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
4263 : ipa_auto_call_arg_values *avals,
4264 : ipa_call_estimates *estimates)
4265 : {
4266 282216 : clause_t clause, nonspec_clause;
4267 :
4268 282216 : evaluate_conditions_for_known_args (node, false, avals, &clause,
4269 : &nonspec_clause, NULL);
4270 282216 : ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals);
4271 282216 : ctx.estimate_size_and_time (estimates);
4272 282216 : }
4273 :
4274 : /* Return stack frame offset where frame of NODE is supposed to start inside
4275 : of the function it is inlined to.
4276 : Return 0 for functions that are not inlined. */
4277 :
4278 : HOST_WIDE_INT
4279 5089532 : ipa_get_stack_frame_offset (struct cgraph_node *node)
4280 : {
4281 5089532 : HOST_WIDE_INT offset = 0;
4282 5089532 : if (!node->inlined_to)
4283 : return 0;
4284 3953333 : node = node->callers->caller;
4285 5123055 : while (true)
4286 : {
4287 5123055 : offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
4288 4538194 : if (!node->inlined_to)
4289 : return offset;
4290 584861 : node = node->callers->caller;
4291 : }
4292 : }
4293 :
4294 :
4295 : /* Update summary information of inline clones after inlining.
4296 : Compute peak stack usage. */
4297 :
4298 : static void
4299 4644598 : inline_update_callee_summaries (struct cgraph_node *node, int depth)
4300 : {
4301 4644598 : struct cgraph_edge *e;
4302 :
4303 4644598 : ipa_propagate_frequency (node);
4304 8975536 : for (e = node->callees; e; e = e->next_callee)
4305 : {
4306 4330938 : if (!e->inline_failed)
4307 691819 : inline_update_callee_summaries (e->callee, depth);
4308 : else
4309 3639119 : ipa_call_summaries->get (e)->loop_depth += depth;
4310 : }
4311 4746595 : for (e = node->indirect_calls; e; e = e->next_callee)
4312 101997 : ipa_call_summaries->get (e)->loop_depth += depth;
4313 4644598 : }
4314 :
4315 : /* Update change_prob and points_to_local_or_readonly_memory of EDGE after
4316 : INLINED_EDGE has been inlined.
4317 :
4318 : When function A is inlined in B and A calls C with parameter that
4319 : changes with probability PROB1 and C is known to be passthrough
4320 : of argument if B that change with probability PROB2, the probability
4321 : of change is now PROB1*PROB2. */
4322 :
4323 : static void
4324 3741429 : remap_edge_params (struct cgraph_edge *inlined_edge,
4325 : struct cgraph_edge *edge)
4326 : {
4327 3741429 : if (ipa_node_params_sum)
4328 : {
4329 2689650 : int i;
4330 2689650 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4331 2689650 : if (!args)
4332 : return;
4333 1507736 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4334 1507736 : class ipa_call_summary *inlined_es
4335 1507736 : = ipa_call_summaries->get (inlined_edge);
4336 :
4337 1507736 : if (es->param.length () == 0)
4338 : return;
4339 :
4340 8778976 : for (i = 0; i < ipa_get_cs_argument_count (args); i++)
4341 : {
4342 2956910 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4343 2956910 : if (jfunc->type == IPA_JF_PASS_THROUGH
4344 2163026 : || jfunc->type == IPA_JF_ANCESTOR)
4345 : {
4346 928707 : int id = jfunc->type == IPA_JF_PASS_THROUGH
4347 928707 : ? ipa_get_jf_pass_through_formal_id (jfunc)
4348 134823 : : ipa_get_jf_ancestor_formal_id (jfunc);
4349 1857322 : if (id < (int) inlined_es->param.length ())
4350 : {
4351 928607 : int prob1 = es->param[i].change_prob;
4352 928607 : int prob2 = inlined_es->param[id].change_prob;
4353 928607 : int prob = combine_probabilities (prob1, prob2);
4354 :
4355 928607 : if (prob1 && prob2 && !prob)
4356 928607 : prob = 1;
4357 :
4358 928607 : es->param[i].change_prob = prob;
4359 :
4360 1857214 : if (inlined_es
4361 928607 : ->param[id].points_to_local_or_readonly_memory)
4362 201933 : es->param[i].points_to_local_or_readonly_memory = true;
4363 1857214 : if (inlined_es
4364 928607 : ->param[id].points_to_possible_sra_candidate)
4365 164538 : es->param[i].points_to_possible_sra_candidate = true;
4366 : }
4367 928707 : if (!es->param[i].points_to_local_or_readonly_memory
4368 : && jfunc->type == IPA_JF_CONST
4369 : && points_to_local_or_readonly_memory_p
4370 : (ipa_get_jf_constant (jfunc)))
4371 : es->param[i].points_to_local_or_readonly_memory = true;
4372 : }
4373 : }
4374 : }
4375 : }
4376 :
4377 : /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
4378 :
4379 : Remap predicates of callees of NODE. Rest of arguments match
4380 : remap_predicate.
4381 :
4382 : Also update change probabilities. */
4383 :
4384 : static void
4385 4644598 : remap_edge_summaries (struct cgraph_edge *inlined_edge,
4386 : struct cgraph_node *node,
4387 : class ipa_fn_summary *info,
4388 : class ipa_node_params *params_summary,
4389 : class ipa_fn_summary *callee_info,
4390 : const vec<int> &operand_map,
4391 : const vec<HOST_WIDE_INT> &offset_map,
4392 : clause_t possible_truths,
4393 : ipa_predicate *toplev_predicate)
4394 : {
4395 4644598 : struct cgraph_edge *e, *next;
4396 8975112 : for (e = node->callees; e; e = next)
4397 : {
4398 4330514 : ipa_predicate p;
4399 4330514 : next = e->next_callee;
4400 :
4401 4330514 : if (e->inline_failed)
4402 : {
4403 3638695 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4404 3638695 : remap_edge_params (inlined_edge, e);
4405 :
4406 3638695 : if (es->predicate)
4407 : {
4408 1268039 : p = es->predicate->remap_after_inlining
4409 1268039 : (info, params_summary,
4410 : callee_info, operand_map,
4411 : offset_map, possible_truths,
4412 : *toplev_predicate);
4413 1268039 : edge_set_predicate (e, &p);
4414 : }
4415 : else
4416 2370656 : edge_set_predicate (e, toplev_predicate);
4417 : }
4418 : else
4419 691819 : remap_edge_summaries (inlined_edge, e->callee, info,
4420 : params_summary, callee_info,
4421 : operand_map, offset_map, possible_truths,
4422 : toplev_predicate);
4423 : }
4424 4747332 : for (e = node->indirect_calls; e; e = next)
4425 : {
4426 102734 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4427 102734 : ipa_predicate p;
4428 102734 : next = e->next_callee;
4429 :
4430 102734 : remap_edge_params (inlined_edge, e);
4431 102734 : if (es->predicate)
4432 : {
4433 27387 : p = es->predicate->remap_after_inlining
4434 27387 : (info, params_summary,
4435 : callee_info, operand_map, offset_map,
4436 : possible_truths, *toplev_predicate);
4437 27387 : edge_set_predicate (e, &p);
4438 : }
4439 : else
4440 75347 : edge_set_predicate (e, toplev_predicate);
4441 : }
4442 4644598 : }
4443 :
4444 : /* Run remap_after_inlining on each predicate in V. */
4445 :
4446 : static void
4447 7905558 : remap_freqcounting_predicate (class ipa_fn_summary *info,
4448 : class ipa_node_params *params_summary,
4449 : class ipa_fn_summary *callee_info,
4450 : vec<ipa_freqcounting_predicate, va_gc> *v,
4451 : const vec<int> &operand_map,
4452 : const vec<HOST_WIDE_INT> &offset_map,
4453 : clause_t possible_truths,
4454 : ipa_predicate *toplev_predicate)
4455 :
4456 : {
4457 7905558 : ipa_freqcounting_predicate *fcp;
4458 7932883 : for (int i = 0; vec_safe_iterate (v, i, &fcp); i++)
4459 : {
4460 27325 : ipa_predicate p
4461 27325 : = fcp->predicate->remap_after_inlining (info, params_summary,
4462 : callee_info, operand_map,
4463 : offset_map, possible_truths,
4464 : *toplev_predicate);
4465 38193 : if (p != false && p != true)
4466 2795 : *fcp->predicate &= p;
4467 : }
4468 7905558 : }
4469 :
4470 : /* We inlined EDGE. Update summary of the function we inlined into. */
4471 :
4472 : void
4473 3952779 : ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
4474 : {
4475 3952779 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
4476 3677032 : struct cgraph_node *to = (edge->caller->inlined_to
4477 3952779 : ? edge->caller->inlined_to : edge->caller);
4478 3952779 : class ipa_fn_summary *info = ipa_fn_summaries->get (to);
4479 3952779 : clause_t clause = 0; /* not_inline is known to be false. */
4480 3952779 : size_time_entry *e;
4481 3952779 : auto_vec<int, 8> operand_map;
4482 3952779 : auto_vec<HOST_WIDE_INT, 8> offset_map;
4483 3952779 : int i;
4484 3952779 : ipa_predicate toplev_predicate;
4485 3952779 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4486 3952779 : ipa_node_params *params_summary = (ipa_node_params_sum
4487 3952779 : ? ipa_node_params_sum->get (to) : NULL);
4488 :
4489 3952779 : if (es->predicate)
4490 339955 : toplev_predicate = *es->predicate;
4491 : else
4492 3612824 : toplev_predicate = true;
4493 :
4494 3952779 : info->fp_expressions |= callee_info->fp_expressions;
4495 3952779 : info->target_info |= callee_info->target_info;
4496 :
4497 3952779 : if (callee_info->conds)
4498 : {
4499 2870100 : ipa_auto_call_arg_values avals;
4500 2870100 : evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
4501 2870100 : }
4502 3952779 : if (ipa_node_params_sum && callee_info->conds)
4503 : {
4504 797212 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4505 797212 : int count = args ? ipa_get_cs_argument_count (args) : 0;
4506 797070 : int i;
4507 :
4508 797070 : if (count)
4509 : {
4510 797070 : operand_map.safe_grow_cleared (count, true);
4511 797070 : offset_map.safe_grow_cleared (count, true);
4512 : }
4513 2558896 : for (i = 0; i < count; i++)
4514 : {
4515 1761684 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4516 1761684 : int map = -1;
4517 :
4518 : /* TODO: handle non-NOPs when merging. */
4519 1761684 : if (jfunc->type == IPA_JF_PASS_THROUGH)
4520 : {
4521 282973 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4522 280049 : map = ipa_get_jf_pass_through_formal_id (jfunc);
4523 282973 : if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
4524 189663 : offset_map[i] = -1;
4525 : }
4526 1478711 : else if (jfunc->type == IPA_JF_ANCESTOR)
4527 : {
4528 88033 : HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
4529 88033 : if (offset >= 0 && offset < INT_MAX)
4530 : {
4531 88033 : map = ipa_get_jf_ancestor_formal_id (jfunc);
4532 88033 : if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
4533 54559 : offset = -1;
4534 88033 : offset_map[i] = offset;
4535 : }
4536 : }
4537 1761684 : operand_map[i] = map;
4538 3184270 : gcc_assert (map < ipa_get_param_count (params_summary));
4539 : }
4540 :
4541 : int ip;
4542 799793 : for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++)
4543 2581 : if (ip < count && operand_map[ip] >= 0)
4544 36 : add_builtin_constant_p_parm (info, operand_map[ip]);
4545 : }
4546 3952779 : sreal freq = edge->sreal_frequency ();
4547 21839963 : for (i = 0; callee_info->size_time_table.iterate (i, &e); i++)
4548 : {
4549 17887184 : ipa_predicate p;
4550 17887184 : p = e->exec_predicate.remap_after_inlining
4551 17887184 : (info, params_summary,
4552 : callee_info, operand_map,
4553 : offset_map, clause,
4554 : toplev_predicate);
4555 17887184 : ipa_predicate nonconstp;
4556 17887184 : nonconstp = e->nonconst_predicate.remap_after_inlining
4557 17887184 : (info, params_summary,
4558 : callee_info, operand_map,
4559 : offset_map, clause,
4560 : toplev_predicate);
4561 27004886 : if (p != false && nonconstp != false)
4562 : {
4563 8804488 : sreal add_time = ((sreal)e->time * freq);
4564 8804488 : int prob = e->nonconst_predicate.probability (callee_info->conds,
4565 : clause, es->param);
4566 8804488 : if (prob != REG_BR_PROB_BASE)
4567 1086877 : add_time = add_time * prob / REG_BR_PROB_BASE;
4568 1086877 : if (prob != REG_BR_PROB_BASE
4569 1086877 : && dump_file && (dump_flags & TDF_DETAILS))
4570 : {
4571 13 : fprintf (dump_file, "\t\tScaling time by probability:%f\n",
4572 13 : (double) prob / REG_BR_PROB_BASE);
4573 : }
4574 8804488 : info->account_size_time (e->size, add_time, p, nonconstp);
4575 : }
4576 : }
4577 3952779 : remap_edge_summaries (edge, edge->callee, info, params_summary,
4578 : callee_info, operand_map,
4579 : offset_map, clause, &toplev_predicate);
4580 3952779 : remap_freqcounting_predicate (info, params_summary, callee_info,
4581 : info->loop_iterations, operand_map,
4582 : offset_map, clause, &toplev_predicate);
4583 3952779 : remap_freqcounting_predicate (info, params_summary, callee_info,
4584 : info->loop_strides, operand_map,
4585 : offset_map, clause, &toplev_predicate);
4586 :
4587 3952779 : HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
4588 3952779 : HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
4589 :
4590 3952779 : if (info->estimated_stack_size < peak)
4591 125463 : info->estimated_stack_size = peak;
4592 :
4593 3952779 : inline_update_callee_summaries (edge->callee, es->loop_depth);
4594 3952779 : if (info->call_size_time_table.length ())
4595 : {
4596 856856 : int edge_size = 0;
4597 856856 : sreal edge_time = 0;
4598 :
4599 856856 : estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0);
4600 : /* Unaccount size and time of the optimized out call. */
4601 856856 : info->account_size_time (-edge_size, -edge_time,
4602 856856 : es->predicate ? *es->predicate : true,
4603 856856 : es->predicate ? *es->predicate : true,
4604 : true);
4605 : /* Account new calls. */
4606 856856 : summarize_calls_size_and_time (edge->callee, info);
4607 : }
4608 :
4609 : /* Free summaries that are not maintained for inline clones/edges. */
4610 3952779 : ipa_call_summaries->remove (edge);
4611 3952779 : ipa_fn_summaries->remove (edge->callee);
4612 3952779 : ipa_remove_from_growth_caches (edge);
4613 3952779 : }
4614 :
4615 : /* For performance reasons ipa_merge_fn_summary_after_inlining is not updating
4616 : overall size and time. Recompute it.
4617 : If RESET is true also recompute call_time_size_table. */
4618 :
4619 : void
4620 9403939 : ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset)
4621 : {
4622 9403939 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
4623 9403939 : class ipa_size_summary *size_info = ipa_size_summaries->get (node);
4624 9403939 : size_time_entry *e;
4625 9403939 : int i;
4626 :
4627 9403939 : size_info->size = 0;
4628 9403939 : info->time = 0;
4629 47673417 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4630 : {
4631 38269478 : size_info->size += e->size;
4632 38269478 : info->time += e->time;
4633 : }
4634 9403939 : info->min_size = info->size_time_table[0].size;
4635 9403939 : if (reset)
4636 8505003 : info->call_size_time_table.release ();
4637 9403939 : if (node->callees || node->indirect_calls)
4638 7153640 : estimate_calls_size_and_time (node, &size_info->size, &info->min_size,
4639 : &info->time, NULL,
4640 : ~(clause_t) (1 << ipa_predicate::false_condition),
4641 : NULL);
4642 9403939 : size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale);
4643 9403939 : info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale);
4644 9403939 : }
4645 :
4646 :
4647 : /* This function performs intraprocedural analysis in NODE that is required to
4648 : inline indirect calls. */
4649 :
4650 : static void
4651 1375983 : inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4652 : {
4653 1375983 : ipa_analyze_node (node);
4654 1375983 : if (dump_file && (dump_flags & TDF_DETAILS))
4655 : {
4656 14 : ipa_print_node_params (dump_file, node);
4657 14 : ipa_print_node_jump_functions (dump_file, node);
4658 : }
4659 1375983 : }
4660 :
4661 :
4662 : /* Note function body size. */
4663 :
4664 : void
4665 1386839 : inline_analyze_function (struct cgraph_node *node)
4666 : {
4667 1386839 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4668 :
4669 1386839 : if (dump_file)
4670 94 : fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ());
4671 1386839 : if (opt_for_fn (node->decl, optimize) && !node->thunk)
4672 1375983 : inline_indirect_intraprocedural_analysis (node);
4673 1386839 : compute_fn_summary (node, false);
4674 1386839 : if (!optimize)
4675 : {
4676 9584 : struct cgraph_edge *e;
4677 22325 : for (e = node->callees; e; e = e->next_callee)
4678 12741 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4679 9655 : for (e = node->indirect_calls; e; e = e->next_callee)
4680 71 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4681 : }
4682 :
4683 1386839 : pop_cfun ();
4684 1386839 : }
4685 :
4686 :
4687 : /* Called when new function is inserted to callgraph late. */
4688 :
4689 : void
4690 19209 : ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
4691 : {
4692 19209 : inline_analyze_function (node);
4693 19209 : }
4694 :
4695 : /* Note function body size. */
4696 :
4697 : static void
4698 232295 : ipa_fn_summary_generate (void)
4699 : {
4700 232295 : struct cgraph_node *node;
4701 :
4702 2095946 : FOR_EACH_DEFINED_FUNCTION (node)
4703 1863651 : if (DECL_STRUCT_FUNCTION (node->decl))
4704 1850471 : node->versionable = tree_versionable_function_p (node->decl);
4705 :
4706 232295 : ipa_fn_summary_alloc ();
4707 :
4708 232295 : ipa_fn_summaries->enable_insertion_hook ();
4709 :
4710 232295 : ipa_register_cgraph_hooks ();
4711 :
4712 2095946 : FOR_EACH_DEFINED_FUNCTION (node)
4713 1863651 : if (!node->alias
4714 1863651 : && (flag_generate_lto || flag_generate_offload|| flag_wpa
4715 1672237 : || opt_for_fn (node->decl, optimize)))
4716 1349555 : inline_analyze_function (node);
4717 232295 : }
4718 :
4719 :
4720 : /* Write inline summary for edge E to OB. */
4721 :
4722 : static void
4723 347234 : read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
4724 : bool prevails)
4725 : {
4726 347234 : class ipa_call_summary *es = prevails
4727 347234 : ? ipa_call_summaries->get_create (e) : NULL;
4728 347234 : ipa_predicate p;
4729 347234 : int length, i;
4730 :
4731 347234 : int size = streamer_read_uhwi (ib);
4732 347234 : int time = streamer_read_uhwi (ib);
4733 347234 : int depth = streamer_read_uhwi (ib);
4734 :
4735 347234 : if (es)
4736 : {
4737 347185 : es->call_stmt_size = size;
4738 347185 : es->call_stmt_time = time;
4739 347185 : es->loop_depth = depth;
4740 : }
4741 :
4742 347234 : bitpack_d bp = streamer_read_bitpack (ib);
4743 347234 : if (es)
4744 347185 : es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
4745 : else
4746 49 : bp_unpack_value (&bp, 1);
4747 :
4748 347234 : p.stream_in (ib);
4749 347234 : if (es)
4750 347185 : edge_set_predicate (e, &p);
4751 347234 : length = streamer_read_uhwi (ib);
4752 347234 : if (length && es
4753 347234 : && (e->possibly_call_in_translation_unit_p ()
4754 : /* Also stream in jump functions to builtins in hope that they
4755 : will get fnspecs. */
4756 118765 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
4757 : {
4758 232310 : es->param.safe_grow_cleared (length, true);
4759 799633 : for (i = 0; i < length; i++)
4760 : {
4761 567323 : es->param[i].change_prob = streamer_read_uhwi (ib);
4762 567323 : bitpack_d bp = streamer_read_bitpack (ib);
4763 1701969 : es->param[i].points_to_local_or_readonly_memory
4764 567323 : = bp_unpack_value (&bp, 1);
4765 1701969 : es->param[i].points_to_possible_sra_candidate
4766 567323 : = bp_unpack_value (&bp, 1);
4767 : }
4768 : }
4769 : else
4770 : {
4771 137714 : for (i = 0; i < length; i++)
4772 : {
4773 22790 : streamer_read_uhwi (ib);
4774 22790 : streamer_read_uhwi (ib);
4775 : }
4776 : }
4777 347234 : }
4778 :
4779 :
4780 : /* Stream in inline summaries from the section. */
4781 :
4782 : static void
4783 13454 : inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4784 : size_t len)
4785 : {
4786 13454 : const struct lto_function_header *header =
4787 : (const struct lto_function_header *) data;
4788 13454 : const int cfg_offset = sizeof (struct lto_function_header);
4789 13454 : const int main_offset = cfg_offset + header->cfg_size;
4790 13454 : const int string_offset = main_offset + header->main_size;
4791 13454 : class data_in *data_in;
4792 13454 : unsigned int i, count2, j;
4793 13454 : unsigned int f_count;
4794 :
4795 13454 : lto_input_block ib ((const char *) data + main_offset, header->main_size,
4796 13454 : file_data);
4797 :
4798 13454 : data_in =
4799 26908 : lto_data_in_create (file_data, (const char *) data + string_offset,
4800 13454 : header->string_size, vNULL);
4801 13454 : f_count = streamer_read_uhwi (&ib);
4802 99694 : for (i = 0; i < f_count; i++)
4803 : {
4804 86240 : unsigned int index;
4805 86240 : struct cgraph_node *node;
4806 86240 : class ipa_fn_summary *info;
4807 86240 : class ipa_node_params *params_summary;
4808 86240 : class ipa_size_summary *size_info;
4809 86240 : lto_symtab_encoder_t encoder;
4810 86240 : struct bitpack_d bp;
4811 86240 : struct cgraph_edge *e;
4812 86240 : ipa_predicate p;
4813 :
4814 86240 : index = streamer_read_uhwi (&ib);
4815 86240 : encoder = file_data->symtab_node_encoder;
4816 86240 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4817 : index));
4818 86240 : info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
4819 86240 : params_summary = node->prevailing_p ()
4820 86240 : ? ipa_node_params_sum->get (node) : NULL;
4821 86240 : size_info = node->prevailing_p ()
4822 86240 : ? ipa_size_summaries->get_create (node) : NULL;
4823 :
4824 86240 : int stack_size = streamer_read_uhwi (&ib);
4825 86240 : int size = streamer_read_uhwi (&ib);
4826 86240 : sreal time = sreal::stream_in (&ib);
4827 :
4828 86240 : if (info)
4829 : {
4830 86182 : info->estimated_stack_size
4831 86182 : = size_info->estimated_self_stack_size = stack_size;
4832 86182 : size_info->size = size_info->self_size = size;
4833 86182 : info->time = time;
4834 : }
4835 :
4836 86240 : bp = streamer_read_bitpack (&ib);
4837 86240 : if (info)
4838 : {
4839 86182 : info->inlinable = bp_unpack_value (&bp, 1);
4840 86182 : info->fp_expressions = bp_unpack_value (&bp, 1);
4841 86182 : if (!lto_stream_offload_p)
4842 86182 : info->target_info = streamer_read_uhwi (&ib);
4843 : }
4844 : else
4845 : {
4846 58 : bp_unpack_value (&bp, 1);
4847 58 : bp_unpack_value (&bp, 1);
4848 58 : if (!lto_stream_offload_p)
4849 58 : streamer_read_uhwi (&ib);
4850 : }
4851 :
4852 86240 : count2 = streamer_read_uhwi (&ib);
4853 86240 : gcc_assert (!info || !info->conds);
4854 86182 : if (info)
4855 86182 : vec_safe_reserve_exact (info->conds, count2);
4856 163094 : for (j = 0; j < count2; j++)
4857 : {
4858 76854 : struct condition c;
4859 76854 : unsigned int k, count3;
4860 76854 : c.operand_num = streamer_read_uhwi (&ib);
4861 76854 : c.code = (enum tree_code) streamer_read_uhwi (&ib);
4862 76854 : c.type = stream_read_tree (&ib, data_in);
4863 76854 : c.val = stream_read_tree (&ib, data_in);
4864 76854 : bp = streamer_read_bitpack (&ib);
4865 76854 : c.agg_contents = bp_unpack_value (&bp, 1);
4866 76854 : c.by_ref = bp_unpack_value (&bp, 1);
4867 76854 : if (c.agg_contents)
4868 12406 : c.offset = streamer_read_uhwi (&ib);
4869 76854 : count3 = streamer_read_uhwi (&ib);
4870 76854 : c.param_ops = NULL;
4871 76854 : if (info)
4872 76854 : vec_safe_reserve_exact (c.param_ops, count3);
4873 76854 : if (params_summary)
4874 76854 : ipa_set_param_used_by_ipa_predicates
4875 76854 : (params_summary, c.operand_num, true);
4876 80356 : for (k = 0; k < count3; k++)
4877 : {
4878 3502 : struct expr_eval_op op;
4879 3502 : enum gimple_rhs_class rhs_class;
4880 3502 : op.code = (enum tree_code) streamer_read_uhwi (&ib);
4881 3502 : op.type = stream_read_tree (&ib, data_in);
4882 3502 : switch (rhs_class = get_gimple_rhs_class (op.code))
4883 : {
4884 1401 : case GIMPLE_UNARY_RHS:
4885 1401 : op.index = 0;
4886 1401 : op.val[0] = NULL_TREE;
4887 1401 : op.val[1] = NULL_TREE;
4888 1401 : break;
4889 :
4890 2101 : case GIMPLE_BINARY_RHS:
4891 2101 : case GIMPLE_TERNARY_RHS:
4892 2101 : bp = streamer_read_bitpack (&ib);
4893 2101 : op.index = bp_unpack_value (&bp, 2);
4894 2101 : op.val[0] = stream_read_tree (&ib, data_in);
4895 2101 : if (rhs_class == GIMPLE_BINARY_RHS)
4896 2101 : op.val[1] = NULL_TREE;
4897 : else
4898 0 : op.val[1] = stream_read_tree (&ib, data_in);
4899 : break;
4900 :
4901 0 : default:
4902 0 : fatal_error (UNKNOWN_LOCATION,
4903 : "invalid fnsummary in LTO stream");
4904 : }
4905 3502 : if (info)
4906 3502 : c.param_ops->quick_push (op);
4907 : }
4908 76854 : if (info)
4909 76854 : info->conds->quick_push (c);
4910 : }
4911 86240 : count2 = streamer_read_uhwi (&ib);
4912 86240 : gcc_assert (!info || !info->size_time_table.length ());
4913 86240 : if (info && count2)
4914 86182 : info->size_time_table.reserve_exact (count2);
4915 327498 : for (j = 0; j < count2; j++)
4916 : {
4917 241258 : class size_time_entry e;
4918 :
4919 241258 : e.size = streamer_read_uhwi (&ib);
4920 241258 : e.time = sreal::stream_in (&ib);
4921 241258 : e.exec_predicate.stream_in (&ib);
4922 241258 : e.nonconst_predicate.stream_in (&ib);
4923 :
4924 241258 : if (info)
4925 241142 : info->size_time_table.quick_push (e);
4926 : }
4927 :
4928 86240 : count2 = streamer_read_uhwi (&ib);
4929 87588 : for (j = 0; j < count2; j++)
4930 : {
4931 1348 : p.stream_in (&ib);
4932 1348 : sreal fcp_freq = sreal::stream_in (&ib);
4933 1348 : if (info)
4934 : {
4935 1348 : ipa_freqcounting_predicate fcp;
4936 1348 : fcp.predicate = NULL;
4937 1348 : set_hint_predicate (&fcp.predicate, p);
4938 1348 : fcp.freq = fcp_freq;
4939 1348 : vec_safe_push (info->loop_iterations, fcp);
4940 : }
4941 : }
4942 86240 : count2 = streamer_read_uhwi (&ib);
4943 86527 : for (j = 0; j < count2; j++)
4944 : {
4945 287 : p.stream_in (&ib);
4946 287 : sreal fcp_freq = sreal::stream_in (&ib);
4947 287 : if (info)
4948 : {
4949 287 : ipa_freqcounting_predicate fcp;
4950 287 : fcp.predicate = NULL;
4951 287 : set_hint_predicate (&fcp.predicate, p);
4952 287 : fcp.freq = fcp_freq;
4953 287 : vec_safe_push (info->loop_strides, fcp);
4954 : }
4955 : }
4956 86240 : count2 = streamer_read_uhwi (&ib);
4957 86240 : if (info && count2)
4958 6 : info->builtin_constant_p_parms.reserve_exact (count2);
4959 86246 : for (j = 0; j < count2; j++)
4960 : {
4961 6 : int parm = streamer_read_uhwi (&ib);
4962 6 : if (info)
4963 6 : info->builtin_constant_p_parms.quick_push (parm);
4964 : }
4965 431994 : for (e = node->callees; e; e = e->next_callee)
4966 345754 : read_ipa_call_summary (&ib, e, info != NULL);
4967 87720 : for (e = node->indirect_calls; e; e = e->next_callee)
4968 1480 : read_ipa_call_summary (&ib, e, info != NULL);
4969 : }
4970 :
4971 13454 : lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4972 : len);
4973 13454 : lto_data_in_delete (data_in);
4974 13454 : }
4975 :
4976 :
4977 : /* Read inline summary. Jump functions are shared among ipa-cp
4978 : and inliner, so when ipa-cp is active, we don't need to write them
4979 : twice. */
4980 :
4981 : static void
4982 12399 : ipa_fn_summary_read (void)
4983 : {
4984 12399 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4985 12399 : struct lto_file_decl_data *file_data;
4986 12399 : unsigned int j = 0;
4987 :
4988 12399 : ipa_prop_read_jump_functions ();
4989 12399 : ipa_fn_summary_alloc ();
4990 :
4991 38252 : while ((file_data = file_data_vec[j++]))
4992 : {
4993 13454 : size_t len;
4994 13454 : const char *data
4995 13454 : = lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary,
4996 : &len);
4997 13454 : if (data)
4998 13454 : inline_read_section (file_data, data, len);
4999 : else
5000 : /* Fatal error here. We do not want to support compiling ltrans units
5001 : with different version of compiler or different flags than the WPA
5002 : unit, so this should never happen. */
5003 0 : fatal_error (input_location,
5004 : "ipa inline summary is missing in input file");
5005 : }
5006 12399 : ipa_register_cgraph_hooks ();
5007 :
5008 12399 : gcc_assert (ipa_fn_summaries);
5009 12399 : ipa_fn_summaries->enable_insertion_hook ();
5010 12399 : }
5011 :
5012 :
5013 : /* Write inline summary for edge E to OB. */
5014 :
5015 : static void
5016 378654 : write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
5017 : {
5018 378654 : class ipa_call_summary *es = ipa_call_summaries->get (e);
5019 378654 : int i;
5020 :
5021 378654 : streamer_write_uhwi (ob, es->call_stmt_size);
5022 378654 : streamer_write_uhwi (ob, es->call_stmt_time);
5023 378654 : streamer_write_uhwi (ob, es->loop_depth);
5024 :
5025 378654 : bitpack_d bp = bitpack_create (ob->main_stream);
5026 378654 : bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
5027 378654 : streamer_write_bitpack (&bp);
5028 :
5029 378654 : if (es->predicate)
5030 10583 : es->predicate->stream_out (ob);
5031 : else
5032 368071 : streamer_write_uhwi (ob, 0);
5033 378654 : streamer_write_uhwi (ob, es->param.length ());
5034 2285908 : for (i = 0; i < (int) es->param.length (); i++)
5035 : {
5036 630403 : streamer_write_uhwi (ob, es->param[i].change_prob);
5037 630403 : bp = bitpack_create (ob->main_stream);
5038 630403 : bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1);
5039 630403 : bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1);
5040 630403 : streamer_write_bitpack (&bp);
5041 : }
5042 378654 : }
5043 :
5044 :
5045 : /* Write inline summary for node in SET.
5046 : Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
5047 : active, we don't need to write them twice. */
5048 :
5049 : static void
5050 23316 : ipa_fn_summary_write (void)
5051 : {
5052 23316 : struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
5053 23316 : lto_symtab_encoder_iterator lsei;
5054 23316 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5055 23316 : unsigned int count = 0;
5056 :
5057 128405 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5058 105089 : lsei_next_function_in_partition (&lsei))
5059 : {
5060 105089 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5061 105089 : if (cnode->definition && !cnode->alias)
5062 102639 : count++;
5063 : }
5064 23316 : streamer_write_uhwi (ob, count);
5065 :
5066 128405 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5067 105089 : lsei_next_function_in_partition (&lsei))
5068 : {
5069 105089 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5070 105089 : if (cnode->definition && !cnode->alias)
5071 : {
5072 102639 : class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
5073 102639 : class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
5074 102639 : struct bitpack_d bp;
5075 102639 : struct cgraph_edge *edge;
5076 102639 : int i;
5077 102639 : size_time_entry *e;
5078 102639 : struct condition *c;
5079 :
5080 102639 : streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
5081 102639 : streamer_write_hwi (ob, size_info->estimated_self_stack_size);
5082 102639 : streamer_write_hwi (ob, size_info->self_size);
5083 102639 : info->time.stream_out (ob);
5084 102639 : bp = bitpack_create (ob->main_stream);
5085 102639 : bp_pack_value (&bp, info->inlinable, 1);
5086 102639 : bp_pack_value (&bp, info->fp_expressions, 1);
5087 102639 : streamer_write_bitpack (&bp);
5088 102639 : if (!lto_stream_offload_p)
5089 102639 : streamer_write_uhwi (ob, info->target_info);
5090 102639 : streamer_write_uhwi (ob, vec_safe_length (info->conds));
5091 195129 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
5092 : {
5093 92490 : int j;
5094 92490 : struct expr_eval_op *op;
5095 :
5096 92490 : streamer_write_uhwi (ob, c->operand_num);
5097 92490 : streamer_write_uhwi (ob, c->code);
5098 92490 : stream_write_tree (ob, c->type, true);
5099 92490 : stream_write_tree (ob, c->val, true);
5100 92490 : bp = bitpack_create (ob->main_stream);
5101 92490 : bp_pack_value (&bp, c->agg_contents, 1);
5102 92490 : bp_pack_value (&bp, c->by_ref, 1);
5103 92490 : streamer_write_bitpack (&bp);
5104 92490 : if (c->agg_contents)
5105 15565 : streamer_write_uhwi (ob, c->offset);
5106 92490 : streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
5107 191961 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
5108 : {
5109 4083 : streamer_write_uhwi (ob, op->code);
5110 4083 : stream_write_tree (ob, op->type, true);
5111 4083 : if (op->val[0])
5112 : {
5113 2468 : bp = bitpack_create (ob->main_stream);
5114 2468 : bp_pack_value (&bp, op->index, 2);
5115 2468 : streamer_write_bitpack (&bp);
5116 2468 : stream_write_tree (ob, op->val[0], true);
5117 2468 : if (op->val[1])
5118 4 : stream_write_tree (ob, op->val[1], true);
5119 : }
5120 : }
5121 : }
5122 102639 : streamer_write_uhwi (ob, info->size_time_table.length ());
5123 495863 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
5124 : {
5125 290585 : streamer_write_uhwi (ob, e->size);
5126 290585 : e->time.stream_out (ob);
5127 290585 : e->exec_predicate.stream_out (ob);
5128 290585 : e->nonconst_predicate.stream_out (ob);
5129 : }
5130 102639 : ipa_freqcounting_predicate *fcp;
5131 102639 : streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations));
5132 207252 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
5133 : {
5134 1974 : fcp->predicate->stream_out (ob);
5135 1974 : fcp->freq.stream_out (ob);
5136 : }
5137 102639 : streamer_write_uhwi (ob, vec_safe_length (info->loop_strides));
5138 205619 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
5139 : {
5140 341 : fcp->predicate->stream_out (ob);
5141 341 : fcp->freq.stream_out (ob);
5142 : }
5143 102639 : streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ());
5144 102639 : int ip;
5145 205292 : for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip);
5146 : i++)
5147 14 : streamer_write_uhwi (ob, ip);
5148 478609 : for (edge = cnode->callees; edge; edge = edge->next_callee)
5149 375970 : write_ipa_call_summary (ob, edge);
5150 105323 : for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
5151 2684 : write_ipa_call_summary (ob, edge);
5152 : }
5153 : }
5154 23316 : streamer_write_char_stream (ob->main_stream, 0);
5155 23316 : produce_asm (ob);
5156 23316 : destroy_output_block (ob);
5157 :
5158 23316 : ipa_prop_write_jump_functions ();
5159 23316 : }
5160 :
5161 :
5162 : /* Release function summary. */
5163 :
5164 : void
5165 725315 : ipa_free_fn_summary (void)
5166 : {
5167 725315 : if (!ipa_call_summaries)
5168 : return;
5169 459107 : ggc_delete (ipa_fn_summaries);
5170 459107 : ipa_fn_summaries = NULL;
5171 459107 : delete ipa_call_summaries;
5172 459107 : ipa_call_summaries = NULL;
5173 459107 : edge_predicate_pool.release ();
5174 : /* During IPA this is one of largest datastructures to release. */
5175 459107 : if (flag_wpa)
5176 7978 : ggc_trim ();
5177 : }
5178 :
5179 : /* Release function summary. */
5180 :
5181 : void
5182 725315 : ipa_free_size_summary (void)
5183 : {
5184 725315 : if (!ipa_size_summaries)
5185 : return;
5186 459107 : delete ipa_size_summaries;
5187 459107 : ipa_size_summaries = NULL;
5188 : }
5189 :
5190 : namespace {
5191 :
5192 : const pass_data pass_data_local_fn_summary =
5193 : {
5194 : GIMPLE_PASS, /* type */
5195 : "local-fnsummary", /* name */
5196 : OPTGROUP_INLINE, /* optinfo_flags */
5197 : TV_INLINE_PARAMETERS, /* tv_id */
5198 : 0, /* properties_required */
5199 : 0, /* properties_provided */
5200 : 0, /* properties_destroyed */
5201 : 0, /* todo_flags_start */
5202 : 0, /* todo_flags_finish */
5203 : };
5204 :
5205 : class pass_local_fn_summary : public gimple_opt_pass
5206 : {
5207 : public:
5208 577550 : pass_local_fn_summary (gcc::context *ctxt)
5209 1155100 : : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
5210 : {}
5211 :
5212 : /* opt_pass methods: */
5213 288775 : opt_pass * clone () final override
5214 : {
5215 288775 : return new pass_local_fn_summary (m_ctxt);
5216 : }
5217 5757227 : unsigned int execute (function *) final override
5218 : {
5219 5757227 : return compute_fn_summary_for_current ();
5220 : }
5221 :
5222 : }; // class pass_local_fn_summary
5223 :
5224 : } // anon namespace
5225 :
5226 : gimple_opt_pass *
5227 288775 : make_pass_local_fn_summary (gcc::context *ctxt)
5228 : {
5229 288775 : return new pass_local_fn_summary (ctxt);
5230 : }
5231 :
5232 :
5233 : /* Free inline summary. */
5234 :
5235 : namespace {
5236 :
5237 : const pass_data pass_data_ipa_free_fn_summary =
5238 : {
5239 : SIMPLE_IPA_PASS, /* type */
5240 : "free-fnsummary", /* name */
5241 : OPTGROUP_NONE, /* optinfo_flags */
5242 : TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
5243 : 0, /* properties_required */
5244 : 0, /* properties_provided */
5245 : 0, /* properties_destroyed */
5246 : 0, /* todo_flags_start */
5247 : 0, /* todo_flags_finish */
5248 : };
5249 :
5250 : class pass_ipa_free_fn_summary : public simple_ipa_opt_pass
5251 : {
5252 : public:
5253 577550 : pass_ipa_free_fn_summary (gcc::context *ctxt)
5254 577550 : : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
5255 1155100 : small_p (false)
5256 : {}
5257 :
5258 : /* opt_pass methods: */
5259 288775 : opt_pass *clone () final override
5260 : {
5261 288775 : return new pass_ipa_free_fn_summary (m_ctxt);
5262 : }
5263 577550 : void set_pass_param (unsigned int n, bool param) final override
5264 : {
5265 577550 : gcc_assert (n == 0);
5266 577550 : small_p = param;
5267 577550 : }
5268 485727 : bool gate (function *) final override { return true; }
5269 464965 : unsigned int execute (function *) final override
5270 : {
5271 464965 : ipa_free_fn_summary ();
5272 : /* Free ipa-prop structures if they are no longer needed. */
5273 464965 : ipa_free_all_structures_after_iinln ();
5274 464965 : if (!flag_wpa)
5275 456987 : ipa_free_size_summary ();
5276 464965 : return 0;
5277 : }
5278 :
5279 : private:
5280 : bool small_p;
5281 : }; // class pass_ipa_free_fn_summary
5282 :
5283 : } // anon namespace
5284 :
5285 : simple_ipa_opt_pass *
5286 288775 : make_pass_ipa_free_fn_summary (gcc::context *ctxt)
5287 : {
5288 288775 : return new pass_ipa_free_fn_summary (ctxt);
5289 : }
5290 :
5291 : namespace {
5292 :
5293 : const pass_data pass_data_ipa_fn_summary =
5294 : {
5295 : IPA_PASS, /* type */
5296 : "fnsummary", /* name */
5297 : OPTGROUP_INLINE, /* optinfo_flags */
5298 : TV_IPA_FNSUMMARY, /* tv_id */
5299 : 0, /* properties_required */
5300 : 0, /* properties_provided */
5301 : 0, /* properties_destroyed */
5302 : 0, /* todo_flags_start */
5303 : ( TODO_dump_symtab ), /* todo_flags_finish */
5304 : };
5305 :
5306 : class pass_ipa_fn_summary : public ipa_opt_pass_d
5307 : {
5308 : public:
5309 288775 : pass_ipa_fn_summary (gcc::context *ctxt)
5310 : : ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt,
5311 : ipa_fn_summary_generate, /* generate_summary */
5312 : ipa_fn_summary_write, /* write_summary */
5313 : ipa_fn_summary_read, /* read_summary */
5314 : NULL, /* write_optimization_summary */
5315 : NULL, /* read_optimization_summary */
5316 : NULL, /* stmt_fixup */
5317 : 0, /* function_transform_todo_flags_start */
5318 : NULL, /* function_transform */
5319 288775 : NULL) /* variable_transform */
5320 288775 : {}
5321 :
5322 : /* opt_pass methods: */
5323 232521 : unsigned int execute (function *) final override { return 0; }
5324 :
5325 : }; // class pass_ipa_fn_summary
5326 :
5327 : } // anon namespace
5328 :
5329 : ipa_opt_pass_d *
5330 288775 : make_pass_ipa_fn_summary (gcc::context *ctxt)
5331 : {
5332 288775 : return new pass_ipa_fn_summary (ctxt);
5333 : }
5334 :
5335 : /* Reset all state within ipa-fnsummary.cc so that we can rerun the compiler
5336 : within the same process. For use by toplev::finalize. */
5337 :
5338 : void
5339 259496 : ipa_fnsummary_cc_finalize (void)
5340 : {
5341 259496 : ipa_free_fn_summary ();
5342 259496 : ipa_free_size_summary ();
5343 259496 : }
|