[Patch AArch64 1/3] Enable CRC by default for armv8.1-a
[official-gcc.git] / gcc / auto-profile.c
blob5c0640af4ae49ccf690298c861e5a4891bcda1cc
1 /* Read and annotate call graph profile from the auto profile data file.
2 Copyright (C) 2014-2016 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"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "predict.h"
28 #include "alloc-pool.h"
29 #include "tree-pass.h"
30 #include "ssa.h"
31 #include "cgraph.h"
32 #include "gcov-io.h"
33 #include "diagnostic-core.h"
35 #include <string.h>
36 #include <map>
37 #include <set>
39 #include "profile.h"
40 #include "langhooks.h"
41 #include "cfgloop.h"
42 #include "tree-cfg.h"
43 #include "tree-cfgcleanup.h"
44 #include "tree-into-ssa.h"
45 #include "gimple-iterator.h"
46 #include "value-prof.h"
47 #include "params.h"
48 #include "symbol-summary.h"
49 #include "ipa-prop.h"
50 #include "ipa-inline.h"
51 #include "tree-inline.h"
52 #include "auto-profile.h"
54 /* The following routines implements AutoFDO optimization.
56 This optimization uses sampling profiles to annotate basic block counts
57 and uses heuristics to estimate branch probabilities.
59 There are three phases in AutoFDO:
61 Phase 1: Read profile from the profile data file.
62 The following info is read from the profile datafile:
63 * string_table: a map between function name and its index.
64 * autofdo_source_profile: a map from function_instance name to
65 function_instance. This is represented as a forest of
66 function_instances.
67 * WorkingSet: a histogram of how many instructions are covered for a
68 given percentage of total cycles. This is describing the binary
69 level information (not source level). This info is used to help
70 decide if we want aggressive optimizations that could increase
71 code footprint (e.g. loop unroll etc.)
72 A function instance is an instance of function that could either be a
73 standalone symbol, or a clone of a function that is inlined into another
74 function.
76 Phase 2: Early inline + value profile transformation.
77 Early inline uses autofdo_source_profile to find if a callsite is:
78 * inlined in the profiled binary.
79 * callee body is hot in the profiling run.
80 If both condition satisfies, early inline will inline the callsite
81 regardless of the code growth.
82 Phase 2 is an iterative process. During each iteration, we also check
83 if an indirect callsite is promoted and inlined in the profiling run.
84 If yes, vpt will happen to force promote it and in the next iteration,
85 einline will inline the promoted callsite in the next iteration.
87 Phase 3: Annotate control flow graph.
88 AutoFDO uses a separate pass to:
89 * Annotate basic block count
90 * Estimate branch probability
92 After the above 3 phases, all profile is readily annotated on the GCC IR.
93 AutoFDO tries to reuse all FDO infrastructure as much as possible to make
94 use of the profile. E.g. it uses existing mechanism to calculate the basic
95 block/edge frequency, as well as the cgraph node/edge count.
98 #define DEFAULT_AUTO_PROFILE_FILE "fbdata.afdo"
99 #define AUTO_PROFILE_VERSION 1
101 namespace autofdo
104 /* Represent a source location: (function_decl, lineno). */
105 typedef std::pair<tree, unsigned> decl_lineno;
107 /* Represent an inline stack. vector[0] is the leaf node. */
108 typedef auto_vec<decl_lineno> inline_stack;
110 /* String array that stores function names. */
111 typedef auto_vec<char *> string_vector;
113 /* Map from function name's index in string_table to target's
114 execution count. */
115 typedef std::map<unsigned, gcov_type> icall_target_map;
117 /* Set of gimple stmts. Used to track if the stmt has already been promoted
118 to direct call. */
119 typedef std::set<gimple *> stmt_set;
121 /* Represent count info of an inline stack. */
122 struct count_info
124 /* Sampled count of the inline stack. */
125 gcov_type count;
127 /* Map from indirect call target to its sample count. */
128 icall_target_map targets;
130 /* Whether this inline stack is already used in annotation.
132 Each inline stack should only be used to annotate IR once.
133 This will be enforced when instruction-level discriminator
134 is supported. */
135 bool annotated;
138 /* operator< for "const char *". */
139 struct string_compare
141 bool operator()(const char *a, const char *b) const
143 return strcmp (a, b) < 0;
147 /* Store a string array, indexed by string position in the array. */
148 class string_table
150 public:
151 string_table ()
154 ~string_table ();
156 /* For a given string, returns its index. */
157 int get_index (const char *name) const;
159 /* For a given decl, returns the index of the decl name. */
160 int get_index_by_decl (tree decl) const;
162 /* For a given index, returns the string. */
163 const char *get_name (int index) const;
165 /* Read profile, return TRUE on success. */
166 bool read ();
168 private:
169 typedef std::map<const char *, unsigned, string_compare> string_index_map;
170 string_vector vector_;
171 string_index_map map_;
174 /* Profile of a function instance:
175 1. total_count of the function.
176 2. head_count (entry basic block count) of the function (only valid when
177 function is a top-level function_instance, i.e. it is the original copy
178 instead of the inlined copy).
179 3. map from source location (decl_lineno) to profile (count_info).
180 4. map from callsite to callee function_instance. */
181 class function_instance
183 public:
184 typedef auto_vec<function_instance *> function_instance_stack;
186 /* Read the profile and return a function_instance with head count as
187 HEAD_COUNT. Recursively read callsites to create nested function_instances
188 too. STACK is used to track the recursive creation process. */
189 static function_instance *
190 read_function_instance (function_instance_stack *stack,
191 gcov_type head_count);
193 /* Recursively deallocate all callsites (nested function_instances). */
194 ~function_instance ();
196 /* Accessors. */
198 name () const
200 return name_;
202 gcov_type
203 total_count () const
205 return total_count_;
207 gcov_type
208 head_count () const
210 return head_count_;
213 /* Traverse callsites of the current function_instance to find one at the
214 location of LINENO and callee name represented in DECL. */
215 function_instance *get_function_instance_by_decl (unsigned lineno,
216 tree decl) const;
218 /* Store the profile info for LOC in INFO. Return TRUE if profile info
219 is found. */
220 bool get_count_info (location_t loc, count_info *info) const;
222 /* Read the inlined indirect call target profile for STMT and store it in
223 MAP, return the total count for all inlined indirect calls. */
224 gcov_type find_icall_target_map (gcall *stmt, icall_target_map *map) const;
226 /* Sum of counts that is used during annotation. */
227 gcov_type total_annotated_count () const;
229 /* Mark LOC as annotated. */
230 void mark_annotated (location_t loc);
232 private:
233 /* Callsite, represented as (decl_lineno, callee_function_name_index). */
234 typedef std::pair<unsigned, unsigned> callsite;
236 /* Map from callsite to callee function_instance. */
237 typedef std::map<callsite, function_instance *> callsite_map;
239 function_instance (unsigned name, gcov_type head_count)
240 : name_ (name), total_count_ (0), head_count_ (head_count)
244 /* Map from source location (decl_lineno) to profile (count_info). */
245 typedef std::map<unsigned, count_info> position_count_map;
247 /* function_instance name index in the string_table. */
248 unsigned name_;
250 /* Total sample count. */
251 gcov_type total_count_;
253 /* Entry BB's sample count. */
254 gcov_type head_count_;
256 /* Map from callsite location to callee function_instance. */
257 callsite_map callsites;
259 /* Map from source location to count_info. */
260 position_count_map pos_counts;
263 /* Profile for all functions. */
264 class autofdo_source_profile
266 public:
267 static autofdo_source_profile *
268 create ()
270 autofdo_source_profile *map = new autofdo_source_profile ();
272 if (map->read ())
273 return map;
274 delete map;
275 return NULL;
278 ~autofdo_source_profile ();
280 /* For a given DECL, returns the top-level function_instance. */
281 function_instance *get_function_instance_by_decl (tree decl) const;
283 /* Find count_info for a given gimple STMT. If found, store the count_info
284 in INFO and return true; otherwise return false. */
285 bool get_count_info (gimple *stmt, count_info *info) const;
287 /* Find total count of the callee of EDGE. */
288 gcov_type get_callsite_total_count (struct cgraph_edge *edge) const;
290 /* Update value profile INFO for STMT from the inlined indirect callsite.
291 Return true if INFO is updated. */
292 bool update_inlined_ind_target (gcall *stmt, count_info *info);
294 /* Mark LOC as annotated. */
295 void mark_annotated (location_t loc);
297 private:
298 /* Map from function_instance name index (in string_table) to
299 function_instance. */
300 typedef std::map<unsigned, function_instance *> name_function_instance_map;
302 autofdo_source_profile () {}
304 /* Read AutoFDO profile and returns TRUE on success. */
305 bool read ();
307 /* Return the function_instance in the profile that correspond to the
308 inline STACK. */
309 function_instance *
310 get_function_instance_by_inline_stack (const inline_stack &stack) const;
312 name_function_instance_map map_;
315 /* Store the strings read from the profile data file. */
316 static string_table *afdo_string_table;
318 /* Store the AutoFDO source profile. */
319 static autofdo_source_profile *afdo_source_profile;
321 /* gcov_ctr_summary structure to store the profile_info. */
322 static struct gcov_ctr_summary *afdo_profile_info;
324 /* Helper functions. */
326 /* Return the original name of NAME: strip the suffix that starts
327 with '.' Caller is responsible for freeing RET. */
329 static char *
330 get_original_name (const char *name)
332 char *ret = xstrdup (name);
333 char *find = strchr (ret, '.');
334 if (find != NULL)
335 *find = 0;
336 return ret;
339 /* Return the combined location, which is a 32bit integer in which
340 higher 16 bits stores the line offset of LOC to the start lineno
341 of DECL, The lower 16 bits stores the discriminator. */
343 static unsigned
344 get_combined_location (location_t loc, tree decl)
346 /* TODO: allow more bits for line and less bits for discriminator. */
347 if (LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl) >= (1<<16))
348 warning_at (loc, OPT_Woverflow, "Offset exceeds 16 bytes.");
349 return ((LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) << 16);
352 /* Return the function decl of a given lexical BLOCK. */
354 static tree
355 get_function_decl_from_block (tree block)
357 tree decl;
359 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block) == UNKNOWN_LOCATION))
360 return NULL_TREE;
362 for (decl = BLOCK_ABSTRACT_ORIGIN (block);
363 decl && (TREE_CODE (decl) == BLOCK);
364 decl = BLOCK_ABSTRACT_ORIGIN (decl))
365 if (TREE_CODE (decl) == FUNCTION_DECL)
366 break;
367 return decl;
370 /* Store inline stack for STMT in STACK. */
372 static void
373 get_inline_stack (location_t locus, inline_stack *stack)
375 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
376 return;
378 tree block = LOCATION_BLOCK (locus);
379 if (block && TREE_CODE (block) == BLOCK)
381 int level = 0;
382 for (block = BLOCK_SUPERCONTEXT (block);
383 block && (TREE_CODE (block) == BLOCK);
384 block = BLOCK_SUPERCONTEXT (block))
386 location_t tmp_locus = BLOCK_SOURCE_LOCATION (block);
387 if (LOCATION_LOCUS (tmp_locus) == UNKNOWN_LOCATION)
388 continue;
390 tree decl = get_function_decl_from_block (block);
391 stack->safe_push (
392 std::make_pair (decl, get_combined_location (locus, decl)));
393 locus = tmp_locus;
394 level++;
397 stack->safe_push (
398 std::make_pair (current_function_decl,
399 get_combined_location (locus, current_function_decl)));
402 /* Return STMT's combined location, which is a 32bit integer in which
403 higher 16 bits stores the line offset of LOC to the start lineno
404 of DECL, The lower 16 bits stores the discriminator. */
406 static unsigned
407 get_relative_location_for_stmt (gimple *stmt)
409 location_t locus = gimple_location (stmt);
410 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
411 return UNKNOWN_LOCATION;
413 for (tree block = gimple_block (stmt); block && (TREE_CODE (block) == BLOCK);
414 block = BLOCK_SUPERCONTEXT (block))
415 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block)) != UNKNOWN_LOCATION)
416 return get_combined_location (locus,
417 get_function_decl_from_block (block));
418 return get_combined_location (locus, current_function_decl);
421 /* Return true if BB contains indirect call. */
423 static bool
424 has_indirect_call (basic_block bb)
426 gimple_stmt_iterator gsi;
428 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
430 gimple *stmt = gsi_stmt (gsi);
431 if (gimple_code (stmt) == GIMPLE_CALL && !gimple_call_internal_p (stmt)
432 && (gimple_call_fn (stmt) == NULL
433 || TREE_CODE (gimple_call_fn (stmt)) != FUNCTION_DECL))
434 return true;
436 return false;
439 /* Member functions for string_table. */
441 /* Deconstructor. */
443 string_table::~string_table ()
445 for (unsigned i = 0; i < vector_.length (); i++)
446 free (vector_[i]);
450 /* Return the index of a given function NAME. Return -1 if NAME is not
451 found in string table. */
454 string_table::get_index (const char *name) const
456 if (name == NULL)
457 return -1;
458 string_index_map::const_iterator iter = map_.find (name);
459 if (iter == map_.end ())
460 return -1;
462 return iter->second;
465 /* Return the index of a given function DECL. Return -1 if DECL is not
466 found in string table. */
469 string_table::get_index_by_decl (tree decl) const
471 char *name
472 = get_original_name (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
473 int ret = get_index (name);
474 free (name);
475 if (ret != -1)
476 return ret;
477 ret = get_index (lang_hooks.dwarf_name (decl, 0));
478 if (ret != -1)
479 return ret;
480 if (DECL_ABSTRACT_ORIGIN (decl))
481 return get_index_by_decl (DECL_ABSTRACT_ORIGIN (decl));
483 return -1;
486 /* Return the function name of a given INDEX. */
488 const char *
489 string_table::get_name (int index) const
491 gcc_assert (index > 0 && index < (int)vector_.length ());
492 return vector_[index];
495 /* Read the string table. Return TRUE if reading is successful. */
497 bool
498 string_table::read ()
500 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FILE_NAMES)
501 return false;
502 /* Skip the length of the section. */
503 gcov_read_unsigned ();
504 /* Read in the file name table. */
505 unsigned string_num = gcov_read_unsigned ();
506 for (unsigned i = 0; i < string_num; i++)
508 vector_.safe_push (get_original_name (gcov_read_string ()));
509 map_[vector_.last ()] = i;
511 return true;
514 /* Member functions for function_instance. */
516 function_instance::~function_instance ()
518 for (callsite_map::iterator iter = callsites.begin ();
519 iter != callsites.end (); ++iter)
520 delete iter->second;
523 /* Traverse callsites of the current function_instance to find one at the
524 location of LINENO and callee name represented in DECL. */
526 function_instance *
527 function_instance::get_function_instance_by_decl (unsigned lineno,
528 tree decl) const
530 int func_name_idx = afdo_string_table->get_index_by_decl (decl);
531 if (func_name_idx != -1)
533 callsite_map::const_iterator ret
534 = callsites.find (std::make_pair (lineno, func_name_idx));
535 if (ret != callsites.end ())
536 return ret->second;
538 func_name_idx
539 = afdo_string_table->get_index (lang_hooks.dwarf_name (decl, 0));
540 if (func_name_idx != -1)
542 callsite_map::const_iterator ret
543 = callsites.find (std::make_pair (lineno, func_name_idx));
544 if (ret != callsites.end ())
545 return ret->second;
547 if (DECL_ABSTRACT_ORIGIN (decl))
548 return get_function_instance_by_decl (lineno, DECL_ABSTRACT_ORIGIN (decl));
550 return NULL;
553 /* Store the profile info for LOC in INFO. Return TRUE if profile info
554 is found. */
556 bool
557 function_instance::get_count_info (location_t loc, count_info *info) const
559 position_count_map::const_iterator iter = pos_counts.find (loc);
560 if (iter == pos_counts.end ())
561 return false;
562 *info = iter->second;
563 return true;
566 /* Mark LOC as annotated. */
568 void
569 function_instance::mark_annotated (location_t loc)
571 position_count_map::iterator iter = pos_counts.find (loc);
572 if (iter == pos_counts.end ())
573 return;
574 iter->second.annotated = true;
577 /* Read the inlined indirect call target profile for STMT and store it in
578 MAP, return the total count for all inlined indirect calls. */
580 gcov_type
581 function_instance::find_icall_target_map (gcall *stmt,
582 icall_target_map *map) const
584 gcov_type ret = 0;
585 unsigned stmt_offset = get_relative_location_for_stmt (stmt);
587 for (callsite_map::const_iterator iter = callsites.begin ();
588 iter != callsites.end (); ++iter)
590 unsigned callee = iter->second->name ();
591 /* Check if callsite location match the stmt. */
592 if (iter->first.first != stmt_offset)
593 continue;
594 struct cgraph_node *node = cgraph_node::get_for_asmname (
595 get_identifier (afdo_string_table->get_name (callee)));
596 if (node == NULL)
597 continue;
598 if (!check_ic_target (stmt, node))
599 continue;
600 (*map)[callee] = iter->second->total_count ();
601 ret += iter->second->total_count ();
603 return ret;
606 /* Read the profile and create a function_instance with head count as
607 HEAD_COUNT. Recursively read callsites to create nested function_instances
608 too. STACK is used to track the recursive creation process. */
610 /* function instance profile format:
612 ENTRY_COUNT: 8 bytes
613 NAME_INDEX: 4 bytes
614 NUM_POS_COUNTS: 4 bytes
615 NUM_CALLSITES: 4 byte
616 POS_COUNT_1:
617 POS_1_OFFSET: 4 bytes
618 NUM_TARGETS: 4 bytes
619 COUNT: 8 bytes
620 TARGET_1:
621 VALUE_PROFILE_TYPE: 4 bytes
622 TARGET_IDX: 8 bytes
623 COUNT: 8 bytes
624 TARGET_2
626 TARGET_n
627 POS_COUNT_2
629 POS_COUNT_N
630 CALLSITE_1:
631 CALLSITE_1_OFFSET: 4 bytes
632 FUNCTION_INSTANCE_PROFILE (nested)
633 CALLSITE_2
635 CALLSITE_n. */
637 function_instance *
638 function_instance::read_function_instance (function_instance_stack *stack,
639 gcov_type head_count)
641 unsigned name = gcov_read_unsigned ();
642 unsigned num_pos_counts = gcov_read_unsigned ();
643 unsigned num_callsites = gcov_read_unsigned ();
644 function_instance *s = new function_instance (name, head_count);
645 stack->safe_push (s);
647 for (unsigned i = 0; i < num_pos_counts; i++)
649 unsigned offset = gcov_read_unsigned () & 0xffff0000;
650 unsigned num_targets = gcov_read_unsigned ();
651 gcov_type count = gcov_read_counter ();
652 s->pos_counts[offset].count = count;
653 for (unsigned j = 0; j < stack->length (); j++)
654 (*stack)[j]->total_count_ += count;
655 for (unsigned j = 0; j < num_targets; j++)
657 /* Only indirect call target histogram is supported now. */
658 gcov_read_unsigned ();
659 gcov_type target_idx = gcov_read_counter ();
660 s->pos_counts[offset].targets[target_idx] = gcov_read_counter ();
663 for (unsigned i = 0; i < num_callsites; i++)
665 unsigned offset = gcov_read_unsigned ();
666 function_instance *callee_function_instance
667 = read_function_instance (stack, 0);
668 s->callsites[std::make_pair (offset, callee_function_instance->name ())]
669 = callee_function_instance;
671 stack->pop ();
672 return s;
675 /* Sum of counts that is used during annotation. */
677 gcov_type
678 function_instance::total_annotated_count () const
680 gcov_type ret = 0;
681 for (callsite_map::const_iterator iter = callsites.begin ();
682 iter != callsites.end (); ++iter)
683 ret += iter->second->total_annotated_count ();
684 for (position_count_map::const_iterator iter = pos_counts.begin ();
685 iter != pos_counts.end (); ++iter)
686 if (iter->second.annotated)
687 ret += iter->second.count;
688 return ret;
691 /* Member functions for autofdo_source_profile. */
693 autofdo_source_profile::~autofdo_source_profile ()
695 for (name_function_instance_map::const_iterator iter = map_.begin ();
696 iter != map_.end (); ++iter)
697 delete iter->second;
700 /* For a given DECL, returns the top-level function_instance. */
702 function_instance *
703 autofdo_source_profile::get_function_instance_by_decl (tree decl) const
705 int index = afdo_string_table->get_index_by_decl (decl);
706 if (index == -1)
707 return NULL;
708 name_function_instance_map::const_iterator ret = map_.find (index);
709 return ret == map_.end () ? NULL : ret->second;
712 /* Find count_info for a given gimple STMT. If found, store the count_info
713 in INFO and return true; otherwise return false. */
715 bool
716 autofdo_source_profile::get_count_info (gimple *stmt, count_info *info) const
718 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
719 return false;
721 inline_stack stack;
722 get_inline_stack (gimple_location (stmt), &stack);
723 if (stack.length () == 0)
724 return false;
725 function_instance *s = get_function_instance_by_inline_stack (stack);
726 if (s == NULL)
727 return false;
728 return s->get_count_info (stack[0].second, info);
731 /* Mark LOC as annotated. */
733 void
734 autofdo_source_profile::mark_annotated (location_t loc)
736 inline_stack stack;
737 get_inline_stack (loc, &stack);
738 if (stack.length () == 0)
739 return;
740 function_instance *s = get_function_instance_by_inline_stack (stack);
741 if (s == NULL)
742 return;
743 s->mark_annotated (stack[0].second);
746 /* Update value profile INFO for STMT from the inlined indirect callsite.
747 Return true if INFO is updated. */
749 bool
750 autofdo_source_profile::update_inlined_ind_target (gcall *stmt,
751 count_info *info)
753 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
754 return false;
756 count_info old_info;
757 get_count_info (stmt, &old_info);
758 gcov_type total = 0;
759 for (icall_target_map::const_iterator iter = old_info.targets.begin ();
760 iter != old_info.targets.end (); ++iter)
761 total += iter->second;
763 /* Program behavior changed, original promoted (and inlined) target is not
764 hot any more. Will avoid promote the original target.
766 To check if original promoted target is still hot, we check the total
767 count of the unpromoted targets (stored in old_info). If it is no less
768 than half of the callsite count (stored in INFO), the original promoted
769 target is considered not hot any more. */
770 if (total >= info->count / 2)
771 return false;
773 inline_stack stack;
774 get_inline_stack (gimple_location (stmt), &stack);
775 if (stack.length () == 0)
776 return false;
777 function_instance *s = get_function_instance_by_inline_stack (stack);
778 if (s == NULL)
779 return false;
780 icall_target_map map;
781 if (s->find_icall_target_map (stmt, &map) == 0)
782 return false;
783 for (icall_target_map::const_iterator iter = map.begin ();
784 iter != map.end (); ++iter)
785 info->targets[iter->first] = iter->second;
786 return true;
789 /* Find total count of the callee of EDGE. */
791 gcov_type
792 autofdo_source_profile::get_callsite_total_count (
793 struct cgraph_edge *edge) const
795 inline_stack stack;
796 stack.safe_push (std::make_pair (edge->callee->decl, 0));
797 get_inline_stack (gimple_location (edge->call_stmt), &stack);
799 function_instance *s = get_function_instance_by_inline_stack (stack);
800 if (s == NULL
801 || afdo_string_table->get_index (IDENTIFIER_POINTER (
802 DECL_ASSEMBLER_NAME (edge->callee->decl))) != s->name ())
803 return 0;
805 return s->total_count ();
808 /* Read AutoFDO profile and returns TRUE on success. */
810 /* source profile format:
812 GCOV_TAG_AFDO_FUNCTION: 4 bytes
813 LENGTH: 4 bytes
814 NUM_FUNCTIONS: 4 bytes
815 FUNCTION_INSTANCE_1
816 FUNCTION_INSTANCE_2
818 FUNCTION_INSTANCE_N. */
820 bool
821 autofdo_source_profile::read ()
823 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FUNCTION)
825 inform (0, "Not expected TAG.");
826 return false;
829 /* Skip the length of the section. */
830 gcov_read_unsigned ();
832 /* Read in the function/callsite profile, and store it in local
833 data structure. */
834 unsigned function_num = gcov_read_unsigned ();
835 for (unsigned i = 0; i < function_num; i++)
837 function_instance::function_instance_stack stack;
838 function_instance *s = function_instance::read_function_instance (
839 &stack, gcov_read_counter ());
840 afdo_profile_info->sum_all += s->total_count ();
841 map_[s->name ()] = s;
843 return true;
846 /* Return the function_instance in the profile that correspond to the
847 inline STACK. */
849 function_instance *
850 autofdo_source_profile::get_function_instance_by_inline_stack (
851 const inline_stack &stack) const
853 name_function_instance_map::const_iterator iter = map_.find (
854 afdo_string_table->get_index_by_decl (stack[stack.length () - 1].first));
855 if (iter == map_.end())
856 return NULL;
857 function_instance *s = iter->second;
858 for (unsigned i = stack.length() - 1; i > 0; i--)
860 s = s->get_function_instance_by_decl (
861 stack[i].second, stack[i - 1].first);
862 if (s == NULL)
863 return NULL;
865 return s;
868 /* Module profile is only used by LIPO. Here we simply ignore it. */
870 static void
871 fake_read_autofdo_module_profile ()
873 /* Read in the module info. */
874 gcov_read_unsigned ();
876 /* Skip the length of the section. */
877 gcov_read_unsigned ();
879 /* Read in the file name table. */
880 unsigned total_module_num = gcov_read_unsigned ();
881 gcc_assert (total_module_num == 0);
884 /* Read data from profile data file. */
886 static void
887 read_profile (void)
889 if (gcov_open (auto_profile_file, 1) == 0)
890 error ("Cannot open profile file %s.", auto_profile_file);
892 if (gcov_read_unsigned () != GCOV_DATA_MAGIC)
893 error ("AutoFDO profile magic number does not mathch.");
895 /* Skip the version number. */
896 unsigned version = gcov_read_unsigned ();
897 if (version != AUTO_PROFILE_VERSION)
898 error ("AutoFDO profile version %u does match %u.",
899 version, AUTO_PROFILE_VERSION);
901 /* Skip the empty integer. */
902 gcov_read_unsigned ();
904 /* string_table. */
905 afdo_string_table = new string_table ();
906 if (!afdo_string_table->read())
907 error ("Cannot read string table from %s.", auto_profile_file);
909 /* autofdo_source_profile. */
910 afdo_source_profile = autofdo_source_profile::create ();
911 if (afdo_source_profile == NULL)
912 error ("Cannot read function profile from %s.", auto_profile_file);
914 /* autofdo_module_profile. */
915 fake_read_autofdo_module_profile ();
917 /* Read in the working set. */
918 if (gcov_read_unsigned () != GCOV_TAG_AFDO_WORKING_SET)
919 error ("Cannot read working set from %s.", auto_profile_file);
921 /* Skip the length of the section. */
922 gcov_read_unsigned ();
923 gcov_working_set_t set[128];
924 for (unsigned i = 0; i < 128; i++)
926 set[i].num_counters = gcov_read_unsigned ();
927 set[i].min_counter = gcov_read_counter ();
929 add_working_set (set);
932 /* From AutoFDO profiles, find values inside STMT for that we want to measure
933 histograms for indirect-call optimization.
935 This function is actually served for 2 purposes:
936 * before annotation, we need to mark histogram, promote and inline
937 * after annotation, we just need to mark, and let follow-up logic to
938 decide if it needs to promote and inline. */
940 static void
941 afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map,
942 bool transform)
944 gimple *gs = gsi_stmt (*gsi);
945 tree callee;
947 if (map.size () == 0)
948 return;
949 gcall *stmt = dyn_cast <gcall *> (gs);
950 if ((!stmt) || gimple_call_fndecl (stmt) != NULL_TREE)
951 return;
953 callee = gimple_call_fn (stmt);
955 histogram_value hist = gimple_alloc_histogram_value (
956 cfun, HIST_TYPE_INDIR_CALL, stmt, callee);
957 hist->n_counters = 3;
958 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
959 gimple_add_histogram_value (cfun, stmt, hist);
961 gcov_type total = 0;
962 icall_target_map::const_iterator max_iter = map.end ();
964 for (icall_target_map::const_iterator iter = map.begin ();
965 iter != map.end (); ++iter)
967 total += iter->second;
968 if (max_iter == map.end () || max_iter->second < iter->second)
969 max_iter = iter;
972 hist->hvalue.counters[0]
973 = (unsigned long long)afdo_string_table->get_name (max_iter->first);
974 hist->hvalue.counters[1] = max_iter->second;
975 hist->hvalue.counters[2] = total;
977 if (!transform)
978 return;
980 struct cgraph_edge *indirect_edge
981 = cgraph_node::get (current_function_decl)->get_edge (stmt);
982 struct cgraph_node *direct_call = cgraph_node::get_for_asmname (
983 get_identifier ((const char *) hist->hvalue.counters[0]));
985 if (direct_call == NULL || !check_ic_target (stmt, direct_call))
986 return;
987 if (DECL_STRUCT_FUNCTION (direct_call->decl) == NULL)
988 return;
989 struct cgraph_edge *new_edge
990 = indirect_edge->make_speculative (direct_call, 0, 0);
991 new_edge->redirect_call_stmt_to_callee ();
992 gimple_remove_histogram_value (cfun, stmt, hist);
993 inline_call (new_edge, true, NULL, NULL, false);
996 /* From AutoFDO profiles, find values inside STMT for that we want to measure
997 histograms and adds them to list VALUES. */
999 static void
1000 afdo_vpt (gimple_stmt_iterator *gsi, const icall_target_map &map,
1001 bool transform)
1003 afdo_indirect_call (gsi, map, transform);
1006 typedef std::set<basic_block> bb_set;
1007 typedef std::set<edge> edge_set;
1009 static bool
1010 is_bb_annotated (const basic_block bb, const bb_set &annotated)
1012 return annotated.find (bb) != annotated.end ();
1015 static void
1016 set_bb_annotated (basic_block bb, bb_set *annotated)
1018 annotated->insert (bb);
1021 static bool
1022 is_edge_annotated (const edge e, const edge_set &annotated)
1024 return annotated.find (e) != annotated.end ();
1027 static void
1028 set_edge_annotated (edge e, edge_set *annotated)
1030 annotated->insert (e);
1033 /* For a given BB, set its execution count. Attach value profile if a stmt
1034 is not in PROMOTED, because we only want to promote an indirect call once.
1035 Return TRUE if BB is annotated. */
1037 static bool
1038 afdo_set_bb_count (basic_block bb, const stmt_set &promoted)
1040 gimple_stmt_iterator gsi;
1041 edge e;
1042 edge_iterator ei;
1043 gcov_type max_count = 0;
1044 bool has_annotated = false;
1046 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1048 count_info info;
1049 gimple *stmt = gsi_stmt (gsi);
1050 if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1051 continue;
1052 if (afdo_source_profile->get_count_info (stmt, &info))
1054 if (info.count > max_count)
1055 max_count = info.count;
1056 has_annotated = true;
1057 if (info.targets.size () > 0
1058 && promoted.find (stmt) == promoted.end ())
1059 afdo_vpt (&gsi, info.targets, false);
1063 if (!has_annotated)
1064 return false;
1066 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1067 afdo_source_profile->mark_annotated (gimple_location (gsi_stmt (gsi)));
1068 for (gphi_iterator gpi = gsi_start_phis (bb);
1069 !gsi_end_p (gpi);
1070 gsi_next (&gpi))
1072 gphi *phi = gpi.phi ();
1073 size_t i;
1074 for (i = 0; i < gimple_phi_num_args (phi); i++)
1075 afdo_source_profile->mark_annotated (gimple_phi_arg_location (phi, i));
1077 FOR_EACH_EDGE (e, ei, bb->succs)
1078 afdo_source_profile->mark_annotated (e->goto_locus);
1080 bb->count = max_count;
1081 return true;
1084 /* BB1 and BB2 are in an equivalent class iff:
1085 1. BB1 dominates BB2.
1086 2. BB2 post-dominates BB1.
1087 3. BB1 and BB2 are in the same loop nest.
1088 This function finds the equivalent class for each basic block, and
1089 stores a pointer to the first BB in its equivalent class. Meanwhile,
1090 set bb counts for the same equivalent class to be idenical. Update
1091 ANNOTATED_BB for the first BB in its equivalent class. */
1093 static void
1094 afdo_find_equiv_class (bb_set *annotated_bb)
1096 basic_block bb;
1098 FOR_ALL_BB_FN (bb, cfun)
1099 bb->aux = NULL;
1101 FOR_ALL_BB_FN (bb, cfun)
1103 vec<basic_block> dom_bbs;
1104 basic_block bb1;
1105 int i;
1107 if (bb->aux != NULL)
1108 continue;
1109 bb->aux = bb;
1110 dom_bbs = get_dominated_by (CDI_DOMINATORS, bb);
1111 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1112 if (bb1->aux == NULL && dominated_by_p (CDI_POST_DOMINATORS, bb, bb1)
1113 && bb1->loop_father == bb->loop_father)
1115 bb1->aux = bb;
1116 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1118 bb->count = bb1->count;
1119 set_bb_annotated (bb, annotated_bb);
1122 dom_bbs = get_dominated_by (CDI_POST_DOMINATORS, bb);
1123 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1124 if (bb1->aux == NULL && dominated_by_p (CDI_DOMINATORS, bb, bb1)
1125 && bb1->loop_father == bb->loop_father)
1127 bb1->aux = bb;
1128 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1130 bb->count = bb1->count;
1131 set_bb_annotated (bb, annotated_bb);
1137 /* If a basic block's count is known, and only one of its in/out edges' count
1138 is unknown, its count can be calculated. Meanwhile, if all of the in/out
1139 edges' counts are known, then the basic block's unknown count can also be
1140 calculated.
1141 IS_SUCC is true if out edges of a basic blocks are examined.
1142 Update ANNOTATED_BB and ANNOTATED_EDGE accordingly.
1143 Return TRUE if any basic block/edge count is changed. */
1145 static bool
1146 afdo_propagate_edge (bool is_succ, bb_set *annotated_bb,
1147 edge_set *annotated_edge)
1149 basic_block bb;
1150 bool changed = false;
1152 FOR_EACH_BB_FN (bb, cfun)
1154 edge e, unknown_edge = NULL;
1155 edge_iterator ei;
1156 int num_unknown_edge = 0;
1157 gcov_type total_known_count = 0;
1159 FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
1160 if (!is_edge_annotated (e, *annotated_edge))
1161 num_unknown_edge++, unknown_edge = e;
1162 else
1163 total_known_count += e->count;
1165 if (num_unknown_edge == 0)
1167 if (total_known_count > bb->count)
1169 bb->count = total_known_count;
1170 changed = true;
1172 if (!is_bb_annotated (bb, *annotated_bb))
1174 set_bb_annotated (bb, annotated_bb);
1175 changed = true;
1178 else if (num_unknown_edge == 1 && is_bb_annotated (bb, *annotated_bb))
1180 if (bb->count >= total_known_count)
1181 unknown_edge->count = bb->count - total_known_count;
1182 else
1183 unknown_edge->count = 0;
1184 set_edge_annotated (unknown_edge, annotated_edge);
1185 changed = true;
1188 return changed;
1191 /* Special propagation for circuit expressions. Because GCC translates
1192 control flow into data flow for circuit expressions. E.g.
1193 BB1:
1194 if (a && b)
1196 else
1199 will be translated into:
1201 BB1:
1202 if (a)
1203 goto BB.t1
1204 else
1205 goto BB.t3
1206 BB.t1:
1207 if (b)
1208 goto BB.t2
1209 else
1210 goto BB.t3
1211 BB.t2:
1212 goto BB.t3
1213 BB.t3:
1214 tmp = PHI (0 (BB1), 0 (BB.t1), 1 (BB.t2)
1215 if (tmp)
1216 goto BB2
1217 else
1218 goto BB3
1220 In this case, we need to propagate through PHI to determine the edge
1221 count of BB1->BB.t1, BB.t1->BB.t2.
1222 Update ANNOTATED_EDGE accordingly. */
1224 static void
1225 afdo_propagate_circuit (const bb_set &annotated_bb, edge_set *annotated_edge)
1227 basic_block bb;
1228 FOR_ALL_BB_FN (bb, cfun)
1230 gimple *def_stmt;
1231 tree cmp_rhs, cmp_lhs;
1232 gimple *cmp_stmt = last_stmt (bb);
1233 edge e;
1234 edge_iterator ei;
1236 if (!cmp_stmt || gimple_code (cmp_stmt) != GIMPLE_COND)
1237 continue;
1238 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1239 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1240 if (!TREE_CONSTANT (cmp_rhs)
1241 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1242 continue;
1243 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1244 continue;
1245 if (!is_bb_annotated (bb, annotated_bb))
1246 continue;
1247 def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1248 while (def_stmt && gimple_code (def_stmt) == GIMPLE_ASSIGN
1249 && gimple_assign_single_p (def_stmt)
1250 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1251 def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
1252 if (!def_stmt)
1253 continue;
1254 gphi *phi_stmt = dyn_cast <gphi *> (def_stmt);
1255 if (!phi_stmt)
1256 continue;
1257 FOR_EACH_EDGE (e, ei, bb->succs)
1259 unsigned i, total = 0;
1260 edge only_one;
1261 bool check_value_one = (((integer_onep (cmp_rhs))
1262 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1263 ^ ((e->flags & EDGE_TRUE_VALUE) != 0));
1264 if (!is_edge_annotated (e, *annotated_edge))
1265 continue;
1266 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1268 tree val = gimple_phi_arg_def (phi_stmt, i);
1269 edge ep = gimple_phi_arg_edge (phi_stmt, i);
1271 if (!TREE_CONSTANT (val)
1272 || !(integer_zerop (val) || integer_onep (val)))
1273 continue;
1274 if (check_value_one ^ integer_onep (val))
1275 continue;
1276 total++;
1277 only_one = ep;
1278 if (e->probability == 0 && !is_edge_annotated (ep, *annotated_edge))
1280 ep->probability = 0;
1281 ep->count = 0;
1282 set_edge_annotated (ep, annotated_edge);
1285 if (total == 1 && !is_edge_annotated (only_one, *annotated_edge))
1287 only_one->probability = e->probability;
1288 only_one->count = e->count;
1289 set_edge_annotated (only_one, annotated_edge);
1295 /* Propagate the basic block count and edge count on the control flow
1296 graph. We do the propagation iteratively until stablize. */
1298 static void
1299 afdo_propagate (bb_set *annotated_bb, edge_set *annotated_edge)
1301 basic_block bb;
1302 bool changed = true;
1303 int i = 0;
1305 FOR_ALL_BB_FN (bb, cfun)
1307 bb->count = ((basic_block)bb->aux)->count;
1308 if (is_bb_annotated ((const basic_block)bb->aux, *annotated_bb))
1309 set_bb_annotated (bb, annotated_bb);
1312 while (changed && i++ < 10)
1314 changed = false;
1316 if (afdo_propagate_edge (true, annotated_bb, annotated_edge))
1317 changed = true;
1318 if (afdo_propagate_edge (false, annotated_bb, annotated_edge))
1319 changed = true;
1320 afdo_propagate_circuit (*annotated_bb, annotated_edge);
1324 /* Propagate counts on control flow graph and calculate branch
1325 probabilities. */
1327 static void
1328 afdo_calculate_branch_prob (bb_set *annotated_bb, edge_set *annotated_edge)
1330 basic_block bb;
1331 bool has_sample = false;
1333 FOR_EACH_BB_FN (bb, cfun)
1335 if (bb->count > 0)
1337 has_sample = true;
1338 break;
1342 if (!has_sample)
1343 return;
1345 calculate_dominance_info (CDI_POST_DOMINATORS);
1346 calculate_dominance_info (CDI_DOMINATORS);
1347 loop_optimizer_init (0);
1349 afdo_find_equiv_class (annotated_bb);
1350 afdo_propagate (annotated_bb, annotated_edge);
1352 FOR_EACH_BB_FN (bb, cfun)
1354 edge e;
1355 edge_iterator ei;
1356 int num_unknown_succ = 0;
1357 gcov_type total_count = 0;
1359 FOR_EACH_EDGE (e, ei, bb->succs)
1361 if (!is_edge_annotated (e, *annotated_edge))
1362 num_unknown_succ++;
1363 else
1364 total_count += e->count;
1366 if (num_unknown_succ == 0 && total_count > 0)
1368 FOR_EACH_EDGE (e, ei, bb->succs)
1369 e->probability = (double)e->count * REG_BR_PROB_BASE / total_count;
1372 FOR_ALL_BB_FN (bb, cfun)
1374 edge e;
1375 edge_iterator ei;
1377 FOR_EACH_EDGE (e, ei, bb->succs)
1378 e->count = (double)bb->count * e->probability / REG_BR_PROB_BASE;
1379 bb->aux = NULL;
1382 loop_optimizer_finalize ();
1383 free_dominance_info (CDI_DOMINATORS);
1384 free_dominance_info (CDI_POST_DOMINATORS);
1387 /* Perform value profile transformation using AutoFDO profile. Add the
1388 promoted stmts to PROMOTED_STMTS. Return TRUE if there is any
1389 indirect call promoted. */
1391 static bool
1392 afdo_vpt_for_early_inline (stmt_set *promoted_stmts)
1394 basic_block bb;
1395 if (afdo_source_profile->get_function_instance_by_decl (
1396 current_function_decl) == NULL)
1397 return false;
1399 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1401 bool has_vpt = false;
1402 FOR_EACH_BB_FN (bb, cfun)
1404 if (!has_indirect_call (bb))
1405 continue;
1406 gimple_stmt_iterator gsi;
1408 gcov_type bb_count = 0;
1409 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1411 count_info info;
1412 gimple *stmt = gsi_stmt (gsi);
1413 if (afdo_source_profile->get_count_info (stmt, &info))
1414 bb_count = MAX (bb_count, info.count);
1417 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1419 gcall *stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
1420 /* IC_promotion and early_inline_2 is done in multiple iterations.
1421 No need to promoted the stmt if its in promoted_stmts (means
1422 it is already been promoted in the previous iterations). */
1423 if ((!stmt) || gimple_call_fn (stmt) == NULL
1424 || TREE_CODE (gimple_call_fn (stmt)) == FUNCTION_DECL
1425 || promoted_stmts->find (stmt) != promoted_stmts->end ())
1426 continue;
1428 count_info info;
1429 afdo_source_profile->get_count_info (stmt, &info);
1430 info.count = bb_count;
1431 if (afdo_source_profile->update_inlined_ind_target (stmt, &info))
1433 /* Promote the indirect call and update the promoted_stmts. */
1434 promoted_stmts->insert (stmt);
1435 afdo_vpt (&gsi, info.targets, true);
1436 has_vpt = true;
1441 if (has_vpt)
1443 optimize_inline_calls (current_function_decl);
1444 return true;
1447 return false;
1450 /* Annotate auto profile to the control flow graph. Do not annotate value
1451 profile for stmts in PROMOTED_STMTS. */
1453 static void
1454 afdo_annotate_cfg (const stmt_set &promoted_stmts)
1456 basic_block bb;
1457 bb_set annotated_bb;
1458 edge_set annotated_edge;
1459 const function_instance *s
1460 = afdo_source_profile->get_function_instance_by_decl (
1461 current_function_decl);
1463 if (s == NULL)
1464 return;
1465 cgraph_node::get (current_function_decl)->count = s->head_count ();
1466 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = s->head_count ();
1467 gcov_type max_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1469 FOR_EACH_BB_FN (bb, cfun)
1471 edge e;
1472 edge_iterator ei;
1474 bb->count = 0;
1475 FOR_EACH_EDGE (e, ei, bb->succs)
1476 e->count = 0;
1478 if (afdo_set_bb_count (bb, promoted_stmts))
1479 set_bb_annotated (bb, &annotated_bb);
1480 if (bb->count > max_count)
1481 max_count = bb->count;
1483 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1484 > ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count)
1486 ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count
1487 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1488 set_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, &annotated_bb);
1490 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1491 > EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count)
1493 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count
1494 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1495 set_bb_annotated (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, &annotated_bb);
1497 afdo_source_profile->mark_annotated (
1498 DECL_SOURCE_LOCATION (current_function_decl));
1499 afdo_source_profile->mark_annotated (cfun->function_start_locus);
1500 afdo_source_profile->mark_annotated (cfun->function_end_locus);
1501 if (max_count > 0)
1503 afdo_calculate_branch_prob (&annotated_bb, &annotated_edge);
1504 counts_to_freqs ();
1505 profile_status_for_fn (cfun) = PROFILE_READ;
1507 if (flag_value_profile_transformations)
1509 gimple_value_profile_transformations ();
1510 free_dominance_info (CDI_DOMINATORS);
1511 free_dominance_info (CDI_POST_DOMINATORS);
1512 update_ssa (TODO_update_ssa);
1516 /* Wrapper function to invoke early inliner. */
1518 static void
1519 early_inline ()
1521 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1522 unsigned todo = early_inliner (cfun);
1523 if (todo & TODO_update_ssa_any)
1524 update_ssa (TODO_update_ssa);
1527 /* Use AutoFDO profile to annoate the control flow graph.
1528 Return the todo flag. */
1530 static unsigned int
1531 auto_profile (void)
1533 struct cgraph_node *node;
1535 if (symtab->state == FINISHED)
1536 return 0;
1538 init_node_map (true);
1539 profile_info = autofdo::afdo_profile_info;
1541 FOR_EACH_FUNCTION (node)
1543 if (!gimple_has_body_p (node->decl))
1544 continue;
1546 /* Don't profile functions produced for builtin stuff. */
1547 if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
1548 continue;
1550 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1552 /* First do indirect call promotion and early inline to make the
1553 IR match the profiled binary before actual annotation.
1555 This is needed because an indirect call might have been promoted
1556 and inlined in the profiled binary. If we do not promote and
1557 inline these indirect calls before annotation, the profile for
1558 these promoted functions will be lost.
1560 e.g. foo() --indirect_call--> bar()
1561 In profiled binary, the callsite is promoted and inlined, making
1562 the profile look like:
1564 foo: {
1565 loc_foo_1: count_1
1566 bar@loc_foo_2: {
1567 loc_bar_1: count_2
1568 loc_bar_2: count_3
1572 Before AutoFDO pass, loc_foo_2 is not promoted thus not inlined.
1573 If we perform annotation on it, the profile inside bar@loc_foo2
1574 will be wasted.
1576 To avoid this, we promote loc_foo_2 and inline the promoted bar
1577 function before annotation, so the profile inside bar@loc_foo2
1578 will be useful. */
1579 autofdo::stmt_set promoted_stmts;
1580 for (int i = 0; i < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS); i++)
1582 if (!flag_value_profile_transformations
1583 || !autofdo::afdo_vpt_for_early_inline (&promoted_stmts))
1584 break;
1585 early_inline ();
1588 early_inline ();
1589 autofdo::afdo_annotate_cfg (promoted_stmts);
1590 compute_function_frequency ();
1592 /* Local pure-const may imply need to fixup the cfg. */
1593 if (execute_fixup_cfg () & TODO_cleanup_cfg)
1594 cleanup_tree_cfg ();
1596 free_dominance_info (CDI_DOMINATORS);
1597 free_dominance_info (CDI_POST_DOMINATORS);
1598 cgraph_edge::rebuild_edges ();
1599 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1600 pop_cfun ();
1603 return TODO_rebuild_cgraph_edges;
1605 } /* namespace autofdo. */
1607 /* Read the profile from the profile data file. */
1609 void
1610 read_autofdo_file (void)
1612 if (auto_profile_file == NULL)
1613 auto_profile_file = DEFAULT_AUTO_PROFILE_FILE;
1615 autofdo::afdo_profile_info = (struct gcov_ctr_summary *)xcalloc (
1616 1, sizeof (struct gcov_ctr_summary));
1617 autofdo::afdo_profile_info->runs = 1;
1618 autofdo::afdo_profile_info->sum_max = 0;
1619 autofdo::afdo_profile_info->sum_all = 0;
1621 /* Read the profile from the profile file. */
1622 autofdo::read_profile ();
1625 /* Free the resources. */
1627 void
1628 end_auto_profile (void)
1630 delete autofdo::afdo_source_profile;
1631 delete autofdo::afdo_string_table;
1632 profile_info = NULL;
1635 /* Returns TRUE if EDGE is hot enough to be inlined early. */
1637 bool
1638 afdo_callsite_hot_enough_for_early_inline (struct cgraph_edge *edge)
1640 gcov_type count
1641 = autofdo::afdo_source_profile->get_callsite_total_count (edge);
1643 if (count > 0)
1645 bool is_hot;
1646 const struct gcov_ctr_summary *saved_profile_info = profile_info;
1647 /* At early inline stage, profile_info is not set yet. We need to
1648 temporarily set it to afdo_profile_info to calculate hotness. */
1649 profile_info = autofdo::afdo_profile_info;
1650 is_hot = maybe_hot_count_p (NULL, count);
1651 profile_info = saved_profile_info;
1652 return is_hot;
1655 return false;
1658 namespace
1661 const pass_data pass_data_ipa_auto_profile = {
1662 SIMPLE_IPA_PASS, "afdo", /* name */
1663 OPTGROUP_NONE, /* optinfo_flags */
1664 TV_IPA_AUTOFDO, /* tv_id */
1665 0, /* properties_required */
1666 0, /* properties_provided */
1667 0, /* properties_destroyed */
1668 0, /* todo_flags_start */
1669 0, /* todo_flags_finish */
1672 class pass_ipa_auto_profile : public simple_ipa_opt_pass
1674 public:
1675 pass_ipa_auto_profile (gcc::context *ctxt)
1676 : simple_ipa_opt_pass (pass_data_ipa_auto_profile, ctxt)
1680 /* opt_pass methods: */
1681 virtual bool
1682 gate (function *)
1684 return flag_auto_profile;
1686 virtual unsigned int
1687 execute (function *)
1689 return autofdo::auto_profile ();
1691 }; // class pass_ipa_auto_profile
1693 } // anon namespace
1695 simple_ipa_opt_pass *
1696 make_pass_ipa_auto_profile (gcc::context *ctxt)
1698 return new pass_ipa_auto_profile (ctxt);