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