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