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