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