PR tree-optimization/66718
[official-gcc.git] / gcc / auto-profile.c
blob33f5632f9ea201c63b29f708efbe3b56f860706f
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 "alias.h"
30 #include "backend.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "hard-reg-set.h"
34 #include "ssa.h"
35 #include "options.h"
36 #include "fold-const.h"
37 #include "tree-pass.h"
38 #include "flags.h"
39 #include "diagnostic-core.h"
40 #include "gcov-io.h"
41 #include "profile.h"
42 #include "langhooks.h"
43 #include "opts.h"
44 #include "tree-pass.h"
45 #include "cfgloop.h"
46 #include "tree-cfg.h"
47 #include "tree-cfgcleanup.h"
48 #include "tree-into-ssa.h"
49 #include "internal-fn.h"
50 #include "gimple-iterator.h"
51 #include "cgraph.h"
52 #include "value-prof.h"
53 #include "coverage.h"
54 #include "params.h"
55 #include "alloc-pool.h"
56 #include "symbol-summary.h"
57 #include "ipa-prop.h"
58 #include "ipa-inline.h"
59 #include "tree-inline.h"
60 #include "auto-profile.h"
62 /* The following routines implements AutoFDO optimization.
64 This optimization uses sampling profiles to annotate basic block counts
65 and uses heuristics to estimate branch probabilities.
67 There are three phases in AutoFDO:
69 Phase 1: Read profile from the profile data file.
70 The following info is read from the profile datafile:
71 * string_table: a map between function name and its index.
72 * autofdo_source_profile: a map from function_instance name to
73 function_instance. This is represented as a forest of
74 function_instances.
75 * WorkingSet: a histogram of how many instructions are covered for a
76 given percentage of total cycles. This is describing the binary
77 level information (not source level). This info is used to help
78 decide if we want aggressive optimizations that could increase
79 code footprint (e.g. loop unroll etc.)
80 A function instance is an instance of function that could either be a
81 standalone symbol, or a clone of a function that is inlined into another
82 function.
84 Phase 2: Early inline + value profile transformation.
85 Early inline uses autofdo_source_profile to find if a callsite is:
86 * inlined in the profiled binary.
87 * callee body is hot in the profiling run.
88 If both condition satisfies, early inline will inline the callsite
89 regardless of the code growth.
90 Phase 2 is an iterative process. During each iteration, we also check
91 if an indirect callsite is promoted and inlined in the profiling run.
92 If yes, vpt will happen to force promote it and in the next iteration,
93 einline will inline the promoted callsite in the next iteration.
95 Phase 3: Annotate control flow graph.
96 AutoFDO uses a separate pass to:
97 * Annotate basic block count
98 * Estimate branch probability
100 After the above 3 phases, all profile is readily annotated on the GCC IR.
101 AutoFDO tries to reuse all FDO infrastructure as much as possible to make
102 use of the profile. E.g. it uses existing mechanism to calculate the basic
103 block/edge frequency, as well as the cgraph node/edge count.
106 #define DEFAULT_AUTO_PROFILE_FILE "fbdata.afdo"
107 #define AUTO_PROFILE_VERSION 1
109 namespace autofdo
112 /* Represent a source location: (function_decl, lineno). */
113 typedef std::pair<tree, unsigned> decl_lineno;
115 /* Represent an inline stack. vector[0] is the leaf node. */
116 typedef auto_vec<decl_lineno> inline_stack;
118 /* String array that stores function names. */
119 typedef auto_vec<char *> string_vector;
121 /* Map from function name's index in string_table to target's
122 execution count. */
123 typedef std::map<unsigned, gcov_type> icall_target_map;
125 /* Set of gimple stmts. Used to track if the stmt has already been promoted
126 to direct call. */
127 typedef std::set<gimple> stmt_set;
129 /* Represent count info of an inline stack. */
130 struct count_info
132 /* Sampled count of the inline stack. */
133 gcov_type count;
135 /* Map from indirect call target to its sample count. */
136 icall_target_map targets;
138 /* Whether this inline stack is already used in annotation.
140 Each inline stack should only be used to annotate IR once.
141 This will be enforced when instruction-level discriminator
142 is supported. */
143 bool annotated;
146 /* operator< for "const char *". */
147 struct string_compare
149 bool operator()(const char *a, const char *b) const
151 return strcmp (a, b) < 0;
155 /* Store a string array, indexed by string position in the array. */
156 class string_table
158 public:
159 string_table ()
162 ~string_table ();
164 /* For a given string, returns its index. */
165 int get_index (const char *name) const;
167 /* For a given decl, returns the index of the decl name. */
168 int get_index_by_decl (tree decl) const;
170 /* For a given index, returns the string. */
171 const char *get_name (int index) const;
173 /* Read profile, return TRUE on success. */
174 bool read ();
176 private:
177 typedef std::map<const char *, unsigned, string_compare> string_index_map;
178 string_vector vector_;
179 string_index_map map_;
182 /* Profile of a function instance:
183 1. total_count of the function.
184 2. head_count (entry basic block count) of the function (only valid when
185 function is a top-level function_instance, i.e. it is the original copy
186 instead of the inlined copy).
187 3. map from source location (decl_lineno) to profile (count_info).
188 4. map from callsite to callee function_instance. */
189 class function_instance
191 public:
192 typedef auto_vec<function_instance *> function_instance_stack;
194 /* Read the profile and return a function_instance with head count as
195 HEAD_COUNT. Recursively read callsites to create nested function_instances
196 too. STACK is used to track the recursive creation process. */
197 static function_instance *
198 read_function_instance (function_instance_stack *stack,
199 gcov_type head_count);
201 /* Recursively deallocate all callsites (nested function_instances). */
202 ~function_instance ();
204 /* Accessors. */
206 name () const
208 return name_;
210 gcov_type
211 total_count () const
213 return total_count_;
215 gcov_type
216 head_count () const
218 return head_count_;
221 /* Traverse callsites of the current function_instance to find one at the
222 location of LINENO and callee name represented in DECL. */
223 function_instance *get_function_instance_by_decl (unsigned lineno,
224 tree decl) const;
226 /* Store the profile info for LOC in INFO. Return TRUE if profile info
227 is found. */
228 bool get_count_info (location_t loc, count_info *info) const;
230 /* Read the inlined indirect call target profile for STMT and store it in
231 MAP, return the total count for all inlined indirect calls. */
232 gcov_type find_icall_target_map (gcall *stmt, icall_target_map *map) const;
234 /* Sum of counts that is used during annotation. */
235 gcov_type total_annotated_count () const;
237 /* Mark LOC as annotated. */
238 void mark_annotated (location_t loc);
240 private:
241 /* Callsite, represented as (decl_lineno, callee_function_name_index). */
242 typedef std::pair<unsigned, unsigned> callsite;
244 /* Map from callsite to callee function_instance. */
245 typedef std::map<callsite, function_instance *> callsite_map;
247 function_instance (unsigned name, gcov_type head_count)
248 : name_ (name), total_count_ (0), head_count_ (head_count)
252 /* Map from source location (decl_lineno) to profile (count_info). */
253 typedef std::map<unsigned, count_info> position_count_map;
255 /* function_instance name index in the string_table. */
256 unsigned name_;
258 /* Total sample count. */
259 gcov_type total_count_;
261 /* Entry BB's sample count. */
262 gcov_type head_count_;
264 /* Map from callsite location to callee function_instance. */
265 callsite_map callsites;
267 /* Map from source location to count_info. */
268 position_count_map pos_counts;
271 /* Profile for all functions. */
272 class autofdo_source_profile
274 public:
275 static autofdo_source_profile *
276 create ()
278 autofdo_source_profile *map = new autofdo_source_profile ();
280 if (map->read ())
281 return map;
282 delete map;
283 return NULL;
286 ~autofdo_source_profile ();
288 /* For a given DECL, returns the top-level function_instance. */
289 function_instance *get_function_instance_by_decl (tree decl) const;
291 /* Find count_info for a given gimple STMT. If found, store the count_info
292 in INFO and return true; otherwise return false. */
293 bool get_count_info (gimple stmt, count_info *info) const;
295 /* Find total count of the callee of EDGE. */
296 gcov_type get_callsite_total_count (struct cgraph_edge *edge) const;
298 /* Update value profile INFO for STMT from the inlined indirect callsite.
299 Return true if INFO is updated. */
300 bool update_inlined_ind_target (gcall *stmt, count_info *info);
302 /* Mark LOC as annotated. */
303 void mark_annotated (location_t loc);
305 private:
306 /* Map from function_instance name index (in string_table) to
307 function_instance. */
308 typedef std::map<unsigned, function_instance *> name_function_instance_map;
310 autofdo_source_profile () {}
312 /* Read AutoFDO profile and returns TRUE on success. */
313 bool read ();
315 /* Return the function_instance in the profile that correspond to the
316 inline STACK. */
317 function_instance *
318 get_function_instance_by_inline_stack (const inline_stack &stack) const;
320 name_function_instance_map map_;
323 /* Store the strings read from the profile data file. */
324 static string_table *afdo_string_table;
326 /* Store the AutoFDO source profile. */
327 static autofdo_source_profile *afdo_source_profile;
329 /* gcov_ctr_summary structure to store the profile_info. */
330 static struct gcov_ctr_summary *afdo_profile_info;
332 /* Helper functions. */
334 /* Return the original name of NAME: strip the suffix that starts
335 with '.' Caller is responsible for freeing RET. */
337 static char *
338 get_original_name (const char *name)
340 char *ret = xstrdup (name);
341 char *find = strchr (ret, '.');
342 if (find != NULL)
343 *find = 0;
344 return ret;
347 /* Return the combined location, which is a 32bit integer in which
348 higher 16 bits stores the line offset of LOC to the start lineno
349 of DECL, The lower 16 bits stores the discriminator. */
351 static unsigned
352 get_combined_location (location_t loc, tree decl)
354 /* TODO: allow more bits for line and less bits for discriminator. */
355 if (LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl) >= (1<<16))
356 warning_at (loc, OPT_Woverflow, "Offset exceeds 16 bytes.");
357 return ((LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) << 16);
360 /* Return the function decl of a given lexical BLOCK. */
362 static tree
363 get_function_decl_from_block (tree block)
365 tree decl;
367 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block) == UNKNOWN_LOCATION))
368 return NULL_TREE;
370 for (decl = BLOCK_ABSTRACT_ORIGIN (block);
371 decl && (TREE_CODE (decl) == BLOCK);
372 decl = BLOCK_ABSTRACT_ORIGIN (decl))
373 if (TREE_CODE (decl) == FUNCTION_DECL)
374 break;
375 return decl;
378 /* Store inline stack for STMT in STACK. */
380 static void
381 get_inline_stack (location_t locus, inline_stack *stack)
383 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
384 return;
386 tree block = LOCATION_BLOCK (locus);
387 if (block && TREE_CODE (block) == BLOCK)
389 int level = 0;
390 for (block = BLOCK_SUPERCONTEXT (block);
391 block && (TREE_CODE (block) == BLOCK);
392 block = BLOCK_SUPERCONTEXT (block))
394 location_t tmp_locus = BLOCK_SOURCE_LOCATION (block);
395 if (LOCATION_LOCUS (tmp_locus) == UNKNOWN_LOCATION)
396 continue;
398 tree decl = get_function_decl_from_block (block);
399 stack->safe_push (
400 std::make_pair (decl, get_combined_location (locus, decl)));
401 locus = tmp_locus;
402 level++;
405 stack->safe_push (
406 std::make_pair (current_function_decl,
407 get_combined_location (locus, current_function_decl)));
410 /* Return STMT's combined location, which is a 32bit integer in which
411 higher 16 bits stores the line offset of LOC to the start lineno
412 of DECL, The lower 16 bits stores the discriminator. */
414 static unsigned
415 get_relative_location_for_stmt (gimple stmt)
417 location_t locus = gimple_location (stmt);
418 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
419 return UNKNOWN_LOCATION;
421 for (tree block = gimple_block (stmt); block && (TREE_CODE (block) == BLOCK);
422 block = BLOCK_SUPERCONTEXT (block))
423 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block)) != UNKNOWN_LOCATION)
424 return get_combined_location (locus,
425 get_function_decl_from_block (block));
426 return get_combined_location (locus, current_function_decl);
429 /* Return true if BB contains indirect call. */
431 static bool
432 has_indirect_call (basic_block bb)
434 gimple_stmt_iterator gsi;
436 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
438 gimple stmt = gsi_stmt (gsi);
439 if (gimple_code (stmt) == GIMPLE_CALL && !gimple_call_internal_p (stmt)
440 && (gimple_call_fn (stmt) == NULL
441 || TREE_CODE (gimple_call_fn (stmt)) != FUNCTION_DECL))
442 return true;
444 return false;
447 /* Member functions for string_table. */
449 /* Deconstructor. */
451 string_table::~string_table ()
453 for (unsigned i = 0; i < vector_.length (); i++)
454 free (vector_[i]);
458 /* Return the index of a given function NAME. Return -1 if NAME is not
459 found in string table. */
462 string_table::get_index (const char *name) const
464 if (name == NULL)
465 return -1;
466 string_index_map::const_iterator iter = map_.find (name);
467 if (iter == map_.end ())
468 return -1;
470 return iter->second;
473 /* Return the index of a given function DECL. Return -1 if DECL is not
474 found in string table. */
477 string_table::get_index_by_decl (tree decl) const
479 char *name
480 = get_original_name (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
481 int ret = get_index (name);
482 free (name);
483 if (ret != -1)
484 return ret;
485 ret = get_index (lang_hooks.dwarf_name (decl, 0));
486 if (ret != -1)
487 return ret;
488 if (DECL_ABSTRACT_ORIGIN (decl))
489 return get_index_by_decl (DECL_ABSTRACT_ORIGIN (decl));
491 return -1;
494 /* Return the function name of a given INDEX. */
496 const char *
497 string_table::get_name (int index) const
499 gcc_assert (index > 0 && index < (int)vector_.length ());
500 return vector_[index];
503 /* Read the string table. Return TRUE if reading is successful. */
505 bool
506 string_table::read ()
508 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FILE_NAMES)
509 return false;
510 /* Skip the length of the section. */
511 gcov_read_unsigned ();
512 /* Read in the file name table. */
513 unsigned string_num = gcov_read_unsigned ();
514 for (unsigned i = 0; i < string_num; i++)
516 vector_.safe_push (get_original_name (gcov_read_string ()));
517 map_[vector_.last ()] = i;
519 return true;
522 /* Member functions for function_instance. */
524 function_instance::~function_instance ()
526 for (callsite_map::iterator iter = callsites.begin ();
527 iter != callsites.end (); ++iter)
528 delete iter->second;
531 /* Traverse callsites of the current function_instance to find one at the
532 location of LINENO and callee name represented in DECL. */
534 function_instance *
535 function_instance::get_function_instance_by_decl (unsigned lineno,
536 tree decl) const
538 int func_name_idx = afdo_string_table->get_index_by_decl (decl);
539 if (func_name_idx != -1)
541 callsite_map::const_iterator ret
542 = callsites.find (std::make_pair (lineno, func_name_idx));
543 if (ret != callsites.end ())
544 return ret->second;
546 func_name_idx
547 = afdo_string_table->get_index (lang_hooks.dwarf_name (decl, 0));
548 if (func_name_idx != -1)
550 callsite_map::const_iterator ret
551 = callsites.find (std::make_pair (lineno, func_name_idx));
552 if (ret != callsites.end ())
553 return ret->second;
555 if (DECL_ABSTRACT_ORIGIN (decl))
556 return get_function_instance_by_decl (lineno, DECL_ABSTRACT_ORIGIN (decl));
558 return NULL;
561 /* Store the profile info for LOC in INFO. Return TRUE if profile info
562 is found. */
564 bool
565 function_instance::get_count_info (location_t loc, count_info *info) const
567 position_count_map::const_iterator iter = pos_counts.find (loc);
568 if (iter == pos_counts.end ())
569 return false;
570 *info = iter->second;
571 return true;
574 /* Mark LOC as annotated. */
576 void
577 function_instance::mark_annotated (location_t loc)
579 position_count_map::iterator iter = pos_counts.find (loc);
580 if (iter == pos_counts.end ())
581 return;
582 iter->second.annotated = true;
585 /* Read the inlined indirect call target profile for STMT and store it in
586 MAP, return the total count for all inlined indirect calls. */
588 gcov_type
589 function_instance::find_icall_target_map (gcall *stmt,
590 icall_target_map *map) const
592 gcov_type ret = 0;
593 unsigned stmt_offset = get_relative_location_for_stmt (stmt);
595 for (callsite_map::const_iterator iter = callsites.begin ();
596 iter != callsites.end (); ++iter)
598 unsigned callee = iter->second->name ();
599 /* Check if callsite location match the stmt. */
600 if (iter->first.first != stmt_offset)
601 continue;
602 struct cgraph_node *node = cgraph_node::get_for_asmname (
603 get_identifier (afdo_string_table->get_name (callee)));
604 if (node == NULL)
605 continue;
606 if (!check_ic_target (stmt, node))
607 continue;
608 (*map)[callee] = iter->second->total_count ();
609 ret += iter->second->total_count ();
611 return ret;
614 /* Read the profile and create a function_instance with head count as
615 HEAD_COUNT. Recursively read callsites to create nested function_instances
616 too. STACK is used to track the recursive creation process. */
618 /* function instance profile format:
620 ENTRY_COUNT: 8 bytes
621 NAME_INDEX: 4 bytes
622 NUM_POS_COUNTS: 4 bytes
623 NUM_CALLSITES: 4 byte
624 POS_COUNT_1:
625 POS_1_OFFSET: 4 bytes
626 NUM_TARGETS: 4 bytes
627 COUNT: 8 bytes
628 TARGET_1:
629 VALUE_PROFILE_TYPE: 4 bytes
630 TARGET_IDX: 8 bytes
631 COUNT: 8 bytes
632 TARGET_2
634 TARGET_n
635 POS_COUNT_2
637 POS_COUNT_N
638 CALLSITE_1:
639 CALLSITE_1_OFFSET: 4 bytes
640 FUNCTION_INSTANCE_PROFILE (nested)
641 CALLSITE_2
643 CALLSITE_n. */
645 function_instance *
646 function_instance::read_function_instance (function_instance_stack *stack,
647 gcov_type head_count)
649 unsigned name = gcov_read_unsigned ();
650 unsigned num_pos_counts = gcov_read_unsigned ();
651 unsigned num_callsites = gcov_read_unsigned ();
652 function_instance *s = new function_instance (name, head_count);
653 stack->safe_push (s);
655 for (unsigned i = 0; i < num_pos_counts; i++)
657 unsigned offset = gcov_read_unsigned () & 0xffff0000;
658 unsigned num_targets = gcov_read_unsigned ();
659 gcov_type count = gcov_read_counter ();
660 s->pos_counts[offset].count = count;
661 for (unsigned j = 0; j < stack->length (); j++)
662 (*stack)[j]->total_count_ += count;
663 for (unsigned j = 0; j < num_targets; j++)
665 /* Only indirect call target histogram is supported now. */
666 gcov_read_unsigned ();
667 gcov_type target_idx = gcov_read_counter ();
668 s->pos_counts[offset].targets[target_idx] = gcov_read_counter ();
671 for (unsigned i = 0; i < num_callsites; i++)
673 unsigned offset = gcov_read_unsigned ();
674 function_instance *callee_function_instance
675 = read_function_instance (stack, 0);
676 s->callsites[std::make_pair (offset, callee_function_instance->name ())]
677 = callee_function_instance;
679 stack->pop ();
680 return s;
683 /* Sum of counts that is used during annotation. */
685 gcov_type
686 function_instance::total_annotated_count () const
688 gcov_type ret = 0;
689 for (callsite_map::const_iterator iter = callsites.begin ();
690 iter != callsites.end (); ++iter)
691 ret += iter->second->total_annotated_count ();
692 for (position_count_map::const_iterator iter = pos_counts.begin ();
693 iter != pos_counts.end (); ++iter)
694 if (iter->second.annotated)
695 ret += iter->second.count;
696 return ret;
699 /* Member functions for autofdo_source_profile. */
701 autofdo_source_profile::~autofdo_source_profile ()
703 for (name_function_instance_map::const_iterator iter = map_.begin ();
704 iter != map_.end (); ++iter)
705 delete iter->second;
708 /* For a given DECL, returns the top-level function_instance. */
710 function_instance *
711 autofdo_source_profile::get_function_instance_by_decl (tree decl) const
713 int index = afdo_string_table->get_index_by_decl (decl);
714 if (index == -1)
715 return NULL;
716 name_function_instance_map::const_iterator ret = map_.find (index);
717 return ret == map_.end () ? NULL : ret->second;
720 /* Find count_info for a given gimple STMT. If found, store the count_info
721 in INFO and return true; otherwise return false. */
723 bool
724 autofdo_source_profile::get_count_info (gimple stmt, count_info *info) const
726 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
727 return false;
729 inline_stack stack;
730 get_inline_stack (gimple_location (stmt), &stack);
731 if (stack.length () == 0)
732 return false;
733 function_instance *s = get_function_instance_by_inline_stack (stack);
734 if (s == NULL)
735 return false;
736 return s->get_count_info (stack[0].second, info);
739 /* Mark LOC as annotated. */
741 void
742 autofdo_source_profile::mark_annotated (location_t loc)
744 inline_stack stack;
745 get_inline_stack (loc, &stack);
746 if (stack.length () == 0)
747 return;
748 function_instance *s = get_function_instance_by_inline_stack (stack);
749 if (s == NULL)
750 return;
751 s->mark_annotated (stack[0].second);
754 /* Update value profile INFO for STMT from the inlined indirect callsite.
755 Return true if INFO is updated. */
757 bool
758 autofdo_source_profile::update_inlined_ind_target (gcall *stmt,
759 count_info *info)
761 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
762 return false;
764 count_info old_info;
765 get_count_info (stmt, &old_info);
766 gcov_type total = 0;
767 for (icall_target_map::const_iterator iter = old_info.targets.begin ();
768 iter != old_info.targets.end (); ++iter)
769 total += iter->second;
771 /* Program behavior changed, original promoted (and inlined) target is not
772 hot any more. Will avoid promote the original target.
774 To check if original promoted target is still hot, we check the total
775 count of the unpromoted targets (stored in old_info). If it is no less
776 than half of the callsite count (stored in INFO), the original promoted
777 target is considered not hot any more. */
778 if (total >= info->count / 2)
779 return false;
781 inline_stack stack;
782 get_inline_stack (gimple_location (stmt), &stack);
783 if (stack.length () == 0)
784 return false;
785 function_instance *s = get_function_instance_by_inline_stack (stack);
786 if (s == NULL)
787 return false;
788 icall_target_map map;
789 if (s->find_icall_target_map (stmt, &map) == 0)
790 return false;
791 for (icall_target_map::const_iterator iter = map.begin ();
792 iter != map.end (); ++iter)
793 info->targets[iter->first] = iter->second;
794 return true;
797 /* Find total count of the callee of EDGE. */
799 gcov_type
800 autofdo_source_profile::get_callsite_total_count (
801 struct cgraph_edge *edge) const
803 inline_stack stack;
804 stack.safe_push (std::make_pair (edge->callee->decl, 0));
805 get_inline_stack (gimple_location (edge->call_stmt), &stack);
807 function_instance *s = get_function_instance_by_inline_stack (stack);
808 if (s == NULL
809 || afdo_string_table->get_index (IDENTIFIER_POINTER (
810 DECL_ASSEMBLER_NAME (edge->callee->decl))) != s->name ())
811 return 0;
813 return s->total_count ();
816 /* Read AutoFDO profile and returns TRUE on success. */
818 /* source profile format:
820 GCOV_TAG_AFDO_FUNCTION: 4 bytes
821 LENGTH: 4 bytes
822 NUM_FUNCTIONS: 4 bytes
823 FUNCTION_INSTANCE_1
824 FUNCTION_INSTANCE_2
826 FUNCTION_INSTANCE_N. */
828 bool
829 autofdo_source_profile::read ()
831 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FUNCTION)
833 inform (0, "Not expected TAG.");
834 return false;
837 /* Skip the length of the section. */
838 gcov_read_unsigned ();
840 /* Read in the function/callsite profile, and store it in local
841 data structure. */
842 unsigned function_num = gcov_read_unsigned ();
843 for (unsigned i = 0; i < function_num; i++)
845 function_instance::function_instance_stack stack;
846 function_instance *s = function_instance::read_function_instance (
847 &stack, gcov_read_counter ());
848 afdo_profile_info->sum_all += s->total_count ();
849 map_[s->name ()] = s;
851 return true;
854 /* Return the function_instance in the profile that correspond to the
855 inline STACK. */
857 function_instance *
858 autofdo_source_profile::get_function_instance_by_inline_stack (
859 const inline_stack &stack) const
861 name_function_instance_map::const_iterator iter = map_.find (
862 afdo_string_table->get_index_by_decl (stack[stack.length () - 1].first));
863 if (iter == map_.end())
864 return NULL;
865 function_instance *s = iter->second;
866 for (unsigned i = stack.length() - 1; i > 0; i--)
868 s = s->get_function_instance_by_decl (
869 stack[i].second, stack[i - 1].first);
870 if (s == NULL)
871 return NULL;
873 return s;
876 /* Module profile is only used by LIPO. Here we simply ignore it. */
878 static void
879 fake_read_autofdo_module_profile ()
881 /* Read in the module info. */
882 gcov_read_unsigned ();
884 /* Skip the length of the section. */
885 gcov_read_unsigned ();
887 /* Read in the file name table. */
888 unsigned total_module_num = gcov_read_unsigned ();
889 gcc_assert (total_module_num == 0);
892 /* Read data from profile data file. */
894 static void
895 read_profile (void)
897 if (gcov_open (auto_profile_file, 1) == 0)
898 error ("Cannot open profile file %s.", auto_profile_file);
900 if (gcov_read_unsigned () != GCOV_DATA_MAGIC)
901 error ("AutoFDO profile magic number does not mathch.");
903 /* Skip the version number. */
904 unsigned version = gcov_read_unsigned ();
905 if (version != AUTO_PROFILE_VERSION)
906 error ("AutoFDO profile version %u does match %u.",
907 version, AUTO_PROFILE_VERSION);
909 /* Skip the empty integer. */
910 gcov_read_unsigned ();
912 /* string_table. */
913 afdo_string_table = new string_table ();
914 if (!afdo_string_table->read())
915 error ("Cannot read string table from %s.", auto_profile_file);
917 /* autofdo_source_profile. */
918 afdo_source_profile = autofdo_source_profile::create ();
919 if (afdo_source_profile == NULL)
920 error ("Cannot read function profile from %s.", auto_profile_file);
922 /* autofdo_module_profile. */
923 fake_read_autofdo_module_profile ();
925 /* Read in the working set. */
926 if (gcov_read_unsigned () != GCOV_TAG_AFDO_WORKING_SET)
927 error ("Cannot read working set from %s.", auto_profile_file);
929 /* Skip the length of the section. */
930 gcov_read_unsigned ();
931 gcov_working_set_t set[128];
932 for (unsigned i = 0; i < 128; i++)
934 set[i].num_counters = gcov_read_unsigned ();
935 set[i].min_counter = gcov_read_counter ();
937 add_working_set (set);
940 /* From AutoFDO profiles, find values inside STMT for that we want to measure
941 histograms for indirect-call optimization.
943 This function is actually served for 2 purposes:
944 * before annotation, we need to mark histogram, promote and inline
945 * after annotation, we just need to mark, and let follow-up logic to
946 decide if it needs to promote and inline. */
948 static void
949 afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map,
950 bool transform)
952 gimple gs = gsi_stmt (*gsi);
953 tree callee;
955 if (map.size () == 0)
956 return;
957 gcall *stmt = dyn_cast <gcall *> (gs);
958 if ((!stmt) || gimple_call_fndecl (stmt) != NULL_TREE)
959 return;
961 callee = gimple_call_fn (stmt);
963 histogram_value hist = gimple_alloc_histogram_value (
964 cfun, HIST_TYPE_INDIR_CALL, stmt, callee);
965 hist->n_counters = 3;
966 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
967 gimple_add_histogram_value (cfun, stmt, hist);
969 gcov_type total = 0;
970 icall_target_map::const_iterator max_iter = map.end ();
972 for (icall_target_map::const_iterator iter = map.begin ();
973 iter != map.end (); ++iter)
975 total += iter->second;
976 if (max_iter == map.end () || max_iter->second < iter->second)
977 max_iter = iter;
980 hist->hvalue.counters[0]
981 = (unsigned long long)afdo_string_table->get_name (max_iter->first);
982 hist->hvalue.counters[1] = max_iter->second;
983 hist->hvalue.counters[2] = total;
985 if (!transform)
986 return;
988 struct cgraph_edge *indirect_edge
989 = cgraph_node::get (current_function_decl)->get_edge (stmt);
990 struct cgraph_node *direct_call = cgraph_node::get_for_asmname (
991 get_identifier ((const char *) hist->hvalue.counters[0]));
993 if (direct_call == NULL || !check_ic_target (stmt, direct_call))
994 return;
995 if (DECL_STRUCT_FUNCTION (direct_call->decl) == NULL)
996 return;
997 struct cgraph_edge *new_edge
998 = indirect_edge->make_speculative (direct_call, 0, 0);
999 new_edge->redirect_call_stmt_to_callee ();
1000 gimple_remove_histogram_value (cfun, stmt, hist);
1001 inline_call (new_edge, true, NULL, NULL, false);
1004 /* From AutoFDO profiles, find values inside STMT for that we want to measure
1005 histograms and adds them to list VALUES. */
1007 static void
1008 afdo_vpt (gimple_stmt_iterator *gsi, const icall_target_map &map,
1009 bool transform)
1011 afdo_indirect_call (gsi, map, transform);
1014 typedef std::set<basic_block> bb_set;
1015 typedef std::set<edge> edge_set;
1017 static bool
1018 is_bb_annotated (const basic_block bb, const bb_set &annotated)
1020 return annotated.find (bb) != annotated.end ();
1023 static void
1024 set_bb_annotated (basic_block bb, bb_set *annotated)
1026 annotated->insert (bb);
1029 static bool
1030 is_edge_annotated (const edge e, const edge_set &annotated)
1032 return annotated.find (e) != annotated.end ();
1035 static void
1036 set_edge_annotated (edge e, edge_set *annotated)
1038 annotated->insert (e);
1041 /* For a given BB, set its execution count. Attach value profile if a stmt
1042 is not in PROMOTED, because we only want to promote an indirect call once.
1043 Return TRUE if BB is annotated. */
1045 static bool
1046 afdo_set_bb_count (basic_block bb, const stmt_set &promoted)
1048 gimple_stmt_iterator gsi;
1049 edge e;
1050 edge_iterator ei;
1051 gcov_type max_count = 0;
1052 bool has_annotated = false;
1054 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1056 count_info info;
1057 gimple stmt = gsi_stmt (gsi);
1058 if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1059 continue;
1060 if (afdo_source_profile->get_count_info (stmt, &info))
1062 if (info.count > max_count)
1063 max_count = info.count;
1064 has_annotated = true;
1065 if (info.targets.size () > 0
1066 && promoted.find (stmt) == promoted.end ())
1067 afdo_vpt (&gsi, info.targets, false);
1071 if (!has_annotated)
1072 return false;
1074 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1075 afdo_source_profile->mark_annotated (gimple_location (gsi_stmt (gsi)));
1076 for (gphi_iterator gpi = gsi_start_phis (bb);
1077 !gsi_end_p (gpi);
1078 gsi_next (&gpi))
1080 gphi *phi = gpi.phi ();
1081 size_t i;
1082 for (i = 0; i < gimple_phi_num_args (phi); i++)
1083 afdo_source_profile->mark_annotated (gimple_phi_arg_location (phi, i));
1085 FOR_EACH_EDGE (e, ei, bb->succs)
1086 afdo_source_profile->mark_annotated (e->goto_locus);
1088 bb->count = max_count;
1089 return true;
1092 /* BB1 and BB2 are in an equivalent class iff:
1093 1. BB1 dominates BB2.
1094 2. BB2 post-dominates BB1.
1095 3. BB1 and BB2 are in the same loop nest.
1096 This function finds the equivalent class for each basic block, and
1097 stores a pointer to the first BB in its equivalent class. Meanwhile,
1098 set bb counts for the same equivalent class to be idenical. Update
1099 ANNOTATED_BB for the first BB in its equivalent class. */
1101 static void
1102 afdo_find_equiv_class (bb_set *annotated_bb)
1104 basic_block bb;
1106 FOR_ALL_BB_FN (bb, cfun)
1107 bb->aux = NULL;
1109 FOR_ALL_BB_FN (bb, cfun)
1111 vec<basic_block> dom_bbs;
1112 basic_block bb1;
1113 int i;
1115 if (bb->aux != NULL)
1116 continue;
1117 bb->aux = bb;
1118 dom_bbs = get_dominated_by (CDI_DOMINATORS, bb);
1119 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1120 if (bb1->aux == NULL && dominated_by_p (CDI_POST_DOMINATORS, bb, bb1)
1121 && bb1->loop_father == bb->loop_father)
1123 bb1->aux = bb;
1124 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1126 bb->count = bb1->count;
1127 set_bb_annotated (bb, annotated_bb);
1130 dom_bbs = get_dominated_by (CDI_POST_DOMINATORS, bb);
1131 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1132 if (bb1->aux == NULL && dominated_by_p (CDI_DOMINATORS, bb, bb1)
1133 && bb1->loop_father == bb->loop_father)
1135 bb1->aux = bb;
1136 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1138 bb->count = bb1->count;
1139 set_bb_annotated (bb, annotated_bb);
1145 /* If a basic block's count is known, and only one of its in/out edges' count
1146 is unknown, its count can be calculated. Meanwhile, if all of the in/out
1147 edges' counts are known, then the basic block's unknown count can also be
1148 calculated.
1149 IS_SUCC is true if out edges of a basic blocks are examined.
1150 Update ANNOTATED_BB and ANNOTATED_EDGE accordingly.
1151 Return TRUE if any basic block/edge count is changed. */
1153 static bool
1154 afdo_propagate_edge (bool is_succ, bb_set *annotated_bb,
1155 edge_set *annotated_edge)
1157 basic_block bb;
1158 bool changed = false;
1160 FOR_EACH_BB_FN (bb, cfun)
1162 edge e, unknown_edge = NULL;
1163 edge_iterator ei;
1164 int num_unknown_edge = 0;
1165 gcov_type total_known_count = 0;
1167 FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
1168 if (!is_edge_annotated (e, *annotated_edge))
1169 num_unknown_edge++, unknown_edge = e;
1170 else
1171 total_known_count += e->count;
1173 if (num_unknown_edge == 0)
1175 if (total_known_count > bb->count)
1177 bb->count = total_known_count;
1178 changed = true;
1180 if (!is_bb_annotated (bb, *annotated_bb))
1182 set_bb_annotated (bb, annotated_bb);
1183 changed = true;
1186 else if (num_unknown_edge == 1 && is_bb_annotated (bb, *annotated_bb))
1188 if (bb->count >= total_known_count)
1189 unknown_edge->count = bb->count - total_known_count;
1190 else
1191 unknown_edge->count = 0;
1192 set_edge_annotated (unknown_edge, annotated_edge);
1193 changed = true;
1196 return changed;
1199 /* Special propagation for circuit expressions. Because GCC translates
1200 control flow into data flow for circuit expressions. E.g.
1201 BB1:
1202 if (a && b)
1204 else
1207 will be translated into:
1209 BB1:
1210 if (a)
1211 goto BB.t1
1212 else
1213 goto BB.t3
1214 BB.t1:
1215 if (b)
1216 goto BB.t2
1217 else
1218 goto BB.t3
1219 BB.t2:
1220 goto BB.t3
1221 BB.t3:
1222 tmp = PHI (0 (BB1), 0 (BB.t1), 1 (BB.t2)
1223 if (tmp)
1224 goto BB2
1225 else
1226 goto BB3
1228 In this case, we need to propagate through PHI to determine the edge
1229 count of BB1->BB.t1, BB.t1->BB.t2.
1230 Update ANNOTATED_EDGE accordingly. */
1232 static void
1233 afdo_propagate_circuit (const bb_set &annotated_bb, edge_set *annotated_edge)
1235 basic_block bb;
1236 FOR_ALL_BB_FN (bb, cfun)
1238 gimple def_stmt;
1239 tree cmp_rhs, cmp_lhs;
1240 gimple cmp_stmt = last_stmt (bb);
1241 edge e;
1242 edge_iterator ei;
1244 if (!cmp_stmt || gimple_code (cmp_stmt) != GIMPLE_COND)
1245 continue;
1246 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1247 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1248 if (!TREE_CONSTANT (cmp_rhs)
1249 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1250 continue;
1251 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1252 continue;
1253 if (!is_bb_annotated (bb, annotated_bb))
1254 continue;
1255 def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1256 while (def_stmt && gimple_code (def_stmt) == GIMPLE_ASSIGN
1257 && gimple_assign_single_p (def_stmt)
1258 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1259 def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
1260 if (!def_stmt)
1261 continue;
1262 gphi *phi_stmt = dyn_cast <gphi *> (def_stmt);
1263 if (!phi_stmt)
1264 continue;
1265 FOR_EACH_EDGE (e, ei, bb->succs)
1267 unsigned i, total = 0;
1268 edge only_one;
1269 bool check_value_one = (((integer_onep (cmp_rhs))
1270 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1271 ^ ((e->flags & EDGE_TRUE_VALUE) != 0));
1272 if (!is_edge_annotated (e, *annotated_edge))
1273 continue;
1274 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1276 tree val = gimple_phi_arg_def (phi_stmt, i);
1277 edge ep = gimple_phi_arg_edge (phi_stmt, i);
1279 if (!TREE_CONSTANT (val)
1280 || !(integer_zerop (val) || integer_onep (val)))
1281 continue;
1282 if (check_value_one ^ integer_onep (val))
1283 continue;
1284 total++;
1285 only_one = ep;
1286 if (e->probability == 0 && !is_edge_annotated (ep, *annotated_edge))
1288 ep->probability = 0;
1289 ep->count = 0;
1290 set_edge_annotated (ep, annotated_edge);
1293 if (total == 1 && !is_edge_annotated (only_one, *annotated_edge))
1295 only_one->probability = e->probability;
1296 only_one->count = e->count;
1297 set_edge_annotated (only_one, annotated_edge);
1303 /* Propagate the basic block count and edge count on the control flow
1304 graph. We do the propagation iteratively until stablize. */
1306 static void
1307 afdo_propagate (bb_set *annotated_bb, edge_set *annotated_edge)
1309 basic_block bb;
1310 bool changed = true;
1311 int i = 0;
1313 FOR_ALL_BB_FN (bb, cfun)
1315 bb->count = ((basic_block)bb->aux)->count;
1316 if (is_bb_annotated ((const basic_block)bb->aux, *annotated_bb))
1317 set_bb_annotated (bb, annotated_bb);
1320 while (changed && i++ < 10)
1322 changed = false;
1324 if (afdo_propagate_edge (true, annotated_bb, annotated_edge))
1325 changed = true;
1326 if (afdo_propagate_edge (false, annotated_bb, annotated_edge))
1327 changed = true;
1328 afdo_propagate_circuit (*annotated_bb, annotated_edge);
1332 /* Propagate counts on control flow graph and calculate branch
1333 probabilities. */
1335 static void
1336 afdo_calculate_branch_prob (bb_set *annotated_bb, edge_set *annotated_edge)
1338 basic_block bb;
1339 bool has_sample = false;
1341 FOR_EACH_BB_FN (bb, cfun)
1343 if (bb->count > 0)
1345 has_sample = true;
1346 break;
1350 if (!has_sample)
1351 return;
1353 calculate_dominance_info (CDI_POST_DOMINATORS);
1354 calculate_dominance_info (CDI_DOMINATORS);
1355 loop_optimizer_init (0);
1357 afdo_find_equiv_class (annotated_bb);
1358 afdo_propagate (annotated_bb, annotated_edge);
1360 FOR_EACH_BB_FN (bb, cfun)
1362 edge e;
1363 edge_iterator ei;
1364 int num_unknown_succ = 0;
1365 gcov_type total_count = 0;
1367 FOR_EACH_EDGE (e, ei, bb->succs)
1369 if (!is_edge_annotated (e, *annotated_edge))
1370 num_unknown_succ++;
1371 else
1372 total_count += e->count;
1374 if (num_unknown_succ == 0 && total_count > 0)
1376 FOR_EACH_EDGE (e, ei, bb->succs)
1377 e->probability = (double)e->count * REG_BR_PROB_BASE / total_count;
1380 FOR_ALL_BB_FN (bb, cfun)
1382 edge e;
1383 edge_iterator ei;
1385 FOR_EACH_EDGE (e, ei, bb->succs)
1386 e->count = (double)bb->count * e->probability / REG_BR_PROB_BASE;
1387 bb->aux = NULL;
1390 loop_optimizer_finalize ();
1391 free_dominance_info (CDI_DOMINATORS);
1392 free_dominance_info (CDI_POST_DOMINATORS);
1395 /* Perform value profile transformation using AutoFDO profile. Add the
1396 promoted stmts to PROMOTED_STMTS. Return TRUE if there is any
1397 indirect call promoted. */
1399 static bool
1400 afdo_vpt_for_early_inline (stmt_set *promoted_stmts)
1402 basic_block bb;
1403 if (afdo_source_profile->get_function_instance_by_decl (
1404 current_function_decl) == NULL)
1405 return false;
1407 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1409 bool has_vpt = false;
1410 FOR_EACH_BB_FN (bb, cfun)
1412 if (!has_indirect_call (bb))
1413 continue;
1414 gimple_stmt_iterator gsi;
1416 gcov_type bb_count = 0;
1417 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1419 count_info info;
1420 gimple stmt = gsi_stmt (gsi);
1421 if (afdo_source_profile->get_count_info (stmt, &info))
1422 bb_count = MAX (bb_count, info.count);
1425 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1427 gcall *stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
1428 /* IC_promotion and early_inline_2 is done in multiple iterations.
1429 No need to promoted the stmt if its in promoted_stmts (means
1430 it is already been promoted in the previous iterations). */
1431 if ((!stmt) || gimple_call_fn (stmt) == NULL
1432 || TREE_CODE (gimple_call_fn (stmt)) == FUNCTION_DECL
1433 || promoted_stmts->find (stmt) != promoted_stmts->end ())
1434 continue;
1436 count_info info;
1437 afdo_source_profile->get_count_info (stmt, &info);
1438 info.count = bb_count;
1439 if (afdo_source_profile->update_inlined_ind_target (stmt, &info))
1441 /* Promote the indirect call and update the promoted_stmts. */
1442 promoted_stmts->insert (stmt);
1443 afdo_vpt (&gsi, info.targets, true);
1444 has_vpt = true;
1449 if (has_vpt)
1451 optimize_inline_calls (current_function_decl);
1452 return true;
1455 return false;
1458 /* Annotate auto profile to the control flow graph. Do not annotate value
1459 profile for stmts in PROMOTED_STMTS. */
1461 static void
1462 afdo_annotate_cfg (const stmt_set &promoted_stmts)
1464 basic_block bb;
1465 bb_set annotated_bb;
1466 edge_set annotated_edge;
1467 const function_instance *s
1468 = afdo_source_profile->get_function_instance_by_decl (
1469 current_function_decl);
1471 if (s == NULL)
1472 return;
1473 cgraph_node::get (current_function_decl)->count = s->head_count ();
1474 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = s->head_count ();
1475 gcov_type max_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1477 FOR_EACH_BB_FN (bb, cfun)
1479 edge e;
1480 edge_iterator ei;
1482 bb->count = 0;
1483 FOR_EACH_EDGE (e, ei, bb->succs)
1484 e->count = 0;
1486 if (afdo_set_bb_count (bb, promoted_stmts))
1487 set_bb_annotated (bb, &annotated_bb);
1488 if (bb->count > max_count)
1489 max_count = bb->count;
1491 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1492 > ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count)
1494 ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count
1495 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1496 set_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, &annotated_bb);
1498 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1499 > EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count)
1501 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count
1502 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1503 set_bb_annotated (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, &annotated_bb);
1505 afdo_source_profile->mark_annotated (
1506 DECL_SOURCE_LOCATION (current_function_decl));
1507 afdo_source_profile->mark_annotated (cfun->function_start_locus);
1508 afdo_source_profile->mark_annotated (cfun->function_end_locus);
1509 if (max_count > 0)
1511 afdo_calculate_branch_prob (&annotated_bb, &annotated_edge);
1512 counts_to_freqs ();
1513 profile_status_for_fn (cfun) = PROFILE_READ;
1515 if (flag_value_profile_transformations)
1517 gimple_value_profile_transformations ();
1518 free_dominance_info (CDI_DOMINATORS);
1519 free_dominance_info (CDI_POST_DOMINATORS);
1520 update_ssa (TODO_update_ssa);
1524 /* Wrapper function to invoke early inliner. */
1526 static void
1527 early_inline ()
1529 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1530 unsigned todo = early_inliner (cfun);
1531 if (todo & TODO_update_ssa_any)
1532 update_ssa (TODO_update_ssa);
1535 /* Use AutoFDO profile to annoate the control flow graph.
1536 Return the todo flag. */
1538 static unsigned int
1539 auto_profile (void)
1541 struct cgraph_node *node;
1543 if (symtab->state == FINISHED)
1544 return 0;
1546 init_node_map (true);
1547 profile_info = autofdo::afdo_profile_info;
1549 FOR_EACH_FUNCTION (node)
1551 if (!gimple_has_body_p (node->decl))
1552 continue;
1554 /* Don't profile functions produced for builtin stuff. */
1555 if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
1556 continue;
1558 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1560 /* First do indirect call promotion and early inline to make the
1561 IR match the profiled binary before actual annotation.
1563 This is needed because an indirect call might have been promoted
1564 and inlined in the profiled binary. If we do not promote and
1565 inline these indirect calls before annotation, the profile for
1566 these promoted functions will be lost.
1568 e.g. foo() --indirect_call--> bar()
1569 In profiled binary, the callsite is promoted and inlined, making
1570 the profile look like:
1572 foo: {
1573 loc_foo_1: count_1
1574 bar@loc_foo_2: {
1575 loc_bar_1: count_2
1576 loc_bar_2: count_3
1580 Before AutoFDO pass, loc_foo_2 is not promoted thus not inlined.
1581 If we perform annotation on it, the profile inside bar@loc_foo2
1582 will be wasted.
1584 To avoid this, we promote loc_foo_2 and inline the promoted bar
1585 function before annotation, so the profile inside bar@loc_foo2
1586 will be useful. */
1587 autofdo::stmt_set promoted_stmts;
1588 for (int i = 0; i < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS); i++)
1590 if (!flag_value_profile_transformations
1591 || !autofdo::afdo_vpt_for_early_inline (&promoted_stmts))
1592 break;
1593 early_inline ();
1596 early_inline ();
1597 autofdo::afdo_annotate_cfg (promoted_stmts);
1598 compute_function_frequency ();
1600 /* Local pure-const may imply need to fixup the cfg. */
1601 if (execute_fixup_cfg () & TODO_cleanup_cfg)
1602 cleanup_tree_cfg ();
1604 free_dominance_info (CDI_DOMINATORS);
1605 free_dominance_info (CDI_POST_DOMINATORS);
1606 cgraph_edge::rebuild_edges ();
1607 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1608 pop_cfun ();
1611 return TODO_rebuild_cgraph_edges;
1613 } /* namespace autofdo. */
1615 /* Read the profile from the profile data file. */
1617 void
1618 read_autofdo_file (void)
1620 if (auto_profile_file == NULL)
1621 auto_profile_file = DEFAULT_AUTO_PROFILE_FILE;
1623 autofdo::afdo_profile_info = (struct gcov_ctr_summary *)xcalloc (
1624 1, sizeof (struct gcov_ctr_summary));
1625 autofdo::afdo_profile_info->runs = 1;
1626 autofdo::afdo_profile_info->sum_max = 0;
1627 autofdo::afdo_profile_info->sum_all = 0;
1629 /* Read the profile from the profile file. */
1630 autofdo::read_profile ();
1633 /* Free the resources. */
1635 void
1636 end_auto_profile (void)
1638 delete autofdo::afdo_source_profile;
1639 delete autofdo::afdo_string_table;
1640 profile_info = NULL;
1643 /* Returns TRUE if EDGE is hot enough to be inlined early. */
1645 bool
1646 afdo_callsite_hot_enough_for_early_inline (struct cgraph_edge *edge)
1648 gcov_type count
1649 = autofdo::afdo_source_profile->get_callsite_total_count (edge);
1651 if (count > 0)
1653 bool is_hot;
1654 const struct gcov_ctr_summary *saved_profile_info = profile_info;
1655 /* At early inline stage, profile_info is not set yet. We need to
1656 temporarily set it to afdo_profile_info to calculate hotness. */
1657 profile_info = autofdo::afdo_profile_info;
1658 is_hot = maybe_hot_count_p (NULL, count);
1659 profile_info = saved_profile_info;
1660 return is_hot;
1663 return false;
1666 namespace
1669 const pass_data pass_data_ipa_auto_profile = {
1670 SIMPLE_IPA_PASS, "afdo", /* name */
1671 OPTGROUP_NONE, /* optinfo_flags */
1672 TV_IPA_AUTOFDO, /* tv_id */
1673 0, /* properties_required */
1674 0, /* properties_provided */
1675 0, /* properties_destroyed */
1676 0, /* todo_flags_start */
1677 0, /* todo_flags_finish */
1680 class pass_ipa_auto_profile : public simple_ipa_opt_pass
1682 public:
1683 pass_ipa_auto_profile (gcc::context *ctxt)
1684 : simple_ipa_opt_pass (pass_data_ipa_auto_profile, ctxt)
1688 /* opt_pass methods: */
1689 virtual bool
1690 gate (function *)
1692 return flag_auto_profile;
1694 virtual unsigned int
1695 execute (function *)
1697 return autofdo::auto_profile ();
1699 }; // class pass_ipa_auto_profile
1701 } // anon namespace
1703 simple_ipa_opt_pass *
1704 make_pass_ipa_auto_profile (gcc::context *ctxt)
1706 return new pass_ipa_auto_profile (ctxt);