New syntax for -fsanitize-recover.
[gcc.git] / gcc / auto-profile.c
1 /* Read and annotate call graph profile from the auto profile data file.
2 Copyright (C) 2014. Free Software Foundation, Inc.
3 Contributed by Dehao Chen (dehao@google.com)
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include <string.h>
22 #include <map>
23 #include <set>
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tree.h"
29 #include "tree-pass.h"
30 #include "flags.h"
31 #include "basic-block.h"
32 #include "diagnostic-core.h"
33 #include "gcov-io.h"
34 #include "input.h"
35 #include "profile.h"
36 #include "langhooks.h"
37 #include "opts.h"
38 #include "tree-pass.h"
39 #include "cfgloop.h"
40 #include "tree-ssa-alias.h"
41 #include "tree-cfg.h"
42 #include "tree-cfgcleanup.h"
43 #include "tree-ssa-operands.h"
44 #include "tree-into-ssa.h"
45 #include "internal-fn.h"
46 #include "is-a.h"
47 #include "gimple-expr.h"
48 #include "gimple.h"
49 #include "gimple-iterator.h"
50 #include "gimple-ssa.h"
51 #include "cgraph.h"
52 #include "value-prof.h"
53 #include "coverage.h"
54 #include "params.h"
55 #include "ipa-inline.h"
56 #include "tree-inline.h"
57 #include "stringpool.h"
58 #include "auto-profile.h"
59 #include "vec.h"
60
61 /* The following routines implements AutoFDO optimization.
62
63 This optimization uses sampling profiles to annotate basic block counts
64 and uses heuristics to estimate branch probabilities.
65
66 There are three phases in AutoFDO:
67
68 Phase 1: Read profile from the profile data file.
69 The following info is read from the profile datafile:
70 * string_table: a map between function name and its index.
71 * autofdo_source_profile: a map from function_instance name to
72 function_instance. This is represented as a forest of
73 function_instances.
74 * WorkingSet: a histogram of how many instructions are covered for a
75 given percentage of total cycles. This is describing the binary
76 level information (not source level). This info is used to help
77 decide if we want aggressive optimizations that could increase
78 code footprint (e.g. loop unroll etc.)
79 A function instance is an instance of function that could either be a
80 standalone symbol, or a clone of a function that is inlined into another
81 function.
82
83 Phase 2: Early inline + valur profile transformation.
84 Early inline uses autofdo_source_profile to find if a callsite is:
85 * inlined in the profiled binary.
86 * callee body is hot in the profiling run.
87 If both condition satisfies, early inline will inline the callsite
88 regardless of the code growth.
89 Phase 2 is an iterative process. During each iteration, we also check
90 if an indirect callsite is promoted and inlined in the profiling run.
91 If yes, vpt will happen to force promote it and in the next iteration,
92 einline will inline the promoted callsite in the next iteration.
93
94 Phase 3: Annotate control flow graph.
95 AutoFDO uses a separate pass to:
96 * Annotate basic block count
97 * Estimate branch probability
98
99 After the above 3 phases, all profile is readily annotated on the GCC IR.
100 AutoFDO tries to reuse all FDO infrastructure as much as possible to make
101 use of the profile. E.g. it uses existing mechanism to calculate the basic
102 block/edge frequency, as well as the cgraph node/edge count.
103 */
104
105 #define DEFAULT_AUTO_PROFILE_FILE "fbdata.afdo"
106 #define AUTO_PROFILE_VERSION 1
107
108 namespace autofdo
109 {
110
111 /* Represent a source location: (function_decl, lineno). */
112 typedef std::pair<tree, unsigned> decl_lineno;
113
114 /* Represent an inline stack. vector[0] is the leaf node. */
115 typedef auto_vec<decl_lineno> inline_stack;
116
117 /* String array that stores function names. */
118 typedef auto_vec<char *> string_vector;
119
120 /* Map from function name's index in string_table to target's
121 execution count. */
122 typedef std::map<unsigned, gcov_type> icall_target_map;
123
124 /* Set of gimple stmts. Used to track if the stmt has already been promoted
125 to direct call. */
126 typedef std::set<gimple> stmt_set;
127
128 /* Represent count info of an inline stack. */
129 struct count_info
130 {
131 /* Sampled count of the inline stack. */
132 gcov_type count;
133
134 /* Map from indirect call target to its sample count. */
135 icall_target_map targets;
136
137 /* Whether this inline stack is already used in annotation.
138
139 Each inline stack should only be used to annotate IR once.
140 This will be enforced when instruction-level discriminator
141 is supported. */
142 bool annotated;
143 };
144
145 /* operator< for "const char *". */
146 struct string_compare
147 {
148 bool operator()(const char *a, const char *b) const
149 {
150 return strcmp (a, b) < 0;
151 }
152 };
153
154 /* Store a string array, indexed by string position in the array. */
155 class string_table
156 {
157 public:
158 string_table ()
159 {}
160
161 ~string_table ();
162
163 /* For a given string, returns its index. */
164 int get_index (const char *name) const;
165
166 /* For a given decl, returns the index of the decl name. */
167 int get_index_by_decl (tree decl) const;
168
169 /* For a given index, returns the string. */
170 const char *get_name (int index) const;
171
172 /* Read profile, return TRUE on success. */
173 bool read ();
174
175 private:
176 typedef std::map<const char *, unsigned, string_compare> string_index_map;
177 string_vector vector_;
178 string_index_map map_;
179 };
180
181 /* Profile of a function instance:
182 1. total_count of the function.
183 2. head_count (entry basic block count) of the function (only valid when
184 function is a top-level function_instance, i.e. it is the original copy
185 instead of the inlined copy).
186 3. map from source location (decl_lineno) to profile (count_info).
187 4. map from callsite to callee function_instance. */
188 class function_instance
189 {
190 public:
191 typedef auto_vec<function_instance *> function_instance_stack;
192
193 /* Read the profile and return a function_instance with head count as
194 HEAD_COUNT. Recursively read callsites to create nested function_instances
195 too. STACK is used to track the recursive creation process. */
196 static function_instance *
197 read_function_instance (function_instance_stack *stack,
198 gcov_type head_count);
199
200 /* Recursively deallocate all callsites (nested function_instances). */
201 ~function_instance ();
202
203 /* Accessors. */
204 int
205 name () const
206 {
207 return name_;
208 }
209 gcov_type
210 total_count () const
211 {
212 return total_count_;
213 }
214 gcov_type
215 head_count () const
216 {
217 return head_count_;
218 }
219
220 /* Traverse callsites of the current function_instance to find one at the
221 location of LINENO and callee name represented in DECL. */
222 function_instance *get_function_instance_by_decl (unsigned lineno,
223 tree decl) const;
224
225 /* Store the profile info for LOC in INFO. Return TRUE if profile info
226 is found. */
227 bool get_count_info (location_t loc, count_info *info) const;
228
229 /* Read the inlined indirect call target profile for STMT and store it in
230 MAP, return the total count for all inlined indirect calls. */
231 gcov_type find_icall_target_map (gimple stmt, icall_target_map *map) const;
232
233 /* Sum of counts that is used during annotation. */
234 gcov_type total_annotated_count () const;
235
236 /* Mark LOC as annotated. */
237 void mark_annotated (location_t loc);
238
239 private:
240 /* Callsite, represented as (decl_lineno, callee_function_name_index). */
241 typedef std::pair<unsigned, unsigned> callsite;
242
243 /* Map from callsite to callee function_instance. */
244 typedef std::map<callsite, function_instance *> callsite_map;
245
246 function_instance (unsigned name, gcov_type head_count)
247 : name_ (name), total_count_ (0), head_count_ (head_count)
248 {
249 }
250
251 /* Map from source location (decl_lineno) to profile (count_info). */
252 typedef std::map<unsigned, count_info> position_count_map;
253
254 /* function_instance name index in the string_table. */
255 unsigned name_;
256
257 /* Total sample count. */
258 gcov_type total_count_;
259
260 /* Entry BB's sample count. */
261 gcov_type head_count_;
262
263 /* Map from callsite location to callee function_instance. */
264 callsite_map callsites;
265
266 /* Map from source location to count_info. */
267 position_count_map pos_counts;
268 };
269
270 /* Profile for all functions. */
271 class autofdo_source_profile
272 {
273 public:
274 static autofdo_source_profile *
275 create ()
276 {
277 autofdo_source_profile *map = new autofdo_source_profile ();
278
279 if (map->read ())
280 return map;
281 delete map;
282 return NULL;
283 }
284
285 ~autofdo_source_profile ();
286
287 /* For a given DECL, returns the top-level function_instance. */
288 function_instance *get_function_instance_by_decl (tree decl) const;
289
290 /* Find count_info for a given gimple STMT. If found, store the count_info
291 in INFO and return true; otherwise return false. */
292 bool get_count_info (gimple stmt, count_info *info) const;
293
294 /* Find total count of the callee of EDGE. */
295 gcov_type get_callsite_total_count (struct cgraph_edge *edge) const;
296
297 /* Update value profile INFO for STMT from the inlined indirect callsite.
298 Return true if INFO is updated. */
299 bool update_inlined_ind_target (gimple stmt, count_info *info);
300
301 /* Mark LOC as annotated. */
302 void mark_annotated (location_t loc);
303
304 private:
305 /* Map from function_instance name index (in string_table) to
306 function_instance. */
307 typedef std::map<unsigned, function_instance *> name_function_instance_map;
308
309 autofdo_source_profile () {}
310
311 /* Read AutoFDO profile and returns TRUE on success. */
312 bool read ();
313
314 /* Return the function_instance in the profile that correspond to the
315 inline STACK. */
316 function_instance *
317 get_function_instance_by_inline_stack (const inline_stack &stack) const;
318
319 name_function_instance_map map_;
320 };
321
322 /* Store the strings read from the profile data file. */
323 static string_table *afdo_string_table;
324
325 /* Store the AutoFDO source profile. */
326 static autofdo_source_profile *afdo_source_profile;
327
328 /* gcov_ctr_summary structure to store the profile_info. */
329 static struct gcov_ctr_summary *afdo_profile_info;
330
331 /* Helper functions. */
332
333 /* Return the original name of NAME: strip the suffix that starts
334 with '.' Caller is responsible for freeing RET. */
335
336 static char *
337 get_original_name (const char *name)
338 {
339 char *ret = xstrdup (name);
340 char *find = strchr (ret, '.');
341 if (find != NULL)
342 *find = 0;
343 return ret;
344 }
345
346 /* Return the combined location, which is a 32bit integer in which
347 higher 16 bits stores the line offset of LOC to the start lineno
348 of DECL, The lower 16 bits stores the discrimnator. */
349
350 static unsigned
351 get_combined_location (location_t loc, tree decl)
352 {
353 /* TODO: allow more bits for line and less bits for discriminator. */
354 if (LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl) >= (1<<16))
355 warning_at (loc, OPT_Woverflow, "Offset exceeds 16 bytes.");
356 return ((LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) << 16);
357 }
358
359 /* Return the function decl of a given lexical BLOCK. */
360
361 static tree
362 get_function_decl_from_block (tree block)
363 {
364 tree decl;
365
366 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block) == UNKNOWN_LOCATION))
367 return NULL_TREE;
368
369 for (decl = BLOCK_ABSTRACT_ORIGIN (block);
370 decl && (TREE_CODE (decl) == BLOCK);
371 decl = BLOCK_ABSTRACT_ORIGIN (decl))
372 if (TREE_CODE (decl) == FUNCTION_DECL)
373 break;
374 return decl;
375 }
376
377 /* Store inline stack for STMT in STACK. */
378
379 static void
380 get_inline_stack (location_t locus, inline_stack *stack)
381 {
382 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
383 return;
384
385 tree block = LOCATION_BLOCK (locus);
386 if (block && TREE_CODE (block) == BLOCK)
387 {
388 int level = 0;
389 for (block = BLOCK_SUPERCONTEXT (block);
390 block && (TREE_CODE (block) == BLOCK);
391 block = BLOCK_SUPERCONTEXT (block))
392 {
393 location_t tmp_locus = BLOCK_SOURCE_LOCATION (block);
394 if (LOCATION_LOCUS (tmp_locus) == UNKNOWN_LOCATION)
395 continue;
396
397 tree decl = get_function_decl_from_block (block);
398 stack->safe_push (
399 std::make_pair (decl, get_combined_location (locus, decl)));
400 locus = tmp_locus;
401 level++;
402 }
403 }
404 stack->safe_push (
405 std::make_pair (current_function_decl,
406 get_combined_location (locus, current_function_decl)));
407 }
408
409 /* Return STMT's combined location, which is a 32bit integer in which
410 higher 16 bits stores the line offset of LOC to the start lineno
411 of DECL, The lower 16 bits stores the discrimnator. */
412
413 static unsigned
414 get_relative_location_for_stmt (gimple stmt)
415 {
416 location_t locus = gimple_location (stmt);
417 if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
418 return UNKNOWN_LOCATION;
419
420 for (tree block = gimple_block (stmt); block && (TREE_CODE (block) == BLOCK);
421 block = BLOCK_SUPERCONTEXT (block))
422 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (block)) != UNKNOWN_LOCATION)
423 return get_combined_location (locus,
424 get_function_decl_from_block (block));
425 return get_combined_location (locus, current_function_decl);
426 }
427
428 /* Return true if BB contains indirect call. */
429
430 static bool
431 has_indirect_call (basic_block bb)
432 {
433 gimple_stmt_iterator gsi;
434
435 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
436 {
437 gimple stmt = gsi_stmt (gsi);
438 if (gimple_code (stmt) == GIMPLE_CALL && !gimple_call_internal_p (stmt)
439 && (gimple_call_fn (stmt) == NULL
440 || TREE_CODE (gimple_call_fn (stmt)) != FUNCTION_DECL))
441 return true;
442 }
443 return false;
444 }
445
446 /* Member functions for string_table. */
447
448 /* Deconstructor. */
449
450 string_table::~string_table ()
451 {
452 for (unsigned i = 0; i < vector_.length (); i++)
453 free (vector_[i]);
454 }
455
456
457 /* Return the index of a given function NAME. Return -1 if NAME is not
458 found in string table. */
459
460 int
461 string_table::get_index (const char *name) const
462 {
463 if (name == NULL)
464 return -1;
465 string_index_map::const_iterator iter = map_.find (name);
466 if (iter == map_.end ())
467 return -1;
468 else
469 return iter->second;
470 }
471
472 /* Return the index of a given function DECL. Return -1 if DECL is not
473 found in string table. */
474
475 int
476 string_table::get_index_by_decl (tree decl) const
477 {
478 char *name
479 = get_original_name (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
480 int ret = get_index (name);
481 free (name);
482 if (ret != -1)
483 return ret;
484 ret = get_index (lang_hooks.dwarf_name (decl, 0));
485 if (ret != -1)
486 return ret;
487 if (DECL_ABSTRACT_ORIGIN (decl))
488 return get_index_by_decl (DECL_ABSTRACT_ORIGIN (decl));
489 else
490 return -1;
491 }
492
493 /* Return the function name of a given INDEX. */
494
495 const char *
496 string_table::get_name (int index) const
497 {
498 gcc_assert (index > 0 && index < (int)vector_.length ());
499 return vector_[index];
500 }
501
502 /* Read the string table. Return TRUE if reading is successful. */
503
504 bool
505 string_table::read ()
506 {
507 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FILE_NAMES)
508 return false;
509 /* Skip the length of the section. */
510 gcov_read_unsigned ();
511 /* Read in the file name table. */
512 unsigned string_num = gcov_read_unsigned ();
513 for (unsigned i = 0; i < string_num; i++)
514 {
515 vector_.safe_push (get_original_name (gcov_read_string ()));
516 map_[vector_.last ()] = i;
517 }
518 return true;
519 }
520
521 /* Member functions for function_instance. */
522
523 function_instance::~function_instance ()
524 {
525 for (callsite_map::iterator iter = callsites.begin ();
526 iter != callsites.end (); ++iter)
527 delete iter->second;
528 }
529
530 /* Traverse callsites of the current function_instance to find one at the
531 location of LINENO and callee name represented in DECL. */
532
533 function_instance *
534 function_instance::get_function_instance_by_decl (unsigned lineno,
535 tree decl) const
536 {
537 int func_name_idx = afdo_string_table->get_index_by_decl (decl);
538 if (func_name_idx != -1)
539 {
540 callsite_map::const_iterator ret
541 = callsites.find (std::make_pair (lineno, func_name_idx));
542 if (ret != callsites.end ())
543 return ret->second;
544 }
545 func_name_idx
546 = afdo_string_table->get_index (lang_hooks.dwarf_name (decl, 0));
547 if (func_name_idx != -1)
548 {
549 callsite_map::const_iterator ret
550 = callsites.find (std::make_pair (lineno, func_name_idx));
551 if (ret != callsites.end ())
552 return ret->second;
553 }
554 if (DECL_ABSTRACT_ORIGIN (decl))
555 return get_function_instance_by_decl (lineno, DECL_ABSTRACT_ORIGIN (decl));
556 else
557 return NULL;
558 }
559
560 /* Store the profile info for LOC in INFO. Return TRUE if profile info
561 is found. */
562
563 bool
564 function_instance::get_count_info (location_t loc, count_info *info) const
565 {
566 position_count_map::const_iterator iter = pos_counts.find (loc);
567 if (iter == pos_counts.end ())
568 return false;
569 *info = iter->second;
570 return true;
571 }
572
573 /* Mark LOC as annotated. */
574
575 void
576 function_instance::mark_annotated (location_t loc)
577 {
578 position_count_map::iterator iter = pos_counts.find (loc);
579 if (iter == pos_counts.end ())
580 return;
581 iter->second.annotated = true;
582 }
583
584 /* Read the inlinied indirect call target profile for STMT and store it in
585 MAP, return the total count for all inlined indirect calls. */
586
587 gcov_type
588 function_instance::find_icall_target_map (gimple stmt,
589 icall_target_map *map) const
590 {
591 gcov_type ret = 0;
592 unsigned stmt_offset = get_relative_location_for_stmt (stmt);
593
594 for (callsite_map::const_iterator iter = callsites.begin ();
595 iter != callsites.end (); ++iter)
596 {
597 unsigned callee = iter->second->name ();
598 /* Check if callsite location match the stmt. */
599 if (iter->first.first != stmt_offset)
600 continue;
601 struct cgraph_node *node = cgraph_node::get_for_asmname (
602 get_identifier (afdo_string_table->get_name (callee)));
603 if (node == NULL)
604 continue;
605 if (!check_ic_target (stmt, node))
606 continue;
607 (*map)[callee] = iter->second->total_count ();
608 ret += iter->second->total_count ();
609 }
610 return ret;
611 }
612
613 /* Read the profile and create a function_instance with head count as
614 HEAD_COUNT. Recursively read callsites to create nested function_instances
615 too. STACK is used to track the recursive creation process. */
616
617 /* function instance profile format:
618
619 ENTRY_COUNT: 8 bytes
620 NAME_INDEX: 4 bytes
621 NUM_POS_COUNTS: 4 bytes
622 NUM_CALLSITES: 4 byte
623 POS_COUNT_1:
624 POS_1_OFFSET: 4 bytes
625 NUM_TARGETS: 4 bytes
626 COUNT: 8 bytes
627 TARGET_1:
628 VALUE_PROFILE_TYPE: 4 bytes
629 TARGET_IDX: 8 bytes
630 COUNT: 8 bytes
631 TARGET_2
632 ...
633 TARGET_n
634 POS_COUNT_2
635 ...
636 POS_COUNT_N
637 CALLSITE_1:
638 CALLSITE_1_OFFSET: 4 bytes
639 FUNCTION_INSTANCE_PROFILE (nested)
640 CALLSITE_2
641 ...
642 CALLSITE_n. */
643
644 function_instance *
645 function_instance::read_function_instance (function_instance_stack *stack,
646 gcov_type head_count)
647 {
648 unsigned name = gcov_read_unsigned ();
649 unsigned num_pos_counts = gcov_read_unsigned ();
650 unsigned num_callsites = gcov_read_unsigned ();
651 function_instance *s = new function_instance (name, head_count);
652 stack->safe_push (s);
653
654 for (unsigned i = 0; i < num_pos_counts; i++)
655 {
656 unsigned offset = gcov_read_unsigned () & 0xffff0000;
657 unsigned num_targets = gcov_read_unsigned ();
658 gcov_type count = gcov_read_counter ();
659 s->pos_counts[offset].count = count;
660 for (unsigned j = 0; j < stack->length (); j++)
661 (*stack)[j]->total_count_ += count;
662 for (unsigned j = 0; j < num_targets; j++)
663 {
664 /* Only indirect call target histogram is supported now. */
665 gcov_read_unsigned ();
666 gcov_type target_idx = gcov_read_counter ();
667 s->pos_counts[offset].targets[target_idx] = gcov_read_counter ();
668 }
669 }
670 for (unsigned i = 0; i < num_callsites; i++)
671 {
672 unsigned offset = gcov_read_unsigned ();
673 function_instance *callee_function_instance
674 = read_function_instance (stack, 0);
675 s->callsites[std::make_pair (offset, callee_function_instance->name ())]
676 = callee_function_instance;
677 }
678 stack->pop ();
679 return s;
680 }
681
682 /* Sum of counts that is used during annotation. */
683
684 gcov_type
685 function_instance::total_annotated_count () const
686 {
687 gcov_type ret = 0;
688 for (callsite_map::const_iterator iter = callsites.begin ();
689 iter != callsites.end (); ++iter)
690 ret += iter->second->total_annotated_count ();
691 for (position_count_map::const_iterator iter = pos_counts.begin ();
692 iter != pos_counts.end (); ++iter)
693 if (iter->second.annotated)
694 ret += iter->second.count;
695 return ret;
696 }
697
698 /* Member functions for autofdo_source_profile. */
699
700 autofdo_source_profile::~autofdo_source_profile ()
701 {
702 for (name_function_instance_map::const_iterator iter = map_.begin ();
703 iter != map_.end (); ++iter)
704 delete iter->second;
705 }
706
707 /* For a given DECL, returns the top-level function_instance. */
708
709 function_instance *
710 autofdo_source_profile::get_function_instance_by_decl (tree decl) const
711 {
712 int index = afdo_string_table->get_index_by_decl (decl);
713 if (index == -1)
714 return NULL;
715 name_function_instance_map::const_iterator ret = map_.find (index);
716 return ret == map_.end () ? NULL : ret->second;
717 }
718
719 /* Find count_info for a given gimple STMT. If found, store the count_info
720 in INFO and return true; otherwise return false. */
721
722 bool
723 autofdo_source_profile::get_count_info (gimple stmt, count_info *info) const
724 {
725 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
726 return false;
727
728 inline_stack stack;
729 get_inline_stack (gimple_location (stmt), &stack);
730 if (stack.length () == 0)
731 return false;
732 function_instance *s = get_function_instance_by_inline_stack (stack);
733 if (s == NULL)
734 return false;
735 return s->get_count_info (stack[0].second, info);
736 }
737
738 /* Mark LOC as annotated. */
739
740 void
741 autofdo_source_profile::mark_annotated (location_t loc)
742 {
743 inline_stack stack;
744 get_inline_stack (loc, &stack);
745 if (stack.length () == 0)
746 return;
747 function_instance *s = get_function_instance_by_inline_stack (stack);
748 if (s == NULL)
749 return;
750 s->mark_annotated (stack[0].second);
751 }
752
753 /* Update value profile INFO for STMT from the inlined indirect callsite.
754 Return true if INFO is updated. */
755
756 bool
757 autofdo_source_profile::update_inlined_ind_target (gimple stmt,
758 count_info *info)
759 {
760 if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
761 return false;
762
763 count_info old_info;
764 get_count_info (stmt, &old_info);
765 gcov_type total = 0;
766 for (icall_target_map::const_iterator iter = old_info.targets.begin ();
767 iter != old_info.targets.end (); ++iter)
768 total += iter->second;
769
770 /* Program behavior changed, original promoted (and inlined) target is not
771 hot any more. Will avoid promote the original target.
772
773 To check if original promoted target is still hot, we check the total
774 count of the unpromoted targets (stored in old_info). If it is no less
775 than half of the callsite count (stored in INFO), the original promoted
776 target is considered not hot any more. */
777 if (total >= info->count / 2)
778 return false;
779
780 inline_stack stack;
781 get_inline_stack (gimple_location (stmt), &stack);
782 if (stack.length () == 0)
783 return false;
784 function_instance *s = get_function_instance_by_inline_stack (stack);
785 if (s == NULL)
786 return false;
787 icall_target_map map;
788 if (s->find_icall_target_map (stmt, &map) == 0)
789 return false;
790 for (icall_target_map::const_iterator iter = map.begin ();
791 iter != map.end (); ++iter)
792 info->targets[iter->first] = iter->second;
793 return true;
794 }
795
796 /* Find total count of the callee of EDGE. */
797
798 gcov_type
799 autofdo_source_profile::get_callsite_total_count (
800 struct cgraph_edge *edge) const
801 {
802 inline_stack stack;
803 stack.safe_push (std::make_pair (edge->callee->decl, 0));
804 get_inline_stack (gimple_location (edge->call_stmt), &stack);
805
806 function_instance *s = get_function_instance_by_inline_stack (stack);
807 if (s == NULL
808 || afdo_string_table->get_index (IDENTIFIER_POINTER (
809 DECL_ASSEMBLER_NAME (edge->callee->decl))) != s->name ())
810 return 0;
811 else
812 return s->total_count ();
813 }
814
815 /* Read AutoFDO profile and returns TRUE on success. */
816
817 /* source profile format:
818
819 GCOV_TAG_AFDO_FUNCTION: 4 bytes
820 LENGTH: 4 bytes
821 NUM_FUNCTIONS: 4 bytes
822 FUNCTION_INSTANCE_1
823 FUNCTION_INSTANCE_2
824 ...
825 FUNCTION_INSTANCE_N. */
826
827 bool
828 autofdo_source_profile::read ()
829 {
830 if (gcov_read_unsigned () != GCOV_TAG_AFDO_FUNCTION)
831 {
832 inform (0, "Not expected TAG.");
833 return false;
834 }
835
836 /* Skip the length of the section. */
837 gcov_read_unsigned ();
838
839 /* Read in the function/callsite profile, and store it in local
840 data structure. */
841 unsigned function_num = gcov_read_unsigned ();
842 for (unsigned i = 0; i < function_num; i++)
843 {
844 function_instance::function_instance_stack stack;
845 function_instance *s = function_instance::read_function_instance (
846 &stack, gcov_read_counter ());
847 afdo_profile_info->sum_all += s->total_count ();
848 map_[s->name ()] = s;
849 }
850 return true;
851 }
852
853 /* Return the function_instance in the profile that correspond to the
854 inline STACK. */
855
856 function_instance *
857 autofdo_source_profile::get_function_instance_by_inline_stack (
858 const inline_stack &stack) const
859 {
860 name_function_instance_map::const_iterator iter = map_.find (
861 afdo_string_table->get_index_by_decl (stack[stack.length () - 1].first));
862 if (iter == map_.end())
863 return NULL;
864 function_instance *s = iter->second;
865 for (unsigned i = stack.length() - 1; i > 0; i--)
866 {
867 s = s->get_function_instance_by_decl (
868 stack[i].second, stack[i - 1].first);
869 if (s == NULL)
870 return NULL;
871 }
872 return s;
873 }
874
875 /* Module profile is only used by LIPO. Here we simply ignore it. */
876
877 static void
878 fake_read_autofdo_module_profile ()
879 {
880 /* Read in the module info. */
881 gcov_read_unsigned ();
882
883 /* Skip the length of the section. */
884 gcov_read_unsigned ();
885
886 /* Read in the file name table. */
887 unsigned total_module_num = gcov_read_unsigned ();
888 gcc_assert (total_module_num == 0);
889 }
890
891 /* Read data from profile data file. */
892
893 static void
894 read_profile (void)
895 {
896 if (gcov_open (auto_profile_file, 1) == 0)
897 error ("Cannot open profile file %s.", auto_profile_file);
898
899 if (gcov_read_unsigned () != GCOV_DATA_MAGIC)
900 error ("AutoFDO profile magic number does not mathch.");
901
902 /* Skip the version number. */
903 unsigned version = gcov_read_unsigned ();
904 if (version != AUTO_PROFILE_VERSION)
905 error ("AutoFDO profile version %u does match %u.",
906 version, AUTO_PROFILE_VERSION);
907
908 /* Skip the empty integer. */
909 gcov_read_unsigned ();
910
911 /* string_table. */
912 afdo_string_table = new string_table ();
913 if (!afdo_string_table->read())
914 error ("Cannot read string table from %s.", auto_profile_file);
915
916 /* autofdo_source_profile. */
917 afdo_source_profile = autofdo_source_profile::create ();
918 if (afdo_source_profile == NULL)
919 error ("Cannot read function profile from %s.", auto_profile_file);
920
921 /* autofdo_module_profile. */
922 fake_read_autofdo_module_profile ();
923
924 /* Read in the working set. */
925 if (gcov_read_unsigned () != GCOV_TAG_AFDO_WORKING_SET)
926 error ("Cannot read working set from %s.", auto_profile_file);
927
928 /* Skip the length of the section. */
929 gcov_read_unsigned ();
930 gcov_working_set_t set[128];
931 for (unsigned i = 0; i < 128; i++)
932 {
933 set[i].num_counters = gcov_read_unsigned ();
934 set[i].min_counter = gcov_read_counter ();
935 }
936 add_working_set (set);
937 }
938
939 /* From AutoFDO profiles, find values inside STMT for that we want to measure
940 histograms for indirect-call optimization.
941
942 This function is actually served for 2 purposes:
943     * before annotation, we need to mark histogram, promote and inline
944     * after annotation, we just need to mark, and let follow-up logic to
945       decide if it needs to promote and inline. */
946
947 static void
948 afdo_indirect_call (gimple_stmt_iterator *gsi, const icall_target_map &map,
949 bool transform)
950 {
951 gimple stmt = gsi_stmt (*gsi);
952 tree callee;
953
954 if (map.size () == 0 || gimple_code (stmt) != GIMPLE_CALL
955 || gimple_call_fndecl (stmt) != NULL_TREE)
956 return;
957
958 callee = gimple_call_fn (stmt);
959
960 histogram_value hist = gimple_alloc_histogram_value (
961 cfun, HIST_TYPE_INDIR_CALL, stmt, callee);
962 hist->n_counters = 3;
963 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
964 gimple_add_histogram_value (cfun, stmt, hist);
965
966 gcov_type total = 0;
967 icall_target_map::const_iterator max_iter = map.end ();
968
969 for (icall_target_map::const_iterator iter = map.begin ();
970 iter != map.end (); ++iter)
971 {
972 total += iter->second;
973 if (max_iter == map.end () || max_iter->second < iter->second)
974 max_iter = iter;
975 }
976
977 hist->hvalue.counters[0]
978 = (unsigned long long)afdo_string_table->get_name (max_iter->first);
979 hist->hvalue.counters[1] = max_iter->second;
980 hist->hvalue.counters[2] = total;
981
982 if (!transform)
983 return;
984
985 struct cgraph_edge *indirect_edge
986 = cgraph_node::get (current_function_decl)->get_edge (stmt);
987 struct cgraph_node *direct_call = cgraph_node::get_for_asmname (
988 get_identifier ((const char *) hist->hvalue.counters[0]));
989
990 if (direct_call == NULL || !check_ic_target (stmt, direct_call))
991 return;
992 if (DECL_STRUCT_FUNCTION (direct_call->decl) == NULL)
993 return;
994 struct cgraph_edge *new_edge
995 = indirect_edge->make_speculative (direct_call, 0, 0);
996 new_edge->redirect_call_stmt_to_callee ();
997 gimple_remove_histogram_value (cfun, stmt, hist);
998 inline_call (new_edge, true, NULL, NULL, false);
999 }
1000
1001 /* From AutoFDO profiles, find values inside STMT for that we want to measure
1002 histograms and adds them to list VALUES. */
1003
1004 static void
1005 afdo_vpt (gimple_stmt_iterator *gsi, const icall_target_map &map,
1006 bool transform)
1007 {
1008 afdo_indirect_call (gsi, map, transform);
1009 }
1010
1011 typedef std::set<basic_block> bb_set;
1012 typedef std::set<edge> edge_set;
1013
1014 static bool
1015 is_bb_annotated (const basic_block bb, const bb_set &annotated)
1016 {
1017 return annotated.find (bb) != annotated.end ();
1018 }
1019
1020 static void
1021 set_bb_annotated (basic_block bb, bb_set *annotated)
1022 {
1023 annotated->insert (bb);
1024 }
1025
1026 static bool
1027 is_edge_annotated (const edge e, const edge_set &annotated)
1028 {
1029 return annotated.find (e) != annotated.end ();
1030 }
1031
1032 static void
1033 set_edge_annotated (edge e, edge_set *annotated)
1034 {
1035 annotated->insert (e);
1036 }
1037
1038 /* For a given BB, set its execution count. Attach value profile if a stmt
1039 is not in PROMOTED, because we only want to promot an indirect call once.
1040 Return TRUE if BB is annotated. */
1041
1042 static bool
1043 afdo_set_bb_count (basic_block bb, const stmt_set &promoted)
1044 {
1045 gimple_stmt_iterator gsi;
1046 edge e;
1047 edge_iterator ei;
1048 gcov_type max_count = 0;
1049 bool has_annotated = false;
1050
1051 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1052 {
1053 count_info info;
1054 gimple stmt = gsi_stmt (gsi);
1055 if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1056 continue;
1057 if (afdo_source_profile->get_count_info (stmt, &info))
1058 {
1059 if (info.count > max_count)
1060 max_count = info.count;
1061 has_annotated = true;
1062 if (info.targets.size () > 0
1063 && promoted.find (stmt) == promoted.end ())
1064 afdo_vpt (&gsi, info.targets, false);
1065 }
1066 }
1067
1068 if (!has_annotated)
1069 return false;
1070
1071 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1072 afdo_source_profile->mark_annotated (gimple_location (gsi_stmt (gsi)));
1073 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1074 {
1075 gimple phi = gsi_stmt (gsi);
1076 size_t i;
1077 for (i = 0; i < gimple_phi_num_args (phi); i++)
1078 afdo_source_profile->mark_annotated (gimple_phi_arg_location (phi, i));
1079 }
1080 FOR_EACH_EDGE (e, ei, bb->succs)
1081 afdo_source_profile->mark_annotated (e->goto_locus);
1082
1083 bb->count = max_count;
1084 return true;
1085 }
1086
1087 /* BB1 and BB2 are in an equivalent class iff:
1088 1. BB1 dominates BB2.
1089 2. BB2 post-dominates BB1.
1090 3. BB1 and BB2 are in the same loop nest.
1091 This function finds the equivalent class for each basic block, and
1092 stores a pointer to the first BB in its equivalent class. Meanwhile,
1093 set bb counts for the same equivalent class to be idenical. Update
1094 ANNOTATED_BB for the first BB in its equivalent class. */
1095
1096 static void
1097 afdo_find_equiv_class (bb_set *annotated_bb)
1098 {
1099 basic_block bb;
1100
1101 FOR_ALL_BB_FN (bb, cfun)
1102 bb->aux = NULL;
1103
1104 FOR_ALL_BB_FN (bb, cfun)
1105 {
1106 vec<basic_block> dom_bbs;
1107 basic_block bb1;
1108 int i;
1109
1110 if (bb->aux != NULL)
1111 continue;
1112 bb->aux = bb;
1113 dom_bbs = get_dominated_by (CDI_DOMINATORS, bb);
1114 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1115 if (bb1->aux == NULL && dominated_by_p (CDI_POST_DOMINATORS, bb, bb1)
1116 && bb1->loop_father == bb->loop_father)
1117 {
1118 bb1->aux = bb;
1119 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1120 {
1121 bb->count = MAX (bb->count, bb1->count);
1122 set_bb_annotated (bb, annotated_bb);
1123 }
1124 }
1125 dom_bbs = get_dominated_by (CDI_POST_DOMINATORS, bb);
1126 FOR_EACH_VEC_ELT (dom_bbs, i, bb1)
1127 if (bb1->aux == NULL && dominated_by_p (CDI_DOMINATORS, bb, bb1)
1128 && bb1->loop_father == bb->loop_father)
1129 {
1130 bb1->aux = bb;
1131 if (bb1->count > bb->count && is_bb_annotated (bb1, *annotated_bb))
1132 {
1133 bb->count = MAX (bb->count, bb1->count);
1134 set_bb_annotated (bb, annotated_bb);
1135 }
1136 }
1137 }
1138 }
1139
1140 /* If a basic block's count is known, and only one of its in/out edges' count
1141 is unknown, its count can be calculated. Meanwhile, if all of the in/out
1142 edges' counts are known, then the basic block's unknown count can also be
1143 calculated.
1144 IS_SUCC is true if out edges of a basic blocks are examined.
1145 Update ANNOTATED_BB and ANNOTATED_EDGE accordingly.
1146 Return TRUE if any basic block/edge count is changed. */
1147
1148 static bool
1149 afdo_propagate_edge (bool is_succ, bb_set *annotated_bb,
1150 edge_set *annotated_edge)
1151 {
1152 basic_block bb;
1153 bool changed = false;
1154
1155 FOR_EACH_BB_FN (bb, cfun)
1156 {
1157 edge e, unknown_edge = NULL;
1158 edge_iterator ei;
1159 int num_unknown_edge = 0;
1160 gcov_type total_known_count = 0;
1161
1162 FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
1163 if (!is_edge_annotated (e, *annotated_edge))
1164 num_unknown_edge++, unknown_edge = e;
1165 else
1166 total_known_count += e->count;
1167
1168 if (num_unknown_edge == 0)
1169 {
1170 if (total_known_count > bb->count)
1171 {
1172 bb->count = total_known_count;
1173 changed = true;
1174 }
1175 if (!is_bb_annotated (bb, *annotated_bb))
1176 {
1177 set_bb_annotated (bb, annotated_bb);
1178 changed = true;
1179 }
1180 }
1181 else if (num_unknown_edge == 1 && is_bb_annotated (bb, *annotated_bb))
1182 {
1183 if (bb->count >= total_known_count)
1184 unknown_edge->count = bb->count - total_known_count;
1185 else
1186 unknown_edge->count = 0;
1187 set_edge_annotated (unknown_edge, annotated_edge);
1188 changed = true;
1189 }
1190 }
1191 return changed;
1192 }
1193
1194 /* Special propagation for circuit expressions. Because GCC translates
1195 control flow into data flow for circuit expressions. E.g.
1196 BB1:
1197 if (a && b)
1198 BB2
1199 else
1200 BB3
1201
1202 will be translated into:
1203
1204 BB1:
1205 if (a)
1206 goto BB.t1
1207 else
1208 goto BB.t3
1209 BB.t1:
1210 if (b)
1211 goto BB.t2
1212 else
1213 goto BB.t3
1214 BB.t2:
1215 goto BB.t3
1216 BB.t3:
1217 tmp = PHI (0 (BB1), 0 (BB.t1), 1 (BB.t2)
1218 if (tmp)
1219 goto BB2
1220 else
1221 goto BB3
1222
1223 In this case, we need to propagate through PHI to determine the edge
1224 count of BB1->BB.t1, BB.t1->BB.t2.
1225 Update ANNOTATED_EDGE accordingly. */
1226
1227 static void
1228 afdo_propagate_circuit (const bb_set &annotated_bb, edge_set *annotated_edge)
1229 {
1230 basic_block bb;
1231 FOR_ALL_BB_FN (bb, cfun)
1232 {
1233 gimple phi_stmt;
1234 tree cmp_rhs, cmp_lhs;
1235 gimple cmp_stmt = last_stmt (bb);
1236 edge e;
1237 edge_iterator ei;
1238
1239 if (!cmp_stmt || gimple_code (cmp_stmt) != GIMPLE_COND)
1240 continue;
1241 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1242 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1243 if (!TREE_CONSTANT (cmp_rhs)
1244 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1245 continue;
1246 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1247 continue;
1248 if (!is_bb_annotated (bb, annotated_bb))
1249 continue;
1250 phi_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1251 while (phi_stmt && gimple_code (phi_stmt) == GIMPLE_ASSIGN
1252 && gimple_assign_single_p (phi_stmt)
1253 && TREE_CODE (gimple_assign_rhs1 (phi_stmt)) == SSA_NAME)
1254 phi_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (phi_stmt));
1255 if (!phi_stmt || gimple_code (phi_stmt) != GIMPLE_PHI)
1256 continue;
1257 FOR_EACH_EDGE (e, ei, bb->succs)
1258 {
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++)
1267 {
1268 tree val = gimple_phi_arg_def (phi_stmt, i);
1269 edge ep = gimple_phi_arg_edge (phi_stmt, i);
1270
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))
1279 {
1280 ep->probability = 0;
1281 ep->count = 0;
1282 set_edge_annotated (ep, annotated_edge);
1283 }
1284 }
1285 if (total == 1 && !is_edge_annotated (only_one, *annotated_edge))
1286 {
1287 only_one->probability = e->probability;
1288 only_one->count = e->count;
1289 set_edge_annotated (only_one, annotated_edge);
1290 }
1291 }
1292 }
1293 }
1294
1295 /* Propagate the basic block count and edge count on the control flow
1296 graph. We do the propagation iteratively until stablize. */
1297
1298 static void
1299 afdo_propagate (bb_set *annotated_bb, edge_set *annotated_edge)
1300 {
1301 basic_block bb;
1302 bool changed = true;
1303 int i = 0;
1304
1305 FOR_ALL_BB_FN (bb, cfun)
1306 {
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);
1310 }
1311
1312 while (changed && i++ < 10)
1313 {
1314 changed = false;
1315
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);
1321 }
1322 }
1323
1324 /* Propagate counts on control flow graph and calculate branch
1325 probabilities. */
1326
1327 static void
1328 afdo_calculate_branch_prob (bb_set *annotated_bb, edge_set *annotated_edge)
1329 {
1330 basic_block bb;
1331 bool has_sample = false;
1332
1333 FOR_EACH_BB_FN (bb, cfun)
1334 if (bb->count > 0)
1335 has_sample = true;
1336
1337 if (!has_sample)
1338 return;
1339
1340 calculate_dominance_info (CDI_POST_DOMINATORS);
1341 calculate_dominance_info (CDI_DOMINATORS);
1342 loop_optimizer_init (0);
1343
1344 afdo_find_equiv_class (annotated_bb);
1345 afdo_propagate (annotated_bb, annotated_edge);
1346
1347 FOR_EACH_BB_FN (bb, cfun)
1348 {
1349 edge e;
1350 edge_iterator ei;
1351 int num_unknown_succ = 0;
1352 gcov_type total_count = 0;
1353
1354 FOR_EACH_EDGE (e, ei, bb->succs)
1355 {
1356 if (!is_edge_annotated (e, *annotated_edge))
1357 num_unknown_succ++;
1358 else
1359 total_count += e->count;
1360 }
1361 if (num_unknown_succ == 0 && total_count > 0)
1362 {
1363 FOR_EACH_EDGE (e, ei, bb->succs)
1364 e->probability = (double)e->count * REG_BR_PROB_BASE / total_count;
1365 }
1366 }
1367 FOR_ALL_BB_FN (bb, cfun)
1368 {
1369 edge e;
1370 edge_iterator ei;
1371
1372 FOR_EACH_EDGE (e, ei, bb->succs)
1373 e->count = (double)bb->count * e->probability / REG_BR_PROB_BASE;
1374 bb->aux = NULL;
1375 }
1376
1377 loop_optimizer_finalize ();
1378 free_dominance_info (CDI_DOMINATORS);
1379 free_dominance_info (CDI_POST_DOMINATORS);
1380 }
1381
1382 /* Perform value profile transformation using AutoFDO profile. Add the
1383 promoted stmts to PROMOTED_STMTS. Return TRUE if there is any
1384 indirect call promoted. */
1385
1386 static bool
1387 afdo_vpt_for_early_inline (stmt_set *promoted_stmts)
1388 {
1389 basic_block bb;
1390 if (afdo_source_profile->get_function_instance_by_decl (
1391 current_function_decl) == NULL)
1392 return false;
1393
1394 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1395
1396 bool has_vpt = false;
1397 FOR_EACH_BB_FN (bb, cfun)
1398 {
1399 if (!has_indirect_call (bb))
1400 continue;
1401 gimple_stmt_iterator gsi;
1402
1403 gcov_type bb_count = 0;
1404 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1405 {
1406 count_info info;
1407 gimple stmt = gsi_stmt (gsi);
1408 if (afdo_source_profile->get_count_info (stmt, &info))
1409 bb_count = MAX (bb_count, info.count);
1410 }
1411
1412 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1413 {
1414 gimple stmt = gsi_stmt (gsi);
1415 /* IC_promotion and early_inline_2 is done in multiple iterations.
1416 No need to promoted the stmt if its in promoted_stmts (means
1417 it is already been promoted in the previous iterations). */
1418 if (gimple_code (stmt) != GIMPLE_CALL || gimple_call_fn (stmt) == NULL
1419 || TREE_CODE (gimple_call_fn (stmt)) == FUNCTION_DECL
1420 || promoted_stmts->find (stmt) != promoted_stmts->end ())
1421 continue;
1422
1423 count_info info;
1424 afdo_source_profile->get_count_info (stmt, &info);
1425 info.count = bb_count;
1426 if (afdo_source_profile->update_inlined_ind_target (stmt, &info))
1427 {
1428 /* Promote the indirect call and update the promoted_stmts. */
1429 promoted_stmts->insert (stmt);
1430 afdo_vpt (&gsi, info.targets, true);
1431 has_vpt = true;
1432 }
1433 }
1434 }
1435 if (has_vpt)
1436 {
1437 optimize_inline_calls (current_function_decl);
1438 return true;
1439 }
1440 else
1441 return false;
1442 }
1443
1444 /* Annotate auto profile to the control flow graph. Do not annotate value
1445 profile for stmts in PROMOTED_STMTS. */
1446
1447 static void
1448 afdo_annotate_cfg (const stmt_set &promoted_stmts)
1449 {
1450 basic_block bb;
1451 bb_set annotated_bb;
1452 edge_set annotated_edge;
1453 const function_instance *s
1454 = afdo_source_profile->get_function_instance_by_decl (
1455 current_function_decl);
1456
1457 if (s == NULL)
1458 return;
1459 cgraph_node::get (current_function_decl)->count = s->head_count ();
1460 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = s->head_count ();
1461 gcov_type max_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1462
1463 FOR_EACH_BB_FN (bb, cfun)
1464 {
1465 edge e;
1466 edge_iterator ei;
1467
1468 bb->count = 0;
1469 FOR_EACH_EDGE (e, ei, bb->succs)
1470 e->count = 0;
1471
1472 if (afdo_set_bb_count (bb, promoted_stmts))
1473 set_bb_annotated (bb, &annotated_bb);
1474 if (bb->count > max_count)
1475 max_count = bb->count;
1476 }
1477 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1478 > ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count)
1479 {
1480 ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count
1481 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1482 set_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, &annotated_bb);
1483 }
1484 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
1485 > EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count)
1486 {
1487 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count
1488 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1489 set_bb_annotated (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, &annotated_bb);
1490 }
1491 afdo_source_profile->mark_annotated (
1492 DECL_SOURCE_LOCATION (current_function_decl));
1493 afdo_source_profile->mark_annotated (cfun->function_start_locus);
1494 afdo_source_profile->mark_annotated (cfun->function_end_locus);
1495 if (max_count > 0)
1496 {
1497 afdo_calculate_branch_prob (&annotated_bb, &annotated_edge);
1498 counts_to_freqs ();
1499 profile_status_for_fn (cfun) = PROFILE_READ;
1500 }
1501 if (flag_value_profile_transformations)
1502 gimple_value_profile_transformations ();
1503 }
1504
1505 /* Wrapper function to invoke early inliner. */
1506
1507 static void
1508 early_inline ()
1509 {
1510 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
1511 unsigned todo = early_inliner (cfun);
1512 if (todo & TODO_update_ssa_any)
1513 update_ssa (TODO_update_ssa);
1514 }
1515
1516 /* Use AutoFDO profile to annoate the control flow graph.
1517 Return the todo flag. */
1518
1519 static unsigned int
1520 auto_profile (void)
1521 {
1522 struct cgraph_node *node;
1523
1524 if (symtab->state == FINISHED)
1525 return 0;
1526
1527 init_node_map (true);
1528 profile_info = autofdo::afdo_profile_info;
1529
1530 FOR_EACH_FUNCTION (node)
1531 {
1532 if (!gimple_has_body_p (node->decl))
1533 continue;
1534
1535 /* Don't profile functions produced for builtin stuff. */
1536 if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
1537 continue;
1538
1539 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1540
1541 /* First do indirect call promotion and early inline to make the
1542 IR match the profiled binary before actual annotation.
1543
1544 This is needed because an indirect call might have been promoted
1545 and inlined in the profiled binary. If we do not promote and
1546 inline these indirect calls before annotation, the profile for
1547 these promoted functions will be lost.
1548
1549 e.g. foo() --indirect_call--> bar()
1550 In profiled binary, the callsite is promoted and inlined, making
1551 the profile look like:
1552
1553 foo: {
1554 loc_foo_1: count_1
1555 bar@loc_foo_2: {
1556 loc_bar_1: count_2
1557 loc_bar_2: count_3
1558 }
1559 }
1560
1561 Before AutoFDO pass, loc_foo_2 is not promoted thus not inlined.
1562 If we perform annotation on it, the profile inside bar@loc_foo2
1563 will be wasted.
1564
1565 To avoid this, we promote loc_foo_2 and inline the promoted bar
1566 function before annotation, so the profile inside bar@loc_foo2
1567 will be useful. */
1568 autofdo::stmt_set promoted_stmts;
1569 for (int i = 0; i < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS); i++)
1570 {
1571 if (!flag_value_profile_transformations
1572 || !autofdo::afdo_vpt_for_early_inline (&promoted_stmts))
1573 break;
1574 early_inline ();
1575 }
1576
1577 early_inline ();
1578 autofdo::afdo_annotate_cfg (promoted_stmts);
1579 compute_function_frequency ();
1580 update_ssa (TODO_update_ssa);
1581
1582 /* Local pure-const may imply need to fixup the cfg. */
1583 if (execute_fixup_cfg () & TODO_cleanup_cfg)
1584 cleanup_tree_cfg ();
1585
1586 free_dominance_info (CDI_DOMINATORS);
1587 free_dominance_info (CDI_POST_DOMINATORS);
1588 cgraph_edge::rebuild_edges ();
1589 pop_cfun ();
1590 }
1591
1592 return TODO_rebuild_cgraph_edges;
1593 }
1594 } /* namespace autofdo. */
1595
1596 /* Read the profile from the profile data file. */
1597
1598 void
1599 read_autofdo_file (void)
1600 {
1601 if (auto_profile_file == NULL)
1602 auto_profile_file = DEFAULT_AUTO_PROFILE_FILE;
1603
1604 autofdo::afdo_profile_info = (struct gcov_ctr_summary *)xcalloc (
1605 1, sizeof (struct gcov_ctr_summary));
1606 autofdo::afdo_profile_info->runs = 1;
1607 autofdo::afdo_profile_info->sum_max = 0;
1608 autofdo::afdo_profile_info->sum_all = 0;
1609
1610 /* Read the profile from the profile file. */
1611 autofdo::read_profile ();
1612 }
1613
1614 /* Free the resources. */
1615
1616 void
1617 end_auto_profile (void)
1618 {
1619 delete autofdo::afdo_source_profile;
1620 delete autofdo::afdo_string_table;
1621 profile_info = NULL;
1622 }
1623
1624 /* Returns TRUE if EDGE is hot enough to be inlined early. */
1625
1626 bool
1627 afdo_callsite_hot_enough_for_early_inline (struct cgraph_edge *edge)
1628 {
1629 gcov_type count
1630 = autofdo::afdo_source_profile->get_callsite_total_count (edge);
1631 if (count > 0)
1632 {
1633 bool is_hot;
1634 const struct gcov_ctr_summary *saved_profile_info = profile_info;
1635 /* At earling inline stage, profile_info is not set yet. We need to
1636 temporarily set it to afdo_profile_info to calculate hotness. */
1637 profile_info = autofdo::afdo_profile_info;
1638 is_hot = maybe_hot_count_p (NULL, count);
1639 profile_info = saved_profile_info;
1640 return is_hot;
1641 }
1642 else
1643 return false;
1644 }
1645
1646 namespace
1647 {
1648
1649 const pass_data pass_data_ipa_auto_profile = {
1650 SIMPLE_IPA_PASS, "afdo", /* name */
1651 OPTGROUP_NONE, /* optinfo_flags */
1652 TV_IPA_AUTOFDO, /* tv_id */
1653 0, /* properties_required */
1654 0, /* properties_provided */
1655 0, /* properties_destroyed */
1656 0, /* todo_flags_start */
1657 0, /* todo_flags_finish */
1658 };
1659
1660 class pass_ipa_auto_profile : public simple_ipa_opt_pass
1661 {
1662 public:
1663 pass_ipa_auto_profile (gcc::context *ctxt)
1664 : simple_ipa_opt_pass (pass_data_ipa_auto_profile, ctxt)
1665 {
1666 }
1667
1668 /* opt_pass methods: */
1669 virtual bool
1670 gate (function *)
1671 {
1672 return flag_auto_profile;
1673 }
1674 virtual unsigned int
1675 execute (function *)
1676 {
1677 return autofdo::auto_profile ();
1678 }
1679 }; // class pass_ipa_auto_profile
1680
1681 } // anon namespace
1682
1683 simple_ipa_opt_pass *
1684 make_pass_ipa_auto_profile (gcc::context *ctxt)
1685 {
1686 return new pass_ipa_auto_profile (ctxt);
1687 }