1 /* Basic IPA optimizations based on profile.
2 Copyright (C) 2003-2014 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 complette only with LTO,
26 otherwise it contains information only about the current unit.
28 Similar histogram is also estimated by coverage runtime. This histogram
29 is not dependent on LTO, but it suffers from various defects; first
30 gcov runtime is not weighting individual basic block by estimated execution
31 time and second the merging of multiple runs makes assumption that the
32 histogram distribution did not change. Consequentely histogram constructed
33 here may be more precise.
35 The information is used to set hot/cold thresholds.
36 - Next speculative indirect call resolution is performed: the local
37 profile pass assigns profile-id to each function and provide us with a
38 histogram specifying the most common target. We look up the callgraph
39 node corresponding to the target and produce a speculative call.
41 This call may or may not survive through IPA optimization based on decision
43 - Finally we propagate the following flags: unlikely executed, executed
44 once, executed at startup and executed at exit. These flags are used to
45 control code size/performance threshold and and code placement (by producing
46 .text.unlikely/.text.hot/.text.startup/.text.exit subsections). */
49 #include "coretypes.h"
53 #include "tree-pass.h"
54 #include "tree-ssa-alias.h"
55 #include "internal-fn.h"
56 #include "gimple-expr.h"
58 #include "gimple-iterator.h"
61 #include "tree-iterator.h"
62 #include "ipa-utils.h"
65 #include "value-prof.h"
66 #include "alloc-pool.h"
67 #include "tree-inline.h"
68 #include "lto-streamer.h"
69 #include "data-streamer.h"
70 #include "ipa-inline.h"
72 /* Entry in the histogram. */
74 struct histogram_entry
81 /* Histogram of profile values.
82 The histogram is represented as an ordered vector of entries allocated via
83 histogram_pool. During construction a separate hashtable is kept to lookup
86 vec
<histogram_entry
*> histogram
;
87 static alloc_pool histogram_pool
;
89 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
91 struct histogram_hash
: typed_noop_remove
<histogram_entry
>
93 typedef histogram_entry value_type
;
94 typedef histogram_entry compare_type
;
95 static inline hashval_t
hash (const value_type
*);
96 static inline int equal (const value_type
*, const compare_type
*);
100 histogram_hash::hash (const histogram_entry
*val
)
106 histogram_hash::equal (const histogram_entry
*val
, const histogram_entry
*val2
)
108 return val
->count
== val2
->count
;
111 /* Account TIME and SIZE executed COUNT times into HISTOGRAM.
112 HASHTABLE is the on-side hash kept to avoid duplicates. */
115 account_time_size (hash_table
<histogram_hash
> *hashtable
,
116 vec
<histogram_entry
*> &histogram
,
117 gcov_type count
, int time
, int size
)
119 histogram_entry key
= {count
, 0, 0};
120 histogram_entry
**val
= hashtable
->find_slot (&key
, INSERT
);
124 *val
= (histogram_entry
*) pool_alloc (histogram_pool
);
126 histogram
.safe_push (*val
);
128 (*val
)->time
+= time
;
129 (*val
)->size
+= size
;
133 cmp_counts (const void *v1
, const void *v2
)
135 const histogram_entry
*h1
= *(const histogram_entry
* const *)v1
;
136 const histogram_entry
*h2
= *(const histogram_entry
* const *)v2
;
137 if (h1
->count
< h2
->count
)
139 if (h1
->count
> h2
->count
)
144 /* Dump HISTOGRAM to FILE. */
147 dump_histogram (FILE *file
, vec
<histogram_entry
*> histogram
)
150 gcov_type overall_time
= 0, cumulated_time
= 0, cumulated_size
= 0, overall_size
= 0;
152 fprintf (dump_file
, "Histogram:\n");
153 for (i
= 0; i
< histogram
.length (); i
++)
155 overall_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
156 overall_size
+= histogram
[i
]->size
;
162 for (i
= 0; i
< histogram
.length (); i
++)
164 cumulated_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
165 cumulated_size
+= histogram
[i
]->size
;
166 fprintf (file
, " %"PRId64
": time:%i (%2.2f) size:%i (%2.2f)\n",
167 (int64_t) histogram
[i
]->count
,
169 cumulated_time
* 100.0 / overall_time
,
171 cumulated_size
* 100.0 / overall_size
);
175 /* Collect histogram from CFG profiles. */
178 ipa_profile_generate_summary (void)
180 struct cgraph_node
*node
;
181 gimple_stmt_iterator gsi
;
184 hash_table
<histogram_hash
> hashtable (10);
185 histogram_pool
= create_alloc_pool ("IPA histogram", sizeof (struct histogram_entry
),
188 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
189 FOR_EACH_BB_FN (bb
, DECL_STRUCT_FUNCTION (node
->decl
))
193 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
195 gimple stmt
= gsi_stmt (gsi
);
196 if (gimple_code (stmt
) == GIMPLE_CALL
197 && !gimple_call_fndecl (stmt
))
200 h
= gimple_histogram_value_of_type
201 (DECL_STRUCT_FUNCTION (node
->decl
),
202 stmt
, HIST_TYPE_INDIR_CALL
);
203 /* No need to do sanity check: gimple_ic_transform already
204 takes away bad histograms. */
207 /* counter 0 is target, counter 1 is number of execution we called target,
208 counter 2 is total number of executions. */
209 if (h
->hvalue
.counters
[2])
211 struct cgraph_edge
* e
= cgraph_edge (node
, stmt
);
212 if (e
&& !e
->indirect_unknown_callee
)
214 e
->indirect_info
->common_target_id
215 = h
->hvalue
.counters
[0];
216 e
->indirect_info
->common_target_probability
217 = GCOV_COMPUTE_SCALE (h
->hvalue
.counters
[1], h
->hvalue
.counters
[2]);
218 if (e
->indirect_info
->common_target_probability
> REG_BR_PROB_BASE
)
221 fprintf (dump_file
, "Probability capped to 1\n");
222 e
->indirect_info
->common_target_probability
= REG_BR_PROB_BASE
;
225 gimple_remove_histogram_value (DECL_STRUCT_FUNCTION (node
->decl
),
229 time
+= estimate_num_insns (stmt
, &eni_time_weights
);
230 size
+= estimate_num_insns (stmt
, &eni_size_weights
);
232 account_time_size (&hashtable
, histogram
, bb
->count
, time
, size
);
234 histogram
.qsort (cmp_counts
);
237 /* Serialize the ipa info for lto. */
240 ipa_profile_write_summary (void)
242 struct lto_simple_output_block
*ob
243 = lto_create_simple_output_block (LTO_section_ipa_profile
);
246 streamer_write_uhwi_stream (ob
->main_stream
, histogram
.length ());
247 for (i
= 0; i
< histogram
.length (); i
++)
249 streamer_write_gcov_count_stream (ob
->main_stream
, histogram
[i
]->count
);
250 streamer_write_uhwi_stream (ob
->main_stream
, histogram
[i
]->time
);
251 streamer_write_uhwi_stream (ob
->main_stream
, histogram
[i
]->size
);
253 lto_destroy_simple_output_block (ob
);
256 /* Deserialize the ipa info for lto. */
259 ipa_profile_read_summary (void)
261 struct lto_file_decl_data
** file_data_vec
262 = lto_get_file_decl_data ();
263 struct lto_file_decl_data
* file_data
;
266 hash_table
<histogram_hash
> hashtable (10);
267 histogram_pool
= create_alloc_pool ("IPA histogram", sizeof (struct histogram_entry
),
270 while ((file_data
= file_data_vec
[j
++]))
274 struct lto_input_block
*ib
275 = lto_create_simple_input_block (file_data
,
276 LTO_section_ipa_profile
,
280 unsigned int num
= streamer_read_uhwi (ib
);
282 for (n
= 0; n
< num
; n
++)
284 gcov_type count
= streamer_read_gcov_count (ib
);
285 int time
= streamer_read_uhwi (ib
);
286 int size
= streamer_read_uhwi (ib
);
287 account_time_size (&hashtable
, histogram
,
290 lto_destroy_simple_input_block (file_data
,
291 LTO_section_ipa_profile
,
295 histogram
.qsort (cmp_counts
);
298 /* Data used by ipa_propagate_frequency. */
300 struct ipa_propagate_frequency_data
302 bool maybe_unlikely_executed
;
303 bool maybe_executed_once
;
304 bool only_called_at_startup
;
305 bool only_called_at_exit
;
308 /* Worker for ipa_propagate_frequency_1. */
311 ipa_propagate_frequency_1 (struct cgraph_node
*node
, void *data
)
313 struct ipa_propagate_frequency_data
*d
;
314 struct cgraph_edge
*edge
;
316 d
= (struct ipa_propagate_frequency_data
*)data
;
317 for (edge
= node
->callers
;
318 edge
&& (d
->maybe_unlikely_executed
|| d
->maybe_executed_once
319 || d
->only_called_at_startup
|| d
->only_called_at_exit
);
320 edge
= edge
->next_caller
)
322 if (edge
->caller
!= node
)
324 d
->only_called_at_startup
&= edge
->caller
->only_called_at_startup
;
325 /* It makes sense to put main() together with the static constructors.
326 It will be executed for sure, but rest of functions called from
327 main are definitely not at startup only. */
328 if (MAIN_NAME_P (DECL_NAME (edge
->caller
->decl
)))
329 d
->only_called_at_startup
= 0;
330 d
->only_called_at_exit
&= edge
->caller
->only_called_at_exit
;
333 /* When profile feedback is available, do not try to propagate too hard;
334 counts are already good guide on function frequencies and roundoff
335 errors can make us to push function into unlikely section even when
336 it is executed by the train run. Transfer the function only if all
337 callers are unlikely executed. */
338 if (profile_info
&& flag_branch_probabilities
339 && (edge
->caller
->frequency
!= NODE_FREQUENCY_UNLIKELY_EXECUTED
340 || (edge
->caller
->global
.inlined_to
341 && edge
->caller
->global
.inlined_to
->frequency
342 != NODE_FREQUENCY_UNLIKELY_EXECUTED
)))
343 d
->maybe_unlikely_executed
= false;
344 if (!edge
->frequency
)
346 switch (edge
->caller
->frequency
)
348 case NODE_FREQUENCY_UNLIKELY_EXECUTED
:
350 case NODE_FREQUENCY_EXECUTED_ONCE
:
351 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
352 fprintf (dump_file
, " Called by %s that is executed once\n",
353 edge
->caller
->name ());
354 d
->maybe_unlikely_executed
= false;
355 if (inline_edge_summary (edge
)->loop_depth
)
357 d
->maybe_executed_once
= false;
358 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
359 fprintf (dump_file
, " Called in loop\n");
362 case NODE_FREQUENCY_HOT
:
363 case NODE_FREQUENCY_NORMAL
:
364 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
365 fprintf (dump_file
, " Called by %s that is normal or hot\n",
366 edge
->caller
->name ());
367 d
->maybe_unlikely_executed
= false;
368 d
->maybe_executed_once
= false;
375 /* Return ture if NODE contains hot calls. */
378 contains_hot_call_p (struct cgraph_node
*node
)
380 struct cgraph_edge
*e
;
381 for (e
= node
->callees
; e
; e
= e
->next_callee
)
382 if (cgraph_maybe_hot_edge_p (e
))
384 else if (!e
->inline_failed
385 && contains_hot_call_p (e
->callee
))
387 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
388 if (cgraph_maybe_hot_edge_p (e
))
393 /* See if the frequency of NODE can be updated based on frequencies of its
396 ipa_propagate_frequency (struct cgraph_node
*node
)
398 struct ipa_propagate_frequency_data d
= {true, true, true, true};
399 bool changed
= false;
401 /* We can not propagate anything useful about externally visible functions
402 nor about virtuals. */
403 if (!node
->local
.local
405 || (flag_devirtualize
&& DECL_VIRTUAL_P (node
->decl
)))
407 gcc_assert (node
->analyzed
);
408 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
409 fprintf (dump_file
, "Processing frequency %s\n", node
->name ());
411 cgraph_for_node_and_aliases (node
, ipa_propagate_frequency_1
, &d
, true);
413 if ((d
.only_called_at_startup
&& !d
.only_called_at_exit
)
414 && !node
->only_called_at_startup
)
416 node
->only_called_at_startup
= true;
418 fprintf (dump_file
, "Node %s promoted to only called at startup.\n",
422 if ((d
.only_called_at_exit
&& !d
.only_called_at_startup
)
423 && !node
->only_called_at_exit
)
425 node
->only_called_at_exit
= true;
427 fprintf (dump_file
, "Node %s promoted to only called at exit.\n",
432 /* With profile we can decide on hot/normal based on count. */
436 if (node
->count
>= get_hot_bb_threshold ())
439 hot
|= contains_hot_call_p (node
);
442 if (node
->frequency
!= NODE_FREQUENCY_HOT
)
445 fprintf (dump_file
, "Node %s promoted to hot.\n",
447 node
->frequency
= NODE_FREQUENCY_HOT
;
452 else if (node
->frequency
== NODE_FREQUENCY_HOT
)
455 fprintf (dump_file
, "Node %s reduced to normal.\n",
457 node
->frequency
= NODE_FREQUENCY_NORMAL
;
461 /* These come either from profile or user hints; never update them. */
462 if (node
->frequency
== NODE_FREQUENCY_HOT
463 || node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
)
465 if (d
.maybe_unlikely_executed
)
467 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
469 fprintf (dump_file
, "Node %s promoted to unlikely executed.\n",
473 else if (d
.maybe_executed_once
&& node
->frequency
!= NODE_FREQUENCY_EXECUTED_ONCE
)
475 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
477 fprintf (dump_file
, "Node %s promoted to executed once.\n",
484 /* Simple ipa profile pass propagating frequencies across the callgraph. */
489 struct cgraph_node
**order
;
490 struct cgraph_edge
*e
;
492 bool something_changed
= false;
494 gcov_type overall_time
= 0, cutoff
= 0, cumulated
= 0, overall_size
= 0;
495 struct cgraph_node
*n
,*n2
;
496 int nindirect
= 0, ncommon
= 0, nunknown
= 0, nuseless
= 0, nconverted
= 0;
497 bool node_map_initialized
= false;
500 dump_histogram (dump_file
, histogram
);
501 for (i
= 0; i
< (int)histogram
.length (); i
++)
503 overall_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
504 overall_size
+= histogram
[i
]->size
;
510 gcc_assert (overall_size
);
513 gcov_type min
, cumulated_time
= 0, cumulated_size
= 0;
515 fprintf (dump_file
, "Overall time: %"PRId64
"\n",
516 (int64_t)overall_time
);
517 min
= get_hot_bb_threshold ();
518 for (i
= 0; i
< (int)histogram
.length () && histogram
[i
]->count
>= min
;
521 cumulated_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
522 cumulated_size
+= histogram
[i
]->size
;
524 fprintf (dump_file
, "GCOV min count: %"PRId64
525 " Time:%3.2f%% Size:%3.2f%%\n",
527 cumulated_time
* 100.0 / overall_time
,
528 cumulated_size
* 100.0 / overall_size
);
530 cutoff
= (overall_time
* PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE
) + 500) / 1000;
532 for (i
= 0; cumulated
< cutoff
; i
++)
534 cumulated
+= histogram
[i
]->count
* histogram
[i
]->time
;
535 threshold
= histogram
[i
]->count
;
541 gcov_type cumulated_time
= 0, cumulated_size
= 0;
544 i
< (int)histogram
.length () && histogram
[i
]->count
>= threshold
;
547 cumulated_time
+= histogram
[i
]->count
* histogram
[i
]->time
;
548 cumulated_size
+= histogram
[i
]->size
;
550 fprintf (dump_file
, "Determined min count: %"PRId64
551 " Time:%3.2f%% Size:%3.2f%%\n",
553 cumulated_time
* 100.0 / overall_time
,
554 cumulated_size
* 100.0 / overall_size
);
556 if (threshold
> get_hot_bb_threshold ()
560 fprintf (dump_file
, "Threshold updated.\n");
561 set_hot_bb_threshold (threshold
);
564 histogram
.release ();
565 free_alloc_pool (histogram_pool
);
567 /* Produce speculative calls: we saved common traget from porfiling into
568 e->common_target_id. Now, at link time, we can look up corresponding
569 function node and produce speculative call. */
571 FOR_EACH_DEFINED_FUNCTION (n
)
575 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
579 if (e
->indirect_info
->common_target_id
)
581 if (!node_map_initialized
)
582 init_node_map (false);
583 node_map_initialized
= true;
585 n2
= find_func_by_profile_id (e
->indirect_info
->common_target_id
);
590 fprintf (dump_file
, "Indirect call -> direct call from"
591 " other module %s/%i => %s/%i, prob %3.2f\n",
592 xstrdup (n
->name ()), n
->order
,
593 xstrdup (n2
->name ()), n2
->order
,
594 e
->indirect_info
->common_target_probability
595 / (float)REG_BR_PROB_BASE
);
597 if (e
->indirect_info
->common_target_probability
598 < REG_BR_PROB_BASE
/ 2)
603 "Not speculating: probability is too low.\n");
605 else if (!cgraph_maybe_hot_edge_p (e
))
610 "Not speculating: call is cold.\n");
612 else if (cgraph_function_body_availability (n2
)
613 <= AVAIL_OVERWRITABLE
614 && symtab_can_be_discarded (n2
))
619 "Not speculating: target is overwritable "
620 "and can be discarded.\n");
624 /* Target may be overwritable, but profile says that
625 control flow goes to this particular implementation
626 of N2. Speculate on the local alias to allow inlining.
628 if (!symtab_can_be_discarded (n2
))
631 alias
= cgraph (symtab_nonoverwritable_alias
637 cgraph_turn_edge_to_speculative
639 apply_scale (e
->count
,
640 e
->indirect_info
->common_target_probability
),
641 apply_scale (e
->frequency
,
642 e
->indirect_info
->common_target_probability
));
649 fprintf (dump_file
, "Function with profile-id %i not found.\n",
650 e
->indirect_info
->common_target_id
);
656 inline_update_overall_summary (n
);
658 if (node_map_initialized
)
660 if (dump_file
&& nindirect
)
662 "%i indirect calls trained.\n"
663 "%i (%3.2f%%) have common target.\n"
664 "%i (%3.2f%%) targets was not found.\n"
665 "%i (%3.2f%%) speculations seems useless.\n"
666 "%i (%3.2f%%) speculations produced.\n",
668 ncommon
, ncommon
* 100.0 / nindirect
,
669 nunknown
, nunknown
* 100.0 / nindirect
,
670 nuseless
, nuseless
* 100.0 / nindirect
,
671 nconverted
, nconverted
* 100.0 / nindirect
);
673 order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
674 order_pos
= ipa_reverse_postorder (order
);
675 for (i
= order_pos
- 1; i
>= 0; i
--)
677 if (order
[i
]->local
.local
&& ipa_propagate_frequency (order
[i
]))
679 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
680 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
682 something_changed
= true;
683 e
->callee
->aux
= (void *)1;
686 order
[i
]->aux
= NULL
;
689 while (something_changed
)
691 something_changed
= false;
692 for (i
= order_pos
- 1; i
>= 0; i
--)
694 if (order
[i
]->aux
&& ipa_propagate_frequency (order
[i
]))
696 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
697 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
699 something_changed
= true;
700 e
->callee
->aux
= (void *)1;
703 order
[i
]->aux
= NULL
;
712 const pass_data pass_data_ipa_profile
=
715 "profile_estimate", /* name */
716 OPTGROUP_NONE
, /* optinfo_flags */
717 TV_IPA_PROFILE
, /* tv_id */
718 0, /* properties_required */
719 0, /* properties_provided */
720 0, /* properties_destroyed */
721 0, /* todo_flags_start */
722 0, /* todo_flags_finish */
725 class pass_ipa_profile
: public ipa_opt_pass_d
728 pass_ipa_profile (gcc::context
*ctxt
)
729 : ipa_opt_pass_d (pass_data_ipa_profile
, ctxt
,
730 ipa_profile_generate_summary
, /* generate_summary */
731 ipa_profile_write_summary
, /* write_summary */
732 ipa_profile_read_summary
, /* read_summary */
733 NULL
, /* write_optimization_summary */
734 NULL
, /* read_optimization_summary */
735 NULL
, /* stmt_fixup */
736 0, /* function_transform_todo_flags_start */
737 NULL
, /* function_transform */
738 NULL
) /* variable_transform */
741 /* opt_pass methods: */
742 virtual bool gate (function
*) { return flag_ipa_profile
; }
743 virtual unsigned int execute (function
*) { return ipa_profile (); }
745 }; // class pass_ipa_profile
750 make_pass_ipa_profile (gcc::context
*ctxt
)
752 return new pass_ipa_profile (ctxt
);