Branch data Line data Source code
1 : : /* Function summary pass.
2 : : Copyright (C) 2003-2024 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 : 200 : ipa_dump_hints (FILE *f, ipa_hints hints)
106 : : {
107 : 200 : if (!hints)
108 : : return;
109 : 156 : fprintf (f, "IPA hints:");
110 : 156 : if (hints & INLINE_HINT_indirect_call)
111 : : {
112 : 30 : hints &= ~INLINE_HINT_indirect_call;
113 : 30 : fprintf (f, " indirect_call");
114 : : }
115 : 156 : if (hints & INLINE_HINT_loop_iterations)
116 : : {
117 : 3 : hints &= ~INLINE_HINT_loop_iterations;
118 : 3 : fprintf (f, " loop_iterations");
119 : : }
120 : 156 : if (hints & INLINE_HINT_loop_stride)
121 : : {
122 : 3 : hints &= ~INLINE_HINT_loop_stride;
123 : 3 : fprintf (f, " loop_stride");
124 : : }
125 : 156 : if (hints & INLINE_HINT_same_scc)
126 : : {
127 : 4 : hints &= ~INLINE_HINT_same_scc;
128 : 4 : fprintf (f, " same_scc");
129 : : }
130 : 156 : if (hints & INLINE_HINT_in_scc)
131 : : {
132 : 11 : hints &= ~INLINE_HINT_in_scc;
133 : 11 : fprintf (f, " in_scc");
134 : : }
135 : 156 : if (hints & INLINE_HINT_cross_module)
136 : : {
137 : 2 : hints &= ~INLINE_HINT_cross_module;
138 : 2 : fprintf (f, " cross_module");
139 : : }
140 : 156 : if (hints & INLINE_HINT_declared_inline)
141 : : {
142 : 128 : hints &= ~INLINE_HINT_declared_inline;
143 : 128 : fprintf (f, " declared_inline");
144 : : }
145 : 156 : if (hints & INLINE_HINT_known_hot)
146 : : {
147 : 1 : hints &= ~INLINE_HINT_known_hot;
148 : 1 : fprintf (f, " known_hot");
149 : : }
150 : 156 : 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 : 156 : 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 : 122929978 : 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 : 122929978 : size_time_entry *e;
173 : 122929978 : bool found = false;
174 : 122929978 : int i;
175 : 122929978 : ipa_predicate nonconst_pred;
176 : 122929978 : vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table;
177 : :
178 : 122929978 : if (exec_pred == false)
179 : 3306318 : return;
180 : :
181 : 122798938 : nonconst_pred = nonconst_pred_in & exec_pred;
182 : :
183 : 122798938 : 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 : 143304120 : if (!size && time == 0 && table->length ())
189 : 2720692 : return;
190 : :
191 : : /* Only for calls we are unaccounting what we previously recorded. */
192 : 119623660 : gcc_checking_assert (time >= 0 || call);
193 : :
194 : 431594081 : for (i = 0; table->iterate (i, &e); i++)
195 : 405113030 : if (e->exec_predicate == exec_pred
196 : 405113030 : && e->nonconst_predicate == nonconst_pred)
197 : : {
198 : : found = true;
199 : : break;
200 : : }
201 : 119623660 : 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 : 119628168 : if (dump_file && (dump_flags & TDF_DETAILS) && (time != 0 || size))
212 : : {
213 : 4159 : fprintf (dump_file,
214 : : "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate exec:",
215 : 3928 : ((double) size) / ipa_fn_summary::size_scale,
216 : : (time.to_double ()), found ? "" : "new ");
217 : 3928 : exec_pred.dump (dump_file, conds, 0);
218 : 3928 : if (exec_pred != nonconst_pred)
219 : : {
220 : 83 : fprintf (dump_file, " nonconst:");
221 : 83 : nonconst_pred.dump (dump_file, conds);
222 : : }
223 : : else
224 : 3845 : fprintf (dump_file, "\n");
225 : : }
226 : 119623660 : if (!found)
227 : : {
228 : 26481051 : class size_time_entry new_entry;
229 : 26481051 : new_entry.size = size;
230 : 26481051 : new_entry.time = time;
231 : 26481051 : new_entry.exec_predicate = exec_pred;
232 : 26481051 : new_entry.nonconst_predicate = nonconst_pred;
233 : 26481051 : if (call)
234 : 1566041 : call_size_time_table.safe_push (new_entry);
235 : : else
236 : 24915010 : size_time_table.safe_push (new_entry);
237 : : }
238 : : else
239 : : {
240 : 93142609 : e->size += size;
241 : 93142609 : e->time += time;
242 : : /* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */
243 : : /* Tolerate small roundoff issues. */
244 : 93142609 : if (e->time < 0)
245 : 46 : e->time = 0;
246 : : }
247 : : }
248 : :
249 : : /* We proved E to be unreachable, redirect it to __builtin_unreachable. */
250 : :
251 : : static struct cgraph_edge *
252 : 149409 : redirect_to_unreachable (struct cgraph_edge *e)
253 : : {
254 : 149409 : struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
255 : 149409 : struct cgraph_node *target
256 : 149409 : = cgraph_node::get_create (builtin_decl_unreachable ());
257 : :
258 : 149409 : if (e->speculative)
259 : 209 : e = cgraph_edge::resolve_speculation (e, target->decl);
260 : 149200 : else if (!e->callee)
261 : 739 : e = cgraph_edge::make_direct (e, target);
262 : : else
263 : 148461 : e->redirect_callee (target);
264 : 149409 : class ipa_call_summary *es = ipa_call_summaries->get (e);
265 : 149409 : e->inline_failed = CIF_UNREACHABLE;
266 : 149409 : e->count = profile_count::zero ();
267 : 149409 : es->call_stmt_size = 0;
268 : 149409 : es->call_stmt_time = 0;
269 : 149409 : if (callee)
270 : 0 : callee->remove_symbol_and_inline_clones ();
271 : 149409 : return e;
272 : : }
273 : :
274 : : /* Set predicate for edge E. */
275 : :
276 : : static void
277 : 26473792 : edge_set_predicate (struct cgraph_edge *e, ipa_predicate *predicate)
278 : : {
279 : : /* If the edge is determined to be never executed, redirect it
280 : : to BUILTIN_UNREACHABLE to make it clear to IPA passes the call will
281 : : be optimized out. */
282 : 24350107 : if (predicate && *predicate == false
283 : : /* When handling speculative edges, we need to do the redirection
284 : : just once. Do it always on the direct edge, so we do not
285 : : attempt to resolve speculation while duplicating the edge. */
286 : 26623363 : && (!e->speculative || e->callee))
287 : 149409 : e = redirect_to_unreachable (e);
288 : :
289 : 26473792 : class ipa_call_summary *es = ipa_call_summaries->get (e);
290 : 50823899 : if (predicate && *predicate != true)
291 : : {
292 : 3263915 : if (!es->predicate)
293 : 3019998 : es->predicate = edge_predicate_pool.allocate ();
294 : 3263915 : *es->predicate = *predicate;
295 : : }
296 : : else
297 : : {
298 : 23209877 : if (es->predicate)
299 : 409625 : edge_predicate_pool.remove (es->predicate);
300 : 23209877 : es->predicate = NULL;
301 : : }
302 : 26473792 : }
303 : :
304 : : /* Set predicate for hint *P. */
305 : :
306 : : static void
307 : 136900 : set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate)
308 : : {
309 : 136900 : if (new_predicate == false || new_predicate == true)
310 : : {
311 : 1738 : if (*p)
312 : 0 : edge_predicate_pool.remove (*p);
313 : 1738 : *p = NULL;
314 : : }
315 : : else
316 : : {
317 : 135162 : if (!*p)
318 : 135162 : *p = edge_predicate_pool.allocate ();
319 : 135162 : **p = new_predicate;
320 : : }
321 : 136900 : }
322 : :
323 : : /* Find if NEW_PREDICATE is already in V and if so, increment its freq.
324 : : Otherwise add a new item to the vector with this predicate and frerq equal
325 : : to add_freq, unless the number of predicates would exceed MAX_NUM_PREDICATES
326 : : in which case the function does nothing. */
327 : :
328 : : static void
329 : 991209 : add_freqcounting_predicate (vec<ipa_freqcounting_predicate, va_gc> **v,
330 : : const ipa_predicate &new_predicate, sreal add_freq,
331 : : unsigned max_num_predicates)
332 : : {
333 : 991209 : if (new_predicate == false || new_predicate == true)
334 : : return;
335 : : ipa_freqcounting_predicate *f;
336 : 119427 : for (int i = 0; vec_safe_iterate (*v, i, &f); i++)
337 : 60241 : if (new_predicate == f->predicate)
338 : : {
339 : 0 : f->freq += add_freq;
340 : 0 : return;
341 : : }
342 : 80455 : if (vec_safe_length (*v) >= max_num_predicates)
343 : : /* Too many different predicates to account for. */
344 : : return;
345 : :
346 : 58914 : ipa_freqcounting_predicate fcp;
347 : 58914 : fcp.predicate = NULL;
348 : 58914 : set_hint_predicate (&fcp.predicate, new_predicate);
349 : 58914 : fcp.freq = add_freq;
350 : 58914 : vec_safe_push (*v, fcp);
351 : 58914 : return;
352 : : }
353 : :
354 : : /* Compute what conditions may or may not hold given information about
355 : : parameters. RET_CLAUSE returns truths that may hold in a specialized copy,
356 : : while RET_NONSPEC_CLAUSE returns truths that may hold in an nonspecialized
357 : : copy when called in a given context. It is a bitmask of conditions. Bit
358 : : 0 means that condition is known to be false, while bit 1 means that condition
359 : : may or may not be true. These differs - for example NOT_INLINED condition
360 : : is always false in the second and also builtin_constant_p tests cannot use
361 : : the fact that parameter is indeed a constant.
362 : :
363 : : When INLINE_P is true, assume that we are inlining. AVAL contains known
364 : : information about argument values. The function does not modify its content
365 : : and so AVALs could also be of type ipa_call_arg_values but so far all
366 : : callers work with the auto version and so we avoid the conversion for
367 : : convenience.
368 : :
369 : : ERROR_MARK value of an argument means compile time invariant. */
370 : :
371 : : static void
372 : 18299419 : evaluate_conditions_for_known_args (struct cgraph_node *node,
373 : : bool inline_p,
374 : : ipa_auto_call_arg_values *avals,
375 : : clause_t *ret_clause,
376 : : clause_t *ret_nonspec_clause,
377 : : ipa_call_summary *es)
378 : : {
379 : 18299419 : clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
380 : 18299419 : clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition;
381 : 18299419 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
382 : 18299419 : int i;
383 : 18299419 : struct condition *c;
384 : :
385 : 71060808 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
386 : : {
387 : 52761389 : tree val = NULL;
388 : 52761389 : tree res;
389 : 52761389 : int j;
390 : 52761389 : struct expr_eval_op *op;
391 : :
392 : 52761389 : if (c->code == ipa_predicate::not_sra_candidate)
393 : : {
394 : 12768026 : if (!inline_p
395 : 12768026 : || !es
396 : 12592449 : || (int)es->param.length () <= c->operand_num
397 : 19064249 : || !es->param[c->operand_num].points_to_possible_sra_candidate)
398 : 9685448 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
399 : 12768026 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
400 : 52761389 : continue;
401 : : }
402 : :
403 : 39993363 : if (c->agg_contents)
404 : : {
405 : 19071038 : if (c->code == ipa_predicate::changed
406 : 19066701 : && !c->by_ref
407 : 19066701 : && (avals->safe_sval_at(c->operand_num) == error_mark_node))
408 : 4337 : continue;
409 : :
410 : 19062364 : if (tree sval = avals->safe_sval_at (c->operand_num))
411 : 8678382 : val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref);
412 : 8678382 : if (!val)
413 : : {
414 : 19053120 : ipa_argagg_value_list avs (avals);
415 : 19053120 : val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
416 : 19053120 : c->by_ref);
417 : : }
418 : : }
419 : : else
420 : : {
421 : 20926662 : val = avals->safe_sval_at (c->operand_num);
422 : 20926662 : if (val && val == error_mark_node
423 : 1734286 : && c->code != ipa_predicate::changed)
424 : : val = NULL_TREE;
425 : : }
426 : :
427 : 29523688 : if (!val
428 : 28621281 : && (c->code == ipa_predicate::changed
429 : : || c->code == ipa_predicate::is_not_constant))
430 : : {
431 : 19961075 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
432 : 19961075 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
433 : 19961075 : continue;
434 : : }
435 : 20027951 : if (c->code == ipa_predicate::changed)
436 : : {
437 : 6155243 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
438 : 6155243 : continue;
439 : : }
440 : :
441 : 13872708 : if (c->code == ipa_predicate::is_not_constant)
442 : : {
443 : 5166 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
444 : 5166 : continue;
445 : : }
446 : :
447 : 13867542 : if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val)))
448 : : {
449 : 4986832 : if (c->type != TREE_TYPE (val))
450 : 979100 : val = fold_unary (VIEW_CONVERT_EXPR, c->type, val);
451 : 5249926 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
452 : : {
453 : 264122 : if (!val)
454 : : break;
455 : 263094 : if (!op->val[0])
456 : 96312 : val = fold_unary (op->code, op->type, val);
457 : 166782 : else if (!op->val[1])
458 : 333564 : val = fold_binary (op->code, op->type,
459 : : op->index ? op->val[0] : val,
460 : : op->index ? val : op->val[0]);
461 : 0 : else if (op->index == 0)
462 : 0 : val = fold_ternary (op->code, op->type,
463 : : val, op->val[0], op->val[1]);
464 : 0 : else if (op->index == 1)
465 : 0 : val = fold_ternary (op->code, op->type,
466 : : op->val[0], val, op->val[1]);
467 : 0 : else if (op->index == 2)
468 : 0 : val = fold_ternary (op->code, op->type,
469 : : op->val[0], op->val[1], val);
470 : : else
471 : : val = NULL_TREE;
472 : : }
473 : :
474 : 12421402 : res = val
475 : 4986832 : ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
476 : : : NULL;
477 : :
478 : 4985784 : if (res && integer_zerop (res))
479 : 2538046 : continue;
480 : 2448786 : if (res && integer_onep (res))
481 : : {
482 : 2431240 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
483 : 2431240 : nonspec_clause
484 : 2431240 : |= 1 << (i + ipa_predicate::first_dynamic_condition);
485 : 2431240 : continue;
486 : : }
487 : : }
488 : 8898256 : if (c->operand_num < (int) avals->m_known_value_ranges.length ()
489 : 4466702 : && !c->agg_contents
490 : 10471114 : && (!val || TREE_CODE (val) != INTEGER_CST))
491 : : {
492 : 1439675 : Value_Range vr (avals->m_known_value_ranges[c->operand_num]);
493 : 1439675 : if (!vr.undefined_p ()
494 : 784096 : && !vr.varying_p ()
495 : 2223771 : && (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ())))
496 : : {
497 : 784096 : if (!useless_type_conversion_p (c->type, vr.type ()))
498 : 0 : range_cast (vr, c->type);
499 : :
500 : 1000792 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
501 : : {
502 : 256856 : if (vr.varying_p () || vr.undefined_p ())
503 : : break;
504 : :
505 : 216696 : Value_Range res (op->type);
506 : 216696 : if (!op->val[0])
507 : : {
508 : 80044 : Value_Range varying (op->type);
509 : 80044 : varying.set_varying (op->type);
510 : 80044 : range_op_handler handler (op->code);
511 : 80044 : if (!handler
512 : 80036 : || !res.supports_type_p (op->type)
513 : 160080 : || !handler.fold_range (res, op->type, vr, varying))
514 : 8 : res.set_varying (op->type);
515 : 80044 : }
516 : 136652 : else if (!op->val[1])
517 : : {
518 : 136480 : Value_Range op0 (op->type);
519 : 136480 : range_op_handler handler (op->code);
520 : :
521 : 136480 : ipa_range_set_and_normalize (op0, op->val[0]);
522 : :
523 : 136480 : if (!handler
524 : 136480 : || !res.supports_type_p (op->type)
525 : 407990 : || !handler.fold_range (res, op->type,
526 : : op->index ? op0 : vr,
527 : 136480 : op->index ? vr : op0))
528 : 0 : res.set_varying (op->type);
529 : 136480 : }
530 : : else
531 : 172 : res.set_varying (op->type);
532 : 216696 : vr = res;
533 : 216696 : }
534 : 784096 : if (!vr.varying_p () && !vr.undefined_p ())
535 : : {
536 : 739480 : int_range<2> res;
537 : 739480 : Value_Range val_vr (TREE_TYPE (c->val));
538 : 739480 : range_op_handler handler (c->code);
539 : :
540 : 739480 : ipa_range_set_and_normalize (val_vr, c->val);
541 : :
542 : 739480 : if (!handler
543 : 739480 : || !val_vr.supports_type_p (TREE_TYPE (c->val))
544 : 1478960 : || !handler.fold_range (res, boolean_type_node, vr, val_vr))
545 : 0 : res.set_varying (boolean_type_node);
546 : :
547 : 739480 : if (res.zero_p ())
548 : 158088 : continue;
549 : 739480 : }
550 : : }
551 : 1439675 : }
552 : :
553 : 8740168 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
554 : 8740168 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
555 : : }
556 : 18299419 : *ret_clause = clause;
557 : 18299419 : if (ret_nonspec_clause)
558 : 15971039 : *ret_nonspec_clause = nonspec_clause;
559 : 18299419 : }
560 : :
561 : : /* Return true if VRP will be exectued on the function.
562 : : We do not want to anticipate optimizations that will not happen.
563 : :
564 : : FIXME: This can be confused with -fdisable and debug counters and thus
565 : : it should not be used for correctness (only to make heuristics work).
566 : : This means that inliner should do its own optimizations of expressions
567 : : that it predicts to be constant so wrong code can not be triggered by
568 : : builtin_constant_p. */
569 : :
570 : : static bool
571 : 9531513 : vrp_will_run_p (struct cgraph_node *node)
572 : : {
573 : 9531513 : return (opt_for_fn (node->decl, optimize)
574 : 9531513 : && !opt_for_fn (node->decl, optimize_debug)
575 : 19061752 : && opt_for_fn (node->decl, flag_tree_vrp));
576 : : }
577 : :
578 : : /* Similarly about FRE. */
579 : :
580 : : static bool
581 : 11765802 : fre_will_run_p (struct cgraph_node *node)
582 : : {
583 : 11765802 : return (opt_for_fn (node->decl, optimize)
584 : 11765802 : && !opt_for_fn (node->decl, optimize_debug)
585 : 23529263 : && opt_for_fn (node->decl, flag_tree_fre));
586 : : }
587 : :
588 : : /* Work out what conditions might be true at invocation of E.
589 : : Compute costs for inlined edge if INLINE_P is true.
590 : :
591 : : Return in CLAUSE_PTR the evaluated conditions and in NONSPEC_CLAUSE_PTR
592 : : (if non-NULL) conditions evaluated for nonspecialized clone called
593 : : in a given context.
594 : :
595 : : Vectors in AVALS will be populated with useful known information about
596 : : argument values - information not known to have any uses will be omitted -
597 : : except for m_known_contexts which will only be calculated if
598 : : COMPUTE_CONTEXTS is true. */
599 : :
600 : : void
601 : 18136120 : evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
602 : : clause_t *clause_ptr,
603 : : clause_t *nonspec_clause_ptr,
604 : : ipa_auto_call_arg_values *avals,
605 : : bool compute_contexts)
606 : : {
607 : 18136120 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
608 : 18136120 : class ipa_fn_summary *info = ipa_fn_summaries->get (callee);
609 : 18136120 : class ipa_edge_args *args;
610 : 18136120 : class ipa_call_summary *es = NULL;
611 : :
612 : 18136120 : if (clause_ptr)
613 : 18136120 : *clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
614 : :
615 : 18136120 : if (ipa_node_params_sum
616 : 8278095 : && !e->call_stmt_cannot_inline_p
617 : 8278095 : && (info->conds || compute_contexts)
618 : 26414215 : && (args = ipa_edge_args_sum->get (e)) != NULL)
619 : : {
620 : 8226453 : struct cgraph_node *caller;
621 : 8226453 : class ipa_node_params *caller_parms_info, *callee_pi = NULL;
622 : 8226453 : int i, count = ipa_get_cs_argument_count (args);
623 : 8226453 : es = ipa_call_summaries->get (e);
624 : :
625 : 8226453 : if (count)
626 : : {
627 : 7613993 : if (e->caller->inlined_to)
628 : : caller = e->caller->inlined_to;
629 : : else
630 : 6075049 : caller = e->caller;
631 : 7613993 : caller_parms_info = ipa_node_params_sum->get (caller);
632 : 7613993 : callee_pi = ipa_node_params_sum->get (callee);
633 : :
634 : : /* Watch for thunks. */
635 : 7613993 : if (callee_pi)
636 : : /* Watch for variadic functions. */
637 : 15226312 : count = MIN (count, ipa_get_param_count (callee_pi));
638 : : }
639 : :
640 : 8226453 : if (callee_pi)
641 : 24947353 : for (i = 0; i < count; i++)
642 : : {
643 : 17333700 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
644 : :
645 : 17333700 : if (ipa_is_param_used_by_indirect_call (callee_pi, i)
646 : 17333700 : || ipa_is_param_used_by_ipa_predicates (callee_pi, i))
647 : : {
648 : : /* Determine if we know constant value of the parameter. */
649 : 11765802 : tree type = ipa_get_type (callee_pi, i);
650 : 11765802 : tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type);
651 : :
652 : 8923936 : if (!cst && e->call_stmt
653 : 20655659 : && i < (int)gimple_call_num_args (e->call_stmt))
654 : : {
655 : 8889857 : cst = gimple_call_arg (e->call_stmt, i);
656 : 8889857 : if (!is_gimple_min_invariant (cst))
657 : : cst = NULL;
658 : : }
659 : 3005612 : if (cst)
660 : : {
661 : 2971533 : gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
662 : 2971533 : if (!avals->m_known_vals.length ())
663 : 1574277 : avals->m_known_vals.safe_grow_cleared (count, true);
664 : 2971533 : avals->m_known_vals[i] = cst;
665 : : }
666 : 8794269 : else if (inline_p && !es->param[i].change_prob)
667 : : {
668 : 3114588 : if (!avals->m_known_vals.length ())
669 : 2681645 : avals->m_known_vals.safe_grow_cleared (count, true);
670 : 3114588 : avals->m_known_vals[i] = error_mark_node;
671 : : }
672 : :
673 : : /* If we failed to get simple constant, try value range. */
674 : 2971533 : if ((!cst || TREE_CODE (cst) != INTEGER_CST)
675 : 9531513 : && vrp_will_run_p (caller)
676 : 21159563 : && ipa_is_param_used_by_ipa_predicates (callee_pi, i))
677 : : {
678 : 9363594 : Value_Range vr (type);
679 : :
680 : 9363594 : ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type);
681 : 9363594 : if (!vr.undefined_p () && !vr.varying_p ())
682 : : {
683 : 6132781 : if (!avals->m_known_value_ranges.length ())
684 : 4642736 : avals->m_known_value_ranges.safe_grow_cleared (count,
685 : : true);
686 : 6132781 : avals->m_known_value_ranges[i] = vr;
687 : : }
688 : 9363594 : }
689 : :
690 : : /* Determine known aggregate values. */
691 : 11765802 : if (fre_will_run_p (caller))
692 : 11763111 : ipa_push_agg_values_from_jfunc (caller_parms_info,
693 : : caller, &jf->agg, i,
694 : : &avals->m_known_aggs);
695 : : }
696 : :
697 : : /* For calls used in polymorphic calls we further determine
698 : : polymorphic call context. */
699 : 17333700 : if (compute_contexts
700 : 17333700 : && ipa_is_param_used_by_polymorphic_call (callee_pi, i))
701 : : {
702 : 73460 : ipa_polymorphic_call_context
703 : 73460 : ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
704 : 146920 : if (!ctx.useless_p ())
705 : : {
706 : 72580 : if (!avals->m_known_contexts.length ())
707 : 72435 : avals->m_known_contexts.safe_grow_cleared (count, true);
708 : 72580 : avals->m_known_contexts[i]
709 : 145160 : = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
710 : : }
711 : : }
712 : : }
713 : : else
714 : 612800 : gcc_assert (!count || callee->thunk);
715 : : }
716 : 9909667 : else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds)
717 : : {
718 : 7671718 : int i, count = (int)gimple_call_num_args (e->call_stmt);
719 : :
720 : 21788619 : for (i = 0; i < count; i++)
721 : : {
722 : 14116901 : tree cst = gimple_call_arg (e->call_stmt, i);
723 : 14116901 : if (!is_gimple_min_invariant (cst))
724 : : cst = NULL;
725 : 5033216 : if (cst)
726 : : {
727 : 5033216 : if (!avals->m_known_vals.length ())
728 : 3463263 : avals->m_known_vals.safe_grow_cleared (count, true);
729 : 5033216 : avals->m_known_vals[i] = cst;
730 : : }
731 : : }
732 : : }
733 : :
734 : 18136120 : evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr,
735 : : nonspec_clause_ptr, es);
736 : 18136120 : }
737 : :
738 : :
739 : : /* Allocate the function summary. */
740 : :
741 : : static void
742 : 454700 : ipa_fn_summary_alloc (void)
743 : : {
744 : 454700 : gcc_checking_assert (!ipa_fn_summaries);
745 : 454700 : ipa_size_summaries = new ipa_size_summary_t (symtab);
746 : 454700 : ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab);
747 : 454700 : ipa_call_summaries = new ipa_call_summary_t (symtab);
748 : 454700 : }
749 : :
750 : 23779346 : ipa_call_summary::~ipa_call_summary ()
751 : : {
752 : 23779346 : if (predicate)
753 : 2610373 : edge_predicate_pool.remove (predicate);
754 : :
755 : 23779346 : param.release ();
756 : 23779346 : }
757 : :
758 : 8788495 : ipa_fn_summary::~ipa_fn_summary ()
759 : : {
760 : 8788495 : unsigned len = vec_safe_length (loop_iterations);
761 : 8889297 : for (unsigned i = 0; i < len; i++)
762 : 100802 : edge_predicate_pool.remove ((*loop_iterations)[i].predicate);
763 : 8788495 : len = vec_safe_length (loop_strides);
764 : 8822855 : for (unsigned i = 0; i < len; i++)
765 : 34360 : edge_predicate_pool.remove ((*loop_strides)[i].predicate);
766 : 8788495 : vec_free (conds);
767 : 8788495 : call_size_time_table.release ();
768 : 8788495 : vec_free (loop_iterations);
769 : 8788495 : vec_free (loop_strides);
770 : 8788495 : builtin_constant_p_parms.release ();
771 : 8788495 : }
772 : :
773 : : void
774 : 6568248 : ipa_fn_summary_t::remove_callees (cgraph_node *node)
775 : : {
776 : 6568248 : cgraph_edge *e;
777 : 26798375 : for (e = node->callees; e; e = e->next_callee)
778 : 20230127 : ipa_call_summaries->remove (e);
779 : 7034982 : for (e = node->indirect_calls; e; e = e->next_callee)
780 : 466734 : ipa_call_summaries->remove (e);
781 : 6568248 : }
782 : :
783 : : /* Duplicate predicates in loop hint vector, allocating memory for them and
784 : : remove and deallocate any uninteresting (true or false) ones. Return the
785 : : result. */
786 : :
787 : : static vec<ipa_freqcounting_predicate, va_gc> *
788 : 23566 : remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v,
789 : : clause_t possible_truths)
790 : : {
791 : 23566 : if (vec_safe_length (v) == 0)
792 : : return NULL;
793 : :
794 : 2804 : vec<ipa_freqcounting_predicate, va_gc> *res = v->copy ();
795 : 2804 : int len = res->length();
796 : 6515 : for (int i = len - 1; i >= 0; i--)
797 : : {
798 : 3711 : ipa_predicate new_predicate
799 : 3711 : = (*res)[i].predicate->remap_after_duplication (possible_truths);
800 : : /* We do not want to free previous predicate; it is used by node
801 : : origin. */
802 : 3711 : (*res)[i].predicate = NULL;
803 : 3711 : set_hint_predicate (&(*res)[i].predicate, new_predicate);
804 : :
805 : 3711 : if (!(*res)[i].predicate)
806 : 1738 : res->unordered_remove (i);
807 : : }
808 : :
809 : : return res;
810 : : }
811 : :
812 : :
813 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
814 : : void
815 : 2135201 : ipa_fn_summary_t::duplicate (cgraph_node *src,
816 : : cgraph_node *dst,
817 : : ipa_fn_summary *src_info,
818 : : ipa_fn_summary *info)
819 : : {
820 : 2135201 : new (info) ipa_fn_summary (*src_info);
821 : : /* TODO: as an optimization, we may avoid copying conditions
822 : : that are known to be false or true. */
823 : 2135201 : info->conds = vec_safe_copy (info->conds);
824 : :
825 : 2135201 : clone_info *cinfo = clone_info::get (dst);
826 : : /* When there are any replacements in the function body, see if we can figure
827 : : out that something was optimized out. */
828 : 2135201 : if (ipa_node_params_sum && cinfo && cinfo->tree_map)
829 : : {
830 : : /* Use SRC parm info since it may not be copied yet. */
831 : 11783 : ipa_node_params *parms_info = ipa_node_params_sum->get (src);
832 : 11783 : ipa_auto_call_arg_values avals;
833 : 11783 : int count = ipa_get_param_count (parms_info);
834 : 11783 : int i, j;
835 : 11783 : clause_t possible_truths;
836 : 11783 : ipa_predicate true_pred = true;
837 : 11783 : size_time_entry *e;
838 : 11783 : int optimized_out_size = 0;
839 : 11783 : bool inlined_to_p = false;
840 : 11783 : struct cgraph_edge *edge, *next;
841 : :
842 : 11783 : info->size_time_table.release ();
843 : 11783 : avals.m_known_vals.safe_grow_cleared (count, true);
844 : 47633 : for (i = 0; i < count; i++)
845 : : {
846 : : struct ipa_replace_map *r;
847 : :
848 : 116643 : for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++)
849 : : {
850 : 64051 : if (r->parm_num == i)
851 : : {
852 : 19108 : avals.m_known_vals[i] = r->new_tree;
853 : 19108 : break;
854 : : }
855 : : }
856 : : }
857 : 11783 : evaluate_conditions_for_known_args (dst, false,
858 : : &avals,
859 : : &possible_truths,
860 : : /* We are going to specialize,
861 : : so ignore nonspec truths. */
862 : : NULL,
863 : : NULL);
864 : :
865 : 11783 : info->account_size_time (0, 0, true_pred, true_pred);
866 : :
867 : : /* Remap size_time vectors.
868 : : Simplify the predicate by pruning out alternatives that are known
869 : : to be false.
870 : : TODO: as on optimization, we can also eliminate conditions known
871 : : to be true. */
872 : 80972 : for (i = 0; src_info->size_time_table.iterate (i, &e); i++)
873 : : {
874 : 69189 : ipa_predicate new_exec_pred;
875 : 69189 : ipa_predicate new_nonconst_pred;
876 : 69189 : new_exec_pred = e->exec_predicate.remap_after_duplication
877 : 69189 : (possible_truths);
878 : 69189 : new_nonconst_pred = e->nonconst_predicate.remap_after_duplication
879 : 69189 : (possible_truths);
880 : 69189 : if (new_exec_pred == false || new_nonconst_pred == false)
881 : 7913 : optimized_out_size += e->size;
882 : : else
883 : 61276 : info->account_size_time (e->size, e->time, new_exec_pred,
884 : : new_nonconst_pred);
885 : : }
886 : :
887 : : /* Remap edge predicates with the same simplification as above.
888 : : Also copy constantness arrays. */
889 : 178785 : for (edge = dst->callees; edge; edge = next)
890 : : {
891 : 167002 : ipa_predicate new_predicate;
892 : 167002 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
893 : 167002 : next = edge->next_callee;
894 : :
895 : 167002 : if (!edge->inline_failed)
896 : 0 : inlined_to_p = true;
897 : 167002 : if (!es->predicate)
898 : 155788 : continue;
899 : 11214 : new_predicate = es->predicate->remap_after_duplication
900 : 11214 : (possible_truths);
901 : 11214 : if (new_predicate == false && *es->predicate != false)
902 : 1948 : optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale;
903 : 11214 : edge_set_predicate (edge, &new_predicate);
904 : : }
905 : :
906 : : /* Remap indirect edge predicates with the same simplification as above.
907 : : Also copy constantness arrays. */
908 : 12602 : for (edge = dst->indirect_calls; edge; edge = next)
909 : : {
910 : 819 : ipa_predicate new_predicate;
911 : 819 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
912 : 819 : next = edge->next_callee;
913 : :
914 : 819 : gcc_checking_assert (edge->inline_failed);
915 : 819 : if (!es->predicate)
916 : 720 : continue;
917 : 99 : new_predicate = es->predicate->remap_after_duplication
918 : 99 : (possible_truths);
919 : 99 : if (new_predicate == false && *es->predicate != false)
920 : 19 : optimized_out_size
921 : 19 : += es->call_stmt_size * ipa_fn_summary::size_scale;
922 : 99 : edge_set_predicate (edge, &new_predicate);
923 : : }
924 : 11783 : info->loop_iterations
925 : 11783 : = remap_freqcounting_preds_after_dup (info->loop_iterations,
926 : : possible_truths);
927 : 11783 : info->loop_strides
928 : 11783 : = remap_freqcounting_preds_after_dup (info->loop_strides,
929 : : possible_truths);
930 : 11783 : if (info->builtin_constant_p_parms.length())
931 : : {
932 : 25 : vec <int, va_heap, vl_ptr> parms = info->builtin_constant_p_parms;
933 : 25 : int ip;
934 : 25 : info->builtin_constant_p_parms = vNULL;
935 : 50 : for (i = 0; parms.iterate (i, &ip); i++)
936 : 25 : if (!avals.m_known_vals[ip])
937 : 4 : info->builtin_constant_p_parms.safe_push (ip);
938 : : }
939 : :
940 : : /* If inliner or someone after inliner will ever start producing
941 : : non-trivial clones, we will get trouble with lack of information
942 : : about updating self sizes, because size vectors already contains
943 : : sizes of the callees. */
944 : 11783 : gcc_assert (!inlined_to_p || !optimized_out_size);
945 : 11783 : }
946 : : else
947 : : {
948 : 2123418 : info->size_time_table = src_info->size_time_table.copy ();
949 : 2123418 : info->loop_iterations = vec_safe_copy (src_info->loop_iterations);
950 : 2123418 : info->loop_strides = vec_safe_copy (info->loop_strides);
951 : :
952 : 2123418 : info->builtin_constant_p_parms
953 : 2123418 : = info->builtin_constant_p_parms.copy ();
954 : :
955 : 2123418 : ipa_freqcounting_predicate *f;
956 : 2169533 : for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++)
957 : : {
958 : 46115 : ipa_predicate p = *f->predicate;
959 : 46115 : f->predicate = NULL;
960 : 46115 : set_hint_predicate (&f->predicate, p);
961 : : }
962 : 2150112 : for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++)
963 : : {
964 : 26694 : ipa_predicate p = *f->predicate;
965 : 26694 : f->predicate = NULL;
966 : 26694 : set_hint_predicate (&f->predicate, p);
967 : : }
968 : : }
969 : 2135201 : if (!dst->inlined_to)
970 : 109360 : ipa_update_overall_fn_summary (dst);
971 : 2135201 : }
972 : :
973 : :
974 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
975 : :
976 : : void
977 : 2721752 : ipa_call_summary_t::duplicate (struct cgraph_edge *src,
978 : : struct cgraph_edge *dst,
979 : : class ipa_call_summary *srcinfo,
980 : : class ipa_call_summary *info)
981 : : {
982 : 2721752 : new (info) ipa_call_summary (*srcinfo);
983 : 2721752 : info->predicate = NULL;
984 : 2721752 : edge_set_predicate (dst, srcinfo->predicate);
985 : 2721752 : info->param = srcinfo->param.copy ();
986 : 2721752 : if (!dst->indirect_unknown_callee && src->indirect_unknown_callee)
987 : : {
988 : 10709 : info->call_stmt_size -= (eni_size_weights.indirect_call_cost
989 : 10709 : - eni_size_weights.call_cost);
990 : 10709 : info->call_stmt_time -= (eni_time_weights.indirect_call_cost
991 : 10709 : - eni_time_weights.call_cost);
992 : : }
993 : 2721752 : }
994 : :
995 : : /* Dump edge summaries associated to NODE and recursively to all clones.
996 : : Indent by INDENT. */
997 : :
998 : : static void
999 : 2297 : dump_ipa_call_summary (FILE *f, int indent, struct cgraph_node *node,
1000 : : class ipa_fn_summary *info)
1001 : : {
1002 : 2297 : struct cgraph_edge *edge;
1003 : 10306 : for (edge = node->callees; edge; edge = edge->next_callee)
1004 : : {
1005 : 8009 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1006 : 8009 : struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1007 : 8009 : int i;
1008 : :
1009 : 16018 : fprintf (f,
1010 : : "%*s%s %s\n%*s freq:%4.2f",
1011 : : indent, "", callee->dump_name (),
1012 : 8009 : !edge->inline_failed
1013 : 7387 : ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1014 : 8009 : indent, "", edge->sreal_frequency ().to_double ());
1015 : :
1016 : 8009 : if (cross_module_call_p (edge))
1017 : 4 : fprintf (f, " cross module");
1018 : :
1019 : 8009 : if (es)
1020 : 7387 : fprintf (f, " loop depth:%2i size:%2i time: %2i",
1021 : : es->loop_depth, es->call_stmt_size, es->call_stmt_time);
1022 : :
1023 : 8009 : ipa_fn_summary *s = ipa_fn_summaries->get (callee);
1024 : 8009 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1025 : 8009 : if (s != NULL)
1026 : 607 : fprintf (f, " callee size:%2i stack:%2i",
1027 : 607 : (int) (ss->size / ipa_fn_summary::size_scale),
1028 : 607 : (int) s->estimated_stack_size);
1029 : :
1030 : 8009 : if (es && es->predicate)
1031 : : {
1032 : 4638 : fprintf (f, " predicate: ");
1033 : 4638 : es->predicate->dump (f, info->conds);
1034 : : }
1035 : : else
1036 : 3371 : fprintf (f, "\n");
1037 : 8009 : if (es && es->param.exists ())
1038 : 5662 : for (i = 0; i < (int) es->param.length (); i++)
1039 : : {
1040 : 1669 : int prob = es->param[i].change_prob;
1041 : :
1042 : 1669 : if (!prob)
1043 : 708 : fprintf (f, "%*s op%i is compile time invariant\n",
1044 : : indent + 2, "", i);
1045 : 961 : else if (prob != REG_BR_PROB_BASE)
1046 : 40 : fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1047 : 40 : prob * 100.0 / REG_BR_PROB_BASE);
1048 : 1669 : if (es->param[i].points_to_local_or_readonly_memory)
1049 : 443 : fprintf (f, "%*s op%i points to local or readonly memory\n",
1050 : : indent + 2, "", i);
1051 : 1669 : if (es->param[i].points_to_possible_sra_candidate)
1052 : 278 : fprintf (f, "%*s op%i points to possible sra candidate\n",
1053 : : indent + 2, "", i);
1054 : : }
1055 : 8009 : if (!edge->inline_failed)
1056 : : {
1057 : 622 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1058 : 622 : fprintf (f, "%*sStack frame offset %i, callee self size %i\n",
1059 : : indent + 2, "",
1060 : 622 : (int) ipa_get_stack_frame_offset (callee),
1061 : 622 : (int) ss->estimated_self_stack_size);
1062 : 622 : dump_ipa_call_summary (f, indent + 2, callee, info);
1063 : : }
1064 : : }
1065 : 2648 : for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1066 : : {
1067 : 351 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1068 : 351 : fprintf (f, "%*sindirect call loop depth:%2i freq:%4.2f size:%2i"
1069 : : " time: %2i",
1070 : : indent, "",
1071 : : es->loop_depth,
1072 : 351 : edge->sreal_frequency ().to_double (), es->call_stmt_size,
1073 : : es->call_stmt_time);
1074 : 351 : if (es->predicate)
1075 : : {
1076 : 8 : fprintf (f, "predicate: ");
1077 : 8 : es->predicate->dump (f, info->conds);
1078 : : }
1079 : : else
1080 : 343 : fprintf (f, "\n");
1081 : : }
1082 : 2297 : }
1083 : :
1084 : :
1085 : : void
1086 : 1924 : ipa_dump_fn_summary (FILE *f, struct cgraph_node *node)
1087 : : {
1088 : 1924 : if (node->definition)
1089 : : {
1090 : 1924 : class ipa_fn_summary *s = ipa_fn_summaries->get (node);
1091 : 1924 : class ipa_size_summary *ss = ipa_size_summaries->get (node);
1092 : 1924 : if (s != NULL)
1093 : : {
1094 : 1675 : size_time_entry *e;
1095 : 1675 : int i;
1096 : 1675 : fprintf (f, "IPA function summary for %s", node->dump_name ());
1097 : 1675 : if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1098 : 0 : fprintf (f, " always_inline");
1099 : 1675 : if (s->inlinable)
1100 : 1484 : fprintf (f, " inlinable");
1101 : 1675 : if (s->fp_expressions)
1102 : 52 : fprintf (f, " fp_expression");
1103 : 1675 : if (s->builtin_constant_p_parms.length ())
1104 : : {
1105 : 2 : fprintf (f, " builtin_constant_p_parms");
1106 : 4 : for (unsigned int i = 0;
1107 : 8 : i < s->builtin_constant_p_parms.length (); i++)
1108 : 2 : fprintf (f, " %i", s->builtin_constant_p_parms[i]);
1109 : : }
1110 : 1675 : fprintf (f, "\n global time: %f\n", s->time.to_double ());
1111 : 1675 : fprintf (f, " self size: %i\n", ss->self_size);
1112 : 1675 : fprintf (f, " global size: %i\n", ss->size);
1113 : 1675 : fprintf (f, " min size: %i\n", s->min_size);
1114 : 1675 : fprintf (f, " self stack: %i\n",
1115 : 1675 : (int) ss->estimated_self_stack_size);
1116 : 1675 : fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1117 : 1675 : if (s->growth)
1118 : 99 : fprintf (f, " estimated growth:%i\n", (int) s->growth);
1119 : 1675 : if (s->scc_no)
1120 : 6 : fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1121 : 6999 : for (i = 0; s->size_time_table.iterate (i, &e); i++)
1122 : : {
1123 : 5324 : fprintf (f, " size:%f, time:%f",
1124 : 5324 : (double) e->size / ipa_fn_summary::size_scale,
1125 : : e->time.to_double ());
1126 : 5324 : if (e->exec_predicate != true)
1127 : : {
1128 : 3114 : fprintf (f, ", executed if:");
1129 : 3114 : e->exec_predicate.dump (f, s->conds, 0);
1130 : : }
1131 : 5324 : if (e->exec_predicate != e->nonconst_predicate)
1132 : : {
1133 : 1294 : fprintf (f, ", nonconst if:");
1134 : 1294 : e->nonconst_predicate.dump (f, s->conds, 0);
1135 : : }
1136 : 5324 : fprintf (f, "\n");
1137 : : }
1138 : : ipa_freqcounting_predicate *fcp;
1139 : : bool first_fcp = true;
1140 : 1787 : for (int i = 0; vec_safe_iterate (s->loop_iterations, i, &fcp); i++)
1141 : : {
1142 : 112 : if (first_fcp)
1143 : : {
1144 : 84 : fprintf (f, " loop iterations:");
1145 : 84 : first_fcp = false;
1146 : : }
1147 : 112 : fprintf (f, " %3.2f for ", fcp->freq.to_double ());
1148 : 112 : fcp->predicate->dump (f, s->conds);
1149 : : }
1150 : : first_fcp = true;
1151 : 1693 : for (int i = 0; vec_safe_iterate (s->loop_strides, i, &fcp); i++)
1152 : : {
1153 : 18 : if (first_fcp)
1154 : : {
1155 : 10 : fprintf (f, " loop strides:");
1156 : 10 : first_fcp = false;
1157 : : }
1158 : 18 : fprintf (f, " %3.2f for :", fcp->freq.to_double ());
1159 : 18 : fcp->predicate->dump (f, s->conds);
1160 : : }
1161 : 1675 : fprintf (f, " calls:\n");
1162 : 1675 : dump_ipa_call_summary (f, 4, node, s);
1163 : 1675 : fprintf (f, "\n");
1164 : 1675 : if (s->target_info)
1165 : 0 : fprintf (f, " target_info: %x\n", s->target_info);
1166 : : }
1167 : : else
1168 : 249 : fprintf (f, "IPA summary for %s is missing.\n", node->dump_name ());
1169 : : }
1170 : 1924 : }
1171 : :
1172 : : DEBUG_FUNCTION void
1173 : 0 : ipa_debug_fn_summary (struct cgraph_node *node)
1174 : : {
1175 : 0 : ipa_dump_fn_summary (stderr, node);
1176 : 0 : }
1177 : :
1178 : : void
1179 : 416 : ipa_dump_fn_summaries (FILE *f)
1180 : : {
1181 : 416 : struct cgraph_node *node;
1182 : :
1183 : 2725 : FOR_EACH_DEFINED_FUNCTION (node)
1184 : 2309 : if (!node->inlined_to)
1185 : 1687 : ipa_dump_fn_summary (f, node);
1186 : 416 : }
1187 : :
1188 : : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1189 : : boolean variable pointed to by DATA. */
1190 : :
1191 : : static bool
1192 : 635993 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1193 : : void *data)
1194 : : {
1195 : 635993 : bool *b = (bool *) data;
1196 : 635993 : *b = true;
1197 : 635993 : return true;
1198 : : }
1199 : :
1200 : : /* If OP refers to value of function parameter, return the corresponding
1201 : : parameter. If non-NULL, the size of the memory load (or the SSA_NAME of the
1202 : : PARM_DECL) will be stored to *SIZE_P in that case too. */
1203 : :
1204 : : static tree
1205 : 168096619 : unmodified_parm_1 (ipa_func_body_info *fbi, gimple *stmt, tree op,
1206 : : poly_int64 *size_p)
1207 : : {
1208 : : /* SSA_NAME referring to parm default def? */
1209 : 168096619 : if (TREE_CODE (op) == SSA_NAME
1210 : 98183778 : && SSA_NAME_IS_DEFAULT_DEF (op)
1211 : 190841403 : && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1212 : : {
1213 : 22595015 : if (size_p)
1214 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1215 : 22595015 : return SSA_NAME_VAR (op);
1216 : : }
1217 : : /* Non-SSA parm reference? */
1218 : 145501604 : if (TREE_CODE (op) == PARM_DECL
1219 : 1238102 : && fbi->aa_walk_budget > 0)
1220 : : {
1221 : 1235546 : bool modified = false;
1222 : :
1223 : 1235546 : ao_ref refd;
1224 : 1235546 : ao_ref_init (&refd, op);
1225 : 2471092 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
1226 : : mark_modified, &modified, NULL, NULL,
1227 : : fbi->aa_walk_budget);
1228 : 1235546 : if (walked < 0)
1229 : : {
1230 : 8 : fbi->aa_walk_budget = 0;
1231 : 828269 : return NULL_TREE;
1232 : : }
1233 : 1235538 : fbi->aa_walk_budget -= walked;
1234 : 1235538 : if (!modified)
1235 : : {
1236 : 828261 : if (size_p)
1237 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1238 : 828261 : return op;
1239 : : }
1240 : : }
1241 : : return NULL_TREE;
1242 : : }
1243 : :
1244 : : /* If OP refers to value of function parameter, return the corresponding
1245 : : parameter. Also traverse chains of SSA register assignments. If non-NULL,
1246 : : the size of the memory load (or the SSA_NAME of the PARM_DECL) will be
1247 : : stored to *SIZE_P in that case too. */
1248 : :
1249 : : static tree
1250 : 109447606 : unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op,
1251 : : poly_int64 *size_p)
1252 : : {
1253 : 136663795 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1254 : 136663795 : if (res)
1255 : : return res;
1256 : :
1257 : 113950093 : if (TREE_CODE (op) == SSA_NAME
1258 : 64817063 : && !SSA_NAME_IS_DEFAULT_DEF (op)
1259 : 178621767 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1260 : 27216189 : return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op),
1261 : 27216189 : gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)),
1262 : 27216189 : size_p);
1263 : : return NULL_TREE;
1264 : : }
1265 : :
1266 : : /* If OP refers to a value of a function parameter or value loaded from an
1267 : : aggregate passed to a parameter (either by value or reference), return TRUE
1268 : : and store the number of the parameter to *INDEX_P, the access size into
1269 : : *SIZE_P, and information whether and how it has been loaded from an
1270 : : aggregate into *AGGPOS. INFO describes the function parameters, STMT is the
1271 : : statement in which OP is used or loaded. */
1272 : :
1273 : : static bool
1274 : 30043176 : unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi,
1275 : : gimple *stmt, tree op, int *index_p,
1276 : : poly_int64 *size_p,
1277 : : struct agg_position_info *aggpos)
1278 : : {
1279 : 31432824 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1280 : :
1281 : 31432824 : gcc_checking_assert (aggpos);
1282 : 31432824 : if (res)
1283 : : {
1284 : 709574 : *index_p = ipa_get_param_decl_index (fbi->info, res);
1285 : 709574 : if (*index_p < 0)
1286 : : return false;
1287 : 709446 : aggpos->agg_contents = false;
1288 : 709446 : aggpos->by_ref = false;
1289 : 709446 : return true;
1290 : : }
1291 : :
1292 : 30723250 : if (TREE_CODE (op) == SSA_NAME)
1293 : : {
1294 : 10771700 : if (SSA_NAME_IS_DEFAULT_DEF (op)
1295 : 10771700 : || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1296 : : return false;
1297 : 4385555 : stmt = SSA_NAME_DEF_STMT (op);
1298 : 4385555 : op = gimple_assign_rhs1 (stmt);
1299 : 4385555 : if (!REFERENCE_CLASS_P (op))
1300 : : return unmodified_parm_or_parm_agg_item (fbi, stmt, op, index_p, size_p,
1301 : : aggpos);
1302 : : }
1303 : :
1304 : 22947457 : aggpos->agg_contents = true;
1305 : 22947457 : return ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
1306 : : stmt, op, index_p, &aggpos->offset,
1307 : 22947457 : size_p, &aggpos->by_ref);
1308 : : }
1309 : :
1310 : : /* If stmt is simple load or store of value pointed to by a function parmaeter,
1311 : : return its index. */
1312 : :
1313 : : static int
1314 : 102768156 : load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt)
1315 : : {
1316 : 102768156 : if (!optimize)
1317 : : return -1;
1318 : 147061593 : gassign *assign = dyn_cast <gassign *> (stmt);
1319 : 50721791 : if (!assign)
1320 : : return -1;
1321 : 50721791 : tree param;
1322 : 50721791 : if (gimple_assign_load_p (stmt))
1323 : 19031578 : param = gimple_assign_rhs1 (stmt);
1324 : 31690213 : else if (gimple_store_p (stmt))
1325 : 16833176 : param = gimple_assign_lhs (stmt);
1326 : : else
1327 : : return -1;
1328 : 35864754 : tree base = get_base_address (param);
1329 : 35864754 : if (TREE_CODE (base) != MEM_REF
1330 : 12185170 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1331 : 48045827 : || !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1332 : : return -1;
1333 : 6485717 : tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1334 : 6485717 : if (TREE_CODE (p) != PARM_DECL)
1335 : : return -1;
1336 : 6428354 : return ipa_get_param_decl_index (fbi->info, p);
1337 : : }
1338 : :
1339 : : /* See if statement might disappear after inlining.
1340 : : 0 - means not eliminated
1341 : : 1 - half of statements goes away
1342 : : 2 - for sure it is eliminated.
1343 : : We are not terribly sophisticated, basically looking for simple abstraction
1344 : : penalty wrappers. */
1345 : :
1346 : : static int
1347 : 102768156 : eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt)
1348 : : {
1349 : 102768156 : enum gimple_code code = gimple_code (stmt);
1350 : 102768156 : enum tree_code rhs_code;
1351 : :
1352 : 102768156 : if (!optimize)
1353 : : return 0;
1354 : :
1355 : 84877193 : switch (code)
1356 : : {
1357 : : case GIMPLE_RETURN:
1358 : : return 2;
1359 : 50721791 : case GIMPLE_ASSIGN:
1360 : 50721791 : if (gimple_num_ops (stmt) != 2)
1361 : : return 0;
1362 : :
1363 : 36635711 : rhs_code = gimple_assign_rhs_code (stmt);
1364 : :
1365 : : /* Casts of parameters, loads from parameters passed by reference
1366 : : and stores to return value or parameters are often free after
1367 : : inlining due to SRA and further combining.
1368 : : Assume that half of statements goes away. */
1369 : 36635711 : if (CONVERT_EXPR_CODE_P (rhs_code)
1370 : : || rhs_code == VIEW_CONVERT_EXPR
1371 : : || rhs_code == ADDR_EXPR
1372 : 34090639 : || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1373 : : {
1374 : 35864754 : tree rhs = gimple_assign_rhs1 (stmt);
1375 : 35864754 : tree lhs = gimple_assign_lhs (stmt);
1376 : 35864754 : tree inner_rhs = get_base_address (rhs);
1377 : 35864754 : tree inner_lhs = get_base_address (lhs);
1378 : 35864754 : bool rhs_free = false;
1379 : 35864754 : bool lhs_free = false;
1380 : :
1381 : 35864754 : if (!inner_rhs)
1382 : 0 : inner_rhs = rhs;
1383 : 35864754 : if (!inner_lhs)
1384 : 0 : inner_lhs = lhs;
1385 : :
1386 : : /* Reads of parameter are expected to be free. */
1387 : 35864754 : if (unmodified_parm (fbi, stmt, inner_rhs, NULL))
1388 : : rhs_free = true;
1389 : : /* Match expressions of form &this->field. Those will most likely
1390 : : combine with something upstream after inlining. */
1391 : 34727288 : else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1392 : : {
1393 : 2249006 : tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1394 : 2249006 : if (TREE_CODE (op) == PARM_DECL)
1395 : : rhs_free = true;
1396 : 2219724 : else if (TREE_CODE (op) == MEM_REF
1397 : 2219724 : && unmodified_parm (fbi, stmt, TREE_OPERAND (op, 0),
1398 : : NULL))
1399 : : rhs_free = true;
1400 : : }
1401 : :
1402 : : /* When parameter is not SSA register because its address is taken
1403 : : and it is just copied into one, the statement will be completely
1404 : : free after inlining (we will copy propagate backward). */
1405 : 1166748 : if (rhs_free && is_gimple_reg (lhs))
1406 : : return 2;
1407 : :
1408 : : /* Reads of parameters passed by reference
1409 : : expected to be free (i.e. optimized out after inlining). */
1410 : 35248400 : if (TREE_CODE (inner_rhs) == MEM_REF
1411 : 35248400 : && unmodified_parm (fbi, stmt, TREE_OPERAND (inner_rhs, 0), NULL))
1412 : : rhs_free = true;
1413 : :
1414 : : /* Copying parameter passed by reference into gimple register is
1415 : : probably also going to copy propagate, but we can't be quite
1416 : : sure. */
1417 : 35248400 : if (rhs_free && is_gimple_reg (lhs))
1418 : : lhs_free = true;
1419 : :
1420 : : /* Writes to parameters, parameters passed by value and return value
1421 : : (either directly or passed via invisible reference) are free.
1422 : :
1423 : : TODO: We ought to handle testcase like
1424 : : struct a {int a,b;};
1425 : : struct a
1426 : : returnstruct (void)
1427 : : {
1428 : : struct a a ={1,2};
1429 : : return a;
1430 : : }
1431 : :
1432 : : This translate into:
1433 : :
1434 : : returnstruct ()
1435 : : {
1436 : : int a$b;
1437 : : int a$a;
1438 : : struct a a;
1439 : : struct a D.2739;
1440 : :
1441 : : <bb 2>:
1442 : : D.2739.a = 1;
1443 : : D.2739.b = 2;
1444 : : return D.2739;
1445 : :
1446 : : }
1447 : : For that we either need to copy ipa-split logic detecting writes
1448 : : to return value. */
1449 : 35248400 : if (TREE_CODE (inner_lhs) == PARM_DECL
1450 : 35173995 : || TREE_CODE (inner_lhs) == RESULT_DECL
1451 : 69585452 : || (TREE_CODE (inner_lhs) == MEM_REF
1452 : 4472327 : && (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0),
1453 : : NULL)
1454 : 2319044 : || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1455 : 2317870 : && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1456 : 310335 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1457 : : (inner_lhs,
1458 : : 0))) == RESULT_DECL))))
1459 : : lhs_free = true;
1460 : 32131772 : if (lhs_free
1461 : 35248400 : && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1462 : : rhs_free = true;
1463 : 35248400 : if (lhs_free && rhs_free)
1464 : : return 1;
1465 : : }
1466 : : return 0;
1467 : : default:
1468 : : return 0;
1469 : : }
1470 : : }
1471 : :
1472 : : /* Analyze EXPR if it represents a series of simple operations performed on
1473 : : a function parameter and return true if so. FBI, STMT, EXPR, INDEX_P and
1474 : : AGGPOS have the same meaning like in unmodified_parm_or_parm_agg_item.
1475 : : Type of the parameter or load from an aggregate via the parameter is
1476 : : stored in *TYPE_P. Operations on the parameter are recorded to
1477 : : PARAM_OPS_P if it is not NULL. */
1478 : :
1479 : : static bool
1480 : 24765169 : decompose_param_expr (struct ipa_func_body_info *fbi,
1481 : : gimple *stmt, tree expr,
1482 : : int *index_p, tree *type_p,
1483 : : struct agg_position_info *aggpos,
1484 : : expr_eval_ops *param_ops_p = NULL)
1485 : : {
1486 : 24765169 : int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops);
1487 : 24765169 : int op_count = 0;
1488 : :
1489 : 24765169 : if (param_ops_p)
1490 : 8262145 : *param_ops_p = NULL;
1491 : :
1492 : 30043176 : while (true)
1493 : : {
1494 : 30043176 : expr_eval_op eval_op;
1495 : 30043176 : unsigned rhs_count;
1496 : 30043176 : unsigned cst_count = 0;
1497 : :
1498 : 30043176 : if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL,
1499 : : aggpos))
1500 : : {
1501 : 3955415 : tree type = TREE_TYPE (expr);
1502 : :
1503 : 3955415 : if (aggpos->agg_contents)
1504 : : {
1505 : : /* Stop if containing bit-field. */
1506 : 3245969 : if (TREE_CODE (expr) == BIT_FIELD_REF
1507 : 3245969 : || contains_bitfld_component_ref_p (expr))
1508 : : break;
1509 : : }
1510 : :
1511 : 3927371 : *type_p = type;
1512 : 3927371 : return true;
1513 : : }
1514 : :
1515 : 26087761 : if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr))
1516 : : break;
1517 : 9560518 : stmt = SSA_NAME_DEF_STMT (expr);
1518 : :
1519 : 9560518 : if (gcall *call = dyn_cast <gcall *> (stmt))
1520 : : {
1521 : 2258636 : int flags = gimple_call_return_flags (call);
1522 : 2258636 : if (!(flags & ERF_RETURNS_ARG))
1523 : 2750620 : goto fail;
1524 : 179180 : int arg = flags & ERF_RETURN_ARG_MASK;
1525 : 179180 : if (arg >= (int)gimple_call_num_args (call))
1526 : 0 : goto fail;
1527 : 179180 : expr = gimple_call_arg (stmt, arg);
1528 : 3699750 : continue;
1529 : 179180 : }
1530 : :
1531 : 7301882 : if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr)))
1532 : : break;
1533 : :
1534 : 5770088 : switch (gimple_assign_rhs_class (stmt))
1535 : : {
1536 : 3520570 : case GIMPLE_SINGLE_RHS:
1537 : 3520570 : expr = gimple_assign_rhs1 (stmt);
1538 : 3520570 : continue;
1539 : :
1540 : : case GIMPLE_UNARY_RHS:
1541 : : rhs_count = 1;
1542 : : break;
1543 : :
1544 : 1335260 : case GIMPLE_BINARY_RHS:
1545 : 1335260 : rhs_count = 2;
1546 : 1335260 : break;
1547 : :
1548 : 625 : case GIMPLE_TERNARY_RHS:
1549 : 625 : rhs_count = 3;
1550 : 625 : break;
1551 : :
1552 : 0 : default:
1553 : 0 : goto fail;
1554 : : }
1555 : :
1556 : : /* Stop if expression is too complex. */
1557 : 2249518 : if (op_count++ == op_limit)
1558 : : break;
1559 : :
1560 : 2249421 : if (param_ops_p)
1561 : : {
1562 : 2247938 : eval_op.code = gimple_assign_rhs_code (stmt);
1563 : 2247938 : eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt));
1564 : 2247938 : eval_op.val[0] = NULL_TREE;
1565 : 2247938 : eval_op.val[1] = NULL_TREE;
1566 : : }
1567 : :
1568 : : expr = NULL_TREE;
1569 : 5163544 : for (unsigned i = 0; i < rhs_count; i++)
1570 : : {
1571 : 3585287 : tree op = gimple_op (stmt, i + 1);
1572 : :
1573 : 3585287 : gcc_assert (op && !TYPE_P (op));
1574 : 3585287 : if (is_gimple_ip_invariant (op))
1575 : : {
1576 : 666842 : if (++cst_count == rhs_count)
1577 : 1070 : goto fail;
1578 : :
1579 : 665772 : eval_op.val[cst_count - 1] = op;
1580 : : }
1581 : 2918445 : else if (!expr)
1582 : : {
1583 : : /* Found a non-constant operand, and record its index in rhs
1584 : : operands. */
1585 : 2248351 : eval_op.index = i;
1586 : 2248351 : expr = op;
1587 : : }
1588 : : else
1589 : : {
1590 : : /* Found more than one non-constant operands. */
1591 : 670094 : goto fail;
1592 : : }
1593 : : }
1594 : :
1595 : 1578257 : if (param_ops_p)
1596 : 1577011 : vec_safe_insert (*param_ops_p, 0, eval_op);
1597 : : }
1598 : :
1599 : : /* Failed to decompose, free resource and return. */
1600 : 20837798 : fail:
1601 : 20837798 : if (param_ops_p)
1602 : 8191012 : vec_free (*param_ops_p);
1603 : :
1604 : : return false;
1605 : : }
1606 : :
1607 : : /* Record to SUMMARY that PARM is used by builtin_constant_p. */
1608 : :
1609 : : static void
1610 : 5668 : add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm)
1611 : : {
1612 : 5668 : int ip;
1613 : :
1614 : : /* Avoid duplicates. */
1615 : 5676 : for (unsigned int i = 0;
1616 : 5676 : summary->builtin_constant_p_parms.iterate (i, &ip); i++)
1617 : 2686 : if (ip == parm)
1618 : 5668 : return;
1619 : 2990 : summary->builtin_constant_p_parms.safe_push (parm);
1620 : : }
1621 : :
1622 : : /* If BB ends by a conditional we can turn into predicates, attach corresponding
1623 : : predicates to the CFG edges. */
1624 : :
1625 : : static void
1626 : 31554496 : set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1627 : : class ipa_fn_summary *summary,
1628 : : class ipa_node_params *params_summary,
1629 : : basic_block bb)
1630 : : {
1631 : 31554496 : tree op, op2;
1632 : 31554496 : int index;
1633 : 31554496 : struct agg_position_info aggpos;
1634 : 31554496 : enum tree_code code, inverted_code;
1635 : 31554496 : edge e;
1636 : 31554496 : edge_iterator ei;
1637 : 31554496 : gimple *set_stmt;
1638 : 31554496 : tree param_type;
1639 : 31554496 : expr_eval_ops param_ops;
1640 : :
1641 : 63108992 : gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1642 : 10310654 : if (!last)
1643 : 31548820 : return;
1644 : 10310654 : if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1645 : : return;
1646 : 8184336 : op = gimple_cond_lhs (last);
1647 : :
1648 : 8184336 : if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1649 : : ¶m_ops))
1650 : : {
1651 : 1072028 : code = gimple_cond_code (last);
1652 : 1072028 : inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1653 : :
1654 : 3216084 : FOR_EACH_EDGE (e, ei, bb->succs)
1655 : : {
1656 : 4288112 : enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1657 : 2144056 : ? code : inverted_code);
1658 : : /* invert_tree_comparison will return ERROR_MARK on FP
1659 : : comparisons that are not EQ/NE instead of returning proper
1660 : : unordered one. Be sure it is not confused with NON_CONSTANT.
1661 : :
1662 : : And if the edge's target is the final block of diamond CFG graph
1663 : : of this conditional statement, we do not need to compute
1664 : : predicate for the edge because the final block's predicate must
1665 : : be at least as that of the first block of the statement. */
1666 : 2144056 : if (this_code != ERROR_MARK
1667 : 2144056 : && !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1668 : : {
1669 : 1801654 : ipa_predicate p
1670 : 1801654 : = add_condition (summary, params_summary, index,
1671 : : param_type, &aggpos,
1672 : : this_code, gimple_cond_rhs (last), param_ops);
1673 : 1801654 : e->aux = edge_predicate_pool.allocate ();
1674 : 1801654 : *(ipa_predicate *) e->aux = p;
1675 : : }
1676 : : }
1677 : 1072028 : vec_free (param_ops);
1678 : 1072028 : return;
1679 : : }
1680 : :
1681 : 7112308 : if (TREE_CODE (op) != SSA_NAME)
1682 : : return;
1683 : : /* Special case
1684 : : if (builtin_constant_p (op))
1685 : : constant_code
1686 : : else
1687 : : nonconstant_code.
1688 : : Here we can predicate nonconstant_code. We can't
1689 : : really handle constant_code since we have no predicate
1690 : : for this and also the constant code is not known to be
1691 : : optimized away when inliner doesn't see operand is constant.
1692 : : Other optimizers might think otherwise. */
1693 : 7110025 : if (gimple_cond_code (last) != NE_EXPR
1694 : 7110025 : || !integer_zerop (gimple_cond_rhs (last)))
1695 : 4098659 : return;
1696 : 3011366 : set_stmt = SSA_NAME_DEF_STMT (op);
1697 : 3011366 : if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1698 : 3011366 : || gimple_call_num_args (set_stmt) != 1)
1699 : : return;
1700 : 6790 : op2 = gimple_call_arg (set_stmt, 0);
1701 : 6790 : if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos))
1702 : : return;
1703 : 5676 : if (!aggpos.by_ref)
1704 : 5651 : add_builtin_constant_p_parm (summary, index);
1705 : 17028 : FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1706 : : {
1707 : 5676 : ipa_predicate p = add_condition (summary, params_summary, index,
1708 : : param_type, &aggpos,
1709 : : ipa_predicate::is_not_constant, NULL_TREE);
1710 : 5676 : e->aux = edge_predicate_pool.allocate ();
1711 : 5676 : *(ipa_predicate *) e->aux = p;
1712 : : }
1713 : : }
1714 : :
1715 : :
1716 : : /* If BB ends by a switch we can turn into predicates, attach corresponding
1717 : : predicates to the CFG edges. */
1718 : :
1719 : : static void
1720 : 31554496 : set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1721 : : class ipa_fn_summary *summary,
1722 : : class ipa_node_params *params_summary,
1723 : : basic_block bb)
1724 : : {
1725 : 31554496 : tree op;
1726 : 31554496 : int index;
1727 : 31554496 : struct agg_position_info aggpos;
1728 : 31554496 : edge e;
1729 : 31554496 : edge_iterator ei;
1730 : 31554496 : size_t n;
1731 : 31554496 : size_t case_idx;
1732 : 31554496 : tree param_type;
1733 : 31554496 : expr_eval_ops param_ops;
1734 : :
1735 : 63108992 : gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
1736 : 77809 : if (!last)
1737 : 31524951 : return;
1738 : 77809 : op = gimple_switch_index (last);
1739 : 77809 : if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1740 : : ¶m_ops))
1741 : : return;
1742 : :
1743 : 43333 : auto_vec<std::pair<tree, tree> > ranges;
1744 : 43333 : tree type = TREE_TYPE (op);
1745 : 43333 : int bound_limit = opt_for_fn (fbi->node->decl,
1746 : : param_ipa_max_switch_predicate_bounds);
1747 : 43333 : int bound_count = 0;
1748 : : // This can safely be an integer range, as switches can only hold
1749 : : // integers.
1750 : 43333 : int_range<2> vr;
1751 : :
1752 : 86666 : get_range_query (cfun)->range_of_expr (vr, op);
1753 : 43333 : if (vr.undefined_p ())
1754 : 0 : vr.set_varying (TREE_TYPE (op));
1755 : 43333 : tree vr_min, vr_max;
1756 : : // TODO: This entire function could use a rewrite to use the irange
1757 : : // API, instead of trying to recreate its intersection/union logic.
1758 : : // Any use of get_legacy_range() is a serious code smell.
1759 : 43333 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
1760 : 43333 : wide_int vr_wmin = wi::to_wide (vr_min);
1761 : 43333 : wide_int vr_wmax = wi::to_wide (vr_max);
1762 : :
1763 : 303776 : FOR_EACH_EDGE (e, ei, bb->succs)
1764 : : {
1765 : 260443 : e->aux = edge_predicate_pool.allocate ();
1766 : 260443 : *(ipa_predicate *) e->aux = false;
1767 : : }
1768 : :
1769 : 43333 : e = gimple_switch_edge (cfun, last, 0);
1770 : : /* Set BOUND_COUNT to maximum count to bypass computing predicate for
1771 : : default case if its target basic block is in convergence point of all
1772 : : switch cases, which can be determined by checking whether it
1773 : : post-dominates the switch statement. */
1774 : 43333 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1775 : 12186 : bound_count = INT_MAX;
1776 : :
1777 : 43333 : n = gimple_switch_num_labels (last);
1778 : 284685 : for (case_idx = 1; case_idx < n; ++case_idx)
1779 : : {
1780 : 241352 : tree cl = gimple_switch_label (last, case_idx);
1781 : 241352 : tree min = CASE_LOW (cl);
1782 : 241352 : tree max = CASE_HIGH (cl);
1783 : 241352 : ipa_predicate p;
1784 : :
1785 : 241352 : e = gimple_switch_edge (cfun, last, case_idx);
1786 : :
1787 : : /* The case value might not have same type as switch expression,
1788 : : extend the value based on the expression type. */
1789 : 241352 : if (TREE_TYPE (min) != type)
1790 : 43323 : min = wide_int_to_tree (type, wi::to_wide (min));
1791 : :
1792 : 241352 : if (!max)
1793 : : max = min;
1794 : 16512 : else if (TREE_TYPE (max) != type)
1795 : 3027 : max = wide_int_to_tree (type, wi::to_wide (max));
1796 : :
1797 : : /* The case's target basic block is in convergence point of all switch
1798 : : cases, its predicate should be at least as that of the switch
1799 : : statement. */
1800 : 241352 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1801 : 2685 : p = true;
1802 : 238667 : else if (min == max)
1803 : 222920 : p = add_condition (summary, params_summary, index, param_type,
1804 : : &aggpos, EQ_EXPR, min, param_ops);
1805 : : else
1806 : : {
1807 : 15747 : ipa_predicate p1, p2;
1808 : 15747 : p1 = add_condition (summary, params_summary, index, param_type,
1809 : : &aggpos, GE_EXPR, min, param_ops);
1810 : 15747 : p2 = add_condition (summary, params_summary,index, param_type,
1811 : : &aggpos, LE_EXPR, max, param_ops);
1812 : 15747 : p = p1 & p2;
1813 : : }
1814 : 241352 : *(ipa_predicate *) e->aux
1815 : 241352 : = p.or_with (summary->conds, *(ipa_predicate *) e->aux);
1816 : :
1817 : : /* If there are too many disjoint case ranges, predicate for default
1818 : : case might become too complicated. So add a limit here. */
1819 : 241352 : if (bound_count > bound_limit)
1820 : 94741 : continue;
1821 : :
1822 : 146611 : bool new_range = true;
1823 : :
1824 : 146611 : if (!ranges.is_empty ())
1825 : : {
1826 : 115464 : wide_int curr_wmin = wi::to_wide (min);
1827 : 115464 : wide_int last_wmax = wi::to_wide (ranges.last ().second);
1828 : :
1829 : : /* Merge case ranges if they are continuous. */
1830 : 115464 : if (curr_wmin == last_wmax + 1)
1831 : : new_range = false;
1832 : 36598 : else if (vr_type == VR_ANTI_RANGE)
1833 : : {
1834 : : /* If two disjoint case ranges can be connected by anti-range
1835 : : of switch index, combine them to one range. */
1836 : 37 : if (wi::lt_p (vr_wmax, curr_wmin - 1, TYPE_SIGN (type)))
1837 : : vr_type = VR_UNDEFINED;
1838 : 0 : else if (wi::le_p (vr_wmin, last_wmax + 1, TYPE_SIGN (type)))
1839 : 0 : new_range = false;
1840 : : }
1841 : 115464 : }
1842 : :
1843 : : /* Create/extend a case range. And we count endpoints of range set,
1844 : : this number nearly equals to number of conditions that we will create
1845 : : for predicate of default case. */
1846 : 115464 : if (new_range)
1847 : : {
1848 : 67745 : bound_count += (min == max) ? 1 : 2;
1849 : 67745 : ranges.safe_push (std::make_pair (min, max));
1850 : : }
1851 : : else
1852 : : {
1853 : 78866 : bound_count += (ranges.last ().first == ranges.last ().second);
1854 : 78866 : ranges.last ().second = max;
1855 : : }
1856 : : }
1857 : :
1858 : 43333 : e = gimple_switch_edge (cfun, last, 0);
1859 : 43333 : if (bound_count > bound_limit)
1860 : : {
1861 : 13788 : *(ipa_predicate *) e->aux = true;
1862 : 13788 : vec_free (param_ops);
1863 : 13788 : return;
1864 : : }
1865 : :
1866 : 29545 : ipa_predicate p_seg = true;
1867 : 29545 : ipa_predicate p_all = false;
1868 : :
1869 : 29545 : if (vr_type != VR_RANGE)
1870 : : {
1871 : 29086 : vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type));
1872 : 29086 : vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type));
1873 : : }
1874 : :
1875 : : /* Construct predicate to represent default range set that is negation of
1876 : : all case ranges. Case range is classified as containing single/non-single
1877 : : values. Suppose a piece of case ranges in the following.
1878 : :
1879 : : [D1...D2] [S1] ... [Sn] [D3...D4]
1880 : :
1881 : : To represent default case's range sets between two non-single value
1882 : : case ranges (From D2 to D3), we construct predicate as:
1883 : :
1884 : : D2 < x < D3 && x != S1 && ... && x != Sn
1885 : : */
1886 : 178460 : for (size_t i = 0; i < ranges.length (); i++)
1887 : : {
1888 : 59781 : tree min = ranges[i].first;
1889 : 59781 : tree max = ranges[i].second;
1890 : :
1891 : 59781 : if (min == max)
1892 : 74446 : p_seg &= add_condition (summary, params_summary, index,
1893 : : param_type, &aggpos, NE_EXPR,
1894 : 37223 : min, param_ops);
1895 : : else
1896 : : {
1897 : : /* Do not create sub-predicate for range that is beyond low bound
1898 : : of switch index. */
1899 : 22558 : if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type)))
1900 : : {
1901 : 14112 : p_seg &= add_condition (summary, params_summary, index,
1902 : : param_type, &aggpos,
1903 : 14112 : LT_EXPR, min, param_ops);
1904 : 14112 : p_all = p_all.or_with (summary->conds, p_seg);
1905 : : }
1906 : :
1907 : : /* Do not create sub-predicate for range that is beyond up bound
1908 : : of switch index. */
1909 : 22558 : if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type)))
1910 : : {
1911 : 96 : p_seg = false;
1912 : 96 : break;
1913 : : }
1914 : :
1915 : 22462 : p_seg = add_condition (summary, params_summary, index,
1916 : : param_type, &aggpos, GT_EXPR,
1917 : : max, param_ops);
1918 : : }
1919 : : }
1920 : :
1921 : 29545 : p_all = p_all.or_with (summary->conds, p_seg);
1922 : 29545 : *(ipa_predicate *) e->aux
1923 : 29545 : = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux);
1924 : :
1925 : 36750 : vec_free (param_ops);
1926 : 43333 : }
1927 : :
1928 : :
1929 : : /* For each BB in NODE attach to its AUX pointer predicate under
1930 : : which it is executable. */
1931 : :
1932 : : static void
1933 : 5659981 : compute_bb_predicates (struct ipa_func_body_info *fbi,
1934 : : struct cgraph_node *node,
1935 : : class ipa_fn_summary *summary,
1936 : : class ipa_node_params *params_summary)
1937 : : {
1938 : 5659981 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1939 : 5659981 : bool done = false;
1940 : 5659981 : basic_block bb;
1941 : :
1942 : 37214477 : FOR_EACH_BB_FN (bb, my_function)
1943 : : {
1944 : 31554496 : set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb);
1945 : 31554496 : set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb);
1946 : : }
1947 : :
1948 : : /* Entry block is always executable. */
1949 : 5659981 : ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1950 : 5659981 : = edge_predicate_pool.allocate ();
1951 : 5659981 : *(ipa_predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux = true;
1952 : :
1953 : : /* A simple dataflow propagation of predicates forward in the CFG.
1954 : : TODO: work in reverse postorder. */
1955 : 17089605 : while (!done)
1956 : : {
1957 : 11429624 : done = true;
1958 : 79595649 : FOR_EACH_BB_FN (bb, my_function)
1959 : : {
1960 : 68166025 : ipa_predicate p = false;
1961 : 68166025 : edge e;
1962 : 68166025 : edge_iterator ei;
1963 : 84084001 : FOR_EACH_EDGE (e, ei, bb->preds)
1964 : : {
1965 : 72405232 : if (e->src->aux)
1966 : : {
1967 : 70691136 : ipa_predicate this_bb_predicate
1968 : : = *(ipa_predicate *) e->src->aux;
1969 : 70691136 : if (e->aux)
1970 : 4333616 : this_bb_predicate &= (*(ipa_predicate *) e->aux);
1971 : 70691136 : p = p.or_with (summary->conds, this_bb_predicate);
1972 : 70691136 : if (p == true)
1973 : : break;
1974 : : }
1975 : : }
1976 : 68166025 : if (p != false)
1977 : : {
1978 : 67073400 : basic_block pdom_bb;
1979 : :
1980 : 67073400 : if (!bb->aux)
1981 : : {
1982 : 24666777 : done = false;
1983 : 24666777 : bb->aux = edge_predicate_pool.allocate ();
1984 : 24666777 : *((ipa_predicate *) bb->aux) = p;
1985 : : }
1986 : 42406623 : else if (p != *(ipa_predicate *) bb->aux)
1987 : : {
1988 : : /* This OR operation is needed to ensure monotonous data flow
1989 : : in the case we hit the limit on number of clauses and the
1990 : : and/or operations above give approximate answers. */
1991 : 131163 : p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux);
1992 : 131163 : if (p != *(ipa_predicate *)bb->aux)
1993 : : {
1994 : 106003 : done = false;
1995 : 106003 : *((ipa_predicate *)bb->aux) = p;
1996 : : }
1997 : : }
1998 : :
1999 : : /* For switch/if statement, we can OR-combine predicates of all
2000 : : its cases/branches to get predicate for basic block in their
2001 : : convergence point, but sometimes this will generate very
2002 : : complicated predicate. Actually, we can get simplified
2003 : : predicate in another way by using the fact that predicate
2004 : : for a basic block must also hold true for its post dominators.
2005 : : To be specific, basic block in convergence point of
2006 : : conditional statement should include predicate of the
2007 : : statement. */
2008 : 67073400 : pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
2009 : 67073400 : if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb)
2010 : : ;
2011 : 34628033 : else if (!pdom_bb->aux)
2012 : : {
2013 : 6887705 : done = false;
2014 : 6887705 : pdom_bb->aux = edge_predicate_pool.allocate ();
2015 : 6887705 : *((ipa_predicate *)pdom_bb->aux) = p;
2016 : : }
2017 : 27740328 : else if (p != *(ipa_predicate *)pdom_bb->aux)
2018 : : {
2019 : 2992350 : p = p.or_with (summary->conds,
2020 : : *(ipa_predicate *)pdom_bb->aux);
2021 : 2992350 : if (p != *(ipa_predicate *)pdom_bb->aux)
2022 : : {
2023 : 151853 : done = false;
2024 : 151853 : *((ipa_predicate *)pdom_bb->aux) = p;
2025 : : }
2026 : : }
2027 : : }
2028 : : }
2029 : : }
2030 : 5659981 : }
2031 : :
2032 : :
2033 : : /* Return predicate specifying when the STMT might have result that is not
2034 : : a compile time constant. */
2035 : :
2036 : : static ipa_predicate
2037 : 3804514 : will_be_nonconstant_expr_predicate (ipa_func_body_info *fbi,
2038 : : class ipa_fn_summary *summary,
2039 : : class ipa_node_params *params_summary,
2040 : : tree expr,
2041 : : vec<ipa_predicate> nonconstant_names)
2042 : : {
2043 : 3804514 : tree parm;
2044 : 3804514 : int index;
2045 : :
2046 : 4020813 : while (UNARY_CLASS_P (expr))
2047 : 216299 : expr = TREE_OPERAND (expr, 0);
2048 : :
2049 : 3804514 : parm = unmodified_parm (fbi, NULL, expr, NULL);
2050 : 3804514 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2051 : 201463 : return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL,
2052 : 201463 : ipa_predicate::changed, NULL_TREE);
2053 : 3603051 : if (is_gimple_min_invariant (expr))
2054 : 52152 : return false;
2055 : 3550899 : if (TREE_CODE (expr) == SSA_NAME)
2056 : 3398692 : return nonconstant_names[SSA_NAME_VERSION (expr)];
2057 : 152207 : if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2058 : : {
2059 : 151907 : ipa_predicate p1
2060 : 151907 : = will_be_nonconstant_expr_predicate (fbi, summary,
2061 : : params_summary,
2062 : 151907 : TREE_OPERAND (expr, 0),
2063 : : nonconstant_names);
2064 : 151907 : if (p1 == true)
2065 : 90139 : return p1;
2066 : :
2067 : 61768 : ipa_predicate p2
2068 : 61768 : = will_be_nonconstant_expr_predicate (fbi, summary,
2069 : : params_summary,
2070 : 61768 : TREE_OPERAND (expr, 1),
2071 : : nonconstant_names);
2072 : 61768 : return p1.or_with (summary->conds, p2);
2073 : : }
2074 : 300 : else if (TREE_CODE (expr) == COND_EXPR)
2075 : : {
2076 : 141 : ipa_predicate p1
2077 : 141 : = will_be_nonconstant_expr_predicate (fbi, summary,
2078 : : params_summary,
2079 : 141 : TREE_OPERAND (expr, 0),
2080 : : nonconstant_names);
2081 : 141 : if (p1 == true)
2082 : 81 : return p1;
2083 : :
2084 : 60 : ipa_predicate p2
2085 : 60 : = will_be_nonconstant_expr_predicate (fbi, summary,
2086 : : params_summary,
2087 : 60 : TREE_OPERAND (expr, 1),
2088 : : nonconstant_names);
2089 : 60 : if (p2 == true)
2090 : 60 : return p2;
2091 : 0 : p1 = p1.or_with (summary->conds, p2);
2092 : 0 : p2 = will_be_nonconstant_expr_predicate (fbi, summary,
2093 : : params_summary,
2094 : 0 : TREE_OPERAND (expr, 2),
2095 : : nonconstant_names);
2096 : 0 : return p2.or_with (summary->conds, p1);
2097 : : }
2098 : 159 : else if (TREE_CODE (expr) == CALL_EXPR)
2099 : 159 : return true;
2100 : : else
2101 : : {
2102 : 0 : debug_tree (expr);
2103 : 0 : gcc_unreachable ();
2104 : : }
2105 : : }
2106 : :
2107 : :
2108 : : /* Return predicate specifying when the STMT might have result that is not
2109 : : a compile time constant. */
2110 : :
2111 : : static ipa_predicate
2112 : 106359410 : will_be_nonconstant_predicate (struct ipa_func_body_info *fbi,
2113 : : class ipa_fn_summary *summary,
2114 : : class ipa_node_params *params_summary,
2115 : : gimple *stmt,
2116 : : vec<ipa_predicate> nonconstant_names)
2117 : : {
2118 : 106359410 : ipa_predicate p = true;
2119 : 106359410 : ssa_op_iter iter;
2120 : 106359410 : tree use;
2121 : 106359410 : tree param_type = NULL_TREE;
2122 : 106359410 : ipa_predicate op_non_const;
2123 : 106359410 : bool is_load;
2124 : 106359410 : int base_index;
2125 : 106359410 : struct agg_position_info aggpos;
2126 : :
2127 : : /* What statements might be optimized away
2128 : : when their arguments are constant. */
2129 : 106359410 : if (gimple_code (stmt) != GIMPLE_ASSIGN
2130 : 37214941 : && gimple_code (stmt) != GIMPLE_COND
2131 : 26904287 : && gimple_code (stmt) != GIMPLE_SWITCH
2132 : 133185888 : && (gimple_code (stmt) != GIMPLE_CALL
2133 : 18408690 : || !(gimple_call_flags (stmt) & ECF_CONST)))
2134 : 25661168 : return p;
2135 : :
2136 : : /* Stores will stay anyway. */
2137 : 80698242 : if (gimple_store_p (stmt))
2138 : 23341772 : return p;
2139 : :
2140 : 57356470 : is_load = gimple_assign_load_p (stmt);
2141 : :
2142 : : /* Loads can be optimized when the value is known. */
2143 : 57356470 : if (is_load)
2144 : : {
2145 : 16496234 : tree op = gimple_assign_rhs1 (stmt);
2146 : 16496234 : if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type,
2147 : : &aggpos))
2148 : 13689900 : return p;
2149 : : }
2150 : : else
2151 : 40860236 : base_index = -1;
2152 : :
2153 : : /* See if we understand all operands before we start
2154 : : adding conditionals. */
2155 : 58589798 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2156 : : {
2157 : 43064690 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2158 : : /* For arguments we can build a condition. */
2159 : 43064690 : if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0)
2160 : 7569292 : continue;
2161 : 35495398 : if (TREE_CODE (use) != SSA_NAME)
2162 : 0 : return p;
2163 : : /* If we know when operand is constant,
2164 : : we still can say something useful. */
2165 : 35495398 : if (nonconstant_names[SSA_NAME_VERSION (use)] != true)
2166 : 7353936 : continue;
2167 : 28141462 : return p;
2168 : : }
2169 : :
2170 : 15525108 : if (is_load)
2171 : 2806248 : op_non_const =
2172 : 2806248 : add_condition (summary, params_summary,
2173 : : base_index, param_type, &aggpos,
2174 : : ipa_predicate::changed, NULL_TREE);
2175 : : else
2176 : 12718860 : op_non_const = false;
2177 : 29522589 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2178 : : {
2179 : 13997481 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2180 : 13997481 : int index;
2181 : :
2182 : 13997481 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2183 : : {
2184 : 7121714 : if (index != base_index)
2185 : 4951701 : p = add_condition (summary, params_summary, index,
2186 : 4951701 : TREE_TYPE (parm), NULL,
2187 : : ipa_predicate::changed, NULL_TREE);
2188 : : else
2189 : 2170013 : continue;
2190 : : }
2191 : : else
2192 : 6875767 : p = nonconstant_names[SSA_NAME_VERSION (use)];
2193 : 11827468 : op_non_const = p.or_with (summary->conds, op_non_const);
2194 : : }
2195 : 15525108 : if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2196 : 13670945 : && gimple_op (stmt, 0)
2197 : 29094520 : && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2198 : 13569412 : nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2199 : 13569412 : = op_non_const;
2200 : 15525108 : return op_non_const;
2201 : : }
2202 : :
2203 : : struct record_modified_bb_info
2204 : : {
2205 : : tree op;
2206 : : bitmap bb_set;
2207 : : gimple *stmt;
2208 : : };
2209 : :
2210 : : /* Value is initialized in INIT_BB and used in USE_BB. We want to compute
2211 : : probability how often it changes between USE_BB.
2212 : : INIT_BB->count/USE_BB->count is an estimate, but if INIT_BB
2213 : : is in different loop nest, we can do better.
2214 : : This is all just estimate. In theory we look for minimal cut separating
2215 : : INIT_BB and USE_BB, but we only want to anticipate loop invariant motion
2216 : : anyway. */
2217 : :
2218 : : static basic_block
2219 : 9795513 : get_minimal_bb (basic_block init_bb, basic_block use_bb)
2220 : : {
2221 : 9795513 : class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father);
2222 : 9795513 : if (l && l->header->count < init_bb->count)
2223 : 350275 : return l->header;
2224 : : return init_bb;
2225 : : }
2226 : :
2227 : : /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2228 : : set except for info->stmt. */
2229 : :
2230 : : static bool
2231 : 4662154 : record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2232 : : {
2233 : 4662154 : struct record_modified_bb_info *info =
2234 : : (struct record_modified_bb_info *) data;
2235 : 4662154 : if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2236 : : return false;
2237 : 4633796 : if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef)))
2238 : : return false;
2239 : 4570013 : bitmap_set_bit (info->bb_set,
2240 : 4570013 : SSA_NAME_IS_DEFAULT_DEF (vdef)
2241 : 0 : ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2242 : : : get_minimal_bb
2243 : 4570013 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2244 : 4570013 : gimple_bb (info->stmt))->index);
2245 : 4570013 : if (dump_file)
2246 : : {
2247 : 0 : fprintf (dump_file, " Param ");
2248 : 0 : print_generic_expr (dump_file, info->op, TDF_SLIM);
2249 : 0 : fprintf (dump_file, " changed at bb %i, minimal: %i stmt: ",
2250 : 0 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index,
2251 : : get_minimal_bb
2252 : 0 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2253 : 0 : gimple_bb (info->stmt))->index);
2254 : 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (vdef), 0);
2255 : : }
2256 : : return false;
2257 : : }
2258 : :
2259 : : /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2260 : : will change since last invocation of STMT.
2261 : :
2262 : : Value 0 is reserved for compile time invariants.
2263 : : For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2264 : : ought to be REG_BR_PROB_BASE / estimated_iters. */
2265 : :
2266 : : static int
2267 : 34409711 : param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i)
2268 : : {
2269 : 34409711 : tree op = gimple_call_arg (stmt, i);
2270 : 34409711 : basic_block bb = gimple_bb (stmt);
2271 : :
2272 : 34409711 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
2273 : 299 : op = TREE_OPERAND (op, 0);
2274 : :
2275 : 34409711 : tree base = get_base_address (op);
2276 : :
2277 : : /* Global invariants never change. */
2278 : 34409711 : if (is_gimple_min_invariant (base))
2279 : : return 0;
2280 : :
2281 : : /* We would have to do non-trivial analysis to really work out what
2282 : : is the probability of value to change (i.e. when init statement
2283 : : is in a sibling loop of the call).
2284 : :
2285 : : We do an conservative estimate: when call is executed N times more often
2286 : : than the statement defining value, we take the frequency 1/N. */
2287 : 17352099 : if (TREE_CODE (base) == SSA_NAME)
2288 : : {
2289 : 15291366 : profile_count init_count;
2290 : :
2291 : 23389405 : if (!bb->count.nonzero_p ())
2292 : : return REG_BR_PROB_BASE;
2293 : :
2294 : 7953497 : if (SSA_NAME_IS_DEFAULT_DEF (base))
2295 : 2727997 : init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2296 : : else
2297 : 5225500 : init_count = get_minimal_bb
2298 : 5225500 : (gimple_bb (SSA_NAME_DEF_STMT (base)),
2299 : : gimple_bb (stmt))->count;
2300 : :
2301 : 7953497 : if (init_count < bb->count)
2302 : 409883 : return MAX ((init_count.to_sreal_scale (bb->count)
2303 : : * REG_BR_PROB_BASE).to_int (), 1);
2304 : : return REG_BR_PROB_BASE;
2305 : : }
2306 : : else
2307 : : {
2308 : 2060733 : ao_ref refd;
2309 : 2060733 : profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2310 : 2060733 : struct record_modified_bb_info info;
2311 : 2060733 : tree init = ctor_for_folding (base);
2312 : :
2313 : 2060733 : if (init != error_mark_node)
2314 : : return 0;
2315 : 4585162 : if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0)
2316 : : return REG_BR_PROB_BASE;
2317 : 1273379 : if (dump_file)
2318 : : {
2319 : 0 : fprintf (dump_file, " Analyzing param change probability of ");
2320 : 0 : print_generic_expr (dump_file, op, TDF_SLIM);
2321 : 0 : fprintf (dump_file, "\n");
2322 : : }
2323 : 1273379 : ao_ref_init (&refd, op);
2324 : 1273379 : info.op = op;
2325 : 1273379 : info.stmt = stmt;
2326 : 1273379 : info.bb_set = BITMAP_ALLOC (NULL);
2327 : 1273379 : int walked
2328 : 2546758 : = walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2329 : : NULL, NULL, fbi->aa_walk_budget);
2330 : 1273379 : if (walked > 0)
2331 : 1162967 : fbi->aa_walk_budget -= walked;
2332 : 1273379 : if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index))
2333 : : {
2334 : 797009 : if (walked < 0)
2335 : 204 : fbi->aa_walk_budget = 0;
2336 : 797009 : if (dump_file)
2337 : : {
2338 : 0 : if (walked < 0)
2339 : 0 : fprintf (dump_file, " Ran out of AA walking budget.\n");
2340 : : else
2341 : 0 : fprintf (dump_file, " Set in same BB as used.\n");
2342 : : }
2343 : 797009 : BITMAP_FREE (info.bb_set);
2344 : 797009 : return REG_BR_PROB_BASE;
2345 : : }
2346 : :
2347 : 476370 : bitmap_iterator bi;
2348 : 476370 : unsigned index;
2349 : : /* Lookup the most frequent update of the value and believe that
2350 : : it dominates all the other; precise analysis here is difficult. */
2351 : 1482980 : EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2352 : 1006610 : max = max.max (BASIC_BLOCK_FOR_FN (cfun, index)->count);
2353 : 476370 : if (dump_file)
2354 : : {
2355 : 0 : fprintf (dump_file, " Set with count ");
2356 : 0 : max.dump (dump_file);
2357 : 0 : fprintf (dump_file, " and used with count ");
2358 : 0 : bb->count.dump (dump_file);
2359 : 0 : fprintf (dump_file, " freq %f\n",
2360 : 0 : max.to_sreal_scale (bb->count).to_double ());
2361 : : }
2362 : :
2363 : 476370 : BITMAP_FREE (info.bb_set);
2364 : 476370 : if (max < bb->count)
2365 : 79172 : return MAX ((max.to_sreal_scale (bb->count)
2366 : : * REG_BR_PROB_BASE).to_int (), 1);
2367 : : return REG_BR_PROB_BASE;
2368 : : }
2369 : : }
2370 : :
2371 : : /* Find whether a basic block BB is the final block of a (half) diamond CFG
2372 : : sub-graph and if the predicate the condition depends on is known. If so,
2373 : : return true and store the pointer the predicate in *P. */
2374 : :
2375 : : static bool
2376 : 5850339 : phi_result_unknown_predicate (ipa_func_body_info *fbi,
2377 : : ipa_fn_summary *summary,
2378 : : class ipa_node_params *params_summary,
2379 : : basic_block bb,
2380 : : ipa_predicate *p,
2381 : : vec<ipa_predicate> nonconstant_names)
2382 : : {
2383 : 5850339 : edge e;
2384 : 5850339 : edge_iterator ei;
2385 : 5850339 : basic_block first_bb = NULL;
2386 : :
2387 : 5850339 : if (single_pred_p (bb))
2388 : : {
2389 : 20746 : *p = false;
2390 : 20746 : return true;
2391 : : }
2392 : :
2393 : 13355661 : FOR_EACH_EDGE (e, ei, bb->preds)
2394 : : {
2395 : 11379128 : if (single_succ_p (e->src))
2396 : : {
2397 : 15207462 : if (!single_pred_p (e->src))
2398 : : return false;
2399 : 6613999 : if (!first_bb)
2400 : 2894898 : first_bb = single_pred (e->src);
2401 : 3719101 : else if (single_pred (e->src) != first_bb)
2402 : : return false;
2403 : : }
2404 : : else
2405 : : {
2406 : 3773896 : if (!first_bb)
2407 : : first_bb = e->src;
2408 : 1310164 : else if (e->src != first_bb)
2409 : : return false;
2410 : : }
2411 : : }
2412 : :
2413 : 1976533 : if (!first_bb)
2414 : : return false;
2415 : :
2416 : 3953066 : gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb));
2417 : 1932352 : if (!stmt
2418 : 1932352 : || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2419 : 314477 : return false;
2420 : :
2421 : 1662056 : *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary,
2422 : : gimple_cond_lhs (stmt),
2423 : : nonconstant_names);
2424 : 1662056 : if (*p == true)
2425 : : return false;
2426 : : else
2427 : : return true;
2428 : : }
2429 : :
2430 : : /* Given a PHI statement in a function described by inline properties SUMMARY
2431 : : and *P being the predicate describing whether the selected PHI argument is
2432 : : known, store a predicate for the result of the PHI statement into
2433 : : NONCONSTANT_NAMES, if possible. */
2434 : :
2435 : : static void
2436 : 605430 : predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi,
2437 : : ipa_predicate *p,
2438 : : vec<ipa_predicate> nonconstant_names)
2439 : : {
2440 : 605430 : unsigned i;
2441 : :
2442 : 855014 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2443 : : {
2444 : 749058 : tree arg = gimple_phi_arg (phi, i)->def;
2445 : 749058 : if (!is_gimple_min_invariant (arg))
2446 : : {
2447 : 642555 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
2448 : 1285110 : *p = p->or_with (summary->conds,
2449 : 642555 : nonconstant_names[SSA_NAME_VERSION (arg)]);
2450 : 642555 : if (*p == true)
2451 : : return;
2452 : : }
2453 : : }
2454 : :
2455 : 105956 : if (dump_file && (dump_flags & TDF_DETAILS))
2456 : : {
2457 : 3 : fprintf (dump_file, "\t\tphi predicate: ");
2458 : 3 : p->dump (dump_file, summary->conds);
2459 : : }
2460 : 105956 : nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2461 : : }
2462 : :
2463 : : /* For a typical usage of __builtin_expect (a<b, 1), we
2464 : : may introduce an extra relation stmt:
2465 : : With the builtin, we have
2466 : : t1 = a <= b;
2467 : : t2 = (long int) t1;
2468 : : t3 = __builtin_expect (t2, 1);
2469 : : if (t3 != 0)
2470 : : goto ...
2471 : : Without the builtin, we have
2472 : : if (a<=b)
2473 : : goto...
2474 : : This affects the size/time estimation and may have
2475 : : an impact on the earlier inlining.
2476 : : Here find this pattern and fix it up later. */
2477 : :
2478 : : static gimple *
2479 : 37555198 : find_foldable_builtin_expect (basic_block bb)
2480 : : {
2481 : 37555198 : gimple_stmt_iterator bsi;
2482 : :
2483 : 252180646 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2484 : : {
2485 : 177201785 : gimple *stmt = gsi_stmt (bsi);
2486 : 177201785 : if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2487 : 177021470 : || gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY)
2488 : 354223188 : || gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT))
2489 : : {
2490 : 276558 : tree var = gimple_call_lhs (stmt);
2491 : 276558 : tree arg = gimple_call_arg (stmt, 0);
2492 : 276558 : use_operand_p use_p;
2493 : 276558 : gimple *use_stmt;
2494 : 276558 : bool match = false;
2495 : 276558 : bool done = false;
2496 : :
2497 : 276558 : if (!var || !arg)
2498 : 3 : continue;
2499 : 276555 : gcc_assert (TREE_CODE (var) == SSA_NAME);
2500 : :
2501 : 552007 : while (TREE_CODE (arg) == SSA_NAME)
2502 : : {
2503 : 552007 : gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg);
2504 : 552007 : if (!is_gimple_assign (stmt_tmp))
2505 : : break;
2506 : 543491 : switch (gimple_assign_rhs_code (stmt_tmp))
2507 : : {
2508 : : case LT_EXPR:
2509 : : case LE_EXPR:
2510 : : case GT_EXPR:
2511 : : case GE_EXPR:
2512 : : case EQ_EXPR:
2513 : : case NE_EXPR:
2514 : : match = true;
2515 : : done = true;
2516 : : break;
2517 : : CASE_CONVERT:
2518 : : break;
2519 : : default:
2520 : : done = true;
2521 : : break;
2522 : : }
2523 : 275452 : if (done)
2524 : : break;
2525 : 275452 : arg = gimple_assign_rhs1 (stmt_tmp);
2526 : : }
2527 : :
2528 : 237360 : if (match && single_imm_use (var, &use_p, &use_stmt)
2529 : 513613 : && gimple_code (use_stmt) == GIMPLE_COND)
2530 : 131535 : return use_stmt;
2531 : : }
2532 : : }
2533 : : return NULL;
2534 : : }
2535 : :
2536 : : /* Return true when the basic blocks contains only clobbers followed by RESX.
2537 : : Such BBs are kept around to make removal of dead stores possible with
2538 : : presence of EH and will be optimized out by optimize_clobbers later in the
2539 : : game.
2540 : :
2541 : : NEED_EH is used to recurse in case the clobber has non-EH predecessors
2542 : : that can be clobber only, too.. When it is false, the RESX is not necessary
2543 : : on the end of basic block. */
2544 : :
2545 : : static bool
2546 : 38053716 : clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2547 : : {
2548 : 38053716 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2549 : 38053716 : edge_iterator ei;
2550 : 38053716 : edge e;
2551 : :
2552 : 38053716 : if (need_eh)
2553 : : {
2554 : 37988322 : if (gsi_end_p (gsi))
2555 : : return false;
2556 : 36866923 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2557 : : return false;
2558 : 870839 : gsi_prev (&gsi);
2559 : : }
2560 : 130788 : else if (!single_succ_p (bb))
2561 : : return false;
2562 : :
2563 : 4178505 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2564 : : {
2565 : 2471941 : gimple *stmt = gsi_stmt (gsi);
2566 : 2471941 : if (is_gimple_debug (stmt))
2567 : 925232 : continue;
2568 : 1546709 : if (gimple_clobber_p (stmt))
2569 : 725724 : continue;
2570 : 820985 : if (gimple_code (stmt) == GIMPLE_LABEL)
2571 : : break;
2572 : : return false;
2573 : : }
2574 : :
2575 : : /* See if all predecessors are either throws or clobber only BBs. */
2576 : 2943316 : FOR_EACH_EDGE (e, ei, bb->preds)
2577 : 2505347 : if (!(e->flags & EDGE_EH)
2578 : 2505347 : && !clobber_only_eh_bb_p (e->src, false))
2579 : : return false;
2580 : :
2581 : : return true;
2582 : : }
2583 : :
2584 : : /* Return true if STMT compute a floating point expression that may be affected
2585 : : by -ffast-math and similar flags. */
2586 : :
2587 : : static bool
2588 : 86268649 : fp_expression_p (gimple *stmt)
2589 : : {
2590 : 86268649 : ssa_op_iter i;
2591 : 86268649 : tree op;
2592 : :
2593 : 193121139 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE)
2594 : 107402255 : if (FLOAT_TYPE_P (TREE_TYPE (op)))
2595 : : return true;
2596 : : return false;
2597 : : }
2598 : :
2599 : : /* Return true if T references memory location that is local
2600 : : for the function (that means, dead after return) or read-only. */
2601 : :
2602 : : bool
2603 : 52288987 : refs_local_or_readonly_memory_p (tree t)
2604 : : {
2605 : : /* Non-escaping memory is fine. */
2606 : 52288987 : t = get_base_address (t);
2607 : 52288987 : if ((TREE_CODE (t) == MEM_REF
2608 : 52288987 : || TREE_CODE (t) == TARGET_MEM_REF))
2609 : 22386733 : return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2610 : :
2611 : : /* Automatic variables are fine. */
2612 : 29902254 : if (DECL_P (t)
2613 : 29902254 : && auto_var_in_fn_p (t, current_function_decl))
2614 : : return true;
2615 : :
2616 : : /* Read-only variables are fine. */
2617 : 10216466 : if (DECL_P (t) && TREE_READONLY (t))
2618 : : return true;
2619 : :
2620 : : return false;
2621 : : }
2622 : :
2623 : : /* Return true if T is a pointer pointing to memory location that is local
2624 : : for the function (that means, dead after return) or read-only. */
2625 : :
2626 : : bool
2627 : 64596863 : points_to_local_or_readonly_memory_p (tree t)
2628 : : {
2629 : : /* See if memory location is clearly invalid. */
2630 : 64596863 : if (integer_zerop (t))
2631 : 2069200 : return flag_delete_null_pointer_checks;
2632 : 62527663 : if (TREE_CODE (t) == SSA_NAME)
2633 : : {
2634 : : /* For IPA passes we can consinder accesses to return slot local
2635 : : even if it is not local in the sense that memory is dead by
2636 : : the end of founction.
2637 : : The outer function will see a store in the call assignment
2638 : : and thus this will do right thing for all uses of this
2639 : : function in the current IPA passes (modref, pure/const discovery
2640 : : and inlining heuristics). */
2641 : 39489818 : if (DECL_RESULT (current_function_decl)
2642 : 39489818 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
2643 : 40228794 : && t == ssa_default_def (cfun, DECL_RESULT (current_function_decl)))
2644 : : return true;
2645 : 39268475 : return !ptr_deref_may_alias_global_p (t, false);
2646 : : }
2647 : 23037845 : if (TREE_CODE (t) == ADDR_EXPR)
2648 : 11673078 : return refs_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2649 : : return false;
2650 : : }
2651 : :
2652 : : /* Return true if T is a pointer pointing to memory location that is possible
2653 : : sra candidate if all functions it is passed to are inlined. */
2654 : :
2655 : : static bool
2656 : 34409711 : points_to_possible_sra_candidate_p (tree t)
2657 : : {
2658 : 34409711 : if (TREE_CODE (t) != ADDR_EXPR)
2659 : : return false;
2660 : :
2661 : 10035457 : t = get_base_address (TREE_OPERAND (t, 0));
2662 : :
2663 : : /* Automatic variables are fine. */
2664 : 10035457 : if (DECL_P (t)
2665 : 10035457 : && auto_var_in_fn_p (t, current_function_decl))
2666 : : return true;
2667 : : return false;
2668 : : }
2669 : :
2670 : : /* Analyze function body for NODE.
2671 : : EARLY indicates run from early optimization pipeline. */
2672 : :
2673 : : static void
2674 : 6566827 : analyze_function_body (struct cgraph_node *node, bool early)
2675 : : {
2676 : 6566827 : sreal time = opt_for_fn (node->decl, param_uninlined_function_time);
2677 : : /* Estimate static overhead for function prologue/epilogue and alignment. */
2678 : 6566827 : int size = opt_for_fn (node->decl, param_uninlined_function_insns);
2679 : : /* Benefits are scaled by probability of elimination that is in range
2680 : : <0,2>. */
2681 : 6566827 : basic_block bb;
2682 : 6566827 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2683 : 6566827 : sreal freq;
2684 : 6566827 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
2685 : 13133654 : ipa_node_params *params_summary
2686 : 6566827 : = early ? NULL : ipa_node_params_sum->get (node);
2687 : 6566827 : ipa_predicate bb_predicate;
2688 : 6566827 : struct ipa_func_body_info fbi;
2689 : 6566827 : vec<ipa_predicate> nonconstant_names = vNULL;
2690 : 6566827 : int nblocks, n;
2691 : 6566827 : int *order;
2692 : 6566827 : gimple *fix_builtin_expect_stmt;
2693 : :
2694 : 6566827 : gcc_assert (my_function && my_function->cfg);
2695 : 6566827 : gcc_assert (cfun == my_function);
2696 : :
2697 : 6566827 : memset(&fbi, 0, sizeof(fbi));
2698 : 6566827 : vec_free (info->conds);
2699 : 6566827 : info->conds = NULL;
2700 : 6566827 : info->size_time_table.release ();
2701 : 6566827 : info->call_size_time_table.release ();
2702 : :
2703 : : /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2704 : : so we can produce proper inline hints.
2705 : :
2706 : : When optimizing and analyzing for early inliner, initialize node params
2707 : : so we can produce correct BB predicates. */
2708 : :
2709 : 6566827 : if (opt_for_fn (node->decl, optimize))
2710 : : {
2711 : 5659981 : calculate_dominance_info (CDI_DOMINATORS);
2712 : 5659981 : calculate_dominance_info (CDI_POST_DOMINATORS);
2713 : 5659981 : if (!early)
2714 : 1265855 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2715 : : else
2716 : : {
2717 : 4394126 : ipa_check_create_node_params ();
2718 : 4394126 : ipa_initialize_node_params (node);
2719 : : }
2720 : :
2721 : 5659981 : if (ipa_node_params_sum)
2722 : : {
2723 : 5659981 : fbi.node = node;
2724 : 5659981 : fbi.info = ipa_node_params_sum->get (node);
2725 : 5659981 : fbi.bb_infos = vNULL;
2726 : 5659981 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
2727 : 5659981 : fbi.param_count = count_formal_params (node->decl);
2728 : 5659981 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
2729 : :
2730 : 5659981 : nonconstant_names.safe_grow_cleared
2731 : 5659981 : (SSANAMES (my_function)->length (), true);
2732 : : }
2733 : : }
2734 : :
2735 : 6566827 : if (dump_file)
2736 : 237 : fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2737 : : node->dump_name ());
2738 : :
2739 : : /* When we run into maximal number of entries, we assign everything to the
2740 : : constant truth case. Be sure to have it in list. */
2741 : 6566827 : bb_predicate = true;
2742 : 6566827 : info->account_size_time (0, 0, bb_predicate, bb_predicate);
2743 : :
2744 : 6566827 : bb_predicate = ipa_predicate::not_inlined ();
2745 : 6566827 : info->account_size_time (opt_for_fn (node->decl,
2746 : : param_uninlined_function_insns)
2747 : : * ipa_fn_summary::size_scale,
2748 : 6566827 : opt_for_fn (node->decl,
2749 : : param_uninlined_function_time),
2750 : : bb_predicate,
2751 : : bb_predicate);
2752 : :
2753 : : /* Only look for target information for inlinable functions. */
2754 : 6566827 : bool scan_for_target_info =
2755 : 6566827 : info->inlinable
2756 : 11677057 : && targetm.target_option.need_ipa_fn_target_info (node->decl,
2757 : 5110230 : info->target_info);
2758 : :
2759 : 6566827 : if (fbi.info)
2760 : 5659981 : compute_bb_predicates (&fbi, node, info, params_summary);
2761 : 6566827 : const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2762 : 6566827 : order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2763 : 6566827 : nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2764 : 44555149 : for (n = 0; n < nblocks; n++)
2765 : : {
2766 : 37988322 : bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2767 : 37988322 : freq = bb->count.to_sreal_scale (entry_count);
2768 : 37988322 : if (clobber_only_eh_bb_p (bb))
2769 : : {
2770 : 433124 : if (dump_file && (dump_flags & TDF_DETAILS))
2771 : 0 : fprintf (dump_file, "\n Ignoring BB %i;"
2772 : : " it will be optimized away by cleanup_clobbers\n",
2773 : : bb->index);
2774 : 433124 : continue;
2775 : : }
2776 : :
2777 : : /* TODO: Obviously predicates can be propagated down across CFG. */
2778 : 37555198 : if (fbi.info)
2779 : : {
2780 : 31184894 : if (bb->aux)
2781 : 31184880 : bb_predicate = *(ipa_predicate *)bb->aux;
2782 : : else
2783 : 14 : bb_predicate = false;
2784 : : }
2785 : : else
2786 : 6370304 : bb_predicate = true;
2787 : :
2788 : 37555198 : if (dump_file && (dump_flags & TDF_DETAILS))
2789 : : {
2790 : 67 : fprintf (dump_file, "\n BB %i predicate:", bb->index);
2791 : 67 : bb_predicate.dump (dump_file, info->conds);
2792 : : }
2793 : :
2794 : 37555198 : if (fbi.info && nonconstant_names.exists ())
2795 : : {
2796 : 31184894 : ipa_predicate phi_predicate;
2797 : 31184894 : bool first_phi = true;
2798 : :
2799 : 31790324 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2800 : 605430 : gsi_next (&bsi))
2801 : : {
2802 : 5915743 : if (first_phi
2803 : 5915743 : && !phi_result_unknown_predicate (&fbi, info,
2804 : : params_summary,
2805 : : bb,
2806 : : &phi_predicate,
2807 : : nonconstant_names))
2808 : : break;
2809 : 605430 : first_phi = false;
2810 : 605430 : if (dump_file && (dump_flags & TDF_DETAILS))
2811 : : {
2812 : 3 : fprintf (dump_file, " ");
2813 : 3 : print_gimple_stmt (dump_file, gsi_stmt (bsi), 0);
2814 : : }
2815 : 605430 : predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2816 : : nonconstant_names);
2817 : : }
2818 : : }
2819 : :
2820 : 37555198 : fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2821 : :
2822 : 37555198 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
2823 : 167023288 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
2824 : : {
2825 : 129468090 : gimple *stmt = gsi_stmt (bsi);
2826 : 129468090 : int this_size = estimate_num_insns (stmt, &eni_size_weights);
2827 : 129468090 : int this_time = estimate_num_insns (stmt, &eni_time_weights);
2828 : 129468090 : int prob;
2829 : 129468090 : ipa_predicate will_be_nonconstant;
2830 : :
2831 : : /* This relation stmt should be folded after we remove
2832 : : __builtin_expect call. Adjust the cost here. */
2833 : 129468090 : if (stmt == fix_builtin_expect_stmt)
2834 : : {
2835 : 131535 : this_size--;
2836 : 131535 : this_time--;
2837 : : }
2838 : :
2839 : 129468090 : if (dump_file && (dump_flags & TDF_DETAILS))
2840 : : {
2841 : 222 : fprintf (dump_file, " ");
2842 : 222 : print_gimple_stmt (dump_file, stmt, 0);
2843 : 222 : fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2844 : : freq.to_double (), this_size,
2845 : : this_time);
2846 : : }
2847 : :
2848 : 129468090 : if (is_gimple_call (stmt)
2849 : 129468090 : && !gimple_call_internal_p (stmt))
2850 : : {
2851 : 20695440 : struct cgraph_edge *edge = node->get_edge (stmt);
2852 : 20695440 : ipa_call_summary *es = ipa_call_summaries->get_create (edge);
2853 : :
2854 : : /* Special case: results of BUILT_IN_CONSTANT_P will be always
2855 : : resolved as constant. We however don't want to optimize
2856 : : out the cgraph edges. */
2857 : 20695440 : if (nonconstant_names.exists ()
2858 : 17823018 : && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2859 : 5402 : && gimple_call_lhs (stmt)
2860 : 20700842 : && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2861 : : {
2862 : 5402 : ipa_predicate false_p = false;
2863 : 5402 : nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2864 : 5402 : = false_p;
2865 : : }
2866 : 20695440 : if (ipa_node_params_sum)
2867 : : {
2868 : 17836173 : int count = gimple_call_num_args (stmt);
2869 : 17836173 : int i;
2870 : :
2871 : 17836173 : if (count)
2872 : 15292523 : es->param.safe_grow_cleared (count, true);
2873 : 52245884 : for (i = 0; i < count; i++)
2874 : : {
2875 : 34409711 : int prob = param_change_prob (&fbi, stmt, i);
2876 : 34409711 : gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2877 : 34409711 : es->param[i].change_prob = prob;
2878 : 34409711 : es->param[i].points_to_local_or_readonly_memory
2879 : 34409711 : = points_to_local_or_readonly_memory_p
2880 : 34409711 : (gimple_call_arg (stmt, i));
2881 : 34409711 : es->param[i].points_to_possible_sra_candidate
2882 : 34409711 : = points_to_possible_sra_candidate_p
2883 : 34409711 : (gimple_call_arg (stmt, i));
2884 : : }
2885 : : }
2886 : : /* We cannot setup VLA parameters during inlining. */
2887 : 60871570 : for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i)
2888 : 40176526 : if (TREE_CODE (gimple_call_arg (stmt, i)) == WITH_SIZE_EXPR)
2889 : : {
2890 : 396 : edge->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
2891 : 396 : break;
2892 : : }
2893 : 20695440 : es->call_stmt_size = this_size;
2894 : 20695440 : es->call_stmt_time = this_time;
2895 : 20695440 : es->loop_depth = bb_loop_depth (bb);
2896 : 20695440 : edge_set_predicate (edge, &bb_predicate);
2897 : 20695440 : if (edge->speculative)
2898 : : {
2899 : 0 : cgraph_edge *indirect
2900 : 0 : = edge->speculative_call_indirect_edge ();
2901 : 0 : ipa_call_summary *es2
2902 : 0 : = ipa_call_summaries->get_create (indirect);
2903 : 0 : ipa_call_summaries->duplicate (edge, indirect,
2904 : : es, es2);
2905 : :
2906 : : /* Edge is the first direct call.
2907 : : create and duplicate call summaries for multiple
2908 : : speculative call targets. */
2909 : 0 : for (cgraph_edge *direct
2910 : 0 : = edge->next_speculative_call_target ();
2911 : 0 : direct;
2912 : 0 : direct = direct->next_speculative_call_target ())
2913 : : {
2914 : 0 : ipa_call_summary *es3
2915 : 0 : = ipa_call_summaries->get_create (direct);
2916 : 0 : ipa_call_summaries->duplicate (edge, direct,
2917 : : es, es3);
2918 : : }
2919 : : }
2920 : : }
2921 : :
2922 : : /* TODO: When conditional jump or switch is known to be constant, but
2923 : : we did not translate it into the predicates, we really can account
2924 : : just maximum of the possible paths. */
2925 : 129468090 : if (fbi.info)
2926 : 106359410 : will_be_nonconstant
2927 : 106359410 : = will_be_nonconstant_predicate (&fbi, info, params_summary,
2928 : : stmt, nonconstant_names);
2929 : : else
2930 : 23108680 : will_be_nonconstant = true;
2931 : 129468090 : if (this_time || this_size)
2932 : : {
2933 : 102768156 : sreal final_time = (sreal)this_time * freq;
2934 : 102768156 : prob = eliminated_by_inlining_prob (&fbi, stmt);
2935 : 102768156 : if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2936 : 11 : fprintf (dump_file,
2937 : : "\t\t50%% will be eliminated by inlining\n");
2938 : 102768156 : if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2939 : 18 : fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2940 : :
2941 : 102768156 : ipa_predicate p = bb_predicate & will_be_nonconstant;
2942 : 102768156 : int parm = load_or_store_of_ptr_parameter (&fbi, stmt);
2943 : 102768156 : ipa_predicate sra_predicate = true;
2944 : 102768156 : if (parm != -1)
2945 : 12551008 : sra_predicate &= add_condition (info, params_summary, parm,
2946 : : ptr_type_node, NULL,
2947 : 6275504 : ipa_predicate::not_sra_candidate, NULL, 0);
2948 : :
2949 : : /* We can ignore statement when we proved it is never going
2950 : : to happen, but we cannot do that for call statements
2951 : : because edges are accounted specially. */
2952 : :
2953 : 205536312 : if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false)
2954 : : {
2955 : 102313570 : time += final_time;
2956 : 102313570 : size += this_size;
2957 : : }
2958 : :
2959 : : /* We account everything but the calls. Calls have their own
2960 : : size/time info attached to cgraph edges. This is necessary
2961 : : in order to make the cost disappear after inlining. */
2962 : 102768156 : if (!is_gimple_call (stmt))
2963 : : {
2964 : 82491950 : if (prob)
2965 : : {
2966 : 13319291 : ipa_predicate ip
2967 : 13319291 : = bb_predicate & ipa_predicate::not_inlined () & sra_predicate;
2968 : 26638582 : info->account_size_time (this_size * prob,
2969 : 13319291 : (final_time * prob) / 2, ip,
2970 : : p);
2971 : : }
2972 : 13319291 : if (prob != 2)
2973 : 152639516 : info->account_size_time (this_size * (2 - prob),
2974 : 76319758 : (final_time * (2 - prob) / 2),
2975 : 152639516 : bb_predicate & sra_predicate,
2976 : : p);
2977 : : }
2978 : :
2979 : 102768156 : if (!info->fp_expressions && fp_expression_p (stmt))
2980 : : {
2981 : 549765 : info->fp_expressions = true;
2982 : 549765 : if (dump_file)
2983 : 11 : fprintf (dump_file, " fp_expression set\n");
2984 : : }
2985 : : }
2986 : :
2987 : : /* For target specific information, we want to scan all statements
2988 : : rather than those statements with non-zero weights, to avoid
2989 : : missing to scan something interesting for target information,
2990 : : such as: internal function calls. */
2991 : 129468090 : if (scan_for_target_info)
2992 : 0 : scan_for_target_info =
2993 : 0 : targetm.target_option.update_ipa_fn_target_info
2994 : 0 : (info->target_info, stmt);
2995 : :
2996 : : /* Account cost of address calculations in the statements. */
2997 : 491243218 : for (unsigned int i = 0; i < gimple_num_ops (stmt); i++)
2998 : : {
2999 : 361775128 : for (tree op = gimple_op (stmt, i);
3000 : 698387928 : op && handled_component_p (op);
3001 : 43963702 : op = TREE_OPERAND (op, 0))
3002 : 43963702 : if ((TREE_CODE (op) == ARRAY_REF
3003 : 43963702 : || TREE_CODE (op) == ARRAY_RANGE_REF)
3004 : 43963702 : && TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
3005 : : {
3006 : 2160724 : ipa_predicate p = bb_predicate;
3007 : 2160724 : if (fbi.info)
3008 : 1659259 : p = p & will_be_nonconstant_expr_predicate
3009 : 1659259 : (&fbi, info, params_summary,
3010 : 1659259 : TREE_OPERAND (op, 1),
3011 : 1659259 : nonconstant_names);
3012 : 2160724 : if (p != false)
3013 : : {
3014 : 2155712 : time += freq;
3015 : 2155712 : size += 1;
3016 : 2155712 : if (dump_file)
3017 : 24 : fprintf (dump_file,
3018 : : "\t\tAccounting address calculation.\n");
3019 : 2155712 : info->account_size_time (ipa_fn_summary::size_scale,
3020 : : freq,
3021 : : bb_predicate,
3022 : : p);
3023 : : }
3024 : : }
3025 : : }
3026 : :
3027 : : }
3028 : : }
3029 : 6566827 : free (order);
3030 : :
3031 : 6566827 : if (nonconstant_names.exists () && !early)
3032 : : {
3033 : 1265855 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3034 : 1265855 : unsigned max_loop_predicates = opt_for_fn (node->decl,
3035 : : param_ipa_max_loop_predicates);
3036 : :
3037 : 1265855 : if (dump_file && (dump_flags & TDF_DETAILS))
3038 : 14 : flow_loops_dump (dump_file, NULL, 0);
3039 : 1265855 : scev_initialize ();
3040 : 4349761 : for (auto loop : loops_list (cfun, 0))
3041 : : {
3042 : 552196 : ipa_predicate loop_iterations = true;
3043 : 552196 : sreal header_freq;
3044 : 552196 : edge ex;
3045 : 552196 : unsigned int j;
3046 : 552196 : class tree_niter_desc niter_desc;
3047 : 552196 : if (!loop->header->aux)
3048 : 0 : continue;
3049 : :
3050 : 552196 : profile_count phdr_count = loop_preheader_edge (loop)->count ();
3051 : 552196 : sreal phdr_freq = phdr_count.to_sreal_scale (entry_count);
3052 : :
3053 : 552196 : bb_predicate = *(ipa_predicate *)loop->header->aux;
3054 : 552196 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3055 : 1515986 : FOR_EACH_VEC_ELT (exits, j, ex)
3056 : 963790 : if (number_of_iterations_exit (loop, ex, &niter_desc, false)
3057 : 963790 : && !is_gimple_min_invariant (niter_desc.niter))
3058 : : {
3059 : 134247 : ipa_predicate will_be_nonconstant
3060 : 134247 : = will_be_nonconstant_expr_predicate (&fbi, info,
3061 : : params_summary,
3062 : : niter_desc.niter,
3063 : : nonconstant_names);
3064 : 134247 : if (will_be_nonconstant != true)
3065 : 53597 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3066 : 134247 : if (will_be_nonconstant != true
3067 : 187844 : && will_be_nonconstant != false)
3068 : 53068 : loop_iterations &= will_be_nonconstant;
3069 : : }
3070 : 552196 : add_freqcounting_predicate (&s->loop_iterations, loop_iterations,
3071 : : phdr_freq, max_loop_predicates);
3072 : 552196 : }
3073 : :
3074 : : /* To avoid quadratic behavior we analyze stride predicates only
3075 : : with respect to the containing loop. Thus we simply iterate
3076 : : over all defs in the outermost loop body. */
3077 : 1265855 : for (class loop *loop = loops_for_fn (cfun)->tree_root->inner;
3078 : 1704868 : loop != NULL; loop = loop->next)
3079 : : {
3080 : 439013 : ipa_predicate loop_stride = true;
3081 : 439013 : basic_block *body = get_loop_body (loop);
3082 : 439013 : profile_count phdr_count = loop_preheader_edge (loop)->count ();
3083 : 439013 : sreal phdr_freq = phdr_count.to_sreal_scale (entry_count);
3084 : 2635945 : for (unsigned i = 0; i < loop->num_nodes; i++)
3085 : : {
3086 : 2196932 : gimple_stmt_iterator gsi;
3087 : 2196932 : if (!body[i]->aux)
3088 : 7 : continue;
3089 : :
3090 : 2196925 : bb_predicate = *(ipa_predicate *)body[i]->aux;
3091 : 13828855 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
3092 : 9435005 : gsi_next (&gsi))
3093 : : {
3094 : 9435005 : gimple *stmt = gsi_stmt (gsi);
3095 : :
3096 : 9435005 : if (!is_gimple_assign (stmt))
3097 : 9299929 : continue;
3098 : :
3099 : 4911125 : tree def = gimple_assign_lhs (stmt);
3100 : 4911125 : if (TREE_CODE (def) != SSA_NAME)
3101 : 971518 : continue;
3102 : :
3103 : 3939607 : affine_iv iv;
3104 : 7879214 : if (!simple_iv (loop_containing_stmt (stmt),
3105 : : loop_containing_stmt (stmt),
3106 : : def, &iv, true)
3107 : 3939607 : || is_gimple_min_invariant (iv.step))
3108 : 3804531 : continue;
3109 : :
3110 : 135076 : ipa_predicate will_be_nonconstant
3111 : 135076 : = will_be_nonconstant_expr_predicate (&fbi, info,
3112 : : params_summary,
3113 : : iv.step,
3114 : : nonconstant_names);
3115 : 135076 : if (will_be_nonconstant != true)
3116 : 47616 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3117 : 135076 : if (will_be_nonconstant != true
3118 : 182692 : && will_be_nonconstant != false)
3119 : 28756 : loop_stride = loop_stride & will_be_nonconstant;
3120 : : }
3121 : : }
3122 : 439013 : add_freqcounting_predicate (&s->loop_strides, loop_stride,
3123 : : phdr_freq, max_loop_predicates);
3124 : 439013 : free (body);
3125 : : }
3126 : 1265855 : scev_finalize ();
3127 : : }
3128 : 57688803 : FOR_ALL_BB_FN (bb, my_function)
3129 : : {
3130 : 51121976 : edge e;
3131 : 51121976 : edge_iterator ei;
3132 : :
3133 : 51121976 : if (bb->aux)
3134 : 37214463 : edge_predicate_pool.remove ((ipa_predicate *)bb->aux);
3135 : 51121976 : bb->aux = NULL;
3136 : 109122151 : FOR_EACH_EDGE (e, ei, bb->succs)
3137 : : {
3138 : 58000175 : if (e->aux)
3139 : 2067773 : edge_predicate_pool.remove ((ipa_predicate *)e->aux);
3140 : 58000175 : e->aux = NULL;
3141 : : }
3142 : : }
3143 : 6566827 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3144 : 6566827 : ipa_size_summary *ss = ipa_size_summaries->get (node);
3145 : 6566827 : s->time = time;
3146 : 6566827 : ss->self_size = size;
3147 : 6566827 : nonconstant_names.release ();
3148 : 6566827 : ipa_release_body_info (&fbi);
3149 : 6566827 : if (opt_for_fn (node->decl, optimize))
3150 : : {
3151 : 5659981 : if (!early)
3152 : 1265855 : loop_optimizer_finalize ();
3153 : 4394126 : else if (!ipa_edge_args_sum)
3154 : 4394112 : ipa_free_all_node_params ();
3155 : 5659981 : free_dominance_info (CDI_DOMINATORS);
3156 : 5659981 : free_dominance_info (CDI_POST_DOMINATORS);
3157 : : }
3158 : 6566827 : if (dump_file)
3159 : : {
3160 : 237 : fprintf (dump_file, "\n");
3161 : 237 : ipa_dump_fn_summary (dump_file, node);
3162 : : }
3163 : 6566827 : }
3164 : :
3165 : :
3166 : : /* Compute function summary.
3167 : : EARLY is true when we compute parameters during early opts. */
3168 : :
3169 : : void
3170 : 6568248 : compute_fn_summary (struct cgraph_node *node, bool early)
3171 : : {
3172 : 6568248 : HOST_WIDE_INT self_stack_size;
3173 : 6568248 : struct cgraph_edge *e;
3174 : :
3175 : 6568248 : gcc_assert (!node->inlined_to);
3176 : :
3177 : 6568248 : if (!ipa_fn_summaries)
3178 : 211946 : ipa_fn_summary_alloc ();
3179 : :
3180 : : /* Create a new ipa_fn_summary. */
3181 : 6568248 : ((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node);
3182 : 6568248 : ipa_fn_summaries->remove (node);
3183 : 6568248 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
3184 : 6568248 : class ipa_size_summary *size_info = ipa_size_summaries->get_create (node);
3185 : :
3186 : : /* Estimate the stack size for the function if we're optimizing. */
3187 : 5661386 : self_stack_size = optimize && !node->thunk
3188 : 12228229 : ? estimated_stack_frame_size (node) : 0;
3189 : 6568248 : size_info->estimated_self_stack_size = self_stack_size;
3190 : 6568248 : info->estimated_stack_size = self_stack_size;
3191 : :
3192 : 6568248 : if (node->thunk)
3193 : : {
3194 : 1421 : ipa_call_summary *es = ipa_call_summaries->get_create (node->callees);
3195 : 1421 : ipa_predicate t = true;
3196 : :
3197 : 1421 : node->can_change_signature = false;
3198 : 1421 : es->call_stmt_size = eni_size_weights.call_cost;
3199 : 1421 : es->call_stmt_time = eni_time_weights.call_cost;
3200 : 7105 : info->account_size_time (ipa_fn_summary::size_scale
3201 : 1421 : * opt_for_fn (node->decl,
3202 : : param_uninlined_function_thunk_insns),
3203 : 1421 : opt_for_fn (node->decl,
3204 : : param_uninlined_function_thunk_time), t, t);
3205 : 1421 : t = ipa_predicate::not_inlined ();
3206 : 1421 : info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t);
3207 : 1421 : ipa_update_overall_fn_summary (node);
3208 : 1421 : size_info->self_size = size_info->size;
3209 : 1421 : if (stdarg_p (TREE_TYPE (node->decl)))
3210 : : {
3211 : 10 : info->inlinable = false;
3212 : 10 : node->callees->inline_failed = CIF_VARIADIC_THUNK;
3213 : : }
3214 : : else
3215 : 1411 : info->inlinable = true;
3216 : : }
3217 : : else
3218 : : {
3219 : : /* Even is_gimple_min_invariant rely on current_function_decl. */
3220 : 6566827 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3221 : :
3222 : : /* During IPA profile merging we may be called w/o virtual SSA form
3223 : : built. */
3224 : 6566827 : update_ssa (TODO_update_ssa_only_virtuals);
3225 : :
3226 : : /* Can this function be inlined at all? */
3227 : 6566827 : if (!opt_for_fn (node->decl, optimize)
3228 : 7473673 : && !lookup_attribute ("always_inline",
3229 : 906846 : DECL_ATTRIBUTES (node->decl)))
3230 : 851302 : info->inlinable = false;
3231 : : else
3232 : 5715525 : info->inlinable = tree_inlinable_function_p (node->decl);
3233 : :
3234 : 6566827 : bool no_signature = false;
3235 : : /* Type attributes can use parameter indices to describe them.
3236 : : Special case fn spec since we can safely preserve them in
3237 : : modref summaries. */
3238 : 6566827 : for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl));
3239 : 6918713 : list && !no_signature; list = TREE_CHAIN (list))
3240 : 351886 : if (!ipa_param_adjustments::type_attribute_allowed_p
3241 : 351886 : (get_attribute_name (list)))
3242 : : {
3243 : 151170 : if (dump_file)
3244 : : {
3245 : 0 : fprintf (dump_file, "No signature change:"
3246 : : " function type has unhandled attribute %s.\n",
3247 : 0 : IDENTIFIER_POINTER (get_attribute_name (list)));
3248 : : }
3249 : : no_signature = true;
3250 : : }
3251 : 6566827 : for (tree parm = DECL_ARGUMENTS (node->decl);
3252 : 19597444 : parm && !no_signature; parm = DECL_CHAIN (parm))
3253 : 13030617 : if (variably_modified_type_p (TREE_TYPE (parm), node->decl))
3254 : : {
3255 : 13760 : if (dump_file)
3256 : : {
3257 : 0 : fprintf (dump_file, "No signature change:"
3258 : : " has parameter with variably modified type.\n");
3259 : : }
3260 : : no_signature = true;
3261 : : }
3262 : :
3263 : : /* Likewise for #pragma omp declare simd functions or functions
3264 : : with simd attribute. */
3265 : 6566827 : if (no_signature
3266 : 12968724 : || lookup_attribute ("omp declare simd",
3267 : 6401897 : DECL_ATTRIBUTES (node->decl)))
3268 : 166749 : node->can_change_signature = false;
3269 : : else
3270 : : {
3271 : : /* Otherwise, inlinable functions always can change signature. */
3272 : 6400078 : if (info->inlinable)
3273 : 5036024 : node->can_change_signature = true;
3274 : : else
3275 : : {
3276 : : /* Functions calling builtin_apply cannot change signature. */
3277 : 5340157 : for (e = node->callees; e; e = e->next_callee)
3278 : : {
3279 : 4008241 : tree cdecl = e->callee->decl;
3280 : 4008241 : if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS,
3281 : : BUILT_IN_VA_START))
3282 : : break;
3283 : : }
3284 : 1364054 : node->can_change_signature = !e;
3285 : : }
3286 : : }
3287 : 6566827 : analyze_function_body (node, early);
3288 : 6566827 : pop_cfun ();
3289 : : }
3290 : :
3291 : : /* Inlining characteristics are maintained by the cgraph_mark_inline. */
3292 : 6568248 : size_info->size = size_info->self_size;
3293 : 6568248 : info->estimated_stack_size = size_info->estimated_self_stack_size;
3294 : :
3295 : : /* Code above should compute exactly the same result as
3296 : : ipa_update_overall_fn_summary except for case when speculative
3297 : : edges are present since these are accounted to size but not
3298 : : self_size. Do not compare time since different order the roundoff
3299 : : errors result in slight changes. */
3300 : 6568248 : ipa_update_overall_fn_summary (node);
3301 : 6568248 : if (flag_checking)
3302 : : {
3303 : 7034881 : for (e = node->indirect_calls; e; e = e->next_callee)
3304 : 466734 : if (e->speculative)
3305 : : break;
3306 : 6568147 : gcc_assert (e || size_info->size == size_info->self_size);
3307 : : }
3308 : 6568248 : }
3309 : :
3310 : :
3311 : : /* Compute parameters of functions used by inliner using
3312 : : current_function_decl. */
3313 : :
3314 : : static unsigned int
3315 : 5246891 : compute_fn_summary_for_current (void)
3316 : : {
3317 : 5246891 : compute_fn_summary (cgraph_node::get (current_function_decl), true);
3318 : 5246891 : return 0;
3319 : : }
3320 : :
3321 : : /* Estimate benefit devirtualizing indirect edge IE and return true if it can
3322 : : be devirtualized and inlined, provided m_known_vals, m_known_contexts and
3323 : : m_known_aggs in AVALS. Return false straight away if AVALS is NULL. */
3324 : :
3325 : : static bool
3326 : 397590 : estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3327 : : int *size, int *time,
3328 : : ipa_call_arg_values *avals)
3329 : : {
3330 : 397590 : tree target;
3331 : 397590 : struct cgraph_node *callee;
3332 : 397590 : class ipa_fn_summary *isummary;
3333 : 397590 : enum availability avail;
3334 : 397590 : bool speculative;
3335 : :
3336 : 397590 : if (!avals
3337 : 584831 : || (!avals->m_known_vals.length() && !avals->m_known_contexts.length ()))
3338 : : return false;
3339 : 373700 : if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3340 : : return false;
3341 : :
3342 : 372908 : target = ipa_get_indirect_edge_target (ie, avals, &speculative);
3343 : 372908 : if (!target || speculative)
3344 : : return false;
3345 : :
3346 : : /* Account for difference in cost between indirect and direct calls. */
3347 : 199747 : *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3348 : 199747 : *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3349 : 199747 : gcc_checking_assert (*time >= 0);
3350 : 199747 : gcc_checking_assert (*size >= 0);
3351 : :
3352 : 199747 : callee = cgraph_node::get (target);
3353 : 199747 : if (!callee || !callee->definition)
3354 : : return false;
3355 : 186463 : callee = callee->function_symbol (&avail);
3356 : 186463 : if (avail < AVAIL_AVAILABLE)
3357 : : return false;
3358 : 186463 : isummary = ipa_fn_summaries->get (callee);
3359 : 186463 : if (isummary == NULL)
3360 : : return false;
3361 : :
3362 : 186459 : return isummary->inlinable;
3363 : : }
3364 : :
3365 : : /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3366 : : handle edge E with probability PROB. Set HINTS accordingly if edge may be
3367 : : devirtualized. AVALS, if non-NULL, describes the context of the call site
3368 : : as far as values of parameters are concerened. */
3369 : :
3370 : : static inline void
3371 : 184147282 : estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3372 : : sreal *time, ipa_call_arg_values *avals,
3373 : : ipa_hints *hints)
3374 : : {
3375 : 184147282 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3376 : 184147282 : int call_size = es->call_stmt_size;
3377 : 184147282 : int call_time = es->call_stmt_time;
3378 : 184147282 : int cur_size;
3379 : :
3380 : 3487523 : if (!e->callee && hints && e->maybe_hot_p ()
3381 : 184544872 : && estimate_edge_devirt_benefit (e, &call_size, &call_time, avals))
3382 : 186335 : *hints |= INLINE_HINT_indirect_call;
3383 : 184147282 : cur_size = call_size * ipa_fn_summary::size_scale;
3384 : 184147282 : *size += cur_size;
3385 : 184147282 : if (min_size)
3386 : 22900099 : *min_size += cur_size;
3387 : 184147282 : if (time)
3388 : 176516348 : *time += ((sreal)call_time) * e->sreal_frequency ();
3389 : 184147282 : }
3390 : :
3391 : :
3392 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3393 : : calls in NODE. POSSIBLE_TRUTHS and AVALS describe the context of the call
3394 : : site.
3395 : :
3396 : : Helper for estimate_calls_size_and_time which does the same but
3397 : : (in most cases) faster. */
3398 : :
3399 : : static void
3400 : 59279616 : estimate_calls_size_and_time_1 (struct cgraph_node *node, int *size,
3401 : : int *min_size, sreal *time,
3402 : : ipa_hints *hints,
3403 : : clause_t possible_truths,
3404 : : ipa_call_arg_values *avals)
3405 : : {
3406 : 59279616 : struct cgraph_edge *e;
3407 : 285841276 : for (e = node->callees; e; e = e->next_callee)
3408 : : {
3409 : 226561660 : if (!e->inline_failed)
3410 : : {
3411 : 42638555 : gcc_checking_assert (!ipa_call_summaries->get (e));
3412 : 42638555 : estimate_calls_size_and_time_1 (e->callee, size, min_size, time,
3413 : : hints, possible_truths, avals);
3414 : :
3415 : 42638555 : continue;
3416 : : }
3417 : 183923105 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3418 : :
3419 : : /* Do not care about zero sized builtins. */
3420 : 183923105 : if (!es->call_stmt_size)
3421 : : {
3422 : 11294122 : gcc_checking_assert (!es->call_stmt_time);
3423 : 11294122 : continue;
3424 : : }
3425 : 172628983 : if (!es->predicate
3426 : 172628983 : || es->predicate->evaluate (possible_truths))
3427 : : {
3428 : : /* Predicates of calls shall not use NOT_CHANGED codes,
3429 : : so we do not need to compute probabilities. */
3430 : 171043871 : estimate_edge_size_and_time (e, size,
3431 : 171043871 : es->predicate ? NULL : min_size,
3432 : : time, avals, hints);
3433 : : }
3434 : : }
3435 : 62557839 : for (e = node->indirect_calls; e; e = e->next_callee)
3436 : : {
3437 : 3278223 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3438 : 3278223 : if (!es->predicate
3439 : 3278223 : || es->predicate->evaluate (possible_truths))
3440 : 3252143 : estimate_edge_size_and_time (e, size,
3441 : 3252143 : es->predicate ? NULL : min_size,
3442 : : time, avals, hints);
3443 : : }
3444 : 59279616 : }
3445 : :
3446 : : /* Populate sum->call_size_time_table for edges from NODE. */
3447 : :
3448 : : static void
3449 : 2764055 : summarize_calls_size_and_time (struct cgraph_node *node,
3450 : : ipa_fn_summary *sum)
3451 : : {
3452 : 2764055 : struct cgraph_edge *e;
3453 : 12395486 : for (e = node->callees; e; e = e->next_callee)
3454 : : {
3455 : 9631431 : if (!e->inline_failed)
3456 : : {
3457 : 799508 : gcc_checking_assert (!ipa_call_summaries->get (e));
3458 : 799508 : summarize_calls_size_and_time (e->callee, sum);
3459 : 799508 : continue;
3460 : : }
3461 : 8831923 : int size = 0;
3462 : 8831923 : sreal time = 0;
3463 : :
3464 : 8831923 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3465 : :
3466 : 8831923 : ipa_predicate pred = true;
3467 : 8831923 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3468 : :
3469 : 8831923 : if (es->predicate)
3470 : 1176779 : pred = *es->predicate;
3471 : 8831923 : sum->account_size_time (size, time, pred, pred, true);
3472 : : }
3473 : 2999435 : for (e = node->indirect_calls; e; e = e->next_callee)
3474 : : {
3475 : 235380 : int size = 0;
3476 : 235380 : sreal time = 0;
3477 : :
3478 : 235380 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3479 : 235380 : ipa_predicate pred = true;
3480 : 235380 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3481 : :
3482 : 235380 : if (es->predicate)
3483 : 36357 : pred = *es->predicate;
3484 : 235380 : sum->account_size_time (size, time, pred, pred, true);
3485 : : }
3486 : 2764055 : }
3487 : :
3488 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3489 : : calls in NODE. POSSIBLE_TRUTHS and AVALS (the latter if non-NULL) describe
3490 : : context of the call site. */
3491 : :
3492 : : static void
3493 : 16641105 : estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3494 : : int *min_size, sreal *time,
3495 : : ipa_hints *hints,
3496 : : clause_t possible_truths,
3497 : : ipa_call_arg_values *avals)
3498 : : {
3499 : 16641105 : class ipa_fn_summary *sum = ipa_fn_summaries->get (node);
3500 : 16641105 : bool use_table = true;
3501 : :
3502 : 16641105 : gcc_assert (node->callees || node->indirect_calls);
3503 : :
3504 : : /* During early inlining we do not calculate info for very
3505 : : large functions and thus there is no need for producing
3506 : : summaries. */
3507 : 16641105 : if (!ipa_node_params_sum)
3508 : : use_table = false;
3509 : : /* Do not calculate summaries for simple wrappers; it is waste
3510 : : of memory. */
3511 : 8976431 : else if (node->callees && node->indirect_calls
3512 : 493164 : && node->callees->inline_failed && !node->callees->next_callee)
3513 : : use_table = false;
3514 : : /* If there is an indirect edge that may be optimized, we need
3515 : : to go the slow way. */
3516 : 8919175 : else if (avals && hints
3517 : 8919175 : && (avals->m_known_vals.length ()
3518 : 2302243 : || avals->m_known_contexts.length ()
3519 : 2258952 : || avals->m_known_aggs.length ()))
3520 : : {
3521 : 3079910 : ipa_node_params *params_summary = ipa_node_params_sum->get (node);
3522 : 3079910 : unsigned int nargs = params_summary
3523 : 6159820 : ? ipa_get_param_count (params_summary) : 0;
3524 : :
3525 : 10456813 : for (unsigned int i = 0; i < nargs && use_table; i++)
3526 : : {
3527 : 7376903 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3528 : 7376903 : && (avals->safe_sval_at (i)
3529 : 123602 : || (ipa_argagg_value_list (avals).value_for_index_p (i))))
3530 : : use_table = false;
3531 : 7309711 : else if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3532 : 7309711 : && (avals->m_known_contexts.length () > i
3533 : 7429578 : && !avals->m_known_contexts[i].useless_p ()))
3534 : : use_table = false;
3535 : : }
3536 : : }
3537 : :
3538 : : /* Fast path is via the call size time table. */
3539 : 3079910 : if (use_table)
3540 : : {
3541 : : /* Build summary if it is absent. */
3542 : 8799538 : if (!sum->call_size_time_table.length ())
3543 : : {
3544 : 1180582 : ipa_predicate true_pred = true;
3545 : 1180582 : sum->account_size_time (0, 0, true_pred, true_pred, true);
3546 : 1180582 : summarize_calls_size_and_time (node, sum);
3547 : : }
3548 : :
3549 : 8799538 : int old_size = *size;
3550 : 8799538 : sreal old_time = time ? *time : 0;
3551 : :
3552 : 8799538 : if (min_size)
3553 : 8799538 : *min_size += sum->call_size_time_table[0].size;
3554 : :
3555 : : unsigned int i;
3556 : : size_time_entry *e;
3557 : :
3558 : : /* Walk the table and account sizes and times. */
3559 : 22568045 : for (i = 0; sum->call_size_time_table.iterate (i, &e);
3560 : : i++)
3561 : 13768507 : if (e->exec_predicate.evaluate (possible_truths))
3562 : : {
3563 : 12464205 : *size += e->size;
3564 : 12464205 : if (time)
3565 : 9993272 : *time += e->time;
3566 : : }
3567 : :
3568 : : /* Be careful and see if both methods agree. */
3569 : 44 : if ((flag_checking || dump_file)
3570 : : /* Do not try to sanity check when we know we lost some
3571 : : precision. */
3572 : 8799538 : && sum->call_size_time_table.length ()
3573 : : < ipa_fn_summary::max_size_time_table_size)
3574 : : {
3575 : 8799494 : estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL,
3576 : : possible_truths, avals);
3577 : 8799494 : gcc_assert (*size == old_size);
3578 : 15811644 : if (time && (*time - old_time > 1 || *time - old_time < -1)
3579 : 8799515 : && dump_file)
3580 : 0 : fprintf (dump_file, "Time mismatch in call summary %f!=%f\n",
3581 : : old_time.to_double (),
3582 : : time->to_double ());
3583 : : }
3584 : : }
3585 : : /* Slow path by walking all edges. */
3586 : : else
3587 : 7841567 : estimate_calls_size_and_time_1 (node, size, min_size, time, hints,
3588 : : possible_truths, avals);
3589 : 16641105 : }
3590 : :
3591 : : /* Main constructor for ipa call context. Memory allocation of ARG_VALUES
3592 : : is owned by the caller. INLINE_PARAM_SUMMARY is also owned by the
3593 : : caller. */
3594 : :
3595 : 15971039 : ipa_call_context::ipa_call_context (cgraph_node *node, clause_t possible_truths,
3596 : : clause_t nonspec_possible_truths,
3597 : : vec<inline_param_summary>
3598 : : inline_param_summary,
3599 : 15971039 : ipa_auto_call_arg_values *arg_values)
3600 : 15971039 : : m_node (node), m_possible_truths (possible_truths),
3601 : 15971039 : m_nonspec_possible_truths (nonspec_possible_truths),
3602 : 15971039 : m_inline_param_summary (inline_param_summary),
3603 : 15971039 : m_avals (arg_values)
3604 : : {
3605 : 15971039 : }
3606 : :
3607 : : /* Set THIS to be a duplicate of CTX. Copy all relevant info. */
3608 : :
3609 : : void
3610 : 1640387 : ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx)
3611 : : {
3612 : 1640387 : m_node = ctx.m_node;
3613 : 1640387 : m_possible_truths = ctx.m_possible_truths;
3614 : 1640387 : m_nonspec_possible_truths = ctx.m_nonspec_possible_truths;
3615 : 1640387 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3616 : 1640387 : unsigned int nargs = params_summary
3617 : 3280667 : ? ipa_get_param_count (params_summary) : 0;
3618 : :
3619 : 1640387 : m_inline_param_summary = vNULL;
3620 : : /* Copy the info only if there is at least one useful entry. */
3621 : 1640387 : if (ctx.m_inline_param_summary.exists ())
3622 : : {
3623 : 1462401 : unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs);
3624 : :
3625 : 3393451 : for (unsigned int i = 0; i < n; i++)
3626 : 2805187 : if (ipa_is_param_used_by_ipa_predicates (params_summary, i)
3627 : 2805187 : && !ctx.m_inline_param_summary[i].useless_p ())
3628 : : {
3629 : 874137 : m_inline_param_summary
3630 : 874137 : = ctx.m_inline_param_summary.copy ();
3631 : 874137 : break;
3632 : : }
3633 : : }
3634 : 1640387 : m_avals.m_known_vals = vNULL;
3635 : 1640387 : if (ctx.m_avals.m_known_vals.exists ())
3636 : : {
3637 : 1640387 : unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs);
3638 : :
3639 : 3931795 : for (unsigned int i = 0; i < n; i++)
3640 : 2313070 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3641 : 2313070 : && ctx.m_avals.m_known_vals[i])
3642 : : {
3643 : 21662 : m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy ();
3644 : 21662 : break;
3645 : : }
3646 : : }
3647 : :
3648 : 1640387 : m_avals.m_known_contexts = vNULL;
3649 : 1640387 : if (ctx.m_avals.m_known_contexts.exists ())
3650 : : {
3651 : 1640387 : unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs);
3652 : :
3653 : 1641586 : for (unsigned int i = 0; i < n; i++)
3654 : 18019 : if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3655 : 18019 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3656 : : {
3657 : 16820 : m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy ();
3658 : 16820 : break;
3659 : : }
3660 : : }
3661 : :
3662 : 1640387 : m_avals.m_known_aggs = vNULL;
3663 : 1640387 : if (ctx.m_avals.m_known_aggs.exists ())
3664 : : {
3665 : 1640387 : const ipa_argagg_value_list avl (&ctx.m_avals);
3666 : 5599358 : for (unsigned int i = 0; i < nargs; i++)
3667 : 3962404 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3668 : 3962404 : && avl.value_for_index_p (i))
3669 : : {
3670 : 3433 : m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy ();
3671 : 3433 : break;
3672 : : }
3673 : : }
3674 : :
3675 : 1640387 : m_avals.m_known_value_ranges = vNULL;
3676 : 1640387 : }
3677 : :
3678 : : /* Release memory used by known_vals/contexts/aggs vectors. and
3679 : : inline_param_summary. */
3680 : :
3681 : : void
3682 : 2638992 : ipa_cached_call_context::release ()
3683 : : {
3684 : : /* See if context is initialized at first place. */
3685 : 2638992 : if (!m_node)
3686 : : return;
3687 : 1640387 : m_avals.m_known_aggs.release ();
3688 : 1640387 : m_avals.m_known_vals.release ();
3689 : 1640387 : m_avals.m_known_contexts.release ();
3690 : 1640387 : m_inline_param_summary.release ();
3691 : : }
3692 : :
3693 : : /* Return true if CTX describes the same call context as THIS. */
3694 : :
3695 : : bool
3696 : 5558358 : ipa_call_context::equal_to (const ipa_call_context &ctx)
3697 : : {
3698 : 5558358 : if (m_node != ctx.m_node
3699 : : || m_possible_truths != ctx.m_possible_truths
3700 : 4559753 : || m_nonspec_possible_truths != ctx.m_nonspec_possible_truths)
3701 : : return false;
3702 : :
3703 : 4057991 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3704 : 4057991 : unsigned int nargs = params_summary
3705 : 8115935 : ? ipa_get_param_count (params_summary) : 0;
3706 : :
3707 : 4057991 : if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ())
3708 : : {
3709 : 11941833 : for (unsigned int i = 0; i < nargs; i++)
3710 : : {
3711 : 8202859 : if (!ipa_is_param_used_by_ipa_predicates (params_summary, i))
3712 : 2509608 : continue;
3713 : 5693251 : if (i >= m_inline_param_summary.length ()
3714 : 4060293 : || m_inline_param_summary[i].useless_p ())
3715 : : {
3716 : 2393511 : if (i < ctx.m_inline_param_summary.length ()
3717 : 2393511 : && !ctx.m_inline_param_summary[i].useless_p ())
3718 : : return false;
3719 : 2353833 : continue;
3720 : : }
3721 : 3299740 : if (i >= ctx.m_inline_param_summary.length ()
3722 : 3299736 : || ctx.m_inline_param_summary[i].useless_p ())
3723 : : {
3724 : 1640387 : if (i < m_inline_param_summary.length ()
3725 : 1640387 : && !m_inline_param_summary[i].useless_p ())
3726 : : return false;
3727 : : continue;
3728 : : }
3729 : 3260578 : if (!m_inline_param_summary[i].equal_to
3730 : 11329749 : (ctx.m_inline_param_summary[i]))
3731 : : return false;
3732 : : }
3733 : : }
3734 : 3924303 : if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ())
3735 : : {
3736 : 11849931 : for (unsigned int i = 0; i < nargs; i++)
3737 : : {
3738 : 7930771 : if (!ipa_is_param_used_by_indirect_call (params_summary, i))
3739 : 7817607 : continue;
3740 : 113164 : if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i])
3741 : : {
3742 : 60880 : if (i < ctx.m_avals.m_known_vals.length ()
3743 : 60880 : && ctx.m_avals.m_known_vals[i])
3744 : : return false;
3745 : 60858 : continue;
3746 : : }
3747 : 52284 : if (i >= ctx.m_avals.m_known_vals.length ()
3748 : 52284 : || !ctx.m_avals.m_known_vals[i])
3749 : : {
3750 : 1640387 : if (i < m_avals.m_known_vals.length () && m_avals.m_known_vals[i])
3751 : : return false;
3752 : : continue;
3753 : : }
3754 : 52261 : if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i])
3755 : : return false;
3756 : : }
3757 : : }
3758 : 3919160 : if (m_avals.m_known_contexts.exists ()
3759 : 3919160 : || ctx.m_avals.m_known_contexts.exists ())
3760 : : {
3761 : 11842883 : for (unsigned int i = 0; i < nargs; i++)
3762 : : {
3763 : 7924180 : if (!ipa_is_param_used_by_polymorphic_call (params_summary, i))
3764 : 7876893 : continue;
3765 : 47287 : if (i >= m_avals.m_known_contexts.length ()
3766 : 46818 : || m_avals.m_known_contexts[i].useless_p ())
3767 : : {
3768 : 469 : if (i < ctx.m_avals.m_known_contexts.length ()
3769 : 469 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3770 : : return false;
3771 : 469 : continue;
3772 : : }
3773 : 46818 : if (i >= ctx.m_avals.m_known_contexts.length ()
3774 : 46818 : || ctx.m_avals.m_known_contexts[i].useless_p ())
3775 : : {
3776 : 0 : if (i < m_avals.m_known_contexts.length ()
3777 : 1640387 : && !m_avals.m_known_contexts[i].useless_p ())
3778 : : return false;
3779 : 0 : continue;
3780 : : }
3781 : 46818 : if (!m_avals.m_known_contexts[i].equal_to
3782 : 46818 : (ctx.m_avals.m_known_contexts[i]))
3783 : : return false;
3784 : : }
3785 : : }
3786 : 3918703 : if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ())
3787 : : {
3788 : : unsigned i = 0, j = 0;
3789 : 8652821 : while (i < m_avals.m_known_aggs.length ()
3790 : 4325504 : || j < ctx.m_avals.m_known_aggs.length ())
3791 : : {
3792 : 412819 : if (i >= m_avals.m_known_aggs.length ())
3793 : : {
3794 : 402247 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
3795 : 402247 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
3796 : : return false;
3797 : 401943 : j++;
3798 : 401943 : continue;
3799 : 401943 : }
3800 : 10572 : if (j >= ctx.m_avals.m_known_aggs.length ())
3801 : : {
3802 : 319 : int idx1 = m_avals.m_known_aggs[i].index;
3803 : 319 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
3804 : : return false;
3805 : 4 : i++;
3806 : 4 : continue;
3807 : 4 : }
3808 : :
3809 : 4967 : int idx1 = m_avals.m_known_aggs[i].index;
3810 : 4967 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
3811 : 4967 : if (idx1 < idx2)
3812 : : {
3813 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
3814 : : return false;
3815 : 0 : i++;
3816 : 0 : continue;
3817 : : }
3818 : 4967 : if (idx1 > idx2)
3819 : : {
3820 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
3821 : : return false;
3822 : 0 : j++;
3823 : 0 : continue;
3824 : : }
3825 : 4967 : if (!ipa_is_param_used_by_indirect_call (params_summary, idx1))
3826 : : {
3827 : 295 : i++;
3828 : 295 : j++;
3829 : 295 : continue;
3830 : : }
3831 : :
3832 : 4672 : if ((m_avals.m_known_aggs[i].unit_offset
3833 : 4672 : != ctx.m_avals.m_known_aggs[j].unit_offset)
3834 : 4664 : || (m_avals.m_known_aggs[i].by_ref
3835 : 4664 : != ctx.m_avals.m_known_aggs[j].by_ref)
3836 : 9336 : || !operand_equal_p (m_avals.m_known_aggs[i].value,
3837 : 4664 : ctx.m_avals.m_known_aggs[j].value))
3838 : 113 : return false;
3839 : 4559 : i++;
3840 : 4559 : j++;
3841 : : }
3842 : : }
3843 : : return true;
3844 : : }
3845 : :
3846 : : /* Fill in the selected fields in ESTIMATES with value estimated for call in
3847 : : this context. Always compute size and min_size. Only compute time and
3848 : : nonspecialized_time if EST_TIMES is true. Only compute hints if EST_HINTS
3849 : : is true. */
3850 : :
3851 : : void
3852 : 15939662 : ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates,
3853 : : bool est_times, bool est_hints)
3854 : : {
3855 : 15939662 : class ipa_fn_summary *info = ipa_fn_summaries->get (m_node);
3856 : 15939662 : size_time_entry *e;
3857 : 15939662 : int size = 0;
3858 : 15939662 : sreal time = 0;
3859 : 15939662 : int min_size = 0;
3860 : 15939662 : ipa_hints hints = 0;
3861 : 15939662 : sreal loops_with_known_iterations = 0;
3862 : 15939662 : sreal loops_with_known_strides = 0;
3863 : 15939662 : int i;
3864 : :
3865 : 15939662 : if (dump_file && (dump_flags & TDF_DETAILS))
3866 : : {
3867 : 1193 : bool found = false;
3868 : 1193 : fprintf (dump_file, " Estimating body: %s\n"
3869 : : " Known to be false: ", m_node->dump_name ());
3870 : :
3871 : 3794 : for (i = ipa_predicate::not_inlined_condition;
3872 : 7588 : i < (ipa_predicate::first_dynamic_condition
3873 : 6260 : + (int) vec_safe_length (info->conds)); i++)
3874 : 2601 : if (!(m_possible_truths & (1 << i)))
3875 : : {
3876 : 1608 : if (found)
3877 : 461 : fprintf (dump_file, ", ");
3878 : 1608 : found = true;
3879 : 1608 : dump_condition (dump_file, info->conds, i);
3880 : : }
3881 : : }
3882 : :
3883 : 15939662 : if (m_node->callees || m_node->indirect_calls)
3884 : 20354653 : estimate_calls_size_and_time (m_node, &size, &min_size,
3885 : : est_times ? &time : NULL,
3886 : : est_hints ? &hints : NULL, m_possible_truths,
3887 : : &m_avals);
3888 : :
3889 : 15939662 : sreal nonspecialized_time = time;
3890 : :
3891 : 15939662 : min_size += info->size_time_table[0].size;
3892 : 104438702 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
3893 : : {
3894 : 88499040 : bool exec = e->exec_predicate.evaluate (m_nonspec_possible_truths);
3895 : :
3896 : : /* Because predicates are conservative, it can happen that nonconst is 1
3897 : : but exec is 0. */
3898 : 88499040 : if (exec)
3899 : : {
3900 : 86304396 : bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths);
3901 : :
3902 : 86304396 : gcc_checking_assert (e->time >= 0);
3903 : 86304396 : gcc_checking_assert (time >= 0);
3904 : :
3905 : : /* We compute specialized size only because size of nonspecialized
3906 : : copy is context independent.
3907 : :
3908 : : The difference between nonspecialized execution and specialized is
3909 : : that nonspecialized is not going to have optimized out computations
3910 : : known to be constant in a specialized setting. */
3911 : 86304396 : if (nonconst)
3912 : 43590337 : size += e->size;
3913 : 86304396 : if (!est_times)
3914 : 46614655 : continue;
3915 : 39689741 : nonspecialized_time += e->time;
3916 : 39689741 : if (!nonconst)
3917 : : ;
3918 : 19014015 : else if (!m_inline_param_summary.exists ())
3919 : : {
3920 : 1284889 : if (nonconst)
3921 : 1284889 : time += e->time;
3922 : : }
3923 : : else
3924 : : {
3925 : 17729126 : int prob = e->nonconst_predicate.probability
3926 : 17729126 : (info->conds, m_possible_truths,
3927 : : m_inline_param_summary);
3928 : 17729126 : gcc_checking_assert (prob >= 0);
3929 : 17729126 : gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3930 : 17729126 : if (prob == REG_BR_PROB_BASE)
3931 : 15206994 : time += e->time;
3932 : : else
3933 : 2522132 : time += e->time * prob / REG_BR_PROB_BASE;
3934 : : }
3935 : 39689741 : gcc_checking_assert (time >= 0);
3936 : : }
3937 : : }
3938 : 15939662 : gcc_checking_assert (info->size_time_table[0].exec_predicate == true);
3939 : 15939662 : gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true);
3940 : 15939662 : gcc_checking_assert (min_size >= 0);
3941 : 15939662 : gcc_checking_assert (size >= 0);
3942 : 15939662 : gcc_checking_assert (time >= 0);
3943 : : /* nonspecialized_time should be always bigger than specialized time.
3944 : : Roundoff issues however may get into the way. */
3945 : 15939662 : gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1);
3946 : :
3947 : : /* Roundoff issues may make specialized time bigger than nonspecialized
3948 : : time. We do not really want that to happen because some heuristics
3949 : : may get confused by seeing negative speedups. */
3950 : 15939662 : if (time > nonspecialized_time)
3951 : 0 : time = nonspecialized_time;
3952 : :
3953 : 15939662 : if (est_hints)
3954 : : {
3955 : 5682282 : if (info->scc_no)
3956 : 189876 : hints |= INLINE_HINT_in_scc;
3957 : 5682282 : if (DECL_DECLARED_INLINE_P (m_node->decl))
3958 : 3253105 : hints |= INLINE_HINT_declared_inline;
3959 : 5682282 : if (info->builtin_constant_p_parms.length ()
3960 : 7505 : && DECL_DECLARED_INLINE_P (m_node->decl))
3961 : 7491 : hints |= INLINE_HINT_builtin_constant_p;
3962 : :
3963 : : ipa_freqcounting_predicate *fcp;
3964 : 6253970 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
3965 : 571688 : if (!fcp->predicate->evaluate (m_possible_truths))
3966 : : {
3967 : 421892 : hints |= INLINE_HINT_loop_iterations;
3968 : 421892 : loops_with_known_iterations += fcp->freq;
3969 : : }
3970 : 5682282 : estimates->loops_with_known_iterations = loops_with_known_iterations;
3971 : :
3972 : 6042622 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
3973 : 360340 : if (!fcp->predicate->evaluate (m_possible_truths))
3974 : : {
3975 : 350313 : hints |= INLINE_HINT_loop_stride;
3976 : 350313 : loops_with_known_strides += fcp->freq;
3977 : : }
3978 : 5682282 : estimates->loops_with_known_strides = loops_with_known_strides;
3979 : : }
3980 : :
3981 : 15939662 : size = RDIV (size, ipa_fn_summary::size_scale);
3982 : 15939662 : min_size = RDIV (min_size, ipa_fn_summary::size_scale);
3983 : :
3984 : 15939662 : if (dump_file && (dump_flags & TDF_DETAILS))
3985 : : {
3986 : 1193 : fprintf (dump_file, "\n size:%i", (int) size);
3987 : 1193 : if (est_times)
3988 : 969 : fprintf (dump_file, " time:%f nonspec time:%f",
3989 : : time.to_double (), nonspecialized_time.to_double ());
3990 : 1193 : if (est_hints)
3991 : 969 : fprintf (dump_file, " loops with known iterations:%f "
3992 : : "known strides:%f", loops_with_known_iterations.to_double (),
3993 : : loops_with_known_strides.to_double ());
3994 : 1193 : fprintf (dump_file, "\n");
3995 : : }
3996 : 15939662 : if (est_times)
3997 : : {
3998 : 5682282 : estimates->time = time;
3999 : 5682282 : estimates->nonspecialized_time = nonspecialized_time;
4000 : : }
4001 : 15939662 : estimates->size = size;
4002 : 15939662 : estimates->min_size = min_size;
4003 : 15939662 : if (est_hints)
4004 : 5682282 : estimates->hints = hints;
4005 : 15939662 : return;
4006 : : }
4007 : :
4008 : :
4009 : : /* Estimate size and time needed to execute callee of EDGE assuming that
4010 : : parameters known to be constant at caller of EDGE are propagated.
4011 : : KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
4012 : : and types for parameters. */
4013 : :
4014 : : void
4015 : 151516 : estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
4016 : : ipa_auto_call_arg_values *avals,
4017 : : ipa_call_estimates *estimates)
4018 : : {
4019 : 151516 : clause_t clause, nonspec_clause;
4020 : :
4021 : 151516 : evaluate_conditions_for_known_args (node, false, avals, &clause,
4022 : : &nonspec_clause, NULL);
4023 : 151516 : ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals);
4024 : 151516 : ctx.estimate_size_and_time (estimates);
4025 : 151516 : }
4026 : :
4027 : : /* Return stack frame offset where frame of NODE is supposed to start inside
4028 : : of the function it is inlined to.
4029 : : Return 0 for functions that are not inlined. */
4030 : :
4031 : : HOST_WIDE_INT
4032 : 4043756 : ipa_get_stack_frame_offset (struct cgraph_node *node)
4033 : : {
4034 : 4043756 : HOST_WIDE_INT offset = 0;
4035 : 4043756 : if (!node->inlined_to)
4036 : : return 0;
4037 : 3247642 : node = node->callers->caller;
4038 : 4156858 : while (true)
4039 : : {
4040 : 4156858 : offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
4041 : 3702250 : if (!node->inlined_to)
4042 : 3247642 : return offset;
4043 : 454608 : node = node->callers->caller;
4044 : : }
4045 : : }
4046 : :
4047 : :
4048 : : /* Update summary information of inline clones after inlining.
4049 : : Compute peak stack usage. */
4050 : :
4051 : : static void
4052 : 3677702 : inline_update_callee_summaries (struct cgraph_node *node, int depth)
4053 : : {
4054 : 3677702 : struct cgraph_edge *e;
4055 : :
4056 : 3677702 : ipa_propagate_frequency (node);
4057 : 6716776 : for (e = node->callees; e; e = e->next_callee)
4058 : : {
4059 : 3039074 : if (!e->inline_failed)
4060 : 430682 : inline_update_callee_summaries (e->callee, depth);
4061 : : else
4062 : 2608392 : ipa_call_summaries->get (e)->loop_depth += depth;
4063 : : }
4064 : 3772380 : for (e = node->indirect_calls; e; e = e->next_callee)
4065 : 94678 : ipa_call_summaries->get (e)->loop_depth += depth;
4066 : 3677702 : }
4067 : :
4068 : : /* Update change_prob and points_to_local_or_readonly_memory of EDGE after
4069 : : INLINED_EDGE has been inlined.
4070 : :
4071 : : When function A is inlined in B and A calls C with parameter that
4072 : : changes with probability PROB1 and C is known to be passthrough
4073 : : of argument if B that change with probability PROB2, the probability
4074 : : of change is now PROB1*PROB2. */
4075 : :
4076 : : static void
4077 : 2703278 : remap_edge_params (struct cgraph_edge *inlined_edge,
4078 : : struct cgraph_edge *edge)
4079 : : {
4080 : 2703278 : if (ipa_node_params_sum)
4081 : : {
4082 : 1962683 : int i;
4083 : 1962683 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4084 : 1962683 : if (!args)
4085 : : return;
4086 : 1022028 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4087 : 1022028 : class ipa_call_summary *inlined_es
4088 : 1022028 : = ipa_call_summaries->get (inlined_edge);
4089 : :
4090 : 1022028 : if (es->param.length () == 0)
4091 : : return;
4092 : :
4093 : 5987278 : for (i = 0; i < ipa_get_cs_argument_count (args); i++)
4094 : : {
4095 : 2022223 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4096 : 2022223 : if (jfunc->type == IPA_JF_PASS_THROUGH
4097 : 1410068 : || jfunc->type == IPA_JF_ANCESTOR)
4098 : : {
4099 : 738005 : int id = jfunc->type == IPA_JF_PASS_THROUGH
4100 : 738005 : ? ipa_get_jf_pass_through_formal_id (jfunc)
4101 : 125850 : : ipa_get_jf_ancestor_formal_id (jfunc);
4102 : 1475908 : if (id < (int) inlined_es->param.length ())
4103 : : {
4104 : 737895 : int prob1 = es->param[i].change_prob;
4105 : 737895 : int prob2 = inlined_es->param[id].change_prob;
4106 : 737895 : int prob = combine_probabilities (prob1, prob2);
4107 : :
4108 : 737895 : if (prob1 && prob2 && !prob)
4109 : 737895 : prob = 1;
4110 : :
4111 : 737895 : es->param[i].change_prob = prob;
4112 : :
4113 : 1475790 : if (inlined_es
4114 : 737895 : ->param[id].points_to_local_or_readonly_memory)
4115 : 191998 : es->param[i].points_to_local_or_readonly_memory = true;
4116 : 1475790 : if (inlined_es
4117 : 737895 : ->param[id].points_to_possible_sra_candidate)
4118 : 169588 : es->param[i].points_to_possible_sra_candidate = true;
4119 : : }
4120 : 738005 : if (!es->param[i].points_to_local_or_readonly_memory
4121 : : && jfunc->type == IPA_JF_CONST
4122 : : && points_to_local_or_readonly_memory_p
4123 : : (ipa_get_jf_constant (jfunc)))
4124 : : es->param[i].points_to_local_or_readonly_memory = true;
4125 : : }
4126 : : }
4127 : : }
4128 : : }
4129 : :
4130 : : /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
4131 : :
4132 : : Remap predicates of callees of NODE. Rest of arguments match
4133 : : remap_predicate.
4134 : :
4135 : : Also update change probabilities. */
4136 : :
4137 : : static void
4138 : 3677702 : remap_edge_summaries (struct cgraph_edge *inlined_edge,
4139 : : struct cgraph_node *node,
4140 : : class ipa_fn_summary *info,
4141 : : class ipa_node_params *params_summary,
4142 : : class ipa_fn_summary *callee_info,
4143 : : const vec<int> &operand_map,
4144 : : const vec<HOST_WIDE_INT> &offset_map,
4145 : : clause_t possible_truths,
4146 : : ipa_predicate *toplev_predicate)
4147 : : {
4148 : 3677702 : struct cgraph_edge *e, *next;
4149 : 6716265 : for (e = node->callees; e; e = next)
4150 : : {
4151 : 3038563 : ipa_predicate p;
4152 : 3038563 : next = e->next_callee;
4153 : :
4154 : 3038563 : if (e->inline_failed)
4155 : : {
4156 : 2607881 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4157 : 2607881 : remap_edge_params (inlined_edge, e);
4158 : :
4159 : 2607881 : if (es->predicate)
4160 : : {
4161 : 618167 : p = es->predicate->remap_after_inlining
4162 : 618167 : (info, params_summary,
4163 : : callee_info, operand_map,
4164 : : offset_map, possible_truths,
4165 : : *toplev_predicate);
4166 : 618167 : edge_set_predicate (e, &p);
4167 : : }
4168 : : else
4169 : 1989714 : edge_set_predicate (e, toplev_predicate);
4170 : : }
4171 : : else
4172 : 430682 : remap_edge_summaries (inlined_edge, e->callee, info,
4173 : : params_summary, callee_info,
4174 : : operand_map, offset_map, possible_truths,
4175 : : toplev_predicate);
4176 : : }
4177 : 3773099 : for (e = node->indirect_calls; e; e = next)
4178 : : {
4179 : 95397 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4180 : 95397 : ipa_predicate p;
4181 : 95397 : next = e->next_callee;
4182 : :
4183 : 95397 : remap_edge_params (inlined_edge, e);
4184 : 95397 : if (es->predicate)
4185 : : {
4186 : 24062 : p = es->predicate->remap_after_inlining
4187 : 24062 : (info, params_summary,
4188 : : callee_info, operand_map, offset_map,
4189 : : possible_truths, *toplev_predicate);
4190 : 24062 : edge_set_predicate (e, &p);
4191 : : }
4192 : : else
4193 : 71335 : edge_set_predicate (e, toplev_predicate);
4194 : : }
4195 : 3677702 : }
4196 : :
4197 : : /* Run remap_after_inlining on each predicate in V. */
4198 : :
4199 : : static void
4200 : 6494040 : remap_freqcounting_predicate (class ipa_fn_summary *info,
4201 : : class ipa_node_params *params_summary,
4202 : : class ipa_fn_summary *callee_info,
4203 : : vec<ipa_freqcounting_predicate, va_gc> *v,
4204 : : const vec<int> &operand_map,
4205 : : const vec<HOST_WIDE_INT> &offset_map,
4206 : : clause_t possible_truths,
4207 : : ipa_predicate *toplev_predicate)
4208 : :
4209 : : {
4210 : 6494040 : ipa_freqcounting_predicate *fcp;
4211 : 6518426 : for (int i = 0; vec_safe_iterate (v, i, &fcp); i++)
4212 : : {
4213 : 24386 : ipa_predicate p
4214 : 24386 : = fcp->predicate->remap_after_inlining (info, params_summary,
4215 : : callee_info, operand_map,
4216 : : offset_map, possible_truths,
4217 : : *toplev_predicate);
4218 : 32621 : if (p != false && p != true)
4219 : 2254 : *fcp->predicate &= p;
4220 : : }
4221 : 6494040 : }
4222 : :
4223 : : /* We inlined EDGE. Update summary of the function we inlined into. */
4224 : :
4225 : : void
4226 : 3247020 : ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
4227 : : {
4228 : 3247020 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
4229 : 6494040 : struct cgraph_node *to = (edge->caller->inlined_to
4230 : 3247020 : ? edge->caller->inlined_to : edge->caller);
4231 : 3247020 : class ipa_fn_summary *info = ipa_fn_summaries->get (to);
4232 : 3247020 : clause_t clause = 0; /* not_inline is known to be false. */
4233 : 3247020 : size_time_entry *e;
4234 : 3247020 : auto_vec<int, 8> operand_map;
4235 : 3247020 : auto_vec<HOST_WIDE_INT, 8> offset_map;
4236 : 3247020 : int i;
4237 : 3247020 : ipa_predicate toplev_predicate;
4238 : 3247020 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4239 : 6494040 : ipa_node_params *params_summary = (ipa_node_params_sum
4240 : 3247020 : ? ipa_node_params_sum->get (to) : NULL);
4241 : :
4242 : 3247020 : if (es->predicate)
4243 : 276863 : toplev_predicate = *es->predicate;
4244 : : else
4245 : 2970157 : toplev_predicate = true;
4246 : :
4247 : 3247020 : info->fp_expressions |= callee_info->fp_expressions;
4248 : 3247020 : info->target_info |= callee_info->target_info;
4249 : :
4250 : 3247020 : if (callee_info->conds)
4251 : : {
4252 : 2316597 : ipa_auto_call_arg_values avals;
4253 : 2316597 : evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
4254 : 2316597 : }
4255 : 3247020 : if (ipa_node_params_sum && callee_info->conds)
4256 : : {
4257 : 654222 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4258 : 654222 : int count = args ? ipa_get_cs_argument_count (args) : 0;
4259 : 654057 : int i;
4260 : :
4261 : 654057 : if (count)
4262 : : {
4263 : 654057 : operand_map.safe_grow_cleared (count, true);
4264 : 654057 : offset_map.safe_grow_cleared (count, true);
4265 : : }
4266 : 2012874 : for (i = 0; i < count; i++)
4267 : : {
4268 : 1358652 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4269 : 1358652 : int map = -1;
4270 : :
4271 : : /* TODO: handle non-NOPs when merging. */
4272 : 1358652 : if (jfunc->type == IPA_JF_PASS_THROUGH)
4273 : : {
4274 : 226027 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4275 : 222942 : map = ipa_get_jf_pass_through_formal_id (jfunc);
4276 : 226027 : if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
4277 : 151784 : offset_map[i] = -1;
4278 : : }
4279 : 1132625 : else if (jfunc->type == IPA_JF_ANCESTOR)
4280 : : {
4281 : 73420 : HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
4282 : 73420 : if (offset >= 0 && offset < INT_MAX)
4283 : : {
4284 : 73420 : map = ipa_get_jf_ancestor_formal_id (jfunc);
4285 : 73420 : if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
4286 : 48954 : offset = -1;
4287 : 73420 : offset_map[i] = offset;
4288 : : }
4289 : : }
4290 : 1358652 : operand_map[i] = map;
4291 : 2471270 : gcc_assert (map < ipa_get_param_count (params_summary));
4292 : : }
4293 : :
4294 : : int ip;
4295 : 656003 : for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++)
4296 : 1781 : if (ip < count && operand_map[ip] >= 0)
4297 : 17 : add_builtin_constant_p_parm (info, operand_map[ip]);
4298 : : }
4299 : 3247020 : sreal freq = edge->sreal_frequency ();
4300 : 17339489 : for (i = 0; callee_info->size_time_table.iterate (i, &e); i++)
4301 : : {
4302 : 14092469 : ipa_predicate p;
4303 : 14092469 : p = e->exec_predicate.remap_after_inlining
4304 : 14092469 : (info, params_summary,
4305 : : callee_info, operand_map,
4306 : : offset_map, clause,
4307 : : toplev_predicate);
4308 : 14092469 : ipa_predicate nonconstp;
4309 : 14092469 : nonconstp = e->nonconst_predicate.remap_after_inlining
4310 : 14092469 : (info, params_summary,
4311 : : callee_info, operand_map,
4312 : : offset_map, clause,
4313 : : toplev_predicate);
4314 : 21263620 : if (p != false && nonconstp != false)
4315 : : {
4316 : 6893812 : sreal add_time = ((sreal)e->time * freq);
4317 : 6893812 : int prob = e->nonconst_predicate.probability (callee_info->conds,
4318 : : clause, es->param);
4319 : 6893812 : if (prob != REG_BR_PROB_BASE)
4320 : 852824 : add_time = add_time * prob / REG_BR_PROB_BASE;
4321 : 852824 : if (prob != REG_BR_PROB_BASE
4322 : 852824 : && dump_file && (dump_flags & TDF_DETAILS))
4323 : : {
4324 : 3 : fprintf (dump_file, "\t\tScaling time by probability:%f\n",
4325 : 3 : (double) prob / REG_BR_PROB_BASE);
4326 : : }
4327 : 6893812 : info->account_size_time (e->size, add_time, p, nonconstp);
4328 : : }
4329 : : }
4330 : 3247020 : remap_edge_summaries (edge, edge->callee, info, params_summary,
4331 : : callee_info, operand_map,
4332 : : offset_map, clause, &toplev_predicate);
4333 : 3247020 : remap_freqcounting_predicate (info, params_summary, callee_info,
4334 : : info->loop_iterations, operand_map,
4335 : : offset_map, clause, &toplev_predicate);
4336 : 3247020 : remap_freqcounting_predicate (info, params_summary, callee_info,
4337 : : info->loop_strides, operand_map,
4338 : : offset_map, clause, &toplev_predicate);
4339 : :
4340 : 3247020 : HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
4341 : 3247020 : HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
4342 : :
4343 : 3247020 : if (info->estimated_stack_size < peak)
4344 : 95242 : info->estimated_stack_size = peak;
4345 : :
4346 : 3247020 : inline_update_callee_summaries (edge->callee, es->loop_depth);
4347 : 3247020 : if (info->call_size_time_table.length ())
4348 : : {
4349 : 783965 : int edge_size = 0;
4350 : 783965 : sreal edge_time = 0;
4351 : :
4352 : 783965 : estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0);
4353 : : /* Unaccount size and time of the optimized out call. */
4354 : 783965 : info->account_size_time (-edge_size, -edge_time,
4355 : 783965 : es->predicate ? *es->predicate : true,
4356 : 783965 : es->predicate ? *es->predicate : true,
4357 : : true);
4358 : : /* Account new calls. */
4359 : 783965 : summarize_calls_size_and_time (edge->callee, info);
4360 : : }
4361 : :
4362 : : /* Free summaries that are not maintained for inline clones/edges. */
4363 : 3247020 : ipa_call_summaries->remove (edge);
4364 : 3247020 : ipa_fn_summaries->remove (edge->callee);
4365 : 3247020 : ipa_remove_from_growth_caches (edge);
4366 : 3247020 : }
4367 : :
4368 : : /* For performance reasons ipa_merge_fn_summary_after_inlining is not updating
4369 : : overall size and time. Recompute it.
4370 : : If RESET is true also recompute call_time_size_table. */
4371 : :
4372 : : void
4373 : 8392863 : ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset)
4374 : : {
4375 : 8392863 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
4376 : 8392863 : class ipa_size_summary *size_info = ipa_size_summaries->get (node);
4377 : 8392863 : size_time_entry *e;
4378 : 8392863 : int i;
4379 : :
4380 : 8392863 : size_info->size = 0;
4381 : 8392863 : info->time = 0;
4382 : 41763507 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4383 : : {
4384 : 33370644 : size_info->size += e->size;
4385 : 33370644 : info->time += e->time;
4386 : : }
4387 : 8392863 : info->min_size = info->size_time_table[0].size;
4388 : 8392863 : if (reset)
4389 : 7637395 : info->call_size_time_table.release ();
4390 : 8392863 : if (node->callees || node->indirect_calls)
4391 : 6344978 : estimate_calls_size_and_time (node, &size_info->size, &info->min_size,
4392 : : &info->time, NULL,
4393 : : ~(clause_t) (1 << ipa_predicate::false_condition),
4394 : : NULL);
4395 : 8392863 : size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale);
4396 : 8392863 : info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale);
4397 : 8392863 : }
4398 : :
4399 : :
4400 : : /* This function performs intraprocedural analysis in NODE that is required to
4401 : : inline indirect calls. */
4402 : :
4403 : : static void
4404 : 1265851 : inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4405 : : {
4406 : 1265851 : ipa_analyze_node (node);
4407 : 1265851 : if (dump_file && (dump_flags & TDF_DETAILS))
4408 : : {
4409 : 14 : ipa_print_node_params (dump_file, node);
4410 : 14 : ipa_print_node_jump_functions (dump_file, node);
4411 : : }
4412 : 1265851 : }
4413 : :
4414 : :
4415 : : /* Note function body size. */
4416 : :
4417 : : void
4418 : 1278853 : inline_analyze_function (struct cgraph_node *node)
4419 : : {
4420 : 1278853 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4421 : :
4422 : 1278853 : if (dump_file)
4423 : 102 : fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ());
4424 : 1278853 : if (opt_for_fn (node->decl, optimize) && !node->thunk)
4425 : 1265851 : inline_indirect_intraprocedural_analysis (node);
4426 : 1278853 : compute_fn_summary (node, false);
4427 : 1278853 : if (!optimize)
4428 : : {
4429 : 11597 : struct cgraph_edge *e;
4430 : 24693 : for (e = node->callees; e; e = e->next_callee)
4431 : 13096 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4432 : 11668 : for (e = node->indirect_calls; e; e = e->next_callee)
4433 : 71 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4434 : : }
4435 : :
4436 : 1278853 : pop_cfun ();
4437 : 1278853 : }
4438 : :
4439 : :
4440 : : /* Called when new function is inserted to callgraph late. */
4441 : :
4442 : : void
4443 : 18061 : ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
4444 : : {
4445 : 18061 : inline_analyze_function (node);
4446 : 18061 : }
4447 : :
4448 : : /* Note function body size. */
4449 : :
4450 : : static void
4451 : 229880 : ipa_fn_summary_generate (void)
4452 : : {
4453 : 229880 : struct cgraph_node *node;
4454 : :
4455 : 1990521 : FOR_EACH_DEFINED_FUNCTION (node)
4456 : 1760641 : if (DECL_STRUCT_FUNCTION (node->decl))
4457 : 1744084 : node->versionable = tree_versionable_function_p (node->decl);
4458 : :
4459 : 229880 : ipa_fn_summary_alloc ();
4460 : :
4461 : 229880 : ipa_fn_summaries->enable_insertion_hook ();
4462 : :
4463 : 229880 : ipa_register_cgraph_hooks ();
4464 : :
4465 : 1990521 : FOR_EACH_DEFINED_FUNCTION (node)
4466 : 1760641 : if (!node->alias
4467 : 1760641 : && (flag_generate_lto || flag_generate_offload|| flag_wpa
4468 : 1575961 : || opt_for_fn (node->decl, optimize)))
4469 : 1243975 : inline_analyze_function (node);
4470 : 229880 : }
4471 : :
4472 : :
4473 : : /* Write inline summary for edge E to OB. */
4474 : :
4475 : : static void
4476 : 342056 : read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
4477 : : bool prevails)
4478 : : {
4479 : 342056 : class ipa_call_summary *es = prevails
4480 : 342056 : ? ipa_call_summaries->get_create (e) : NULL;
4481 : 342056 : ipa_predicate p;
4482 : 342056 : int length, i;
4483 : :
4484 : 342056 : int size = streamer_read_uhwi (ib);
4485 : 342056 : int time = streamer_read_uhwi (ib);
4486 : 342056 : int depth = streamer_read_uhwi (ib);
4487 : :
4488 : 342056 : if (es)
4489 : : {
4490 : 342009 : es->call_stmt_size = size;
4491 : 342009 : es->call_stmt_time = time;
4492 : 342009 : es->loop_depth = depth;
4493 : : }
4494 : :
4495 : 342056 : bitpack_d bp = streamer_read_bitpack (ib);
4496 : 342056 : if (es)
4497 : 342009 : es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
4498 : : else
4499 : 47 : bp_unpack_value (&bp, 1);
4500 : :
4501 : 342056 : p.stream_in (ib);
4502 : 342056 : if (es)
4503 : 342009 : edge_set_predicate (e, &p);
4504 : 342056 : length = streamer_read_uhwi (ib);
4505 : 342056 : if (length && es
4506 : 342056 : && (e->possibly_call_in_translation_unit_p ()
4507 : : /* Also stream in jump functions to builtins in hope that they
4508 : : will get fnspecs. */
4509 : 117685 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
4510 : : {
4511 : 230711 : es->param.safe_grow_cleared (length, true);
4512 : 795590 : for (i = 0; i < length; i++)
4513 : : {
4514 : 564879 : es->param[i].change_prob = streamer_read_uhwi (ib);
4515 : 564879 : bitpack_d bp = streamer_read_bitpack (ib);
4516 : 1694637 : es->param[i].points_to_local_or_readonly_memory
4517 : 564879 : = bp_unpack_value (&bp, 1);
4518 : 1694637 : es->param[i].points_to_possible_sra_candidate
4519 : 564879 : = bp_unpack_value (&bp, 1);
4520 : : }
4521 : : }
4522 : : else
4523 : : {
4524 : 131529 : for (i = 0; i < length; i++)
4525 : : {
4526 : 20184 : streamer_read_uhwi (ib);
4527 : 20184 : streamer_read_uhwi (ib);
4528 : : }
4529 : : }
4530 : 342056 : }
4531 : :
4532 : :
4533 : : /* Stream in inline summaries from the section. */
4534 : :
4535 : : static void
4536 : 13877 : inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4537 : : size_t len)
4538 : : {
4539 : 13877 : const struct lto_function_header *header =
4540 : : (const struct lto_function_header *) data;
4541 : 13877 : const int cfg_offset = sizeof (struct lto_function_header);
4542 : 13877 : const int main_offset = cfg_offset + header->cfg_size;
4543 : 13877 : const int string_offset = main_offset + header->main_size;
4544 : 13877 : class data_in *data_in;
4545 : 13877 : unsigned int i, count2, j;
4546 : 13877 : unsigned int f_count;
4547 : :
4548 : 13877 : lto_input_block ib ((const char *) data + main_offset, header->main_size,
4549 : 13877 : file_data);
4550 : :
4551 : 13877 : data_in =
4552 : 27754 : lto_data_in_create (file_data, (const char *) data + string_offset,
4553 : 13877 : header->string_size, vNULL);
4554 : 13877 : f_count = streamer_read_uhwi (&ib);
4555 : 97742 : for (i = 0; i < f_count; i++)
4556 : : {
4557 : 83865 : unsigned int index;
4558 : 83865 : struct cgraph_node *node;
4559 : 83865 : class ipa_fn_summary *info;
4560 : 83865 : class ipa_node_params *params_summary;
4561 : 83865 : class ipa_size_summary *size_info;
4562 : 83865 : lto_symtab_encoder_t encoder;
4563 : 83865 : struct bitpack_d bp;
4564 : 83865 : struct cgraph_edge *e;
4565 : 83865 : ipa_predicate p;
4566 : :
4567 : 83865 : index = streamer_read_uhwi (&ib);
4568 : 83865 : encoder = file_data->symtab_node_encoder;
4569 : 83865 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4570 : : index));
4571 : 83865 : info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
4572 : 83865 : params_summary = node->prevailing_p ()
4573 : 83865 : ? ipa_node_params_sum->get (node) : NULL;
4574 : 167730 : size_info = node->prevailing_p ()
4575 : 83865 : ? ipa_size_summaries->get_create (node) : NULL;
4576 : :
4577 : 83865 : int stack_size = streamer_read_uhwi (&ib);
4578 : 83865 : int size = streamer_read_uhwi (&ib);
4579 : 83865 : sreal time = sreal::stream_in (&ib);
4580 : :
4581 : 83865 : if (info)
4582 : : {
4583 : 83811 : info->estimated_stack_size
4584 : 83811 : = size_info->estimated_self_stack_size = stack_size;
4585 : 83811 : size_info->size = size_info->self_size = size;
4586 : 83811 : info->time = time;
4587 : : }
4588 : :
4589 : 83865 : bp = streamer_read_bitpack (&ib);
4590 : 83865 : if (info)
4591 : : {
4592 : 83811 : info->inlinable = bp_unpack_value (&bp, 1);
4593 : 83811 : info->fp_expressions = bp_unpack_value (&bp, 1);
4594 : 83811 : if (!lto_stream_offload_p)
4595 : 83811 : info->target_info = streamer_read_uhwi (&ib);
4596 : : }
4597 : : else
4598 : : {
4599 : 54 : bp_unpack_value (&bp, 1);
4600 : 54 : bp_unpack_value (&bp, 1);
4601 : 54 : if (!lto_stream_offload_p)
4602 : 54 : streamer_read_uhwi (&ib);
4603 : : }
4604 : :
4605 : 83865 : count2 = streamer_read_uhwi (&ib);
4606 : 83865 : gcc_assert (!info || !info->conds);
4607 : 83811 : if (info)
4608 : 83811 : vec_safe_reserve_exact (info->conds, count2);
4609 : 154858 : for (j = 0; j < count2; j++)
4610 : : {
4611 : 70993 : struct condition c;
4612 : 70993 : unsigned int k, count3;
4613 : 70993 : c.operand_num = streamer_read_uhwi (&ib);
4614 : 70993 : c.code = (enum tree_code) streamer_read_uhwi (&ib);
4615 : 70993 : c.type = stream_read_tree (&ib, data_in);
4616 : 70993 : c.val = stream_read_tree (&ib, data_in);
4617 : 70993 : bp = streamer_read_bitpack (&ib);
4618 : 70993 : c.agg_contents = bp_unpack_value (&bp, 1);
4619 : 70993 : c.by_ref = bp_unpack_value (&bp, 1);
4620 : 70993 : if (c.agg_contents)
4621 : 9949 : c.offset = streamer_read_uhwi (&ib);
4622 : 70993 : count3 = streamer_read_uhwi (&ib);
4623 : 70993 : c.param_ops = NULL;
4624 : 70993 : if (info)
4625 : 70993 : vec_safe_reserve_exact (c.param_ops, count3);
4626 : 70993 : if (params_summary)
4627 : 70993 : ipa_set_param_used_by_ipa_predicates
4628 : 70993 : (params_summary, c.operand_num, true);
4629 : 74048 : for (k = 0; k < count3; k++)
4630 : : {
4631 : 3055 : struct expr_eval_op op;
4632 : 3055 : enum gimple_rhs_class rhs_class;
4633 : 3055 : op.code = (enum tree_code) streamer_read_uhwi (&ib);
4634 : 3055 : op.type = stream_read_tree (&ib, data_in);
4635 : 3055 : switch (rhs_class = get_gimple_rhs_class (op.code))
4636 : : {
4637 : 1345 : case GIMPLE_UNARY_RHS:
4638 : 1345 : op.index = 0;
4639 : 1345 : op.val[0] = NULL_TREE;
4640 : 1345 : op.val[1] = NULL_TREE;
4641 : 1345 : break;
4642 : :
4643 : 1710 : case GIMPLE_BINARY_RHS:
4644 : 1710 : case GIMPLE_TERNARY_RHS:
4645 : 1710 : bp = streamer_read_bitpack (&ib);
4646 : 1710 : op.index = bp_unpack_value (&bp, 2);
4647 : 1710 : op.val[0] = stream_read_tree (&ib, data_in);
4648 : 1710 : if (rhs_class == GIMPLE_BINARY_RHS)
4649 : 1710 : op.val[1] = NULL_TREE;
4650 : : else
4651 : 0 : op.val[1] = stream_read_tree (&ib, data_in);
4652 : : break;
4653 : :
4654 : 0 : default:
4655 : 0 : fatal_error (UNKNOWN_LOCATION,
4656 : : "invalid fnsummary in LTO stream");
4657 : : }
4658 : 3055 : if (info)
4659 : 3055 : c.param_ops->quick_push (op);
4660 : : }
4661 : 70993 : if (info)
4662 : 70993 : info->conds->quick_push (c);
4663 : : }
4664 : 83865 : count2 = streamer_read_uhwi (&ib);
4665 : 83865 : gcc_assert (!info || !info->size_time_table.length ());
4666 : 83865 : if (info && count2)
4667 : 83811 : info->size_time_table.reserve_exact (count2);
4668 : 312665 : for (j = 0; j < count2; j++)
4669 : : {
4670 : 228800 : class size_time_entry e;
4671 : :
4672 : 228800 : e.size = streamer_read_uhwi (&ib);
4673 : 228800 : e.time = sreal::stream_in (&ib);
4674 : 228800 : e.exec_predicate.stream_in (&ib);
4675 : 228800 : e.nonconst_predicate.stream_in (&ib);
4676 : :
4677 : 228800 : if (info)
4678 : 228692 : info->size_time_table.quick_push (e);
4679 : : }
4680 : :
4681 : 83865 : count2 = streamer_read_uhwi (&ib);
4682 : 85051 : for (j = 0; j < count2; j++)
4683 : : {
4684 : 1186 : p.stream_in (&ib);
4685 : 1186 : sreal fcp_freq = sreal::stream_in (&ib);
4686 : 1186 : if (info)
4687 : : {
4688 : 1186 : ipa_freqcounting_predicate fcp;
4689 : 1186 : fcp.predicate = NULL;
4690 : 1186 : set_hint_predicate (&fcp.predicate, p);
4691 : 1186 : fcp.freq = fcp_freq;
4692 : 1186 : vec_safe_push (info->loop_iterations, fcp);
4693 : : }
4694 : : }
4695 : 83865 : count2 = streamer_read_uhwi (&ib);
4696 : 84145 : for (j = 0; j < count2; j++)
4697 : : {
4698 : 280 : p.stream_in (&ib);
4699 : 280 : sreal fcp_freq = sreal::stream_in (&ib);
4700 : 280 : if (info)
4701 : : {
4702 : 280 : ipa_freqcounting_predicate fcp;
4703 : 280 : fcp.predicate = NULL;
4704 : 280 : set_hint_predicate (&fcp.predicate, p);
4705 : 280 : fcp.freq = fcp_freq;
4706 : 280 : vec_safe_push (info->loop_strides, fcp);
4707 : : }
4708 : : }
4709 : 83865 : count2 = streamer_read_uhwi (&ib);
4710 : 83865 : if (info && count2)
4711 : 6 : info->builtin_constant_p_parms.reserve_exact (count2);
4712 : 83871 : for (j = 0; j < count2; j++)
4713 : : {
4714 : 6 : int parm = streamer_read_uhwi (&ib);
4715 : 6 : if (info)
4716 : 6 : info->builtin_constant_p_parms.quick_push (parm);
4717 : : }
4718 : 424541 : for (e = node->callees; e; e = e->next_callee)
4719 : 340676 : read_ipa_call_summary (&ib, e, info != NULL);
4720 : 85245 : for (e = node->indirect_calls; e; e = e->next_callee)
4721 : 1380 : read_ipa_call_summary (&ib, e, info != NULL);
4722 : : }
4723 : :
4724 : 13877 : lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4725 : : len);
4726 : 13877 : lto_data_in_delete (data_in);
4727 : 13877 : }
4728 : :
4729 : :
4730 : : /* Read inline summary. Jump functions are shared among ipa-cp
4731 : : and inliner, so when ipa-cp is active, we don't need to write them
4732 : : twice. */
4733 : :
4734 : : static void
4735 : 12874 : ipa_fn_summary_read (void)
4736 : : {
4737 : 12874 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4738 : 12874 : struct lto_file_decl_data *file_data;
4739 : 12874 : unsigned int j = 0;
4740 : :
4741 : 12874 : ipa_prop_read_jump_functions ();
4742 : 12874 : ipa_fn_summary_alloc ();
4743 : :
4744 : 39625 : while ((file_data = file_data_vec[j++]))
4745 : : {
4746 : 13877 : size_t len;
4747 : 13877 : const char *data
4748 : 13877 : = lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary,
4749 : : &len);
4750 : 13877 : if (data)
4751 : 13877 : inline_read_section (file_data, data, len);
4752 : : else
4753 : : /* Fatal error here. We do not want to support compiling ltrans units
4754 : : with different version of compiler or different flags than the WPA
4755 : : unit, so this should never happen. */
4756 : 0 : fatal_error (input_location,
4757 : : "ipa inline summary is missing in input file");
4758 : : }
4759 : 12874 : ipa_register_cgraph_hooks ();
4760 : :
4761 : 12874 : gcc_assert (ipa_fn_summaries);
4762 : 12874 : ipa_fn_summaries->enable_insertion_hook ();
4763 : 12874 : }
4764 : :
4765 : :
4766 : : /* Write inline summary for edge E to OB. */
4767 : :
4768 : : static void
4769 : 371969 : write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
4770 : : {
4771 : 371969 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4772 : 371969 : int i;
4773 : :
4774 : 371969 : streamer_write_uhwi (ob, es->call_stmt_size);
4775 : 371969 : streamer_write_uhwi (ob, es->call_stmt_time);
4776 : 371969 : streamer_write_uhwi (ob, es->loop_depth);
4777 : :
4778 : 371969 : bitpack_d bp = bitpack_create (ob->main_stream);
4779 : 371969 : bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
4780 : 371969 : streamer_write_bitpack (&bp);
4781 : :
4782 : 371969 : if (es->predicate)
4783 : 9114 : es->predicate->stream_out (ob);
4784 : : else
4785 : 362855 : streamer_write_uhwi (ob, 0);
4786 : 371969 : streamer_write_uhwi (ob, es->param.length ());
4787 : 2255176 : for (i = 0; i < (int) es->param.length (); i++)
4788 : : {
4789 : 623778 : streamer_write_uhwi (ob, es->param[i].change_prob);
4790 : 623778 : bp = bitpack_create (ob->main_stream);
4791 : 623778 : bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1);
4792 : 623778 : bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1);
4793 : 623778 : streamer_write_bitpack (&bp);
4794 : : }
4795 : 371969 : }
4796 : :
4797 : :
4798 : : /* Write inline summary for node in SET.
4799 : : Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4800 : : active, we don't need to write them twice. */
4801 : :
4802 : : static void
4803 : 23637 : ipa_fn_summary_write (void)
4804 : : {
4805 : 23637 : struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
4806 : 23637 : lto_symtab_encoder_iterator lsei;
4807 : 23637 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4808 : 23637 : unsigned int count = 0;
4809 : :
4810 : 251544 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4811 : 102249 : lsei_next_function_in_partition (&lsei))
4812 : : {
4813 : 102249 : cgraph_node *cnode = lsei_cgraph_node (lsei);
4814 : 102249 : if (cnode->definition && !cnode->alias)
4815 : 99818 : count++;
4816 : : }
4817 : 23637 : streamer_write_uhwi (ob, count);
4818 : :
4819 : 251544 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4820 : 102249 : lsei_next_function_in_partition (&lsei))
4821 : : {
4822 : 102249 : cgraph_node *cnode = lsei_cgraph_node (lsei);
4823 : 102249 : if (cnode->definition && !cnode->alias)
4824 : : {
4825 : 99818 : class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
4826 : 99818 : class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
4827 : 99818 : struct bitpack_d bp;
4828 : 99818 : struct cgraph_edge *edge;
4829 : 99818 : int i;
4830 : 99818 : size_time_entry *e;
4831 : 99818 : struct condition *c;
4832 : :
4833 : 99818 : streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
4834 : 99818 : streamer_write_hwi (ob, size_info->estimated_self_stack_size);
4835 : 99818 : streamer_write_hwi (ob, size_info->self_size);
4836 : 99818 : info->time.stream_out (ob);
4837 : 99818 : bp = bitpack_create (ob->main_stream);
4838 : 99818 : bp_pack_value (&bp, info->inlinable, 1);
4839 : 99818 : bp_pack_value (&bp, info->fp_expressions, 1);
4840 : 99818 : streamer_write_bitpack (&bp);
4841 : 99818 : if (!lto_stream_offload_p)
4842 : 99818 : streamer_write_uhwi (ob, info->target_info);
4843 : 99818 : streamer_write_uhwi (ob, vec_safe_length (info->conds));
4844 : 185210 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4845 : : {
4846 : 85392 : int j;
4847 : 85392 : struct expr_eval_op *op;
4848 : :
4849 : 85392 : streamer_write_uhwi (ob, c->operand_num);
4850 : 85392 : streamer_write_uhwi (ob, c->code);
4851 : 85392 : stream_write_tree (ob, c->type, true);
4852 : 85392 : stream_write_tree (ob, c->val, true);
4853 : 85392 : bp = bitpack_create (ob->main_stream);
4854 : 85392 : bp_pack_value (&bp, c->agg_contents, 1);
4855 : 85392 : bp_pack_value (&bp, c->by_ref, 1);
4856 : 85392 : streamer_write_bitpack (&bp);
4857 : 85392 : if (c->agg_contents)
4858 : 12637 : streamer_write_uhwi (ob, c->offset);
4859 : 85392 : streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
4860 : 176729 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
4861 : : {
4862 : 3518 : streamer_write_uhwi (ob, op->code);
4863 : 3518 : stream_write_tree (ob, op->type, true);
4864 : 3518 : if (op->val[0])
4865 : : {
4866 : 1990 : bp = bitpack_create (ob->main_stream);
4867 : 1990 : bp_pack_value (&bp, op->index, 2);
4868 : 1990 : streamer_write_bitpack (&bp);
4869 : 1990 : stream_write_tree (ob, op->val[0], true);
4870 : 1990 : if (op->val[1])
4871 : 4 : stream_write_tree (ob, op->val[1], true);
4872 : : }
4873 : : }
4874 : : }
4875 : 99818 : streamer_write_uhwi (ob, info->size_time_table.length ());
4876 : 475548 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4877 : : {
4878 : 275912 : streamer_write_uhwi (ob, e->size);
4879 : 275912 : e->time.stream_out (ob);
4880 : 275912 : e->exec_predicate.stream_out (ob);
4881 : 275912 : e->nonconst_predicate.stream_out (ob);
4882 : : }
4883 : 99818 : ipa_freqcounting_predicate *fcp;
4884 : 99818 : streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations));
4885 : 201352 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
4886 : : {
4887 : 1716 : fcp->predicate->stream_out (ob);
4888 : 1716 : fcp->freq.stream_out (ob);
4889 : : }
4890 : 99818 : streamer_write_uhwi (ob, vec_safe_length (info->loop_strides));
4891 : 199962 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
4892 : : {
4893 : 326 : fcp->predicate->stream_out (ob);
4894 : 326 : fcp->freq.stream_out (ob);
4895 : : }
4896 : 99818 : streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ());
4897 : 99818 : int ip;
4898 : 199648 : for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip);
4899 : : i++)
4900 : 12 : streamer_write_uhwi (ob, ip);
4901 : 469984 : for (edge = cnode->callees; edge; edge = edge->next_callee)
4902 : 370166 : write_ipa_call_summary (ob, edge);
4903 : 101621 : for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
4904 : 1803 : write_ipa_call_summary (ob, edge);
4905 : : }
4906 : : }
4907 : 23637 : streamer_write_char_stream (ob->main_stream, 0);
4908 : 23637 : produce_asm (ob, NULL);
4909 : 23637 : destroy_output_block (ob);
4910 : :
4911 : 23637 : ipa_prop_write_jump_functions ();
4912 : 23637 : }
4913 : :
4914 : :
4915 : : /* Release function summary. */
4916 : :
4917 : : void
4918 : 717402 : ipa_free_fn_summary (void)
4919 : : {
4920 : 717402 : if (!ipa_call_summaries)
4921 : : return;
4922 : 454694 : ggc_delete (ipa_fn_summaries);
4923 : 454694 : ipa_fn_summaries = NULL;
4924 : 454694 : delete ipa_call_summaries;
4925 : 454694 : ipa_call_summaries = NULL;
4926 : 454694 : edge_predicate_pool.release ();
4927 : : /* During IPA this is one of largest datastructures to release. */
4928 : 454694 : if (flag_wpa)
4929 : 8797 : ggc_trim ();
4930 : : }
4931 : :
4932 : : /* Release function summary. */
4933 : :
4934 : : void
4935 : 717402 : ipa_free_size_summary (void)
4936 : : {
4937 : 717402 : if (!ipa_size_summaries)
4938 : : return;
4939 : 454694 : delete ipa_size_summaries;
4940 : 454694 : ipa_size_summaries = NULL;
4941 : : }
4942 : :
4943 : : namespace {
4944 : :
4945 : : const pass_data pass_data_local_fn_summary =
4946 : : {
4947 : : GIMPLE_PASS, /* type */
4948 : : "local-fnsummary", /* name */
4949 : : OPTGROUP_INLINE, /* optinfo_flags */
4950 : : TV_INLINE_PARAMETERS, /* tv_id */
4951 : : 0, /* properties_required */
4952 : : 0, /* properties_provided */
4953 : : 0, /* properties_destroyed */
4954 : : 0, /* todo_flags_start */
4955 : : 0, /* todo_flags_finish */
4956 : : };
4957 : :
4958 : : class pass_local_fn_summary : public gimple_opt_pass
4959 : : {
4960 : : public:
4961 : 571234 : pass_local_fn_summary (gcc::context *ctxt)
4962 : 1142468 : : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
4963 : : {}
4964 : :
4965 : : /* opt_pass methods: */
4966 : 285617 : opt_pass * clone () final override
4967 : : {
4968 : 285617 : return new pass_local_fn_summary (m_ctxt);
4969 : : }
4970 : 5246891 : unsigned int execute (function *) final override
4971 : : {
4972 : 5246891 : return compute_fn_summary_for_current ();
4973 : : }
4974 : :
4975 : : }; // class pass_local_fn_summary
4976 : :
4977 : : } // anon namespace
4978 : :
4979 : : gimple_opt_pass *
4980 : 285617 : make_pass_local_fn_summary (gcc::context *ctxt)
4981 : : {
4982 : 285617 : return new pass_local_fn_summary (ctxt);
4983 : : }
4984 : :
4985 : :
4986 : : /* Free inline summary. */
4987 : :
4988 : : namespace {
4989 : :
4990 : : const pass_data pass_data_ipa_free_fn_summary =
4991 : : {
4992 : : SIMPLE_IPA_PASS, /* type */
4993 : : "free-fnsummary", /* name */
4994 : : OPTGROUP_NONE, /* optinfo_flags */
4995 : : TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
4996 : : 0, /* properties_required */
4997 : : 0, /* properties_provided */
4998 : : 0, /* properties_destroyed */
4999 : : 0, /* todo_flags_start */
5000 : : 0, /* todo_flags_finish */
5001 : : };
5002 : :
5003 : : class pass_ipa_free_fn_summary : public simple_ipa_opt_pass
5004 : : {
5005 : : public:
5006 : 571234 : pass_ipa_free_fn_summary (gcc::context *ctxt)
5007 : 571234 : : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
5008 : 1142468 : small_p (false)
5009 : : {}
5010 : :
5011 : : /* opt_pass methods: */
5012 : 285617 : opt_pass *clone () final override
5013 : : {
5014 : 285617 : return new pass_ipa_free_fn_summary (m_ctxt);
5015 : : }
5016 : 571234 : void set_pass_param (unsigned int n, bool param) final override
5017 : : {
5018 : 571234 : gcc_assert (n == 0);
5019 : 571234 : small_p = param;
5020 : 571234 : }
5021 : 481333 : bool gate (function *) final override { return true; }
5022 : 459293 : unsigned int execute (function *) final override
5023 : : {
5024 : 459293 : ipa_free_fn_summary ();
5025 : : /* Free ipa-prop structures if they are no longer needed. */
5026 : 459293 : ipa_free_all_structures_after_iinln ();
5027 : 459293 : if (!flag_wpa)
5028 : 450496 : ipa_free_size_summary ();
5029 : 459293 : return 0;
5030 : : }
5031 : :
5032 : : private:
5033 : : bool small_p;
5034 : : }; // class pass_ipa_free_fn_summary
5035 : :
5036 : : } // anon namespace
5037 : :
5038 : : simple_ipa_opt_pass *
5039 : 285617 : make_pass_ipa_free_fn_summary (gcc::context *ctxt)
5040 : : {
5041 : 285617 : return new pass_ipa_free_fn_summary (ctxt);
5042 : : }
5043 : :
5044 : : namespace {
5045 : :
5046 : : const pass_data pass_data_ipa_fn_summary =
5047 : : {
5048 : : IPA_PASS, /* type */
5049 : : "fnsummary", /* name */
5050 : : OPTGROUP_INLINE, /* optinfo_flags */
5051 : : TV_IPA_FNSUMMARY, /* tv_id */
5052 : : 0, /* properties_required */
5053 : : 0, /* properties_provided */
5054 : : 0, /* properties_destroyed */
5055 : : 0, /* todo_flags_start */
5056 : : ( TODO_dump_symtab ), /* todo_flags_finish */
5057 : : };
5058 : :
5059 : : class pass_ipa_fn_summary : public ipa_opt_pass_d
5060 : : {
5061 : : public:
5062 : 285617 : pass_ipa_fn_summary (gcc::context *ctxt)
5063 : : : ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt,
5064 : : ipa_fn_summary_generate, /* generate_summary */
5065 : : ipa_fn_summary_write, /* write_summary */
5066 : : ipa_fn_summary_read, /* read_summary */
5067 : : NULL, /* write_optimization_summary */
5068 : : NULL, /* read_optimization_summary */
5069 : : NULL, /* stmt_fixup */
5070 : : 0, /* function_transform_todo_flags_start */
5071 : : NULL, /* function_transform */
5072 : 285617 : NULL) /* variable_transform */
5073 : 285617 : {}
5074 : :
5075 : : /* opt_pass methods: */
5076 : 229269 : unsigned int execute (function *) final override { return 0; }
5077 : :
5078 : : }; // class pass_ipa_fn_summary
5079 : :
5080 : : } // anon namespace
5081 : :
5082 : : ipa_opt_pass_d *
5083 : 285617 : make_pass_ipa_fn_summary (gcc::context *ctxt)
5084 : : {
5085 : 285617 : return new pass_ipa_fn_summary (ctxt);
5086 : : }
5087 : :
5088 : : /* Reset all state within ipa-fnsummary.cc so that we can rerun the compiler
5089 : : within the same process. For use by toplev::finalize. */
5090 : :
5091 : : void
5092 : 257333 : ipa_fnsummary_cc_finalize (void)
5093 : : {
5094 : 257333 : ipa_free_fn_summary ();
5095 : 257333 : ipa_free_size_summary ();
5096 : 257333 : }
|