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 : 135169703 : 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 : 135169703 : size_time_entry *e;
173 : 135169703 : bool found = false;
174 : 135169703 : int i;
175 : 135169703 : ipa_predicate nonconst_pred;
176 : 135169703 : vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table;
177 : :
178 : 135169703 : if (exec_pred == false)
179 : 4542329 : return;
180 : :
181 : 134967861 : nonconst_pred = nonconst_pred_in & exec_pred;
182 : :
183 : 134967861 : 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 : 157872900 : if (!size && time == 0 && table->length ())
189 : 3629426 : return;
190 : :
191 : : /* Only for calls we are unaccounting what we previously recorded. */
192 : 130627374 : gcc_checking_assert (time >= 0 || call);
193 : :
194 : 471305038 : for (i = 0; table->iterate (i, &e); i++)
195 : 441694497 : if (e->exec_predicate == exec_pred
196 : 441694497 : && e->nonconst_predicate == nonconst_pred)
197 : : {
198 : : found = true;
199 : : break;
200 : : }
201 : 130627374 : 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 : 130631584 : if (dump_file && (dump_flags & TDF_DETAILS) && (time != 0 || size))
212 : : {
213 : 3945 : fprintf (dump_file,
214 : : "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate exec:",
215 : 3714 : ((double) size) / ipa_fn_summary::size_scale,
216 : : (time.to_double ()), found ? "" : "new ");
217 : 3714 : exec_pred.dump (dump_file, conds, 0);
218 : 3714 : if (exec_pred != nonconst_pred)
219 : : {
220 : 84 : fprintf (dump_file, " nonconst:");
221 : 84 : nonconst_pred.dump (dump_file, conds);
222 : : }
223 : : else
224 : 3630 : fprintf (dump_file, "\n");
225 : : }
226 : 130627374 : if (!found)
227 : : {
228 : 29610541 : class size_time_entry new_entry;
229 : 29610541 : new_entry.size = size;
230 : 29610541 : new_entry.time = time;
231 : 29610541 : new_entry.exec_predicate = exec_pred;
232 : 29610541 : new_entry.nonconst_predicate = nonconst_pred;
233 : 29610541 : if (call)
234 : 1348854 : call_size_time_table.safe_push (new_entry);
235 : : else
236 : 28261687 : size_time_table.safe_push (new_entry);
237 : : }
238 : : else
239 : : {
240 : 101016833 : e->size += size;
241 : 101016833 : e->time += time;
242 : : /* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */
243 : : /* Tolerate small roundoff issues. */
244 : 101016833 : if (e->time < 0)
245 : 88 : e->time = 0;
246 : : }
247 : : }
248 : :
249 : : /* We proved E to be unreachable, redirect it to __builtin_unreachable. */
250 : :
251 : : static struct cgraph_edge *
252 : 230291 : redirect_to_unreachable (struct cgraph_edge *e)
253 : : {
254 : 230291 : struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
255 : 230291 : struct cgraph_node *target
256 : 230291 : = cgraph_node::get_create (builtin_decl_unreachable ());
257 : :
258 : 230291 : gcc_checking_assert (lookup_attribute ("cold",
259 : : DECL_ATTRIBUTES (target->decl)));
260 : :
261 : 230291 : if (e->speculative)
262 : 309 : e = cgraph_edge::resolve_speculation (e, target->decl);
263 : 229982 : else if (!e->callee)
264 : 778 : e = cgraph_edge::make_direct (e, target);
265 : : else
266 : 229204 : e->redirect_callee (target);
267 : 230291 : class ipa_call_summary *es = ipa_call_summaries->get (e);
268 : 230291 : e->inline_failed = CIF_UNREACHABLE;
269 : 230291 : e->count = profile_count::zero ();
270 : 230291 : es->call_stmt_size = 0;
271 : 230291 : es->call_stmt_time = 0;
272 : 230291 : if (callee)
273 : 0 : callee->remove_symbol_and_inline_clones ();
274 : 230291 : if (e->has_callback)
275 : 2 : e->purge_callback_edges ();
276 : 230291 : return e;
277 : : }
278 : :
279 : : /* Set predicate for edge E. */
280 : :
281 : : static void
282 : 30199936 : edge_set_predicate (struct cgraph_edge *e, ipa_predicate *predicate)
283 : : {
284 : : /* If the edge is determined to be never executed, redirect it
285 : : to BUILTIN_UNREACHABLE to make it clear to IPA passes the call will
286 : : be optimized out. */
287 : 27598332 : if (predicate && *predicate == false
288 : : /* When handling speculative edges, we need to do the redirection
289 : : just once. Do it always on the direct edge, so we do not
290 : : attempt to resolve speculation while duplicating the edge. */
291 : 30430291 : && (!e->speculative || e->callee))
292 : 230291 : e = redirect_to_unreachable (e);
293 : :
294 : 30199936 : class ipa_call_summary *es = ipa_call_summaries->get (e);
295 : 57798268 : if (predicate && *predicate != true)
296 : : {
297 : 4272873 : if (!es->predicate)
298 : 3884727 : es->predicate = edge_predicate_pool.allocate ();
299 : 4272873 : *es->predicate = *predicate;
300 : : }
301 : : else
302 : : {
303 : 25927063 : if (es->predicate)
304 : 661445 : edge_predicate_pool.remove (es->predicate);
305 : 25927063 : es->predicate = NULL;
306 : : }
307 : 30199936 : }
308 : :
309 : : /* Set predicate for hint *P. */
310 : :
311 : : static void
312 : 165993 : set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate)
313 : : {
314 : 165993 : if (new_predicate == false || new_predicate == true)
315 : : {
316 : 2268 : if (*p)
317 : 0 : edge_predicate_pool.remove (*p);
318 : 2268 : *p = NULL;
319 : : }
320 : : else
321 : : {
322 : 163725 : if (!*p)
323 : 163725 : *p = edge_predicate_pool.allocate ();
324 : 163725 : **p = new_predicate;
325 : : }
326 : 165993 : }
327 : :
328 : : /* Find if NEW_PREDICATE is already in V and if so, increment its freq.
329 : : Otherwise add a new item to the vector with this predicate and frerq equal
330 : : to add_freq, unless the number of predicates would exceed MAX_NUM_PREDICATES
331 : : in which case the function does nothing. */
332 : :
333 : : static void
334 : 1112180 : add_freqcounting_predicate (vec<ipa_freqcounting_predicate, va_gc> **v,
335 : : const ipa_predicate &new_predicate, sreal add_freq,
336 : : unsigned max_num_predicates)
337 : : {
338 : 1112180 : if (new_predicate == false || new_predicate == true)
339 : : return;
340 : : ipa_freqcounting_predicate *f;
341 : 139583 : for (int i = 0; vec_safe_iterate (*v, i, &f); i++)
342 : 71083 : if (new_predicate == f->predicate)
343 : : {
344 : 0 : f->freq += add_freq;
345 : 0 : return;
346 : : }
347 : 94171 : if (vec_safe_length (*v) >= max_num_predicates)
348 : : /* Too many different predicates to account for. */
349 : : return;
350 : :
351 : 68228 : ipa_freqcounting_predicate fcp;
352 : 68228 : fcp.predicate = NULL;
353 : 68228 : set_hint_predicate (&fcp.predicate, new_predicate);
354 : 68228 : fcp.freq = add_freq;
355 : 68228 : vec_safe_push (*v, fcp);
356 : 68228 : return;
357 : : }
358 : :
359 : : /* Compute what conditions may or may not hold given information about
360 : : parameters. RET_CLAUSE returns truths that may hold in a specialized copy,
361 : : while RET_NONSPEC_CLAUSE returns truths that may hold in an nonspecialized
362 : : copy when called in a given context. It is a bitmask of conditions. Bit
363 : : 0 means that condition is known to be false, while bit 1 means that condition
364 : : may or may not be true. These differs - for example NOT_INLINED condition
365 : : is always false in the second and also builtin_constant_p tests cannot use
366 : : the fact that parameter is indeed a constant.
367 : :
368 : : When INLINE_P is true, assume that we are inlining. AVAL contains known
369 : : information about argument values. The function does not modify its content
370 : : and so AVALs could also be of type ipa_call_arg_values but so far all
371 : : callers work with the auto version and so we avoid the conversion for
372 : : convenience.
373 : :
374 : : ERROR_MARK value of an argument means compile time invariant. */
375 : :
376 : : static void
377 : 22325285 : evaluate_conditions_for_known_args (struct cgraph_node *node,
378 : : bool inline_p,
379 : : ipa_auto_call_arg_values *avals,
380 : : clause_t *ret_clause,
381 : : clause_t *ret_nonspec_clause,
382 : : ipa_call_summary *es)
383 : : {
384 : 22325285 : clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
385 : 22325285 : clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition;
386 : 22325285 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
387 : 22325285 : int i;
388 : 22325285 : struct condition *c;
389 : :
390 : 91145633 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
391 : : {
392 : 68820348 : tree val = NULL;
393 : 68820348 : tree res;
394 : 68820348 : int j;
395 : 68820348 : struct expr_eval_op *op;
396 : :
397 : 68820348 : if (c->code == ipa_predicate::not_sra_candidate)
398 : : {
399 : 16432712 : if (!inline_p
400 : 16432712 : || !es
401 : 16180511 : || (int)es->param.length () <= c->operand_num
402 : 24522940 : || !es->param[c->operand_num].points_to_possible_sra_candidate)
403 : 12307207 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
404 : 16432712 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
405 : 68820348 : continue;
406 : : }
407 : :
408 : 52387636 : if (c->agg_contents)
409 : : {
410 : 27870533 : if (c->code == ipa_predicate::changed
411 : 18262037 : && !c->by_ref
412 : 30359797 : && (avals->safe_sval_at(c->operand_num) == error_mark_node))
413 : 5212 : continue;
414 : :
415 : 27860109 : if (tree sval = avals->safe_sval_at (c->operand_num))
416 : 13403514 : val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref);
417 : 13403514 : if (!val)
418 : : {
419 : 27851654 : ipa_argagg_value_list avs (avals);
420 : 27851654 : val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
421 : 27851654 : c->by_ref);
422 : : }
423 : : }
424 : : else
425 : : {
426 : 24522315 : val = avals->safe_sval_at (c->operand_num);
427 : 24522315 : if (val && val == error_mark_node
428 : 2307028 : && c->code != ipa_predicate::changed)
429 : : val = NULL_TREE;
430 : : }
431 : :
432 : 39878780 : if (!val
433 : 38987428 : && (c->code == ipa_predicate::changed
434 : : || c->code == ipa_predicate::is_not_constant))
435 : : {
436 : 25049375 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
437 : 25049375 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
438 : 25049375 : continue;
439 : : }
440 : 27333049 : if (c->code == ipa_predicate::changed)
441 : : {
442 : 7398733 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
443 : 7398733 : continue;
444 : : }
445 : :
446 : 19934316 : if (c->code == ipa_predicate::is_not_constant)
447 : : {
448 : 5279 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
449 : 5279 : continue;
450 : : }
451 : :
452 : 19929037 : if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val)))
453 : : {
454 : 5963372 : if (c->type != TREE_TYPE (val))
455 : 1072024 : val = fold_unary (VIEW_CONVERT_EXPR, c->type, val);
456 : 6320284 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
457 : : {
458 : 357357 : if (!val)
459 : : break;
460 : 356912 : if (!op->val[0])
461 : 112652 : val = fold_unary (op->code, op->type, val);
462 : 244260 : else if (!op->val[1])
463 : 488520 : val = fold_binary (op->code, op->type,
464 : : op->index ? op->val[0] : val,
465 : : op->index ? val : op->val[0]);
466 : 0 : else if (op->index == 0)
467 : 0 : val = fold_ternary (op->code, op->type,
468 : : val, op->val[0], op->val[1]);
469 : 0 : else if (op->index == 1)
470 : 0 : val = fold_ternary (op->code, op->type,
471 : : op->val[0], val, op->val[1]);
472 : 0 : else if (op->index == 2)
473 : 0 : val = fold_ternary (op->code, op->type,
474 : : op->val[0], op->val[1], val);
475 : : else
476 : : val = NULL_TREE;
477 : : }
478 : :
479 : 5963372 : res = val
480 : 5963372 : ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
481 : : : NULL;
482 : :
483 : 5962907 : if (res && integer_zerop (res))
484 : 3079141 : continue;
485 : 2884231 : if (res && integer_onep (res))
486 : : {
487 : 2862706 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
488 : 2862706 : nonspec_clause
489 : 2862706 : |= 1 << (i + ipa_predicate::first_dynamic_condition);
490 : 2862706 : continue;
491 : : }
492 : : }
493 : 13987190 : if (c->operand_num < (int) avals->m_known_value_ranges.length ()
494 : 8180116 : && !c->agg_contents
495 : 16222384 : && (!val || TREE_CODE (val) != INTEGER_CST))
496 : : {
497 : 2234881 : value_range vr (avals->m_known_value_ranges[c->operand_num]);
498 : 2234881 : if (!vr.undefined_p ()
499 : 1194178 : && !vr.varying_p ()
500 : 3429059 : && (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ())))
501 : : {
502 : 1193156 : if (!useless_type_conversion_p (c->type, vr.type ()))
503 : 1109 : range_cast (vr, c->type);
504 : :
505 : 1507264 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
506 : : {
507 : 351475 : if (vr.varying_p () || vr.undefined_p ())
508 : : break;
509 : :
510 : 314108 : value_range res (op->type);
511 : 314108 : if (!op->val[0])
512 : : {
513 : 120721 : value_range varying (op->type);
514 : 120721 : varying.set_varying (op->type);
515 : 120721 : range_op_handler handler (op->code);
516 : 120721 : if (!handler
517 : 120721 : || !res.supports_type_p (op->type)
518 : 241442 : || !handler.fold_range (res, op->type, vr, varying))
519 : 0 : res.set_varying (op->type);
520 : 120721 : }
521 : 193387 : else if (!op->val[1])
522 : : {
523 : 193215 : value_range op0 (TREE_TYPE (op->val[0]));
524 : 193215 : range_op_handler handler (op->code);
525 : :
526 : 193215 : ipa_get_range_from_ip_invariant (op0, op->val[0], node);
527 : :
528 : 193215 : if (!handler
529 : 193215 : || !res.supports_type_p (op->type)
530 : 386430 : || !handler.fold_range (res, op->type,
531 : 193215 : op->index ? op0 : vr,
532 : 385834 : op->index ? vr : op0))
533 : 0 : res.set_varying (op->type);
534 : 193215 : }
535 : : else
536 : 172 : res.set_varying (op->type);
537 : 314108 : vr = res;
538 : 314108 : }
539 : 1193156 : if (!vr.varying_p () && !vr.undefined_p ())
540 : : {
541 : 1141448 : int_range<2> res;
542 : 1141448 : value_range val_vr (TREE_TYPE (c->val));
543 : 1141448 : range_op_handler handler (c->code);
544 : :
545 : 1141448 : ipa_get_range_from_ip_invariant (val_vr, c->val, node);
546 : :
547 : 1141448 : if (!handler
548 : 1141448 : || !val_vr.supports_type_p (TREE_TYPE (c->val))
549 : 2282896 : || !handler.fold_range (res, boolean_type_node, vr, val_vr))
550 : 0 : res.set_varying (boolean_type_node);
551 : :
552 : 1141448 : if (res.zero_p ())
553 : 205106 : continue;
554 : 1141448 : }
555 : : }
556 : 2234881 : }
557 : :
558 : 13782084 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
559 : 13782084 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
560 : : }
561 : 22325285 : *ret_clause = clause;
562 : 22325285 : if (ret_nonspec_clause)
563 : 19362975 : *ret_nonspec_clause = nonspec_clause;
564 : 22325285 : }
565 : :
566 : : /* Return true if VRP will be exectued on the function.
567 : : We do not want to anticipate optimizations that will not happen.
568 : :
569 : : FIXME: This can be confused with -fdisable and debug counters and thus
570 : : it should not be used for correctness (only to make heuristics work).
571 : : This means that inliner should do its own optimizations of expressions
572 : : that it predicts to be constant so wrong code can not be triggered by
573 : : builtin_constant_p. */
574 : :
575 : : static bool
576 : 12093881 : vrp_will_run_p (struct cgraph_node *node)
577 : : {
578 : 12093881 : return (opt_for_fn (node->decl, optimize)
579 : 12093881 : && !opt_for_fn (node->decl, optimize_debug)
580 : 24186546 : && opt_for_fn (node->decl, flag_tree_vrp));
581 : : }
582 : :
583 : : /* Similarly about FRE. */
584 : :
585 : : static bool
586 : 14473073 : fre_will_run_p (struct cgraph_node *node)
587 : : {
588 : 14473073 : return (opt_for_fn (node->decl, optimize)
589 : 14473073 : && !opt_for_fn (node->decl, optimize_debug)
590 : 28943866 : && opt_for_fn (node->decl, flag_tree_fre));
591 : : }
592 : :
593 : : /* Work out what conditions might be true at invocation of E.
594 : : Compute costs for inlined edge if INLINE_P is true.
595 : :
596 : : Return in CLAUSE_PTR the evaluated conditions and in NONSPEC_CLAUSE_PTR
597 : : (if non-NULL) conditions evaluated for nonspecialized clone called
598 : : in a given context.
599 : :
600 : : Vectors in AVALS will be populated with useful known information about
601 : : argument values - information not known to have any uses will be omitted -
602 : : except for m_known_contexts which will only be calculated if
603 : : COMPUTE_CONTEXTS is true. */
604 : :
605 : : void
606 : 22133780 : evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
607 : : clause_t *clause_ptr,
608 : : clause_t *nonspec_clause_ptr,
609 : : ipa_auto_call_arg_values *avals,
610 : : bool compute_contexts)
611 : : {
612 : 22133780 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
613 : 22133780 : class ipa_fn_summary *info = ipa_fn_summaries->get (callee);
614 : 22133780 : class ipa_edge_args *args;
615 : 22133780 : class ipa_call_summary *es = NULL;
616 : :
617 : 22133780 : if (clause_ptr)
618 : 22133780 : *clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
619 : :
620 : 22133780 : if (ipa_node_params_sum
621 : 10041212 : && !e->call_stmt_cannot_inline_p
622 : 10041212 : && (info->conds || compute_contexts)
623 : 32174992 : && (args = ipa_edge_args_sum->get (e)) != NULL)
624 : : {
625 : 9987038 : struct cgraph_node *caller;
626 : 9987038 : class ipa_node_params *caller_parms_info, *callee_pi = NULL;
627 : 9987038 : int i, count = ipa_get_cs_argument_count (args);
628 : 9987038 : es = ipa_call_summaries->get (e);
629 : :
630 : 9987038 : if (count)
631 : : {
632 : 9285431 : if (e->caller->inlined_to)
633 : : caller = e->caller->inlined_to;
634 : : else
635 : 7486671 : caller = e->caller;
636 : 9285431 : caller_parms_info = ipa_node_params_sum->get (caller);
637 : 9285431 : callee_pi = ipa_node_params_sum->get (callee);
638 : :
639 : : /* Watch for thunks. */
640 : 9285431 : if (callee_pi)
641 : : /* Watch for variadic functions. */
642 : 9285086 : count = MIN (count, ipa_get_param_count (callee_pi));
643 : : }
644 : :
645 : 9285086 : if (callee_pi)
646 : 30602373 : for (i = 0; i < count; i++)
647 : : {
648 : 21317287 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
649 : :
650 : 21317287 : if (ipa_is_param_used_by_indirect_call (callee_pi, i)
651 : 21317287 : || ipa_is_param_used_by_ipa_predicates (callee_pi, i))
652 : : {
653 : : /* Determine if we know constant value of the parameter. */
654 : 14473073 : tree type = ipa_get_type (callee_pi, i);
655 : 14473073 : tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type);
656 : :
657 : 11363050 : if (!cst && e->call_stmt
658 : 25789324 : && i < (int)gimple_call_num_args (e->call_stmt))
659 : : {
660 : 11316251 : cst = gimple_call_arg (e->call_stmt, i);
661 : 11316251 : if (!is_gimple_min_invariant (cst))
662 : : cst = NULL;
663 : : }
664 : 3351241 : if (cst)
665 : : {
666 : 3304442 : gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
667 : 3304442 : if (!avals->m_known_vals.length ())
668 : 1751804 : avals->m_known_vals.safe_grow_cleared (count, true);
669 : 3304442 : avals->m_known_vals[i] = cst;
670 : : }
671 : 11168631 : else if (inline_p && !es->param[i].change_prob)
672 : : {
673 : 4188932 : if (!avals->m_known_vals.length ())
674 : 3460131 : avals->m_known_vals.safe_grow_cleared (count, true);
675 : 4188932 : avals->m_known_vals[i] = error_mark_node;
676 : : }
677 : :
678 : : /* If we failed to get simple constant, try value range. */
679 : 3304442 : if ((!cst || TREE_CODE (cst) != INTEGER_CST)
680 : 12093881 : && vrp_will_run_p (caller)
681 : 26409196 : && ipa_is_param_used_by_ipa_predicates (callee_pi, i))
682 : : {
683 : 11895958 : value_range vr (type);
684 : :
685 : 11895958 : ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type);
686 : 11895958 : if (!vr.undefined_p () && !vr.varying_p ())
687 : : {
688 : 8302846 : if (!avals->m_known_value_ranges.length ())
689 : : {
690 : 5997639 : avals->m_known_value_ranges.safe_grow_cleared (count,
691 : : true);
692 : 19129954 : for (int i = 0; i < count; ++i)
693 : 13132315 : avals->m_known_value_ranges[i].set_type (void_type_node);
694 : : }
695 : 8302846 : avals->m_known_value_ranges[i] = vr;
696 : : }
697 : 11895958 : }
698 : :
699 : : /* Determine known aggregate values. */
700 : 14473073 : if (fre_will_run_p (caller))
701 : 14470393 : ipa_push_agg_values_from_jfunc (caller_parms_info,
702 : : caller, &jf->agg, i,
703 : : &avals->m_known_aggs);
704 : : }
705 : :
706 : : /* For calls used in polymorphic calls we further determine
707 : : polymorphic call context. */
708 : 21317287 : if (compute_contexts
709 : 21317287 : && ipa_is_param_used_by_polymorphic_call (callee_pi, i))
710 : : {
711 : 214721 : ipa_polymorphic_call_context
712 : 214721 : ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
713 : 429442 : if (!ctx.useless_p ())
714 : : {
715 : 212837 : if (!avals->m_known_contexts.length ())
716 : 212700 : avals->m_known_contexts.safe_grow_cleared (count, true);
717 : 212837 : avals->m_known_contexts[i]
718 : 425674 : = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
719 : : }
720 : : }
721 : : }
722 : : else
723 : 701952 : gcc_assert (!count || callee->thunk);
724 : : }
725 : 12146742 : else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds)
726 : : {
727 : 9541293 : int i, count = (int)gimple_call_num_args (e->call_stmt);
728 : :
729 : 27012454 : for (i = 0; i < count; i++)
730 : : {
731 : 17471161 : tree cst = gimple_call_arg (e->call_stmt, i);
732 : 17471161 : if (!is_gimple_min_invariant (cst))
733 : : cst = NULL;
734 : 6374468 : if (cst)
735 : : {
736 : 6374468 : if (!avals->m_known_vals.length ())
737 : 4439551 : avals->m_known_vals.safe_grow_cleared (count, true);
738 : 6374468 : avals->m_known_vals[i] = cst;
739 : : }
740 : : }
741 : : }
742 : :
743 : 22133780 : evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr,
744 : : nonspec_clause_ptr, es);
745 : 22133780 : }
746 : :
747 : :
748 : : /* Allocate the function summary. */
749 : :
750 : : static void
751 : 458133 : ipa_fn_summary_alloc (void)
752 : : {
753 : 458133 : gcc_checking_assert (!ipa_fn_summaries);
754 : 458133 : ipa_size_summaries = new ipa_size_summary_t (symtab);
755 : 458133 : ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab);
756 : 458133 : ipa_call_summaries = new ipa_call_summary_t (symtab);
757 : 458133 : }
758 : :
759 : 26754021 : ipa_call_summary::~ipa_call_summary ()
760 : : {
761 : 26754021 : if (predicate)
762 : 3223282 : edge_predicate_pool.remove (predicate);
763 : :
764 : 26754021 : param.release ();
765 : 26754021 : }
766 : :
767 : 10091462 : ipa_fn_summary::~ipa_fn_summary ()
768 : : {
769 : 10091462 : unsigned len = vec_safe_length (loop_iterations);
770 : 10212071 : for (unsigned i = 0; i < len; i++)
771 : 120609 : edge_predicate_pool.remove ((*loop_iterations)[i].predicate);
772 : 10091462 : len = vec_safe_length (loop_strides);
773 : 10134578 : for (unsigned i = 0; i < len; i++)
774 : 43116 : edge_predicate_pool.remove ((*loop_strides)[i].predicate);
775 : 10091462 : vec_free (conds);
776 : 10091462 : call_size_time_table.release ();
777 : 10091462 : vec_free (loop_iterations);
778 : 10091462 : vec_free (loop_strides);
779 : 10091462 : builtin_constant_p_parms.release ();
780 : 10091462 : }
781 : :
782 : : void
783 : 7295108 : ipa_fn_summary_t::remove_callees (cgraph_node *node)
784 : : {
785 : 7295108 : cgraph_edge *e;
786 : 29680252 : for (e = node->callees; e; e = e->next_callee)
787 : 22385144 : ipa_call_summaries->remove (e);
788 : 7779605 : for (e = node->indirect_calls; e; e = e->next_callee)
789 : 484497 : ipa_call_summaries->remove (e);
790 : 7295108 : }
791 : :
792 : : /* Duplicate predicates in loop hint vector, allocating memory for them and
793 : : remove and deallocate any uninteresting (true or false) ones. Return the
794 : : result. */
795 : :
796 : : static vec<ipa_freqcounting_predicate, va_gc> *
797 : 25992 : remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v,
798 : : clause_t possible_truths)
799 : : {
800 : 25992 : if (vec_safe_length (v) == 0)
801 : : return NULL;
802 : :
803 : 3791 : vec<ipa_freqcounting_predicate, va_gc> *res = v->copy ();
804 : 3791 : int len = res->length();
805 : 9243 : for (int i = len - 1; i >= 0; i--)
806 : : {
807 : 5452 : ipa_predicate new_predicate
808 : 5452 : = (*res)[i].predicate->remap_after_duplication (possible_truths);
809 : : /* We do not want to free previous predicate; it is used by node
810 : : origin. */
811 : 5452 : (*res)[i].predicate = NULL;
812 : 5452 : set_hint_predicate (&(*res)[i].predicate, new_predicate);
813 : :
814 : 5452 : if (!(*res)[i].predicate)
815 : 2268 : res->unordered_remove (i);
816 : : }
817 : :
818 : : return res;
819 : : }
820 : :
821 : :
822 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
823 : : void
824 : 2708805 : ipa_fn_summary_t::duplicate (cgraph_node *src,
825 : : cgraph_node *dst,
826 : : ipa_fn_summary *src_info,
827 : : ipa_fn_summary *info)
828 : : {
829 : 2708805 : new (info) ipa_fn_summary (*src_info);
830 : : /* TODO: as an optimization, we may avoid copying conditions
831 : : that are known to be false or true. */
832 : 2708805 : info->conds = vec_safe_copy (info->conds);
833 : :
834 : 2708805 : clone_info *cinfo = clone_info::get (dst);
835 : : /* When there are any replacements in the function body, see if we can figure
836 : : out that something was optimized out. */
837 : 2708805 : if (ipa_node_params_sum && cinfo && cinfo->tree_map)
838 : : {
839 : : /* Use SRC parm info since it may not be copied yet. */
840 : 12996 : ipa_node_params *parms_info = ipa_node_params_sum->get (src);
841 : 12996 : ipa_auto_call_arg_values avals;
842 : 12996 : int count = ipa_get_param_count (parms_info);
843 : 12996 : int i, j;
844 : 12996 : clause_t possible_truths;
845 : 12996 : ipa_predicate true_pred = true;
846 : 12996 : size_time_entry *e;
847 : 12996 : int optimized_out_size = 0;
848 : 12996 : bool inlined_to_p = false;
849 : 12996 : struct cgraph_edge *edge, *next;
850 : :
851 : 12996 : info->size_time_table.release ();
852 : 12996 : avals.m_known_vals.safe_grow_cleared (count, true);
853 : 53438 : for (i = 0; i < count; i++)
854 : : {
855 : : struct ipa_replace_map *r;
856 : :
857 : 132930 : for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++)
858 : : {
859 : 73494 : if (r->parm_num == i)
860 : : {
861 : 21448 : avals.m_known_vals[i] = r->new_tree;
862 : 21448 : break;
863 : : }
864 : : }
865 : : }
866 : 12996 : evaluate_conditions_for_known_args (dst, false,
867 : : &avals,
868 : : &possible_truths,
869 : : /* We are going to specialize,
870 : : so ignore nonspec truths. */
871 : : NULL,
872 : : NULL);
873 : :
874 : 12996 : info->account_size_time (0, 0, true_pred, true_pred);
875 : :
876 : : /* Remap size_time vectors.
877 : : Simplify the predicate by pruning out alternatives that are known
878 : : to be false.
879 : : TODO: as on optimization, we can also eliminate conditions known
880 : : to be true. */
881 : 96560 : for (i = 0; src_info->size_time_table.iterate (i, &e); i++)
882 : : {
883 : 83564 : ipa_predicate new_exec_pred;
884 : 83564 : ipa_predicate new_nonconst_pred;
885 : 83564 : new_exec_pred = e->exec_predicate.remap_after_duplication
886 : 83564 : (possible_truths);
887 : 83564 : new_nonconst_pred = e->nonconst_predicate.remap_after_duplication
888 : 83564 : (possible_truths);
889 : 83564 : if (new_exec_pred == false || new_nonconst_pred == false)
890 : 9810 : optimized_out_size += e->size;
891 : : else
892 : 73754 : info->account_size_time (e->size, e->time, new_exec_pred,
893 : : new_nonconst_pred);
894 : : }
895 : :
896 : : /* Remap edge predicates with the same simplification as above.
897 : : Also copy constantness arrays. */
898 : 186142 : for (edge = dst->callees; edge; edge = next)
899 : : {
900 : 173146 : ipa_predicate new_predicate;
901 : 173146 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
902 : 173146 : next = edge->next_callee;
903 : :
904 : 173146 : if (!edge->inline_failed)
905 : 0 : inlined_to_p = true;
906 : 173146 : if (!es->predicate)
907 : 159762 : continue;
908 : 13384 : new_predicate = es->predicate->remap_after_duplication
909 : 13384 : (possible_truths);
910 : 13384 : if (new_predicate == false && *es->predicate != false)
911 : 2655 : optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale;
912 : 13384 : edge_set_predicate (edge, &new_predicate);
913 : : }
914 : :
915 : : /* Remap indirect edge predicates with the same simplification as above.
916 : : Also copy constantness arrays. */
917 : 13868 : for (edge = dst->indirect_calls; edge; edge = next)
918 : : {
919 : 872 : ipa_predicate new_predicate;
920 : 872 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
921 : 872 : next = edge->next_callee;
922 : :
923 : 872 : gcc_checking_assert (edge->inline_failed);
924 : 872 : if (!es->predicate)
925 : 744 : continue;
926 : 128 : new_predicate = es->predicate->remap_after_duplication
927 : 128 : (possible_truths);
928 : 128 : if (new_predicate == false && *es->predicate != false)
929 : 24 : optimized_out_size
930 : 24 : += es->call_stmt_size * ipa_fn_summary::size_scale;
931 : 128 : edge_set_predicate (edge, &new_predicate);
932 : : }
933 : 12996 : info->loop_iterations
934 : 12996 : = remap_freqcounting_preds_after_dup (info->loop_iterations,
935 : : possible_truths);
936 : 12996 : info->loop_strides
937 : 12996 : = remap_freqcounting_preds_after_dup (info->loop_strides,
938 : : possible_truths);
939 : 12996 : if (info->builtin_constant_p_parms.length())
940 : : {
941 : 25 : vec <int, va_heap, vl_ptr> parms = info->builtin_constant_p_parms;
942 : 25 : int ip;
943 : 25 : info->builtin_constant_p_parms = vNULL;
944 : 50 : for (i = 0; parms.iterate (i, &ip); i++)
945 : 25 : if (!avals.m_known_vals[ip])
946 : 4 : info->builtin_constant_p_parms.safe_push (ip);
947 : : }
948 : :
949 : : /* If inliner or someone after inliner will ever start producing
950 : : non-trivial clones, we will get trouble with lack of information
951 : : about updating self sizes, because size vectors already contains
952 : : sizes of the callees. */
953 : 12996 : gcc_assert (!inlined_to_p || !optimized_out_size);
954 : 12996 : }
955 : : else
956 : : {
957 : 2695809 : info->size_time_table = src_info->size_time_table.copy ();
958 : 2695809 : info->loop_iterations = vec_safe_copy (src_info->loop_iterations);
959 : 2695809 : info->loop_strides = vec_safe_copy (info->loop_strides);
960 : :
961 : 2695809 : info->builtin_constant_p_parms
962 : 2695809 : = info->builtin_constant_p_parms.copy ();
963 : :
964 : 2695809 : ipa_freqcounting_predicate *f;
965 : 2752910 : for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++)
966 : : {
967 : 57101 : ipa_predicate p = *f->predicate;
968 : 57101 : f->predicate = NULL;
969 : 57101 : set_hint_predicate (&f->predicate, p);
970 : : }
971 : 2729415 : for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++)
972 : : {
973 : 33606 : ipa_predicate p = *f->predicate;
974 : 33606 : f->predicate = NULL;
975 : 33606 : set_hint_predicate (&f->predicate, p);
976 : : }
977 : : }
978 : 2708805 : if (!dst->inlined_to)
979 : 131380 : ipa_update_overall_fn_summary (dst);
980 : 2708805 : }
981 : :
982 : :
983 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
984 : :
985 : : void
986 : 3534072 : ipa_call_summary_t::duplicate (struct cgraph_edge *src,
987 : : struct cgraph_edge *dst,
988 : : class ipa_call_summary *srcinfo,
989 : : class ipa_call_summary *info)
990 : : {
991 : 3534072 : new (info) ipa_call_summary (*srcinfo);
992 : 3534072 : info->predicate = NULL;
993 : 3534072 : edge_set_predicate (dst, srcinfo->predicate);
994 : 3534072 : info->param = srcinfo->param.copy ();
995 : 3534072 : if (!dst->indirect_unknown_callee && src->indirect_unknown_callee
996 : : /* Don't subtract the size when dealing with callback pairs, since the
997 : : edge has no real size. */
998 : 25111 : && !src->has_callback && !dst->callback)
999 : : {
1000 : 25111 : info->call_stmt_size -= (eni_size_weights.indirect_call_cost
1001 : 25111 : - eni_size_weights.call_cost);
1002 : 25111 : info->call_stmt_time -= (eni_time_weights.indirect_call_cost
1003 : 25111 : - eni_time_weights.call_cost);
1004 : : }
1005 : 3534072 : }
1006 : :
1007 : : /* Dump edge summaries associated to NODE and recursively to all clones.
1008 : : Indent by INDENT. */
1009 : :
1010 : : static void
1011 : 1961 : dump_ipa_call_summary (FILE *f, int indent, struct cgraph_node *node,
1012 : : class ipa_fn_summary *info)
1013 : : {
1014 : 1961 : struct cgraph_edge *edge;
1015 : 9771 : for (edge = node->callees; edge; edge = edge->next_callee)
1016 : : {
1017 : 7810 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1018 : 7810 : struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1019 : 7810 : int i;
1020 : :
1021 : 15620 : fprintf (f,
1022 : : "%*s%s %s\n%*s freq:%4.2f",
1023 : : indent, "", callee->dump_name (),
1024 : 7810 : !edge->inline_failed
1025 : 7257 : ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1026 : 7810 : indent, "", edge->sreal_frequency ().to_double ());
1027 : :
1028 : 7810 : if (cross_module_call_p (edge))
1029 : 4 : fprintf (f, " cross module");
1030 : :
1031 : 7810 : if (es)
1032 : 7257 : fprintf (f, " loop depth:%2i size:%2i time: %2i",
1033 : : es->loop_depth, es->call_stmt_size, es->call_stmt_time);
1034 : :
1035 : 7810 : ipa_fn_summary *s = ipa_fn_summaries->get (callee);
1036 : 7810 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1037 : 7810 : if (s != NULL)
1038 : 612 : fprintf (f, " callee size:%2i stack:%2i",
1039 : 612 : (int) (ss->size / ipa_fn_summary::size_scale),
1040 : 612 : (int) s->estimated_stack_size);
1041 : :
1042 : 7810 : if (es && es->predicate)
1043 : : {
1044 : 4627 : fprintf (f, " predicate: ");
1045 : 4627 : es->predicate->dump (f, info->conds);
1046 : : }
1047 : : else
1048 : 3183 : fprintf (f, "\n");
1049 : 7810 : if (es && es->param.exists ())
1050 : 5340 : for (i = 0; i < (int) es->param.length (); i++)
1051 : : {
1052 : 1580 : int prob = es->param[i].change_prob;
1053 : :
1054 : 1580 : if (!prob)
1055 : 612 : fprintf (f, "%*s op%i is compile time invariant\n",
1056 : : indent + 2, "", i);
1057 : 968 : else if (prob != REG_BR_PROB_BASE)
1058 : 35 : fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1059 : 35 : prob * 100.0 / REG_BR_PROB_BASE);
1060 : 1580 : if (es->param[i].points_to_local_or_readonly_memory)
1061 : 400 : fprintf (f, "%*s op%i points to local or readonly memory\n",
1062 : : indent + 2, "", i);
1063 : 1580 : if (es->param[i].points_to_possible_sra_candidate)
1064 : 227 : fprintf (f, "%*s op%i points to possible sra candidate\n",
1065 : : indent + 2, "", i);
1066 : : }
1067 : 7810 : if (!edge->inline_failed)
1068 : : {
1069 : 553 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1070 : 553 : fprintf (f, "%*sStack frame offset %i, callee self size %i\n",
1071 : : indent + 2, "",
1072 : 553 : (int) ipa_get_stack_frame_offset (callee),
1073 : 553 : (int) ss->estimated_self_stack_size);
1074 : 553 : dump_ipa_call_summary (f, indent + 2, callee, info);
1075 : : }
1076 : : }
1077 : 2259 : for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1078 : : {
1079 : 298 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1080 : 298 : fprintf (f, "%*sindirect call loop depth:%2i freq:%4.2f size:%2i"
1081 : : " time: %2i",
1082 : : indent, "",
1083 : : es->loop_depth,
1084 : 298 : edge->sreal_frequency ().to_double (), es->call_stmt_size,
1085 : : es->call_stmt_time);
1086 : 298 : if (es->predicate)
1087 : : {
1088 : 6 : fprintf (f, "predicate: ");
1089 : 6 : es->predicate->dump (f, info->conds);
1090 : : }
1091 : : else
1092 : 292 : fprintf (f, "\n");
1093 : : }
1094 : 1961 : }
1095 : :
1096 : :
1097 : : void
1098 : 1614 : ipa_dump_fn_summary (FILE *f, struct cgraph_node *node)
1099 : : {
1100 : 1614 : if (node->definition)
1101 : : {
1102 : 1614 : class ipa_fn_summary *s = ipa_fn_summaries->get (node);
1103 : 1614 : class ipa_size_summary *ss = ipa_size_summaries->get (node);
1104 : 1614 : if (s != NULL)
1105 : : {
1106 : 1408 : size_time_entry *e;
1107 : 1408 : int i;
1108 : 1408 : fprintf (f, "IPA function summary for %s", node->dump_name ());
1109 : 1408 : if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1110 : 0 : fprintf (f, " always_inline");
1111 : 1408 : if (s->inlinable)
1112 : 1239 : fprintf (f, " inlinable");
1113 : 1408 : if (s->fp_expressions)
1114 : 44 : fprintf (f, " fp_expression");
1115 : 1408 : if (s->builtin_constant_p_parms.length ())
1116 : : {
1117 : 2 : fprintf (f, " builtin_constant_p_parms");
1118 : 4 : for (unsigned int i = 0;
1119 : 4 : i < s->builtin_constant_p_parms.length (); i++)
1120 : 2 : fprintf (f, " %i", s->builtin_constant_p_parms[i]);
1121 : : }
1122 : 1408 : fprintf (f, "\n global time: %f\n", s->time.to_double ());
1123 : 1408 : fprintf (f, " self size: %i\n", ss->self_size);
1124 : 1408 : fprintf (f, " global size: %i\n", ss->size);
1125 : 1408 : fprintf (f, " min size: %i\n", s->min_size);
1126 : 1408 : fprintf (f, " self stack: %i\n",
1127 : 1408 : (int) ss->estimated_self_stack_size);
1128 : 1408 : fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1129 : 1408 : if (s->growth)
1130 : 165 : fprintf (f, " estimated growth:%i\n", (int) s->growth);
1131 : 1408 : if (s->scc_no)
1132 : 5 : fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1133 : 5890 : for (i = 0; s->size_time_table.iterate (i, &e); i++)
1134 : : {
1135 : 4482 : fprintf (f, " size:%f, time:%f",
1136 : 4482 : (double) e->size / ipa_fn_summary::size_scale,
1137 : : e->time.to_double ());
1138 : 4482 : if (e->exec_predicate != true)
1139 : : {
1140 : 2619 : fprintf (f, ", executed if:");
1141 : 2619 : e->exec_predicate.dump (f, s->conds, 0);
1142 : : }
1143 : 4482 : if (e->exec_predicate != e->nonconst_predicate)
1144 : : {
1145 : 1169 : fprintf (f, ", nonconst if:");
1146 : 1169 : e->nonconst_predicate.dump (f, s->conds, 0);
1147 : : }
1148 : 4482 : fprintf (f, "\n");
1149 : : }
1150 : : ipa_freqcounting_predicate *fcp;
1151 : : bool first_fcp = true;
1152 : 1514 : for (int i = 0; vec_safe_iterate (s->loop_iterations, i, &fcp); i++)
1153 : : {
1154 : 106 : if (first_fcp)
1155 : : {
1156 : 78 : fprintf (f, " loop iterations:");
1157 : 78 : first_fcp = false;
1158 : : }
1159 : 106 : fprintf (f, " %3.2f for ", fcp->freq.to_double ());
1160 : 106 : fcp->predicate->dump (f, s->conds);
1161 : : }
1162 : : first_fcp = true;
1163 : 1426 : for (int i = 0; vec_safe_iterate (s->loop_strides, i, &fcp); i++)
1164 : : {
1165 : 18 : if (first_fcp)
1166 : : {
1167 : 10 : fprintf (f, " loop strides:");
1168 : 10 : first_fcp = false;
1169 : : }
1170 : 18 : fprintf (f, " %3.2f for :", fcp->freq.to_double ());
1171 : 18 : fcp->predicate->dump (f, s->conds);
1172 : : }
1173 : 1408 : fprintf (f, " calls:\n");
1174 : 1408 : dump_ipa_call_summary (f, 4, node, s);
1175 : 1408 : fprintf (f, "\n");
1176 : 1408 : if (s->target_info)
1177 : 0 : fprintf (f, " target_info: %x\n", s->target_info);
1178 : : }
1179 : : else
1180 : 206 : fprintf (f, "IPA summary for %s is missing.\n", node->dump_name ());
1181 : : }
1182 : 1614 : }
1183 : :
1184 : : DEBUG_FUNCTION void
1185 : 0 : ipa_debug_fn_summary (struct cgraph_node *node)
1186 : : {
1187 : 0 : ipa_dump_fn_summary (stderr, node);
1188 : 0 : }
1189 : :
1190 : : void
1191 : 356 : ipa_dump_fn_summaries (FILE *f)
1192 : : {
1193 : 356 : struct cgraph_node *node;
1194 : :
1195 : 2294 : FOR_EACH_DEFINED_FUNCTION (node)
1196 : 1938 : if (!node->inlined_to)
1197 : 1385 : ipa_dump_fn_summary (f, node);
1198 : 356 : }
1199 : :
1200 : : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1201 : : boolean variable pointed to by DATA. */
1202 : :
1203 : : static bool
1204 : 942666 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1205 : : void *data)
1206 : : {
1207 : 942666 : bool *b = (bool *) data;
1208 : 942666 : *b = true;
1209 : 942666 : return true;
1210 : : }
1211 : :
1212 : : /* If OP refers to value of function parameter, return the corresponding
1213 : : parameter. If non-NULL, the size of the memory load (or the SSA_NAME of the
1214 : : PARM_DECL) will be stored to *SIZE_P in that case too. */
1215 : :
1216 : : static tree
1217 : 184163848 : unmodified_parm_1 (ipa_func_body_info *fbi, gimple *stmt, tree op,
1218 : : poly_int64 *size_p)
1219 : : {
1220 : : /* SSA_NAME referring to parm default def? */
1221 : 184163848 : if (TREE_CODE (op) == SSA_NAME
1222 : 107630420 : && SSA_NAME_IS_DEFAULT_DEF (op)
1223 : 209722514 : && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1224 : : {
1225 : 25361014 : if (size_p)
1226 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1227 : 25361014 : return SSA_NAME_VAR (op);
1228 : : }
1229 : : /* Non-SSA parm reference? */
1230 : 158802834 : if (TREE_CODE (op) == PARM_DECL
1231 : 1484617 : && fbi->aa_walk_budget > 0)
1232 : : {
1233 : 1482071 : bool modified = false;
1234 : :
1235 : 1482071 : ao_ref refd;
1236 : 1482071 : ao_ref_init (&refd, op);
1237 : 2964142 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
1238 : : mark_modified, &modified, NULL, NULL,
1239 : : fbi->aa_walk_budget);
1240 : 1482071 : if (walked < 0)
1241 : : {
1242 : 8 : fbi->aa_walk_budget = 0;
1243 : 942430 : return NULL_TREE;
1244 : : }
1245 : 1482063 : fbi->aa_walk_budget -= walked;
1246 : 1482063 : if (!modified)
1247 : : {
1248 : 942422 : if (size_p)
1249 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1250 : 942422 : return op;
1251 : : }
1252 : : }
1253 : : return NULL_TREE;
1254 : : }
1255 : :
1256 : : /* If OP refers to value of function parameter, return the corresponding
1257 : : parameter. Also traverse chains of SSA register assignments. If non-NULL,
1258 : : the size of the memory load (or the SSA_NAME of the PARM_DECL) will be
1259 : : stored to *SIZE_P in that case too. */
1260 : :
1261 : : static tree
1262 : 120256554 : unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op,
1263 : : poly_int64 *size_p)
1264 : : {
1265 : 149571911 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1266 : 149571911 : if (res)
1267 : : return res;
1268 : :
1269 : 124063134 : if (TREE_CODE (op) == SSA_NAME
1270 : 70278665 : && !SSA_NAME_IS_DEFAULT_DEF (op)
1271 : 194148597 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1272 : 29315357 : return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op),
1273 : 29315357 : gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)),
1274 : 29315357 : size_p);
1275 : : return NULL_TREE;
1276 : : }
1277 : :
1278 : : /* If OP refers to a value of a function parameter or value loaded from an
1279 : : aggregate passed to a parameter (either by value or reference), return TRUE
1280 : : and store the number of the parameter to *INDEX_P, the access size into
1281 : : *SIZE_P, and information whether and how it has been loaded from an
1282 : : aggregate into *AGGPOS. INFO describes the function parameters, STMT is the
1283 : : statement in which OP is used or loaded. */
1284 : :
1285 : : static bool
1286 : 33074853 : unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi,
1287 : : gimple *stmt, tree op, int *index_p,
1288 : : poly_int64 *size_p,
1289 : : struct agg_position_info *aggpos)
1290 : : {
1291 : 34591937 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1292 : :
1293 : 34591937 : gcc_checking_assert (aggpos);
1294 : 34591937 : if (res)
1295 : : {
1296 : 794659 : *index_p = ipa_get_param_decl_index (fbi->info, res);
1297 : 794659 : if (*index_p < 0)
1298 : : return false;
1299 : 794530 : aggpos->agg_contents = false;
1300 : 794530 : aggpos->by_ref = false;
1301 : 794530 : return true;
1302 : : }
1303 : :
1304 : 33797278 : if (TREE_CODE (op) == SSA_NAME)
1305 : : {
1306 : 11990741 : if (SSA_NAME_IS_DEFAULT_DEF (op)
1307 : 11990741 : || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1308 : : return false;
1309 : 4887445 : stmt = SSA_NAME_DEF_STMT (op);
1310 : 4887445 : op = gimple_assign_rhs1 (stmt);
1311 : 4887445 : if (!REFERENCE_CLASS_P (op))
1312 : : return unmodified_parm_or_parm_agg_item (fbi, stmt, op, index_p, size_p,
1313 : : aggpos);
1314 : : }
1315 : :
1316 : 25176898 : aggpos->agg_contents = true;
1317 : 25176898 : return ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
1318 : : stmt, op, index_p, &aggpos->offset,
1319 : 25176898 : size_p, &aggpos->by_ref);
1320 : : }
1321 : :
1322 : : /* If stmt is simple load or store of value pointed to by a function parmaeter,
1323 : : return its index. */
1324 : :
1325 : : static int
1326 : 111566882 : load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt)
1327 : : {
1328 : 111566882 : if (!optimize)
1329 : : return -1;
1330 : 159887787 : gassign *assign = dyn_cast <gassign *> (stmt);
1331 : 55656918 : if (!assign)
1332 : : return -1;
1333 : 55656918 : tree param;
1334 : 55656918 : if (gimple_assign_load_p (stmt))
1335 : 20551715 : param = gimple_assign_rhs1 (stmt);
1336 : 35105203 : else if (gimple_store_p (stmt))
1337 : 18726792 : param = gimple_assign_lhs (stmt);
1338 : : else
1339 : : return -1;
1340 : 39278507 : tree base = get_base_address (param);
1341 : 39278507 : if (TREE_CODE (base) != MEM_REF
1342 : 13632102 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1343 : 52906535 : || !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1344 : : return -1;
1345 : 7422436 : tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1346 : 7422436 : if (TREE_CODE (p) != PARM_DECL)
1347 : : return -1;
1348 : 7336013 : return ipa_get_param_decl_index (fbi->info, p);
1349 : : }
1350 : :
1351 : : /* See if statement might disappear after inlining.
1352 : : 0 - means not eliminated
1353 : : 1 - half of statements goes away
1354 : : 2 - for sure it is eliminated.
1355 : : We are not terribly sophisticated, basically looking for simple abstraction
1356 : : penalty wrappers. */
1357 : :
1358 : : static int
1359 : 111566882 : eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt)
1360 : : {
1361 : 111566882 : enum gimple_code code = gimple_code (stmt);
1362 : 111566882 : enum tree_code rhs_code;
1363 : :
1364 : 111566882 : if (!optimize)
1365 : : return 0;
1366 : :
1367 : 93571556 : switch (code)
1368 : : {
1369 : : case GIMPLE_RETURN:
1370 : : return 2;
1371 : 55656918 : case GIMPLE_ASSIGN:
1372 : 55656918 : if (gimple_num_ops (stmt) != 2)
1373 : : return 0;
1374 : :
1375 : 40090020 : rhs_code = gimple_assign_rhs_code (stmt);
1376 : :
1377 : : /* Casts of parameters, loads from parameters passed by reference
1378 : : and stores to return value or parameters are often free after
1379 : : inlining due to SRA and further combining.
1380 : : Assume that half of statements goes away. */
1381 : 40090020 : if (CONVERT_EXPR_CODE_P (rhs_code)
1382 : : || rhs_code == VIEW_CONVERT_EXPR
1383 : : || rhs_code == ADDR_EXPR
1384 : 37295589 : || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1385 : : {
1386 : 39278507 : tree rhs = gimple_assign_rhs1 (stmt);
1387 : 39278507 : tree lhs = gimple_assign_lhs (stmt);
1388 : 39278507 : tree inner_rhs = get_base_address (rhs);
1389 : 39278507 : tree inner_lhs = get_base_address (lhs);
1390 : 39278507 : bool rhs_free = false;
1391 : 39278507 : bool lhs_free = false;
1392 : :
1393 : 39278507 : if (!inner_rhs)
1394 : 0 : inner_rhs = rhs;
1395 : 39278507 : if (!inner_lhs)
1396 : 0 : inner_lhs = lhs;
1397 : :
1398 : : /* Reads of parameter are expected to be free. */
1399 : 39278507 : if (unmodified_parm (fbi, stmt, inner_rhs, NULL))
1400 : : rhs_free = true;
1401 : : /* Match expressions of form &this->field. Those will most likely
1402 : : combine with something upstream after inlining. */
1403 : 37980174 : else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1404 : : {
1405 : 2496300 : tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1406 : 2496300 : if (TREE_CODE (op) == PARM_DECL)
1407 : : rhs_free = true;
1408 : 2461352 : else if (TREE_CODE (op) == MEM_REF
1409 : 2461352 : && unmodified_parm (fbi, stmt, TREE_OPERAND (op, 0),
1410 : : NULL))
1411 : : rhs_free = true;
1412 : : }
1413 : :
1414 : : /* When parameter is not SSA register because its address is taken
1415 : : and it is just copied into one, the statement will be completely
1416 : : free after inlining (we will copy propagate backward). */
1417 : 1333281 : if (rhs_free && is_gimple_reg (lhs))
1418 : : return 2;
1419 : :
1420 : : /* Reads of parameters passed by reference
1421 : : expected to be free (i.e. optimized out after inlining). */
1422 : 38589290 : if (TREE_CODE (inner_rhs) == MEM_REF
1423 : 38589290 : && unmodified_parm (fbi, stmt, TREE_OPERAND (inner_rhs, 0), NULL))
1424 : : rhs_free = true;
1425 : :
1426 : : /* Copying parameter passed by reference into gimple register is
1427 : : probably also going to copy propagate, but we can't be quite
1428 : : sure. */
1429 : 38589290 : if (rhs_free && is_gimple_reg (lhs))
1430 : : lhs_free = true;
1431 : :
1432 : : /* Writes to parameters, parameters passed by value and return value
1433 : : (either directly or passed via invisible reference) are free.
1434 : :
1435 : : TODO: We ought to handle testcase like
1436 : : struct a {int a,b;};
1437 : : struct a
1438 : : returnstruct (void)
1439 : : {
1440 : : struct a a ={1,2};
1441 : : return a;
1442 : : }
1443 : :
1444 : : This translate into:
1445 : :
1446 : : returnstruct ()
1447 : : {
1448 : : int a$b;
1449 : : int a$a;
1450 : : struct a a;
1451 : : struct a D.2739;
1452 : :
1453 : : <bb 2>:
1454 : : D.2739.a = 1;
1455 : : D.2739.b = 2;
1456 : : return D.2739;
1457 : :
1458 : : }
1459 : : For that we either need to copy ipa-split logic detecting writes
1460 : : to return value. */
1461 : 38589290 : if (TREE_CODE (inner_lhs) == PARM_DECL
1462 : 38498218 : || TREE_CODE (inner_lhs) == RESULT_DECL
1463 : 76238831 : || (TREE_CODE (inner_lhs) == MEM_REF
1464 : 5045055 : && (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0),
1465 : : NULL)
1466 : 2557327 : || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1467 : 2556140 : && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1468 : 367800 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1469 : : (inner_lhs,
1470 : : 0))) == RESULT_DECL))))
1471 : : lhs_free = true;
1472 : 35078439 : if (lhs_free
1473 : 38589290 : && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1474 : : rhs_free = true;
1475 : 38589290 : if (lhs_free && rhs_free)
1476 : : return 1;
1477 : : }
1478 : : return 0;
1479 : : default:
1480 : : return 0;
1481 : : }
1482 : : }
1483 : :
1484 : : /* Analyze EXPR if it represents a series of simple operations performed on
1485 : : a function parameter and return true if so. FBI, STMT, EXPR, INDEX_P and
1486 : : AGGPOS have the same meaning like in unmodified_parm_or_parm_agg_item.
1487 : : Type of the parameter or load from an aggregate via the parameter is
1488 : : stored in *TYPE_P. Operations on the parameter are recorded to
1489 : : PARAM_OPS_P if it is not NULL. */
1490 : :
1491 : : static bool
1492 : 27240115 : decompose_param_expr (struct ipa_func_body_info *fbi,
1493 : : gimple *stmt, tree expr,
1494 : : int *index_p, tree *type_p,
1495 : : struct agg_position_info *aggpos,
1496 : : expr_eval_ops *param_ops_p = NULL)
1497 : : {
1498 : 27240115 : int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops);
1499 : 27240115 : int op_count = 0;
1500 : :
1501 : 27240115 : if (param_ops_p)
1502 : 9274961 : *param_ops_p = NULL;
1503 : :
1504 : 33074853 : while (true)
1505 : : {
1506 : 33074853 : expr_eval_op eval_op;
1507 : 33074853 : unsigned rhs_count;
1508 : 33074853 : unsigned cst_count = 0;
1509 : :
1510 : 33074853 : if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL,
1511 : : aggpos))
1512 : : {
1513 : 4661271 : tree type = TREE_TYPE (expr);
1514 : :
1515 : 4661271 : if (aggpos->agg_contents)
1516 : : {
1517 : : /* Stop if containing bit-field. */
1518 : 3866741 : if (TREE_CODE (expr) == BIT_FIELD_REF
1519 : 3866741 : || contains_bitfld_component_ref_p (expr))
1520 : : break;
1521 : : }
1522 : :
1523 : 4622395 : *type_p = type;
1524 : 4622395 : return true;
1525 : : }
1526 : :
1527 : 28413582 : if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr))
1528 : : break;
1529 : 10601880 : stmt = SSA_NAME_DEF_STMT (expr);
1530 : :
1531 : 10601880 : if (gcall *call = dyn_cast <gcall *> (stmt))
1532 : : {
1533 : 2474131 : int flags = gimple_call_return_flags (call);
1534 : 2474131 : if (!(flags & ERF_RETURNS_ARG))
1535 : 3066404 : goto fail;
1536 : 195464 : int arg = flags & ERF_RETURN_ARG_MASK;
1537 : 195464 : if (arg >= (int)gimple_call_num_args (call))
1538 : 0 : goto fail;
1539 : 195464 : expr = gimple_call_arg (stmt, arg);
1540 : 4063881 : continue;
1541 : 195464 : }
1542 : :
1543 : 8127749 : if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr)))
1544 : : break;
1545 : :
1546 : 6427128 : switch (gimple_assign_rhs_class (stmt))
1547 : : {
1548 : 3868417 : case GIMPLE_SINGLE_RHS:
1549 : 3868417 : expr = gimple_assign_rhs1 (stmt);
1550 : 3868417 : continue;
1551 : :
1552 : : case GIMPLE_UNARY_RHS:
1553 : : rhs_count = 1;
1554 : : break;
1555 : :
1556 : 1546332 : case GIMPLE_BINARY_RHS:
1557 : 1546332 : rhs_count = 2;
1558 : 1546332 : break;
1559 : :
1560 : 642 : case GIMPLE_TERNARY_RHS:
1561 : 642 : rhs_count = 3;
1562 : 642 : break;
1563 : :
1564 : 0 : default:
1565 : 0 : goto fail;
1566 : : }
1567 : :
1568 : : /* Stop if expression is too complex. */
1569 : 2558711 : if (op_count++ == op_limit)
1570 : : break;
1571 : :
1572 : 2558594 : if (param_ops_p)
1573 : : {
1574 : 2557060 : eval_op.code = gimple_assign_rhs_code (stmt);
1575 : 2557060 : eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt));
1576 : 2557060 : eval_op.val[0] = NULL_TREE;
1577 : 2557060 : eval_op.val[1] = NULL_TREE;
1578 : : }
1579 : :
1580 : : expr = NULL_TREE;
1581 : 5876397 : for (unsigned i = 0; i < rhs_count; i++)
1582 : : {
1583 : 4105540 : tree op = gimple_op (stmt, i + 1);
1584 : :
1585 : 4105540 : gcc_assert (op && !TYPE_P (op));
1586 : 4105540 : if (is_gimple_ip_invariant (op))
1587 : : {
1588 : 761493 : if (++cst_count == rhs_count)
1589 : 1142 : goto fail;
1590 : :
1591 : 760351 : eval_op.val[cst_count - 1] = op;
1592 : : }
1593 : 3344047 : else if (!expr)
1594 : : {
1595 : : /* Found a non-constant operand, and record its index in rhs
1596 : : operands. */
1597 : 2557452 : eval_op.index = i;
1598 : 2557452 : expr = op;
1599 : : }
1600 : : else
1601 : : {
1602 : : /* Found more than one non-constant operands. */
1603 : 786595 : goto fail;
1604 : : }
1605 : : }
1606 : :
1607 : 1770857 : if (param_ops_p)
1608 : 1769593 : vec_safe_insert (*param_ops_p, 0, eval_op);
1609 : : }
1610 : :
1611 : : /* Failed to decompose, free resource and return. */
1612 : 22617720 : fail:
1613 : 22617720 : if (param_ops_p)
1614 : 9128590 : vec_free (*param_ops_p);
1615 : :
1616 : : return false;
1617 : : }
1618 : :
1619 : : /* Record to SUMMARY that PARM is used by builtin_constant_p. */
1620 : :
1621 : : static void
1622 : 12172 : add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm)
1623 : : {
1624 : 12172 : int ip;
1625 : :
1626 : : /* Avoid duplicates. */
1627 : 12184 : for (unsigned int i = 0;
1628 : 12184 : summary->builtin_constant_p_parms.iterate (i, &ip); i++)
1629 : 5928 : if (ip == parm)
1630 : 12172 : return;
1631 : 6256 : summary->builtin_constant_p_parms.safe_push (parm);
1632 : : }
1633 : :
1634 : : /* If BB ends by a conditional we can turn into predicates, attach corresponding
1635 : : predicates to the CFG edges. */
1636 : :
1637 : : static void
1638 : 35401915 : set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1639 : : class ipa_fn_summary *summary,
1640 : : class ipa_node_params *params_summary,
1641 : : basic_block bb)
1642 : : {
1643 : 35401915 : tree op, op2;
1644 : 35401915 : int index;
1645 : 35401915 : struct agg_position_info aggpos;
1646 : 35401915 : enum tree_code code, inverted_code;
1647 : 35401915 : edge e;
1648 : 35401915 : edge_iterator ei;
1649 : 35401915 : gimple *set_stmt;
1650 : 35401915 : tree param_type;
1651 : 35401915 : expr_eval_ops param_ops;
1652 : :
1653 : 70803830 : gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1654 : 11610494 : if (!last)
1655 : 35389743 : return;
1656 : 11610494 : if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1657 : : return;
1658 : 9187426 : op = gimple_cond_lhs (last);
1659 : :
1660 : 9187426 : if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1661 : : ¶m_ops))
1662 : : {
1663 : 1273947 : code = gimple_cond_code (last);
1664 : 1273947 : inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1665 : :
1666 : 3821841 : FOR_EACH_EDGE (e, ei, bb->succs)
1667 : : {
1668 : 1273947 : enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1669 : 2547894 : ? code : inverted_code);
1670 : : /* invert_tree_comparison will return ERROR_MARK on FP
1671 : : comparisons that are not EQ/NE instead of returning proper
1672 : : unordered one. Be sure it is not confused with NON_CONSTANT.
1673 : :
1674 : : And if the edge's target is the final block of diamond CFG graph
1675 : : of this conditional statement, we do not need to compute
1676 : : predicate for the edge because the final block's predicate must
1677 : : be at least as that of the first block of the statement. */
1678 : 2547894 : if (this_code != ERROR_MARK
1679 : 2547894 : && !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1680 : : {
1681 : 2141282 : ipa_predicate p
1682 : 2141282 : = add_condition (summary, params_summary, index,
1683 : : param_type, &aggpos,
1684 : : this_code, gimple_cond_rhs (last), param_ops);
1685 : 2141282 : e->aux = edge_predicate_pool.allocate ();
1686 : 2141282 : *(ipa_predicate *) e->aux = p;
1687 : : }
1688 : : }
1689 : 1273947 : vec_free (param_ops);
1690 : 1273947 : return;
1691 : : }
1692 : :
1693 : 7913479 : if (TREE_CODE (op) != SSA_NAME)
1694 : : return;
1695 : : /* Special case
1696 : : if (builtin_constant_p (op))
1697 : : constant_code
1698 : : else
1699 : : nonconstant_code.
1700 : : Here we can predicate nonconstant_code. We can't
1701 : : really handle constant_code since we have no predicate
1702 : : for this and also the constant code is not known to be
1703 : : optimized away when inliner doesn't see operand is constant.
1704 : : Other optimizers might think otherwise. */
1705 : 7912348 : if (gimple_cond_code (last) != NE_EXPR
1706 : 7912348 : || !integer_zerop (gimple_cond_rhs (last)))
1707 : 4489515 : return;
1708 : 3422833 : set_stmt = SSA_NAME_DEF_STMT (op);
1709 : 3422833 : if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1710 : 3422833 : || gimple_call_num_args (set_stmt) != 1)
1711 : : return;
1712 : 13330 : op2 = gimple_call_arg (set_stmt, 0);
1713 : 13330 : if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos))
1714 : : return;
1715 : 12172 : if (!aggpos.by_ref)
1716 : 12139 : add_builtin_constant_p_parm (summary, index);
1717 : 36516 : FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1718 : : {
1719 : 12172 : ipa_predicate p = add_condition (summary, params_summary, index,
1720 : : param_type, &aggpos,
1721 : : ipa_predicate::is_not_constant, NULL_TREE);
1722 : 12172 : e->aux = edge_predicate_pool.allocate ();
1723 : 12172 : *(ipa_predicate *) e->aux = p;
1724 : : }
1725 : : }
1726 : :
1727 : :
1728 : : /* If BB ends by a switch we can turn into predicates, attach corresponding
1729 : : predicates to the CFG edges. */
1730 : :
1731 : : static void
1732 : 35401915 : set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1733 : : class ipa_fn_summary *summary,
1734 : : class ipa_node_params *params_summary,
1735 : : basic_block bb)
1736 : : {
1737 : 35401915 : tree op;
1738 : 35401915 : int index;
1739 : 35401915 : struct agg_position_info aggpos;
1740 : 35401915 : edge e;
1741 : 35401915 : edge_iterator ei;
1742 : 35401915 : size_t n;
1743 : 35401915 : size_t case_idx;
1744 : 35401915 : tree param_type;
1745 : 35401915 : expr_eval_ops param_ops;
1746 : :
1747 : 70803830 : gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
1748 : 87535 : if (!last)
1749 : 35366981 : return;
1750 : 87535 : op = gimple_switch_index (last);
1751 : 87535 : if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1752 : : ¶m_ops))
1753 : : return;
1754 : :
1755 : 51135 : auto_vec<std::pair<tree, tree> > ranges;
1756 : 51135 : tree type = TREE_TYPE (op);
1757 : 51135 : int bound_limit = opt_for_fn (fbi->node->decl,
1758 : : param_ipa_max_switch_predicate_bounds);
1759 : 51135 : int bound_count = 0;
1760 : : // This can safely be an integer range, as switches can only hold
1761 : : // integers.
1762 : 51135 : int_range<2> vr;
1763 : :
1764 : 102270 : get_range_query (cfun)->range_of_expr (vr, op);
1765 : 51135 : if (vr.undefined_p ())
1766 : 0 : vr.set_varying (TREE_TYPE (op));
1767 : 51135 : tree vr_min, vr_max;
1768 : : // TODO: This entire function could use a rewrite to use the irange
1769 : : // API, instead of trying to recreate its intersection/union logic.
1770 : : // Any use of get_legacy_range() is a serious code smell.
1771 : 51135 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
1772 : 51135 : wide_int vr_wmin = wi::to_wide (vr_min);
1773 : 51135 : wide_int vr_wmax = wi::to_wide (vr_max);
1774 : :
1775 : 360743 : FOR_EACH_EDGE (e, ei, bb->succs)
1776 : : {
1777 : 309608 : e->aux = edge_predicate_pool.allocate ();
1778 : 309608 : *(ipa_predicate *) e->aux = false;
1779 : : }
1780 : :
1781 : 51135 : e = gimple_switch_edge (cfun, last, 0);
1782 : : /* Set BOUND_COUNT to maximum count to bypass computing predicate for
1783 : : default case if its target basic block is in convergence point of all
1784 : : switch cases, which can be determined by checking whether it
1785 : : post-dominates the switch statement. */
1786 : 51135 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1787 : 12779 : bound_count = INT_MAX;
1788 : :
1789 : 51135 : n = gimple_switch_num_labels (last);
1790 : 345316 : for (case_idx = 1; case_idx < n; ++case_idx)
1791 : : {
1792 : 294181 : tree cl = gimple_switch_label (last, case_idx);
1793 : 294181 : tree min = CASE_LOW (cl);
1794 : 294181 : tree max = CASE_HIGH (cl);
1795 : 294181 : ipa_predicate p;
1796 : :
1797 : 294181 : e = gimple_switch_edge (cfun, last, case_idx);
1798 : :
1799 : : /* The case value might not have same type as switch expression,
1800 : : extend the value based on the expression type. */
1801 : 294181 : if (TREE_TYPE (min) != type)
1802 : 57468 : min = wide_int_to_tree (type, wi::to_wide (min));
1803 : :
1804 : 294181 : if (!max)
1805 : : max = min;
1806 : 17656 : else if (TREE_TYPE (max) != type)
1807 : 7880 : max = wide_int_to_tree (type, wi::to_wide (max));
1808 : :
1809 : : /* The case's target basic block is in convergence point of all switch
1810 : : cases, its predicate should be at least as that of the switch
1811 : : statement. */
1812 : 294181 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1813 : 6159 : p = true;
1814 : 288022 : else if (min == max)
1815 : 272586 : p = add_condition (summary, params_summary, index, param_type,
1816 : : &aggpos, EQ_EXPR, min, param_ops);
1817 : : else
1818 : : {
1819 : 15436 : ipa_predicate p1, p2;
1820 : 15436 : p1 = add_condition (summary, params_summary, index, param_type,
1821 : : &aggpos, GE_EXPR, min, param_ops);
1822 : 15436 : p2 = add_condition (summary, params_summary,index, param_type,
1823 : : &aggpos, LE_EXPR, max, param_ops);
1824 : 15436 : p = p1 & p2;
1825 : : }
1826 : 294181 : *(ipa_predicate *) e->aux
1827 : 294181 : = p.or_with (summary->conds, *(ipa_predicate *) e->aux);
1828 : :
1829 : : /* If there are too many disjoint case ranges, predicate for default
1830 : : case might become too complicated. So add a limit here. */
1831 : 294181 : if (bound_count > bound_limit)
1832 : 97848 : continue;
1833 : :
1834 : 196333 : bool new_range = true;
1835 : :
1836 : 196333 : if (!ranges.is_empty ())
1837 : : {
1838 : 157977 : wide_int curr_wmin = wi::to_wide (min);
1839 : 157977 : wide_int last_wmax = wi::to_wide (ranges.last ().second);
1840 : :
1841 : : /* Merge case ranges if they are continuous. */
1842 : 157977 : if (curr_wmin == last_wmax + 1)
1843 : : new_range = false;
1844 : 52964 : else if (vr_type == VR_ANTI_RANGE)
1845 : : {
1846 : : /* If two disjoint case ranges can be connected by anti-range
1847 : : of switch index, combine them to one range. */
1848 : 79 : if (wi::lt_p (vr_wmax, curr_wmin - 1, TYPE_SIGN (type)))
1849 : : vr_type = VR_UNDEFINED;
1850 : 0 : else if (wi::le_p (vr_wmin, last_wmax + 1, TYPE_SIGN (type)))
1851 : 0 : new_range = false;
1852 : : }
1853 : 157977 : }
1854 : :
1855 : : /* Create/extend a case range. And we count endpoints of range set,
1856 : : this number nearly equals to number of conditions that we will create
1857 : : for predicate of default case. */
1858 : 157977 : if (new_range)
1859 : : {
1860 : 91320 : bound_count += (min == max) ? 1 : 2;
1861 : 91320 : ranges.safe_push (std::make_pair (min, max));
1862 : : }
1863 : : else
1864 : : {
1865 : 105013 : bound_count += (ranges.last ().first == ranges.last ().second);
1866 : 105013 : ranges.last ().second = max;
1867 : : }
1868 : : }
1869 : :
1870 : 51135 : e = gimple_switch_edge (cfun, last, 0);
1871 : 51135 : if (bound_count > bound_limit)
1872 : : {
1873 : 16201 : *(ipa_predicate *) e->aux = true;
1874 : 16201 : vec_free (param_ops);
1875 : 16201 : return;
1876 : : }
1877 : :
1878 : 34934 : ipa_predicate p_seg = true;
1879 : 34934 : ipa_predicate p_all = false;
1880 : :
1881 : 34934 : if (vr_type != VR_RANGE)
1882 : : {
1883 : 34403 : vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type));
1884 : 34403 : vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type));
1885 : : }
1886 : :
1887 : : /* Construct predicate to represent default range set that is negation of
1888 : : all case ranges. Case range is classified as containing single/non-single
1889 : : values. Suppose a piece of case ranges in the following.
1890 : :
1891 : : [D1...D2] [S1] ... [Sn] [D3...D4]
1892 : :
1893 : : To represent default case's range sets between two non-single value
1894 : : case ranges (From D2 to D3), we construct predicate as:
1895 : :
1896 : : D2 < x < D3 && x != S1 && ... && x != Sn
1897 : : */
1898 : 109344 : for (size_t i = 0; i < ranges.length (); i++)
1899 : : {
1900 : 74505 : tree min = ranges[i].first;
1901 : 74505 : tree max = ranges[i].second;
1902 : :
1903 : 74505 : if (min == max)
1904 : 92712 : p_seg &= add_condition (summary, params_summary, index,
1905 : : param_type, &aggpos, NE_EXPR,
1906 : 46356 : min, param_ops);
1907 : : else
1908 : : {
1909 : : /* Do not create sub-predicate for range that is beyond low bound
1910 : : of switch index. */
1911 : 28149 : if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type)))
1912 : : {
1913 : 19684 : p_seg &= add_condition (summary, params_summary, index,
1914 : : param_type, &aggpos,
1915 : 19684 : LT_EXPR, min, param_ops);
1916 : 19684 : p_all = p_all.or_with (summary->conds, p_seg);
1917 : : }
1918 : :
1919 : : /* Do not create sub-predicate for range that is beyond up bound
1920 : : of switch index. */
1921 : 28149 : if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type)))
1922 : : {
1923 : 95 : p_seg = false;
1924 : 95 : break;
1925 : : }
1926 : :
1927 : 28054 : p_seg = add_condition (summary, params_summary, index,
1928 : : param_type, &aggpos, GT_EXPR,
1929 : : max, param_ops);
1930 : : }
1931 : : }
1932 : :
1933 : 34934 : p_all = p_all.or_with (summary->conds, p_seg);
1934 : 34934 : *(ipa_predicate *) e->aux
1935 : 34934 : = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux);
1936 : :
1937 : 41615 : vec_free (param_ops);
1938 : 51135 : }
1939 : :
1940 : :
1941 : : /* For each BB in NODE attach to its AUX pointer predicate under
1942 : : which it is executable. */
1943 : :
1944 : : static void
1945 : 6408693 : compute_bb_predicates (struct ipa_func_body_info *fbi,
1946 : : struct cgraph_node *node,
1947 : : class ipa_fn_summary *summary,
1948 : : class ipa_node_params *params_summary)
1949 : : {
1950 : 6408693 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1951 : 6408693 : bool done = false;
1952 : 6408693 : basic_block bb;
1953 : :
1954 : 41810608 : FOR_EACH_BB_FN (bb, my_function)
1955 : : {
1956 : 35401915 : set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb);
1957 : 35401915 : set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb);
1958 : : }
1959 : :
1960 : : /* Entry block is always executable. */
1961 : 6408693 : ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1962 : 6408693 : = edge_predicate_pool.allocate ();
1963 : 6408693 : *(ipa_predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux = true;
1964 : :
1965 : : /* A simple dataflow propagation of predicates forward in the CFG.
1966 : : TODO: work in reverse postorder. */
1967 : 19350085 : while (!done)
1968 : : {
1969 : 12941392 : done = true;
1970 : 89009163 : FOR_EACH_BB_FN (bb, my_function)
1971 : : {
1972 : 76067771 : ipa_predicate p = false;
1973 : 76067771 : edge e;
1974 : 76067771 : edge_iterator ei;
1975 : 94293322 : FOR_EACH_EDGE (e, ei, bb->preds)
1976 : : {
1977 : 80921421 : if (e->src->aux)
1978 : : {
1979 : 79024041 : ipa_predicate this_bb_predicate
1980 : : = *(ipa_predicate *) e->src->aux;
1981 : 79024041 : if (e->aux)
1982 : 5138686 : this_bb_predicate &= (*(ipa_predicate *) e->aux);
1983 : 79024041 : p = p.or_with (summary->conds, this_bb_predicate);
1984 : 79024041 : if (p == true)
1985 : : break;
1986 : : }
1987 : : }
1988 : 76067771 : if (p != false)
1989 : : {
1990 : 74832007 : basic_block pdom_bb;
1991 : :
1992 : 74832007 : if (!bb->aux)
1993 : : {
1994 : 27770649 : done = false;
1995 : 27770649 : bb->aux = edge_predicate_pool.allocate ();
1996 : 27770649 : *((ipa_predicate *) bb->aux) = p;
1997 : : }
1998 : 47061358 : else if (p != *(ipa_predicate *) bb->aux)
1999 : : {
2000 : : /* This OR operation is needed to ensure monotonous data flow
2001 : : in the case we hit the limit on number of clauses and the
2002 : : and/or operations above give approximate answers. */
2003 : 134329 : p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux);
2004 : 134329 : if (p != *(ipa_predicate *)bb->aux)
2005 : : {
2006 : 109639 : done = false;
2007 : 109639 : *((ipa_predicate *)bb->aux) = p;
2008 : : }
2009 : : }
2010 : :
2011 : : /* For switch/if statement, we can OR-combine predicates of all
2012 : : its cases/branches to get predicate for basic block in their
2013 : : convergence point, but sometimes this will generate very
2014 : : complicated predicate. Actually, we can get simplified
2015 : : predicate in another way by using the fact that predicate
2016 : : for a basic block must also hold true for its post dominators.
2017 : : To be specific, basic block in convergence point of
2018 : : conditional statement should include predicate of the
2019 : : statement. */
2020 : 74832007 : pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
2021 : 74832007 : if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb)
2022 : : ;
2023 : 38266656 : else if (!pdom_bb->aux)
2024 : : {
2025 : 7631252 : done = false;
2026 : 7631252 : pdom_bb->aux = edge_predicate_pool.allocate ();
2027 : 7631252 : *((ipa_predicate *)pdom_bb->aux) = p;
2028 : : }
2029 : 30635404 : else if (p != *(ipa_predicate *)pdom_bb->aux)
2030 : : {
2031 : 3459623 : p = p.or_with (summary->conds,
2032 : : *(ipa_predicate *)pdom_bb->aux);
2033 : 3459623 : if (p != *(ipa_predicate *)pdom_bb->aux)
2034 : : {
2035 : 169751 : done = false;
2036 : 169751 : *((ipa_predicate *)pdom_bb->aux) = p;
2037 : : }
2038 : : }
2039 : : }
2040 : : }
2041 : : }
2042 : 6408693 : }
2043 : :
2044 : :
2045 : : /* Return predicate specifying when the STMT might have result that is not
2046 : : a compile time constant. */
2047 : :
2048 : : static ipa_predicate
2049 : 4204686 : will_be_nonconstant_expr_predicate (ipa_func_body_info *fbi,
2050 : : class ipa_fn_summary *summary,
2051 : : class ipa_node_params *params_summary,
2052 : : tree expr,
2053 : : vec<ipa_predicate> nonconstant_names)
2054 : : {
2055 : 4204686 : tree parm;
2056 : 4204686 : int index;
2057 : :
2058 : 4452343 : while (UNARY_CLASS_P (expr))
2059 : 247657 : expr = TREE_OPERAND (expr, 0);
2060 : :
2061 : 4204686 : parm = unmodified_parm (fbi, NULL, expr, NULL);
2062 : 4204686 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2063 : 220831 : return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL,
2064 : 220831 : ipa_predicate::changed, NULL_TREE);
2065 : 3983855 : if (is_gimple_min_invariant (expr))
2066 : 62131 : return false;
2067 : 3921724 : if (TREE_CODE (expr) == SSA_NAME)
2068 : 3741229 : return nonconstant_names[SSA_NAME_VERSION (expr)];
2069 : 180495 : if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2070 : : {
2071 : 179915 : ipa_predicate p1
2072 : 179915 : = will_be_nonconstant_expr_predicate (fbi, summary,
2073 : : params_summary,
2074 : 179915 : TREE_OPERAND (expr, 0),
2075 : : nonconstant_names);
2076 : 179915 : if (p1 == true)
2077 : 104475 : return p1;
2078 : :
2079 : 75440 : ipa_predicate p2
2080 : 75440 : = will_be_nonconstant_expr_predicate (fbi, summary,
2081 : : params_summary,
2082 : 75440 : TREE_OPERAND (expr, 1),
2083 : : nonconstant_names);
2084 : 75440 : return p1.or_with (summary->conds, p2);
2085 : : }
2086 : 580 : else if (TREE_CODE (expr) == COND_EXPR)
2087 : : {
2088 : 168 : ipa_predicate p1
2089 : 168 : = will_be_nonconstant_expr_predicate (fbi, summary,
2090 : : params_summary,
2091 : 168 : TREE_OPERAND (expr, 0),
2092 : : nonconstant_names);
2093 : 168 : if (p1 == true)
2094 : 33 : return p1;
2095 : :
2096 : 135 : ipa_predicate p2
2097 : 135 : = will_be_nonconstant_expr_predicate (fbi, summary,
2098 : : params_summary,
2099 : 135 : TREE_OPERAND (expr, 1),
2100 : : nonconstant_names);
2101 : 135 : if (p2 == true)
2102 : 135 : return p2;
2103 : 0 : p1 = p1.or_with (summary->conds, p2);
2104 : 0 : p2 = will_be_nonconstant_expr_predicate (fbi, summary,
2105 : : params_summary,
2106 : 0 : TREE_OPERAND (expr, 2),
2107 : : nonconstant_names);
2108 : 0 : return p2.or_with (summary->conds, p1);
2109 : : }
2110 : 412 : else if (TREE_CODE (expr) == CALL_EXPR)
2111 : 412 : return true;
2112 : : else
2113 : : {
2114 : 0 : debug_tree (expr);
2115 : 0 : gcc_unreachable ();
2116 : : }
2117 : : }
2118 : :
2119 : :
2120 : : /* Return predicate specifying when the STMT might have result that is not
2121 : : a compile time constant. */
2122 : :
2123 : : static ipa_predicate
2124 : 114206253 : will_be_nonconstant_predicate (struct ipa_func_body_info *fbi,
2125 : : class ipa_fn_summary *summary,
2126 : : class ipa_node_params *params_summary,
2127 : : gimple *stmt,
2128 : : vec<ipa_predicate> nonconstant_names)
2129 : : {
2130 : 114206253 : ipa_predicate p = true;
2131 : 114206253 : ssa_op_iter iter;
2132 : 114206253 : tree use;
2133 : 114206253 : tree param_type = NULL_TREE;
2134 : 114206253 : ipa_predicate op_non_const;
2135 : 114206253 : bool is_load;
2136 : 114206253 : int base_index;
2137 : 114206253 : struct agg_position_info aggpos;
2138 : :
2139 : : /* What statements might be optimized away
2140 : : when their arguments are constant. */
2141 : 114206253 : if (gimple_code (stmt) != GIMPLE_ASSIGN
2142 : 39304505 : && gimple_code (stmt) != GIMPLE_COND
2143 : 27923842 : && gimple_code (stmt) != GIMPLE_SWITCH
2144 : 142042560 : && (gimple_code (stmt) != GIMPLE_CALL
2145 : 20836265 : || !(gimple_call_flags (stmt) & ECF_CONST)))
2146 : 26180611 : return p;
2147 : :
2148 : : /* Stores will stay anyway. */
2149 : 88025642 : if (gimple_store_p (stmt))
2150 : 25944202 : return p;
2151 : :
2152 : 62081440 : is_load = gimple_assign_load_p (stmt);
2153 : :
2154 : : /* Loads can be optimized when the value is known. */
2155 : 62081440 : if (is_load)
2156 : : {
2157 : 17951824 : tree op = gimple_assign_rhs1 (stmt);
2158 : 17951824 : if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type,
2159 : : &aggpos))
2160 : 14666683 : return p;
2161 : : }
2162 : : else
2163 : 44129616 : base_index = -1;
2164 : :
2165 : : /* See if we understand all operands before we start
2166 : : adding conditionals. */
2167 : 64021013 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2168 : : {
2169 : 47024498 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2170 : : /* For arguments we can build a condition. */
2171 : 47024498 : if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0)
2172 : 8428430 : continue;
2173 : 38596068 : if (TREE_CODE (use) != SSA_NAME)
2174 : 0 : return p;
2175 : : /* If we know when operand is constant,
2176 : : we still can say something useful. */
2177 : 38596068 : if (nonconstant_names[SSA_NAME_VERSION (use)] != true)
2178 : 8177826 : continue;
2179 : 30418242 : return p;
2180 : : }
2181 : :
2182 : 16996515 : if (is_load)
2183 : 3285060 : op_non_const =
2184 : 3285060 : add_condition (summary, params_summary,
2185 : : base_index, param_type, &aggpos,
2186 : : ipa_predicate::changed, NULL_TREE);
2187 : : else
2188 : 13711455 : op_non_const = false;
2189 : 32564539 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2190 : : {
2191 : 15568024 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2192 : 15568024 : int index;
2193 : :
2194 : 15568024 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2195 : : {
2196 : 7936436 : if (index != base_index)
2197 : 5361389 : p = add_condition (summary, params_summary, index,
2198 : 5361389 : TREE_TYPE (parm), NULL,
2199 : : ipa_predicate::changed, NULL_TREE);
2200 : : else
2201 : 2575047 : continue;
2202 : : }
2203 : : else
2204 : 7631588 : p = nonconstant_names[SSA_NAME_VERSION (use)];
2205 : 12992977 : op_non_const = p.or_with (summary->conds, op_non_const);
2206 : : }
2207 : 16996515 : if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2208 : 14883817 : && gimple_op (stmt, 0)
2209 : 31604432 : && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2210 : 14607917 : nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2211 : 14607917 : = op_non_const;
2212 : 16996515 : return op_non_const;
2213 : : }
2214 : :
2215 : : struct record_modified_bb_info
2216 : : {
2217 : : tree op;
2218 : : bitmap bb_set;
2219 : : gimple *stmt;
2220 : : };
2221 : :
2222 : : /* Value is initialized in INIT_BB and used in USE_BB. We want to compute
2223 : : probability how often it changes between USE_BB.
2224 : : INIT_BB->count/USE_BB->count is an estimate, but if INIT_BB
2225 : : is in different loop nest, we can do better.
2226 : : This is all just estimate. In theory we look for minimal cut separating
2227 : : INIT_BB and USE_BB, but we only want to anticipate loop invariant motion
2228 : : anyway. */
2229 : :
2230 : : static basic_block
2231 : 10586374 : get_minimal_bb (basic_block init_bb, basic_block use_bb)
2232 : : {
2233 : 10586374 : class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father);
2234 : 10586374 : if (l && l->header->count < init_bb->count)
2235 : 362909 : return l->header;
2236 : : return init_bb;
2237 : : }
2238 : :
2239 : : /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2240 : : set except for info->stmt. */
2241 : :
2242 : : static bool
2243 : 5024986 : record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2244 : : {
2245 : 5024986 : struct record_modified_bb_info *info =
2246 : : (struct record_modified_bb_info *) data;
2247 : 5024986 : if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2248 : : return false;
2249 : 4992718 : if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef)))
2250 : : return false;
2251 : 4944423 : bitmap_set_bit (info->bb_set,
2252 : 4944423 : SSA_NAME_IS_DEFAULT_DEF (vdef)
2253 : 0 : ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2254 : : : get_minimal_bb
2255 : 4944423 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2256 : 4944423 : gimple_bb (info->stmt))->index);
2257 : 4944423 : if (dump_file)
2258 : : {
2259 : 0 : fprintf (dump_file, " Param ");
2260 : 0 : print_generic_expr (dump_file, info->op, TDF_SLIM);
2261 : 0 : fprintf (dump_file, " changed at bb %i, minimal: %i stmt: ",
2262 : 0 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index,
2263 : : get_minimal_bb
2264 : 0 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2265 : 0 : gimple_bb (info->stmt))->index);
2266 : 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (vdef), 0);
2267 : : }
2268 : : return false;
2269 : : }
2270 : :
2271 : : /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2272 : : will change since last invocation of STMT.
2273 : :
2274 : : Value 0 is reserved for compile time invariants.
2275 : : For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2276 : : ought to be REG_BR_PROB_BASE / estimated_iters. */
2277 : :
2278 : : static int
2279 : 38514623 : param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i)
2280 : : {
2281 : 38514623 : tree op = gimple_call_arg (stmt, i);
2282 : 38514623 : basic_block bb = gimple_bb (stmt);
2283 : :
2284 : 38514623 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
2285 : 299 : op = TREE_OPERAND (op, 0);
2286 : :
2287 : 38514623 : tree base = get_base_address (op);
2288 : :
2289 : : /* Global invariants never change. */
2290 : 38514623 : if (is_gimple_min_invariant (base))
2291 : : return 0;
2292 : :
2293 : : /* We would have to do non-trivial analysis to really work out what
2294 : : is the probability of value to change (i.e. when init statement
2295 : : is in a sibling loop of the call).
2296 : :
2297 : : We do an conservative estimate: when call is executed N times more often
2298 : : than the statement defining value, we take the frequency 1/N. */
2299 : 19336992 : if (TREE_CODE (base) == SSA_NAME)
2300 : : {
2301 : 16960912 : profile_count init_count;
2302 : :
2303 : 25776693 : if (!bb->count.nonzero_p ())
2304 : : return REG_BR_PROB_BASE;
2305 : :
2306 : 8670518 : if (SSA_NAME_IS_DEFAULT_DEF (base))
2307 : 3028567 : init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2308 : : else
2309 : 5641951 : init_count = get_minimal_bb
2310 : 5641951 : (gimple_bb (SSA_NAME_DEF_STMT (base)),
2311 : : gimple_bb (stmt))->count;
2312 : :
2313 : 8670518 : if (init_count < bb->count)
2314 : 434203 : return MAX ((init_count.to_sreal_scale (bb->count)
2315 : : * REG_BR_PROB_BASE).to_int (), 1);
2316 : : return REG_BR_PROB_BASE;
2317 : : }
2318 : : else
2319 : : {
2320 : 2376080 : ao_ref refd;
2321 : 2376080 : profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2322 : 2376080 : struct record_modified_bb_info info;
2323 : 2376080 : tree init = ctor_for_folding (base);
2324 : :
2325 : 2376080 : if (init != error_mark_node)
2326 : : return 0;
2327 : 5249779 : if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0)
2328 : : return REG_BR_PROB_BASE;
2329 : 1449861 : if (dump_file)
2330 : : {
2331 : 0 : fprintf (dump_file, " Analyzing param change probability of ");
2332 : 0 : print_generic_expr (dump_file, op, TDF_SLIM);
2333 : 0 : fprintf (dump_file, "\n");
2334 : : }
2335 : 1449861 : ao_ref_init (&refd, op);
2336 : 1449861 : info.op = op;
2337 : 1449861 : info.stmt = stmt;
2338 : 1449861 : info.bb_set = BITMAP_ALLOC (NULL);
2339 : 1449861 : int walked
2340 : 2899722 : = walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2341 : : NULL, NULL, fbi->aa_walk_budget);
2342 : 1449861 : if (walked > 0)
2343 : 1319459 : fbi->aa_walk_budget -= walked;
2344 : 1449861 : if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index))
2345 : : {
2346 : 886585 : if (walked < 0)
2347 : 206 : fbi->aa_walk_budget = 0;
2348 : 886585 : if (dump_file)
2349 : : {
2350 : 0 : if (walked < 0)
2351 : 0 : fprintf (dump_file, " Ran out of AA walking budget.\n");
2352 : : else
2353 : 0 : fprintf (dump_file, " Set in same BB as used.\n");
2354 : : }
2355 : 886585 : BITMAP_FREE (info.bb_set);
2356 : 886585 : return REG_BR_PROB_BASE;
2357 : : }
2358 : :
2359 : 563276 : bitmap_iterator bi;
2360 : 563276 : unsigned index;
2361 : : /* Lookup the most frequent update of the value and believe that
2362 : : it dominates all the other; precise analysis here is difficult. */
2363 : 1682153 : EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2364 : 1118877 : max = profile_count::max_prefer_initialized
2365 : 1118877 : (max, BASIC_BLOCK_FOR_FN (cfun, index)->count);
2366 : 563276 : if (dump_file)
2367 : : {
2368 : 0 : fprintf (dump_file, " Set with count ");
2369 : 0 : max.dump (dump_file);
2370 : 0 : fprintf (dump_file, " and used with count ");
2371 : 0 : bb->count.dump (dump_file);
2372 : 0 : fprintf (dump_file, " freq %f\n",
2373 : 0 : max.to_sreal_scale (bb->count).to_double ());
2374 : : }
2375 : :
2376 : 563276 : BITMAP_FREE (info.bb_set);
2377 : 563276 : if (max < bb->count)
2378 : 91412 : return MAX ((max.to_sreal_scale (bb->count)
2379 : : * REG_BR_PROB_BASE).to_int (), 1);
2380 : : return REG_BR_PROB_BASE;
2381 : : }
2382 : : }
2383 : :
2384 : : /* Find whether a basic block BB is the final block of a (half) diamond CFG
2385 : : sub-graph and if the predicate the condition depends on is known. If so,
2386 : : return true and store the pointer the predicate in *P. */
2387 : :
2388 : : static bool
2389 : 6410114 : phi_result_unknown_predicate (ipa_func_body_info *fbi,
2390 : : ipa_fn_summary *summary,
2391 : : class ipa_node_params *params_summary,
2392 : : basic_block bb,
2393 : : ipa_predicate *p,
2394 : : vec<ipa_predicate> nonconstant_names)
2395 : : {
2396 : 6410114 : edge e;
2397 : 6410114 : edge_iterator ei;
2398 : 6410114 : basic_block first_bb = NULL;
2399 : :
2400 : 6410114 : if (single_pred_p (bb))
2401 : : {
2402 : 21084 : *p = false;
2403 : 21084 : return true;
2404 : : }
2405 : :
2406 : 14713913 : FOR_EACH_EDGE (e, ei, bb->preds)
2407 : : {
2408 : 12585680 : if (single_succ_p (e->src))
2409 : : {
2410 : 13162512 : if (!single_pred_p (e->src))
2411 : : return false;
2412 : 7362963 : if (!first_bb)
2413 : 3189111 : first_bb = single_pred (e->src);
2414 : 4173852 : else if (single_pred (e->src) != first_bb)
2415 : : return false;
2416 : : }
2417 : : else
2418 : : {
2419 : 4226319 : if (!first_bb)
2420 : : first_bb = e->src;
2421 : 1456541 : else if (e->src != first_bb)
2422 : : return false;
2423 : : }
2424 : : }
2425 : :
2426 : 2128233 : if (!first_bb)
2427 : : return false;
2428 : :
2429 : 4256466 : gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb));
2430 : 2075844 : if (!stmt
2431 : 2075844 : || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2432 : 317173 : return false;
2433 : :
2434 : 1811060 : *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary,
2435 : : gimple_cond_lhs (stmt),
2436 : : nonconstant_names);
2437 : 1811060 : if (*p == true)
2438 : : return false;
2439 : : else
2440 : : return true;
2441 : : }
2442 : :
2443 : : /* Given a PHI statement in a function described by inline properties SUMMARY
2444 : : and *P being the predicate describing whether the selected PHI argument is
2445 : : known, store a predicate for the result of the PHI statement into
2446 : : NONCONSTANT_NAMES, if possible. */
2447 : :
2448 : : static void
2449 : 693470 : predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi,
2450 : : ipa_predicate *p,
2451 : : vec<ipa_predicate> nonconstant_names)
2452 : : {
2453 : 693470 : unsigned i;
2454 : :
2455 : 996724 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2456 : : {
2457 : 869922 : tree arg = gimple_phi_arg (phi, i)->def;
2458 : 869922 : if (!is_gimple_min_invariant (arg))
2459 : : {
2460 : 740477 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
2461 : 1480954 : *p = p->or_with (summary->conds,
2462 : 740477 : nonconstant_names[SSA_NAME_VERSION (arg)]);
2463 : 740477 : if (*p == true)
2464 : : return;
2465 : : }
2466 : : }
2467 : :
2468 : 126802 : if (dump_file && (dump_flags & TDF_DETAILS))
2469 : : {
2470 : 3 : fprintf (dump_file, "\t\tphi predicate: ");
2471 : 3 : p->dump (dump_file, summary->conds);
2472 : : }
2473 : 126802 : nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2474 : : }
2475 : :
2476 : : /* For a typical usage of __builtin_expect (a<b, 1), we
2477 : : may introduce an extra relation stmt:
2478 : : With the builtin, we have
2479 : : t1 = a <= b;
2480 : : t2 = (long int) t1;
2481 : : t3 = __builtin_expect (t2, 1);
2482 : : if (t3 != 0)
2483 : : goto ...
2484 : : Without the builtin, we have
2485 : : if (a<=b)
2486 : : goto...
2487 : : This affects the size/time estimation and may have
2488 : : an impact on the earlier inlining.
2489 : : Here find this pattern and fix it up later. */
2490 : :
2491 : : static gimple *
2492 : 41241988 : find_foldable_builtin_expect (basic_block bb)
2493 : : {
2494 : 41241988 : gimple_stmt_iterator bsi;
2495 : :
2496 : 284613178 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2497 : : {
2498 : 202280489 : gimple *stmt = gsi_stmt (bsi);
2499 : 202280489 : if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2500 : 202082900 : || gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY)
2501 : 404363324 : || gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT))
2502 : : {
2503 : 325960 : tree var = gimple_call_lhs (stmt);
2504 : 325960 : tree arg = gimple_call_arg (stmt, 0);
2505 : 325960 : use_operand_p use_p;
2506 : 325960 : gimple *use_stmt;
2507 : 325960 : bool match = false;
2508 : 325960 : bool done = false;
2509 : :
2510 : 325960 : if (!var || !arg)
2511 : 3 : continue;
2512 : 325957 : gcc_assert (TREE_CODE (var) == SSA_NAME);
2513 : :
2514 : 650615 : while (TREE_CODE (arg) == SSA_NAME)
2515 : : {
2516 : 650615 : gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg);
2517 : 650615 : if (!is_gimple_assign (stmt_tmp))
2518 : : break;
2519 : 643913 : switch (gimple_assign_rhs_code (stmt_tmp))
2520 : : {
2521 : : case LT_EXPR:
2522 : : case LE_EXPR:
2523 : : case GT_EXPR:
2524 : : case GE_EXPR:
2525 : : case EQ_EXPR:
2526 : : case NE_EXPR:
2527 : : match = true;
2528 : : done = true;
2529 : : break;
2530 : : CASE_CONVERT:
2531 : : break;
2532 : : default:
2533 : : done = true;
2534 : : break;
2535 : : }
2536 : 324658 : if (done)
2537 : : break;
2538 : 324658 : arg = gimple_assign_rhs1 (stmt_tmp);
2539 : : }
2540 : :
2541 : 292043 : if (match && single_imm_use (var, &use_p, &use_stmt)
2542 : 617561 : && gimple_code (use_stmt) == GIMPLE_COND)
2543 : 151287 : return use_stmt;
2544 : : }
2545 : : }
2546 : : return NULL;
2547 : : }
2548 : :
2549 : : /* Return true when the basic blocks contains only clobbers followed by RESX.
2550 : : Such BBs are kept around to make removal of dead stores possible with
2551 : : presence of EH and will be optimized out by optimize_clobbers later in the
2552 : : game.
2553 : :
2554 : : NEED_EH is used to recurse in case the clobber has non-EH predecessors
2555 : : that can be clobber only, too.. When it is false, the RESX is not necessary
2556 : : on the end of basic block. */
2557 : :
2558 : : static bool
2559 : 41872843 : clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2560 : : {
2561 : 41872843 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2562 : 41872843 : edge_iterator ei;
2563 : 41872843 : edge e;
2564 : :
2565 : 41872843 : if (need_eh)
2566 : : {
2567 : 41790490 : if (gsi_end_p (gsi))
2568 : : return false;
2569 : 40653332 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2570 : : return false;
2571 : 1044181 : gsi_prev (&gsi);
2572 : : }
2573 : 41325329 : else if (!single_succ_p (bb))
2574 : : return false;
2575 : :
2576 : 5186155 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2577 : : {
2578 : 3061517 : gimple *stmt = gsi_stmt (gsi);
2579 : 3061517 : if (is_gimple_debug (stmt))
2580 : 1172369 : continue;
2581 : 1889148 : if (gimple_clobber_p (stmt))
2582 : 890624 : continue;
2583 : 998524 : if (gimple_code (stmt) == GIMPLE_LABEL)
2584 : : break;
2585 : : return false;
2586 : : }
2587 : :
2588 : : /* See if all predecessors are either throws or clobber only BBs. */
2589 : 3221250 : FOR_EACH_EDGE (e, ei, bb->preds)
2590 : 2657748 : if (!(e->flags & EDGE_EH)
2591 : 2657748 : && !clobber_only_eh_bb_p (e->src, false))
2592 : : return false;
2593 : :
2594 : : return true;
2595 : : }
2596 : :
2597 : : /* Return true if STMT compute a floating point expression that may be affected
2598 : : by -ffast-math and similar flags. */
2599 : :
2600 : : static bool
2601 : 94111710 : fp_expression_p (gimple *stmt)
2602 : : {
2603 : 94111710 : ssa_op_iter i;
2604 : 94111710 : tree op;
2605 : :
2606 : 210826483 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE)
2607 : 117317786 : if (FLOAT_TYPE_P (TREE_TYPE (op)))
2608 : : return true;
2609 : : return false;
2610 : : }
2611 : :
2612 : : /* Return true if T references memory location that is local
2613 : : for the function (that means, dead after return) or read-only. */
2614 : :
2615 : : bool
2616 : 59754658 : refs_local_or_readonly_memory_p (tree t)
2617 : : {
2618 : : /* Non-escaping memory is fine. */
2619 : 59754658 : t = get_base_address (t);
2620 : 59754658 : if ((TREE_CODE (t) == MEM_REF
2621 : 59754658 : || TREE_CODE (t) == TARGET_MEM_REF))
2622 : 25083077 : return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2623 : :
2624 : : /* Automatic variables are fine. */
2625 : 34671581 : if (DECL_P (t)
2626 : 34671581 : && auto_var_in_fn_p (t, current_function_decl))
2627 : : return true;
2628 : :
2629 : : /* Read-only variables are fine. */
2630 : 10963929 : if (DECL_P (t) && TREE_READONLY (t))
2631 : : return true;
2632 : :
2633 : : return false;
2634 : : }
2635 : :
2636 : : /* Return true if T is a pointer pointing to memory location that is local
2637 : : for the function (that means, dead after return) or read-only. */
2638 : :
2639 : : bool
2640 : 72246555 : points_to_local_or_readonly_memory_p (tree t)
2641 : : {
2642 : : /* See if memory location is clearly invalid. */
2643 : 72246555 : if (integer_zerop (t))
2644 : 2369363 : return flag_delete_null_pointer_checks;
2645 : 69877192 : if (TREE_CODE (t) == SSA_NAME)
2646 : : {
2647 : : /* For IPA passes we can consinder accesses to return slot local
2648 : : even if it is not local in the sense that memory is dead by
2649 : : the end of founction.
2650 : : The outer function will see a store in the call assignment
2651 : : and thus this will do right thing for all uses of this
2652 : : function in the current IPA passes (modref, pure/const discovery
2653 : : and inlining heuristics). */
2654 : 44181416 : if (DECL_RESULT (current_function_decl)
2655 : 44181416 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
2656 : 45238117 : && t == ssa_default_def (cfun, DECL_RESULT (current_function_decl)))
2657 : : return true;
2658 : 43868318 : return !ptr_deref_may_alias_global_p (t, false);
2659 : : }
2660 : 25695776 : if (TREE_CODE (t) == ADDR_EXPR
2661 : 25695776 : && (TREE_CODE (TREE_OPERAND (t, 0)) != TARGET_MEM_REF
2662 : 4513 : || TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) != INTEGER_CST))
2663 : 13204364 : return refs_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2664 : : return false;
2665 : : }
2666 : :
2667 : : /* Return true if T is a pointer pointing to memory location that is possible
2668 : : sra candidate if all functions it is passed to are inlined. */
2669 : :
2670 : : static bool
2671 : 38514623 : points_to_possible_sra_candidate_p (tree t)
2672 : : {
2673 : 38514623 : if (TREE_CODE (t) != ADDR_EXPR)
2674 : : return false;
2675 : :
2676 : 11516620 : t = get_base_address (TREE_OPERAND (t, 0));
2677 : :
2678 : : /* Automatic variables are fine. */
2679 : 11516620 : if (DECL_P (t)
2680 : 11516620 : && auto_var_in_fn_p (t, current_function_decl))
2681 : : return true;
2682 : : return false;
2683 : : }
2684 : :
2685 : : /* Return true if BB only calls builtin_unreachable.
2686 : : We skip empty basic blocks, debug statements, clobbers and predicts.
2687 : : CACHE is used to memoize already analyzed blocks. */
2688 : :
2689 : : static bool
2690 : 27202517 : builtin_unreachable_bb_p (basic_block bb, vec<unsigned char> &cache)
2691 : : {
2692 : 27202517 : if (cache[bb->index])
2693 : 2555272 : return cache[bb->index] - 1;
2694 : 24647245 : gimple_stmt_iterator si;
2695 : 24647245 : auto_vec <basic_block, 4> visited_bbs;
2696 : 24647245 : bool ret = false;
2697 : 25456086 : while (true)
2698 : : {
2699 : 25456086 : bool empty_bb = true;
2700 : 25456086 : visited_bbs.safe_push (bb);
2701 : 25456086 : cache[bb->index] = 3;
2702 : 25456086 : for (si = gsi_start_nondebug_bb (bb);
2703 : 26950392 : !gsi_end_p (si) && empty_bb;
2704 : 1494306 : gsi_next_nondebug (&si))
2705 : : {
2706 : 25576543 : if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT
2707 : 25196311 : && !gimple_clobber_p (gsi_stmt (si))
2708 : 49691815 : && !gimple_nop_p (gsi_stmt (si)))
2709 : : {
2710 : : empty_bb = false;
2711 : : break;
2712 : : }
2713 : : }
2714 : 25456086 : if (!empty_bb)
2715 : : break;
2716 : : else
2717 : 1373849 : bb = single_succ_edge (bb)->dest;
2718 : 1373849 : if (cache[bb->index])
2719 : : {
2720 : 565008 : ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1;
2721 : 565008 : goto done;
2722 : : }
2723 : : }
2724 : 24082237 : if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE)
2725 : 24082237 : || gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP))
2726 : : ret = true;
2727 : 24647245 : done:
2728 : 99397821 : for (basic_block vbb:visited_bbs)
2729 : 25456086 : cache[vbb->index] = (unsigned char)ret + 1;
2730 : 24647245 : return ret;
2731 : 24647245 : }
2732 : :
2733 : : static bool
2734 : 13707352 : guards_builtin_unreachable (basic_block bb, vec<unsigned char> &cache)
2735 : : {
2736 : 13707352 : edge_iterator ei;
2737 : 13707352 : edge e;
2738 : 40678278 : FOR_EACH_EDGE (e, ei, bb->succs)
2739 : 27202517 : if (builtin_unreachable_bb_p (e->dest, cache))
2740 : : {
2741 : 231591 : if (dump_file && (dump_flags & TDF_DETAILS))
2742 : 1 : fprintf (dump_file,
2743 : : "BB %i ends with conditional guarding __builtin_unreachable;"
2744 : : " conditinal is unnecesary\n", bb->index);
2745 : 231591 : return true;
2746 : : }
2747 : : return false;
2748 : : }
2749 : :
2750 : : #define STMT_NECESSARY GF_PLF_1
2751 : :
2752 : : /* If STMT is not already marked necessary, mark it, and add it to the
2753 : : worklist if ADD_TO_WORKLIST is true. */
2754 : :
2755 : : static inline void
2756 : 171787343 : mark_stmt_necessary (gimple *stmt, auto_vec<gimple *> &worklist)
2757 : : {
2758 : 171787343 : gcc_assert (stmt);
2759 : :
2760 : 171787343 : if (gimple_plf (stmt, STMT_NECESSARY))
2761 : : return;
2762 : :
2763 : 142541227 : if (dump_file && (dump_flags & TDF_DETAILS))
2764 : : {
2765 : 207 : fprintf (dump_file, "Marking useful stmt: ");
2766 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2767 : 207 : fprintf (dump_file, "\n");
2768 : : }
2769 : :
2770 : 142541227 : gimple_set_plf (stmt, STMT_NECESSARY, true);
2771 : 142541227 : worklist.safe_push (stmt);
2772 : : }
2773 : :
2774 : : /* Mark the statement defining operand OP as necessary. */
2775 : :
2776 : : static inline void
2777 : 120854898 : mark_operand_necessary (tree op, auto_vec<gimple *> &worklist)
2778 : : {
2779 : 120854898 : gimple *stmt = SSA_NAME_DEF_STMT (op);
2780 : 120854898 : if (gimple_nop_p (stmt))
2781 : : return;
2782 : 98022595 : mark_stmt_necessary (stmt, worklist);
2783 : : }
2784 : :
2785 : : /* Mark all statements that will remain in the body after optimizing out
2786 : : conditionals guarding __builtin_unreachable which we keep to preserve
2787 : : value ranges. */
2788 : :
2789 : : static void
2790 : 7293787 : find_necessary_statements (struct cgraph_node *node)
2791 : : {
2792 : 7293787 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2793 : 7293787 : auto_vec<unsigned char, 10> cache;
2794 : 7293787 : basic_block bb;
2795 : 7293787 : auto_vec<gimple *> worklist;
2796 : :
2797 : 7293787 : cache.safe_grow_cleared (last_basic_block_for_fn (cfun));
2798 : : /* Mark all obviously necessary statements. */
2799 : 49084277 : FOR_EACH_BB_FN (bb, my_function)
2800 : : {
2801 : 41790490 : for (gimple_stmt_iterator gsi = gsi_start_phis (bb);
2802 : 53508778 : !gsi_end_p (gsi); gsi_next (&gsi))
2803 : 11718288 : gimple_set_plf (gsi_stmt (gsi), STMT_NECESSARY, false);
2804 : :
2805 : 233019815 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2806 : 149438835 : gsi_next_nondebug (&bsi))
2807 : : {
2808 : 149438835 : gimple *stmt = gsi_stmt (bsi);
2809 : :
2810 : 149438835 : gimple_set_plf (stmt, STMT_NECESSARY, false);
2811 : 149438835 : if (gimple_has_side_effects (stmt)
2812 : 122497053 : || (is_ctrl_stmt (stmt)
2813 : 22043615 : && (gimple_code (stmt) != GIMPLE_COND
2814 : 13707352 : || !guards_builtin_unreachable (bb, cache)))
2815 : 100685029 : || gimple_store_p (stmt)
2816 : 225134504 : || gimple_code (stmt) == GIMPLE_ASM)
2817 : 73764748 : mark_stmt_necessary (stmt, worklist);
2818 : : }
2819 : : }
2820 : 149835014 : while (worklist.length () > 0)
2821 : : {
2822 : 142541227 : gimple *stmt = worklist.pop ();
2823 : :
2824 : 142541227 : if (dump_file && (dump_flags & TDF_DETAILS))
2825 : : {
2826 : 207 : fprintf (dump_file, "processing: ");
2827 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2828 : 207 : fprintf (dump_file, "\n");
2829 : : }
2830 : 142541227 : if (gimple_code (stmt) == GIMPLE_PHI)
2831 : 18771497 : for (unsigned int k = 0; k < gimple_phi_num_args (stmt); k++)
2832 : : {
2833 : 13335213 : tree arg = PHI_ARG_DEF (stmt, k);
2834 : :
2835 : 13335213 : if (TREE_CODE (arg) == SSA_NAME)
2836 : 10245675 : mark_operand_necessary (arg, worklist);
2837 : : }
2838 : : else
2839 : : {
2840 : 137104943 : ssa_op_iter iter;
2841 : 137104943 : tree use;
2842 : :
2843 : 247714166 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2844 : 110609223 : mark_operand_necessary (use, worklist);
2845 : : }
2846 : : }
2847 : 7293787 : }
2848 : :
2849 : : /* Analyze function body for NODE.
2850 : : EARLY indicates run from early optimization pipeline. */
2851 : :
2852 : : static void
2853 : 7293787 : analyze_function_body (struct cgraph_node *node, bool early)
2854 : : {
2855 : 7293787 : sreal time = opt_for_fn (node->decl, param_uninlined_function_time);
2856 : : /* Estimate static overhead for function prologue/epilogue and alignment. */
2857 : 7293787 : int size = opt_for_fn (node->decl, param_uninlined_function_insns);
2858 : : /* Benefits are scaled by probability of elimination that is in range
2859 : : <0,2>. */
2860 : 7293787 : basic_block bb;
2861 : 7293787 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2862 : 7293787 : sreal freq;
2863 : 7293787 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
2864 : 7293787 : ipa_node_params *params_summary
2865 : 7293787 : = early ? NULL : ipa_node_params_sum->get (node);
2866 : 7293787 : ipa_predicate bb_predicate;
2867 : 7293787 : struct ipa_func_body_info fbi;
2868 : 7293787 : vec<ipa_predicate> nonconstant_names = vNULL;
2869 : 7293787 : int nblocks, n;
2870 : 7293787 : int *order;
2871 : 7293787 : gimple *fix_builtin_expect_stmt;
2872 : :
2873 : 7293787 : gcc_assert (my_function && my_function->cfg);
2874 : 7293787 : gcc_assert (cfun == my_function);
2875 : :
2876 : 7293787 : memset(&fbi, 0, sizeof(fbi));
2877 : 7293787 : vec_free (info->conds);
2878 : 7293787 : info->conds = NULL;
2879 : 7293787 : info->size_time_table.release ();
2880 : 7293787 : info->call_size_time_table.release ();
2881 : :
2882 : : /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2883 : : so we can produce proper inline hints.
2884 : :
2885 : : When optimizing and analyzing for early inliner, initialize node params
2886 : : so we can produce correct BB predicates. */
2887 : :
2888 : 7293787 : if (opt_for_fn (node->decl, optimize))
2889 : : {
2890 : 6408693 : calculate_dominance_info (CDI_DOMINATORS);
2891 : 6408693 : calculate_dominance_info (CDI_POST_DOMINATORS);
2892 : 6408693 : if (!early)
2893 : 1387039 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2894 : : else
2895 : : {
2896 : 5021654 : ipa_check_create_node_params ();
2897 : 5021654 : ipa_initialize_node_params (node);
2898 : : }
2899 : :
2900 : 6408693 : if (ipa_node_params_sum)
2901 : : {
2902 : 6408693 : fbi.node = node;
2903 : 6408693 : fbi.info = ipa_node_params_sum->get (node);
2904 : 6408693 : fbi.bb_infos = vNULL;
2905 : 6408693 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
2906 : 6408693 : fbi.param_count = count_formal_params (node->decl);
2907 : 6408693 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
2908 : :
2909 : 6408693 : nonconstant_names.safe_grow_cleared
2910 : 6408693 : (SSANAMES (my_function)->length (), true);
2911 : : }
2912 : : }
2913 : :
2914 : 7293787 : if (dump_file)
2915 : 229 : fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2916 : : node->dump_name ());
2917 : :
2918 : : /* When we run into maximal number of entries, we assign everything to the
2919 : : constant truth case. Be sure to have it in list. */
2920 : 7293787 : bb_predicate = true;
2921 : 7293787 : info->account_size_time (0, 0, bb_predicate, bb_predicate);
2922 : :
2923 : 7293787 : bb_predicate = ipa_predicate::not_inlined ();
2924 : 7293787 : info->account_size_time (opt_for_fn (node->decl,
2925 : : param_uninlined_function_insns)
2926 : : * ipa_fn_summary::size_scale,
2927 : 7293787 : opt_for_fn (node->decl,
2928 : : param_uninlined_function_time),
2929 : : bb_predicate,
2930 : : bb_predicate);
2931 : :
2932 : : /* Only look for target information for inlinable functions. */
2933 : 7293787 : bool scan_for_target_info =
2934 : 7293787 : info->inlinable
2935 : 13145065 : && targetm.target_option.need_ipa_fn_target_info (node->decl,
2936 : 5851278 : info->target_info);
2937 : :
2938 : 7293787 : if (fbi.info)
2939 : 6408693 : compute_bb_predicates (&fbi, node, info, params_summary);
2940 : 7293787 : find_necessary_statements (node);
2941 : 7293787 : const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2942 : 7293787 : order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2943 : 7293787 : nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2944 : 49084277 : for (n = 0; n < nblocks; n++)
2945 : : {
2946 : 41790490 : bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2947 : 41790490 : freq = bb->count.to_sreal_scale (entry_count);
2948 : 41790490 : if (clobber_only_eh_bb_p (bb))
2949 : : {
2950 : 548502 : if (dump_file && (dump_flags & TDF_DETAILS))
2951 : 0 : fprintf (dump_file, "\n Ignoring BB %i;"
2952 : : " it will be optimized away by cleanup_clobbers\n",
2953 : : bb->index);
2954 : 548502 : continue;
2955 : : }
2956 : :
2957 : : /* TODO: Obviously predicates can be propagated down across CFG. */
2958 : 41241988 : if (fbi.info)
2959 : : {
2960 : 34914007 : if (bb->aux)
2961 : 34913993 : bb_predicate = *(ipa_predicate *)bb->aux;
2962 : : else
2963 : 14 : bb_predicate = false;
2964 : : }
2965 : : else
2966 : 6327981 : bb_predicate = true;
2967 : :
2968 : 41241988 : if (dump_file && (dump_flags & TDF_DETAILS))
2969 : : {
2970 : 67 : fprintf (dump_file, "\n BB %i predicate:", bb->index);
2971 : 67 : bb_predicate.dump (dump_file, info->conds);
2972 : : }
2973 : :
2974 : 41241988 : if (fbi.info && nonconstant_names.exists ())
2975 : : {
2976 : 34914007 : ipa_predicate phi_predicate;
2977 : 34914007 : bool first_phi = true;
2978 : :
2979 : 35607477 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2980 : 693470 : gsi_next (&bsi))
2981 : : {
2982 : 6493019 : if (first_phi
2983 : 6493019 : && !phi_result_unknown_predicate (&fbi, info,
2984 : : params_summary,
2985 : : bb,
2986 : : &phi_predicate,
2987 : : nonconstant_names))
2988 : : break;
2989 : 693470 : first_phi = false;
2990 : 693470 : if (dump_file && (dump_flags & TDF_DETAILS))
2991 : : {
2992 : 3 : fprintf (dump_file, " ");
2993 : 3 : print_gimple_stmt (dump_file, gsi_stmt (bsi), 0);
2994 : : }
2995 : 693470 : predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2996 : : nonconstant_names);
2997 : : }
2998 : : }
2999 : :
3000 : 41241988 : fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
3001 : :
3002 : 41241988 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
3003 : 182646427 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
3004 : : {
3005 : 141404439 : gimple *stmt = gsi_stmt (bsi);
3006 : 141404439 : if (!gimple_plf (stmt, STMT_NECESSARY))
3007 : : {
3008 : 5601998 : if (dump_file && (dump_flags & TDF_DETAILS))
3009 : : {
3010 : 19 : fprintf (dump_file, " skipping unnecesary stmt ");
3011 : 19 : print_gimple_stmt (dump_file, stmt, 0);
3012 : : }
3013 : : /* TODO: const calls used only to produce values for
3014 : : builtion_unreachable guards should not be accounted. However
3015 : : we still want to inline them and this does does not work well
3016 : : with the cost model. For now account them as usual. */
3017 : 5601998 : if (!is_gimple_call (stmt)
3018 : 5601998 : || gimple_call_internal_p (stmt))
3019 : 5296803 : continue;
3020 : : }
3021 : 136107636 : int this_size = estimate_num_insns (stmt, &eni_size_weights);
3022 : 136107636 : int this_time = estimate_num_insns (stmt, &eni_time_weights);
3023 : 136107636 : int prob;
3024 : 136107636 : ipa_predicate will_be_nonconstant;
3025 : :
3026 : : /* This relation stmt should be folded after we remove
3027 : : __builtin_expect call. Adjust the cost here. */
3028 : 136107636 : if (stmt == fix_builtin_expect_stmt)
3029 : : {
3030 : 150847 : this_size--;
3031 : 150847 : this_time--;
3032 : : }
3033 : :
3034 : 136107636 : if (dump_file && (dump_flags & TDF_DETAILS))
3035 : : {
3036 : 201 : fprintf (dump_file, " ");
3037 : 201 : print_gimple_stmt (dump_file, stmt, 0);
3038 : 201 : fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
3039 : : freq.to_double (), this_size,
3040 : : this_time);
3041 : : }
3042 : :
3043 : 136107636 : if (is_gimple_call (stmt)
3044 : 136107636 : && !gimple_call_internal_p (stmt))
3045 : : {
3046 : 22853268 : struct cgraph_edge *edge = node->get_edge (stmt);
3047 : 22853268 : ipa_call_summary *es = ipa_call_summaries->get_create (edge);
3048 : :
3049 : : /* Special case: results of BUILT_IN_CONSTANT_P will be always
3050 : : resolved as constant. We however don't want to optimize
3051 : : out the cgraph edges. */
3052 : 22853268 : if (nonconstant_names.exists ()
3053 : 20014237 : && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
3054 : 9811 : && gimple_call_lhs (stmt)
3055 : 22863079 : && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3056 : : {
3057 : 9811 : ipa_predicate false_p = false;
3058 : 9811 : nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
3059 : 9811 : = false_p;
3060 : : }
3061 : 22853268 : if (ipa_node_params_sum)
3062 : : {
3063 : 20027233 : int count = gimple_call_num_args (stmt);
3064 : 20027233 : int i;
3065 : :
3066 : 20027233 : if (count)
3067 : 17139178 : es->param.safe_grow_cleared (count, true);
3068 : 58541856 : for (i = 0; i < count; i++)
3069 : : {
3070 : 38514623 : int prob = param_change_prob (&fbi, stmt, i);
3071 : 38514623 : gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
3072 : 38514623 : es->param[i].change_prob = prob;
3073 : 38514623 : es->param[i].points_to_local_or_readonly_memory
3074 : 38514623 : = points_to_local_or_readonly_memory_p
3075 : 38514623 : (gimple_call_arg (stmt, i));
3076 : 38514623 : es->param[i].points_to_possible_sra_candidate
3077 : 38514623 : = points_to_possible_sra_candidate_p
3078 : 38514623 : (gimple_call_arg (stmt, i));
3079 : : }
3080 : : }
3081 : : /* We cannot setup VLA parameters during inlining. */
3082 : 67125462 : for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i)
3083 : 44272590 : if (TREE_CODE (gimple_call_arg (stmt, i)) == WITH_SIZE_EXPR)
3084 : : {
3085 : 396 : edge->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
3086 : 396 : break;
3087 : : }
3088 : 22853268 : es->call_stmt_size = this_size;
3089 : 22853268 : es->call_stmt_time = this_time;
3090 : 22853268 : es->loop_depth = bb_loop_depth (bb);
3091 : 22853268 : edge_set_predicate (edge, &bb_predicate);
3092 : 22853268 : if (edge->speculative)
3093 : : {
3094 : 0 : cgraph_edge *indirect
3095 : 0 : = edge->speculative_call_indirect_edge ();
3096 : 0 : ipa_call_summary *es2
3097 : 0 : = ipa_call_summaries->get_create (indirect);
3098 : 0 : ipa_call_summaries->duplicate (edge, indirect,
3099 : : es, es2);
3100 : :
3101 : : /* Edge is the first direct call.
3102 : : create and duplicate call summaries for multiple
3103 : : speculative call targets. */
3104 : 0 : for (cgraph_edge *direct
3105 : 0 : = edge->next_speculative_call_target ();
3106 : 0 : direct;
3107 : 0 : direct = direct->next_speculative_call_target ())
3108 : : {
3109 : 0 : ipa_call_summary *es3
3110 : 0 : = ipa_call_summaries->get_create (direct);
3111 : 0 : ipa_call_summaries->duplicate (edge, direct,
3112 : : es, es3);
3113 : : }
3114 : : }
3115 : :
3116 : : /* If dealing with a carrying edge, copy its summary over to its
3117 : : attached edges as well. */
3118 : 22853268 : if (edge->has_callback)
3119 : : {
3120 : 15052 : cgraph_edge *cbe;
3121 : 30104 : for (cbe = edge->first_callback_edge (); cbe;
3122 : 15052 : cbe = cbe->next_callback_edge ())
3123 : : {
3124 : 15052 : ipa_call_summary *es2 = ipa_call_summaries->get_create (cbe);
3125 : 15052 : ipa_call_summaries->duplicate (edge, cbe, es, es2);
3126 : : /* Unlike speculative edges, callback edges have no real
3127 : : size or time; the call doesn't exist. Reflect that in
3128 : : their summaries. */
3129 : 15052 : es2->call_stmt_size = 0;
3130 : 15052 : es2->call_stmt_time = 0;
3131 : : }
3132 : : }
3133 : : }
3134 : :
3135 : : /* TODO: When conditional jump or switch is known to be constant, but
3136 : : we did not translate it into the predicates, we really can account
3137 : : just maximum of the possible paths. */
3138 : 136107636 : if (fbi.info)
3139 : 114206253 : will_be_nonconstant
3140 : 114206253 : = will_be_nonconstant_predicate (&fbi, info, params_summary,
3141 : : stmt, nonconstant_names);
3142 : : else
3143 : 21901383 : will_be_nonconstant = true;
3144 : 136107636 : if (this_time || this_size)
3145 : : {
3146 : 111566882 : sreal final_time = (sreal)this_time * freq;
3147 : 111566882 : prob = eliminated_by_inlining_prob (&fbi, stmt);
3148 : 111566882 : if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
3149 : 11 : fprintf (dump_file,
3150 : : "\t\t50%% will be eliminated by inlining\n");
3151 : 111566882 : if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
3152 : 18 : fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
3153 : :
3154 : 111566882 : ipa_predicate p = bb_predicate & will_be_nonconstant;
3155 : 111566882 : int parm = load_or_store_of_ptr_parameter (&fbi, stmt);
3156 : 111566882 : ipa_predicate sra_predicate = true;
3157 : 111566882 : if (parm != -1)
3158 : 14359652 : sra_predicate &= add_condition (info, params_summary, parm,
3159 : : ptr_type_node, NULL,
3160 : 7179826 : ipa_predicate::not_sra_candidate, NULL, 0);
3161 : :
3162 : : /* We can ignore statement when we proved it is never going
3163 : : to happen, but we cannot do that for call statements
3164 : : because edges are accounted specially. */
3165 : :
3166 : 223133764 : if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false)
3167 : : {
3168 : 110855821 : time += final_time;
3169 : 110855821 : size += this_size;
3170 : : }
3171 : :
3172 : : /* We account everything but the calls. Calls have their own
3173 : : size/time info attached to cgraph edges. This is necessary
3174 : : in order to make the cost disappear after inlining. */
3175 : 111566882 : if (!is_gimple_call (stmt))
3176 : : {
3177 : 89330200 : if (prob)
3178 : : {
3179 : 15108951 : ipa_predicate ip
3180 : 15108951 : = bb_predicate & ipa_predicate::not_inlined () & sra_predicate;
3181 : 30217902 : info->account_size_time (this_size * prob,
3182 : 15108951 : (final_time * prob) / 2, ip,
3183 : : p);
3184 : : }
3185 : 15108951 : if (prob != 2)
3186 : 164675864 : info->account_size_time (this_size * (2 - prob),
3187 : 82337932 : (final_time * (2 - prob) / 2),
3188 : 164675864 : bb_predicate & sra_predicate,
3189 : : p);
3190 : : }
3191 : :
3192 : 111566882 : if (!info->fp_expressions && fp_expression_p (stmt))
3193 : : {
3194 : 603013 : info->fp_expressions = true;
3195 : 603013 : if (dump_file)
3196 : 9 : fprintf (dump_file, " fp_expression set\n");
3197 : : }
3198 : : }
3199 : :
3200 : : /* For target specific information, we want to scan all statements
3201 : : rather than those statements with non-zero weights, to avoid
3202 : : missing to scan something interesting for target information,
3203 : : such as: internal function calls. */
3204 : 136107636 : if (scan_for_target_info)
3205 : 0 : scan_for_target_info =
3206 : 0 : targetm.target_option.update_ipa_fn_target_info
3207 : 0 : (info->target_info, stmt);
3208 : :
3209 : : /* Account cost of address calculations in the statements. */
3210 : 526196987 : for (unsigned int i = 0; i < gimple_num_ops (stmt); i++)
3211 : : {
3212 : 390089351 : for (tree op = gimple_op (stmt, i);
3213 : 750660762 : op && handled_component_p (op);
3214 : 46760834 : op = TREE_OPERAND (op, 0))
3215 : 46760834 : if ((TREE_CODE (op) == ARRAY_REF
3216 : 46760834 : || TREE_CODE (op) == ARRAY_RANGE_REF)
3217 : 46760834 : && TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
3218 : : {
3219 : 2347363 : ipa_predicate p = bb_predicate;
3220 : 2347363 : if (fbi.info)
3221 : 1834069 : p = p & will_be_nonconstant_expr_predicate
3222 : 1834069 : (&fbi, info, params_summary,
3223 : 1834069 : TREE_OPERAND (op, 1),
3224 : 1834069 : nonconstant_names);
3225 : 2347363 : if (p != false)
3226 : : {
3227 : 2341862 : time += freq;
3228 : 2341862 : size += 1;
3229 : 2341862 : if (dump_file)
3230 : 24 : fprintf (dump_file,
3231 : : "\t\tAccounting address calculation.\n");
3232 : 2341862 : info->account_size_time (ipa_fn_summary::size_scale,
3233 : : freq,
3234 : : bb_predicate,
3235 : : p);
3236 : : }
3237 : : }
3238 : : }
3239 : :
3240 : : }
3241 : : }
3242 : 7293787 : free (order);
3243 : :
3244 : 7293787 : if (nonconstant_names.exists () && !early)
3245 : : {
3246 : 1387039 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3247 : 1387039 : unsigned max_loop_predicates = opt_for_fn (node->decl,
3248 : : param_ipa_max_loop_predicates);
3249 : :
3250 : 1387039 : if (dump_file && (dump_flags & TDF_DETAILS))
3251 : 14 : flow_loops_dump (dump_file, NULL, 0);
3252 : 1387039 : scev_initialize ();
3253 : 4784016 : for (auto loop : loops_list (cfun, 0))
3254 : : {
3255 : 622899 : ipa_predicate loop_iterations = true;
3256 : 622899 : sreal header_freq;
3257 : 622899 : edge ex;
3258 : 622899 : unsigned int j;
3259 : 622899 : class tree_niter_desc niter_desc;
3260 : 622899 : if (!loop->header->aux)
3261 : 0 : continue;
3262 : :
3263 : 622899 : profile_count hdr_count = loop->header->count;
3264 : 622899 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3265 : :
3266 : 622899 : bb_predicate = *(ipa_predicate *)loop->header->aux;
3267 : 622899 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3268 : 1686167 : FOR_EACH_VEC_ELT (exits, j, ex)
3269 : 1063268 : if (number_of_iterations_exit (loop, ex, &niter_desc, false)
3270 : 1063268 : && !is_gimple_min_invariant (niter_desc.niter))
3271 : : {
3272 : 153480 : ipa_predicate will_be_nonconstant
3273 : 153480 : = will_be_nonconstant_expr_predicate (&fbi, info,
3274 : : params_summary,
3275 : : niter_desc.niter,
3276 : : nonconstant_names);
3277 : 153480 : if (will_be_nonconstant != true)
3278 : 61772 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3279 : 153480 : if (will_be_nonconstant != true
3280 : 215252 : && will_be_nonconstant != false)
3281 : 61216 : loop_iterations &= will_be_nonconstant;
3282 : : }
3283 : 622899 : add_freqcounting_predicate (&s->loop_iterations, loop_iterations,
3284 : : hdr_freq, max_loop_predicates);
3285 : 622899 : }
3286 : :
3287 : : /* To avoid quadratic behavior we analyze stride predicates only
3288 : : with respect to the containing loop. Thus we simply iterate
3289 : : over all defs in the outermost loop body. */
3290 : 1387039 : for (class loop *loop = loops_for_fn (cfun)->tree_root->inner;
3291 : 1876320 : loop != NULL; loop = loop->next)
3292 : : {
3293 : 489281 : ipa_predicate loop_stride = true;
3294 : 489281 : basic_block *body = get_loop_body (loop);
3295 : 489281 : profile_count hdr_count = loop->header->count;
3296 : 489281 : sreal hdr_freq = hdr_count.to_sreal_scale (entry_count);
3297 : 2920966 : for (unsigned i = 0; i < loop->num_nodes; i++)
3298 : : {
3299 : 2431685 : gimple_stmt_iterator gsi;
3300 : 2431685 : if (!body[i]->aux)
3301 : 7 : continue;
3302 : :
3303 : 2431678 : bb_predicate = *(ipa_predicate *)body[i]->aux;
3304 : 15971036 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
3305 : 11107680 : gsi_next (&gsi))
3306 : : {
3307 : 11107680 : gimple *stmt = gsi_stmt (gsi);
3308 : :
3309 : 11107680 : if (!is_gimple_assign (stmt))
3310 : 10957261 : continue;
3311 : :
3312 : 5356854 : tree def = gimple_assign_lhs (stmt);
3313 : 5356854 : if (TREE_CODE (def) != SSA_NAME)
3314 : 1022368 : continue;
3315 : :
3316 : 4334486 : affine_iv iv;
3317 : 8668972 : if (!simple_iv (loop_containing_stmt (stmt),
3318 : : loop_containing_stmt (stmt),
3319 : : def, &iv, true)
3320 : 4334486 : || is_gimple_min_invariant (iv.step))
3321 : 4184067 : continue;
3322 : :
3323 : 150419 : ipa_predicate will_be_nonconstant
3324 : 150419 : = will_be_nonconstant_expr_predicate (&fbi, info,
3325 : : params_summary,
3326 : : iv.step,
3327 : : nonconstant_names);
3328 : 150419 : if (will_be_nonconstant != true)
3329 : 53184 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3330 : 150419 : if (will_be_nonconstant != true
3331 : 203603 : && will_be_nonconstant != false)
3332 : 34312 : loop_stride = loop_stride & will_be_nonconstant;
3333 : : }
3334 : : }
3335 : 489281 : add_freqcounting_predicate (&s->loop_strides, loop_stride,
3336 : : hdr_freq, max_loop_predicates);
3337 : 489281 : free (body);
3338 : : }
3339 : 1387039 : scev_finalize ();
3340 : : }
3341 : 63671851 : FOR_ALL_BB_FN (bb, my_function)
3342 : : {
3343 : 56378064 : edge e;
3344 : 56378064 : edge_iterator ei;
3345 : :
3346 : 56378064 : if (bb->aux)
3347 : 41810594 : edge_predicate_pool.remove ((ipa_predicate *)bb->aux);
3348 : 56378064 : bb->aux = NULL;
3349 : 120026440 : FOR_EACH_EDGE (e, ei, bb->succs)
3350 : : {
3351 : 63648376 : if (e->aux)
3352 : 2463062 : edge_predicate_pool.remove ((ipa_predicate *)e->aux);
3353 : 63648376 : e->aux = NULL;
3354 : : }
3355 : : }
3356 : 7293787 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3357 : 7293787 : ipa_size_summary *ss = ipa_size_summaries->get (node);
3358 : 7293787 : s->time = time;
3359 : 7293787 : ss->self_size = size;
3360 : 7293787 : nonconstant_names.release ();
3361 : 7293787 : ipa_release_body_info (&fbi);
3362 : 7293787 : if (opt_for_fn (node->decl, optimize))
3363 : : {
3364 : 6408693 : if (!early)
3365 : 1387039 : loop_optimizer_finalize ();
3366 : 5021654 : else if (!ipa_edge_args_sum)
3367 : 5021640 : ipa_free_all_node_params ();
3368 : 6408693 : free_dominance_info (CDI_DOMINATORS);
3369 : 6408693 : free_dominance_info (CDI_POST_DOMINATORS);
3370 : : }
3371 : 7293787 : if (dump_file)
3372 : : {
3373 : 229 : fprintf (dump_file, "\n");
3374 : 229 : ipa_dump_fn_summary (dump_file, node);
3375 : : }
3376 : 7293787 : }
3377 : :
3378 : :
3379 : : /* Compute function summary.
3380 : : EARLY is true when we compute parameters during early opts. */
3381 : :
3382 : : void
3383 : 7295108 : compute_fn_summary (struct cgraph_node *node, bool early)
3384 : : {
3385 : 7295108 : HOST_WIDE_INT self_stack_size;
3386 : 7295108 : struct cgraph_edge *e;
3387 : :
3388 : 7295108 : gcc_assert (!node->inlined_to);
3389 : :
3390 : 7295108 : if (!ipa_fn_summaries)
3391 : 213529 : ipa_fn_summary_alloc ();
3392 : :
3393 : : /* Create a new ipa_fn_summary. */
3394 : 7295108 : ((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node);
3395 : 7295108 : ipa_fn_summaries->remove (node);
3396 : 7295108 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
3397 : 7295108 : class ipa_size_summary *size_info = ipa_size_summaries->get_create (node);
3398 : :
3399 : : /* Estimate the stack size for the function if we're optimizing. */
3400 : 12818691 : self_stack_size = optimize && !node->thunk
3401 : 13703801 : ? estimated_stack_frame_size (node) : 0;
3402 : 7295108 : size_info->estimated_self_stack_size = self_stack_size;
3403 : 7295108 : info->estimated_stack_size = self_stack_size;
3404 : :
3405 : 7295108 : if (node->thunk)
3406 : : {
3407 : 1321 : ipa_call_summary *es = ipa_call_summaries->get_create (node->callees);
3408 : 1321 : ipa_predicate t = true;
3409 : :
3410 : 1321 : node->can_change_signature = false;
3411 : 1321 : es->call_stmt_size = eni_size_weights.call_cost;
3412 : 1321 : es->call_stmt_time = eni_time_weights.call_cost;
3413 : 3963 : info->account_size_time (ipa_fn_summary::size_scale
3414 : 1321 : * opt_for_fn (node->decl,
3415 : : param_uninlined_function_thunk_insns),
3416 : 1321 : opt_for_fn (node->decl,
3417 : : param_uninlined_function_thunk_time), t, t);
3418 : 1321 : t = ipa_predicate::not_inlined ();
3419 : 1321 : info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t);
3420 : 1321 : ipa_update_overall_fn_summary (node);
3421 : 1321 : size_info->self_size = size_info->size;
3422 : 1321 : if (stdarg_p (TREE_TYPE (node->decl)))
3423 : : {
3424 : 9 : info->inlinable = false;
3425 : 9 : node->callees->inline_failed = CIF_VARIADIC_THUNK;
3426 : : }
3427 : : else
3428 : 1312 : info->inlinable = true;
3429 : : }
3430 : : else
3431 : : {
3432 : : /* Even is_gimple_min_invariant rely on current_function_decl. */
3433 : 7293787 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3434 : :
3435 : : /* During IPA profile merging we may be called w/o virtual SSA form
3436 : : built. */
3437 : 7293787 : update_ssa (TODO_update_ssa_only_virtuals);
3438 : :
3439 : : /* Can this function be inlined at all? */
3440 : 7293787 : if (!opt_for_fn (node->decl, optimize)
3441 : 8178881 : && !lookup_attribute ("always_inline",
3442 : 885094 : DECL_ATTRIBUTES (node->decl)))
3443 : 818212 : info->inlinable = false;
3444 : : else
3445 : 6475575 : info->inlinable = tree_inlinable_function_p (node->decl);
3446 : :
3447 : 7293787 : bool no_signature = false;
3448 : :
3449 : : /* Don't allow signature changes for functions which have
3450 : : [[gnu::musttail]] or [[clang::musttail]] calls. Sometimes
3451 : : (more often on targets which pass everything on the stack)
3452 : : signature changes can result in tail calls being impossible
3453 : : even when without the signature changes they would be ok.
3454 : : See PR121023. */
3455 : 7293787 : if (cfun->has_musttail)
3456 : : {
3457 : 1410 : if (dump_file)
3458 : 0 : fprintf (dump_file, "No signature change:"
3459 : : " function has calls with musttail attribute.\n");
3460 : : no_signature = true;
3461 : : }
3462 : :
3463 : : /* Type attributes can use parameter indices to describe them.
3464 : : Special case fn spec since we can safely preserve them in
3465 : : modref summaries. */
3466 : 7293787 : for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl));
3467 : 7677208 : list && !no_signature; list = TREE_CHAIN (list))
3468 : 383421 : if (!ipa_param_adjustments::type_attribute_allowed_p
3469 : 383421 : (get_attribute_name (list)))
3470 : : {
3471 : 157172 : if (dump_file)
3472 : : {
3473 : 0 : fprintf (dump_file, "No signature change:"
3474 : : " function type has unhandled attribute %s.\n",
3475 : 0 : IDENTIFIER_POINTER (get_attribute_name (list)));
3476 : : }
3477 : : no_signature = true;
3478 : : }
3479 : 7293787 : for (tree parm = DECL_ARGUMENTS (node->decl);
3480 : 21707655 : parm && !no_signature; parm = DECL_CHAIN (parm))
3481 : 14413868 : if (variably_modified_type_p (TREE_TYPE (parm), node->decl))
3482 : : {
3483 : 15070 : if (dump_file)
3484 : : {
3485 : 0 : fprintf (dump_file, "No signature change:"
3486 : : " has parameter with variably modified type.\n");
3487 : : }
3488 : : no_signature = true;
3489 : : }
3490 : :
3491 : : /* Likewise for #pragma omp declare simd functions or functions
3492 : : with simd attribute. */
3493 : 7293787 : if (no_signature
3494 : 14413922 : || lookup_attribute ("omp declare simd",
3495 : 7120135 : DECL_ATTRIBUTES (node->decl)))
3496 : 175340 : node->can_change_signature = false;
3497 : : else
3498 : : {
3499 : : /* Otherwise, inlinable functions always can change signature. */
3500 : 7118447 : if (info->inlinable)
3501 : 5772556 : node->can_change_signature = true;
3502 : : else
3503 : : {
3504 : : /* Functions calling builtin_apply cannot change signature. */
3505 : 5303572 : for (e = node->callees; e; e = e->next_callee)
3506 : : {
3507 : 3990157 : tree cdecl = e->callee->decl;
3508 : 3990157 : if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS,
3509 : : BUILT_IN_VA_START))
3510 : : break;
3511 : : }
3512 : 1345891 : node->can_change_signature = !e;
3513 : : }
3514 : : }
3515 : 7293787 : analyze_function_body (node, early);
3516 : 7293787 : pop_cfun ();
3517 : : }
3518 : :
3519 : : /* Inlining characteristics are maintained by the cgraph_mark_inline. */
3520 : 7295108 : size_info->size = size_info->self_size;
3521 : 7295108 : info->estimated_stack_size = size_info->estimated_self_stack_size;
3522 : :
3523 : : /* Code above should compute exactly the same result as
3524 : : ipa_update_overall_fn_summary except for case when speculative
3525 : : edges are present since these are accounted to size but not
3526 : : self_size. Do not compare time since different order the roundoff
3527 : : errors result in slight changes. */
3528 : 7295108 : ipa_update_overall_fn_summary (node);
3529 : 7295108 : if (flag_checking)
3530 : : {
3531 : 7779523 : for (e = node->indirect_calls; e; e = e->next_callee)
3532 : 484497 : if (e->speculative)
3533 : : break;
3534 : 7295026 : gcc_assert (e || size_info->size == size_info->self_size);
3535 : : }
3536 : 7295108 : }
3537 : :
3538 : :
3539 : : /* Compute parameters of functions used by inliner using
3540 : : current_function_decl. */
3541 : :
3542 : : static unsigned int
3543 : 5844467 : compute_fn_summary_for_current (void)
3544 : : {
3545 : 5844467 : compute_fn_summary (cgraph_node::get (current_function_decl), true);
3546 : 5844467 : return 0;
3547 : : }
3548 : :
3549 : : /* Estimate benefit devirtualizing indirect edge IE and return true if it can
3550 : : be devirtualized and inlined, provided m_known_vals, m_known_contexts and
3551 : : m_known_aggs in AVALS. Return false straight away if AVALS is NULL. */
3552 : :
3553 : : static bool
3554 : 5323368 : estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3555 : : int *size, int *time,
3556 : : ipa_call_arg_values *avals)
3557 : : {
3558 : 5323368 : tree target;
3559 : 5323368 : struct cgraph_node *callee;
3560 : 5323368 : class ipa_fn_summary *isummary;
3561 : 5323368 : enum availability avail;
3562 : 5323368 : bool speculative;
3563 : :
3564 : 5323368 : if (!avals
3565 : 6762583 : || (!avals->m_known_vals.length() && !avals->m_known_contexts.length ()))
3566 : : return false;
3567 : 1654441 : if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3568 : : return false;
3569 : :
3570 : 1648317 : target = ipa_get_indirect_edge_target
3571 : 1648317 : (ie->callee ? ie->speculative_call_indirect_edge () : ie,
3572 : : avals, &speculative);
3573 : 1648317 : if (!target || speculative)
3574 : : return false;
3575 : :
3576 : : /* If this is speculative call, turn its cost into 0; we will account
3577 : : the call when processing the indirect call. */
3578 : 235462 : if (ie->callee)
3579 : : {
3580 : 11677 : gcc_checking_assert (ie->speculative && *size > 0);
3581 : 11677 : *size = 0;
3582 : 11677 : *time = 0;
3583 : : }
3584 : : else
3585 : : {
3586 : : /* Account for difference in cost between indirect and direct calls. */
3587 : 223785 : *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3588 : 223785 : *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3589 : : }
3590 : 235462 : gcc_checking_assert (*time >= 0);
3591 : 235462 : gcc_checking_assert (*size >= 0);
3592 : :
3593 : 235462 : callee = cgraph_node::get (target);
3594 : 235462 : if (!callee || !callee->definition)
3595 : : return false;
3596 : 215257 : callee = callee->function_symbol (&avail);
3597 : 215257 : if (avail < AVAIL_AVAILABLE)
3598 : : return false;
3599 : 215231 : isummary = ipa_fn_summaries->get (callee);
3600 : 215231 : if (isummary == NULL)
3601 : : return false;
3602 : :
3603 : 215226 : return isummary->inlinable;
3604 : : }
3605 : :
3606 : : /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3607 : : handle edge E with probability PROB. Set HINTS accordingly if edge may be
3608 : : devirtualized. AVALS, if non-NULL, describes the context of the call site
3609 : : as far as values of parameters are concerened. */
3610 : :
3611 : : static inline void
3612 : 201042441 : estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3613 : : sreal *time, ipa_call_arg_values *avals,
3614 : : ipa_hints *hints)
3615 : : {
3616 : 201042441 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3617 : 201042441 : int call_size = es->call_stmt_size;
3618 : 201042441 : int call_time = es->call_stmt_time;
3619 : 201042441 : int cur_size;
3620 : :
3621 : 196579707 : if ((!e->callee || e->speculative)
3622 : 201903075 : && estimate_edge_devirt_benefit (e, &call_size, &call_time, avals))
3623 : : {
3624 : 214893 : if (hints && e->maybe_hot_p ())
3625 : 204012 : *hints |= INLINE_HINT_indirect_call;
3626 : : }
3627 : 201042441 : cur_size = call_size * ipa_fn_summary::size_scale;
3628 : 201042441 : *size += cur_size;
3629 : 201042441 : if (min_size)
3630 : 27947859 : *min_size += cur_size;
3631 : 201042441 : if (time)
3632 : 191509300 : *time += ((sreal)call_time) * e->sreal_frequency ();
3633 : 201042441 : }
3634 : :
3635 : :
3636 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3637 : : calls in NODE. POSSIBLE_TRUTHS and AVALS describe the context of the call
3638 : : site.
3639 : :
3640 : : Helper for estimate_calls_size_and_time which does the same but
3641 : : (in most cases) faster. */
3642 : :
3643 : : static void
3644 : 69111726 : estimate_calls_size_and_time_1 (struct cgraph_node *node, int *size,
3645 : : int *min_size, sreal *time,
3646 : : ipa_hints *hints,
3647 : : clause_t possible_truths,
3648 : : ipa_call_arg_values *avals)
3649 : : {
3650 : 69111726 : struct cgraph_edge *e;
3651 : 326449117 : for (e = node->callees; e; e = e->next_callee)
3652 : : {
3653 : 257337391 : if (!e->inline_failed)
3654 : : {
3655 : 49260585 : gcc_checking_assert (!ipa_call_summaries->get (e));
3656 : 49260585 : estimate_calls_size_and_time_1 (e->callee, size, min_size, time,
3657 : : hints, possible_truths, avals);
3658 : :
3659 : 49260585 : continue;
3660 : : }
3661 : 208076806 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3662 : :
3663 : : /* Do not care about zero sized builtins. */
3664 : 208076806 : if (!es->call_stmt_size)
3665 : : {
3666 : 19763801 : gcc_checking_assert (!es->call_stmt_time);
3667 : 19763801 : continue;
3668 : : }
3669 : 188313005 : if (!es->predicate
3670 : 188313005 : || es->predicate->evaluate (possible_truths))
3671 : : {
3672 : : /* Predicates of calls shall not use NOT_CHANGED codes,
3673 : : so we do not need to compute probabilities. */
3674 : 185945200 : estimate_edge_size_and_time (e, size,
3675 : 185945200 : es->predicate ? NULL : min_size,
3676 : : time, avals, hints);
3677 : : }
3678 : : }
3679 : 73227733 : for (e = node->indirect_calls; e; e = e->next_callee)
3680 : : {
3681 : 4116007 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3682 : 4116007 : if (!es->predicate
3683 : 4116007 : || es->predicate->evaluate (possible_truths))
3684 : 4089434 : estimate_edge_size_and_time (e, size,
3685 : 4089434 : es->predicate ? NULL : min_size,
3686 : : time, avals, hints);
3687 : : }
3688 : 69111726 : }
3689 : :
3690 : : /* Populate sum->call_size_time_table for edges from NODE. */
3691 : :
3692 : : static void
3693 : 3025866 : summarize_calls_size_and_time (struct cgraph_node *node,
3694 : : ipa_fn_summary *sum)
3695 : : {
3696 : 3025866 : struct cgraph_edge *e;
3697 : 14109224 : for (e = node->callees; e; e = e->next_callee)
3698 : : {
3699 : 11083358 : if (!e->inline_failed)
3700 : : {
3701 : 1301438 : gcc_checking_assert (!ipa_call_summaries->get (e));
3702 : 1301438 : summarize_calls_size_and_time (e->callee, sum);
3703 : 1301438 : continue;
3704 : : }
3705 : 9781920 : int size = 0;
3706 : 9781920 : sreal time = 0;
3707 : :
3708 : 9781920 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3709 : :
3710 : 9781920 : ipa_predicate pred = true;
3711 : 9781920 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3712 : :
3713 : 9781920 : if (es->predicate)
3714 : 1819627 : pred = *es->predicate;
3715 : 9781920 : sum->account_size_time (size, time, pred, pred, true);
3716 : : }
3717 : 3399166 : for (e = node->indirect_calls; e; e = e->next_callee)
3718 : : {
3719 : 373300 : int size = 0;
3720 : 373300 : sreal time = 0;
3721 : :
3722 : 373300 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3723 : 373300 : ipa_predicate pred = true;
3724 : 373300 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3725 : :
3726 : 373300 : if (es->predicate)
3727 : 137400 : pred = *es->predicate;
3728 : 373300 : sum->account_size_time (size, time, pred, pred, true);
3729 : : }
3730 : 3025866 : }
3731 : :
3732 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3733 : : calls in NODE. POSSIBLE_TRUTHS and AVALS (the latter if non-NULL) describe
3734 : : context of the call site. */
3735 : :
3736 : : static void
3737 : 19851165 : estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3738 : : int *min_size, sreal *time,
3739 : : ipa_hints *hints,
3740 : : clause_t possible_truths,
3741 : : ipa_call_arg_values *avals)
3742 : : {
3743 : 19851165 : class ipa_fn_summary *sum = ipa_fn_summaries->get (node);
3744 : 19851165 : bool use_table = true;
3745 : :
3746 : 19851165 : gcc_assert (node->callees || node->indirect_calls);
3747 : :
3748 : : /* During early inlining we do not calculate info for very
3749 : : large functions and thus there is no need for producing
3750 : : summaries. */
3751 : 19851165 : if (!ipa_node_params_sum)
3752 : : use_table = false;
3753 : : /* Do not calculate summaries for simple wrappers; it is waste
3754 : : of memory. */
3755 : 10763996 : else if (node->callees && !node->indirect_calls
3756 : 9963433 : && node->callees->inline_failed && !node->callees->next_callee)
3757 : : use_table = false;
3758 : : /* If there is an indirect edge that may be optimized, we need
3759 : : to go the slow way. */
3760 : 8587692 : else if (avals
3761 : 8587692 : && (avals->m_known_vals.length ()
3762 : 3287221 : || avals->m_known_contexts.length ()
3763 : 3103356 : || avals->m_known_aggs.length ()))
3764 : : {
3765 : 3940660 : ipa_node_params *params_summary = ipa_node_params_sum->get (node);
3766 : 3940660 : unsigned int nargs = params_summary
3767 : 3940660 : ? ipa_get_param_count (params_summary) : 0;
3768 : :
3769 : 13224374 : for (unsigned int i = 0; i < nargs && use_table; i++)
3770 : : {
3771 : 9283714 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3772 : 9283714 : && (avals->safe_sval_at (i)
3773 : 312148 : || (ipa_argagg_value_list (avals).value_for_index_p (i))))
3774 : : use_table = false;
3775 : 9179218 : else if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3776 : 9179218 : && (avals->m_known_contexts.length () > i
3777 : 9484759 : && !avals->m_known_contexts[i].useless_p ()))
3778 : : use_table = false;
3779 : : }
3780 : : }
3781 : :
3782 : : /* Fast path is via the call size time table. */
3783 : 3940660 : if (use_table)
3784 : : {
3785 : : /* Build summary if it is absent. */
3786 : 8282463 : if (!sum->call_size_time_table.length ())
3787 : : {
3788 : 871841 : ipa_predicate true_pred = true;
3789 : 871841 : sum->account_size_time (0, 0, true_pred, true_pred, true);
3790 : 871841 : summarize_calls_size_and_time (node, sum);
3791 : : }
3792 : :
3793 : 8282463 : int old_size = *size;
3794 : 8282463 : sreal old_time = time ? *time : 0;
3795 : :
3796 : 8282463 : if (min_size)
3797 : 8282463 : *min_size += sum->call_size_time_table[0].size;
3798 : :
3799 : : unsigned int i;
3800 : : size_time_entry *e;
3801 : :
3802 : : /* Walk the table and account sizes and times. */
3803 : 22356732 : for (i = 0; sum->call_size_time_table.iterate (i, &e);
3804 : : i++)
3805 : 14074269 : if (e->exec_predicate.evaluate (possible_truths))
3806 : : {
3807 : 12937873 : *size += e->size;
3808 : 12937873 : if (time)
3809 : 10828749 : *time += e->time;
3810 : : }
3811 : :
3812 : : /* Be careful and see if both methods agree. */
3813 : 24 : if ((flag_checking || dump_file)
3814 : : /* Do not try to sanity check when we know we lost some
3815 : : precision. */
3816 : 8282463 : && sum->call_size_time_table.length ()
3817 : : < ipa_fn_summary::max_size_time_table_size)
3818 : : {
3819 : 8282439 : estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL,
3820 : : possible_truths, avals);
3821 : 8282439 : gcc_assert (*size == old_size);
3822 : 15189518 : if (time && (*time - old_time > 1 || *time - old_time < -1)
3823 : 8282452 : && dump_file)
3824 : 0 : fprintf (dump_file, "Time mismatch in call summary %f!=%f\n",
3825 : : old_time.to_double (),
3826 : : time->to_double ());
3827 : : }
3828 : : }
3829 : : /* Slow path by walking all edges. */
3830 : : else
3831 : 11568702 : estimate_calls_size_and_time_1 (node, size, min_size, time, hints,
3832 : : possible_truths, avals);
3833 : 19851165 : }
3834 : :
3835 : : /* Main constructor for ipa call context. Memory allocation of ARG_VALUES
3836 : : is owned by the caller. INLINE_PARAM_SUMMARY is also owned by the
3837 : : caller. */
3838 : :
3839 : 19362975 : ipa_call_context::ipa_call_context (cgraph_node *node, clause_t possible_truths,
3840 : : clause_t nonspec_possible_truths,
3841 : : vec<inline_param_summary>
3842 : : inline_param_summary,
3843 : 19362975 : ipa_auto_call_arg_values *arg_values)
3844 : 19362975 : : m_node (node), m_possible_truths (possible_truths),
3845 : 19362975 : m_nonspec_possible_truths (nonspec_possible_truths),
3846 : 19362975 : m_inline_param_summary (inline_param_summary),
3847 : 19362975 : m_avals (arg_values)
3848 : : {
3849 : 19362975 : }
3850 : :
3851 : : /* Set THIS to be a duplicate of CTX. Copy all relevant info. */
3852 : :
3853 : : void
3854 : 1940999 : ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx)
3855 : : {
3856 : 1940999 : m_node = ctx.m_node;
3857 : 1940999 : m_possible_truths = ctx.m_possible_truths;
3858 : 1940999 : m_nonspec_possible_truths = ctx.m_nonspec_possible_truths;
3859 : 1940999 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3860 : 1940999 : unsigned int nargs = params_summary
3861 : 1940999 : ? ipa_get_param_count (params_summary) : 0;
3862 : :
3863 : 1940999 : m_inline_param_summary = vNULL;
3864 : : /* Copy the info only if there is at least one useful entry. */
3865 : 1940999 : if (ctx.m_inline_param_summary.exists ())
3866 : : {
3867 : 1731740 : unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs);
3868 : :
3869 : 3964091 : for (unsigned int i = 0; i < n; i++)
3870 : 3262822 : if (ipa_is_param_used_by_ipa_predicates (params_summary, i)
3871 : 4589002 : && !ctx.m_inline_param_summary[i].useless_p ())
3872 : : {
3873 : 1030471 : m_inline_param_summary
3874 : 1030471 : = ctx.m_inline_param_summary.copy ();
3875 : 1030471 : break;
3876 : : }
3877 : : }
3878 : 1940999 : m_avals.m_known_vals = vNULL;
3879 : 1940999 : if (ctx.m_avals.m_known_vals.exists ())
3880 : : {
3881 : 1940999 : unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs);
3882 : :
3883 : 4704382 : for (unsigned int i = 0; i < n; i++)
3884 : 2794233 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3885 : 2794233 : && ctx.m_avals.m_known_vals[i])
3886 : : {
3887 : 30850 : m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy ();
3888 : 30850 : break;
3889 : : }
3890 : : }
3891 : :
3892 : 1940999 : m_avals.m_known_contexts = vNULL;
3893 : 1940999 : if (ctx.m_avals.m_known_contexts.exists ())
3894 : : {
3895 : 1940999 : unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs);
3896 : :
3897 : 1942197 : for (unsigned int i = 0; i < n; i++)
3898 : 26980 : if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3899 : 26980 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3900 : : {
3901 : 25782 : m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy ();
3902 : 25782 : break;
3903 : : }
3904 : : }
3905 : :
3906 : 1940999 : m_avals.m_known_aggs = vNULL;
3907 : 1940999 : if (ctx.m_avals.m_known_aggs.exists ())
3908 : : {
3909 : 1940999 : const ipa_argagg_value_list avl (&ctx.m_avals);
3910 : 6597581 : for (unsigned int i = 0; i < nargs; i++)
3911 : 4660992 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3912 : 4660992 : && avl.value_for_index_p (i))
3913 : : {
3914 : 4410 : m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy ();
3915 : 4410 : break;
3916 : : }
3917 : : }
3918 : :
3919 : 1940999 : m_avals.m_known_value_ranges = vNULL;
3920 : 1940999 : }
3921 : :
3922 : : /* Release memory used by known_vals/contexts/aggs vectors. and
3923 : : inline_param_summary. */
3924 : :
3925 : : void
3926 : 3147063 : ipa_cached_call_context::release ()
3927 : : {
3928 : : /* See if context is initialized at first place. */
3929 : 3147063 : if (!m_node)
3930 : : return;
3931 : 1940999 : m_avals.m_known_aggs.release ();
3932 : 1940999 : m_avals.m_known_vals.release ();
3933 : 1940999 : m_avals.m_known_contexts.release ();
3934 : 1940999 : m_inline_param_summary.release ();
3935 : : }
3936 : :
3937 : : /* Return true if CTX describes the same call context as THIS. */
3938 : :
3939 : : bool
3940 : 6868743 : ipa_call_context::equal_to (const ipa_call_context &ctx)
3941 : : {
3942 : 6868743 : if (m_node != ctx.m_node
3943 : 5662679 : || m_possible_truths != ctx.m_possible_truths
3944 : 5051349 : || m_nonspec_possible_truths != ctx.m_nonspec_possible_truths)
3945 : : return false;
3946 : :
3947 : 5051349 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3948 : 5051349 : unsigned int nargs = params_summary
3949 : 5051349 : ? ipa_get_param_count (params_summary) : 0;
3950 : :
3951 : 5051349 : if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ())
3952 : : {
3953 : 15180027 : for (unsigned int i = 0; i < nargs; i++)
3954 : : {
3955 : 10456653 : if (!ipa_is_param_used_by_ipa_predicates (params_summary, i))
3956 : 3309590 : continue;
3957 : 7147063 : if (i >= m_inline_param_summary.length ()
3958 : 4986901 : || m_inline_param_summary[i].useless_p ())
3959 : : {
3960 : 3085463 : if (i < ctx.m_inline_param_summary.length ()
3961 : 3085463 : && !ctx.m_inline_param_summary[i].useless_p ())
3962 : : return false;
3963 : 3042394 : continue;
3964 : : }
3965 : 4061600 : if (i >= ctx.m_inline_param_summary.length ()
3966 : 4061597 : || ctx.m_inline_param_summary[i].useless_p ())
3967 : : {
3968 : 42738 : if (i < m_inline_param_summary.length ()
3969 : 42738 : && !m_inline_param_summary[i].useless_p ())
3970 : : return false;
3971 : 0 : continue;
3972 : : }
3973 : 4018862 : if (!m_inline_param_summary[i].equal_to
3974 : 4018862 : (ctx.m_inline_param_summary[i]))
3975 : : return false;
3976 : : }
3977 : : }
3978 : 4941526 : if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ())
3979 : : {
3980 : 15200086 : for (unsigned int i = 0; i < nargs; i++)
3981 : : {
3982 : 10268951 : if (!ipa_is_param_used_by_indirect_call (params_summary, i))
3983 : 10025083 : continue;
3984 : 243868 : if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i])
3985 : : {
3986 : 182470 : if (i < ctx.m_avals.m_known_vals.length ()
3987 : 182470 : && ctx.m_avals.m_known_vals[i])
3988 : : return false;
3989 : 182383 : continue;
3990 : : }
3991 : 61398 : if (i >= ctx.m_avals.m_known_vals.length ()
3992 : 61398 : || !ctx.m_avals.m_known_vals[i])
3993 : : {
3994 : : if (i < m_avals.m_known_vals.length () && m_avals.m_known_vals[i])
3995 : : return false;
3996 : : continue;
3997 : : }
3998 : 61327 : if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i])
3999 : : return false;
4000 : : }
4001 : : }
4002 : 4931135 : if (m_avals.m_known_contexts.exists ()
4003 : 4931135 : || ctx.m_avals.m_known_contexts.exists ())
4004 : : {
4005 : 15184383 : for (unsigned int i = 0; i < nargs; i++)
4006 : : {
4007 : 10255122 : if (!ipa_is_param_used_by_polymorphic_call (params_summary, i))
4008 : 10086594 : continue;
4009 : 168528 : if (i >= m_avals.m_known_contexts.length ()
4010 : 167512 : || m_avals.m_known_contexts[i].useless_p ())
4011 : : {
4012 : 1016 : if (i < ctx.m_avals.m_known_contexts.length ()
4013 : 1016 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
4014 : : return false;
4015 : 1008 : continue;
4016 : : }
4017 : 167512 : if (i >= ctx.m_avals.m_known_contexts.length ()
4018 : 167512 : || ctx.m_avals.m_known_contexts[i].useless_p ())
4019 : : {
4020 : 8 : if (i < m_avals.m_known_contexts.length ()
4021 : 1941007 : && !m_avals.m_known_contexts[i].useless_p ())
4022 : : return false;
4023 : 0 : continue;
4024 : : }
4025 : 167504 : if (!m_avals.m_known_contexts[i].equal_to
4026 : 167504 : (ctx.m_avals.m_known_contexts[i]))
4027 : : return false;
4028 : : }
4029 : : }
4030 : 4929261 : if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ())
4031 : : {
4032 : : unsigned i = 0, j = 0;
4033 : 11211032 : while (i < m_avals.m_known_aggs.length ()
4034 : 5604337 : || j < ctx.m_avals.m_known_aggs.length ())
4035 : : {
4036 : 676593 : if (i >= m_avals.m_known_aggs.length ())
4037 : : {
4038 : 667507 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4039 : 667507 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4040 : : return false;
4041 : 666841 : j++;
4042 : 666841 : continue;
4043 : 666841 : }
4044 : 9086 : if (j >= ctx.m_avals.m_known_aggs.length ())
4045 : : {
4046 : 684 : int idx1 = m_avals.m_known_aggs[i].index;
4047 : 684 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4048 : : return false;
4049 : 4 : i++;
4050 : 4 : continue;
4051 : 4 : }
4052 : :
4053 : 8402 : int idx1 = m_avals.m_known_aggs[i].index;
4054 : 8402 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4055 : 8402 : if (idx1 < idx2)
4056 : : {
4057 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4058 : : return false;
4059 : 0 : i++;
4060 : 0 : continue;
4061 : : }
4062 : 8402 : if (idx1 > idx2)
4063 : : {
4064 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4065 : : return false;
4066 : 0 : j++;
4067 : 0 : continue;
4068 : : }
4069 : 8402 : if (!ipa_is_param_used_by_indirect_call (params_summary, idx1))
4070 : : {
4071 : 335 : i++;
4072 : 335 : j++;
4073 : 335 : continue;
4074 : : }
4075 : :
4076 : 8067 : if ((m_avals.m_known_aggs[i].unit_offset
4077 : 8067 : != ctx.m_avals.m_known_aggs[j].unit_offset)
4078 : 8059 : || (m_avals.m_known_aggs[i].by_ref
4079 : 8059 : != ctx.m_avals.m_known_aggs[j].by_ref)
4080 : 16126 : || !operand_equal_p (m_avals.m_known_aggs[i].value,
4081 : 8059 : ctx.m_avals.m_known_aggs[j].value))
4082 : 171 : return false;
4083 : 7896 : i++;
4084 : 7896 : j++;
4085 : : }
4086 : : }
4087 : : return true;
4088 : : }
4089 : :
4090 : : /* Fill in the selected fields in ESTIMATES with value estimated for call in
4091 : : this context. Always compute size and min_size. Only compute time and
4092 : : nonspecialized_time if EST_TIMES is true. Only compute hints if EST_HINTS
4093 : : is true. */
4094 : :
4095 : : void
4096 : 19322709 : ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates,
4097 : : bool est_times, bool est_hints)
4098 : : {
4099 : 19322709 : class ipa_fn_summary *info = ipa_fn_summaries->get (m_node);
4100 : 19322709 : size_time_entry *e;
4101 : 19322709 : int size = 0;
4102 : 19322709 : sreal time = 0;
4103 : 19322709 : int min_size = 0;
4104 : 19322709 : ipa_hints hints = 0;
4105 : 19322709 : sreal loops_with_known_iterations = 0;
4106 : 19322709 : sreal loops_with_known_strides = 0;
4107 : 19322709 : int i;
4108 : :
4109 : 19322709 : if (dump_file && (dump_flags & TDF_DETAILS))
4110 : : {
4111 : 1276 : bool found = false;
4112 : 1276 : fprintf (dump_file, " Estimating body: %s\n"
4113 : : " Known to be false: ", m_node->dump_name ());
4114 : :
4115 : 3931 : for (i = ipa_predicate::not_inlined_condition;
4116 : 7862 : i < (ipa_predicate::first_dynamic_condition
4117 : 6300 : + (int) vec_safe_length (info->conds)); i++)
4118 : 2655 : if (!(m_possible_truths & (1 << i)))
4119 : : {
4120 : 1661 : if (found)
4121 : 435 : fprintf (dump_file, ", ");
4122 : 1661 : found = true;
4123 : 1661 : dump_condition (dump_file, info->conds, i);
4124 : : }
4125 : : }
4126 : :
4127 : 19322709 : if (m_node->callees || m_node->indirect_calls)
4128 : 24955891 : estimate_calls_size_and_time (m_node, &size, &min_size,
4129 : : est_times ? &time : NULL,
4130 : : est_hints ? &hints : NULL, m_possible_truths,
4131 : : &m_avals);
4132 : :
4133 : 19322709 : sreal nonspecialized_time = time;
4134 : :
4135 : 19322709 : min_size += info->size_time_table[0].size;
4136 : 131943353 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4137 : : {
4138 : 112620644 : bool exec = e->exec_predicate.evaluate (m_nonspec_possible_truths);
4139 : :
4140 : : /* Because predicates are conservative, it can happen that nonconst is 1
4141 : : but exec is 0. */
4142 : 112620644 : if (exec)
4143 : : {
4144 : 109688703 : bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths);
4145 : :
4146 : 109688703 : gcc_checking_assert (e->time >= 0);
4147 : 109688703 : gcc_checking_assert (time >= 0);
4148 : :
4149 : : /* We compute specialized size only because size of nonspecialized
4150 : : copy is context independent.
4151 : :
4152 : : The difference between nonspecialized execution and specialized is
4153 : : that nonspecialized is not going to have optimized out computations
4154 : : known to be constant in a specialized setting. */
4155 : 109688703 : if (nonconst)
4156 : 54364120 : size += e->size;
4157 : 109688703 : if (!est_times)
4158 : 57867672 : continue;
4159 : 51821031 : nonspecialized_time += e->time;
4160 : 51821031 : if (!nonconst)
4161 : : ;
4162 : 24121159 : else if (!m_inline_param_summary.exists ())
4163 : : {
4164 : 1621990 : if (nonconst)
4165 : 1621990 : time += e->time;
4166 : : }
4167 : : else
4168 : : {
4169 : 22499169 : int prob = e->nonconst_predicate.probability
4170 : 22499169 : (info->conds, m_possible_truths,
4171 : : m_inline_param_summary);
4172 : 22499169 : gcc_checking_assert (prob >= 0);
4173 : 22499169 : gcc_checking_assert (prob <= REG_BR_PROB_BASE);
4174 : 22499169 : if (prob == REG_BR_PROB_BASE)
4175 : 19339875 : time += e->time;
4176 : : else
4177 : 3159294 : time += e->time * prob / REG_BR_PROB_BASE;
4178 : : }
4179 : 51821031 : gcc_checking_assert (time >= 0);
4180 : : }
4181 : : }
4182 : 19322709 : gcc_checking_assert (info->size_time_table[0].exec_predicate == true);
4183 : 19322709 : gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true);
4184 : 19322709 : gcc_checking_assert (min_size >= 0);
4185 : 19322709 : gcc_checking_assert (size >= 0);
4186 : 19322709 : gcc_checking_assert (time >= 0);
4187 : : /* nonspecialized_time should be always bigger than specialized time.
4188 : : Roundoff issues however may get into the way. */
4189 : 19322709 : gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1);
4190 : :
4191 : : /* Roundoff issues may make specialized time bigger than nonspecialized
4192 : : time. We do not really want that to happen because some heuristics
4193 : : may get confused by seeing negative speedups. */
4194 : 19322709 : if (time > nonspecialized_time)
4195 : 0 : time = nonspecialized_time;
4196 : :
4197 : 19322709 : if (est_hints)
4198 : : {
4199 : 7010787 : if (info->scc_no)
4200 : 202927 : hints |= INLINE_HINT_in_scc;
4201 : 7010787 : if (DECL_DECLARED_INLINE_P (m_node->decl))
4202 : 4284564 : hints |= INLINE_HINT_declared_inline;
4203 : 7010787 : if (info->builtin_constant_p_parms.length ()
4204 : 11154 : && DECL_DECLARED_INLINE_P (m_node->decl))
4205 : 11098 : hints |= INLINE_HINT_builtin_constant_p;
4206 : :
4207 : : ipa_freqcounting_predicate *fcp;
4208 : 7651241 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
4209 : 640454 : if (!fcp->predicate->evaluate (m_possible_truths))
4210 : : {
4211 : 457677 : hints |= INLINE_HINT_loop_iterations;
4212 : 457677 : loops_with_known_iterations += fcp->freq;
4213 : : }
4214 : 7010787 : estimates->loops_with_known_iterations = loops_with_known_iterations;
4215 : :
4216 : 7396548 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
4217 : 385761 : if (!fcp->predicate->evaluate (m_possible_truths))
4218 : : {
4219 : 369498 : hints |= INLINE_HINT_loop_stride;
4220 : 369498 : loops_with_known_strides += fcp->freq;
4221 : : }
4222 : 7010787 : estimates->loops_with_known_strides = loops_with_known_strides;
4223 : : }
4224 : :
4225 : 19322709 : size = RDIV (size, ipa_fn_summary::size_scale);
4226 : 19322709 : min_size = RDIV (min_size, ipa_fn_summary::size_scale);
4227 : :
4228 : 19322709 : if (dump_file && (dump_flags & TDF_DETAILS))
4229 : : {
4230 : 1276 : fprintf (dump_file, "\n size:%i", (int) size);
4231 : 1276 : if (est_times)
4232 : 1039 : fprintf (dump_file, " time:%f nonspec time:%f",
4233 : : time.to_double (), nonspecialized_time.to_double ());
4234 : 1276 : if (est_hints)
4235 : 1039 : fprintf (dump_file, " loops with known iterations:%f "
4236 : : "known strides:%f", loops_with_known_iterations.to_double (),
4237 : : loops_with_known_strides.to_double ());
4238 : 1276 : fprintf (dump_file, "\n");
4239 : : }
4240 : 19322709 : if (est_times)
4241 : : {
4242 : 7010787 : estimates->time = time;
4243 : 7010787 : estimates->nonspecialized_time = nonspecialized_time;
4244 : : }
4245 : 19322709 : estimates->size = size;
4246 : 19322709 : estimates->min_size = min_size;
4247 : 19322709 : if (est_hints)
4248 : 7010787 : estimates->hints = hints;
4249 : 19322709 : return;
4250 : : }
4251 : :
4252 : :
4253 : : /* Estimate size and time needed to execute callee of EDGE assuming that
4254 : : parameters known to be constant at caller of EDGE are propagated.
4255 : : KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
4256 : : and types for parameters. */
4257 : :
4258 : : void
4259 : 178509 : estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
4260 : : ipa_auto_call_arg_values *avals,
4261 : : ipa_call_estimates *estimates)
4262 : : {
4263 : 178509 : clause_t clause, nonspec_clause;
4264 : :
4265 : 178509 : evaluate_conditions_for_known_args (node, false, avals, &clause,
4266 : : &nonspec_clause, NULL);
4267 : 178509 : ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals);
4268 : 178509 : ctx.estimate_size_and_time (estimates);
4269 : 178509 : }
4270 : :
4271 : : /* Return stack frame offset where frame of NODE is supposed to start inside
4272 : : of the function it is inlined to.
4273 : : Return 0 for functions that are not inlined. */
4274 : :
4275 : : HOST_WIDE_INT
4276 : 5165613 : ipa_get_stack_frame_offset (struct cgraph_node *node)
4277 : : {
4278 : 5165613 : HOST_WIDE_INT offset = 0;
4279 : 5165613 : if (!node->inlined_to)
4280 : : return 0;
4281 : 4060596 : node = node->callers->caller;
4282 : 4983134 : while (true)
4283 : : {
4284 : 4983134 : offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
4285 : 4521865 : if (!node->inlined_to)
4286 : : return offset;
4287 : 461269 : node = node->callers->caller;
4288 : : }
4289 : : }
4290 : :
4291 : :
4292 : : /* Update summary information of inline clones after inlining.
4293 : : Compute peak stack usage. */
4294 : :
4295 : : static void
4296 : 4730283 : inline_update_callee_summaries (struct cgraph_node *node, int depth)
4297 : : {
4298 : 4730283 : struct cgraph_edge *e;
4299 : :
4300 : 4730283 : ipa_propagate_frequency (node);
4301 : 8739777 : for (e = node->callees; e; e = e->next_callee)
4302 : : {
4303 : 4009494 : if (!e->inline_failed)
4304 : 670240 : inline_update_callee_summaries (e->callee, depth);
4305 : : else
4306 : 3339254 : ipa_call_summaries->get (e)->loop_depth += depth;
4307 : : }
4308 : 4844032 : for (e = node->indirect_calls; e; e = e->next_callee)
4309 : 113749 : ipa_call_summaries->get (e)->loop_depth += depth;
4310 : 4730283 : }
4311 : :
4312 : : /* Update change_prob and points_to_local_or_readonly_memory of EDGE after
4313 : : INLINED_EDGE has been inlined.
4314 : :
4315 : : When function A is inlined in B and A calls C with parameter that
4316 : : changes with probability PROB1 and C is known to be passthrough
4317 : : of argument if B that change with probability PROB2, the probability
4318 : : of change is now PROB1*PROB2. */
4319 : :
4320 : : static void
4321 : 3453311 : remap_edge_params (struct cgraph_edge *inlined_edge,
4322 : : struct cgraph_edge *edge)
4323 : : {
4324 : 3453311 : if (ipa_node_params_sum)
4325 : : {
4326 : 2486118 : int i;
4327 : 2486118 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4328 : 2486118 : if (!args)
4329 : : return;
4330 : 1405897 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4331 : 1405897 : class ipa_call_summary *inlined_es
4332 : 1405897 : = ipa_call_summaries->get (inlined_edge);
4333 : :
4334 : 1405897 : if (es->param.length () == 0)
4335 : : return;
4336 : :
4337 : 8122806 : for (i = 0; i < ipa_get_cs_argument_count (args); i++)
4338 : : {
4339 : 2736248 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4340 : 2736248 : if (jfunc->type == IPA_JF_PASS_THROUGH
4341 : 2008337 : || jfunc->type == IPA_JF_ANCESTOR)
4342 : : {
4343 : 880911 : int id = jfunc->type == IPA_JF_PASS_THROUGH
4344 : 880911 : ? ipa_get_jf_pass_through_formal_id (jfunc)
4345 : 153000 : : ipa_get_jf_ancestor_formal_id (jfunc);
4346 : 1761739 : if (id < (int) inlined_es->param.length ())
4347 : : {
4348 : 880820 : int prob1 = es->param[i].change_prob;
4349 : 880820 : int prob2 = inlined_es->param[id].change_prob;
4350 : 880820 : int prob = combine_probabilities (prob1, prob2);
4351 : :
4352 : 880820 : if (prob1 && prob2 && !prob)
4353 : 880820 : prob = 1;
4354 : :
4355 : 880820 : es->param[i].change_prob = prob;
4356 : :
4357 : 1761640 : if (inlined_es
4358 : 880820 : ->param[id].points_to_local_or_readonly_memory)
4359 : 224842 : es->param[i].points_to_local_or_readonly_memory = true;
4360 : 1761640 : if (inlined_es
4361 : 880820 : ->param[id].points_to_possible_sra_candidate)
4362 : 197280 : es->param[i].points_to_possible_sra_candidate = true;
4363 : : }
4364 : 880911 : if (!es->param[i].points_to_local_or_readonly_memory
4365 : : && jfunc->type == IPA_JF_CONST
4366 : : && points_to_local_or_readonly_memory_p
4367 : : (ipa_get_jf_constant (jfunc)))
4368 : : es->param[i].points_to_local_or_readonly_memory = true;
4369 : : }
4370 : : }
4371 : : }
4372 : : }
4373 : :
4374 : : /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
4375 : :
4376 : : Remap predicates of callees of NODE. Rest of arguments match
4377 : : remap_predicate.
4378 : :
4379 : : Also update change probabilities. */
4380 : :
4381 : : static void
4382 : 4730283 : remap_edge_summaries (struct cgraph_edge *inlined_edge,
4383 : : struct cgraph_node *node,
4384 : : class ipa_fn_summary *info,
4385 : : class ipa_node_params *params_summary,
4386 : : class ipa_fn_summary *callee_info,
4387 : : const vec<int> &operand_map,
4388 : : const vec<HOST_WIDE_INT> &offset_map,
4389 : : clause_t possible_truths,
4390 : : ipa_predicate *toplev_predicate)
4391 : : {
4392 : 4730283 : struct cgraph_edge *e, *next;
4393 : 8739332 : for (e = node->callees; e; e = next)
4394 : : {
4395 : 4009049 : ipa_predicate p;
4396 : 4009049 : next = e->next_callee;
4397 : :
4398 : 4009049 : if (e->inline_failed)
4399 : : {
4400 : 3338809 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4401 : 3338809 : remap_edge_params (inlined_edge, e);
4402 : :
4403 : 3338809 : if (es->predicate)
4404 : : {
4405 : 1008299 : p = es->predicate->remap_after_inlining
4406 : 1008299 : (info, params_summary,
4407 : : callee_info, operand_map,
4408 : : offset_map, possible_truths,
4409 : : *toplev_predicate);
4410 : 1008299 : edge_set_predicate (e, &p);
4411 : : }
4412 : : else
4413 : 2330510 : edge_set_predicate (e, toplev_predicate);
4414 : : }
4415 : : else
4416 : 670240 : remap_edge_summaries (inlined_edge, e->callee, info,
4417 : : params_summary, callee_info,
4418 : : operand_map, offset_map, possible_truths,
4419 : : toplev_predicate);
4420 : : }
4421 : 4844785 : for (e = node->indirect_calls; e; e = next)
4422 : : {
4423 : 114502 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4424 : 114502 : ipa_predicate p;
4425 : 114502 : next = e->next_callee;
4426 : :
4427 : 114502 : remap_edge_params (inlined_edge, e);
4428 : 114502 : if (es->predicate)
4429 : : {
4430 : 27780 : p = es->predicate->remap_after_inlining
4431 : 27780 : (info, params_summary,
4432 : : callee_info, operand_map, offset_map,
4433 : : possible_truths, *toplev_predicate);
4434 : 27780 : edge_set_predicate (e, &p);
4435 : : }
4436 : : else
4437 : 86722 : edge_set_predicate (e, toplev_predicate);
4438 : : }
4439 : 4730283 : }
4440 : :
4441 : : /* Run remap_after_inlining on each predicate in V. */
4442 : :
4443 : : static void
4444 : 8120086 : remap_freqcounting_predicate (class ipa_fn_summary *info,
4445 : : class ipa_node_params *params_summary,
4446 : : class ipa_fn_summary *callee_info,
4447 : : vec<ipa_freqcounting_predicate, va_gc> *v,
4448 : : const vec<int> &operand_map,
4449 : : const vec<HOST_WIDE_INT> &offset_map,
4450 : : clause_t possible_truths,
4451 : : ipa_predicate *toplev_predicate)
4452 : :
4453 : : {
4454 : 8120086 : ipa_freqcounting_predicate *fcp;
4455 : 8149938 : for (int i = 0; vec_safe_iterate (v, i, &fcp); i++)
4456 : : {
4457 : 29852 : ipa_predicate p
4458 : 29852 : = fcp->predicate->remap_after_inlining (info, params_summary,
4459 : : callee_info, operand_map,
4460 : : offset_map, possible_truths,
4461 : : *toplev_predicate);
4462 : 41639 : if (p != false && p != true)
4463 : 3362 : *fcp->predicate &= p;
4464 : : }
4465 : 8120086 : }
4466 : :
4467 : : /* We inlined EDGE. Update summary of the function we inlined into. */
4468 : :
4469 : : void
4470 : 4060043 : ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
4471 : : {
4472 : 4060043 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
4473 : 3824855 : struct cgraph_node *to = (edge->caller->inlined_to
4474 : 4060043 : ? edge->caller->inlined_to : edge->caller);
4475 : 4060043 : class ipa_fn_summary *info = ipa_fn_summaries->get (to);
4476 : 4060043 : clause_t clause = 0; /* not_inline is known to be false. */
4477 : 4060043 : size_time_entry *e;
4478 : 4060043 : auto_vec<int, 8> operand_map;
4479 : 4060043 : auto_vec<HOST_WIDE_INT, 8> offset_map;
4480 : 4060043 : int i;
4481 : 4060043 : ipa_predicate toplev_predicate;
4482 : 4060043 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4483 : 4060043 : ipa_node_params *params_summary = (ipa_node_params_sum
4484 : 4060043 : ? ipa_node_params_sum->get (to) : NULL);
4485 : :
4486 : 4060043 : if (es->predicate)
4487 : 347089 : toplev_predicate = *es->predicate;
4488 : : else
4489 : 3712954 : toplev_predicate = true;
4490 : :
4491 : 4060043 : info->fp_expressions |= callee_info->fp_expressions;
4492 : 4060043 : info->target_info |= callee_info->target_info;
4493 : :
4494 : 4060043 : if (callee_info->conds)
4495 : : {
4496 : 2949314 : ipa_auto_call_arg_values avals;
4497 : 2949314 : evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
4498 : 2949314 : }
4499 : 4060043 : if (ipa_node_params_sum && callee_info->conds)
4500 : : {
4501 : 792646 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4502 : 792646 : int count = args ? ipa_get_cs_argument_count (args) : 0;
4503 : 792509 : int i;
4504 : :
4505 : 792509 : if (count)
4506 : : {
4507 : 792509 : operand_map.safe_grow_cleared (count, true);
4508 : 792509 : offset_map.safe_grow_cleared (count, true);
4509 : : }
4510 : 2494555 : for (i = 0; i < count; i++)
4511 : : {
4512 : 1701909 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4513 : 1701909 : int map = -1;
4514 : :
4515 : : /* TODO: handle non-NOPs when merging. */
4516 : 1701909 : if (jfunc->type == IPA_JF_PASS_THROUGH)
4517 : : {
4518 : 291225 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4519 : 288123 : map = ipa_get_jf_pass_through_formal_id (jfunc);
4520 : 291225 : if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
4521 : 193057 : offset_map[i] = -1;
4522 : : }
4523 : 1410684 : else if (jfunc->type == IPA_JF_ANCESTOR)
4524 : : {
4525 : 90883 : HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
4526 : 90883 : if (offset >= 0 && offset < INT_MAX)
4527 : : {
4528 : 90883 : map = ipa_get_jf_ancestor_formal_id (jfunc);
4529 : 90883 : if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
4530 : 53355 : offset = -1;
4531 : 90883 : offset_map[i] = offset;
4532 : : }
4533 : : }
4534 : 1701909 : operand_map[i] = map;
4535 : 3083992 : gcc_assert (map < ipa_get_param_count (params_summary));
4536 : : }
4537 : :
4538 : : int ip;
4539 : 795618 : for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++)
4540 : 2972 : if (ip < count && operand_map[ip] >= 0)
4541 : 33 : add_builtin_constant_p_parm (info, operand_map[ip]);
4542 : : }
4543 : 4060043 : sreal freq = edge->sreal_frequency ();
4544 : 22227962 : for (i = 0; callee_info->size_time_table.iterate (i, &e); i++)
4545 : : {
4546 : 18167919 : ipa_predicate p;
4547 : 18167919 : p = e->exec_predicate.remap_after_inlining
4548 : 18167919 : (info, params_summary,
4549 : : callee_info, operand_map,
4550 : : offset_map, clause,
4551 : : toplev_predicate);
4552 : 18167919 : ipa_predicate nonconstp;
4553 : 18167919 : nonconstp = e->nonconst_predicate.remap_after_inlining
4554 : 18167919 : (info, params_summary,
4555 : : callee_info, operand_map,
4556 : : offset_map, clause,
4557 : : toplev_predicate);
4558 : 27318425 : if (p != false && nonconstp != false)
4559 : : {
4560 : 8824344 : sreal add_time = ((sreal)e->time * freq);
4561 : 8824344 : int prob = e->nonconst_predicate.probability (callee_info->conds,
4562 : : clause, es->param);
4563 : 8824344 : if (prob != REG_BR_PROB_BASE)
4564 : 1138404 : add_time = add_time * prob / REG_BR_PROB_BASE;
4565 : 1138404 : if (prob != REG_BR_PROB_BASE
4566 : 1138404 : && dump_file && (dump_flags & TDF_DETAILS))
4567 : : {
4568 : 3 : fprintf (dump_file, "\t\tScaling time by probability:%f\n",
4569 : 3 : (double) prob / REG_BR_PROB_BASE);
4570 : : }
4571 : 8824344 : info->account_size_time (e->size, add_time, p, nonconstp);
4572 : : }
4573 : : }
4574 : 4060043 : remap_edge_summaries (edge, edge->callee, info, params_summary,
4575 : : callee_info, operand_map,
4576 : : offset_map, clause, &toplev_predicate);
4577 : 4060043 : remap_freqcounting_predicate (info, params_summary, callee_info,
4578 : : info->loop_iterations, operand_map,
4579 : : offset_map, clause, &toplev_predicate);
4580 : 4060043 : remap_freqcounting_predicate (info, params_summary, callee_info,
4581 : : info->loop_strides, operand_map,
4582 : : offset_map, clause, &toplev_predicate);
4583 : :
4584 : 4060043 : HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
4585 : 4060043 : HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
4586 : :
4587 : 4060043 : if (info->estimated_stack_size < peak)
4588 : 126001 : info->estimated_stack_size = peak;
4589 : :
4590 : 4060043 : inline_update_callee_summaries (edge->callee, es->loop_depth);
4591 : 4060043 : if (info->call_size_time_table.length ())
4592 : : {
4593 : 852587 : int edge_size = 0;
4594 : 852587 : sreal edge_time = 0;
4595 : :
4596 : 852587 : estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0);
4597 : : /* Unaccount size and time of the optimized out call. */
4598 : 852587 : info->account_size_time (-edge_size, -edge_time,
4599 : 852587 : es->predicate ? *es->predicate : true,
4600 : 852587 : es->predicate ? *es->predicate : true,
4601 : : true);
4602 : : /* Account new calls. */
4603 : 852587 : summarize_calls_size_and_time (edge->callee, info);
4604 : : }
4605 : :
4606 : : /* Free summaries that are not maintained for inline clones/edges. */
4607 : 4060043 : ipa_call_summaries->remove (edge);
4608 : 4060043 : ipa_fn_summaries->remove (edge->callee);
4609 : 4060043 : ipa_remove_from_growth_caches (edge);
4610 : 4060043 : }
4611 : :
4612 : : /* For performance reasons ipa_merge_fn_summary_after_inlining is not updating
4613 : : overall size and time. Recompute it.
4614 : : If RESET is true also recompute call_time_size_table. */
4615 : :
4616 : : void
4617 : 9546662 : ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset)
4618 : : {
4619 : 9546662 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
4620 : 9546662 : class ipa_size_summary *size_info = ipa_size_summaries->get (node);
4621 : 9546662 : size_time_entry *e;
4622 : 9546662 : int i;
4623 : :
4624 : 9546662 : size_info->size = 0;
4625 : 9546662 : info->time = 0;
4626 : 48585396 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4627 : : {
4628 : 39038734 : size_info->size += e->size;
4629 : 39038734 : info->time += e->time;
4630 : : }
4631 : 9546662 : info->min_size = info->size_time_table[0].size;
4632 : 9546662 : if (reset)
4633 : 8648362 : info->call_size_time_table.release ();
4634 : 9546662 : if (node->callees || node->indirect_calls)
4635 : 7202974 : estimate_calls_size_and_time (node, &size_info->size, &info->min_size,
4636 : : &info->time, NULL,
4637 : : ~(clause_t) (1 << ipa_predicate::false_condition),
4638 : : NULL);
4639 : 9546662 : size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale);
4640 : 9546662 : info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale);
4641 : 9546662 : }
4642 : :
4643 : :
4644 : : /* This function performs intraprocedural analysis in NODE that is required to
4645 : : inline indirect calls. */
4646 : :
4647 : : static void
4648 : 1387035 : inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4649 : : {
4650 : 1387035 : ipa_analyze_node (node);
4651 : 1387035 : if (dump_file && (dump_flags & TDF_DETAILS))
4652 : : {
4653 : 14 : ipa_print_node_params (dump_file, node);
4654 : 14 : ipa_print_node_jump_functions (dump_file, node);
4655 : : }
4656 : 1387035 : }
4657 : :
4658 : :
4659 : : /* Note function body size. */
4660 : :
4661 : : void
4662 : 1399345 : inline_analyze_function (struct cgraph_node *node)
4663 : : {
4664 : 1399345 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4665 : :
4666 : 1399345 : if (dump_file)
4667 : 94 : fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ());
4668 : 1399345 : if (opt_for_fn (node->decl, optimize) && !node->thunk)
4669 : 1387035 : inline_indirect_intraprocedural_analysis (node);
4670 : 1399345 : compute_fn_summary (node, false);
4671 : 1399345 : if (!optimize)
4672 : : {
4673 : 11005 : struct cgraph_edge *e;
4674 : 23942 : for (e = node->callees; e; e = e->next_callee)
4675 : 12937 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4676 : 11076 : for (e = node->indirect_calls; e; e = e->next_callee)
4677 : 71 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4678 : : }
4679 : :
4680 : 1399345 : pop_cfun ();
4681 : 1399345 : }
4682 : :
4683 : :
4684 : : /* Called when new function is inserted to callgraph late. */
4685 : :
4686 : : void
4687 : 19270 : ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
4688 : : {
4689 : 19270 : inline_analyze_function (node);
4690 : 19270 : }
4691 : :
4692 : : /* Note function body size. */
4693 : :
4694 : : static void
4695 : 231437 : ipa_fn_summary_generate (void)
4696 : : {
4697 : 231437 : struct cgraph_node *node;
4698 : :
4699 : 2099327 : FOR_EACH_DEFINED_FUNCTION (node)
4700 : 1867890 : if (DECL_STRUCT_FUNCTION (node->decl))
4701 : 1854908 : node->versionable = tree_versionable_function_p (node->decl);
4702 : :
4703 : 231437 : ipa_fn_summary_alloc ();
4704 : :
4705 : 231437 : ipa_fn_summaries->enable_insertion_hook ();
4706 : :
4707 : 231437 : ipa_register_cgraph_hooks ();
4708 : :
4709 : 2099327 : FOR_EACH_DEFINED_FUNCTION (node)
4710 : 1867890 : if (!node->alias
4711 : 1867890 : && (flag_generate_lto || flag_generate_offload|| flag_wpa
4712 : 1677130 : || opt_for_fn (node->decl, optimize)))
4713 : 1361970 : inline_analyze_function (node);
4714 : 231437 : }
4715 : :
4716 : :
4717 : : /* Write inline summary for edge E to OB. */
4718 : :
4719 : : static void
4720 : 345822 : read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
4721 : : bool prevails)
4722 : : {
4723 : 345822 : class ipa_call_summary *es = prevails
4724 : 345822 : ? ipa_call_summaries->get_create (e) : NULL;
4725 : 345822 : ipa_predicate p;
4726 : 345822 : int length, i;
4727 : :
4728 : 345822 : int size = streamer_read_uhwi (ib);
4729 : 345822 : int time = streamer_read_uhwi (ib);
4730 : 345822 : int depth = streamer_read_uhwi (ib);
4731 : :
4732 : 345822 : if (es)
4733 : : {
4734 : 345773 : es->call_stmt_size = size;
4735 : 345773 : es->call_stmt_time = time;
4736 : 345773 : es->loop_depth = depth;
4737 : : }
4738 : :
4739 : 345822 : bitpack_d bp = streamer_read_bitpack (ib);
4740 : 345822 : if (es)
4741 : 345773 : es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
4742 : : else
4743 : 49 : bp_unpack_value (&bp, 1);
4744 : :
4745 : 345822 : p.stream_in (ib);
4746 : 345822 : if (es)
4747 : 345773 : edge_set_predicate (e, &p);
4748 : 345822 : length = streamer_read_uhwi (ib);
4749 : 345822 : if (length && es
4750 : 345822 : && (e->possibly_call_in_translation_unit_p ()
4751 : : /* Also stream in jump functions to builtins in hope that they
4752 : : will get fnspecs. */
4753 : 118111 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
4754 : : {
4755 : 231691 : es->param.safe_grow_cleared (length, true);
4756 : 797577 : for (i = 0; i < length; i++)
4757 : : {
4758 : 565886 : es->param[i].change_prob = streamer_read_uhwi (ib);
4759 : 565886 : bitpack_d bp = streamer_read_bitpack (ib);
4760 : 1697658 : es->param[i].points_to_local_or_readonly_memory
4761 : 565886 : = bp_unpack_value (&bp, 1);
4762 : 1697658 : es->param[i].points_to_possible_sra_candidate
4763 : 565886 : = bp_unpack_value (&bp, 1);
4764 : : }
4765 : : }
4766 : : else
4767 : : {
4768 : 136433 : for (i = 0; i < length; i++)
4769 : : {
4770 : 22302 : streamer_read_uhwi (ib);
4771 : 22302 : streamer_read_uhwi (ib);
4772 : : }
4773 : : }
4774 : 345822 : }
4775 : :
4776 : :
4777 : : /* Stream in inline summaries from the section. */
4778 : :
4779 : : static void
4780 : 14207 : inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4781 : : size_t len)
4782 : : {
4783 : 14207 : const struct lto_function_header *header =
4784 : : (const struct lto_function_header *) data;
4785 : 14207 : const int cfg_offset = sizeof (struct lto_function_header);
4786 : 14207 : const int main_offset = cfg_offset + header->cfg_size;
4787 : 14207 : const int string_offset = main_offset + header->main_size;
4788 : 14207 : class data_in *data_in;
4789 : 14207 : unsigned int i, count2, j;
4790 : 14207 : unsigned int f_count;
4791 : :
4792 : 14207 : lto_input_block ib ((const char *) data + main_offset, header->main_size,
4793 : 14207 : file_data);
4794 : :
4795 : 14207 : data_in =
4796 : 28414 : lto_data_in_create (file_data, (const char *) data + string_offset,
4797 : 14207 : header->string_size, vNULL);
4798 : 14207 : f_count = streamer_read_uhwi (&ib);
4799 : 100658 : for (i = 0; i < f_count; i++)
4800 : : {
4801 : 86451 : unsigned int index;
4802 : 86451 : struct cgraph_node *node;
4803 : 86451 : class ipa_fn_summary *info;
4804 : 86451 : class ipa_node_params *params_summary;
4805 : 86451 : class ipa_size_summary *size_info;
4806 : 86451 : lto_symtab_encoder_t encoder;
4807 : 86451 : struct bitpack_d bp;
4808 : 86451 : struct cgraph_edge *e;
4809 : 86451 : ipa_predicate p;
4810 : :
4811 : 86451 : index = streamer_read_uhwi (&ib);
4812 : 86451 : encoder = file_data->symtab_node_encoder;
4813 : 86451 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4814 : : index));
4815 : 86451 : info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
4816 : 86451 : params_summary = node->prevailing_p ()
4817 : 86451 : ? ipa_node_params_sum->get (node) : NULL;
4818 : 86451 : size_info = node->prevailing_p ()
4819 : 86451 : ? ipa_size_summaries->get_create (node) : NULL;
4820 : :
4821 : 86451 : int stack_size = streamer_read_uhwi (&ib);
4822 : 86451 : int size = streamer_read_uhwi (&ib);
4823 : 86451 : sreal time = sreal::stream_in (&ib);
4824 : :
4825 : 86451 : if (info)
4826 : : {
4827 : 86393 : info->estimated_stack_size
4828 : 86393 : = size_info->estimated_self_stack_size = stack_size;
4829 : 86393 : size_info->size = size_info->self_size = size;
4830 : 86393 : info->time = time;
4831 : : }
4832 : :
4833 : 86451 : bp = streamer_read_bitpack (&ib);
4834 : 86451 : if (info)
4835 : : {
4836 : 86393 : info->inlinable = bp_unpack_value (&bp, 1);
4837 : 86393 : info->fp_expressions = bp_unpack_value (&bp, 1);
4838 : 86393 : if (!lto_stream_offload_p)
4839 : 86393 : info->target_info = streamer_read_uhwi (&ib);
4840 : : }
4841 : : else
4842 : : {
4843 : 58 : bp_unpack_value (&bp, 1);
4844 : 58 : bp_unpack_value (&bp, 1);
4845 : 58 : if (!lto_stream_offload_p)
4846 : 58 : streamer_read_uhwi (&ib);
4847 : : }
4848 : :
4849 : 86451 : count2 = streamer_read_uhwi (&ib);
4850 : 86451 : gcc_assert (!info || !info->conds);
4851 : 86393 : if (info)
4852 : 86393 : vec_safe_reserve_exact (info->conds, count2);
4853 : 162528 : for (j = 0; j < count2; j++)
4854 : : {
4855 : 76077 : struct condition c;
4856 : 76077 : unsigned int k, count3;
4857 : 76077 : c.operand_num = streamer_read_uhwi (&ib);
4858 : 76077 : c.code = (enum tree_code) streamer_read_uhwi (&ib);
4859 : 76077 : c.type = stream_read_tree (&ib, data_in);
4860 : 76077 : c.val = stream_read_tree (&ib, data_in);
4861 : 76077 : bp = streamer_read_bitpack (&ib);
4862 : 76077 : c.agg_contents = bp_unpack_value (&bp, 1);
4863 : 76077 : c.by_ref = bp_unpack_value (&bp, 1);
4864 : 76077 : if (c.agg_contents)
4865 : 12149 : c.offset = streamer_read_uhwi (&ib);
4866 : 76077 : count3 = streamer_read_uhwi (&ib);
4867 : 76077 : c.param_ops = NULL;
4868 : 76077 : if (info)
4869 : 76077 : vec_safe_reserve_exact (c.param_ops, count3);
4870 : 76077 : if (params_summary)
4871 : 76077 : ipa_set_param_used_by_ipa_predicates
4872 : 76077 : (params_summary, c.operand_num, true);
4873 : 79553 : for (k = 0; k < count3; k++)
4874 : : {
4875 : 3476 : struct expr_eval_op op;
4876 : 3476 : enum gimple_rhs_class rhs_class;
4877 : 3476 : op.code = (enum tree_code) streamer_read_uhwi (&ib);
4878 : 3476 : op.type = stream_read_tree (&ib, data_in);
4879 : 3476 : switch (rhs_class = get_gimple_rhs_class (op.code))
4880 : : {
4881 : 1389 : case GIMPLE_UNARY_RHS:
4882 : 1389 : op.index = 0;
4883 : 1389 : op.val[0] = NULL_TREE;
4884 : 1389 : op.val[1] = NULL_TREE;
4885 : 1389 : break;
4886 : :
4887 : 2087 : case GIMPLE_BINARY_RHS:
4888 : 2087 : case GIMPLE_TERNARY_RHS:
4889 : 2087 : bp = streamer_read_bitpack (&ib);
4890 : 2087 : op.index = bp_unpack_value (&bp, 2);
4891 : 2087 : op.val[0] = stream_read_tree (&ib, data_in);
4892 : 2087 : if (rhs_class == GIMPLE_BINARY_RHS)
4893 : 2087 : op.val[1] = NULL_TREE;
4894 : : else
4895 : 0 : op.val[1] = stream_read_tree (&ib, data_in);
4896 : : break;
4897 : :
4898 : 0 : default:
4899 : 0 : fatal_error (UNKNOWN_LOCATION,
4900 : : "invalid fnsummary in LTO stream");
4901 : : }
4902 : 3476 : if (info)
4903 : 3476 : c.param_ops->quick_push (op);
4904 : : }
4905 : 76077 : if (info)
4906 : 76077 : info->conds->quick_push (c);
4907 : : }
4908 : 86451 : count2 = streamer_read_uhwi (&ib);
4909 : 86451 : gcc_assert (!info || !info->size_time_table.length ());
4910 : 86451 : if (info && count2)
4911 : 86393 : info->size_time_table.reserve_exact (count2);
4912 : 327328 : for (j = 0; j < count2; j++)
4913 : : {
4914 : 240877 : class size_time_entry e;
4915 : :
4916 : 240877 : e.size = streamer_read_uhwi (&ib);
4917 : 240877 : e.time = sreal::stream_in (&ib);
4918 : 240877 : e.exec_predicate.stream_in (&ib);
4919 : 240877 : e.nonconst_predicate.stream_in (&ib);
4920 : :
4921 : 240877 : if (info)
4922 : 240761 : info->size_time_table.quick_push (e);
4923 : : }
4924 : :
4925 : 86451 : count2 = streamer_read_uhwi (&ib);
4926 : 87771 : for (j = 0; j < count2; j++)
4927 : : {
4928 : 1320 : p.stream_in (&ib);
4929 : 1320 : sreal fcp_freq = sreal::stream_in (&ib);
4930 : 1320 : if (info)
4931 : : {
4932 : 1320 : ipa_freqcounting_predicate fcp;
4933 : 1320 : fcp.predicate = NULL;
4934 : 1320 : set_hint_predicate (&fcp.predicate, p);
4935 : 1320 : fcp.freq = fcp_freq;
4936 : 1320 : vec_safe_push (info->loop_iterations, fcp);
4937 : : }
4938 : : }
4939 : 86451 : count2 = streamer_read_uhwi (&ib);
4940 : 86737 : for (j = 0; j < count2; j++)
4941 : : {
4942 : 286 : p.stream_in (&ib);
4943 : 286 : sreal fcp_freq = sreal::stream_in (&ib);
4944 : 286 : if (info)
4945 : : {
4946 : 286 : ipa_freqcounting_predicate fcp;
4947 : 286 : fcp.predicate = NULL;
4948 : 286 : set_hint_predicate (&fcp.predicate, p);
4949 : 286 : fcp.freq = fcp_freq;
4950 : 286 : vec_safe_push (info->loop_strides, fcp);
4951 : : }
4952 : : }
4953 : 86451 : count2 = streamer_read_uhwi (&ib);
4954 : 86451 : if (info && count2)
4955 : 6 : info->builtin_constant_p_parms.reserve_exact (count2);
4956 : 86457 : for (j = 0; j < count2; j++)
4957 : : {
4958 : 6 : int parm = streamer_read_uhwi (&ib);
4959 : 6 : if (info)
4960 : 6 : info->builtin_constant_p_parms.quick_push (parm);
4961 : : }
4962 : 430809 : for (e = node->callees; e; e = e->next_callee)
4963 : 344358 : read_ipa_call_summary (&ib, e, info != NULL);
4964 : 87915 : for (e = node->indirect_calls; e; e = e->next_callee)
4965 : 1464 : read_ipa_call_summary (&ib, e, info != NULL);
4966 : : }
4967 : :
4968 : 14207 : lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4969 : : len);
4970 : 14207 : lto_data_in_delete (data_in);
4971 : 14207 : }
4972 : :
4973 : :
4974 : : /* Read inline summary. Jump functions are shared among ipa-cp
4975 : : and inliner, so when ipa-cp is active, we don't need to write them
4976 : : twice. */
4977 : :
4978 : : static void
4979 : 13167 : ipa_fn_summary_read (void)
4980 : : {
4981 : 13167 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4982 : 13167 : struct lto_file_decl_data *file_data;
4983 : 13167 : unsigned int j = 0;
4984 : :
4985 : 13167 : ipa_prop_read_jump_functions ();
4986 : 13167 : ipa_fn_summary_alloc ();
4987 : :
4988 : 40541 : while ((file_data = file_data_vec[j++]))
4989 : : {
4990 : 14207 : size_t len;
4991 : 14207 : const char *data
4992 : 14207 : = lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary,
4993 : : &len);
4994 : 14207 : if (data)
4995 : 14207 : inline_read_section (file_data, data, len);
4996 : : else
4997 : : /* Fatal error here. We do not want to support compiling ltrans units
4998 : : with different version of compiler or different flags than the WPA
4999 : : unit, so this should never happen. */
5000 : 0 : fatal_error (input_location,
5001 : : "ipa inline summary is missing in input file");
5002 : : }
5003 : 13167 : ipa_register_cgraph_hooks ();
5004 : :
5005 : 13167 : gcc_assert (ipa_fn_summaries);
5006 : 13167 : ipa_fn_summaries->enable_insertion_hook ();
5007 : 13167 : }
5008 : :
5009 : :
5010 : : /* Write inline summary for edge E to OB. */
5011 : :
5012 : : static void
5013 : 375952 : write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
5014 : : {
5015 : 375952 : class ipa_call_summary *es = ipa_call_summaries->get (e);
5016 : 375952 : int i;
5017 : :
5018 : 375952 : streamer_write_uhwi (ob, es->call_stmt_size);
5019 : 375952 : streamer_write_uhwi (ob, es->call_stmt_time);
5020 : 375952 : streamer_write_uhwi (ob, es->loop_depth);
5021 : :
5022 : 375952 : bitpack_d bp = bitpack_create (ob->main_stream);
5023 : 375952 : bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
5024 : 375952 : streamer_write_bitpack (&bp);
5025 : :
5026 : 375952 : if (es->predicate)
5027 : 10144 : es->predicate->stream_out (ob);
5028 : : else
5029 : 365808 : streamer_write_uhwi (ob, 0);
5030 : 375952 : streamer_write_uhwi (ob, es->param.length ());
5031 : 2271245 : for (i = 0; i < (int) es->param.length (); i++)
5032 : : {
5033 : 626813 : streamer_write_uhwi (ob, es->param[i].change_prob);
5034 : 626813 : bp = bitpack_create (ob->main_stream);
5035 : 626813 : bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1);
5036 : 626813 : bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1);
5037 : 626813 : streamer_write_bitpack (&bp);
5038 : : }
5039 : 375952 : }
5040 : :
5041 : :
5042 : : /* Write inline summary for node in SET.
5043 : : Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
5044 : : active, we don't need to write them twice. */
5045 : :
5046 : : static void
5047 : 24263 : ipa_fn_summary_write (void)
5048 : : {
5049 : 24263 : struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
5050 : 24263 : lto_symtab_encoder_iterator lsei;
5051 : 24263 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5052 : 24263 : unsigned int count = 0;
5053 : :
5054 : 129612 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5055 : 105349 : lsei_next_function_in_partition (&lsei))
5056 : : {
5057 : 105349 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5058 : 105349 : if (cnode->definition && !cnode->alias)
5059 : 102866 : count++;
5060 : : }
5061 : 24263 : streamer_write_uhwi (ob, count);
5062 : :
5063 : 129612 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5064 : 105349 : lsei_next_function_in_partition (&lsei))
5065 : : {
5066 : 105349 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5067 : 105349 : if (cnode->definition && !cnode->alias)
5068 : : {
5069 : 102866 : class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
5070 : 102866 : class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
5071 : 102866 : struct bitpack_d bp;
5072 : 102866 : struct cgraph_edge *edge;
5073 : 102866 : int i;
5074 : 102866 : size_time_entry *e;
5075 : 102866 : struct condition *c;
5076 : :
5077 : 102866 : streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
5078 : 102866 : streamer_write_hwi (ob, size_info->estimated_self_stack_size);
5079 : 102866 : streamer_write_hwi (ob, size_info->self_size);
5080 : 102866 : info->time.stream_out (ob);
5081 : 102866 : bp = bitpack_create (ob->main_stream);
5082 : 102866 : bp_pack_value (&bp, info->inlinable, 1);
5083 : 102866 : bp_pack_value (&bp, info->fp_expressions, 1);
5084 : 102866 : streamer_write_bitpack (&bp);
5085 : 102866 : if (!lto_stream_offload_p)
5086 : 102866 : streamer_write_uhwi (ob, info->target_info);
5087 : 102866 : streamer_write_uhwi (ob, vec_safe_length (info->conds));
5088 : 194054 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
5089 : : {
5090 : 91188 : int j;
5091 : 91188 : struct expr_eval_op *op;
5092 : :
5093 : 91188 : streamer_write_uhwi (ob, c->operand_num);
5094 : 91188 : streamer_write_uhwi (ob, c->code);
5095 : 91188 : stream_write_tree (ob, c->type, true);
5096 : 91188 : stream_write_tree (ob, c->val, true);
5097 : 91188 : bp = bitpack_create (ob->main_stream);
5098 : 91188 : bp_pack_value (&bp, c->agg_contents, 1);
5099 : 91188 : bp_pack_value (&bp, c->by_ref, 1);
5100 : 91188 : streamer_write_bitpack (&bp);
5101 : 91188 : if (c->agg_contents)
5102 : 15145 : streamer_write_uhwi (ob, c->offset);
5103 : 91188 : streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
5104 : 189231 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
5105 : : {
5106 : 4016 : streamer_write_uhwi (ob, op->code);
5107 : 4016 : stream_write_tree (ob, op->type, true);
5108 : 4016 : if (op->val[0])
5109 : : {
5110 : 2418 : bp = bitpack_create (ob->main_stream);
5111 : 2418 : bp_pack_value (&bp, op->index, 2);
5112 : 2418 : streamer_write_bitpack (&bp);
5113 : 2418 : stream_write_tree (ob, op->val[0], true);
5114 : 2418 : if (op->val[1])
5115 : 4 : stream_write_tree (ob, op->val[1], true);
5116 : : }
5117 : : }
5118 : : }
5119 : 102866 : streamer_write_uhwi (ob, info->size_time_table.length ());
5120 : 495422 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
5121 : : {
5122 : 289690 : streamer_write_uhwi (ob, e->size);
5123 : 289690 : e->time.stream_out (ob);
5124 : 289690 : e->exec_predicate.stream_out (ob);
5125 : 289690 : e->nonconst_predicate.stream_out (ob);
5126 : : }
5127 : 102866 : ipa_freqcounting_predicate *fcp;
5128 : 102866 : streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations));
5129 : 207649 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
5130 : : {
5131 : 1917 : fcp->predicate->stream_out (ob);
5132 : 1917 : fcp->freq.stream_out (ob);
5133 : : }
5134 : 102866 : streamer_write_uhwi (ob, vec_safe_length (info->loop_strides));
5135 : 206069 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
5136 : : {
5137 : 337 : fcp->predicate->stream_out (ob);
5138 : 337 : fcp->freq.stream_out (ob);
5139 : : }
5140 : 102866 : streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ());
5141 : 102866 : int ip;
5142 : 205746 : for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip);
5143 : : i++)
5144 : 14 : streamer_write_uhwi (ob, ip);
5145 : 476926 : for (edge = cnode->callees; edge; edge = edge->next_callee)
5146 : 374060 : write_ipa_call_summary (ob, edge);
5147 : 104758 : for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
5148 : 1892 : write_ipa_call_summary (ob, edge);
5149 : : }
5150 : : }
5151 : 24263 : streamer_write_char_stream (ob->main_stream, 0);
5152 : 24263 : produce_asm (ob);
5153 : 24263 : destroy_output_block (ob);
5154 : :
5155 : 24263 : ipa_prop_write_jump_functions ();
5156 : 24263 : }
5157 : :
5158 : :
5159 : : /* Release function summary. */
5160 : :
5161 : : void
5162 : 723963 : ipa_free_fn_summary (void)
5163 : : {
5164 : 723963 : if (!ipa_call_summaries)
5165 : : return;
5166 : 458128 : ggc_delete (ipa_fn_summaries);
5167 : 458128 : ipa_fn_summaries = NULL;
5168 : 458128 : delete ipa_call_summaries;
5169 : 458128 : ipa_call_summaries = NULL;
5170 : 458128 : edge_predicate_pool.release ();
5171 : : /* During IPA this is one of largest datastructures to release. */
5172 : 458128 : if (flag_wpa)
5173 : 8818 : ggc_trim ();
5174 : : }
5175 : :
5176 : : /* Release function summary. */
5177 : :
5178 : : void
5179 : 723963 : ipa_free_size_summary (void)
5180 : : {
5181 : 723963 : if (!ipa_size_summaries)
5182 : : return;
5183 : 458128 : delete ipa_size_summaries;
5184 : 458128 : ipa_size_summaries = NULL;
5185 : : }
5186 : :
5187 : : namespace {
5188 : :
5189 : : const pass_data pass_data_local_fn_summary =
5190 : : {
5191 : : GIMPLE_PASS, /* type */
5192 : : "local-fnsummary", /* name */
5193 : : OPTGROUP_INLINE, /* optinfo_flags */
5194 : : TV_INLINE_PARAMETERS, /* tv_id */
5195 : : 0, /* properties_required */
5196 : : 0, /* properties_provided */
5197 : : 0, /* properties_destroyed */
5198 : : 0, /* todo_flags_start */
5199 : : 0, /* todo_flags_finish */
5200 : : };
5201 : :
5202 : : class pass_local_fn_summary : public gimple_opt_pass
5203 : : {
5204 : : public:
5205 : 578604 : pass_local_fn_summary (gcc::context *ctxt)
5206 : 1157208 : : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
5207 : : {}
5208 : :
5209 : : /* opt_pass methods: */
5210 : 289302 : opt_pass * clone () final override
5211 : : {
5212 : 289302 : return new pass_local_fn_summary (m_ctxt);
5213 : : }
5214 : 5844467 : unsigned int execute (function *) final override
5215 : : {
5216 : 5844467 : return compute_fn_summary_for_current ();
5217 : : }
5218 : :
5219 : : }; // class pass_local_fn_summary
5220 : :
5221 : : } // anon namespace
5222 : :
5223 : : gimple_opt_pass *
5224 : 289302 : make_pass_local_fn_summary (gcc::context *ctxt)
5225 : : {
5226 : 289302 : return new pass_local_fn_summary (ctxt);
5227 : : }
5228 : :
5229 : :
5230 : : /* Free inline summary. */
5231 : :
5232 : : namespace {
5233 : :
5234 : : const pass_data pass_data_ipa_free_fn_summary =
5235 : : {
5236 : : SIMPLE_IPA_PASS, /* type */
5237 : : "free-fnsummary", /* name */
5238 : : OPTGROUP_NONE, /* optinfo_flags */
5239 : : TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
5240 : : 0, /* properties_required */
5241 : : 0, /* properties_provided */
5242 : : 0, /* properties_destroyed */
5243 : : 0, /* todo_flags_start */
5244 : : 0, /* todo_flags_finish */
5245 : : };
5246 : :
5247 : : class pass_ipa_free_fn_summary : public simple_ipa_opt_pass
5248 : : {
5249 : : public:
5250 : 578604 : pass_ipa_free_fn_summary (gcc::context *ctxt)
5251 : 578604 : : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
5252 : 1157208 : small_p (false)
5253 : : {}
5254 : :
5255 : : /* opt_pass methods: */
5256 : 289302 : opt_pass *clone () final override
5257 : : {
5258 : 289302 : return new pass_ipa_free_fn_summary (m_ctxt);
5259 : : }
5260 : 578604 : void set_pass_param (unsigned int n, bool param) final override
5261 : : {
5262 : 578604 : gcc_assert (n == 0);
5263 : 578604 : small_p = param;
5264 : 578604 : }
5265 : 485173 : bool gate (function *) final override { return true; }
5266 : 462790 : unsigned int execute (function *) final override
5267 : : {
5268 : 462790 : ipa_free_fn_summary ();
5269 : : /* Free ipa-prop structures if they are no longer needed. */
5270 : 462790 : ipa_free_all_structures_after_iinln ();
5271 : 462790 : if (!flag_wpa)
5272 : 453972 : ipa_free_size_summary ();
5273 : 462790 : return 0;
5274 : : }
5275 : :
5276 : : private:
5277 : : bool small_p;
5278 : : }; // class pass_ipa_free_fn_summary
5279 : :
5280 : : } // anon namespace
5281 : :
5282 : : simple_ipa_opt_pass *
5283 : 289302 : make_pass_ipa_free_fn_summary (gcc::context *ctxt)
5284 : : {
5285 : 289302 : return new pass_ipa_free_fn_summary (ctxt);
5286 : : }
5287 : :
5288 : : namespace {
5289 : :
5290 : : const pass_data pass_data_ipa_fn_summary =
5291 : : {
5292 : : IPA_PASS, /* type */
5293 : : "fnsummary", /* name */
5294 : : OPTGROUP_INLINE, /* optinfo_flags */
5295 : : TV_IPA_FNSUMMARY, /* tv_id */
5296 : : 0, /* properties_required */
5297 : : 0, /* properties_provided */
5298 : : 0, /* properties_destroyed */
5299 : : 0, /* todo_flags_start */
5300 : : ( TODO_dump_symtab ), /* todo_flags_finish */
5301 : : };
5302 : :
5303 : : class pass_ipa_fn_summary : public ipa_opt_pass_d
5304 : : {
5305 : : public:
5306 : 289302 : pass_ipa_fn_summary (gcc::context *ctxt)
5307 : : : ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt,
5308 : : ipa_fn_summary_generate, /* generate_summary */
5309 : : ipa_fn_summary_write, /* write_summary */
5310 : : ipa_fn_summary_read, /* read_summary */
5311 : : NULL, /* write_optimization_summary */
5312 : : NULL, /* read_optimization_summary */
5313 : : NULL, /* stmt_fixup */
5314 : : 0, /* function_transform_todo_flags_start */
5315 : : NULL, /* function_transform */
5316 : 289302 : NULL) /* variable_transform */
5317 : 289302 : {}
5318 : :
5319 : : /* opt_pass methods: */
5320 : 231204 : unsigned int execute (function *) final override { return 0; }
5321 : :
5322 : : }; // class pass_ipa_fn_summary
5323 : :
5324 : : } // anon namespace
5325 : :
5326 : : ipa_opt_pass_d *
5327 : 289302 : make_pass_ipa_fn_summary (gcc::context *ctxt)
5328 : : {
5329 : 289302 : return new pass_ipa_fn_summary (ctxt);
5330 : : }
5331 : :
5332 : : /* Reset all state within ipa-fnsummary.cc so that we can rerun the compiler
5333 : : within the same process. For use by toplev::finalize. */
5334 : :
5335 : : void
5336 : 260333 : ipa_fnsummary_cc_finalize (void)
5337 : : {
5338 : 260333 : ipa_free_fn_summary ();
5339 : 260333 : ipa_free_size_summary ();
5340 : 260333 : }
|