Branch data Line data Source code
1 : : /* Function summary pass.
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : : Contributed by Jan Hubicka
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /* Analysis of function bodies used by inter-procedural passes
22 : :
23 : : We estimate for each function
24 : : - function body size and size after specializing into given context
25 : : - average function execution time in a given context
26 : : - function frame size
27 : : For each call
28 : : - call statement size, time and how often the parameters change
29 : :
30 : : ipa_fn_summary data structures store above information locally (i.e.
31 : : parameters of the function itself) and globally (i.e. parameters of
32 : : the function created by applying all the inline decisions already
33 : : present in the callgraph).
34 : :
35 : : We provide access to the ipa_fn_summary data structure and
36 : : basic logic updating the parameters when inlining is performed.
37 : :
38 : : The summaries are context sensitive. Context means
39 : : 1) partial assignment of known constant values of operands
40 : : 2) whether function is inlined into the call or not.
41 : : It is easy to add more variants. To represent function size and time
42 : : that depends on context (i.e. it is known to be optimized away when
43 : : context is known either by inlining or from IP-CP and cloning),
44 : : we use predicates.
45 : :
46 : : estimate_edge_size_and_time can be used to query
47 : : function size/time in the given context. ipa_merge_fn_summary_after_inlining merges
48 : : properties of caller and callee after inlining.
49 : :
50 : : Finally pass_inline_parameters is exported. This is used to drive
51 : : computation of function parameters used by the early inliner. IPA
52 : : inlined performs analysis via its analyze_function method. */
53 : :
54 : : #include "config.h"
55 : : #define INCLUDE_VECTOR
56 : : #include "system.h"
57 : : #include "coretypes.h"
58 : : #include "backend.h"
59 : : #include "target.h"
60 : : #include "tree.h"
61 : : #include "gimple.h"
62 : : #include "alloc-pool.h"
63 : : #include "tree-pass.h"
64 : : #include "ssa.h"
65 : : #include "tree-streamer.h"
66 : : #include "cgraph.h"
67 : : #include "diagnostic.h"
68 : : #include "fold-const.h"
69 : : #include "print-tree.h"
70 : : #include "tree-inline.h"
71 : : #include "gimple-pretty-print.h"
72 : : #include "cfganal.h"
73 : : #include "gimple-iterator.h"
74 : : #include "tree-cfg.h"
75 : : #include "tree-ssa-loop-niter.h"
76 : : #include "tree-ssa-loop.h"
77 : : #include "symbol-summary.h"
78 : : #include "sreal.h"
79 : : #include "ipa-cp.h"
80 : : #include "ipa-prop.h"
81 : : #include "ipa-fnsummary.h"
82 : : #include "cfgloop.h"
83 : : #include "tree-scalar-evolution.h"
84 : : #include "ipa-utils.h"
85 : : #include "cfgexpand.h"
86 : : #include "gimplify.h"
87 : : #include "stringpool.h"
88 : : #include "attribs.h"
89 : : #include "tree-into-ssa.h"
90 : : #include "symtab-clones.h"
91 : : #include "gimple-range.h"
92 : : #include "tree-dfa.h"
93 : :
94 : : /* Summaries. */
95 : : fast_function_summary <ipa_fn_summary *, va_gc> *ipa_fn_summaries;
96 : : fast_function_summary <ipa_size_summary *, va_heap> *ipa_size_summaries;
97 : : fast_call_summary <ipa_call_summary *, va_heap> *ipa_call_summaries;
98 : :
99 : : /* Edge predicates goes here. */
100 : : static object_allocator<ipa_predicate> edge_predicate_pool ("edge predicates");
101 : :
102 : :
103 : : /* Dump IPA hints. */
104 : : void
105 : 185 : ipa_dump_hints (FILE *f, ipa_hints hints)
106 : : {
107 : 185 : if (!hints)
108 : : return;
109 : 148 : fprintf (f, "IPA hints:");
110 : 148 : if (hints & INLINE_HINT_indirect_call)
111 : : {
112 : 23 : hints &= ~INLINE_HINT_indirect_call;
113 : 23 : fprintf (f, " indirect_call");
114 : : }
115 : 148 : if (hints & INLINE_HINT_loop_iterations)
116 : : {
117 : 3 : hints &= ~INLINE_HINT_loop_iterations;
118 : 3 : fprintf (f, " loop_iterations");
119 : : }
120 : 148 : if (hints & INLINE_HINT_loop_stride)
121 : : {
122 : 3 : hints &= ~INLINE_HINT_loop_stride;
123 : 3 : fprintf (f, " loop_stride");
124 : : }
125 : 148 : if (hints & INLINE_HINT_same_scc)
126 : : {
127 : 4 : hints &= ~INLINE_HINT_same_scc;
128 : 4 : fprintf (f, " same_scc");
129 : : }
130 : 148 : if (hints & INLINE_HINT_in_scc)
131 : : {
132 : 11 : hints &= ~INLINE_HINT_in_scc;
133 : 11 : fprintf (f, " in_scc");
134 : : }
135 : 148 : if (hints & INLINE_HINT_cross_module)
136 : : {
137 : 2 : hints &= ~INLINE_HINT_cross_module;
138 : 2 : fprintf (f, " cross_module");
139 : : }
140 : 148 : if (hints & INLINE_HINT_declared_inline)
141 : : {
142 : 122 : hints &= ~INLINE_HINT_declared_inline;
143 : 122 : fprintf (f, " declared_inline");
144 : : }
145 : 148 : if (hints & INLINE_HINT_known_hot)
146 : : {
147 : 1 : hints &= ~INLINE_HINT_known_hot;
148 : 1 : fprintf (f, " known_hot");
149 : : }
150 : 148 : if (hints & INLINE_HINT_builtin_constant_p)
151 : : {
152 : 4 : hints &= ~INLINE_HINT_builtin_constant_p;
153 : 4 : fprintf (f, " builtin_constant_p");
154 : : }
155 : 148 : gcc_assert (!hints);
156 : : }
157 : :
158 : :
159 : : /* Record SIZE and TIME to SUMMARY.
160 : : The accounted code will be executed when EXEC_PRED is true.
161 : : When NONCONST_PRED is false the code will evaluate to constant and
162 : : will get optimized out in specialized clones of the function.
163 : : If CALL is true account to call_size_time_table rather than
164 : : size_time_table. */
165 : :
166 : : void
167 : 134638027 : 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 : 134638027 : size_time_entry *e;
173 : 134638027 : bool found = false;
174 : 134638027 : int i;
175 : 134638027 : ipa_predicate nonconst_pred;
176 : 134638027 : vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table;
177 : :
178 : 134638027 : if (exec_pred == false)
179 : 4492053 : return;
180 : :
181 : 134441641 : nonconst_pred = nonconst_pred_in & exec_pred;
182 : :
183 : 134441641 : 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 : 157150232 : if (!size && time == 0 && table->length ())
189 : 3584558 : return;
190 : :
191 : : /* Only for calls we are unaccounting what we previously recorded. */
192 : 130145974 : gcc_checking_assert (time >= 0 || call);
193 : :
194 : 468670965 : for (i = 0; table->iterate (i, &e); i++)
195 : 439238031 : if (e->exec_predicate == exec_pred
196 : 439238031 : && e->nonconst_predicate == nonconst_pred)
197 : : {
198 : : found = true;
199 : : break;
200 : : }
201 : 130145974 : 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 : 130150175 : if (dump_file && (dump_flags & TDF_DETAILS) && (time != 0 || size))
212 : : {
213 : 3940 : fprintf (dump_file,
214 : : "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate exec:",
215 : 3710 : ((double) size) / ipa_fn_summary::size_scale,
216 : : (time.to_double ()), found ? "" : "new ");
217 : 3710 : exec_pred.dump (dump_file, conds, 0);
218 : 3710 : if (exec_pred != nonconst_pred)
219 : : {
220 : 84 : fprintf (dump_file, " nonconst:");
221 : 84 : nonconst_pred.dump (dump_file, conds);
222 : : }
223 : : else
224 : 3626 : fprintf (dump_file, "\n");
225 : : }
226 : 130145974 : if (!found)
227 : : {
228 : 29432934 : class size_time_entry new_entry;
229 : 29432934 : new_entry.size = size;
230 : 29432934 : new_entry.time = time;
231 : 29432934 : new_entry.exec_predicate = exec_pred;
232 : 29432934 : new_entry.nonconst_predicate = nonconst_pred;
233 : 29432934 : if (call)
234 : 1336968 : call_size_time_table.safe_push (new_entry);
235 : : else
236 : 28095966 : size_time_table.safe_push (new_entry);
237 : : }
238 : : else
239 : : {
240 : 100713040 : e->size += size;
241 : 100713040 : e->time += time;
242 : : /* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */
243 : : /* Tolerate small roundoff issues. */
244 : 100713040 : if (e->time < 0)
245 : 87 : 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 : 225575 : redirect_to_unreachable (struct cgraph_edge *e)
253 : : {
254 : 225575 : struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
255 : 225575 : struct cgraph_node *target
256 : 225575 : = cgraph_node::get_create (builtin_decl_unreachable ());
257 : :
258 : 225575 : gcc_checking_assert (lookup_attribute ("cold",
259 : : DECL_ATTRIBUTES (target->decl)));
260 : :
261 : 225575 : if (e->speculative)
262 : 303 : e = cgraph_edge::resolve_speculation (e, target->decl);
263 : 225272 : else if (!e->callee)
264 : 772 : e = cgraph_edge::make_direct (e, target);
265 : : else
266 : 224500 : e->redirect_callee (target);
267 : 225575 : class ipa_call_summary *es = ipa_call_summaries->get (e);
268 : 225575 : e->inline_failed = CIF_UNREACHABLE;
269 : 225575 : e->count = profile_count::zero ();
270 : 225575 : es->call_stmt_size = 0;
271 : 225575 : es->call_stmt_time = 0;
272 : 225575 : if (callee)
273 : 0 : callee->remove_symbol_and_inline_clones ();
274 : 225575 : return e;
275 : : }
276 : :
277 : : /* Set predicate for edge E. */
278 : :
279 : : static void
280 : 30003996 : edge_set_predicate (struct cgraph_edge *e, ipa_predicate *predicate)
281 : : {
282 : : /* If the edge is determined to be never executed, redirect it
283 : : to BUILTIN_UNREACHABLE to make it clear to IPA passes the call will
284 : : be optimized out. */
285 : 27444115 : if (predicate && *predicate == false
286 : : /* When handling speculative edges, we need to do the redirection
287 : : just once. Do it always on the direct edge, so we do not
288 : : attempt to resolve speculation while duplicating the edge. */
289 : 30229635 : && (!e->speculative || e->callee))
290 : 225575 : e = redirect_to_unreachable (e);
291 : :
292 : 30003996 : class ipa_call_summary *es = ipa_call_summaries->get (e);
293 : 57448111 : if (predicate && *predicate != true)
294 : : {
295 : 4236693 : if (!es->predicate)
296 : 3855848 : es->predicate = edge_predicate_pool.allocate ();
297 : 4236693 : *es->predicate = *predicate;
298 : : }
299 : : else
300 : : {
301 : 25767303 : if (es->predicate)
302 : 654700 : edge_predicate_pool.remove (es->predicate);
303 : 25767303 : es->predicate = NULL;
304 : : }
305 : 30003996 : }
306 : :
307 : : /* Set predicate for hint *P. */
308 : :
309 : : static void
310 : 164796 : set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate)
311 : : {
312 : 164796 : if (new_predicate == false || new_predicate == true)
313 : : {
314 : 2259 : if (*p)
315 : 0 : edge_predicate_pool.remove (*p);
316 : 2259 : *p = NULL;
317 : : }
318 : : else
319 : : {
320 : 162537 : if (!*p)
321 : 162537 : *p = edge_predicate_pool.allocate ();
322 : 162537 : **p = new_predicate;
323 : : }
324 : 164796 : }
325 : :
326 : : /* Find if NEW_PREDICATE is already in V and if so, increment its freq.
327 : : Otherwise add a new item to the vector with this predicate and frerq equal
328 : : to add_freq, unless the number of predicates would exceed MAX_NUM_PREDICATES
329 : : in which case the function does nothing. */
330 : :
331 : : static void
332 : 1106234 : add_freqcounting_predicate (vec<ipa_freqcounting_predicate, va_gc> **v,
333 : : const ipa_predicate &new_predicate, sreal add_freq,
334 : : unsigned max_num_predicates)
335 : : {
336 : 1106234 : if (new_predicate == false || new_predicate == true)
337 : : return;
338 : : ipa_freqcounting_predicate *f;
339 : 138559 : for (int i = 0; vec_safe_iterate (*v, i, &f); i++)
340 : 70850 : if (new_predicate == f->predicate)
341 : : {
342 : 0 : f->freq += add_freq;
343 : 0 : return;
344 : : }
345 : 93259 : if (vec_safe_length (*v) >= max_num_predicates)
346 : : /* Too many different predicates to account for. */
347 : : return;
348 : :
349 : 67437 : ipa_freqcounting_predicate fcp;
350 : 67437 : fcp.predicate = NULL;
351 : 67437 : set_hint_predicate (&fcp.predicate, new_predicate);
352 : 67437 : fcp.freq = add_freq;
353 : 67437 : vec_safe_push (*v, fcp);
354 : 67437 : return;
355 : : }
356 : :
357 : : /* Compute what conditions may or may not hold given information about
358 : : parameters. RET_CLAUSE returns truths that may hold in a specialized copy,
359 : : while RET_NONSPEC_CLAUSE returns truths that may hold in an nonspecialized
360 : : copy when called in a given context. It is a bitmask of conditions. Bit
361 : : 0 means that condition is known to be false, while bit 1 means that condition
362 : : may or may not be true. These differs - for example NOT_INLINED condition
363 : : is always false in the second and also builtin_constant_p tests cannot use
364 : : the fact that parameter is indeed a constant.
365 : :
366 : : When INLINE_P is true, assume that we are inlining. AVAL contains known
367 : : information about argument values. The function does not modify its content
368 : : and so AVALs could also be of type ipa_call_arg_values but so far all
369 : : callers work with the auto version and so we avoid the conversion for
370 : : convenience.
371 : :
372 : : ERROR_MARK value of an argument means compile time invariant. */
373 : :
374 : : static void
375 : 22098256 : evaluate_conditions_for_known_args (struct cgraph_node *node,
376 : : bool inline_p,
377 : : ipa_auto_call_arg_values *avals,
378 : : clause_t *ret_clause,
379 : : clause_t *ret_nonspec_clause,
380 : : ipa_call_summary *es)
381 : : {
382 : 22098256 : clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
383 : 22098256 : clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition;
384 : 22098256 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
385 : 22098256 : int i;
386 : 22098256 : struct condition *c;
387 : :
388 : 90175230 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
389 : : {
390 : 68076974 : tree val = NULL;
391 : 68076974 : tree res;
392 : 68076974 : int j;
393 : 68076974 : struct expr_eval_op *op;
394 : :
395 : 68076974 : if (c->code == ipa_predicate::not_sra_candidate)
396 : : {
397 : 16225764 : if (!inline_p
398 : 16225764 : || !es
399 : 15841935 : || (int)es->param.length () <= c->operand_num
400 : 24146704 : || !es->param[c->operand_num].points_to_possible_sra_candidate)
401 : 12186679 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
402 : 16225764 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
403 : 68076974 : continue;
404 : : }
405 : :
406 : 51851210 : if (c->agg_contents)
407 : : {
408 : 27651461 : if (c->code == ipa_predicate::changed
409 : 18061729 : && !c->by_ref
410 : 30120947 : && (avals->safe_sval_at(c->operand_num) == error_mark_node))
411 : 5212 : continue;
412 : :
413 : 27641037 : if (tree sval = avals->safe_sval_at (c->operand_num))
414 : 13228964 : val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref);
415 : 13228964 : if (!val)
416 : : {
417 : 27632642 : ipa_argagg_value_list avs (avals);
418 : 27632642 : val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
419 : 27632642 : c->by_ref);
420 : : }
421 : : }
422 : : else
423 : : {
424 : 24204961 : val = avals->safe_sval_at (c->operand_num);
425 : 24204961 : if (val && val == error_mark_node
426 : 2236641 : && c->code != ipa_predicate::changed)
427 : : val = NULL_TREE;
428 : : }
429 : :
430 : 39441908 : if (!val
431 : 38719245 : && (c->code == ipa_predicate::changed
432 : : || c->code == ipa_predicate::is_not_constant))
433 : : {
434 : 24792834 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
435 : 24792834 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
436 : 24792834 : continue;
437 : : }
438 : 27053164 : if (c->code == ipa_predicate::changed)
439 : : {
440 : 7257837 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
441 : 7257837 : continue;
442 : : }
443 : :
444 : 19795327 : if (c->code == ipa_predicate::is_not_constant)
445 : : {
446 : 5279 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
447 : 5279 : continue;
448 : : }
449 : :
450 : 19790048 : if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val)))
451 : : {
452 : 5836009 : if (c->type != TREE_TYPE (val))
453 : 990938 : val = fold_unary (VIEW_CONVERT_EXPR, c->type, val);
454 : 6191809 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
455 : : {
456 : 356233 : if (!val)
457 : : break;
458 : 355800 : if (!op->val[0])
459 : 112190 : val = fold_unary (op->code, op->type, val);
460 : 243610 : else if (!op->val[1])
461 : 487220 : val = fold_binary (op->code, op->type,
462 : : op->index ? op->val[0] : val,
463 : : op->index ? val : op->val[0]);
464 : 0 : else if (op->index == 0)
465 : 0 : val = fold_ternary (op->code, op->type,
466 : : val, op->val[0], op->val[1]);
467 : 0 : else if (op->index == 1)
468 : 0 : val = fold_ternary (op->code, op->type,
469 : : op->val[0], val, op->val[1]);
470 : 0 : else if (op->index == 2)
471 : 0 : val = fold_ternary (op->code, op->type,
472 : : op->val[0], op->val[1], val);
473 : : else
474 : : val = NULL_TREE;
475 : : }
476 : :
477 : 5836009 : res = val
478 : 5836009 : ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
479 : : : NULL;
480 : :
481 : 5835556 : if (res && integer_zerop (res))
482 : 3012785 : continue;
483 : 2823224 : if (res && integer_onep (res))
484 : : {
485 : 2801673 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
486 : 2801673 : nonspec_clause
487 : 2801673 : |= 1 << (i + ipa_predicate::first_dynamic_condition);
488 : 2801673 : continue;
489 : : }
490 : : }
491 : 13975590 : if (c->operand_num < (int) avals->m_known_value_ranges.length ()
492 : 8167600 : && !c->agg_contents
493 : 16181338 : && (!val || TREE_CODE (val) != INTEGER_CST))
494 : : {
495 : 2205435 : value_range vr (avals->m_known_value_ranges[c->operand_num]);
496 : 2205435 : if (!vr.undefined_p ()
497 : 1183266 : && !vr.varying_p ()
498 : 3388701 : && (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ())))
499 : : {
500 : 1182253 : if (!useless_type_conversion_p (c->type, vr.type ()))
501 : 1109 : range_cast (vr, c->type);
502 : :
503 : 1498046 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
504 : : {
505 : 353203 : if (vr.varying_p () || vr.undefined_p ())
506 : : break;
507 : :
508 : 315793 : value_range res (op->type);
509 : 315793 : if (!op->val[0])
510 : : {
511 : 121697 : value_range varying (op->type);
512 : 121697 : varying.set_varying (op->type);
513 : 121697 : range_op_handler handler (op->code);
514 : 121697 : if (!handler
515 : 121697 : || !res.supports_type_p (op->type)
516 : 243394 : || !handler.fold_range (res, op->type, vr, varying))
517 : 0 : res.set_varying (op->type);
518 : 121697 : }
519 : 194096 : else if (!op->val[1])
520 : : {
521 : 193924 : value_range op0 (TREE_TYPE (op->val[0]));
522 : 193924 : range_op_handler handler (op->code);
523 : :
524 : 193924 : ipa_get_range_from_ip_invariant (op0, op->val[0], node);
525 : :
526 : 193924 : if (!handler
527 : 193924 : || !res.supports_type_p (op->type)
528 : 387848 : || !handler.fold_range (res, op->type,
529 : 193924 : op->index ? op0 : vr,
530 : 387252 : op->index ? vr : op0))
531 : 0 : res.set_varying (op->type);
532 : 193924 : }
533 : : else
534 : 172 : res.set_varying (op->type);
535 : 315793 : vr = res;
536 : 315793 : }
537 : 1182253 : if (!vr.varying_p () && !vr.undefined_p ())
538 : : {
539 : 1130186 : int_range<2> res;
540 : 1130186 : value_range val_vr (TREE_TYPE (c->val));
541 : 1130186 : range_op_handler handler (c->code);
542 : :
543 : 1130186 : ipa_get_range_from_ip_invariant (val_vr, c->val, node);
544 : :
545 : 1130186 : if (!handler
546 : 1130186 : || !val_vr.supports_type_p (TREE_TYPE (c->val))
547 : 2260372 : || !handler.fold_range (res, boolean_type_node, vr, val_vr))
548 : 0 : res.set_varying (boolean_type_node);
549 : :
550 : 1130186 : if (res.zero_p ())
551 : 205462 : continue;
552 : 1130186 : }
553 : : }
554 : 2205435 : }
555 : :
556 : 13770128 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
557 : 13770128 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
558 : : }
559 : 22098256 : *ret_clause = clause;
560 : 22098256 : if (ret_nonspec_clause)
561 : 19157129 : *ret_nonspec_clause = nonspec_clause;
562 : 22098256 : }
563 : :
564 : : /* Return true if VRP will be exectued on the function.
565 : : We do not want to anticipate optimizations that will not happen.
566 : :
567 : : FIXME: This can be confused with -fdisable and debug counters and thus
568 : : it should not be used for correctness (only to make heuristics work).
569 : : This means that inliner should do its own optimizations of expressions
570 : : that it predicts to be constant so wrong code can not be triggered by
571 : : builtin_constant_p. */
572 : :
573 : : static bool
574 : 11872404 : vrp_will_run_p (struct cgraph_node *node)
575 : : {
576 : 11872404 : return (opt_for_fn (node->decl, optimize)
577 : 11872404 : && !opt_for_fn (node->decl, optimize_debug)
578 : 23743592 : && opt_for_fn (node->decl, flag_tree_vrp));
579 : : }
580 : :
581 : : /* Similarly about FRE. */
582 : :
583 : : static bool
584 : 14235355 : fre_will_run_p (struct cgraph_node *node)
585 : : {
586 : 14235355 : return (opt_for_fn (node->decl, optimize)
587 : 14235355 : && !opt_for_fn (node->decl, optimize_debug)
588 : 28468430 : && opt_for_fn (node->decl, flag_tree_fre));
589 : : }
590 : :
591 : : /* Work out what conditions might be true at invocation of E.
592 : : Compute costs for inlined edge if INLINE_P is true.
593 : :
594 : : Return in CLAUSE_PTR the evaluated conditions and in NONSPEC_CLAUSE_PTR
595 : : (if non-NULL) conditions evaluated for nonspecialized clone called
596 : : in a given context.
597 : :
598 : : Vectors in AVALS will be populated with useful known information about
599 : : argument values - information not known to have any uses will be omitted -
600 : : except for m_known_contexts which will only be calculated if
601 : : COMPUTE_CONTEXTS is true. */
602 : :
603 : : void
604 : 21910112 : evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
605 : : clause_t *clause_ptr,
606 : : clause_t *nonspec_clause_ptr,
607 : : ipa_auto_call_arg_values *avals,
608 : : bool compute_contexts)
609 : : {
610 : 21910112 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
611 : 21910112 : class ipa_fn_summary *info = ipa_fn_summaries->get (callee);
612 : 21910112 : class ipa_edge_args *args;
613 : 21910112 : class ipa_call_summary *es = NULL;
614 : :
615 : 21910112 : if (clause_ptr)
616 : 21910112 : *clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
617 : :
618 : 21910112 : if (ipa_node_params_sum
619 : 9890948 : && !e->call_stmt_cannot_inline_p
620 : 9890948 : && (info->conds || compute_contexts)
621 : 31801060 : && (args = ipa_edge_args_sum->get (e)) != NULL)
622 : : {
623 : 9836783 : struct cgraph_node *caller;
624 : 9836783 : class ipa_node_params *caller_parms_info, *callee_pi = NULL;
625 : 9836783 : int i, count = ipa_get_cs_argument_count (args);
626 : 9836783 : es = ipa_call_summaries->get (e);
627 : :
628 : 9836783 : if (count)
629 : : {
630 : 9138850 : if (e->caller->inlined_to)
631 : : caller = e->caller->inlined_to;
632 : : else
633 : 7358958 : caller = e->caller;
634 : 9138850 : caller_parms_info = ipa_node_params_sum->get (caller);
635 : 9138850 : callee_pi = ipa_node_params_sum->get (callee);
636 : :
637 : : /* Watch for thunks. */
638 : 9138850 : if (callee_pi)
639 : : /* Watch for variadic functions. */
640 : 9138505 : count = MIN (count, ipa_get_param_count (callee_pi));
641 : : }
642 : :
643 : 9138505 : if (callee_pi)
644 : 30161190 : for (i = 0; i < count; i++)
645 : : {
646 : 21022685 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
647 : :
648 : 21022685 : if (ipa_is_param_used_by_indirect_call (callee_pi, i)
649 : 21022685 : || ipa_is_param_used_by_ipa_predicates (callee_pi, i))
650 : : {
651 : : /* Determine if we know constant value of the parameter. */
652 : 14235355 : tree type = ipa_get_type (callee_pi, i);
653 : 14235355 : tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type);
654 : :
655 : 11177864 : if (!cst && e->call_stmt
656 : 25366460 : && i < (int)gimple_call_num_args (e->call_stmt))
657 : : {
658 : 11131105 : cst = gimple_call_arg (e->call_stmt, i);
659 : 11131105 : if (!is_gimple_min_invariant (cst))
660 : : cst = NULL;
661 : : }
662 : 3296332 : if (cst)
663 : : {
664 : 3249573 : gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
665 : 3249573 : if (!avals->m_known_vals.length ())
666 : 1730282 : avals->m_known_vals.safe_grow_cleared (count, true);
667 : 3249573 : avals->m_known_vals[i] = cst;
668 : : }
669 : 10985782 : else if (inline_p && !es->param[i].change_prob)
670 : : {
671 : 4101893 : if (!avals->m_known_vals.length ())
672 : 3377080 : avals->m_known_vals.safe_grow_cleared (count, true);
673 : 4101893 : avals->m_known_vals[i] = error_mark_node;
674 : : }
675 : :
676 : : /* If we failed to get simple constant, try value range. */
677 : 3249573 : if ((!cst || TREE_CODE (cst) != INTEGER_CST)
678 : 11872404 : && vrp_will_run_p (caller)
679 : 25950050 : && ipa_is_param_used_by_ipa_predicates (callee_pi, i))
680 : : {
681 : 11675795 : value_range vr (type);
682 : :
683 : 11675795 : ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type);
684 : 11675795 : if (!vr.undefined_p () && !vr.varying_p ())
685 : : {
686 : 8122208 : if (!avals->m_known_value_ranges.length ())
687 : : {
688 : 5865955 : avals->m_known_value_ranges.safe_grow_cleared (count,
689 : : true);
690 : 18728891 : for (int i = 0; i < count; ++i)
691 : 12862936 : avals->m_known_value_ranges[i].set_type (void_type_node);
692 : : }
693 : 8122208 : avals->m_known_value_ranges[i] = vr;
694 : : }
695 : 11675795 : }
696 : :
697 : : /* Determine known aggregate values. */
698 : 14235355 : if (fre_will_run_p (caller))
699 : 14232675 : ipa_push_agg_values_from_jfunc (caller_parms_info,
700 : : caller, &jf->agg, i,
701 : : &avals->m_known_aggs);
702 : : }
703 : :
704 : : /* For calls used in polymorphic calls we further determine
705 : : polymorphic call context. */
706 : 21022685 : if (compute_contexts
707 : 21022685 : && ipa_is_param_used_by_polymorphic_call (callee_pi, i))
708 : : {
709 : 214021 : ipa_polymorphic_call_context
710 : 214021 : ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
711 : 428042 : if (!ctx.useless_p ())
712 : : {
713 : 212137 : if (!avals->m_known_contexts.length ())
714 : 212000 : avals->m_known_contexts.safe_grow_cleared (count, true);
715 : 212137 : avals->m_known_contexts[i]
716 : 424274 : = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
717 : : }
718 : : }
719 : : }
720 : : else
721 : 698278 : gcc_assert (!count || callee->thunk);
722 : : }
723 : 12073329 : else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds)
724 : : {
725 : 9501223 : int i, count = (int)gimple_call_num_args (e->call_stmt);
726 : :
727 : 26905122 : for (i = 0; i < count; i++)
728 : : {
729 : 17403899 : tree cst = gimple_call_arg (e->call_stmt, i);
730 : 17403899 : if (!is_gimple_min_invariant (cst))
731 : : cst = NULL;
732 : 6350631 : if (cst)
733 : : {
734 : 6350631 : if (!avals->m_known_vals.length ())
735 : 4421525 : avals->m_known_vals.safe_grow_cleared (count, true);
736 : 6350631 : avals->m_known_vals[i] = cst;
737 : : }
738 : : }
739 : : }
740 : :
741 : 21910112 : evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr,
742 : : nonspec_clause_ptr, es);
743 : 21910112 : }
744 : :
745 : :
746 : : /* Allocate the function summary. */
747 : :
748 : : static void
749 : 457694 : ipa_fn_summary_alloc (void)
750 : : {
751 : 457694 : gcc_checking_assert (!ipa_fn_summaries);
752 : 457694 : ipa_size_summaries = new ipa_size_summary_t (symtab);
753 : 457694 : ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab);
754 : 457694 : ipa_call_summaries = new ipa_call_summary_t (symtab);
755 : 457694 : }
756 : :
757 : 26598623 : ipa_call_summary::~ipa_call_summary ()
758 : : {
759 : 26598623 : if (predicate)
760 : 3201148 : edge_predicate_pool.remove (predicate);
761 : :
762 : 26598623 : param.release ();
763 : 26598623 : }
764 : :
765 : 10024063 : ipa_fn_summary::~ipa_fn_summary ()
766 : : {
767 : 10024063 : unsigned len = vec_safe_length (loop_iterations);
768 : 10143685 : for (unsigned i = 0; i < len; i++)
769 : 119622 : edge_predicate_pool.remove ((*loop_iterations)[i].predicate);
770 : 10024063 : len = vec_safe_length (loop_strides);
771 : 10066978 : for (unsigned i = 0; i < len; i++)
772 : 42915 : edge_predicate_pool.remove ((*loop_strides)[i].predicate);
773 : 10024063 : vec_free (conds);
774 : 10024063 : call_size_time_table.release ();
775 : 10024063 : vec_free (loop_iterations);
776 : 10024063 : vec_free (loop_strides);
777 : 10024063 : builtin_constant_p_parms.release ();
778 : 10024063 : }
779 : :
780 : : void
781 : 7250745 : ipa_fn_summary_t::remove_callees (cgraph_node *node)
782 : : {
783 : 7250745 : cgraph_edge *e;
784 : 29522035 : for (e = node->callees; e; e = e->next_callee)
785 : 22271290 : ipa_call_summaries->remove (e);
786 : 7734130 : for (e = node->indirect_calls; e; e = e->next_callee)
787 : 483385 : ipa_call_summaries->remove (e);
788 : 7250745 : }
789 : :
790 : : /* Duplicate predicates in loop hint vector, allocating memory for them and
791 : : remove and deallocate any uninteresting (true or false) ones. Return the
792 : : result. */
793 : :
794 : : static vec<ipa_freqcounting_predicate, va_gc> *
795 : 25798 : remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v,
796 : : clause_t possible_truths)
797 : : {
798 : 25798 : if (vec_safe_length (v) == 0)
799 : : return NULL;
800 : :
801 : 3738 : vec<ipa_freqcounting_predicate, va_gc> *res = v->copy ();
802 : 3738 : int len = res->length();
803 : 9129 : for (int i = len - 1; i >= 0; i--)
804 : : {
805 : 5391 : ipa_predicate new_predicate
806 : 5391 : = (*res)[i].predicate->remap_after_duplication (possible_truths);
807 : : /* We do not want to free previous predicate; it is used by node
808 : : origin. */
809 : 5391 : (*res)[i].predicate = NULL;
810 : 5391 : set_hint_predicate (&(*res)[i].predicate, new_predicate);
811 : :
812 : 5391 : if (!(*res)[i].predicate)
813 : 2259 : res->unordered_remove (i);
814 : : }
815 : :
816 : : return res;
817 : : }
818 : :
819 : :
820 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
821 : : void
822 : 2685722 : ipa_fn_summary_t::duplicate (cgraph_node *src,
823 : : cgraph_node *dst,
824 : : ipa_fn_summary *src_info,
825 : : ipa_fn_summary *info)
826 : : {
827 : 2685722 : new (info) ipa_fn_summary (*src_info);
828 : : /* TODO: as an optimization, we may avoid copying conditions
829 : : that are known to be false or true. */
830 : 2685722 : info->conds = vec_safe_copy (info->conds);
831 : :
832 : 2685722 : clone_info *cinfo = clone_info::get (dst);
833 : : /* When there are any replacements in the function body, see if we can figure
834 : : out that something was optimized out. */
835 : 2685722 : if (ipa_node_params_sum && cinfo && cinfo->tree_map)
836 : : {
837 : : /* Use SRC parm info since it may not be copied yet. */
838 : 12899 : ipa_node_params *parms_info = ipa_node_params_sum->get (src);
839 : 12899 : ipa_auto_call_arg_values avals;
840 : 12899 : int count = ipa_get_param_count (parms_info);
841 : 12899 : int i, j;
842 : 12899 : clause_t possible_truths;
843 : 12899 : ipa_predicate true_pred = true;
844 : 12899 : size_time_entry *e;
845 : 12899 : int optimized_out_size = 0;
846 : 12899 : bool inlined_to_p = false;
847 : 12899 : struct cgraph_edge *edge, *next;
848 : :
849 : 12899 : info->size_time_table.release ();
850 : 12899 : avals.m_known_vals.safe_grow_cleared (count, true);
851 : 53037 : for (i = 0; i < count; i++)
852 : : {
853 : : struct ipa_replace_map *r;
854 : :
855 : 132039 : for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++)
856 : : {
857 : 73062 : if (r->parm_num == i)
858 : : {
859 : 21299 : avals.m_known_vals[i] = r->new_tree;
860 : 21299 : break;
861 : : }
862 : : }
863 : : }
864 : 12899 : evaluate_conditions_for_known_args (dst, false,
865 : : &avals,
866 : : &possible_truths,
867 : : /* We are going to specialize,
868 : : so ignore nonspec truths. */
869 : : NULL,
870 : : NULL);
871 : :
872 : 12899 : info->account_size_time (0, 0, true_pred, true_pred);
873 : :
874 : : /* Remap size_time vectors.
875 : : Simplify the predicate by pruning out alternatives that are known
876 : : to be false.
877 : : TODO: as on optimization, we can also eliminate conditions known
878 : : to be true. */
879 : 96117 : for (i = 0; src_info->size_time_table.iterate (i, &e); i++)
880 : : {
881 : 83218 : ipa_predicate new_exec_pred;
882 : 83218 : ipa_predicate new_nonconst_pred;
883 : 83218 : new_exec_pred = e->exec_predicate.remap_after_duplication
884 : 83218 : (possible_truths);
885 : 83218 : new_nonconst_pred = e->nonconst_predicate.remap_after_duplication
886 : 83218 : (possible_truths);
887 : 83218 : if (new_exec_pred == false || new_nonconst_pred == false)
888 : 9854 : optimized_out_size += e->size;
889 : : else
890 : 73364 : info->account_size_time (e->size, e->time, new_exec_pred,
891 : : new_nonconst_pred);
892 : : }
893 : :
894 : : /* Remap edge predicates with the same simplification as above.
895 : : Also copy constantness arrays. */
896 : 185764 : for (edge = dst->callees; edge; edge = next)
897 : : {
898 : 172865 : ipa_predicate new_predicate;
899 : 172865 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
900 : 172865 : next = edge->next_callee;
901 : :
902 : 172865 : if (!edge->inline_failed)
903 : 0 : inlined_to_p = true;
904 : 172865 : if (!es->predicate)
905 : 159476 : continue;
906 : 13389 : new_predicate = es->predicate->remap_after_duplication
907 : 13389 : (possible_truths);
908 : 13389 : if (new_predicate == false && *es->predicate != false)
909 : 2688 : optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale;
910 : 13389 : edge_set_predicate (edge, &new_predicate);
911 : : }
912 : :
913 : : /* Remap indirect edge predicates with the same simplification as above.
914 : : Also copy constantness arrays. */
915 : 13771 : for (edge = dst->indirect_calls; edge; edge = next)
916 : : {
917 : 872 : ipa_predicate new_predicate;
918 : 872 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
919 : 872 : next = edge->next_callee;
920 : :
921 : 872 : gcc_checking_assert (edge->inline_failed);
922 : 872 : if (!es->predicate)
923 : 744 : continue;
924 : 128 : new_predicate = es->predicate->remap_after_duplication
925 : 128 : (possible_truths);
926 : 128 : if (new_predicate == false && *es->predicate != false)
927 : 24 : optimized_out_size
928 : 24 : += es->call_stmt_size * ipa_fn_summary::size_scale;
929 : 128 : edge_set_predicate (edge, &new_predicate);
930 : : }
931 : 12899 : info->loop_iterations
932 : 12899 : = remap_freqcounting_preds_after_dup (info->loop_iterations,
933 : : possible_truths);
934 : 12899 : info->loop_strides
935 : 12899 : = remap_freqcounting_preds_after_dup (info->loop_strides,
936 : : possible_truths);
937 : 12899 : if (info->builtin_constant_p_parms.length())
938 : : {
939 : 25 : vec <int, va_heap, vl_ptr> parms = info->builtin_constant_p_parms;
940 : 25 : int ip;
941 : 25 : info->builtin_constant_p_parms = vNULL;
942 : 50 : for (i = 0; parms.iterate (i, &ip); i++)
943 : 25 : if (!avals.m_known_vals[ip])
944 : 4 : info->builtin_constant_p_parms.safe_push (ip);
945 : : }
946 : :
947 : : /* If inliner or someone after inliner will ever start producing
948 : : non-trivial clones, we will get trouble with lack of information
949 : : about updating self sizes, because size vectors already contains
950 : : sizes of the callees. */
951 : 12899 : gcc_assert (!inlined_to_p || !optimized_out_size);
952 : 12899 : }
953 : : else
954 : : {
955 : 2672823 : info->size_time_table = src_info->size_time_table.copy ();
956 : 2672823 : info->loop_iterations = vec_safe_copy (src_info->loop_iterations);
957 : 2672823 : info->loop_strides = vec_safe_copy (info->loop_strides);
958 : :
959 : 2672823 : info->builtin_constant_p_parms
960 : 2672823 : = info->builtin_constant_p_parms.copy ();
961 : :
962 : 2672823 : ipa_freqcounting_predicate *f;
963 : 2729789 : for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++)
964 : : {
965 : 56966 : ipa_predicate p = *f->predicate;
966 : 56966 : f->predicate = NULL;
967 : 56966 : set_hint_predicate (&f->predicate, p);
968 : : }
969 : 2706297 : for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++)
970 : : {
971 : 33474 : ipa_predicate p = *f->predicate;
972 : 33474 : f->predicate = NULL;
973 : 33474 : set_hint_predicate (&f->predicate, p);
974 : : }
975 : : }
976 : 2685722 : if (!dst->inlined_to)
977 : 130223 : ipa_update_overall_fn_summary (dst);
978 : 2685722 : }
979 : :
980 : :
981 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
982 : :
983 : : void
984 : 3478988 : ipa_call_summary_t::duplicate (struct cgraph_edge *src,
985 : : struct cgraph_edge *dst,
986 : : class ipa_call_summary *srcinfo,
987 : : class ipa_call_summary *info)
988 : : {
989 : 3478988 : new (info) ipa_call_summary (*srcinfo);
990 : 3478988 : info->predicate = NULL;
991 : 3478988 : edge_set_predicate (dst, srcinfo->predicate);
992 : 3478988 : info->param = srcinfo->param.copy ();
993 : 3478988 : if (!dst->indirect_unknown_callee && src->indirect_unknown_callee
994 : : /* Don't subtract the size when dealing with callback pairs, since the
995 : : edge has no real size. */
996 : 24841 : && !src->has_callback && !dst->callback)
997 : : {
998 : 24841 : info->call_stmt_size -= (eni_size_weights.indirect_call_cost
999 : 24841 : - eni_size_weights.call_cost);
1000 : 24841 : info->call_stmt_time -= (eni_time_weights.indirect_call_cost
1001 : 24841 : - eni_time_weights.call_cost);
1002 : : }
1003 : 3478988 : }
1004 : :
1005 : : /* Dump edge summaries associated to NODE and recursively to all clones.
1006 : : Indent by INDENT. */
1007 : :
1008 : : static void
1009 : 1961 : dump_ipa_call_summary (FILE *f, int indent, struct cgraph_node *node,
1010 : : class ipa_fn_summary *info)
1011 : : {
1012 : 1961 : struct cgraph_edge *edge;
1013 : 9771 : for (edge = node->callees; edge; edge = edge->next_callee)
1014 : : {
1015 : 7810 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1016 : 7810 : struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1017 : 7810 : int i;
1018 : :
1019 : 15620 : fprintf (f,
1020 : : "%*s%s %s\n%*s freq:%4.2f",
1021 : : indent, "", callee->dump_name (),
1022 : 7810 : !edge->inline_failed
1023 : 7257 : ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1024 : 7810 : indent, "", edge->sreal_frequency ().to_double ());
1025 : :
1026 : 7810 : if (cross_module_call_p (edge))
1027 : 4 : fprintf (f, " cross module");
1028 : :
1029 : 7810 : if (es)
1030 : 7257 : fprintf (f, " loop depth:%2i size:%2i time: %2i",
1031 : : es->loop_depth, es->call_stmt_size, es->call_stmt_time);
1032 : :
1033 : 7810 : ipa_fn_summary *s = ipa_fn_summaries->get (callee);
1034 : 7810 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1035 : 7810 : if (s != NULL)
1036 : 612 : fprintf (f, " callee size:%2i stack:%2i",
1037 : 612 : (int) (ss->size / ipa_fn_summary::size_scale),
1038 : 612 : (int) s->estimated_stack_size);
1039 : :
1040 : 7810 : if (es && es->predicate)
1041 : : {
1042 : 4627 : fprintf (f, " predicate: ");
1043 : 4627 : es->predicate->dump (f, info->conds);
1044 : : }
1045 : : else
1046 : 3183 : fprintf (f, "\n");
1047 : 7810 : if (es && es->param.exists ())
1048 : 5340 : for (i = 0; i < (int) es->param.length (); i++)
1049 : : {
1050 : 1580 : int prob = es->param[i].change_prob;
1051 : :
1052 : 1580 : if (!prob)
1053 : 612 : fprintf (f, "%*s op%i is compile time invariant\n",
1054 : : indent + 2, "", i);
1055 : 968 : else if (prob != REG_BR_PROB_BASE)
1056 : 35 : fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1057 : 35 : prob * 100.0 / REG_BR_PROB_BASE);
1058 : 1580 : if (es->param[i].points_to_local_or_readonly_memory)
1059 : 400 : fprintf (f, "%*s op%i points to local or readonly memory\n",
1060 : : indent + 2, "", i);
1061 : 1580 : if (es->param[i].points_to_possible_sra_candidate)
1062 : 227 : fprintf (f, "%*s op%i points to possible sra candidate\n",
1063 : : indent + 2, "", i);
1064 : : }
1065 : 7810 : if (!edge->inline_failed)
1066 : : {
1067 : 553 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1068 : 553 : fprintf (f, "%*sStack frame offset %i, callee self size %i\n",
1069 : : indent + 2, "",
1070 : 553 : (int) ipa_get_stack_frame_offset (callee),
1071 : 553 : (int) ss->estimated_self_stack_size);
1072 : 553 : dump_ipa_call_summary (f, indent + 2, callee, info);
1073 : : }
1074 : : }
1075 : 2259 : for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1076 : : {
1077 : 298 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1078 : 298 : fprintf (f, "%*sindirect call loop depth:%2i freq:%4.2f size:%2i"
1079 : : " time: %2i",
1080 : : indent, "",
1081 : : es->loop_depth,
1082 : 298 : edge->sreal_frequency ().to_double (), es->call_stmt_size,
1083 : : es->call_stmt_time);
1084 : 298 : if (es->predicate)
1085 : : {
1086 : 6 : fprintf (f, "predicate: ");
1087 : 6 : es->predicate->dump (f, info->conds);
1088 : : }
1089 : : else
1090 : 292 : fprintf (f, "\n");
1091 : : }
1092 : 1961 : }
1093 : :
1094 : :
1095 : : void
1096 : 1614 : ipa_dump_fn_summary (FILE *f, struct cgraph_node *node)
1097 : : {
1098 : 1614 : if (node->definition)
1099 : : {
1100 : 1614 : class ipa_fn_summary *s = ipa_fn_summaries->get (node);
1101 : 1614 : class ipa_size_summary *ss = ipa_size_summaries->get (node);
1102 : 1614 : if (s != NULL)
1103 : : {
1104 : 1408 : size_time_entry *e;
1105 : 1408 : int i;
1106 : 1408 : fprintf (f, "IPA function summary for %s", node->dump_name ());
1107 : 1408 : if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1108 : 0 : fprintf (f, " always_inline");
1109 : 1408 : if (s->inlinable)
1110 : 1239 : fprintf (f, " inlinable");
1111 : 1408 : if (s->fp_expressions)
1112 : 44 : fprintf (f, " fp_expression");
1113 : 1408 : if (s->builtin_constant_p_parms.length ())
1114 : : {
1115 : 2 : fprintf (f, " builtin_constant_p_parms");
1116 : 4 : for (unsigned int i = 0;
1117 : 4 : i < s->builtin_constant_p_parms.length (); i++)
1118 : 2 : fprintf (f, " %i", s->builtin_constant_p_parms[i]);
1119 : : }
1120 : 1408 : fprintf (f, "\n global time: %f\n", s->time.to_double ());
1121 : 1408 : fprintf (f, " self size: %i\n", ss->self_size);
1122 : 1408 : fprintf (f, " global size: %i\n", ss->size);
1123 : 1408 : fprintf (f, " min size: %i\n", s->min_size);
1124 : 1408 : fprintf (f, " self stack: %i\n",
1125 : 1408 : (int) ss->estimated_self_stack_size);
1126 : 1408 : fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1127 : 1408 : if (s->growth)
1128 : 165 : fprintf (f, " estimated growth:%i\n", (int) s->growth);
1129 : 1408 : if (s->scc_no)
1130 : 5 : fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1131 : 5890 : for (i = 0; s->size_time_table.iterate (i, &e); i++)
1132 : : {
1133 : 4482 : fprintf (f, " size:%f, time:%f",
1134 : 4482 : (double) e->size / ipa_fn_summary::size_scale,
1135 : : e->time.to_double ());
1136 : 4482 : if (e->exec_predicate != true)
1137 : : {
1138 : 2619 : fprintf (f, ", executed if:");
1139 : 2619 : e->exec_predicate.dump (f, s->conds, 0);
1140 : : }
1141 : 4482 : if (e->exec_predicate != e->nonconst_predicate)
1142 : : {
1143 : 1169 : fprintf (f, ", nonconst if:");
1144 : 1169 : e->nonconst_predicate.dump (f, s->conds, 0);
1145 : : }
1146 : 4482 : fprintf (f, "\n");
1147 : : }
1148 : : ipa_freqcounting_predicate *fcp;
1149 : : bool first_fcp = true;
1150 : 1514 : for (int i = 0; vec_safe_iterate (s->loop_iterations, i, &fcp); i++)
1151 : : {
1152 : 106 : if (first_fcp)
1153 : : {
1154 : 78 : fprintf (f, " loop iterations:");
1155 : 78 : first_fcp = false;
1156 : : }
1157 : 106 : fprintf (f, " %3.2f for ", fcp->freq.to_double ());
1158 : 106 : fcp->predicate->dump (f, s->conds);
1159 : : }
1160 : : first_fcp = true;
1161 : 1426 : for (int i = 0; vec_safe_iterate (s->loop_strides, i, &fcp); i++)
1162 : : {
1163 : 18 : if (first_fcp)
1164 : : {
1165 : 10 : fprintf (f, " loop strides:");
1166 : 10 : first_fcp = false;
1167 : : }
1168 : 18 : fprintf (f, " %3.2f for :", fcp->freq.to_double ());
1169 : 18 : fcp->predicate->dump (f, s->conds);
1170 : : }
1171 : 1408 : fprintf (f, " calls:\n");
1172 : 1408 : dump_ipa_call_summary (f, 4, node, s);
1173 : 1408 : fprintf (f, "\n");
1174 : 1408 : if (s->target_info)
1175 : 0 : fprintf (f, " target_info: %x\n", s->target_info);
1176 : : }
1177 : : else
1178 : 206 : fprintf (f, "IPA summary for %s is missing.\n", node->dump_name ());
1179 : : }
1180 : 1614 : }
1181 : :
1182 : : DEBUG_FUNCTION void
1183 : 0 : ipa_debug_fn_summary (struct cgraph_node *node)
1184 : : {
1185 : 0 : ipa_dump_fn_summary (stderr, node);
1186 : 0 : }
1187 : :
1188 : : void
1189 : 356 : ipa_dump_fn_summaries (FILE *f)
1190 : : {
1191 : 356 : struct cgraph_node *node;
1192 : :
1193 : 2294 : FOR_EACH_DEFINED_FUNCTION (node)
1194 : 1938 : if (!node->inlined_to)
1195 : 1385 : ipa_dump_fn_summary (f, node);
1196 : 356 : }
1197 : :
1198 : : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1199 : : boolean variable pointed to by DATA. */
1200 : :
1201 : : static bool
1202 : 941330 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1203 : : void *data)
1204 : : {
1205 : 941330 : bool *b = (bool *) data;
1206 : 941330 : *b = true;
1207 : 941330 : return true;
1208 : : }
1209 : :
1210 : : /* If OP refers to value of function parameter, return the corresponding
1211 : : parameter. If non-NULL, the size of the memory load (or the SSA_NAME of the
1212 : : PARM_DECL) will be stored to *SIZE_P in that case too. */
1213 : :
1214 : : static tree
1215 : 183516024 : unmodified_parm_1 (ipa_func_body_info *fbi, gimple *stmt, tree op,
1216 : : poly_int64 *size_p)
1217 : : {
1218 : : /* SSA_NAME referring to parm default def? */
1219 : 183516024 : if (TREE_CODE (op) == SSA_NAME
1220 : 107216250 : && SSA_NAME_IS_DEFAULT_DEF (op)
1221 : 208936515 : && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1222 : : {
1223 : 25224630 : if (size_p)
1224 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1225 : 25224630 : return SSA_NAME_VAR (op);
1226 : : }
1227 : : /* Non-SSA parm reference? */
1228 : 158291394 : if (TREE_CODE (op) == PARM_DECL
1229 : 1476216 : && fbi->aa_walk_budget > 0)
1230 : : {
1231 : 1473670 : bool modified = false;
1232 : :
1233 : 1473670 : ao_ref refd;
1234 : 1473670 : ao_ref_init (&refd, op);
1235 : 2947340 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
1236 : : mark_modified, &modified, NULL, NULL,
1237 : : fbi->aa_walk_budget);
1238 : 1473670 : if (walked < 0)
1239 : : {
1240 : 8 : fbi->aa_walk_budget = 0;
1241 : 935359 : return NULL_TREE;
1242 : : }
1243 : 1473662 : fbi->aa_walk_budget -= walked;
1244 : 1473662 : if (!modified)
1245 : : {
1246 : 935351 : if (size_p)
1247 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1248 : 935351 : return op;
1249 : : }
1250 : : }
1251 : : return NULL_TREE;
1252 : : }
1253 : :
1254 : : /* If OP refers to value of function parameter, return the corresponding
1255 : : parameter. Also traverse chains of SSA register assignments. If non-NULL,
1256 : : the size of the memory load (or the SSA_NAME of the PARM_DECL) will be
1257 : : stored to *SIZE_P in that case too. */
1258 : :
1259 : : static tree
1260 : 119797867 : unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op,
1261 : : poly_int64 *size_p)
1262 : : {
1263 : 149024822 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1264 : 149024822 : if (res)
1265 : : return res;
1266 : :
1267 : 123652255 : if (TREE_CODE (op) == SSA_NAME
1268 : 70026791 : && !SSA_NAME_IS_DEFAULT_DEF (op)
1269 : 193487623 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1270 : 29226955 : return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op),
1271 : 29226955 : gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)),
1272 : 29226955 : size_p);
1273 : : return NULL_TREE;
1274 : : }
1275 : :
1276 : : /* If OP refers to a value of a function parameter or value loaded from an
1277 : : aggregate passed to a parameter (either by value or reference), return TRUE
1278 : : and store the number of the parameter to *INDEX_P, the access size into
1279 : : *SIZE_P, and information whether and how it has been loaded from an
1280 : : aggregate into *AGGPOS. INFO describes the function parameters, STMT is the
1281 : : statement in which OP is used or loaded. */
1282 : :
1283 : : static bool
1284 : 32976270 : unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi,
1285 : : gimple *stmt, tree op, int *index_p,
1286 : : poly_int64 *size_p,
1287 : : struct agg_position_info *aggpos)
1288 : : {
1289 : 34491202 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1290 : :
1291 : 34491202 : gcc_checking_assert (aggpos);
1292 : 34491202 : if (res)
1293 : : {
1294 : 787414 : *index_p = ipa_get_param_decl_index (fbi->info, res);
1295 : 787414 : if (*index_p < 0)
1296 : : return false;
1297 : 787285 : aggpos->agg_contents = false;
1298 : 787285 : aggpos->by_ref = false;
1299 : 787285 : return true;
1300 : : }
1301 : :
1302 : 33703788 : if (TREE_CODE (op) == SSA_NAME)
1303 : : {
1304 : 11964829 : if (SSA_NAME_IS_DEFAULT_DEF (op)
1305 : 11964829 : || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1306 : : return false;
1307 : 4876585 : stmt = SSA_NAME_DEF_STMT (op);
1308 : 4876585 : op = gimple_assign_rhs1 (stmt);
1309 : 4876585 : if (!REFERENCE_CLASS_P (op))
1310 : : return unmodified_parm_or_parm_agg_item (fbi, stmt, op, index_p, size_p,
1311 : : aggpos);
1312 : : }
1313 : :
1314 : 25100612 : aggpos->agg_contents = true;
1315 : 25100612 : return ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
1316 : : stmt, op, index_p, &aggpos->offset,
1317 : 25100612 : size_p, &aggpos->by_ref);
1318 : : }
1319 : :
1320 : : /* If stmt is simple load or store of value pointed to by a function parmaeter,
1321 : : return its index. */
1322 : :
1323 : : static int
1324 : 111226839 : load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt)
1325 : : {
1326 : 111226839 : if (!optimize)
1327 : : return -1;
1328 : 159415269 : gassign *assign = dyn_cast <gassign *> (stmt);
1329 : 55494469 : if (!assign)
1330 : : return -1;
1331 : 55494469 : tree param;
1332 : 55494469 : if (gimple_assign_load_p (stmt))
1333 : 20486661 : param = gimple_assign_rhs1 (stmt);
1334 : 35007808 : else if (gimple_store_p (stmt))
1335 : 18689679 : param = gimple_assign_lhs (stmt);
1336 : : else
1337 : : return -1;
1338 : 39176340 : tree base = get_base_address (param);
1339 : 39176340 : if (TREE_CODE (base) != MEM_REF
1340 : 13587736 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1341 : 52760014 : || !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1342 : : return -1;
1343 : 7391996 : tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1344 : 7391996 : if (TREE_CODE (p) != PARM_DECL)
1345 : : return -1;
1346 : 7306039 : return ipa_get_param_decl_index (fbi->info, p);
1347 : : }
1348 : :
1349 : : /* See if statement might disappear after inlining.
1350 : : 0 - means not eliminated
1351 : : 1 - half of statements goes away
1352 : : 2 - for sure it is eliminated.
1353 : : We are not terribly sophisticated, basically looking for simple abstraction
1354 : : penalty wrappers. */
1355 : :
1356 : : static int
1357 : 111226839 : eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt)
1358 : : {
1359 : 111226839 : enum gimple_code code = gimple_code (stmt);
1360 : 111226839 : enum tree_code rhs_code;
1361 : :
1362 : 111226839 : if (!optimize)
1363 : : return 0;
1364 : :
1365 : 93212301 : switch (code)
1366 : : {
1367 : : case GIMPLE_RETURN:
1368 : : return 2;
1369 : 55494469 : case GIMPLE_ASSIGN:
1370 : 55494469 : if (gimple_num_ops (stmt) != 2)
1371 : : return 0;
1372 : :
1373 : 39984098 : rhs_code = gimple_assign_rhs_code (stmt);
1374 : :
1375 : : /* Casts of parameters, loads from parameters passed by reference
1376 : : and stores to return value or parameters are often free after
1377 : : inlining due to SRA and further combining.
1378 : : Assume that half of statements goes away. */
1379 : 39984098 : if (CONVERT_EXPR_CODE_P (rhs_code)
1380 : : || rhs_code == VIEW_CONVERT_EXPR
1381 : : || rhs_code == ADDR_EXPR
1382 : 37187281 : || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1383 : : {
1384 : 39176340 : tree rhs = gimple_assign_rhs1 (stmt);
1385 : 39176340 : tree lhs = gimple_assign_lhs (stmt);
1386 : 39176340 : tree inner_rhs = get_base_address (rhs);
1387 : 39176340 : tree inner_lhs = get_base_address (lhs);
1388 : 39176340 : bool rhs_free = false;
1389 : 39176340 : bool lhs_free = false;
1390 : :
1391 : 39176340 : if (!inner_rhs)
1392 : 0 : inner_rhs = rhs;
1393 : 39176340 : if (!inner_lhs)
1394 : 0 : inner_lhs = lhs;
1395 : :
1396 : : /* Reads of parameter are expected to be free. */
1397 : 39176340 : if (unmodified_parm (fbi, stmt, inner_rhs, NULL))
1398 : : rhs_free = true;
1399 : : /* Match expressions of form &this->field. Those will most likely
1400 : : combine with something upstream after inlining. */
1401 : 37884051 : else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1402 : : {
1403 : 2498696 : tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1404 : 2498696 : if (TREE_CODE (op) == PARM_DECL)
1405 : : rhs_free = true;
1406 : 2463796 : else if (TREE_CODE (op) == MEM_REF
1407 : 2463796 : && unmodified_parm (fbi, stmt, TREE_OPERAND (op, 0),
1408 : : NULL))
1409 : : rhs_free = true;
1410 : : }
1411 : :
1412 : : /* When parameter is not SSA register because its address is taken
1413 : : and it is just copied into one, the statement will be completely
1414 : : free after inlining (we will copy propagate backward). */
1415 : 1327189 : if (rhs_free && is_gimple_reg (lhs))
1416 : : return 2;
1417 : :
1418 : : /* Reads of parameters passed by reference
1419 : : expected to be free (i.e. optimized out after inlining). */
1420 : 38490196 : if (TREE_CODE (inner_rhs) == MEM_REF
1421 : 38490196 : && unmodified_parm (fbi, stmt, TREE_OPERAND (inner_rhs, 0), NULL))
1422 : : rhs_free = true;
1423 : :
1424 : : /* Copying parameter passed by reference into gimple register is
1425 : : probably also going to copy propagate, but we can't be quite
1426 : : sure. */
1427 : 38490196 : if (rhs_free && is_gimple_reg (lhs))
1428 : : lhs_free = true;
1429 : :
1430 : : /* Writes to parameters, parameters passed by value and return value
1431 : : (either directly or passed via invisible reference) are free.
1432 : :
1433 : : TODO: We ought to handle testcase like
1434 : : struct a {int a,b;};
1435 : : struct a
1436 : : returnstruct (void)
1437 : : {
1438 : : struct a a ={1,2};
1439 : : return a;
1440 : : }
1441 : :
1442 : : This translate into:
1443 : :
1444 : : returnstruct ()
1445 : : {
1446 : : int a$b;
1447 : : int a$a;
1448 : : struct a a;
1449 : : struct a D.2739;
1450 : :
1451 : : <bb 2>:
1452 : : D.2739.a = 1;
1453 : : D.2739.b = 2;
1454 : : return D.2739;
1455 : :
1456 : : }
1457 : : For that we either need to copy ipa-split logic detecting writes
1458 : : to return value. */
1459 : 38490196 : if (TREE_CODE (inner_lhs) == PARM_DECL
1460 : 38399315 : || TREE_CODE (inner_lhs) == RESULT_DECL
1461 : 76035839 : || (TREE_CODE (inner_lhs) == MEM_REF
1462 : 5031963 : && (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0),
1463 : : NULL)
1464 : 2551438 : || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1465 : 2550251 : && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1466 : 368673 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1467 : : (inner_lhs,
1468 : : 0))) == RESULT_DECL))))
1469 : : lhs_free = true;
1470 : 34982206 : if (lhs_free
1471 : 38490196 : && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1472 : : rhs_free = true;
1473 : 38490196 : if (lhs_free && rhs_free)
1474 : : return 1;
1475 : : }
1476 : : return 0;
1477 : : default:
1478 : : return 0;
1479 : : }
1480 : : }
1481 : :
1482 : : /* Analyze EXPR if it represents a series of simple operations performed on
1483 : : a function parameter and return true if so. FBI, STMT, EXPR, INDEX_P and
1484 : : AGGPOS have the same meaning like in unmodified_parm_or_parm_agg_item.
1485 : : Type of the parameter or load from an aggregate via the parameter is
1486 : : stored in *TYPE_P. Operations on the parameter are recorded to
1487 : : PARAM_OPS_P if it is not NULL. */
1488 : :
1489 : : static bool
1490 : 27146349 : decompose_param_expr (struct ipa_func_body_info *fbi,
1491 : : gimple *stmt, tree expr,
1492 : : int *index_p, tree *type_p,
1493 : : struct agg_position_info *aggpos,
1494 : : expr_eval_ops *param_ops_p = NULL)
1495 : : {
1496 : 27146349 : int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops);
1497 : 27146349 : int op_count = 0;
1498 : :
1499 : 27146349 : if (param_ops_p)
1500 : 9241644 : *param_ops_p = NULL;
1501 : :
1502 : 32976270 : while (true)
1503 : : {
1504 : 32976270 : expr_eval_op eval_op;
1505 : 32976270 : unsigned rhs_count;
1506 : 32976270 : unsigned cst_count = 0;
1507 : :
1508 : 32976270 : if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL,
1509 : : aggpos))
1510 : : {
1511 : 4630130 : tree type = TREE_TYPE (expr);
1512 : :
1513 : 4630130 : if (aggpos->agg_contents)
1514 : : {
1515 : : /* Stop if containing bit-field. */
1516 : 3842845 : if (TREE_CODE (expr) == BIT_FIELD_REF
1517 : 3842845 : || contains_bitfld_component_ref_p (expr))
1518 : : break;
1519 : : }
1520 : :
1521 : 4591254 : *type_p = type;
1522 : 4591254 : return true;
1523 : : }
1524 : :
1525 : 28346140 : if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr))
1526 : : break;
1527 : 10579367 : stmt = SSA_NAME_DEF_STMT (expr);
1528 : :
1529 : 10579367 : if (gcall *call = dyn_cast <gcall *> (stmt))
1530 : : {
1531 : 2465478 : int flags = gimple_call_return_flags (call);
1532 : 2465478 : if (!(flags & ERF_RETURNS_ARG))
1533 : 3056864 : goto fail;
1534 : 194467 : int arg = flags & ERF_RETURN_ARG_MASK;
1535 : 194467 : if (arg >= (int)gimple_call_num_args (call))
1536 : 0 : goto fail;
1537 : 194467 : expr = gimple_call_arg (stmt, arg);
1538 : 4054340 : continue;
1539 : 194467 : }
1540 : :
1541 : 8113889 : if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr)))
1542 : : break;
1543 : :
1544 : 6421424 : switch (gimple_assign_rhs_class (stmt))
1545 : : {
1546 : 3859873 : case GIMPLE_SINGLE_RHS:
1547 : 3859873 : expr = gimple_assign_rhs1 (stmt);
1548 : 3859873 : continue;
1549 : :
1550 : : case GIMPLE_UNARY_RHS:
1551 : : rhs_count = 1;
1552 : : break;
1553 : :
1554 : 1549786 : case GIMPLE_BINARY_RHS:
1555 : 1549786 : rhs_count = 2;
1556 : 1549786 : break;
1557 : :
1558 : 642 : case GIMPLE_TERNARY_RHS:
1559 : 642 : rhs_count = 3;
1560 : 642 : break;
1561 : :
1562 : 0 : default:
1563 : 0 : goto fail;
1564 : : }
1565 : :
1566 : : /* Stop if expression is too complex. */
1567 : 2561551 : if (op_count++ == op_limit)
1568 : : break;
1569 : :
1570 : 2561434 : if (param_ops_p)
1571 : : {
1572 : 2559900 : eval_op.code = gimple_assign_rhs_code (stmt);
1573 : 2559900 : eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt));
1574 : 2559900 : eval_op.val[0] = NULL_TREE;
1575 : 2559900 : eval_op.val[1] = NULL_TREE;
1576 : : }
1577 : :
1578 : : expr = NULL_TREE;
1579 : 5887415 : for (unsigned i = 0; i < rhs_count; i++)
1580 : : {
1581 : 4111834 : tree op = gimple_op (stmt, i + 1);
1582 : :
1583 : 4111834 : gcc_assert (op && !TYPE_P (op));
1584 : 4111834 : if (is_gimple_ip_invariant (op))
1585 : : {
1586 : 766731 : if (++cst_count == rhs_count)
1587 : 1092 : goto fail;
1588 : :
1589 : 765639 : eval_op.val[cst_count - 1] = op;
1590 : : }
1591 : 3345103 : else if (!expr)
1592 : : {
1593 : : /* Found a non-constant operand, and record its index in rhs
1594 : : operands. */
1595 : 2560342 : eval_op.index = i;
1596 : 2560342 : expr = op;
1597 : : }
1598 : : else
1599 : : {
1600 : : /* Found more than one non-constant operands. */
1601 : 784761 : goto fail;
1602 : : }
1603 : : }
1604 : :
1605 : 1775581 : if (param_ops_p)
1606 : 1774317 : vec_safe_insert (*param_ops_p, 0, eval_op);
1607 : : }
1608 : :
1609 : : /* Failed to decompose, free resource and return. */
1610 : 22555095 : fail:
1611 : 22555095 : if (param_ops_p)
1612 : 9108789 : vec_free (*param_ops_p);
1613 : :
1614 : : return false;
1615 : : }
1616 : :
1617 : : /* Record to SUMMARY that PARM is used by builtin_constant_p. */
1618 : :
1619 : : static void
1620 : 11370 : add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm)
1621 : : {
1622 : 11370 : int ip;
1623 : :
1624 : : /* Avoid duplicates. */
1625 : 11382 : for (unsigned int i = 0;
1626 : 11382 : summary->builtin_constant_p_parms.iterate (i, &ip); i++)
1627 : 5527 : if (ip == parm)
1628 : 11370 : return;
1629 : 5855 : summary->builtin_constant_p_parms.safe_push (parm);
1630 : : }
1631 : :
1632 : : /* If BB ends by a conditional we can turn into predicates, attach corresponding
1633 : : predicates to the CFG edges. */
1634 : :
1635 : : static void
1636 : 35339275 : set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1637 : : class ipa_fn_summary *summary,
1638 : : class ipa_node_params *params_summary,
1639 : : basic_block bb)
1640 : : {
1641 : 35339275 : tree op, op2;
1642 : 35339275 : int index;
1643 : 35339275 : struct agg_position_info aggpos;
1644 : 35339275 : enum tree_code code, inverted_code;
1645 : 35339275 : edge e;
1646 : 35339275 : edge_iterator ei;
1647 : 35339275 : gimple *set_stmt;
1648 : 35339275 : tree param_type;
1649 : 35339275 : expr_eval_ops param_ops;
1650 : :
1651 : 70678550 : gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1652 : 11564898 : if (!last)
1653 : 35327905 : return;
1654 : 11564898 : if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1655 : : return;
1656 : 9154690 : op = gimple_cond_lhs (last);
1657 : :
1658 : 9154690 : if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1659 : : ¶m_ops))
1660 : : {
1661 : 1265945 : code = gimple_cond_code (last);
1662 : 1265945 : inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1663 : :
1664 : 3797835 : FOR_EACH_EDGE (e, ei, bb->succs)
1665 : : {
1666 : 1265945 : enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1667 : 2531890 : ? code : inverted_code);
1668 : : /* invert_tree_comparison will return ERROR_MARK on FP
1669 : : comparisons that are not EQ/NE instead of returning proper
1670 : : unordered one. Be sure it is not confused with NON_CONSTANT.
1671 : :
1672 : : And if the edge's target is the final block of diamond CFG graph
1673 : : of this conditional statement, we do not need to compute
1674 : : predicate for the edge because the final block's predicate must
1675 : : be at least as that of the first block of the statement. */
1676 : 2531890 : if (this_code != ERROR_MARK
1677 : 2531890 : && !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1678 : : {
1679 : 2135468 : ipa_predicate p
1680 : 2135468 : = add_condition (summary, params_summary, index,
1681 : : param_type, &aggpos,
1682 : : this_code, gimple_cond_rhs (last), param_ops);
1683 : 2135468 : e->aux = edge_predicate_pool.allocate ();
1684 : 2135468 : *(ipa_predicate *) e->aux = p;
1685 : : }
1686 : : }
1687 : 1265945 : vec_free (param_ops);
1688 : 1265945 : return;
1689 : : }
1690 : :
1691 : 7888745 : if (TREE_CODE (op) != SSA_NAME)
1692 : : return;
1693 : : /* Special case
1694 : : if (builtin_constant_p (op))
1695 : : constant_code
1696 : : else
1697 : : nonconstant_code.
1698 : : Here we can predicate nonconstant_code. We can't
1699 : : really handle constant_code since we have no predicate
1700 : : for this and also the constant code is not known to be
1701 : : optimized away when inliner doesn't see operand is constant.
1702 : : Other optimizers might think otherwise. */
1703 : 7887622 : if (gimple_cond_code (last) != NE_EXPR
1704 : 7887622 : || !integer_zerop (gimple_cond_rhs (last)))
1705 : 4466493 : return;
1706 : 3421129 : set_stmt = SSA_NAME_DEF_STMT (op);
1707 : 3421129 : if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1708 : 3421129 : || gimple_call_num_args (set_stmt) != 1)
1709 : : return;
1710 : 12528 : op2 = gimple_call_arg (set_stmt, 0);
1711 : 12528 : if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos))
1712 : : return;
1713 : 11370 : if (!aggpos.by_ref)
1714 : 11337 : add_builtin_constant_p_parm (summary, index);
1715 : 34110 : FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1716 : : {
1717 : 11370 : ipa_predicate p = add_condition (summary, params_summary, index,
1718 : : param_type, &aggpos,
1719 : : ipa_predicate::is_not_constant, NULL_TREE);
1720 : 11370 : e->aux = edge_predicate_pool.allocate ();
1721 : 11370 : *(ipa_predicate *) e->aux = p;
1722 : : }
1723 : : }
1724 : :
1725 : :
1726 : : /* If BB ends by a switch we can turn into predicates, attach corresponding
1727 : : predicates to the CFG edges. */
1728 : :
1729 : : static void
1730 : 35339275 : set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1731 : : class ipa_fn_summary *summary,
1732 : : class ipa_node_params *params_summary,
1733 : : basic_block bb)
1734 : : {
1735 : 35339275 : tree op;
1736 : 35339275 : int index;
1737 : 35339275 : struct agg_position_info aggpos;
1738 : 35339275 : edge e;
1739 : 35339275 : edge_iterator ei;
1740 : 35339275 : size_t n;
1741 : 35339275 : size_t case_idx;
1742 : 35339275 : tree param_type;
1743 : 35339275 : expr_eval_ops param_ops;
1744 : :
1745 : 70678550 : gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
1746 : 86954 : if (!last)
1747 : 35304149 : return;
1748 : 86954 : op = gimple_switch_index (last);
1749 : 86954 : if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1750 : : ¶m_ops))
1751 : : return;
1752 : :
1753 : 50961 : auto_vec<std::pair<tree, tree> > ranges;
1754 : 50961 : tree type = TREE_TYPE (op);
1755 : 50961 : int bound_limit = opt_for_fn (fbi->node->decl,
1756 : : param_ipa_max_switch_predicate_bounds);
1757 : 50961 : int bound_count = 0;
1758 : : // This can safely be an integer range, as switches can only hold
1759 : : // integers.
1760 : 50961 : int_range<2> vr;
1761 : :
1762 : 101922 : get_range_query (cfun)->range_of_expr (vr, op);
1763 : 50961 : if (vr.undefined_p ())
1764 : 0 : vr.set_varying (TREE_TYPE (op));
1765 : 50961 : tree vr_min, vr_max;
1766 : : // TODO: This entire function could use a rewrite to use the irange
1767 : : // API, instead of trying to recreate its intersection/union logic.
1768 : : // Any use of get_legacy_range() is a serious code smell.
1769 : 50961 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
1770 : 50961 : wide_int vr_wmin = wi::to_wide (vr_min);
1771 : 50961 : wide_int vr_wmax = wi::to_wide (vr_max);
1772 : :
1773 : 358738 : FOR_EACH_EDGE (e, ei, bb->succs)
1774 : : {
1775 : 307777 : e->aux = edge_predicate_pool.allocate ();
1776 : 307777 : *(ipa_predicate *) e->aux = false;
1777 : : }
1778 : :
1779 : 50961 : e = gimple_switch_edge (cfun, last, 0);
1780 : : /* Set BOUND_COUNT to maximum count to bypass computing predicate for
1781 : : default case if its target basic block is in convergence point of all
1782 : : switch cases, which can be determined by checking whether it
1783 : : post-dominates the switch statement. */
1784 : 50961 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1785 : 12413 : bound_count = INT_MAX;
1786 : :
1787 : 50961 : n = gimple_switch_num_labels (last);
1788 : 341237 : for (case_idx = 1; case_idx < n; ++case_idx)
1789 : : {
1790 : 290276 : tree cl = gimple_switch_label (last, case_idx);
1791 : 290276 : tree min = CASE_LOW (cl);
1792 : 290276 : tree max = CASE_HIGH (cl);
1793 : 290276 : ipa_predicate p;
1794 : :
1795 : 290276 : e = gimple_switch_edge (cfun, last, case_idx);
1796 : :
1797 : : /* The case value might not have same type as switch expression,
1798 : : extend the value based on the expression type. */
1799 : 290276 : if (TREE_TYPE (min) != type)
1800 : 55928 : min = wide_int_to_tree (type, wi::to_wide (min));
1801 : :
1802 : 290276 : if (!max)
1803 : : max = min;
1804 : 17656 : else if (TREE_TYPE (max) != type)
1805 : 7880 : max = wide_int_to_tree (type, wi::to_wide (max));
1806 : :
1807 : : /* The case's target basic block is in convergence point of all switch
1808 : : cases, its predicate should be at least as that of the switch
1809 : : statement. */
1810 : 290276 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1811 : 4807 : p = true;
1812 : 285469 : else if (min == max)
1813 : 269223 : p = add_condition (summary, params_summary, index, param_type,
1814 : : &aggpos, EQ_EXPR, min, param_ops);
1815 : : else
1816 : : {
1817 : 16246 : ipa_predicate p1, p2;
1818 : 16246 : p1 = add_condition (summary, params_summary, index, param_type,
1819 : : &aggpos, GE_EXPR, min, param_ops);
1820 : 16246 : p2 = add_condition (summary, params_summary,index, param_type,
1821 : : &aggpos, LE_EXPR, max, param_ops);
1822 : 16246 : p = p1 & p2;
1823 : : }
1824 : 290276 : *(ipa_predicate *) e->aux
1825 : 290276 : = p.or_with (summary->conds, *(ipa_predicate *) e->aux);
1826 : :
1827 : : /* If there are too many disjoint case ranges, predicate for default
1828 : : case might become too complicated. So add a limit here. */
1829 : 290276 : if (bound_count > bound_limit)
1830 : 96514 : continue;
1831 : :
1832 : 193762 : bool new_range = true;
1833 : :
1834 : 193762 : if (!ranges.is_empty ())
1835 : : {
1836 : 155214 : wide_int curr_wmin = wi::to_wide (min);
1837 : 155214 : wide_int last_wmax = wi::to_wide (ranges.last ().second);
1838 : :
1839 : : /* Merge case ranges if they are continuous. */
1840 : 155214 : if (curr_wmin == last_wmax + 1)
1841 : : new_range = false;
1842 : 53507 : else if (vr_type == VR_ANTI_RANGE)
1843 : : {
1844 : : /* If two disjoint case ranges can be connected by anti-range
1845 : : of switch index, combine them to one range. */
1846 : 79 : if (wi::lt_p (vr_wmax, curr_wmin - 1, TYPE_SIGN (type)))
1847 : : vr_type = VR_UNDEFINED;
1848 : 0 : else if (wi::le_p (vr_wmin, last_wmax + 1, TYPE_SIGN (type)))
1849 : 0 : new_range = false;
1850 : : }
1851 : 155214 : }
1852 : :
1853 : : /* Create/extend a case range. And we count endpoints of range set,
1854 : : this number nearly equals to number of conditions that we will create
1855 : : for predicate of default case. */
1856 : 155214 : if (new_range)
1857 : : {
1858 : 92055 : bound_count += (min == max) ? 1 : 2;
1859 : 92055 : ranges.safe_push (std::make_pair (min, max));
1860 : : }
1861 : : else
1862 : : {
1863 : 101707 : bound_count += (ranges.last ().first == ranges.last ().second);
1864 : 101707 : ranges.last ().second = max;
1865 : : }
1866 : : }
1867 : :
1868 : 50961 : e = gimple_switch_edge (cfun, last, 0);
1869 : 50961 : if (bound_count > bound_limit)
1870 : : {
1871 : 15835 : *(ipa_predicate *) e->aux = true;
1872 : 15835 : vec_free (param_ops);
1873 : 15835 : return;
1874 : : }
1875 : :
1876 : 35126 : ipa_predicate p_seg = true;
1877 : 35126 : ipa_predicate p_all = false;
1878 : :
1879 : 35126 : if (vr_type != VR_RANGE)
1880 : : {
1881 : 34579 : vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type));
1882 : 34579 : vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type));
1883 : : }
1884 : :
1885 : : /* Construct predicate to represent default range set that is negation of
1886 : : all case ranges. Case range is classified as containing single/non-single
1887 : : values. Suppose a piece of case ranges in the following.
1888 : :
1889 : : [D1...D2] [S1] ... [Sn] [D3...D4]
1890 : :
1891 : : To represent default case's range sets between two non-single value
1892 : : case ranges (From D2 to D3), we construct predicate as:
1893 : :
1894 : : D2 < x < D3 && x != S1 && ... && x != Sn
1895 : : */
1896 : 110271 : for (size_t i = 0; i < ranges.length (); i++)
1897 : : {
1898 : 75240 : tree min = ranges[i].first;
1899 : 75240 : tree max = ranges[i].second;
1900 : :
1901 : 75240 : if (min == max)
1902 : 95354 : p_seg &= add_condition (summary, params_summary, index,
1903 : : param_type, &aggpos, NE_EXPR,
1904 : 47677 : min, param_ops);
1905 : : else
1906 : : {
1907 : : /* Do not create sub-predicate for range that is beyond low bound
1908 : : of switch index. */
1909 : 27563 : if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type)))
1910 : : {
1911 : 19068 : p_seg &= add_condition (summary, params_summary, index,
1912 : : param_type, &aggpos,
1913 : 19068 : LT_EXPR, min, param_ops);
1914 : 19068 : p_all = p_all.or_with (summary->conds, p_seg);
1915 : : }
1916 : :
1917 : : /* Do not create sub-predicate for range that is beyond up bound
1918 : : of switch index. */
1919 : 27563 : if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type)))
1920 : : {
1921 : 95 : p_seg = false;
1922 : 95 : break;
1923 : : }
1924 : :
1925 : 27468 : p_seg = add_condition (summary, params_summary, index,
1926 : : param_type, &aggpos, GT_EXPR,
1927 : : max, param_ops);
1928 : : }
1929 : : }
1930 : :
1931 : 35126 : p_all = p_all.or_with (summary->conds, p_seg);
1932 : 35126 : *(ipa_predicate *) e->aux
1933 : 35126 : = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux);
1934 : :
1935 : 41691 : vec_free (param_ops);
1936 : 50961 : }
1937 : :
1938 : :
1939 : : /* For each BB in NODE attach to its AUX pointer predicate under
1940 : : which it is executable. */
1941 : :
1942 : : static void
1943 : 6360442 : compute_bb_predicates (struct ipa_func_body_info *fbi,
1944 : : struct cgraph_node *node,
1945 : : class ipa_fn_summary *summary,
1946 : : class ipa_node_params *params_summary)
1947 : : {
1948 : 6360442 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1949 : 6360442 : bool done = false;
1950 : 6360442 : basic_block bb;
1951 : :
1952 : 41699717 : FOR_EACH_BB_FN (bb, my_function)
1953 : : {
1954 : 35339275 : set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb);
1955 : 35339275 : set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb);
1956 : : }
1957 : :
1958 : : /* Entry block is always executable. */
1959 : 6360442 : ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1960 : 6360442 : = edge_predicate_pool.allocate ();
1961 : 6360442 : *(ipa_predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux = true;
1962 : :
1963 : : /* A simple dataflow propagation of predicates forward in the CFG.
1964 : : TODO: work in reverse postorder. */
1965 : 19207009 : while (!done)
1966 : : {
1967 : 12846567 : done = true;
1968 : 88969090 : FOR_EACH_BB_FN (bb, my_function)
1969 : : {
1970 : 76122523 : ipa_predicate p = false;
1971 : 76122523 : edge e;
1972 : 76122523 : edge_iterator ei;
1973 : 94386571 : FOR_EACH_EDGE (e, ei, bb->preds)
1974 : : {
1975 : 81003006 : if (e->src->aux)
1976 : : {
1977 : 79127885 : ipa_predicate this_bb_predicate
1978 : : = *(ipa_predicate *) e->src->aux;
1979 : 79127885 : if (e->aux)
1980 : 5124939 : this_bb_predicate &= (*(ipa_predicate *) e->aux);
1981 : 79127885 : p = p.or_with (summary->conds, this_bb_predicate);
1982 : 79127885 : if (p == true)
1983 : : break;
1984 : : }
1985 : : }
1986 : 76122523 : if (p != false)
1987 : : {
1988 : 74930547 : basic_block pdom_bb;
1989 : :
1990 : 74930547 : if (!bb->aux)
1991 : : {
1992 : 27664929 : done = false;
1993 : 27664929 : bb->aux = edge_predicate_pool.allocate ();
1994 : 27664929 : *((ipa_predicate *) bb->aux) = p;
1995 : : }
1996 : 47265618 : else if (p != *(ipa_predicate *) bb->aux)
1997 : : {
1998 : : /* This OR operation is needed to ensure monotonous data flow
1999 : : in the case we hit the limit on number of clauses and the
2000 : : and/or operations above give approximate answers. */
2001 : 138801 : p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux);
2002 : 138801 : if (p != *(ipa_predicate *)bb->aux)
2003 : : {
2004 : 112365 : done = false;
2005 : 112365 : *((ipa_predicate *)bb->aux) = p;
2006 : : }
2007 : : }
2008 : :
2009 : : /* For switch/if statement, we can OR-combine predicates of all
2010 : : its cases/branches to get predicate for basic block in their
2011 : : convergence point, but sometimes this will generate very
2012 : : complicated predicate. Actually, we can get simplified
2013 : : predicate in another way by using the fact that predicate
2014 : : for a basic block must also hold true for its post dominators.
2015 : : To be specific, basic block in convergence point of
2016 : : conditional statement should include predicate of the
2017 : : statement. */
2018 : 74930547 : pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
2019 : 74930547 : if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb)
2020 : : ;
2021 : 38431366 : else if (!pdom_bb->aux)
2022 : : {
2023 : 7674332 : done = false;
2024 : 7674332 : pdom_bb->aux = edge_predicate_pool.allocate ();
2025 : 7674332 : *((ipa_predicate *)pdom_bb->aux) = p;
2026 : : }
2027 : 30757034 : else if (p != *(ipa_predicate *)pdom_bb->aux)
2028 : : {
2029 : 3425706 : p = p.or_with (summary->conds,
2030 : : *(ipa_predicate *)pdom_bb->aux);
2031 : 3425706 : if (p != *(ipa_predicate *)pdom_bb->aux)
2032 : : {
2033 : 179175 : done = false;
2034 : 179175 : *((ipa_predicate *)pdom_bb->aux) = p;
2035 : : }
2036 : : }
2037 : : }
2038 : : }
2039 : : }
2040 : 6360442 : }
2041 : :
2042 : :
2043 : : /* Return predicate specifying when the STMT might have result that is not
2044 : : a compile time constant. */
2045 : :
2046 : : static ipa_predicate
2047 : 4203980 : will_be_nonconstant_expr_predicate (ipa_func_body_info *fbi,
2048 : : class ipa_fn_summary *summary,
2049 : : class ipa_node_params *params_summary,
2050 : : tree expr,
2051 : : vec<ipa_predicate> nonconstant_names)
2052 : : {
2053 : 4203980 : tree parm;
2054 : 4203980 : int index;
2055 : :
2056 : 4448258 : while (UNARY_CLASS_P (expr))
2057 : 244278 : expr = TREE_OPERAND (expr, 0);
2058 : :
2059 : 4203980 : parm = unmodified_parm (fbi, NULL, expr, NULL);
2060 : 4203980 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2061 : 218497 : return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL,
2062 : 218497 : ipa_predicate::changed, NULL_TREE);
2063 : 3985483 : if (is_gimple_min_invariant (expr))
2064 : 61690 : return false;
2065 : 3923793 : if (TREE_CODE (expr) == SSA_NAME)
2066 : 3746879 : return nonconstant_names[SSA_NAME_VERSION (expr)];
2067 : 176914 : if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2068 : : {
2069 : 176305 : ipa_predicate p1
2070 : 176305 : = will_be_nonconstant_expr_predicate (fbi, summary,
2071 : : params_summary,
2072 : 176305 : TREE_OPERAND (expr, 0),
2073 : : nonconstant_names);
2074 : 176305 : if (p1 == true)
2075 : 101397 : return p1;
2076 : :
2077 : 74908 : ipa_predicate p2
2078 : 74908 : = will_be_nonconstant_expr_predicate (fbi, summary,
2079 : : params_summary,
2080 : 74908 : TREE_OPERAND (expr, 1),
2081 : : nonconstant_names);
2082 : 74908 : return p1.or_with (summary->conds, p2);
2083 : : }
2084 : 609 : else if (TREE_CODE (expr) == COND_EXPR)
2085 : : {
2086 : 232 : ipa_predicate p1
2087 : 232 : = will_be_nonconstant_expr_predicate (fbi, summary,
2088 : : params_summary,
2089 : 232 : TREE_OPERAND (expr, 0),
2090 : : nonconstant_names);
2091 : 232 : if (p1 == true)
2092 : 81 : return p1;
2093 : :
2094 : 151 : ipa_predicate p2
2095 : 151 : = will_be_nonconstant_expr_predicate (fbi, summary,
2096 : : params_summary,
2097 : 151 : TREE_OPERAND (expr, 1),
2098 : : nonconstant_names);
2099 : 151 : if (p2 == true)
2100 : 151 : return p2;
2101 : 0 : p1 = p1.or_with (summary->conds, p2);
2102 : 0 : p2 = will_be_nonconstant_expr_predicate (fbi, summary,
2103 : : params_summary,
2104 : 0 : TREE_OPERAND (expr, 2),
2105 : : nonconstant_names);
2106 : 0 : return p2.or_with (summary->conds, p1);
2107 : : }
2108 : 377 : else if (TREE_CODE (expr) == CALL_EXPR)
2109 : 377 : return true;
2110 : : else
2111 : : {
2112 : 0 : debug_tree (expr);
2113 : 0 : gcc_unreachable ();
2114 : : }
2115 : : }
2116 : :
2117 : :
2118 : : /* Return predicate specifying when the STMT might have result that is not
2119 : : a compile time constant. */
2120 : :
2121 : : static ipa_predicate
2122 : 113748294 : will_be_nonconstant_predicate (struct ipa_func_body_info *fbi,
2123 : : class ipa_fn_summary *summary,
2124 : : class ipa_node_params *params_summary,
2125 : : gimple *stmt,
2126 : : vec<ipa_predicate> nonconstant_names)
2127 : : {
2128 : 113748294 : ipa_predicate p = true;
2129 : 113748294 : ssa_op_iter iter;
2130 : 113748294 : tree use;
2131 : 113748294 : tree param_type = NULL_TREE;
2132 : 113748294 : ipa_predicate op_non_const;
2133 : 113748294 : bool is_load;
2134 : 113748294 : int base_index;
2135 : 113748294 : struct agg_position_info aggpos;
2136 : :
2137 : : /* What statements might be optimized away
2138 : : when their arguments are constant. */
2139 : 113748294 : if (gimple_code (stmt) != GIMPLE_ASSIGN
2140 : 39102270 : && gimple_code (stmt) != GIMPLE_COND
2141 : 27764657 : && gimple_code (stmt) != GIMPLE_SWITCH
2142 : 141425997 : && (gimple_code (stmt) != GIMPLE_CALL
2143 : 20730409 : || !(gimple_call_flags (stmt) & ECF_CONST)))
2144 : 26034305 : return p;
2145 : :
2146 : : /* Stores will stay anyway. */
2147 : 87713989 : if (gimple_store_p (stmt))
2148 : 25868176 : return p;
2149 : :
2150 : 61845813 : is_load = gimple_assign_load_p (stmt);
2151 : :
2152 : : /* Loads can be optimized when the value is known. */
2153 : 61845813 : if (is_load)
2154 : : {
2155 : 17892177 : tree op = gimple_assign_rhs1 (stmt);
2156 : 17892177 : if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type,
2157 : : &aggpos))
2158 : 14629199 : return p;
2159 : : }
2160 : : else
2161 : 43953636 : base_index = -1;
2162 : :
2163 : : /* See if we understand all operands before we start
2164 : : adding conditionals. */
2165 : 63711101 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2166 : : {
2167 : 46820977 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2168 : : /* For arguments we can build a condition. */
2169 : 46820977 : if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0)
2170 : 8378382 : continue;
2171 : 38442595 : if (TREE_CODE (use) != SSA_NAME)
2172 : 0 : return p;
2173 : : /* If we know when operand is constant,
2174 : : we still can say something useful. */
2175 : 38442595 : if (nonconstant_names[SSA_NAME_VERSION (use)] != true)
2176 : 8116105 : continue;
2177 : 30326490 : return p;
2178 : : }
2179 : :
2180 : 16890124 : if (is_load)
2181 : 3262897 : op_non_const =
2182 : 3262897 : add_condition (summary, params_summary,
2183 : : base_index, param_type, &aggpos,
2184 : : ipa_predicate::changed, NULL_TREE);
2185 : : else
2186 : 13627227 : op_non_const = false;
2187 : 32353196 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2188 : : {
2189 : 15463072 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2190 : 15463072 : int index;
2191 : :
2192 : 15463072 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2193 : : {
2194 : 7891073 : if (index != base_index)
2195 : 5335762 : p = add_condition (summary, params_summary, index,
2196 : 5335762 : TREE_TYPE (parm), NULL,
2197 : : ipa_predicate::changed, NULL_TREE);
2198 : : else
2199 : 2555311 : continue;
2200 : : }
2201 : : else
2202 : 7571999 : p = nonconstant_names[SSA_NAME_VERSION (use)];
2203 : 12907761 : op_non_const = p.or_with (summary->conds, op_non_const);
2204 : : }
2205 : 16890124 : if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2206 : 14798950 : && gimple_op (stmt, 0)
2207 : 31415763 : && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2208 : 14525639 : nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2209 : 14525639 : = op_non_const;
2210 : 16890124 : return op_non_const;
2211 : : }
2212 : :
2213 : : struct record_modified_bb_info
2214 : : {
2215 : : tree op;
2216 : : bitmap bb_set;
2217 : : gimple *stmt;
2218 : : };
2219 : :
2220 : : /* Value is initialized in INIT_BB and used in USE_BB. We want to compute
2221 : : probability how often it changes between USE_BB.
2222 : : INIT_BB->count/USE_BB->count is an estimate, but if INIT_BB
2223 : : is in different loop nest, we can do better.
2224 : : This is all just estimate. In theory we look for minimal cut separating
2225 : : INIT_BB and USE_BB, but we only want to anticipate loop invariant motion
2226 : : anyway. */
2227 : :
2228 : : static basic_block
2229 : 10550156 : get_minimal_bb (basic_block init_bb, basic_block use_bb)
2230 : : {
2231 : 10550156 : class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father);
2232 : 10550156 : if (l && l->header->count < init_bb->count)
2233 : 360494 : return l->header;
2234 : : return init_bb;
2235 : : }
2236 : :
2237 : : /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2238 : : set except for info->stmt. */
2239 : :
2240 : : static bool
2241 : 5017259 : record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2242 : : {
2243 : 5017259 : struct record_modified_bb_info *info =
2244 : : (struct record_modified_bb_info *) data;
2245 : 5017259 : if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2246 : : return false;
2247 : 4985015 : if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef)))
2248 : : return false;
2249 : 4936576 : bitmap_set_bit (info->bb_set,
2250 : 4936576 : SSA_NAME_IS_DEFAULT_DEF (vdef)
2251 : 0 : ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2252 : : : get_minimal_bb
2253 : 4936576 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2254 : 4936576 : gimple_bb (info->stmt))->index);
2255 : 4936576 : if (dump_file)
2256 : : {
2257 : 0 : fprintf (dump_file, " Param ");
2258 : 0 : print_generic_expr (dump_file, info->op, TDF_SLIM);
2259 : 0 : fprintf (dump_file, " changed at bb %i, minimal: %i stmt: ",
2260 : 0 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index,
2261 : : get_minimal_bb
2262 : 0 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2263 : 0 : gimple_bb (info->stmt))->index);
2264 : 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (vdef), 0);
2265 : : }
2266 : : return false;
2267 : : }
2268 : :
2269 : : /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2270 : : will change since last invocation of STMT.
2271 : :
2272 : : Value 0 is reserved for compile time invariants.
2273 : : For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2274 : : ought to be REG_BR_PROB_BASE / estimated_iters. */
2275 : :
2276 : : static int
2277 : 38324837 : param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i)
2278 : : {
2279 : 38324837 : tree op = gimple_call_arg (stmt, i);
2280 : 38324837 : basic_block bb = gimple_bb (stmt);
2281 : :
2282 : 38324837 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
2283 : 299 : op = TREE_OPERAND (op, 0);
2284 : :
2285 : 38324837 : tree base = get_base_address (op);
2286 : :
2287 : : /* Global invariants never change. */
2288 : 38324837 : if (is_gimple_min_invariant (base))
2289 : : return 0;
2290 : :
2291 : : /* We would have to do non-trivial analysis to really work out what
2292 : : is the probability of value to change (i.e. when init statement
2293 : : is in a sibling loop of the call).
2294 : :
2295 : : We do an conservative estimate: when call is executed N times more often
2296 : : than the statement defining value, we take the frequency 1/N. */
2297 : 19233192 : if (TREE_CODE (base) == SSA_NAME)
2298 : : {
2299 : 16871303 : profile_count init_count;
2300 : :
2301 : 25638142 : if (!bb->count.nonzero_p ())
2302 : : return REG_BR_PROB_BASE;
2303 : :
2304 : 8621558 : if (SSA_NAME_IS_DEFAULT_DEF (base))
2305 : 3007978 : init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2306 : : else
2307 : 5613580 : init_count = get_minimal_bb
2308 : 5613580 : (gimple_bb (SSA_NAME_DEF_STMT (base)),
2309 : : gimple_bb (stmt))->count;
2310 : :
2311 : 8621558 : if (init_count < bb->count)
2312 : 433095 : return MAX ((init_count.to_sreal_scale (bb->count)
2313 : : * REG_BR_PROB_BASE).to_int (), 1);
2314 : : return REG_BR_PROB_BASE;
2315 : : }
2316 : : else
2317 : : {
2318 : 2361889 : ao_ref refd;
2319 : 2361889 : profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2320 : 2361889 : struct record_modified_bb_info info;
2321 : 2361889 : tree init = ctor_for_folding (base);
2322 : :
2323 : 2361889 : if (init != error_mark_node)
2324 : : return 0;
2325 : 5219942 : if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0)
2326 : : return REG_BR_PROB_BASE;
2327 : 1441992 : if (dump_file)
2328 : : {
2329 : 0 : fprintf (dump_file, " Analyzing param change probability of ");
2330 : 0 : print_generic_expr (dump_file, op, TDF_SLIM);
2331 : 0 : fprintf (dump_file, "\n");
2332 : : }
2333 : 1441992 : ao_ref_init (&refd, op);
2334 : 1441992 : info.op = op;
2335 : 1441992 : info.stmt = stmt;
2336 : 1441992 : info.bb_set = BITMAP_ALLOC (NULL);
2337 : 1441992 : int walked
2338 : 2883984 : = walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2339 : : NULL, NULL, fbi->aa_walk_budget);
2340 : 1441992 : if (walked > 0)
2341 : 1313205 : fbi->aa_walk_budget -= walked;
2342 : 1441992 : if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index))
2343 : : {
2344 : 881722 : if (walked < 0)
2345 : 206 : fbi->aa_walk_budget = 0;
2346 : 881722 : if (dump_file)
2347 : : {
2348 : 0 : if (walked < 0)
2349 : 0 : fprintf (dump_file, " Ran out of AA walking budget.\n");
2350 : : else
2351 : 0 : fprintf (dump_file, " Set in same BB as used.\n");
2352 : : }
2353 : 881722 : BITMAP_FREE (info.bb_set);
2354 : 881722 : return REG_BR_PROB_BASE;
2355 : : }
2356 : :
2357 : 560270 : bitmap_iterator bi;
2358 : 560270 : unsigned index;
2359 : : /* Lookup the most frequent update of the value and believe that
2360 : : it dominates all the other; precise analysis here is difficult. */
2361 : 1677319 : EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2362 : 1117049 : max = profile_count::max_prefer_initialized
2363 : 1117049 : (max, BASIC_BLOCK_FOR_FN (cfun, index)->count);
2364 : 560270 : if (dump_file)
2365 : : {
2366 : 0 : fprintf (dump_file, " Set with count ");
2367 : 0 : max.dump (dump_file);
2368 : 0 : fprintf (dump_file, " and used with count ");
2369 : 0 : bb->count.dump (dump_file);
2370 : 0 : fprintf (dump_file, " freq %f\n",
2371 : 0 : max.to_sreal_scale (bb->count).to_double ());
2372 : : }
2373 : :
2374 : 560270 : BITMAP_FREE (info.bb_set);
2375 : 560270 : if (max < bb->count)
2376 : 91324 : return MAX ((max.to_sreal_scale (bb->count)
2377 : : * REG_BR_PROB_BASE).to_int (), 1);
2378 : : return REG_BR_PROB_BASE;
2379 : : }
2380 : : }
2381 : :
2382 : : /* Find whether a basic block BB is the final block of a (half) diamond CFG
2383 : : sub-graph and if the predicate the condition depends on is known. If so,
2384 : : return true and store the pointer the predicate in *P. */
2385 : :
2386 : : static bool
2387 : 6501496 : phi_result_unknown_predicate (ipa_func_body_info *fbi,
2388 : : ipa_fn_summary *summary,
2389 : : class ipa_node_params *params_summary,
2390 : : basic_block bb,
2391 : : ipa_predicate *p,
2392 : : vec<ipa_predicate> nonconstant_names)
2393 : : {
2394 : 6501496 : edge e;
2395 : 6501496 : edge_iterator ei;
2396 : 6501496 : basic_block first_bb = NULL;
2397 : :
2398 : 6501496 : if (single_pred_p (bb))
2399 : : {
2400 : 22029 : *p = false;
2401 : 22029 : return true;
2402 : : }
2403 : :
2404 : 14847449 : FOR_EACH_EDGE (e, ei, bb->preds)
2405 : : {
2406 : 12698554 : if (single_succ_p (e->src))
2407 : : {
2408 : 13247615 : if (!single_pred_p (e->src))
2409 : : return false;
2410 : 7354278 : if (!first_bb)
2411 : 3195047 : first_bb = single_pred (e->src);
2412 : 4159231 : else if (single_pred (e->src) != first_bb)
2413 : : return false;
2414 : : }
2415 : : else
2416 : : {
2417 : 4266634 : if (!first_bb)
2418 : : first_bb = e->src;
2419 : 1473563 : else if (e->src != first_bb)
2420 : : return false;
2421 : : }
2422 : : }
2423 : :
2424 : 2148895 : if (!first_bb)
2425 : : return false;
2426 : :
2427 : 4297790 : gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb));
2428 : 2096277 : if (!stmt
2429 : 2096277 : || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2430 : 319669 : return false;
2431 : :
2432 : 1829226 : *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary,
2433 : : gimple_cond_lhs (stmt),
2434 : : nonconstant_names);
2435 : 1829226 : if (*p == true)
2436 : : return false;
2437 : : else
2438 : : return true;
2439 : : }
2440 : :
2441 : : /* Given a PHI statement in a function described by inline properties SUMMARY
2442 : : and *P being the predicate describing whether the selected PHI argument is
2443 : : known, store a predicate for the result of the PHI statement into
2444 : : NONCONSTANT_NAMES, if possible. */
2445 : :
2446 : : static void
2447 : 688084 : predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi,
2448 : : ipa_predicate *p,
2449 : : vec<ipa_predicate> nonconstant_names)
2450 : : {
2451 : 688084 : unsigned i;
2452 : :
2453 : 990373 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2454 : : {
2455 : 863913 : tree arg = gimple_phi_arg (phi, i)->def;
2456 : 863913 : if (!is_gimple_min_invariant (arg))
2457 : : {
2458 : 734573 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
2459 : 1469146 : *p = p->or_with (summary->conds,
2460 : 734573 : nonconstant_names[SSA_NAME_VERSION (arg)]);
2461 : 734573 : if (*p == true)
2462 : : return;
2463 : : }
2464 : : }
2465 : :
2466 : 126460 : if (dump_file && (dump_flags & TDF_DETAILS))
2467 : : {
2468 : 3 : fprintf (dump_file, "\t\tphi predicate: ");
2469 : 3 : p->dump (dump_file, summary->conds);
2470 : : }
2471 : 126460 : nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2472 : : }
2473 : :
2474 : : /* For a typical usage of __builtin_expect (a<b, 1), we
2475 : : may introduce an extra relation stmt:
2476 : : With the builtin, we have
2477 : : t1 = a <= b;
2478 : : t2 = (long int) t1;
2479 : : t3 = __builtin_expect (t2, 1);
2480 : : if (t3 != 0)
2481 : : goto ...
2482 : : Without the builtin, we have
2483 : : if (a<=b)
2484 : : goto...
2485 : : This affects the size/time estimation and may have
2486 : : an impact on the earlier inlining.
2487 : : Here find this pattern and fix it up later. */
2488 : :
2489 : : static gimple *
2490 : 41196646 : find_foldable_builtin_expect (basic_block bb)
2491 : : {
2492 : 41196646 : gimple_stmt_iterator bsi;
2493 : :
2494 : 283756239 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2495 : : {
2496 : 201513482 : gimple *stmt = gsi_stmt (bsi);
2497 : 201513482 : if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2498 : 201316914 : || gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY)
2499 : 402830331 : || gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT))
2500 : : {
2501 : 324484 : tree var = gimple_call_lhs (stmt);
2502 : 324484 : tree arg = gimple_call_arg (stmt, 0);
2503 : 324484 : use_operand_p use_p;
2504 : 324484 : gimple *use_stmt;
2505 : 324484 : bool match = false;
2506 : 324484 : bool done = false;
2507 : :
2508 : 324484 : if (!var || !arg)
2509 : 3 : continue;
2510 : 324481 : gcc_assert (TREE_CODE (var) == SSA_NAME);
2511 : :
2512 : 647677 : while (TREE_CODE (arg) == SSA_NAME)
2513 : : {
2514 : 647677 : gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg);
2515 : 647677 : if (!is_gimple_assign (stmt_tmp))
2516 : : break;
2517 : 640995 : switch (gimple_assign_rhs_code (stmt_tmp))
2518 : : {
2519 : : case LT_EXPR:
2520 : : case LE_EXPR:
2521 : : case GT_EXPR:
2522 : : case GE_EXPR:
2523 : : case EQ_EXPR:
2524 : : case NE_EXPR:
2525 : : match = true;
2526 : : done = true;
2527 : : break;
2528 : : CASE_CONVERT:
2529 : : break;
2530 : : default:
2531 : : done = true;
2532 : : break;
2533 : : }
2534 : 323196 : if (done)
2535 : : break;
2536 : 323196 : arg = gimple_assign_rhs1 (stmt_tmp);
2537 : : }
2538 : :
2539 : 290674 : if (match && single_imm_use (var, &use_p, &use_stmt)
2540 : 614716 : && gimple_code (use_stmt) == GIMPLE_COND)
2541 : 150535 : return use_stmt;
2542 : : }
2543 : : }
2544 : : return NULL;
2545 : : }
2546 : :
2547 : : /* Return true when the basic blocks contains only clobbers followed by RESX.
2548 : : Such BBs are kept around to make removal of dead stores possible with
2549 : : presence of EH and will be optimized out by optimize_clobbers later in the
2550 : : game.
2551 : :
2552 : : NEED_EH is used to recurse in case the clobber has non-EH predecessors
2553 : : that can be clobber only, too.. When it is false, the RESX is not necessary
2554 : : on the end of basic block. */
2555 : :
2556 : : static bool
2557 : 41823137 : clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2558 : : {
2559 : 41823137 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2560 : 41823137 : edge_iterator ei;
2561 : 41823137 : edge e;
2562 : :
2563 : 41823137 : if (need_eh)
2564 : : {
2565 : 41742571 : if (gsi_end_p (gsi))
2566 : : return false;
2567 : 40502387 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2568 : : return false;
2569 : 1037751 : gsi_prev (&gsi);
2570 : : }
2571 : 41278200 : else if (!single_succ_p (bb))
2572 : : return false;
2573 : :
2574 : 5111565 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2575 : : {
2576 : 3020462 : gimple *stmt = gsi_stmt (gsi);
2577 : 3020462 : if (is_gimple_debug (stmt))
2578 : 1142412 : continue;
2579 : 1878050 : if (gimple_clobber_p (stmt))
2580 : 887281 : continue;
2581 : 990769 : if (gimple_code (stmt) == GIMPLE_LABEL)
2582 : : break;
2583 : : return false;
2584 : : }
2585 : :
2586 : : /* See if all predecessors are either throws or clobber only BBs. */
2587 : 3211370 : FOR_EACH_EDGE (e, ei, bb->preds)
2588 : 2652005 : if (!(e->flags & EDGE_EH)
2589 : 2652005 : && !clobber_only_eh_bb_p (e->src, false))
2590 : : return false;
2591 : :
2592 : : return true;
2593 : : }
2594 : :
2595 : : /* Return true if STMT compute a floating point expression that may be affected
2596 : : by -ffast-math and similar flags. */
2597 : :
2598 : : static bool
2599 : 93867061 : fp_expression_p (gimple *stmt)
2600 : : {
2601 : 93867061 : ssa_op_iter i;
2602 : 93867061 : tree op;
2603 : :
2604 : 210263409 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE)
2605 : 116993183 : if (FLOAT_TYPE_P (TREE_TYPE (op)))
2606 : : return true;
2607 : : return false;
2608 : : }
2609 : :
2610 : : /* Return true if T references memory location that is local
2611 : : for the function (that means, dead after return) or read-only. */
2612 : :
2613 : : bool
2614 : 59484146 : refs_local_or_readonly_memory_p (tree t)
2615 : : {
2616 : : /* Non-escaping memory is fine. */
2617 : 59484146 : t = get_base_address (t);
2618 : 59484146 : if ((TREE_CODE (t) == MEM_REF
2619 : 59484146 : || TREE_CODE (t) == TARGET_MEM_REF))
2620 : 24952500 : return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2621 : :
2622 : : /* Automatic variables are fine. */
2623 : 34531646 : if (DECL_P (t)
2624 : 34531646 : && auto_var_in_fn_p (t, current_function_decl))
2625 : : return true;
2626 : :
2627 : : /* Read-only variables are fine. */
2628 : 10935801 : if (DECL_P (t) && TREE_READONLY (t))
2629 : : return true;
2630 : :
2631 : : return false;
2632 : : }
2633 : :
2634 : : /* Return true if T is a pointer pointing to memory location that is local
2635 : : for the function (that means, dead after return) or read-only. */
2636 : :
2637 : : bool
2638 : 71861312 : points_to_local_or_readonly_memory_p (tree t)
2639 : : {
2640 : : /* See if memory location is clearly invalid. */
2641 : 71861312 : if (integer_zerop (t))
2642 : 2360560 : return flag_delete_null_pointer_checks;
2643 : 69500752 : if (TREE_CODE (t) == SSA_NAME)
2644 : : {
2645 : : /* For IPA passes we can consinder accesses to return slot local
2646 : : even if it is not local in the sense that memory is dead by
2647 : : the end of founction.
2648 : : The outer function will see a store in the call assignment
2649 : : and thus this will do right thing for all uses of this
2650 : : function in the current IPA passes (modref, pure/const discovery
2651 : : and inlining heuristics). */
2652 : 43940890 : if (DECL_RESULT (current_function_decl)
2653 : 43940890 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
2654 : 44989982 : && t == ssa_default_def (cfun, DECL_RESULT (current_function_decl)))
2655 : : return true;
2656 : 43630543 : return !ptr_deref_may_alias_global_p (t, false);
2657 : : }
2658 : 25559862 : if (TREE_CODE (t) == ADDR_EXPR
2659 : 25559862 : && (TREE_CODE (TREE_OPERAND (t, 0)) != TARGET_MEM_REF
2660 : 4615 : || TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) != INTEGER_CST))
2661 : 13127561 : return refs_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2662 : : return false;
2663 : : }
2664 : :
2665 : : /* Return true if T is a pointer pointing to memory location that is possible
2666 : : sra candidate if all functions it is passed to are inlined. */
2667 : :
2668 : : static bool
2669 : 38324837 : points_to_possible_sra_candidate_p (tree t)
2670 : : {
2671 : 38324837 : if (TREE_CODE (t) != ADDR_EXPR)
2672 : : return false;
2673 : :
2674 : 11458419 : t = get_base_address (TREE_OPERAND (t, 0));
2675 : :
2676 : : /* Automatic variables are fine. */
2677 : 11458419 : if (DECL_P (t)
2678 : 11458419 : && auto_var_in_fn_p (t, current_function_decl))
2679 : : return true;
2680 : : return false;
2681 : : }
2682 : :
2683 : : /* Return true if BB only calls builtin_unreachable.
2684 : : We skip empty basic blocks, debug statements, clobbers and predicts.
2685 : : CACHE is used to memoize already analyzed blocks. */
2686 : :
2687 : : static bool
2688 : 27119976 : builtin_unreachable_bb_p (basic_block bb, vec<unsigned char> &cache)
2689 : : {
2690 : 27119976 : if (cache[bb->index])
2691 : 2530002 : return cache[bb->index] - 1;
2692 : 24589974 : gimple_stmt_iterator si;
2693 : 24589974 : auto_vec <basic_block, 4> visited_bbs;
2694 : 24589974 : bool ret = false;
2695 : 25460234 : while (true)
2696 : : {
2697 : 25460234 : bool empty_bb = true;
2698 : 25460234 : visited_bbs.safe_push (bb);
2699 : 25460234 : cache[bb->index] = 3;
2700 : 25460234 : for (si = gsi_start_nondebug_bb (bb);
2701 : 26943214 : !gsi_end_p (si) && empty_bb;
2702 : 1482980 : gsi_next_nondebug (&si))
2703 : : {
2704 : 25486317 : if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT
2705 : 25107790 : && !gimple_clobber_p (gsi_stmt (si))
2706 : 49522689 : && !gimple_nop_p (gsi_stmt (si)))
2707 : : {
2708 : : empty_bb = false;
2709 : : break;
2710 : : }
2711 : : }
2712 : 25460234 : if (!empty_bb)
2713 : : break;
2714 : : else
2715 : 1456897 : bb = single_succ_edge (bb)->dest;
2716 : 1456897 : if (cache[bb->index])
2717 : : {
2718 : 586637 : ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1;
2719 : 586637 : goto done;
2720 : : }
2721 : : }
2722 : 24003337 : if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE)
2723 : 24003337 : || gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP))
2724 : : ret = true;
2725 : 24589974 : done:
2726 : 99230156 : for (basic_block vbb:visited_bbs)
2727 : 25460234 : cache[vbb->index] = (unsigned char)ret + 1;
2728 : 24589974 : return ret;
2729 : 24589974 : }
2730 : :
2731 : : static bool
2732 : 13664808 : guards_builtin_unreachable (basic_block bb, vec<unsigned char> &cache)
2733 : : {
2734 : 13664808 : edge_iterator ei;
2735 : 13664808 : edge e;
2736 : 40555742 : FOR_EACH_EDGE (e, ei, bb->succs)
2737 : 27119976 : if (builtin_unreachable_bb_p (e->dest, cache))
2738 : : {
2739 : 229042 : if (dump_file && (dump_flags & TDF_DETAILS))
2740 : 1 : fprintf (dump_file,
2741 : : "BB %i ends with conditional guarding __builtin_unreachable;"
2742 : : " conditinal is unnecesary\n", bb->index);
2743 : 229042 : return true;
2744 : : }
2745 : : return false;
2746 : : }
2747 : :
2748 : : #define STMT_NECESSARY GF_PLF_1
2749 : :
2750 : : /* If STMT is not already marked necessary, mark it, and add it to the
2751 : : worklist if ADD_TO_WORKLIST is true. */
2752 : :
2753 : : static inline void
2754 : 171096467 : mark_stmt_necessary (gimple *stmt, auto_vec<gimple *> &worklist)
2755 : : {
2756 : 171096467 : gcc_assert (stmt);
2757 : :
2758 : 171096467 : if (gimple_plf (stmt, STMT_NECESSARY))
2759 : : return;
2760 : :
2761 : 142099755 : if (dump_file && (dump_flags & TDF_DETAILS))
2762 : : {
2763 : 207 : fprintf (dump_file, "Marking useful stmt: ");
2764 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2765 : 207 : fprintf (dump_file, "\n");
2766 : : }
2767 : :
2768 : 142099755 : gimple_set_plf (stmt, STMT_NECESSARY, true);
2769 : 142099755 : worklist.safe_push (stmt);
2770 : : }
2771 : :
2772 : : /* Mark the statement defining operand OP as necessary. */
2773 : :
2774 : : static inline void
2775 : 120293708 : mark_operand_necessary (tree op, auto_vec<gimple *> &worklist)
2776 : : {
2777 : 120293708 : gimple *stmt = SSA_NAME_DEF_STMT (op);
2778 : 120293708 : if (gimple_nop_p (stmt))
2779 : : return;
2780 : 97567652 : mark_stmt_necessary (stmt, worklist);
2781 : : }
2782 : :
2783 : : /* Mark all statements that will remain in the body after optimizing out
2784 : : conditionals guarding __builtin_unreachable which we keep to preserve
2785 : : value ranges. */
2786 : :
2787 : : static void
2788 : 7249422 : find_necessary_statements (struct cgraph_node *node)
2789 : : {
2790 : 7249422 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2791 : 7249422 : auto_vec<unsigned char, 10> cache;
2792 : 7249422 : basic_block bb;
2793 : 7249422 : auto_vec<gimple *> worklist;
2794 : :
2795 : 7249422 : cache.safe_grow_cleared (last_basic_block_for_fn (cfun));
2796 : : /* Mark all obviously necessary statements. */
2797 : 48991993 : FOR_EACH_BB_FN (bb, my_function)
2798 : : {
2799 : 41742571 : for (gimple_stmt_iterator gsi = gsi_start_phis (bb);
2800 : 53532700 : !gsi_end_p (gsi); gsi_next (&gsi))
2801 : 11790129 : gimple_set_plf (gsi_stmt (gsi), STMT_NECESSARY, false);
2802 : :
2803 : 232438993 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2804 : 148953851 : gsi_next_nondebug (&bsi))
2805 : : {
2806 : 148953851 : gimple *stmt = gsi_stmt (bsi);
2807 : :
2808 : 148953851 : gimple_set_plf (stmt, STMT_NECESSARY, false);
2809 : 148953851 : if (gimple_has_side_effects (stmt)
2810 : 122111029 : || (is_ctrl_stmt (stmt)
2811 : 21949887 : && (gimple_code (stmt) != GIMPLE_COND
2812 : 13664808 : || !guards_builtin_unreachable (bb, cache)))
2813 : 100390184 : || gimple_store_p (stmt)
2814 : 224400113 : || gimple_code (stmt) == GIMPLE_ASM)
2815 : 73528815 : mark_stmt_necessary (stmt, worklist);
2816 : : }
2817 : : }
2818 : 149349177 : while (worklist.length () > 0)
2819 : : {
2820 : 142099755 : gimple *stmt = worklist.pop ();
2821 : :
2822 : 142099755 : if (dump_file && (dump_flags & TDF_DETAILS))
2823 : : {
2824 : 207 : fprintf (dump_file, "processing: ");
2825 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2826 : 207 : fprintf (dump_file, "\n");
2827 : : }
2828 : 142099755 : if (gimple_code (stmt) == GIMPLE_PHI)
2829 : 18378887 : for (unsigned int k = 0; k < gimple_phi_num_args (stmt); k++)
2830 : : {
2831 : 12960082 : tree arg = PHI_ARG_DEF (stmt, k);
2832 : :
2833 : 12960082 : if (TREE_CODE (arg) == SSA_NAME)
2834 : 10046528 : mark_operand_necessary (arg, worklist);
2835 : : }
2836 : : else
2837 : : {
2838 : 136680950 : ssa_op_iter iter;
2839 : 136680950 : tree use;
2840 : :
2841 : 246928130 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2842 : 110247180 : mark_operand_necessary (use, worklist);
2843 : : }
2844 : : }
2845 : 7249422 : }
2846 : :
2847 : : /* Analyze function body for NODE.
2848 : : EARLY indicates run from early optimization pipeline. */
2849 : :
2850 : : static void
2851 : 7249422 : analyze_function_body (struct cgraph_node *node, bool early)
2852 : : {
2853 : 7249422 : sreal time = opt_for_fn (node->decl, param_uninlined_function_time);
2854 : : /* Estimate static overhead for function prologue/epilogue and alignment. */
2855 : 7249422 : int size = opt_for_fn (node->decl, param_uninlined_function_insns);
2856 : : /* Benefits are scaled by probability of elimination that is in range
2857 : : <0,2>. */
2858 : 7249422 : basic_block bb;
2859 : 7249422 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2860 : 7249422 : sreal freq;
2861 : 7249422 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
2862 : 7249422 : ipa_node_params *params_summary
2863 : 7249422 : = early ? NULL : ipa_node_params_sum->get (node);
2864 : 7249422 : ipa_predicate bb_predicate;
2865 : 7249422 : struct ipa_func_body_info fbi;
2866 : 7249422 : vec<ipa_predicate> nonconstant_names = vNULL;
2867 : 7249422 : int nblocks, n;
2868 : 7249422 : int *order;
2869 : 7249422 : gimple *fix_builtin_expect_stmt;
2870 : :
2871 : 7249422 : gcc_assert (my_function && my_function->cfg);
2872 : 7249422 : gcc_assert (cfun == my_function);
2873 : :
2874 : 7249422 : memset(&fbi, 0, sizeof(fbi));
2875 : 7249422 : vec_free (info->conds);
2876 : 7249422 : info->conds = NULL;
2877 : 7249422 : info->size_time_table.release ();
2878 : 7249422 : info->call_size_time_table.release ();
2879 : :
2880 : : /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2881 : : so we can produce proper inline hints.
2882 : :
2883 : : When optimizing and analyzing for early inliner, initialize node params
2884 : : so we can produce correct BB predicates. */
2885 : :
2886 : 7249422 : if (opt_for_fn (node->decl, optimize))
2887 : : {
2888 : 6360442 : calculate_dominance_info (CDI_DOMINATORS);
2889 : 6360442 : calculate_dominance_info (CDI_POST_DOMINATORS);
2890 : 6360442 : if (!early)
2891 : 1378437 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2892 : : else
2893 : : {
2894 : 4982005 : ipa_check_create_node_params ();
2895 : 4982005 : ipa_initialize_node_params (node);
2896 : : }
2897 : :
2898 : 6360442 : if (ipa_node_params_sum)
2899 : : {
2900 : 6360442 : fbi.node = node;
2901 : 6360442 : fbi.info = ipa_node_params_sum->get (node);
2902 : 6360442 : fbi.bb_infos = vNULL;
2903 : 6360442 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
2904 : 6360442 : fbi.param_count = count_formal_params (node->decl);
2905 : 6360442 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
2906 : :
2907 : 6360442 : nonconstant_names.safe_grow_cleared
2908 : 6360442 : (SSANAMES (my_function)->length (), true);
2909 : : }
2910 : : }
2911 : :
2912 : 7249422 : if (dump_file)
2913 : 229 : fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2914 : : node->dump_name ());
2915 : :
2916 : : /* When we run into maximal number of entries, we assign everything to the
2917 : : constant truth case. Be sure to have it in list. */
2918 : 7249422 : bb_predicate = true;
2919 : 7249422 : info->account_size_time (0, 0, bb_predicate, bb_predicate);
2920 : :
2921 : 7249422 : bb_predicate = ipa_predicate::not_inlined ();
2922 : 7249422 : info->account_size_time (opt_for_fn (node->decl,
2923 : : param_uninlined_function_insns)
2924 : : * ipa_fn_summary::size_scale,
2925 : 7249422 : opt_for_fn (node->decl,
2926 : : param_uninlined_function_time),
2927 : : bb_predicate,
2928 : : bb_predicate);
2929 : :
2930 : : /* Only look for target information for inlinable functions. */
2931 : 7249422 : bool scan_for_target_info =
2932 : 7249422 : info->inlinable
2933 : 13062299 : && targetm.target_option.need_ipa_fn_target_info (node->decl,
2934 : 5812877 : info->target_info);
2935 : :
2936 : 7249422 : if (fbi.info)
2937 : 6360442 : compute_bb_predicates (&fbi, node, info, params_summary);
2938 : 7249422 : find_necessary_statements (node);
2939 : 7249422 : const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2940 : 7249422 : order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2941 : 7249422 : nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2942 : 48991993 : for (n = 0; n < nblocks; n++)
2943 : : {
2944 : 41742571 : bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2945 : 41742571 : freq = bb->count.to_sreal_scale (entry_count);
2946 : 41742571 : if (clobber_only_eh_bb_p (bb))
2947 : : {
2948 : 545925 : if (dump_file && (dump_flags & TDF_DETAILS))
2949 : 0 : fprintf (dump_file, "\n Ignoring BB %i;"
2950 : : " it will be optimized away by cleanup_clobbers\n",
2951 : : bb->index);
2952 : 545925 : continue;
2953 : : }
2954 : :
2955 : : /* TODO: Obviously predicates can be propagated down across CFG. */
2956 : 41196646 : if (fbi.info)
2957 : : {
2958 : 34854226 : if (bb->aux)
2959 : 34854212 : bb_predicate = *(ipa_predicate *)bb->aux;
2960 : : else
2961 : 14 : bb_predicate = false;
2962 : : }
2963 : : else
2964 : 6342420 : bb_predicate = true;
2965 : :
2966 : 41196646 : if (dump_file && (dump_flags & TDF_DETAILS))
2967 : : {
2968 : 67 : fprintf (dump_file, "\n BB %i predicate:", bb->index);
2969 : 67 : bb_predicate.dump (dump_file, info->conds);
2970 : : }
2971 : :
2972 : 41196646 : if (fbi.info && nonconstant_names.exists ())
2973 : : {
2974 : 34854226 : ipa_predicate phi_predicate;
2975 : 34854226 : bool first_phi = true;
2976 : :
2977 : 35542310 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2978 : 688084 : gsi_next (&bsi))
2979 : : {
2980 : 6581421 : if (first_phi
2981 : 6581421 : && !phi_result_unknown_predicate (&fbi, info,
2982 : : params_summary,
2983 : : bb,
2984 : : &phi_predicate,
2985 : : nonconstant_names))
2986 : : break;
2987 : 688084 : first_phi = false;
2988 : 688084 : if (dump_file && (dump_flags & TDF_DETAILS))
2989 : : {
2990 : 3 : fprintf (dump_file, " ");
2991 : 3 : print_gimple_stmt (dump_file, gsi_stmt (bsi), 0);
2992 : : }
2993 : 688084 : predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2994 : : nonconstant_names);
2995 : : }
2996 : : }
2997 : :
2998 : 41196646 : fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2999 : :
3000 : 41196646 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
3001 : 182156730 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
3002 : : {
3003 : 140960084 : gimple *stmt = gsi_stmt (bsi);
3004 : 140960084 : if (!gimple_plf (stmt, STMT_NECESSARY))
3005 : : {
3006 : 5577214 : if (dump_file && (dump_flags & TDF_DETAILS))
3007 : : {
3008 : 19 : fprintf (dump_file, " skipping unnecesary stmt ");
3009 : 19 : print_gimple_stmt (dump_file, stmt, 0);
3010 : : }
3011 : : /* TODO: const calls used only to produce values for
3012 : : builtion_unreachable guards should not be accounted. However
3013 : : we still want to inline them and this does does not work well
3014 : : with the cost model. For now account them as usual. */
3015 : 5577214 : if (!is_gimple_call (stmt)
3016 : 5577214 : || gimple_call_internal_p (stmt))
3017 : 5276375 : continue;
3018 : : }
3019 : 135683709 : int this_size = estimate_num_insns (stmt, &eni_size_weights);
3020 : 135683709 : int this_time = estimate_num_insns (stmt, &eni_time_weights);
3021 : 135683709 : int prob;
3022 : 135683709 : ipa_predicate will_be_nonconstant;
3023 : :
3024 : : /* This relation stmt should be folded after we remove
3025 : : __builtin_expect call. Adjust the cost here. */
3026 : 135683709 : if (stmt == fix_builtin_expect_stmt)
3027 : : {
3028 : 150095 : this_size--;
3029 : 150095 : this_time--;
3030 : : }
3031 : :
3032 : 135683709 : if (dump_file && (dump_flags & TDF_DETAILS))
3033 : : {
3034 : 201 : fprintf (dump_file, " ");
3035 : 201 : print_gimple_stmt (dump_file, stmt, 0);
3036 : 201 : fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
3037 : : freq.to_double (), this_size,
3038 : : this_time);
3039 : : }
3040 : :
3041 : 135683709 : if (is_gimple_call (stmt)
3042 : 135683709 : && !gimple_call_internal_p (stmt))
3043 : : {
3044 : 22753336 : struct cgraph_edge *edge = node->get_edge (stmt);
3045 : 22753336 : ipa_call_summary *es = ipa_call_summaries->get_create (edge);
3046 : :
3047 : : /* Special case: results of BUILT_IN_CONSTANT_P will be always
3048 : : resolved as constant. We however don't want to optimize
3049 : : out the cgraph edges. */
3050 : 22753336 : if (nonconstant_names.exists ()
3051 : 19909412 : && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
3052 : 9276 : && gimple_call_lhs (stmt)
3053 : 22762612 : && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3054 : : {
3055 : 9276 : ipa_predicate false_p = false;
3056 : 9276 : nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
3057 : 9276 : = false_p;
3058 : : }
3059 : 22753336 : if (ipa_node_params_sum)
3060 : : {
3061 : 19922407 : int count = gimple_call_num_args (stmt);
3062 : 19922407 : int i;
3063 : :
3064 : 19922407 : if (count)
3065 : 17045912 : es->param.safe_grow_cleared (count, true);
3066 : 58247244 : for (i = 0; i < count; i++)
3067 : : {
3068 : 38324837 : int prob = param_change_prob (&fbi, stmt, i);
3069 : 38324837 : gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
3070 : 38324837 : es->param[i].change_prob = prob;
3071 : 38324837 : es->param[i].points_to_local_or_readonly_memory
3072 : 38324837 : = points_to_local_or_readonly_memory_p
3073 : 38324837 : (gimple_call_arg (stmt, i));
3074 : 38324837 : es->param[i].points_to_possible_sra_candidate
3075 : 38324837 : = points_to_possible_sra_candidate_p
3076 : 38324837 : (gimple_call_arg (stmt, i));
3077 : : }
3078 : : }
3079 : : /* We cannot setup VLA parameters during inlining. */
3080 : 66845594 : for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i)
3081 : 44092654 : if (TREE_CODE (gimple_call_arg (stmt, i)) == WITH_SIZE_EXPR)
3082 : : {
3083 : 396 : edge->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
3084 : 396 : break;
3085 : : }
3086 : 22753336 : es->call_stmt_size = this_size;
3087 : 22753336 : es->call_stmt_time = this_time;
3088 : 22753336 : es->loop_depth = bb_loop_depth (bb);
3089 : 22753336 : edge_set_predicate (edge, &bb_predicate);
3090 : 22753336 : if (edge->speculative)
3091 : : {
3092 : 0 : cgraph_edge *indirect
3093 : 0 : = edge->speculative_call_indirect_edge ();
3094 : 0 : ipa_call_summary *es2
3095 : 0 : = ipa_call_summaries->get_create (indirect);
3096 : 0 : ipa_call_summaries->duplicate (edge, indirect,
3097 : : es, es2);
3098 : :
3099 : : /* Edge is the first direct call.
3100 : : create and duplicate call summaries for multiple
3101 : : speculative call targets. */
3102 : 0 : for (cgraph_edge *direct
3103 : 0 : = edge->next_speculative_call_target ();
3104 : 0 : direct;
3105 : 0 : direct = direct->next_speculative_call_target ())
3106 : : {
3107 : 0 : ipa_call_summary *es3
3108 : 0 : = ipa_call_summaries->get_create (direct);
3109 : 0 : ipa_call_summaries->duplicate (edge, direct,
3110 : : es, es3);
3111 : : }
3112 : : }
3113 : :
3114 : : /* If dealing with a carrying edge, copy its summary over to its
3115 : : attached edges as well. */
3116 : 22753336 : if (edge->has_callback)
3117 : : {
3118 : 16 : cgraph_edge *cbe;
3119 : 32 : for (cbe = edge->first_callback_edge (); cbe;
3120 : 16 : cbe = cbe->next_callback_edge ())
3121 : : {
3122 : 16 : ipa_call_summary *es2 = ipa_call_summaries->get (cbe);
3123 : 16 : es2 = ipa_call_summaries->get_create (cbe);
3124 : 16 : ipa_call_summaries->duplicate (edge, cbe, es, es2);
3125 : : /* Unlike speculative edges, callback edges have no real
3126 : : size or time; the call doesn't exist. Reflect that in
3127 : : their summaries. */
3128 : 16 : es2->call_stmt_size = 0;
3129 : 16 : es2->call_stmt_time = 0;
3130 : : }
3131 : : }
3132 : : }
3133 : :
3134 : : /* TODO: When conditional jump or switch is known to be constant, but
3135 : : we did not translate it into the predicates, we really can account
3136 : : just maximum of the possible paths. */
3137 : 135683709 : if (fbi.info)
3138 : 113748294 : will_be_nonconstant
3139 : 113748294 : = will_be_nonconstant_predicate (&fbi, info, params_summary,
3140 : : stmt, nonconstant_names);
3141 : : else
3142 : 21935415 : will_be_nonconstant = true;
3143 : 135683709 : if (this_time || this_size)
3144 : : {
3145 : 111226839 : sreal final_time = (sreal)this_time * freq;
3146 : 111226839 : prob = eliminated_by_inlining_prob (&fbi, stmt);
3147 : 111226839 : if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
3148 : 11 : fprintf (dump_file,
3149 : : "\t\t50%% will be eliminated by inlining\n");
3150 : 111226839 : if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
3151 : 18 : fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
3152 : :
3153 : 111226839 : ipa_predicate p = bb_predicate & will_be_nonconstant;
3154 : 111226839 : int parm = load_or_store_of_ptr_parameter (&fbi, stmt);
3155 : 111226839 : ipa_predicate sra_predicate = true;
3156 : 111226839 : if (parm != -1)
3157 : 14299716 : sra_predicate &= add_condition (info, params_summary, parm,
3158 : : ptr_type_node, NULL,
3159 : 7149858 : ipa_predicate::not_sra_candidate, NULL, 0);
3160 : :
3161 : : /* We can ignore statement when we proved it is never going
3162 : : to happen, but we cannot do that for call statements
3163 : : because edges are accounted specially. */
3164 : :
3165 : 222453678 : if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false)
3166 : : {
3167 : 110515730 : time += final_time;
3168 : 110515730 : size += this_size;
3169 : : }
3170 : :
3171 : : /* We account everything but the calls. Calls have their own
3172 : : size/time info attached to cgraph edges. This is necessary
3173 : : in order to make the cost disappear after inlining. */
3174 : 111226839 : if (!is_gimple_call (stmt))
3175 : : {
3176 : 89085643 : if (prob)
3177 : : {
3178 : 15029156 : ipa_predicate ip
3179 : 15029156 : = bb_predicate & ipa_predicate::not_inlined () & sra_predicate;
3180 : 30058312 : info->account_size_time (this_size * prob,
3181 : 15029156 : (final_time * prob) / 2, ip,
3182 : : p);
3183 : : }
3184 : 15029156 : if (prob != 2)
3185 : 164289204 : info->account_size_time (this_size * (2 - prob),
3186 : 82144602 : (final_time * (2 - prob) / 2),
3187 : 164289204 : bb_predicate & sra_predicate,
3188 : : p);
3189 : : }
3190 : :
3191 : 111226839 : if (!info->fp_expressions && fp_expression_p (stmt))
3192 : : {
3193 : 596835 : info->fp_expressions = true;
3194 : 596835 : if (dump_file)
3195 : 9 : fprintf (dump_file, " fp_expression set\n");
3196 : : }
3197 : : }
3198 : :
3199 : : /* For target specific information, we want to scan all statements
3200 : : rather than those statements with non-zero weights, to avoid
3201 : : missing to scan something interesting for target information,
3202 : : such as: internal function calls. */
3203 : 135683709 : if (scan_for_target_info)
3204 : 0 : scan_for_target_info =
3205 : 0 : targetm.target_option.update_ipa_fn_target_info
3206 : 0 : (info->target_info, stmt);
3207 : :
3208 : : /* Account cost of address calculations in the statements. */
3209 : 524506087 : for (unsigned int i = 0; i < gimple_num_ops (stmt); i++)
3210 : : {
3211 : 388822378 : for (tree op = gimple_op (stmt, i);
3212 : 748264412 : op && handled_component_p (op);
3213 : 46607772 : op = TREE_OPERAND (op, 0))
3214 : 46607772 : if ((TREE_CODE (op) == ARRAY_REF
3215 : 46607772 : || TREE_CODE (op) == ARRAY_RANGE_REF)
3216 : 46607772 : && TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
3217 : : {
3218 : 2334029 : ipa_predicate p = bb_predicate;
3219 : 2334029 : if (fbi.info)
3220 : 1821689 : p = p & will_be_nonconstant_expr_predicate
3221 : 1821689 : (&fbi, info, params_summary,
3222 : 1821689 : TREE_OPERAND (op, 1),
3223 : 1821689 : nonconstant_names);
3224 : 2334029 : if (p != false)
3225 : : {
3226 : 2328518 : time += freq;
3227 : 2328518 : size += 1;
3228 : 2328518 : if (dump_file)
3229 : 24 : fprintf (dump_file,
3230 : : "\t\tAccounting address calculation.\n");
3231 : 2328518 : info->account_size_time (ipa_fn_summary::size_scale,
3232 : : freq,
3233 : : bb_predicate,
3234 : : p);
3235 : : }
3236 : : }
3237 : : }
3238 : :
3239 : : }
3240 : : }
3241 : 7249422 : free (order);
3242 : :
3243 : 7249422 : if (nonconstant_names.exists () && !early)
3244 : : {
3245 : 1378437 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3246 : 1378437 : unsigned max_loop_predicates = opt_for_fn (node->decl,
3247 : : param_ipa_max_loop_predicates);
3248 : :
3249 : 1378437 : if (dump_file && (dump_flags & TDF_DETAILS))
3250 : 14 : flow_loops_dump (dump_file, NULL, 0);
3251 : 1378437 : scev_initialize ();
3252 : 4755091 : for (auto loop : loops_list (cfun, 0))
3253 : : {
3254 : 619780 : ipa_predicate loop_iterations = true;
3255 : 619780 : sreal header_freq;
3256 : 619780 : edge ex;
3257 : 619780 : unsigned int j;
3258 : 619780 : class tree_niter_desc niter_desc;
3259 : 619780 : if (!loop->header->aux)
3260 : 0 : continue;
3261 : :
3262 : 619780 : profile_count hdr_count = loop->header->count;
3263 : 619780 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3264 : :
3265 : 619780 : bb_predicate = *(ipa_predicate *)loop->header->aux;
3266 : 619780 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3267 : 1678388 : FOR_EACH_VEC_ELT (exits, j, ex)
3268 : 1058608 : if (number_of_iterations_exit (loop, ex, &niter_desc, false)
3269 : 1058608 : && !is_gimple_min_invariant (niter_desc.niter))
3270 : : {
3271 : 151400 : ipa_predicate will_be_nonconstant
3272 : 151400 : = will_be_nonconstant_expr_predicate (&fbi, info,
3273 : : params_summary,
3274 : : niter_desc.niter,
3275 : : nonconstant_names);
3276 : 151400 : if (will_be_nonconstant != true)
3277 : 61032 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3278 : 151400 : if (will_be_nonconstant != true
3279 : 212432 : && will_be_nonconstant != false)
3280 : 60476 : loop_iterations &= will_be_nonconstant;
3281 : : }
3282 : 619780 : add_freqcounting_predicate (&s->loop_iterations, loop_iterations,
3283 : : hdr_freq, max_loop_predicates);
3284 : 619780 : }
3285 : :
3286 : : /* To avoid quadratic behavior we analyze stride predicates only
3287 : : with respect to the containing loop. Thus we simply iterate
3288 : : over all defs in the outermost loop body. */
3289 : 1378437 : for (class loop *loop = loops_for_fn (cfun)->tree_root->inner;
3290 : 1864891 : loop != NULL; loop = loop->next)
3291 : : {
3292 : 486454 : ipa_predicate loop_stride = true;
3293 : 486454 : basic_block *body = get_loop_body (loop);
3294 : 486454 : profile_count hdr_count = loop->header->count;
3295 : 486454 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3296 : 2916567 : for (unsigned i = 0; i < loop->num_nodes; i++)
3297 : : {
3298 : 2430113 : gimple_stmt_iterator gsi;
3299 : 2430113 : if (!body[i]->aux)
3300 : 7 : continue;
3301 : :
3302 : 2430106 : bb_predicate = *(ipa_predicate *)body[i]->aux;
3303 : 15927748 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
3304 : 11067536 : gsi_next (&gsi))
3305 : : {
3306 : 11067536 : gimple *stmt = gsi_stmt (gsi);
3307 : :
3308 : 11067536 : if (!is_gimple_assign (stmt))
3309 : 10917467 : continue;
3310 : :
3311 : 5334386 : tree def = gimple_assign_lhs (stmt);
3312 : 5334386 : if (TREE_CODE (def) != SSA_NAME)
3313 : 1018248 : continue;
3314 : :
3315 : 4316138 : affine_iv iv;
3316 : 8632276 : if (!simple_iv (loop_containing_stmt (stmt),
3317 : : loop_containing_stmt (stmt),
3318 : : def, &iv, true)
3319 : 4316138 : || is_gimple_min_invariant (iv.step))
3320 : 4166069 : continue;
3321 : :
3322 : 150069 : ipa_predicate will_be_nonconstant
3323 : 150069 : = will_be_nonconstant_expr_predicate (&fbi, info,
3324 : : params_summary,
3325 : : iv.step,
3326 : : nonconstant_names);
3327 : 150069 : if (will_be_nonconstant != true)
3328 : 52865 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3329 : 150069 : if (will_be_nonconstant != true
3330 : 202934 : && will_be_nonconstant != false)
3331 : 33993 : loop_stride = loop_stride & will_be_nonconstant;
3332 : : }
3333 : : }
3334 : 486454 : add_freqcounting_predicate (&s->loop_strides, loop_stride,
3335 : : hdr_freq, max_loop_predicates);
3336 : 486454 : free (body);
3337 : : }
3338 : 1378437 : scev_finalize ();
3339 : : }
3340 : 63490837 : FOR_ALL_BB_FN (bb, my_function)
3341 : : {
3342 : 56241415 : edge e;
3343 : 56241415 : edge_iterator ei;
3344 : :
3345 : 56241415 : if (bb->aux)
3346 : 41699703 : edge_predicate_pool.remove ((ipa_predicate *)bb->aux);
3347 : 56241415 : bb->aux = NULL;
3348 : 119756587 : FOR_EACH_EDGE (e, ei, bb->succs)
3349 : : {
3350 : 63515172 : if (e->aux)
3351 : 2454615 : edge_predicate_pool.remove ((ipa_predicate *)e->aux);
3352 : 63515172 : e->aux = NULL;
3353 : : }
3354 : : }
3355 : 7249422 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3356 : 7249422 : ipa_size_summary *ss = ipa_size_summaries->get (node);
3357 : 7249422 : s->time = time;
3358 : 7249422 : ss->self_size = size;
3359 : 7249422 : nonconstant_names.release ();
3360 : 7249422 : ipa_release_body_info (&fbi);
3361 : 7249422 : if (opt_for_fn (node->decl, optimize))
3362 : : {
3363 : 6360442 : if (!early)
3364 : 1378437 : loop_optimizer_finalize ();
3365 : 4982005 : else if (!ipa_edge_args_sum)
3366 : 4981991 : ipa_free_all_node_params ();
3367 : 6360442 : free_dominance_info (CDI_DOMINATORS);
3368 : 6360442 : free_dominance_info (CDI_POST_DOMINATORS);
3369 : : }
3370 : 7249422 : if (dump_file)
3371 : : {
3372 : 229 : fprintf (dump_file, "\n");
3373 : 229 : ipa_dump_fn_summary (dump_file, node);
3374 : : }
3375 : 7249422 : }
3376 : :
3377 : :
3378 : : /* Compute function summary.
3379 : : EARLY is true when we compute parameters during early opts. */
3380 : :
3381 : : void
3382 : 7250745 : compute_fn_summary (struct cgraph_node *node, bool early)
3383 : : {
3384 : 7250745 : HOST_WIDE_INT self_stack_size;
3385 : 7250745 : struct cgraph_edge *e;
3386 : :
3387 : 7250745 : gcc_assert (!node->inlined_to);
3388 : :
3389 : 7250745 : if (!ipa_fn_summaries)
3390 : 213318 : ipa_fn_summary_alloc ();
3391 : :
3392 : : /* Create a new ipa_fn_summary. */
3393 : 7250745 : ((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node);
3394 : 7250745 : ipa_fn_summaries->remove (node);
3395 : 7250745 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
3396 : 7250745 : class ipa_size_summary *size_info = ipa_size_summaries->get_create (node);
3397 : :
3398 : : /* Estimate the stack size for the function if we're optimizing. */
3399 : 12722191 : self_stack_size = optimize && !node->thunk
3400 : 13611187 : ? estimated_stack_frame_size (node) : 0;
3401 : 7250745 : size_info->estimated_self_stack_size = self_stack_size;
3402 : 7250745 : info->estimated_stack_size = self_stack_size;
3403 : :
3404 : 7250745 : if (node->thunk)
3405 : : {
3406 : 1323 : ipa_call_summary *es = ipa_call_summaries->get_create (node->callees);
3407 : 1323 : ipa_predicate t = true;
3408 : :
3409 : 1323 : node->can_change_signature = false;
3410 : 1323 : es->call_stmt_size = eni_size_weights.call_cost;
3411 : 1323 : es->call_stmt_time = eni_time_weights.call_cost;
3412 : 3969 : info->account_size_time (ipa_fn_summary::size_scale
3413 : 1323 : * opt_for_fn (node->decl,
3414 : : param_uninlined_function_thunk_insns),
3415 : 1323 : opt_for_fn (node->decl,
3416 : : param_uninlined_function_thunk_time), t, t);
3417 : 1323 : t = ipa_predicate::not_inlined ();
3418 : 1323 : info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t);
3419 : 1323 : ipa_update_overall_fn_summary (node);
3420 : 1323 : size_info->self_size = size_info->size;
3421 : 1323 : if (stdarg_p (TREE_TYPE (node->decl)))
3422 : : {
3423 : 9 : info->inlinable = false;
3424 : 9 : node->callees->inline_failed = CIF_VARIADIC_THUNK;
3425 : : }
3426 : : else
3427 : 1314 : info->inlinable = true;
3428 : : }
3429 : : else
3430 : : {
3431 : : /* Even is_gimple_min_invariant rely on current_function_decl. */
3432 : 7249422 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3433 : :
3434 : : /* During IPA profile merging we may be called w/o virtual SSA form
3435 : : built. */
3436 : 7249422 : update_ssa (TODO_update_ssa_only_virtuals);
3437 : :
3438 : : /* Can this function be inlined at all? */
3439 : 7249422 : if (!opt_for_fn (node->decl, optimize)
3440 : 8138402 : && !lookup_attribute ("always_inline",
3441 : 888980 : DECL_ATTRIBUTES (node->decl)))
3442 : 822084 : info->inlinable = false;
3443 : : else
3444 : 6427338 : info->inlinable = tree_inlinable_function_p (node->decl);
3445 : :
3446 : 7249422 : bool no_signature = false;
3447 : :
3448 : : /* Don't allow signature changes for functions which have
3449 : : [[gnu::musttail]] or [[clang::musttail]] calls. Sometimes
3450 : : (more often on targets which pass everything on the stack)
3451 : : signature changes can result in tail calls being impossible
3452 : : even when without the signature changes they would be ok.
3453 : : See PR121023. */
3454 : 7249422 : if (cfun->has_musttail)
3455 : : {
3456 : 1410 : if (dump_file)
3457 : 0 : fprintf (dump_file, "No signature change:"
3458 : : " function has calls with musttail attribute.\n");
3459 : : no_signature = true;
3460 : : }
3461 : :
3462 : : /* Type attributes can use parameter indices to describe them.
3463 : : Special case fn spec since we can safely preserve them in
3464 : : modref summaries. */
3465 : 7249422 : for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl));
3466 : 7631426 : list && !no_signature; list = TREE_CHAIN (list))
3467 : 382004 : if (!ipa_param_adjustments::type_attribute_allowed_p
3468 : 382004 : (get_attribute_name (list)))
3469 : : {
3470 : 156890 : if (dump_file)
3471 : : {
3472 : 0 : fprintf (dump_file, "No signature change:"
3473 : : " function type has unhandled attribute %s.\n",
3474 : 0 : IDENTIFIER_POINTER (get_attribute_name (list)));
3475 : : }
3476 : : no_signature = true;
3477 : : }
3478 : 7249422 : for (tree parm = DECL_ARGUMENTS (node->decl);
3479 : 21583549 : parm && !no_signature; parm = DECL_CHAIN (parm))
3480 : 14334127 : if (variably_modified_type_p (TREE_TYPE (parm), node->decl))
3481 : : {
3482 : 15061 : if (dump_file)
3483 : : {
3484 : 0 : fprintf (dump_file, "No signature change:"
3485 : : " has parameter with variably modified type.\n");
3486 : : }
3487 : : no_signature = true;
3488 : : }
3489 : :
3490 : : /* Likewise for #pragma omp declare simd functions or functions
3491 : : with simd attribute. */
3492 : 7249422 : if (no_signature
3493 : 14325483 : || lookup_attribute ("omp declare simd",
3494 : 7076061 : DECL_ATTRIBUTES (node->decl)))
3495 : 175037 : node->can_change_signature = false;
3496 : : else
3497 : : {
3498 : : /* Otherwise, inlinable functions always can change signature. */
3499 : 7074385 : if (info->inlinable)
3500 : 5734283 : node->can_change_signature = true;
3501 : : else
3502 : : {
3503 : : /* Functions calling builtin_apply cannot change signature. */
3504 : 5279680 : for (e = node->callees; e; e = e->next_callee)
3505 : : {
3506 : 3972016 : tree cdecl = e->callee->decl;
3507 : 3972016 : if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS,
3508 : : BUILT_IN_VA_START))
3509 : : break;
3510 : : }
3511 : 1340102 : node->can_change_signature = !e;
3512 : : }
3513 : : }
3514 : 7249422 : analyze_function_body (node, early);
3515 : 7249422 : pop_cfun ();
3516 : : }
3517 : :
3518 : : /* Inlining characteristics are maintained by the cgraph_mark_inline. */
3519 : 7250745 : size_info->size = size_info->self_size;
3520 : 7250745 : info->estimated_stack_size = size_info->estimated_self_stack_size;
3521 : :
3522 : : /* Code above should compute exactly the same result as
3523 : : ipa_update_overall_fn_summary except for case when speculative
3524 : : edges are present since these are accounted to size but not
3525 : : self_size. Do not compare time since different order the roundoff
3526 : : errors result in slight changes. */
3527 : 7250745 : ipa_update_overall_fn_summary (node);
3528 : 7250745 : if (flag_checking)
3529 : : {
3530 : 7734048 : for (e = node->indirect_calls; e; e = e->next_callee)
3531 : 483385 : if (e->speculative)
3532 : : break;
3533 : 7250663 : gcc_assert (e || size_info->size == size_info->self_size);
3534 : : }
3535 : 7250745 : }
3536 : :
3537 : :
3538 : : /* Compute parameters of functions used by inliner using
3539 : : current_function_decl. */
3540 : :
3541 : : static unsigned int
3542 : 5810459 : compute_fn_summary_for_current (void)
3543 : : {
3544 : 5810459 : compute_fn_summary (cgraph_node::get (current_function_decl), true);
3545 : 5810459 : return 0;
3546 : : }
3547 : :
3548 : : /* Estimate benefit devirtualizing indirect edge IE and return true if it can
3549 : : be devirtualized and inlined, provided m_known_vals, m_known_contexts and
3550 : : m_known_aggs in AVALS. Return false straight away if AVALS is NULL. */
3551 : :
3552 : : static bool
3553 : 5284871 : estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3554 : : int *size, int *time,
3555 : : ipa_call_arg_values *avals)
3556 : : {
3557 : 5284871 : tree target;
3558 : 5284871 : struct cgraph_node *callee;
3559 : 5284871 : class ipa_fn_summary *isummary;
3560 : 5284871 : enum availability avail;
3561 : 5284871 : bool speculative;
3562 : :
3563 : 5284871 : if (!avals
3564 : 6718391 : || (!avals->m_known_vals.length() && !avals->m_known_contexts.length ()))
3565 : : return false;
3566 : 1647752 : if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3567 : : return false;
3568 : :
3569 : 1641612 : target = ipa_get_indirect_edge_target
3570 : 1641612 : (ie->callee ? ie->speculative_call_indirect_edge () : ie,
3571 : : avals, &speculative);
3572 : 1641612 : if (!target || speculative)
3573 : : return false;
3574 : :
3575 : : /* If this is speculative call, turn its cost into 0; we will account
3576 : : the call when processing the indirect call. */
3577 : 234468 : if (ie->callee)
3578 : : {
3579 : 11678 : gcc_checking_assert (ie->speculative && *size > 0);
3580 : 11678 : *size = 0;
3581 : 11678 : *time = 0;
3582 : : }
3583 : : else
3584 : : {
3585 : : /* Account for difference in cost between indirect and direct calls. */
3586 : 222790 : *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3587 : 222790 : *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3588 : : }
3589 : 234468 : gcc_checking_assert (*time >= 0);
3590 : 234468 : gcc_checking_assert (*size >= 0);
3591 : :
3592 : 234468 : callee = cgraph_node::get (target);
3593 : 234468 : if (!callee || !callee->definition)
3594 : : return false;
3595 : 214263 : callee = callee->function_symbol (&avail);
3596 : 214263 : if (avail < AVAIL_AVAILABLE)
3597 : : return false;
3598 : 214237 : isummary = ipa_fn_summaries->get (callee);
3599 : 214237 : if (isummary == NULL)
3600 : : return false;
3601 : :
3602 : 214232 : return isummary->inlinable;
3603 : : }
3604 : :
3605 : : /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3606 : : handle edge E with probability PROB. Set HINTS accordingly if edge may be
3607 : : devirtualized. AVALS, if non-NULL, describes the context of the call site
3608 : : as far as values of parameters are concerened. */
3609 : :
3610 : : static inline void
3611 : 196376928 : estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3612 : : sreal *time, ipa_call_arg_values *avals,
3613 : : ipa_hints *hints)
3614 : : {
3615 : 196376928 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3616 : 196376928 : int call_size = es->call_stmt_size;
3617 : 196376928 : int call_time = es->call_stmt_time;
3618 : 196376928 : int cur_size;
3619 : :
3620 : 191955207 : if ((!e->callee || e->speculative)
3621 : 197240078 : && estimate_edge_devirt_benefit (e, &call_size, &call_time, avals))
3622 : : {
3623 : 213899 : if (hints && e->maybe_hot_p ())
3624 : 203022 : *hints |= INLINE_HINT_indirect_call;
3625 : : }
3626 : 196376928 : cur_size = call_size * ipa_fn_summary::size_scale;
3627 : 196376928 : *size += cur_size;
3628 : 196376928 : if (min_size)
3629 : 27800383 : *min_size += cur_size;
3630 : 196376928 : if (time)
3631 : 186917778 : *time += ((sreal)call_time) * e->sreal_frequency ();
3632 : 196376928 : }
3633 : :
3634 : :
3635 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3636 : : calls in NODE. POSSIBLE_TRUTHS and AVALS describe the context of the call
3637 : : site.
3638 : :
3639 : : Helper for estimate_calls_size_and_time which does the same but
3640 : : (in most cases) faster. */
3641 : :
3642 : : static void
3643 : 66579491 : estimate_calls_size_and_time_1 (struct cgraph_node *node, int *size,
3644 : : int *min_size, sreal *time,
3645 : : ipa_hints *hints,
3646 : : clause_t possible_truths,
3647 : : ipa_call_arg_values *avals)
3648 : : {
3649 : 66579491 : struct cgraph_edge *e;
3650 : 315885132 : for (e = node->callees; e; e = e->next_callee)
3651 : : {
3652 : 249305641 : if (!e->inline_failed)
3653 : : {
3654 : 46942753 : gcc_checking_assert (!ipa_call_summaries->get (e));
3655 : 46942753 : estimate_calls_size_and_time_1 (e->callee, size, min_size, time,
3656 : : hints, possible_truths, avals);
3657 : :
3658 : 46942753 : continue;
3659 : : }
3660 : 202362888 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3661 : :
3662 : : /* Do not care about zero sized builtins. */
3663 : 202362888 : if (!es->call_stmt_size)
3664 : : {
3665 : 18691255 : gcc_checking_assert (!es->call_stmt_time);
3666 : 18691255 : continue;
3667 : : }
3668 : 183671633 : if (!es->predicate
3669 : 183671633 : || es->predicate->evaluate (possible_truths))
3670 : : {
3671 : : /* Predicates of calls shall not use NOT_CHANGED codes,
3672 : : so we do not need to compute probabilities. */
3673 : 181396338 : estimate_edge_size_and_time (e, size,
3674 : 181396338 : es->predicate ? NULL : min_size,
3675 : : time, avals, hints);
3676 : : }
3677 : : }
3678 : 70655222 : for (e = node->indirect_calls; e; e = e->next_callee)
3679 : : {
3680 : 4075731 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3681 : 4075731 : if (!es->predicate
3682 : 4075731 : || es->predicate->evaluate (possible_truths))
3683 : 4050217 : estimate_edge_size_and_time (e, size,
3684 : 4050217 : es->predicate ? NULL : min_size,
3685 : : time, avals, hints);
3686 : : }
3687 : 66579491 : }
3688 : :
3689 : : /* Populate sum->call_size_time_table for edges from NODE. */
3690 : :
3691 : : static void
3692 : 2998074 : summarize_calls_size_and_time (struct cgraph_node *node,
3693 : : ipa_fn_summary *sum)
3694 : : {
3695 : 2998074 : struct cgraph_edge *e;
3696 : 14006742 : for (e = node->callees; e; e = e->next_callee)
3697 : : {
3698 : 11008668 : if (!e->inline_failed)
3699 : : {
3700 : 1292451 : gcc_checking_assert (!ipa_call_summaries->get (e));
3701 : 1292451 : summarize_calls_size_and_time (e->callee, sum);
3702 : 1292451 : continue;
3703 : : }
3704 : 9716217 : int size = 0;
3705 : 9716217 : sreal time = 0;
3706 : :
3707 : 9716217 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3708 : :
3709 : 9716217 : ipa_predicate pred = true;
3710 : 9716217 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3711 : :
3712 : 9716217 : if (es->predicate)
3713 : 1804345 : pred = *es->predicate;
3714 : 9716217 : sum->account_size_time (size, time, pred, pred, true);
3715 : : }
3716 : 3369578 : for (e = node->indirect_calls; e; e = e->next_callee)
3717 : : {
3718 : 371504 : int size = 0;
3719 : 371504 : sreal time = 0;
3720 : :
3721 : 371504 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3722 : 371504 : ipa_predicate pred = true;
3723 : 371504 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3724 : :
3725 : 371504 : if (es->predicate)
3726 : 136926 : pred = *es->predicate;
3727 : 371504 : sum->account_size_time (size, time, pred, pred, true);
3728 : : }
3729 : 2998074 : }
3730 : :
3731 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3732 : : calls in NODE. POSSIBLE_TRUTHS and AVALS (the latter if non-NULL) describe
3733 : : context of the call site. */
3734 : :
3735 : : static void
3736 : 19636762 : estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3737 : : int *min_size, sreal *time,
3738 : : ipa_hints *hints,
3739 : : clause_t possible_truths,
3740 : : ipa_call_arg_values *avals)
3741 : : {
3742 : 19636762 : class ipa_fn_summary *sum = ipa_fn_summaries->get (node);
3743 : 19636762 : bool use_table = true;
3744 : :
3745 : 19636762 : gcc_assert (node->callees || node->indirect_calls);
3746 : :
3747 : : /* During early inlining we do not calculate info for very
3748 : : large functions and thus there is no need for producing
3749 : : summaries. */
3750 : 19636762 : if (!ipa_node_params_sum)
3751 : : use_table = false;
3752 : : /* Do not calculate summaries for simple wrappers; it is waste
3753 : : of memory. */
3754 : 10612586 : else if (node->callees && !node->indirect_calls
3755 : 9816848 : && node->callees->inline_failed && !node->callees->next_callee)
3756 : : use_table = false;
3757 : : /* If there is an indirect edge that may be optimized, we need
3758 : : to go the slow way. */
3759 : 8441121 : else if (avals
3760 : 8441121 : && (avals->m_known_vals.length ()
3761 : 3256965 : || avals->m_known_contexts.length ()
3762 : 3073794 : || avals->m_known_aggs.length ()))
3763 : : {
3764 : 3834998 : ipa_node_params *params_summary = ipa_node_params_sum->get (node);
3765 : 3834998 : unsigned int nargs = params_summary
3766 : 3834998 : ? ipa_get_param_count (params_summary) : 0;
3767 : :
3768 : 12901437 : for (unsigned int i = 0; i < nargs && use_table; i++)
3769 : : {
3770 : 9066439 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3771 : 9066439 : && (avals->safe_sval_at (i)
3772 : 310831 : || (ipa_argagg_value_list (avals).value_for_index_p (i))))
3773 : : use_table = false;
3774 : 8962443 : else if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3775 : 8962443 : && (avals->m_known_contexts.length () > i
3776 : 9266765 : && !avals->m_known_contexts[i].useless_p ()))
3777 : : use_table = false;
3778 : : }
3779 : : }
3780 : :
3781 : : /* Fast path is via the call size time table. */
3782 : 3834998 : if (use_table)
3783 : : {
3784 : : /* Build summary if it is absent. */
3785 : 8137099 : if (!sum->call_size_time_table.length ())
3786 : : {
3787 : 862971 : ipa_predicate true_pred = true;
3788 : 862971 : sum->account_size_time (0, 0, true_pred, true_pred, true);
3789 : 862971 : summarize_calls_size_and_time (node, sum);
3790 : : }
3791 : :
3792 : 8137099 : int old_size = *size;
3793 : 8137099 : sreal old_time = time ? *time : 0;
3794 : :
3795 : 8137099 : if (min_size)
3796 : 8137099 : *min_size += sum->call_size_time_table[0].size;
3797 : :
3798 : : unsigned int i;
3799 : : size_time_entry *e;
3800 : :
3801 : : /* Walk the table and account sizes and times. */
3802 : 21920204 : for (i = 0; sum->call_size_time_table.iterate (i, &e);
3803 : : i++)
3804 : 13783105 : if (e->exec_predicate.evaluate (possible_truths))
3805 : : {
3806 : 12689661 : *size += e->size;
3807 : 12689661 : if (time)
3808 : 10593848 : *time += e->time;
3809 : : }
3810 : :
3811 : : /* Be careful and see if both methods agree. */
3812 : 24 : if ((flag_checking || dump_file)
3813 : : /* Do not try to sanity check when we know we lost some
3814 : : precision. */
3815 : 8137099 : && sum->call_size_time_table.length ()
3816 : : < ipa_fn_summary::max_size_time_table_size)
3817 : : {
3818 : 8137075 : estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL,
3819 : : possible_truths, avals);
3820 : 8137075 : gcc_assert (*size == old_size);
3821 : 14907078 : if (time && (*time - old_time > 1 || *time - old_time < -1)
3822 : 8137088 : && dump_file)
3823 : 0 : fprintf (dump_file, "Time mismatch in call summary %f!=%f\n",
3824 : : old_time.to_double (),
3825 : : time->to_double ());
3826 : : }
3827 : : }
3828 : : /* Slow path by walking all edges. */
3829 : : else
3830 : 11499663 : estimate_calls_size_and_time_1 (node, size, min_size, time, hints,
3831 : : possible_truths, avals);
3832 : 19636762 : }
3833 : :
3834 : : /* Main constructor for ipa call context. Memory allocation of ARG_VALUES
3835 : : is owned by the caller. INLINE_PARAM_SUMMARY is also owned by the
3836 : : caller. */
3837 : :
3838 : 19157129 : ipa_call_context::ipa_call_context (cgraph_node *node, clause_t possible_truths,
3839 : : clause_t nonspec_possible_truths,
3840 : : vec<inline_param_summary>
3841 : : inline_param_summary,
3842 : 19157129 : ipa_auto_call_arg_values *arg_values)
3843 : 19157129 : : m_node (node), m_possible_truths (possible_truths),
3844 : 19157129 : m_nonspec_possible_truths (nonspec_possible_truths),
3845 : 19157129 : m_inline_param_summary (inline_param_summary),
3846 : 19157129 : m_avals (arg_values)
3847 : : {
3848 : 19157129 : }
3849 : :
3850 : : /* Set THIS to be a duplicate of CTX. Copy all relevant info. */
3851 : :
3852 : : void
3853 : 1909784 : ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx)
3854 : : {
3855 : 1909784 : m_node = ctx.m_node;
3856 : 1909784 : m_possible_truths = ctx.m_possible_truths;
3857 : 1909784 : m_nonspec_possible_truths = ctx.m_nonspec_possible_truths;
3858 : 1909784 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3859 : 1909784 : unsigned int nargs = params_summary
3860 : 1909784 : ? ipa_get_param_count (params_summary) : 0;
3861 : :
3862 : 1909784 : m_inline_param_summary = vNULL;
3863 : : /* Copy the info only if there is at least one useful entry. */
3864 : 1909784 : if (ctx.m_inline_param_summary.exists ())
3865 : : {
3866 : 1701416 : unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs);
3867 : :
3868 : 3902995 : for (unsigned int i = 0; i < n; i++)
3869 : 3208815 : if (ipa_is_param_used_by_ipa_predicates (params_summary, i)
3870 : 4504279 : && !ctx.m_inline_param_summary[i].useless_p ())
3871 : : {
3872 : 1007236 : m_inline_param_summary
3873 : 1007236 : = ctx.m_inline_param_summary.copy ();
3874 : 1007236 : break;
3875 : : }
3876 : : }
3877 : 1909784 : m_avals.m_known_vals = vNULL;
3878 : 1909784 : if (ctx.m_avals.m_known_vals.exists ())
3879 : : {
3880 : 1909784 : unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs);
3881 : :
3882 : 4617963 : for (unsigned int i = 0; i < n; i++)
3883 : 2739040 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3884 : 2739040 : && ctx.m_avals.m_known_vals[i])
3885 : : {
3886 : 30861 : m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy ();
3887 : 30861 : break;
3888 : : }
3889 : : }
3890 : :
3891 : 1909784 : m_avals.m_known_contexts = vNULL;
3892 : 1909784 : if (ctx.m_avals.m_known_contexts.exists ())
3893 : : {
3894 : 1909784 : unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs);
3895 : :
3896 : 1910982 : for (unsigned int i = 0; i < n; i++)
3897 : 26892 : if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3898 : 26892 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3899 : : {
3900 : 25694 : m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy ();
3901 : 25694 : break;
3902 : : }
3903 : : }
3904 : :
3905 : 1909784 : m_avals.m_known_aggs = vNULL;
3906 : 1909784 : if (ctx.m_avals.m_known_aggs.exists ())
3907 : : {
3908 : 1909784 : const ipa_argagg_value_list avl (&ctx.m_avals);
3909 : 6499990 : for (unsigned int i = 0; i < nargs; i++)
3910 : 4594615 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3911 : 4594615 : && avl.value_for_index_p (i))
3912 : : {
3913 : 4409 : m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy ();
3914 : 4409 : break;
3915 : : }
3916 : : }
3917 : :
3918 : 1909784 : m_avals.m_known_value_ranges = vNULL;
3919 : 1909784 : }
3920 : :
3921 : : /* Release memory used by known_vals/contexts/aggs vectors. and
3922 : : inline_param_summary. */
3923 : :
3924 : : void
3925 : 3103305 : ipa_cached_call_context::release ()
3926 : : {
3927 : : /* See if context is initialized at first place. */
3928 : 3103305 : if (!m_node)
3929 : : return;
3930 : 1909784 : m_avals.m_known_aggs.release ();
3931 : 1909784 : m_avals.m_known_vals.release ();
3932 : 1909784 : m_avals.m_known_contexts.release ();
3933 : 1909784 : m_inline_param_summary.release ();
3934 : : }
3935 : :
3936 : : /* Return true if CTX describes the same call context as THIS. */
3937 : :
3938 : : bool
3939 : 6743252 : ipa_call_context::equal_to (const ipa_call_context &ctx)
3940 : : {
3941 : 6743252 : if (m_node != ctx.m_node
3942 : 5549731 : || m_possible_truths != ctx.m_possible_truths
3943 : 4956734 : || m_nonspec_possible_truths != ctx.m_nonspec_possible_truths)
3944 : : return false;
3945 : :
3946 : 4956734 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3947 : 4956734 : unsigned int nargs = params_summary
3948 : 4956734 : ? ipa_get_param_count (params_summary) : 0;
3949 : :
3950 : 4956734 : if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ())
3951 : : {
3952 : 14899991 : for (unsigned int i = 0; i < nargs; i++)
3953 : : {
3954 : 10270169 : if (!ipa_is_param_used_by_ipa_predicates (params_summary, i))
3955 : 3267008 : continue;
3956 : 7003161 : if (i >= m_inline_param_summary.length ()
3957 : 4883609 : || m_inline_param_summary[i].useless_p ())
3958 : : {
3959 : 3042144 : if (i < ctx.m_inline_param_summary.length ()
3960 : 3042144 : && !ctx.m_inline_param_summary[i].useless_p ())
3961 : : return false;
3962 : 2998956 : continue;
3963 : : }
3964 : 3961017 : if (i >= ctx.m_inline_param_summary.length ()
3965 : 3961014 : || ctx.m_inline_param_summary[i].useless_p ())
3966 : : {
3967 : 42393 : if (i < m_inline_param_summary.length ()
3968 : 42393 : && !m_inline_param_summary[i].useless_p ())
3969 : : return false;
3970 : 0 : continue;
3971 : : }
3972 : 3918624 : if (!m_inline_param_summary[i].equal_to
3973 : 3918624 : (ctx.m_inline_param_summary[i]))
3974 : : return false;
3975 : : }
3976 : : }
3977 : 4847204 : if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ())
3978 : : {
3979 : 14920574 : for (unsigned int i = 0; i < nargs; i++)
3980 : : {
3981 : 10083713 : if (!ipa_is_param_used_by_indirect_call (params_summary, i))
3982 : 9841321 : continue;
3983 : 242392 : if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i])
3984 : : {
3985 : 181459 : if (i < ctx.m_avals.m_known_vals.length ()
3986 : 181459 : && ctx.m_avals.m_known_vals[i])
3987 : : return false;
3988 : 181400 : continue;
3989 : : }
3990 : 60933 : if (i >= ctx.m_avals.m_known_vals.length ()
3991 : 60933 : || !ctx.m_avals.m_known_vals[i])
3992 : : {
3993 : : if (i < m_avals.m_known_vals.length () && m_avals.m_known_vals[i])
3994 : : return false;
3995 : : continue;
3996 : : }
3997 : 60882 : if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i])
3998 : : return false;
3999 : : }
4000 : : }
4001 : 4836861 : if (m_avals.m_known_contexts.exists ()
4002 : 4836861 : || ctx.m_avals.m_known_contexts.exists ())
4003 : : {
4004 : 14904967 : for (unsigned int i = 0; i < nargs; i++)
4005 : : {
4006 : 10069980 : if (!ipa_is_param_used_by_polymorphic_call (params_summary, i))
4007 : 9901975 : continue;
4008 : 168005 : if (i >= m_avals.m_known_contexts.length ()
4009 : 166989 : || m_avals.m_known_contexts[i].useless_p ())
4010 : : {
4011 : 1016 : if (i < ctx.m_avals.m_known_contexts.length ()
4012 : 1016 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
4013 : : return false;
4014 : 1008 : continue;
4015 : : }
4016 : 166989 : if (i >= ctx.m_avals.m_known_contexts.length ()
4017 : 166989 : || ctx.m_avals.m_known_contexts[i].useless_p ())
4018 : : {
4019 : 8 : if (i < m_avals.m_known_contexts.length ()
4020 : 1909792 : && !m_avals.m_known_contexts[i].useless_p ())
4021 : : return false;
4022 : 0 : continue;
4023 : : }
4024 : 166981 : if (!m_avals.m_known_contexts[i].equal_to
4025 : 166981 : (ctx.m_avals.m_known_contexts[i]))
4026 : : return false;
4027 : : }
4028 : : }
4029 : 4834987 : if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ())
4030 : : {
4031 : : unsigned i = 0, j = 0;
4032 : 10999600 : while (i < m_avals.m_known_aggs.length ()
4033 : 5498621 : || j < ctx.m_avals.m_known_aggs.length ())
4034 : : {
4035 : 665153 : if (i >= m_avals.m_known_aggs.length ())
4036 : : {
4037 : 656161 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4038 : 656161 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4039 : : return false;
4040 : 655495 : j++;
4041 : 655495 : continue;
4042 : 655495 : }
4043 : 8992 : if (j >= ctx.m_avals.m_known_aggs.length ())
4044 : : {
4045 : 684 : int idx1 = m_avals.m_known_aggs[i].index;
4046 : 684 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4047 : : return false;
4048 : 4 : i++;
4049 : 4 : continue;
4050 : 4 : }
4051 : :
4052 : 8308 : int idx1 = m_avals.m_known_aggs[i].index;
4053 : 8308 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4054 : 8308 : if (idx1 < idx2)
4055 : : {
4056 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4057 : : return false;
4058 : 0 : i++;
4059 : 0 : continue;
4060 : : }
4061 : 8308 : if (idx1 > idx2)
4062 : : {
4063 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4064 : : return false;
4065 : 0 : j++;
4066 : 0 : continue;
4067 : : }
4068 : 8308 : if (!ipa_is_param_used_by_indirect_call (params_summary, idx1))
4069 : : {
4070 : 245 : i++;
4071 : 245 : j++;
4072 : 245 : continue;
4073 : : }
4074 : :
4075 : 8063 : if ((m_avals.m_known_aggs[i].unit_offset
4076 : 8063 : != ctx.m_avals.m_known_aggs[j].unit_offset)
4077 : 8055 : || (m_avals.m_known_aggs[i].by_ref
4078 : 8055 : != ctx.m_avals.m_known_aggs[j].by_ref)
4079 : 16118 : || !operand_equal_p (m_avals.m_known_aggs[i].value,
4080 : 8055 : ctx.m_avals.m_known_aggs[j].value))
4081 : 173 : return false;
4082 : 7890 : i++;
4083 : 7890 : j++;
4084 : : }
4085 : : }
4086 : : return true;
4087 : : }
4088 : :
4089 : : /* Fill in the selected fields in ESTIMATES with value estimated for call in
4090 : : this context. Always compute size and min_size. Only compute time and
4091 : : nonspecialized_time if EST_TIMES is true. Only compute hints if EST_HINTS
4092 : : is true. */
4093 : :
4094 : : void
4095 : 19117038 : ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates,
4096 : : bool est_times, bool est_hints)
4097 : : {
4098 : 19117038 : class ipa_fn_summary *info = ipa_fn_summaries->get (m_node);
4099 : 19117038 : size_time_entry *e;
4100 : 19117038 : int size = 0;
4101 : 19117038 : sreal time = 0;
4102 : 19117038 : int min_size = 0;
4103 : 19117038 : ipa_hints hints = 0;
4104 : 19117038 : sreal loops_with_known_iterations = 0;
4105 : 19117038 : sreal loops_with_known_strides = 0;
4106 : 19117038 : int i;
4107 : :
4108 : 19117038 : if (dump_file && (dump_flags & TDF_DETAILS))
4109 : : {
4110 : 1273 : bool found = false;
4111 : 1273 : fprintf (dump_file, " Estimating body: %s\n"
4112 : : " Known to be false: ", m_node->dump_name ());
4113 : :
4114 : 3926 : for (i = ipa_predicate::not_inlined_condition;
4115 : 7852 : i < (ipa_predicate::first_dynamic_condition
4116 : 6294 : + (int) vec_safe_length (info->conds)); i++)
4117 : 2653 : if (!(m_possible_truths & (1 << i)))
4118 : : {
4119 : 1660 : if (found)
4120 : 435 : fprintf (dump_file, ", ");
4121 : 1660 : found = true;
4122 : 1660 : dump_condition (dump_file, info->conds, i);
4123 : : }
4124 : : }
4125 : :
4126 : 19117038 : if (m_node->callees || m_node->indirect_calls)
4127 : 24700420 : estimate_calls_size_and_time (m_node, &size, &min_size,
4128 : : est_times ? &time : NULL,
4129 : : est_hints ? &hints : NULL, m_possible_truths,
4130 : : &m_avals);
4131 : :
4132 : 19117038 : sreal nonspecialized_time = time;
4133 : :
4134 : 19117038 : min_size += info->size_time_table[0].size;
4135 : 130463205 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4136 : : {
4137 : 111346167 : bool exec = e->exec_predicate.evaluate (m_nonspec_possible_truths);
4138 : :
4139 : : /* Because predicates are conservative, it can happen that nonconst is 1
4140 : : but exec is 0. */
4141 : 111346167 : if (exec)
4142 : : {
4143 : 108446582 : bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths);
4144 : :
4145 : 108446582 : gcc_checking_assert (e->time >= 0);
4146 : 108446582 : gcc_checking_assert (time >= 0);
4147 : :
4148 : : /* We compute specialized size only because size of nonspecialized
4149 : : copy is context independent.
4150 : :
4151 : : The difference between nonspecialized execution and specialized is
4152 : : that nonspecialized is not going to have optimized out computations
4153 : : known to be constant in a specialized setting. */
4154 : 108446582 : if (nonconst)
4155 : 53821610 : size += e->size;
4156 : 108446582 : if (!est_times)
4157 : 57591451 : continue;
4158 : 50855131 : nonspecialized_time += e->time;
4159 : 50855131 : if (!nonconst)
4160 : : ;
4161 : 23711845 : else if (!m_inline_param_summary.exists ())
4162 : : {
4163 : 1601434 : if (nonconst)
4164 : 1601434 : time += e->time;
4165 : : }
4166 : : else
4167 : : {
4168 : 22110411 : int prob = e->nonconst_predicate.probability
4169 : 22110411 : (info->conds, m_possible_truths,
4170 : : m_inline_param_summary);
4171 : 22110411 : gcc_checking_assert (prob >= 0);
4172 : 22110411 : gcc_checking_assert (prob <= REG_BR_PROB_BASE);
4173 : 22110411 : if (prob == REG_BR_PROB_BASE)
4174 : 19031150 : time += e->time;
4175 : : else
4176 : 3079261 : time += e->time * prob / REG_BR_PROB_BASE;
4177 : : }
4178 : 50855131 : gcc_checking_assert (time >= 0);
4179 : : }
4180 : : }
4181 : 19117038 : gcc_checking_assert (info->size_time_table[0].exec_predicate == true);
4182 : 19117038 : gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true);
4183 : 19117038 : gcc_checking_assert (min_size >= 0);
4184 : 19117038 : gcc_checking_assert (size >= 0);
4185 : 19117038 : gcc_checking_assert (time >= 0);
4186 : : /* nonspecialized_time should be always bigger than specialized time.
4187 : : Roundoff issues however may get into the way. */
4188 : 19117038 : gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1);
4189 : :
4190 : : /* Roundoff issues may make specialized time bigger than nonspecialized
4191 : : time. We do not really want that to happen because some heuristics
4192 : : may get confused by seeing negative speedups. */
4193 : 19117038 : if (time > nonspecialized_time)
4194 : 0 : time = nonspecialized_time;
4195 : :
4196 : 19117038 : if (est_hints)
4197 : : {
4198 : 6882207 : if (info->scc_no)
4199 : 200954 : hints |= INLINE_HINT_in_scc;
4200 : 6882207 : if (DECL_DECLARED_INLINE_P (m_node->decl))
4201 : 4191777 : hints |= INLINE_HINT_declared_inline;
4202 : 6882207 : if (info->builtin_constant_p_parms.length ()
4203 : 11690 : && DECL_DECLARED_INLINE_P (m_node->decl))
4204 : 11634 : hints |= INLINE_HINT_builtin_constant_p;
4205 : :
4206 : : ipa_freqcounting_predicate *fcp;
4207 : 7518757 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
4208 : 636550 : if (!fcp->predicate->evaluate (m_possible_truths))
4209 : : {
4210 : 455609 : hints |= INLINE_HINT_loop_iterations;
4211 : 455609 : loops_with_known_iterations += fcp->freq;
4212 : : }
4213 : 6882207 : estimates->loops_with_known_iterations = loops_with_known_iterations;
4214 : :
4215 : 7267460 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
4216 : 385253 : if (!fcp->predicate->evaluate (m_possible_truths))
4217 : : {
4218 : 369147 : hints |= INLINE_HINT_loop_stride;
4219 : 369147 : loops_with_known_strides += fcp->freq;
4220 : : }
4221 : 6882207 : estimates->loops_with_known_strides = loops_with_known_strides;
4222 : : }
4223 : :
4224 : 19117038 : size = RDIV (size, ipa_fn_summary::size_scale);
4225 : 19117038 : min_size = RDIV (min_size, ipa_fn_summary::size_scale);
4226 : :
4227 : 19117038 : if (dump_file && (dump_flags & TDF_DETAILS))
4228 : : {
4229 : 1273 : fprintf (dump_file, "\n size:%i", (int) size);
4230 : 1273 : if (est_times)
4231 : 1036 : fprintf (dump_file, " time:%f nonspec time:%f",
4232 : : time.to_double (), nonspecialized_time.to_double ());
4233 : 1273 : if (est_hints)
4234 : 1036 : fprintf (dump_file, " loops with known iterations:%f "
4235 : : "known strides:%f", loops_with_known_iterations.to_double (),
4236 : : loops_with_known_strides.to_double ());
4237 : 1273 : fprintf (dump_file, "\n");
4238 : : }
4239 : 19117038 : if (est_times)
4240 : : {
4241 : 6882207 : estimates->time = time;
4242 : 6882207 : estimates->nonspecialized_time = nonspecialized_time;
4243 : : }
4244 : 19117038 : estimates->size = size;
4245 : 19117038 : estimates->min_size = min_size;
4246 : 19117038 : if (est_hints)
4247 : 6882207 : estimates->hints = hints;
4248 : 19117038 : return;
4249 : : }
4250 : :
4251 : :
4252 : : /* Estimate size and time needed to execute callee of EDGE assuming that
4253 : : parameters known to be constant at caller of EDGE are propagated.
4254 : : KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
4255 : : and types for parameters. */
4256 : :
4257 : : void
4258 : 175245 : estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
4259 : : ipa_auto_call_arg_values *avals,
4260 : : ipa_call_estimates *estimates)
4261 : : {
4262 : 175245 : clause_t clause, nonspec_clause;
4263 : :
4264 : 175245 : evaluate_conditions_for_known_args (node, false, avals, &clause,
4265 : : &nonspec_clause, NULL);
4266 : 175245 : ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals);
4267 : 175245 : ctx.estimate_size_and_time (estimates);
4268 : 175245 : }
4269 : :
4270 : : /* Return stack frame offset where frame of NODE is supposed to start inside
4271 : : of the function it is inlined to.
4272 : : Return 0 for functions that are not inlined. */
4273 : :
4274 : : HOST_WIDE_INT
4275 : 5123960 : ipa_get_stack_frame_offset (struct cgraph_node *node)
4276 : : {
4277 : 5123960 : HOST_WIDE_INT offset = 0;
4278 : 5123960 : if (!node->inlined_to)
4279 : : return 0;
4280 : 4024282 : node = node->callers->caller;
4281 : 4937636 : while (true)
4282 : : {
4283 : 4937636 : offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
4284 : 4480959 : if (!node->inlined_to)
4285 : : return offset;
4286 : 456677 : node = node->callers->caller;
4287 : : }
4288 : : }
4289 : :
4290 : :
4291 : : /* Update summary information of inline clones after inlining.
4292 : : Compute peak stack usage. */
4293 : :
4294 : : static void
4295 : 4681652 : inline_update_callee_summaries (struct cgraph_node *node, int depth)
4296 : : {
4297 : 4681652 : struct cgraph_edge *e;
4298 : :
4299 : 4681652 : ipa_propagate_frequency (node);
4300 : 8639947 : for (e = node->callees; e; e = e->next_callee)
4301 : : {
4302 : 3958295 : if (!e->inline_failed)
4303 : 657923 : inline_update_callee_summaries (e->callee, depth);
4304 : : else
4305 : 3300372 : ipa_call_summaries->get (e)->loop_depth += depth;
4306 : : }
4307 : 4793549 : for (e = node->indirect_calls; e; e = e->next_callee)
4308 : 111897 : ipa_call_summaries->get (e)->loop_depth += depth;
4309 : 4681652 : }
4310 : :
4311 : : /* Update change_prob and points_to_local_or_readonly_memory of EDGE after
4312 : : INLINED_EDGE has been inlined.
4313 : :
4314 : : When function A is inlined in B and A calls C with parameter that
4315 : : changes with probability PROB1 and C is known to be passthrough
4316 : : of argument if B that change with probability PROB2, the probability
4317 : : of change is now PROB1*PROB2. */
4318 : :
4319 : : static void
4320 : 3412571 : remap_edge_params (struct cgraph_edge *inlined_edge,
4321 : : struct cgraph_edge *edge)
4322 : : {
4323 : 3412571 : if (ipa_node_params_sum)
4324 : : {
4325 : 2455682 : int i;
4326 : 2455682 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4327 : 2455682 : if (!args)
4328 : : return;
4329 : 1385461 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4330 : 1385461 : class ipa_call_summary *inlined_es
4331 : 1385461 : = ipa_call_summaries->get (inlined_edge);
4332 : :
4333 : 1385461 : if (es->param.length () == 0)
4334 : : return;
4335 : :
4336 : 7995100 : for (i = 0; i < ipa_get_cs_argument_count (args); i++)
4337 : : {
4338 : 2692737 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4339 : 2692737 : if (jfunc->type == IPA_JF_PASS_THROUGH
4340 : 1972720 : || jfunc->type == IPA_JF_ANCESTOR)
4341 : : {
4342 : 870628 : int id = jfunc->type == IPA_JF_PASS_THROUGH
4343 : 870628 : ? ipa_get_jf_pass_through_formal_id (jfunc)
4344 : 150611 : : ipa_get_jf_ancestor_formal_id (jfunc);
4345 : 1741173 : if (id < (int) inlined_es->param.length ())
4346 : : {
4347 : 870537 : int prob1 = es->param[i].change_prob;
4348 : 870537 : int prob2 = inlined_es->param[id].change_prob;
4349 : 870537 : int prob = combine_probabilities (prob1, prob2);
4350 : :
4351 : 870537 : if (prob1 && prob2 && !prob)
4352 : 870537 : prob = 1;
4353 : :
4354 : 870537 : es->param[i].change_prob = prob;
4355 : :
4356 : 1741074 : if (inlined_es
4357 : 870537 : ->param[id].points_to_local_or_readonly_memory)
4358 : 223800 : es->param[i].points_to_local_or_readonly_memory = true;
4359 : 1741074 : if (inlined_es
4360 : 870537 : ->param[id].points_to_possible_sra_candidate)
4361 : 196244 : es->param[i].points_to_possible_sra_candidate = true;
4362 : : }
4363 : 870628 : if (!es->param[i].points_to_local_or_readonly_memory
4364 : : && jfunc->type == IPA_JF_CONST
4365 : : && points_to_local_or_readonly_memory_p
4366 : : (ipa_get_jf_constant (jfunc)))
4367 : : es->param[i].points_to_local_or_readonly_memory = true;
4368 : : }
4369 : : }
4370 : : }
4371 : : }
4372 : :
4373 : : /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
4374 : :
4375 : : Remap predicates of callees of NODE. Rest of arguments match
4376 : : remap_predicate.
4377 : :
4378 : : Also update change probabilities. */
4379 : :
4380 : : static void
4381 : 4681652 : remap_edge_summaries (struct cgraph_edge *inlined_edge,
4382 : : struct cgraph_node *node,
4383 : : class ipa_fn_summary *info,
4384 : : class ipa_node_params *params_summary,
4385 : : class ipa_fn_summary *callee_info,
4386 : : const vec<int> &operand_map,
4387 : : const vec<HOST_WIDE_INT> &offset_map,
4388 : : clause_t possible_truths,
4389 : : ipa_predicate *toplev_predicate)
4390 : : {
4391 : 4681652 : struct cgraph_edge *e, *next;
4392 : 8639502 : for (e = node->callees; e; e = next)
4393 : : {
4394 : 3957850 : ipa_predicate p;
4395 : 3957850 : next = e->next_callee;
4396 : :
4397 : 3957850 : if (e->inline_failed)
4398 : : {
4399 : 3299927 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4400 : 3299927 : remap_edge_params (inlined_edge, e);
4401 : :
4402 : 3299927 : if (es->predicate)
4403 : : {
4404 : 995096 : p = es->predicate->remap_after_inlining
4405 : 995096 : (info, params_summary,
4406 : : callee_info, operand_map,
4407 : : offset_map, possible_truths,
4408 : : *toplev_predicate);
4409 : 995096 : edge_set_predicate (e, &p);
4410 : : }
4411 : : else
4412 : 2304831 : edge_set_predicate (e, toplev_predicate);
4413 : : }
4414 : : else
4415 : 657923 : remap_edge_summaries (inlined_edge, e->callee, info,
4416 : : params_summary, callee_info,
4417 : : operand_map, offset_map, possible_truths,
4418 : : toplev_predicate);
4419 : : }
4420 : 4794296 : for (e = node->indirect_calls; e; e = next)
4421 : : {
4422 : 112644 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4423 : 112644 : ipa_predicate p;
4424 : 112644 : next = e->next_callee;
4425 : :
4426 : 112644 : remap_edge_params (inlined_edge, e);
4427 : 112644 : if (es->predicate)
4428 : : {
4429 : 26932 : p = es->predicate->remap_after_inlining
4430 : 26932 : (info, params_summary,
4431 : : callee_info, operand_map, offset_map,
4432 : : possible_truths, *toplev_predicate);
4433 : 26932 : edge_set_predicate (e, &p);
4434 : : }
4435 : : else
4436 : 85712 : edge_set_predicate (e, toplev_predicate);
4437 : : }
4438 : 4681652 : }
4439 : :
4440 : : /* Run remap_after_inlining on each predicate in V. */
4441 : :
4442 : : static void
4443 : 8047458 : remap_freqcounting_predicate (class ipa_fn_summary *info,
4444 : : class ipa_node_params *params_summary,
4445 : : class ipa_fn_summary *callee_info,
4446 : : vec<ipa_freqcounting_predicate, va_gc> *v,
4447 : : const vec<int> &operand_map,
4448 : : const vec<HOST_WIDE_INT> &offset_map,
4449 : : clause_t possible_truths,
4450 : : ipa_predicate *toplev_predicate)
4451 : :
4452 : : {
4453 : 8047458 : ipa_freqcounting_predicate *fcp;
4454 : 8076307 : for (int i = 0; vec_safe_iterate (v, i, &fcp); i++)
4455 : : {
4456 : 28849 : ipa_predicate p
4457 : 28849 : = fcp->predicate->remap_after_inlining (info, params_summary,
4458 : : callee_info, operand_map,
4459 : : offset_map, possible_truths,
4460 : : *toplev_predicate);
4461 : 39730 : if (p != false && p != true)
4462 : 2995 : *fcp->predicate &= p;
4463 : : }
4464 : 8047458 : }
4465 : :
4466 : : /* We inlined EDGE. Update summary of the function we inlined into. */
4467 : :
4468 : : void
4469 : 4023729 : ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
4470 : : {
4471 : 4023729 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
4472 : 3791417 : struct cgraph_node *to = (edge->caller->inlined_to
4473 : 4023729 : ? edge->caller->inlined_to : edge->caller);
4474 : 4023729 : class ipa_fn_summary *info = ipa_fn_summaries->get (to);
4475 : 4023729 : clause_t clause = 0; /* not_inline is known to be false. */
4476 : 4023729 : size_time_entry *e;
4477 : 4023729 : auto_vec<int, 8> operand_map;
4478 : 4023729 : auto_vec<HOST_WIDE_INT, 8> offset_map;
4479 : 4023729 : int i;
4480 : 4023729 : ipa_predicate toplev_predicate;
4481 : 4023729 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4482 : 4023729 : ipa_node_params *params_summary = (ipa_node_params_sum
4483 : 4023729 : ? ipa_node_params_sum->get (to) : NULL);
4484 : :
4485 : 4023729 : if (es->predicate)
4486 : 343703 : toplev_predicate = *es->predicate;
4487 : : else
4488 : 3680026 : toplev_predicate = true;
4489 : :
4490 : 4023729 : info->fp_expressions |= callee_info->fp_expressions;
4491 : 4023729 : info->target_info |= callee_info->target_info;
4492 : :
4493 : 4023729 : if (callee_info->conds)
4494 : : {
4495 : 2928228 : ipa_auto_call_arg_values avals;
4496 : 2928228 : evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
4497 : 2928228 : }
4498 : 4023729 : if (ipa_node_params_sum && callee_info->conds)
4499 : : {
4500 : 782814 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4501 : 782814 : int count = args ? ipa_get_cs_argument_count (args) : 0;
4502 : 782677 : int i;
4503 : :
4504 : 782677 : if (count)
4505 : : {
4506 : 782677 : operand_map.safe_grow_cleared (count, true);
4507 : 782677 : offset_map.safe_grow_cleared (count, true);
4508 : : }
4509 : 2465950 : for (i = 0; i < count; i++)
4510 : : {
4511 : 1683136 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4512 : 1683136 : int map = -1;
4513 : :
4514 : : /* TODO: handle non-NOPs when merging. */
4515 : 1683136 : if (jfunc->type == IPA_JF_PASS_THROUGH)
4516 : : {
4517 : 288240 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4518 : 285148 : map = ipa_get_jf_pass_through_formal_id (jfunc);
4519 : 288240 : if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
4520 : 191094 : offset_map[i] = -1;
4521 : : }
4522 : 1394896 : else if (jfunc->type == IPA_JF_ANCESTOR)
4523 : : {
4524 : 90335 : HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
4525 : 90335 : if (offset >= 0 && offset < INT_MAX)
4526 : : {
4527 : 90335 : map = ipa_get_jf_ancestor_formal_id (jfunc);
4528 : 90335 : if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
4529 : 52987 : offset = -1;
4530 : 90335 : offset_map[i] = offset;
4531 : : }
4532 : : }
4533 : 1683136 : operand_map[i] = map;
4534 : 3047125 : gcc_assert (map < ipa_get_param_count (params_summary));
4535 : : }
4536 : :
4537 : : int ip;
4538 : 785920 : for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++)
4539 : 3106 : if (ip < count && operand_map[ip] >= 0)
4540 : 33 : add_builtin_constant_p_parm (info, operand_map[ip]);
4541 : : }
4542 : 4023729 : sreal freq = edge->sreal_frequency ();
4543 : 22049403 : for (i = 0; callee_info->size_time_table.iterate (i, &e); i++)
4544 : : {
4545 : 18025674 : ipa_predicate p;
4546 : 18025674 : p = e->exec_predicate.remap_after_inlining
4547 : 18025674 : (info, params_summary,
4548 : : callee_info, operand_map,
4549 : : offset_map, clause,
4550 : : toplev_predicate);
4551 : 18025674 : ipa_predicate nonconstp;
4552 : 18025674 : nonconstp = e->nonconst_predicate.remap_after_inlining
4553 : 18025674 : (info, params_summary,
4554 : : callee_info, operand_map,
4555 : : offset_map, clause,
4556 : : toplev_predicate);
4557 : 27102708 : if (p != false && nonconstp != false)
4558 : : {
4559 : 8754654 : sreal add_time = ((sreal)e->time * freq);
4560 : 8754654 : int prob = e->nonconst_predicate.probability (callee_info->conds,
4561 : : clause, es->param);
4562 : 8754654 : if (prob != REG_BR_PROB_BASE)
4563 : 1129896 : add_time = add_time * prob / REG_BR_PROB_BASE;
4564 : 1129896 : if (prob != REG_BR_PROB_BASE
4565 : 1129896 : && dump_file && (dump_flags & TDF_DETAILS))
4566 : : {
4567 : 3 : fprintf (dump_file, "\t\tScaling time by probability:%f\n",
4568 : 3 : (double) prob / REG_BR_PROB_BASE);
4569 : : }
4570 : 8754654 : info->account_size_time (e->size, add_time, p, nonconstp);
4571 : : }
4572 : : }
4573 : 4023729 : remap_edge_summaries (edge, edge->callee, info, params_summary,
4574 : : callee_info, operand_map,
4575 : : offset_map, clause, &toplev_predicate);
4576 : 4023729 : remap_freqcounting_predicate (info, params_summary, callee_info,
4577 : : info->loop_iterations, operand_map,
4578 : : offset_map, clause, &toplev_predicate);
4579 : 4023729 : remap_freqcounting_predicate (info, params_summary, callee_info,
4580 : : info->loop_strides, operand_map,
4581 : : offset_map, clause, &toplev_predicate);
4582 : :
4583 : 4023729 : HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
4584 : 4023729 : HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
4585 : :
4586 : 4023729 : if (info->estimated_stack_size < peak)
4587 : 125001 : info->estimated_stack_size = peak;
4588 : :
4589 : 4023729 : inline_update_callee_summaries (edge->callee, es->loop_depth);
4590 : 4023729 : if (info->call_size_time_table.length ())
4591 : : {
4592 : 842652 : int edge_size = 0;
4593 : 842652 : sreal edge_time = 0;
4594 : :
4595 : 842652 : estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0);
4596 : : /* Unaccount size and time of the optimized out call. */
4597 : 842652 : info->account_size_time (-edge_size, -edge_time,
4598 : 842652 : es->predicate ? *es->predicate : true,
4599 : 842652 : es->predicate ? *es->predicate : true,
4600 : : true);
4601 : : /* Account new calls. */
4602 : 842652 : summarize_calls_size_and_time (edge->callee, info);
4603 : : }
4604 : :
4605 : : /* Free summaries that are not maintained for inline clones/edges. */
4606 : 4023729 : ipa_call_summaries->remove (edge);
4607 : 4023729 : ipa_fn_summaries->remove (edge->callee);
4608 : 4023729 : ipa_remove_from_growth_caches (edge);
4609 : 4023729 : }
4610 : :
4611 : : /* For performance reasons ipa_merge_fn_summary_after_inlining is not updating
4612 : : overall size and time. Recompute it.
4613 : : If RESET is true also recompute call_time_size_table. */
4614 : :
4615 : : void
4616 : 9480236 : ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset)
4617 : : {
4618 : 9480236 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
4619 : 9480236 : class ipa_size_summary *size_info = ipa_size_summaries->get (node);
4620 : 9480236 : size_time_entry *e;
4621 : 9480236 : int i;
4622 : :
4623 : 9480236 : size_info->size = 0;
4624 : 9480236 : info->time = 0;
4625 : 48238900 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4626 : : {
4627 : 38758664 : size_info->size += e->size;
4628 : 38758664 : info->time += e->time;
4629 : : }
4630 : 9480236 : info->min_size = info->size_time_table[0].size;
4631 : 9480236 : if (reset)
4632 : 8593049 : info->call_size_time_table.release ();
4633 : 9480236 : if (node->callees || node->indirect_calls)
4634 : 7155622 : estimate_calls_size_and_time (node, &size_info->size, &info->min_size,
4635 : : &info->time, NULL,
4636 : : ~(clause_t) (1 << ipa_predicate::false_condition),
4637 : : NULL);
4638 : 9480236 : size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale);
4639 : 9480236 : info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale);
4640 : 9480236 : }
4641 : :
4642 : :
4643 : : /* This function performs intraprocedural analysis in NODE that is required to
4644 : : inline indirect calls. */
4645 : :
4646 : : static void
4647 : 1378433 : inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4648 : : {
4649 : 1378433 : ipa_analyze_node (node);
4650 : 1378433 : if (dump_file && (dump_flags & TDF_DETAILS))
4651 : : {
4652 : 14 : ipa_print_node_params (dump_file, node);
4653 : 14 : ipa_print_node_jump_functions (dump_file, node);
4654 : : }
4655 : 1378433 : }
4656 : :
4657 : :
4658 : : /* Note function body size. */
4659 : :
4660 : : void
4661 : 1390999 : inline_analyze_function (struct cgraph_node *node)
4662 : : {
4663 : 1390999 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4664 : :
4665 : 1390999 : if (dump_file)
4666 : 94 : fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ());
4667 : 1390999 : if (opt_for_fn (node->decl, optimize) && !node->thunk)
4668 : 1378433 : inline_indirect_intraprocedural_analysis (node);
4669 : 1390999 : compute_fn_summary (node, false);
4670 : 1390999 : if (!optimize)
4671 : : {
4672 : 11259 : struct cgraph_edge *e;
4673 : 24195 : for (e = node->callees; e; e = e->next_callee)
4674 : 12936 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4675 : 11330 : for (e = node->indirect_calls; e; e = e->next_callee)
4676 : 71 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4677 : : }
4678 : :
4679 : 1390999 : pop_cfun ();
4680 : 1390999 : }
4681 : :
4682 : :
4683 : : /* Called when new function is inserted to callgraph late. */
4684 : :
4685 : : void
4686 : 19356 : ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
4687 : : {
4688 : 19356 : inline_analyze_function (node);
4689 : 19356 : }
4690 : :
4691 : : /* Note function body size. */
4692 : :
4693 : : static void
4694 : 231117 : ipa_fn_summary_generate (void)
4695 : : {
4696 : 231117 : struct cgraph_node *node;
4697 : :
4698 : 2091482 : FOR_EACH_DEFINED_FUNCTION (node)
4699 : 1860365 : if (DECL_STRUCT_FUNCTION (node->decl))
4700 : 1847378 : node->versionable = tree_versionable_function_p (node->decl);
4701 : :
4702 : 231117 : ipa_fn_summary_alloc ();
4703 : :
4704 : 231117 : ipa_fn_summaries->enable_insertion_hook ();
4705 : :
4706 : 231117 : ipa_register_cgraph_hooks ();
4707 : :
4708 : 2091482 : FOR_EACH_DEFINED_FUNCTION (node)
4709 : 1860365 : if (!node->alias
4710 : 1860365 : && (flag_generate_lto || flag_generate_offload|| flag_wpa
4711 : 1670056 : || opt_for_fn (node->decl, optimize)))
4712 : 1353454 : inline_analyze_function (node);
4713 : 231117 : }
4714 : :
4715 : :
4716 : : /* Write inline summary for edge E to OB. */
4717 : :
4718 : : static void
4719 : 345633 : read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
4720 : : bool prevails)
4721 : : {
4722 : 345633 : class ipa_call_summary *es = prevails
4723 : 345633 : ? ipa_call_summaries->get_create (e) : NULL;
4724 : 345633 : ipa_predicate p;
4725 : 345633 : int length, i;
4726 : :
4727 : 345633 : int size = streamer_read_uhwi (ib);
4728 : 345633 : int time = streamer_read_uhwi (ib);
4729 : 345633 : int depth = streamer_read_uhwi (ib);
4730 : :
4731 : 345633 : if (es)
4732 : : {
4733 : 345584 : es->call_stmt_size = size;
4734 : 345584 : es->call_stmt_time = time;
4735 : 345584 : es->loop_depth = depth;
4736 : : }
4737 : :
4738 : 345633 : bitpack_d bp = streamer_read_bitpack (ib);
4739 : 345633 : if (es)
4740 : 345584 : es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
4741 : : else
4742 : 49 : bp_unpack_value (&bp, 1);
4743 : :
4744 : 345633 : p.stream_in (ib);
4745 : 345633 : if (es)
4746 : 345584 : edge_set_predicate (e, &p);
4747 : 345633 : length = streamer_read_uhwi (ib);
4748 : 345633 : if (length && es
4749 : 345633 : && (e->possibly_call_in_translation_unit_p ()
4750 : : /* Also stream in jump functions to builtins in hope that they
4751 : : will get fnspecs. */
4752 : 117984 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
4753 : : {
4754 : 231609 : es->param.safe_grow_cleared (length, true);
4755 : 797387 : for (i = 0; i < length; i++)
4756 : : {
4757 : 565778 : es->param[i].change_prob = streamer_read_uhwi (ib);
4758 : 565778 : bitpack_d bp = streamer_read_bitpack (ib);
4759 : 1697334 : es->param[i].points_to_local_or_readonly_memory
4760 : 565778 : = bp_unpack_value (&bp, 1);
4761 : 1697334 : es->param[i].points_to_possible_sra_candidate
4762 : 565778 : = bp_unpack_value (&bp, 1);
4763 : : }
4764 : : }
4765 : : else
4766 : : {
4767 : 136245 : for (i = 0; i < length; i++)
4768 : : {
4769 : 22221 : streamer_read_uhwi (ib);
4770 : 22221 : streamer_read_uhwi (ib);
4771 : : }
4772 : : }
4773 : 345633 : }
4774 : :
4775 : :
4776 : : /* Stream in inline summaries from the section. */
4777 : :
4778 : : static void
4779 : 14297 : inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4780 : : size_t len)
4781 : : {
4782 : 14297 : const struct lto_function_header *header =
4783 : : (const struct lto_function_header *) data;
4784 : 14297 : const int cfg_offset = sizeof (struct lto_function_header);
4785 : 14297 : const int main_offset = cfg_offset + header->cfg_size;
4786 : 14297 : const int string_offset = main_offset + header->main_size;
4787 : 14297 : class data_in *data_in;
4788 : 14297 : unsigned int i, count2, j;
4789 : 14297 : unsigned int f_count;
4790 : :
4791 : 14297 : lto_input_block ib ((const char *) data + main_offset, header->main_size,
4792 : 14297 : file_data);
4793 : :
4794 : 14297 : data_in =
4795 : 28594 : lto_data_in_create (file_data, (const char *) data + string_offset,
4796 : 14297 : header->string_size, vNULL);
4797 : 14297 : f_count = streamer_read_uhwi (&ib);
4798 : 100793 : for (i = 0; i < f_count; i++)
4799 : : {
4800 : 86496 : unsigned int index;
4801 : 86496 : struct cgraph_node *node;
4802 : 86496 : class ipa_fn_summary *info;
4803 : 86496 : class ipa_node_params *params_summary;
4804 : 86496 : class ipa_size_summary *size_info;
4805 : 86496 : lto_symtab_encoder_t encoder;
4806 : 86496 : struct bitpack_d bp;
4807 : 86496 : struct cgraph_edge *e;
4808 : 86496 : ipa_predicate p;
4809 : :
4810 : 86496 : index = streamer_read_uhwi (&ib);
4811 : 86496 : encoder = file_data->symtab_node_encoder;
4812 : 86496 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4813 : : index));
4814 : 86496 : info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
4815 : 86496 : params_summary = node->prevailing_p ()
4816 : 86496 : ? ipa_node_params_sum->get (node) : NULL;
4817 : 86496 : size_info = node->prevailing_p ()
4818 : 86496 : ? ipa_size_summaries->get_create (node) : NULL;
4819 : :
4820 : 86496 : int stack_size = streamer_read_uhwi (&ib);
4821 : 86496 : int size = streamer_read_uhwi (&ib);
4822 : 86496 : sreal time = sreal::stream_in (&ib);
4823 : :
4824 : 86496 : if (info)
4825 : : {
4826 : 86438 : info->estimated_stack_size
4827 : 86438 : = size_info->estimated_self_stack_size = stack_size;
4828 : 86438 : size_info->size = size_info->self_size = size;
4829 : 86438 : info->time = time;
4830 : : }
4831 : :
4832 : 86496 : bp = streamer_read_bitpack (&ib);
4833 : 86496 : if (info)
4834 : : {
4835 : 86438 : info->inlinable = bp_unpack_value (&bp, 1);
4836 : 86438 : info->fp_expressions = bp_unpack_value (&bp, 1);
4837 : 86438 : if (!lto_stream_offload_p)
4838 : 86438 : info->target_info = streamer_read_uhwi (&ib);
4839 : : }
4840 : : else
4841 : : {
4842 : 58 : bp_unpack_value (&bp, 1);
4843 : 58 : bp_unpack_value (&bp, 1);
4844 : 58 : if (!lto_stream_offload_p)
4845 : 58 : streamer_read_uhwi (&ib);
4846 : : }
4847 : :
4848 : 86496 : count2 = streamer_read_uhwi (&ib);
4849 : 86496 : gcc_assert (!info || !info->conds);
4850 : 86438 : if (info)
4851 : 86438 : vec_safe_reserve_exact (info->conds, count2);
4852 : 162533 : for (j = 0; j < count2; j++)
4853 : : {
4854 : 76037 : struct condition c;
4855 : 76037 : unsigned int k, count3;
4856 : 76037 : c.operand_num = streamer_read_uhwi (&ib);
4857 : 76037 : c.code = (enum tree_code) streamer_read_uhwi (&ib);
4858 : 76037 : c.type = stream_read_tree (&ib, data_in);
4859 : 76037 : c.val = stream_read_tree (&ib, data_in);
4860 : 76037 : bp = streamer_read_bitpack (&ib);
4861 : 76037 : c.agg_contents = bp_unpack_value (&bp, 1);
4862 : 76037 : c.by_ref = bp_unpack_value (&bp, 1);
4863 : 76037 : if (c.agg_contents)
4864 : 12149 : c.offset = streamer_read_uhwi (&ib);
4865 : 76037 : count3 = streamer_read_uhwi (&ib);
4866 : 76037 : c.param_ops = NULL;
4867 : 76037 : if (info)
4868 : 76037 : vec_safe_reserve_exact (c.param_ops, count3);
4869 : 76037 : if (params_summary)
4870 : 76037 : ipa_set_param_used_by_ipa_predicates
4871 : 76037 : (params_summary, c.operand_num, true);
4872 : 79521 : for (k = 0; k < count3; k++)
4873 : : {
4874 : 3484 : struct expr_eval_op op;
4875 : 3484 : enum gimple_rhs_class rhs_class;
4876 : 3484 : op.code = (enum tree_code) streamer_read_uhwi (&ib);
4877 : 3484 : op.type = stream_read_tree (&ib, data_in);
4878 : 3484 : switch (rhs_class = get_gimple_rhs_class (op.code))
4879 : : {
4880 : 1393 : case GIMPLE_UNARY_RHS:
4881 : 1393 : op.index = 0;
4882 : 1393 : op.val[0] = NULL_TREE;
4883 : 1393 : op.val[1] = NULL_TREE;
4884 : 1393 : break;
4885 : :
4886 : 2091 : case GIMPLE_BINARY_RHS:
4887 : 2091 : case GIMPLE_TERNARY_RHS:
4888 : 2091 : bp = streamer_read_bitpack (&ib);
4889 : 2091 : op.index = bp_unpack_value (&bp, 2);
4890 : 2091 : op.val[0] = stream_read_tree (&ib, data_in);
4891 : 2091 : if (rhs_class == GIMPLE_BINARY_RHS)
4892 : 2091 : op.val[1] = NULL_TREE;
4893 : : else
4894 : 0 : op.val[1] = stream_read_tree (&ib, data_in);
4895 : : break;
4896 : :
4897 : 0 : default:
4898 : 0 : fatal_error (UNKNOWN_LOCATION,
4899 : : "invalid fnsummary in LTO stream");
4900 : : }
4901 : 3484 : if (info)
4902 : 3484 : c.param_ops->quick_push (op);
4903 : : }
4904 : 76037 : if (info)
4905 : 76037 : info->conds->quick_push (c);
4906 : : }
4907 : 86496 : count2 = streamer_read_uhwi (&ib);
4908 : 86496 : gcc_assert (!info || !info->size_time_table.length ());
4909 : 86496 : if (info && count2)
4910 : 86438 : info->size_time_table.reserve_exact (count2);
4911 : 327438 : for (j = 0; j < count2; j++)
4912 : : {
4913 : 240942 : class size_time_entry e;
4914 : :
4915 : 240942 : e.size = streamer_read_uhwi (&ib);
4916 : 240942 : e.time = sreal::stream_in (&ib);
4917 : 240942 : e.exec_predicate.stream_in (&ib);
4918 : 240942 : e.nonconst_predicate.stream_in (&ib);
4919 : :
4920 : 240942 : if (info)
4921 : 240826 : info->size_time_table.quick_push (e);
4922 : : }
4923 : :
4924 : 86496 : count2 = streamer_read_uhwi (&ib);
4925 : 87738 : for (j = 0; j < count2; j++)
4926 : : {
4927 : 1242 : p.stream_in (&ib);
4928 : 1242 : sreal fcp_freq = sreal::stream_in (&ib);
4929 : 1242 : if (info)
4930 : : {
4931 : 1242 : ipa_freqcounting_predicate fcp;
4932 : 1242 : fcp.predicate = NULL;
4933 : 1242 : set_hint_predicate (&fcp.predicate, p);
4934 : 1242 : fcp.freq = fcp_freq;
4935 : 1242 : vec_safe_push (info->loop_iterations, fcp);
4936 : : }
4937 : : }
4938 : 86496 : count2 = streamer_read_uhwi (&ib);
4939 : 86782 : for (j = 0; j < count2; j++)
4940 : : {
4941 : 286 : p.stream_in (&ib);
4942 : 286 : sreal fcp_freq = sreal::stream_in (&ib);
4943 : 286 : if (info)
4944 : : {
4945 : 286 : ipa_freqcounting_predicate fcp;
4946 : 286 : fcp.predicate = NULL;
4947 : 286 : set_hint_predicate (&fcp.predicate, p);
4948 : 286 : fcp.freq = fcp_freq;
4949 : 286 : vec_safe_push (info->loop_strides, fcp);
4950 : : }
4951 : : }
4952 : 86496 : count2 = streamer_read_uhwi (&ib);
4953 : 86496 : if (info && count2)
4954 : 6 : info->builtin_constant_p_parms.reserve_exact (count2);
4955 : 86502 : for (j = 0; j < count2; j++)
4956 : : {
4957 : 6 : int parm = streamer_read_uhwi (&ib);
4958 : 6 : if (info)
4959 : 6 : info->builtin_constant_p_parms.quick_push (parm);
4960 : : }
4961 : 430665 : for (e = node->callees; e; e = e->next_callee)
4962 : 344169 : read_ipa_call_summary (&ib, e, info != NULL);
4963 : 87960 : for (e = node->indirect_calls; e; e = e->next_callee)
4964 : 1464 : read_ipa_call_summary (&ib, e, info != NULL);
4965 : : }
4966 : :
4967 : 14297 : lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4968 : : len);
4969 : 14297 : lto_data_in_delete (data_in);
4970 : 14297 : }
4971 : :
4972 : :
4973 : : /* Read inline summary. Jump functions are shared among ipa-cp
4974 : : and inliner, so when ipa-cp is active, we don't need to write them
4975 : : twice. */
4976 : :
4977 : : static void
4978 : 13259 : ipa_fn_summary_read (void)
4979 : : {
4980 : 13259 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4981 : 13259 : struct lto_file_decl_data *file_data;
4982 : 13259 : unsigned int j = 0;
4983 : :
4984 : 13259 : ipa_prop_read_jump_functions ();
4985 : 13259 : ipa_fn_summary_alloc ();
4986 : :
4987 : 40815 : while ((file_data = file_data_vec[j++]))
4988 : : {
4989 : 14297 : size_t len;
4990 : 14297 : const char *data
4991 : 14297 : = lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary,
4992 : : &len);
4993 : 14297 : if (data)
4994 : 14297 : inline_read_section (file_data, data, len);
4995 : : else
4996 : : /* Fatal error here. We do not want to support compiling ltrans units
4997 : : with different version of compiler or different flags than the WPA
4998 : : unit, so this should never happen. */
4999 : 0 : fatal_error (input_location,
5000 : : "ipa inline summary is missing in input file");
5001 : : }
5002 : 13259 : ipa_register_cgraph_hooks ();
5003 : :
5004 : 13259 : gcc_assert (ipa_fn_summaries);
5005 : 13259 : ipa_fn_summaries->enable_insertion_hook ();
5006 : 13259 : }
5007 : :
5008 : :
5009 : : /* Write inline summary for edge E to OB. */
5010 : :
5011 : : static void
5012 : 375737 : write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
5013 : : {
5014 : 375737 : class ipa_call_summary *es = ipa_call_summaries->get (e);
5015 : 375737 : int i;
5016 : :
5017 : 375737 : streamer_write_uhwi (ob, es->call_stmt_size);
5018 : 375737 : streamer_write_uhwi (ob, es->call_stmt_time);
5019 : 375737 : streamer_write_uhwi (ob, es->loop_depth);
5020 : :
5021 : 375737 : bitpack_d bp = bitpack_create (ob->main_stream);
5022 : 375737 : bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
5023 : 375737 : streamer_write_bitpack (&bp);
5024 : :
5025 : 375737 : if (es->predicate)
5026 : 10146 : es->predicate->stream_out (ob);
5027 : : else
5028 : 365591 : streamer_write_uhwi (ob, 0);
5029 : 375737 : streamer_write_uhwi (ob, es->param.length ());
5030 : 2270306 : for (i = 0; i < (int) es->param.length (); i++)
5031 : : {
5032 : 626634 : streamer_write_uhwi (ob, es->param[i].change_prob);
5033 : 626634 : bp = bitpack_create (ob->main_stream);
5034 : 626634 : bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1);
5035 : 626634 : bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1);
5036 : 626634 : streamer_write_bitpack (&bp);
5037 : : }
5038 : 375737 : }
5039 : :
5040 : :
5041 : : /* Write inline summary for node in SET.
5042 : : Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
5043 : : active, we don't need to write them twice. */
5044 : :
5045 : : static void
5046 : 24372 : ipa_fn_summary_write (void)
5047 : : {
5048 : 24372 : struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
5049 : 24372 : lto_symtab_encoder_iterator lsei;
5050 : 24372 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5051 : 24372 : unsigned int count = 0;
5052 : :
5053 : 129768 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5054 : 105396 : lsei_next_function_in_partition (&lsei))
5055 : : {
5056 : 105396 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5057 : 105396 : if (cnode->definition && !cnode->alias)
5058 : 102913 : count++;
5059 : : }
5060 : 24372 : streamer_write_uhwi (ob, count);
5061 : :
5062 : 129768 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5063 : 105396 : lsei_next_function_in_partition (&lsei))
5064 : : {
5065 : 105396 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5066 : 105396 : if (cnode->definition && !cnode->alias)
5067 : : {
5068 : 102913 : class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
5069 : 102913 : class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
5070 : 102913 : struct bitpack_d bp;
5071 : 102913 : struct cgraph_edge *edge;
5072 : 102913 : int i;
5073 : 102913 : size_time_entry *e;
5074 : 102913 : struct condition *c;
5075 : :
5076 : 102913 : streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
5077 : 102913 : streamer_write_hwi (ob, size_info->estimated_self_stack_size);
5078 : 102913 : streamer_write_hwi (ob, size_info->self_size);
5079 : 102913 : info->time.stream_out (ob);
5080 : 102913 : bp = bitpack_create (ob->main_stream);
5081 : 102913 : bp_pack_value (&bp, info->inlinable, 1);
5082 : 102913 : bp_pack_value (&bp, info->fp_expressions, 1);
5083 : 102913 : streamer_write_bitpack (&bp);
5084 : 102913 : if (!lto_stream_offload_p)
5085 : 102913 : streamer_write_uhwi (ob, info->target_info);
5086 : 102913 : streamer_write_uhwi (ob, vec_safe_length (info->conds));
5087 : 194041 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
5088 : : {
5089 : 91128 : int j;
5090 : 91128 : struct expr_eval_op *op;
5091 : :
5092 : 91128 : streamer_write_uhwi (ob, c->operand_num);
5093 : 91128 : streamer_write_uhwi (ob, c->code);
5094 : 91128 : stream_write_tree (ob, c->type, true);
5095 : 91128 : stream_write_tree (ob, c->val, true);
5096 : 91128 : bp = bitpack_create (ob->main_stream);
5097 : 91128 : bp_pack_value (&bp, c->agg_contents, 1);
5098 : 91128 : bp_pack_value (&bp, c->by_ref, 1);
5099 : 91128 : streamer_write_bitpack (&bp);
5100 : 91128 : if (c->agg_contents)
5101 : 15148 : streamer_write_uhwi (ob, c->offset);
5102 : 91128 : streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
5103 : 189123 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
5104 : : {
5105 : 4024 : streamer_write_uhwi (ob, op->code);
5106 : 4024 : stream_write_tree (ob, op->type, true);
5107 : 4024 : if (op->val[0])
5108 : : {
5109 : 2422 : bp = bitpack_create (ob->main_stream);
5110 : 2422 : bp_pack_value (&bp, op->index, 2);
5111 : 2422 : streamer_write_bitpack (&bp);
5112 : 2422 : stream_write_tree (ob, op->val[0], true);
5113 : 2422 : if (op->val[1])
5114 : 4 : stream_write_tree (ob, op->val[1], true);
5115 : : }
5116 : : }
5117 : : }
5118 : 102913 : streamer_write_uhwi (ob, info->size_time_table.length ());
5119 : 495562 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
5120 : : {
5121 : 289736 : streamer_write_uhwi (ob, e->size);
5122 : 289736 : e->time.stream_out (ob);
5123 : 289736 : e->exec_predicate.stream_out (ob);
5124 : 289736 : e->nonconst_predicate.stream_out (ob);
5125 : : }
5126 : 102913 : ipa_freqcounting_predicate *fcp;
5127 : 102913 : streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations));
5128 : 207650 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
5129 : : {
5130 : 1824 : fcp->predicate->stream_out (ob);
5131 : 1824 : fcp->freq.stream_out (ob);
5132 : : }
5133 : 102913 : streamer_write_uhwi (ob, vec_safe_length (info->loop_strides));
5134 : 206163 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
5135 : : {
5136 : 337 : fcp->predicate->stream_out (ob);
5137 : 337 : fcp->freq.stream_out (ob);
5138 : : }
5139 : 102913 : streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ());
5140 : 102913 : int ip;
5141 : 205840 : for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip);
5142 : : i++)
5143 : 14 : streamer_write_uhwi (ob, ip);
5144 : 476758 : for (edge = cnode->callees; edge; edge = edge->next_callee)
5145 : 373845 : write_ipa_call_summary (ob, edge);
5146 : 104805 : for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
5147 : 1892 : write_ipa_call_summary (ob, edge);
5148 : : }
5149 : : }
5150 : 24372 : streamer_write_char_stream (ob->main_stream, 0);
5151 : 24372 : produce_asm (ob);
5152 : 24372 : destroy_output_block (ob);
5153 : :
5154 : 24372 : ipa_prop_write_jump_functions ();
5155 : 24372 : }
5156 : :
5157 : :
5158 : : /* Release function summary. */
5159 : :
5160 : : void
5161 : 723091 : ipa_free_fn_summary (void)
5162 : : {
5163 : 723091 : if (!ipa_call_summaries)
5164 : : return;
5165 : 457689 : ggc_delete (ipa_fn_summaries);
5166 : 457689 : ipa_fn_summaries = NULL;
5167 : 457689 : delete ipa_call_summaries;
5168 : 457689 : ipa_call_summaries = NULL;
5169 : 457689 : edge_predicate_pool.release ();
5170 : : /* During IPA this is one of largest datastructures to release. */
5171 : 457689 : if (flag_wpa)
5172 : 8926 : ggc_trim ();
5173 : : }
5174 : :
5175 : : /* Release function summary. */
5176 : :
5177 : : void
5178 : 723091 : ipa_free_size_summary (void)
5179 : : {
5180 : 723091 : if (!ipa_size_summaries)
5181 : : return;
5182 : 457689 : delete ipa_size_summaries;
5183 : 457689 : ipa_size_summaries = NULL;
5184 : : }
5185 : :
5186 : : namespace {
5187 : :
5188 : : const pass_data pass_data_local_fn_summary =
5189 : : {
5190 : : GIMPLE_PASS, /* type */
5191 : : "local-fnsummary", /* name */
5192 : : OPTGROUP_INLINE, /* optinfo_flags */
5193 : : TV_INLINE_PARAMETERS, /* tv_id */
5194 : : 0, /* properties_required */
5195 : : 0, /* properties_provided */
5196 : : 0, /* properties_destroyed */
5197 : : 0, /* todo_flags_start */
5198 : : 0, /* todo_flags_finish */
5199 : : };
5200 : :
5201 : : class pass_local_fn_summary : public gimple_opt_pass
5202 : : {
5203 : : public:
5204 : 578160 : pass_local_fn_summary (gcc::context *ctxt)
5205 : 1156320 : : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
5206 : : {}
5207 : :
5208 : : /* opt_pass methods: */
5209 : 289080 : opt_pass * clone () final override
5210 : : {
5211 : 289080 : return new pass_local_fn_summary (m_ctxt);
5212 : : }
5213 : 5810459 : unsigned int execute (function *) final override
5214 : : {
5215 : 5810459 : return compute_fn_summary_for_current ();
5216 : : }
5217 : :
5218 : : }; // class pass_local_fn_summary
5219 : :
5220 : : } // anon namespace
5221 : :
5222 : : gimple_opt_pass *
5223 : 289080 : make_pass_local_fn_summary (gcc::context *ctxt)
5224 : : {
5225 : 289080 : return new pass_local_fn_summary (ctxt);
5226 : : }
5227 : :
5228 : :
5229 : : /* Free inline summary. */
5230 : :
5231 : : namespace {
5232 : :
5233 : : const pass_data pass_data_ipa_free_fn_summary =
5234 : : {
5235 : : SIMPLE_IPA_PASS, /* type */
5236 : : "free-fnsummary", /* name */
5237 : : OPTGROUP_NONE, /* optinfo_flags */
5238 : : TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
5239 : : 0, /* properties_required */
5240 : : 0, /* properties_provided */
5241 : : 0, /* properties_destroyed */
5242 : : 0, /* todo_flags_start */
5243 : : 0, /* todo_flags_finish */
5244 : : };
5245 : :
5246 : : class pass_ipa_free_fn_summary : public simple_ipa_opt_pass
5247 : : {
5248 : : public:
5249 : 578160 : pass_ipa_free_fn_summary (gcc::context *ctxt)
5250 : 578160 : : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
5251 : 1156320 : small_p (false)
5252 : : {}
5253 : :
5254 : : /* opt_pass methods: */
5255 : 289080 : opt_pass *clone () final override
5256 : : {
5257 : 289080 : return new pass_ipa_free_fn_summary (m_ctxt);
5258 : : }
5259 : 578160 : void set_pass_param (unsigned int n, bool param) final override
5260 : : {
5261 : 578160 : gcc_assert (n == 0);
5262 : 578160 : small_p = param;
5263 : 578160 : }
5264 : 484627 : bool gate (function *) final override { return true; }
5265 : 462070 : unsigned int execute (function *) final override
5266 : : {
5267 : 462070 : ipa_free_fn_summary ();
5268 : : /* Free ipa-prop structures if they are no longer needed. */
5269 : 462070 : ipa_free_all_structures_after_iinln ();
5270 : 462070 : if (!flag_wpa)
5271 : 453144 : ipa_free_size_summary ();
5272 : 462070 : return 0;
5273 : : }
5274 : :
5275 : : private:
5276 : : bool small_p;
5277 : : }; // class pass_ipa_free_fn_summary
5278 : :
5279 : : } // anon namespace
5280 : :
5281 : : simple_ipa_opt_pass *
5282 : 289080 : make_pass_ipa_free_fn_summary (gcc::context *ctxt)
5283 : : {
5284 : 289080 : return new pass_ipa_free_fn_summary (ctxt);
5285 : : }
5286 : :
5287 : : namespace {
5288 : :
5289 : : const pass_data pass_data_ipa_fn_summary =
5290 : : {
5291 : : IPA_PASS, /* type */
5292 : : "fnsummary", /* name */
5293 : : OPTGROUP_INLINE, /* optinfo_flags */
5294 : : TV_IPA_FNSUMMARY, /* tv_id */
5295 : : 0, /* properties_required */
5296 : : 0, /* properties_provided */
5297 : : 0, /* properties_destroyed */
5298 : : 0, /* todo_flags_start */
5299 : : ( TODO_dump_symtab ), /* todo_flags_finish */
5300 : : };
5301 : :
5302 : : class pass_ipa_fn_summary : public ipa_opt_pass_d
5303 : : {
5304 : : public:
5305 : 289080 : pass_ipa_fn_summary (gcc::context *ctxt)
5306 : : : ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt,
5307 : : ipa_fn_summary_generate, /* generate_summary */
5308 : : ipa_fn_summary_write, /* write_summary */
5309 : : ipa_fn_summary_read, /* read_summary */
5310 : : NULL, /* write_optimization_summary */
5311 : : NULL, /* read_optimization_summary */
5312 : : NULL, /* stmt_fixup */
5313 : : 0, /* function_transform_todo_flags_start */
5314 : : NULL, /* function_transform */
5315 : 289080 : NULL) /* variable_transform */
5316 : 289080 : {}
5317 : :
5318 : : /* opt_pass methods: */
5319 : 230804 : unsigned int execute (function *) final override { return 0; }
5320 : :
5321 : : }; // class pass_ipa_fn_summary
5322 : :
5323 : : } // anon namespace
5324 : :
5325 : : ipa_opt_pass_d *
5326 : 289080 : make_pass_ipa_fn_summary (gcc::context *ctxt)
5327 : : {
5328 : 289080 : return new pass_ipa_fn_summary (ctxt);
5329 : : }
5330 : :
5331 : : /* Reset all state within ipa-fnsummary.cc so that we can rerun the compiler
5332 : : within the same process. For use by toplev::finalize. */
5333 : :
5334 : : void
5335 : 260181 : ipa_fnsummary_cc_finalize (void)
5336 : : {
5337 : 260181 : ipa_free_fn_summary ();
5338 : 260181 : ipa_free_size_summary ();
5339 : 260181 : }
|