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