Branch data 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 : 134168580 : 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 : 134168580 : size_time_entry *e;
173 : 134168580 : bool found = false;
174 : 134168580 : int i;
175 : 134168580 : ipa_predicate nonconst_pred;
176 : 134168580 : vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table;
177 : :
178 : 134168580 : if (exec_pred == false)
179 : 4670151 : return;
180 : :
181 : 133920283 : nonconst_pred = nonconst_pred_in & exec_pred;
182 : :
183 : 133920283 : 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 : 156786749 : if (!size && time == 0 && table->length ())
189 : 3712234 : return;
190 : :
191 : : /* Only for calls we are unaccounting what we previously recorded. */
192 : 129498429 : gcc_checking_assert (time >= 0 || call);
193 : :
194 : 470741789 : for (i = 0; table->iterate (i, &e); i++)
195 : 441454575 : if (e->exec_predicate == exec_pred
196 : 441454575 : && e->nonconst_predicate == nonconst_pred)
197 : : {
198 : : found = true;
199 : : break;
200 : : }
201 : 129498429 : 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 : 129502703 : 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 : 129498429 : if (!found)
227 : : {
228 : 29287214 : class size_time_entry new_entry;
229 : 29287214 : new_entry.size = size;
230 : 29287214 : new_entry.time = time;
231 : 29287214 : new_entry.exec_predicate = exec_pred;
232 : 29287214 : new_entry.nonconst_predicate = nonconst_pred;
233 : 29287214 : if (call)
234 : 1340854 : call_size_time_table.safe_push (new_entry);
235 : : else
236 : 27946360 : size_time_table.safe_push (new_entry);
237 : : }
238 : : else
239 : : {
240 : 100211215 : e->size += size;
241 : 100211215 : e->time += time;
242 : : /* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */
243 : : /* Tolerate small roundoff issues. */
244 : 100211215 : if (e->time < 0)
245 : 88 : 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 : 272594 : redirect_to_unreachable (struct cgraph_edge *e)
253 : : {
254 : 272594 : struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
255 : 272594 : struct cgraph_node *target
256 : 272594 : = cgraph_node::get_create (builtin_decl_unreachable ());
257 : :
258 : 272594 : gcc_checking_assert (lookup_attribute ("cold",
259 : : DECL_ATTRIBUTES (target->decl)));
260 : :
261 : 272594 : if (e->speculative)
262 : 314 : e = cgraph_edge::resolve_speculation (e, target->decl);
263 : 272280 : else if (!e->callee)
264 : 762 : e = cgraph_edge::make_direct (e, target);
265 : : else
266 : 271518 : e->redirect_callee (target);
267 : 272594 : class ipa_call_summary *es = ipa_call_summaries->get (e);
268 : 272594 : e->inline_failed = CIF_UNREACHABLE;
269 : 272594 : e->count = profile_count::zero ();
270 : 272594 : es->call_stmt_size = 0;
271 : 272594 : es->call_stmt_time = 0;
272 : 272594 : if (callee)
273 : 0 : callee->remove_symbol_and_inline_clones ();
274 : 272594 : 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 : 272594 : return e;
280 : : }
281 : :
282 : : /* Set predicate for edge E. */
283 : :
284 : : static void
285 : 30553136 : 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 : 27924549 : 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 : 30825805 : && (!e->speculative || e->callee))
295 : 272593 : e = redirect_to_unreachable (e);
296 : :
297 : 30553136 : class ipa_call_summary *es = ipa_call_summaries->get (e);
298 : 58477685 : if (predicate && *predicate != true)
299 : : {
300 : 4560749 : if (!es->predicate)
301 : 4085817 : es->predicate = edge_predicate_pool.allocate ();
302 : 4560749 : *es->predicate = *predicate;
303 : : }
304 : : else
305 : : {
306 : 25992387 : if (es->predicate)
307 : 796505 : edge_predicate_pool.remove (es->predicate);
308 : 25992387 : es->predicate = NULL;
309 : : }
310 : 30553136 : }
311 : :
312 : : /* Set predicate for hint *P. */
313 : :
314 : : static void
315 : 148660 : set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate)
316 : : {
317 : 148660 : if (new_predicate == false || new_predicate == true)
318 : : {
319 : 3003 : if (*p)
320 : 0 : edge_predicate_pool.remove (*p);
321 : 3003 : *p = NULL;
322 : : }
323 : : else
324 : : {
325 : 145657 : if (!*p)
326 : 145657 : *p = edge_predicate_pool.allocate ();
327 : 145657 : **p = new_predicate;
328 : : }
329 : 148660 : }
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 : 1103761 : 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 : 1103761 : if (new_predicate == false || new_predicate == true)
342 : : return;
343 : : ipa_freqcounting_predicate *f;
344 : 139981 : for (int i = 0; vec_safe_iterate (*v, i, &f); i++)
345 : 71520 : if (new_predicate == f->predicate)
346 : : {
347 : 0 : f->freq += add_freq;
348 : 0 : return;
349 : : }
350 : 94260 : if (vec_safe_length (*v) >= max_num_predicates)
351 : : /* Too many different predicates to account for. */
352 : : return;
353 : :
354 : 68189 : ipa_freqcounting_predicate fcp;
355 : 68189 : fcp.predicate = NULL;
356 : 68189 : set_hint_predicate (&fcp.predicate, new_predicate);
357 : 68189 : fcp.freq = add_freq;
358 : 68189 : vec_safe_push (*v, fcp);
359 : 68189 : 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 : 22069730 : 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 : 22069730 : clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
388 : 22069730 : clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition;
389 : 22069730 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
390 : 22069730 : int i;
391 : 22069730 : struct condition *c;
392 : :
393 : 92221902 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
394 : : {
395 : 70152172 : tree val = NULL;
396 : 70152172 : tree res;
397 : 70152172 : int j;
398 : 70152172 : struct expr_eval_op *op;
399 : :
400 : 70152172 : if (c->code == ipa_predicate::not_sra_candidate)
401 : : {
402 : 16423048 : if (!inline_p
403 : 16423048 : || !es
404 : 16528785 : || (int)es->param.length () <= c->operand_num
405 : 24687409 : || !es->param[c->operand_num].points_to_possible_sra_candidate)
406 : 12267806 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
407 : 16423048 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
408 : 70152172 : continue;
409 : : }
410 : :
411 : 53729124 : if (c->agg_contents)
412 : : {
413 : 28888179 : if (c->code == ipa_predicate::changed
414 : 18407499 : && !c->by_ref
415 : 31317256 : && (avals->safe_sval_at(c->operand_num) == error_mark_node))
416 : 5208 : continue;
417 : :
418 : 28877763 : if (tree sval = avals->safe_sval_at (c->operand_num))
419 : 13543067 : val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref);
420 : 13543067 : if (!val)
421 : : {
422 : 28870235 : ipa_argagg_value_list avs (avals);
423 : 28870235 : val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
424 : 28870235 : c->by_ref);
425 : : }
426 : : }
427 : : else
428 : : {
429 : 24846153 : val = avals->safe_sval_at (c->operand_num);
430 : 24846153 : if (val && val == error_mark_node
431 : 2357037 : && c->code != ipa_predicate::changed)
432 : : val = NULL_TREE;
433 : : }
434 : :
435 : 40961510 : if (!val
436 : 39971619 : && (c->code == ipa_predicate::changed
437 : : || c->code == ipa_predicate::is_not_constant))
438 : : {
439 : 25262248 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
440 : 25262248 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
441 : 25262248 : continue;
442 : : }
443 : 28461668 : if (c->code == ipa_predicate::changed)
444 : : {
445 : 7661307 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
446 : 7661307 : continue;
447 : : }
448 : :
449 : 20800361 : if (c->code == ipa_predicate::is_not_constant)
450 : : {
451 : 5053 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
452 : 5053 : continue;
453 : : }
454 : :
455 : 20795308 : if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val)))
456 : : {
457 : 6039203 : if (c->type != TREE_TYPE (val))
458 : 1297439 : val = fold_unary (VIEW_CONVERT_EXPR, c->type, val);
459 : 6410480 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
460 : : {
461 : 371719 : if (!val)
462 : : break;
463 : 371277 : if (!op->val[0])
464 : 122064 : val = fold_unary (op->code, op->type, val);
465 : 249213 : else if (!op->val[1])
466 : 498426 : 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 : 6039203 : res = val
483 : 6039203 : ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
484 : : : NULL;
485 : :
486 : 6038741 : if (res && integer_zerop (res))
487 : 3128595 : continue;
488 : 2910608 : if (res && integer_onep (res))
489 : : {
490 : 2887968 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
491 : 2887968 : nonspec_clause
492 : 2887968 : |= 1 << (i + ipa_predicate::first_dynamic_condition);
493 : 2887968 : continue;
494 : : }
495 : : }
496 : 14778745 : if (c->operand_num < (int) avals->m_known_value_ranges.length ()
497 : 8597890 : && !c->agg_contents
498 : 16893201 : && (!val || TREE_CODE (val) != INTEGER_CST))
499 : : {
500 : 2114370 : value_range vr (avals->m_known_value_ranges[c->operand_num]);
501 : 2114370 : if (!vr.undefined_p ()
502 : 1256640 : && !vr.varying_p ()
503 : 3371010 : && (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ())))
504 : : {
505 : 1255944 : if (!useless_type_conversion_p (c->type, vr.type ()))
506 : 840 : range_cast (vr, c->type);
507 : :
508 : 1566776 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
509 : : {
510 : 349072 : if (vr.varying_p () || vr.undefined_p ())
511 : : break;
512 : :
513 : 310832 : value_range res (op->type);
514 : 310832 : if (!op->val[0])
515 : : {
516 : 112635 : value_range varying (op->type);
517 : 112635 : varying.set_varying (op->type);
518 : 112635 : range_op_handler handler (op->code);
519 : 112635 : if (!handler
520 : 112635 : || !res.supports_type_p (op->type)
521 : 225270 : || !handler.fold_range (res, op->type, vr, varying))
522 : 0 : res.set_varying (op->type);
523 : 112635 : }
524 : 198197 : else if (!op->val[1])
525 : : {
526 : 198025 : value_range op0 (TREE_TYPE (op->val[0]));
527 : 198025 : range_op_handler handler (op->code);
528 : :
529 : 198025 : ipa_get_range_from_ip_invariant (op0, op->val[0], node);
530 : :
531 : 198025 : if (!handler
532 : 198025 : || !res.supports_type_p (op->type)
533 : 396050 : || !handler.fold_range (res, op->type,
534 : 198025 : op->index ? op0 : vr,
535 : 395484 : op->index ? vr : op0))
536 : 0 : res.set_varying (op->type);
537 : 198025 : }
538 : : else
539 : 172 : res.set_varying (op->type);
540 : 310832 : vr = res;
541 : 310832 : }
542 : 1255944 : if (!vr.varying_p () && !vr.undefined_p ())
543 : : {
544 : 1208011 : int_range<2> res;
545 : 1208011 : value_range val_vr (TREE_TYPE (c->val));
546 : 1208011 : range_op_handler handler (c->code);
547 : :
548 : 1208011 : ipa_get_range_from_ip_invariant (val_vr, c->val, node);
549 : :
550 : 1208011 : if (!handler
551 : 1208011 : || !val_vr.supports_type_p (TREE_TYPE (c->val))
552 : 2416022 : || !handler.fold_range (res, boolean_type_node, vr, val_vr))
553 : 0 : res.set_varying (boolean_type_node);
554 : :
555 : 1208011 : if (res.zero_p ())
556 : 206503 : continue;
557 : 1208011 : }
558 : : }
559 : 2114370 : }
560 : :
561 : 14572242 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
562 : 14572242 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
563 : : }
564 : 22069730 : *ret_clause = clause;
565 : 22069730 : if (ret_nonspec_clause)
566 : 19189544 : *ret_nonspec_clause = nonspec_clause;
567 : 22069730 : }
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 : 12131351 : vrp_will_run_p (struct cgraph_node *node)
580 : : {
581 : 12131351 : return (opt_for_fn (node->decl, optimize)
582 : 12131351 : && !opt_for_fn (node->decl, optimize_debug)
583 : 24261485 : && opt_for_fn (node->decl, flag_tree_vrp));
584 : : }
585 : :
586 : : /* Similarly about FRE. */
587 : :
588 : : static bool
589 : 14476855 : fre_will_run_p (struct cgraph_node *node)
590 : : {
591 : 14476855 : return (opt_for_fn (node->decl, optimize)
592 : 14476855 : && !opt_for_fn (node->decl, optimize_debug)
593 : 28951429 : && 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 : 21772443 : 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 : 21772443 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
616 : 21772443 : class ipa_fn_summary *info = ipa_fn_summaries->get (callee);
617 : 21772443 : class ipa_edge_args *args;
618 : 21772443 : class ipa_call_summary *es = NULL;
619 : :
620 : 21772443 : if (clause_ptr)
621 : 21772443 : *clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
622 : :
623 : 21772443 : if (ipa_node_params_sum
624 : 10004986 : && !e->call_stmt_cannot_inline_p
625 : 10004986 : && (info->conds || compute_contexts)
626 : 31777429 : && (args = ipa_edge_args_sum->get (e)) != NULL)
627 : : {
628 : 9950716 : struct cgraph_node *caller;
629 : 9950716 : class ipa_node_params *caller_parms_info, *callee_pi = NULL;
630 : 9950716 : int i, count = ipa_get_cs_argument_count (args);
631 : 9950716 : es = ipa_call_summaries->get (e);
632 : :
633 : 9950716 : if (count)
634 : : {
635 : 9268362 : if (e->caller->inlined_to)
636 : : caller = e->caller->inlined_to;
637 : : else
638 : 7537555 : caller = e->caller;
639 : 9268362 : caller_parms_info = ipa_node_params_sum->get (caller);
640 : 9268362 : callee_pi = ipa_node_params_sum->get (callee);
641 : :
642 : : /* Watch for thunks. */
643 : 9268362 : if (callee_pi)
644 : : /* Watch for variadic functions. */
645 : 9268039 : count = MIN (count, ipa_get_param_count (callee_pi));
646 : : }
647 : :
648 : 9268039 : if (callee_pi)
649 : 30414939 : for (i = 0; i < count; i++)
650 : : {
651 : 21146900 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
652 : :
653 : 21146900 : if (ipa_is_param_used_by_indirect_call (callee_pi, i)
654 : 21146900 : || ipa_is_param_used_by_ipa_predicates (callee_pi, i))
655 : : {
656 : : /* Determine if we know constant value of the parameter. */
657 : 14476855 : tree type = ipa_get_type (callee_pi, i);
658 : 14476855 : tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type);
659 : :
660 : 11242972 : if (!cst && e->call_stmt
661 : 25670176 : && i < (int)gimple_call_num_args (e->call_stmt))
662 : : {
663 : 11193321 : cst = gimple_call_arg (e->call_stmt, i);
664 : 11193321 : if (!is_gimple_min_invariant (cst))
665 : : cst = NULL;
666 : : }
667 : 3465188 : if (cst)
668 : : {
669 : 3415537 : gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
670 : 3415537 : if (!avals->m_known_vals.length ())
671 : 1816291 : avals->m_known_vals.safe_grow_cleared (count, true);
672 : 3415537 : avals->m_known_vals[i] = cst;
673 : : }
674 : 11061318 : else if (inline_p && !es->param[i].change_prob)
675 : : {
676 : 4204290 : if (!avals->m_known_vals.length ())
677 : 3510138 : avals->m_known_vals.safe_grow_cleared (count, true);
678 : 4204290 : avals->m_known_vals[i] = error_mark_node;
679 : : }
680 : :
681 : : /* If we failed to get simple constant, try value range. */
682 : 3415537 : if ((!cst || TREE_CODE (cst) != INTEGER_CST)
683 : 12131351 : && vrp_will_run_p (caller)
684 : 26446763 : && ipa_is_param_used_by_ipa_predicates (callee_pi, i))
685 : : {
686 : 11929571 : value_range vr (type);
687 : :
688 : 11929571 : ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type);
689 : 11929571 : if (!vr.undefined_p () && !vr.varying_p ())
690 : : {
691 : 8507167 : if (!avals->m_known_value_ranges.length ())
692 : : {
693 : 6106870 : avals->m_known_value_ranges.safe_grow_cleared (count,
694 : : true);
695 : 19426871 : for (int i = 0; i < count; ++i)
696 : 13320001 : avals->m_known_value_ranges[i].set_type (void_type_node);
697 : : }
698 : 8507167 : avals->m_known_value_ranges[i] = vr;
699 : : }
700 : 11929571 : }
701 : :
702 : : /* Determine known aggregate values. */
703 : 14476855 : if (fre_will_run_p (caller))
704 : 14474170 : 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 : 21146900 : if (compute_contexts
712 : 21146900 : && ipa_is_param_used_by_polymorphic_call (callee_pi, i))
713 : : {
714 : 158228 : ipa_polymorphic_call_context
715 : 158228 : ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
716 : 316456 : if (!ctx.useless_p ())
717 : : {
718 : 156280 : if (!avals->m_known_contexts.length ())
719 : 156143 : avals->m_known_contexts.safe_grow_cleared (count, true);
720 : 156280 : avals->m_known_contexts[i]
721 : 312560 : = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
722 : : }
723 : : }
724 : : }
725 : : else
726 : 682677 : gcc_assert (!count || callee->thunk);
727 : : }
728 : 11821727 : else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds)
729 : : {
730 : 9253030 : int i, count = (int)gimple_call_num_args (e->call_stmt);
731 : :
732 : 26307184 : for (i = 0; i < count; i++)
733 : : {
734 : 17054154 : tree cst = gimple_call_arg (e->call_stmt, i);
735 : 17054154 : if (!is_gimple_min_invariant (cst))
736 : : cst = NULL;
737 : 6032803 : if (cst)
738 : : {
739 : 6032803 : if (!avals->m_known_vals.length ())
740 : 4172245 : avals->m_known_vals.safe_grow_cleared (count, true);
741 : 6032803 : avals->m_known_vals[i] = cst;
742 : : }
743 : : }
744 : : }
745 : :
746 : 21772443 : evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr,
747 : : nonspec_clause_ptr, es);
748 : 21772443 : }
749 : :
750 : :
751 : : /* Allocate the function summary. */
752 : :
753 : : static void
754 : 456856 : ipa_fn_summary_alloc (void)
755 : : {
756 : 456856 : gcc_checking_assert (!ipa_fn_summaries);
757 : 456856 : ipa_size_summaries = new ipa_size_summary_t (symtab);
758 : 456856 : ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab);
759 : 456856 : ipa_call_summaries = new ipa_call_summary_t (symtab);
760 : 456856 : }
761 : :
762 : 26884285 : ipa_call_summary::~ipa_call_summary ()
763 : : {
764 : 26884285 : if (predicate)
765 : 3289312 : edge_predicate_pool.remove (predicate);
766 : :
767 : 26884285 : param.release ();
768 : 26884285 : }
769 : :
770 : 9938047 : ipa_fn_summary::~ipa_fn_summary ()
771 : : {
772 : 9938047 : unsigned len = vec_safe_length (loop_iterations);
773 : 10050465 : for (unsigned i = 0; i < len; i++)
774 : 112418 : edge_predicate_pool.remove ((*loop_iterations)[i].predicate);
775 : 9938047 : len = vec_safe_length (loop_strides);
776 : 9971286 : for (unsigned i = 0; i < len; i++)
777 : 33239 : edge_predicate_pool.remove ((*loop_strides)[i].predicate);
778 : 9938047 : vec_free (conds);
779 : 9938047 : call_size_time_table.release ();
780 : 9938047 : vec_free (loop_iterations);
781 : 9938047 : vec_free (loop_strides);
782 : 9938047 : builtin_constant_p_parms.release ();
783 : 9938047 : }
784 : :
785 : : void
786 : 7193040 : ipa_fn_summary_t::remove_callees (cgraph_node *node)
787 : : {
788 : 7193040 : cgraph_edge *e;
789 : 29519992 : for (e = node->callees; e; e = e->next_callee)
790 : 22326952 : ipa_call_summaries->remove (e);
791 : 7673834 : for (e = node->indirect_calls; e; e = e->next_callee)
792 : 480794 : ipa_call_summaries->remove (e);
793 : 7193040 : }
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 : 29150 : remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v,
801 : : clause_t possible_truths)
802 : : {
803 : 29150 : 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 : 11038 : for (int i = len - 1; i >= 0; i--)
809 : : {
810 : 6339 : ipa_predicate new_predicate
811 : 6339 : = (*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 : 6339 : (*res)[i].predicate = NULL;
815 : 6339 : set_hint_predicate (&(*res)[i].predicate, new_predicate);
816 : :
817 : 6339 : if (!(*res)[i].predicate)
818 : 3003 : 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 : 2658590 : 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 : 2658590 : 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 : 2658590 : info->conds = vec_safe_copy (info->conds);
836 : :
837 : 2658590 : 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 : 2658590 : if (ipa_node_params_sum && cinfo && cinfo->tree_map)
841 : : {
842 : : /* Use SRC parm info since it may not be copied yet. */
843 : 14575 : ipa_node_params *parms_info = ipa_node_params_sum->get (src);
844 : 14575 : ipa_auto_call_arg_values avals;
845 : 14575 : int count = ipa_get_param_count (parms_info);
846 : 14575 : int i, j;
847 : 14575 : clause_t possible_truths;
848 : 14575 : ipa_predicate true_pred = true;
849 : 14575 : size_time_entry *e;
850 : 14575 : int optimized_out_size = 0;
851 : 14575 : bool inlined_to_p = false;
852 : 14575 : struct cgraph_edge *edge, *next;
853 : :
854 : 14575 : info->size_time_table.release ();
855 : 14575 : avals.m_known_vals.safe_grow_cleared (count, true);
856 : 59541 : for (i = 0; i < count; i++)
857 : : {
858 : : struct ipa_replace_map *r;
859 : :
860 : 147909 : for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++)
861 : : {
862 : 83443 : if (r->parm_num == i)
863 : : {
864 : 25466 : avals.m_known_vals[i] = r->new_tree;
865 : 25466 : break;
866 : : }
867 : : }
868 : : }
869 : 14575 : 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 : 14575 : 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 : 104353 : for (i = 0; src_info->size_time_table.iterate (i, &e); i++)
885 : : {
886 : 89778 : ipa_predicate new_exec_pred;
887 : 89778 : ipa_predicate new_nonconst_pred;
888 : 89778 : new_exec_pred = e->exec_predicate.remap_after_duplication
889 : 89778 : (possible_truths);
890 : 89778 : new_nonconst_pred = e->nonconst_predicate.remap_after_duplication
891 : 89778 : (possible_truths);
892 : 89778 : if (new_exec_pred == false || new_nonconst_pred == false)
893 : 12923 : optimized_out_size += e->size;
894 : : else
895 : 76855 : 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 : 189935 : for (edge = dst->callees; edge; edge = next)
902 : : {
903 : 175360 : ipa_predicate new_predicate;
904 : 175360 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
905 : 175360 : next = edge->next_callee;
906 : :
907 : 175360 : if (!edge->inline_failed)
908 : 0 : inlined_to_p = true;
909 : 175360 : if (!es->predicate)
910 : 159497 : continue;
911 : 15863 : new_predicate = es->predicate->remap_after_duplication
912 : 15863 : (possible_truths);
913 : 15863 : if (new_predicate == false && *es->predicate != false)
914 : 4440 : optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale;
915 : 15863 : 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 : 15420 : for (edge = dst->indirect_calls; edge; edge = next)
921 : : {
922 : 845 : ipa_predicate new_predicate;
923 : 845 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
924 : 845 : next = edge->next_callee;
925 : :
926 : 845 : gcc_checking_assert (edge->inline_failed);
927 : 845 : if (!es->predicate)
928 : 724 : continue;
929 : 121 : new_predicate = es->predicate->remap_after_duplication
930 : 121 : (possible_truths);
931 : 121 : if (new_predicate == false && *es->predicate != false)
932 : 24 : optimized_out_size
933 : 24 : += es->call_stmt_size * ipa_fn_summary::size_scale;
934 : 121 : edge_set_predicate (edge, &new_predicate);
935 : : }
936 : 14575 : info->loop_iterations
937 : 14575 : = remap_freqcounting_preds_after_dup (info->loop_iterations,
938 : : possible_truths);
939 : 14575 : info->loop_strides
940 : 14575 : = remap_freqcounting_preds_after_dup (info->loop_strides,
941 : : possible_truths);
942 : 14575 : 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 : 14575 : gcc_assert (!inlined_to_p || !optimized_out_size);
957 : 14575 : }
958 : : else
959 : : {
960 : 2644015 : info->size_time_table = src_info->size_time_table.copy ();
961 : 2644015 : info->loop_iterations = vec_safe_copy (src_info->loop_iterations);
962 : 2644015 : info->loop_strides = vec_safe_copy (info->loop_strides);
963 : :
964 : 2644015 : info->builtin_constant_p_parms
965 : 2644015 : = info->builtin_constant_p_parms.copy ();
966 : :
967 : 2644015 : ipa_freqcounting_predicate *f;
968 : 2692716 : for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++)
969 : : {
970 : 48701 : ipa_predicate p = *f->predicate;
971 : 48701 : f->predicate = NULL;
972 : 48701 : set_hint_predicate (&f->predicate, p);
973 : : }
974 : 2667828 : for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++)
975 : : {
976 : 23813 : ipa_predicate p = *f->predicate;
977 : 23813 : f->predicate = NULL;
978 : 23813 : set_hint_predicate (&f->predicate, p);
979 : : }
980 : : }
981 : 2658590 : if (!dst->inlined_to)
982 : 135010 : ipa_update_overall_fn_summary (dst);
983 : 2658590 : }
984 : :
985 : :
986 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
987 : :
988 : : void
989 : 3724601 : 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 : 3724601 : new (info) ipa_call_summary (*srcinfo);
995 : 3724601 : info->predicate = NULL;
996 : 3724601 : edge_set_predicate (dst, srcinfo->predicate);
997 : 3724601 : info->param = srcinfo->param.copy ();
998 : 3724601 : 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 : 24788 : && !src->has_callback && !dst->callback)
1002 : : {
1003 : 24788 : info->call_stmt_size -= (eni_size_weights.indirect_call_cost
1004 : 24788 : - eni_size_weights.call_cost);
1005 : 24788 : info->call_stmt_time -= (eni_time_weights.indirect_call_cost
1006 : 24788 : - eni_time_weights.call_cost);
1007 : : }
1008 : 3724601 : }
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 : 5978 : for (i = 0; s->size_time_table.iterate (i, &e); i++)
1137 : : {
1138 : 4548 : fprintf (f, " size:%f, time:%f",
1139 : 4548 : (double) e->size / ipa_fn_summary::size_scale,
1140 : : e->time.to_double ());
1141 : 4548 : if (e->exec_predicate != true)
1142 : : {
1143 : 2659 : fprintf (f, ", executed if:");
1144 : 2659 : e->exec_predicate.dump (f, s->conds, 0);
1145 : : }
1146 : 4548 : if (e->exec_predicate != e->nonconst_predicate)
1147 : : {
1148 : 1189 : fprintf (f, ", nonconst if:");
1149 : 1189 : e->nonconst_predicate.dump (f, s->conds, 0);
1150 : : }
1151 : 4548 : 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 : 841946 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1208 : : void *data)
1209 : : {
1210 : 841946 : bool *b = (bool *) data;
1211 : 841946 : *b = true;
1212 : 841946 : 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 : 182453013 : 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 : 182453013 : if (TREE_CODE (op) == SSA_NAME
1225 : 106853709 : && SSA_NAME_IS_DEFAULT_DEF (op)
1226 : 207854177 : && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1227 : : {
1228 : 25205402 : if (size_p)
1229 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1230 : 25205402 : return SSA_NAME_VAR (op);
1231 : : }
1232 : : /* Non-SSA parm reference? */
1233 : 157247611 : if (TREE_CODE (op) == PARM_DECL
1234 : 1449993 : && fbi->aa_walk_budget > 0)
1235 : : {
1236 : 1447447 : bool modified = false;
1237 : :
1238 : 1447447 : ao_ref refd;
1239 : 1447447 : ao_ref_init (&refd, op);
1240 : 2894894 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
1241 : : mark_modified, &modified, NULL, NULL,
1242 : : fbi->aa_walk_budget);
1243 : 1447447 : if (walked < 0)
1244 : : {
1245 : 8 : fbi->aa_walk_budget = 0;
1246 : 928296 : return NULL_TREE;
1247 : : }
1248 : 1447439 : fbi->aa_walk_budget -= walked;
1249 : 1447439 : if (!modified)
1250 : : {
1251 : 928288 : if (size_p)
1252 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1253 : 928288 : 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 : 119223954 : unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op,
1266 : : poly_int64 *size_p)
1267 : : {
1268 : 148298494 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1269 : 148298494 : if (res)
1270 : : return res;
1271 : :
1272 : 122980379 : if (TREE_CODE (op) == SSA_NAME
1273 : 69815453 : && !SSA_NAME_IS_DEFAULT_DEF (op)
1274 : 192604545 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1275 : 29074540 : return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op),
1276 : 29074540 : gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)),
1277 : 29074540 : 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 : 32706442 : 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 : 34154519 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1295 : :
1296 : 34154519 : gcc_checking_assert (aggpos);
1297 : 34154519 : if (res)
1298 : : {
1299 : 815575 : *index_p = ipa_get_param_decl_index (fbi->info, res);
1300 : 815575 : if (*index_p < 0)
1301 : : return false;
1302 : 815446 : aggpos->agg_contents = false;
1303 : 815446 : aggpos->by_ref = false;
1304 : 815446 : return true;
1305 : : }
1306 : :
1307 : 33338944 : if (TREE_CODE (op) == SSA_NAME)
1308 : : {
1309 : 11832854 : if (SSA_NAME_IS_DEFAULT_DEF (op)
1310 : 11832854 : || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1311 : : return false;
1312 : 4775053 : stmt = SSA_NAME_DEF_STMT (op);
1313 : 4775053 : op = gimple_assign_rhs1 (stmt);
1314 : 4775053 : 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 : 24833066 : aggpos->agg_contents = true;
1320 : 24833066 : return ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
1321 : : stmt, op, index_p, &aggpos->offset,
1322 : 24833066 : 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 : 110753026 : load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt)
1330 : : {
1331 : 110753026 : if (!optimize)
1332 : : return -1;
1333 : 158568597 : gassign *assign = dyn_cast <gassign *> (stmt);
1334 : 55007871 : if (!assign)
1335 : : return -1;
1336 : 55007871 : tree param;
1337 : 55007871 : if (gimple_assign_load_p (stmt))
1338 : 20259860 : param = gimple_assign_rhs1 (stmt);
1339 : 34748011 : else if (gimple_store_p (stmt))
1340 : 18441535 : param = gimple_assign_lhs (stmt);
1341 : : else
1342 : : return -1;
1343 : 38701395 : tree base = get_base_address (param);
1344 : 38701395 : if (TREE_CODE (base) != MEM_REF
1345 : 13432305 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1346 : 52129626 : || !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1347 : : return -1;
1348 : 7276200 : tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1349 : 7276200 : if (TREE_CODE (p) != PARM_DECL)
1350 : : return -1;
1351 : 7192300 : 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 : 110753026 : eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt)
1363 : : {
1364 : 110753026 : enum gimple_code code = gimple_code (stmt);
1365 : 110753026 : enum tree_code rhs_code;
1366 : :
1367 : 110753026 : if (!optimize)
1368 : : return 0;
1369 : :
1370 : 92559509 : switch (code)
1371 : : {
1372 : : case GIMPLE_RETURN:
1373 : : return 2;
1374 : 55007871 : case GIMPLE_ASSIGN:
1375 : 55007871 : if (gimple_num_ops (stmt) != 2)
1376 : : return 0;
1377 : :
1378 : 39504740 : 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 : 39504740 : if (CONVERT_EXPR_CODE_P (rhs_code)
1385 : : || rhs_code == VIEW_CONVERT_EXPR
1386 : : || rhs_code == ADDR_EXPR
1387 : 36747141 : || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1388 : : {
1389 : 38701395 : tree rhs = gimple_assign_rhs1 (stmt);
1390 : 38701395 : tree lhs = gimple_assign_lhs (stmt);
1391 : 38701395 : tree inner_rhs = get_base_address (rhs);
1392 : 38701395 : tree inner_lhs = get_base_address (lhs);
1393 : 38701395 : bool rhs_free = false;
1394 : 38701395 : bool lhs_free = false;
1395 : :
1396 : 38701395 : if (!inner_rhs)
1397 : 0 : inner_rhs = rhs;
1398 : 38701395 : if (!inner_lhs)
1399 : 0 : inner_lhs = lhs;
1400 : :
1401 : : /* Reads of parameter are expected to be free. */
1402 : 38701395 : 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 : 37436134 : else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1407 : : {
1408 : 2459578 : tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1409 : 2459578 : if (TREE_CODE (op) == PARM_DECL)
1410 : : rhs_free = true;
1411 : 2422568 : else if (TREE_CODE (op) == MEM_REF
1412 : 2422568 : && 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 : 1302271 : 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 : 38027324 : if (TREE_CODE (inner_rhs) == MEM_REF
1426 : 38027324 : && 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 : 38027324 : 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 : 38027324 : if (TREE_CODE (inner_lhs) == PARM_DECL
1465 : 37938042 : || TREE_CODE (inner_lhs) == RESULT_DECL
1466 : 75129603 : || (TREE_CODE (inner_lhs) == MEM_REF
1467 : 4991146 : && (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0),
1468 : : NULL)
1469 : 2561074 : || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1470 : 2559887 : && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1471 : 359017 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1472 : : (inner_lhs,
1473 : : 0))) == RESULT_DECL))))
1474 : : lhs_free = true;
1475 : 34591529 : if (lhs_free
1476 : 38027324 : && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1477 : : rhs_free = true;
1478 : 38027324 : 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 : 26950673 : 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 : 26950673 : int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops);
1502 : 26950673 : int op_count = 0;
1503 : :
1504 : 26950673 : if (param_ops_p)
1505 : 9222556 : *param_ops_p = NULL;
1506 : :
1507 : 32706442 : while (true)
1508 : : {
1509 : 32706442 : expr_eval_op eval_op;
1510 : 32706442 : unsigned rhs_count;
1511 : 32706442 : unsigned cst_count = 0;
1512 : :
1513 : 32706442 : if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL,
1514 : : aggpos))
1515 : : {
1516 : 4619675 : tree type = TREE_TYPE (expr);
1517 : :
1518 : 4619675 : if (aggpos->agg_contents)
1519 : : {
1520 : : /* Stop if containing bit-field. */
1521 : 3804229 : if (TREE_CODE (expr) == BIT_FIELD_REF
1522 : 3804229 : || contains_bitfld_component_ref_p (expr))
1523 : : break;
1524 : : }
1525 : :
1526 : 4591662 : *type_p = type;
1527 : 4591662 : return true;
1528 : : }
1529 : :
1530 : 28086767 : if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr))
1531 : : break;
1532 : 10490629 : stmt = SSA_NAME_DEF_STMT (expr);
1533 : :
1534 : 10490629 : if (gcall *call = dyn_cast <gcall *> (stmt))
1535 : : {
1536 : 2461492 : int flags = gimple_call_return_flags (call);
1537 : 2461492 : if (!(flags & ERF_RETURNS_ARG))
1538 : 3058662 : goto fail;
1539 : 208603 : int arg = flags & ERF_RETURN_ARG_MASK;
1540 : 208603 : if (arg >= (int)gimple_call_num_args (call))
1541 : 0 : goto fail;
1542 : 208603 : expr = gimple_call_arg (stmt, arg);
1543 : 3981937 : continue;
1544 : 208603 : }
1545 : :
1546 : 8029137 : if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr)))
1547 : : break;
1548 : :
1549 : 6353014 : switch (gimple_assign_rhs_class (stmt))
1550 : : {
1551 : 3773334 : case GIMPLE_SINGLE_RHS:
1552 : 3773334 : expr = gimple_assign_rhs1 (stmt);
1553 : 3773334 : continue;
1554 : :
1555 : : case GIMPLE_UNARY_RHS:
1556 : : rhs_count = 1;
1557 : : break;
1558 : :
1559 : 1553397 : case GIMPLE_BINARY_RHS:
1560 : 1553397 : rhs_count = 2;
1561 : 1553397 : 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 : 2579680 : if (op_count++ == op_limit)
1573 : : break;
1574 : :
1575 : 2579605 : if (param_ops_p)
1576 : : {
1577 : 2578092 : eval_op.code = gimple_assign_rhs_code (stmt);
1578 : 2578092 : eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt));
1579 : 2578092 : eval_op.val[0] = NULL_TREE;
1580 : 2578092 : eval_op.val[1] = NULL_TREE;
1581 : : }
1582 : :
1583 : : expr = NULL_TREE;
1584 : 5907490 : for (unsigned i = 0; i < rhs_count; i++)
1585 : : {
1586 : 4133658 : tree op = gimple_op (stmt, i + 1);
1587 : :
1588 : 4133658 : gcc_assert (op && !TYPE_P (op));
1589 : 4133658 : if (is_gimple_ip_invariant (op))
1590 : : {
1591 : 750618 : if (++cst_count == rhs_count)
1592 : 1169 : goto fail;
1593 : :
1594 : 749449 : eval_op.val[cst_count - 1] = op;
1595 : : }
1596 : 3383040 : else if (!expr)
1597 : : {
1598 : : /* Found a non-constant operand, and record its index in rhs
1599 : : operands. */
1600 : 2578436 : eval_op.index = i;
1601 : 2578436 : expr = op;
1602 : : }
1603 : : else
1604 : : {
1605 : : /* Found more than one non-constant operands. */
1606 : 804604 : goto fail;
1607 : : }
1608 : : }
1609 : :
1610 : 1773832 : if (param_ops_p)
1611 : 1772573 : vec_safe_insert (*param_ops_p, 0, eval_op);
1612 : : }
1613 : :
1614 : : /* Failed to decompose, free resource and return. */
1615 : 22359011 : fail:
1616 : 22359011 : if (param_ops_p)
1617 : 9028151 : 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 : 10528 : add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm)
1626 : : {
1627 : 10528 : int ip;
1628 : :
1629 : : /* Avoid duplicates. */
1630 : 10546 : for (unsigned int i = 0;
1631 : 10546 : summary->builtin_constant_p_parms.iterate (i, &ip); i++)
1632 : 5102 : if (ip == parm)
1633 : 10528 : return;
1634 : 5444 : 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 : 34936224 : 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 : 34936224 : tree op, op2;
1647 : 34936224 : int index;
1648 : 34936224 : struct agg_position_info aggpos;
1649 : 34936224 : enum tree_code code, inverted_code;
1650 : 34936224 : edge e;
1651 : 34936224 : edge_iterator ei;
1652 : 34936224 : gimple *set_stmt;
1653 : 34936224 : tree param_type;
1654 : 34936224 : expr_eval_ops param_ops;
1655 : :
1656 : 69872448 : gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1657 : 11563807 : if (!last)
1658 : 34925696 : return;
1659 : 11563807 : if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1660 : : return;
1661 : 9141113 : op = gimple_cond_lhs (last);
1662 : :
1663 : 9141113 : if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1664 : : ¶m_ops))
1665 : : {
1666 : 1312335 : code = gimple_cond_code (last);
1667 : 1312335 : inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1668 : :
1669 : 3937005 : FOR_EACH_EDGE (e, ei, bb->succs)
1670 : : {
1671 : 1312335 : enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1672 : 2624670 : ? 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 : 2624670 : if (this_code != ERROR_MARK
1682 : 2624670 : && !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1683 : : {
1684 : 2212454 : ipa_predicate p
1685 : 2212454 : = add_condition (summary, params_summary, index,
1686 : : param_type, &aggpos,
1687 : : this_code, gimple_cond_rhs (last), param_ops);
1688 : 2212454 : e->aux = edge_predicate_pool.allocate ();
1689 : 2212454 : *(ipa_predicate *) e->aux = p;
1690 : : }
1691 : : }
1692 : 1312335 : vec_free (param_ops);
1693 : 1312335 : return;
1694 : : }
1695 : :
1696 : 7828778 : 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 : 7827707 : if (gimple_cond_code (last) != NE_EXPR
1709 : 7827707 : || !integer_zerop (gimple_cond_rhs (last)))
1710 : 4445241 : return;
1711 : 3382466 : set_stmt = SSA_NAME_DEF_STMT (op);
1712 : 3382466 : if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1713 : 3382466 : || gimple_call_num_args (set_stmt) != 1)
1714 : : return;
1715 : 11678 : op2 = gimple_call_arg (set_stmt, 0);
1716 : 11678 : if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos))
1717 : : return;
1718 : 10528 : if (!aggpos.by_ref)
1719 : 10495 : add_builtin_constant_p_parm (summary, index);
1720 : 31584 : FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1721 : : {
1722 : 10528 : ipa_predicate p = add_condition (summary, params_summary, index,
1723 : : param_type, &aggpos,
1724 : : ipa_predicate::is_not_constant, NULL_TREE);
1725 : 10528 : e->aux = edge_predicate_pool.allocate ();
1726 : 10528 : *(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 : 34936224 : 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 : 34936224 : tree op;
1741 : 34936224 : int index;
1742 : 34936224 : struct agg_position_info aggpos;
1743 : 34936224 : edge e;
1744 : 34936224 : edge_iterator ei;
1745 : 34936224 : size_t n;
1746 : 34936224 : size_t case_idx;
1747 : 34936224 : tree param_type;
1748 : 34936224 : expr_eval_ops param_ops;
1749 : :
1750 : 69872448 : gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
1751 : 81443 : if (!last)
1752 : 34904880 : return;
1753 : 81443 : op = gimple_switch_index (last);
1754 : 81443 : if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1755 : : ¶m_ops))
1756 : : return;
1757 : :
1758 : 46656 : auto_vec<std::pair<tree, tree> > ranges;
1759 : 46656 : tree type = TREE_TYPE (op);
1760 : 46656 : int bound_limit = opt_for_fn (fbi->node->decl,
1761 : : param_ipa_max_switch_predicate_bounds);
1762 : 46656 : int bound_count = 0;
1763 : : // This can safely be an integer range, as switches can only hold
1764 : : // integers.
1765 : 46656 : int_range<2> vr;
1766 : :
1767 : 93312 : get_range_query (cfun)->range_of_expr (vr, op);
1768 : 46656 : if (vr.undefined_p ())
1769 : 0 : vr.set_varying (TREE_TYPE (op));
1770 : 46656 : 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 : 46656 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
1775 : 46656 : wide_int vr_wmin = wi::to_wide (vr_min);
1776 : 46656 : wide_int vr_wmax = wi::to_wide (vr_max);
1777 : :
1778 : 325093 : FOR_EACH_EDGE (e, ei, bb->succs)
1779 : : {
1780 : 278437 : e->aux = edge_predicate_pool.allocate ();
1781 : 278437 : *(ipa_predicate *) e->aux = false;
1782 : : }
1783 : :
1784 : 46656 : 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 : 46656 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1790 : 12598 : bound_count = INT_MAX;
1791 : :
1792 : 46656 : n = gimple_switch_num_labels (last);
1793 : 310435 : for (case_idx = 1; case_idx < n; ++case_idx)
1794 : : {
1795 : 263779 : tree cl = gimple_switch_label (last, case_idx);
1796 : 263779 : tree min = CASE_LOW (cl);
1797 : 263779 : tree max = CASE_HIGH (cl);
1798 : 263779 : ipa_predicate p;
1799 : :
1800 : 263779 : 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 : 263779 : if (TREE_TYPE (min) != type)
1805 : 50316 : min = wide_int_to_tree (type, wi::to_wide (min));
1806 : :
1807 : 263779 : if (!max)
1808 : : max = min;
1809 : 14479 : else if (TREE_TYPE (max) != type)
1810 : 5703 : 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 : 263779 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1816 : 4974 : p = true;
1817 : 258805 : else if (min == max)
1818 : 246006 : p = add_condition (summary, params_summary, index, param_type,
1819 : : &aggpos, EQ_EXPR, min, param_ops);
1820 : : else
1821 : : {
1822 : 12799 : ipa_predicate p1, p2;
1823 : 12799 : p1 = add_condition (summary, params_summary, index, param_type,
1824 : : &aggpos, GE_EXPR, min, param_ops);
1825 : 12799 : p2 = add_condition (summary, params_summary,index, param_type,
1826 : : &aggpos, LE_EXPR, max, param_ops);
1827 : 12799 : p = p1 & p2;
1828 : : }
1829 : 263779 : *(ipa_predicate *) e->aux
1830 : 263779 : = 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 : 263779 : if (bound_count > bound_limit)
1835 : 96239 : continue;
1836 : :
1837 : 167540 : bool new_range = true;
1838 : :
1839 : 167540 : if (!ranges.is_empty ())
1840 : : {
1841 : 133482 : wide_int curr_wmin = wi::to_wide (min);
1842 : 133482 : wide_int last_wmax = wi::to_wide (ranges.last ().second);
1843 : :
1844 : : /* Merge case ranges if they are continuous. */
1845 : 133482 : if (curr_wmin == last_wmax + 1)
1846 : : new_range = false;
1847 : 45714 : 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 : 133482 : }
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 : 133482 : if (new_range)
1862 : : {
1863 : 79772 : bound_count += (min == max) ? 1 : 2;
1864 : 79772 : ranges.safe_push (std::make_pair (min, max));
1865 : : }
1866 : : else
1867 : : {
1868 : 87768 : bound_count += (ranges.last ().first == ranges.last ().second);
1869 : 87768 : ranges.last ().second = max;
1870 : : }
1871 : : }
1872 : :
1873 : 46656 : e = gimple_switch_edge (cfun, last, 0);
1874 : 46656 : if (bound_count > bound_limit)
1875 : : {
1876 : 15312 : *(ipa_predicate *) e->aux = true;
1877 : 15312 : vec_free (param_ops);
1878 : 15312 : return;
1879 : : }
1880 : :
1881 : 31344 : ipa_predicate p_seg = true;
1882 : 31344 : ipa_predicate p_all = false;
1883 : :
1884 : 31344 : if (vr_type != VR_RANGE)
1885 : : {
1886 : 30823 : vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type));
1887 : 30823 : 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 : 97648 : for (size_t i = 0; i < ranges.length (); i++)
1902 : : {
1903 : 66399 : tree min = ranges[i].first;
1904 : 66399 : tree max = ranges[i].second;
1905 : :
1906 : 66399 : if (min == max)
1907 : 82698 : p_seg &= add_condition (summary, params_summary, index,
1908 : : param_type, &aggpos, NE_EXPR,
1909 : 41349 : min, param_ops);
1910 : : else
1911 : : {
1912 : : /* Do not create sub-predicate for range that is beyond low bound
1913 : : of switch index. */
1914 : 25050 : if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type)))
1915 : : {
1916 : 17615 : p_seg &= add_condition (summary, params_summary, index,
1917 : : param_type, &aggpos,
1918 : 17615 : LT_EXPR, min, param_ops);
1919 : 17615 : 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 : 25050 : 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 : 24955 : p_seg = add_condition (summary, params_summary, index,
1931 : : param_type, &aggpos, GT_EXPR,
1932 : : max, param_ops);
1933 : : }
1934 : : }
1935 : :
1936 : 31344 : p_all = p_all.or_with (summary->conds, p_seg);
1937 : 31344 : *(ipa_predicate *) e->aux
1938 : 31344 : = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux);
1939 : :
1940 : 37799 : vec_free (param_ops);
1941 : 46656 : }
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 : 6281644 : 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 : 6281644 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1954 : 6281644 : bool done = false;
1955 : 6281644 : basic_block bb;
1956 : :
1957 : 41217868 : FOR_EACH_BB_FN (bb, my_function)
1958 : : {
1959 : 34936224 : set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb);
1960 : 34936224 : set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb);
1961 : : }
1962 : :
1963 : : /* Entry block is always executable. */
1964 : 6281644 : ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1965 : 6281644 : = edge_predicate_pool.allocate ();
1966 : 6281644 : *(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 : 18964127 : while (!done)
1971 : : {
1972 : 12682483 : done = true;
1973 : 87668336 : FOR_EACH_BB_FN (bb, my_function)
1974 : : {
1975 : 74985853 : ipa_predicate p = false;
1976 : 74985853 : edge e;
1977 : 74985853 : edge_iterator ei;
1978 : 93069028 : FOR_EACH_EDGE (e, ei, bb->preds)
1979 : : {
1980 : 79728496 : if (e->src->aux)
1981 : : {
1982 : 77976683 : ipa_predicate this_bb_predicate
1983 : : = *(ipa_predicate *) e->src->aux;
1984 : 77976683 : if (e->aux)
1985 : 5210198 : this_bb_predicate &= (*(ipa_predicate *) e->aux);
1986 : 77976683 : p = p.or_with (summary->conds, this_bb_predicate);
1987 : 77976683 : if (p == true)
1988 : : break;
1989 : : }
1990 : : }
1991 : 74985853 : if (p != false)
1992 : : {
1993 : 73842073 : basic_block pdom_bb;
1994 : :
1995 : 73842073 : if (!bb->aux)
1996 : : {
1997 : 27358451 : done = false;
1998 : 27358451 : bb->aux = edge_predicate_pool.allocate ();
1999 : 27358451 : *((ipa_predicate *) bb->aux) = p;
2000 : : }
2001 : 46483622 : 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 : 129437 : p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux);
2007 : 129437 : if (p != *(ipa_predicate *)bb->aux)
2008 : : {
2009 : 106098 : done = false;
2010 : 106098 : *((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 : 73842073 : pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
2024 : 73842073 : if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb)
2025 : : ;
2026 : 37994245 : else if (!pdom_bb->aux)
2027 : : {
2028 : 7577759 : done = false;
2029 : 7577759 : pdom_bb->aux = edge_predicate_pool.allocate ();
2030 : 7577759 : *((ipa_predicate *)pdom_bb->aux) = p;
2031 : : }
2032 : 30416486 : else if (p != *(ipa_predicate *)pdom_bb->aux)
2033 : : {
2034 : 3404620 : p = p.or_with (summary->conds,
2035 : : *(ipa_predicate *)pdom_bb->aux);
2036 : 3404620 : if (p != *(ipa_predicate *)pdom_bb->aux)
2037 : : {
2038 : 154661 : done = false;
2039 : 154661 : *((ipa_predicate *)pdom_bb->aux) = p;
2040 : : }
2041 : : }
2042 : : }
2043 : : }
2044 : : }
2045 : 6281644 : }
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 : 4203239 : 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 : 4203239 : tree parm;
2059 : 4203239 : int index;
2060 : :
2061 : 4450449 : while (UNARY_CLASS_P (expr))
2062 : 247210 : expr = TREE_OPERAND (expr, 0);
2063 : :
2064 : 4203239 : parm = unmodified_parm (fbi, NULL, expr, NULL);
2065 : 4203239 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2066 : 228747 : return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL,
2067 : 228747 : ipa_predicate::changed, NULL_TREE);
2068 : 3974492 : if (is_gimple_min_invariant (expr))
2069 : 62186 : return false;
2070 : 3912306 : if (TREE_CODE (expr) == SSA_NAME)
2071 : 3730956 : return nonconstant_names[SSA_NAME_VERSION (expr)];
2072 : 181350 : if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2073 : : {
2074 : 180738 : ipa_predicate p1
2075 : 180738 : = will_be_nonconstant_expr_predicate (fbi, summary,
2076 : : params_summary,
2077 : 180738 : TREE_OPERAND (expr, 0),
2078 : : nonconstant_names);
2079 : 180738 : if (p1 == true)
2080 : 105450 : return p1;
2081 : :
2082 : 75288 : ipa_predicate p2
2083 : 75288 : = will_be_nonconstant_expr_predicate (fbi, summary,
2084 : : params_summary,
2085 : 75288 : TREE_OPERAND (expr, 1),
2086 : : nonconstant_names);
2087 : 75288 : return p1.or_with (summary->conds, p2);
2088 : : }
2089 : 612 : 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 : 428 : else if (TREE_CODE (expr) == CALL_EXPR)
2114 : 428 : 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 : 112932161 : 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 : 112932161 : ipa_predicate p = true;
2134 : 112932161 : ssa_op_iter iter;
2135 : 112932161 : tree use;
2136 : 112932161 : tree param_type = NULL_TREE;
2137 : 112932161 : ipa_predicate op_non_const;
2138 : 112932161 : bool is_load;
2139 : 112932161 : int base_index;
2140 : 112932161 : struct agg_position_info aggpos;
2141 : :
2142 : : /* What statements might be optimized away
2143 : : when their arguments are constant. */
2144 : 112932161 : if (gimple_code (stmt) != GIMPLE_ASSIGN
2145 : 38911524 : && gimple_code (stmt) != GIMPLE_COND
2146 : 27609231 : && gimple_code (stmt) != GIMPLE_SWITCH
2147 : 140459949 : && (gimple_code (stmt) != GIMPLE_CALL
2148 : 20645753 : || !(gimple_call_flags (stmt) & ECF_CONST)))
2149 : 25917232 : return p;
2150 : :
2151 : : /* Stores will stay anyway. */
2152 : 87014929 : if (gimple_store_p (stmt))
2153 : 25442836 : return p;
2154 : :
2155 : 61572093 : is_load = gimple_assign_load_p (stmt);
2156 : :
2157 : : /* Loads can be optimized when the value is known. */
2158 : 61572093 : if (is_load)
2159 : : {
2160 : 17716439 : tree op = gimple_assign_rhs1 (stmt);
2161 : 17716439 : if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type,
2162 : : &aggpos))
2163 : 14494296 : return p;
2164 : : }
2165 : : else
2166 : 43855654 : base_index = -1;
2167 : :
2168 : : /* See if we understand all operands before we start
2169 : : adding conditionals. */
2170 : 63707741 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2171 : : {
2172 : 46755031 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2173 : : /* For arguments we can build a condition. */
2174 : 46755031 : if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0)
2175 : 8423620 : continue;
2176 : 38331411 : 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 : 38331411 : if (nonconstant_names[SSA_NAME_VERSION (use)] != true)
2181 : 8206324 : continue;
2182 : 30125087 : return p;
2183 : : }
2184 : :
2185 : 16952710 : if (is_load)
2186 : 3222062 : op_non_const =
2187 : 3222062 : add_condition (summary, params_summary,
2188 : : base_index, param_type, &aggpos,
2189 : : ipa_predicate::changed, NULL_TREE);
2190 : : else
2191 : 13730648 : op_non_const = false;
2192 : 32549480 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2193 : : {
2194 : 15596770 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2195 : 15596770 : int index;
2196 : :
2197 : 15596770 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2198 : : {
2199 : 7930277 : if (index != base_index)
2200 : 5403533 : p = add_condition (summary, params_summary, index,
2201 : 5403533 : TREE_TYPE (parm), NULL,
2202 : : ipa_predicate::changed, NULL_TREE);
2203 : : else
2204 : 2526744 : continue;
2205 : : }
2206 : : else
2207 : 7666493 : p = nonconstant_names[SSA_NAME_VERSION (use)];
2208 : 13070026 : op_non_const = p.or_with (summary->conds, op_non_const);
2209 : : }
2210 : 16952710 : if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2211 : 14787378 : && gimple_op (stmt, 0)
2212 : 31442951 : && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2213 : 14490241 : nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2214 : 14490241 : = op_non_const;
2215 : 16952710 : 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 : 10385922 : get_minimal_bb (basic_block init_bb, basic_block use_bb)
2235 : : {
2236 : 10385922 : class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father);
2237 : 10385922 : if (l && l->header->count < init_bb->count)
2238 : 372138 : 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 : 4826682 : record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2247 : : {
2248 : 4826682 : struct record_modified_bb_info *info =
2249 : : (struct record_modified_bb_info *) data;
2250 : 4826682 : if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2251 : : return false;
2252 : 4798096 : if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef)))
2253 : : return false;
2254 : 4750721 : bitmap_set_bit (info->bb_set,
2255 : 4750721 : SSA_NAME_IS_DEFAULT_DEF (vdef)
2256 : 0 : ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2257 : : : get_minimal_bb
2258 : 4750721 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2259 : 4750721 : gimple_bb (info->stmt))->index);
2260 : 4750721 : 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 : 38190244 : param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i)
2283 : : {
2284 : 38190244 : tree op = gimple_call_arg (stmt, i);
2285 : 38190244 : basic_block bb = gimple_bb (stmt);
2286 : :
2287 : 38190244 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
2288 : 299 : op = TREE_OPERAND (op, 0);
2289 : :
2290 : 38190244 : tree base = get_base_address (op);
2291 : :
2292 : : /* Global invariants never change. */
2293 : 38190244 : 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 : 19223083 : if (TREE_CODE (base) == SSA_NAME)
2303 : : {
2304 : 16929569 : profile_count init_count;
2305 : :
2306 : 25745813 : if (!bb->count.nonzero_p ())
2307 : : return REG_BR_PROB_BASE;
2308 : :
2309 : 8671413 : if (SSA_NAME_IS_DEFAULT_DEF (base))
2310 : 3036212 : init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2311 : : else
2312 : 5635201 : init_count = get_minimal_bb
2313 : 5635201 : (gimple_bb (SSA_NAME_DEF_STMT (base)),
2314 : : gimple_bb (stmt))->count;
2315 : :
2316 : 8671413 : if (init_count < bb->count)
2317 : 453925 : 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 : 2293514 : ao_ref refd;
2324 : 2293514 : profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2325 : 2293514 : struct record_modified_bb_info info;
2326 : 2293514 : tree init = ctor_for_folding (base);
2327 : :
2328 : 2293514 : if (init != error_mark_node)
2329 : : return 0;
2330 : 5087852 : if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0)
2331 : : return REG_BR_PROB_BASE;
2332 : 1410328 : 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 : 1410328 : ao_ref_init (&refd, op);
2339 : 1410328 : info.op = op;
2340 : 1410328 : info.stmt = stmt;
2341 : 1410328 : info.bb_set = BITMAP_ALLOC (NULL);
2342 : 1410328 : int walked
2343 : 2820656 : = walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2344 : : NULL, NULL, fbi->aa_walk_budget);
2345 : 1410328 : if (walked > 0)
2346 : 1280482 : fbi->aa_walk_budget -= walked;
2347 : 1410328 : if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index))
2348 : : {
2349 : 869050 : if (walked < 0)
2350 : 206 : fbi->aa_walk_budget = 0;
2351 : 869050 : 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 : 869050 : BITMAP_FREE (info.bb_set);
2359 : 869050 : return REG_BR_PROB_BASE;
2360 : : }
2361 : :
2362 : 541278 : bitmap_iterator bi;
2363 : 541278 : 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 : 1526945 : EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2367 : 985667 : max = profile_count::max_prefer_initialized
2368 : 985667 : (max, BASIC_BLOCK_FOR_FN (cfun, index)->count);
2369 : 541278 : 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 : 541278 : BITMAP_FREE (info.bb_set);
2380 : 541278 : if (max < bb->count)
2381 : 91672 : 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 : 6338546 : 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 : 6338546 : edge e;
2400 : 6338546 : edge_iterator ei;
2401 : 6338546 : basic_block first_bb = NULL;
2402 : :
2403 : 6338546 : if (single_pred_p (bb))
2404 : : {
2405 : 20437 : *p = false;
2406 : 20437 : return true;
2407 : : }
2408 : :
2409 : 14566659 : FOR_EACH_EDGE (e, ei, bb->preds)
2410 : : {
2411 : 12442409 : if (single_succ_p (e->src))
2412 : : {
2413 : 13025189 : if (!single_pred_p (e->src))
2414 : : return false;
2415 : 7302283 : if (!first_bb)
2416 : 3179886 : first_bb = single_pred (e->src);
2417 : 4122397 : else if (single_pred (e->src) != first_bb)
2418 : : return false;
2419 : : }
2420 : : else
2421 : : {
2422 : 4149973 : if (!first_bb)
2423 : : first_bb = e->src;
2424 : 1426347 : else if (e->src != first_bb)
2425 : : return false;
2426 : : }
2427 : : }
2428 : :
2429 : 2124250 : if (!first_bb)
2430 : : return false;
2431 : :
2432 : 4248500 : gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb));
2433 : 2077768 : if (!stmt
2434 : 2077768 : || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2435 : 309426 : return false;
2436 : :
2437 : 1814824 : *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary,
2438 : : gimple_cond_lhs (stmt),
2439 : : nonconstant_names);
2440 : 1814824 : 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 : 699173 : predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi,
2453 : : ipa_predicate *p,
2454 : : vec<ipa_predicate> nonconstant_names)
2455 : : {
2456 : 699173 : unsigned i;
2457 : :
2458 : 991461 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2459 : : {
2460 : 872064 : tree arg = gimple_phi_arg (phi, i)->def;
2461 : 872064 : if (!is_gimple_min_invariant (arg))
2462 : : {
2463 : 750747 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
2464 : 1501494 : *p = p->or_with (summary->conds,
2465 : 750747 : nonconstant_names[SSA_NAME_VERSION (arg)]);
2466 : 750747 : if (*p == true)
2467 : : return;
2468 : : }
2469 : : }
2470 : :
2471 : 119397 : 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 : 119397 : 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 : 40905131 : find_foldable_builtin_expect (basic_block bb)
2496 : : {
2497 : 40905131 : gimple_stmt_iterator bsi;
2498 : :
2499 : 280466665 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2500 : : {
2501 : 198817347 : gimple *stmt = gsi_stmt (bsi);
2502 : 198817347 : if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2503 : 198606504 : || gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY)
2504 : 397423786 : || gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT))
2505 : : {
2506 : 339522 : tree var = gimple_call_lhs (stmt);
2507 : 339522 : tree arg = gimple_call_arg (stmt, 0);
2508 : 339522 : use_operand_p use_p;
2509 : 339522 : gimple *use_stmt;
2510 : 339522 : bool match = false;
2511 : 339522 : bool done = false;
2512 : :
2513 : 339522 : if (!var || !arg)
2514 : 3 : continue;
2515 : 339519 : gcc_assert (TREE_CODE (var) == SSA_NAME);
2516 : :
2517 : 677739 : while (TREE_CODE (arg) == SSA_NAME)
2518 : : {
2519 : 677739 : gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg);
2520 : 677739 : if (!is_gimple_assign (stmt_tmp))
2521 : : break;
2522 : 670430 : 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 : 338220 : if (done)
2540 : : break;
2541 : 338220 : arg = gimple_assign_rhs1 (stmt_tmp);
2542 : : }
2543 : :
2544 : 304514 : if (match && single_imm_use (var, &use_p, &use_stmt)
2545 : 643590 : && gimple_code (use_stmt) == GIMPLE_COND)
2546 : 160944 : 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 : 41501916 : clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2563 : : {
2564 : 41501916 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2565 : 41501916 : edge_iterator ei;
2566 : 41501916 : edge e;
2567 : :
2568 : 41501916 : if (need_eh)
2569 : : {
2570 : 41434231 : if (gsi_end_p (gsi))
2571 : : return false;
2572 : 40311198 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2573 : : return false;
2574 : 1029486 : gsi_prev (&gsi);
2575 : : }
2576 : 40973281 : else if (!single_succ_p (bb))
2577 : : return false;
2578 : :
2579 : 4617169 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2580 : : {
2581 : 2763232 : gimple *stmt = gsi_stmt (gsi);
2582 : 2763232 : if (is_gimple_debug (stmt))
2583 : 913660 : continue;
2584 : 1849572 : if (gimple_clobber_p (stmt))
2585 : 879223 : continue;
2586 : 970349 : 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 : 3014022 : FOR_EACH_EDGE (e, ei, bb->preds)
2593 : 2483470 : if (!(e->flags & EDGE_EH)
2594 : 2483470 : && !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 : 93645636 : fp_expression_p (gimple *stmt)
2605 : : {
2606 : 93645636 : ssa_op_iter i;
2607 : 93645636 : tree op;
2608 : :
2609 : 210272029 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE)
2610 : 117221364 : 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 : 58448094 : refs_local_or_readonly_memory_p (tree t)
2620 : : {
2621 : : /* Non-escaping memory is fine. */
2622 : 58448094 : t = get_base_address (t);
2623 : 58448094 : if ((TREE_CODE (t) == MEM_REF
2624 : 58448094 : || TREE_CODE (t) == TARGET_MEM_REF))
2625 : 24696995 : return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2626 : :
2627 : : /* Automatic variables are fine. */
2628 : 33751099 : if (DECL_P (t)
2629 : 33751099 : && auto_var_in_fn_p (t, current_function_decl))
2630 : : return true;
2631 : :
2632 : : /* Read-only variables are fine. */
2633 : 10938277 : 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 : 71500925 : points_to_local_or_readonly_memory_p (tree t)
2644 : : {
2645 : : /* See if memory location is clearly invalid. */
2646 : 71500925 : if (integer_zerop (t))
2647 : 2403484 : return flag_delete_null_pointer_checks;
2648 : 69097441 : 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 : 43759677 : if (DECL_RESULT (current_function_decl)
2658 : 43759677 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
2659 : 44810713 : && t == ssa_default_def (cfun, DECL_RESULT (current_function_decl)))
2660 : : return true;
2661 : 43450132 : return !ptr_deref_may_alias_global_p (t, false);
2662 : : }
2663 : 25337764 : if (TREE_CODE (t) == ADDR_EXPR
2664 : 25337764 : && (TREE_CODE (TREE_OPERAND (t, 0)) != TARGET_MEM_REF
2665 : 4515 : || TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) != INTEGER_CST))
2666 : 13042296 : 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 : 38190244 : points_to_possible_sra_candidate_p (tree t)
2675 : : {
2676 : 38190244 : if (TREE_CODE (t) != ADDR_EXPR)
2677 : : return false;
2678 : :
2679 : 11323142 : t = get_base_address (TREE_OPERAND (t, 0));
2680 : :
2681 : : /* Automatic variables are fine. */
2682 : 11323142 : if (DECL_P (t)
2683 : 11323142 : && 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 : 27130051 : builtin_unreachable_bb_p (basic_block bb, vec<unsigned char> &cache)
2694 : : {
2695 : 27130051 : if (cache[bb->index])
2696 : 2533791 : return cache[bb->index] - 1;
2697 : 24596260 : gimple_stmt_iterator si;
2698 : 24596260 : auto_vec <basic_block, 4> visited_bbs;
2699 : 24596260 : bool ret = false;
2700 : 25396613 : while (true)
2701 : : {
2702 : 25396613 : bool empty_bb = true;
2703 : 25396613 : visited_bbs.safe_push (bb);
2704 : 25396613 : cache[bb->index] = 3;
2705 : 25396613 : for (si = gsi_start_nondebug_bb (bb);
2706 : 26864405 : !gsi_end_p (si) && empty_bb;
2707 : 1467792 : gsi_next_nondebug (&si))
2708 : : {
2709 : 25504742 : if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT
2710 : 25137535 : && !gimple_clobber_p (gsi_stmt (si))
2711 : 49574948 : && !gimple_nop_p (gsi_stmt (si)))
2712 : : {
2713 : : empty_bb = false;
2714 : : break;
2715 : : }
2716 : : }
2717 : 25396613 : if (!empty_bb)
2718 : : break;
2719 : : else
2720 : 1359663 : bb = single_succ_edge (bb)->dest;
2721 : 1359663 : if (cache[bb->index])
2722 : : {
2723 : 559310 : ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1;
2724 : 559310 : goto done;
2725 : : }
2726 : : }
2727 : 24036950 : if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE)
2728 : 24036950 : || gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP))
2729 : : ret = true;
2730 : 24596260 : done:
2731 : 99185393 : for (basic_block vbb:visited_bbs)
2732 : 25396613 : cache[vbb->index] = (unsigned char)ret + 1;
2733 : 24596260 : return ret;
2734 : 24596260 : }
2735 : :
2736 : : static bool
2737 : 13688403 : guards_builtin_unreachable (basic_block bb, vec<unsigned char> &cache)
2738 : : {
2739 : 13688403 : edge_iterator ei;
2740 : 13688403 : edge e;
2741 : 40555166 : FOR_EACH_EDGE (e, ei, bb->succs)
2742 : 27130051 : if (builtin_unreachable_bb_p (e->dest, cache))
2743 : : {
2744 : 263288 : 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 : 263288 : 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 : 170567817 : mark_stmt_necessary (gimple *stmt, auto_vec<gimple *> &worklist)
2760 : : {
2761 : 170567817 : gcc_assert (stmt);
2762 : :
2763 : 170567817 : if (gimple_plf (stmt, STMT_NECESSARY))
2764 : : return;
2765 : :
2766 : 141495820 : 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 : 141495820 : gimple_set_plf (stmt, STMT_NECESSARY, true);
2774 : 141495820 : worklist.safe_push (stmt);
2775 : : }
2776 : :
2777 : : /* Mark the statement defining operand OP as necessary. */
2778 : :
2779 : : static inline void
2780 : 120252089 : mark_operand_necessary (tree op, auto_vec<gimple *> &worklist)
2781 : : {
2782 : 120252089 : gimple *stmt = SSA_NAME_DEF_STMT (op);
2783 : 120252089 : if (gimple_nop_p (stmt))
2784 : : return;
2785 : 97473614 : 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 : 7191755 : find_necessary_statements (struct cgraph_node *node)
2794 : : {
2795 : 7191755 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2796 : 7191755 : auto_vec<unsigned char, 10> cache;
2797 : 7191755 : basic_block bb;
2798 : 7191755 : auto_vec<gimple *> worklist;
2799 : :
2800 : 7191755 : cache.safe_grow_cleared (last_basic_block_for_fn (cfun));
2801 : : /* Mark all obviously necessary statements. */
2802 : 48625986 : FOR_EACH_BB_FN (bb, my_function)
2803 : : {
2804 : 41434231 : for (gimple_stmt_iterator gsi = gsi_start_phis (bb);
2805 : 53009786 : !gsi_end_p (gsi); gsi_next (&gsi))
2806 : 11575555 : gimple_set_plf (gsi_stmt (gsi), STMT_NECESSARY, false);
2807 : :
2808 : 231218306 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2809 : 148349844 : gsi_next_nondebug (&bsi))
2810 : : {
2811 : 148349844 : gimple *stmt = gsi_stmt (bsi);
2812 : :
2813 : 148349844 : gimple_set_plf (stmt, STMT_NECESSARY, false);
2814 : 148349844 : if (gimple_has_side_effects (stmt)
2815 : 121491099 : || (is_ctrl_stmt (stmt)
2816 : 21904787 : && (gimple_code (stmt) != GIMPLE_COND
2817 : 13688403 : || !guards_builtin_unreachable (bb, cache)))
2818 : 99849600 : || gimple_store_p (stmt)
2819 : 223627145 : || gimple_code (stmt) == GIMPLE_ASM)
2820 : 73094203 : mark_stmt_necessary (stmt, worklist);
2821 : : }
2822 : : }
2823 : 148687575 : while (worklist.length () > 0)
2824 : : {
2825 : 141495820 : gimple *stmt = worklist.pop ();
2826 : :
2827 : 141495820 : 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 : 141495820 : if (gimple_code (stmt) == GIMPLE_PHI)
2834 : 18304320 : for (unsigned int k = 0; k < gimple_phi_num_args (stmt); k++)
2835 : : {
2836 : 12962398 : tree arg = PHI_ARG_DEF (stmt, k);
2837 : :
2838 : 12962398 : if (TREE_CODE (arg) == SSA_NAME)
2839 : 9956558 : mark_operand_necessary (arg, worklist);
2840 : : }
2841 : : else
2842 : : {
2843 : 136153898 : ssa_op_iter iter;
2844 : 136153898 : tree use;
2845 : :
2846 : 246449429 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2847 : 110295531 : mark_operand_necessary (use, worklist);
2848 : : }
2849 : : }
2850 : 7191755 : }
2851 : :
2852 : : /* Analyze function body for NODE.
2853 : : EARLY indicates run from early optimization pipeline. */
2854 : :
2855 : : static void
2856 : 7191755 : analyze_function_body (struct cgraph_node *node, bool early)
2857 : : {
2858 : 7191755 : sreal time = opt_for_fn (node->decl, param_uninlined_function_time);
2859 : : /* Estimate static overhead for function prologue/epilogue and alignment. */
2860 : 7191755 : 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 : 7191755 : basic_block bb;
2864 : 7191755 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2865 : 7191755 : sreal freq;
2866 : 7191755 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
2867 : 7191755 : ipa_node_params *params_summary
2868 : 7191755 : = early ? NULL : ipa_node_params_sum->get (node);
2869 : 7191755 : ipa_predicate bb_predicate;
2870 : 7191755 : struct ipa_func_body_info fbi;
2871 : 7191755 : vec<ipa_predicate> nonconstant_names = vNULL;
2872 : 7191755 : int nblocks, n;
2873 : 7191755 : int *order;
2874 : 7191755 : gimple *fix_builtin_expect_stmt;
2875 : :
2876 : 7191755 : gcc_assert (my_function && my_function->cfg);
2877 : 7191755 : gcc_assert (cfun == my_function);
2878 : :
2879 : 7191755 : memset(&fbi, 0, sizeof(fbi));
2880 : 7191755 : vec_free (info->conds);
2881 : 7191755 : info->conds = NULL;
2882 : 7191755 : info->size_time_table.release ();
2883 : 7191755 : 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 : 7191755 : if (opt_for_fn (node->decl, optimize))
2892 : : {
2893 : 6281644 : calculate_dominance_info (CDI_DOMINATORS);
2894 : 6281644 : calculate_dominance_info (CDI_POST_DOMINATORS);
2895 : 6281644 : if (!early)
2896 : 1376919 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2897 : : else
2898 : : {
2899 : 4904725 : ipa_check_create_node_params ();
2900 : 4904725 : ipa_initialize_node_params (node);
2901 : : }
2902 : :
2903 : 6281644 : if (ipa_node_params_sum)
2904 : : {
2905 : 6281644 : fbi.node = node;
2906 : 6281644 : fbi.info = ipa_node_params_sum->get (node);
2907 : 6281644 : fbi.bb_infos = vNULL;
2908 : 6281644 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
2909 : 6281644 : fbi.param_count = count_formal_params (node->decl);
2910 : 6281644 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
2911 : :
2912 : 6281644 : nonconstant_names.safe_grow_cleared
2913 : 6281644 : (SSANAMES (my_function)->length (), true);
2914 : : }
2915 : : }
2916 : :
2917 : 7191755 : 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 : 7191755 : bb_predicate = true;
2924 : 7191755 : info->account_size_time (0, 0, bb_predicate, bb_predicate);
2925 : :
2926 : 7191755 : bb_predicate = ipa_predicate::not_inlined ();
2927 : 7191755 : info->account_size_time (opt_for_fn (node->decl,
2928 : : param_uninlined_function_insns)
2929 : : * ipa_fn_summary::size_scale,
2930 : 7191755 : 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 : 7191755 : bool scan_for_target_info =
2937 : 7191755 : info->inlinable
2938 : 12891052 : && targetm.target_option.need_ipa_fn_target_info (node->decl,
2939 : 5699297 : info->target_info);
2940 : :
2941 : 7191755 : if (fbi.info)
2942 : 6281644 : compute_bb_predicates (&fbi, node, info, params_summary);
2943 : 7191755 : find_necessary_statements (node);
2944 : 7191755 : const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2945 : 7191755 : order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2946 : 7191755 : nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2947 : 48625986 : for (n = 0; n < nblocks; n++)
2948 : : {
2949 : 41434231 : bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2950 : 41434231 : freq = bb->count.to_sreal_scale (entry_count);
2951 : 41434231 : if (clobber_only_eh_bb_p (bb))
2952 : : {
2953 : 529100 : 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 : 529100 : continue;
2958 : : }
2959 : :
2960 : : /* TODO: Obviously predicates can be propagated down across CFG. */
2961 : 40905131 : if (fbi.info)
2962 : : {
2963 : 34470085 : if (bb->aux)
2964 : 34470071 : bb_predicate = *(ipa_predicate *)bb->aux;
2965 : : else
2966 : 14 : bb_predicate = false;
2967 : : }
2968 : : else
2969 : 6435046 : bb_predicate = true;
2970 : :
2971 : 40905131 : 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 : 40905131 : if (fbi.info && nonconstant_names.exists ())
2978 : : {
2979 : 34470085 : ipa_predicate phi_predicate;
2980 : 34470085 : bool first_phi = true;
2981 : :
2982 : 35169258 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2983 : 699173 : gsi_next (&bsi))
2984 : : {
2985 : 6422079 : if (first_phi
2986 : 6422079 : && !phi_result_unknown_predicate (&fbi, info,
2987 : : params_summary,
2988 : : bb,
2989 : : &phi_predicate,
2990 : : nonconstant_names))
2991 : : break;
2992 : 699173 : first_phi = false;
2993 : 699173 : 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 : 699173 : predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2999 : : nonconstant_names);
3000 : : }
3001 : : }
3002 : :
3003 : 40905131 : fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
3004 : :
3005 : 40905131 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
3006 : 181342964 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
3007 : : {
3008 : 140437833 : gimple *stmt = gsi_stmt (bsi);
3009 : 140437833 : if (!gimple_plf (stmt, STMT_NECESSARY))
3010 : : {
3011 : 5552008 : 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 : 5552008 : if (!is_gimple_call (stmt)
3021 : 5552008 : || gimple_call_internal_p (stmt))
3022 : 5272715 : continue;
3023 : : }
3024 : 135165118 : int this_size = estimate_num_insns (stmt, &eni_size_weights);
3025 : 135165118 : int this_time = estimate_num_insns (stmt, &eni_time_weights);
3026 : 135165118 : int prob;
3027 : 135165118 : 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 : 135165118 : if (stmt == fix_builtin_expect_stmt)
3032 : : {
3033 : 160506 : this_size--;
3034 : 160506 : this_time--;
3035 : : }
3036 : :
3037 : 135165118 : 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 : 135165118 : if (is_gimple_call (stmt)
3047 : 135165118 : && !gimple_call_internal_p (stmt))
3048 : : {
3049 : 22791394 : struct cgraph_edge *edge = node->get_edge (stmt);
3050 : 22791394 : 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 : 22791394 : if (nonconstant_names.exists ()
3056 : 19889042 : && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
3057 : 8713 : && gimple_call_lhs (stmt)
3058 : 22800107 : && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3059 : : {
3060 : 8713 : ipa_predicate false_p = false;
3061 : 8713 : nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
3062 : 8713 : = false_p;
3063 : : }
3064 : 22791394 : if (ipa_node_params_sum)
3065 : : {
3066 : 19901813 : int count = gimple_call_num_args (stmt);
3067 : 19901813 : int i;
3068 : :
3069 : 19901813 : if (count)
3070 : 17002321 : es->param.safe_grow_cleared (count, true);
3071 : 58092057 : for (i = 0; i < count; i++)
3072 : : {
3073 : 38190244 : int prob = param_change_prob (&fbi, stmt, i);
3074 : 38190244 : gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
3075 : 38190244 : es->param[i].change_prob = prob;
3076 : 38190244 : es->param[i].points_to_local_or_readonly_memory
3077 : 38190244 : = points_to_local_or_readonly_memory_p
3078 : 38190244 : (gimple_call_arg (stmt, i));
3079 : 38190244 : es->param[i].points_to_possible_sra_candidate
3080 : 38190244 : = points_to_possible_sra_candidate_p
3081 : 38190244 : (gimple_call_arg (stmt, i));
3082 : : }
3083 : : }
3084 : : /* We cannot setup VLA parameters during inlining. */
3085 : 66878470 : for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i)
3086 : 44087472 : 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 : 22791394 : es->call_stmt_size = this_size;
3092 : 22791394 : es->call_stmt_time = this_time;
3093 : 22791394 : es->loop_depth = bb_loop_depth (bb);
3094 : 22791394 : edge_set_predicate (edge, &bb_predicate);
3095 : 22791394 : 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 : 22791394 : if (edge->has_callback)
3122 : : {
3123 : 15067 : cgraph_edge *cbe;
3124 : 30134 : for (cbe = edge->first_callback_edge (); cbe;
3125 : 15067 : cbe = cbe->next_callback_edge ())
3126 : : {
3127 : 15067 : ipa_call_summary *es2 = ipa_call_summaries->get_create (cbe);
3128 : 15067 : 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 : 15067 : es2->call_stmt_size = 0;
3133 : 15067 : 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 : 135165118 : if (fbi.info)
3142 : 112932161 : will_be_nonconstant
3143 : 112932161 : = will_be_nonconstant_predicate (&fbi, info, params_summary,
3144 : : stmt, nonconstant_names);
3145 : : else
3146 : 22232957 : will_be_nonconstant = true;
3147 : 135165118 : if (this_time || this_size)
3148 : : {
3149 : 110753026 : sreal final_time = (sreal)this_time * freq;
3150 : 110753026 : prob = eliminated_by_inlining_prob (&fbi, stmt);
3151 : 110753026 : if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
3152 : 11 : fprintf (dump_file,
3153 : : "\t\t50%% will be eliminated by inlining\n");
3154 : 110753026 : if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
3155 : 18 : fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
3156 : :
3157 : 110753026 : ipa_predicate p = bb_predicate & will_be_nonconstant;
3158 : 110753026 : int parm = load_or_store_of_ptr_parameter (&fbi, stmt);
3159 : 110753026 : ipa_predicate sra_predicate = true;
3160 : 110753026 : if (parm != -1)
3161 : 14072118 : sra_predicate &= add_condition (info, params_summary, parm,
3162 : : ptr_type_node, NULL,
3163 : 7036059 : 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 : 221506052 : if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false)
3170 : : {
3171 : 110043406 : time += final_time;
3172 : 110043406 : 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 : 110753026 : if (!is_gimple_call (stmt))
3179 : : {
3180 : 88613823 : if (prob)
3181 : : {
3182 : 14822513 : ipa_predicate ip
3183 : 14822513 : = bb_predicate & ipa_predicate::not_inlined () & sra_predicate;
3184 : 29645026 : info->account_size_time (this_size * prob,
3185 : 14822513 : (final_time * prob) / 2, ip,
3186 : : p);
3187 : : }
3188 : 14822513 : if (prob != 2)
3189 : 163520532 : info->account_size_time (this_size * (2 - prob),
3190 : 81760266 : (final_time * (2 - prob) / 2),
3191 : 163520532 : bb_predicate & sra_predicate,
3192 : : p);
3193 : : }
3194 : :
3195 : 110753026 : if (!info->fp_expressions && fp_expression_p (stmt))
3196 : : {
3197 : 594971 : info->fp_expressions = true;
3198 : 594971 : 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 : 135165118 : 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 : 522670041 : for (unsigned int i = 0; i < gimple_num_ops (stmt); i++)
3214 : : {
3215 : 387504923 : for (tree op = gimple_op (stmt, i);
3216 : 745516865 : op && handled_component_p (op);
3217 : 46387544 : op = TREE_OPERAND (op, 0))
3218 : 46387544 : if ((TREE_CODE (op) == ARRAY_REF
3219 : 46387544 : || TREE_CODE (op) == ARRAY_RANGE_REF)
3220 : 46387544 : && TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
3221 : : {
3222 : 2342235 : ipa_predicate p = bb_predicate;
3223 : 2342235 : if (fbi.info)
3224 : 1828113 : p = p & will_be_nonconstant_expr_predicate
3225 : 1828113 : (&fbi, info, params_summary,
3226 : 1828113 : TREE_OPERAND (op, 1),
3227 : 1828113 : nonconstant_names);
3228 : 2342235 : if (p != false)
3229 : : {
3230 : 2336729 : time += freq;
3231 : 2336729 : size += 1;
3232 : 2336729 : if (dump_file)
3233 : 24 : fprintf (dump_file,
3234 : : "\t\tAccounting address calculation.\n");
3235 : 2336729 : info->account_size_time (ipa_fn_summary::size_scale,
3236 : : freq,
3237 : : bb_predicate,
3238 : : p);
3239 : : }
3240 : : }
3241 : : }
3242 : :
3243 : : }
3244 : : }
3245 : 7191755 : free (order);
3246 : :
3247 : 7191755 : if (nonconstant_names.exists () && !early)
3248 : : {
3249 : 1376919 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3250 : 1376919 : unsigned max_loop_predicates = opt_for_fn (node->decl,
3251 : : param_ipa_max_loop_predicates);
3252 : :
3253 : 1376919 : if (dump_file && (dump_flags & TDF_DETAILS))
3254 : 14 : flow_loops_dump (dump_file, NULL, 0);
3255 : 1376919 : scev_initialize ();
3256 : 4748605 : for (auto loop : loops_list (cfun, 0))
3257 : : {
3258 : 617848 : ipa_predicate loop_iterations = true;
3259 : 617848 : sreal header_freq;
3260 : 617848 : edge ex;
3261 : 617848 : unsigned int j;
3262 : 617848 : class tree_niter_desc niter_desc;
3263 : 617848 : if (!loop->header->aux)
3264 : 0 : continue;
3265 : :
3266 : 617848 : profile_count hdr_count = loop->header->count;
3267 : 617848 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3268 : :
3269 : 617848 : bb_predicate = *(ipa_predicate *)loop->header->aux;
3270 : 617848 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3271 : 1617137 : FOR_EACH_VEC_ELT (exits, j, ex)
3272 : 999289 : if (number_of_iterations_exit (loop, ex, &niter_desc, false)
3273 : 999289 : && !is_gimple_min_invariant (niter_desc.niter))
3274 : : {
3275 : 152846 : ipa_predicate will_be_nonconstant
3276 : 152846 : = will_be_nonconstant_expr_predicate (&fbi, info,
3277 : : params_summary,
3278 : : niter_desc.niter,
3279 : : nonconstant_names);
3280 : 152846 : if (will_be_nonconstant != true)
3281 : 61738 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3282 : 152846 : if (will_be_nonconstant != true
3283 : 214584 : && will_be_nonconstant != false)
3284 : 61190 : loop_iterations &= will_be_nonconstant;
3285 : : }
3286 : 617848 : add_freqcounting_predicate (&s->loop_iterations, loop_iterations,
3287 : : hdr_freq, max_loop_predicates);
3288 : 617848 : }
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 : 1376919 : for (class loop *loop = loops_for_fn (cfun)->tree_root->inner;
3294 : 1862832 : loop != NULL; loop = loop->next)
3295 : : {
3296 : 485913 : ipa_predicate loop_stride = true;
3297 : 485913 : basic_block *body = get_loop_body (loop);
3298 : 485913 : profile_count hdr_count = loop->header->count;
3299 : 485913 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3300 : 2863238 : for (unsigned i = 0; i < loop->num_nodes; i++)
3301 : : {
3302 : 2377325 : gimple_stmt_iterator gsi;
3303 : 2377325 : if (!body[i]->aux)
3304 : 7 : continue;
3305 : :
3306 : 2377318 : bb_predicate = *(ipa_predicate *)body[i]->aux;
3307 : 15697258 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
3308 : 10942622 : gsi_next (&gsi))
3309 : : {
3310 : 10942622 : gimple *stmt = gsi_stmt (gsi);
3311 : :
3312 : 10942622 : if (!is_gimple_assign (stmt))
3313 : 10791527 : continue;
3314 : :
3315 : 5325355 : tree def = gimple_assign_lhs (stmt);
3316 : 5325355 : if (TREE_CODE (def) != SSA_NAME)
3317 : 1015026 : continue;
3318 : :
3319 : 4310329 : affine_iv iv;
3320 : 8620658 : if (!simple_iv (loop_containing_stmt (stmt),
3321 : : loop_containing_stmt (stmt),
3322 : : def, &iv, true)
3323 : 4310329 : || is_gimple_min_invariant (iv.step))
3324 : 4159234 : continue;
3325 : :
3326 : 151095 : ipa_predicate will_be_nonconstant
3327 : 151095 : = will_be_nonconstant_expr_predicate (&fbi, info,
3328 : : params_summary,
3329 : : iv.step,
3330 : : nonconstant_names);
3331 : 151095 : if (will_be_nonconstant != true)
3332 : 53257 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3333 : 151095 : if (will_be_nonconstant != true
3334 : 204352 : && will_be_nonconstant != false)
3335 : 34385 : loop_stride = loop_stride & will_be_nonconstant;
3336 : : }
3337 : : }
3338 : 485913 : add_freqcounting_predicate (&s->loop_strides, loop_stride,
3339 : : hdr_freq, max_loop_predicates);
3340 : 485913 : free (body);
3341 : : }
3342 : 1376919 : scev_finalize ();
3343 : : }
3344 : 63009496 : FOR_ALL_BB_FN (bb, my_function)
3345 : : {
3346 : 55817741 : edge e;
3347 : 55817741 : edge_iterator ei;
3348 : :
3349 : 55817741 : if (bb->aux)
3350 : 41217854 : edge_predicate_pool.remove ((ipa_predicate *)bb->aux);
3351 : 55817741 : bb->aux = NULL;
3352 : 118761650 : FOR_EACH_EDGE (e, ei, bb->succs)
3353 : : {
3354 : 62943909 : if (e->aux)
3355 : 2501419 : edge_predicate_pool.remove ((ipa_predicate *)e->aux);
3356 : 62943909 : e->aux = NULL;
3357 : : }
3358 : : }
3359 : 7191755 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3360 : 7191755 : ipa_size_summary *ss = ipa_size_summaries->get (node);
3361 : 7191755 : s->time = time;
3362 : 7191755 : ss->self_size = size;
3363 : 7191755 : nonconstant_names.release ();
3364 : 7191755 : ipa_release_body_info (&fbi);
3365 : 7191755 : if (opt_for_fn (node->decl, optimize))
3366 : : {
3367 : 6281644 : if (!early)
3368 : 1376919 : loop_optimizer_finalize ();
3369 : 4904725 : else if (!ipa_edge_args_sum)
3370 : 4904711 : ipa_free_all_node_params ();
3371 : 6281644 : free_dominance_info (CDI_DOMINATORS);
3372 : 6281644 : free_dominance_info (CDI_POST_DOMINATORS);
3373 : : }
3374 : 7191755 : if (dump_file)
3375 : : {
3376 : 247 : fprintf (dump_file, "\n");
3377 : 247 : ipa_dump_fn_summary (dump_file, node);
3378 : : }
3379 : 7191755 : }
3380 : :
3381 : :
3382 : : /* Compute function summary.
3383 : : EARLY is true when we compute parameters during early opts. */
3384 : :
3385 : : void
3386 : 7193040 : compute_fn_summary (struct cgraph_node *node, bool early)
3387 : : {
3388 : 7193040 : HOST_WIDE_INT self_stack_size;
3389 : 7193040 : struct cgraph_edge *e;
3390 : :
3391 : 7193040 : gcc_assert (!node->inlined_to);
3392 : :
3393 : 7193040 : if (!ipa_fn_summaries)
3394 : 213660 : ipa_fn_summary_alloc ();
3395 : :
3396 : : /* Create a new ipa_fn_summary. */
3397 : 7193040 : ((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node);
3398 : 7193040 : ipa_fn_summaries->remove (node);
3399 : 7193040 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
3400 : 7193040 : 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 : 12564557 : self_stack_size = optimize && !node->thunk
3404 : 13474684 : ? estimated_stack_frame_size (node) : 0;
3405 : 7193040 : size_info->estimated_self_stack_size = self_stack_size;
3406 : 7193040 : info->estimated_stack_size = self_stack_size;
3407 : :
3408 : 7193040 : if (node->thunk)
3409 : : {
3410 : 1285 : ipa_call_summary *es = ipa_call_summaries->get_create (node->callees);
3411 : 1285 : ipa_predicate t = true;
3412 : :
3413 : 1285 : node->can_change_signature = false;
3414 : 1285 : es->call_stmt_size = eni_size_weights.call_cost;
3415 : 1285 : es->call_stmt_time = eni_time_weights.call_cost;
3416 : 3855 : info->account_size_time (ipa_fn_summary::size_scale
3417 : 1285 : * opt_for_fn (node->decl,
3418 : : param_uninlined_function_thunk_insns),
3419 : 1285 : opt_for_fn (node->decl,
3420 : : param_uninlined_function_thunk_time), t, t);
3421 : 1285 : t = ipa_predicate::not_inlined ();
3422 : 1285 : info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t);
3423 : 1285 : ipa_update_overall_fn_summary (node);
3424 : 1285 : size_info->self_size = size_info->size;
3425 : 1285 : 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 : 1276 : info->inlinable = true;
3432 : : }
3433 : : else
3434 : : {
3435 : : /* Even is_gimple_min_invariant rely on current_function_decl. */
3436 : 7191755 : 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 : 7191755 : update_ssa (TODO_update_ssa_only_virtuals);
3441 : :
3442 : : /* Can this function be inlined at all? */
3443 : 7191755 : if (!opt_for_fn (node->decl, optimize)
3444 : 8101866 : && !lookup_attribute ("always_inline",
3445 : 910111 : DECL_ATTRIBUTES (node->decl)))
3446 : 842827 : info->inlinable = false;
3447 : : else
3448 : 6348928 : info->inlinable = tree_inlinable_function_p (node->decl);
3449 : :
3450 : 7191755 : 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 : 7191755 : if (cfun->has_musttail)
3459 : : {
3460 : 1406 : 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 : 7191755 : for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl));
3470 : 7577171 : list && !no_signature; list = TREE_CHAIN (list))
3471 : 385416 : if (!ipa_param_adjustments::type_attribute_allowed_p
3472 : 385416 : (get_attribute_name (list)))
3473 : : {
3474 : 157386 : 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 : 7191755 : for (tree parm = DECL_ARGUMENTS (node->decl);
3483 : 21491721 : parm && !no_signature; parm = DECL_CHAIN (parm))
3484 : 14299966 : if (variably_modified_type_p (TREE_TYPE (parm), node->decl))
3485 : : {
3486 : 15085 : 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 : 7191755 : if (no_signature
3497 : 14209633 : || lookup_attribute ("omp declare simd",
3498 : 7017878 : DECL_ATTRIBUTES (node->decl)))
3499 : 175565 : node->can_change_signature = false;
3500 : : else
3501 : : {
3502 : : /* Otherwise, inlinable functions always can change signature. */
3503 : 7016190 : if (info->inlinable)
3504 : 5620798 : node->can_change_signature = true;
3505 : : else
3506 : : {
3507 : : /* Functions calling builtin_apply cannot change signature. */
3508 : 5452592 : for (e = node->callees; e; e = e->next_callee)
3509 : : {
3510 : 4089728 : tree cdecl = e->callee->decl;
3511 : 4089728 : if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS,
3512 : : BUILT_IN_VA_START))
3513 : : break;
3514 : : }
3515 : 1395392 : node->can_change_signature = !e;
3516 : : }
3517 : : }
3518 : 7191755 : analyze_function_body (node, early);
3519 : 7191755 : pop_cfun ();
3520 : : }
3521 : :
3522 : : /* Inlining characteristics are maintained by the cgraph_mark_inline. */
3523 : 7193040 : size_info->size = size_info->self_size;
3524 : 7193040 : 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 : 7193040 : ipa_update_overall_fn_summary (node);
3532 : 7193040 : if (flag_checking)
3533 : : {
3534 : 7673752 : for (e = node->indirect_calls; e; e = e->next_callee)
3535 : 480794 : if (e->speculative)
3536 : : break;
3537 : 7192958 : gcc_assert (e || size_info->size == size_info->self_size);
3538 : : }
3539 : 7193040 : }
3540 : :
3541 : :
3542 : : /* Compute parameters of functions used by inliner using
3543 : : current_function_decl. */
3544 : :
3545 : : static unsigned int
3546 : 5758407 : compute_fn_summary_for_current (void)
3547 : : {
3548 : 5758407 : compute_fn_summary (cgraph_node::get (current_function_decl), true);
3549 : 5758407 : 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 : 4791552 : estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3558 : : int *size, int *time,
3559 : : ipa_call_arg_values *avals)
3560 : : {
3561 : 4791552 : tree target;
3562 : 4791552 : struct cgraph_node *callee;
3563 : 4791552 : class ipa_fn_summary *isummary;
3564 : 4791552 : enum availability avail;
3565 : 4791552 : bool speculative;
3566 : :
3567 : 4791552 : if (!avals
3568 : 5986960 : || (!avals->m_known_vals.length() && !avals->m_known_contexts.length ()))
3569 : : return false;
3570 : 1402913 : if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3571 : : return false;
3572 : :
3573 : 1397062 : target = ipa_get_indirect_edge_target
3574 : 1397062 : (ie->callee ? ie->speculative_call_indirect_edge () : ie,
3575 : : avals, &speculative);
3576 : 1397062 : 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 : 228145 : if (ie->callee)
3582 : : {
3583 : 7864 : gcc_checking_assert (ie->speculative && *size > 0);
3584 : 7864 : *size = 0;
3585 : 7864 : *time = 0;
3586 : : }
3587 : : else
3588 : : {
3589 : : /* Account for difference in cost between indirect and direct calls. */
3590 : 220281 : *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3591 : 220281 : *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3592 : : }
3593 : 228145 : gcc_checking_assert (*time >= 0);
3594 : 228145 : gcc_checking_assert (*size >= 0);
3595 : :
3596 : 228145 : callee = cgraph_node::get (target);
3597 : 228145 : if (!callee || !callee->definition)
3598 : : return false;
3599 : 207543 : callee = callee->function_symbol (&avail);
3600 : 207543 : if (avail < AVAIL_AVAILABLE)
3601 : : return false;
3602 : 207517 : isummary = ipa_fn_summaries->get (callee);
3603 : 207517 : if (isummary == NULL)
3604 : : return false;
3605 : :
3606 : 207505 : 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 : 207898811 : 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 : 207898811 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3620 : 207898811 : int call_size = es->call_stmt_size;
3621 : 207898811 : int call_time = es->call_stmt_time;
3622 : 207898811 : int cur_size;
3623 : :
3624 : 203729545 : if ((!e->callee || e->speculative)
3625 : 208521097 : && estimate_edge_devirt_benefit (e, &call_size, &call_time, avals))
3626 : : {
3627 : 207163 : if (hints && e->maybe_hot_p ())
3628 : 197122 : *hints |= INLINE_HINT_indirect_call;
3629 : : }
3630 : 207898811 : cur_size = call_size * ipa_fn_summary::size_scale;
3631 : 207898811 : *size += cur_size;
3632 : 207898811 : if (min_size)
3633 : 27283981 : *min_size += cur_size;
3634 : 207898811 : if (time)
3635 : 198442689 : *time += ((sreal)call_time) * e->sreal_frequency ();
3636 : 207898811 : }
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 : 66732500 : 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 : 66732500 : struct cgraph_edge *e;
3654 : 334659159 : for (e = node->callees; e; e = e->next_callee)
3655 : : {
3656 : 267926659 : if (!e->inline_failed)
3657 : : {
3658 : 46802228 : gcc_checking_assert (!ipa_call_summaries->get (e));
3659 : 46802228 : estimate_calls_size_and_time_1 (e->callee, size, min_size, time,
3660 : : hints, possible_truths, avals);
3661 : :
3662 : 46802228 : continue;
3663 : : }
3664 : 221124431 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3665 : :
3666 : : /* Do not care about zero sized builtins. */
3667 : 221124431 : if (!es->call_stmt_size)
3668 : : {
3669 : 25524616 : gcc_checking_assert (!es->call_stmt_time);
3670 : 25524616 : continue;
3671 : : }
3672 : 195599815 : if (!es->predicate
3673 : 195599815 : || 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 : 192890320 : estimate_edge_size_and_time (e, size,
3678 : 192890320 : es->predicate ? NULL : min_size,
3679 : : time, avals, hints);
3680 : : }
3681 : : }
3682 : 70600160 : for (e = node->indirect_calls; e; e = e->next_callee)
3683 : : {
3684 : 3867660 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3685 : 3867660 : if (!es->predicate
3686 : 3867660 : || es->predicate->evaluate (possible_truths))
3687 : 3835866 : estimate_edge_size_and_time (e, size,
3688 : 3835866 : es->predicate ? NULL : min_size,
3689 : : time, avals, hints);
3690 : : }
3691 : 66732500 : }
3692 : :
3693 : : /* Populate sum->call_size_time_table for edges from NODE. */
3694 : :
3695 : : static void
3696 : 3011636 : summarize_calls_size_and_time (struct cgraph_node *node,
3697 : : ipa_fn_summary *sum)
3698 : : {
3699 : 3011636 : struct cgraph_edge *e;
3700 : 14309864 : for (e = node->callees; e; e = e->next_callee)
3701 : : {
3702 : 11298228 : if (!e->inline_failed)
3703 : : {
3704 : 1300580 : gcc_checking_assert (!ipa_call_summaries->get (e));
3705 : 1300580 : summarize_calls_size_and_time (e->callee, sum);
3706 : 1300580 : continue;
3707 : : }
3708 : 9997648 : int size = 0;
3709 : 9997648 : sreal time = 0;
3710 : :
3711 : 9997648 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3712 : :
3713 : 9997648 : ipa_predicate pred = true;
3714 : 9997648 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3715 : :
3716 : 9997648 : if (es->predicate)
3717 : 1887887 : pred = *es->predicate;
3718 : 9997648 : sum->account_size_time (size, time, pred, pred, true);
3719 : : }
3720 : 3345036 : for (e = node->indirect_calls; e; e = e->next_callee)
3721 : : {
3722 : 333400 : int size = 0;
3723 : 333400 : sreal time = 0;
3724 : :
3725 : 333400 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3726 : 333400 : ipa_predicate pred = true;
3727 : 333400 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3728 : :
3729 : 333400 : if (es->predicate)
3730 : 106418 : pred = *es->predicate;
3731 : 333400 : sum->account_size_time (size, time, pred, pred, true);
3732 : : }
3733 : 3011636 : }
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 : 19930293 : 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 : 19930293 : class ipa_fn_summary *sum = ipa_fn_summaries->get (node);
3747 : 19930293 : bool use_table = true;
3748 : :
3749 : 19930293 : 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 : 19930293 : if (!ipa_node_params_sum)
3755 : : use_table = false;
3756 : : /* Do not calculate summaries for simple wrappers; it is waste
3757 : : of memory. */
3758 : 10839710 : else if (node->callees && !node->indirect_calls
3759 : 10099070 : && 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 : 8731517 : else if (avals
3764 : 8731517 : && (avals->m_known_vals.length ()
3765 : 3169950 : || avals->m_known_contexts.length ()
3766 : 3034452 : || avals->m_known_aggs.length ()))
3767 : : {
3768 : 4168702 : ipa_node_params *params_summary = ipa_node_params_sum->get (node);
3769 : 4168702 : unsigned int nargs = params_summary
3770 : 4168702 : ? ipa_get_param_count (params_summary) : 0;
3771 : :
3772 : 14024950 : for (unsigned int i = 0; i < nargs && use_table; i++)
3773 : : {
3774 : 9856248 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3775 : 9856248 : && (avals->safe_sval_at (i)
3776 : 254298 : || (ipa_argagg_value_list (avals).value_for_index_p (i))))
3777 : : use_table = false;
3778 : 9757297 : else if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3779 : 9757297 : && (avals->m_known_contexts.length () > i
3780 : 10003481 : && !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 : 4168702 : if (use_table)
3787 : : {
3788 : : /* Build summary if it is absent. */
3789 : 8485552 : if (!sum->call_size_time_table.length ())
3790 : : {
3791 : 869479 : ipa_predicate true_pred = true;
3792 : 869479 : sum->account_size_time (0, 0, true_pred, true_pred, true);
3793 : 869479 : summarize_calls_size_and_time (node, sum);
3794 : : }
3795 : :
3796 : 8485552 : int old_size = *size;
3797 : 8485552 : sreal old_time = time ? *time : 0;
3798 : :
3799 : 8485552 : if (min_size)
3800 : 8485552 : *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 : 22911383 : for (i = 0; sum->call_size_time_table.iterate (i, &e);
3807 : : i++)
3808 : 14425831 : if (e->exec_predicate.evaluate (possible_truths))
3809 : : {
3810 : 13233297 : *size += e->size;
3811 : 13233297 : if (time)
3812 : 11124408 : *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 : 8485552 : && sum->call_size_time_table.length ()
3820 : : < ipa_fn_summary::max_size_time_table_size)
3821 : : {
3822 : 8485531 : estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL,
3823 : : possible_truths, avals);
3824 : 8485531 : gcc_assert (*size == old_size);
3825 : 15591098 : if (time && (*time - old_time > 1 || *time - old_time < -1)
3826 : 8485560 : && 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 : 11444741 : estimate_calls_size_and_time_1 (node, size, min_size, time, hints,
3835 : : possible_truths, avals);
3836 : 19930293 : }
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 : 19189544 : 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 : 19189544 : ipa_auto_call_arg_values *arg_values)
3847 : 19189544 : : m_node (node), m_possible_truths (possible_truths),
3848 : 19189544 : m_nonspec_possible_truths (nonspec_possible_truths),
3849 : 19189544 : m_inline_param_summary (inline_param_summary),
3850 : 19189544 : m_avals (arg_values)
3851 : : {
3852 : 19189544 : }
3853 : :
3854 : : /* Set THIS to be a duplicate of CTX. Copy all relevant info. */
3855 : :
3856 : : void
3857 : 1883848 : ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx)
3858 : : {
3859 : 1883848 : m_node = ctx.m_node;
3860 : 1883848 : m_possible_truths = ctx.m_possible_truths;
3861 : 1883848 : m_nonspec_possible_truths = ctx.m_nonspec_possible_truths;
3862 : 1883848 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3863 : 1883848 : unsigned int nargs = params_summary
3864 : 1883848 : ? ipa_get_param_count (params_summary) : 0;
3865 : :
3866 : 1883848 : m_inline_param_summary = vNULL;
3867 : : /* Copy the info only if there is at least one useful entry. */
3868 : 1883848 : if (ctx.m_inline_param_summary.exists ())
3869 : : {
3870 : 1683962 : unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs);
3871 : :
3872 : 3841007 : for (unsigned int i = 0; i < n; i++)
3873 : 3155131 : if (ipa_is_param_used_by_ipa_predicates (params_summary, i)
3874 : 4490316 : && !ctx.m_inline_param_summary[i].useless_p ())
3875 : : {
3876 : 998086 : m_inline_param_summary
3877 : 998086 : = ctx.m_inline_param_summary.copy ();
3878 : 998086 : break;
3879 : : }
3880 : : }
3881 : 1883848 : m_avals.m_known_vals = vNULL;
3882 : 1883848 : if (ctx.m_avals.m_known_vals.exists ())
3883 : : {
3884 : 1883848 : unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs);
3885 : :
3886 : 4473688 : for (unsigned int i = 0; i < n; i++)
3887 : 2618468 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3888 : 2618468 : && ctx.m_avals.m_known_vals[i])
3889 : : {
3890 : 28628 : m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy ();
3891 : 28628 : break;
3892 : : }
3893 : : }
3894 : :
3895 : 1883848 : m_avals.m_known_contexts = vNULL;
3896 : 1883848 : if (ctx.m_avals.m_known_contexts.exists ())
3897 : : {
3898 : 1883848 : unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs);
3899 : :
3900 : 1885147 : for (unsigned int i = 0; i < n; i++)
3901 : 23342 : if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3902 : 23342 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3903 : : {
3904 : 22043 : m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy ();
3905 : 22043 : break;
3906 : : }
3907 : : }
3908 : :
3909 : 1883848 : m_avals.m_known_aggs = vNULL;
3910 : 1883848 : if (ctx.m_avals.m_known_aggs.exists ())
3911 : : {
3912 : 1883848 : const ipa_argagg_value_list avl (&ctx.m_avals);
3913 : 6330979 : for (unsigned int i = 0; i < nargs; i++)
3914 : 4450833 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3915 : 4450833 : && avl.value_for_index_p (i))
3916 : : {
3917 : 3702 : m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy ();
3918 : 3702 : break;
3919 : : }
3920 : : }
3921 : :
3922 : 1883848 : m_avals.m_known_value_ranges = vNULL;
3923 : 1883848 : }
3924 : :
3925 : : /* Release memory used by known_vals/contexts/aggs vectors. and
3926 : : inline_param_summary. */
3927 : :
3928 : : void
3929 : 3065502 : ipa_cached_call_context::release ()
3930 : : {
3931 : : /* See if context is initialized at first place. */
3932 : 3065502 : if (!m_node)
3933 : : return;
3934 : 1883848 : m_avals.m_known_aggs.release ();
3935 : 1883848 : m_avals.m_known_vals.release ();
3936 : 1883848 : m_avals.m_known_contexts.release ();
3937 : 1883848 : m_inline_param_summary.release ();
3938 : : }
3939 : :
3940 : : /* Return true if CTX describes the same call context as THIS. */
3941 : :
3942 : : bool
3943 : 6850491 : ipa_call_context::equal_to (const ipa_call_context &ctx)
3944 : : {
3945 : 6850491 : if (m_node != ctx.m_node
3946 : 5668837 : || m_possible_truths != ctx.m_possible_truths
3947 : 5096248 : || m_nonspec_possible_truths != ctx.m_nonspec_possible_truths)
3948 : : return false;
3949 : :
3950 : 5096248 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3951 : 5096248 : unsigned int nargs = params_summary
3952 : 5096248 : ? ipa_get_param_count (params_summary) : 0;
3953 : :
3954 : 5096248 : if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ())
3955 : : {
3956 : 15283406 : for (unsigned int i = 0; i < nargs; i++)
3957 : : {
3958 : 10515026 : if (!ipa_is_param_used_by_ipa_predicates (params_summary, i))
3959 : 3269394 : continue;
3960 : 7245632 : if (i >= m_inline_param_summary.length ()
3961 : 5243506 : || m_inline_param_summary[i].useless_p ())
3962 : : {
3963 : 2942428 : if (i < ctx.m_inline_param_summary.length ()
3964 : 2942428 : && !ctx.m_inline_param_summary[i].useless_p ())
3965 : : return false;
3966 : 2896242 : continue;
3967 : : }
3968 : 4303204 : if (i >= ctx.m_inline_param_summary.length ()
3969 : 4303201 : || ctx.m_inline_param_summary[i].useless_p ())
3970 : : {
3971 : 44367 : if (i < m_inline_param_summary.length ()
3972 : 44367 : && !m_inline_param_summary[i].useless_p ())
3973 : : return false;
3974 : 0 : continue;
3975 : : }
3976 : 4258837 : if (!m_inline_param_summary[i].equal_to
3977 : 4258837 : (ctx.m_inline_param_summary[i]))
3978 : : return false;
3979 : : }
3980 : : }
3981 : 4980050 : if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ())
3982 : : {
3983 : 15291200 : for (unsigned int i = 0; i < nargs; i++)
3984 : : {
3985 : 10321584 : if (!ipa_is_param_used_by_indirect_call (params_summary, i))
3986 : 10129205 : continue;
3987 : 192379 : if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i])
3988 : : {
3989 : 135198 : if (i < ctx.m_avals.m_known_vals.length ()
3990 : 135198 : && ctx.m_avals.m_known_vals[i])
3991 : : return false;
3992 : 135117 : continue;
3993 : : }
3994 : 57181 : if (i >= ctx.m_avals.m_known_vals.length ()
3995 : 57181 : || !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 : 57097 : if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i])
4002 : : return false;
4003 : : }
4004 : : }
4005 : 4969616 : if (m_avals.m_known_contexts.exists ()
4006 : 4969616 : || ctx.m_avals.m_known_contexts.exists ())
4007 : : {
4008 : 15275980 : for (unsigned int i = 0; i < nargs; i++)
4009 : : {
4010 : 10307936 : if (!ipa_is_param_used_by_polymorphic_call (params_summary, i))
4011 : 10187618 : continue;
4012 : 120318 : if (i >= m_avals.m_known_contexts.length ()
4013 : 119172 : || m_avals.m_known_contexts[i].useless_p ())
4014 : : {
4015 : 1146 : if (i < ctx.m_avals.m_known_contexts.length ()
4016 : 1146 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
4017 : : return false;
4018 : 1137 : continue;
4019 : : }
4020 : 119172 : if (i >= ctx.m_avals.m_known_contexts.length ()
4021 : 119172 : || ctx.m_avals.m_known_contexts[i].useless_p ())
4022 : : {
4023 : 8 : if (i < m_avals.m_known_contexts.length ()
4024 : 1883856 : && !m_avals.m_known_contexts[i].useless_p ())
4025 : : return false;
4026 : 0 : continue;
4027 : : }
4028 : 119164 : if (!m_avals.m_known_contexts[i].equal_to
4029 : 119164 : (ctx.m_avals.m_known_contexts[i]))
4030 : : return false;
4031 : : }
4032 : : }
4033 : 4968044 : if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ())
4034 : : {
4035 : : unsigned i = 0, j = 0;
4036 : 11316816 : while (i < m_avals.m_known_aggs.length ()
4037 : 5657448 : || j < ctx.m_avals.m_known_aggs.length ())
4038 : : {
4039 : 690805 : if (i >= m_avals.m_known_aggs.length ())
4040 : : {
4041 : 683904 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4042 : 683904 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4043 : : return false;
4044 : 683304 : j++;
4045 : 683304 : continue;
4046 : 683304 : }
4047 : 6901 : 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 : 6270 : int idx1 = m_avals.m_known_aggs[i].index;
4057 : 6270 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4058 : 6270 : 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 : 6270 : 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 : 6270 : if (!ipa_is_param_used_by_indirect_call (params_summary, idx1))
4073 : : {
4074 : 340 : i++;
4075 : 340 : j++;
4076 : 340 : continue;
4077 : : }
4078 : :
4079 : 5930 : if ((m_avals.m_known_aggs[i].unit_offset
4080 : 5930 : != ctx.m_avals.m_known_aggs[j].unit_offset)
4081 : 5922 : || (m_avals.m_known_aggs[i].by_ref
4082 : 5922 : != ctx.m_avals.m_known_aggs[j].by_ref)
4083 : 11852 : || !operand_equal_p (m_avals.m_known_aggs[i].value,
4084 : 5922 : ctx.m_avals.m_known_aggs[j].value))
4085 : 178 : return false;
4086 : 5752 : i++;
4087 : 5752 : 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 : 19159054 : ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates,
4100 : : bool est_times, bool est_hints)
4101 : : {
4102 : 19159054 : class ipa_fn_summary *info = ipa_fn_summaries->get (m_node);
4103 : 19159054 : size_time_entry *e;
4104 : 19159054 : int size = 0;
4105 : 19159054 : sreal time = 0;
4106 : 19159054 : int min_size = 0;
4107 : 19159054 : ipa_hints hints = 0;
4108 : 19159054 : sreal loops_with_known_iterations = 0;
4109 : 19159054 : sreal loops_with_known_strides = 0;
4110 : 19159054 : int i;
4111 : :
4112 : 19159054 : 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 : 19159054 : if (m_node->callees || m_node->indirect_calls)
4131 : 25154128 : 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 : 19159054 : sreal nonspecialized_time = time;
4137 : :
4138 : 19159054 : min_size += info->size_time_table[0].size;
4139 : 132653234 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4140 : : {
4141 : 113494180 : 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 : 113494180 : if (exec)
4146 : : {
4147 : 110854806 : bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths);
4148 : :
4149 : 110854806 : gcc_checking_assert (e->time >= 0);
4150 : 110854806 : 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 : 110854806 : if (nonconst)
4159 : 55725459 : size += e->size;
4160 : 110854806 : if (!est_times)
4161 : 56749185 : continue;
4162 : 54105621 : nonspecialized_time += e->time;
4163 : 54105621 : if (!nonconst)
4164 : : ;
4165 : 25922871 : else if (!m_inline_param_summary.exists ())
4166 : : {
4167 : 2502190 : if (nonconst)
4168 : 2502190 : time += e->time;
4169 : : }
4170 : : else
4171 : : {
4172 : 23420681 : int prob = e->nonconst_predicate.probability
4173 : 23420681 : (info->conds, m_possible_truths,
4174 : : m_inline_param_summary);
4175 : 23420681 : gcc_checking_assert (prob >= 0);
4176 : 23420681 : gcc_checking_assert (prob <= REG_BR_PROB_BASE);
4177 : 23420681 : if (prob == REG_BR_PROB_BASE)
4178 : 19830251 : time += e->time;
4179 : : else
4180 : 3590430 : time += e->time * prob / REG_BR_PROB_BASE;
4181 : : }
4182 : 54105621 : gcc_checking_assert (time >= 0);
4183 : : }
4184 : : }
4185 : 19159054 : gcc_checking_assert (info->size_time_table[0].exec_predicate == true);
4186 : 19159054 : gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true);
4187 : 19159054 : gcc_checking_assert (min_size >= 0);
4188 : 19159054 : gcc_checking_assert (size >= 0);
4189 : 19159054 : 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 : 19159054 : 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 : 19159054 : if (time > nonspecialized_time)
4198 : 0 : time = nonspecialized_time;
4199 : :
4200 : 19159054 : if (est_hints)
4201 : : {
4202 : 7106514 : if (info->scc_no)
4203 : 168862 : hints |= INLINE_HINT_in_scc;
4204 : 7106514 : if (DECL_DECLARED_INLINE_P (m_node->decl))
4205 : 4452023 : hints |= INLINE_HINT_declared_inline;
4206 : 7106514 : if (info->builtin_constant_p_parms.length ()
4207 : 10243 : && DECL_DECLARED_INLINE_P (m_node->decl))
4208 : 10185 : hints |= INLINE_HINT_builtin_constant_p;
4209 : :
4210 : : ipa_freqcounting_predicate *fcp;
4211 : 7680308 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
4212 : 573794 : if (!fcp->predicate->evaluate (m_possible_truths))
4213 : : {
4214 : 373330 : hints |= INLINE_HINT_loop_iterations;
4215 : 373330 : loops_with_known_iterations += fcp->freq;
4216 : : }
4217 : 7106514 : estimates->loops_with_known_iterations = loops_with_known_iterations;
4218 : :
4219 : 7387207 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
4220 : 280693 : if (!fcp->predicate->evaluate (m_possible_truths))
4221 : : {
4222 : 260723 : hints |= INLINE_HINT_loop_stride;
4223 : 260723 : loops_with_known_strides += fcp->freq;
4224 : : }
4225 : 7106514 : estimates->loops_with_known_strides = loops_with_known_strides;
4226 : : }
4227 : :
4228 : 19159054 : size = RDIV (size, ipa_fn_summary::size_scale);
4229 : 19159054 : min_size = RDIV (min_size, ipa_fn_summary::size_scale);
4230 : :
4231 : 19159054 : 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 : 19159054 : if (est_times)
4244 : : {
4245 : 7106514 : estimates->time = time;
4246 : 7106514 : estimates->nonspecialized_time = nonspecialized_time;
4247 : : }
4248 : 19159054 : estimates->size = size;
4249 : 19159054 : estimates->min_size = min_size;
4250 : 19159054 : if (est_hints)
4251 : 7106514 : estimates->hints = hints;
4252 : 19159054 : 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 : 282712 : estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
4263 : : ipa_auto_call_arg_values *avals,
4264 : : ipa_call_estimates *estimates)
4265 : : {
4266 : 282712 : clause_t clause, nonspec_clause;
4267 : :
4268 : 282712 : evaluate_conditions_for_known_args (node, false, avals, &clause,
4269 : : &nonspec_clause, NULL);
4270 : 282712 : ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals);
4271 : 282712 : ctx.estimate_size_and_time (estimates);
4272 : 282712 : }
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 : 5105670 : ipa_get_stack_frame_offset (struct cgraph_node *node)
4280 : : {
4281 : 5105670 : HOST_WIDE_INT offset = 0;
4282 : 5105670 : if (!node->inlined_to)
4283 : : return 0;
4284 : 3946334 : node = node->callers->caller;
4285 : 4897548 : while (true)
4286 : : {
4287 : 4897548 : offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
4288 : 4421941 : if (!node->inlined_to)
4289 : : return offset;
4290 : 475607 : 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 : 4621178 : inline_update_callee_summaries (struct cgraph_node *node, int depth)
4300 : : {
4301 : 4621178 : struct cgraph_edge *e;
4302 : :
4303 : 4621178 : ipa_propagate_frequency (node);
4304 : 8864619 : for (e = node->callees; e; e = e->next_callee)
4305 : : {
4306 : 4243441 : if (!e->inline_failed)
4307 : 675398 : inline_update_callee_summaries (e->callee, depth);
4308 : : else
4309 : 3568043 : ipa_call_summaries->get (e)->loop_depth += depth;
4310 : : }
4311 : 4727864 : for (e = node->indirect_calls; e; e = e->next_callee)
4312 : 106686 : ipa_call_summaries->get (e)->loop_depth += depth;
4313 : 4621178 : }
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 : 3675042 : remap_edge_params (struct cgraph_edge *inlined_edge,
4325 : : struct cgraph_edge *edge)
4326 : : {
4327 : 3675042 : if (ipa_node_params_sum)
4328 : : {
4329 : 2653620 : int i;
4330 : 2653620 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4331 : 2653620 : if (!args)
4332 : : return;
4333 : 1487879 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4334 : 1487879 : class ipa_call_summary *inlined_es
4335 : 1487879 : = ipa_call_summaries->get (inlined_edge);
4336 : :
4337 : 1487879 : if (es->param.length () == 0)
4338 : : return;
4339 : :
4340 : 8652464 : for (i = 0; i < ipa_get_cs_argument_count (args); i++)
4341 : : {
4342 : 2912928 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4343 : 2912928 : if (jfunc->type == IPA_JF_PASS_THROUGH
4344 : 2161333 : || jfunc->type == IPA_JF_ANCESTOR)
4345 : : {
4346 : 891325 : int id = jfunc->type == IPA_JF_PASS_THROUGH
4347 : 891325 : ? ipa_get_jf_pass_through_formal_id (jfunc)
4348 : 139730 : : ipa_get_jf_ancestor_formal_id (jfunc);
4349 : 1782567 : if (id < (int) inlined_es->param.length ())
4350 : : {
4351 : 891234 : int prob1 = es->param[i].change_prob;
4352 : 891234 : int prob2 = inlined_es->param[id].change_prob;
4353 : 891234 : int prob = combine_probabilities (prob1, prob2);
4354 : :
4355 : 891234 : if (prob1 && prob2 && !prob)
4356 : 891234 : prob = 1;
4357 : :
4358 : 891234 : es->param[i].change_prob = prob;
4359 : :
4360 : 1782468 : if (inlined_es
4361 : 891234 : ->param[id].points_to_local_or_readonly_memory)
4362 : 204071 : es->param[i].points_to_local_or_readonly_memory = true;
4363 : 1782468 : if (inlined_es
4364 : 891234 : ->param[id].points_to_possible_sra_candidate)
4365 : 172718 : es->param[i].points_to_possible_sra_candidate = true;
4366 : : }
4367 : 891325 : 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 : 4621178 : 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 : 4621178 : struct cgraph_edge *e, *next;
4396 : 8864195 : for (e = node->callees; e; e = next)
4397 : : {
4398 : 4243017 : ipa_predicate p;
4399 : 4243017 : next = e->next_callee;
4400 : :
4401 : 4243017 : if (e->inline_failed)
4402 : : {
4403 : 3567619 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4404 : 3567619 : remap_edge_params (inlined_edge, e);
4405 : :
4406 : 3567619 : if (es->predicate)
4407 : : {
4408 : 1227481 : p = es->predicate->remap_after_inlining
4409 : 1227481 : (info, params_summary,
4410 : : callee_info, operand_map,
4411 : : offset_map, possible_truths,
4412 : : *toplev_predicate);
4413 : 1227481 : edge_set_predicate (e, &p);
4414 : : }
4415 : : else
4416 : 2340138 : edge_set_predicate (e, toplev_predicate);
4417 : : }
4418 : : else
4419 : 675398 : 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 : 4728601 : for (e = node->indirect_calls; e; e = next)
4425 : : {
4426 : 107423 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4427 : 107423 : ipa_predicate p;
4428 : 107423 : next = e->next_callee;
4429 : :
4430 : 107423 : remap_edge_params (inlined_edge, e);
4431 : 107423 : if (es->predicate)
4432 : : {
4433 : 27972 : p = es->predicate->remap_after_inlining
4434 : 27972 : (info, params_summary,
4435 : : callee_info, operand_map, offset_map,
4436 : : possible_truths, *toplev_predicate);
4437 : 27972 : edge_set_predicate (e, &p);
4438 : : }
4439 : : else
4440 : 79451 : edge_set_predicate (e, toplev_predicate);
4441 : : }
4442 : 4621178 : }
4443 : :
4444 : : /* Run remap_after_inlining on each predicate in V. */
4445 : :
4446 : : static void
4447 : 7891560 : 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 : 7891560 : ipa_freqcounting_predicate *fcp;
4458 : 7921104 : for (int i = 0; vec_safe_iterate (v, i, &fcp); i++)
4459 : : {
4460 : 29544 : ipa_predicate p
4461 : 29544 : = fcp->predicate->remap_after_inlining (info, params_summary,
4462 : : callee_info, operand_map,
4463 : : offset_map, possible_truths,
4464 : : *toplev_predicate);
4465 : 41846 : if (p != false && p != true)
4466 : 2810 : *fcp->predicate &= p;
4467 : : }
4468 : 7891560 : }
4469 : :
4470 : : /* We inlined EDGE. Update summary of the function we inlined into. */
4471 : :
4472 : : void
4473 : 3945780 : ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
4474 : : {
4475 : 3945780 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
4476 : 3705477 : struct cgraph_node *to = (edge->caller->inlined_to
4477 : 3945780 : ? edge->caller->inlined_to : edge->caller);
4478 : 3945780 : class ipa_fn_summary *info = ipa_fn_summaries->get (to);
4479 : 3945780 : clause_t clause = 0; /* not_inline is known to be false. */
4480 : 3945780 : size_time_entry *e;
4481 : 3945780 : auto_vec<int, 8> operand_map;
4482 : 3945780 : auto_vec<HOST_WIDE_INT, 8> offset_map;
4483 : 3945780 : int i;
4484 : 3945780 : ipa_predicate toplev_predicate;
4485 : 3945780 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4486 : 3945780 : ipa_node_params *params_summary = (ipa_node_params_sum
4487 : 3945780 : ? ipa_node_params_sum->get (to) : NULL);
4488 : :
4489 : 3945780 : if (es->predicate)
4490 : 348334 : toplev_predicate = *es->predicate;
4491 : : else
4492 : 3597446 : toplev_predicate = true;
4493 : :
4494 : 3945780 : info->fp_expressions |= callee_info->fp_expressions;
4495 : 3945780 : info->target_info |= callee_info->target_info;
4496 : :
4497 : 3945780 : if (callee_info->conds)
4498 : : {
4499 : 2865611 : ipa_auto_call_arg_values avals;
4500 : 2865611 : evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
4501 : 2865611 : }
4502 : 3945780 : if (ipa_node_params_sum && callee_info->conds)
4503 : : {
4504 : 783673 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4505 : 783673 : int count = args ? ipa_get_cs_argument_count (args) : 0;
4506 : 783542 : int i;
4507 : :
4508 : 783542 : if (count)
4509 : : {
4510 : 783542 : operand_map.safe_grow_cleared (count, true);
4511 : 783542 : offset_map.safe_grow_cleared (count, true);
4512 : : }
4513 : 2496043 : for (i = 0; i < count; i++)
4514 : : {
4515 : 1712370 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4516 : 1712370 : int map = -1;
4517 : :
4518 : : /* TODO: handle non-NOPs when merging. */
4519 : 1712370 : if (jfunc->type == IPA_JF_PASS_THROUGH)
4520 : : {
4521 : 293094 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4522 : 290169 : map = ipa_get_jf_pass_through_formal_id (jfunc);
4523 : 293094 : if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
4524 : 196090 : offset_map[i] = -1;
4525 : : }
4526 : 1419276 : else if (jfunc->type == IPA_JF_ANCESTOR)
4527 : : {
4528 : 87470 : HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
4529 : 87470 : if (offset >= 0 && offset < INT_MAX)
4530 : : {
4531 : 87470 : map = ipa_get_jf_ancestor_formal_id (jfunc);
4532 : 87470 : if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
4533 : 53608 : offset = -1;
4534 : 87470 : offset_map[i] = offset;
4535 : : }
4536 : : }
4537 : 1712370 : operand_map[i] = map;
4538 : 3113005 : gcc_assert (map < ipa_get_param_count (params_summary));
4539 : : }
4540 : :
4541 : : int ip;
4542 : 786340 : for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++)
4543 : 2667 : if (ip < count && operand_map[ip] >= 0)
4544 : 33 : add_builtin_constant_p_parm (info, operand_map[ip]);
4545 : : }
4546 : 3945780 : sreal freq = edge->sreal_frequency ();
4547 : 21744062 : for (i = 0; callee_info->size_time_table.iterate (i, &e); i++)
4548 : : {
4549 : 17798282 : ipa_predicate p;
4550 : 17798282 : p = e->exec_predicate.remap_after_inlining
4551 : 17798282 : (info, params_summary,
4552 : : callee_info, operand_map,
4553 : : offset_map, clause,
4554 : : toplev_predicate);
4555 : 17798282 : ipa_predicate nonconstp;
4556 : 17798282 : nonconstp = e->nonconst_predicate.remap_after_inlining
4557 : 17798282 : (info, params_summary,
4558 : : callee_info, operand_map,
4559 : : offset_map, clause,
4560 : : toplev_predicate);
4561 : 26834537 : if (p != false && nonconstp != false)
4562 : : {
4563 : 8729458 : sreal add_time = ((sreal)e->time * freq);
4564 : 8729458 : int prob = e->nonconst_predicate.probability (callee_info->conds,
4565 : : clause, es->param);
4566 : 8729458 : if (prob != REG_BR_PROB_BASE)
4567 : 1083254 : add_time = add_time * prob / REG_BR_PROB_BASE;
4568 : 1083254 : if (prob != REG_BR_PROB_BASE
4569 : 1083254 : && 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 : 8729458 : info->account_size_time (e->size, add_time, p, nonconstp);
4575 : : }
4576 : : }
4577 : 3945780 : remap_edge_summaries (edge, edge->callee, info, params_summary,
4578 : : callee_info, operand_map,
4579 : : offset_map, clause, &toplev_predicate);
4580 : 3945780 : remap_freqcounting_predicate (info, params_summary, callee_info,
4581 : : info->loop_iterations, operand_map,
4582 : : offset_map, clause, &toplev_predicate);
4583 : 3945780 : remap_freqcounting_predicate (info, params_summary, callee_info,
4584 : : info->loop_strides, operand_map,
4585 : : offset_map, clause, &toplev_predicate);
4586 : :
4587 : 3945780 : HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
4588 : 3945780 : HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
4589 : :
4590 : 3945780 : if (info->estimated_stack_size < peak)
4591 : 124954 : info->estimated_stack_size = peak;
4592 : :
4593 : 3945780 : inline_update_callee_summaries (edge->callee, es->loop_depth);
4594 : 3945780 : if (info->call_size_time_table.length ())
4595 : : {
4596 : 841577 : int edge_size = 0;
4597 : 841577 : sreal edge_time = 0;
4598 : :
4599 : 841577 : estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0);
4600 : : /* Unaccount size and time of the optimized out call. */
4601 : 841577 : info->account_size_time (-edge_size, -edge_time,
4602 : 841577 : es->predicate ? *es->predicate : true,
4603 : 841577 : es->predicate ? *es->predicate : true,
4604 : : true);
4605 : : /* Account new calls. */
4606 : 841577 : summarize_calls_size_and_time (edge->callee, info);
4607 : : }
4608 : :
4609 : : /* Free summaries that are not maintained for inline clones/edges. */
4610 : 3945780 : ipa_call_summaries->remove (edge);
4611 : 3945780 : ipa_fn_summaries->remove (edge->callee);
4612 : 3945780 : ipa_remove_from_growth_caches (edge);
4613 : 3945780 : }
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 : 9382942 : ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset)
4621 : : {
4622 : 9382942 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
4623 : 9382942 : class ipa_size_summary *size_info = ipa_size_summaries->get (node);
4624 : 9382942 : size_time_entry *e;
4625 : 9382942 : int i;
4626 : :
4627 : 9382942 : size_info->size = 0;
4628 : 9382942 : info->time = 0;
4629 : 47967141 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4630 : : {
4631 : 38584199 : size_info->size += e->size;
4632 : 38584199 : info->time += e->time;
4633 : : }
4634 : 9382942 : info->min_size = info->size_time_table[0].size;
4635 : 9382942 : if (reset)
4636 : 8498839 : info->call_size_time_table.release ();
4637 : 9382942 : if (node->callees || node->indirect_calls)
4638 : 7144487 : 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 : 9382942 : size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale);
4643 : 9382942 : info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale);
4644 : 9382942 : }
4645 : :
4646 : :
4647 : : /* This function performs intraprocedural analysis in NODE that is required to
4648 : : inline indirect calls. */
4649 : :
4650 : : static void
4651 : 1376915 : inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4652 : : {
4653 : 1376915 : ipa_analyze_node (node);
4654 : 1376915 : 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 : 1376915 : }
4660 : :
4661 : :
4662 : : /* Note function body size. */
4663 : :
4664 : : void
4665 : 1387276 : inline_analyze_function (struct cgraph_node *node)
4666 : : {
4667 : 1387276 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4668 : :
4669 : 1387276 : if (dump_file)
4670 : 94 : fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ());
4671 : 1387276 : if (opt_for_fn (node->decl, optimize) && !node->thunk)
4672 : 1376915 : inline_indirect_intraprocedural_analysis (node);
4673 : 1387276 : compute_fn_summary (node, false);
4674 : 1387276 : if (!optimize)
4675 : : {
4676 : 9092 : struct cgraph_edge *e;
4677 : 21804 : for (e = node->callees; e; e = e->next_callee)
4678 : 12712 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4679 : 9163 : for (e = node->indirect_calls; e; e = e->next_callee)
4680 : 71 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4681 : : }
4682 : :
4683 : 1387276 : pop_cfun ();
4684 : 1387276 : }
4685 : :
4686 : :
4687 : : /* Called when new function is inserted to callgraph late. */
4688 : :
4689 : : void
4690 : 19094 : ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
4691 : : {
4692 : 19094 : inline_analyze_function (node);
4693 : 19094 : }
4694 : :
4695 : : /* Note function body size. */
4696 : :
4697 : : static void
4698 : 231275 : ipa_fn_summary_generate (void)
4699 : : {
4700 : 231275 : struct cgraph_node *node;
4701 : :
4702 : 2103489 : FOR_EACH_DEFINED_FUNCTION (node)
4703 : 1872214 : if (DECL_STRUCT_FUNCTION (node->decl))
4704 : 1859079 : node->versionable = tree_versionable_function_p (node->decl);
4705 : :
4706 : 231275 : ipa_fn_summary_alloc ();
4707 : :
4708 : 231275 : ipa_fn_summaries->enable_insertion_hook ();
4709 : :
4710 : 231275 : ipa_register_cgraph_hooks ();
4711 : :
4712 : 2103489 : FOR_EACH_DEFINED_FUNCTION (node)
4713 : 1872214 : if (!node->alias
4714 : 1872214 : && (flag_generate_lto || flag_generate_offload|| flag_wpa
4715 : 1682189 : || opt_for_fn (node->decl, optimize)))
4716 : 1350219 : inline_analyze_function (node);
4717 : 231275 : }
4718 : :
4719 : :
4720 : : /* Write inline summary for edge E to OB. */
4721 : :
4722 : : static void
4723 : 346164 : read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
4724 : : bool prevails)
4725 : : {
4726 : 346164 : class ipa_call_summary *es = prevails
4727 : 346164 : ? ipa_call_summaries->get_create (e) : NULL;
4728 : 346164 : ipa_predicate p;
4729 : 346164 : int length, i;
4730 : :
4731 : 346164 : int size = streamer_read_uhwi (ib);
4732 : 346164 : int time = streamer_read_uhwi (ib);
4733 : 346164 : int depth = streamer_read_uhwi (ib);
4734 : :
4735 : 346164 : if (es)
4736 : : {
4737 : 346115 : es->call_stmt_size = size;
4738 : 346115 : es->call_stmt_time = time;
4739 : 346115 : es->loop_depth = depth;
4740 : : }
4741 : :
4742 : 346164 : bitpack_d bp = streamer_read_bitpack (ib);
4743 : 346164 : if (es)
4744 : 346115 : es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
4745 : : else
4746 : 49 : bp_unpack_value (&bp, 1);
4747 : :
4748 : 346164 : p.stream_in (ib);
4749 : 346164 : if (es)
4750 : 346115 : edge_set_predicate (e, &p);
4751 : 346164 : length = streamer_read_uhwi (ib);
4752 : 346164 : if (length && es
4753 : 346164 : && (e->possibly_call_in_translation_unit_p ()
4754 : : /* Also stream in jump functions to builtins in hope that they
4755 : : will get fnspecs. */
4756 : 118207 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
4757 : : {
4758 : 231721 : es->param.safe_grow_cleared (length, true);
4759 : 797908 : for (i = 0; i < length; i++)
4760 : : {
4761 : 566187 : es->param[i].change_prob = streamer_read_uhwi (ib);
4762 : 566187 : bitpack_d bp = streamer_read_bitpack (ib);
4763 : 1698561 : es->param[i].points_to_local_or_readonly_memory
4764 : 566187 : = bp_unpack_value (&bp, 1);
4765 : 1698561 : es->param[i].points_to_possible_sra_candidate
4766 : 566187 : = bp_unpack_value (&bp, 1);
4767 : : }
4768 : : }
4769 : : else
4770 : : {
4771 : 136771 : for (i = 0; i < length; i++)
4772 : : {
4773 : 22328 : streamer_read_uhwi (ib);
4774 : 22328 : streamer_read_uhwi (ib);
4775 : : }
4776 : : }
4777 : 346164 : }
4778 : :
4779 : :
4780 : : /* Stream in inline summaries from the section. */
4781 : :
4782 : : static void
4783 : 12976 : inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4784 : : size_t len)
4785 : : {
4786 : 12976 : const struct lto_function_header *header =
4787 : : (const struct lto_function_header *) data;
4788 : 12976 : const int cfg_offset = sizeof (struct lto_function_header);
4789 : 12976 : const int main_offset = cfg_offset + header->cfg_size;
4790 : 12976 : const int string_offset = main_offset + header->main_size;
4791 : 12976 : class data_in *data_in;
4792 : 12976 : unsigned int i, count2, j;
4793 : 12976 : unsigned int f_count;
4794 : :
4795 : 12976 : lto_input_block ib ((const char *) data + main_offset, header->main_size,
4796 : 12976 : file_data);
4797 : :
4798 : 12976 : data_in =
4799 : 25952 : lto_data_in_create (file_data, (const char *) data + string_offset,
4800 : 12976 : header->string_size, vNULL);
4801 : 12976 : f_count = streamer_read_uhwi (&ib);
4802 : 98329 : for (i = 0; i < f_count; i++)
4803 : : {
4804 : 85353 : unsigned int index;
4805 : 85353 : struct cgraph_node *node;
4806 : 85353 : class ipa_fn_summary *info;
4807 : 85353 : class ipa_node_params *params_summary;
4808 : 85353 : class ipa_size_summary *size_info;
4809 : 85353 : lto_symtab_encoder_t encoder;
4810 : 85353 : struct bitpack_d bp;
4811 : 85353 : struct cgraph_edge *e;
4812 : 85353 : ipa_predicate p;
4813 : :
4814 : 85353 : index = streamer_read_uhwi (&ib);
4815 : 85353 : encoder = file_data->symtab_node_encoder;
4816 : 85353 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4817 : : index));
4818 : 85353 : info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
4819 : 85353 : params_summary = node->prevailing_p ()
4820 : 85353 : ? ipa_node_params_sum->get (node) : NULL;
4821 : 85353 : size_info = node->prevailing_p ()
4822 : 85353 : ? ipa_size_summaries->get_create (node) : NULL;
4823 : :
4824 : 85353 : int stack_size = streamer_read_uhwi (&ib);
4825 : 85353 : int size = streamer_read_uhwi (&ib);
4826 : 85353 : sreal time = sreal::stream_in (&ib);
4827 : :
4828 : 85353 : if (info)
4829 : : {
4830 : 85295 : info->estimated_stack_size
4831 : 85295 : = size_info->estimated_self_stack_size = stack_size;
4832 : 85295 : size_info->size = size_info->self_size = size;
4833 : 85295 : info->time = time;
4834 : : }
4835 : :
4836 : 85353 : bp = streamer_read_bitpack (&ib);
4837 : 85353 : if (info)
4838 : : {
4839 : 85295 : info->inlinable = bp_unpack_value (&bp, 1);
4840 : 85295 : info->fp_expressions = bp_unpack_value (&bp, 1);
4841 : 85295 : if (!lto_stream_offload_p)
4842 : 85295 : 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 : 85353 : count2 = streamer_read_uhwi (&ib);
4853 : 85353 : gcc_assert (!info || !info->conds);
4854 : 85295 : if (info)
4855 : 85295 : vec_safe_reserve_exact (info->conds, count2);
4856 : 161957 : for (j = 0; j < count2; j++)
4857 : : {
4858 : 76604 : struct condition c;
4859 : 76604 : unsigned int k, count3;
4860 : 76604 : c.operand_num = streamer_read_uhwi (&ib);
4861 : 76604 : c.code = (enum tree_code) streamer_read_uhwi (&ib);
4862 : 76604 : c.type = stream_read_tree (&ib, data_in);
4863 : 76604 : c.val = stream_read_tree (&ib, data_in);
4864 : 76604 : bp = streamer_read_bitpack (&ib);
4865 : 76604 : c.agg_contents = bp_unpack_value (&bp, 1);
4866 : 76604 : c.by_ref = bp_unpack_value (&bp, 1);
4867 : 76604 : if (c.agg_contents)
4868 : 12385 : c.offset = streamer_read_uhwi (&ib);
4869 : 76604 : count3 = streamer_read_uhwi (&ib);
4870 : 76604 : c.param_ops = NULL;
4871 : 76604 : if (info)
4872 : 76604 : vec_safe_reserve_exact (c.param_ops, count3);
4873 : 76604 : if (params_summary)
4874 : 76604 : ipa_set_param_used_by_ipa_predicates
4875 : 76604 : (params_summary, c.operand_num, true);
4876 : 80100 : for (k = 0; k < count3; k++)
4877 : : {
4878 : 3496 : struct expr_eval_op op;
4879 : 3496 : enum gimple_rhs_class rhs_class;
4880 : 3496 : op.code = (enum tree_code) streamer_read_uhwi (&ib);
4881 : 3496 : op.type = stream_read_tree (&ib, data_in);
4882 : 3496 : switch (rhs_class = get_gimple_rhs_class (op.code))
4883 : : {
4884 : 1397 : case GIMPLE_UNARY_RHS:
4885 : 1397 : op.index = 0;
4886 : 1397 : op.val[0] = NULL_TREE;
4887 : 1397 : op.val[1] = NULL_TREE;
4888 : 1397 : break;
4889 : :
4890 : 2099 : case GIMPLE_BINARY_RHS:
4891 : 2099 : case GIMPLE_TERNARY_RHS:
4892 : 2099 : bp = streamer_read_bitpack (&ib);
4893 : 2099 : op.index = bp_unpack_value (&bp, 2);
4894 : 2099 : op.val[0] = stream_read_tree (&ib, data_in);
4895 : 2099 : if (rhs_class == GIMPLE_BINARY_RHS)
4896 : 2099 : 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 : 3496 : if (info)
4906 : 3496 : c.param_ops->quick_push (op);
4907 : : }
4908 : 76604 : if (info)
4909 : 76604 : info->conds->quick_push (c);
4910 : : }
4911 : 85353 : count2 = streamer_read_uhwi (&ib);
4912 : 85353 : gcc_assert (!info || !info->size_time_table.length ());
4913 : 85353 : if (info && count2)
4914 : 85295 : info->size_time_table.reserve_exact (count2);
4915 : 324652 : for (j = 0; j < count2; j++)
4916 : : {
4917 : 239299 : class size_time_entry e;
4918 : :
4919 : 239299 : e.size = streamer_read_uhwi (&ib);
4920 : 239299 : e.time = sreal::stream_in (&ib);
4921 : 239299 : e.exec_predicate.stream_in (&ib);
4922 : 239299 : e.nonconst_predicate.stream_in (&ib);
4923 : :
4924 : 239299 : if (info)
4925 : 239183 : info->size_time_table.quick_push (e);
4926 : : }
4927 : :
4928 : 85353 : count2 = streamer_read_uhwi (&ib);
4929 : 86684 : for (j = 0; j < count2; j++)
4930 : : {
4931 : 1331 : p.stream_in (&ib);
4932 : 1331 : sreal fcp_freq = sreal::stream_in (&ib);
4933 : 1331 : if (info)
4934 : : {
4935 : 1331 : ipa_freqcounting_predicate fcp;
4936 : 1331 : fcp.predicate = NULL;
4937 : 1331 : set_hint_predicate (&fcp.predicate, p);
4938 : 1331 : fcp.freq = fcp_freq;
4939 : 1331 : vec_safe_push (info->loop_iterations, fcp);
4940 : : }
4941 : : }
4942 : 85353 : count2 = streamer_read_uhwi (&ib);
4943 : 85640 : 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 : 85353 : count2 = streamer_read_uhwi (&ib);
4957 : 85353 : if (info && count2)
4958 : 6 : info->builtin_constant_p_parms.reserve_exact (count2);
4959 : 85359 : 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 : 430049 : for (e = node->callees; e; e = e->next_callee)
4966 : 344696 : read_ipa_call_summary (&ib, e, info != NULL);
4967 : 86821 : for (e = node->indirect_calls; e; e = e->next_callee)
4968 : 1468 : read_ipa_call_summary (&ib, e, info != NULL);
4969 : : }
4970 : :
4971 : 12976 : lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4972 : : len);
4973 : 12976 : lto_data_in_delete (data_in);
4974 : 12976 : }
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 : 11921 : ipa_fn_summary_read (void)
4983 : : {
4984 : 11921 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4985 : 11921 : struct lto_file_decl_data *file_data;
4986 : 11921 : unsigned int j = 0;
4987 : :
4988 : 11921 : ipa_prop_read_jump_functions ();
4989 : 11921 : ipa_fn_summary_alloc ();
4990 : :
4991 : 36818 : while ((file_data = file_data_vec[j++]))
4992 : : {
4993 : 12976 : size_t len;
4994 : 12976 : const char *data
4995 : 12976 : = lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary,
4996 : : &len);
4997 : 12976 : if (data)
4998 : 12976 : 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 : 11921 : ipa_register_cgraph_hooks ();
5007 : :
5008 : 11921 : gcc_assert (ipa_fn_summaries);
5009 : 11921 : ipa_fn_summaries->enable_insertion_hook ();
5010 : 11921 : }
5011 : :
5012 : :
5013 : : /* Write inline summary for edge E to OB. */
5014 : :
5015 : : static void
5016 : 377422 : write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
5017 : : {
5018 : 377422 : class ipa_call_summary *es = ipa_call_summaries->get (e);
5019 : 377422 : int i;
5020 : :
5021 : 377422 : streamer_write_uhwi (ob, es->call_stmt_size);
5022 : 377422 : streamer_write_uhwi (ob, es->call_stmt_time);
5023 : 377422 : streamer_write_uhwi (ob, es->loop_depth);
5024 : :
5025 : 377422 : bitpack_d bp = bitpack_create (ob->main_stream);
5026 : 377422 : bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
5027 : 377422 : streamer_write_bitpack (&bp);
5028 : :
5029 : 377422 : if (es->predicate)
5030 : 10417 : es->predicate->stream_out (ob);
5031 : : else
5032 : 367005 : streamer_write_uhwi (ob, 0);
5033 : 377422 : streamer_write_uhwi (ob, es->param.length ());
5034 : 2278968 : for (i = 0; i < (int) es->param.length (); i++)
5035 : : {
5036 : 628636 : streamer_write_uhwi (ob, es->param[i].change_prob);
5037 : 628636 : bp = bitpack_create (ob->main_stream);
5038 : 628636 : bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1);
5039 : 628636 : bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1);
5040 : 628636 : streamer_write_bitpack (&bp);
5041 : : }
5042 : 377422 : }
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 : 22598 : ipa_fn_summary_write (void)
5051 : : {
5052 : 22598 : struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
5053 : 22598 : lto_symtab_encoder_iterator lsei;
5054 : 22598 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5055 : 22598 : unsigned int count = 0;
5056 : :
5057 : 126482 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5058 : 103884 : lsei_next_function_in_partition (&lsei))
5059 : : {
5060 : 103884 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5061 : 103884 : if (cnode->definition && !cnode->alias)
5062 : 101475 : count++;
5063 : : }
5064 : 22598 : streamer_write_uhwi (ob, count);
5065 : :
5066 : 126482 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5067 : 103884 : lsei_next_function_in_partition (&lsei))
5068 : : {
5069 : 103884 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5070 : 103884 : if (cnode->definition && !cnode->alias)
5071 : : {
5072 : 101475 : class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
5073 : 101475 : class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
5074 : 101475 : struct bitpack_d bp;
5075 : 101475 : struct cgraph_edge *edge;
5076 : 101475 : int i;
5077 : 101475 : size_time_entry *e;
5078 : 101475 : struct condition *c;
5079 : :
5080 : 101475 : streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
5081 : 101475 : streamer_write_hwi (ob, size_info->estimated_self_stack_size);
5082 : 101475 : streamer_write_hwi (ob, size_info->self_size);
5083 : 101475 : info->time.stream_out (ob);
5084 : 101475 : bp = bitpack_create (ob->main_stream);
5085 : 101475 : bp_pack_value (&bp, info->inlinable, 1);
5086 : 101475 : bp_pack_value (&bp, info->fp_expressions, 1);
5087 : 101475 : streamer_write_bitpack (&bp);
5088 : 101475 : if (!lto_stream_offload_p)
5089 : 101475 : streamer_write_uhwi (ob, info->target_info);
5090 : 101475 : streamer_write_uhwi (ob, vec_safe_length (info->conds));
5091 : 193567 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
5092 : : {
5093 : 92092 : int j;
5094 : 92092 : struct expr_eval_op *op;
5095 : :
5096 : 92092 : streamer_write_uhwi (ob, c->operand_num);
5097 : 92092 : streamer_write_uhwi (ob, c->code);
5098 : 92092 : stream_write_tree (ob, c->type, true);
5099 : 92092 : stream_write_tree (ob, c->val, true);
5100 : 92092 : bp = bitpack_create (ob->main_stream);
5101 : 92092 : bp_pack_value (&bp, c->agg_contents, 1);
5102 : 92092 : bp_pack_value (&bp, c->by_ref, 1);
5103 : 92092 : streamer_write_bitpack (&bp);
5104 : 92092 : if (c->agg_contents)
5105 : 15516 : streamer_write_uhwi (ob, c->offset);
5106 : 92092 : streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
5107 : 191129 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
5108 : : {
5109 : 4063 : streamer_write_uhwi (ob, op->code);
5110 : 4063 : stream_write_tree (ob, op->type, true);
5111 : 4063 : if (op->val[0])
5112 : : {
5113 : 2453 : bp = bitpack_create (ob->main_stream);
5114 : 2453 : bp_pack_value (&bp, op->index, 2);
5115 : 2453 : streamer_write_bitpack (&bp);
5116 : 2453 : stream_write_tree (ob, op->val[0], true);
5117 : 2453 : if (op->val[1])
5118 : 4 : stream_write_tree (ob, op->val[1], true);
5119 : : }
5120 : : }
5121 : : }
5122 : 101475 : streamer_write_uhwi (ob, info->size_time_table.length ());
5123 : 490896 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
5124 : : {
5125 : 287946 : streamer_write_uhwi (ob, e->size);
5126 : 287946 : e->time.stream_out (ob);
5127 : 287946 : e->exec_predicate.stream_out (ob);
5128 : 287946 : e->nonconst_predicate.stream_out (ob);
5129 : : }
5130 : 101475 : ipa_freqcounting_predicate *fcp;
5131 : 101475 : streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations));
5132 : 204889 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
5133 : : {
5134 : 1939 : fcp->predicate->stream_out (ob);
5135 : 1939 : fcp->freq.stream_out (ob);
5136 : : }
5137 : 101475 : streamer_write_uhwi (ob, vec_safe_length (info->loop_strides));
5138 : 203288 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
5139 : : {
5140 : 338 : fcp->predicate->stream_out (ob);
5141 : 338 : fcp->freq.stream_out (ob);
5142 : : }
5143 : 101475 : streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ());
5144 : 101475 : int ip;
5145 : 202964 : for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip);
5146 : : i++)
5147 : 14 : streamer_write_uhwi (ob, ip);
5148 : 476226 : for (edge = cnode->callees; edge; edge = edge->next_callee)
5149 : 374751 : write_ipa_call_summary (ob, edge);
5150 : 104146 : for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
5151 : 2671 : write_ipa_call_summary (ob, edge);
5152 : : }
5153 : : }
5154 : 22598 : streamer_write_char_stream (ob->main_stream, 0);
5155 : 22598 : produce_asm (ob);
5156 : 22598 : destroy_output_block (ob);
5157 : :
5158 : 22598 : ipa_prop_write_jump_functions ();
5159 : 22598 : }
5160 : :
5161 : :
5162 : : /* Release function summary. */
5163 : :
5164 : : void
5165 : 721367 : ipa_free_fn_summary (void)
5166 : : {
5167 : 721367 : if (!ipa_call_summaries)
5168 : : return;
5169 : 456851 : ggc_delete (ipa_fn_summaries);
5170 : 456851 : ipa_fn_summaries = NULL;
5171 : 456851 : delete ipa_call_summaries;
5172 : 456851 : ipa_call_summaries = NULL;
5173 : 456851 : edge_predicate_pool.release ();
5174 : : /* During IPA this is one of largest datastructures to release. */
5175 : 456851 : if (flag_wpa)
5176 : 7538 : ggc_trim ();
5177 : : }
5178 : :
5179 : : /* Release function summary. */
5180 : :
5181 : : void
5182 : 721367 : ipa_free_size_summary (void)
5183 : : {
5184 : 721367 : if (!ipa_size_summaries)
5185 : : return;
5186 : 456851 : delete ipa_size_summaries;
5187 : 456851 : 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 : 572874 : pass_local_fn_summary (gcc::context *ctxt)
5209 : 1145748 : : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
5210 : : {}
5211 : :
5212 : : /* opt_pass methods: */
5213 : 286437 : opt_pass * clone () final override
5214 : : {
5215 : 286437 : return new pass_local_fn_summary (m_ctxt);
5216 : : }
5217 : 5758407 : unsigned int execute (function *) final override
5218 : : {
5219 : 5758407 : return compute_fn_summary_for_current ();
5220 : : }
5221 : :
5222 : : }; // class pass_local_fn_summary
5223 : :
5224 : : } // anon namespace
5225 : :
5226 : : gimple_opt_pass *
5227 : 286437 : make_pass_local_fn_summary (gcc::context *ctxt)
5228 : : {
5229 : 286437 : 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 : 572874 : pass_ipa_free_fn_summary (gcc::context *ctxt)
5254 : 572874 : : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
5255 : 1145748 : small_p (false)
5256 : : {}
5257 : :
5258 : : /* opt_pass methods: */
5259 : 286437 : opt_pass *clone () final override
5260 : : {
5261 : 286437 : return new pass_ipa_free_fn_summary (m_ctxt);
5262 : : }
5263 : 572874 : void set_pass_param (unsigned int n, bool param) final override
5264 : : {
5265 : 572874 : gcc_assert (n == 0);
5266 : 572874 : small_p = param;
5267 : 572874 : }
5268 : 482835 : bool gate (function *) final override { return true; }
5269 : 462993 : unsigned int execute (function *) final override
5270 : : {
5271 : 462993 : ipa_free_fn_summary ();
5272 : : /* Free ipa-prop structures if they are no longer needed. */
5273 : 462993 : ipa_free_all_structures_after_iinln ();
5274 : 462993 : if (!flag_wpa)
5275 : 455455 : ipa_free_size_summary ();
5276 : 462993 : 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 : 286437 : make_pass_ipa_free_fn_summary (gcc::context *ctxt)
5287 : : {
5288 : 286437 : 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 : 286437 : 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 : 286437 : NULL) /* variable_transform */
5320 : 286437 : {}
5321 : :
5322 : : /* opt_pass methods: */
5323 : 231569 : 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 : 286437 : make_pass_ipa_fn_summary (gcc::context *ctxt)
5331 : : {
5332 : 286437 : 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 : 257532 : ipa_fnsummary_cc_finalize (void)
5340 : : {
5341 : 257532 : ipa_free_fn_summary ();
5342 : 257532 : ipa_free_size_summary ();
5343 : 257532 : }
|