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