1 /* Basic IPA optimizations based on profile.
2 Copyright (C) 2003-2025 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* ipa-profile pass implements the following analysis propagating profille
23 - Count histogram construction. This is a histogram analyzing how much
24 time is spent executing statements with a given execution count read
25 from profile feedback. This histogram is complete only with LTO,
26 otherwise it contains information only about the current unit.
28 The information is used to set hot/cold thresholds.
29 - Next speculative indirect call resolution is performed: the local
30 profile pass assigns profile-id to each function and provide us with a
31 histogram specifying the most common target. We look up the callgraph
32 node corresponding to the target and produce a speculative call.
34 This call may or may not survive through IPA optimization based on decision
36 - Finally we propagate the following flags: unlikely executed, executed
37 once, executed at startup and executed at exit. These flags are used to
38 control code size/performance threshold and code placement (by producing
39 .text.unlikely/.text.hot/.text.startup/.text.exit subsections). */
42 #include "coretypes.h"
47 #include "alloc-pool.h"
48 #include "tree-pass.h"
50 #include "data-streamer.h"
51 #include "gimple-iterator.h"
52 #include "ipa-utils.h"
54 #include "value-prof.h"
55 #include "tree-inline.h"
56 #include "symbol-summary.h"
61 #include "ipa-fnsummary.h"
63 /* Entry in the histogram. */
65 struct histogram_entry
72 /* Histogram of profile values.
73 The histogram is represented as an ordered vector of entries allocated via
74 histogram_pool. During construction a separate hashtable is kept to lookup
77 vec
<histogram_entry
*> histogram
;
78 static object_allocator
<histogram_entry
> histogram_pool ("IPA histogram");
80 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
82 struct histogram_hash
: nofree_ptr_hash
<histogram_entry
>
84 static inline hashval_t
hash (const histogram_entry
*);
85 static inline int equal (const histogram_entry
*, const histogram_entry
*);
89 histogram_hash::hash (const histogram_entry
*val
)
95 histogram_hash::equal (const histogram_entry
*val
, const histogram_entry
*val2
)
97 return val
->count
== val2
->count
;
100 /* Account TIME and SIZE executed COUNT times into HISTOGRAM.
101 HASHTABLE is the on-side hash kept to avoid duplicates. */
104 account_time_size (hash_table
<histogram_hash
> *hashtable
,
105 vec
<histogram_entry
*> &histogram
,
106 gcov_type count
, int time
, int size
)
108 histogram_entry key
= {count
, 0, 0};
109 histogram_entry
**val
= hashtable
->find_slot (&key
, INSERT
);
113 *val
= histogram_pool
.allocate ();
115 histogram
.safe_push (*val
);
117 (*val
)->time
+= time
;
118 (*val
)->size
+= size
;
122 cmp_counts (const void *v1
, const void *v2
)
124 const histogram_entry
*h1
= *(const histogram_entry
* const *)v1
;
125 const histogram_entry
*h2
= *(const histogram_entry
* const *)v2
;
126 if (h1
->count
< h2
->count
)
128 if (h1
->count
> h2
->count
)
133 /* Dump HISTOGRAM to FILE. */
136 dump_histogram (FILE *file
, vec
<histogram_entry
*> histogram
)
139 gcov_type overall_time
= 0, cumulated_time
= 0, cumulated_size
= 0,
142 fprintf (dump_file
, "Histogram:\n");
143 for (i
= 0; i
< histogram
.length (); i
++)
145 overall_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
146 overall_size
+= histogram
[i
]->size
;
152 for (i
= 0; i
< histogram
.length (); i
++)
154 cumulated_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
155 cumulated_size
+= histogram
[i
]->size
;
156 fprintf (file
, " %" PRId64
": time:%i (%2.2f) size:%i (%2.2f)\n",
157 (int64_t) histogram
[i
]->count
,
159 cumulated_time
* 100.0 / overall_time
,
161 cumulated_size
* 100.0 / overall_size
);
165 /* Structure containing speculative target information from profile. */
167 struct speculative_call_target
169 speculative_call_target (unsigned int id
= 0, int prob
= 0)
170 : target_id (id
), target_probability (prob
)
174 /* Profile_id of target obtained from profile. */
175 unsigned int target_id
;
176 /* Probability that call will land in function with target_id. */
177 unsigned int target_probability
;
180 class speculative_call_summary
183 speculative_call_summary () : speculative_call_targets ()
186 auto_vec
<speculative_call_target
> speculative_call_targets
;
192 /* Class to manage call summaries. */
194 class ipa_profile_call_summaries
195 : public call_summary
<speculative_call_summary
*>
198 ipa_profile_call_summaries (symbol_table
*table
)
199 : call_summary
<speculative_call_summary
*> (table
)
202 /* Duplicate info when an edge is cloned. */
203 void duplicate (cgraph_edge
*, cgraph_edge
*,
204 speculative_call_summary
*old_sum
,
205 speculative_call_summary
*new_sum
) final override
;
208 static ipa_profile_call_summaries
*call_sums
= NULL
;
210 /* Dump all information in speculative call summary to F. */
213 speculative_call_summary::dump (FILE *f
)
217 unsigned spec_count
= speculative_call_targets
.length ();
218 for (unsigned i
= 0; i
< spec_count
; i
++)
220 speculative_call_target item
= speculative_call_targets
[i
];
221 n2
= find_func_by_profile_id (item
.target_id
);
223 fprintf (f
, " The %i speculative target is %s with prob %3.2f\n", i
,
225 item
.target_probability
/ (float) REG_BR_PROB_BASE
);
227 fprintf (f
, " The %i speculative target is %u with prob %3.2f\n", i
,
229 item
.target_probability
/ (float) REG_BR_PROB_BASE
);
233 /* Duplicate info when an edge is cloned. */
236 ipa_profile_call_summaries::duplicate (cgraph_edge
*, cgraph_edge
*,
237 speculative_call_summary
*old_sum
,
238 speculative_call_summary
*new_sum
)
243 unsigned old_count
= old_sum
->speculative_call_targets
.length ();
247 new_sum
->speculative_call_targets
.reserve_exact (old_count
);
248 new_sum
->speculative_call_targets
.quick_grow_cleared (old_count
);
250 for (unsigned i
= 0; i
< old_count
; i
++)
252 new_sum
->speculative_call_targets
[i
]
253 = old_sum
->speculative_call_targets
[i
];
257 /* Collect histogram and speculative target summaries from CFG profiles. */
260 ipa_profile_generate_summary (void)
262 struct cgraph_node
*node
;
263 gimple_stmt_iterator gsi
;
266 hash_table
<histogram_hash
> hashtable (10);
268 gcc_checking_assert (!call_sums
);
269 call_sums
= new ipa_profile_call_summaries (symtab
);
271 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
272 if (ENTRY_BLOCK_PTR_FOR_FN
273 (DECL_STRUCT_FUNCTION (node
->decl
))->count
.ipa_p ())
274 FOR_EACH_BB_FN (bb
, DECL_STRUCT_FUNCTION (node
->decl
))
278 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
280 gimple
*stmt
= gsi_stmt (gsi
);
281 if (gimple_code (stmt
) == GIMPLE_CALL
282 && !gimple_call_fndecl (stmt
))
285 h
= gimple_histogram_value_of_type
286 (DECL_STRUCT_FUNCTION (node
->decl
),
287 stmt
, HIST_TYPE_INDIR_CALL
);
288 /* No need to do sanity check: gimple_ic_transform already
289 takes away bad histograms. */
292 gcov_type val
, count
, all
;
293 struct cgraph_edge
*e
= node
->get_edge (stmt
);
294 if (e
&& !e
->indirect_unknown_callee
)
297 speculative_call_summary
*csum
298 = call_sums
->get_create (e
);
300 for (unsigned j
= 0; j
< GCOV_TOPN_MAXIMUM_TRACKED_VALUES
;
303 if (!get_nth_most_common_value (NULL
, "indirect call",
304 h
, &val
, &count
, &all
,
308 if (val
== 0 || count
== 0)
315 "Probability capped to 1\n");
318 speculative_call_target
item (
319 val
, GCOV_COMPUTE_SCALE (count
, all
));
320 csum
->speculative_call_targets
.safe_push (item
);
323 gimple_remove_histogram_value
324 (DECL_STRUCT_FUNCTION (node
->decl
), stmt
, h
);
327 time
+= estimate_num_insns (stmt
, &eni_time_weights
);
328 size
+= estimate_num_insns (stmt
, &eni_size_weights
);
330 if (bb
->count
.ipa_p () && bb
->count
.initialized_p ())
331 account_time_size (&hashtable
, histogram
,
332 bb
->count
.ipa ().to_gcov_type (),
335 histogram
.qsort (cmp_counts
);
338 /* Serialize the speculative summary info for LTO. */
341 ipa_profile_write_edge_summary (lto_simple_output_block
*ob
,
342 speculative_call_summary
*csum
)
346 len
= csum
->speculative_call_targets
.length ();
348 gcc_assert (len
<= GCOV_TOPN_MAXIMUM_TRACKED_VALUES
);
350 streamer_write_hwi_stream (ob
->main_stream
, len
);
354 unsigned spec_count
= csum
->speculative_call_targets
.length ();
355 for (unsigned i
= 0; i
< spec_count
; i
++)
357 speculative_call_target item
= csum
->speculative_call_targets
[i
];
358 gcc_assert (item
.target_id
);
359 streamer_write_hwi_stream (ob
->main_stream
, item
.target_id
);
360 streamer_write_hwi_stream (ob
->main_stream
, item
.target_probability
);
365 /* Serialize the ipa info for lto. */
368 ipa_profile_write_summary (void)
370 struct lto_simple_output_block
*ob
371 = lto_create_simple_output_block (LTO_section_ipa_profile
);
374 streamer_write_uhwi_stream (ob
->main_stream
, histogram
.length ());
375 for (i
= 0; i
< histogram
.length (); i
++)
377 streamer_write_gcov_count_stream (ob
->main_stream
, histogram
[i
]->count
);
378 streamer_write_uhwi_stream (ob
->main_stream
, histogram
[i
]->time
);
379 streamer_write_uhwi_stream (ob
->main_stream
, histogram
[i
]->size
);
385 /* Serialize speculative targets information. */
386 unsigned int count
= 0;
387 lto_symtab_encoder_t encoder
= ob
->decl_state
->symtab_node_encoder
;
388 lto_symtab_encoder_iterator lsei
;
391 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
392 lsei_next_function_in_partition (&lsei
))
394 node
= lsei_cgraph_node (lsei
);
395 if (node
->definition
&& node
->has_gimple_body_p ()
396 && node
->indirect_calls
)
400 streamer_write_uhwi_stream (ob
->main_stream
, count
);
402 /* Process all of the functions. */
403 for (lsei
= lsei_start_function_in_partition (encoder
);
404 !lsei_end_p (lsei
) && count
; lsei_next_function_in_partition (&lsei
))
406 cgraph_node
*node
= lsei_cgraph_node (lsei
);
407 if (node
->definition
&& node
->has_gimple_body_p ()
408 && node
->indirect_calls
)
410 int node_ref
= lto_symtab_encoder_encode (encoder
, node
);
411 streamer_write_uhwi_stream (ob
->main_stream
, node_ref
);
413 for (cgraph_edge
*e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
415 speculative_call_summary
*csum
= call_sums
->get_create (e
);
416 ipa_profile_write_edge_summary (ob
, csum
);
421 lto_destroy_simple_output_block (ob
);
424 /* Dump all profile summary data for all cgraph nodes and edges to file F. */
427 ipa_profile_dump_all_summaries (FILE *f
)
430 "\n========== IPA-profile speculative targets: ==========\n");
432 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
434 fprintf (f
, "\nSummary for node %s:\n", node
->dump_name ());
435 for (cgraph_edge
*e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
437 fprintf (f
, " Summary for %s of indirect edge %d:\n",
438 e
->caller
->dump_name (), e
->lto_stmt_uid
);
439 speculative_call_summary
*csum
= call_sums
->get_create (e
);
446 /* Read speculative targets information about edge for LTO WPA. */
449 ipa_profile_read_edge_summary (class lto_input_block
*ib
, cgraph_edge
*edge
)
453 len
= streamer_read_hwi (ib
);
454 gcc_assert (len
<= GCOV_TOPN_MAXIMUM_TRACKED_VALUES
);
455 speculative_call_summary
*csum
= call_sums
->get_create (edge
);
457 for (i
= 0; i
< len
; i
++)
459 unsigned int target_id
= streamer_read_hwi (ib
);
460 int target_probability
= streamer_read_hwi (ib
);
461 speculative_call_target
item (target_id
, target_probability
);
462 csum
->speculative_call_targets
.safe_push (item
);
466 /* Read profile speculative targets section information for LTO WPA. */
469 ipa_profile_read_summary_section (struct lto_file_decl_data
*file_data
,
470 class lto_input_block
*ib
)
475 lto_symtab_encoder_t encoder
= file_data
->symtab_node_encoder
;
477 unsigned int count
= streamer_read_uhwi (ib
);
483 for (i
= 0; i
< count
; i
++)
485 index
= streamer_read_uhwi (ib
);
487 = dyn_cast
<cgraph_node
*> (lto_symtab_encoder_deref (encoder
, index
));
489 for (cgraph_edge
*e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
490 ipa_profile_read_edge_summary (ib
, e
);
494 /* Deserialize the IPA histogram and speculative targets summary info for LTO.
498 ipa_profile_read_summary (void)
500 struct lto_file_decl_data
** file_data_vec
501 = lto_get_file_decl_data ();
502 struct lto_file_decl_data
* file_data
;
505 hash_table
<histogram_hash
> hashtable (10);
507 gcc_checking_assert (!call_sums
);
508 call_sums
= new ipa_profile_call_summaries (symtab
);
510 while ((file_data
= file_data_vec
[j
++]))
514 class lto_input_block
*ib
515 = lto_create_simple_input_block (file_data
,
516 LTO_section_ipa_profile
,
520 unsigned int num
= streamer_read_uhwi (ib
);
522 for (n
= 0; n
< num
; n
++)
524 gcov_type count
= streamer_read_gcov_count (ib
);
525 int time
= streamer_read_uhwi (ib
);
526 int size
= streamer_read_uhwi (ib
);
527 account_time_size (&hashtable
, histogram
,
531 ipa_profile_read_summary_section (file_data
, ib
);
533 lto_destroy_simple_input_block (file_data
,
534 LTO_section_ipa_profile
,
538 histogram
.qsort (cmp_counts
);
541 /* Data used by ipa_propagate_frequency. */
543 struct ipa_propagate_frequency_data
545 cgraph_node
*function_symbol
;
546 bool maybe_unlikely_executed
;
547 bool maybe_executed_once
;
548 bool only_called_at_startup
;
549 bool only_called_at_exit
;
552 /* Worker for ipa_propagate_frequency_1. */
555 ipa_propagate_frequency_1 (struct cgraph_node
*node
, void *data
)
557 struct ipa_propagate_frequency_data
*d
;
558 struct cgraph_edge
*edge
;
560 d
= (struct ipa_propagate_frequency_data
*)data
;
561 for (edge
= node
->callers
;
562 edge
&& (d
->maybe_unlikely_executed
|| d
->maybe_executed_once
563 || d
->only_called_at_startup
|| d
->only_called_at_exit
);
564 edge
= edge
->next_caller
)
566 if (edge
->caller
!= d
->function_symbol
)
568 d
->only_called_at_startup
&= edge
->caller
->only_called_at_startup
;
569 /* It makes sense to put main() together with the static constructors.
570 It will be executed for sure, but rest of functions called from
571 main are definitely not at startup only. */
572 if (MAIN_NAME_P (DECL_NAME (edge
->caller
->decl
)))
573 d
->only_called_at_startup
= 0;
574 d
->only_called_at_exit
&= edge
->caller
->only_called_at_exit
;
577 /* When profile feedback is available, do not try to propagate too hard;
578 counts are already good guide on function frequencies and roundoff
579 errors can make us to push function into unlikely section even when
580 it is executed by the train run. Transfer the function only if all
581 callers are unlikely executed. */
583 && !(edge
->callee
->count
.ipa () == profile_count::zero ())
584 && (edge
->caller
->frequency
!= NODE_FREQUENCY_UNLIKELY_EXECUTED
585 || (edge
->caller
->inlined_to
586 && edge
->caller
->inlined_to
->frequency
587 != NODE_FREQUENCY_UNLIKELY_EXECUTED
)))
588 d
->maybe_unlikely_executed
= false;
589 if (edge
->count
.ipa ().initialized_p ()
590 && !edge
->count
.ipa ().nonzero_p ())
592 switch (edge
->caller
->frequency
)
594 case NODE_FREQUENCY_UNLIKELY_EXECUTED
:
596 case NODE_FREQUENCY_EXECUTED_ONCE
:
598 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
599 fprintf (dump_file
, " Called by %s that is executed once\n",
600 edge
->caller
->dump_name ());
601 d
->maybe_unlikely_executed
= false;
602 ipa_call_summary
*s
= ipa_call_summaries
->get (edge
);
603 if (s
!= NULL
&& s
->loop_depth
)
605 d
->maybe_executed_once
= false;
606 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
607 fprintf (dump_file
, " Called in loop\n");
611 case NODE_FREQUENCY_HOT
:
612 case NODE_FREQUENCY_NORMAL
:
613 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
614 fprintf (dump_file
, " Called by %s that is normal or hot\n",
615 edge
->caller
->dump_name ());
616 d
->maybe_unlikely_executed
= false;
617 d
->maybe_executed_once
= false;
624 /* Return ture if NODE contains hot calls. */
627 contains_hot_call_p (struct cgraph_node
*node
)
629 struct cgraph_edge
*e
;
630 for (e
= node
->callees
; e
; e
= e
->next_callee
)
631 if (e
->maybe_hot_p ())
633 else if (!e
->inline_failed
634 && contains_hot_call_p (e
->callee
))
636 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
637 if (e
->maybe_hot_p ())
642 /* See if the frequency of NODE can be updated based on frequencies of its
645 ipa_propagate_frequency (struct cgraph_node
*node
)
647 struct ipa_propagate_frequency_data d
= {node
, true, true, true, true};
648 bool changed
= false;
650 /* We cannot propagate anything useful about externally visible functions
651 nor about virtuals. */
654 || (opt_for_fn (node
->decl
, flag_devirtualize
)
655 && DECL_VIRTUAL_P (node
->decl
)))
657 gcc_assert (node
->analyzed
);
658 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
659 fprintf (dump_file
, "Processing frequency %s\n", node
->dump_name ());
661 node
->call_for_symbol_and_aliases (ipa_propagate_frequency_1
, &d
,
664 if ((d
.only_called_at_startup
&& !d
.only_called_at_exit
)
665 && !node
->only_called_at_startup
)
667 node
->only_called_at_startup
= true;
669 fprintf (dump_file
, "Node %s promoted to only called at startup.\n",
673 if ((d
.only_called_at_exit
&& !d
.only_called_at_startup
)
674 && !node
->only_called_at_exit
)
676 node
->only_called_at_exit
= true;
678 fprintf (dump_file
, "Node %s promoted to only called at exit.\n",
683 /* With profile we can decide on hot/normal based on count. */
684 if (node
->count
. ipa().initialized_p ())
687 if (!(node
->count
. ipa() == profile_count::zero ())
688 && node
->count
. ipa() >= get_hot_bb_threshold ())
691 hot
|= contains_hot_call_p (node
);
694 if (node
->frequency
!= NODE_FREQUENCY_HOT
)
697 fprintf (dump_file
, "Node %s promoted to hot.\n",
699 node
->frequency
= NODE_FREQUENCY_HOT
;
704 else if (node
->frequency
== NODE_FREQUENCY_HOT
)
707 fprintf (dump_file
, "Node %s reduced to normal.\n",
709 node
->frequency
= NODE_FREQUENCY_NORMAL
;
713 /* These come either from profile or user hints; never update them. */
714 if (node
->frequency
== NODE_FREQUENCY_HOT
715 || node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
)
717 if (d
.maybe_unlikely_executed
)
719 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
721 fprintf (dump_file
, "Node %s promoted to unlikely executed.\n",
725 else if (d
.maybe_executed_once
&& node
->frequency
!= NODE_FREQUENCY_EXECUTED_ONCE
)
727 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
729 fprintf (dump_file
, "Node %s promoted to executed once.\n",
736 /* Check that number of arguments of N agrees with E.
737 Be conservative when summaries are not present. */
740 check_argument_count (struct cgraph_node
*n
, struct cgraph_edge
*e
)
742 if (!ipa_node_params_sum
|| !ipa_edge_args_sum
)
744 ipa_node_params
*info
= ipa_node_params_sum
->get (n
->function_symbol ());
747 ipa_edge_args
*e_info
= ipa_edge_args_sum
->get (e
);
750 if (ipa_get_param_count (info
) != ipa_get_cs_argument_count (e_info
)
751 && (ipa_get_param_count (info
) >= ipa_get_cs_argument_count (e_info
)
752 || !stdarg_p (TREE_TYPE (n
->decl
))))
757 /* Simple ipa profile pass propagating frequencies across the callgraph. */
762 struct cgraph_node
**order
;
763 struct cgraph_edge
*e
;
765 bool something_changed
= false;
767 gcov_type overall_time
= 0, cutoff
= 0, cumulated
= 0, overall_size
= 0;
768 struct cgraph_node
*n
,*n2
;
769 int nindirect
= 0, ncommon
= 0, nunknown
= 0, nuseless
= 0, nconverted
= 0;
770 int nmismatch
= 0, nimpossible
= 0;
771 bool node_map_initialized
= false;
775 dump_histogram (dump_file
, histogram
);
776 for (i
= 0; i
< (int)histogram
.length (); i
++)
778 overall_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
779 overall_size
+= histogram
[i
]->size
;
784 gcc_assert (overall_size
);
786 cutoff
= (overall_time
* param_hot_bb_count_ws_permille
+ 500) / 1000;
787 for (i
= 0; cumulated
< cutoff
; i
++)
789 cumulated
+= histogram
[i
]->count
* histogram
[i
]->time
;
790 threshold
= histogram
[i
]->count
;
796 gcov_type cumulated_time
= 0, cumulated_size
= 0;
799 i
< (int)histogram
.length () && histogram
[i
]->count
>= threshold
;
802 cumulated_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
803 cumulated_size
+= histogram
[i
]->size
;
805 fprintf (dump_file
, "Determined min count: %" PRId64
806 " Time:%3.2f%% Size:%3.2f%%\n",
808 cumulated_time
* 100.0 / overall_time
,
809 cumulated_size
* 100.0 / overall_size
);
815 fprintf (dump_file
, "Setting hotness threshold in LTO mode.\n");
816 set_hot_bb_threshold (threshold
);
819 histogram
.release ();
820 histogram_pool
.release ();
822 /* Produce speculative calls: we saved common target from profiling into
823 e->target_id. Now, at link time, we can look up corresponding
824 function node and produce speculative call. */
826 gcc_checking_assert (call_sums
);
830 if (!node_map_initialized
)
831 init_node_map (false);
832 node_map_initialized
= true;
834 ipa_profile_dump_all_summaries (dump_file
);
837 FOR_EACH_DEFINED_FUNCTION (n
)
841 if (!opt_for_fn (n
->decl
, flag_ipa_profile
))
844 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
846 if (n
->count
.initialized_p ())
849 speculative_call_summary
*csum
= call_sums
->get_create (e
);
850 unsigned spec_count
= csum
->speculative_call_targets
.length ();
853 if (!node_map_initialized
)
854 init_node_map (false);
855 node_map_initialized
= true;
858 unsigned speculative_id
= 0;
859 profile_count orig
= e
->count
;
860 for (unsigned i
= 0; i
< spec_count
; i
++)
862 speculative_call_target item
863 = csum
->speculative_call_targets
[i
];
864 n2
= find_func_by_profile_id (item
.target_id
);
870 "Indirect call -> direct call from"
871 " other module %s => %s, prob %3.2f\n",
874 item
.target_probability
875 / (float) REG_BR_PROB_BASE
);
877 if (item
.target_probability
< REG_BR_PROB_BASE
/ 2)
883 "probability is too low.\n");
885 else if (!e
->maybe_hot_p ())
890 "Not speculating: call is cold.\n");
892 else if (n2
->get_availability () <= AVAIL_INTERPOSABLE
893 && n2
->can_be_discarded_p ())
898 "Not speculating: target is overwritable "
899 "and can be discarded.\n");
901 else if (!check_argument_count (n2
, e
))
907 "parameter count mismatch\n");
909 else if (e
->indirect_info
->polymorphic
910 && !opt_for_fn (n
->decl
, flag_devirtualize
)
911 && !possible_polymorphic_call_target_p (e
, n2
))
917 "function is not in the polymorphic "
918 "call target list\n");
922 /* Target may be overwritable, but profile says that
923 control flow goes to this particular implementation
924 of N2. Speculate on the local alias to allow
926 if (!n2
->can_be_discarded_p ())
929 alias
= dyn_cast
<cgraph_node
*>
930 (n2
->noninterposable_alias ());
935 profile_probability prob
936 = profile_probability::from_reg_br_prob_base
937 (item
.target_probability
).adjusted ();
938 e
->make_speculative (n2
,
939 orig
.apply_probability (prob
),
949 "Function with profile-id %i not found.\n",
957 ipa_update_overall_fn_summary (n
);
959 if (node_map_initialized
)
961 if (dump_file
&& nindirect
)
963 "%i indirect calls trained.\n"
964 "%i (%3.2f%%) have common target.\n"
965 "%i (%3.2f%%) targets was not found.\n"
966 "%i (%3.2f%%) targets had parameter count mismatch.\n"
967 "%i (%3.2f%%) targets was not in polymorphic call target list.\n"
968 "%i (%3.2f%%) speculations seems useless.\n"
969 "%i (%3.2f%%) speculations produced.\n",
971 ncommon
, ncommon
* 100.0 / nindirect
,
972 nunknown
, nunknown
* 100.0 / nindirect
,
973 nmismatch
, nmismatch
* 100.0 / nindirect
,
974 nimpossible
, nimpossible
* 100.0 / nindirect
,
975 nuseless
, nuseless
* 100.0 / nindirect
,
976 nconverted
, nconverted
* 100.0 / nindirect
);
978 order
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
979 order_pos
= ipa_reverse_postorder (order
);
980 for (i
= order_pos
- 1; i
>= 0; i
--)
983 && opt_for_fn (order
[i
]->decl
, flag_ipa_profile
)
984 && ipa_propagate_frequency (order
[i
]))
986 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
987 if (e
->callee
->local
&& !e
->callee
->aux
)
989 something_changed
= true;
990 e
->callee
->aux
= (void *)1;
993 order
[i
]->aux
= NULL
;
996 while (something_changed
)
998 something_changed
= false;
999 for (i
= order_pos
- 1; i
>= 0; i
--)
1002 && opt_for_fn (order
[i
]->decl
, flag_ipa_profile
)
1003 && ipa_propagate_frequency (order
[i
]))
1005 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1006 if (e
->callee
->local
&& !e
->callee
->aux
)
1008 something_changed
= true;
1009 e
->callee
->aux
= (void *)1;
1012 order
[i
]->aux
= NULL
;
1017 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1018 symtab
->dump (dump_file
);
1028 const pass_data pass_data_ipa_profile
=
1030 IPA_PASS
, /* type */
1031 "profile_estimate", /* name */
1032 OPTGROUP_NONE
, /* optinfo_flags */
1033 TV_IPA_PROFILE
, /* tv_id */
1034 0, /* properties_required */
1035 0, /* properties_provided */
1036 0, /* properties_destroyed */
1037 0, /* todo_flags_start */
1038 0, /* todo_flags_finish */
1041 class pass_ipa_profile
: public ipa_opt_pass_d
1044 pass_ipa_profile (gcc::context
*ctxt
)
1045 : ipa_opt_pass_d (pass_data_ipa_profile
, ctxt
,
1046 ipa_profile_generate_summary
, /* generate_summary */
1047 ipa_profile_write_summary
, /* write_summary */
1048 ipa_profile_read_summary
, /* read_summary */
1049 NULL
, /* write_optimization_summary */
1050 NULL
, /* read_optimization_summary */
1051 NULL
, /* stmt_fixup */
1052 0, /* function_transform_todo_flags_start */
1053 NULL
, /* function_transform */
1054 NULL
) /* variable_transform */
1057 /* opt_pass methods: */
1058 bool gate (function
*) final override
{ return flag_ipa_profile
|| in_lto_p
; }
1059 unsigned int execute (function
*) final override
{ return ipa_profile (); }
1061 }; // class pass_ipa_profile
1066 make_pass_ipa_profile (gcc::context
*ctxt
)
1068 return new pass_ipa_profile (ctxt
);
1071 /* Reset all state within ipa-profile.cc so that we can rerun the compiler
1072 within the same process. For use by toplev::finalize. */
1075 ipa_profile_cc_finalize (void)