2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / auto-profile.c
blob9246eadad7dc0c810a6b74c36f74c0f5d84d5d2c
1 /* Read and annotate call graph profile from the auto profile data file.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Contributed by Dehao Chen (dehao@google.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
24 #include <string.h>
25 #include <map>
26 #include <set>
28 #include "coretypes.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "tree-pass.h"
36 #include "flags.h"
37 #include "predict.h"
38 #include "tm.h"
39 #include "hard-reg-set.h"
40 #include "input.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "basic-block.h"
45 #include "diagnostic-core.h"
46 #include "gcov-io.h"
47 #include "profile.h"
48 #include "langhooks.h"
49 #include "opts.h"
50 #include "tree-pass.h"
51 #include "cfgloop.h"
52 #include "tree-ssa-alias.h"
53 #include "tree-cfg.h"
54 #include "tree-cfgcleanup.h"
55 #include "tree-ssa-operands.h"
56 #include "tree-into-ssa.h"
57 #include "internal-fn.h"
58 #include "is-a.h"
59 #include "gimple-expr.h"
60 #include "gimple.h"
61 #include "gimple-iterator.h"
62 #include "gimple-ssa.h"
63 #include "plugin-api.h"
64 #include "ipa-ref.h"
65 #include "cgraph.h"
66 #include "value-prof.h"
67 #include "coverage.h"
68 #include "params.h"
69 #include "alloc-pool.h"
70 #include "symbol-summary.h"
71 #include "ipa-prop.h"
72 #include "ipa-inline.h"
73 #include "tree-inline.h"
74 #include "stringpool.h"
75 #include "auto-profile.h"
77 /* The following routines implements AutoFDO optimization.
79 This optimization uses sampling profiles to annotate basic block counts
80 and uses heuristics to estimate branch probabilities.
82 There are three phases in AutoFDO:
84 Phase 1: Read profile from the profile data file.
85 The following info is read from the profile datafile:
86 * string_table: a map between function name and its index.
87 * autofdo_source_profile: a map from function_instance name to
88 function_instance. This is represented as a forest of
89 function_instances.
90 * WorkingSet: a histogram of how many instructions are covered for a
91 given percentage of total cycles. This is describing the binary
92 level information (not source level). This info is used to help
93 decide if we want aggressive optimizations that could increase
94 code footprint (e.g. loop unroll etc.)
95 A function instance is an instance of function that could either be a
96 standalone symbol, or a clone of a function that is inlined into another
97 function.
99 Phase 2: Early inline + value profile transformation.
100 Early inline uses autofdo_source_profile to find if a callsite is:
101 * inlined in the profiled binary.
102 * callee body is hot in the profiling run.
103 If both condition satisfies, early inline will inline the callsite
104 regardless of the code growth.
105 Phase 2 is an iterative process. During each iteration, we also check
106 if an indirect callsite is promoted and inlined in the profiling run.
107 If yes, vpt will happen to force promote it and in the next iteration,
108 einline will inline the promoted callsite in the next iteration.
110 Phase 3: Annotate control flow graph.
111 AutoFDO uses a separate pass to:
112 * Annotate basic block count
113 * Estimate branch probability
115 After the above 3 phases, all profile is readily annotated on the GCC IR.
116 AutoFDO tries to reuse all FDO infrastructure as much as possible to make
117 use of the profile. E.g. it uses existing mechanism to calculate the basic
118 block/edge frequency, as well as the cgraph node/edge count.
121 #define DEFAULT_AUTO_PROFILE_FILE "fbdata.afdo"
122 #define AUTO_PROFILE_VERSION 1
124 namespace autofdo
127 /* Represent a source location: (function_decl, lineno). */
128 typedef std::pair<tree, unsigned> decl_lineno;
130 /* Represent an inline stack. vector[0] is the leaf node. */
131 typedef auto_vec<decl_lineno> inline_stack;
133 /* String array that stores function names. */
134 typedef auto_vec<char *> string_vector;
136 /* Map from function name's index in string_table to target's
137 execution count. */
138 typedef std::map<unsigned, gcov_type> icall_target_map;
140 /* Set of gimple stmts. Used to track if the stmt has already been promoted
141 to direct call. */
142 typedef std::set<gimple> stmt_set;
144 /* Represent count info of an inline stack. */
145 struct count_info
147 /* Sampled count of the inline stack. */
148 gcov_type count;
150 /* Map from indirect call target to its sample count. */
151 icall_target_map targets;
153 /* Whether this inline stack is already used in annotation.
155 Each inline stack should only be used to annotate IR once.
156 This will be enforced when instruction-level discriminator
157 is supported. */
158 bool annotated;
161 /* operator< for "const char *". */
162 struct string_compare
164 bool operator()(const char *a, const char *b) const
166 return strcmp (a, b) < 0;
170 /* Store a string array, indexed by string position in the array. */
171 class string_table
173 public:
174 string_table ()
177 ~string_table ();
179 /* For a given string, returns its index. */
180 int get_index (const char *name) const;
182 /* For a given decl, returns the index of the decl name. */
183 int get_index_by_decl (tree decl) const;
185 /* For a given index, returns the string. */
186 const char *get_name (int index) const;
188 /* Read profile, return TRUE on success. */
189 bool read ();
191 private:
192 typedef std::map<const char *, unsigned, string_compare> string_index_map;
193 string_vector vector_;
194 string_index_map map_;
197 /* Profile of a function instance:
198 1. total_count of the function.
199 2. head_count (entry basic block count) of the function (only valid when
200 function is a top-level function_instance, i.e. it is the original copy
201 instead of the inlined copy).
202 3. map from source location (decl_lineno) to profile (count_info).
203 4. map from callsite to callee function_instance. */
204 class function_instance
206 public:
207 typedef auto_vec<function_instance *> function_instance_stack;
209 /* Read the profile and return a function_instance with head count as
210 HEAD_COUNT. Recursively read callsites to create nested function_instances
211 too. STACK is used to track the recursive creation process. */
212 static function_instance *
213 read_function_instance (function_instance_stack *stack,
214 gcov_type head_count);
216 /* Recursively deallocate all callsites (nested function_instances). */
217 ~function_instance ();
219 /* Accessors. */
221 name () const
223 return name_;
225 gcov_type
226 total_count () const
228 return total_count_;
230 gcov_type
231 head_count () const
233 return head_count_;
236 /* Traverse callsites of the current function_instance to find one at the
237 location of LINENO and callee name represented in DECL. */
238 function_instance *get_function_instance_by_decl (unsigned lineno,
239 tree decl) const;
241 /* Store the profile info for LOC in INFO. Return TRUE if profile info
242 is found. */
243 bool get_count_info (location_t loc, count_info *info) const;
245 /* Read the inlined indirect call target profile for STMT and store it in
246 MAP, return the total count for all inlined indirect calls. */
247 gcov_type find_icall_target_map (gcall *stmt, icall_target_map *map) const;
249 /* Sum of counts that is used during annotation. */
250 gcov_type total_annotated_count () const;
252 /* Mark LOC as annotated. */
253 void mark_annotated (location_t loc);
255 private:
256 /* Callsite, represented as (decl_lineno, callee_function_name_index). */
257 typedef std::pair<unsigned, unsigned> callsite;
259 /* Map from callsite to callee function_instance. */
260 typedef std::map<callsite, function_instance *> callsite_map;
262 function_instance (unsigned name, gcov_type head_count)
263 : name_ (name), total_count_ (0), head_count_ (head_count)
267 /* Map from source location (decl_lineno) to profile (count_info). */
268 typedef std::map<unsigned, count_info> position_count_map;
270 /* function_instance name index in the string_table. */
271 unsigned name_;
273 /* Total sample count. */
274 gcov_type total_count_;
276 /* Entry BB's sample count. */
277 gcov_type head_count_;
279 /* Map from callsite location to callee function_instance. */
280 callsite_map callsites;
282 /* Map from source location to count_info. */
283 position_count_map pos_counts;
286 /* Profile for all functions. */
287 class autofdo_source_profile
289 public:
290 static autofdo_source_profile *
291 create ()
293 autofdo_source_profile *map = new autofdo_source_profile ();
295 if (map->read ())
296 return map;
297 delete map;
298 return NULL;
301 ~autofdo_source_profile ();
303 /* For a given DECL, returns the top-level function_instance. */
304 function_instance *get_function_instance_by_decl (tree decl) const;
306 /* Find count_info for a given gimple STMT. If found, store the count_info
307 in INFO and return true; otherwise return false. */
308 bool get_count_info (gimple stmt, count_info *info) const;
310 /* Find total count of the callee of EDGE. */
311 gcov_type get_callsite_total_count (struct cgraph_edge *edge) const;
313 /* Update value profile INFO for STMT from the inlined indirect callsite.
314 Return true if INFO is updated. */
315 bool update_inlined_ind_target (gcall *stmt, count_info *info);
317 /* Mark LOC as annotated. */
318 void mark_annotated (location_t loc);
320 private:
321 /* Map from function_instance name index (in string_table) to
322 function_instance. */
323 typedef std::map<unsigned, function_instance *> name_function_instance_map;
325 autofdo_source_profile () {}
327 /* Read AutoFDO profile and returns TRUE on success. */
328 bool read ();
330 /* Return the function_instance in the profile that correspond to the
331 inline STACK. */
332 function_instance *
333 get_function_instance_by_inline_stack (const inline_stack &stack) const;
335 name_function_instance_map map_;
338 /* Store the strings read from the profile data file. */
339 static string_table *afdo_string_table;
341 /* Store the AutoFDO source profile. */
342 static autofdo_source_profile *afdo_source_profile;
344 /* gcov_ctr_summary structure to store the profile_info. */
345 static struct gcov_ctr_summary *afdo_profile_info;
347 /* Helper functions. */
349 /* Return the original name of NAME: strip the suffix that starts
350 with '.' Caller is responsible for freeing RET. */
352 static char *
353 get_original_name (const char *name)
355 char *ret = xstrdup (name);
356 char *find = strchr (ret, '.');
357 if (find != NULL)
358 *find = 0;
359 return ret;
362 /* Return the combined location, which is a 32bit integer in which
363 higher 16 bits stores the line offset of LOC to the start lineno
364 of DECL, The lower 16 bits stores the discriminator. */
366 static unsigned
367 get_combined_location (location_t loc, tree decl)
369 /* TODO: allow more bits for line and less bits for discriminator. */
370 if (LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl) >= (1<<16))
371 warning_at (loc, OPT_Woverflow, "Offset exceeds 16 bytes.");
372 return ((LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) << 16);
375 /* Return the function decl of a given lexical BLOCK. */
377 static tree
378 get_function_decl_from_block (tree block)
380 tree decl;
382 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block) == UNKNOWN_LOCATION))
383 return NULL_TREE;
385 for (decl = BLOCK_ABSTRACT_ORIGIN (block);
386 decl && (TREE_CODE (decl) == BLOCK);
387 decl = BLOCK_ABSTRACT_ORIGIN (decl))
388 if (TREE_CODE (decl) == FUNCTION_DECL)
389 break;
390 return decl;
393 /* Store inline stack for STMT in STACK. */
395 static void
396 get_inline_stack (location_t locus, inline_stack *stack)
398 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
399 return;
401 tree block = LOCATION_BLOCK (locus);
402 if (block && TREE_CODE (block) == BLOCK)
404 int level = 0;
405 for (block = BLOCK_SUPERCONTEXT (block);
406 block && (TREE_CODE (block) == BLOCK);
407 block = BLOCK_SUPERCONTEXT (block))
409 location_t tmp_locus = BLOCK_SOURCE_LOCATION (block);
410 if (LOCATION_LOCUS (tmp_locus) == UNKNOWN_LOCATION)
411 continue;
413 tree decl = get_function_decl_from_block (block);
414 stack->safe_push (
415 std::make_pair (decl, get_combined_location (locus, decl)));
416 locus = tmp_locus;
417 level++;
420 stack->safe_push (
421 std::make_pair (current_function_decl,
422 get_combined_location (locus, current_function_decl)));
425 /* Return STMT's combined location, which is a 32bit integer in which
426 higher 16 bits stores the line offset of LOC to the start lineno
427 of DECL, The lower 16 bits stores the discriminator. */
429 static unsigned
430 get_relative_location_for_stmt (gimple stmt)
432 location_t locus = gimple_location (stmt);
433 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
434 return UNKNOWN_LOCATION;
436 for (tree block = gimple_block (stmt); block && (TREE_CODE (block) == BLOCK);
437 block = BLOCK_SUPERCONTEXT (block))
438 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block)) != UNKNOWN_LOCATION)
439 return get_combined_location (locus,
440 get_function_decl_from_block (block));
441 return get_combined_location (locus, current_function_decl);
444 /* Return true if BB contains indirect call. */
446 static bool
447 has_indirect_call (basic_block bb)
449 gimple_stmt_iterator gsi;
451 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
453 gimple stmt = gsi_stmt (gsi);
454 if (gimple_code (stmt) == GIMPLE_CALL && !gimple_call_internal_p (stmt)
455 && (gimple_call_fn (stmt) == NULL
456 || TREE_CODE (gimple_call_fn (stmt)) != FUNCTION_DECL))
457 return true;
459 return false;
462 /* Member functions for string_table. */
464 /* Deconstructor. */
466 string_table::~string_table ()
468 for (unsigned i = 0; i < vector_.length (); i++)
469 free (vector_[i]);
473 /* Return the index of a given function NAME. Return -1 if NAME is not
474 found in string table. */
477 string_table::get_index (const char *name) const
479 if (name == NULL)
480 return -1;
481 string_index_map::const_iterator iter = map_.find (name);
482 if (iter == map_.end ())
483 return -1;
485 return iter->second;
488 /* Return the index of a given function DECL. Return -1 if DECL is not
489 found in string table. */
492 string_table::get_index_by_decl (tree decl) const
494 char *name
495 = get_original_name (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
496 int ret = get_index (name);
497 free (name);
498 if (ret != -1)
499 return ret;
500 ret = get_index (lang_hooks.dwarf_name (decl, 0));
501 if (ret != -1)
502 return ret;
503 if (DECL_ABSTRACT_ORIGIN (decl))
504 return get_index_by_decl (DECL_ABSTRACT_ORIGIN (decl));
506 return -1;
509 /* Return the function name of a given INDEX. */
511 const char *
512 string_table::get_name (int index) const
514 gcc_assert (index > 0 && index < (int)vector_.length ());
515 return vector_[index];
518 /* Read the string table. Return TRUE if reading is successful. */
520 bool
521 string_table::read ()
523 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FILE_NAMES)
524 return false;
525 /* Skip the length of the section. */
526 gcov_read_unsigned ();
527 /* Read in the file name table. */
528 unsigned string_num = gcov_read_unsigned ();
529 for (unsigned i = 0; i < string_num; i++)
531 vector_.safe_push (get_original_name (gcov_read_string ()));
532 map_[vector_.last ()] = i;
534 return true;
537 /* Member functions for function_instance. */
539 function_instance::~function_instance ()
541 for (callsite_map::iterator iter = callsites.begin ();
542 iter != callsites.end (); ++iter)
543 delete iter->second;
546 /* Traverse callsites of the current function_instance to find one at the
547 location of LINENO and callee name represented in DECL. */
549 function_instance *
550 function_instance::get_function_instance_by_decl (unsigned lineno,
551 tree decl) const
553 int func_name_idx = afdo_string_table->get_index_by_decl (decl);
554 if (func_name_idx != -1)
556 callsite_map::const_iterator ret
557 = callsites.find (std::make_pair (lineno, func_name_idx));
558 if (ret != callsites.end ())
559 return ret->second;
561 func_name_idx
562 = afdo_string_table->get_index (lang_hooks.dwarf_name (decl, 0));
563 if (func_name_idx != -1)
565 callsite_map::const_iterator ret
566 = callsites.find (std::make_pair (lineno, func_name_idx));
567 if (ret != callsites.end ())
568 return ret->second;
570 if (DECL_ABSTRACT_ORIGIN (decl))
571 return get_function_instance_by_decl (lineno, DECL_ABSTRACT_ORIGIN (decl));
573 return NULL;
576 /* Store the profile info for LOC in INFO. Return TRUE if profile info
577 is found. */
579 bool
580 function_instance::get_count_info (location_t loc, count_info *info) const
582 position_count_map::const_iterator iter = pos_counts.find (loc);
583 if (iter == pos_counts.end ())
584 return false;
585 *info = iter->second;
586 return true;
589 /* Mark LOC as annotated. */
591 void
592 function_instance::mark_annotated (location_t loc)
594 position_count_map::iterator iter = pos_counts.find (loc);
595 if (iter == pos_counts.end ())
596 return;
597 iter->second.annotated = true;
600 /* Read the inlined indirect call target profile for STMT and store it in
601 MAP, return the total count for all inlined indirect calls. */
603 gcov_type
604 function_instance::find_icall_target_map (gcall *stmt,
605 icall_target_map *map) const
607 gcov_type ret = 0;
608 unsigned stmt_offset = get_relative_location_for_stmt (stmt);
610 for (callsite_map::const_iterator iter = callsites.begin ();
611 iter != callsites.end (); ++iter)
613 unsigned callee = iter->second->name ();
614 /* Check if callsite location match the stmt. */
615 if (iter->first.first != stmt_offset)
616 continue;
617 struct cgraph_node *node = cgraph_node::get_for_asmname (
618 get_identifier (afdo_string_table->get_name (callee)));
619 if (node == NULL)
620 continue;
621 if (!check_ic_target (stmt, node))
622 continue;
623 (*map)[callee] = iter->second->total_count ();
624 ret += iter->second->total_count ();
626 return ret;
629 /* Read the profile and create a function_instance with head count as
630 HEAD_COUNT. Recursively read callsites to create nested function_instances
631 too. STACK is used to track the recursive creation process. */
633 /* function instance profile format:
635 ENTRY_COUNT: 8 bytes
636 NAME_INDEX: 4 bytes
637 NUM_POS_COUNTS: 4 bytes
638 NUM_CALLSITES: 4 byte
639 POS_COUNT_1:
640 POS_1_OFFSET: 4 bytes
641 NUM_TARGETS: 4 bytes
642 COUNT: 8 bytes
643 TARGET_1:
644 VALUE_PROFILE_TYPE: 4 bytes
645 TARGET_IDX: 8 bytes
646 COUNT: 8 bytes
647 TARGET_2
649 TARGET_n
650 POS_COUNT_2
652 POS_COUNT_N
653 CALLSITE_1:
654 CALLSITE_1_OFFSET: 4 bytes
655 FUNCTION_INSTANCE_PROFILE (nested)
656 CALLSITE_2
658 CALLSITE_n. */
660 function_instance *
661 function_instance::read_function_instance (function_instance_stack *stack,
662 gcov_type head_count)
664 unsigned name = gcov_read_unsigned ();
665 unsigned num_pos_counts = gcov_read_unsigned ();
666 unsigned num_callsites = gcov_read_unsigned ();
667 function_instance *s = new function_instance (name, head_count);
668 stack->safe_push (s);
670 for (unsigned i = 0; i < num_pos_counts; i++)
672 unsigned offset = gcov_read_unsigned () & 0xffff0000;
673 unsigned num_targets = gcov_read_unsigned ();
674 gcov_type count = gcov_read_counter ();
675 s->pos_counts[offset].count = count;
676 for (unsigned j = 0; j < stack->length (); j++)
677 (*stack)[j]->total_count_ += count;
678 for (unsigned j = 0; j < num_targets; j++)
680 /* Only indirect call target histogram is supported now. */
681 gcov_read_unsigned ();
682 gcov_type target_idx = gcov_read_counter ();
683 s->pos_counts[offset].targets[target_idx] = gcov_read_counter ();
686 for (unsigned i = 0; i < num_callsites; i++)
688 unsigned offset = gcov_read_unsigned ();
689 function_instance *callee_function_instance
690 = read_function_instance (stack, 0);
691 s->callsites[std::make_pair (offset, callee_function_instance->name ())]
692 = callee_function_instance;
694 stack->pop ();
695 return s;
698 /* Sum of counts that is used during annotation. */
700 gcov_type
701 function_instance::total_annotated_count () const
703 gcov_type ret = 0;
704 for (callsite_map::const_iterator iter = callsites.begin ();
705 iter != callsites.end (); ++iter)
706 ret += iter->second->total_annotated_count ();
707 for (position_count_map::const_iterator iter = pos_counts.begin ();
708 iter != pos_counts.end (); ++iter)
709 if (iter->second.annotated)
710 ret += iter->second.count;
711 return ret;
714 /* Member functions for autofdo_source_profile. */
716 autofdo_source_profile::~autofdo_source_profile ()
718 for (name_function_instance_map::const_iterator iter = map_.begin ();
719 iter != map_.end (); ++iter)
720 delete iter->second;
723 /* For a given DECL, returns the top-level function_instance. */
725 function_instance *
726 autofdo_source_profile::get_function_instance_by_decl (tree decl) const
728 int index = afdo_string_table->get_index_by_decl (decl);
729 if (index == -1)
730 return NULL;
731 name_function_instance_map::const_iterator ret = map_.find (index);
732 return ret == map_.end () ? NULL : ret->second;
735 /* Find count_info for a given gimple STMT. If found, store the count_info
736 in INFO and return true; otherwise return false. */
738 bool
739 autofdo_source_profile::get_count_info (gimple stmt, count_info *info) const
741 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
742 return false;
744 inline_stack stack;
745 get_inline_stack (gimple_location (stmt), &stack);
746 if (stack.length () == 0)
747 return false;
748 function_instance *s = get_function_instance_by_inline_stack (stack);
749 if (s == NULL)
750 return false;
751 return s->get_count_info (stack[0].second, info);
754 /* Mark LOC as annotated. */
756 void
757 autofdo_source_profile::mark_annotated (location_t loc)
759 inline_stack stack;
760 get_inline_stack (loc, &stack);
761 if (stack.length () == 0)
762 return;
763 function_instance *s = get_function_instance_by_inline_stack (stack);
764 if (s == NULL)
765 return;
766 s->mark_annotated (stack[0].second);
769 /* Update value profile INFO for STMT from the inlined indirect callsite.
770 Return true if INFO is updated. */
772 bool
773 autofdo_source_profile::update_inlined_ind_target (gcall *stmt,
774 count_info *info)
776 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
777 return false;
779 count_info old_info;
780 get_count_info (stmt, &old_info);
781 gcov_type total = 0;
782 for (icall_target_map::const_iterator iter = old_info.targets.begin ();
783 iter != old_info.targets.end (); ++iter)
784 total += iter->second;
786 /* Program behavior changed, original promoted (and inlined) target is not
787 hot any more. Will avoid promote the original target.
789 To check if original promoted target is still hot, we check the total
790 count of the unpromoted targets (stored in old_info). If it is no less
791 than half of the callsite count (stored in INFO), the original promoted
792 target is considered not hot any more. */
793 if (total >= info->count / 2)
794 return false;
796 inline_stack stack;
797 get_inline_stack (gimple_location (stmt), &stack);
798 if (stack.length () == 0)
799 return false;
800 function_instance *s = get_function_instance_by_inline_stack (stack);
801 if (s == NULL)
802 return false;
803 icall_target_map map;
804 if (s->find_icall_target_map (stmt, &map) == 0)
805 return false;
806 for (icall_target_map::const_iterator iter = map.begin ();
807 iter != map.end (); ++iter)
808 info->targets[iter->first] = iter->second;
809 return true;
812 /* Find total count of the callee of EDGE. */
814 gcov_type
815 autofdo_source_profile::get_callsite_total_count (
816 struct cgraph_edge *edge) const
818 inline_stack stack;
819 stack.safe_push (std::make_pair (edge->callee->decl, 0));
820 get_inline_stack (gimple_location (edge->call_stmt), &stack);
822 function_instance *s = get_function_instance_by_inline_stack (stack);
823 if (s == NULL
824 || afdo_string_table->get_index (IDENTIFIER_POINTER (
825 DECL_ASSEMBLER_NAME (edge->callee->decl))) != s->name ())
826 return 0;
828 return s->total_count ();
831 /* Read AutoFDO profile and returns TRUE on success. */
833 /* source profile format:
835 GCOV_TAG_AFDO_FUNCTION: 4 bytes
836 LENGTH: 4 bytes
837 NUM_FUNCTIONS: 4 bytes
838 FUNCTION_INSTANCE_1
839 FUNCTION_INSTANCE_2
841 FUNCTION_INSTANCE_N. */
843 bool
844 autofdo_source_profile::read ()
846 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FUNCTION)
848 inform (0, "Not expected TAG.");
849 return false;
852 /* Skip the length of the section. */
853 gcov_read_unsigned ();
855 /* Read in the function/callsite profile, and store it in local
856 data structure. */
857 unsigned function_num = gcov_read_unsigned ();
858 for (unsigned i = 0; i < function_num; i++)
860 function_instance::function_instance_stack stack;
861 function_instance *s = function_instance::read_function_instance (
862 &stack, gcov_read_counter ());
863 afdo_profile_info->sum_all += s->total_count ();
864 map_[s->name ()] = s;
866 return true;
869 /* Return the function_instance in the profile that correspond to the
870 inline STACK. */
872 function_instance *
873 autofdo_source_profile::get_function_instance_by_inline_stack (
874 const inline_stack &stack) const
876 name_function_instance_map::const_iterator iter = map_.find (
877 afdo_string_table->get_index_by_decl (stack[stack.length () - 1].first));
878 if (iter == map_.end())
879 return NULL;
880 function_instance *s = iter->second;
881 for (unsigned i = stack.length() - 1; i > 0; i--)
883 s = s->get_function_instance_by_decl (
884 stack[i].second, stack[i - 1].first);
885 if (s == NULL)
886 return NULL;
888 return s;
891 /* Module profile is only used by LIPO. Here we simply ignore it. */
893 static void
894 fake_read_autofdo_module_profile ()
896 /* Read in the module info. */
897 gcov_read_unsigned ();
899 /* Skip the length of the section. */
900 gcov_read_unsigned ();
902 /* Read in the file name table. */
903 unsigned total_module_num = gcov_read_unsigned ();
904 gcc_assert (total_module_num == 0);
907 /* Read data from profile data file. */
909 static void
910 read_profile (void)
912 if (gcov_open (auto_profile_file, 1) == 0)
913 error ("Cannot open profile file %s.", auto_profile_file);
915 if (gcov_read_unsigned () != GCOV_DATA_MAGIC)
916 error ("AutoFDO profile magic number does not mathch.");
918 /* Skip the version number. */
919 unsigned version = gcov_read_unsigned ();
920 if (version != AUTO_PROFILE_VERSION)
921 error ("AutoFDO profile version %u does match %u.",
922 version, AUTO_PROFILE_VERSION);
924 /* Skip the empty integer. */
925 gcov_read_unsigned ();
927 /* string_table. */
928 afdo_string_table = new string_table ();
929 if (!afdo_string_table->read())
930 error ("Cannot read string table from %s.", auto_profile_file);
932 /* autofdo_source_profile. */
933 afdo_source_profile = autofdo_source_profile::create ();
934 if (afdo_source_profile == NULL)
935 error ("Cannot read function profile from %s.", auto_profile_file);
937 /* autofdo_module_profile. */
938 fake_read_autofdo_module_profile ();
940 /* Read in the working set. */
941 if (gcov_read_unsigned () != GCOV_TAG_AFDO_WORKING_SET)
942 error ("Cannot read working set from %s.", auto_profile_file);
944 /* Skip the length of the section. */
945 gcov_read_unsigned ();
946 gcov_working_set_t set[128];
947 for (unsigned i = 0; i < 128; i++)
949 set[i].num_counters = gcov_read_unsigned ();
950 set[i].min_counter = gcov_read_counter ();
952 add_working_set (set);
955 /* From AutoFDO profiles, find values inside STMT for that we want to measure
956 histograms for indirect-call optimization.
958 This function is actually served for 2 purposes:
959 * before annotation, we need to mark histogram, promote and inline
960 * after annotation, we just need to mark, and let follow-up logic to
961 decide if it needs to promote and inline. */
963 static void
964 afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map,
965 bool transform)
967 gimple gs = gsi_stmt (*gsi);
968 tree callee;
970 if (map.size () == 0)
971 return;
972 gcall *stmt = dyn_cast <gcall *> (gs);
973 if ((!stmt) || gimple_call_fndecl (stmt) != NULL_TREE)
974 return;
976 callee = gimple_call_fn (stmt);
978 histogram_value hist = gimple_alloc_histogram_value (
979 cfun, HIST_TYPE_INDIR_CALL, stmt, callee);
980 hist->n_counters = 3;
981 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
982 gimple_add_histogram_value (cfun, stmt, hist);
984 gcov_type total = 0;
985 icall_target_map::const_iterator max_iter = map.end ();
987 for (icall_target_map::const_iterator iter = map.begin ();
988 iter != map.end (); ++iter)
990 total += iter->second;
991 if (max_iter == map.end () || max_iter->second < iter->second)
992 max_iter = iter;
995 hist->hvalue.counters[0]
996 = (unsigned long long)afdo_string_table->get_name (max_iter->first);
997 hist->hvalue.counters[1] = max_iter->second;
998 hist->hvalue.counters[2] = total;
1000 if (!transform)
1001 return;
1003 struct cgraph_edge *indirect_edge
1004 = cgraph_node::get (current_function_decl)->get_edge (stmt);
1005 struct cgraph_node *direct_call = cgraph_node::get_for_asmname (
1006 get_identifier ((const char *) hist->hvalue.counters[0]));
1008 if (direct_call == NULL || !check_ic_target (stmt, direct_call))
1009 return;
1010 if (DECL_STRUCT_FUNCTION (direct_call->decl) == NULL)
1011 return;
1012 struct cgraph_edge *new_edge
1013 = indirect_edge->make_speculative (direct_call, 0, 0);
1014 new_edge->redirect_call_stmt_to_callee ();
1015 gimple_remove_histogram_value (cfun, stmt, hist);
1016 inline_call (new_edge, true, NULL, NULL, false);
1019 /* From AutoFDO profiles, find values inside STMT for that we want to measure
1020 histograms and adds them to list VALUES. */
1022 static void
1023 afdo_vpt (gimple_stmt_iterator *gsi, const icall_target_map &map,
1024 bool transform)
1026 afdo_indirect_call (gsi, map, transform);
1029 typedef std::set<basic_block> bb_set;
1030 typedef std::set<edge> edge_set;
1032 static bool
1033 is_bb_annotated (const basic_block bb, const bb_set &annotated)
1035 return annotated.find (bb) != annotated.end ();
1038 static void
1039 set_bb_annotated (basic_block bb, bb_set *annotated)
1041 annotated->insert (bb);
1044 static bool
1045 is_edge_annotated (const edge e, const edge_set &annotated)
1047 return annotated.find (e) != annotated.end ();
1050 static void
1051 set_edge_annotated (edge e, edge_set *annotated)
1053 annotated->insert (e);
1056 /* For a given BB, set its execution count. Attach value profile if a stmt
1057 is not in PROMOTED, because we only want to promote an indirect call once.
1058 Return TRUE if BB is annotated. */
1060 static bool
1061 afdo_set_bb_count (basic_block bb, const stmt_set &promoted)
1063 gimple_stmt_iterator gsi;
1064 edge e;
1065 edge_iterator ei;
1066 gcov_type max_count = 0;
1067 bool has_annotated = false;
1069 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1071 count_info info;
1072 gimple stmt = gsi_stmt (gsi);
1073 if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1074 continue;
1075 if (afdo_source_profile->get_count_info (stmt, &info))
1077 if (info.count > max_count)
1078 max_count = info.count;
1079 has_annotated = true;
1080 if (info.targets.size () > 0
1081 && promoted.find (stmt) == promoted.end ())
1082 afdo_vpt (&gsi, info.targets, false);
1086 if (!has_annotated)
1087 return false;
1089 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1090 afdo_source_profile->mark_annotated (gimple_location (gsi_stmt (gsi)));
1091 for (gphi_iterator gpi = gsi_start_phis (bb);
1092 !gsi_end_p (gpi);
1093 gsi_next (&gpi))
1095 gphi *phi = gpi.phi ();
1096 size_t i;
1097 for (i = 0; i < gimple_phi_num_args (phi); i++)
1098 afdo_source_profile->mark_annotated (gimple_phi_arg_location (phi, i));
1100 FOR_EACH_EDGE (e, ei, bb->succs)
1101 afdo_source_profile->mark_annotated (e->goto_locus);
1103 bb->count = max_count;
1104 return true;
1107 /* BB1 and BB2 are in an equivalent class iff:
1108 1. BB1 dominates BB2.
1109 2. BB2 post-dominates BB1.
1110 3. BB1 and BB2 are in the same loop nest.
1111 This function finds the equivalent class for each basic block, and
1112 stores a pointer to the first BB in its equivalent class. Meanwhile,
1113 set bb counts for the same equivalent class to be idenical. Update
1114 ANNOTATED_BB for the first BB in its equivalent class. */
1116 static void
1117 afdo_find_equiv_class (bb_set *annotated_bb)
1119 basic_block bb;
1121 FOR_ALL_BB_FN (bb, cfun)
1122 bb->aux = NULL;
1124 FOR_ALL_BB_FN (bb, cfun)
1126 vec<basic_block> dom_bbs;
1127 basic_block bb1;
1128 int i;
1130 if (bb->aux != NULL)
1131 continue;
1132 bb->aux = bb;
1133 dom_bbs = get_dominated_by (CDI_DOMINATORS, bb);
1134 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1135 if (bb1->aux == NULL && dominated_by_p (CDI_POST_DOMINATORS, bb, bb1)
1136 && bb1->loop_father == bb->loop_father)
1138 bb1->aux = bb;
1139 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1141 bb->count = bb1->count;
1142 set_bb_annotated (bb, annotated_bb);
1145 dom_bbs = get_dominated_by (CDI_POST_DOMINATORS, bb);
1146 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1147 if (bb1->aux == NULL && dominated_by_p (CDI_DOMINATORS, bb, bb1)
1148 && bb1->loop_father == bb->loop_father)
1150 bb1->aux = bb;
1151 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1153 bb->count = bb1->count;
1154 set_bb_annotated (bb, annotated_bb);
1160 /* If a basic block's count is known, and only one of its in/out edges' count
1161 is unknown, its count can be calculated. Meanwhile, if all of the in/out
1162 edges' counts are known, then the basic block's unknown count can also be
1163 calculated.
1164 IS_SUCC is true if out edges of a basic blocks are examined.
1165 Update ANNOTATED_BB and ANNOTATED_EDGE accordingly.
1166 Return TRUE if any basic block/edge count is changed. */
1168 static bool
1169 afdo_propagate_edge (bool is_succ, bb_set *annotated_bb,
1170 edge_set *annotated_edge)
1172 basic_block bb;
1173 bool changed = false;
1175 FOR_EACH_BB_FN (bb, cfun)
1177 edge e, unknown_edge = NULL;
1178 edge_iterator ei;
1179 int num_unknown_edge = 0;
1180 gcov_type total_known_count = 0;
1182 FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
1183 if (!is_edge_annotated (e, *annotated_edge))
1184 num_unknown_edge++, unknown_edge = e;
1185 else
1186 total_known_count += e->count;
1188 if (num_unknown_edge == 0)
1190 if (total_known_count > bb->count)
1192 bb->count = total_known_count;
1193 changed = true;
1195 if (!is_bb_annotated (bb, *annotated_bb))
1197 set_bb_annotated (bb, annotated_bb);
1198 changed = true;
1201 else if (num_unknown_edge == 1 && is_bb_annotated (bb, *annotated_bb))
1203 if (bb->count >= total_known_count)
1204 unknown_edge->count = bb->count - total_known_count;
1205 else
1206 unknown_edge->count = 0;
1207 set_edge_annotated (unknown_edge, annotated_edge);
1208 changed = true;
1211 return changed;
1214 /* Special propagation for circuit expressions. Because GCC translates
1215 control flow into data flow for circuit expressions. E.g.
1216 BB1:
1217 if (a && b)
1219 else
1222 will be translated into:
1224 BB1:
1225 if (a)
1226 goto BB.t1
1227 else
1228 goto BB.t3
1229 BB.t1:
1230 if (b)
1231 goto BB.t2
1232 else
1233 goto BB.t3
1234 BB.t2:
1235 goto BB.t3
1236 BB.t3:
1237 tmp = PHI (0 (BB1), 0 (BB.t1), 1 (BB.t2)
1238 if (tmp)
1239 goto BB2
1240 else
1241 goto BB3
1243 In this case, we need to propagate through PHI to determine the edge
1244 count of BB1->BB.t1, BB.t1->BB.t2.
1245 Update ANNOTATED_EDGE accordingly. */
1247 static void
1248 afdo_propagate_circuit (const bb_set &annotated_bb, edge_set *annotated_edge)
1250 basic_block bb;
1251 FOR_ALL_BB_FN (bb, cfun)
1253 gimple def_stmt;
1254 tree cmp_rhs, cmp_lhs;
1255 gimple cmp_stmt = last_stmt (bb);
1256 edge e;
1257 edge_iterator ei;
1259 if (!cmp_stmt || gimple_code (cmp_stmt) != GIMPLE_COND)
1260 continue;
1261 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1262 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1263 if (!TREE_CONSTANT (cmp_rhs)
1264 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1265 continue;
1266 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1267 continue;
1268 if (!is_bb_annotated (bb, annotated_bb))
1269 continue;
1270 def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1271 while (def_stmt && gimple_code (def_stmt) == GIMPLE_ASSIGN
1272 && gimple_assign_single_p (def_stmt)
1273 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1274 def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
1275 if (!def_stmt)
1276 continue;
1277 gphi *phi_stmt = dyn_cast <gphi *> (def_stmt);
1278 if (!phi_stmt)
1279 continue;
1280 FOR_EACH_EDGE (e, ei, bb->succs)
1282 unsigned i, total = 0;
1283 edge only_one;
1284 bool check_value_one = (((integer_onep (cmp_rhs))
1285 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1286 ^ ((e->flags & EDGE_TRUE_VALUE) != 0));
1287 if (!is_edge_annotated (e, *annotated_edge))
1288 continue;
1289 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1291 tree val = gimple_phi_arg_def (phi_stmt, i);
1292 edge ep = gimple_phi_arg_edge (phi_stmt, i);
1294 if (!TREE_CONSTANT (val)
1295 || !(integer_zerop (val) || integer_onep (val)))
1296 continue;
1297 if (check_value_one ^ integer_onep (val))
1298 continue;
1299 total++;
1300 only_one = ep;
1301 if (e->probability == 0 && !is_edge_annotated (ep, *annotated_edge))
1303 ep->probability = 0;
1304 ep->count = 0;
1305 set_edge_annotated (ep, annotated_edge);
1308 if (total == 1 && !is_edge_annotated (only_one, *annotated_edge))
1310 only_one->probability = e->probability;
1311 only_one->count = e->count;
1312 set_edge_annotated (only_one, annotated_edge);
1318 /* Propagate the basic block count and edge count on the control flow
1319 graph. We do the propagation iteratively until stablize. */
1321 static void
1322 afdo_propagate (bb_set *annotated_bb, edge_set *annotated_edge)
1324 basic_block bb;
1325 bool changed = true;
1326 int i = 0;
1328 FOR_ALL_BB_FN (bb, cfun)
1330 bb->count = ((basic_block)bb->aux)->count;
1331 if (is_bb_annotated ((const basic_block)bb->aux, *annotated_bb))
1332 set_bb_annotated (bb, annotated_bb);
1335 while (changed && i++ < 10)
1337 changed = false;
1339 if (afdo_propagate_edge (true, annotated_bb, annotated_edge))
1340 changed = true;
1341 if (afdo_propagate_edge (false, annotated_bb, annotated_edge))
1342 changed = true;
1343 afdo_propagate_circuit (*annotated_bb, annotated_edge);
1347 /* Propagate counts on control flow graph and calculate branch
1348 probabilities. */
1350 static void
1351 afdo_calculate_branch_prob (bb_set *annotated_bb, edge_set *annotated_edge)
1353 basic_block bb;
1354 bool has_sample = false;
1356 FOR_EACH_BB_FN (bb, cfun)
1358 if (bb->count > 0)
1360 has_sample = true;
1361 break;
1365 if (!has_sample)
1366 return;
1368 calculate_dominance_info (CDI_POST_DOMINATORS);
1369 calculate_dominance_info (CDI_DOMINATORS);
1370 loop_optimizer_init (0);
1372 afdo_find_equiv_class (annotated_bb);
1373 afdo_propagate (annotated_bb, annotated_edge);
1375 FOR_EACH_BB_FN (bb, cfun)
1377 edge e;
1378 edge_iterator ei;
1379 int num_unknown_succ = 0;
1380 gcov_type total_count = 0;
1382 FOR_EACH_EDGE (e, ei, bb->succs)
1384 if (!is_edge_annotated (e, *annotated_edge))
1385 num_unknown_succ++;
1386 else
1387 total_count += e->count;
1389 if (num_unknown_succ == 0 && total_count > 0)
1391 FOR_EACH_EDGE (e, ei, bb->succs)
1392 e->probability = (double)e->count * REG_BR_PROB_BASE / total_count;
1395 FOR_ALL_BB_FN (bb, cfun)
1397 edge e;
1398 edge_iterator ei;
1400 FOR_EACH_EDGE (e, ei, bb->succs)
1401 e->count = (double)bb->count * e->probability / REG_BR_PROB_BASE;
1402 bb->aux = NULL;
1405 loop_optimizer_finalize ();
1406 free_dominance_info (CDI_DOMINATORS);
1407 free_dominance_info (CDI_POST_DOMINATORS);
1410 /* Perform value profile transformation using AutoFDO profile. Add the
1411 promoted stmts to PROMOTED_STMTS. Return TRUE if there is any
1412 indirect call promoted. */
1414 static bool
1415 afdo_vpt_for_early_inline (stmt_set *promoted_stmts)
1417 basic_block bb;
1418 if (afdo_source_profile->get_function_instance_by_decl (
1419 current_function_decl) == NULL)
1420 return false;
1422 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1424 bool has_vpt = false;
1425 FOR_EACH_BB_FN (bb, cfun)
1427 if (!has_indirect_call (bb))
1428 continue;
1429 gimple_stmt_iterator gsi;
1431 gcov_type bb_count = 0;
1432 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1434 count_info info;
1435 gimple stmt = gsi_stmt (gsi);
1436 if (afdo_source_profile->get_count_info (stmt, &info))
1437 bb_count = MAX (bb_count, info.count);
1440 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1442 gcall *stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
1443 /* IC_promotion and early_inline_2 is done in multiple iterations.
1444 No need to promoted the stmt if its in promoted_stmts (means
1445 it is already been promoted in the previous iterations). */
1446 if ((!stmt) || gimple_call_fn (stmt) == NULL
1447 || TREE_CODE (gimple_call_fn (stmt)) == FUNCTION_DECL
1448 || promoted_stmts->find (stmt) != promoted_stmts->end ())
1449 continue;
1451 count_info info;
1452 afdo_source_profile->get_count_info (stmt, &info);
1453 info.count = bb_count;
1454 if (afdo_source_profile->update_inlined_ind_target (stmt, &info))
1456 /* Promote the indirect call and update the promoted_stmts. */
1457 promoted_stmts->insert (stmt);
1458 afdo_vpt (&gsi, info.targets, true);
1459 has_vpt = true;
1464 if (has_vpt)
1466 optimize_inline_calls (current_function_decl);
1467 return true;
1470 return false;
1473 /* Annotate auto profile to the control flow graph. Do not annotate value
1474 profile for stmts in PROMOTED_STMTS. */
1476 static void
1477 afdo_annotate_cfg (const stmt_set &promoted_stmts)
1479 basic_block bb;
1480 bb_set annotated_bb;
1481 edge_set annotated_edge;
1482 const function_instance *s
1483 = afdo_source_profile->get_function_instance_by_decl (
1484 current_function_decl);
1486 if (s == NULL)
1487 return;
1488 cgraph_node::get (current_function_decl)->count = s->head_count ();
1489 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = s->head_count ();
1490 gcov_type max_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1492 FOR_EACH_BB_FN (bb, cfun)
1494 edge e;
1495 edge_iterator ei;
1497 bb->count = 0;
1498 FOR_EACH_EDGE (e, ei, bb->succs)
1499 e->count = 0;
1501 if (afdo_set_bb_count (bb, promoted_stmts))
1502 set_bb_annotated (bb, &annotated_bb);
1503 if (bb->count > max_count)
1504 max_count = bb->count;
1506 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1507 > ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count)
1509 ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count
1510 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1511 set_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, &annotated_bb);
1513 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1514 > EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count)
1516 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count
1517 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1518 set_bb_annotated (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, &annotated_bb);
1520 afdo_source_profile->mark_annotated (
1521 DECL_SOURCE_LOCATION (current_function_decl));
1522 afdo_source_profile->mark_annotated (cfun->function_start_locus);
1523 afdo_source_profile->mark_annotated (cfun->function_end_locus);
1524 if (max_count > 0)
1526 afdo_calculate_branch_prob (&annotated_bb, &annotated_edge);
1527 counts_to_freqs ();
1528 profile_status_for_fn (cfun) = PROFILE_READ;
1530 if (flag_value_profile_transformations)
1532 gimple_value_profile_transformations ();
1533 free_dominance_info (CDI_DOMINATORS);
1534 free_dominance_info (CDI_POST_DOMINATORS);
1535 update_ssa (TODO_update_ssa);
1539 /* Wrapper function to invoke early inliner. */
1541 static void
1542 early_inline ()
1544 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1545 unsigned todo = early_inliner (cfun);
1546 if (todo & TODO_update_ssa_any)
1547 update_ssa (TODO_update_ssa);
1550 /* Use AutoFDO profile to annoate the control flow graph.
1551 Return the todo flag. */
1553 static unsigned int
1554 auto_profile (void)
1556 struct cgraph_node *node;
1558 if (symtab->state == FINISHED)
1559 return 0;
1561 init_node_map (true);
1562 profile_info = autofdo::afdo_profile_info;
1564 FOR_EACH_FUNCTION (node)
1566 if (!gimple_has_body_p (node->decl))
1567 continue;
1569 /* Don't profile functions produced for builtin stuff. */
1570 if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
1571 continue;
1573 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1575 /* First do indirect call promotion and early inline to make the
1576 IR match the profiled binary before actual annotation.
1578 This is needed because an indirect call might have been promoted
1579 and inlined in the profiled binary. If we do not promote and
1580 inline these indirect calls before annotation, the profile for
1581 these promoted functions will be lost.
1583 e.g. foo() --indirect_call--> bar()
1584 In profiled binary, the callsite is promoted and inlined, making
1585 the profile look like:
1587 foo: {
1588 loc_foo_1: count_1
1589 bar@loc_foo_2: {
1590 loc_bar_1: count_2
1591 loc_bar_2: count_3
1595 Before AutoFDO pass, loc_foo_2 is not promoted thus not inlined.
1596 If we perform annotation on it, the profile inside bar@loc_foo2
1597 will be wasted.
1599 To avoid this, we promote loc_foo_2 and inline the promoted bar
1600 function before annotation, so the profile inside bar@loc_foo2
1601 will be useful. */
1602 autofdo::stmt_set promoted_stmts;
1603 for (int i = 0; i < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS); i++)
1605 if (!flag_value_profile_transformations
1606 || !autofdo::afdo_vpt_for_early_inline (&promoted_stmts))
1607 break;
1608 early_inline ();
1611 early_inline ();
1612 autofdo::afdo_annotate_cfg (promoted_stmts);
1613 compute_function_frequency ();
1615 /* Local pure-const may imply need to fixup the cfg. */
1616 if (execute_fixup_cfg () & TODO_cleanup_cfg)
1617 cleanup_tree_cfg ();
1619 free_dominance_info (CDI_DOMINATORS);
1620 free_dominance_info (CDI_POST_DOMINATORS);
1621 cgraph_edge::rebuild_edges ();
1622 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1623 pop_cfun ();
1626 return TODO_rebuild_cgraph_edges;
1628 } /* namespace autofdo. */
1630 /* Read the profile from the profile data file. */
1632 void
1633 read_autofdo_file (void)
1635 if (auto_profile_file == NULL)
1636 auto_profile_file = DEFAULT_AUTO_PROFILE_FILE;
1638 autofdo::afdo_profile_info = (struct gcov_ctr_summary *)xcalloc (
1639 1, sizeof (struct gcov_ctr_summary));
1640 autofdo::afdo_profile_info->runs = 1;
1641 autofdo::afdo_profile_info->sum_max = 0;
1642 autofdo::afdo_profile_info->sum_all = 0;
1644 /* Read the profile from the profile file. */
1645 autofdo::read_profile ();
1648 /* Free the resources. */
1650 void
1651 end_auto_profile (void)
1653 delete autofdo::afdo_source_profile;
1654 delete autofdo::afdo_string_table;
1655 profile_info = NULL;
1658 /* Returns TRUE if EDGE is hot enough to be inlined early. */
1660 bool
1661 afdo_callsite_hot_enough_for_early_inline (struct cgraph_edge *edge)
1663 gcov_type count
1664 = autofdo::afdo_source_profile->get_callsite_total_count (edge);
1666 if (count > 0)
1668 bool is_hot;
1669 const struct gcov_ctr_summary *saved_profile_info = profile_info;
1670 /* At early inline stage, profile_info is not set yet. We need to
1671 temporarily set it to afdo_profile_info to calculate hotness. */
1672 profile_info = autofdo::afdo_profile_info;
1673 is_hot = maybe_hot_count_p (NULL, count);
1674 profile_info = saved_profile_info;
1675 return is_hot;
1678 return false;
1681 namespace
1684 const pass_data pass_data_ipa_auto_profile = {
1685 SIMPLE_IPA_PASS, "afdo", /* name */
1686 OPTGROUP_NONE, /* optinfo_flags */
1687 TV_IPA_AUTOFDO, /* tv_id */
1688 0, /* properties_required */
1689 0, /* properties_provided */
1690 0, /* properties_destroyed */
1691 0, /* todo_flags_start */
1692 0, /* todo_flags_finish */
1695 class pass_ipa_auto_profile : public simple_ipa_opt_pass
1697 public:
1698 pass_ipa_auto_profile (gcc::context *ctxt)
1699 : simple_ipa_opt_pass (pass_data_ipa_auto_profile, ctxt)
1703 /* opt_pass methods: */
1704 virtual bool
1705 gate (function *)
1707 return flag_auto_profile;
1709 virtual unsigned int
1710 execute (function *)
1712 return autofdo::auto_profile ();
1714 }; // class pass_ipa_auto_profile
1716 } // anon namespace
1718 simple_ipa_opt_pass *
1719 make_pass_ipa_auto_profile (gcc::context *ctxt)
1721 return new pass_ipa_auto_profile (ctxt);