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