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 : 164 : ipa_dump_hints (FILE *f, ipa_hints hints)
106 : : {
107 : 164 : if (!hints)
108 : : return;
109 : 127 : fprintf (f, "IPA hints:");
110 : 127 : if (hints & INLINE_HINT_indirect_call)
111 : : {
112 : 23 : hints &= ~INLINE_HINT_indirect_call;
113 : 23 : fprintf (f, " indirect_call");
114 : : }
115 : 127 : if (hints & INLINE_HINT_loop_iterations)
116 : : {
117 : 3 : hints &= ~INLINE_HINT_loop_iterations;
118 : 3 : fprintf (f, " loop_iterations");
119 : : }
120 : 127 : if (hints & INLINE_HINT_loop_stride)
121 : : {
122 : 3 : hints &= ~INLINE_HINT_loop_stride;
123 : 3 : fprintf (f, " loop_stride");
124 : : }
125 : 127 : if (hints & INLINE_HINT_same_scc)
126 : : {
127 : 4 : hints &= ~INLINE_HINT_same_scc;
128 : 4 : fprintf (f, " same_scc");
129 : : }
130 : 127 : if (hints & INLINE_HINT_in_scc)
131 : : {
132 : 11 : hints &= ~INLINE_HINT_in_scc;
133 : 11 : fprintf (f, " in_scc");
134 : : }
135 : 127 : if (hints & INLINE_HINT_cross_module)
136 : : {
137 : 2 : hints &= ~INLINE_HINT_cross_module;
138 : 2 : fprintf (f, " cross_module");
139 : : }
140 : 127 : if (hints & INLINE_HINT_declared_inline)
141 : : {
142 : 101 : hints &= ~INLINE_HINT_declared_inline;
143 : 101 : fprintf (f, " declared_inline");
144 : : }
145 : 127 : if (hints & INLINE_HINT_known_hot)
146 : : {
147 : 1 : hints &= ~INLINE_HINT_known_hot;
148 : 1 : fprintf (f, " known_hot");
149 : : }
150 : 127 : 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 : 127 : 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 : 134417225 : 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 : 134417225 : size_time_entry *e;
173 : 134417225 : bool found = false;
174 : 134417225 : int i;
175 : 134417225 : ipa_predicate nonconst_pred;
176 : 134417225 : vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table;
177 : :
178 : 134417225 : if (exec_pred == false)
179 : 4470811 : return;
180 : :
181 : 134207516 : nonconst_pred = nonconst_pred_in & exec_pred;
182 : :
183 : 134207516 : 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 : 157553324 : if (!size && time == 0 && table->length ())
189 : 3554116 : return;
190 : :
191 : : /* Only for calls we are unaccounting what we previously recorded. */
192 : 129946414 : gcc_checking_assert (time >= 0 || call);
193 : :
194 : 466397491 : for (i = 0; table->iterate (i, &e); i++)
195 : 436877224 : if (e->exec_predicate == exec_pred
196 : 436877224 : && e->nonconst_predicate == nonconst_pred)
197 : : {
198 : : found = true;
199 : : break;
200 : : }
201 : 129946414 : 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 : 129950734 : if (dump_file && (dump_flags & TDF_DETAILS) && (time != 0 || size))
212 : : {
213 : 4014 : fprintf (dump_file,
214 : : "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate exec:",
215 : 3787 : ((double) size) / ipa_fn_summary::size_scale,
216 : : (time.to_double ()), found ? "" : "new ");
217 : 3787 : exec_pred.dump (dump_file, conds, 0);
218 : 3787 : if (exec_pred != nonconst_pred)
219 : : {
220 : 81 : fprintf (dump_file, " nonconst:");
221 : 81 : nonconst_pred.dump (dump_file, conds);
222 : : }
223 : : else
224 : 3706 : fprintf (dump_file, "\n");
225 : : }
226 : 129946414 : if (!found)
227 : : {
228 : 29520267 : class size_time_entry new_entry;
229 : 29520267 : new_entry.size = size;
230 : 29520267 : new_entry.time = time;
231 : 29520267 : new_entry.exec_predicate = exec_pred;
232 : 29520267 : new_entry.nonconst_predicate = nonconst_pred;
233 : 29520267 : if (call)
234 : 1761213 : call_size_time_table.safe_push (new_entry);
235 : : else
236 : 27759054 : size_time_table.safe_push (new_entry);
237 : : }
238 : : else
239 : : {
240 : 100426147 : e->size += size;
241 : 100426147 : e->time += time;
242 : : /* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */
243 : : /* Tolerate small roundoff issues. */
244 : 100426147 : if (e->time < 0)
245 : 68 : 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 : 225787 : redirect_to_unreachable (struct cgraph_edge *e)
253 : : {
254 : 225787 : struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
255 : 225787 : struct cgraph_node *target
256 : 225787 : = cgraph_node::get_create (builtin_decl_unreachable ());
257 : :
258 : 225787 : gcc_checking_assert (lookup_attribute ("cold",
259 : : DECL_ATTRIBUTES (target->decl)));
260 : :
261 : 225787 : if (e->speculative)
262 : 230 : e = cgraph_edge::resolve_speculation (e, target->decl);
263 : 225557 : else if (!e->callee)
264 : 771 : e = cgraph_edge::make_direct (e, target);
265 : : else
266 : 224786 : e->redirect_callee (target);
267 : 225787 : class ipa_call_summary *es = ipa_call_summaries->get (e);
268 : 225787 : e->inline_failed = CIF_UNREACHABLE;
269 : 225787 : e->count = profile_count::zero ();
270 : 225787 : es->call_stmt_size = 0;
271 : 225787 : es->call_stmt_time = 0;
272 : 225787 : if (callee)
273 : 0 : callee->remove_symbol_and_inline_clones ();
274 : 225787 : return e;
275 : : }
276 : :
277 : : /* Set predicate for edge E. */
278 : :
279 : : static void
280 : 29853256 : edge_set_predicate (struct cgraph_edge *e, ipa_predicate *predicate)
281 : : {
282 : : /* If the edge is determined to be never executed, redirect it
283 : : to BUILTIN_UNREACHABLE to make it clear to IPA passes the call will
284 : : be optimized out. */
285 : 27340691 : if (predicate && *predicate == false
286 : : /* When handling speculative edges, we need to do the redirection
287 : : just once. Do it always on the direct edge, so we do not
288 : : attempt to resolve speculation while duplicating the edge. */
289 : 30079098 : && (!e->speculative || e->callee))
290 : 225787 : e = redirect_to_unreachable (e);
291 : :
292 : 29853256 : class ipa_call_summary *es = ipa_call_summaries->get (e);
293 : 57193947 : if (predicate && *predicate != true)
294 : : {
295 : 4317506 : if (!es->predicate)
296 : 3939494 : es->predicate = edge_predicate_pool.allocate ();
297 : 4317506 : *es->predicate = *predicate;
298 : : }
299 : : else
300 : : {
301 : 25535750 : if (es->predicate)
302 : 656702 : edge_predicate_pool.remove (es->predicate);
303 : 25535750 : es->predicate = NULL;
304 : : }
305 : 29853256 : }
306 : :
307 : : /* Set predicate for hint *P. */
308 : :
309 : : static void
310 : 163133 : set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate)
311 : : {
312 : 163133 : if (new_predicate == false || new_predicate == true)
313 : : {
314 : 2262 : if (*p)
315 : 0 : edge_predicate_pool.remove (*p);
316 : 2262 : *p = NULL;
317 : : }
318 : : else
319 : : {
320 : 160871 : if (!*p)
321 : 160871 : *p = edge_predicate_pool.allocate ();
322 : 160871 : **p = new_predicate;
323 : : }
324 : 163133 : }
325 : :
326 : : /* Find if NEW_PREDICATE is already in V and if so, increment its freq.
327 : : Otherwise add a new item to the vector with this predicate and frerq equal
328 : : to add_freq, unless the number of predicates would exceed MAX_NUM_PREDICATES
329 : : in which case the function does nothing. */
330 : :
331 : : static void
332 : 1093927 : add_freqcounting_predicate (vec<ipa_freqcounting_predicate, va_gc> **v,
333 : : const ipa_predicate &new_predicate, sreal add_freq,
334 : : unsigned max_num_predicates)
335 : : {
336 : 1093927 : if (new_predicate == false || new_predicate == true)
337 : : return;
338 : : ipa_freqcounting_predicate *f;
339 : 138042 : for (int i = 0; vec_safe_iterate (*v, i, &f); i++)
340 : 70801 : if (new_predicate == f->predicate)
341 : : {
342 : 0 : f->freq += add_freq;
343 : 0 : return;
344 : : }
345 : 92590 : if (vec_safe_length (*v) >= max_num_predicates)
346 : : /* Too many different predicates to account for. */
347 : : return;
348 : :
349 : 66969 : ipa_freqcounting_predicate fcp;
350 : 66969 : fcp.predicate = NULL;
351 : 66969 : set_hint_predicate (&fcp.predicate, new_predicate);
352 : 66969 : fcp.freq = add_freq;
353 : 66969 : vec_safe_push (*v, fcp);
354 : 66969 : return;
355 : : }
356 : :
357 : : /* Compute what conditions may or may not hold given information about
358 : : parameters. RET_CLAUSE returns truths that may hold in a specialized copy,
359 : : while RET_NONSPEC_CLAUSE returns truths that may hold in an nonspecialized
360 : : copy when called in a given context. It is a bitmask of conditions. Bit
361 : : 0 means that condition is known to be false, while bit 1 means that condition
362 : : may or may not be true. These differs - for example NOT_INLINED condition
363 : : is always false in the second and also builtin_constant_p tests cannot use
364 : : the fact that parameter is indeed a constant.
365 : :
366 : : When INLINE_P is true, assume that we are inlining. AVAL contains known
367 : : information about argument values. The function does not modify its content
368 : : and so AVALs could also be of type ipa_call_arg_values but so far all
369 : : callers work with the auto version and so we avoid the conversion for
370 : : convenience.
371 : :
372 : : ERROR_MARK value of an argument means compile time invariant. */
373 : :
374 : : static void
375 : 21897929 : evaluate_conditions_for_known_args (struct cgraph_node *node,
376 : : bool inline_p,
377 : : ipa_auto_call_arg_values *avals,
378 : : clause_t *ret_clause,
379 : : clause_t *ret_nonspec_clause,
380 : : ipa_call_summary *es)
381 : : {
382 : 21897929 : clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
383 : 21897929 : clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition;
384 : 21897929 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
385 : 21897929 : int i;
386 : 21897929 : struct condition *c;
387 : :
388 : 88144611 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
389 : : {
390 : 66246682 : tree val = NULL;
391 : 66246682 : tree res;
392 : 66246682 : int j;
393 : 66246682 : struct expr_eval_op *op;
394 : :
395 : 66246682 : if (c->code == ipa_predicate::not_sra_candidate)
396 : : {
397 : 15980840 : if (!inline_p
398 : 15980840 : || !es
399 : 15420253 : || (int)es->param.length () <= c->operand_num
400 : 23690939 : || !es->param[c->operand_num].points_to_possible_sra_candidate)
401 : 12036947 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
402 : 15980840 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
403 : 66246682 : continue;
404 : : }
405 : :
406 : 50265842 : if (c->agg_contents)
407 : : {
408 : 26708021 : if (c->code == ipa_predicate::changed
409 : 17608709 : && !c->by_ref
410 : 29278117 : && (avals->safe_sval_at(c->operand_num) == error_mark_node))
411 : 5096 : continue;
412 : :
413 : 26697829 : if (tree sval = avals->safe_sval_at (c->operand_num))
414 : 12836699 : val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref);
415 : 12836699 : if (!val)
416 : : {
417 : 26683277 : ipa_argagg_value_list avs (avals);
418 : 26683277 : val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
419 : 26683277 : c->by_ref);
420 : : }
421 : : }
422 : : else
423 : : {
424 : 23562917 : val = avals->safe_sval_at (c->operand_num);
425 : 23562917 : if (val && val == error_mark_node
426 : 2157993 : && c->code != ipa_predicate::changed)
427 : : val = NULL_TREE;
428 : : }
429 : :
430 : 38203770 : if (!val
431 : 37447826 : && (c->code == ipa_predicate::changed
432 : : || c->code == ipa_predicate::is_not_constant))
433 : : {
434 : 24161810 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
435 : 24161810 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
436 : 24161810 : continue;
437 : : }
438 : 26098936 : if (c->code == ipa_predicate::changed)
439 : : {
440 : 7106874 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
441 : 7106874 : continue;
442 : : }
443 : :
444 : 18992062 : if (c->code == ipa_predicate::is_not_constant)
445 : : {
446 : 5243 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
447 : 5243 : continue;
448 : : }
449 : :
450 : 18986819 : if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val)))
451 : : {
452 : 5670553 : if (c->type != TREE_TYPE (val))
453 : 990306 : val = fold_unary (VIEW_CONVERT_EXPR, c->type, val);
454 : 5990461 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
455 : : {
456 : 320341 : if (!val)
457 : : break;
458 : 319908 : if (!op->val[0])
459 : 110792 : val = fold_unary (op->code, op->type, val);
460 : 209116 : else if (!op->val[1])
461 : 418232 : val = fold_binary (op->code, op->type,
462 : : op->index ? op->val[0] : val,
463 : : op->index ? val : op->val[0]);
464 : 0 : else if (op->index == 0)
465 : 0 : val = fold_ternary (op->code, op->type,
466 : : val, op->val[0], op->val[1]);
467 : 0 : else if (op->index == 1)
468 : 0 : val = fold_ternary (op->code, op->type,
469 : : op->val[0], val, op->val[1]);
470 : 0 : else if (op->index == 2)
471 : 0 : val = fold_ternary (op->code, op->type,
472 : : op->val[0], op->val[1], val);
473 : : else
474 : : val = NULL_TREE;
475 : : }
476 : :
477 : 5670553 : res = val
478 : 5670553 : ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
479 : : : NULL;
480 : :
481 : 5670100 : if (res && integer_zerop (res))
482 : 2915106 : continue;
483 : 2755447 : if (res && integer_onep (res))
484 : : {
485 : 2733048 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
486 : 2733048 : nonspec_clause
487 : 2733048 : |= 1 << (i + ipa_predicate::first_dynamic_condition);
488 : 2733048 : continue;
489 : : }
490 : : }
491 : 13338665 : if (c->operand_num < (int) avals->m_known_value_ranges.length ()
492 : 7624873 : && !c->agg_contents
493 : 15402029 : && (!val || TREE_CODE (val) != INTEGER_CST))
494 : : {
495 : 2063065 : value_range vr (avals->m_known_value_ranges[c->operand_num]);
496 : 2063065 : if (!vr.undefined_p ()
497 : 1149643 : && !vr.varying_p ()
498 : 3212708 : && (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ())))
499 : : {
500 : 1148873 : if (!useless_type_conversion_p (c->type, vr.type ()))
501 : 1475 : range_cast (vr, c->type);
502 : :
503 : 1453578 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
504 : : {
505 : 341629 : if (vr.varying_p () || vr.undefined_p ())
506 : : break;
507 : :
508 : 304705 : value_range res (op->type);
509 : 304705 : if (!op->val[0])
510 : : {
511 : 119304 : value_range varying (op->type);
512 : 119304 : varying.set_varying (op->type);
513 : 119304 : range_op_handler handler (op->code);
514 : 119304 : if (!handler
515 : 119304 : || !res.supports_type_p (op->type)
516 : 238608 : || !handler.fold_range (res, op->type, vr, varying))
517 : 0 : res.set_varying (op->type);
518 : 119304 : }
519 : 185401 : else if (!op->val[1])
520 : : {
521 : 185229 : value_range op0 (TREE_TYPE (op->val[0]));
522 : 185229 : range_op_handler handler (op->code);
523 : :
524 : 185229 : ipa_get_range_from_ip_invariant (op0, op->val[0], node);
525 : :
526 : 185229 : if (!handler
527 : 185229 : || !res.supports_type_p (op->type)
528 : 370458 : || !handler.fold_range (res, op->type,
529 : 185229 : op->index ? op0 : vr,
530 : 369922 : op->index ? vr : op0))
531 : 0 : res.set_varying (op->type);
532 : 185229 : }
533 : : else
534 : 172 : res.set_varying (op->type);
535 : 304705 : vr = res;
536 : 304705 : }
537 : 1148873 : if (!vr.varying_p () && !vr.undefined_p ())
538 : : {
539 : 1097628 : int_range<2> res;
540 : 1097628 : value_range val_vr (TREE_TYPE (c->val));
541 : 1097628 : range_op_handler handler (c->code);
542 : :
543 : 1097628 : ipa_get_range_from_ip_invariant (val_vr, c->val, node);
544 : :
545 : 1097628 : if (!handler
546 : 1097628 : || !val_vr.supports_type_p (TREE_TYPE (c->val))
547 : 2195256 : || !handler.fold_range (res, boolean_type_node, vr, val_vr))
548 : 0 : res.set_varying (boolean_type_node);
549 : :
550 : 1097628 : if (res.zero_p ())
551 : 198317 : continue;
552 : 1097628 : }
553 : : }
554 : 2063065 : }
555 : :
556 : 13140348 : clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
557 : 13140348 : nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
558 : : }
559 : 21897929 : *ret_clause = clause;
560 : 21897929 : if (ret_nonspec_clause)
561 : 18991843 : *ret_nonspec_clause = nonspec_clause;
562 : 21897929 : }
563 : :
564 : : /* Return true if VRP will be exectued on the function.
565 : : We do not want to anticipate optimizations that will not happen.
566 : :
567 : : FIXME: This can be confused with -fdisable and debug counters and thus
568 : : it should not be used for correctness (only to make heuristics work).
569 : : This means that inliner should do its own optimizations of expressions
570 : : that it predicts to be constant so wrong code can not be triggered by
571 : : builtin_constant_p. */
572 : :
573 : : static bool
574 : 11711059 : vrp_will_run_p (struct cgraph_node *node)
575 : : {
576 : 11711059 : return (opt_for_fn (node->decl, optimize)
577 : 11711059 : && !opt_for_fn (node->decl, optimize_debug)
578 : 23420862 : && opt_for_fn (node->decl, flag_tree_vrp));
579 : : }
580 : :
581 : : /* Similarly about FRE. */
582 : :
583 : : static bool
584 : 14023821 : fre_will_run_p (struct cgraph_node *node)
585 : : {
586 : 14023821 : return (opt_for_fn (node->decl, optimize)
587 : 14023821 : && !opt_for_fn (node->decl, optimize_debug)
588 : 28045320 : && opt_for_fn (node->decl, flag_tree_fre));
589 : : }
590 : :
591 : : /* Work out what conditions might be true at invocation of E.
592 : : Compute costs for inlined edge if INLINE_P is true.
593 : :
594 : : Return in CLAUSE_PTR the evaluated conditions and in NONSPEC_CLAUSE_PTR
595 : : (if non-NULL) conditions evaluated for nonspecialized clone called
596 : : in a given context.
597 : :
598 : : Vectors in AVALS will be populated with useful known information about
599 : : argument values - information not known to have any uses will be omitted -
600 : : except for m_known_contexts which will only be calculated if
601 : : COMPUTE_CONTEXTS is true. */
602 : :
603 : : void
604 : 21714436 : evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
605 : : clause_t *clause_ptr,
606 : : clause_t *nonspec_clause_ptr,
607 : : ipa_auto_call_arg_values *avals,
608 : : bool compute_contexts)
609 : : {
610 : 21714436 : struct cgraph_node *callee = e->callee->ultimate_alias_target ();
611 : 21714436 : class ipa_fn_summary *info = ipa_fn_summaries->get (callee);
612 : 21714436 : class ipa_edge_args *args;
613 : 21714436 : class ipa_call_summary *es = NULL;
614 : :
615 : 21714436 : if (clause_ptr)
616 : 21714436 : *clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition;
617 : :
618 : 21714436 : if (ipa_node_params_sum
619 : 9709583 : && !e->call_stmt_cannot_inline_p
620 : 9709583 : && (info->conds || compute_contexts)
621 : 31424019 : && (args = ipa_edge_args_sum->get (e)) != NULL)
622 : : {
623 : 9655566 : struct cgraph_node *caller;
624 : 9655566 : class ipa_node_params *caller_parms_info, *callee_pi = NULL;
625 : 9655566 : int i, count = ipa_get_cs_argument_count (args);
626 : 9655566 : es = ipa_call_summaries->get (e);
627 : :
628 : 9655566 : if (count)
629 : : {
630 : 8975359 : if (e->caller->inlined_to)
631 : : caller = e->caller->inlined_to;
632 : : else
633 : 7293719 : caller = e->caller;
634 : 8975359 : caller_parms_info = ipa_node_params_sum->get (caller);
635 : 8975359 : callee_pi = ipa_node_params_sum->get (callee);
636 : :
637 : : /* Watch for thunks. */
638 : 8975359 : if (callee_pi)
639 : : /* Watch for variadic functions. */
640 : 8975070 : count = MIN (count, ipa_get_param_count (callee_pi));
641 : : }
642 : :
643 : 8975070 : if (callee_pi)
644 : 29911362 : for (i = 0; i < count; i++)
645 : : {
646 : 20936292 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
647 : :
648 : 20936292 : if (ipa_is_param_used_by_indirect_call (callee_pi, i)
649 : 20936292 : || ipa_is_param_used_by_ipa_predicates (callee_pi, i))
650 : : {
651 : : /* Determine if we know constant value of the parameter. */
652 : 14023821 : tree type = ipa_get_type (callee_pi, i);
653 : 14023821 : tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type);
654 : :
655 : 11026814 : if (!cst && e->call_stmt
656 : 25011287 : && i < (int)gimple_call_num_args (e->call_stmt))
657 : : {
658 : 10987466 : cst = gimple_call_arg (e->call_stmt, i);
659 : 10987466 : if (!is_gimple_min_invariant (cst))
660 : : cst = NULL;
661 : : }
662 : 3212648 : if (cst)
663 : : {
664 : 3173300 : gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
665 : 3173300 : if (!avals->m_known_vals.length ())
666 : 1683256 : avals->m_known_vals.safe_grow_cleared (count, true);
667 : 3173300 : avals->m_known_vals[i] = cst;
668 : : }
669 : 10850521 : else if (inline_p && !es->param[i].change_prob)
670 : : {
671 : 4009362 : if (!avals->m_known_vals.length ())
672 : 3323380 : avals->m_known_vals.safe_grow_cleared (count, true);
673 : 4009362 : avals->m_known_vals[i] = error_mark_node;
674 : : }
675 : :
676 : : /* If we failed to get simple constant, try value range. */
677 : 3173300 : if ((!cst || TREE_CODE (cst) != INTEGER_CST)
678 : 11711059 : && vrp_will_run_p (caller)
679 : 25592483 : && ipa_is_param_used_by_ipa_predicates (callee_pi, i))
680 : : {
681 : 11532678 : value_range vr (type);
682 : :
683 : 11532678 : ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type);
684 : 11532678 : if (!vr.undefined_p () && !vr.varying_p ())
685 : : {
686 : 8027859 : if (!avals->m_known_value_ranges.length ())
687 : : {
688 : 5770036 : avals->m_known_value_ranges.safe_grow_cleared (count,
689 : : true);
690 : 18670610 : for (int i = 0; i < count; ++i)
691 : 12900574 : avals->m_known_value_ranges[i].set_type (void_type_node);
692 : : }
693 : 8027859 : avals->m_known_value_ranges[i] = vr;
694 : : }
695 : 11532678 : }
696 : :
697 : : /* Determine known aggregate values. */
698 : 14023821 : if (fre_will_run_p (caller))
699 : 14021084 : ipa_push_agg_values_from_jfunc (caller_parms_info,
700 : : caller, &jf->agg, i,
701 : : &avals->m_known_aggs);
702 : : }
703 : :
704 : : /* For calls used in polymorphic calls we further determine
705 : : polymorphic call context. */
706 : 20936292 : if (compute_contexts
707 : 20936292 : && ipa_is_param_used_by_polymorphic_call (callee_pi, i))
708 : : {
709 : 100294 : ipa_polymorphic_call_context
710 : 100294 : ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
711 : 200588 : if (!ctx.useless_p ())
712 : : {
713 : 99045 : if (!avals->m_known_contexts.length ())
714 : 98908 : avals->m_known_contexts.safe_grow_cleared (count, true);
715 : 99045 : avals->m_known_contexts[i]
716 : 198090 : = ipa_context_from_jfunc (caller_parms_info, e, i, jf);
717 : : }
718 : : }
719 : : }
720 : : else
721 : 680496 : gcc_assert (!count || callee->thunk);
722 : : }
723 : 12058870 : else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds)
724 : : {
725 : 9478851 : int i, count = (int)gimple_call_num_args (e->call_stmt);
726 : :
727 : 26808687 : for (i = 0; i < count; i++)
728 : : {
729 : 17329836 : tree cst = gimple_call_arg (e->call_stmt, i);
730 : 17329836 : if (!is_gimple_min_invariant (cst))
731 : : cst = NULL;
732 : 6379265 : if (cst)
733 : : {
734 : 6379265 : if (!avals->m_known_vals.length ())
735 : 4465096 : avals->m_known_vals.safe_grow_cleared (count, true);
736 : 6379265 : avals->m_known_vals[i] = cst;
737 : : }
738 : : }
739 : : }
740 : :
741 : 21714436 : evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr,
742 : : nonspec_clause_ptr, es);
743 : 21714436 : }
744 : :
745 : :
746 : : /* Allocate the function summary. */
747 : :
748 : : static void
749 : 452304 : ipa_fn_summary_alloc (void)
750 : : {
751 : 452304 : gcc_checking_assert (!ipa_fn_summaries);
752 : 452304 : ipa_size_summaries = new ipa_size_summary_t (symtab);
753 : 452304 : ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab);
754 : 452304 : ipa_call_summaries = new ipa_call_summary_t (symtab);
755 : 452304 : }
756 : :
757 : 26476758 : ipa_call_summary::~ipa_call_summary ()
758 : : {
759 : 26476758 : if (predicate)
760 : 3282792 : edge_predicate_pool.remove (predicate);
761 : :
762 : 26476758 : param.release ();
763 : 26476758 : }
764 : :
765 : 9919783 : ipa_fn_summary::~ipa_fn_summary ()
766 : : {
767 : 9919783 : unsigned len = vec_safe_length (loop_iterations);
768 : 10037923 : for (unsigned i = 0; i < len; i++)
769 : 118140 : edge_predicate_pool.remove ((*loop_iterations)[i].predicate);
770 : 9919783 : len = vec_safe_length (loop_strides);
771 : 9962514 : for (unsigned i = 0; i < len; i++)
772 : 42731 : edge_predicate_pool.remove ((*loop_strides)[i].predicate);
773 : 9919783 : vec_free (conds);
774 : 9919783 : call_size_time_table.release ();
775 : 9919783 : vec_free (loop_iterations);
776 : 9919783 : vec_free (loop_strides);
777 : 9919783 : builtin_constant_p_parms.release ();
778 : 9919783 : }
779 : :
780 : : void
781 : 7174334 : ipa_fn_summary_t::remove_callees (cgraph_node *node)
782 : : {
783 : 7174334 : cgraph_edge *e;
784 : 29378983 : for (e = node->callees; e; e = e->next_callee)
785 : 22204649 : ipa_call_summaries->remove (e);
786 : 7655182 : for (e = node->indirect_calls; e; e = e->next_callee)
787 : 480848 : ipa_call_summaries->remove (e);
788 : 7174334 : }
789 : :
790 : : /* Duplicate predicates in loop hint vector, allocating memory for them and
791 : : remove and deallocate any uninteresting (true or false) ones. Return the
792 : : result. */
793 : :
794 : : static vec<ipa_freqcounting_predicate, va_gc> *
795 : 24452 : remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v,
796 : : clause_t possible_truths)
797 : : {
798 : 24452 : if (vec_safe_length (v) == 0)
799 : : return NULL;
800 : :
801 : 3659 : vec<ipa_freqcounting_predicate, va_gc> *res = v->copy ();
802 : 3659 : int len = res->length();
803 : 8817 : for (int i = len - 1; i >= 0; i--)
804 : : {
805 : 5158 : ipa_predicate new_predicate
806 : 5158 : = (*res)[i].predicate->remap_after_duplication (possible_truths);
807 : : /* We do not want to free previous predicate; it is used by node
808 : : origin. */
809 : 5158 : (*res)[i].predicate = NULL;
810 : 5158 : set_hint_predicate (&(*res)[i].predicate, new_predicate);
811 : :
812 : 5158 : if (!(*res)[i].predicate)
813 : 2262 : res->unordered_remove (i);
814 : : }
815 : :
816 : : return res;
817 : : }
818 : :
819 : :
820 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
821 : : void
822 : 2659193 : ipa_fn_summary_t::duplicate (cgraph_node *src,
823 : : cgraph_node *dst,
824 : : ipa_fn_summary *src_info,
825 : : ipa_fn_summary *info)
826 : : {
827 : 2659193 : new (info) ipa_fn_summary (*src_info);
828 : : /* TODO: as an optimization, we may avoid copying conditions
829 : : that are known to be false or true. */
830 : 2659193 : info->conds = vec_safe_copy (info->conds);
831 : :
832 : 2659193 : clone_info *cinfo = clone_info::get (dst);
833 : : /* When there are any replacements in the function body, see if we can figure
834 : : out that something was optimized out. */
835 : 2659193 : if (ipa_node_params_sum && cinfo && cinfo->tree_map)
836 : : {
837 : : /* Use SRC parm info since it may not be copied yet. */
838 : 12226 : ipa_node_params *parms_info = ipa_node_params_sum->get (src);
839 : 12226 : ipa_auto_call_arg_values avals;
840 : 12226 : int count = ipa_get_param_count (parms_info);
841 : 12226 : int i, j;
842 : 12226 : clause_t possible_truths;
843 : 12226 : ipa_predicate true_pred = true;
844 : 12226 : size_time_entry *e;
845 : 12226 : int optimized_out_size = 0;
846 : 12226 : bool inlined_to_p = false;
847 : 12226 : struct cgraph_edge *edge, *next;
848 : :
849 : 12226 : info->size_time_table.release ();
850 : 12226 : avals.m_known_vals.safe_grow_cleared (count, true);
851 : 49244 : for (i = 0; i < count; i++)
852 : : {
853 : : struct ipa_replace_map *r;
854 : :
855 : 123348 : for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++)
856 : : {
857 : 69977 : if (r->parm_num == i)
858 : : {
859 : 20665 : avals.m_known_vals[i] = r->new_tree;
860 : 20665 : break;
861 : : }
862 : : }
863 : : }
864 : 12226 : evaluate_conditions_for_known_args (dst, false,
865 : : &avals,
866 : : &possible_truths,
867 : : /* We are going to specialize,
868 : : so ignore nonspec truths. */
869 : : NULL,
870 : : NULL);
871 : :
872 : 12226 : info->account_size_time (0, 0, true_pred, true_pred);
873 : :
874 : : /* Remap size_time vectors.
875 : : Simplify the predicate by pruning out alternatives that are known
876 : : to be false.
877 : : TODO: as on optimization, we can also eliminate conditions known
878 : : to be true. */
879 : 91054 : for (i = 0; src_info->size_time_table.iterate (i, &e); i++)
880 : : {
881 : 78828 : ipa_predicate new_exec_pred;
882 : 78828 : ipa_predicate new_nonconst_pred;
883 : 78828 : new_exec_pred = e->exec_predicate.remap_after_duplication
884 : 78828 : (possible_truths);
885 : 78828 : new_nonconst_pred = e->nonconst_predicate.remap_after_duplication
886 : 78828 : (possible_truths);
887 : 78828 : if (new_exec_pred == false || new_nonconst_pred == false)
888 : 9665 : optimized_out_size += e->size;
889 : : else
890 : 69163 : info->account_size_time (e->size, e->time, new_exec_pred,
891 : : new_nonconst_pred);
892 : : }
893 : :
894 : : /* Remap edge predicates with the same simplification as above.
895 : : Also copy constantness arrays. */
896 : 182327 : for (edge = dst->callees; edge; edge = next)
897 : : {
898 : 170101 : ipa_predicate new_predicate;
899 : 170101 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
900 : 170101 : next = edge->next_callee;
901 : :
902 : 170101 : if (!edge->inline_failed)
903 : 0 : inlined_to_p = true;
904 : 170101 : if (!es->predicate)
905 : 156724 : continue;
906 : 13377 : new_predicate = es->predicate->remap_after_duplication
907 : 13377 : (possible_truths);
908 : 13377 : if (new_predicate == false && *es->predicate != false)
909 : 2543 : optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale;
910 : 13377 : edge_set_predicate (edge, &new_predicate);
911 : : }
912 : :
913 : : /* Remap indirect edge predicates with the same simplification as above.
914 : : Also copy constantness arrays. */
915 : 13101 : for (edge = dst->indirect_calls; edge; edge = next)
916 : : {
917 : 875 : ipa_predicate new_predicate;
918 : 875 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
919 : 875 : next = edge->next_callee;
920 : :
921 : 875 : gcc_checking_assert (edge->inline_failed);
922 : 875 : if (!es->predicate)
923 : 746 : continue;
924 : 129 : new_predicate = es->predicate->remap_after_duplication
925 : 129 : (possible_truths);
926 : 129 : if (new_predicate == false && *es->predicate != false)
927 : 24 : optimized_out_size
928 : 24 : += es->call_stmt_size * ipa_fn_summary::size_scale;
929 : 129 : edge_set_predicate (edge, &new_predicate);
930 : : }
931 : 12226 : info->loop_iterations
932 : 12226 : = remap_freqcounting_preds_after_dup (info->loop_iterations,
933 : : possible_truths);
934 : 12226 : info->loop_strides
935 : 12226 : = remap_freqcounting_preds_after_dup (info->loop_strides,
936 : : possible_truths);
937 : 12226 : if (info->builtin_constant_p_parms.length())
938 : : {
939 : 25 : vec <int, va_heap, vl_ptr> parms = info->builtin_constant_p_parms;
940 : 25 : int ip;
941 : 25 : info->builtin_constant_p_parms = vNULL;
942 : 50 : for (i = 0; parms.iterate (i, &ip); i++)
943 : 25 : if (!avals.m_known_vals[ip])
944 : 4 : info->builtin_constant_p_parms.safe_push (ip);
945 : : }
946 : :
947 : : /* If inliner or someone after inliner will ever start producing
948 : : non-trivial clones, we will get trouble with lack of information
949 : : about updating self sizes, because size vectors already contains
950 : : sizes of the callees. */
951 : 12226 : gcc_assert (!inlined_to_p || !optimized_out_size);
952 : 12226 : }
953 : : else
954 : : {
955 : 2646967 : info->size_time_table = src_info->size_time_table.copy ();
956 : 2646967 : info->loop_iterations = vec_safe_copy (src_info->loop_iterations);
957 : 2646967 : info->loop_strides = vec_safe_copy (info->loop_strides);
958 : :
959 : 2646967 : info->builtin_constant_p_parms
960 : 2646967 : = info->builtin_constant_p_parms.copy ();
961 : :
962 : 2646967 : ipa_freqcounting_predicate *f;
963 : 2703009 : for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++)
964 : : {
965 : 56042 : ipa_predicate p = *f->predicate;
966 : 56042 : f->predicate = NULL;
967 : 56042 : set_hint_predicate (&f->predicate, p);
968 : : }
969 : 2680410 : for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++)
970 : : {
971 : 33443 : ipa_predicate p = *f->predicate;
972 : 33443 : f->predicate = NULL;
973 : 33443 : set_hint_predicate (&f->predicate, p);
974 : : }
975 : : }
976 : 2659193 : if (!dst->inlined_to)
977 : 129303 : ipa_update_overall_fn_summary (dst);
978 : 2659193 : }
979 : :
980 : :
981 : : /* Hook that is called by cgraph.cc when a node is duplicated. */
982 : :
983 : : void
984 : 3427325 : ipa_call_summary_t::duplicate (struct cgraph_edge *src,
985 : : struct cgraph_edge *dst,
986 : : class ipa_call_summary *srcinfo,
987 : : class ipa_call_summary *info)
988 : : {
989 : 3427325 : new (info) ipa_call_summary (*srcinfo);
990 : 3427325 : info->predicate = NULL;
991 : 3427325 : edge_set_predicate (dst, srcinfo->predicate);
992 : 3427325 : info->param = srcinfo->param.copy ();
993 : 3427325 : if (!dst->indirect_unknown_callee && src->indirect_unknown_callee)
994 : : {
995 : 10070 : info->call_stmt_size -= (eni_size_weights.indirect_call_cost
996 : 10070 : - eni_size_weights.call_cost);
997 : 10070 : info->call_stmt_time -= (eni_time_weights.indirect_call_cost
998 : 10070 : - eni_time_weights.call_cost);
999 : : }
1000 : 3427325 : }
1001 : :
1002 : : /* Dump edge summaries associated to NODE and recursively to all clones.
1003 : : Indent by INDENT. */
1004 : :
1005 : : static void
1006 : 1901 : dump_ipa_call_summary (FILE *f, int indent, struct cgraph_node *node,
1007 : : class ipa_fn_summary *info)
1008 : : {
1009 : 1901 : struct cgraph_edge *edge;
1010 : 9552 : for (edge = node->callees; edge; edge = edge->next_callee)
1011 : : {
1012 : 7651 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1013 : 7651 : struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1014 : 7651 : int i;
1015 : :
1016 : 15302 : fprintf (f,
1017 : : "%*s%s %s\n%*s freq:%4.2f",
1018 : : indent, "", callee->dump_name (),
1019 : 7651 : !edge->inline_failed
1020 : 7152 : ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1021 : 7651 : indent, "", edge->sreal_frequency ().to_double ());
1022 : :
1023 : 7651 : if (cross_module_call_p (edge))
1024 : 4 : fprintf (f, " cross module");
1025 : :
1026 : 7651 : if (es)
1027 : 7152 : fprintf (f, " loop depth:%2i size:%2i time: %2i",
1028 : : es->loop_depth, es->call_stmt_size, es->call_stmt_time);
1029 : :
1030 : 7651 : ipa_fn_summary *s = ipa_fn_summaries->get (callee);
1031 : 7651 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1032 : 7651 : if (s != NULL)
1033 : 507 : fprintf (f, " callee size:%2i stack:%2i",
1034 : 507 : (int) (ss->size / ipa_fn_summary::size_scale),
1035 : 507 : (int) s->estimated_stack_size);
1036 : :
1037 : 7651 : if (es && es->predicate)
1038 : : {
1039 : 4627 : fprintf (f, " predicate: ");
1040 : 4627 : es->predicate->dump (f, info->conds);
1041 : : }
1042 : : else
1043 : 3024 : fprintf (f, "\n");
1044 : 7651 : if (es && es->param.exists ())
1045 : 4782 : for (i = 0; i < (int) es->param.length (); i++)
1046 : : {
1047 : 1406 : int prob = es->param[i].change_prob;
1048 : :
1049 : 1406 : if (!prob)
1050 : 612 : fprintf (f, "%*s op%i is compile time invariant\n",
1051 : : indent + 2, "", i);
1052 : 794 : else if (prob != REG_BR_PROB_BASE)
1053 : 35 : fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1054 : 35 : prob * 100.0 / REG_BR_PROB_BASE);
1055 : 1406 : if (es->param[i].points_to_local_or_readonly_memory)
1056 : 400 : fprintf (f, "%*s op%i points to local or readonly memory\n",
1057 : : indent + 2, "", i);
1058 : 1406 : if (es->param[i].points_to_possible_sra_candidate)
1059 : 227 : fprintf (f, "%*s op%i points to possible sra candidate\n",
1060 : : indent + 2, "", i);
1061 : : }
1062 : 7651 : if (!edge->inline_failed)
1063 : : {
1064 : 499 : ipa_size_summary *ss = ipa_size_summaries->get (callee);
1065 : 499 : fprintf (f, "%*sStack frame offset %i, callee self size %i\n",
1066 : : indent + 2, "",
1067 : 499 : (int) ipa_get_stack_frame_offset (callee),
1068 : 499 : (int) ss->estimated_self_stack_size);
1069 : 499 : dump_ipa_call_summary (f, indent + 2, callee, info);
1070 : : }
1071 : : }
1072 : 2193 : for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1073 : : {
1074 : 292 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
1075 : 292 : fprintf (f, "%*sindirect call loop depth:%2i freq:%4.2f size:%2i"
1076 : : " time: %2i",
1077 : : indent, "",
1078 : : es->loop_depth,
1079 : 292 : edge->sreal_frequency ().to_double (), es->call_stmt_size,
1080 : : es->call_stmt_time);
1081 : 292 : if (es->predicate)
1082 : : {
1083 : 6 : fprintf (f, "predicate: ");
1084 : 6 : es->predicate->dump (f, info->conds);
1085 : : }
1086 : : else
1087 : 286 : fprintf (f, "\n");
1088 : : }
1089 : 1901 : }
1090 : :
1091 : :
1092 : : void
1093 : 1608 : ipa_dump_fn_summary (FILE *f, struct cgraph_node *node)
1094 : : {
1095 : 1608 : if (node->definition)
1096 : : {
1097 : 1608 : class ipa_fn_summary *s = ipa_fn_summaries->get (node);
1098 : 1608 : class ipa_size_summary *ss = ipa_size_summaries->get (node);
1099 : 1608 : if (s != NULL)
1100 : : {
1101 : 1402 : size_time_entry *e;
1102 : 1402 : int i;
1103 : 1402 : fprintf (f, "IPA function summary for %s", node->dump_name ());
1104 : 1402 : if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1105 : 0 : fprintf (f, " always_inline");
1106 : 1402 : if (s->inlinable)
1107 : 1233 : fprintf (f, " inlinable");
1108 : 1402 : if (s->fp_expressions)
1109 : 44 : fprintf (f, " fp_expression");
1110 : 1402 : if (s->builtin_constant_p_parms.length ())
1111 : : {
1112 : 2 : fprintf (f, " builtin_constant_p_parms");
1113 : 4 : for (unsigned int i = 0;
1114 : 4 : i < s->builtin_constant_p_parms.length (); i++)
1115 : 2 : fprintf (f, " %i", s->builtin_constant_p_parms[i]);
1116 : : }
1117 : 1402 : fprintf (f, "\n global time: %f\n", s->time.to_double ());
1118 : 1402 : fprintf (f, " self size: %i\n", ss->self_size);
1119 : 1402 : fprintf (f, " global size: %i\n", ss->size);
1120 : 1402 : fprintf (f, " min size: %i\n", s->min_size);
1121 : 1402 : fprintf (f, " self stack: %i\n",
1122 : 1402 : (int) ss->estimated_self_stack_size);
1123 : 1402 : fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1124 : 1402 : if (s->growth)
1125 : 84 : fprintf (f, " estimated growth:%i\n", (int) s->growth);
1126 : 1402 : if (s->scc_no)
1127 : 5 : fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1128 : 5857 : for (i = 0; s->size_time_table.iterate (i, &e); i++)
1129 : : {
1130 : 4455 : fprintf (f, " size:%f, time:%f",
1131 : 4455 : (double) e->size / ipa_fn_summary::size_scale,
1132 : : e->time.to_double ());
1133 : 4455 : if (e->exec_predicate != true)
1134 : : {
1135 : 2613 : fprintf (f, ", executed if:");
1136 : 2613 : e->exec_predicate.dump (f, s->conds, 0);
1137 : : }
1138 : 4455 : if (e->exec_predicate != e->nonconst_predicate)
1139 : : {
1140 : 1154 : fprintf (f, ", nonconst if:");
1141 : 1154 : e->nonconst_predicate.dump (f, s->conds, 0);
1142 : : }
1143 : 4455 : fprintf (f, "\n");
1144 : : }
1145 : : ipa_freqcounting_predicate *fcp;
1146 : : bool first_fcp = true;
1147 : 1508 : for (int i = 0; vec_safe_iterate (s->loop_iterations, i, &fcp); i++)
1148 : : {
1149 : 106 : if (first_fcp)
1150 : : {
1151 : 78 : fprintf (f, " loop iterations:");
1152 : 78 : first_fcp = false;
1153 : : }
1154 : 106 : fprintf (f, " %3.2f for ", fcp->freq.to_double ());
1155 : 106 : fcp->predicate->dump (f, s->conds);
1156 : : }
1157 : : first_fcp = true;
1158 : 1420 : for (int i = 0; vec_safe_iterate (s->loop_strides, i, &fcp); i++)
1159 : : {
1160 : 18 : if (first_fcp)
1161 : : {
1162 : 10 : fprintf (f, " loop strides:");
1163 : 10 : first_fcp = false;
1164 : : }
1165 : 18 : fprintf (f, " %3.2f for :", fcp->freq.to_double ());
1166 : 18 : fcp->predicate->dump (f, s->conds);
1167 : : }
1168 : 1402 : fprintf (f, " calls:\n");
1169 : 1402 : dump_ipa_call_summary (f, 4, node, s);
1170 : 1402 : fprintf (f, "\n");
1171 : 1402 : if (s->target_info)
1172 : 0 : fprintf (f, " target_info: %x\n", s->target_info);
1173 : : }
1174 : : else
1175 : 206 : fprintf (f, "IPA summary for %s is missing.\n", node->dump_name ());
1176 : : }
1177 : 1608 : }
1178 : :
1179 : : DEBUG_FUNCTION void
1180 : 0 : ipa_debug_fn_summary (struct cgraph_node *node)
1181 : : {
1182 : 0 : ipa_dump_fn_summary (stderr, node);
1183 : 0 : }
1184 : :
1185 : : void
1186 : 356 : ipa_dump_fn_summaries (FILE *f)
1187 : : {
1188 : 356 : struct cgraph_node *node;
1189 : :
1190 : 2234 : FOR_EACH_DEFINED_FUNCTION (node)
1191 : 1878 : if (!node->inlined_to)
1192 : 1379 : ipa_dump_fn_summary (f, node);
1193 : 356 : }
1194 : :
1195 : : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1196 : : boolean variable pointed to by DATA. */
1197 : :
1198 : : static bool
1199 : 1002954 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1200 : : void *data)
1201 : : {
1202 : 1002954 : bool *b = (bool *) data;
1203 : 1002954 : *b = true;
1204 : 1002954 : return true;
1205 : : }
1206 : :
1207 : : /* If OP refers to value of function parameter, return the corresponding
1208 : : parameter. If non-NULL, the size of the memory load (or the SSA_NAME of the
1209 : : PARM_DECL) will be stored to *SIZE_P in that case too. */
1210 : :
1211 : : static tree
1212 : 182166911 : unmodified_parm_1 (ipa_func_body_info *fbi, gimple *stmt, tree op,
1213 : : poly_int64 *size_p)
1214 : : {
1215 : : /* SSA_NAME referring to parm default def? */
1216 : 182166911 : if (TREE_CODE (op) == SSA_NAME
1217 : 106613841 : && SSA_NAME_IS_DEFAULT_DEF (op)
1218 : 207318843 : && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1219 : : {
1220 : 24957951 : if (size_p)
1221 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1222 : 24957951 : return SSA_NAME_VAR (op);
1223 : : }
1224 : : /* Non-SSA parm reference? */
1225 : 157208960 : if (TREE_CODE (op) == PARM_DECL
1226 : 1451302 : && fbi->aa_walk_budget > 0)
1227 : : {
1228 : 1448754 : bool modified = false;
1229 : :
1230 : 1448754 : ao_ref refd;
1231 : 1448754 : ao_ref_init (&refd, op);
1232 : 2897508 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
1233 : : mark_modified, &modified, NULL, NULL,
1234 : : fbi->aa_walk_budget);
1235 : 1448754 : if (walked < 0)
1236 : : {
1237 : 6 : fbi->aa_walk_budget = 0;
1238 : 913189 : return NULL_TREE;
1239 : : }
1240 : 1448748 : fbi->aa_walk_budget -= walked;
1241 : 1448748 : if (!modified)
1242 : : {
1243 : 913183 : if (size_p)
1244 : 0 : *size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op)));
1245 : 913183 : return op;
1246 : : }
1247 : : }
1248 : : return NULL_TREE;
1249 : : }
1250 : :
1251 : : /* If OP refers to value of function parameter, return the corresponding
1252 : : parameter. Also traverse chains of SSA register assignments. If non-NULL,
1253 : : the size of the memory load (or the SSA_NAME of the PARM_DECL) will be
1254 : : stored to *SIZE_P in that case too. */
1255 : :
1256 : : static tree
1257 : 118970210 : unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op,
1258 : : poly_int64 *size_p)
1259 : : {
1260 : 147908456 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1261 : 147908456 : if (res)
1262 : : return res;
1263 : :
1264 : 122831430 : if (TREE_CODE (op) == SSA_NAME
1265 : 69688954 : && !SSA_NAME_IS_DEFAULT_DEF (op)
1266 : 192330967 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1267 : 28938246 : return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op),
1268 : 28938246 : gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)),
1269 : 28938246 : size_p);
1270 : : return NULL_TREE;
1271 : : }
1272 : :
1273 : : /* If OP refers to a value of a function parameter or value loaded from an
1274 : : aggregate passed to a parameter (either by value or reference), return TRUE
1275 : : and store the number of the parameter to *INDEX_P, the access size into
1276 : : *SIZE_P, and information whether and how it has been loaded from an
1277 : : aggregate into *AGGPOS. INFO describes the function parameters, STMT is the
1278 : : statement in which OP is used or loaded. */
1279 : :
1280 : : static bool
1281 : 32763959 : unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi,
1282 : : gimple *stmt, tree op, int *index_p,
1283 : : poly_int64 *size_p,
1284 : : struct agg_position_info *aggpos)
1285 : : {
1286 : 34258455 : tree res = unmodified_parm_1 (fbi, stmt, op, size_p);
1287 : :
1288 : 34258455 : gcc_checking_assert (aggpos);
1289 : 34258455 : if (res)
1290 : : {
1291 : 794108 : *index_p = ipa_get_param_decl_index (fbi->info, res);
1292 : 794108 : if (*index_p < 0)
1293 : : return false;
1294 : 793979 : aggpos->agg_contents = false;
1295 : 793979 : aggpos->by_ref = false;
1296 : 793979 : return true;
1297 : : }
1298 : :
1299 : 33464347 : if (TREE_CODE (op) == SSA_NAME)
1300 : : {
1301 : 11966936 : if (SSA_NAME_IS_DEFAULT_DEF (op)
1302 : 11966936 : || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1303 : : return false;
1304 : 4837018 : stmt = SSA_NAME_DEF_STMT (op);
1305 : 4837018 : op = gimple_assign_rhs1 (stmt);
1306 : 4837018 : if (!REFERENCE_CLASS_P (op))
1307 : : return unmodified_parm_or_parm_agg_item (fbi, stmt, op, index_p, size_p,
1308 : : aggpos);
1309 : : }
1310 : :
1311 : 24839933 : aggpos->agg_contents = true;
1312 : 24839933 : return ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
1313 : : stmt, op, index_p, &aggpos->offset,
1314 : 24839933 : size_p, &aggpos->by_ref);
1315 : : }
1316 : :
1317 : : /* If stmt is simple load or store of value pointed to by a function parmaeter,
1318 : : return its index. */
1319 : :
1320 : : static int
1321 : 110519060 : load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt)
1322 : : {
1323 : 110519060 : if (!optimize)
1324 : : return -1;
1325 : 158420975 : gassign *assign = dyn_cast <gassign *> (stmt);
1326 : 55178164 : if (!assign)
1327 : : return -1;
1328 : 55178164 : tree param;
1329 : 55178164 : if (gimple_assign_load_p (stmt))
1330 : 20389395 : param = gimple_assign_rhs1 (stmt);
1331 : 34788769 : else if (gimple_store_p (stmt))
1332 : 18439024 : param = gimple_assign_lhs (stmt);
1333 : : else
1334 : : return -1;
1335 : 38828419 : tree base = get_base_address (param);
1336 : 38828419 : if (TREE_CODE (base) != MEM_REF
1337 : 13603376 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1338 : 52427733 : || !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1339 : : return -1;
1340 : 7360742 : tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1341 : 7360742 : if (TREE_CODE (p) != PARM_DECL)
1342 : : return -1;
1343 : 7276249 : return ipa_get_param_decl_index (fbi->info, p);
1344 : : }
1345 : :
1346 : : /* See if statement might disappear after inlining.
1347 : : 0 - means not eliminated
1348 : : 1 - half of statements goes away
1349 : : 2 - for sure it is eliminated.
1350 : : We are not terribly sophisticated, basically looking for simple abstraction
1351 : : penalty wrappers. */
1352 : :
1353 : : static int
1354 : 110519060 : eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt)
1355 : : {
1356 : 110519060 : enum gimple_code code = gimple_code (stmt);
1357 : 110519060 : enum tree_code rhs_code;
1358 : :
1359 : 110519060 : if (!optimize)
1360 : : return 0;
1361 : :
1362 : 92688120 : switch (code)
1363 : : {
1364 : : case GIMPLE_RETURN:
1365 : : return 2;
1366 : 55178164 : case GIMPLE_ASSIGN:
1367 : 55178164 : if (gimple_num_ops (stmt) != 2)
1368 : : return 0;
1369 : :
1370 : 39676312 : rhs_code = gimple_assign_rhs_code (stmt);
1371 : :
1372 : : /* Casts of parameters, loads from parameters passed by reference
1373 : : and stores to return value or parameters are often free after
1374 : : inlining due to SRA and further combining.
1375 : : Assume that half of statements goes away. */
1376 : 39676312 : if (CONVERT_EXPR_CODE_P (rhs_code)
1377 : : || rhs_code == VIEW_CONVERT_EXPR
1378 : : || rhs_code == ADDR_EXPR
1379 : 36896503 : || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1380 : : {
1381 : 38828419 : tree rhs = gimple_assign_rhs1 (stmt);
1382 : 38828419 : tree lhs = gimple_assign_lhs (stmt);
1383 : 38828419 : tree inner_rhs = get_base_address (rhs);
1384 : 38828419 : tree inner_lhs = get_base_address (lhs);
1385 : 38828419 : bool rhs_free = false;
1386 : 38828419 : bool lhs_free = false;
1387 : :
1388 : 38828419 : if (!inner_rhs)
1389 : 0 : inner_rhs = rhs;
1390 : 38828419 : if (!inner_lhs)
1391 : 0 : inner_lhs = lhs;
1392 : :
1393 : : /* Reads of parameter are expected to be free. */
1394 : 38828419 : if (unmodified_parm (fbi, stmt, inner_rhs, NULL))
1395 : : rhs_free = true;
1396 : : /* Match expressions of form &this->field. Those will most likely
1397 : : combine with something upstream after inlining. */
1398 : 37564902 : else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1399 : : {
1400 : 2484735 : tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1401 : 2484735 : if (TREE_CODE (op) == PARM_DECL)
1402 : : rhs_free = true;
1403 : 2448453 : else if (TREE_CODE (op) == MEM_REF
1404 : 2448453 : && unmodified_parm (fbi, stmt, TREE_OPERAND (op, 0),
1405 : : NULL))
1406 : : rhs_free = true;
1407 : : }
1408 : :
1409 : : /* When parameter is not SSA register because its address is taken
1410 : : and it is just copied into one, the statement will be completely
1411 : : free after inlining (we will copy propagate backward). */
1412 : 1299799 : if (rhs_free && is_gimple_reg (lhs))
1413 : : return 2;
1414 : :
1415 : : /* Reads of parameters passed by reference
1416 : : expected to be free (i.e. optimized out after inlining). */
1417 : 38166750 : if (TREE_CODE (inner_rhs) == MEM_REF
1418 : 38166750 : && unmodified_parm (fbi, stmt, TREE_OPERAND (inner_rhs, 0), NULL))
1419 : : rhs_free = true;
1420 : :
1421 : : /* Copying parameter passed by reference into gimple register is
1422 : : probably also going to copy propagate, but we can't be quite
1423 : : sure. */
1424 : 38166750 : if (rhs_free && is_gimple_reg (lhs))
1425 : : lhs_free = true;
1426 : :
1427 : : /* Writes to parameters, parameters passed by value and return value
1428 : : (either directly or passed via invisible reference) are free.
1429 : :
1430 : : TODO: We ought to handle testcase like
1431 : : struct a {int a,b;};
1432 : : struct a
1433 : : returnstruct (void)
1434 : : {
1435 : : struct a a ={1,2};
1436 : : return a;
1437 : : }
1438 : :
1439 : : This translate into:
1440 : :
1441 : : returnstruct ()
1442 : : {
1443 : : int a$b;
1444 : : int a$a;
1445 : : struct a a;
1446 : : struct a D.2739;
1447 : :
1448 : : <bb 2>:
1449 : : D.2739.a = 1;
1450 : : D.2739.b = 2;
1451 : : return D.2739;
1452 : :
1453 : : }
1454 : : For that we either need to copy ipa-split logic detecting writes
1455 : : to return value. */
1456 : 38166750 : if (TREE_CODE (inner_lhs) == PARM_DECL
1457 : 38080598 : || TREE_CODE (inner_lhs) == RESULT_DECL
1458 : 75403976 : || (TREE_CODE (inner_lhs) == MEM_REF
1459 : 4960395 : && (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0),
1460 : : NULL)
1461 : 2521954 : || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1462 : 2520766 : && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1463 : 362384 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1464 : : (inner_lhs,
1465 : : 0))) == RESULT_DECL))))
1466 : : lhs_free = true;
1467 : 34718126 : if (lhs_free
1468 : 38166750 : && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1469 : : rhs_free = true;
1470 : 38166750 : if (lhs_free && rhs_free)
1471 : : return 1;
1472 : : }
1473 : : return 0;
1474 : : default:
1475 : : return 0;
1476 : : }
1477 : : }
1478 : :
1479 : : /* Analyze EXPR if it represents a series of simple operations performed on
1480 : : a function parameter and return true if so. FBI, STMT, EXPR, INDEX_P and
1481 : : AGGPOS have the same meaning like in unmodified_parm_or_parm_agg_item.
1482 : : Type of the parameter or load from an aggregate via the parameter is
1483 : : stored in *TYPE_P. Operations on the parameter are recorded to
1484 : : PARAM_OPS_P if it is not NULL. */
1485 : :
1486 : : static bool
1487 : 26916002 : decompose_param_expr (struct ipa_func_body_info *fbi,
1488 : : gimple *stmt, tree expr,
1489 : : int *index_p, tree *type_p,
1490 : : struct agg_position_info *aggpos,
1491 : : expr_eval_ops *param_ops_p = NULL)
1492 : : {
1493 : 26916002 : int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops);
1494 : 26916002 : int op_count = 0;
1495 : :
1496 : 26916002 : if (param_ops_p)
1497 : 9205072 : *param_ops_p = NULL;
1498 : :
1499 : 32763959 : while (true)
1500 : : {
1501 : 32763959 : expr_eval_op eval_op;
1502 : 32763959 : unsigned rhs_count;
1503 : 32763959 : unsigned cst_count = 0;
1504 : :
1505 : 32763959 : if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL,
1506 : : aggpos))
1507 : : {
1508 : 4559654 : tree type = TREE_TYPE (expr);
1509 : :
1510 : 4559654 : if (aggpos->agg_contents)
1511 : : {
1512 : : /* Stop if containing bit-field. */
1513 : 3765675 : if (TREE_CODE (expr) == BIT_FIELD_REF
1514 : 3765675 : || contains_bitfld_component_ref_p (expr))
1515 : : break;
1516 : : }
1517 : :
1518 : 4507231 : *type_p = type;
1519 : 4507231 : return true;
1520 : : }
1521 : :
1522 : 28204305 : if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr))
1523 : : break;
1524 : 10586553 : stmt = SSA_NAME_DEF_STMT (expr);
1525 : :
1526 : 10586553 : if (gcall *call = dyn_cast <gcall *> (stmt))
1527 : : {
1528 : 2476244 : int flags = gimple_call_return_flags (call);
1529 : 2476244 : if (!(flags & ERF_RETURNS_ARG))
1530 : 3063847 : goto fail;
1531 : 193569 : int arg = flags & ERF_RETURN_ARG_MASK;
1532 : 193569 : if (arg >= (int)gimple_call_num_args (call))
1533 : 0 : goto fail;
1534 : 193569 : expr = gimple_call_arg (stmt, arg);
1535 : 4015824 : continue;
1536 : 193569 : }
1537 : :
1538 : 8110309 : if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr)))
1539 : : break;
1540 : :
1541 : 6435675 : switch (gimple_assign_rhs_class (stmt))
1542 : : {
1543 : 3822255 : case GIMPLE_SINGLE_RHS:
1544 : 3822255 : expr = gimple_assign_rhs1 (stmt);
1545 : 3822255 : continue;
1546 : :
1547 : : case GIMPLE_UNARY_RHS:
1548 : : rhs_count = 1;
1549 : : break;
1550 : :
1551 : 1562903 : case GIMPLE_BINARY_RHS:
1552 : 1562903 : rhs_count = 2;
1553 : 1562903 : break;
1554 : :
1555 : 642 : case GIMPLE_TERNARY_RHS:
1556 : 642 : rhs_count = 3;
1557 : 642 : break;
1558 : :
1559 : 0 : default:
1560 : 0 : goto fail;
1561 : : }
1562 : :
1563 : : /* Stop if expression is too complex. */
1564 : 2613420 : if (op_count++ == op_limit)
1565 : : break;
1566 : :
1567 : 2613305 : if (param_ops_p)
1568 : : {
1569 : 2611749 : eval_op.code = gimple_assign_rhs_code (stmt);
1570 : 2611749 : eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt));
1571 : 2611749 : eval_op.val[0] = NULL_TREE;
1572 : 2611749 : eval_op.val[1] = NULL_TREE;
1573 : : }
1574 : :
1575 : : expr = NULL_TREE;
1576 : 6008957 : for (unsigned i = 0; i < rhs_count; i++)
1577 : : {
1578 : 4176824 : tree op = gimple_op (stmt, i + 1);
1579 : :
1580 : 4176824 : gcc_assert (op && !TYPE_P (op));
1581 : 4176824 : if (is_gimple_ip_invariant (op))
1582 : : {
1583 : 784547 : if (++cst_count == rhs_count)
1584 : 1100 : goto fail;
1585 : :
1586 : 783447 : eval_op.val[cst_count - 1] = op;
1587 : : }
1588 : 3392277 : else if (!expr)
1589 : : {
1590 : : /* Found a non-constant operand, and record its index in rhs
1591 : : operands. */
1592 : 2612205 : eval_op.index = i;
1593 : 2612205 : expr = op;
1594 : : }
1595 : : else
1596 : : {
1597 : : /* Found more than one non-constant operands. */
1598 : 780072 : goto fail;
1599 : : }
1600 : : }
1601 : :
1602 : 1832133 : if (param_ops_p)
1603 : 1830853 : vec_safe_insert (*param_ops_p, 0, eval_op);
1604 : : }
1605 : :
1606 : : /* Failed to decompose, free resource and return. */
1607 : 22408771 : fail:
1608 : 22408771 : if (param_ops_p)
1609 : 9112968 : vec_free (*param_ops_p);
1610 : :
1611 : : return false;
1612 : : }
1613 : :
1614 : : /* Record to SUMMARY that PARM is used by builtin_constant_p. */
1615 : :
1616 : : static void
1617 : 11590 : add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm)
1618 : : {
1619 : 11590 : int ip;
1620 : :
1621 : : /* Avoid duplicates. */
1622 : 11602 : for (unsigned int i = 0;
1623 : 11602 : summary->builtin_constant_p_parms.iterate (i, &ip); i++)
1624 : 5635 : if (ip == parm)
1625 : 11590 : return;
1626 : 5967 : summary->builtin_constant_p_parms.safe_push (parm);
1627 : : }
1628 : :
1629 : : /* If BB ends by a conditional we can turn into predicates, attach corresponding
1630 : : predicates to the CFG edges. */
1631 : :
1632 : : static void
1633 : 35120795 : set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1634 : : class ipa_fn_summary *summary,
1635 : : class ipa_node_params *params_summary,
1636 : : basic_block bb)
1637 : : {
1638 : 35120795 : tree op, op2;
1639 : 35120795 : int index;
1640 : 35120795 : struct agg_position_info aggpos;
1641 : 35120795 : enum tree_code code, inverted_code;
1642 : 35120795 : edge e;
1643 : 35120795 : edge_iterator ei;
1644 : 35120795 : gimple *set_stmt;
1645 : 35120795 : tree param_type;
1646 : 35120795 : expr_eval_ops param_ops;
1647 : :
1648 : 70241590 : gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1649 : 11478848 : if (!last)
1650 : 35109205 : return;
1651 : 11478848 : if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1652 : : return;
1653 : 9118598 : op = gimple_cond_lhs (last);
1654 : :
1655 : 9118598 : if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1656 : : ¶m_ops))
1657 : : {
1658 : 1274915 : code = gimple_cond_code (last);
1659 : 1274915 : inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1660 : :
1661 : 3824745 : FOR_EACH_EDGE (e, ei, bb->succs)
1662 : : {
1663 : 1274915 : enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1664 : 2549830 : ? code : inverted_code);
1665 : : /* invert_tree_comparison will return ERROR_MARK on FP
1666 : : comparisons that are not EQ/NE instead of returning proper
1667 : : unordered one. Be sure it is not confused with NON_CONSTANT.
1668 : :
1669 : : And if the edge's target is the final block of diamond CFG graph
1670 : : of this conditional statement, we do not need to compute
1671 : : predicate for the edge because the final block's predicate must
1672 : : be at least as that of the first block of the statement. */
1673 : 2549830 : if (this_code != ERROR_MARK
1674 : 2549830 : && !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1675 : : {
1676 : 2159809 : ipa_predicate p
1677 : 2159809 : = add_condition (summary, params_summary, index,
1678 : : param_type, &aggpos,
1679 : : this_code, gimple_cond_rhs (last), param_ops);
1680 : 2159809 : e->aux = edge_predicate_pool.allocate ();
1681 : 2159809 : *(ipa_predicate *) e->aux = p;
1682 : : }
1683 : : }
1684 : 1274915 : vec_free (param_ops);
1685 : 1274915 : return;
1686 : : }
1687 : :
1688 : 7843683 : if (TREE_CODE (op) != SSA_NAME)
1689 : : return;
1690 : : /* Special case
1691 : : if (builtin_constant_p (op))
1692 : : constant_code
1693 : : else
1694 : : nonconstant_code.
1695 : : Here we can predicate nonconstant_code. We can't
1696 : : really handle constant_code since we have no predicate
1697 : : for this and also the constant code is not known to be
1698 : : optimized away when inliner doesn't see operand is constant.
1699 : : Other optimizers might think otherwise. */
1700 : 7842617 : if (gimple_cond_code (last) != NE_EXPR
1701 : 7842617 : || !integer_zerop (gimple_cond_rhs (last)))
1702 : 4418999 : return;
1703 : 3423618 : set_stmt = SSA_NAME_DEF_STMT (op);
1704 : 3423618 : if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1705 : 3423618 : || gimple_call_num_args (set_stmt) != 1)
1706 : : return;
1707 : 12754 : op2 = gimple_call_arg (set_stmt, 0);
1708 : 12754 : if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos))
1709 : : return;
1710 : 11590 : if (!aggpos.by_ref)
1711 : 11557 : add_builtin_constant_p_parm (summary, index);
1712 : 34770 : FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1713 : : {
1714 : 11590 : ipa_predicate p = add_condition (summary, params_summary, index,
1715 : : param_type, &aggpos,
1716 : : ipa_predicate::is_not_constant, NULL_TREE);
1717 : 11590 : e->aux = edge_predicate_pool.allocate ();
1718 : 11590 : *(ipa_predicate *) e->aux = p;
1719 : : }
1720 : : }
1721 : :
1722 : :
1723 : : /* If BB ends by a switch we can turn into predicates, attach corresponding
1724 : : predicates to the CFG edges. */
1725 : :
1726 : : static void
1727 : 35120795 : set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi,
1728 : : class ipa_fn_summary *summary,
1729 : : class ipa_node_params *params_summary,
1730 : : basic_block bb)
1731 : : {
1732 : 35120795 : tree op;
1733 : 35120795 : int index;
1734 : 35120795 : struct agg_position_info aggpos;
1735 : 35120795 : edge e;
1736 : 35120795 : edge_iterator ei;
1737 : 35120795 : size_t n;
1738 : 35120795 : size_t case_idx;
1739 : 35120795 : tree param_type;
1740 : 35120795 : expr_eval_ops param_ops;
1741 : :
1742 : 70241590 : gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
1743 : 86474 : if (!last)
1744 : 35085888 : return;
1745 : 86474 : op = gimple_switch_index (last);
1746 : 86474 : if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos,
1747 : : ¶m_ops))
1748 : : return;
1749 : :
1750 : 50149 : auto_vec<std::pair<tree, tree> > ranges;
1751 : 50149 : tree type = TREE_TYPE (op);
1752 : 50149 : int bound_limit = opt_for_fn (fbi->node->decl,
1753 : : param_ipa_max_switch_predicate_bounds);
1754 : 50149 : int bound_count = 0;
1755 : : // This can safely be an integer range, as switches can only hold
1756 : : // integers.
1757 : 50149 : int_range<2> vr;
1758 : :
1759 : 100298 : get_range_query (cfun)->range_of_expr (vr, op);
1760 : 50149 : if (vr.undefined_p ())
1761 : 0 : vr.set_varying (TREE_TYPE (op));
1762 : 50149 : tree vr_min, vr_max;
1763 : : // TODO: This entire function could use a rewrite to use the irange
1764 : : // API, instead of trying to recreate its intersection/union logic.
1765 : : // Any use of get_legacy_range() is a serious code smell.
1766 : 50149 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
1767 : 50149 : wide_int vr_wmin = wi::to_wide (vr_min);
1768 : 50149 : wide_int vr_wmax = wi::to_wide (vr_max);
1769 : :
1770 : 353879 : FOR_EACH_EDGE (e, ei, bb->succs)
1771 : : {
1772 : 303730 : e->aux = edge_predicate_pool.allocate ();
1773 : 303730 : *(ipa_predicate *) e->aux = false;
1774 : : }
1775 : :
1776 : 50149 : e = gimple_switch_edge (cfun, last, 0);
1777 : : /* Set BOUND_COUNT to maximum count to bypass computing predicate for
1778 : : default case if its target basic block is in convergence point of all
1779 : : switch cases, which can be determined by checking whether it
1780 : : post-dominates the switch statement. */
1781 : 50149 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1782 : 12448 : bound_count = INT_MAX;
1783 : :
1784 : 50149 : n = gimple_switch_num_labels (last);
1785 : 334037 : for (case_idx = 1; case_idx < n; ++case_idx)
1786 : : {
1787 : 283888 : tree cl = gimple_switch_label (last, case_idx);
1788 : 283888 : tree min = CASE_LOW (cl);
1789 : 283888 : tree max = CASE_HIGH (cl);
1790 : 283888 : ipa_predicate p;
1791 : :
1792 : 283888 : e = gimple_switch_edge (cfun, last, case_idx);
1793 : :
1794 : : /* The case value might not have same type as switch expression,
1795 : : extend the value based on the expression type. */
1796 : 283888 : if (TREE_TYPE (min) != type)
1797 : 56769 : min = wide_int_to_tree (type, wi::to_wide (min));
1798 : :
1799 : 283888 : if (!max)
1800 : : max = min;
1801 : 17836 : else if (TREE_TYPE (max) != type)
1802 : 8746 : max = wide_int_to_tree (type, wi::to_wide (max));
1803 : :
1804 : : /* The case's target basic block is in convergence point of all switch
1805 : : cases, its predicate should be at least as that of the switch
1806 : : statement. */
1807 : 283888 : if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest))
1808 : 3939 : p = true;
1809 : 279949 : else if (min == max)
1810 : 263275 : p = add_condition (summary, params_summary, index, param_type,
1811 : : &aggpos, EQ_EXPR, min, param_ops);
1812 : : else
1813 : : {
1814 : 16674 : ipa_predicate p1, p2;
1815 : 16674 : p1 = add_condition (summary, params_summary, index, param_type,
1816 : : &aggpos, GE_EXPR, min, param_ops);
1817 : 16674 : p2 = add_condition (summary, params_summary,index, param_type,
1818 : : &aggpos, LE_EXPR, max, param_ops);
1819 : 16674 : p = p1 & p2;
1820 : : }
1821 : 283888 : *(ipa_predicate *) e->aux
1822 : 283888 : = p.or_with (summary->conds, *(ipa_predicate *) e->aux);
1823 : :
1824 : : /* If there are too many disjoint case ranges, predicate for default
1825 : : case might become too complicated. So add a limit here. */
1826 : 283888 : if (bound_count > bound_limit)
1827 : 94626 : continue;
1828 : :
1829 : 189262 : bool new_range = true;
1830 : :
1831 : 189262 : if (!ranges.is_empty ())
1832 : : {
1833 : 151561 : wide_int curr_wmin = wi::to_wide (min);
1834 : 151561 : wide_int last_wmax = wi::to_wide (ranges.last ().second);
1835 : :
1836 : : /* Merge case ranges if they are continuous. */
1837 : 151561 : if (curr_wmin == last_wmax + 1)
1838 : : new_range = false;
1839 : 50463 : else if (vr_type == VR_ANTI_RANGE)
1840 : : {
1841 : : /* If two disjoint case ranges can be connected by anti-range
1842 : : of switch index, combine them to one range. */
1843 : 79 : if (wi::lt_p (vr_wmax, curr_wmin - 1, TYPE_SIGN (type)))
1844 : : vr_type = VR_UNDEFINED;
1845 : 0 : else if (wi::le_p (vr_wmin, last_wmax + 1, TYPE_SIGN (type)))
1846 : 0 : new_range = false;
1847 : : }
1848 : 151561 : }
1849 : :
1850 : : /* Create/extend a case range. And we count endpoints of range set,
1851 : : this number nearly equals to number of conditions that we will create
1852 : : for predicate of default case. */
1853 : 151561 : if (new_range)
1854 : : {
1855 : 88164 : bound_count += (min == max) ? 1 : 2;
1856 : 88164 : ranges.safe_push (std::make_pair (min, max));
1857 : : }
1858 : : else
1859 : : {
1860 : 101098 : bound_count += (ranges.last ().first == ranges.last ().second);
1861 : 101098 : ranges.last ().second = max;
1862 : : }
1863 : : }
1864 : :
1865 : 50149 : e = gimple_switch_edge (cfun, last, 0);
1866 : 50149 : if (bound_count > bound_limit)
1867 : : {
1868 : 15242 : *(ipa_predicate *) e->aux = true;
1869 : 15242 : vec_free (param_ops);
1870 : 15242 : return;
1871 : : }
1872 : :
1873 : 34907 : ipa_predicate p_seg = true;
1874 : 34907 : ipa_predicate p_all = false;
1875 : :
1876 : 34907 : if (vr_type != VR_RANGE)
1877 : : {
1878 : 34430 : vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type));
1879 : 34430 : vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type));
1880 : : }
1881 : :
1882 : : /* Construct predicate to represent default range set that is negation of
1883 : : all case ranges. Case range is classified as containing single/non-single
1884 : : values. Suppose a piece of case ranges in the following.
1885 : :
1886 : : [D1...D2] [S1] ... [Sn] [D3...D4]
1887 : :
1888 : : To represent default case's range sets between two non-single value
1889 : : case ranges (From D2 to D3), we construct predicate as:
1890 : :
1891 : : D2 < x < D3 && x != S1 && ... && x != Sn
1892 : : */
1893 : 109069 : for (size_t i = 0; i < ranges.length (); i++)
1894 : : {
1895 : 74257 : tree min = ranges[i].first;
1896 : 74257 : tree max = ranges[i].second;
1897 : :
1898 : 74257 : if (min == max)
1899 : 94140 : p_seg &= add_condition (summary, params_summary, index,
1900 : : param_type, &aggpos, NE_EXPR,
1901 : 47070 : min, param_ops);
1902 : : else
1903 : : {
1904 : : /* Do not create sub-predicate for range that is beyond low bound
1905 : : of switch index. */
1906 : 27187 : if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type)))
1907 : : {
1908 : 18772 : p_seg &= add_condition (summary, params_summary, index,
1909 : : param_type, &aggpos,
1910 : 18772 : LT_EXPR, min, param_ops);
1911 : 18772 : p_all = p_all.or_with (summary->conds, p_seg);
1912 : : }
1913 : :
1914 : : /* Do not create sub-predicate for range that is beyond up bound
1915 : : of switch index. */
1916 : 27187 : if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type)))
1917 : : {
1918 : 95 : p_seg = false;
1919 : 95 : break;
1920 : : }
1921 : :
1922 : 27092 : p_seg = add_condition (summary, params_summary, index,
1923 : : param_type, &aggpos, GT_EXPR,
1924 : : max, param_ops);
1925 : : }
1926 : : }
1927 : :
1928 : 34907 : p_all = p_all.or_with (summary->conds, p_seg);
1929 : 34907 : *(ipa_predicate *) e->aux
1930 : 34907 : = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux);
1931 : :
1932 : 41270 : vec_free (param_ops);
1933 : 50149 : }
1934 : :
1935 : :
1936 : : /* For each BB in NODE attach to its AUX pointer predicate under
1937 : : which it is executable. */
1938 : :
1939 : : static void
1940 : 6292894 : compute_bb_predicates (struct ipa_func_body_info *fbi,
1941 : : struct cgraph_node *node,
1942 : : class ipa_fn_summary *summary,
1943 : : class ipa_node_params *params_summary)
1944 : : {
1945 : 6292894 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1946 : 6292894 : bool done = false;
1947 : 6292894 : basic_block bb;
1948 : :
1949 : 41413689 : FOR_EACH_BB_FN (bb, my_function)
1950 : : {
1951 : 35120795 : set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb);
1952 : 35120795 : set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb);
1953 : : }
1954 : :
1955 : : /* Entry block is always executable. */
1956 : 6292894 : ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1957 : 6292894 : = edge_predicate_pool.allocate ();
1958 : 6292894 : *(ipa_predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux = true;
1959 : :
1960 : : /* A simple dataflow propagation of predicates forward in the CFG.
1961 : : TODO: work in reverse postorder. */
1962 : 19002027 : while (!done)
1963 : : {
1964 : 12709133 : done = true;
1965 : 88346388 : FOR_EACH_BB_FN (bb, my_function)
1966 : : {
1967 : 75637255 : ipa_predicate p = false;
1968 : 75637255 : edge e;
1969 : 75637255 : edge_iterator ei;
1970 : 94084696 : FOR_EACH_EDGE (e, ei, bb->preds)
1971 : : {
1972 : 80582592 : if (e->src->aux)
1973 : : {
1974 : 78726664 : ipa_predicate this_bb_predicate
1975 : : = *(ipa_predicate *) e->src->aux;
1976 : 78726664 : if (e->aux)
1977 : 5161565 : this_bb_predicate &= (*(ipa_predicate *) e->aux);
1978 : 78726664 : p = p.or_with (summary->conds, this_bb_predicate);
1979 : 78726664 : if (p == true)
1980 : : break;
1981 : : }
1982 : : }
1983 : 75637255 : if (p != false)
1984 : : {
1985 : 74456826 : basic_block pdom_bb;
1986 : :
1987 : 74456826 : if (!bb->aux)
1988 : : {
1989 : 27493610 : done = false;
1990 : 27493610 : bb->aux = edge_predicate_pool.allocate ();
1991 : 27493610 : *((ipa_predicate *) bb->aux) = p;
1992 : : }
1993 : 46963216 : else if (p != *(ipa_predicate *) bb->aux)
1994 : : {
1995 : : /* This OR operation is needed to ensure monotonous data flow
1996 : : in the case we hit the limit on number of clauses and the
1997 : : and/or operations above give approximate answers. */
1998 : 152845 : p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux);
1999 : 152845 : if (p != *(ipa_predicate *)bb->aux)
2000 : : {
2001 : 126573 : done = false;
2002 : 126573 : *((ipa_predicate *)bb->aux) = p;
2003 : : }
2004 : : }
2005 : :
2006 : : /* For switch/if statement, we can OR-combine predicates of all
2007 : : its cases/branches to get predicate for basic block in their
2008 : : convergence point, but sometimes this will generate very
2009 : : complicated predicate. Actually, we can get simplified
2010 : : predicate in another way by using the fact that predicate
2011 : : for a basic block must also hold true for its post dominators.
2012 : : To be specific, basic block in convergence point of
2013 : : conditional statement should include predicate of the
2014 : : statement. */
2015 : 74456826 : pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
2016 : 74456826 : if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb)
2017 : : ;
2018 : 38148406 : else if (!pdom_bb->aux)
2019 : : {
2020 : 7627171 : done = false;
2021 : 7627171 : pdom_bb->aux = edge_predicate_pool.allocate ();
2022 : 7627171 : *((ipa_predicate *)pdom_bb->aux) = p;
2023 : : }
2024 : 30521235 : else if (p != *(ipa_predicate *)pdom_bb->aux)
2025 : : {
2026 : 3435429 : p = p.or_with (summary->conds,
2027 : : *(ipa_predicate *)pdom_bb->aux);
2028 : 3435429 : if (p != *(ipa_predicate *)pdom_bb->aux)
2029 : : {
2030 : 194628 : done = false;
2031 : 194628 : *((ipa_predicate *)pdom_bb->aux) = p;
2032 : : }
2033 : : }
2034 : : }
2035 : : }
2036 : : }
2037 : 6292894 : }
2038 : :
2039 : :
2040 : : /* Return predicate specifying when the STMT might have result that is not
2041 : : a compile time constant. */
2042 : :
2043 : : static ipa_predicate
2044 : 4211351 : will_be_nonconstant_expr_predicate (ipa_func_body_info *fbi,
2045 : : class ipa_fn_summary *summary,
2046 : : class ipa_node_params *params_summary,
2047 : : tree expr,
2048 : : vec<ipa_predicate> nonconstant_names)
2049 : : {
2050 : 4211351 : tree parm;
2051 : 4211351 : int index;
2052 : :
2053 : 4453400 : while (UNARY_CLASS_P (expr))
2054 : 242049 : expr = TREE_OPERAND (expr, 0);
2055 : :
2056 : 4211351 : parm = unmodified_parm (fbi, NULL, expr, NULL);
2057 : 4211351 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2058 : 220210 : return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL,
2059 : 220210 : ipa_predicate::changed, NULL_TREE);
2060 : 3991141 : if (is_gimple_min_invariant (expr))
2061 : 61393 : return false;
2062 : 3929748 : if (TREE_CODE (expr) == SSA_NAME)
2063 : 3754013 : return nonconstant_names[SSA_NAME_VERSION (expr)];
2064 : 175735 : if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2065 : : {
2066 : 175126 : ipa_predicate p1
2067 : 175126 : = will_be_nonconstant_expr_predicate (fbi, summary,
2068 : : params_summary,
2069 : 175126 : TREE_OPERAND (expr, 0),
2070 : : nonconstant_names);
2071 : 175126 : if (p1 == true)
2072 : 100481 : return p1;
2073 : :
2074 : 74645 : ipa_predicate p2
2075 : 74645 : = will_be_nonconstant_expr_predicate (fbi, summary,
2076 : : params_summary,
2077 : 74645 : TREE_OPERAND (expr, 1),
2078 : : nonconstant_names);
2079 : 74645 : return p1.or_with (summary->conds, p2);
2080 : : }
2081 : 609 : else if (TREE_CODE (expr) == COND_EXPR)
2082 : : {
2083 : 232 : ipa_predicate p1
2084 : 232 : = will_be_nonconstant_expr_predicate (fbi, summary,
2085 : : params_summary,
2086 : 232 : TREE_OPERAND (expr, 0),
2087 : : nonconstant_names);
2088 : 232 : if (p1 == true)
2089 : 81 : return p1;
2090 : :
2091 : 151 : ipa_predicate p2
2092 : 151 : = will_be_nonconstant_expr_predicate (fbi, summary,
2093 : : params_summary,
2094 : 151 : TREE_OPERAND (expr, 1),
2095 : : nonconstant_names);
2096 : 151 : if (p2 == true)
2097 : 151 : return p2;
2098 : 0 : p1 = p1.or_with (summary->conds, p2);
2099 : 0 : p2 = will_be_nonconstant_expr_predicate (fbi, summary,
2100 : : params_summary,
2101 : 0 : TREE_OPERAND (expr, 2),
2102 : : nonconstant_names);
2103 : 0 : return p2.or_with (summary->conds, p1);
2104 : : }
2105 : 377 : else if (TREE_CODE (expr) == CALL_EXPR)
2106 : 377 : return true;
2107 : : else
2108 : : {
2109 : 0 : debug_tree (expr);
2110 : 0 : gcc_unreachable ();
2111 : : }
2112 : : }
2113 : :
2114 : :
2115 : : /* Return predicate specifying when the STMT might have result that is not
2116 : : a compile time constant. */
2117 : :
2118 : : static ipa_predicate
2119 : 113486561 : will_be_nonconstant_predicate (struct ipa_func_body_info *fbi,
2120 : : class ipa_fn_summary *summary,
2121 : : class ipa_node_params *params_summary,
2122 : : gimple *stmt,
2123 : : vec<ipa_predicate> nonconstant_names)
2124 : : {
2125 : 113486561 : ipa_predicate p = true;
2126 : 113486561 : ssa_op_iter iter;
2127 : 113486561 : tree use;
2128 : 113486561 : tree param_type = NULL_TREE;
2129 : 113486561 : ipa_predicate op_non_const;
2130 : 113486561 : bool is_load;
2131 : 113486561 : int base_index;
2132 : 113486561 : struct agg_position_info aggpos;
2133 : :
2134 : : /* What statements might be optimized away
2135 : : when their arguments are constant. */
2136 : 113486561 : if (gimple_code (stmt) != GIMPLE_ASSIGN
2137 : 38705765 : && gimple_code (stmt) != GIMPLE_COND
2138 : 27452678 : && gimple_code (stmt) != GIMPLE_SWITCH
2139 : 140852765 : && (gimple_code (stmt) != GIMPLE_CALL
2140 : 20510881 : || !(gimple_call_flags (stmt) & ECF_CONST)))
2141 : 25909035 : return p;
2142 : :
2143 : : /* Stores will stay anyway. */
2144 : 87577526 : if (gimple_store_p (stmt))
2145 : 26127774 : return p;
2146 : :
2147 : 61449752 : is_load = gimple_assign_load_p (stmt);
2148 : :
2149 : : /* Loads can be optimized when the value is known. */
2150 : 61449752 : if (is_load)
2151 : : {
2152 : 17698176 : tree op = gimple_assign_rhs1 (stmt);
2153 : 17698176 : if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type,
2154 : : &aggpos))
2155 : 14527599 : return p;
2156 : : }
2157 : : else
2158 : 43751576 : base_index = -1;
2159 : :
2160 : : /* See if we understand all operands before we start
2161 : : adding conditionals. */
2162 : 63187451 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2163 : : {
2164 : 46533057 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2165 : : /* For arguments we can build a condition. */
2166 : 46533057 : if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0)
2167 : 8254981 : continue;
2168 : 38278076 : if (TREE_CODE (use) != SSA_NAME)
2169 : 0 : return p;
2170 : : /* If we know when operand is constant,
2171 : : we still can say something useful. */
2172 : 38278076 : if (nonconstant_names[SSA_NAME_VERSION (use)] != true)
2173 : 8010317 : continue;
2174 : 30267759 : return p;
2175 : : }
2176 : :
2177 : 16654394 : if (is_load)
2178 : 3170496 : op_non_const =
2179 : 3170496 : add_condition (summary, params_summary,
2180 : : base_index, param_type, &aggpos,
2181 : : ipa_predicate::changed, NULL_TREE);
2182 : : else
2183 : 13483898 : op_non_const = false;
2184 : 31907309 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2185 : : {
2186 : 15252915 : tree parm = unmodified_parm (fbi, stmt, use, NULL);
2187 : 15252915 : int index;
2188 : :
2189 : 15252915 : if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0)
2190 : : {
2191 : 7781295 : if (index != base_index)
2192 : 5292157 : p = add_condition (summary, params_summary, index,
2193 : 5292157 : TREE_TYPE (parm), NULL,
2194 : : ipa_predicate::changed, NULL_TREE);
2195 : : else
2196 : 2489138 : continue;
2197 : : }
2198 : : else
2199 : 7471620 : p = nonconstant_names[SSA_NAME_VERSION (use)];
2200 : 12763777 : op_non_const = p.or_with (summary->conds, op_non_const);
2201 : : }
2202 : 16654394 : if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2203 : 14579143 : && gimple_op (stmt, 0)
2204 : 30962553 : && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2205 : 14308159 : nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2206 : 14308159 : = op_non_const;
2207 : 16654394 : return op_non_const;
2208 : : }
2209 : :
2210 : : struct record_modified_bb_info
2211 : : {
2212 : : tree op;
2213 : : bitmap bb_set;
2214 : : gimple *stmt;
2215 : : };
2216 : :
2217 : : /* Value is initialized in INIT_BB and used in USE_BB. We want to compute
2218 : : probability how often it changes between USE_BB.
2219 : : INIT_BB->count/USE_BB->count is an estimate, but if INIT_BB
2220 : : is in different loop nest, we can do better.
2221 : : This is all just estimate. In theory we look for minimal cut separating
2222 : : INIT_BB and USE_BB, but we only want to anticipate loop invariant motion
2223 : : anyway. */
2224 : :
2225 : : static basic_block
2226 : 10380085 : get_minimal_bb (basic_block init_bb, basic_block use_bb)
2227 : : {
2228 : 10380085 : class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father);
2229 : 10380085 : if (l && l->header->count < init_bb->count)
2230 : 371302 : return l->header;
2231 : : return init_bb;
2232 : : }
2233 : :
2234 : : /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2235 : : set except for info->stmt. */
2236 : :
2237 : : static bool
2238 : 4985201 : record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2239 : : {
2240 : 4985201 : struct record_modified_bb_info *info =
2241 : : (struct record_modified_bb_info *) data;
2242 : 4985201 : if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2243 : : return false;
2244 : 4947329 : if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef)))
2245 : : return false;
2246 : 4836645 : bitmap_set_bit (info->bb_set,
2247 : 4836645 : SSA_NAME_IS_DEFAULT_DEF (vdef)
2248 : 0 : ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2249 : : : get_minimal_bb
2250 : 4836645 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2251 : 4836645 : gimple_bb (info->stmt))->index);
2252 : 4836645 : if (dump_file)
2253 : : {
2254 : 0 : fprintf (dump_file, " Param ");
2255 : 0 : print_generic_expr (dump_file, info->op, TDF_SLIM);
2256 : 0 : fprintf (dump_file, " changed at bb %i, minimal: %i stmt: ",
2257 : 0 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index,
2258 : : get_minimal_bb
2259 : 0 : (gimple_bb (SSA_NAME_DEF_STMT (vdef)),
2260 : 0 : gimple_bb (info->stmt))->index);
2261 : 0 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (vdef), 0);
2262 : : }
2263 : : return false;
2264 : : }
2265 : :
2266 : : /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2267 : : will change since last invocation of STMT.
2268 : :
2269 : : Value 0 is reserved for compile time invariants.
2270 : : For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2271 : : ought to be REG_BR_PROB_BASE / estimated_iters. */
2272 : :
2273 : : static int
2274 : 38287338 : param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i)
2275 : : {
2276 : 38287338 : tree op = gimple_call_arg (stmt, i);
2277 : 38287338 : basic_block bb = gimple_bb (stmt);
2278 : :
2279 : 38287338 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
2280 : 299 : op = TREE_OPERAND (op, 0);
2281 : :
2282 : 38287338 : tree base = get_base_address (op);
2283 : :
2284 : : /* Global invariants never change. */
2285 : 38287338 : if (is_gimple_min_invariant (base))
2286 : : return 0;
2287 : :
2288 : : /* We would have to do non-trivial analysis to really work out what
2289 : : is the probability of value to change (i.e. when init statement
2290 : : is in a sibling loop of the call).
2291 : :
2292 : : We do an conservative estimate: when call is executed N times more often
2293 : : than the statement defining value, we take the frequency 1/N. */
2294 : 19213500 : if (TREE_CODE (base) == SSA_NAME)
2295 : : {
2296 : 16786234 : profile_count init_count;
2297 : :
2298 : 25503651 : if (!bb->count.nonzero_p ())
2299 : : return REG_BR_PROB_BASE;
2300 : :
2301 : 8582642 : if (SSA_NAME_IS_DEFAULT_DEF (base))
2302 : 3039202 : init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2303 : : else
2304 : 5543440 : init_count = get_minimal_bb
2305 : 5543440 : (gimple_bb (SSA_NAME_DEF_STMT (base)),
2306 : : gimple_bb (stmt))->count;
2307 : :
2308 : 8582642 : if (init_count < bb->count)
2309 : 448283 : return MAX ((init_count.to_sreal_scale (bb->count)
2310 : : * REG_BR_PROB_BASE).to_int (), 1);
2311 : : return REG_BR_PROB_BASE;
2312 : : }
2313 : : else
2314 : : {
2315 : 2427266 : ao_ref refd;
2316 : 2427266 : profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2317 : 2427266 : struct record_modified_bb_info info;
2318 : 2427266 : tree init = ctor_for_folding (base);
2319 : :
2320 : 2427266 : if (init != error_mark_node)
2321 : : return 0;
2322 : 5370458 : if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0)
2323 : : return REG_BR_PROB_BASE;
2324 : 1484680 : if (dump_file)
2325 : : {
2326 : 0 : fprintf (dump_file, " Analyzing param change probability of ");
2327 : 0 : print_generic_expr (dump_file, op, TDF_SLIM);
2328 : 0 : fprintf (dump_file, "\n");
2329 : : }
2330 : 1484680 : ao_ref_init (&refd, op);
2331 : 1484680 : info.op = op;
2332 : 1484680 : info.stmt = stmt;
2333 : 1484680 : info.bb_set = BITMAP_ALLOC (NULL);
2334 : 1484680 : int walked
2335 : 2969360 : = walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2336 : : NULL, NULL, fbi->aa_walk_budget);
2337 : 1484680 : if (walked > 0)
2338 : 1361038 : fbi->aa_walk_budget -= walked;
2339 : 1484680 : if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index))
2340 : : {
2341 : 919674 : if (walked < 0)
2342 : 208 : fbi->aa_walk_budget = 0;
2343 : 919674 : if (dump_file)
2344 : : {
2345 : 0 : if (walked < 0)
2346 : 0 : fprintf (dump_file, " Ran out of AA walking budget.\n");
2347 : : else
2348 : 0 : fprintf (dump_file, " Set in same BB as used.\n");
2349 : : }
2350 : 919674 : BITMAP_FREE (info.bb_set);
2351 : 919674 : return REG_BR_PROB_BASE;
2352 : : }
2353 : :
2354 : 565006 : bitmap_iterator bi;
2355 : 565006 : unsigned index;
2356 : : /* Lookup the most frequent update of the value and believe that
2357 : : it dominates all the other; precise analysis here is difficult. */
2358 : 1657584 : EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2359 : 1092578 : max = max.max (BASIC_BLOCK_FOR_FN (cfun, index)->count);
2360 : 565006 : if (dump_file)
2361 : : {
2362 : 0 : fprintf (dump_file, " Set with count ");
2363 : 0 : max.dump (dump_file);
2364 : 0 : fprintf (dump_file, " and used with count ");
2365 : 0 : bb->count.dump (dump_file);
2366 : 0 : fprintf (dump_file, " freq %f\n",
2367 : 0 : max.to_sreal_scale (bb->count).to_double ());
2368 : : }
2369 : :
2370 : 565006 : BITMAP_FREE (info.bb_set);
2371 : 565006 : if (max < bb->count)
2372 : 90904 : return MAX ((max.to_sreal_scale (bb->count)
2373 : : * REG_BR_PROB_BASE).to_int (), 1);
2374 : : return REG_BR_PROB_BASE;
2375 : : }
2376 : : }
2377 : :
2378 : : /* Find whether a basic block BB is the final block of a (half) diamond CFG
2379 : : sub-graph and if the predicate the condition depends on is known. If so,
2380 : : return true and store the pointer the predicate in *P. */
2381 : :
2382 : : static bool
2383 : 6474683 : phi_result_unknown_predicate (ipa_func_body_info *fbi,
2384 : : ipa_fn_summary *summary,
2385 : : class ipa_node_params *params_summary,
2386 : : basic_block bb,
2387 : : ipa_predicate *p,
2388 : : vec<ipa_predicate> nonconstant_names)
2389 : : {
2390 : 6474683 : edge e;
2391 : 6474683 : edge_iterator ei;
2392 : 6474683 : basic_block first_bb = NULL;
2393 : :
2394 : 6474683 : if (single_pred_p (bb))
2395 : : {
2396 : 22666 : *p = false;
2397 : 22666 : return true;
2398 : : }
2399 : :
2400 : 14803006 : FOR_EACH_EDGE (e, ei, bb->preds)
2401 : : {
2402 : 12662856 : if (single_succ_p (e->src))
2403 : : {
2404 : 13193591 : if (!single_pred_p (e->src))
2405 : : return false;
2406 : 7327061 : if (!first_bb)
2407 : 3183947 : first_bb = single_pred (e->src);
2408 : 4143114 : else if (single_pred (e->src) != first_bb)
2409 : : return false;
2410 : : }
2411 : : else
2412 : : {
2413 : 4261143 : if (!first_bb)
2414 : : first_bb = e->src;
2415 : 1479286 : else if (e->src != first_bb)
2416 : : return false;
2417 : : }
2418 : : }
2419 : :
2420 : 2140150 : if (!first_bb)
2421 : : return false;
2422 : :
2423 : 4280300 : gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb));
2424 : 2087512 : if (!stmt
2425 : 2087512 : || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2426 : 318170 : return false;
2427 : :
2428 : 1821980 : *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary,
2429 : : gimple_cond_lhs (stmt),
2430 : : nonconstant_names);
2431 : 1821980 : if (*p == true)
2432 : : return false;
2433 : : else
2434 : : return true;
2435 : : }
2436 : :
2437 : : /* Given a PHI statement in a function described by inline properties SUMMARY
2438 : : and *P being the predicate describing whether the selected PHI argument is
2439 : : known, store a predicate for the result of the PHI statement into
2440 : : NONCONSTANT_NAMES, if possible. */
2441 : :
2442 : : static void
2443 : 685695 : predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi,
2444 : : ipa_predicate *p,
2445 : : vec<ipa_predicate> nonconstant_names)
2446 : : {
2447 : 685695 : unsigned i;
2448 : :
2449 : 984312 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2450 : : {
2451 : 859309 : tree arg = gimple_phi_arg (phi, i)->def;
2452 : 859309 : if (!is_gimple_min_invariant (arg))
2453 : : {
2454 : 729946 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
2455 : 1459892 : *p = p->or_with (summary->conds,
2456 : 729946 : nonconstant_names[SSA_NAME_VERSION (arg)]);
2457 : 729946 : if (*p == true)
2458 : : return;
2459 : : }
2460 : : }
2461 : :
2462 : 125003 : if (dump_file && (dump_flags & TDF_DETAILS))
2463 : : {
2464 : 3 : fprintf (dump_file, "\t\tphi predicate: ");
2465 : 3 : p->dump (dump_file, summary->conds);
2466 : : }
2467 : 125003 : nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2468 : : }
2469 : :
2470 : : /* For a typical usage of __builtin_expect (a<b, 1), we
2471 : : may introduce an extra relation stmt:
2472 : : With the builtin, we have
2473 : : t1 = a <= b;
2474 : : t2 = (long int) t1;
2475 : : t3 = __builtin_expect (t2, 1);
2476 : : if (t3 != 0)
2477 : : goto ...
2478 : : Without the builtin, we have
2479 : : if (a<=b)
2480 : : goto...
2481 : : This affects the size/time estimation and may have
2482 : : an impact on the earlier inlining.
2483 : : Here find this pattern and fix it up later. */
2484 : :
2485 : : static gimple *
2486 : 40986138 : find_foldable_builtin_expect (basic_block bb)
2487 : : {
2488 : 40986138 : gimple_stmt_iterator bsi;
2489 : :
2490 : 282643355 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2491 : : {
2492 : 200820811 : gimple *stmt = gsi_stmt (bsi);
2493 : 200820811 : if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2494 : 200625136 : || gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY)
2495 : 401445882 : || gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT))
2496 : : {
2497 : 322626 : tree var = gimple_call_lhs (stmt);
2498 : 322626 : tree arg = gimple_call_arg (stmt, 0);
2499 : 322626 : use_operand_p use_p;
2500 : 322626 : gimple *use_stmt;
2501 : 322626 : bool match = false;
2502 : 322626 : bool done = false;
2503 : :
2504 : 322626 : if (!var || !arg)
2505 : 3 : continue;
2506 : 322623 : gcc_assert (TREE_CODE (var) == SSA_NAME);
2507 : :
2508 : 643973 : while (TREE_CODE (arg) == SSA_NAME)
2509 : : {
2510 : 643973 : gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg);
2511 : 643973 : if (!is_gimple_assign (stmt_tmp))
2512 : : break;
2513 : 637301 : switch (gimple_assign_rhs_code (stmt_tmp))
2514 : : {
2515 : : case LT_EXPR:
2516 : : case LE_EXPR:
2517 : : case GT_EXPR:
2518 : : case GE_EXPR:
2519 : : case EQ_EXPR:
2520 : : case NE_EXPR:
2521 : : match = true;
2522 : : done = true;
2523 : : break;
2524 : : CASE_CONVERT:
2525 : : break;
2526 : : default:
2527 : : done = true;
2528 : : break;
2529 : : }
2530 : 321350 : if (done)
2531 : : break;
2532 : 321350 : arg = gimple_assign_rhs1 (stmt_tmp);
2533 : : }
2534 : :
2535 : 288883 : if (match && single_imm_use (var, &use_p, &use_stmt)
2536 : 611077 : && gimple_code (use_stmt) == GIMPLE_COND)
2537 : 149732 : return use_stmt;
2538 : : }
2539 : : }
2540 : : return NULL;
2541 : : }
2542 : :
2543 : : /* Return true when the basic blocks contains only clobbers followed by RESX.
2544 : : Such BBs are kept around to make removal of dead stores possible with
2545 : : presence of EH and will be optimized out by optimize_clobbers later in the
2546 : : game.
2547 : :
2548 : : NEED_EH is used to recurse in case the clobber has non-EH predecessors
2549 : : that can be clobber only, too.. When it is false, the RESX is not necessary
2550 : : on the end of basic block. */
2551 : :
2552 : : static bool
2553 : 41601543 : clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2554 : : {
2555 : 41601543 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
2556 : 41601543 : edge_iterator ei;
2557 : 41601543 : edge e;
2558 : :
2559 : 41601543 : if (need_eh)
2560 : : {
2561 : 41523693 : if (gsi_end_p (gsi))
2562 : : return false;
2563 : 40299650 : if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2564 : : return false;
2565 : 1024795 : gsi_prev (&gsi);
2566 : : }
2567 : 41064987 : else if (!single_succ_p (bb))
2568 : : return false;
2569 : :
2570 : 4959818 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2571 : : {
2572 : 2939933 : gimple *stmt = gsi_stmt (gsi);
2573 : 2939933 : if (is_gimple_debug (stmt))
2574 : 1081122 : continue;
2575 : 1858811 : if (gimple_clobber_p (stmt))
2576 : 879401 : continue;
2577 : 979410 : if (gimple_code (stmt) == GIMPLE_LABEL)
2578 : : break;
2579 : : return false;
2580 : : }
2581 : :
2582 : : /* See if all predecessors are either throws or clobber only BBs. */
2583 : 3179433 : FOR_EACH_EDGE (e, ei, bb->preds)
2584 : 2628900 : if (!(e->flags & EDGE_EH)
2585 : 2628900 : && !clobber_only_eh_bb_p (e->src, false))
2586 : : return false;
2587 : :
2588 : : return true;
2589 : : }
2590 : :
2591 : : /* Return true if STMT compute a floating point expression that may be affected
2592 : : by -ffast-math and similar flags. */
2593 : :
2594 : : static bool
2595 : 93208140 : fp_expression_p (gimple *stmt)
2596 : : {
2597 : 93208140 : ssa_op_iter i;
2598 : 93208140 : tree op;
2599 : :
2600 : 209003639 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE)
2601 : 116383052 : if (FLOAT_TYPE_P (TREE_TYPE (op)))
2602 : : return true;
2603 : : return false;
2604 : : }
2605 : :
2606 : : /* Return true if T references memory location that is local
2607 : : for the function (that means, dead after return) or read-only. */
2608 : :
2609 : : bool
2610 : 60199065 : refs_local_or_readonly_memory_p (tree t)
2611 : : {
2612 : : /* Non-escaping memory is fine. */
2613 : 60199065 : t = get_base_address (t);
2614 : 60199065 : if ((TREE_CODE (t) == MEM_REF
2615 : 60199065 : || TREE_CODE (t) == TARGET_MEM_REF))
2616 : 25521104 : return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2617 : :
2618 : : /* Automatic variables are fine. */
2619 : 34677961 : if (DECL_P (t)
2620 : 34677961 : && auto_var_in_fn_p (t, current_function_decl))
2621 : : return true;
2622 : :
2623 : : /* Read-only variables are fine. */
2624 : 10807536 : if (DECL_P (t) && TREE_READONLY (t))
2625 : : return true;
2626 : :
2627 : : return false;
2628 : : }
2629 : :
2630 : : /* Return true if T is a pointer pointing to memory location that is local
2631 : : for the function (that means, dead after return) or read-only. */
2632 : :
2633 : : bool
2634 : 72416388 : points_to_local_or_readonly_memory_p (tree t)
2635 : : {
2636 : : /* See if memory location is clearly invalid. */
2637 : 72416388 : if (integer_zerop (t))
2638 : 2343446 : return flag_delete_null_pointer_checks;
2639 : 70072942 : if (TREE_CODE (t) == SSA_NAME)
2640 : : {
2641 : : /* For IPA passes we can consinder accesses to return slot local
2642 : : even if it is not local in the sense that memory is dead by
2643 : : the end of founction.
2644 : : The outer function will see a store in the call assignment
2645 : : and thus this will do right thing for all uses of this
2646 : : function in the current IPA passes (modref, pure/const discovery
2647 : : and inlining heuristics). */
2648 : 44414411 : if (DECL_RESULT (current_function_decl)
2649 : 44414411 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
2650 : 45444879 : && t == ssa_default_def (cfun, DECL_RESULT (current_function_decl)))
2651 : : return true;
2652 : 44110510 : return !ptr_deref_may_alias_global_p (t, false);
2653 : : }
2654 : 25658531 : if (TREE_CODE (t) == ADDR_EXPR
2655 : 25658531 : && (TREE_CODE (TREE_OPERAND (t, 0)) != TARGET_MEM_REF
2656 : 4588 : || TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) != INTEGER_CST))
2657 : 13158722 : return refs_local_or_readonly_memory_p (TREE_OPERAND (t, 0));
2658 : : return false;
2659 : : }
2660 : :
2661 : : /* Return true if T is a pointer pointing to memory location that is possible
2662 : : sra candidate if all functions it is passed to are inlined. */
2663 : :
2664 : : static bool
2665 : 38287338 : points_to_possible_sra_candidate_p (tree t)
2666 : : {
2667 : 38287338 : if (TREE_CODE (t) != ADDR_EXPR)
2668 : : return false;
2669 : :
2670 : 11471664 : t = get_base_address (TREE_OPERAND (t, 0));
2671 : :
2672 : : /* Automatic variables are fine. */
2673 : 11471664 : if (DECL_P (t)
2674 : 11471664 : && auto_var_in_fn_p (t, current_function_decl))
2675 : : return true;
2676 : : return false;
2677 : : }
2678 : :
2679 : : /* Return true if BB only calls builtin_unreachable.
2680 : : We skip empty basic blocks, debug statements, clobbers and predicts.
2681 : : CACHE is used to memoize already analyzed blocks. */
2682 : :
2683 : : static bool
2684 : 26927061 : builtin_unreachable_bb_p (basic_block bb, vec<unsigned char> &cache)
2685 : : {
2686 : 26927061 : if (cache[bb->index])
2687 : 2524529 : return cache[bb->index] - 1;
2688 : 24402532 : gimple_stmt_iterator si;
2689 : 24402532 : auto_vec <basic_block, 4> visited_bbs;
2690 : 24402532 : bool ret = false;
2691 : 25256939 : while (true)
2692 : : {
2693 : 25256939 : bool empty_bb = true;
2694 : 25256939 : visited_bbs.safe_push (bb);
2695 : 25256939 : cache[bb->index] = 3;
2696 : 25256939 : for (si = gsi_start_nondebug_bb (bb);
2697 : 26681862 : !gsi_end_p (si) && empty_bb;
2698 : 1424923 : gsi_next_nondebug (&si))
2699 : : {
2700 : 25241928 : if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT
2701 : 24850353 : && !gimple_clobber_p (gsi_stmt (si))
2702 : 49091953 : && !gimple_nop_p (gsi_stmt (si)))
2703 : : {
2704 : : empty_bb = false;
2705 : : break;
2706 : : }
2707 : : }
2708 : 25256939 : if (!empty_bb)
2709 : : break;
2710 : : else
2711 : 1439934 : bb = single_succ_edge (bb)->dest;
2712 : 1439934 : if (cache[bb->index])
2713 : : {
2714 : 585527 : ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1;
2715 : 585527 : goto done;
2716 : : }
2717 : : }
2718 : 23817005 : if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE)
2719 : 23817005 : || gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP))
2720 : : ret = true;
2721 : 24402532 : done:
2722 : 98464535 : for (basic_block vbb:visited_bbs)
2723 : 25256939 : cache[vbb->index] = (unsigned char)ret + 1;
2724 : 24402532 : return ret;
2725 : 24402532 : }
2726 : :
2727 : : static bool
2728 : 13566823 : guards_builtin_unreachable (basic_block bb, vec<unsigned char> &cache)
2729 : : {
2730 : 13566823 : edge_iterator ei;
2731 : 13566823 : edge e;
2732 : 40266363 : FOR_EACH_EDGE (e, ei, bb->succs)
2733 : 26927061 : if (builtin_unreachable_bb_p (e->dest, cache))
2734 : : {
2735 : 227521 : if (dump_file && (dump_flags & TDF_DETAILS))
2736 : 1 : fprintf (dump_file,
2737 : : "BB %i ends with conditional guarding __builtin_unreachable;"
2738 : : " conditinal is unnecesary\n", bb->index);
2739 : 227521 : return true;
2740 : : }
2741 : : return false;
2742 : : }
2743 : :
2744 : : #define STMT_NECESSARY GF_PLF_1
2745 : :
2746 : : /* If STMT is not already marked necessary, mark it, and add it to the
2747 : : worklist if ADD_TO_WORKLIST is true. */
2748 : :
2749 : : static inline void
2750 : 170365943 : mark_stmt_necessary (gimple *stmt, auto_vec<gimple *> &worklist)
2751 : : {
2752 : 170365943 : gcc_assert (stmt);
2753 : :
2754 : 170365943 : if (gimple_plf (stmt, STMT_NECESSARY))
2755 : : return;
2756 : :
2757 : 141525413 : if (dump_file && (dump_flags & TDF_DETAILS))
2758 : : {
2759 : 207 : fprintf (dump_file, "Marking useful stmt: ");
2760 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2761 : 207 : fprintf (dump_file, "\n");
2762 : : }
2763 : :
2764 : 141525413 : gimple_set_plf (stmt, STMT_NECESSARY, true);
2765 : 141525413 : worklist.safe_push (stmt);
2766 : : }
2767 : :
2768 : : /* Mark the statement defining operand OP as necessary. */
2769 : :
2770 : : static inline void
2771 : 120148911 : mark_operand_necessary (tree op, auto_vec<gimple *> &worklist)
2772 : : {
2773 : 120148911 : gimple *stmt = SSA_NAME_DEF_STMT (op);
2774 : 120148911 : if (gimple_nop_p (stmt))
2775 : : return;
2776 : 96974785 : mark_stmt_necessary (stmt, worklist);
2777 : : }
2778 : :
2779 : : /* Mark all statements that will remain in the body after optimizing out
2780 : : conditionals guarding __builtin_unreachable which we keep to preserve
2781 : : value ranges. */
2782 : :
2783 : : static void
2784 : 7173025 : find_necessary_statements (struct cgraph_node *node)
2785 : : {
2786 : 7173025 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2787 : 7173025 : auto_vec<unsigned char, 10> cache;
2788 : 7173025 : basic_block bb;
2789 : 7173025 : auto_vec<gimple *> worklist;
2790 : :
2791 : 7173025 : cache.safe_grow_cleared (last_basic_block_for_fn (cfun));
2792 : : /* Mark all obviously necessary statements. */
2793 : 48696718 : FOR_EACH_BB_FN (bb, my_function)
2794 : : {
2795 : 41523693 : for (gimple_stmt_iterator gsi = gsi_start_phis (bb);
2796 : 53285668 : !gsi_end_p (gsi); gsi_next (&gsi))
2797 : 11761975 : gimple_set_plf (gsi_stmt (gsi), STMT_NECESSARY, false);
2798 : :
2799 : 231623112 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2800 : 148575726 : gsi_next_nondebug (&bsi))
2801 : : {
2802 : 148575726 : gimple *stmt = gsi_stmt (bsi);
2803 : :
2804 : 148575726 : gimple_set_plf (stmt, STMT_NECESSARY, false);
2805 : 148575726 : if (gimple_has_side_effects (stmt)
2806 : 121251400 : || (is_ctrl_stmt (stmt)
2807 : 21745770 : && (gimple_code (stmt) != GIMPLE_COND
2808 : 13566823 : || !guards_builtin_unreachable (bb, cache)))
2809 : 99733151 : || gimple_store_p (stmt)
2810 : 223781452 : || gimple_code (stmt) == GIMPLE_ASM)
2811 : 73391158 : mark_stmt_necessary (stmt, worklist);
2812 : : }
2813 : : }
2814 : 148698438 : while (worklist.length () > 0)
2815 : : {
2816 : 141525413 : gimple *stmt = worklist.pop ();
2817 : :
2818 : 141525413 : if (dump_file && (dump_flags & TDF_DETAILS))
2819 : : {
2820 : 207 : fprintf (dump_file, "processing: ");
2821 : 207 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2822 : 207 : fprintf (dump_file, "\n");
2823 : : }
2824 : 141525413 : if (gimple_code (stmt) == GIMPLE_PHI)
2825 : 18277777 : for (unsigned int k = 0; k < gimple_phi_num_args (stmt); k++)
2826 : : {
2827 : 12889028 : tree arg = PHI_ARG_DEF (stmt, k);
2828 : :
2829 : 12889028 : if (TREE_CODE (arg) == SSA_NAME)
2830 : 10005182 : mark_operand_necessary (arg, worklist);
2831 : : }
2832 : : else
2833 : : {
2834 : 136136664 : ssa_op_iter iter;
2835 : 136136664 : tree use;
2836 : :
2837 : 246280393 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2838 : 110143729 : mark_operand_necessary (use, worklist);
2839 : : }
2840 : : }
2841 : 7173025 : }
2842 : :
2843 : : /* Analyze function body for NODE.
2844 : : EARLY indicates run from early optimization pipeline. */
2845 : :
2846 : : static void
2847 : 7173025 : analyze_function_body (struct cgraph_node *node, bool early)
2848 : : {
2849 : 7173025 : sreal time = opt_for_fn (node->decl, param_uninlined_function_time);
2850 : : /* Estimate static overhead for function prologue/epilogue and alignment. */
2851 : 7173025 : int size = opt_for_fn (node->decl, param_uninlined_function_insns);
2852 : : /* Benefits are scaled by probability of elimination that is in range
2853 : : <0,2>. */
2854 : 7173025 : basic_block bb;
2855 : 7173025 : struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2856 : 7173025 : sreal freq;
2857 : 7173025 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
2858 : 7173025 : ipa_node_params *params_summary
2859 : 7173025 : = early ? NULL : ipa_node_params_sum->get (node);
2860 : 7173025 : ipa_predicate bb_predicate;
2861 : 7173025 : struct ipa_func_body_info fbi;
2862 : 7173025 : vec<ipa_predicate> nonconstant_names = vNULL;
2863 : 7173025 : int nblocks, n;
2864 : 7173025 : int *order;
2865 : 7173025 : gimple *fix_builtin_expect_stmt;
2866 : :
2867 : 7173025 : gcc_assert (my_function && my_function->cfg);
2868 : 7173025 : gcc_assert (cfun == my_function);
2869 : :
2870 : 7173025 : memset(&fbi, 0, sizeof(fbi));
2871 : 7173025 : vec_free (info->conds);
2872 : 7173025 : info->conds = NULL;
2873 : 7173025 : info->size_time_table.release ();
2874 : 7173025 : info->call_size_time_table.release ();
2875 : :
2876 : : /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2877 : : so we can produce proper inline hints.
2878 : :
2879 : : When optimizing and analyzing for early inliner, initialize node params
2880 : : so we can produce correct BB predicates. */
2881 : :
2882 : 7173025 : if (opt_for_fn (node->decl, optimize))
2883 : : {
2884 : 6292894 : calculate_dominance_info (CDI_DOMINATORS);
2885 : 6292894 : calculate_dominance_info (CDI_POST_DOMINATORS);
2886 : 6292894 : if (!early)
2887 : 1361961 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2888 : : else
2889 : : {
2890 : 4930933 : ipa_check_create_node_params ();
2891 : 4930933 : ipa_initialize_node_params (node);
2892 : : }
2893 : :
2894 : 6292894 : if (ipa_node_params_sum)
2895 : : {
2896 : 6292894 : fbi.node = node;
2897 : 6292894 : fbi.info = ipa_node_params_sum->get (node);
2898 : 6292894 : fbi.bb_infos = vNULL;
2899 : 6292894 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
2900 : 6292894 : fbi.param_count = count_formal_params (node->decl);
2901 : 6292894 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
2902 : :
2903 : 6292894 : nonconstant_names.safe_grow_cleared
2904 : 6292894 : (SSANAMES (my_function)->length (), true);
2905 : : }
2906 : : }
2907 : :
2908 : 7173025 : if (dump_file)
2909 : 229 : fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2910 : : node->dump_name ());
2911 : :
2912 : : /* When we run into maximal number of entries, we assign everything to the
2913 : : constant truth case. Be sure to have it in list. */
2914 : 7173025 : bb_predicate = true;
2915 : 7173025 : info->account_size_time (0, 0, bb_predicate, bb_predicate);
2916 : :
2917 : 7173025 : bb_predicate = ipa_predicate::not_inlined ();
2918 : 7173025 : info->account_size_time (opt_for_fn (node->decl,
2919 : : param_uninlined_function_insns)
2920 : : * ipa_fn_summary::size_scale,
2921 : 7173025 : opt_for_fn (node->decl,
2922 : : param_uninlined_function_time),
2923 : : bb_predicate,
2924 : : bb_predicate);
2925 : :
2926 : : /* Only look for target information for inlinable functions. */
2927 : 7173025 : bool scan_for_target_info =
2928 : 7173025 : info->inlinable
2929 : 12924068 : && targetm.target_option.need_ipa_fn_target_info (node->decl,
2930 : 5751043 : info->target_info);
2931 : :
2932 : 7173025 : if (fbi.info)
2933 : 6292894 : compute_bb_predicates (&fbi, node, info, params_summary);
2934 : 7173025 : find_necessary_statements (node);
2935 : 7173025 : const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
2936 : 7173025 : order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2937 : 7173025 : nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2938 : 48696718 : for (n = 0; n < nblocks; n++)
2939 : : {
2940 : 41523693 : bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2941 : 41523693 : freq = bb->count.to_sreal_scale (entry_count);
2942 : 41523693 : if (clobber_only_eh_bb_p (bb))
2943 : : {
2944 : 537555 : if (dump_file && (dump_flags & TDF_DETAILS))
2945 : 0 : fprintf (dump_file, "\n Ignoring BB %i;"
2946 : : " it will be optimized away by cleanup_clobbers\n",
2947 : : bb->index);
2948 : 537555 : continue;
2949 : : }
2950 : :
2951 : : /* TODO: Obviously predicates can be propagated down across CFG. */
2952 : 40986138 : if (fbi.info)
2953 : : {
2954 : 34642344 : if (bb->aux)
2955 : 34642330 : bb_predicate = *(ipa_predicate *)bb->aux;
2956 : : else
2957 : 14 : bb_predicate = false;
2958 : : }
2959 : : else
2960 : 6343794 : bb_predicate = true;
2961 : :
2962 : 40986138 : if (dump_file && (dump_flags & TDF_DETAILS))
2963 : : {
2964 : 67 : fprintf (dump_file, "\n BB %i predicate:", bb->index);
2965 : 67 : bb_predicate.dump (dump_file, info->conds);
2966 : : }
2967 : :
2968 : 40986138 : if (fbi.info && nonconstant_names.exists ())
2969 : : {
2970 : 34642344 : ipa_predicate phi_predicate;
2971 : 34642344 : bool first_phi = true;
2972 : :
2973 : 35328039 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2974 : 685695 : gsi_next (&bsi))
2975 : : {
2976 : 6552225 : if (first_phi
2977 : 6552225 : && !phi_result_unknown_predicate (&fbi, info,
2978 : : params_summary,
2979 : : bb,
2980 : : &phi_predicate,
2981 : : nonconstant_names))
2982 : : break;
2983 : 685695 : first_phi = false;
2984 : 685695 : if (dump_file && (dump_flags & TDF_DETAILS))
2985 : : {
2986 : 3 : fprintf (dump_file, " ");
2987 : 3 : print_gimple_stmt (dump_file, gsi_stmt (bsi), 0);
2988 : : }
2989 : 685695 : predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2990 : : nonconstant_names);
2991 : : }
2992 : : }
2993 : :
2994 : 40986138 : fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2995 : :
2996 : 40986138 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
2997 : 181302113 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
2998 : : {
2999 : 140315975 : gimple *stmt = gsi_stmt (bsi);
3000 : 140315975 : if (!gimple_plf (stmt, STMT_NECESSARY))
3001 : : {
3002 : 5468350 : if (dump_file && (dump_flags & TDF_DETAILS))
3003 : : {
3004 : 19 : fprintf (dump_file, " skipping unnecesary stmt ");
3005 : 19 : print_gimple_stmt (dump_file, stmt, 0);
3006 : : }
3007 : : /* TODO: const calls used only to produce values for
3008 : : builtion_unreachable guards should not be accounted. However
3009 : : we still want to inline them and this does does not work well
3010 : : with the cost model. For now account them as usual. */
3011 : 5468350 : if (!is_gimple_call (stmt)
3012 : 5468350 : || gimple_call_internal_p (stmt))
3013 : 5176441 : continue;
3014 : : }
3015 : 135139534 : int this_size = estimate_num_insns (stmt, &eni_size_weights);
3016 : 135139534 : int this_time = estimate_num_insns (stmt, &eni_time_weights);
3017 : 135139534 : int prob;
3018 : 135139534 : ipa_predicate will_be_nonconstant;
3019 : :
3020 : : /* This relation stmt should be folded after we remove
3021 : : __builtin_expect call. Adjust the cost here. */
3022 : 135139534 : if (stmt == fix_builtin_expect_stmt)
3023 : : {
3024 : 149292 : this_size--;
3025 : 149292 : this_time--;
3026 : : }
3027 : :
3028 : 135139534 : if (dump_file && (dump_flags & TDF_DETAILS))
3029 : : {
3030 : 201 : fprintf (dump_file, " ");
3031 : 201 : print_gimple_stmt (dump_file, stmt, 0);
3032 : 201 : fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
3033 : : freq.to_double (), this_size,
3034 : : this_time);
3035 : : }
3036 : :
3037 : 135139534 : if (is_gimple_call (stmt)
3038 : 135139534 : && !gimple_call_internal_p (stmt))
3039 : : {
3040 : 22684188 : struct cgraph_edge *edge = node->get_edge (stmt);
3041 : 22684188 : ipa_call_summary *es = ipa_call_summaries->get_create (edge);
3042 : :
3043 : : /* Special case: results of BUILT_IN_CONSTANT_P will be always
3044 : : resolved as constant. We however don't want to optimize
3045 : : out the cgraph edges. */
3046 : 22684188 : if (nonconstant_names.exists ()
3047 : 19874589 : && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
3048 : 9423 : && gimple_call_lhs (stmt)
3049 : 22693611 : && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3050 : : {
3051 : 9423 : ipa_predicate false_p = false;
3052 : 9423 : nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
3053 : 9423 : = false_p;
3054 : : }
3055 : 22684188 : if (ipa_node_params_sum)
3056 : : {
3057 : 19887588 : int count = gimple_call_num_args (stmt);
3058 : 19887588 : int i;
3059 : :
3060 : 19887588 : if (count)
3061 : 17033557 : es->param.safe_grow_cleared (count, true);
3062 : 58174926 : for (i = 0; i < count; i++)
3063 : : {
3064 : 38287338 : int prob = param_change_prob (&fbi, stmt, i);
3065 : 38287338 : gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
3066 : 38287338 : es->param[i].change_prob = prob;
3067 : 38287338 : es->param[i].points_to_local_or_readonly_memory
3068 : 38287338 : = points_to_local_or_readonly_memory_p
3069 : 38287338 : (gimple_call_arg (stmt, i));
3070 : 38287338 : es->param[i].points_to_possible_sra_candidate
3071 : 38287338 : = points_to_possible_sra_candidate_p
3072 : 38287338 : (gimple_call_arg (stmt, i));
3073 : : }
3074 : : }
3075 : : /* We cannot setup VLA parameters during inlining. */
3076 : 66678360 : for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i)
3077 : 43994568 : if (TREE_CODE (gimple_call_arg (stmt, i)) == WITH_SIZE_EXPR)
3078 : : {
3079 : 396 : edge->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
3080 : 396 : break;
3081 : : }
3082 : 22684188 : es->call_stmt_size = this_size;
3083 : 22684188 : es->call_stmt_time = this_time;
3084 : 22684188 : es->loop_depth = bb_loop_depth (bb);
3085 : 22684188 : edge_set_predicate (edge, &bb_predicate);
3086 : 22684188 : if (edge->speculative)
3087 : : {
3088 : 0 : cgraph_edge *indirect
3089 : 0 : = edge->speculative_call_indirect_edge ();
3090 : 0 : ipa_call_summary *es2
3091 : 0 : = ipa_call_summaries->get_create (indirect);
3092 : 0 : ipa_call_summaries->duplicate (edge, indirect,
3093 : : es, es2);
3094 : :
3095 : : /* Edge is the first direct call.
3096 : : create and duplicate call summaries for multiple
3097 : : speculative call targets. */
3098 : 0 : for (cgraph_edge *direct
3099 : 0 : = edge->next_speculative_call_target ();
3100 : 0 : direct;
3101 : 0 : direct = direct->next_speculative_call_target ())
3102 : : {
3103 : 0 : ipa_call_summary *es3
3104 : 0 : = ipa_call_summaries->get_create (direct);
3105 : 0 : ipa_call_summaries->duplicate (edge, direct,
3106 : : es, es3);
3107 : : }
3108 : : }
3109 : : }
3110 : :
3111 : : /* TODO: When conditional jump or switch is known to be constant, but
3112 : : we did not translate it into the predicates, we really can account
3113 : : just maximum of the possible paths. */
3114 : 135139534 : if (fbi.info)
3115 : 113486561 : will_be_nonconstant
3116 : 113486561 : = will_be_nonconstant_predicate (&fbi, info, params_summary,
3117 : : stmt, nonconstant_names);
3118 : : else
3119 : 21652973 : will_be_nonconstant = true;
3120 : 135139534 : if (this_time || this_size)
3121 : : {
3122 : 110519060 : sreal final_time = (sreal)this_time * freq;
3123 : 110519060 : prob = eliminated_by_inlining_prob (&fbi, stmt);
3124 : 110519060 : if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
3125 : 11 : fprintf (dump_file,
3126 : : "\t\t50%% will be eliminated by inlining\n");
3127 : 110519060 : if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
3128 : 18 : fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
3129 : :
3130 : 110519060 : ipa_predicate p = bb_predicate & will_be_nonconstant;
3131 : 110519060 : int parm = load_or_store_of_ptr_parameter (&fbi, stmt);
3132 : 110519060 : ipa_predicate sra_predicate = true;
3133 : 110519060 : if (parm != -1)
3134 : 14239766 : sra_predicate &= add_condition (info, params_summary, parm,
3135 : : ptr_type_node, NULL,
3136 : 7119883 : ipa_predicate::not_sra_candidate, NULL, 0);
3137 : :
3138 : : /* We can ignore statement when we proved it is never going
3139 : : to happen, but we cannot do that for call statements
3140 : : because edges are accounted specially. */
3141 : :
3142 : 221038120 : if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false)
3143 : : {
3144 : 109812074 : time += final_time;
3145 : 109812074 : size += this_size;
3146 : : }
3147 : :
3148 : : /* We account everything but the calls. Calls have their own
3149 : : size/time info attached to cgraph edges. This is necessary
3150 : : in order to make the cost disappear after inlining. */
3151 : 110519060 : if (!is_gimple_call (stmt))
3152 : : {
3153 : 88442761 : if (prob)
3154 : : {
3155 : 14833200 : ipa_predicate ip
3156 : 14833200 : = bb_predicate & ipa_predicate::not_inlined () & sra_predicate;
3157 : 29666400 : info->account_size_time (this_size * prob,
3158 : 14833200 : (final_time * prob) / 2, ip,
3159 : : p);
3160 : : }
3161 : 14833200 : if (prob != 2)
3162 : 163220192 : info->account_size_time (this_size * (2 - prob),
3163 : 81610096 : (final_time * (2 - prob) / 2),
3164 : 163220192 : bb_predicate & sra_predicate,
3165 : : p);
3166 : : }
3167 : :
3168 : 110519060 : if (!info->fp_expressions && fp_expression_p (stmt))
3169 : : {
3170 : 587553 : info->fp_expressions = true;
3171 : 587553 : if (dump_file)
3172 : 9 : fprintf (dump_file, " fp_expression set\n");
3173 : : }
3174 : : }
3175 : :
3176 : : /* For target specific information, we want to scan all statements
3177 : : rather than those statements with non-zero weights, to avoid
3178 : : missing to scan something interesting for target information,
3179 : : such as: internal function calls. */
3180 : 135139534 : if (scan_for_target_info)
3181 : 0 : scan_for_target_info =
3182 : 0 : targetm.target_option.update_ipa_fn_target_info
3183 : 0 : (info->target_info, stmt);
3184 : :
3185 : : /* Account cost of address calculations in the statements. */
3186 : 521772906 : for (unsigned int i = 0; i < gimple_num_ops (stmt); i++)
3187 : : {
3188 : 386633372 : for (tree op = gimple_op (stmt, i);
3189 : 744367811 : op && handled_component_p (op);
3190 : 46208792 : op = TREE_OPERAND (op, 0))
3191 : 46208792 : if ((TREE_CODE (op) == ARRAY_REF
3192 : 46208792 : || TREE_CODE (op) == ARRAY_RANGE_REF)
3193 : 46208792 : && TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
3194 : : {
3195 : 2352586 : ipa_predicate p = bb_predicate;
3196 : 2352586 : if (fbi.info)
3197 : 1841236 : p = p & will_be_nonconstant_expr_predicate
3198 : 1841236 : (&fbi, info, params_summary,
3199 : 1841236 : TREE_OPERAND (op, 1),
3200 : 1841236 : nonconstant_names);
3201 : 2352586 : if (p != false)
3202 : : {
3203 : 2347085 : time += freq;
3204 : 2347085 : size += 1;
3205 : 2347085 : if (dump_file)
3206 : 24 : fprintf (dump_file,
3207 : : "\t\tAccounting address calculation.\n");
3208 : 2347085 : info->account_size_time (ipa_fn_summary::size_scale,
3209 : : freq,
3210 : : bb_predicate,
3211 : : p);
3212 : : }
3213 : : }
3214 : : }
3215 : :
3216 : : }
3217 : : }
3218 : 7173025 : free (order);
3219 : :
3220 : 7173025 : if (nonconstant_names.exists () && !early)
3221 : : {
3222 : 1361961 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3223 : 1361961 : unsigned max_loop_predicates = opt_for_fn (node->decl,
3224 : : param_ipa_max_loop_predicates);
3225 : :
3226 : 1361961 : if (dump_file && (dump_flags & TDF_DETAILS))
3227 : 14 : flow_loops_dump (dump_file, NULL, 0);
3228 : 1361961 : scev_initialize ();
3229 : 4699081 : for (auto loop : loops_list (cfun, 0))
3230 : : {
3231 : 613198 : ipa_predicate loop_iterations = true;
3232 : 613198 : sreal header_freq;
3233 : 613198 : edge ex;
3234 : 613198 : unsigned int j;
3235 : 613198 : class tree_niter_desc niter_desc;
3236 : 613198 : if (!loop->header->aux)
3237 : 0 : continue;
3238 : :
3239 : 613198 : profile_count phdr_count = loop_preheader_edge (loop)->count ();
3240 : 613198 : sreal phdr_freq = phdr_count.to_sreal_scale (entry_count);
3241 : :
3242 : 613198 : bb_predicate = *(ipa_predicate *)loop->header->aux;
3243 : 613198 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3244 : 1665076 : FOR_EACH_VEC_ELT (exits, j, ex)
3245 : 1051878 : if (number_of_iterations_exit (loop, ex, &niter_desc, false)
3246 : 1051878 : && !is_gimple_min_invariant (niter_desc.niter))
3247 : : {
3248 : 149288 : ipa_predicate will_be_nonconstant
3249 : 149288 : = will_be_nonconstant_expr_predicate (&fbi, info,
3250 : : params_summary,
3251 : : niter_desc.niter,
3252 : : nonconstant_names);
3253 : 149288 : if (will_be_nonconstant != true)
3254 : 60597 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3255 : 149288 : if (will_be_nonconstant != true
3256 : 209885 : && will_be_nonconstant != false)
3257 : 60053 : loop_iterations &= will_be_nonconstant;
3258 : : }
3259 : 613198 : add_freqcounting_predicate (&s->loop_iterations, loop_iterations,
3260 : : phdr_freq, max_loop_predicates);
3261 : 613198 : }
3262 : :
3263 : : /* To avoid quadratic behavior we analyze stride predicates only
3264 : : with respect to the containing loop. Thus we simply iterate
3265 : : over all defs in the outermost loop body. */
3266 : 1361961 : for (class loop *loop = loops_for_fn (cfun)->tree_root->inner;
3267 : 1842690 : loop != NULL; loop = loop->next)
3268 : : {
3269 : 480729 : ipa_predicate loop_stride = true;
3270 : 480729 : basic_block *body = get_loop_body (loop);
3271 : 480729 : profile_count phdr_count = loop_preheader_edge (loop)->count ();
3272 : 480729 : sreal phdr_freq = phdr_count.to_sreal_scale (entry_count);
3273 : 2894128 : for (unsigned i = 0; i < loop->num_nodes; i++)
3274 : : {
3275 : 2413399 : gimple_stmt_iterator gsi;
3276 : 2413399 : if (!body[i]->aux)
3277 : 7 : continue;
3278 : :
3279 : 2413392 : bb_predicate = *(ipa_predicate *)body[i]->aux;
3280 : 15667733 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
3281 : 10840949 : gsi_next (&gsi))
3282 : : {
3283 : 10840949 : gimple *stmt = gsi_stmt (gsi);
3284 : :
3285 : 10840949 : if (!is_gimple_assign (stmt))
3286 : 10692256 : continue;
3287 : :
3288 : 5290252 : tree def = gimple_assign_lhs (stmt);
3289 : 5290252 : if (TREE_CODE (def) != SSA_NAME)
3290 : 1025473 : continue;
3291 : :
3292 : 4264779 : affine_iv iv;
3293 : 8529558 : if (!simple_iv (loop_containing_stmt (stmt),
3294 : : loop_containing_stmt (stmt),
3295 : : def, &iv, true)
3296 : 4264779 : || is_gimple_min_invariant (iv.step))
3297 : 4116086 : continue;
3298 : :
3299 : 148693 : ipa_predicate will_be_nonconstant
3300 : 148693 : = will_be_nonconstant_expr_predicate (&fbi, info,
3301 : : params_summary,
3302 : : iv.step,
3303 : : nonconstant_names);
3304 : 148693 : if (will_be_nonconstant != true)
3305 : 52540 : will_be_nonconstant = bb_predicate & will_be_nonconstant;
3306 : 148693 : if (will_be_nonconstant != true
3307 : 201233 : && will_be_nonconstant != false)
3308 : 33668 : loop_stride = loop_stride & will_be_nonconstant;
3309 : : }
3310 : : }
3311 : 480729 : add_freqcounting_predicate (&s->loop_strides, loop_stride,
3312 : : phdr_freq, max_loop_predicates);
3313 : 480729 : free (body);
3314 : : }
3315 : 1361961 : scev_finalize ();
3316 : : }
3317 : 63042768 : FOR_ALL_BB_FN (bb, my_function)
3318 : : {
3319 : 55869743 : edge e;
3320 : 55869743 : edge_iterator ei;
3321 : :
3322 : 55869743 : if (bb->aux)
3323 : 41413675 : edge_predicate_pool.remove ((ipa_predicate *)bb->aux);
3324 : 55869743 : bb->aux = NULL;
3325 : 119015213 : FOR_EACH_EDGE (e, ei, bb->succs)
3326 : : {
3327 : 63145470 : if (e->aux)
3328 : 2475129 : edge_predicate_pool.remove ((ipa_predicate *)e->aux);
3329 : 63145470 : e->aux = NULL;
3330 : : }
3331 : : }
3332 : 7173025 : ipa_fn_summary *s = ipa_fn_summaries->get (node);
3333 : 7173025 : ipa_size_summary *ss = ipa_size_summaries->get (node);
3334 : 7173025 : s->time = time;
3335 : 7173025 : ss->self_size = size;
3336 : 7173025 : nonconstant_names.release ();
3337 : 7173025 : ipa_release_body_info (&fbi);
3338 : 7173025 : if (opt_for_fn (node->decl, optimize))
3339 : : {
3340 : 6292894 : if (!early)
3341 : 1361961 : loop_optimizer_finalize ();
3342 : 4930933 : else if (!ipa_edge_args_sum)
3343 : 4930919 : ipa_free_all_node_params ();
3344 : 6292894 : free_dominance_info (CDI_DOMINATORS);
3345 : 6292894 : free_dominance_info (CDI_POST_DOMINATORS);
3346 : : }
3347 : 7173025 : if (dump_file)
3348 : : {
3349 : 229 : fprintf (dump_file, "\n");
3350 : 229 : ipa_dump_fn_summary (dump_file, node);
3351 : : }
3352 : 7173025 : }
3353 : :
3354 : :
3355 : : /* Compute function summary.
3356 : : EARLY is true when we compute parameters during early opts. */
3357 : :
3358 : : void
3359 : 7174334 : compute_fn_summary (struct cgraph_node *node, bool early)
3360 : : {
3361 : 7174334 : HOST_WIDE_INT self_stack_size;
3362 : 7174334 : struct cgraph_edge *e;
3363 : :
3364 : 7174334 : gcc_assert (!node->inlined_to);
3365 : :
3366 : 7174334 : if (!ipa_fn_summaries)
3367 : 210795 : ipa_fn_summary_alloc ();
3368 : :
3369 : : /* Create a new ipa_fn_summary. */
3370 : 7174334 : ((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node);
3371 : 7174334 : ipa_fn_summaries->remove (node);
3372 : 7174334 : class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
3373 : 7174334 : class ipa_size_summary *size_info = ipa_size_summaries->get_create (node);
3374 : :
3375 : : /* Estimate the stack size for the function if we're optimizing. */
3376 : 12587081 : self_stack_size = optimize && !node->thunk
3377 : 13467228 : ? estimated_stack_frame_size (node) : 0;
3378 : 7174334 : size_info->estimated_self_stack_size = self_stack_size;
3379 : 7174334 : info->estimated_stack_size = self_stack_size;
3380 : :
3381 : 7174334 : if (node->thunk)
3382 : : {
3383 : 1309 : ipa_call_summary *es = ipa_call_summaries->get_create (node->callees);
3384 : 1309 : ipa_predicate t = true;
3385 : :
3386 : 1309 : node->can_change_signature = false;
3387 : 1309 : es->call_stmt_size = eni_size_weights.call_cost;
3388 : 1309 : es->call_stmt_time = eni_time_weights.call_cost;
3389 : 3927 : info->account_size_time (ipa_fn_summary::size_scale
3390 : 1309 : * opt_for_fn (node->decl,
3391 : : param_uninlined_function_thunk_insns),
3392 : 1309 : opt_for_fn (node->decl,
3393 : : param_uninlined_function_thunk_time), t, t);
3394 : 1309 : t = ipa_predicate::not_inlined ();
3395 : 1309 : info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t);
3396 : 1309 : ipa_update_overall_fn_summary (node);
3397 : 1309 : size_info->self_size = size_info->size;
3398 : 1309 : if (stdarg_p (TREE_TYPE (node->decl)))
3399 : : {
3400 : 9 : info->inlinable = false;
3401 : 9 : node->callees->inline_failed = CIF_VARIADIC_THUNK;
3402 : : }
3403 : : else
3404 : 1300 : info->inlinable = true;
3405 : : }
3406 : : else
3407 : : {
3408 : : /* Even is_gimple_min_invariant rely on current_function_decl. */
3409 : 7173025 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3410 : :
3411 : : /* During IPA profile merging we may be called w/o virtual SSA form
3412 : : built. */
3413 : 7173025 : update_ssa (TODO_update_ssa_only_virtuals);
3414 : :
3415 : : /* Can this function be inlined at all? */
3416 : 7173025 : if (!opt_for_fn (node->decl, optimize)
3417 : 8053156 : && !lookup_attribute ("always_inline",
3418 : 880131 : DECL_ATTRIBUTES (node->decl)))
3419 : 813341 : info->inlinable = false;
3420 : : else
3421 : 6359684 : info->inlinable = tree_inlinable_function_p (node->decl);
3422 : :
3423 : 7173025 : bool no_signature = false;
3424 : : /* Type attributes can use parameter indices to describe them.
3425 : : Special case fn spec since we can safely preserve them in
3426 : : modref summaries. */
3427 : 7173025 : for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl));
3428 : 7551853 : list && !no_signature; list = TREE_CHAIN (list))
3429 : 378828 : if (!ipa_param_adjustments::type_attribute_allowed_p
3430 : 378828 : (get_attribute_name (list)))
3431 : : {
3432 : 156236 : if (dump_file)
3433 : : {
3434 : 0 : fprintf (dump_file, "No signature change:"
3435 : : " function type has unhandled attribute %s.\n",
3436 : 0 : IDENTIFIER_POINTER (get_attribute_name (list)));
3437 : : }
3438 : : no_signature = true;
3439 : : }
3440 : 7173025 : for (tree parm = DECL_ARGUMENTS (node->decl);
3441 : 21452620 : parm && !no_signature; parm = DECL_CHAIN (parm))
3442 : 14279595 : if (variably_modified_type_p (TREE_TYPE (parm), node->decl))
3443 : : {
3444 : 14969 : if (dump_file)
3445 : : {
3446 : 0 : fprintf (dump_file, "No signature change:"
3447 : : " has parameter with variably modified type.\n");
3448 : : }
3449 : : no_signature = true;
3450 : : }
3451 : :
3452 : : /* Likewise for #pragma omp declare simd functions or functions
3453 : : with simd attribute. */
3454 : 7173025 : if (no_signature
3455 : 14174845 : || lookup_attribute ("omp declare simd",
3456 : 7001820 : DECL_ATTRIBUTES (node->decl)))
3457 : 172875 : node->can_change_signature = false;
3458 : : else
3459 : : {
3460 : : /* Otherwise, inlinable functions always can change signature. */
3461 : 7000150 : if (info->inlinable)
3462 : 5673483 : node->can_change_signature = true;
3463 : : else
3464 : : {
3465 : : /* Functions calling builtin_apply cannot change signature. */
3466 : 5208204 : for (e = node->callees; e; e = e->next_callee)
3467 : : {
3468 : 3913744 : tree cdecl = e->callee->decl;
3469 : 3913744 : if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS,
3470 : : BUILT_IN_VA_START))
3471 : : break;
3472 : : }
3473 : 1326667 : node->can_change_signature = !e;
3474 : : }
3475 : : }
3476 : 7173025 : analyze_function_body (node, early);
3477 : 7173025 : pop_cfun ();
3478 : : }
3479 : :
3480 : : /* Inlining characteristics are maintained by the cgraph_mark_inline. */
3481 : 7174334 : size_info->size = size_info->self_size;
3482 : 7174334 : info->estimated_stack_size = size_info->estimated_self_stack_size;
3483 : :
3484 : : /* Code above should compute exactly the same result as
3485 : : ipa_update_overall_fn_summary except for case when speculative
3486 : : edges are present since these are accounted to size but not
3487 : : self_size. Do not compare time since different order the roundoff
3488 : : errors result in slight changes. */
3489 : 7174334 : ipa_update_overall_fn_summary (node);
3490 : 7174334 : if (flag_checking)
3491 : : {
3492 : 7655100 : for (e = node->indirect_calls; e; e = e->next_callee)
3493 : 480848 : if (e->speculative)
3494 : : break;
3495 : 7174252 : gcc_assert (e || size_info->size == size_info->self_size);
3496 : : }
3497 : 7174334 : }
3498 : :
3499 : :
3500 : : /* Compute parameters of functions used by inliner using
3501 : : current_function_decl. */
3502 : :
3503 : : static unsigned int
3504 : 5751617 : compute_fn_summary_for_current (void)
3505 : : {
3506 : 5751617 : compute_fn_summary (cgraph_node::get (current_function_decl), true);
3507 : 5751617 : return 0;
3508 : : }
3509 : :
3510 : : /* Estimate benefit devirtualizing indirect edge IE and return true if it can
3511 : : be devirtualized and inlined, provided m_known_vals, m_known_contexts and
3512 : : m_known_aggs in AVALS. Return false straight away if AVALS is NULL. */
3513 : :
3514 : : static bool
3515 : 559852 : estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3516 : : int *size, int *time,
3517 : : ipa_call_arg_values *avals)
3518 : : {
3519 : 559852 : tree target;
3520 : 559852 : struct cgraph_node *callee;
3521 : 559852 : class ipa_fn_summary *isummary;
3522 : 559852 : enum availability avail;
3523 : 559852 : bool speculative;
3524 : :
3525 : 559852 : if (!avals
3526 : 895862 : || (!avals->m_known_vals.length() && !avals->m_known_contexts.length ()))
3527 : : return false;
3528 : 533612 : if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3529 : : return false;
3530 : :
3531 : 532786 : target = ipa_get_indirect_edge_target (ie, avals, &speculative);
3532 : 532786 : if (!target || speculative)
3533 : : return false;
3534 : :
3535 : : /* Account for difference in cost between indirect and direct calls. */
3536 : 215782 : *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3537 : 215782 : *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3538 : 215782 : gcc_checking_assert (*time >= 0);
3539 : 215782 : gcc_checking_assert (*size >= 0);
3540 : :
3541 : 215782 : callee = cgraph_node::get (target);
3542 : 215782 : if (!callee || !callee->definition)
3543 : : return false;
3544 : 197606 : callee = callee->function_symbol (&avail);
3545 : 197606 : if (avail < AVAIL_AVAILABLE)
3546 : : return false;
3547 : 197606 : isummary = ipa_fn_summaries->get (callee);
3548 : 197606 : if (isummary == NULL)
3549 : : return false;
3550 : :
3551 : 197602 : return isummary->inlinable;
3552 : : }
3553 : :
3554 : : /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3555 : : handle edge E with probability PROB. Set HINTS accordingly if edge may be
3556 : : devirtualized. AVALS, if non-NULL, describes the context of the call site
3557 : : as far as values of parameters are concerened. */
3558 : :
3559 : : static inline void
3560 : 194426669 : estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3561 : : sreal *time, ipa_call_arg_values *avals,
3562 : : ipa_hints *hints)
3563 : : {
3564 : 194426669 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3565 : 194426669 : int call_size = es->call_stmt_size;
3566 : 194426669 : int call_time = es->call_stmt_time;
3567 : 194426669 : int cur_size;
3568 : :
3569 : 3903556 : if (!e->callee && hints && e->maybe_hot_p ()
3570 : 194986521 : && estimate_edge_devirt_benefit (e, &call_size, &call_time, avals))
3571 : 197484 : *hints |= INLINE_HINT_indirect_call;
3572 : 194426669 : cur_size = call_size * ipa_fn_summary::size_scale;
3573 : 194426669 : *size += cur_size;
3574 : 194426669 : if (min_size)
3575 : 25769111 : *min_size += cur_size;
3576 : 194426669 : if (time)
3577 : 185556416 : *time += ((sreal)call_time) * e->sreal_frequency ();
3578 : 194426669 : }
3579 : :
3580 : :
3581 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3582 : : calls in NODE. POSSIBLE_TRUTHS and AVALS describe the context of the call
3583 : : site.
3584 : :
3585 : : Helper for estimate_calls_size_and_time which does the same but
3586 : : (in most cases) faster. */
3587 : :
3588 : : static void
3589 : 64922399 : estimate_calls_size_and_time_1 (struct cgraph_node *node, int *size,
3590 : : int *min_size, sreal *time,
3591 : : ipa_hints *hints,
3592 : : clause_t possible_truths,
3593 : : ipa_call_arg_values *avals)
3594 : : {
3595 : 64922399 : struct cgraph_edge *e;
3596 : 310532934 : for (e = node->callees; e; e = e->next_callee)
3597 : : {
3598 : 245610535 : if (!e->inline_failed)
3599 : : {
3600 : 45515454 : gcc_checking_assert (!ipa_call_summaries->get (e));
3601 : 45515454 : estimate_calls_size_and_time_1 (e->callee, size, min_size, time,
3602 : : hints, possible_truths, avals);
3603 : :
3604 : 45515454 : continue;
3605 : : }
3606 : 200095081 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3607 : :
3608 : : /* Do not care about zero sized builtins. */
3609 : 200095081 : if (!es->call_stmt_size)
3610 : : {
3611 : 18418149 : gcc_checking_assert (!es->call_stmt_time);
3612 : 18418149 : continue;
3613 : : }
3614 : 181676932 : if (!es->predicate
3615 : 181676932 : || es->predicate->evaluate (possible_truths))
3616 : : {
3617 : : /* Predicates of calls shall not use NOT_CHANGED codes,
3618 : : so we do not need to compute probabilities. */
3619 : 179493700 : estimate_edge_size_and_time (e, size,
3620 : 179493700 : es->predicate ? NULL : min_size,
3621 : : time, avals, hints);
3622 : : }
3623 : : }
3624 : 68602802 : for (e = node->indirect_calls; e; e = e->next_callee)
3625 : : {
3626 : 3680403 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3627 : 3680403 : if (!es->predicate
3628 : 3680403 : || es->predicate->evaluate (possible_truths))
3629 : 3656678 : estimate_edge_size_and_time (e, size,
3630 : 3656678 : es->predicate ? NULL : min_size,
3631 : : time, avals, hints);
3632 : : }
3633 : 64922399 : }
3634 : :
3635 : : /* Populate sum->call_size_time_table for edges from NODE. */
3636 : :
3637 : : static void
3638 : 3296261 : summarize_calls_size_and_time (struct cgraph_node *node,
3639 : : ipa_fn_summary *sum)
3640 : : {
3641 : 3296261 : struct cgraph_edge *e;
3642 : 14524011 : for (e = node->callees; e; e = e->next_callee)
3643 : : {
3644 : 11227750 : if (!e->inline_failed)
3645 : : {
3646 : 1103784 : gcc_checking_assert (!ipa_call_summaries->get (e));
3647 : 1103784 : summarize_calls_size_and_time (e->callee, sum);
3648 : 1103784 : continue;
3649 : : }
3650 : 10123966 : int size = 0;
3651 : 10123966 : sreal time = 0;
3652 : :
3653 : 10123966 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3654 : :
3655 : 10123966 : ipa_predicate pred = true;
3656 : 10123966 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3657 : :
3658 : 10123966 : if (es->predicate)
3659 : 1714348 : pred = *es->predicate;
3660 : 10123966 : sum->account_size_time (size, time, pred, pred, true);
3661 : : }
3662 : 3543139 : for (e = node->indirect_calls; e; e = e->next_callee)
3663 : : {
3664 : 246878 : int size = 0;
3665 : 246878 : sreal time = 0;
3666 : :
3667 : 246878 : estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL);
3668 : 246878 : ipa_predicate pred = true;
3669 : 246878 : class ipa_call_summary *es = ipa_call_summaries->get (e);
3670 : :
3671 : 246878 : if (es->predicate)
3672 : 37348 : pred = *es->predicate;
3673 : 246878 : sum->account_size_time (size, time, pred, pred, true);
3674 : : }
3675 : 3296261 : }
3676 : :
3677 : : /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3678 : : calls in NODE. POSSIBLE_TRUTHS and AVALS (the latter if non-NULL) describe
3679 : : context of the call site. */
3680 : :
3681 : : static void
3682 : 19406978 : estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3683 : : int *min_size, sreal *time,
3684 : : ipa_hints *hints,
3685 : : clause_t possible_truths,
3686 : : ipa_call_arg_values *avals)
3687 : : {
3688 : 19406978 : class ipa_fn_summary *sum = ipa_fn_summaries->get (node);
3689 : 19406978 : bool use_table = true;
3690 : :
3691 : 19406978 : gcc_assert (node->callees || node->indirect_calls);
3692 : :
3693 : : /* During early inlining we do not calculate info for very
3694 : : large functions and thus there is no need for producing
3695 : : summaries. */
3696 : 19406978 : if (!ipa_node_params_sum)
3697 : : use_table = false;
3698 : : /* Do not calculate summaries for simple wrappers; it is waste
3699 : : of memory. */
3700 : 10416416 : else if (node->callees && node->indirect_calls
3701 : 561169 : && node->callees->inline_failed && !node->callees->next_callee)
3702 : : use_table = false;
3703 : : /* If there is an indirect edge that may be optimized, we need
3704 : : to go the slow way. */
3705 : 10353097 : else if (avals && hints
3706 : 10353097 : && (avals->m_known_vals.length ()
3707 : 2745302 : || avals->m_known_contexts.length ()
3708 : 2685086 : || avals->m_known_aggs.length ()))
3709 : : {
3710 : 3642429 : ipa_node_params *params_summary = ipa_node_params_sum->get (node);
3711 : 3642429 : unsigned int nargs = params_summary
3712 : 3642429 : ? ipa_get_param_count (params_summary) : 0;
3713 : :
3714 : 12590773 : for (unsigned int i = 0; i < nargs && use_table; i++)
3715 : : {
3716 : 8948344 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3717 : 8948344 : && (avals->safe_sval_at (i)
3718 : 154245 : || (ipa_argagg_value_list (avals).value_for_index_p (i))))
3719 : : use_table = false;
3720 : 8875079 : else if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3721 : 8875079 : && (avals->m_known_contexts.length () > i
3722 : 9025234 : && !avals->m_known_contexts[i].useless_p ()))
3723 : : use_table = false;
3724 : : }
3725 : : }
3726 : :
3727 : : /* Fast path is via the call size time table. */
3728 : 3642429 : if (use_table)
3729 : : {
3730 : : /* Build summary if it is absent. */
3731 : 10203160 : if (!sum->call_size_time_table.length ())
3732 : : {
3733 : 1287030 : ipa_predicate true_pred = true;
3734 : 1287030 : sum->account_size_time (0, 0, true_pred, true_pred, true);
3735 : 1287030 : summarize_calls_size_and_time (node, sum);
3736 : : }
3737 : :
3738 : 10203160 : int old_size = *size;
3739 : 10203160 : sreal old_time = time ? *time : 0;
3740 : :
3741 : 10203160 : if (min_size)
3742 : 10203160 : *min_size += sum->call_size_time_table[0].size;
3743 : :
3744 : : unsigned int i;
3745 : : size_time_entry *e;
3746 : :
3747 : : /* Walk the table and account sizes and times. */
3748 : 26696621 : for (i = 0; sum->call_size_time_table.iterate (i, &e);
3749 : : i++)
3750 : 16493461 : if (e->exec_predicate.evaluate (possible_truths))
3751 : : {
3752 : 14975087 : *size += e->size;
3753 : 14975087 : if (time)
3754 : 12051137 : *time += e->time;
3755 : : }
3756 : :
3757 : : /* Be careful and see if both methods agree. */
3758 : 33 : if ((flag_checking || dump_file)
3759 : : /* Do not try to sanity check when we know we lost some
3760 : : precision. */
3761 : 10203160 : && sum->call_size_time_table.length ()
3762 : : < ipa_fn_summary::max_size_time_table_size)
3763 : : {
3764 : 10203127 : estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL,
3765 : : possible_truths, avals);
3766 : 10203127 : gcc_assert (*size == old_size);
3767 : 18361245 : if (time && (*time - old_time > 1 || *time - old_time < -1)
3768 : 10203159 : && dump_file)
3769 : 0 : fprintf (dump_file, "Time mismatch in call summary %f!=%f\n",
3770 : : old_time.to_double (),
3771 : : time->to_double ());
3772 : : }
3773 : : }
3774 : : /* Slow path by walking all edges. */
3775 : : else
3776 : 9203818 : estimate_calls_size_and_time_1 (node, size, min_size, time, hints,
3777 : : possible_truths, avals);
3778 : 19406978 : }
3779 : :
3780 : : /* Main constructor for ipa call context. Memory allocation of ARG_VALUES
3781 : : is owned by the caller. INLINE_PARAM_SUMMARY is also owned by the
3782 : : caller. */
3783 : :
3784 : 18991843 : ipa_call_context::ipa_call_context (cgraph_node *node, clause_t possible_truths,
3785 : : clause_t nonspec_possible_truths,
3786 : : vec<inline_param_summary>
3787 : : inline_param_summary,
3788 : 18991843 : ipa_auto_call_arg_values *arg_values)
3789 : 18991843 : : m_node (node), m_possible_truths (possible_truths),
3790 : 18991843 : m_nonspec_possible_truths (nonspec_possible_truths),
3791 : 18991843 : m_inline_param_summary (inline_param_summary),
3792 : 18991843 : m_avals (arg_values)
3793 : : {
3794 : 18991843 : }
3795 : :
3796 : : /* Set THIS to be a duplicate of CTX. Copy all relevant info. */
3797 : :
3798 : : void
3799 : 1873331 : ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx)
3800 : : {
3801 : 1873331 : m_node = ctx.m_node;
3802 : 1873331 : m_possible_truths = ctx.m_possible_truths;
3803 : 1873331 : m_nonspec_possible_truths = ctx.m_nonspec_possible_truths;
3804 : 1873331 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3805 : 1873331 : unsigned int nargs = params_summary
3806 : 1873331 : ? ipa_get_param_count (params_summary) : 0;
3807 : :
3808 : 1873331 : m_inline_param_summary = vNULL;
3809 : : /* Copy the info only if there is at least one useful entry. */
3810 : 1873331 : if (ctx.m_inline_param_summary.exists ())
3811 : : {
3812 : 1669979 : unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs);
3813 : :
3814 : 3923232 : for (unsigned int i = 0; i < n; i++)
3815 : 3213670 : if (ipa_is_param_used_by_ipa_predicates (params_summary, i)
3816 : 4519951 : && !ctx.m_inline_param_summary[i].useless_p ())
3817 : : {
3818 : 960417 : m_inline_param_summary
3819 : 960417 : = ctx.m_inline_param_summary.copy ();
3820 : 960417 : break;
3821 : : }
3822 : : }
3823 : 1873331 : m_avals.m_known_vals = vNULL;
3824 : 1873331 : if (ctx.m_avals.m_known_vals.exists ())
3825 : : {
3826 : 1873331 : unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs);
3827 : :
3828 : 4427595 : for (unsigned int i = 0; i < n; i++)
3829 : 2584091 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3830 : 2584091 : && ctx.m_avals.m_known_vals[i])
3831 : : {
3832 : 29827 : m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy ();
3833 : 29827 : break;
3834 : : }
3835 : : }
3836 : :
3837 : 1873331 : m_avals.m_known_contexts = vNULL;
3838 : 1873331 : if (ctx.m_avals.m_known_contexts.exists ())
3839 : : {
3840 : 1873331 : unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs);
3841 : :
3842 : 1874391 : for (unsigned int i = 0; i < n; i++)
3843 : 19155 : if (ipa_is_param_used_by_polymorphic_call (params_summary, i)
3844 : 19155 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3845 : : {
3846 : 18095 : m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy ();
3847 : 18095 : break;
3848 : : }
3849 : : }
3850 : :
3851 : 1873331 : m_avals.m_known_aggs = vNULL;
3852 : 1873331 : if (ctx.m_avals.m_known_aggs.exists ())
3853 : : {
3854 : 1873331 : const ipa_argagg_value_list avl (&ctx.m_avals);
3855 : 6412812 : for (unsigned int i = 0; i < nargs; i++)
3856 : 4543517 : if (ipa_is_param_used_by_indirect_call (params_summary, i)
3857 : 4543517 : && avl.value_for_index_p (i))
3858 : : {
3859 : 4036 : m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy ();
3860 : 4036 : break;
3861 : : }
3862 : : }
3863 : :
3864 : 1873331 : m_avals.m_known_value_ranges = vNULL;
3865 : 1873331 : }
3866 : :
3867 : : /* Release memory used by known_vals/contexts/aggs vectors. and
3868 : : inline_param_summary. */
3869 : :
3870 : : void
3871 : 3050315 : ipa_cached_call_context::release ()
3872 : : {
3873 : : /* See if context is initialized at first place. */
3874 : 3050315 : if (!m_node)
3875 : : return;
3876 : 1873331 : m_avals.m_known_aggs.release ();
3877 : 1873331 : m_avals.m_known_vals.release ();
3878 : 1873331 : m_avals.m_known_contexts.release ();
3879 : 1873331 : m_inline_param_summary.release ();
3880 : : }
3881 : :
3882 : : /* Return true if CTX describes the same call context as THIS. */
3883 : :
3884 : : bool
3885 : 6593668 : ipa_call_context::equal_to (const ipa_call_context &ctx)
3886 : : {
3887 : 6593668 : if (m_node != ctx.m_node
3888 : 5416684 : || m_possible_truths != ctx.m_possible_truths
3889 : 4840317 : || m_nonspec_possible_truths != ctx.m_nonspec_possible_truths)
3890 : : return false;
3891 : :
3892 : 4840317 : ipa_node_params *params_summary = ipa_node_params_sum->get (m_node);
3893 : 4840317 : unsigned int nargs = params_summary
3894 : 4840317 : ? ipa_get_param_count (params_summary) : 0;
3895 : :
3896 : 4840317 : if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ())
3897 : : {
3898 : 14756265 : for (unsigned int i = 0; i < nargs; i++)
3899 : : {
3900 : 10231757 : if (!ipa_is_param_used_by_ipa_predicates (params_summary, i))
3901 : 3342061 : continue;
3902 : 6889696 : if (i >= m_inline_param_summary.length ()
3903 : 4790057 : || m_inline_param_summary[i].useless_p ())
3904 : : {
3905 : 2981642 : if (i < ctx.m_inline_param_summary.length ()
3906 : 2981642 : && !ctx.m_inline_param_summary[i].useless_p ())
3907 : : return false;
3908 : 2937798 : continue;
3909 : : }
3910 : 3908054 : if (i >= ctx.m_inline_param_summary.length ()
3911 : 3908051 : || ctx.m_inline_param_summary[i].useless_p ())
3912 : : {
3913 : 41136 : if (i < m_inline_param_summary.length ()
3914 : 41136 : && !m_inline_param_summary[i].useless_p ())
3915 : : return false;
3916 : 0 : continue;
3917 : : }
3918 : 3866918 : if (!m_inline_param_summary[i].equal_to
3919 : 3866918 : (ctx.m_inline_param_summary[i]))
3920 : : return false;
3921 : : }
3922 : : }
3923 : 4732527 : if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ())
3924 : : {
3925 : 14770751 : for (unsigned int i = 0; i < nargs; i++)
3926 : : {
3927 : 10048715 : if (!ipa_is_param_used_by_indirect_call (params_summary, i))
3928 : 9903057 : continue;
3929 : 145658 : if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i])
3930 : : {
3931 : 84948 : if (i < ctx.m_avals.m_known_vals.length ()
3932 : 84948 : && ctx.m_avals.m_known_vals[i])
3933 : : return false;
3934 : 84924 : continue;
3935 : : }
3936 : 60710 : if (i >= ctx.m_avals.m_known_vals.length ()
3937 : 60710 : || !ctx.m_avals.m_known_vals[i])
3938 : : {
3939 : : if (i < m_avals.m_known_vals.length () && m_avals.m_known_vals[i])
3940 : : return false;
3941 : : continue;
3942 : : }
3943 : 60686 : if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i])
3944 : : return false;
3945 : : }
3946 : : }
3947 : 4722036 : if (m_avals.m_known_contexts.exists ()
3948 : 4722036 : || ctx.m_avals.m_known_contexts.exists ())
3949 : : {
3950 : 14757307 : for (unsigned int i = 0; i < nargs; i++)
3951 : : {
3952 : 10036139 : if (!ipa_is_param_used_by_polymorphic_call (params_summary, i))
3953 : 9962970 : continue;
3954 : 73169 : if (i >= m_avals.m_known_contexts.length ()
3955 : 72464 : || m_avals.m_known_contexts[i].useless_p ())
3956 : : {
3957 : 705 : if (i < ctx.m_avals.m_known_contexts.length ()
3958 : 705 : && !ctx.m_avals.m_known_contexts[i].useless_p ())
3959 : : return false;
3960 : 693 : continue;
3961 : : }
3962 : 72464 : if (i >= ctx.m_avals.m_known_contexts.length ()
3963 : 72464 : || ctx.m_avals.m_known_contexts[i].useless_p ())
3964 : : {
3965 : 12 : if (i < m_avals.m_known_contexts.length ()
3966 : 1873343 : && !m_avals.m_known_contexts[i].useless_p ())
3967 : : return false;
3968 : 0 : continue;
3969 : : }
3970 : 72452 : if (!m_avals.m_known_contexts[i].equal_to
3971 : 72452 : (ctx.m_avals.m_known_contexts[i]))
3972 : : return false;
3973 : : }
3974 : : }
3975 : 4721168 : if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ())
3976 : : {
3977 : : unsigned i = 0, j = 0;
3978 : 10908027 : while (i < m_avals.m_known_aggs.length ()
3979 : 5452532 : || j < ctx.m_avals.m_known_aggs.length ())
3980 : : {
3981 : 732195 : if (i >= m_avals.m_known_aggs.length ())
3982 : : {
3983 : 719939 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
3984 : 719939 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
3985 : : return false;
3986 : 719615 : j++;
3987 : 719615 : continue;
3988 : 719615 : }
3989 : 12256 : if (j >= ctx.m_avals.m_known_aggs.length ())
3990 : : {
3991 : 339 : int idx1 = m_avals.m_known_aggs[i].index;
3992 : 339 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
3993 : : return false;
3994 : 4 : i++;
3995 : 4 : continue;
3996 : 4 : }
3997 : :
3998 : 11917 : int idx1 = m_avals.m_known_aggs[i].index;
3999 : 11917 : int idx2 = ctx.m_avals.m_known_aggs[j].index;
4000 : 11917 : if (idx1 < idx2)
4001 : : {
4002 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx1))
4003 : : return false;
4004 : 0 : i++;
4005 : 0 : continue;
4006 : : }
4007 : 11917 : if (idx1 > idx2)
4008 : : {
4009 : 0 : if (ipa_is_param_used_by_indirect_call (params_summary, idx2))
4010 : : return false;
4011 : 0 : j++;
4012 : 0 : continue;
4013 : : }
4014 : 11917 : if (!ipa_is_param_used_by_indirect_call (params_summary, idx1))
4015 : : {
4016 : 254 : i++;
4017 : 254 : j++;
4018 : 254 : continue;
4019 : : }
4020 : :
4021 : 11663 : if ((m_avals.m_known_aggs[i].unit_offset
4022 : 11663 : != ctx.m_avals.m_known_aggs[j].unit_offset)
4023 : 11655 : || (m_avals.m_known_aggs[i].by_ref
4024 : 11655 : != ctx.m_avals.m_known_aggs[j].by_ref)
4025 : 23318 : || !operand_equal_p (m_avals.m_known_aggs[i].value,
4026 : 11655 : ctx.m_avals.m_known_aggs[j].value))
4027 : 172 : return false;
4028 : 11491 : i++;
4029 : 11491 : j++;
4030 : : }
4031 : : }
4032 : : return true;
4033 : : }
4034 : :
4035 : : /* Fill in the selected fields in ESTIMATES with value estimated for call in
4036 : : this context. Always compute size and min_size. Only compute time and
4037 : : nonspecialized_time if EST_TIMES is true. Only compute hints if EST_HINTS
4038 : : is true. */
4039 : :
4040 : : void
4041 : 18944627 : ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates,
4042 : : bool est_times, bool est_hints)
4043 : : {
4044 : 18944627 : class ipa_fn_summary *info = ipa_fn_summaries->get (m_node);
4045 : 18944627 : size_time_entry *e;
4046 : 18944627 : int size = 0;
4047 : 18944627 : sreal time = 0;
4048 : 18944627 : int min_size = 0;
4049 : 18944627 : ipa_hints hints = 0;
4050 : 18944627 : sreal loops_with_known_iterations = 0;
4051 : 18944627 : sreal loops_with_known_strides = 0;
4052 : 18944627 : int i;
4053 : :
4054 : 18944627 : if (dump_file && (dump_flags & TDF_DETAILS))
4055 : : {
4056 : 1012 : bool found = false;
4057 : 1012 : fprintf (dump_file, " Estimating body: %s\n"
4058 : : " Known to be false: ", m_node->dump_name ());
4059 : :
4060 : 3335 : for (i = ipa_predicate::not_inlined_condition;
4061 : 6670 : i < (ipa_predicate::first_dynamic_condition
4062 : 5568 : + (int) vec_safe_length (info->conds)); i++)
4063 : 2323 : if (!(m_possible_truths & (1 << i)))
4064 : : {
4065 : 1387 : if (found)
4066 : 423 : fprintf (dump_file, ", ");
4067 : 1387 : found = true;
4068 : 1387 : dump_condition (dump_file, info->conds, i);
4069 : : }
4070 : : }
4071 : :
4072 : 18944627 : if (m_node->callees || m_node->indirect_calls)
4073 : 24518407 : estimate_calls_size_and_time (m_node, &size, &min_size,
4074 : : est_times ? &time : NULL,
4075 : : est_hints ? &hints : NULL, m_possible_truths,
4076 : : &m_avals);
4077 : :
4078 : 18944627 : sreal nonspecialized_time = time;
4079 : :
4080 : 18944627 : min_size += info->size_time_table[0].size;
4081 : 127548614 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4082 : : {
4083 : 108603987 : bool exec = e->exec_predicate.evaluate (m_nonspec_possible_truths);
4084 : :
4085 : : /* Because predicates are conservative, it can happen that nonconst is 1
4086 : : but exec is 0. */
4087 : 108603987 : if (exec)
4088 : : {
4089 : 105826830 : bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths);
4090 : :
4091 : 105826830 : gcc_checking_assert (e->time >= 0);
4092 : 105826830 : gcc_checking_assert (time >= 0);
4093 : :
4094 : : /* We compute specialized size only because size of nonspecialized
4095 : : copy is context independent.
4096 : :
4097 : : The difference between nonspecialized execution and specialized is
4098 : : that nonspecialized is not going to have optimized out computations
4099 : : known to be constant in a specialized setting. */
4100 : 105826830 : if (nonconst)
4101 : 52573839 : size += e->size;
4102 : 105826830 : if (!est_times)
4103 : 57191312 : continue;
4104 : 48635518 : nonspecialized_time += e->time;
4105 : 48635518 : if (!nonconst)
4106 : : ;
4107 : 22641394 : else if (!m_inline_param_summary.exists ())
4108 : : {
4109 : 1561822 : if (nonconst)
4110 : 1561822 : time += e->time;
4111 : : }
4112 : : else
4113 : : {
4114 : 21079572 : int prob = e->nonconst_predicate.probability
4115 : 21079572 : (info->conds, m_possible_truths,
4116 : : m_inline_param_summary);
4117 : 21079572 : gcc_checking_assert (prob >= 0);
4118 : 21079572 : gcc_checking_assert (prob <= REG_BR_PROB_BASE);
4119 : 21079572 : if (prob == REG_BR_PROB_BASE)
4120 : 18020393 : time += e->time;
4121 : : else
4122 : 3059179 : time += e->time * prob / REG_BR_PROB_BASE;
4123 : : }
4124 : 48635518 : gcc_checking_assert (time >= 0);
4125 : : }
4126 : : }
4127 : 18944627 : gcc_checking_assert (info->size_time_table[0].exec_predicate == true);
4128 : 18944627 : gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true);
4129 : 18944627 : gcc_checking_assert (min_size >= 0);
4130 : 18944627 : gcc_checking_assert (size >= 0);
4131 : 18944627 : gcc_checking_assert (time >= 0);
4132 : : /* nonspecialized_time should be always bigger than specialized time.
4133 : : Roundoff issues however may get into the way. */
4134 : 18944627 : gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1);
4135 : :
4136 : : /* Roundoff issues may make specialized time bigger than nonspecialized
4137 : : time. We do not really want that to happen because some heuristics
4138 : : may get confused by seeing negative speedups. */
4139 : 18944627 : if (time > nonspecialized_time)
4140 : 0 : time = nonspecialized_time;
4141 : :
4142 : 18944627 : if (est_hints)
4143 : : {
4144 : 6721522 : if (info->scc_no)
4145 : 181571 : hints |= INLINE_HINT_in_scc;
4146 : 6721522 : if (DECL_DECLARED_INLINE_P (m_node->decl))
4147 : 4089166 : hints |= INLINE_HINT_declared_inline;
4148 : 6721522 : if (info->builtin_constant_p_parms.length ()
4149 : 12740 : && DECL_DECLARED_INLINE_P (m_node->decl))
4150 : 12684 : hints |= INLINE_HINT_builtin_constant_p;
4151 : :
4152 : : ipa_freqcounting_predicate *fcp;
4153 : 7355780 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
4154 : 634258 : if (!fcp->predicate->evaluate (m_possible_truths))
4155 : : {
4156 : 454827 : hints |= INLINE_HINT_loop_iterations;
4157 : 454827 : loops_with_known_iterations += fcp->freq;
4158 : : }
4159 : 6721522 : estimates->loops_with_known_iterations = loops_with_known_iterations;
4160 : :
4161 : 7106461 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
4162 : 384939 : if (!fcp->predicate->evaluate (m_possible_truths))
4163 : : {
4164 : 369034 : hints |= INLINE_HINT_loop_stride;
4165 : 369034 : loops_with_known_strides += fcp->freq;
4166 : : }
4167 : 6721522 : estimates->loops_with_known_strides = loops_with_known_strides;
4168 : : }
4169 : :
4170 : 18944627 : size = RDIV (size, ipa_fn_summary::size_scale);
4171 : 18944627 : min_size = RDIV (min_size, ipa_fn_summary::size_scale);
4172 : :
4173 : 18944627 : if (dump_file && (dump_flags & TDF_DETAILS))
4174 : : {
4175 : 1012 : fprintf (dump_file, "\n size:%i", (int) size);
4176 : 1012 : if (est_times)
4177 : 811 : fprintf (dump_file, " time:%f nonspec time:%f",
4178 : : time.to_double (), nonspecialized_time.to_double ());
4179 : 1012 : if (est_hints)
4180 : 811 : fprintf (dump_file, " loops with known iterations:%f "
4181 : : "known strides:%f", loops_with_known_iterations.to_double (),
4182 : : loops_with_known_strides.to_double ());
4183 : 1012 : fprintf (dump_file, "\n");
4184 : : }
4185 : 18944627 : if (est_times)
4186 : : {
4187 : 6721522 : estimates->time = time;
4188 : 6721522 : estimates->nonspecialized_time = nonspecialized_time;
4189 : : }
4190 : 18944627 : estimates->size = size;
4191 : 18944627 : estimates->min_size = min_size;
4192 : 18944627 : if (est_hints)
4193 : 6721522 : estimates->hints = hints;
4194 : 18944627 : return;
4195 : : }
4196 : :
4197 : :
4198 : : /* Estimate size and time needed to execute callee of EDGE assuming that
4199 : : parameters known to be constant at caller of EDGE are propagated.
4200 : : KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
4201 : : and types for parameters. */
4202 : :
4203 : : void
4204 : 171267 : estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
4205 : : ipa_auto_call_arg_values *avals,
4206 : : ipa_call_estimates *estimates)
4207 : : {
4208 : 171267 : clause_t clause, nonspec_clause;
4209 : :
4210 : 171267 : evaluate_conditions_for_known_args (node, false, avals, &clause,
4211 : : &nonspec_clause, NULL);
4212 : 171267 : ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals);
4213 : 171267 : ctx.estimate_size_and_time (estimates);
4214 : 171267 : }
4215 : :
4216 : : /* Return stack frame offset where frame of NODE is supposed to start inside
4217 : : of the function it is inlined to.
4218 : : Return 0 for functions that are not inlined. */
4219 : :
4220 : : HOST_WIDE_INT
4221 : 5110980 : ipa_get_stack_frame_offset (struct cgraph_node *node)
4222 : : {
4223 : 5110980 : HOST_WIDE_INT offset = 0;
4224 : 5110980 : if (!node->inlined_to)
4225 : : return 0;
4226 : 3982538 : node = node->callers->caller;
4227 : 4856398 : while (true)
4228 : : {
4229 : 4856398 : offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
4230 : 4419468 : if (!node->inlined_to)
4231 : : return offset;
4232 : 436930 : node = node->callers->caller;
4233 : : }
4234 : : }
4235 : :
4236 : :
4237 : : /* Update summary information of inline clones after inlining.
4238 : : Compute peak stack usage. */
4239 : :
4240 : : static void
4241 : 4606603 : inline_update_callee_summaries (struct cgraph_node *node, int depth)
4242 : : {
4243 : 4606603 : struct cgraph_edge *e;
4244 : :
4245 : 4606603 : ipa_propagate_frequency (node);
4246 : 8507994 : for (e = node->callees; e; e = e->next_callee)
4247 : : {
4248 : 3901391 : if (!e->inline_failed)
4249 : 624564 : inline_update_callee_summaries (e->callee, depth);
4250 : : else
4251 : 3276827 : ipa_call_summaries->get (e)->loop_depth += depth;
4252 : : }
4253 : 4713411 : for (e = node->indirect_calls; e; e = e->next_callee)
4254 : 106808 : ipa_call_summaries->get (e)->loop_depth += depth;
4255 : 4606603 : }
4256 : :
4257 : : /* Update change_prob and points_to_local_or_readonly_memory of EDGE after
4258 : : INLINED_EDGE has been inlined.
4259 : :
4260 : : When function A is inlined in B and A calls C with parameter that
4261 : : changes with probability PROB1 and C is known to be passthrough
4262 : : of argument if B that change with probability PROB2, the probability
4263 : : of change is now PROB1*PROB2. */
4264 : :
4265 : : static void
4266 : 3383864 : remap_edge_params (struct cgraph_edge *inlined_edge,
4267 : : struct cgraph_edge *edge)
4268 : : {
4269 : 3383864 : if (ipa_node_params_sum)
4270 : : {
4271 : 2435166 : int i;
4272 : 2435166 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4273 : 2435166 : if (!args)
4274 : : return;
4275 : 1351860 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4276 : 1351860 : class ipa_call_summary *inlined_es
4277 : 1351860 : = ipa_call_summaries->get (inlined_edge);
4278 : :
4279 : 1351860 : if (es->param.length () == 0)
4280 : : return;
4281 : :
4282 : 7844590 : for (i = 0; i < ipa_get_cs_argument_count (args); i++)
4283 : : {
4284 : 2645602 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4285 : 2645602 : if (jfunc->type == IPA_JF_PASS_THROUGH
4286 : 1925897 : || jfunc->type == IPA_JF_ANCESTOR)
4287 : : {
4288 : 866143 : int id = jfunc->type == IPA_JF_PASS_THROUGH
4289 : 866143 : ? ipa_get_jf_pass_through_formal_id (jfunc)
4290 : 146438 : : ipa_get_jf_ancestor_formal_id (jfunc);
4291 : 1732204 : if (id < (int) inlined_es->param.length ())
4292 : : {
4293 : 866053 : int prob1 = es->param[i].change_prob;
4294 : 866053 : int prob2 = inlined_es->param[id].change_prob;
4295 : 866053 : int prob = combine_probabilities (prob1, prob2);
4296 : :
4297 : 866053 : if (prob1 && prob2 && !prob)
4298 : 866053 : prob = 1;
4299 : :
4300 : 866053 : es->param[i].change_prob = prob;
4301 : :
4302 : 1732106 : if (inlined_es
4303 : 866053 : ->param[id].points_to_local_or_readonly_memory)
4304 : 216278 : es->param[i].points_to_local_or_readonly_memory = true;
4305 : 1732106 : if (inlined_es
4306 : 866053 : ->param[id].points_to_possible_sra_candidate)
4307 : 189534 : es->param[i].points_to_possible_sra_candidate = true;
4308 : : }
4309 : 866143 : if (!es->param[i].points_to_local_or_readonly_memory
4310 : : && jfunc->type == IPA_JF_CONST
4311 : : && points_to_local_or_readonly_memory_p
4312 : : (ipa_get_jf_constant (jfunc)))
4313 : : es->param[i].points_to_local_or_readonly_memory = true;
4314 : : }
4315 : : }
4316 : : }
4317 : : }
4318 : :
4319 : : /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
4320 : :
4321 : : Remap predicates of callees of NODE. Rest of arguments match
4322 : : remap_predicate.
4323 : :
4324 : : Also update change probabilities. */
4325 : :
4326 : : static void
4327 : 4606603 : remap_edge_summaries (struct cgraph_edge *inlined_edge,
4328 : : struct cgraph_node *node,
4329 : : class ipa_fn_summary *info,
4330 : : class ipa_node_params *params_summary,
4331 : : class ipa_fn_summary *callee_info,
4332 : : const vec<int> &operand_map,
4333 : : const vec<HOST_WIDE_INT> &offset_map,
4334 : : clause_t possible_truths,
4335 : : ipa_predicate *toplev_predicate)
4336 : : {
4337 : 4606603 : struct cgraph_edge *e, *next;
4338 : 8507477 : for (e = node->callees; e; e = next)
4339 : : {
4340 : 3900874 : ipa_predicate p;
4341 : 3900874 : next = e->next_callee;
4342 : :
4343 : 3900874 : if (e->inline_failed)
4344 : : {
4345 : 3276310 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4346 : 3276310 : remap_edge_params (inlined_edge, e);
4347 : :
4348 : 3276310 : if (es->predicate)
4349 : : {
4350 : 995601 : p = es->predicate->remap_after_inlining
4351 : 995601 : (info, params_summary,
4352 : : callee_info, operand_map,
4353 : : offset_map, possible_truths,
4354 : : *toplev_predicate);
4355 : 995601 : edge_set_predicate (e, &p);
4356 : : }
4357 : : else
4358 : 2280709 : edge_set_predicate (e, toplev_predicate);
4359 : : }
4360 : : else
4361 : 624564 : remap_edge_summaries (inlined_edge, e->callee, info,
4362 : : params_summary, callee_info,
4363 : : operand_map, offset_map, possible_truths,
4364 : : toplev_predicate);
4365 : : }
4366 : 4714157 : for (e = node->indirect_calls; e; e = next)
4367 : : {
4368 : 107554 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4369 : 107554 : ipa_predicate p;
4370 : 107554 : next = e->next_callee;
4371 : :
4372 : 107554 : remap_edge_params (inlined_edge, e);
4373 : 107554 : if (es->predicate)
4374 : : {
4375 : 25607 : p = es->predicate->remap_after_inlining
4376 : 25607 : (info, params_summary,
4377 : : callee_info, operand_map, offset_map,
4378 : : possible_truths, *toplev_predicate);
4379 : 25607 : edge_set_predicate (e, &p);
4380 : : }
4381 : : else
4382 : 81947 : edge_set_predicate (e, toplev_predicate);
4383 : : }
4384 : 4606603 : }
4385 : :
4386 : : /* Run remap_after_inlining on each predicate in V. */
4387 : :
4388 : : static void
4389 : 7964078 : remap_freqcounting_predicate (class ipa_fn_summary *info,
4390 : : class ipa_node_params *params_summary,
4391 : : class ipa_fn_summary *callee_info,
4392 : : vec<ipa_freqcounting_predicate, va_gc> *v,
4393 : : const vec<int> &operand_map,
4394 : : const vec<HOST_WIDE_INT> &offset_map,
4395 : : clause_t possible_truths,
4396 : : ipa_predicate *toplev_predicate)
4397 : :
4398 : : {
4399 : 7964078 : ipa_freqcounting_predicate *fcp;
4400 : 7992828 : for (int i = 0; vec_safe_iterate (v, i, &fcp); i++)
4401 : : {
4402 : 28750 : ipa_predicate p
4403 : 28750 : = fcp->predicate->remap_after_inlining (info, params_summary,
4404 : : callee_info, operand_map,
4405 : : offset_map, possible_truths,
4406 : : *toplev_predicate);
4407 : 39513 : if (p != false && p != true)
4408 : 2883 : *fcp->predicate &= p;
4409 : : }
4410 : 7964078 : }
4411 : :
4412 : : /* We inlined EDGE. Update summary of the function we inlined into. */
4413 : :
4414 : : void
4415 : 3982039 : ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
4416 : : {
4417 : 3982039 : ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
4418 : 3757013 : struct cgraph_node *to = (edge->caller->inlined_to
4419 : 3982039 : ? edge->caller->inlined_to : edge->caller);
4420 : 3982039 : class ipa_fn_summary *info = ipa_fn_summaries->get (to);
4421 : 3982039 : clause_t clause = 0; /* not_inline is known to be false. */
4422 : 3982039 : size_time_entry *e;
4423 : 3982039 : auto_vec<int, 8> operand_map;
4424 : 3982039 : auto_vec<HOST_WIDE_INT, 8> offset_map;
4425 : 3982039 : int i;
4426 : 3982039 : ipa_predicate toplev_predicate;
4427 : 3982039 : class ipa_call_summary *es = ipa_call_summaries->get (edge);
4428 : 3982039 : ipa_node_params *params_summary = (ipa_node_params_sum
4429 : 3982039 : ? ipa_node_params_sum->get (to) : NULL);
4430 : :
4431 : 3982039 : if (es->predicate)
4432 : 356986 : toplev_predicate = *es->predicate;
4433 : : else
4434 : 3625053 : toplev_predicate = true;
4435 : :
4436 : 3982039 : info->fp_expressions |= callee_info->fp_expressions;
4437 : 3982039 : info->target_info |= callee_info->target_info;
4438 : :
4439 : 3982039 : if (callee_info->conds)
4440 : : {
4441 : 2893860 : ipa_auto_call_arg_values avals;
4442 : 2893860 : evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false);
4443 : 2893860 : }
4444 : 3982039 : if (ipa_node_params_sum && callee_info->conds)
4445 : : {
4446 : 773613 : ipa_edge_args *args = ipa_edge_args_sum->get (edge);
4447 : 773613 : int count = args ? ipa_get_cs_argument_count (args) : 0;
4448 : 773482 : int i;
4449 : :
4450 : 773482 : if (count)
4451 : : {
4452 : 773482 : operand_map.safe_grow_cleared (count, true);
4453 : 773482 : offset_map.safe_grow_cleared (count, true);
4454 : : }
4455 : 2445032 : for (i = 0; i < count; i++)
4456 : : {
4457 : 1671419 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
4458 : 1671419 : int map = -1;
4459 : :
4460 : : /* TODO: handle non-NOPs when merging. */
4461 : 1671419 : if (jfunc->type == IPA_JF_PASS_THROUGH)
4462 : : {
4463 : 298481 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4464 : 295237 : map = ipa_get_jf_pass_through_formal_id (jfunc);
4465 : 298481 : if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
4466 : 201946 : offset_map[i] = -1;
4467 : : }
4468 : 1372938 : else if (jfunc->type == IPA_JF_ANCESTOR)
4469 : : {
4470 : 89210 : HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
4471 : 89210 : if (offset >= 0 && offset < INT_MAX)
4472 : : {
4473 : 89210 : map = ipa_get_jf_ancestor_formal_id (jfunc);
4474 : 89210 : if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
4475 : 55572 : offset = -1;
4476 : 89210 : offset_map[i] = offset;
4477 : : }
4478 : : }
4479 : 1671419 : operand_map[i] = map;
4480 : 3033183 : gcc_assert (map < ipa_get_param_count (params_summary));
4481 : : }
4482 : :
4483 : : int ip;
4484 : 777012 : for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++)
4485 : 3399 : if (ip < count && operand_map[ip] >= 0)
4486 : 33 : add_builtin_constant_p_parm (info, operand_map[ip]);
4487 : : }
4488 : 3982039 : sreal freq = edge->sreal_frequency ();
4489 : 21735438 : for (i = 0; callee_info->size_time_table.iterate (i, &e); i++)
4490 : : {
4491 : 17753399 : ipa_predicate p;
4492 : 17753399 : p = e->exec_predicate.remap_after_inlining
4493 : 17753399 : (info, params_summary,
4494 : : callee_info, operand_map,
4495 : : offset_map, clause,
4496 : : toplev_predicate);
4497 : 17753399 : ipa_predicate nonconstp;
4498 : 17753399 : nonconstp = e->nonconst_predicate.remap_after_inlining
4499 : 17753399 : (info, params_summary,
4500 : : callee_info, operand_map,
4501 : : offset_map, clause,
4502 : : toplev_predicate);
4503 : 26707423 : if (p != false && nonconstp != false)
4504 : : {
4505 : 8633466 : sreal add_time = ((sreal)e->time * freq);
4506 : 8633466 : int prob = e->nonconst_predicate.probability (callee_info->conds,
4507 : : clause, es->param);
4508 : 8633466 : if (prob != REG_BR_PROB_BASE)
4509 : 1116207 : add_time = add_time * prob / REG_BR_PROB_BASE;
4510 : 1116207 : if (prob != REG_BR_PROB_BASE
4511 : 1116207 : && dump_file && (dump_flags & TDF_DETAILS))
4512 : : {
4513 : 3 : fprintf (dump_file, "\t\tScaling time by probability:%f\n",
4514 : 3 : (double) prob / REG_BR_PROB_BASE);
4515 : : }
4516 : 8633466 : info->account_size_time (e->size, add_time, p, nonconstp);
4517 : : }
4518 : : }
4519 : 3982039 : remap_edge_summaries (edge, edge->callee, info, params_summary,
4520 : : callee_info, operand_map,
4521 : : offset_map, clause, &toplev_predicate);
4522 : 3982039 : remap_freqcounting_predicate (info, params_summary, callee_info,
4523 : : info->loop_iterations, operand_map,
4524 : : offset_map, clause, &toplev_predicate);
4525 : 3982039 : remap_freqcounting_predicate (info, params_summary, callee_info,
4526 : : info->loop_strides, operand_map,
4527 : : offset_map, clause, &toplev_predicate);
4528 : :
4529 : 3982039 : HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
4530 : 3982039 : HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
4531 : :
4532 : 3982039 : if (info->estimated_stack_size < peak)
4533 : 123421 : info->estimated_stack_size = peak;
4534 : :
4535 : 3982039 : inline_update_callee_summaries (edge->callee, es->loop_depth);
4536 : 3982039 : if (info->call_size_time_table.length ())
4537 : : {
4538 : 905447 : int edge_size = 0;
4539 : 905447 : sreal edge_time = 0;
4540 : :
4541 : 905447 : estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0);
4542 : : /* Unaccount size and time of the optimized out call. */
4543 : 905447 : info->account_size_time (-edge_size, -edge_time,
4544 : 905447 : es->predicate ? *es->predicate : true,
4545 : 905447 : es->predicate ? *es->predicate : true,
4546 : : true);
4547 : : /* Account new calls. */
4548 : 905447 : summarize_calls_size_and_time (edge->callee, info);
4549 : : }
4550 : :
4551 : : /* Free summaries that are not maintained for inline clones/edges. */
4552 : 3982039 : ipa_call_summaries->remove (edge);
4553 : 3982039 : ipa_fn_summaries->remove (edge->callee);
4554 : 3982039 : ipa_remove_from_growth_caches (edge);
4555 : 3982039 : }
4556 : :
4557 : : /* For performance reasons ipa_merge_fn_summary_after_inlining is not updating
4558 : : overall size and time. Recompute it.
4559 : : If RESET is true also recompute call_time_size_table. */
4560 : :
4561 : : void
4562 : 9367683 : ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset)
4563 : : {
4564 : 9367683 : class ipa_fn_summary *info = ipa_fn_summaries->get (node);
4565 : 9367683 : class ipa_size_summary *size_info = ipa_size_summaries->get (node);
4566 : 9367683 : size_time_entry *e;
4567 : 9367683 : int i;
4568 : :
4569 : 9367683 : size_info->size = 0;
4570 : 9367683 : info->time = 0;
4571 : 47366895 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
4572 : : {
4573 : 37999212 : size_info->size += e->size;
4574 : 37999212 : info->time += e->time;
4575 : : }
4576 : 9367683 : info->min_size = info->size_time_table[0].size;
4577 : 9367683 : if (reset)
4578 : 8493503 : info->call_size_time_table.release ();
4579 : 9367683 : if (node->callees || node->indirect_calls)
4580 : 7084715 : estimate_calls_size_and_time (node, &size_info->size, &info->min_size,
4581 : : &info->time, NULL,
4582 : : ~(clause_t) (1 << ipa_predicate::false_condition),
4583 : : NULL);
4584 : 9367683 : size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale);
4585 : 9367683 : info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale);
4586 : 9367683 : }
4587 : :
4588 : :
4589 : : /* This function performs intraprocedural analysis in NODE that is required to
4590 : : inline indirect calls. */
4591 : :
4592 : : static void
4593 : 1361958 : inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4594 : : {
4595 : 1361958 : ipa_analyze_node (node);
4596 : 1361958 : if (dump_file && (dump_flags & TDF_DETAILS))
4597 : : {
4598 : 14 : ipa_print_node_params (dump_file, node);
4599 : 14 : ipa_print_node_jump_functions (dump_file, node);
4600 : : }
4601 : 1361958 : }
4602 : :
4603 : :
4604 : : /* Note function body size. */
4605 : :
4606 : : void
4607 : 1374221 : inline_analyze_function (struct cgraph_node *node)
4608 : : {
4609 : 1374221 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4610 : :
4611 : 1374221 : if (dump_file)
4612 : 94 : fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ());
4613 : 1374221 : if (opt_for_fn (node->decl, optimize) && !node->thunk)
4614 : 1361958 : inline_indirect_intraprocedural_analysis (node);
4615 : 1374221 : compute_fn_summary (node, false);
4616 : 1374221 : if (!optimize)
4617 : : {
4618 : 10970 : struct cgraph_edge *e;
4619 : 23910 : for (e = node->callees; e; e = e->next_callee)
4620 : 12940 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4621 : 11041 : for (e = node->indirect_calls; e; e = e->next_callee)
4622 : 71 : e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4623 : : }
4624 : :
4625 : 1374221 : pop_cfun ();
4626 : 1374221 : }
4627 : :
4628 : :
4629 : : /* Called when new function is inserted to callgraph late. */
4630 : :
4631 : : void
4632 : 18720 : ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
4633 : : {
4634 : 18720 : inline_analyze_function (node);
4635 : 18720 : }
4636 : :
4637 : : /* Note function body size. */
4638 : :
4639 : : static void
4640 : 228611 : ipa_fn_summary_generate (void)
4641 : : {
4642 : 228611 : struct cgraph_node *node;
4643 : :
4644 : 2068684 : FOR_EACH_DEFINED_FUNCTION (node)
4645 : 1840073 : if (DECL_STRUCT_FUNCTION (node->decl))
4646 : 1827198 : node->versionable = tree_versionable_function_p (node->decl);
4647 : :
4648 : 228611 : ipa_fn_summary_alloc ();
4649 : :
4650 : 228611 : ipa_fn_summaries->enable_insertion_hook ();
4651 : :
4652 : 228611 : ipa_register_cgraph_hooks ();
4653 : :
4654 : 2068684 : FOR_EACH_DEFINED_FUNCTION (node)
4655 : 1840073 : if (!node->alias
4656 : 1840073 : && (flag_generate_lto || flag_generate_offload|| flag_wpa
4657 : 1652168 : || opt_for_fn (node->decl, optimize)))
4658 : 1337931 : inline_analyze_function (node);
4659 : 228611 : }
4660 : :
4661 : :
4662 : : /* Write inline summary for edge E to OB. */
4663 : :
4664 : : static void
4665 : 344422 : read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
4666 : : bool prevails)
4667 : : {
4668 : 344422 : class ipa_call_summary *es = prevails
4669 : 344422 : ? ipa_call_summaries->get_create (e) : NULL;
4670 : 344422 : ipa_predicate p;
4671 : 344422 : int length, i;
4672 : :
4673 : 344422 : int size = streamer_read_uhwi (ib);
4674 : 344422 : int time = streamer_read_uhwi (ib);
4675 : 344422 : int depth = streamer_read_uhwi (ib);
4676 : :
4677 : 344422 : if (es)
4678 : : {
4679 : 344373 : es->call_stmt_size = size;
4680 : 344373 : es->call_stmt_time = time;
4681 : 344373 : es->loop_depth = depth;
4682 : : }
4683 : :
4684 : 344422 : bitpack_d bp = streamer_read_bitpack (ib);
4685 : 344422 : if (es)
4686 : 344373 : es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
4687 : : else
4688 : 49 : bp_unpack_value (&bp, 1);
4689 : :
4690 : 344422 : p.stream_in (ib);
4691 : 344422 : if (es)
4692 : 344373 : edge_set_predicate (e, &p);
4693 : 344422 : length = streamer_read_uhwi (ib);
4694 : 344422 : if (length && es
4695 : 344422 : && (e->possibly_call_in_translation_unit_p ()
4696 : : /* Also stream in jump functions to builtins in hope that they
4697 : : will get fnspecs. */
4698 : 117794 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
4699 : : {
4700 : 230985 : es->param.safe_grow_cleared (length, true);
4701 : 796571 : for (i = 0; i < length; i++)
4702 : : {
4703 : 565586 : es->param[i].change_prob = streamer_read_uhwi (ib);
4704 : 565586 : bitpack_d bp = streamer_read_bitpack (ib);
4705 : 1696758 : es->param[i].points_to_local_or_readonly_memory
4706 : 565586 : = bp_unpack_value (&bp, 1);
4707 : 1696758 : es->param[i].points_to_possible_sra_candidate
4708 : 565586 : = bp_unpack_value (&bp, 1);
4709 : : }
4710 : : }
4711 : : else
4712 : : {
4713 : 134993 : for (i = 0; i < length; i++)
4714 : : {
4715 : 21556 : streamer_read_uhwi (ib);
4716 : 21556 : streamer_read_uhwi (ib);
4717 : : }
4718 : : }
4719 : 344422 : }
4720 : :
4721 : :
4722 : : /* Stream in inline summaries from the section. */
4723 : :
4724 : : static void
4725 : 13931 : inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4726 : : size_t len)
4727 : : {
4728 : 13931 : const struct lto_function_header *header =
4729 : : (const struct lto_function_header *) data;
4730 : 13931 : const int cfg_offset = sizeof (struct lto_function_header);
4731 : 13931 : const int main_offset = cfg_offset + header->cfg_size;
4732 : 13931 : const int string_offset = main_offset + header->main_size;
4733 : 13931 : class data_in *data_in;
4734 : 13931 : unsigned int i, count2, j;
4735 : 13931 : unsigned int f_count;
4736 : :
4737 : 13931 : lto_input_block ib ((const char *) data + main_offset, header->main_size,
4738 : 13931 : file_data);
4739 : :
4740 : 13931 : data_in =
4741 : 27862 : lto_data_in_create (file_data, (const char *) data + string_offset,
4742 : 13931 : header->string_size, vNULL);
4743 : 13931 : f_count = streamer_read_uhwi (&ib);
4744 : 99104 : for (i = 0; i < f_count; i++)
4745 : : {
4746 : 85173 : unsigned int index;
4747 : 85173 : struct cgraph_node *node;
4748 : 85173 : class ipa_fn_summary *info;
4749 : 85173 : class ipa_node_params *params_summary;
4750 : 85173 : class ipa_size_summary *size_info;
4751 : 85173 : lto_symtab_encoder_t encoder;
4752 : 85173 : struct bitpack_d bp;
4753 : 85173 : struct cgraph_edge *e;
4754 : 85173 : ipa_predicate p;
4755 : :
4756 : 85173 : index = streamer_read_uhwi (&ib);
4757 : 85173 : encoder = file_data->symtab_node_encoder;
4758 : 85173 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4759 : : index));
4760 : 85173 : info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
4761 : 85173 : params_summary = node->prevailing_p ()
4762 : 85173 : ? ipa_node_params_sum->get (node) : NULL;
4763 : 85173 : size_info = node->prevailing_p ()
4764 : 85173 : ? ipa_size_summaries->get_create (node) : NULL;
4765 : :
4766 : 85173 : int stack_size = streamer_read_uhwi (&ib);
4767 : 85173 : int size = streamer_read_uhwi (&ib);
4768 : 85173 : sreal time = sreal::stream_in (&ib);
4769 : :
4770 : 85173 : if (info)
4771 : : {
4772 : 85115 : info->estimated_stack_size
4773 : 85115 : = size_info->estimated_self_stack_size = stack_size;
4774 : 85115 : size_info->size = size_info->self_size = size;
4775 : 85115 : info->time = time;
4776 : : }
4777 : :
4778 : 85173 : bp = streamer_read_bitpack (&ib);
4779 : 85173 : if (info)
4780 : : {
4781 : 85115 : info->inlinable = bp_unpack_value (&bp, 1);
4782 : 85115 : info->fp_expressions = bp_unpack_value (&bp, 1);
4783 : 85115 : if (!lto_stream_offload_p)
4784 : 85115 : info->target_info = streamer_read_uhwi (&ib);
4785 : : }
4786 : : else
4787 : : {
4788 : 58 : bp_unpack_value (&bp, 1);
4789 : 58 : bp_unpack_value (&bp, 1);
4790 : 58 : if (!lto_stream_offload_p)
4791 : 58 : streamer_read_uhwi (&ib);
4792 : : }
4793 : :
4794 : 85173 : count2 = streamer_read_uhwi (&ib);
4795 : 85173 : gcc_assert (!info || !info->conds);
4796 : 85115 : if (info)
4797 : 85115 : vec_safe_reserve_exact (info->conds, count2);
4798 : 159226 : for (j = 0; j < count2; j++)
4799 : : {
4800 : 74053 : struct condition c;
4801 : 74053 : unsigned int k, count3;
4802 : 74053 : c.operand_num = streamer_read_uhwi (&ib);
4803 : 74053 : c.code = (enum tree_code) streamer_read_uhwi (&ib);
4804 : 74053 : c.type = stream_read_tree (&ib, data_in);
4805 : 74053 : c.val = stream_read_tree (&ib, data_in);
4806 : 74053 : bp = streamer_read_bitpack (&ib);
4807 : 74053 : c.agg_contents = bp_unpack_value (&bp, 1);
4808 : 74053 : c.by_ref = bp_unpack_value (&bp, 1);
4809 : 74053 : if (c.agg_contents)
4810 : 11324 : c.offset = streamer_read_uhwi (&ib);
4811 : 74053 : count3 = streamer_read_uhwi (&ib);
4812 : 74053 : c.param_ops = NULL;
4813 : 74053 : if (info)
4814 : 74053 : vec_safe_reserve_exact (c.param_ops, count3);
4815 : 74053 : if (params_summary)
4816 : 74053 : ipa_set_param_used_by_ipa_predicates
4817 : 74053 : (params_summary, c.operand_num, true);
4818 : 77521 : for (k = 0; k < count3; k++)
4819 : : {
4820 : 3468 : struct expr_eval_op op;
4821 : 3468 : enum gimple_rhs_class rhs_class;
4822 : 3468 : op.code = (enum tree_code) streamer_read_uhwi (&ib);
4823 : 3468 : op.type = stream_read_tree (&ib, data_in);
4824 : 3468 : switch (rhs_class = get_gimple_rhs_class (op.code))
4825 : : {
4826 : 1393 : case GIMPLE_UNARY_RHS:
4827 : 1393 : op.index = 0;
4828 : 1393 : op.val[0] = NULL_TREE;
4829 : 1393 : op.val[1] = NULL_TREE;
4830 : 1393 : break;
4831 : :
4832 : 2075 : case GIMPLE_BINARY_RHS:
4833 : 2075 : case GIMPLE_TERNARY_RHS:
4834 : 2075 : bp = streamer_read_bitpack (&ib);
4835 : 2075 : op.index = bp_unpack_value (&bp, 2);
4836 : 2075 : op.val[0] = stream_read_tree (&ib, data_in);
4837 : 2075 : if (rhs_class == GIMPLE_BINARY_RHS)
4838 : 2075 : op.val[1] = NULL_TREE;
4839 : : else
4840 : 0 : op.val[1] = stream_read_tree (&ib, data_in);
4841 : : break;
4842 : :
4843 : 0 : default:
4844 : 0 : fatal_error (UNKNOWN_LOCATION,
4845 : : "invalid fnsummary in LTO stream");
4846 : : }
4847 : 3468 : if (info)
4848 : 3468 : c.param_ops->quick_push (op);
4849 : : }
4850 : 74053 : if (info)
4851 : 74053 : info->conds->quick_push (c);
4852 : : }
4853 : 85173 : count2 = streamer_read_uhwi (&ib);
4854 : 85173 : gcc_assert (!info || !info->size_time_table.length ());
4855 : 85173 : if (info && count2)
4856 : 85115 : info->size_time_table.reserve_exact (count2);
4857 : 321262 : for (j = 0; j < count2; j++)
4858 : : {
4859 : 236089 : class size_time_entry e;
4860 : :
4861 : 236089 : e.size = streamer_read_uhwi (&ib);
4862 : 236089 : e.time = sreal::stream_in (&ib);
4863 : 236089 : e.exec_predicate.stream_in (&ib);
4864 : 236089 : e.nonconst_predicate.stream_in (&ib);
4865 : :
4866 : 236089 : if (info)
4867 : 235973 : info->size_time_table.quick_push (e);
4868 : : }
4869 : :
4870 : 85173 : count2 = streamer_read_uhwi (&ib);
4871 : 86410 : for (j = 0; j < count2; j++)
4872 : : {
4873 : 1237 : p.stream_in (&ib);
4874 : 1237 : sreal fcp_freq = sreal::stream_in (&ib);
4875 : 1237 : if (info)
4876 : : {
4877 : 1237 : ipa_freqcounting_predicate fcp;
4878 : 1237 : fcp.predicate = NULL;
4879 : 1237 : set_hint_predicate (&fcp.predicate, p);
4880 : 1237 : fcp.freq = fcp_freq;
4881 : 1237 : vec_safe_push (info->loop_iterations, fcp);
4882 : : }
4883 : : }
4884 : 85173 : count2 = streamer_read_uhwi (&ib);
4885 : 85457 : for (j = 0; j < count2; j++)
4886 : : {
4887 : 284 : p.stream_in (&ib);
4888 : 284 : sreal fcp_freq = sreal::stream_in (&ib);
4889 : 284 : if (info)
4890 : : {
4891 : 284 : ipa_freqcounting_predicate fcp;
4892 : 284 : fcp.predicate = NULL;
4893 : 284 : set_hint_predicate (&fcp.predicate, p);
4894 : 284 : fcp.freq = fcp_freq;
4895 : 284 : vec_safe_push (info->loop_strides, fcp);
4896 : : }
4897 : : }
4898 : 85173 : count2 = streamer_read_uhwi (&ib);
4899 : 85173 : if (info && count2)
4900 : 6 : info->builtin_constant_p_parms.reserve_exact (count2);
4901 : 85179 : for (j = 0; j < count2; j++)
4902 : : {
4903 : 6 : int parm = streamer_read_uhwi (&ib);
4904 : 6 : if (info)
4905 : 6 : info->builtin_constant_p_parms.quick_push (parm);
4906 : : }
4907 : 428140 : for (e = node->callees; e; e = e->next_callee)
4908 : 342967 : read_ipa_call_summary (&ib, e, info != NULL);
4909 : 86628 : for (e = node->indirect_calls; e; e = e->next_callee)
4910 : 1455 : read_ipa_call_summary (&ib, e, info != NULL);
4911 : : }
4912 : :
4913 : 13931 : lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4914 : : len);
4915 : 13931 : lto_data_in_delete (data_in);
4916 : 13931 : }
4917 : :
4918 : :
4919 : : /* Read inline summary. Jump functions are shared among ipa-cp
4920 : : and inliner, so when ipa-cp is active, we don't need to write them
4921 : : twice. */
4922 : :
4923 : : static void
4924 : 12898 : ipa_fn_summary_read (void)
4925 : : {
4926 : 12898 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4927 : 12898 : struct lto_file_decl_data *file_data;
4928 : 12898 : unsigned int j = 0;
4929 : :
4930 : 12898 : ipa_prop_read_jump_functions ();
4931 : 12898 : ipa_fn_summary_alloc ();
4932 : :
4933 : 39727 : while ((file_data = file_data_vec[j++]))
4934 : : {
4935 : 13931 : size_t len;
4936 : 13931 : const char *data
4937 : 13931 : = lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary,
4938 : : &len);
4939 : 13931 : if (data)
4940 : 13931 : inline_read_section (file_data, data, len);
4941 : : else
4942 : : /* Fatal error here. We do not want to support compiling ltrans units
4943 : : with different version of compiler or different flags than the WPA
4944 : : unit, so this should never happen. */
4945 : 0 : fatal_error (input_location,
4946 : : "ipa inline summary is missing in input file");
4947 : : }
4948 : 12898 : ipa_register_cgraph_hooks ();
4949 : :
4950 : 12898 : gcc_assert (ipa_fn_summaries);
4951 : 12898 : ipa_fn_summaries->enable_insertion_hook ();
4952 : 12898 : }
4953 : :
4954 : :
4955 : : /* Write inline summary for edge E to OB. */
4956 : :
4957 : : static void
4958 : 374274 : write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
4959 : : {
4960 : 374274 : class ipa_call_summary *es = ipa_call_summaries->get (e);
4961 : 374274 : int i;
4962 : :
4963 : 374274 : streamer_write_uhwi (ob, es->call_stmt_size);
4964 : 374274 : streamer_write_uhwi (ob, es->call_stmt_time);
4965 : 374274 : streamer_write_uhwi (ob, es->loop_depth);
4966 : :
4967 : 374274 : bitpack_d bp = bitpack_create (ob->main_stream);
4968 : 374274 : bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
4969 : 374274 : streamer_write_bitpack (&bp);
4970 : :
4971 : 374274 : if (es->predicate)
4972 : 9908 : es->predicate->stream_out (ob);
4973 : : else
4974 : 364366 : streamer_write_uhwi (ob, 0);
4975 : 374274 : streamer_write_uhwi (ob, es->param.length ());
4976 : 2264162 : for (i = 0; i < (int) es->param.length (); i++)
4977 : : {
4978 : 625551 : streamer_write_uhwi (ob, es->param[i].change_prob);
4979 : 625551 : bp = bitpack_create (ob->main_stream);
4980 : 625551 : bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1);
4981 : 625551 : bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1);
4982 : 625551 : streamer_write_bitpack (&bp);
4983 : : }
4984 : 374274 : }
4985 : :
4986 : :
4987 : : /* Write inline summary for node in SET.
4988 : : Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4989 : : active, we don't need to write them twice. */
4990 : :
4991 : : static void
4992 : 23852 : ipa_fn_summary_write (void)
4993 : : {
4994 : 23852 : struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
4995 : 23852 : lto_symtab_encoder_iterator lsei;
4996 : 23852 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4997 : 23852 : unsigned int count = 0;
4998 : :
4999 : 127720 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5000 : 103868 : lsei_next_function_in_partition (&lsei))
5001 : : {
5002 : 103868 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5003 : 103868 : if (cnode->definition && !cnode->alias)
5004 : 101388 : count++;
5005 : : }
5006 : 23852 : streamer_write_uhwi (ob, count);
5007 : :
5008 : 127720 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5009 : 103868 : lsei_next_function_in_partition (&lsei))
5010 : : {
5011 : 103868 : cgraph_node *cnode = lsei_cgraph_node (lsei);
5012 : 103868 : if (cnode->definition && !cnode->alias)
5013 : : {
5014 : 101388 : class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
5015 : 101388 : class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
5016 : 101388 : struct bitpack_d bp;
5017 : 101388 : struct cgraph_edge *edge;
5018 : 101388 : int i;
5019 : 101388 : size_time_entry *e;
5020 : 101388 : struct condition *c;
5021 : :
5022 : 101388 : streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
5023 : 101388 : streamer_write_hwi (ob, size_info->estimated_self_stack_size);
5024 : 101388 : streamer_write_hwi (ob, size_info->self_size);
5025 : 101388 : info->time.stream_out (ob);
5026 : 101388 : bp = bitpack_create (ob->main_stream);
5027 : 101388 : bp_pack_value (&bp, info->inlinable, 1);
5028 : 101388 : bp_pack_value (&bp, info->fp_expressions, 1);
5029 : 101388 : streamer_write_bitpack (&bp);
5030 : 101388 : if (!lto_stream_offload_p)
5031 : 101388 : streamer_write_uhwi (ob, info->target_info);
5032 : 101388 : streamer_write_uhwi (ob, vec_safe_length (info->conds));
5033 : 190378 : for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
5034 : : {
5035 : 88990 : int j;
5036 : 88990 : struct expr_eval_op *op;
5037 : :
5038 : 88990 : streamer_write_uhwi (ob, c->operand_num);
5039 : 88990 : streamer_write_uhwi (ob, c->code);
5040 : 88990 : stream_write_tree (ob, c->type, true);
5041 : 88990 : stream_write_tree (ob, c->val, true);
5042 : 88990 : bp = bitpack_create (ob->main_stream);
5043 : 88990 : bp_pack_value (&bp, c->agg_contents, 1);
5044 : 88990 : bp_pack_value (&bp, c->by_ref, 1);
5045 : 88990 : streamer_write_bitpack (&bp);
5046 : 88990 : if (c->agg_contents)
5047 : 14273 : streamer_write_uhwi (ob, c->offset);
5048 : 88990 : streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
5049 : 184815 : for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
5050 : : {
5051 : 4008 : streamer_write_uhwi (ob, op->code);
5052 : 4008 : stream_write_tree (ob, op->type, true);
5053 : 4008 : if (op->val[0])
5054 : : {
5055 : 2406 : bp = bitpack_create (ob->main_stream);
5056 : 2406 : bp_pack_value (&bp, op->index, 2);
5057 : 2406 : streamer_write_bitpack (&bp);
5058 : 2406 : stream_write_tree (ob, op->val[0], true);
5059 : 2406 : if (op->val[1])
5060 : 4 : stream_write_tree (ob, op->val[1], true);
5061 : : }
5062 : : }
5063 : : }
5064 : 101388 : streamer_write_uhwi (ob, info->size_time_table.length ());
5065 : 487077 : for (i = 0; info->size_time_table.iterate (i, &e); i++)
5066 : : {
5067 : 284301 : streamer_write_uhwi (ob, e->size);
5068 : 284301 : e->time.stream_out (ob);
5069 : 284301 : e->exec_predicate.stream_out (ob);
5070 : 284301 : e->nonconst_predicate.stream_out (ob);
5071 : : }
5072 : 101388 : ipa_freqcounting_predicate *fcp;
5073 : 101388 : streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations));
5074 : 204579 : for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++)
5075 : : {
5076 : 1803 : fcp->predicate->stream_out (ob);
5077 : 1803 : fcp->freq.stream_out (ob);
5078 : : }
5079 : 101388 : streamer_write_uhwi (ob, vec_safe_length (info->loop_strides));
5080 : 203109 : for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++)
5081 : : {
5082 : 333 : fcp->predicate->stream_out (ob);
5083 : 333 : fcp->freq.stream_out (ob);
5084 : : }
5085 : 101388 : streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ());
5086 : 101388 : int ip;
5087 : 202790 : for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip);
5088 : : i++)
5089 : 14 : streamer_write_uhwi (ob, ip);
5090 : 473788 : for (edge = cnode->callees; edge; edge = edge->next_callee)
5091 : 372400 : write_ipa_call_summary (ob, edge);
5092 : 103262 : for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
5093 : 1874 : write_ipa_call_summary (ob, edge);
5094 : : }
5095 : : }
5096 : 23852 : streamer_write_char_stream (ob->main_stream, 0);
5097 : 23852 : produce_asm (ob);
5098 : 23852 : destroy_output_block (ob);
5099 : :
5100 : 23852 : ipa_prop_write_jump_functions ();
5101 : 23852 : }
5102 : :
5103 : :
5104 : : /* Release function summary. */
5105 : :
5106 : : void
5107 : 714239 : ipa_free_fn_summary (void)
5108 : : {
5109 : 714239 : if (!ipa_call_summaries)
5110 : : return;
5111 : 452299 : ggc_delete (ipa_fn_summaries);
5112 : 452299 : ipa_fn_summaries = NULL;
5113 : 452299 : delete ipa_call_summaries;
5114 : 452299 : ipa_call_summaries = NULL;
5115 : 452299 : edge_predicate_pool.release ();
5116 : : /* During IPA this is one of largest datastructures to release. */
5117 : 452299 : if (flag_wpa)
5118 : 8633 : ggc_trim ();
5119 : : }
5120 : :
5121 : : /* Release function summary. */
5122 : :
5123 : : void
5124 : 714239 : ipa_free_size_summary (void)
5125 : : {
5126 : 714239 : if (!ipa_size_summaries)
5127 : : return;
5128 : 452299 : delete ipa_size_summaries;
5129 : 452299 : ipa_size_summaries = NULL;
5130 : : }
5131 : :
5132 : : namespace {
5133 : :
5134 : : const pass_data pass_data_local_fn_summary =
5135 : : {
5136 : : GIMPLE_PASS, /* type */
5137 : : "local-fnsummary", /* name */
5138 : : OPTGROUP_INLINE, /* optinfo_flags */
5139 : : TV_INLINE_PARAMETERS, /* tv_id */
5140 : : 0, /* properties_required */
5141 : : 0, /* properties_provided */
5142 : : 0, /* properties_destroyed */
5143 : : 0, /* todo_flags_start */
5144 : : 0, /* todo_flags_finish */
5145 : : };
5146 : :
5147 : : class pass_local_fn_summary : public gimple_opt_pass
5148 : : {
5149 : : public:
5150 : 570162 : pass_local_fn_summary (gcc::context *ctxt)
5151 : 1140324 : : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
5152 : : {}
5153 : :
5154 : : /* opt_pass methods: */
5155 : 285081 : opt_pass * clone () final override
5156 : : {
5157 : 285081 : return new pass_local_fn_summary (m_ctxt);
5158 : : }
5159 : 5751617 : unsigned int execute (function *) final override
5160 : : {
5161 : 5751617 : return compute_fn_summary_for_current ();
5162 : : }
5163 : :
5164 : : }; // class pass_local_fn_summary
5165 : :
5166 : : } // anon namespace
5167 : :
5168 : : gimple_opt_pass *
5169 : 285081 : make_pass_local_fn_summary (gcc::context *ctxt)
5170 : : {
5171 : 285081 : return new pass_local_fn_summary (ctxt);
5172 : : }
5173 : :
5174 : :
5175 : : /* Free inline summary. */
5176 : :
5177 : : namespace {
5178 : :
5179 : : const pass_data pass_data_ipa_free_fn_summary =
5180 : : {
5181 : : SIMPLE_IPA_PASS, /* type */
5182 : : "free-fnsummary", /* name */
5183 : : OPTGROUP_NONE, /* optinfo_flags */
5184 : : TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
5185 : : 0, /* properties_required */
5186 : : 0, /* properties_provided */
5187 : : 0, /* properties_destroyed */
5188 : : 0, /* todo_flags_start */
5189 : : 0, /* todo_flags_finish */
5190 : : };
5191 : :
5192 : : class pass_ipa_free_fn_summary : public simple_ipa_opt_pass
5193 : : {
5194 : : public:
5195 : 570162 : pass_ipa_free_fn_summary (gcc::context *ctxt)
5196 : 570162 : : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
5197 : 1140324 : small_p (false)
5198 : : {}
5199 : :
5200 : : /* opt_pass methods: */
5201 : 285081 : opt_pass *clone () final override
5202 : : {
5203 : 285081 : return new pass_ipa_free_fn_summary (m_ctxt);
5204 : : }
5205 : 570162 : void set_pass_param (unsigned int n, bool param) final override
5206 : : {
5207 : 570162 : gcc_assert (n == 0);
5208 : 570162 : small_p = param;
5209 : 570162 : }
5210 : 478929 : bool gate (function *) final override { return true; }
5211 : 457027 : unsigned int execute (function *) final override
5212 : : {
5213 : 457027 : ipa_free_fn_summary ();
5214 : : /* Free ipa-prop structures if they are no longer needed. */
5215 : 457027 : ipa_free_all_structures_after_iinln ();
5216 : 457027 : if (!flag_wpa)
5217 : 448394 : ipa_free_size_summary ();
5218 : 457027 : return 0;
5219 : : }
5220 : :
5221 : : private:
5222 : : bool small_p;
5223 : : }; // class pass_ipa_free_fn_summary
5224 : :
5225 : : } // anon namespace
5226 : :
5227 : : simple_ipa_opt_pass *
5228 : 285081 : make_pass_ipa_free_fn_summary (gcc::context *ctxt)
5229 : : {
5230 : 285081 : return new pass_ipa_free_fn_summary (ctxt);
5231 : : }
5232 : :
5233 : : namespace {
5234 : :
5235 : : const pass_data pass_data_ipa_fn_summary =
5236 : : {
5237 : : IPA_PASS, /* type */
5238 : : "fnsummary", /* name */
5239 : : OPTGROUP_INLINE, /* optinfo_flags */
5240 : : TV_IPA_FNSUMMARY, /* tv_id */
5241 : : 0, /* properties_required */
5242 : : 0, /* properties_provided */
5243 : : 0, /* properties_destroyed */
5244 : : 0, /* todo_flags_start */
5245 : : ( TODO_dump_symtab ), /* todo_flags_finish */
5246 : : };
5247 : :
5248 : : class pass_ipa_fn_summary : public ipa_opt_pass_d
5249 : : {
5250 : : public:
5251 : 285081 : pass_ipa_fn_summary (gcc::context *ctxt)
5252 : : : ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt,
5253 : : ipa_fn_summary_generate, /* generate_summary */
5254 : : ipa_fn_summary_write, /* write_summary */
5255 : : ipa_fn_summary_read, /* read_summary */
5256 : : NULL, /* write_optimization_summary */
5257 : : NULL, /* read_optimization_summary */
5258 : : NULL, /* stmt_fixup */
5259 : : 0, /* function_transform_todo_flags_start */
5260 : : NULL, /* function_transform */
5261 : 285081 : NULL) /* variable_transform */
5262 : 285081 : {}
5263 : :
5264 : : /* opt_pass methods: */
5265 : 228273 : unsigned int execute (function *) final override { return 0; }
5266 : :
5267 : : }; // class pass_ipa_fn_summary
5268 : :
5269 : : } // anon namespace
5270 : :
5271 : : ipa_opt_pass_d *
5272 : 285081 : make_pass_ipa_fn_summary (gcc::context *ctxt)
5273 : : {
5274 : 285081 : return new pass_ipa_fn_summary (ctxt);
5275 : : }
5276 : :
5277 : : /* Reset all state within ipa-fnsummary.cc so that we can rerun the compiler
5278 : : within the same process. For use by toplev::finalize. */
5279 : :
5280 : : void
5281 : 256374 : ipa_fnsummary_cc_finalize (void)
5282 : : {
5283 : 256374 : ipa_free_fn_summary ();
5284 : 256374 : ipa_free_size_summary ();
5285 : 256374 : }
|