]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-fnsummary.c
Fortran] OpenACC – libgomp/testsuite – use 'stop' and 'dg-do run'
[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
473 if (e->caller->global.inlined_to)
e5cf5e11 474 caller_parms_info = IPA_NODE_REF (e->caller->global.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
JH
748 }
749 if (!dst->global.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)
930 if (!node->global.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
JH
2692
2693 gcc_assert (!node->global.inlined_to);
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
2715 node->local.can_change_signature = false;
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)))
27d020cf
JH
2754 node->local.can_change_signature = false;
2755 else
2756 {
2757 /* Otherwise, inlinable functions always can change signature. */
2758 if (info->inlinable)
2759 node->local.can_change_signature = true;
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 }
2770 node->local.can_change_signature = !e;
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
2943
2944/* Estimate size and time needed to execute NODE assuming
2945 POSSIBLE_TRUTHS clause, and KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
2946 information about NODE's arguments. If non-NULL use also probability
2947 information present in INLINE_PARAM_SUMMARY vector.
2948 Additionally detemine hints determined by the context. Finally compute
2949 minimal size needed for the call that is independent on the call context and
2950 can be used for fast estimates. Return the values in RET_SIZE,
2951 RET_MIN_SIZE, RET_TIME and RET_HINTS. */
2952
2953void
2954estimate_node_size_and_time (struct cgraph_node *node,
2955 clause_t possible_truths,
2956 clause_t nonspec_possible_truths,
2957 vec<tree> known_vals,
2958 vec<ipa_polymorphic_call_context> known_contexts,
2959 vec<ipa_agg_jump_function_p> known_aggs,
2960 int *ret_size, int *ret_min_size,
2961 sreal *ret_time,
2962 sreal *ret_nonspecialized_time,
0bceb671 2963 ipa_hints *ret_hints,
27d020cf
JH
2964 vec<inline_param_summary>
2965 inline_param_summary)
2966{
99b1c316 2967 class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
27d020cf
JH
2968 size_time_entry *e;
2969 int size = 0;
2970 sreal time = 0;
2971 int min_size = 0;
0bceb671 2972 ipa_hints hints = 0;
27d020cf
JH
2973 int i;
2974
2975 if (dump_file && (dump_flags & TDF_DETAILS))
2976 {
2977 bool found = false;
2978 fprintf (dump_file, " Estimating body: %s/%i\n"
2979 " Known to be false: ", node->name (),
2980 node->order);
2981
2982 for (i = predicate::not_inlined_condition;
2983 i < (predicate::first_dynamic_condition
2984 + (int) vec_safe_length (info->conds)); i++)
2985 if (!(possible_truths & (1 << i)))
2986 {
2987 if (found)
2988 fprintf (dump_file, ", ");
2989 found = true;
2990 dump_condition (dump_file, info->conds, i);
2991 }
2992 }
2993
2994 estimate_calls_size_and_time (node, &size, &min_size, &time, &hints, possible_truths,
2995 known_vals, known_contexts, known_aggs);
2996 sreal nonspecialized_time = time;
2997
2998 for (i = 0; vec_safe_iterate (info->size_time_table, i, &e); i++)
2999 {
27d020cf 3000 bool exec = e->exec_predicate.evaluate (nonspec_possible_truths);
3494e738
JH
3001
3002 /* Because predicates are conservative, it can happen that nonconst is 1
3003 but exec is 0. */
27d020cf
JH
3004 if (exec)
3005 {
3494e738
JH
3006 bool nonconst = e->nonconst_predicate.evaluate (possible_truths);
3007
27d020cf
JH
3008 gcc_checking_assert (e->time >= 0);
3009 gcc_checking_assert (time >= 0);
3010
3011 /* We compute specialized size only because size of nonspecialized
3012 copy is context independent.
3013
3014 The difference between nonspecialized execution and specialized is
3015 that nonspecialized is not going to have optimized out computations
3016 known to be constant in a specialized setting. */
3017 if (nonconst)
3018 size += e->size;
3019 nonspecialized_time += e->time;
3020 if (!nonconst)
3021 ;
3022 else if (!inline_param_summary.exists ())
3023 {
3024 if (nonconst)
3025 time += e->time;
3026 }
3027 else
3028 {
3029 int prob = e->nonconst_predicate.probability
3030 (info->conds, possible_truths,
3031 inline_param_summary);
3032 gcc_checking_assert (prob >= 0);
3033 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3034 time += e->time * prob / REG_BR_PROB_BASE;
3035 }
3036 gcc_checking_assert (time >= 0);
3037 }
3038 }
3039 gcc_checking_assert ((*info->size_time_table)[0].exec_predicate == true);
3040 gcc_checking_assert ((*info->size_time_table)[0].nonconst_predicate == true);
3041 min_size = (*info->size_time_table)[0].size;
3042 gcc_checking_assert (size >= 0);
3043 gcc_checking_assert (time >= 0);
3044 /* nonspecialized_time should be always bigger than specialized time.
3045 Roundoff issues however may get into the way. */
59d27026 3046 gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1);
27d020cf
JH
3047
3048 /* Roundoff issues may make specialized time bigger than nonspecialized
3049 time. We do not really want that to happen because some heurstics
3050 may get confused by seeing negative speedups. */
3051 if (time > nonspecialized_time)
3052 time = nonspecialized_time;
3053
3054 if (info->loop_iterations
3055 && !info->loop_iterations->evaluate (possible_truths))
3056 hints |= INLINE_HINT_loop_iterations;
3057 if (info->loop_stride
3058 && !info->loop_stride->evaluate (possible_truths))
3059 hints |= INLINE_HINT_loop_stride;
27d020cf
JH
3060 if (info->scc_no)
3061 hints |= INLINE_HINT_in_scc;
3062 if (DECL_DECLARED_INLINE_P (node->decl))
3063 hints |= INLINE_HINT_declared_inline;
3064
0bceb671
JH
3065 size = RDIV (size, ipa_fn_summary::size_scale);
3066 min_size = RDIV (min_size, ipa_fn_summary::size_scale);
27d020cf
JH
3067
3068 if (dump_file && (dump_flags & TDF_DETAILS))
3069 fprintf (dump_file, "\n size:%i time:%f nonspec time:%f\n", (int) size,
3070 time.to_double (), nonspecialized_time.to_double ());
3071 if (ret_time)
3072 *ret_time = time;
3073 if (ret_nonspecialized_time)
3074 *ret_nonspecialized_time = nonspecialized_time;
3075 if (ret_size)
3076 *ret_size = size;
3077 if (ret_min_size)
3078 *ret_min_size = min_size;
3079 if (ret_hints)
3080 *ret_hints = hints;
3081 return;
3082}
3083
3084
3085/* Estimate size and time needed to execute callee of EDGE assuming that
3086 parameters known to be constant at caller of EDGE are propagated.
3087 KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
3088 and types for parameters. */
3089
3090void
3091estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3092 vec<tree> known_vals,
3093 vec<ipa_polymorphic_call_context>
3094 known_contexts,
3095 vec<ipa_agg_jump_function_p> known_aggs,
3096 int *ret_size, sreal *ret_time,
3097 sreal *ret_nonspec_time,
0bceb671 3098 ipa_hints *hints)
27d020cf
JH
3099{
3100 clause_t clause, nonspec_clause;
3101
3102 evaluate_conditions_for_known_args (node, false, known_vals, known_aggs,
3103 &clause, &nonspec_clause);
3104 estimate_node_size_and_time (node, clause, nonspec_clause,
3105 known_vals, known_contexts,
3106 known_aggs, ret_size, NULL, ret_time,
3107 ret_nonspec_time, hints, vNULL);
3108}
3109
f658ad30
JH
3110/* Return stack frame offset where frame of NODE is supposed to start inside
3111 of the function it is inlined to.
3112 Return 0 for functions that are not inlined. */
3113
3114HOST_WIDE_INT
3115ipa_get_stack_frame_offset (struct cgraph_node *node)
3116{
3117 HOST_WIDE_INT offset = 0;
3118 if (!node->global.inlined_to)
3119 return 0;
3120 node = node->callers->caller;
3121 while (true)
3122 {
3123 offset += ipa_size_summaries->get (node)->estimated_self_stack_size;
3124 if (!node->global.inlined_to)
3125 return offset;
3126 node = node->callers->caller;
3127 }
3128}
3129
27d020cf
JH
3130
3131/* Update summary information of inline clones after inlining.
3132 Compute peak stack usage. */
3133
3134static void
3135inline_update_callee_summaries (struct cgraph_node *node, int depth)
3136{
3137 struct cgraph_edge *e;
f658ad30 3138
27d020cf
JH
3139 ipa_propagate_frequency (node);
3140 for (e = node->callees; e; e = e->next_callee)
3141 {
3142 if (!e->inline_failed)
3143 inline_update_callee_summaries (e->callee, depth);
56f62793 3144 ipa_call_summaries->get (e)->loop_depth += depth;
27d020cf
JH
3145 }
3146 for (e = node->indirect_calls; e; e = e->next_callee)
56f62793 3147 ipa_call_summaries->get (e)->loop_depth += depth;
27d020cf
JH
3148}
3149
3150/* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3151 When functoin A is inlined in B and A calls C with parameter that
3152 changes with probability PROB1 and C is known to be passthroug
3153 of argument if B that change with probability PROB2, the probability
3154 of change is now PROB1*PROB2. */
3155
3156static void
3157remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3158 struct cgraph_edge *edge)
3159{
3160 if (ipa_node_params_sum)
3161 {
3162 int i;
99b1c316 3163 class ipa_edge_args *args = IPA_EDGE_REF (edge);
a33c028e
JH
3164 if (!args)
3165 return;
99b1c316
MS
3166 class ipa_call_summary *es = ipa_call_summaries->get (edge);
3167 class ipa_call_summary *inlined_es
56f62793 3168 = ipa_call_summaries->get (inlined_edge);
27d020cf 3169
8c02e054
JH
3170 if (es->param.length () == 0)
3171 return;
3172
27d020cf
JH
3173 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3174 {
3175 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3176 if (jfunc->type == IPA_JF_PASS_THROUGH
3177 || jfunc->type == IPA_JF_ANCESTOR)
3178 {
3179 int id = jfunc->type == IPA_JF_PASS_THROUGH
3180 ? ipa_get_jf_pass_through_formal_id (jfunc)
3181 : ipa_get_jf_ancestor_formal_id (jfunc);
3182 if (id < (int) inlined_es->param.length ())
3183 {
3184 int prob1 = es->param[i].change_prob;
3185 int prob2 = inlined_es->param[id].change_prob;
3186 int prob = combine_probabilities (prob1, prob2);
3187
3188 if (prob1 && prob2 && !prob)
3189 prob = 1;
3190
3191 es->param[i].change_prob = prob;
3192 }
3193 }
3194 }
3195 }
3196}
3197
3198/* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3199
3200 Remap predicates of callees of NODE. Rest of arguments match
3201 remap_predicate.
3202
3203 Also update change probabilities. */
3204
3205static void
3206remap_edge_summaries (struct cgraph_edge *inlined_edge,
3207 struct cgraph_node *node,
99b1c316
MS
3208 class ipa_fn_summary *info,
3209 class ipa_fn_summary *callee_info,
27d020cf
JH
3210 vec<int> operand_map,
3211 vec<int> offset_map,
3212 clause_t possible_truths,
3213 predicate *toplev_predicate)
3214{
3215 struct cgraph_edge *e, *next;
3216 for (e = node->callees; e; e = next)
3217 {
99b1c316 3218 class ipa_call_summary *es = ipa_call_summaries->get (e);
27d020cf
JH
3219 predicate p;
3220 next = e->next_callee;
3221
3222 if (e->inline_failed)
3223 {
3224 remap_edge_change_prob (inlined_edge, e);
3225
3226 if (es->predicate)
3227 {
3228 p = es->predicate->remap_after_inlining
3229 (info, callee_info, operand_map,
3230 offset_map, possible_truths,
3231 *toplev_predicate);
3232 edge_set_predicate (e, &p);
3233 }
3234 else
3235 edge_set_predicate (e, toplev_predicate);
3236 }
3237 else
3238 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3239 operand_map, offset_map, possible_truths,
3240 toplev_predicate);
3241 }
3242 for (e = node->indirect_calls; e; e = next)
3243 {
99b1c316 3244 class ipa_call_summary *es = ipa_call_summaries->get (e);
27d020cf
JH
3245 predicate p;
3246 next = e->next_callee;
3247
3248 remap_edge_change_prob (inlined_edge, e);
3249 if (es->predicate)
3250 {
3251 p = es->predicate->remap_after_inlining
3252 (info, callee_info, operand_map, offset_map,
3253 possible_truths, *toplev_predicate);
3254 edge_set_predicate (e, &p);
3255 }
3256 else
3257 edge_set_predicate (e, toplev_predicate);
3258 }
3259}
3260
3261/* Same as remap_predicate, but set result into hint *HINT. */
3262
3263static void
99b1c316
MS
3264remap_hint_predicate (class ipa_fn_summary *info,
3265 class ipa_fn_summary *callee_info,
27d020cf
JH
3266 predicate **hint,
3267 vec<int> operand_map,
3268 vec<int> offset_map,
3269 clause_t possible_truths,
3270 predicate *toplev_predicate)
3271{
3272 predicate p;
3273
3274 if (!*hint)
3275 return;
3276 p = (*hint)->remap_after_inlining
3277 (info, callee_info,
3278 operand_map, offset_map,
3279 possible_truths, *toplev_predicate);
3280 if (p != false && p != true)
3281 {
3282 if (!*hint)
3283 set_hint_predicate (hint, p);
3284 else
3285 **hint &= p;
3286 }
3287}
3288
3289/* We inlined EDGE. Update summary of the function we inlined into. */
3290
3291void
0bceb671 3292ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge)
27d020cf 3293{
56f62793 3294 ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee);
27d020cf
JH
3295 struct cgraph_node *to = (edge->caller->global.inlined_to
3296 ? edge->caller->global.inlined_to : edge->caller);
99b1c316 3297 class ipa_fn_summary *info = ipa_fn_summaries->get (to);
27d020cf
JH
3298 clause_t clause = 0; /* not_inline is known to be false. */
3299 size_time_entry *e;
f658ad30
JH
3300 auto_vec<int, 8> operand_map;
3301 auto_vec<int, 8> offset_map;
27d020cf
JH
3302 int i;
3303 predicate toplev_predicate;
99b1c316 3304 class ipa_call_summary *es = ipa_call_summaries->get (edge);
27d020cf
JH
3305
3306 if (es->predicate)
3307 toplev_predicate = *es->predicate;
3308 else
3309 toplev_predicate = true;
3310
3311 info->fp_expressions |= callee_info->fp_expressions;
3312
3313 if (callee_info->conds)
3314 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL, NULL);
3315 if (ipa_node_params_sum && callee_info->conds)
3316 {
99b1c316 3317 class ipa_edge_args *args = IPA_EDGE_REF (edge);
27d020cf
JH
3318 int count = ipa_get_cs_argument_count (args);
3319 int i;
3320
3321 if (count)
3322 {
3323 operand_map.safe_grow_cleared (count);
3324 offset_map.safe_grow_cleared (count);
3325 }
3326 for (i = 0; i < count; i++)
3327 {
3328 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3329 int map = -1;
3330
3331 /* TODO: handle non-NOPs when merging. */
3332 if (jfunc->type == IPA_JF_PASS_THROUGH)
3333 {
3334 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3335 map = ipa_get_jf_pass_through_formal_id (jfunc);
3336 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3337 offset_map[i] = -1;
3338 }
3339 else if (jfunc->type == IPA_JF_ANCESTOR)
3340 {
3341 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3342 if (offset >= 0 && offset < INT_MAX)
3343 {
3344 map = ipa_get_jf_ancestor_formal_id (jfunc);
3345 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3346 offset = -1;
3347 offset_map[i] = offset;
3348 }
3349 }
3350 operand_map[i] = map;
3351 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3352 }
3353 }
3354 for (i = 0; vec_safe_iterate (callee_info->size_time_table, i, &e); i++)
3355 {
3356 predicate p;
3357 p = e->exec_predicate.remap_after_inlining
3358 (info, callee_info, operand_map,
3359 offset_map, clause,
3360 toplev_predicate);
3361 predicate nonconstp;
3362 nonconstp = e->nonconst_predicate.remap_after_inlining
3363 (info, callee_info, operand_map,
3364 offset_map, clause,
3365 toplev_predicate);
3366 if (p != false && nonconstp != false)
3367 {
41f0e819 3368 sreal add_time = ((sreal)e->time * edge->sreal_frequency ());
27d020cf
JH
3369 int prob = e->nonconst_predicate.probability (callee_info->conds,
3370 clause, es->param);
3371 add_time = add_time * prob / REG_BR_PROB_BASE;
3372 if (prob != REG_BR_PROB_BASE
3373 && dump_file && (dump_flags & TDF_DETAILS))
3374 {
3375 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3376 (double) prob / REG_BR_PROB_BASE);
3377 }
3378 info->account_size_time (e->size, add_time, p, nonconstp);
3379 }
3380 }
3381 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3382 offset_map, clause, &toplev_predicate);
3383 remap_hint_predicate (info, callee_info,
3384 &callee_info->loop_iterations,
3385 operand_map, offset_map, clause, &toplev_predicate);
3386 remap_hint_predicate (info, callee_info,
3387 &callee_info->loop_stride,
3388 operand_map, offset_map, clause, &toplev_predicate);
27d020cf 3389
f658ad30
JH
3390 HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee);
3391 HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size;
27d020cf 3392
f658ad30
JH
3393 if (info->estimated_stack_size < peak)
3394 info->estimated_stack_size = peak;
3395
3396 inline_update_callee_summaries (edge->callee, es->loop_depth);
3397
3398 /* Free summaries that are not maintained for inline clones/edges. */
3399 ipa_call_summaries->remove (edge);
3400 ipa_fn_summaries->remove (edge->callee);
27d020cf
JH
3401}
3402
f658ad30
JH
3403/* For performance reasons ipa_merge_fn_summary_after_inlining is not updating
3404 overall size and time. Recompute it. */
27d020cf
JH
3405
3406void
0bceb671 3407ipa_update_overall_fn_summary (struct cgraph_node *node)
27d020cf 3408{
99b1c316 3409 class ipa_fn_summary *info = ipa_fn_summaries->get_create (node);
f658ad30 3410 class ipa_size_summary *size_info = ipa_size_summaries->get_create (node);
27d020cf
JH
3411 size_time_entry *e;
3412 int i;
3413
f658ad30 3414 size_info->size = 0;
27d020cf
JH
3415 info->time = 0;
3416 for (i = 0; vec_safe_iterate (info->size_time_table, i, &e); i++)
3417 {
f658ad30 3418 size_info->size += e->size;
27d020cf
JH
3419 info->time += e->time;
3420 }
f658ad30 3421 estimate_calls_size_and_time (node, &size_info->size, &info->min_size,
27d020cf
JH
3422 &info->time, NULL,
3423 ~(clause_t) (1 << predicate::false_condition),
3424 vNULL, vNULL, vNULL);
f658ad30
JH
3425 size_info->size = (size_info->size + ipa_fn_summary::size_scale / 2)
3426 / ipa_fn_summary::size_scale;
27d020cf
JH
3427}
3428
3429
3430/* This function performs intraprocedural analysis in NODE that is required to
3431 inline indirect calls. */
3432
3433static void
3434inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3435{
3436 ipa_analyze_node (node);
3437 if (dump_file && (dump_flags & TDF_DETAILS))
3438 {
3439 ipa_print_node_params (dump_file, node);
3440 ipa_print_node_jump_functions (dump_file, node);
3441 }
3442}
3443
3444
3445/* Note function body size. */
3446
3447void
3448inline_analyze_function (struct cgraph_node *node)
3449{
3450 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3451
3452 if (dump_file)
3453 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3454 node->name (), node->order);
3455 if (opt_for_fn (node->decl, optimize) && !node->thunk.thunk_p)
3456 inline_indirect_intraprocedural_analysis (node);
0bceb671 3457 compute_fn_summary (node, false);
27d020cf
JH
3458 if (!optimize)
3459 {
3460 struct cgraph_edge *e;
3461 for (e = node->callees; e; e = e->next_callee)
3462 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3463 for (e = node->indirect_calls; e; e = e->next_callee)
3464 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3465 }
3466
3467 pop_cfun ();
3468}
3469
3470
3471/* Called when new function is inserted to callgraph late. */
3472
3473void
0bceb671 3474ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *)
27d020cf
JH
3475{
3476 inline_analyze_function (node);
3477}
3478
3479/* Note function body size. */
3480
d2db2e6b
JH
3481static void
3482ipa_fn_summary_generate (void)
27d020cf
JH
3483{
3484 struct cgraph_node *node;
3485
3486 FOR_EACH_DEFINED_FUNCTION (node)
3487 if (DECL_STRUCT_FUNCTION (node->decl))
0688f9c1 3488 node->local.versionable = tree_versionable_function_p (node->decl);
27d020cf 3489
0bceb671 3490 ipa_fn_summary_alloc ();
27d020cf 3491
0bceb671 3492 ipa_fn_summaries->enable_insertion_hook ();
27d020cf
JH
3493
3494 ipa_register_cgraph_hooks ();
27d020cf
JH
3495
3496 FOR_EACH_DEFINED_FUNCTION (node)
29f1e2b1
JH
3497 if (!node->alias
3498 && (flag_generate_lto || flag_generate_offload|| flag_wpa
3499 || opt_for_fn (node->decl, optimize)))
27d020cf
JH
3500 inline_analyze_function (node);
3501}
3502
3503
3504/* Write inline summary for edge E to OB. */
3505
3506static void
99b1c316 3507read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e,
ddfb1317 3508 bool prevails)
27d020cf 3509{
99b1c316 3510 class ipa_call_summary *es = prevails
ddfb1317 3511 ? ipa_call_summaries->get_create (e) : NULL;
27d020cf
JH
3512 predicate p;
3513 int length, i;
3514
ddfb1317
JH
3515 int size = streamer_read_uhwi (ib);
3516 int time = streamer_read_uhwi (ib);
3517 int depth = streamer_read_uhwi (ib);
3518
3519 if (es)
3520 {
3521 es->call_stmt_size = size;
3522 es->call_stmt_time = time;
3523 es->loop_depth = depth;
3524 }
0fab169b
PK
3525
3526 bitpack_d bp = streamer_read_bitpack (ib);
ddfb1317
JH
3527 if (es)
3528 es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1);
3529 else
3530 bp_unpack_value (&bp, 1);
0fab169b 3531
27d020cf 3532 p.stream_in (ib);
ddfb1317
JH
3533 if (es)
3534 edge_set_predicate (e, &p);
27d020cf 3535 length = streamer_read_uhwi (ib);
ddfb1317 3536 if (length && es && e->possibly_call_in_translation_unit_p ())
27d020cf
JH
3537 {
3538 es->param.safe_grow_cleared (length);
3539 for (i = 0; i < length; i++)
3540 es->param[i].change_prob = streamer_read_uhwi (ib);
3541 }
ddfb1317
JH
3542 else
3543 {
3544 for (i = 0; i < length; i++)
3545 streamer_read_uhwi (ib);
3546 }
27d020cf
JH
3547}
3548
3549
3550/* Stream in inline summaries from the section. */
3551
3552static void
3553inline_read_section (struct lto_file_decl_data *file_data, const char *data,
3554 size_t len)
3555{
3556 const struct lto_function_header *header =
3557 (const struct lto_function_header *) data;
3558 const int cfg_offset = sizeof (struct lto_function_header);
3559 const int main_offset = cfg_offset + header->cfg_size;
3560 const int string_offset = main_offset + header->main_size;
99b1c316 3561 class data_in *data_in;
27d020cf
JH
3562 unsigned int i, count2, j;
3563 unsigned int f_count;
3564
3565 lto_input_block ib ((const char *) data + main_offset, header->main_size,
3566 file_data->mode_table);
3567
3568 data_in =
3569 lto_data_in_create (file_data, (const char *) data + string_offset,
3570 header->string_size, vNULL);
3571 f_count = streamer_read_uhwi (&ib);
3572 for (i = 0; i < f_count; i++)
3573 {
3574 unsigned int index;
3575 struct cgraph_node *node;
99b1c316 3576 class ipa_fn_summary *info;
f658ad30 3577 class ipa_size_summary *size_info;
27d020cf
JH
3578 lto_symtab_encoder_t encoder;
3579 struct bitpack_d bp;
3580 struct cgraph_edge *e;
3581 predicate p;
3582
3583 index = streamer_read_uhwi (&ib);
3584 encoder = file_data->symtab_node_encoder;
3585 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
3586 index));
ddfb1317 3587 info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL;
f658ad30
JH
3588 size_info = node->prevailing_p ()
3589 ? ipa_size_summaries->get_create (node) : NULL;
27d020cf 3590
ddfb1317
JH
3591 int stack_size = streamer_read_uhwi (&ib);
3592 int size = streamer_read_uhwi (&ib);
3593 sreal time = sreal::stream_in (&ib);
3594
3595 if (info)
3596 {
3597 info->estimated_stack_size
f658ad30
JH
3598 = size_info->estimated_self_stack_size = stack_size;
3599 size_info->size = size_info->self_size = size;
ddfb1317
JH
3600 info->time = time;
3601 }
27d020cf
JH
3602
3603 bp = streamer_read_bitpack (&ib);
ddfb1317
JH
3604 if (info)
3605 {
3606 info->inlinable = bp_unpack_value (&bp, 1);
3607 info->fp_expressions = bp_unpack_value (&bp, 1);
3608 }
3609 else
3610 {
3611 bp_unpack_value (&bp, 1);
3612 bp_unpack_value (&bp, 1);
3613 }
27d020cf
JH
3614
3615 count2 = streamer_read_uhwi (&ib);
ddfb1317 3616 gcc_assert (!info || !info->conds);
27d020cf
JH
3617 for (j = 0; j < count2; j++)
3618 {
3619 struct condition c;
4307a485 3620 unsigned int k, count3;
27d020cf 3621 c.operand_num = streamer_read_uhwi (&ib);
27d020cf 3622 c.code = (enum tree_code) streamer_read_uhwi (&ib);
4307a485 3623 c.type = stream_read_tree (&ib, data_in);
27d020cf
JH
3624 c.val = stream_read_tree (&ib, data_in);
3625 bp = streamer_read_bitpack (&ib);
3626 c.agg_contents = bp_unpack_value (&bp, 1);
3627 c.by_ref = bp_unpack_value (&bp, 1);
3628 if (c.agg_contents)
3629 c.offset = streamer_read_uhwi (&ib);
4307a485
FX
3630 c.param_ops = NULL;
3631 count3 = streamer_read_uhwi (&ib);
3632 for (k = 0; k < count3; k++)
3633 {
3634 struct expr_eval_op op;
3635 enum gimple_rhs_class rhs_class;
3636 op.code = (enum tree_code) streamer_read_uhwi (&ib);
3637 op.type = stream_read_tree (&ib, data_in);
3638 switch (rhs_class = get_gimple_rhs_class (op.code))
3639 {
3640 case GIMPLE_UNARY_RHS:
3641 op.index = 0;
3642 op.val[0] = NULL_TREE;
3643 op.val[1] = NULL_TREE;
3644 break;
3645
3646 case GIMPLE_BINARY_RHS:
3647 case GIMPLE_TERNARY_RHS:
3648 bp = streamer_read_bitpack (&ib);
3649 op.index = bp_unpack_value (&bp, 2);
3650 op.val[0] = stream_read_tree (&ib, data_in);
3651 if (rhs_class == GIMPLE_BINARY_RHS)
3652 op.val[1] = NULL_TREE;
3653 else
3654 op.val[1] = stream_read_tree (&ib, data_in);
3655 break;
3656
3657 default:
3658 fatal_error (UNKNOWN_LOCATION,
3659 "invalid fnsummary in LTO stream");
3660 }
3661 vec_safe_push (c.param_ops, op);
3662 }
ddfb1317
JH
3663 if (info)
3664 vec_safe_push (info->conds, c);
27d020cf
JH
3665 }
3666 count2 = streamer_read_uhwi (&ib);
ddfb1317 3667 gcc_assert (!info || !info->size_time_table);
27d020cf
JH
3668 for (j = 0; j < count2; j++)
3669 {
99b1c316 3670 class size_time_entry e;
27d020cf
JH
3671
3672 e.size = streamer_read_uhwi (&ib);
3673 e.time = sreal::stream_in (&ib);
3674 e.exec_predicate.stream_in (&ib);
3675 e.nonconst_predicate.stream_in (&ib);
3676
ddfb1317
JH
3677 if (info)
3678 vec_safe_push (info->size_time_table, e);
27d020cf
JH
3679 }
3680
3681 p.stream_in (&ib);
ddfb1317
JH
3682 if (info)
3683 set_hint_predicate (&info->loop_iterations, p);
27d020cf 3684 p.stream_in (&ib);
ddfb1317
JH
3685 if (info)
3686 set_hint_predicate (&info->loop_stride, p);
27d020cf 3687 for (e = node->callees; e; e = e->next_callee)
ddfb1317 3688 read_ipa_call_summary (&ib, e, info != NULL);
27d020cf 3689 for (e = node->indirect_calls; e; e = e->next_callee)
ddfb1317 3690 read_ipa_call_summary (&ib, e, info != NULL);
27d020cf
JH
3691 }
3692
0bceb671 3693 lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
27d020cf
JH
3694 len);
3695 lto_data_in_delete (data_in);
3696}
3697
3698
3699/* Read inline summary. Jump functions are shared among ipa-cp
3700 and inliner, so when ipa-cp is active, we don't need to write them
3701 twice. */
3702
d2db2e6b
JH
3703static void
3704ipa_fn_summary_read (void)
27d020cf
JH
3705{
3706 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3707 struct lto_file_decl_data *file_data;
3708 unsigned int j = 0;
3709
0bceb671 3710 ipa_fn_summary_alloc ();
27d020cf
JH
3711
3712 while ((file_data = file_data_vec[j++]))
3713 {
3714 size_t len;
3715 const char *data = lto_get_section_data (file_data,
0bceb671 3716 LTO_section_ipa_fn_summary,
27d020cf
JH
3717 NULL, &len);
3718 if (data)
3719 inline_read_section (file_data, data, len);
3720 else
3721 /* Fatal error here. We do not want to support compiling ltrans units
3722 with different version of compiler or different flags than the WPA
3723 unit, so this should never happen. */
3724 fatal_error (input_location,
3725 "ipa inline summary is missing in input file");
3726 }
29f1e2b1
JH
3727 ipa_register_cgraph_hooks ();
3728 if (!flag_ipa_cp)
3729 ipa_prop_read_jump_functions ();
27d020cf 3730
0bceb671
JH
3731 gcc_assert (ipa_fn_summaries);
3732 ipa_fn_summaries->enable_insertion_hook ();
27d020cf
JH
3733}
3734
3735
3736/* Write inline summary for edge E to OB. */
3737
3738static void
3739write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e)
3740{
99b1c316 3741 class ipa_call_summary *es = ipa_call_summaries->get (e);
27d020cf
JH
3742 int i;
3743
3744 streamer_write_uhwi (ob, es->call_stmt_size);
3745 streamer_write_uhwi (ob, es->call_stmt_time);
3746 streamer_write_uhwi (ob, es->loop_depth);
0fab169b
PK
3747
3748 bitpack_d bp = bitpack_create (ob->main_stream);
3749 bp_pack_value (&bp, es->is_return_callee_uncaptured, 1);
3750 streamer_write_bitpack (&bp);
3751
27d020cf
JH
3752 if (es->predicate)
3753 es->predicate->stream_out (ob);
3754 else
3755 streamer_write_uhwi (ob, 0);
3756 streamer_write_uhwi (ob, es->param.length ());
3757 for (i = 0; i < (int) es->param.length (); i++)
3758 streamer_write_uhwi (ob, es->param[i].change_prob);
3759}
3760
3761
3762/* Write inline summary for node in SET.
3763 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
3764 active, we don't need to write them twice. */
3765
d2db2e6b
JH
3766static void
3767ipa_fn_summary_write (void)
27d020cf 3768{
0bceb671 3769 struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary);
27d020cf
JH
3770 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3771 unsigned int count = 0;
3772 int i;
3773
3774 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3775 {
3776 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
3777 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
3778 if (cnode && cnode->definition && !cnode->alias)
3779 count++;
3780 }
3781 streamer_write_uhwi (ob, count);
3782
3783 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3784 {
3785 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
3786 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
3787 if (cnode && cnode->definition && !cnode->alias)
3788 {
99b1c316 3789 class ipa_fn_summary *info = ipa_fn_summaries->get (cnode);
f658ad30 3790 class ipa_size_summary *size_info = ipa_size_summaries->get (cnode);
27d020cf
JH
3791 struct bitpack_d bp;
3792 struct cgraph_edge *edge;
3793 int i;
3794 size_time_entry *e;
3795 struct condition *c;
3796
3797 streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode));
f658ad30
JH
3798 streamer_write_hwi (ob, size_info->estimated_self_stack_size);
3799 streamer_write_hwi (ob, size_info->self_size);
27d020cf
JH
3800 info->time.stream_out (ob);
3801 bp = bitpack_create (ob->main_stream);
3802 bp_pack_value (&bp, info->inlinable, 1);
5e9d6aa4 3803 bp_pack_value (&bp, false, 1);
27d020cf
JH
3804 bp_pack_value (&bp, info->fp_expressions, 1);
3805 streamer_write_bitpack (&bp);
3806 streamer_write_uhwi (ob, vec_safe_length (info->conds));
3807 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
3808 {
4307a485
FX
3809 int j;
3810 struct expr_eval_op *op;
3811
27d020cf 3812 streamer_write_uhwi (ob, c->operand_num);
27d020cf 3813 streamer_write_uhwi (ob, c->code);
4307a485 3814 stream_write_tree (ob, c->type, true);
27d020cf
JH
3815 stream_write_tree (ob, c->val, true);
3816 bp = bitpack_create (ob->main_stream);
3817 bp_pack_value (&bp, c->agg_contents, 1);
3818 bp_pack_value (&bp, c->by_ref, 1);
3819 streamer_write_bitpack (&bp);
3820 if (c->agg_contents)
3821 streamer_write_uhwi (ob, c->offset);
4307a485
FX
3822 streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
3823 for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
3824 {
3825 streamer_write_uhwi (ob, op->code);
3826 stream_write_tree (ob, op->type, true);
3827 if (op->val[0])
3828 {
3829 bp = bitpack_create (ob->main_stream);
3830 bp_pack_value (&bp, op->index, 2);
3831 streamer_write_bitpack (&bp);
3832 stream_write_tree (ob, op->val[0], true);
3833 if (op->val[1])
3834 stream_write_tree (ob, op->val[1], true);
3835 }
3836 }
27d020cf
JH
3837 }
3838 streamer_write_uhwi (ob, vec_safe_length (info->size_time_table));
3839 for (i = 0; vec_safe_iterate (info->size_time_table, i, &e); i++)
3840 {
3841 streamer_write_uhwi (ob, e->size);
3842 e->time.stream_out (ob);
3843 e->exec_predicate.stream_out (ob);
3844 e->nonconst_predicate.stream_out (ob);
3845 }
3846 if (info->loop_iterations)
3847 info->loop_iterations->stream_out (ob);
3848 else
3849 streamer_write_uhwi (ob, 0);
3850 if (info->loop_stride)
3851 info->loop_stride->stream_out (ob);
3852 else
3853 streamer_write_uhwi (ob, 0);
27d020cf
JH
3854 for (edge = cnode->callees; edge; edge = edge->next_callee)
3855 write_ipa_call_summary (ob, edge);
3856 for (edge = cnode->indirect_calls; edge; edge = edge->next_callee)
3857 write_ipa_call_summary (ob, edge);
3858 }
3859 }
3860 streamer_write_char_stream (ob->main_stream, 0);
3861 produce_asm (ob, NULL);
3862 destroy_output_block (ob);
3863
29f1e2b1 3864 if (!flag_ipa_cp)
27d020cf
JH
3865 ipa_prop_write_jump_functions ();
3866}
3867
3868
f658ad30 3869/* Release function summary. */
27d020cf
JH
3870
3871void
d2db2e6b 3872ipa_free_fn_summary (void)
27d020cf 3873{
27d020cf
JH
3874 if (!ipa_call_summaries)
3875 return;
0bceb671
JH
3876 ipa_fn_summaries->release ();
3877 ipa_fn_summaries = NULL;
27d020cf
JH
3878 ipa_call_summaries->release ();
3879 delete ipa_call_summaries;
3880 ipa_call_summaries = NULL;
3881 edge_predicate_pool.release ();
f658ad30
JH
3882 /* During IPA this is one of largest datastructures to release. */
3883 if (flag_wpa)
3884 ggc_trim ();
3885}
3886
3887/* Release function summary. */
3888
3889void
3890ipa_free_size_summary (void)
3891{
3892 if (!ipa_size_summaries)
3893 return;
3894 ipa_size_summaries->release ();
3895 ipa_size_summaries = NULL;
27d020cf 3896}
d2db2e6b
JH
3897
3898namespace {
3899
3900const pass_data pass_data_local_fn_summary =
3901{
3902 GIMPLE_PASS, /* type */
3903 "local-fnsummary", /* name */
3904 OPTGROUP_INLINE, /* optinfo_flags */
3905 TV_INLINE_PARAMETERS, /* tv_id */
3906 0, /* properties_required */
3907 0, /* properties_provided */
3908 0, /* properties_destroyed */
3909 0, /* todo_flags_start */
3910 0, /* todo_flags_finish */
3911};
3912
3913class pass_local_fn_summary : public gimple_opt_pass
3914{
3915public:
3916 pass_local_fn_summary (gcc::context *ctxt)
3917 : gimple_opt_pass (pass_data_local_fn_summary, ctxt)
3918 {}
3919
3920 /* opt_pass methods: */
3921 opt_pass * clone () { return new pass_local_fn_summary (m_ctxt); }
3922 virtual unsigned int execute (function *)
3923 {
3924 return compute_fn_summary_for_current ();
3925 }
3926
3927}; // class pass_local_fn_summary
3928
3929} // anon namespace
3930
3931gimple_opt_pass *
3932make_pass_local_fn_summary (gcc::context *ctxt)
3933{
3934 return new pass_local_fn_summary (ctxt);
3935}
3936
3937
3938/* Free inline summary. */
3939
3940namespace {
3941
3942const pass_data pass_data_ipa_free_fn_summary =
3943{
3944 SIMPLE_IPA_PASS, /* type */
3945 "free-fnsummary", /* name */
3946 OPTGROUP_NONE, /* optinfo_flags */
3947 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
3948 0, /* properties_required */
3949 0, /* properties_provided */
3950 0, /* properties_destroyed */
3951 0, /* todo_flags_start */
442db276 3952 0, /* todo_flags_finish */
d2db2e6b
JH
3953};
3954
3955class pass_ipa_free_fn_summary : public simple_ipa_opt_pass
3956{
3957public:
3958 pass_ipa_free_fn_summary (gcc::context *ctxt)
442db276
JJ
3959 : simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt),
3960 small_p (false)
d2db2e6b
JH
3961 {}
3962
3963 /* opt_pass methods: */
442db276
JJ
3964 opt_pass *clone () { return new pass_ipa_free_fn_summary (m_ctxt); }
3965 void set_pass_param (unsigned int n, bool param)
3966 {
3967 gcc_assert (n == 0);
3968 small_p = param;
3969 }
f658ad30 3970 virtual bool gate (function *) { return true; }
d2db2e6b
JH
3971 virtual unsigned int execute (function *)
3972 {
3973 ipa_free_fn_summary ();
f658ad30
JH
3974 if (!flag_wpa)
3975 ipa_free_size_summary ();
12485662 3976 return 0;
d2db2e6b
JH
3977 }
3978
442db276
JJ
3979private:
3980 bool small_p;
d2db2e6b
JH
3981}; // class pass_ipa_free_fn_summary
3982
3983} // anon namespace
3984
3985simple_ipa_opt_pass *
3986make_pass_ipa_free_fn_summary (gcc::context *ctxt)
3987{
3988 return new pass_ipa_free_fn_summary (ctxt);
3989}
3990
3991namespace {
3992
3993const pass_data pass_data_ipa_fn_summary =
3994{
3995 IPA_PASS, /* type */
3996 "fnsummary", /* name */
3997 OPTGROUP_INLINE, /* optinfo_flags */
66447ef0 3998 TV_IPA_FNSUMMARY, /* tv_id */
d2db2e6b
JH
3999 0, /* properties_required */
4000 0, /* properties_provided */
4001 0, /* properties_destroyed */
4002 0, /* todo_flags_start */
4003 ( TODO_dump_symtab ), /* todo_flags_finish */
4004};
4005
4006class pass_ipa_fn_summary : public ipa_opt_pass_d
4007{
4008public:
4009 pass_ipa_fn_summary (gcc::context *ctxt)
4010 : ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt,
4011 ipa_fn_summary_generate, /* generate_summary */
4012 ipa_fn_summary_write, /* write_summary */
4013 ipa_fn_summary_read, /* read_summary */
4014 NULL, /* write_optimization_summary */
4015 NULL, /* read_optimization_summary */
4016 NULL, /* stmt_fixup */
4017 0, /* function_transform_todo_flags_start */
4018 NULL, /* function_transform */
4019 NULL) /* variable_transform */
4020 {}
4021
4022 /* opt_pass methods: */
4023 virtual unsigned int execute (function *) { return 0; }
4024
4025}; // class pass_ipa_fn_summary
4026
4027} // anon namespace
4028
4029ipa_opt_pass_d *
4030make_pass_ipa_fn_summary (gcc::context *ctxt)
4031{
4032 return new pass_ipa_fn_summary (ctxt);
4033}
de4381a4
DM
4034
4035/* Reset all state within ipa-fnsummary.c so that we can rerun the compiler
4036 within the same process. For use by toplev::finalize. */
4037
4038void
4039ipa_fnsummary_c_finalize (void)
4040{
4041 ipa_free_fn_summary ();
4042}