Branch data Line data Source code
1 : : /* Read and annotate call graph profile from the auto profile data file.
2 : : Copyright (C) 2014-2025 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 "config.h"
22 : : #define INCLUDE_MAP
23 : : #define INCLUDE_SET
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "backend.h"
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"
36 : : #include "profile.h"
37 : : #include "langhooks.h"
38 : : #include "context.h"
39 : : #include "pass_manager.h"
40 : : #include "cfgloop.h"
41 : : #include "tree-cfg.h"
42 : : #include "tree-cfgcleanup.h"
43 : : #include "tree-into-ssa.h"
44 : : #include "gimple-iterator.h"
45 : : #include "value-prof.h"
46 : : #include "symbol-summary.h"
47 : : #include "sreal.h"
48 : : #include "ipa-cp.h"
49 : : #include "ipa-prop.h"
50 : : #include "ipa-fnsummary.h"
51 : : #include "ipa-inline.h"
52 : : #include "tree-inline.h"
53 : : #include "auto-profile.h"
54 : : #include "tree-pretty-print.h"
55 : : #include "gimple-pretty-print.h"
56 : :
57 : : /* The following routines implements AutoFDO optimization.
58 : :
59 : : This optimization uses sampling profiles to annotate basic block counts
60 : : and uses heuristics to estimate branch probabilities.
61 : :
62 : : There are three phases in AutoFDO:
63 : :
64 : : Phase 1: At startup.
65 : : Read profile from the profile data file.
66 : : The following info is read from the profile datafile:
67 : : * string_table: a map between function name and its index.
68 : : * autofdo_source_profile: a map from function_instance name to
69 : : function_instance. This is represented as a forest of
70 : : function_instances.
71 : : * WorkingSet: a histogram of how many instructions are covered for a
72 : : given percentage of total cycles. This is describing the binary
73 : : level information (not source level). This info is used to help
74 : : decide if we want aggressive optimizations that could increase
75 : : code footprint (e.g. loop unroll etc.)
76 : : A function instance is an instance of function that could either be a
77 : : standalone symbol, or a clone of a function that is inlined into another
78 : : function.
79 : :
80 : : Phase 2: In afdo_offline pass.
81 : : Remove function instances from other translation units
82 : : and offline all cross-translation unit inlining done during train
83 : : run compilation. This is necessary to not lose profiles with
84 : : LTO train run.
85 : :
86 : : Phase 3: During early optimization.
87 : : AFDO inline + value profile transformation.
88 : : This happens during early optimization.
89 : : During early inlning AFDO inliner is executed which
90 : : uses autofdo_source_profile to find if a callsite is:
91 : : * inlined in the profiled binary.
92 : : * callee body is hot in the profiling run.
93 : : If both condition satisfies, early inline will inline the callsite
94 : : regardless of the code growth.
95 : :
96 : : Performing this early has benefit of doing early optimizations
97 : : before read IPA passe and getting more "context sensitivity" of
98 : : the profile read. Profile of inlined functions may differ
99 : : significantly form one inline instance to another and from the
100 : : offline version.
101 : :
102 : : This is controlled by -fauto-profile-inlinig and is independent
103 : : of -fearly-inlining.
104 : :
105 : : Phase 4: In AFDO pass.
106 : : Offline all functions that has been inlined in the
107 : : train run but were not inlined in early inlining nor AFDO
108 : : inline.
109 : :
110 : : Phase 5: In AFDO pass.
111 : : Annotate control flow graph.
112 : : * Annotate basic block count
113 : : * Estimate branch probability
114 : : * Use earlier static profile to fill in the gaps
115 : : if AFDO profile is ambigous
116 : :
117 : : After the above 5 phases, all profile is readily annotated on the GCC IR.
118 : : AutoFDO tries to reuse all FDO infrastructure as much as possible to make
119 : : use of the profile. E.g. it uses existing mechanism to calculate the basic
120 : : block/edge frequency, as well as the cgraph node/edge count.
121 : : */
122 : :
123 : : #define DEFAULT_AUTO_PROFILE_FILE "fbdata.afdo"
124 : : #define AUTO_PROFILE_VERSION 2
125 : :
126 : : /* profile counts determined by AFDO smaller than afdo_hot_bb_threshold are
127 : : considered cols. */
128 : : gcov_type afdo_hot_bb_threshod = -1;
129 : :
130 : : /* Return ture if COUNT is possiby hot. */
131 : : bool
132 : 0 : maybe_hot_afdo_count_p (profile_count count)
133 : : {
134 : 0 : gcc_checking_assert (count.ipa ().initialized_p ());
135 : 0 : return count.ipa ().to_gcov_type () >= afdo_hot_bb_threshod;
136 : : }
137 : :
138 : : namespace autofdo
139 : : {
140 : :
141 : : /* Intermediate edge info used when propagating AutoFDO profile information.
142 : : We can't edge->count() directly since it's computed from edge's probability
143 : : while probability is yet not decided during propagation. */
144 : : #define AFDO_EINFO(e) ((class edge_info *) e->aux)
145 : : class edge_info
146 : : {
147 : : public:
148 : 0 : edge_info () : count_ (profile_count::zero ().afdo ()), annotated_ (false) {}
149 : 0 : bool is_annotated () const { return annotated_; }
150 : 0 : void set_annotated () { annotated_ = true; }
151 : 0 : profile_count get_count () const { return count_; }
152 : 0 : void set_count (profile_count count) { count_ = count; }
153 : : private:
154 : : profile_count count_;
155 : : bool annotated_;
156 : : };
157 : :
158 : : /* Represent a source location: (function_decl, lineno). */
159 : : struct decl_lineno
160 : : {
161 : : tree decl;
162 : : /* Relative locations stored in auto-profile. */
163 : : unsigned int afdo_loc;
164 : : /* Actual location afdo_loc was computed from used to output diagnostics. */
165 : : location_t location;
166 : : };
167 : :
168 : : /* Represent an inline stack. vector[0] is the leaf node. */
169 : : typedef auto_vec<decl_lineno, 20> inline_stack;
170 : :
171 : : /* String array that stores function names. */
172 : : typedef auto_vec<char *> string_vector;
173 : :
174 : : /* Map from function name's index in string_table to target's
175 : : execution count. */
176 : : typedef std::map<unsigned, gcov_type> icall_target_map;
177 : :
178 : : /* Set of gimple stmts. Used to track if the stmt has already been promoted
179 : : to direct call. */
180 : : typedef std::set<gimple *> stmt_set;
181 : :
182 : : /* Set and map used to translate name indexes. */
183 : : typedef hash_set<int_hash <int, -1, -2>> name_index_set;
184 : : typedef hash_map<int_hash <int, -1, -2>, int> name_index_map;
185 : :
186 : : /* Represent count info of an inline stack. */
187 : 0 : class count_info
188 : : {
189 : : public:
190 : : /* Sampled count of the inline stack. */
191 : : gcov_type count;
192 : :
193 : : /* Map from indirect call target to its sample count. */
194 : : icall_target_map targets;
195 : :
196 : : /* Whether this inline stack is already used in annotation.
197 : :
198 : : Each inline stack should only be used to annotate IR once.
199 : : This will be enforced when instruction-level discriminator
200 : : is supported. */
201 : : };
202 : :
203 : : /* operator< for "const char *". */
204 : : struct string_compare
205 : : {
206 : 0 : bool operator()(const char *a, const char *b) const
207 : : {
208 : 0 : return strcmp (a, b) < 0;
209 : : }
210 : : };
211 : :
212 : : /* Store a string array, indexed by string position in the array. */
213 : : class string_table
214 : : {
215 : : public:
216 : 0 : string_table ()
217 : 0 : {}
218 : :
219 : : ~string_table ();
220 : :
221 : : /* For a given string, returns its index. */
222 : : int get_index (const char *name) const;
223 : :
224 : : /* For a given decl, returns the index of the decl name. */
225 : : int get_index_by_decl (tree decl) const;
226 : :
227 : : /* For a given index, returns the string. */
228 : : const char *get_name (int index) const;
229 : :
230 : : /* Read profile, return TRUE on success. */
231 : : bool read ();
232 : :
233 : : /* Return number of entries. */
234 : 0 : size_t num_entries ()
235 : : {
236 : 0 : return vector_.length ();
237 : : }
238 : :
239 : : /* Add new name and return its index. */
240 : : int add_name (char *);
241 : :
242 : : private:
243 : : typedef std::map<const char *, unsigned, string_compare> string_index_map;
244 : : string_vector vector_;
245 : : string_index_map map_;
246 : : };
247 : :
248 : : /* Profile of a function instance:
249 : : 1. total_count of the function.
250 : : 2. head_count (entry basic block count) of the function (only valid when
251 : : function is a top-level function_instance, i.e. it is the original copy
252 : : instead of the inlined copy).
253 : : 3. map from source location (decl_lineno) to profile (count_info).
254 : : 4. map from callsite to callee function_instance. */
255 : : class function_instance
256 : : {
257 : : public:
258 : : typedef auto_vec<function_instance *> function_instance_stack;
259 : :
260 : : /* Read the profile and return a function_instance with head count as
261 : : HEAD_COUNT. Recursively read callsites to create nested function_instances
262 : : too. STACK is used to track the recursive creation process. */
263 : : static function_instance *
264 : : read_function_instance (function_instance_stack *stack,
265 : : gcov_type head_count);
266 : :
267 : : /* Recursively deallocate all callsites (nested function_instances). */
268 : : ~function_instance ();
269 : :
270 : : /* Accessors. */
271 : : int
272 : 0 : name () const
273 : : {
274 : 0 : return name_;
275 : : }
276 : : int
277 : 0 : set_name (int index)
278 : : {
279 : 0 : return name_ = index;
280 : : }
281 : : gcov_type
282 : 0 : total_count () const
283 : : {
284 : 0 : return total_count_;
285 : : }
286 : :
287 : : /* Return head count or -1 if unknown. */
288 : : gcov_type
289 : 0 : head_count () const
290 : : {
291 : 0 : return head_count_;
292 : : }
293 : :
294 : : /* Traverse callsites of the current function_instance to find one at the
295 : : location of LINENO and callee name represented in DECL.
296 : : LOCATION should match LINENO and is used to output diagnostics. */
297 : : function_instance *get_function_instance_by_decl (unsigned lineno,
298 : : tree decl,
299 : : location_t location) const;
300 : :
301 : : /* Merge profile of clones. Note that cloning hasnt been performed when
302 : : we annotate the CFG (at this stage). */
303 : : void merge (function_instance *other,
304 : : vec <function_instance *> &new_functions);
305 : :
306 : : /* Look for inline instancs that was not realized and
307 : : remove them while possibly merging them to offline variants. */
308 : : void offline_if_not_realized (vec <function_instance *> &new_functions);
309 : :
310 : : /* Match function instance with gimple body. */
311 : : bool match (cgraph_node *node, vec <function_instance *> &new_functions,
312 : : name_index_map &to_symbol_name);
313 : :
314 : : /* Offline all inlined functions with name in SEEN.
315 : : If new toplevel functions are created, add them to NEW_FUNCTIONS. */
316 : : void offline_if_in_set (name_index_set &seen,
317 : : vec <function_instance *> &new_functions);
318 : :
319 : : /* Walk inlined functions and if their name is not in SEEN
320 : : remove it. */
321 : :
322 : : void remove_external_functions (name_index_set &seen,
323 : : name_index_map &to_symbol_name,
324 : : vec <function_instance *> &new_functions);
325 : :
326 : : /* Store the profile info for LOC in INFO. Return TRUE if profile info
327 : : is found. */
328 : : bool get_count_info (location_t loc, count_info *info) const;
329 : :
330 : : /* Read the inlined indirect call target profile for STMT in FN and store it
331 : : in MAP, return the total count for all inlined indirect calls. */
332 : : gcov_type find_icall_target_map (tree fn, gcall *stmt,
333 : : icall_target_map *map) const;
334 : :
335 : : /* Remove inlined indirect call target profile for STMT in FN. */
336 : : void remove_icall_target (tree fn, gcall *stmt);
337 : :
338 : : /* Mark LOC as annotated. */
339 : : void mark_annotated (location_t loc);
340 : :
341 : : void dump (FILE *f, int indent = 0, bool nested = false) const;
342 : :
343 : : void dump_inline_stack (FILE *f) const;
344 : :
345 : : DEBUG_FUNCTION void debug () const;
346 : :
347 : : /* Mark function as removed from indir target list. */
348 : : void
349 : 0 : remove_icall_target ()
350 : : {
351 : 0 : removed_icall_target_ = true;
352 : : }
353 : :
354 : : /* Reutrn true if function is removed from indir target list. */
355 : : bool
356 : 0 : removed_icall_target ()
357 : : {
358 : 0 : return removed_icall_target_;
359 : : }
360 : :
361 : : /* Set inlined_to pointer. */
362 : : void
363 : 0 : set_inlined_to (function_instance *inlined_to)
364 : : {
365 : 0 : gcc_checking_assert (inlined_to != this);
366 : 0 : inlined_to_ = inlined_to;
367 : 0 : }
368 : :
369 : : /* Return pointer to the function instance this function is inlined
370 : : to or NULL if it is outer instance. */
371 : : function_instance *
372 : 0 : inlined_to () const
373 : : {
374 : 0 : return inlined_to_;
375 : : }
376 : :
377 : : /* Mark function as realized. */
378 : : void
379 : 0 : set_realized ()
380 : : {
381 : 0 : realized_ = true;
382 : 0 : }
383 : :
384 : : /* Return true if function is realized. */
385 : : bool
386 : 0 : realized_p ()
387 : : {
388 : 0 : return realized_;
389 : : }
390 : :
391 : : /* Mark function as in_worklist. */
392 : : void
393 : 0 : set_in_worklist ()
394 : : {
395 : 0 : gcc_checking_assert (!inlined_to_ && !in_worklist_p ());
396 : 0 : in_worklist_ = true;
397 : 0 : }
398 : :
399 : : void
400 : 0 : clear_in_worklist ()
401 : : {
402 : 0 : gcc_checking_assert (!inlined_to_ && in_worklist_p ());
403 : 0 : in_worklist_ = false;
404 : 0 : }
405 : :
406 : :
407 : : /* Return true if function is in_worklist. */
408 : : bool
409 : 0 : in_worklist_p ()
410 : : {
411 : 0 : return in_worklist_;
412 : : }
413 : :
414 : : /* Return corresponding cgraph node. */
415 : : cgraph_node *get_cgraph_node ();
416 : :
417 : : void
418 : 0 : set_location (location_t l)
419 : : {
420 : 0 : gcc_checking_assert (location_ == UNKNOWN_LOCATION);
421 : 0 : location_= l;
422 : 0 : }
423 : :
424 : : location_t
425 : 0 : get_location ()
426 : : {
427 : 0 : return location_;
428 : : }
429 : :
430 : : void
431 : 0 : set_call_location (location_t l)
432 : : {
433 : 0 : gcc_checking_assert (call_location_ == UNKNOWN_LOCATION);
434 : 0 : call_location_= l;
435 : 0 : }
436 : :
437 : : location_t
438 : 0 : get_call_location ()
439 : : {
440 : 0 : return call_location_;
441 : : }
442 : :
443 : : /* Lookup count and warn about duplicates. */
444 : : count_info *lookup_count (location_t loc, inline_stack &stack,
445 : : cgraph_node *node);
446 : :
447 : : private:
448 : : /* Callsite, represented as (decl_lineno, callee_function_name_index). */
449 : : typedef std::pair<unsigned, unsigned> callsite;
450 : :
451 : : /* Map from callsite to callee function_instance. */
452 : : typedef std::map<callsite, function_instance *> callsite_map;
453 : :
454 : 0 : function_instance (unsigned name, gcov_type head_count)
455 : 0 : : name_ (name), total_count_ (0), head_count_ (head_count),
456 : 0 : removed_icall_target_ (false), realized_ (false),
457 : 0 : in_worklist_ (false), inlined_to_ (NULL),
458 : 0 : location_ (UNKNOWN_LOCATION), call_location_ (UNKNOWN_LOCATION)
459 : : {
460 : : }
461 : :
462 : : /* Map from source location (decl_lineno) to profile (count_info). */
463 : : typedef std::map<unsigned, count_info> position_count_map;
464 : :
465 : : /* function_instance name index in the string_table. */
466 : : unsigned name_;
467 : :
468 : : /* Total sample count. */
469 : : gcov_type total_count_;
470 : :
471 : : /* Entry BB's sample count. */
472 : : gcov_type head_count_;
473 : :
474 : : /* Map from callsite location to callee function_instance. */
475 : : callsite_map callsites;
476 : :
477 : : /* Map from source location to count_info. */
478 : : position_count_map pos_counts;
479 : :
480 : : /* True if function was removed from indir target list. */
481 : : bool removed_icall_target_;
482 : :
483 : : /* True if function exists in IL. I.e. for toplevel instance we
484 : : have corresponding symbol and for inline instance we inlined
485 : : to it. */
486 : : bool realized_;
487 : :
488 : : /* Ture if function is in worklist for merging/offlining. */
489 : : bool in_worklist_;
490 : :
491 : : /* Pointer to outer function instance or NULL if this
492 : : is a toplevel one. */
493 : : function_instance *inlined_to_;
494 : :
495 : : /* Location of function and its call (in case it is inlined). */
496 : : location_t location_, call_location_;
497 : :
498 : : /* Turn inline instance to offline. */
499 : : static bool offline (function_instance *fn,
500 : : vec <function_instance *> &new_functions);
501 : : };
502 : :
503 : : /* Profile for all functions. */
504 : : class autofdo_source_profile
505 : : {
506 : : public:
507 : : static autofdo_source_profile *
508 : 0 : create ()
509 : : {
510 : 0 : autofdo_source_profile *map = new autofdo_source_profile ();
511 : :
512 : 0 : if (map->read ())
513 : : return map;
514 : 0 : delete map;
515 : 0 : return NULL;
516 : : }
517 : :
518 : : ~autofdo_source_profile ();
519 : :
520 : : /* For a given DECL, returns the top-level function_instance. */
521 : : function_instance *get_function_instance_by_decl (tree decl) const;
522 : :
523 : : /* For a given name index, returns the top-level function_instance. */
524 : : function_instance *get_function_instance_by_name_index (int) const;
525 : :
526 : : void add_function_instance (function_instance *);
527 : :
528 : : /* Find count_info for a given gimple STMT. If found, store the count_info
529 : : in INFO and return true; otherwise return false.
530 : : NODE can be used to specify particular inline clone. */
531 : : bool get_count_info (gimple *stmt, count_info *info,
532 : : cgraph_node *node = NULL) const;
533 : :
534 : : /* Find count_info for a given gimple location GIMPLE_LOC. If found,
535 : : store the count_info in INFO and return true; otherwise return false.
536 : : NODE can be used to specify particular inline clone. */
537 : : bool get_count_info (location_t gimple_loc, count_info *info,
538 : : cgraph_node *node = NULL) const;
539 : :
540 : : /* Find total count of the callee of EDGE. */
541 : : gcov_type get_callsite_total_count (struct cgraph_edge *edge) const;
542 : :
543 : : /* Update value profile INFO for STMT within NODE from the inlined indirect
544 : : callsite. Return true if INFO is updated. */
545 : : bool update_inlined_ind_target (gcall *stmt, count_info *info,
546 : : cgraph_node *node);
547 : :
548 : : void remove_icall_target (cgraph_edge *e);
549 : :
550 : : /* Offline all functions not defined in the current translation unit. */
551 : : void offline_external_functions ();
552 : :
553 : : void offline_unrealized_inlines ();
554 : : private:
555 : : /* Map from function_instance name index (in string_table) to
556 : : function_instance. */
557 : : typedef std::map<unsigned, function_instance *> name_function_instance_map;
558 : :
559 : 0 : autofdo_source_profile () {}
560 : :
561 : : /* Read AutoFDO profile and returns TRUE on success. */
562 : : bool read ();
563 : :
564 : : /* Return the function_instance in the profile that correspond to the
565 : : inline STACK. */
566 : : function_instance *
567 : : get_function_instance_by_inline_stack (const inline_stack &stack) const;
568 : :
569 : : name_function_instance_map map_;
570 : :
571 : : auto_vec <function_instance *> duplicate_functions_;
572 : : };
573 : :
574 : : /* Store the strings read from the profile data file. */
575 : : static string_table *afdo_string_table;
576 : :
577 : : /* Store the AutoFDO source profile. */
578 : : static autofdo_source_profile *afdo_source_profile;
579 : :
580 : : /* gcov_summary structure to store the profile_info. */
581 : : static gcov_summary *afdo_profile_info;
582 : :
583 : : /* Scaling factor for afdo data. Compared to normal profile
584 : : AFDO profile counts are much lower, depending on sampling
585 : : frequency. We scale data up to reudce effects of roundoff
586 : : errors. */
587 : :
588 : : static gcov_type afdo_count_scale = 1;
589 : :
590 : : /* Helper functions. */
591 : :
592 : :
593 : : /* Return the original name of NAME: strip the suffix that starts
594 : : with '.' for names that are generetad after auto-profile pass.
595 : : This is to match profiled names with the names in the IR at this stage.
596 : : Note that we only have to strip sufix and not in the middle.
597 : : Caller is responsible for freeing RET. */
598 : :
599 : : static char *
600 : 0 : get_original_name (const char *name, bool alloc = true)
601 : : {
602 : 0 : char *ret = alloc ? xstrdup (name) : const_cast<char *> (name);
603 : 0 : char *last_dot = strrchr (ret, '.');
604 : 0 : if (last_dot == NULL)
605 : : return ret;
606 : 0 : bool only_digits = true;
607 : : char *ptr = last_dot;
608 : 0 : while (*(++ptr) != 0)
609 : 0 : if (*ptr < '0' || *ptr > '9')
610 : : {
611 : : only_digits = false;
612 : : break;
613 : : }
614 : 0 : if (only_digits)
615 : 0 : *last_dot = 0;
616 : 0 : char *next_dot = strrchr (ret, '.');
617 : : /* if nested function such as foo.0, return foo.0 */
618 : 0 : if (next_dot == NULL)
619 : : {
620 : 0 : *last_dot = '.';
621 : 0 : return ret;
622 : : }
623 : : /* Suffixes of clones that compiler generates after auto-profile. */
624 : 0 : const char *suffixes[] = {"isra", "constprop", "lto_priv", "part", "cold"};
625 : 0 : for (unsigned i = 0; i < sizeof (suffixes); ++i)
626 : : {
627 : 0 : if (strncmp (next_dot + 1, suffixes[i], strlen (suffixes[i])) == 0)
628 : : {
629 : 0 : *next_dot = 0;
630 : 0 : return get_original_name (ret, false);
631 : : }
632 : : }
633 : : /* Otherwise, it is for clones such as .omp_fn.N that was done before
634 : : auto-profile and should be kept as it is. */
635 : : *last_dot = '.';
636 : : return ret;
637 : : }
638 : :
639 : : /* Return the combined location, which is a 32bit integer in which
640 : : higher 16 bits stores the line offset of LOC to the start lineno
641 : : of DECL, The lower 16 bits stores the discriminator. */
642 : :
643 : : static unsigned
644 : 0 : get_combined_location (location_t loc, tree decl)
645 : : {
646 : 0 : bool warned = false;
647 : : /* TODO: allow more bits for line and less bits for discriminator. */
648 : 0 : if ((LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) >= (1<<15)
649 : 0 : || (LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) <= -(1<<15))
650 : 0 : warned = warning_at (loc, OPT_Wauto_profile,
651 : : "auto-profile cannot encode offset %i "
652 : : "that exceeds 16 bytes",
653 : 0 : LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl));
654 : 0 : if (warned)
655 : 0 : inform (DECL_SOURCE_LOCATION (decl), "location offset is related to");
656 : 0 : if ((unsigned)get_discriminator_from_loc (loc) >= (1u << 16))
657 : 0 : warning_at (loc, OPT_Wauto_profile,
658 : : "auto-profile cannot encode discriminators "
659 : : "that exceeds 16 bytes");
660 : 0 : return ((unsigned)(LOCATION_LINE (loc) - DECL_SOURCE_LINE (decl)) << 16)
661 : 0 : | get_discriminator_from_loc (loc);
662 : : }
663 : :
664 : : /* Return the function decl of a given lexical BLOCK. */
665 : :
666 : : static tree
667 : 0 : get_function_decl_from_block (tree block)
668 : : {
669 : 0 : if (!inlined_function_outer_scope_p (block))
670 : : return NULL_TREE;
671 : :
672 : 0 : return BLOCK_ABSTRACT_ORIGIN (block);
673 : : }
674 : :
675 : : /* Dump LOC to F. */
676 : :
677 : : static void
678 : 0 : dump_afdo_loc (FILE *f, unsigned loc)
679 : : {
680 : 0 : if (loc & 65535)
681 : 0 : fprintf (f, "%i.%i", loc >> 16, loc & 65535);
682 : : else
683 : 0 : fprintf (f, "%i", loc >> 16);
684 : 0 : }
685 : :
686 : : /* Dump STACK to F. */
687 : :
688 : : static void
689 : 0 : dump_inline_stack (FILE *f, inline_stack *stack)
690 : : {
691 : 0 : bool first = true;
692 : 0 : for (decl_lineno &p : *stack)
693 : : {
694 : 0 : fprintf (f, "%s%s:",
695 : : first ? "" : "; ",
696 : 0 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p.decl)));
697 : 0 : dump_afdo_loc (f, p.afdo_loc);
698 : 0 : first = false;
699 : : }
700 : 0 : fprintf (f, "\n");
701 : 0 : }
702 : :
703 : : /* Store inline stack for STMT in STACK. */
704 : :
705 : : static void
706 : 0 : get_inline_stack (location_t locus, inline_stack *stack,
707 : : tree fn = current_function_decl)
708 : : {
709 : 0 : if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
710 : : return;
711 : :
712 : 0 : tree block = LOCATION_BLOCK (locus);
713 : 0 : if (block && TREE_CODE (block) == BLOCK)
714 : : {
715 : 0 : for (block = BLOCK_SUPERCONTEXT (block);
716 : 0 : block && (TREE_CODE (block) == BLOCK);
717 : 0 : block = BLOCK_SUPERCONTEXT (block))
718 : : {
719 : 0 : location_t tmp_locus = BLOCK_SOURCE_LOCATION (block);
720 : 0 : if (LOCATION_LOCUS (tmp_locus) == UNKNOWN_LOCATION)
721 : 0 : continue;
722 : :
723 : 0 : tree decl = get_function_decl_from_block (block);
724 : 0 : stack->safe_push (
725 : 0 : {decl, get_combined_location (locus, decl), locus});
726 : 0 : locus = tmp_locus;
727 : : }
728 : : }
729 : 0 : stack->safe_push ({fn, get_combined_location (locus, fn), locus});
730 : : }
731 : :
732 : : /* Same as get_inline_stack for a given node which may be
733 : : an inline clone. If NODE is NULL, assume current_function_decl. */
734 : : static void
735 : 0 : get_inline_stack_in_node (location_t locus, inline_stack *stack,
736 : : cgraph_node *node)
737 : : {
738 : 0 : if (!node)
739 : 0 : return get_inline_stack (locus, stack);
740 : 0 : do
741 : : {
742 : 0 : get_inline_stack (locus, stack, node->decl);
743 : : /* If caller is inlined, continue building stack. */
744 : 0 : if (!node->inlined_to)
745 : : node = NULL;
746 : : else
747 : : {
748 : 0 : locus = gimple_location (node->callers->call_stmt);
749 : 0 : node = node->callers->caller;
750 : : }
751 : : }
752 : 0 : while (node);
753 : : }
754 : :
755 : : /* Return combined location of LOCUS within BLOCK that is in
756 : : function FN.
757 : :
758 : : This is a 32bit integer in which higher 16 bits stores the line offset of
759 : : LOC to the start lineno of DECL, The lower 16 bits stores the
760 : : discriminator. */
761 : :
762 : : static unsigned
763 : 0 : get_relative_location_for_locus (tree fn, tree block, location_t locus)
764 : : {
765 : 0 : if (LOCATION_LOCUS (locus) == UNKNOWN_LOCATION)
766 : : return -1;
767 : :
768 : 0 : for (; block && (TREE_CODE (block) == BLOCK);
769 : 0 : block = BLOCK_SUPERCONTEXT (block))
770 : 0 : if (inlined_function_outer_scope_p (block))
771 : 0 : return get_combined_location (locus,
772 : 0 : get_function_decl_from_block (block));
773 : 0 : return get_combined_location (locus, fn);
774 : : }
775 : :
776 : : /* Return combined location of STMT in function FN. */
777 : :
778 : : static unsigned
779 : 0 : get_relative_location_for_stmt (tree fn, gimple *stmt)
780 : : {
781 : 0 : return get_relative_location_for_locus
782 : 0 : (fn, LOCATION_BLOCK (gimple_location (stmt)),
783 : 0 : gimple_location (stmt));
784 : : }
785 : :
786 : : /* Member functions for string_table. */
787 : :
788 : : /* Deconstructor. */
789 : :
790 : 0 : string_table::~string_table ()
791 : : {
792 : 0 : for (unsigned i = 0; i < vector_.length (); i++)
793 : 0 : free (vector_[i]);
794 : 0 : }
795 : :
796 : :
797 : : /* Return the index of a given function NAME. Return -1 if NAME is not
798 : : found in string table. */
799 : :
800 : : int
801 : 0 : string_table::get_index (const char *name) const
802 : : {
803 : 0 : if (name == NULL)
804 : : return -1;
805 : 0 : string_index_map::const_iterator iter = map_.find (name);
806 : 0 : if (iter == map_.end ())
807 : : return -1;
808 : :
809 : 0 : return iter->second;
810 : : }
811 : :
812 : : /* Return the index of a given function DECL. Return -1 if DECL is not
813 : : found in string table. */
814 : :
815 : : int
816 : 0 : string_table::get_index_by_decl (tree decl) const
817 : : {
818 : 0 : const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
819 : 0 : int ret = get_index (name);
820 : 0 : if (ret != -1)
821 : : return ret;
822 : 0 : if (DECL_FROM_INLINE (decl))
823 : 0 : return get_index_by_decl (DECL_ABSTRACT_ORIGIN (decl));
824 : :
825 : : return -1;
826 : : }
827 : :
828 : : /* Return the function name of a given INDEX. */
829 : :
830 : : const char *
831 : 0 : string_table::get_name (int index) const
832 : : {
833 : 0 : gcc_assert (index > 0 && index < (int)vector_.length ());
834 : 0 : return vector_[index];
835 : : }
836 : :
837 : : /* Add new name SRRING and return its index. */
838 : :
839 : : int
840 : 0 : string_table::add_name (char *string)
841 : : {
842 : 0 : vector_.safe_push (string);
843 : 0 : map_[vector_.last ()] = vector_.length () - 1;
844 : 0 : return vector_.length () - 1;
845 : : }
846 : :
847 : : /* Read the string table. Return TRUE if reading is successful. */
848 : :
849 : : bool
850 : 0 : string_table::read ()
851 : : {
852 : 0 : if (gcov_read_unsigned () != GCOV_TAG_AFDO_FILE_NAMES)
853 : : return false;
854 : : /* Skip the length of the section. */
855 : 0 : gcov_read_unsigned ();
856 : : /* Read in the file name table. */
857 : 0 : unsigned string_num = gcov_read_unsigned ();
858 : 0 : vector_.reserve (string_num);
859 : 0 : for (unsigned i = 0; i < string_num; i++)
860 : : {
861 : 0 : vector_.quick_push (xstrdup (gcov_read_string ()));
862 : 0 : map_[vector_.last ()] = i;
863 : : }
864 : : return true;
865 : : }
866 : :
867 : : /* Member functions for function_instance. */
868 : :
869 : 0 : function_instance::~function_instance ()
870 : : {
871 : 0 : gcc_assert (!in_worklist_p ());
872 : 0 : for (callsite_map::iterator iter = callsites.begin ();
873 : 0 : iter != callsites.end (); ++iter)
874 : 0 : delete iter->second;
875 : 0 : }
876 : :
877 : : /* Return corresponding cgraph node, NULL if unavailable. */
878 : : cgraph_node *
879 : 0 : function_instance::get_cgraph_node ()
880 : : {
881 : 0 : for (symtab_node *n = cgraph_node::get_for_asmname
882 : 0 : (get_identifier
883 : : (afdo_string_table->get_name (name ())));
884 : 0 : n; n = n->next_sharing_asm_name)
885 : 0 : if (cgraph_node *cn = dyn_cast <cgraph_node *> (n))
886 : 0 : if (cn->definition && cn->has_gimple_body_p ())
887 : : return cn;
888 : : return NULL;
889 : : }
890 : :
891 : : /* Traverse callsites of the current function_instance to find one at the
892 : : location of LINENO and callee name represented in DECL. */
893 : :
894 : : function_instance *
895 : 0 : function_instance::get_function_instance_by_decl (unsigned lineno,
896 : : tree decl,
897 : : location_t location) const
898 : : {
899 : 0 : int func_name_idx = afdo_string_table->get_index_by_decl (decl);
900 : 0 : if (func_name_idx != -1)
901 : : {
902 : 0 : callsite_map::const_iterator ret
903 : 0 : = callsites.find (std::make_pair (lineno, func_name_idx));
904 : 0 : if (ret != callsites.end ())
905 : 0 : return ret->second;
906 : : }
907 : 0 : if (DECL_FROM_INLINE (decl))
908 : : {
909 : 0 : function_instance
910 : 0 : *ret = get_function_instance_by_decl (lineno,
911 : 0 : DECL_ABSTRACT_ORIGIN (decl),
912 : : location);
913 : 0 : return ret;
914 : : }
915 : 0 : if (dump_enabled_p ())
916 : : {
917 : 0 : for (auto const &iter : callsites)
918 : 0 : if (iter.first.first == lineno)
919 : 0 : dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS,
920 : 0 : dump_user_location_t::from_location_t (location),
921 : : "auto-profile has mismatched function name %s"
922 : : " instaed of %s at loc %i:%i",
923 : 0 : afdo_string_table->get_name (iter.first.second),
924 : 0 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
925 : : lineno << 16,
926 : : lineno & 65535);
927 : : }
928 : :
929 : : return NULL;
930 : : }
931 : :
932 : : /* Merge profile of OTHER to THIS. Note that cloning hasnt been performed when
933 : : we annotate the CFG (at this stage). */
934 : :
935 : : void
936 : 0 : function_instance::merge (function_instance *other,
937 : : vec <function_instance *> &new_functions)
938 : : {
939 : : /* Do not merge to itself and only merge functions of same name. */
940 : 0 : gcc_checking_assert (other != this && other->name () == name ());
941 : 0 : total_count_ += other->total_count_;
942 : 0 : if (other->total_count () && total_count () && other->head_count () == -1)
943 : 0 : head_count_ = -1;
944 : 0 : else if (head_count_ != -1)
945 : 0 : head_count_ += other->head_count_;
946 : :
947 : : bool changed = true;
948 : :
949 : 0 : while (changed)
950 : : {
951 : 0 : changed = false;
952 : : /* If both function instances agree on particular inlined function,
953 : : merge profiles. Otherwise offline the instance. */
954 : 0 : for (callsite_map::const_iterator iter = other->callsites.begin ();
955 : 0 : iter != other->callsites.end ();)
956 : 0 : if (callsites.count (iter->first) == 0)
957 : : {
958 : 0 : function_instance *f = iter->second;
959 : 0 : if (dump_file)
960 : : {
961 : 0 : fprintf (dump_file, " Mismatch in inlined functions;"
962 : : " offlining in merge source:");
963 : 0 : f->dump_inline_stack (dump_file);
964 : 0 : fprintf (dump_file, "\n");
965 : : }
966 : : /* We already merged outer part of the function acounting
967 : : the inlined calll; compensate. */
968 : 0 : for (function_instance *s = this; s; s = s->inlined_to ())
969 : : {
970 : 0 : s->total_count_ -= f->total_count ();
971 : 0 : gcc_checking_assert (s->total_count_ >= 0);
972 : : }
973 : 0 : other->callsites.erase (iter);
974 : 0 : function_instance::offline (f, new_functions);
975 : : /* Start from begining as merging might have offlined
976 : : some functions in the case of recursive inlining. */
977 : 0 : iter = other->callsites.begin ();
978 : : }
979 : : else
980 : 0 : ++iter;
981 : 0 : for (callsite_map::const_iterator iter = callsites.begin ();
982 : 0 : iter != callsites.end ();)
983 : 0 : if (other->callsites.count (iter->first) == 0)
984 : : {
985 : 0 : function_instance *f = iter->second;
986 : 0 : if (dump_file)
987 : : {
988 : 0 : fprintf (dump_file, " Mismatch in inlined functions;"
989 : : " offlining in merge destination:");
990 : 0 : f->dump_inline_stack (dump_file);
991 : 0 : fprintf (dump_file, "\n");
992 : : }
993 : 0 : callsites.erase (iter);
994 : 0 : function_instance::offline (f, new_functions);
995 : 0 : iter = callsites.begin ();
996 : 0 : changed = true;
997 : : }
998 : : else
999 : 0 : ++iter;
1000 : : }
1001 : 0 : for (callsite_map::const_iterator iter = other->callsites.begin ();
1002 : 0 : iter != other->callsites.end (); ++iter)
1003 : : {
1004 : 0 : if (dump_file)
1005 : : {
1006 : 0 : fprintf (dump_file, " Merging profile for inlined function\n"
1007 : : " from: ");
1008 : 0 : iter->second->dump_inline_stack (dump_file);
1009 : 0 : fprintf (dump_file, " total:%" PRIu64 "\n to : ",
1010 : 0 : (int64_t)iter->second->total_count ());
1011 : 0 : callsites[iter->first]->dump_inline_stack (dump_file);
1012 : 0 : fprintf (dump_file, " total:%" PRIu64 "\n",
1013 : 0 : (int64_t)callsites[iter->first]->total_count ());
1014 : : }
1015 : :
1016 : 0 : callsites[iter->first]->merge (iter->second, new_functions);
1017 : : }
1018 : :
1019 : 0 : for (position_count_map::const_iterator iter = other->pos_counts.begin ();
1020 : 0 : iter != other->pos_counts.end (); ++iter)
1021 : 0 : if (pos_counts.count (iter->first) == 0)
1022 : 0 : pos_counts[iter->first] = iter->second;
1023 : : else
1024 : : {
1025 : 0 : pos_counts[iter->first].count += iter->second.count;
1026 : 0 : for (icall_target_map::const_iterator titer
1027 : 0 : = iter->second.targets.begin ();
1028 : 0 : titer != iter->second.targets.end (); ++titer)
1029 : 0 : if (pos_counts[iter->first].targets.count (titer->first) == 0)
1030 : 0 : pos_counts[iter->first].targets[titer->first]
1031 : 0 : = titer->second;
1032 : : else
1033 : 0 : pos_counts[iter->first].targets[titer->first]
1034 : 0 : += titer->second;
1035 : : }
1036 : 0 : }
1037 : :
1038 : : /* Make inline function FN offline.
1039 : : If tolevel function of same name already exists, then merge profiles.
1040 : : Otherwise turn FN toplevel. Return true if new toplevel function
1041 : : was introduced.
1042 : : If new toplevel functions are created and NEW_FUNCTIONS != NULL,
1043 : : add them to NEW_FUNCTIONS.
1044 : :
1045 : : TODO: When offlining indirect call we lose information about the
1046 : : call target. It should be possible to add it into
1047 : : targets histogram. */
1048 : :
1049 : : bool
1050 : 0 : function_instance::offline (function_instance *fn,
1051 : : vec <function_instance *> &new_functions)
1052 : : {
1053 : 0 : gcc_checking_assert (fn->inlined_to ());
1054 : 0 : for (function_instance *s = fn->inlined_to (); s; s = s->inlined_to ())
1055 : : {
1056 : 0 : s->total_count_ -= fn->total_count ();
1057 : 0 : gcc_checking_assert (s->total_count_ >= 0);
1058 : : }
1059 : 0 : function_instance *to
1060 : 0 : = afdo_source_profile->get_function_instance_by_name_index (fn->name ());
1061 : 0 : fn->set_inlined_to (NULL);
1062 : : /* If there is offline function of same name, we need to merge profile.
1063 : : Delay this by adding function to a worklist so we do not run into
1064 : : problem with recursive inlining. */
1065 : 0 : if (to)
1066 : : {
1067 : 0 : if (fn->in_worklist_p ())
1068 : : return false;
1069 : 0 : fn->set_in_worklist ();
1070 : 0 : new_functions.safe_push (fn);
1071 : 0 : if (dump_file)
1072 : : {
1073 : 0 : fprintf (dump_file, " Recoding duplicate: ");
1074 : 0 : to->dump_inline_stack (dump_file);
1075 : 0 : fprintf (dump_file, "\n");
1076 : : }
1077 : 0 : return true;
1078 : : }
1079 : 0 : if (dump_file)
1080 : : {
1081 : 0 : fprintf (dump_file, " Added as offline instance: ");
1082 : 0 : fn->dump_inline_stack (dump_file);
1083 : 0 : fprintf (dump_file, "\n");
1084 : : }
1085 : 0 : if (fn->total_count ())
1086 : 0 : fn->head_count_ = -1;
1087 : 0 : afdo_source_profile->add_function_instance (fn);
1088 : 0 : fn->set_in_worklist ();
1089 : 0 : new_functions.safe_push (fn);
1090 : 0 : return true;
1091 : : }
1092 : :
1093 : : /* Offline all inlined functions with name in SEEN.
1094 : : If new toplevel functions are created, add them to NEW_FUNCTIONS. */
1095 : :
1096 : : void
1097 : 0 : function_instance::offline_if_in_set (name_index_set &seen,
1098 : : vec <function_instance *> &new_functions)
1099 : : {
1100 : 0 : for (callsite_map::const_iterator iter = callsites.begin ();
1101 : 0 : iter != callsites.end ();)
1102 : 0 : if (seen.contains (iter->first.second))
1103 : : {
1104 : 0 : function_instance *f = iter->second;
1105 : 0 : if (dump_file)
1106 : : {
1107 : 0 : fprintf (dump_file, "Offlining function inlined to other module: ");
1108 : 0 : f->dump_inline_stack (dump_file);
1109 : 0 : fprintf (dump_file, "\n");
1110 : : }
1111 : 0 : iter = callsites.erase (iter);
1112 : 0 : function_instance::offline (f, new_functions);
1113 : : /* Start from begining as merging might have offlined
1114 : : some functions in the case of recursive inlining. */
1115 : 0 : iter = callsites.begin ();
1116 : : }
1117 : : else
1118 : : {
1119 : 0 : iter->second->offline_if_in_set (seen, new_functions);
1120 : 0 : ++iter;
1121 : : }
1122 : 0 : }
1123 : :
1124 : : /* Try to check if inlined_fn can correspond to a call of function N.
1125 : : Return non-zero if it correspons and 2 if renaming was done. */
1126 : :
1127 : : static int
1128 : 0 : match_with_target (gimple *stmt, function_instance *inlined_fn, cgraph_node *n)
1129 : : {
1130 : 0 : const char *symbol_name = IDENTIFIER_POINTER
1131 : : (DECL_ASSEMBLER_NAME (n->decl));
1132 : 0 : const char *name = afdo_string_table->get_name (inlined_fn->name ());
1133 : 0 : if (strcmp (name, symbol_name))
1134 : : {
1135 : 0 : int i;
1136 : 0 : bool in_suffix = false;
1137 : 0 : for (i = 0; i; i++)
1138 : : {
1139 : : if (name[i] != symbol_name[i])
1140 : : break;
1141 : : if (name[i] == '.')
1142 : : in_suffix = true;
1143 : : }
1144 : : /* Accept dwarf names and stripped suffixes. */
1145 : 0 : if (!strcmp (lang_hooks.dwarf_name (n->decl, 0),
1146 : : afdo_string_table->get_name (inlined_fn->name ()))
1147 : 0 : || (!name[i] && symbol_name[i] == '.')
1148 : 0 : || in_suffix)
1149 : : {
1150 : 0 : int index = afdo_string_table->get_index (symbol_name);
1151 : 0 : if (index == -1)
1152 : 0 : index = afdo_string_table->add_name (xstrdup (symbol_name));
1153 : 0 : if (dump_file)
1154 : 0 : fprintf (dump_file,
1155 : : " Renaming inlined call target %s to %s\n",
1156 : : name, symbol_name);
1157 : 0 : inlined_fn->set_name (index);
1158 : 0 : return 2;
1159 : : }
1160 : 0 : warning_at (gimple_location (stmt), OPT_Wauto_profile,
1161 : : "auto-profile contains inlined "
1162 : : "function with symbol name %s instead of symbol name %s",
1163 : : name, symbol_name);
1164 : 0 : return 0;
1165 : : }
1166 : : return 1;
1167 : : }
1168 : :
1169 : : static void
1170 : 0 : dump_stmt (gimple *stmt, count_info *info, function_instance *inlined_fn,
1171 : : inline_stack &stack)
1172 : : {
1173 : 0 : if (dump_file)
1174 : : {
1175 : 0 : fprintf (dump_file, " ");
1176 : 0 : if (!stack.length ())
1177 : 0 : fprintf (dump_file, " ");
1178 : : else
1179 : : {
1180 : 0 : gcc_checking_assert (stack.length () == 1);
1181 : 0 : fprintf (dump_file, "%5i", stack[0].afdo_loc >> 16);
1182 : 0 : if (stack[0].afdo_loc & 65535)
1183 : 0 : fprintf (dump_file, ".%-5i", stack[0].afdo_loc & 65535);
1184 : : else
1185 : 0 : fprintf (dump_file, " ");
1186 : 0 : if (info)
1187 : 0 : fprintf (dump_file, "%9" PRIu64 " ", (int64_t)info->count);
1188 : 0 : else if (inlined_fn)
1189 : 0 : fprintf (dump_file, " inlined ");
1190 : : else
1191 : 0 : fprintf (dump_file, " no info ");
1192 : : }
1193 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1194 : : }
1195 : 0 : }
1196 : :
1197 : : /* Lookup count and warn about duplicates. */
1198 : : count_info *
1199 : 0 : function_instance::lookup_count (location_t loc, inline_stack &stack,
1200 : : cgraph_node *node)
1201 : : {
1202 : 0 : gcc_checking_assert (stack.length () < 2);
1203 : 0 : if (stack.length ())
1204 : : {
1205 : 0 : int c = pos_counts.count (stack[0].afdo_loc);
1206 : 0 : if (c > 1)
1207 : : warning_at (loc, OPT_Wauto_profile,
1208 : : "duplicated count information"
1209 : : " in auto-profile of %q+F"
1210 : : " with relative location %i discriminator %i",
1211 : : node->decl, stack[0].afdo_loc >> 16,
1212 : : stack[0].afdo_loc & 65535);
1213 : 0 : if (c)
1214 : 0 : return &pos_counts[stack[0].afdo_loc];
1215 : : }
1216 : : return NULL;
1217 : : }
1218 : :
1219 : : /* Mark expr locations as used. */
1220 : : void
1221 : 0 : mark_expr_locations (function_instance *f, tree t, cgraph_node *node,
1222 : : hash_set<const count_info *> &counts)
1223 : : {
1224 : 0 : inline_stack stack;
1225 : 0 : return;
1226 : : if (!t)
1227 : : return;
1228 : : do
1229 : : {
1230 : : get_inline_stack_in_node (EXPR_LOCATION (t), &stack, node);
1231 : : /* FIXME: EXPR_LOCATION does not always originate from current
1232 : : function. */
1233 : : if (stack.length () > 1)
1234 : : break;
1235 : : count_info *info = f->lookup_count (EXPR_LOCATION (t), stack, node);
1236 : : if (info)
1237 : : counts.add (info);
1238 : : if (handled_component_p (t))
1239 : : t = TREE_OPERAND (t, 0);
1240 : : else
1241 : : break;
1242 : : }
1243 : : while (true);
1244 : 0 : }
1245 : :
1246 : : /* Match function instance with gimple body.
1247 : : Report mismatches, attempt to fix them if possible and remove data we will
1248 : : not use.
1249 : :
1250 : : Set location and call_location so we can output diagnostics and know what
1251 : : functions was already matched. */
1252 : :
1253 : : bool
1254 : 0 : function_instance::match (cgraph_node *node,
1255 : : vec <function_instance *> &new_functions,
1256 : : name_index_map &to_symbol_name)
1257 : : {
1258 : 0 : if (get_location () != UNKNOWN_LOCATION)
1259 : : return false;
1260 : 0 : set_location (DECL_SOURCE_LOCATION (node->decl));
1261 : 0 : if (dump_file)
1262 : : {
1263 : 0 : fprintf (dump_file,
1264 : : "\nMatching gimple function %s with auto profile: ",
1265 : : node->dump_name ());
1266 : 0 : dump_inline_stack (dump_file);
1267 : 0 : fprintf (dump_file, "\n");
1268 : : }
1269 : 0 : basic_block bb;
1270 : : /* Sets used to track if entires in auto-profile are useful. */
1271 : 0 : hash_set<const count_info *> counts;
1272 : 0 : hash_set<const count_info *> targets;
1273 : 0 : hash_set<const function_instance *> functions;
1274 : :
1275 : : /* We try to fill in lost disciminator if there is unique call
1276 : : with given line number. This map is used to record them. */
1277 : 0 : hash_map<int_hash <int, -1, -2>,auto_vec <gcall *>> lineno_to_call;
1278 : 0 : bool lineno_to_call_computed = false;
1279 : :
1280 : 0 : for (tree arg = DECL_ARGUMENTS (node->decl); arg; arg = DECL_CHAIN (arg))
1281 : : {
1282 : 0 : inline_stack stack;
1283 : :
1284 : 0 : get_inline_stack_in_node (DECL_SOURCE_LOCATION (arg), &stack, node);
1285 : 0 : count_info *info = lookup_count (DECL_SOURCE_LOCATION (arg), stack, node);
1286 : 0 : if (stack.length () && dump_file)
1287 : : {
1288 : 0 : gcc_checking_assert (stack.length () == 1);
1289 : 0 : fprintf (dump_file, "%5i", stack[0].afdo_loc >> 16);
1290 : 0 : if (stack[0].afdo_loc & 65535)
1291 : 0 : fprintf (dump_file, " .%-5i arg", stack[0].afdo_loc & 65535);
1292 : : else
1293 : 0 : fprintf (dump_file, " arg ");
1294 : 0 : print_generic_expr (dump_file, arg);
1295 : 0 : fprintf (dump_file, "\n");
1296 : : }
1297 : 0 : if (info)
1298 : 0 : counts.add (info);
1299 : 0 : }
1300 : 0 : FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
1301 : : {
1302 : 0 : if (dump_file)
1303 : 0 : fprintf (dump_file, " basic block %i\n", bb->index);
1304 : 0 : for (gphi_iterator gpi = gsi_start_phis (bb);
1305 : 0 : !gsi_end_p (gpi);
1306 : 0 : gsi_next (&gpi))
1307 : : {
1308 : 0 : gphi *phi = gpi.phi ();
1309 : 0 : inline_stack stack;
1310 : :
1311 : 0 : get_inline_stack_in_node (gimple_location (phi), &stack, node);
1312 : 0 : count_info *info = lookup_count (gimple_location (phi), stack, node);
1313 : 0 : if (info)
1314 : 0 : counts.add (info);
1315 : 0 : dump_stmt (phi, info, NULL, stack);
1316 : 0 : counts.add (info);
1317 : 0 : for (edge e : bb->succs)
1318 : : {
1319 : 0 : location_t phi_loc
1320 : 0 : = gimple_phi_arg_location_from_edge (phi, e);
1321 : 0 : inline_stack stack;
1322 : 0 : get_inline_stack_in_node (phi_loc, &stack, node);
1323 : 0 : count_info *info = lookup_count (phi_loc, stack, node);
1324 : 0 : if (info)
1325 : 0 : counts.add (info);
1326 : 0 : gcc_checking_assert (stack.length () < 2);
1327 : 0 : mark_expr_locations (this,
1328 : : gimple_phi_arg_def_from_edge (phi, e),
1329 : : node, counts);
1330 : 0 : }
1331 : 0 : }
1332 : : /* TODO: goto locuses are not used for BB annotation. */
1333 : 0 : for (edge e : bb->succs)
1334 : : {
1335 : 0 : inline_stack stack;
1336 : 0 : get_inline_stack_in_node (e->goto_locus, &stack, node);
1337 : 0 : count_info *info = lookup_count (e->goto_locus, stack, node);
1338 : 0 : if (info)
1339 : 0 : counts.add (info);
1340 : 0 : }
1341 : 0 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1342 : 0 : !gsi_end_p (gsi); gsi_next (&gsi))
1343 : : {
1344 : 0 : inline_stack stack;
1345 : 0 : gimple *stmt = gsi_stmt (gsi);
1346 : 0 : get_inline_stack_in_node (gimple_location (stmt), &stack, node);
1347 : :
1348 : 0 : count_info *info = lookup_count (gimple_location (stmt), stack, node);
1349 : 0 : if (info)
1350 : 0 : counts.add (info);
1351 : 0 : for (unsigned int op = 0; op < gimple_num_ops (stmt); op++)
1352 : 0 : mark_expr_locations (this, gimple_op (stmt, op), node, counts);
1353 : 0 : if (gimple_code (stmt) == GIMPLE_CALL)
1354 : : {
1355 : 0 : function_instance *inlined_fn = NULL;
1356 : 0 : function_instance *inlined_fn_nodisc = NULL;
1357 : : /* Lookup callsite. */
1358 : 0 : if (stack.length ())
1359 : : {
1360 : 0 : int c = 0;
1361 : 0 : int cnodis = 0;
1362 : 0 : for (auto const &iter : callsites)
1363 : 0 : if (iter.first.first == stack[0].afdo_loc)
1364 : : {
1365 : 0 : if (!c)
1366 : 0 : inlined_fn = iter.second;
1367 : 0 : c++;
1368 : : }
1369 : : /* Discriminators are sometimes lost; try to find the
1370 : : call without discriminator info. */
1371 : 0 : else if (iter.first.first == (stack[0].afdo_loc & ~65535))
1372 : : {
1373 : 0 : if (!cnodis)
1374 : 0 : inlined_fn_nodisc = iter.second;
1375 : 0 : cnodis++;
1376 : : }
1377 : 0 : if (c > 1 || cnodis > 1)
1378 : 0 : warning_at (gimple_location (stmt), OPT_Wauto_profile,
1379 : : "duplicated callsite in auto-profile of %q+F"
1380 : : " with relative location %i, discriminator %i",
1381 : 0 : node->decl, stack[0].afdo_loc >> 16,
1382 : 0 : stack[0].afdo_loc & 65535);
1383 : 0 : if (inlined_fn && info && info->targets.size ())
1384 : 0 : warning_at (gimple_location (stmt), OPT_Wauto_profile,
1385 : : "both call targets and inline callsite"
1386 : : " information is present in auto-profile"
1387 : : " of function %q+F with relative location"
1388 : : " %i, discriminator %i",
1389 : 0 : node->decl, stack[0].afdo_loc >> 16,
1390 : 0 : stack[0].afdo_loc & 65535);
1391 : 0 : tree callee = gimple_call_fndecl (stmt);
1392 : 0 : cgraph_node *callee_node;
1393 : 0 : unsigned int loc = stack[0].afdo_loc;
1394 : 0 : bool lost_discriminator = false;
1395 : 0 : if (!inlined_fn && inlined_fn_nodisc)
1396 : : {
1397 : 0 : if (!lineno_to_call_computed)
1398 : : {
1399 : 0 : basic_block bb2;
1400 : 0 : FOR_EACH_BB_FN (bb2,
1401 : : DECL_STRUCT_FUNCTION (node->decl))
1402 : 0 : for (gimple_stmt_iterator gsi2
1403 : 0 : = gsi_start_bb (bb2);
1404 : 0 : !gsi_end_p (gsi2); gsi_next (&gsi2))
1405 : 0 : if (gcall *call
1406 : 0 : = dyn_cast <gcall *> (gsi_stmt (gsi2)))
1407 : : {
1408 : 0 : inline_stack stack2;
1409 : 0 : get_inline_stack_in_node
1410 : 0 : (gimple_location (call),
1411 : : &stack2, node);
1412 : 0 : if (stack2.length ())
1413 : 0 : lineno_to_call.get_or_insert
1414 : 0 : (stack2[0].afdo_loc >> 16).safe_push (call);
1415 : 0 : }
1416 : : lineno_to_call_computed = true;
1417 : : }
1418 : : /* If we can determine lost discriminator uniquely,
1419 : : use it. */
1420 : 0 : if (lineno_to_call.get
1421 : 0 : (stack[0].afdo_loc >> 16)->length () == 1)
1422 : : {
1423 : 0 : warning_at (gimple_location (stmt), OPT_Wauto_profile,
1424 : : "auto-profile of %q+F seem to contain"
1425 : : " lost discriminator %i for call at"
1426 : : " relative location %i",
1427 : : node->decl, loc & 65535, loc >> 16);
1428 : 0 : inlined_fn = inlined_fn_nodisc;
1429 : 0 : if (dump_file)
1430 : 0 : fprintf (dump_file, " Lost discriminator %i\n",
1431 : : loc & 65535);
1432 : 0 : loc = loc & ~65535;
1433 : : }
1434 : : lost_discriminator = true;
1435 : : }
1436 : 0 : if (callee && (callee_node = cgraph_node::get (callee)))
1437 : : {
1438 : 0 : callee_node = callee_node->ultimate_alias_target ();
1439 : 0 : if (inlined_fn)
1440 : : {
1441 : 0 : int old_name = inlined_fn->name ();
1442 : 0 : int r = match_with_target (stmt, inlined_fn,
1443 : : callee_node);
1444 : 0 : if (r == 2)
1445 : : {
1446 : 0 : auto iter = callsites.find ({loc, old_name});
1447 : 0 : gcc_checking_assert (old_name
1448 : : != inlined_fn->name ()
1449 : : && iter != callsites.end ()
1450 : : && iter->second
1451 : : == inlined_fn);
1452 : 0 : callsite key2 = {stack[0].afdo_loc,
1453 : 0 : inlined_fn->name ()};
1454 : 0 : callsites[key2] = inlined_fn;
1455 : 0 : callsites.erase (iter);
1456 : : }
1457 : 0 : if (r)
1458 : 0 : functions.add (inlined_fn);
1459 : : }
1460 : :
1461 : 0 : if (info && info->targets.size () > 1)
1462 : 0 : warning_at (gimple_location (stmt), OPT_Wauto_profile,
1463 : : "auto-profile of %q+F contains multiple"
1464 : : " targets for a direct call with relative"
1465 : : " location %i, discriminator %i",
1466 : 0 : node->decl, stack[0].afdo_loc >> 16,
1467 : 0 : stack[0].afdo_loc & 65535);
1468 : : /* We do not need target profile for direct calls. */
1469 : 0 : if (info)
1470 : 0 : info->targets.clear ();
1471 : : }
1472 : : else
1473 : : {
1474 : 0 : if (inlined_fn
1475 : 0 : && inlined_fn->get_call_location ()
1476 : : != UNKNOWN_LOCATION
1477 : 0 : && warning_at (gimple_location (stmt),
1478 : 0 : OPT_Wauto_profile,
1479 : : "%q+F contains two calls of the same"
1480 : : " relative location +%i,"
1481 : : " discrimnator %i,"
1482 : : " that leads to lost auto-profile",
1483 : : node->decl,
1484 : : loc << 16,
1485 : : loc & 65535))
1486 : : {
1487 : 0 : inform (inlined_fn->get_call_location (),
1488 : : "location of the earlier call");
1489 : 0 : inlined_fn = NULL;
1490 : : }
1491 : 0 : if (inlined_fn)
1492 : : {
1493 : 0 : inlined_fn->set_call_location
1494 : 0 : (gimple_location (stmt));
1495 : : /* Do renaming if needed so we can look up
1496 : : cgraph node and recurse into inlined function. */
1497 : 0 : int *newn = to_symbol_name.get (inlined_fn->name ());
1498 : 0 : gcc_checking_assert
1499 : : (!newn || *newn != inlined_fn->name ());
1500 : 0 : if (newn || lost_discriminator)
1501 : : {
1502 : 0 : auto iter = callsites.find
1503 : 0 : ({loc, inlined_fn->name ()});
1504 : 0 : gcc_checking_assert (iter != callsites.end ()
1505 : : && iter->second
1506 : : == inlined_fn);
1507 : 0 : callsite key2 = {stack[0].afdo_loc,
1508 : 0 : newn ? *newn
1509 : 0 : : inlined_fn->name ()};
1510 : 0 : callsites[key2] = inlined_fn;
1511 : 0 : inlined_fn->set_name (newn ? *newn
1512 : 0 : : inlined_fn->name ());
1513 : 0 : callsites.erase (iter);
1514 : : }
1515 : 0 : functions.add (inlined_fn);
1516 : : }
1517 : 0 : if (info)
1518 : 0 : targets.add (info);
1519 : : }
1520 : : }
1521 : 0 : dump_stmt (stmt, info, inlined_fn, stack);
1522 : : }
1523 : : else
1524 : 0 : dump_stmt (stmt, info, NULL, stack);
1525 : 0 : }
1526 : : }
1527 : 0 : bool warned = false;
1528 : 0 : for (auto &iter : pos_counts)
1529 : 0 : if (iter.second.targets.size ()
1530 : 0 : && counts.contains (&iter.second)
1531 : 0 : && !targets.contains (&iter.second))
1532 : : {
1533 : 0 : if (!warned)
1534 : 0 : warned = warning_at
1535 : 0 : (DECL_SOURCE_LOCATION (node->decl),
1536 : 0 : OPT_Wauto_profile,
1537 : : "auto-profile of %q+F contains indirect call targets"
1538 : : " not associated with an indirect call statement",
1539 : : node->decl);
1540 : 0 : if (warned)
1541 : 0 : inform (DECL_SOURCE_LOCATION (node->decl),
1542 : : "count %" PRIu64
1543 : : " with relative location +%i, discriminator %i",
1544 : 0 : iter.second.count, iter.first >> 16, iter.first & 65535);
1545 : 0 : if (dump_file)
1546 : : {
1547 : 0 : fprintf (dump_file, "Removing targets of ");
1548 : 0 : dump_afdo_loc (dump_file, iter.first);
1549 : 0 : fprintf (dump_file, "\n");
1550 : : }
1551 : 0 : iter.second.targets.clear ();
1552 : : }
1553 : 0 : warned = false;
1554 : : /* Profile sometimes contains extra location for start or end of function
1555 : : (prologue, epilogue).
1556 : : TODO: If present, perhaps it can be used to determine entry block
1557 : : and exit block counts. */
1558 : 0 : unsigned int end_location = get_combined_location
1559 : 0 : (DECL_STRUCT_FUNCTION (node->decl)->function_end_locus, node->decl);
1560 : 0 : unsigned int start_location = get_combined_location
1561 : 0 : (DECL_STRUCT_FUNCTION (node->decl)->function_start_locus, node->decl);
1562 : 0 : for (position_count_map::const_iterator iter = pos_counts.begin ();
1563 : 0 : iter != pos_counts.end ();)
1564 : 0 : if (!counts.contains (&iter->second))
1565 : : {
1566 : 0 : if (iter->first != end_location && iter->first != start_location
1567 : 0 : && iter->first)
1568 : : {
1569 : 0 : if (!warned)
1570 : 0 : warned = warning_at (DECL_SOURCE_LOCATION (node->decl),
1571 : 0 : OPT_Wauto_profile,
1572 : : "auto-profile of %q+F contains extra statements",
1573 : : node->decl);
1574 : 0 : if (warned)
1575 : 0 : inform (DECL_SOURCE_LOCATION (node->decl),
1576 : : "count %" PRIu64 " with relative location +%i,"
1577 : : " discriminator %i",
1578 : 0 : iter->second.count, iter->first >> 16,
1579 : 0 : iter->first & 65535);
1580 : 0 : if ((iter->first >> 16) > (end_location >> 16) && warned)
1581 : 0 : inform (DECL_SOURCE_LOCATION (node->decl),
1582 : : "location is after end of function");
1583 : : }
1584 : 0 : if (dump_file)
1585 : : {
1586 : 0 : fprintf (dump_file, "Removing unmatched count ");
1587 : 0 : dump_afdo_loc (dump_file, iter->first);
1588 : 0 : fprintf (dump_file, ":%" PRIu64, iter->second.count);
1589 : 0 : for (auto &titer : iter->second.targets)
1590 : 0 : fprintf (dump_file, " %s:%" PRIu64,
1591 : 0 : afdo_string_table->get_name (titer.first),
1592 : 0 : (int64_t)titer.second);
1593 : 0 : fprintf (dump_file, "\n");
1594 : : }
1595 : 0 : iter = pos_counts.erase (iter);
1596 : : }
1597 : : else
1598 : 0 : iter++;
1599 : 0 : warned = false;
1600 : 0 : for (callsite_map::const_iterator iter = callsites.begin ();
1601 : 0 : iter != callsites.end ();)
1602 : 0 : if (!functions.contains (iter->second))
1603 : : {
1604 : 0 : if (!warned)
1605 : 0 : warned = warning_at (DECL_SOURCE_LOCATION (node->decl),
1606 : 0 : OPT_Wauto_profile,
1607 : : "auto-profile of %q+F contains extra callsites",
1608 : : node->decl);
1609 : 0 : if (warned)
1610 : 0 : inform (DECL_SOURCE_LOCATION (node->decl),
1611 : : "call of %s with relative location +%i, discriminator %i",
1612 : 0 : afdo_string_table->get_name (iter->first.second),
1613 : 0 : iter->first.first >> 16, iter->first.first & 65535);
1614 : 0 : if ((iter->first.first >> 16) > (end_location >> 16) && warned)
1615 : 0 : inform (DECL_SOURCE_LOCATION (node->decl),
1616 : : "location is after end of function");
1617 : 0 : warned = true;
1618 : 0 : function_instance *f = iter->second;
1619 : 0 : if (dump_file)
1620 : : {
1621 : 0 : fprintf (dump_file,
1622 : : "Offlining inline with no corresponding gimple stmt ");
1623 : 0 : f->dump_inline_stack (dump_file);
1624 : 0 : fprintf (dump_file, "\n");
1625 : : }
1626 : 0 : callsites.erase (iter);
1627 : 0 : offline (f, new_functions);
1628 : 0 : iter = callsites.begin ();
1629 : : }
1630 : : else
1631 : 0 : iter++;
1632 : 0 : for (auto &iter : callsites)
1633 : 0 : if (cgraph_node *n = iter.second->get_cgraph_node ())
1634 : 0 : iter.second->match (n, new_functions, to_symbol_name);
1635 : 0 : return true;
1636 : 0 : }
1637 : :
1638 : : /* Walk inlined functions and if their name is not in SEEN
1639 : : remove it. Also rename function names as given by
1640 : : to_symbol_name map. */
1641 : :
1642 : : void
1643 : 0 : function_instance::remove_external_functions
1644 : : (name_index_set &seen,
1645 : : name_index_map &to_symbol_name,
1646 : : vec <function_instance *> &new_functions)
1647 : : {
1648 : 0 : auto_vec <callsite, 20> to_rename;
1649 : :
1650 : 0 : for (callsite_map::const_iterator iter = callsites.begin ();
1651 : 0 : iter != callsites.end ();)
1652 : 0 : if (!seen.contains (iter->first.second))
1653 : : {
1654 : 0 : function_instance *f = iter->second;
1655 : 0 : if (dump_file)
1656 : : {
1657 : 0 : fprintf (dump_file, " Removing external inline: ");
1658 : 0 : f->dump_inline_stack (dump_file);
1659 : 0 : fprintf (dump_file, "\n");
1660 : : }
1661 : 0 : iter = callsites.erase (iter);
1662 : 0 : f->set_inlined_to (NULL);
1663 : 0 : f->offline_if_in_set (seen, new_functions);
1664 : 0 : delete f;
1665 : : }
1666 : : else
1667 : : {
1668 : 0 : gcc_checking_assert ((int)iter->first.second
1669 : : == iter->second->name ());
1670 : 0 : int *newn = iter->second->get_call_location () == UNKNOWN_LOCATION
1671 : 0 : ? to_symbol_name.get (iter->first.second)
1672 : : : NULL;
1673 : 0 : if (newn)
1674 : : {
1675 : 0 : gcc_checking_assert (iter->second->inlined_to ());
1676 : 0 : to_rename.safe_push (iter->first);
1677 : : }
1678 : 0 : iter->second->remove_external_functions
1679 : 0 : (seen, to_symbol_name, new_functions);
1680 : 0 : ++iter;
1681 : : }
1682 : 0 : for (auto &key : to_rename)
1683 : : {
1684 : 0 : auto iter = callsites.find (key);
1685 : 0 : callsite key2 = key;
1686 : 0 : key2.second = *to_symbol_name.get (key.second);
1687 : 0 : callsites[key2] = iter->second;
1688 : 0 : iter->second->set_name (key2.second);
1689 : 0 : callsites.erase (iter);
1690 : : }
1691 : 0 : auto_vec <int, 20> target_to_rename;
1692 : 0 : for (auto &iter : pos_counts)
1693 : : {
1694 : 0 : for (auto const &titer : iter.second.targets)
1695 : : {
1696 : 0 : int *ren = to_symbol_name.get (titer.first);
1697 : 0 : if (ren)
1698 : 0 : target_to_rename.safe_push (titer.first);
1699 : : }
1700 : 0 : while (target_to_rename.length ())
1701 : : {
1702 : 0 : int key = target_to_rename.pop ();
1703 : 0 : int key2 = *to_symbol_name.get (key);
1704 : 0 : auto i = iter.second.targets.find (key);
1705 : 0 : if (iter.second.targets.count (key2) == 0)
1706 : 0 : iter.second.targets[key2] = i->second;
1707 : : else
1708 : 0 : iter.second.targets[key2] += i->second;
1709 : 0 : iter.second.targets.erase (i);
1710 : : }
1711 : : }
1712 : 0 : }
1713 : :
1714 : : /* Look for inline instances that was not realized and
1715 : : remove them while possibly merging them to offline variants. */
1716 : :
1717 : : void
1718 : 0 : function_instance::offline_if_not_realized
1719 : : (vec <function_instance *> &new_functions)
1720 : : {
1721 : 0 : for (callsite_map::const_iterator iter = callsites.begin ();
1722 : 0 : iter != callsites.end ();)
1723 : 0 : if (!iter->second->realized_p ())
1724 : : {
1725 : 0 : function_instance *f = iter->second;
1726 : 0 : if (dump_file)
1727 : : {
1728 : 0 : fprintf (dump_file, "Offlining unrealized inline ");
1729 : 0 : f->dump_inline_stack (dump_file);
1730 : 0 : fprintf (dump_file, "\n");
1731 : : }
1732 : 0 : iter = callsites.erase (iter);
1733 : 0 : offline (f, new_functions);
1734 : : }
1735 : : else
1736 : : {
1737 : 0 : iter->second->offline_if_not_realized (new_functions);
1738 : 0 : ++iter;
1739 : : }
1740 : 0 : }
1741 : :
1742 : : /* Dump instance to F indented by INDENT. */
1743 : :
1744 : : void
1745 : 0 : function_instance::dump (FILE *f, int indent, bool nested) const
1746 : : {
1747 : 0 : if (!nested)
1748 : 0 : fprintf (f, "%*s%s total:%" PRIu64 " head:%" PRId64 "\n",
1749 : : indent, "", afdo_string_table->get_name (name ()),
1750 : 0 : (int64_t)total_count (), (int64_t)head_count ());
1751 : : else
1752 : 0 : fprintf (f, " total:%" PRIu64 "\n", (int64_t)total_count ());
1753 : 0 : for (auto const &iter : pos_counts)
1754 : : {
1755 : 0 : fprintf (f, "%*s", indent + 2, "");
1756 : 0 : dump_afdo_loc (f, iter.first);
1757 : 0 : fprintf (f, ": %" PRIu64, (int64_t)iter.second.count);
1758 : :
1759 : 0 : for (auto const &titer : iter.second.targets)
1760 : 0 : fprintf (f, " %s:%" PRIu64,
1761 : 0 : afdo_string_table->get_name (titer.first),
1762 : 0 : (int64_t)titer.second);
1763 : 0 : fprintf (f,"\n");
1764 : : }
1765 : 0 : for (auto const &iter : callsites)
1766 : : {
1767 : 0 : fprintf (f, "%*s", indent + 2, "");
1768 : 0 : dump_afdo_loc (f, iter.first.first);
1769 : 0 : fprintf (f, ": %s", afdo_string_table->get_name (iter.first.second));
1770 : 0 : iter.second->dump (f, indent + 2, true);
1771 : 0 : gcc_checking_assert ((int)iter.first.second == iter.second->name ());
1772 : : }
1773 : 0 : }
1774 : :
1775 : : /* Dump inline path. */
1776 : :
1777 : : void
1778 : 0 : function_instance::dump_inline_stack (FILE *f) const
1779 : : {
1780 : 0 : auto_vec <callsite, 20> stack;
1781 : 0 : const function_instance *p = this, *s = inlined_to ();
1782 : 0 : while (s)
1783 : : {
1784 : 0 : bool found = false;
1785 : 0 : for (callsite_map::const_iterator iter = s->callsites.begin ();
1786 : 0 : iter != s->callsites.end (); ++iter)
1787 : 0 : if (iter->second == p)
1788 : : {
1789 : 0 : gcc_checking_assert (!found
1790 : : && (int)iter->first.second == p->name ());
1791 : 0 : stack.safe_push ({iter->first.first, s->name ()});
1792 : 0 : found = true;
1793 : : }
1794 : 0 : gcc_checking_assert (found);
1795 : 0 : p = s;
1796 : 0 : s = s->inlined_to ();
1797 : : }
1798 : 0 : for (callsite &s: stack)
1799 : : {
1800 : 0 : fprintf (f, "%s:", afdo_string_table->get_name (s.second));
1801 : 0 : dump_afdo_loc (f, s.first);
1802 : 0 : fprintf (f, " ");
1803 : : }
1804 : 0 : fprintf (f, "%s", afdo_string_table->get_name (name ()));
1805 : 0 : }
1806 : :
1807 : : /* Dump instance to stderr. */
1808 : :
1809 : : void
1810 : 0 : function_instance::debug () const
1811 : : {
1812 : 0 : dump (stderr);
1813 : 0 : }
1814 : :
1815 : : /* Return profile info for LOC in INFO. */
1816 : :
1817 : : bool
1818 : 0 : function_instance::get_count_info (location_t loc, count_info *info) const
1819 : : {
1820 : 0 : position_count_map::const_iterator iter = pos_counts.find (loc);
1821 : 0 : if (iter == pos_counts.end ())
1822 : : return false;
1823 : 0 : *info = iter->second;
1824 : 0 : return true;
1825 : : }
1826 : :
1827 : : /* Read the inlined indirect call target profile for STMT and store it in
1828 : : MAP, return the total count for all inlined indirect calls. */
1829 : :
1830 : : gcov_type
1831 : 0 : function_instance::find_icall_target_map (tree fn, gcall *stmt,
1832 : : icall_target_map *map) const
1833 : : {
1834 : 0 : gcov_type ret = 0;
1835 : 0 : unsigned stmt_offset = get_relative_location_for_stmt (fn, stmt);
1836 : :
1837 : 0 : for (callsite_map::const_iterator iter = callsites.begin ();
1838 : 0 : iter != callsites.end (); ++iter)
1839 : : {
1840 : 0 : unsigned callee = iter->second->name ();
1841 : : /* Check if callsite location match the stmt. */
1842 : 0 : if (iter->first.first != stmt_offset
1843 : 0 : || iter->second->removed_icall_target ())
1844 : 0 : continue;
1845 : 0 : struct cgraph_node *node = cgraph_node::get_for_asmname (
1846 : : get_identifier (afdo_string_table->get_name (callee)));
1847 : 0 : if (node == NULL)
1848 : 0 : continue;
1849 : 0 : (*map)[callee] = iter->second->total_count () * afdo_count_scale;
1850 : 0 : ret += iter->second->total_count () * afdo_count_scale;
1851 : : }
1852 : 0 : return ret;
1853 : : }
1854 : :
1855 : : /* Remove the inlined indirect call target profile for STMT. */
1856 : :
1857 : : void
1858 : 0 : function_instance::remove_icall_target (tree fn, gcall *stmt)
1859 : : {
1860 : 0 : unsigned stmt_offset = get_relative_location_for_stmt (fn, stmt);
1861 : 0 : int n = 0;
1862 : :
1863 : 0 : for (auto iter : callsites)
1864 : 0 : if (iter.first.first == stmt_offset)
1865 : : {
1866 : 0 : iter.second->remove_icall_target ();
1867 : 0 : n++;
1868 : : }
1869 : : /* TODO: If we add support for multiple targets, we may want to
1870 : : remove only those we succesfully inlined. */
1871 : 0 : gcc_assert (n);
1872 : 0 : }
1873 : :
1874 : : /* Offline all functions not defined in the current unit.
1875 : : We will not be able to early inline them.
1876 : : Doint so early will get VPT decisions more realistic. */
1877 : :
1878 : : void
1879 : 0 : autofdo_source_profile::offline_external_functions ()
1880 : : {
1881 : : /* First check all available definitions and mark their names as
1882 : : visible. */
1883 : 0 : cgraph_node *node;
1884 : 0 : name_index_set seen;
1885 : 0 : name_index_map to_symbol_name;
1886 : :
1887 : : /* Add renames erasing suffixes produced by late clones, such as
1888 : : .isra, .ipcp. */
1889 : 0 : for (size_t i = 1; i < afdo_string_table->num_entries (); i++)
1890 : : {
1891 : 0 : const char *n1 = afdo_string_table->get_name (i);
1892 : 0 : char *n2 = get_original_name (n1);
1893 : 0 : if (!strcmp (n1, n2))
1894 : : {
1895 : 0 : free (n2);
1896 : : /* Watch for duplicate entries.
1897 : : This seems to happen in practice and may be useful to distingush
1898 : : multiple static symbols of the same name, but we do not realy
1899 : : have a way to differentiate them in get_name lookup. */
1900 : 0 : int index = afdo_string_table->get_index (n1);
1901 : 0 : if (index != (int)i)
1902 : : {
1903 : 0 : if (dump_file)
1904 : 0 : fprintf (dump_file,
1905 : : "string table in auto-profile contains"
1906 : : " duplicated name %s\n", n1);
1907 : 0 : to_symbol_name.put (i, index);
1908 : : }
1909 : 0 : continue;
1910 : 0 : }
1911 : 0 : if (dump_file)
1912 : 0 : fprintf (dump_file, "Adding rename removing clone suffxes %s -> %s\n",
1913 : : n1, n2);
1914 : 0 : int index = afdo_string_table->get_index (n2);
1915 : 0 : if (index != -1)
1916 : 0 : free (n2);
1917 : : else
1918 : 0 : index = afdo_string_table->add_name (n2);
1919 : 0 : to_symbol_name.put (i, index);
1920 : : }
1921 : 0 : FOR_EACH_DEFINED_FUNCTION (node)
1922 : : {
1923 : 0 : const char *name
1924 : 0 : = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl));
1925 : 0 : const char *dwarf_name = lang_hooks.dwarf_name (node->decl, 0);
1926 : 0 : int index = afdo_string_table->get_index (name);
1927 : :
1928 : : /* Inline function may be identified by its dwarf names;
1929 : : rename them to symbol names. With LTO dwarf names are
1930 : : lost in free_lange_data. */
1931 : 0 : if (strcmp (name, dwarf_name))
1932 : : {
1933 : 0 : int index2 = afdo_string_table->get_index (dwarf_name);
1934 : 0 : if (index2 != -1)
1935 : : {
1936 : 0 : if (index == -1)
1937 : 0 : index = afdo_string_table->add_name (xstrdup (name));
1938 : 0 : if (dump_file)
1939 : : {
1940 : 0 : fprintf (dump_file, "Adding dwarf->symbol rename %s -> %s\n",
1941 : : afdo_string_table->get_name (index2), name);
1942 : 0 : if (to_symbol_name.get (index2))
1943 : 0 : fprintf (dump_file, "Dwarf name is not unique");
1944 : : }
1945 : 0 : to_symbol_name.put (index2, index);
1946 : 0 : seen.add (index2);
1947 : : }
1948 : : }
1949 : 0 : if (index != -1)
1950 : : {
1951 : 0 : if (dump_file)
1952 : 0 : fprintf (dump_file, "%s is defined in node %s\n",
1953 : : afdo_string_table->get_name (index),
1954 : : node->dump_name ());
1955 : 0 : seen.add (index);
1956 : : }
1957 : : else
1958 : : {
1959 : 0 : if (dump_file)
1960 : : {
1961 : 0 : fprintf (dump_file,
1962 : : "Node %s not in auto profile (%s neither %s)\n",
1963 : : node->dump_name (),
1964 : : name,
1965 : : dwarf_name);
1966 : : }
1967 : : }
1968 : : }
1969 : :
1970 : 0 : for (auto iter : to_symbol_name)
1971 : : {
1972 : : /* In case dwarf name was duplicated and later renamed,
1973 : : handle both. No more than one hop shold be needed. */
1974 : 0 : int *newn = to_symbol_name.get (iter.second);
1975 : 0 : if (newn)
1976 : 0 : iter.second = *newn;
1977 : 0 : gcc_checking_assert (!to_symbol_name.get (iter.second));
1978 : 0 : if (seen.contains (iter.second))
1979 : 0 : seen.add (iter.first);
1980 : : }
1981 : :
1982 : : /* Now process all tolevel (offline) function instances.
1983 : :
1984 : : If instance has no definition in this translation unit,
1985 : : first offline all inlined functions which are defined here
1986 : : (so we do not lose porfile due to cross-module inlining
1987 : : done by link-time optimizers).
1988 : :
1989 : : If instance has a definition, look into all inlined functions
1990 : : and remove external ones (result of cross-module inlining).
1991 : :
1992 : : TODO: after early-inlining we ought to offline all functions
1993 : : that were not inlined. */
1994 : 0 : vec <function_instance *>&fns = duplicate_functions_;
1995 : 0 : auto_vec <function_instance *, 20>fns2;
1996 : : /* Poppulate worklist with all functions to process. Processing
1997 : : may introduce new functions by offlining. */
1998 : 0 : for (auto const &iter : map_)
1999 : : {
2000 : 0 : iter.second->set_in_worklist ();
2001 : 0 : fns.safe_push (iter.second);
2002 : : }
2003 : :
2004 : : /* There are two worklists. First all functions needs to be matched
2005 : : with gimple body and only then we want to do merging, since matching
2006 : : should be done on unmodified profile and merging works better if
2007 : : mismatches are already resolved both in source and destination. */
2008 : 0 : while (fns.length () || fns2.length ())
2009 : 0 : if (fns.length ())
2010 : : {
2011 : 0 : function_instance *f = fns.pop ();
2012 : 0 : if (f->get_location () == UNKNOWN_LOCATION)
2013 : : {
2014 : 0 : int index = f->name ();
2015 : 0 : int *newn = to_symbol_name.get (index);
2016 : 0 : if (newn)
2017 : : {
2018 : 0 : f->set_name (*newn);
2019 : 0 : if (map_.count (index)
2020 : 0 : && map_[index] == f)
2021 : 0 : map_.erase (index);
2022 : 0 : if (!map_.count (*newn))
2023 : 0 : map_[*newn] = f;
2024 : : }
2025 : 0 : if (cgraph_node *n = f->get_cgraph_node ())
2026 : : {
2027 : 0 : gcc_checking_assert (seen.contains (f->name ()));
2028 : 0 : f->match (n, fns, to_symbol_name);
2029 : : }
2030 : : }
2031 : 0 : fns2.safe_push (f);
2032 : : }
2033 : : else
2034 : : {
2035 : 0 : function_instance *f = fns2.pop ();
2036 : 0 : int index = f->name ();
2037 : 0 : gcc_checking_assert (f->in_worklist_p ());
2038 : :
2039 : : /* If map has different function_instance of same name, then
2040 : : this is a duplicated entry which needs to be merged. */
2041 : 0 : if (map_.count (index) && map_[index] != f)
2042 : : {
2043 : 0 : if (dump_file)
2044 : : {
2045 : 0 : fprintf (dump_file, "Merging duplicate instance: ");
2046 : 0 : f->dump_inline_stack (dump_file);
2047 : 0 : fprintf (dump_file, "\n");
2048 : : }
2049 : 0 : map_[index]->merge (f, fns);
2050 : 0 : gcc_checking_assert (!f->inlined_to ());
2051 : 0 : f->clear_in_worklist ();
2052 : 0 : delete f;
2053 : : }
2054 : : /* If name was not seen in the symbol table, remove it. */
2055 : 0 : else if (!seen.contains (index))
2056 : : {
2057 : 0 : f->offline_if_in_set (seen, fns);
2058 : 0 : f->clear_in_worklist ();
2059 : 0 : if (dump_file)
2060 : 0 : fprintf (dump_file, "Removing external %s\n",
2061 : : afdo_string_table->get_name (f->name ()));
2062 : 0 : if (map_.count (index) && map_[index] == f)
2063 : 0 : map_.erase (f->name ());
2064 : 0 : delete f;
2065 : : }
2066 : : /* If this is offline function instance seen in this
2067 : : translation unit offline external inlines and possibly
2068 : : rename from dwarf name. */
2069 : : else
2070 : : {
2071 : 0 : f->remove_external_functions (seen, to_symbol_name, fns);
2072 : 0 : f->clear_in_worklist ();
2073 : : }
2074 : : }
2075 : 0 : if (dump_file)
2076 : 0 : for (auto const &iter : map_)
2077 : : {
2078 : 0 : seen.contains (iter.second->name ());
2079 : 0 : iter.second->dump (dump_file);
2080 : : }
2081 : 0 : }
2082 : :
2083 : : /* Walk scope block BLOCK and mark all inlined functions as realized. */
2084 : :
2085 : : static void
2086 : 0 : walk_block (tree fn, function_instance *s, tree block)
2087 : : {
2088 : 0 : if (inlined_function_outer_scope_p (block))
2089 : : {
2090 : 0 : unsigned loc = get_relative_location_for_locus
2091 : 0 : (fn, BLOCK_SUPERCONTEXT (block),
2092 : 0 : BLOCK_SOURCE_LOCATION (block));
2093 : 0 : function_instance *ns
2094 : : = s->get_function_instance_by_decl
2095 : 0 : (loc, BLOCK_ABSTRACT_ORIGIN (block),
2096 : 0 : BLOCK_SOURCE_LOCATION (block));
2097 : 0 : if (!ns)
2098 : : {
2099 : 0 : if (dump_file)
2100 : : {
2101 : 0 : fprintf (dump_file, " Failed to find inlined instance:");
2102 : 0 : s->dump_inline_stack (dump_file);
2103 : 0 : fprintf (dump_file, ":");
2104 : 0 : dump_afdo_loc (dump_file, loc);
2105 : 0 : fprintf (dump_file, " %s\n",
2106 : 0 : IDENTIFIER_POINTER
2107 : : (DECL_ASSEMBLER_NAME (BLOCK_ABSTRACT_ORIGIN (block))));
2108 : : }
2109 : 0 : return;
2110 : : }
2111 : 0 : s = ns;
2112 : 0 : if (dump_file)
2113 : : {
2114 : 0 : fprintf (dump_file, " Marking realized inline: ");
2115 : 0 : s->dump_inline_stack (dump_file);
2116 : 0 : fprintf (dump_file, "\n");
2117 : : }
2118 : 0 : s->set_realized ();
2119 : : }
2120 : 0 : for (tree t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
2121 : 0 : walk_block (fn, s, t);
2122 : : }
2123 : :
2124 : : /* Offline all inline functions that are not marked as realized.
2125 : : This will merge their profile into offline versions where available.
2126 : : Also remove all functions we will no longer use. */
2127 : :
2128 : : void
2129 : 0 : autofdo_source_profile::offline_unrealized_inlines ()
2130 : : {
2131 : 0 : auto_vec <function_instance *>fns;
2132 : : /* Poppulate worklist with all functions to process. Processing
2133 : : may introduce new functions by offlining. */
2134 : 0 : for (auto const &iter : map_)
2135 : : {
2136 : 0 : fns.safe_push (iter.second);
2137 : 0 : iter.second->set_in_worklist ();
2138 : : }
2139 : 0 : while (fns.length ())
2140 : : {
2141 : 0 : function_instance *f = fns.pop ();
2142 : 0 : int index = f->name ();
2143 : 0 : bool in_map = map_.count (index);
2144 : 0 : if (in_map)
2145 : 0 : if (cgraph_node *n = f->get_cgraph_node ())
2146 : : {
2147 : 0 : if (dump_file)
2148 : 0 : fprintf (dump_file, "Marking realized %s\n",
2149 : : afdo_string_table->get_name (index));
2150 : 0 : f->set_realized ();
2151 : 0 : if (DECL_INITIAL (n->decl)
2152 : 0 : && DECL_INITIAL (n->decl) != error_mark_node)
2153 : 0 : walk_block (n->decl, f, DECL_INITIAL (n->decl));
2154 : : }
2155 : 0 : f->offline_if_not_realized (fns);
2156 : 0 : gcc_checking_assert ((in_map || !f->realized_p ())
2157 : : && f->in_worklist_p ());
2158 : :
2159 : : /* If this is duplicated instance, merge it into one in map. */
2160 : 0 : if (in_map && map_[index] != f)
2161 : : {
2162 : 0 : if (dump_file)
2163 : : {
2164 : 0 : fprintf (dump_file, "Merging duplicate instance: ");
2165 : 0 : f->dump_inline_stack (dump_file);
2166 : 0 : fprintf (dump_file, "\n");
2167 : : }
2168 : 0 : map_[index]->merge (f, fns);
2169 : 0 : f->clear_in_worklist ();
2170 : 0 : gcc_checking_assert (!f->inlined_to ());
2171 : 0 : delete f;
2172 : : }
2173 : : /* If function is not in symbol table, remove it. */
2174 : 0 : else if (!f->realized_p ())
2175 : : {
2176 : 0 : if (dump_file)
2177 : 0 : fprintf (dump_file, "Removing optimized out function %s\n",
2178 : : afdo_string_table->get_name (f->name ()));
2179 : 0 : map_.erase (index);
2180 : 0 : f->clear_in_worklist ();
2181 : 0 : delete f;
2182 : : }
2183 : : else
2184 : 0 : f->clear_in_worklist ();
2185 : : }
2186 : 0 : if (dump_file)
2187 : 0 : for (auto const &iter : map_)
2188 : 0 : iter.second->dump (dump_file);
2189 : 0 : }
2190 : :
2191 : : /* Read the profile and create a function_instance with head count as
2192 : : HEAD_COUNT. Recursively read callsites to create nested function_instances
2193 : : too. STACK is used to track the recursive creation process. */
2194 : :
2195 : : /* function instance profile format:
2196 : :
2197 : : ENTRY_COUNT: 8 bytes
2198 : : NAME_INDEX: 4 bytes
2199 : : NUM_POS_COUNTS: 4 bytes
2200 : : NUM_CALLSITES: 4 byte
2201 : : POS_COUNT_1:
2202 : : POS_1_OFFSET: 4 bytes
2203 : : NUM_TARGETS: 4 bytes
2204 : : COUNT: 8 bytes
2205 : : TARGET_1:
2206 : : VALUE_PROFILE_TYPE: 4 bytes
2207 : : TARGET_IDX: 8 bytes
2208 : : COUNT: 8 bytes
2209 : : TARGET_2
2210 : : ...
2211 : : TARGET_n
2212 : : POS_COUNT_2
2213 : : ...
2214 : : POS_COUNT_N
2215 : : CALLSITE_1:
2216 : : CALLSITE_1_OFFSET: 4 bytes
2217 : : FUNCTION_INSTANCE_PROFILE (nested)
2218 : : CALLSITE_2
2219 : : ...
2220 : : CALLSITE_n. */
2221 : :
2222 : : function_instance *
2223 : 0 : function_instance::read_function_instance (function_instance_stack *stack,
2224 : : gcov_type head_count)
2225 : : {
2226 : 0 : unsigned name = gcov_read_unsigned ();
2227 : 0 : unsigned num_pos_counts = gcov_read_unsigned ();
2228 : 0 : unsigned num_callsites = gcov_read_unsigned ();
2229 : 0 : function_instance *s = new function_instance (name, head_count);
2230 : 0 : if (!stack->is_empty ())
2231 : 0 : s->set_inlined_to (stack->last ());
2232 : 0 : stack->safe_push (s);
2233 : :
2234 : 0 : for (unsigned i = 0; i < num_pos_counts; i++)
2235 : : {
2236 : 0 : unsigned offset = gcov_read_unsigned ();
2237 : 0 : unsigned num_targets = gcov_read_unsigned ();
2238 : 0 : gcov_type count = gcov_read_counter ();
2239 : 0 : s->pos_counts[offset].count = count;
2240 : 0 : afdo_profile_info->sum_max = std::max (afdo_profile_info->sum_max,
2241 : : count);
2242 : :
2243 : 0 : for (unsigned j = 0; j < stack->length (); j++)
2244 : 0 : (*stack)[j]->total_count_ += count;
2245 : 0 : for (unsigned j = 0; j < num_targets; j++)
2246 : : {
2247 : : /* Only indirect call target histogram is supported now. */
2248 : 0 : gcov_read_unsigned ();
2249 : 0 : gcov_type target_idx = gcov_read_counter ();
2250 : 0 : s->pos_counts[offset].targets[target_idx] = gcov_read_counter ();
2251 : : }
2252 : : }
2253 : 0 : for (unsigned i = 0; i < num_callsites; i++)
2254 : : {
2255 : 0 : unsigned offset = gcov_read_unsigned ();
2256 : 0 : function_instance *callee_function_instance
2257 : 0 : = read_function_instance (stack, -1);
2258 : 0 : s->callsites[std::make_pair (offset, callee_function_instance->name ())]
2259 : 0 : = callee_function_instance;
2260 : : }
2261 : 0 : stack->pop ();
2262 : 0 : return s;
2263 : : }
2264 : :
2265 : : /* Member functions for autofdo_source_profile. */
2266 : :
2267 : 0 : autofdo_source_profile::~autofdo_source_profile ()
2268 : : {
2269 : 0 : for (name_function_instance_map::const_iterator iter = map_.begin ();
2270 : 0 : iter != map_.end (); ++iter)
2271 : 0 : delete iter->second;
2272 : 0 : }
2273 : :
2274 : : /* For a given DECL, returns the top-level function_instance. */
2275 : :
2276 : : function_instance *
2277 : 0 : autofdo_source_profile::get_function_instance_by_decl (tree decl) const
2278 : : {
2279 : 0 : int index = afdo_string_table->get_index_by_decl (decl);
2280 : 0 : if (index == -1)
2281 : : return NULL;
2282 : 0 : name_function_instance_map::const_iterator ret = map_.find (index);
2283 : 0 : return ret == map_.end () ? NULL : ret->second;
2284 : : }
2285 : :
2286 : : /* For a given NAME_INDEX, returns the top-level function_instance. */
2287 : :
2288 : : function_instance *
2289 : 0 : autofdo_source_profile::get_function_instance_by_name_index (int name_index)
2290 : : const
2291 : : {
2292 : 0 : name_function_instance_map::const_iterator ret = map_.find (name_index);
2293 : 0 : return ret == map_.end () ? NULL : ret->second;
2294 : : }
2295 : :
2296 : : /* Add function instance FN. */
2297 : :
2298 : : void
2299 : 0 : autofdo_source_profile::add_function_instance (function_instance *fn)
2300 : : {
2301 : 0 : int index = fn->name ();
2302 : 0 : gcc_checking_assert (map_.count (index) == 0);
2303 : 0 : map_[index] = fn;
2304 : 0 : }
2305 : :
2306 : : /* Find count_info for a given gimple STMT. If found, store the count_info
2307 : : in INFO and return true; otherwise return false. */
2308 : :
2309 : : bool
2310 : 0 : autofdo_source_profile::get_count_info (gimple *stmt, count_info *info,
2311 : : cgraph_node *node) const
2312 : : {
2313 : 0 : return get_count_info (gimple_location (stmt), info, node);
2314 : : }
2315 : :
2316 : : bool
2317 : 0 : autofdo_source_profile::get_count_info (location_t gimple_loc,
2318 : : count_info *info,
2319 : : cgraph_node *node) const
2320 : : {
2321 : 0 : if (LOCATION_LOCUS (gimple_loc) == cfun->function_end_locus)
2322 : : return false;
2323 : :
2324 : 0 : inline_stack stack;
2325 : 0 : get_inline_stack_in_node (gimple_loc, &stack, node);
2326 : 0 : if (stack.length () == 0)
2327 : : return false;
2328 : 0 : function_instance *s = get_function_instance_by_inline_stack (stack);
2329 : 0 : if (s == NULL)
2330 : : return false;
2331 : 0 : return s->get_count_info (stack[0].afdo_loc, info);
2332 : 0 : }
2333 : :
2334 : : /* Update value profile INFO for STMT from the inlined indirect callsite.
2335 : : Return true if INFO is updated. */
2336 : :
2337 : : bool
2338 : 0 : autofdo_source_profile::update_inlined_ind_target (gcall *stmt,
2339 : : count_info *info,
2340 : : cgraph_node *node)
2341 : : {
2342 : 0 : if (dump_file)
2343 : : {
2344 : 0 : fprintf (dump_file, "Checking indirect call -> direct call ");
2345 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2346 : : }
2347 : :
2348 : 0 : if (LOCATION_LOCUS (gimple_location (stmt)) == cfun->function_end_locus)
2349 : : {
2350 : 0 : if (dump_file)
2351 : 0 : fprintf (dump_file, " bad locus (funciton end)\n");
2352 : 0 : return false;
2353 : : }
2354 : :
2355 : 0 : count_info old_info;
2356 : 0 : get_count_info (stmt, &old_info, node);
2357 : 0 : gcov_type total = 0;
2358 : 0 : for (icall_target_map::const_iterator iter = old_info.targets.begin ();
2359 : 0 : iter != old_info.targets.end (); ++iter)
2360 : 0 : total += iter->second;
2361 : 0 : total *= afdo_count_scale;
2362 : :
2363 : : /* Program behavior changed, original promoted (and inlined) target is not
2364 : : hot any more. Will avoid promote the original target.
2365 : :
2366 : : To check if original promoted target is still hot, we check the total
2367 : : count of the unpromoted targets (stored in TOTAL). If a callsite count
2368 : : (stored in INFO) is smaller than half of the total count, the original
2369 : : promoted target is considered not hot any more. */
2370 : 0 : if (info->count < total / 2)
2371 : : {
2372 : 0 : if (dump_file)
2373 : 0 : fprintf (dump_file, " not hot anymore %ld < %ld",
2374 : : (long)info->count,
2375 : : (long)total /2);
2376 : 0 : return false;
2377 : : }
2378 : :
2379 : 0 : inline_stack stack;
2380 : 0 : get_inline_stack_in_node (gimple_location (stmt), &stack, node);
2381 : 0 : if (stack.length () == 0)
2382 : : {
2383 : 0 : if (dump_file)
2384 : 0 : fprintf (dump_file, " no inline stack\n");
2385 : 0 : return false;
2386 : : }
2387 : 0 : function_instance *s = get_function_instance_by_inline_stack (stack);
2388 : 0 : if (s == NULL)
2389 : : {
2390 : 0 : if (dump_file)
2391 : : {
2392 : 0 : fprintf (dump_file, " function not found in inline stack:");
2393 : 0 : dump_inline_stack (dump_file, &stack);
2394 : : }
2395 : 0 : return false;
2396 : : }
2397 : 0 : icall_target_map map;
2398 : 0 : if (s->find_icall_target_map (node ? node->decl
2399 : : : current_function_decl,
2400 : : stmt, &map) == 0)
2401 : : {
2402 : 0 : if (dump_file)
2403 : : {
2404 : 0 : fprintf (dump_file, " no target map for stack: ");
2405 : 0 : dump_inline_stack (dump_file, &stack);
2406 : : }
2407 : 0 : return false;
2408 : : }
2409 : 0 : for (icall_target_map::const_iterator iter = map.begin ();
2410 : 0 : iter != map.end (); ++iter)
2411 : 0 : info->targets[iter->first] = iter->second;
2412 : 0 : if (dump_file)
2413 : : {
2414 : 0 : fprintf (dump_file, " looks good; stack:");
2415 : 0 : dump_inline_stack (dump_file, &stack);
2416 : : }
2417 : : return true;
2418 : 0 : }
2419 : :
2420 : : void
2421 : 0 : autofdo_source_profile::remove_icall_target (cgraph_edge *e)
2422 : : {
2423 : 0 : autofdo::inline_stack stack;
2424 : 0 : autofdo::get_inline_stack_in_node (gimple_location (e->call_stmt),
2425 : : &stack, e->caller);
2426 : 0 : autofdo::function_instance *s
2427 : 0 : = get_function_instance_by_inline_stack (stack);
2428 : 0 : s->remove_icall_target (e->caller->decl, e->call_stmt);
2429 : 0 : }
2430 : :
2431 : : /* Find total count of the callee of EDGE. */
2432 : :
2433 : : gcov_type
2434 : 0 : autofdo_source_profile::get_callsite_total_count (
2435 : : struct cgraph_edge *edge) const
2436 : : {
2437 : 0 : inline_stack stack;
2438 : 0 : stack.safe_push ({edge->callee->decl, 0, UNKNOWN_LOCATION});
2439 : :
2440 : 0 : get_inline_stack_in_node (gimple_location (edge->call_stmt), &stack,
2441 : : edge->caller);
2442 : 0 : if (dump_file)
2443 : : {
2444 : 0 : if (!edge->caller->inlined_to)
2445 : 0 : fprintf (dump_file, "Looking up afdo profile for call %s -> %s stack:",
2446 : 0 : edge->caller->dump_name (), edge->callee->dump_name ());
2447 : : else
2448 : 0 : fprintf (dump_file, "Looking up afdo profile for call %s -> %s transitively %s stack:",
2449 : 0 : edge->caller->dump_name (), edge->callee->dump_name (),
2450 : : edge->caller->inlined_to->dump_name ());
2451 : 0 : dump_inline_stack (dump_file, &stack);
2452 : : }
2453 : :
2454 : 0 : function_instance *s = get_function_instance_by_inline_stack (stack);
2455 : 0 : if (s == NULL)
2456 : : {
2457 : 0 : if (dump_file)
2458 : 0 : fprintf (dump_file, "No function instance found\n");
2459 : 0 : return 0;
2460 : : }
2461 : 0 : if (afdo_string_table->get_index_by_decl (edge->callee->decl) != s->name ())
2462 : : {
2463 : 0 : if (dump_file)
2464 : 0 : fprintf (dump_file, "Mismatched name of callee %s and profile %s\n",
2465 : 0 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (edge->callee->decl)),
2466 : : afdo_string_table->get_name (s->name ()));
2467 : 0 : return 0;
2468 : : }
2469 : :
2470 : 0 : return s->total_count () * afdo_count_scale;
2471 : 0 : }
2472 : :
2473 : : /* Read AutoFDO profile and returns TRUE on success. */
2474 : :
2475 : : /* source profile format:
2476 : :
2477 : : GCOV_TAG_AFDO_FUNCTION: 4 bytes
2478 : : LENGTH: 4 bytes
2479 : : NUM_FUNCTIONS: 4 bytes
2480 : : FUNCTION_INSTANCE_1
2481 : : FUNCTION_INSTANCE_2
2482 : : ...
2483 : : FUNCTION_INSTANCE_N. */
2484 : :
2485 : : bool
2486 : 0 : autofdo_source_profile::read ()
2487 : : {
2488 : 0 : if (gcov_read_unsigned () != GCOV_TAG_AFDO_FUNCTION)
2489 : : {
2490 : 0 : inform (UNKNOWN_LOCATION, "Not expected TAG.");
2491 : 0 : return false;
2492 : : }
2493 : :
2494 : 0 : gcc_checking_assert (!afdo_source_profile);
2495 : 0 : afdo_source_profile = this;
2496 : :
2497 : : /* Skip the length of the section. */
2498 : 0 : gcov_read_unsigned ();
2499 : :
2500 : : /* Read in the function/callsite profile, and store it in local
2501 : : data structure. */
2502 : 0 : unsigned function_num = gcov_read_unsigned ();
2503 : 0 : for (unsigned i = 0; i < function_num; i++)
2504 : : {
2505 : 0 : function_instance::function_instance_stack stack;
2506 : 0 : function_instance *s = function_instance::read_function_instance (
2507 : : &stack, gcov_read_counter ());
2508 : 0 : int fun_id = s->name ();
2509 : : /* If function_instace with get_original_name (without the clone
2510 : : suffix) exixts, merge the function instances. */
2511 : 0 : if (map_.count (fun_id) == 0)
2512 : 0 : map_[fun_id] = s;
2513 : : else
2514 : 0 : fatal_error (UNKNOWN_LOCATION,
2515 : : "auto-profile contains duplicated function instance %s",
2516 : : afdo_string_table->get_name (s->name ()));
2517 : 0 : }
2518 : 0 : int hot_frac = param_hot_bb_count_fraction;
2519 : : /* Scale up the profile, but leave some bits in case some counts gets
2520 : : bigger than sum_max eventually. */
2521 : 0 : if (afdo_profile_info->sum_max)
2522 : 0 : afdo_count_scale
2523 : 0 : = MAX (((gcov_type)1 << (profile_count::n_bits / 2))
2524 : : / afdo_profile_info->sum_max, 1);
2525 : 0 : afdo_hot_bb_threshod
2526 : 0 : = hot_frac
2527 : 0 : ? afdo_profile_info->sum_max * afdo_count_scale / hot_frac
2528 : : : (gcov_type)profile_count::max_count;
2529 : 0 : set_hot_bb_threshold (afdo_hot_bb_threshod);
2530 : 0 : if (dump_file)
2531 : 0 : fprintf (dump_file, "Max count in profile %" PRIu64 "\n"
2532 : : "Setting scale %" PRIu64 "\n"
2533 : : "Scaled max count %" PRIu64 "\n"
2534 : : "Hot count threshold %" PRIu64 "\n\n",
2535 : : (int64_t)afdo_profile_info->sum_max,
2536 : : (int64_t)afdo_count_scale,
2537 : 0 : (int64_t)(afdo_profile_info->sum_max * afdo_count_scale),
2538 : : (int64_t)afdo_hot_bb_threshod);
2539 : 0 : afdo_profile_info->sum_max *= afdo_count_scale;
2540 : 0 : return true;
2541 : : }
2542 : :
2543 : : /* Return the function_instance in the profile that correspond to the
2544 : : inline STACK. */
2545 : :
2546 : : function_instance *
2547 : 0 : autofdo_source_profile::get_function_instance_by_inline_stack (
2548 : : const inline_stack &stack) const
2549 : : {
2550 : 0 : name_function_instance_map::const_iterator iter = map_.find (
2551 : 0 : afdo_string_table->get_index_by_decl (stack[stack.length () - 1].decl));
2552 : 0 : if (iter == map_.end ())
2553 : : {
2554 : 0 : if (dump_file)
2555 : 0 : fprintf (dump_file, "No offline instance for %s\n",
2556 : 0 : IDENTIFIER_POINTER
2557 : : (DECL_ASSEMBLER_NAME (stack[stack.length () - 1].decl)));
2558 : 0 : return NULL;
2559 : : }
2560 : 0 : function_instance *s = iter->second;
2561 : 0 : for (unsigned i = stack.length () - 1; i > 0; i--)
2562 : : {
2563 : 0 : s = s->get_function_instance_by_decl (stack[i].afdo_loc,
2564 : 0 : stack[i - 1].decl,
2565 : 0 : stack[i].location);
2566 : 0 : if (s == NULL)
2567 : : {
2568 : : /* afdo inliner extends the stack by last entry with unknown
2569 : : location while chekcing if function was inlined during train run.
2570 : : We do not want to print diagnostics about every function
2571 : : which is not inlined. */
2572 : : if (s && dump_enabled_p () && stack[i].location != UNKNOWN_LOCATION)
2573 : : dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS,
2574 : : dump_user_location_t::from_location_t
2575 : : (stack[i].location),
2576 : : "auto-profile has no inlined function instance "
2577 : : "for inlined call of %s at relative "
2578 : : " locaction +%i, discriminator %i\n",
2579 : : IDENTIFIER_POINTER
2580 : : (DECL_ASSEMBLER_NAME (stack[i - 1].decl)),
2581 : : stack[i].afdo_loc >> 16,
2582 : : stack[i].afdo_loc & 65535);
2583 : : return NULL;
2584 : : }
2585 : : }
2586 : : return s;
2587 : : }
2588 : :
2589 : : /* Module profile is only used by LIPO. Here we simply ignore it. */
2590 : :
2591 : : static void
2592 : 0 : fake_read_autofdo_module_profile ()
2593 : : {
2594 : : /* Read in the module info. */
2595 : 0 : gcov_read_unsigned ();
2596 : :
2597 : : /* Skip the length of the section. */
2598 : 0 : gcov_read_unsigned ();
2599 : :
2600 : : /* Read in the file name table. */
2601 : 0 : unsigned total_module_num = gcov_read_unsigned ();
2602 : 0 : gcc_assert (total_module_num == 0);
2603 : 0 : }
2604 : :
2605 : : /* Read data from profile data file. */
2606 : :
2607 : : static void
2608 : 0 : read_profile (void)
2609 : : {
2610 : 0 : if (gcov_open (auto_profile_file, 1) == 0)
2611 : : {
2612 : 0 : error ("cannot open profile file %s", auto_profile_file);
2613 : 0 : return;
2614 : : }
2615 : :
2616 : 0 : if (gcov_read_unsigned () != GCOV_DATA_MAGIC)
2617 : : {
2618 : 0 : error ("AutoFDO profile magic number does not match");
2619 : 0 : return;
2620 : : }
2621 : :
2622 : : /* Skip the version number. */
2623 : 0 : unsigned version = gcov_read_unsigned ();
2624 : 0 : if (version != AUTO_PROFILE_VERSION)
2625 : : {
2626 : 0 : error ("AutoFDO profile version %u does not match %u",
2627 : : version, AUTO_PROFILE_VERSION);
2628 : 0 : return;
2629 : : }
2630 : :
2631 : : /* Skip the empty integer. */
2632 : 0 : gcov_read_unsigned ();
2633 : :
2634 : : /* string_table. */
2635 : 0 : afdo_string_table = new string_table ();
2636 : 0 : if (!afdo_string_table->read ())
2637 : : {
2638 : 0 : error ("cannot read string table from %s", auto_profile_file);
2639 : 0 : return;
2640 : : }
2641 : :
2642 : : /* autofdo_source_profile. */
2643 : 0 : afdo_source_profile = autofdo_source_profile::create ();
2644 : 0 : if (afdo_source_profile == NULL)
2645 : : {
2646 : 0 : error ("cannot read function profile from %s", auto_profile_file);
2647 : 0 : return;
2648 : : }
2649 : :
2650 : : /* autofdo_module_profile. */
2651 : 0 : fake_read_autofdo_module_profile ();
2652 : : }
2653 : :
2654 : : /* From AutoFDO profiles, find values inside STMT for that we want to measure
2655 : : histograms for indirect-call optimization.
2656 : :
2657 : : This function is actually served for 2 purposes:
2658 : : * before annotation, we need to mark histogram, promote and inline
2659 : : * after annotation, we just need to mark, and let follow-up logic to
2660 : : decide if it needs to promote and inline. */
2661 : :
2662 : : static bool
2663 : 0 : afdo_indirect_call (gcall *stmt, const icall_target_map &map,
2664 : : bool transform, cgraph_edge *indirect_edge)
2665 : : {
2666 : 0 : tree callee;
2667 : :
2668 : 0 : if (map.size () == 0)
2669 : : {
2670 : 0 : if (dump_file)
2671 : 0 : fprintf (dump_file, "No targets found\n");
2672 : 0 : return false;
2673 : : }
2674 : 0 : if (!stmt)
2675 : : {
2676 : 0 : if (dump_file)
2677 : 0 : fprintf (dump_file, "No call statement\n");
2678 : 0 : return false;
2679 : : }
2680 : 0 : if (gimple_call_internal_p (stmt))
2681 : : {
2682 : 0 : if (dump_file)
2683 : 0 : fprintf (dump_file, "Internal call\n");
2684 : 0 : return false;
2685 : : }
2686 : 0 : if (gimple_call_fndecl (stmt) != NULL_TREE)
2687 : : {
2688 : 0 : if (dump_file)
2689 : 0 : fprintf (dump_file, "Call is already direct\n");
2690 : 0 : return false;
2691 : : }
2692 : :
2693 : 0 : gcov_type total = 0;
2694 : 0 : icall_target_map::const_iterator max_iter = map.end ();
2695 : :
2696 : 0 : for (icall_target_map::const_iterator iter = map.begin ();
2697 : 0 : iter != map.end (); ++iter)
2698 : : {
2699 : 0 : total += iter->second;
2700 : 0 : if (max_iter == map.end () || max_iter->second < iter->second)
2701 : : max_iter = iter;
2702 : : }
2703 : 0 : total *= afdo_count_scale;
2704 : 0 : struct cgraph_node *direct_call = cgraph_node::get_for_asmname (
2705 : 0 : get_identifier (afdo_string_table->get_name (max_iter->first)));
2706 : 0 : if (direct_call == NULL)
2707 : : {
2708 : 0 : if (dump_file)
2709 : 0 : fprintf (dump_file, "Failed to find cgraph node for %s\n",
2710 : 0 : afdo_string_table->get_name (max_iter->first));
2711 : 0 : return false;
2712 : : }
2713 : :
2714 : 0 : callee = gimple_call_fn (stmt);
2715 : :
2716 : 0 : if (!transform)
2717 : : {
2718 : 0 : if (!direct_call->profile_id)
2719 : : {
2720 : 0 : if (dump_file)
2721 : 0 : fprintf (dump_file, "No profile id\n");
2722 : 0 : return false;
2723 : : }
2724 : 0 : histogram_value hist = gimple_alloc_histogram_value (
2725 : : cfun, HIST_TYPE_INDIR_CALL, stmt, callee);
2726 : 0 : hist->n_counters = 4;
2727 : 0 : hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
2728 : 0 : gimple_add_histogram_value (cfun, stmt, hist);
2729 : :
2730 : : /* Total counter */
2731 : 0 : hist->hvalue.counters[0] = total;
2732 : : /* Number of value/counter pairs */
2733 : 0 : hist->hvalue.counters[1] = 1;
2734 : : /* Value */
2735 : 0 : hist->hvalue.counters[2] = direct_call->profile_id;
2736 : : /* Counter */
2737 : 0 : hist->hvalue.counters[3] = max_iter->second * afdo_count_scale;
2738 : :
2739 : 0 : if (!direct_call->profile_id)
2740 : : {
2741 : 0 : if (dump_file)
2742 : 0 : fprintf (dump_file, "Histogram attached\n");
2743 : 0 : return false;
2744 : : }
2745 : : return false;
2746 : : }
2747 : :
2748 : 0 : if (dump_file)
2749 : : {
2750 : 0 : fprintf (dump_file, "Indirect call -> direct call ");
2751 : 0 : print_generic_expr (dump_file, callee, TDF_SLIM);
2752 : 0 : fprintf (dump_file, " => ");
2753 : 0 : print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
2754 : : }
2755 : :
2756 : 0 : if (!direct_call->definition)
2757 : : {
2758 : 0 : if (dump_file)
2759 : 0 : fprintf (dump_file, " no definition available\n");
2760 : 0 : return false;
2761 : : }
2762 : :
2763 : 0 : if (dump_file)
2764 : : {
2765 : 0 : fprintf (dump_file, " transformation on insn ");
2766 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2767 : 0 : fprintf (dump_file, "\n");
2768 : : }
2769 : :
2770 : 0 : indirect_edge->make_speculative
2771 : 0 : (direct_call,
2772 : 0 : gimple_bb (stmt)->count.apply_scale (99, 100));
2773 : 0 : return true;
2774 : : }
2775 : :
2776 : : /* From AutoFDO profiles, find values inside STMT for that we want to measure
2777 : : histograms and adds them to list VALUES. */
2778 : :
2779 : : static bool
2780 : 0 : afdo_vpt (gcall *gs, const icall_target_map &map,
2781 : : bool transform, cgraph_edge *indirect_edge)
2782 : : {
2783 : 0 : return afdo_indirect_call (gs, map, transform, indirect_edge);
2784 : : }
2785 : :
2786 : : typedef std::set<basic_block> bb_set;
2787 : :
2788 : : static bool
2789 : 0 : is_bb_annotated (const basic_block bb, const bb_set &annotated)
2790 : : {
2791 : 0 : if (annotated.find (bb) != annotated.end ())
2792 : : {
2793 : 0 : gcc_checking_assert (bb->count.quality () == AFDO
2794 : : || !bb->count.nonzero_p ());
2795 : : return true;
2796 : : }
2797 : 0 : gcc_checking_assert (bb->count.quality () != AFDO
2798 : : || !bb->count.nonzero_p ());
2799 : : return false;
2800 : : }
2801 : :
2802 : : static void
2803 : 0 : set_bb_annotated (basic_block bb, bb_set *annotated)
2804 : : {
2805 : 0 : gcc_checking_assert (bb->count.quality () == AFDO
2806 : : || !bb->count.nonzero_p ());
2807 : 0 : annotated->insert (bb);
2808 : 0 : }
2809 : :
2810 : : /* Update COUNT by known autofdo count C. */
2811 : : static void
2812 : 0 : update_count_by_afdo_count (profile_count *count, gcov_type c)
2813 : : {
2814 : 0 : if (c)
2815 : 0 : *count = profile_count::from_gcov_type (c).afdo ();
2816 : : /* In case we have guessed profile which is already zero, preserve
2817 : : quality info. */
2818 : 0 : else if (count->nonzero_p ()
2819 : 0 : || count->quality () == GUESSED
2820 : 0 : || count->quality () == GUESSED_LOCAL)
2821 : 0 : *count = profile_count::zero ().afdo ();
2822 : 0 : }
2823 : :
2824 : : /* Update COUNT by known autofdo count C. */
2825 : : static void
2826 : 0 : update_count_by_afdo_count (profile_count *count, profile_count c)
2827 : : {
2828 : 0 : if (c.nonzero_p ())
2829 : 0 : *count = c;
2830 : : /* In case we have guessed profile which is already zero, preserve
2831 : : quality info. */
2832 : 0 : else if (count->nonzero_p ()
2833 : 0 : || count->quality () < c.quality ())
2834 : 0 : *count = c;
2835 : 0 : }
2836 : :
2837 : : /* For a given BB, set its execution count. Attach value profile if a stmt
2838 : : is not in PROMOTED, because we only want to promote an indirect call once.
2839 : : Return TRUE if BB is annotated. */
2840 : :
2841 : : static bool
2842 : 0 : afdo_set_bb_count (basic_block bb, hash_set <basic_block> &zero_bbs)
2843 : : {
2844 : 0 : gimple_stmt_iterator gsi;
2845 : 0 : gcov_type max_count = 0;
2846 : 0 : bool has_annotated = false;
2847 : 0 : if (dump_file)
2848 : 0 : fprintf (dump_file, " Looking up AFDO count of bb %i\n", bb->index);
2849 : :
2850 : 0 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2851 : : {
2852 : 0 : count_info info;
2853 : 0 : gimple *stmt = gsi_stmt (gsi);
2854 : 0 : if (gimple_clobber_p (stmt))
2855 : 0 : continue;
2856 : 0 : if (afdo_source_profile->get_count_info (stmt, &info))
2857 : : {
2858 : 0 : if (info.count > max_count)
2859 : : max_count = info.count;
2860 : 0 : if (dump_file)
2861 : : {
2862 : 0 : fprintf (dump_file, " count %" PRIu64 " in stmt: ",
2863 : : (int64_t)info.count);
2864 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2865 : : }
2866 : 0 : has_annotated = true;
2867 : 0 : gcall *call = dyn_cast <gcall *> (gsi_stmt (gsi));
2868 : : /* TODO; if inlined early and indirect call was not optimized out,
2869 : : we will end up speculating again. Early inliner should remove
2870 : : all targets for edges it speculated into safely. */
2871 : 0 : if (call
2872 : 0 : && info.targets.size () > 0)
2873 : 0 : afdo_vpt (call, info.targets, false, NULL);
2874 : : }
2875 : 0 : }
2876 : :
2877 : 0 : if (!has_annotated)
2878 : : {
2879 : : /* For an empty BB with all debug stmt which assigne a value with
2880 : : constant, check successors PHIs corresponding to the block and
2881 : : use those counts. */
2882 : 0 : edge tmp_e;
2883 : 0 : edge_iterator tmp_ei;
2884 : 0 : FOR_EACH_EDGE (tmp_e, tmp_ei, bb->succs)
2885 : : {
2886 : 0 : basic_block bb_succ = tmp_e->dest;
2887 : 0 : for (gphi_iterator gpi = gsi_start_phis (bb_succ);
2888 : 0 : !gsi_end_p (gpi);
2889 : 0 : gsi_next (&gpi))
2890 : : {
2891 : 0 : gphi *phi = gpi.phi ();
2892 : 0 : location_t phi_loc
2893 : 0 : = gimple_phi_arg_location_from_edge (phi, tmp_e);
2894 : 0 : count_info info;
2895 : 0 : if (afdo_source_profile->get_count_info (phi_loc, &info)
2896 : 0 : && info.count != 0)
2897 : : {
2898 : 0 : if (info.count > max_count)
2899 : : max_count = info.count;
2900 : 0 : if (dump_file && info.count)
2901 : : {
2902 : 0 : fprintf (dump_file,
2903 : : " phi op in BB %i with count %" PRIu64": ",
2904 : : bb_succ->index, (int64_t)info.count);
2905 : 0 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
2906 : : }
2907 : : has_annotated = true;
2908 : : }
2909 : 0 : }
2910 : : }
2911 : :
2912 : 0 : if (!has_annotated)
2913 : 0 : return false;
2914 : : }
2915 : :
2916 : 0 : if (max_count)
2917 : : {
2918 : 0 : update_count_by_afdo_count (&bb->count, max_count * afdo_count_scale);
2919 : 0 : if (dump_file)
2920 : 0 : fprintf (dump_file,
2921 : : " Annotated bb %i with count %" PRId64
2922 : : ", scaled to %" PRId64 "\n",
2923 : : bb->index, (int64_t)max_count,
2924 : 0 : (int64_t)(max_count * afdo_count_scale));
2925 : 0 : return true;
2926 : : }
2927 : : else
2928 : : {
2929 : 0 : if (dump_file)
2930 : 0 : fprintf (dump_file,
2931 : : " bb %i has statements with 0 count\n", bb->index);
2932 : 0 : zero_bbs.add (bb);
2933 : : }
2934 : 0 : return false;
2935 : : }
2936 : :
2937 : : /* BB1 and BB2 are in an equivalent class iff:
2938 : : 1. BB1 dominates BB2.
2939 : : 2. BB2 post-dominates BB1.
2940 : : 3. BB1 and BB2 are in the same loop nest.
2941 : : This function finds the equivalent class for each basic block, and
2942 : : stores a pointer to the first BB in its equivalent class. Meanwhile,
2943 : : set bb counts for the same equivalent class to be idenical. Update
2944 : : ANNOTATED_BB for the first BB in its equivalent class. */
2945 : :
2946 : : static void
2947 : 0 : afdo_find_equiv_class (bb_set *annotated_bb)
2948 : : {
2949 : 0 : basic_block bb;
2950 : :
2951 : 0 : FOR_ALL_BB_FN (bb, cfun)
2952 : 0 : bb->aux = NULL;
2953 : :
2954 : 0 : FOR_ALL_BB_FN (bb, cfun)
2955 : : {
2956 : 0 : if (bb->aux != NULL)
2957 : 0 : continue;
2958 : 0 : bb->aux = bb;
2959 : 0 : for (basic_block bb1 : get_dominated_by (CDI_DOMINATORS, bb))
2960 : 0 : if (bb1->aux == NULL && dominated_by_p (CDI_POST_DOMINATORS, bb, bb1)
2961 : 0 : && bb1->loop_father == bb->loop_father)
2962 : : {
2963 : 0 : bb1->aux = bb;
2964 : 0 : if (is_bb_annotated (bb1, *annotated_bb)
2965 : 0 : && (!is_bb_annotated (bb, *annotated_bb)
2966 : 0 : || bb1->count > bb->count))
2967 : : {
2968 : 0 : if (dump_file)
2969 : : {
2970 : 0 : fprintf (dump_file,
2971 : : " Copying count of bb %i to bb %i; count is:",
2972 : : bb1->index,
2973 : : bb->index);
2974 : 0 : bb1->count.dump (dump_file);
2975 : 0 : fprintf (dump_file, "\n");
2976 : : }
2977 : 0 : update_count_by_afdo_count (&bb->count, bb1->count);
2978 : 0 : set_bb_annotated (bb, annotated_bb);
2979 : : }
2980 : 0 : }
2981 : :
2982 : 0 : for (basic_block bb1 : get_dominated_by (CDI_POST_DOMINATORS, bb))
2983 : 0 : if (bb1->aux == NULL && dominated_by_p (CDI_DOMINATORS, bb, bb1)
2984 : 0 : && bb1->loop_father == bb->loop_father)
2985 : : {
2986 : 0 : bb1->aux = bb;
2987 : 0 : if (is_bb_annotated (bb1, *annotated_bb)
2988 : 0 : && (!is_bb_annotated (bb, *annotated_bb)
2989 : 0 : || bb1->count > bb->count))
2990 : : {
2991 : 0 : if (dump_file)
2992 : : {
2993 : 0 : fprintf (dump_file,
2994 : : " Copying count of bb %i to bb %i; count is:",
2995 : : bb1->index,
2996 : : bb->index);
2997 : 0 : bb1->count.dump (dump_file);
2998 : 0 : fprintf (dump_file, "\n");
2999 : : }
3000 : 0 : update_count_by_afdo_count (&bb->count, bb1->count);
3001 : 0 : set_bb_annotated (bb, annotated_bb);
3002 : : }
3003 : 0 : }
3004 : : }
3005 : 0 : }
3006 : :
3007 : : /* If a basic block's count is known, and only one of its in/out edges' count
3008 : : is unknown, its count can be calculated. Meanwhile, if all of the in/out
3009 : : edges' counts are known, then the basic block's unknown count can also be
3010 : : calculated. Also, if a block has a single predecessor or successor, the block's
3011 : : count can be propagated to that predecessor or successor.
3012 : : IS_SUCC is true if out edges of a basic blocks are examined.
3013 : : Update ANNOTATED_BB accordingly.
3014 : : Return TRUE if any basic block/edge count is changed. */
3015 : :
3016 : : static bool
3017 : 0 : afdo_propagate_edge (bool is_succ, bb_set *annotated_bb)
3018 : : {
3019 : 0 : basic_block bb;
3020 : 0 : bool changed = false;
3021 : :
3022 : 0 : FOR_EACH_BB_FN (bb, cfun)
3023 : : {
3024 : 0 : edge e, unknown_edge = NULL;
3025 : 0 : edge_iterator ei;
3026 : 0 : int num_unknown_edges = 0;
3027 : 0 : int num_edges = 0;
3028 : 0 : profile_count total_known_count = profile_count::zero ().afdo ();
3029 : :
3030 : 0 : FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
3031 : : {
3032 : 0 : gcc_assert (AFDO_EINFO (e) != NULL);
3033 : 0 : if (! AFDO_EINFO (e)->is_annotated ())
3034 : 0 : num_unknown_edges++, unknown_edge = e;
3035 : : else
3036 : 0 : total_known_count += AFDO_EINFO (e)->get_count ();
3037 : 0 : num_edges++;
3038 : : }
3039 : 0 : if (dump_file)
3040 : : {
3041 : 0 : fprintf (dump_file, "bb %i %s propagating %s edges %i, "
3042 : : "unknown edges %i, known count ",
3043 : : bb->index,
3044 : 0 : is_bb_annotated (bb, *annotated_bb) ? "(annotated)" : "",
3045 : : is_succ ? "succesors" : "predecessors", num_edges,
3046 : : num_unknown_edges);
3047 : 0 : total_known_count.dump (dump_file);
3048 : 0 : fprintf (dump_file, " bb count ");
3049 : 0 : bb->count.dump (dump_file);
3050 : 0 : fprintf (dump_file, "\n");
3051 : : }
3052 : :
3053 : : /* Be careful not to annotate block with no successor in special cases. */
3054 : 0 : if (num_unknown_edges == 0 && num_edges
3055 : 0 : && !is_bb_annotated (bb, *annotated_bb))
3056 : : {
3057 : 0 : if (dump_file)
3058 : : {
3059 : 0 : fprintf (dump_file, " Annotating bb %i with count ", bb->index);
3060 : 0 : total_known_count.dump (dump_file);
3061 : 0 : fprintf (dump_file, "\n");
3062 : : }
3063 : 0 : update_count_by_afdo_count (&bb->count, total_known_count);
3064 : 0 : set_bb_annotated (bb, annotated_bb);
3065 : 0 : changed = true;
3066 : : }
3067 : 0 : else if (is_bb_annotated (bb, *annotated_bb)
3068 : 0 : && bb->count < total_known_count)
3069 : : {
3070 : 0 : if (dump_file)
3071 : : {
3072 : 0 : fprintf (dump_file, " Increasing bb %i count from ",
3073 : : bb->index);
3074 : 0 : bb->count.dump (dump_file);
3075 : 0 : fprintf (dump_file, " to ");
3076 : 0 : total_known_count.dump (dump_file);
3077 : 0 : fprintf (dump_file, " hoping to mitigate afdo inconsistency\n");
3078 : : }
3079 : 0 : bb->count = total_known_count;
3080 : 0 : changed = true;
3081 : : }
3082 : 0 : else if (num_unknown_edges == 1 && is_bb_annotated (bb, *annotated_bb))
3083 : : {
3084 : 0 : if (bb->count > total_known_count)
3085 : : {
3086 : 0 : profile_count new_count = bb->count - total_known_count;
3087 : 0 : AFDO_EINFO (unknown_edge)->set_count (new_count);
3088 : : }
3089 : : else
3090 : 0 : AFDO_EINFO (unknown_edge)->set_count
3091 : 0 : (profile_count::zero ().afdo ());
3092 : 0 : if (dump_file)
3093 : : {
3094 : 0 : fprintf (dump_file, " Annotated edge %i->%i with count ",
3095 : 0 : unknown_edge->src->index, unknown_edge->dest->index);
3096 : 0 : AFDO_EINFO (unknown_edge)->get_count ().dump (dump_file);
3097 : 0 : fprintf (dump_file, "\n");
3098 : : }
3099 : 0 : AFDO_EINFO (unknown_edge)->set_annotated ();
3100 : 0 : changed = true;
3101 : : }
3102 : 0 : else if (num_unknown_edges > 1
3103 : 0 : && is_bb_annotated (bb, *annotated_bb)
3104 : 0 : && (total_known_count >= bb->count || !bb->count.nonzero_p ()))
3105 : : {
3106 : 0 : FOR_EACH_EDGE (e, ei, is_succ ? bb->succs : bb->preds)
3107 : : {
3108 : 0 : gcc_assert (AFDO_EINFO (e) != NULL);
3109 : 0 : if (! AFDO_EINFO (e)->is_annotated ())
3110 : : {
3111 : 0 : AFDO_EINFO (e)->set_count
3112 : 0 : (profile_count::zero ().afdo ());
3113 : 0 : AFDO_EINFO (e)->set_annotated ();
3114 : 0 : if (dump_file)
3115 : : {
3116 : 0 : fprintf (dump_file, " Annotated edge %i->%i with count ",
3117 : 0 : e->src->index, e->dest->index);
3118 : 0 : AFDO_EINFO (unknown_edge)->get_count ().dump (dump_file);
3119 : 0 : fprintf (dump_file, "\n");
3120 : : }
3121 : : }
3122 : : }
3123 : : }
3124 : 0 : else if (num_unknown_edges == 0
3125 : 0 : && is_bb_annotated (bb, *annotated_bb)
3126 : 0 : && (is_succ ? single_succ_p (bb) : single_pred_p (bb)))
3127 : : {
3128 : 0 : edge e = is_succ ? single_succ_edge (bb) : single_pred_edge (bb);
3129 : 0 : if (AFDO_EINFO (e)->is_annotated ()
3130 : 0 : && AFDO_EINFO (e)->get_count () < bb->count)
3131 : : {
3132 : 0 : if (dump_file)
3133 : : {
3134 : 0 : fprintf (dump_file, " Increasing edge %i->%i count from ",
3135 : 0 : e->src->index, e->dest->index);
3136 : 0 : AFDO_EINFO (e)->get_count ().dump (dump_file);
3137 : 0 : fprintf (dump_file, " to ");
3138 : 0 : bb->count.dump (dump_file);
3139 : 0 : fprintf (dump_file, " hoping to mitigate afdo inconsistency\n");
3140 : : }
3141 : 0 : AFDO_EINFO (e)->set_count (bb->count);
3142 : 0 : changed = true;
3143 : : }
3144 : : }
3145 : : }
3146 : 0 : return changed;
3147 : : }
3148 : :
3149 : : /* Special propagation for circuit expressions. Because GCC translates
3150 : : control flow into data flow for circuit expressions. E.g.
3151 : : BB1:
3152 : : if (a && b)
3153 : : BB2
3154 : : else
3155 : : BB3
3156 : :
3157 : : will be translated into:
3158 : :
3159 : : BB1:
3160 : : if (a)
3161 : : goto BB.t1
3162 : : else
3163 : : goto BB.t3
3164 : : BB.t1:
3165 : : if (b)
3166 : : goto BB.t2
3167 : : else
3168 : : goto BB.t3
3169 : : BB.t2:
3170 : : goto BB.t3
3171 : : BB.t3:
3172 : : tmp = PHI (0 (BB1), 0 (BB.t1), 1 (BB.t2)
3173 : : if (tmp)
3174 : : goto BB2
3175 : : else
3176 : : goto BB3
3177 : :
3178 : : In this case, we need to propagate through PHI to determine the edge
3179 : : count of BB1->BB.t1, BB.t1->BB.t2. */
3180 : :
3181 : : static void
3182 : 0 : afdo_propagate_circuit (const bb_set &annotated_bb)
3183 : : {
3184 : 0 : basic_block bb;
3185 : 0 : FOR_ALL_BB_FN (bb, cfun)
3186 : : {
3187 : 0 : gimple *def_stmt;
3188 : 0 : tree cmp_rhs, cmp_lhs;
3189 : 0 : gimple *cmp_stmt = last_nondebug_stmt (bb);
3190 : 0 : edge e;
3191 : 0 : edge_iterator ei;
3192 : :
3193 : 0 : if (!cmp_stmt || gimple_code (cmp_stmt) != GIMPLE_COND)
3194 : 0 : continue;
3195 : 0 : cmp_rhs = gimple_cond_rhs (cmp_stmt);
3196 : 0 : cmp_lhs = gimple_cond_lhs (cmp_stmt);
3197 : 0 : if (!TREE_CONSTANT (cmp_rhs)
3198 : 0 : || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
3199 : 0 : continue;
3200 : 0 : if (TREE_CODE (cmp_lhs) != SSA_NAME)
3201 : 0 : continue;
3202 : 0 : if (!is_bb_annotated (bb, annotated_bb))
3203 : 0 : continue;
3204 : 0 : def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
3205 : 0 : while (def_stmt && gimple_code (def_stmt) == GIMPLE_ASSIGN
3206 : 0 : && gimple_assign_single_p (def_stmt)
3207 : 0 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3208 : 0 : def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
3209 : 0 : if (!def_stmt)
3210 : 0 : continue;
3211 : 0 : gphi *phi_stmt = dyn_cast <gphi *> (def_stmt);
3212 : 0 : if (!phi_stmt)
3213 : 0 : continue;
3214 : 0 : FOR_EACH_EDGE (e, ei, bb->succs)
3215 : : {
3216 : 0 : unsigned i, total = 0;
3217 : 0 : edge only_one;
3218 : 0 : bool check_value_one = (((integer_onep (cmp_rhs))
3219 : 0 : ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
3220 : 0 : ^ ((e->flags & EDGE_TRUE_VALUE) != 0));
3221 : 0 : if (! AFDO_EINFO (e)->is_annotated ())
3222 : 0 : continue;
3223 : 0 : for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
3224 : : {
3225 : 0 : tree val = gimple_phi_arg_def (phi_stmt, i);
3226 : 0 : edge ep = gimple_phi_arg_edge (phi_stmt, i);
3227 : :
3228 : 0 : if (!TREE_CONSTANT (val)
3229 : 0 : || !(integer_zerop (val) || integer_onep (val)))
3230 : 0 : continue;
3231 : 0 : if (check_value_one ^ integer_onep (val))
3232 : 0 : continue;
3233 : 0 : total++;
3234 : 0 : only_one = ep;
3235 : 0 : if (! (AFDO_EINFO (e)->get_count ()).nonzero_p ()
3236 : 0 : && ! AFDO_EINFO (ep)->is_annotated ())
3237 : : {
3238 : 0 : AFDO_EINFO (ep)->set_count (profile_count::zero ().afdo ());
3239 : 0 : AFDO_EINFO (ep)->set_annotated ();
3240 : : }
3241 : : }
3242 : 0 : if (total == 1 && ! AFDO_EINFO (only_one)->is_annotated ())
3243 : : {
3244 : 0 : AFDO_EINFO (only_one)->set_count (AFDO_EINFO (e)->get_count ());
3245 : 0 : AFDO_EINFO (only_one)->set_annotated ();
3246 : : }
3247 : : }
3248 : : }
3249 : 0 : }
3250 : :
3251 : : /* Propagate the basic block count and edge count on the control flow
3252 : : graph. We do the propagation iteratively until stablize. */
3253 : :
3254 : : static void
3255 : 0 : afdo_propagate (bb_set *annotated_bb)
3256 : : {
3257 : 0 : bool changed = true;
3258 : 0 : int i = 0;
3259 : :
3260 : 0 : basic_block bb;
3261 : 0 : FOR_ALL_BB_FN (bb, cfun)
3262 : 0 : if (!is_bb_annotated (bb, *annotated_bb)
3263 : 0 : && is_bb_annotated ((basic_block)bb->aux, *annotated_bb))
3264 : : {
3265 : 0 : update_count_by_afdo_count (&bb->count, ((basic_block)bb->aux)->count);
3266 : 0 : set_bb_annotated (bb, annotated_bb);
3267 : 0 : if (dump_file)
3268 : : {
3269 : 0 : fprintf (dump_file,
3270 : : " Copying count of bb %i to bb %i; count is:",
3271 : 0 : ((basic_block)bb->aux)->index,
3272 : : bb->index);
3273 : 0 : bb->count.dump (dump_file);
3274 : 0 : fprintf (dump_file, "\n");
3275 : : }
3276 : : }
3277 : :
3278 : 0 : while (changed && i++ < 100)
3279 : : {
3280 : 0 : changed = false;
3281 : :
3282 : 0 : if (afdo_propagate_edge (true, annotated_bb))
3283 : : changed = true;
3284 : 0 : if (afdo_propagate_edge (false, annotated_bb))
3285 : 0 : changed = true;
3286 : 0 : afdo_propagate_circuit (*annotated_bb);
3287 : : }
3288 : 0 : if (dump_file)
3289 : 0 : fprintf (dump_file, "Propagation took %i iterations %s\n",
3290 : : i, changed ? "; iteration limit reached\n" : "");
3291 : 0 : }
3292 : :
3293 : : /* qsort comparator of sreals. */
3294 : : static int
3295 : 0 : cmp (const void *a, const void *b)
3296 : : {
3297 : 0 : if (*(const sreal *)a < *(const sreal *)b)
3298 : : return 1;
3299 : 0 : if (*(const sreal *)a > *(const sreal *)b)
3300 : 0 : return -1;
3301 : : return 0;
3302 : : }
3303 : :
3304 : : /* Add scale ORIG/ANNOTATED to SCALES. */
3305 : :
3306 : : static void
3307 : 0 : add_scale (vec <sreal> *scales, profile_count annotated, profile_count orig)
3308 : : {
3309 : 0 : if (dump_file)
3310 : : {
3311 : 0 : orig.dump (dump_file);
3312 : 0 : fprintf (dump_file, " should be ");
3313 : 0 : annotated.dump (dump_file);
3314 : 0 : fprintf (dump_file, "\n");
3315 : : }
3316 : 0 : if (orig.nonzero_p ())
3317 : : {
3318 : 0 : sreal scale
3319 : 0 : = annotated.guessed_local ()
3320 : 0 : .to_sreal_scale (orig);
3321 : 0 : if (dump_file)
3322 : 0 : fprintf (dump_file, " adding scale %.16f\n",
3323 : : scale.to_double ());
3324 : 0 : scales->safe_push (scale);
3325 : : }
3326 : 0 : }
3327 : :
3328 : : /* Scale counts of all basic blocks in BBS by SCALE and convert them to
3329 : : IPA quality. */
3330 : :
3331 : : static void
3332 : 0 : scale_bbs (const vec <basic_block> &bbs, sreal scale)
3333 : : {
3334 : 0 : if (dump_file)
3335 : 0 : fprintf (dump_file, " Scaling by %.16f\n", scale.to_double ());
3336 : 0 : for (basic_block b : bbs)
3337 : 0 : if (!(b->count == profile_count::zero ())
3338 : 0 : && b->count.initialized_p ())
3339 : : {
3340 : 0 : profile_count o = b->count;
3341 : 0 : b->count = b->count.force_guessed () * scale;
3342 : :
3343 : : /* If we scaled to 0, make it auto-fdo since that is treated
3344 : : less agressively. */
3345 : 0 : if (!b->count.nonzero_p () && o.nonzero_p ())
3346 : 0 : b->count = profile_count::zero ().afdo ();
3347 : 0 : if (dump_file)
3348 : : {
3349 : 0 : fprintf (dump_file, " bb %i count updated ", b->index);
3350 : 0 : o.dump (dump_file);
3351 : 0 : fprintf (dump_file, " -> ");
3352 : 0 : b->count.dump (dump_file);
3353 : 0 : fprintf (dump_file, "\n");
3354 : : }
3355 : : }
3356 : 0 : }
3357 : :
3358 : : /* In case given basic block was fully optimized out, AutoFDO
3359 : : will have no data about it. In this case try to preserve static profile.
3360 : : Identify connected components (in undirected form of CFG) which has
3361 : : no annotations at all. Look at thir boundaries and try to determine
3362 : : scaling factor and scale. */
3363 : :
3364 : : void
3365 : 0 : afdo_adjust_guessed_profile (bb_set *annotated_bb)
3366 : : {
3367 : : /* Basic blocks of connected component currently processed. */
3368 : 0 : auto_vec <basic_block, 20> bbs (n_basic_blocks_for_fn (cfun));
3369 : : /* Scale factors found. */
3370 : 0 : auto_vec <sreal, 20> scales;
3371 : 0 : auto_vec <basic_block, 20> stack (n_basic_blocks_for_fn (cfun));
3372 : :
3373 : 0 : basic_block seed_bb;
3374 : 0 : unsigned int component_id = 1;
3375 : :
3376 : : /* Map from basic block to its component.
3377 : : 0 is used for univisited BBs,
3378 : : 1 means that BB is annotated,
3379 : : >=2 is an id of the component BB belongs to. */
3380 : 0 : auto_vec <unsigned int, 20> component;
3381 : 0 : component.safe_grow (last_basic_block_for_fn (cfun));
3382 : 0 : FOR_ALL_BB_FN (seed_bb, cfun)
3383 : 0 : component[seed_bb->index]
3384 : 0 : = is_bb_annotated (seed_bb, *annotated_bb) ? 1 : 0;
3385 : 0 : FOR_ALL_BB_FN (seed_bb, cfun)
3386 : 0 : if (!component[seed_bb->index])
3387 : : {
3388 : 0 : stack.quick_push (seed_bb);
3389 : 0 : component_id++;
3390 : 0 : bbs.truncate (0);
3391 : 0 : scales.truncate (0);
3392 : 0 : component[seed_bb->index] = component_id;
3393 : 0 : profile_count max_count = profile_count::zero ();
3394 : :
3395 : : /* Identify connected component starting in BB. */
3396 : 0 : if (dump_file)
3397 : 0 : fprintf (dump_file, "Starting connected component in bb %i\n",
3398 : : seed_bb->index);
3399 : 0 : do
3400 : : {
3401 : 0 : basic_block b = stack.pop ();
3402 : :
3403 : 0 : bbs.quick_push (b);
3404 : 0 : max_count = max_count.max (b->count);
3405 : :
3406 : 0 : for (edge e: b->preds)
3407 : 0 : if (!component[e->src->index])
3408 : : {
3409 : 0 : stack.quick_push (e->src);
3410 : 0 : component[e->src->index] = component_id;
3411 : : }
3412 : 0 : for (edge e: b->succs)
3413 : 0 : if (!component[e->dest->index])
3414 : : {
3415 : 0 : stack.quick_push (e->dest);
3416 : 0 : component[e->dest->index] = component_id;
3417 : : }
3418 : : }
3419 : 0 : while (!stack.is_empty ());
3420 : :
3421 : : /* If all blocks in components has 0 count, we do not need
3422 : : to scale, only we must convert to IPA quality. */
3423 : 0 : if (!max_count.nonzero_p ())
3424 : : {
3425 : 0 : if (dump_file)
3426 : 0 : fprintf (dump_file, " All counts are 0; scale = 1\n");
3427 : 0 : scale_bbs (bbs, 1);
3428 : 0 : continue;
3429 : : }
3430 : :
3431 : : /* Now visit the component and try to figure out its desired
3432 : : frequency. */
3433 : 0 : for (basic_block b : bbs)
3434 : : {
3435 : 0 : if (dump_file)
3436 : : {
3437 : 0 : fprintf (dump_file, " visiting bb %i with count ", b->index);
3438 : 0 : b->count.dump (dump_file);
3439 : 0 : fprintf (dump_file, "\n");
3440 : : }
3441 : 0 : if (!b->count.nonzero_p ())
3442 : 0 : continue;
3443 : : /* Sum of counts of annotated edges into B. */
3444 : 0 : profile_count annotated_count = profile_count::zero ();
3445 : : /* Sum of counts of edges into B with source in current
3446 : : component. */
3447 : 0 : profile_count current_component_count = profile_count::zero ();
3448 : 0 : bool boundary = false;
3449 : :
3450 : 0 : for (edge e: b->preds)
3451 : 0 : if (AFDO_EINFO (e)->is_annotated ())
3452 : : {
3453 : 0 : if (dump_file)
3454 : : {
3455 : 0 : fprintf (dump_file, " Annotated pred edge to %i "
3456 : 0 : "with count ", e->src->index);
3457 : 0 : AFDO_EINFO (e)->get_count ().dump (dump_file);
3458 : 0 : fprintf (dump_file, "\n");
3459 : : }
3460 : 0 : boundary = true;
3461 : 0 : annotated_count += AFDO_EINFO (e)->get_count ();
3462 : : }
3463 : : /* If source is anotated, combine with static
3464 : : probability prediction.
3465 : : TODO: We can do better in case some of edges out are
3466 : : annotated and distribute only remaining count out of BB. */
3467 : 0 : else if (is_bb_annotated (e->src, *annotated_bb))
3468 : : {
3469 : 0 : boundary = true;
3470 : 0 : if (dump_file)
3471 : : {
3472 : 0 : fprintf (dump_file, " Annotated predecesor %i "
3473 : : "with count ", e->src->index);
3474 : 0 : e->src->count.dump (dump_file);
3475 : 0 : fprintf (dump_file, " edge count using static profile ");
3476 : 0 : e->count ().dump (dump_file);
3477 : 0 : fprintf (dump_file, "\n");
3478 : : }
3479 : 0 : annotated_count += e->count ();
3480 : : }
3481 : : else
3482 : : {
3483 : 0 : current_component_count += e->count ();
3484 : 0 : gcc_checking_assert (component[e->src->index] == component_id);
3485 : : }
3486 : 0 : if (boundary && current_component_count.initialized_p ())
3487 : : {
3488 : 0 : if (dump_file)
3489 : 0 : fprintf (dump_file, " bb %i in count ", b->index);
3490 : 0 : add_scale (&scales,
3491 : : annotated_count,
3492 : : b->count - current_component_count);
3493 : : }
3494 : 0 : for (edge e: b->succs)
3495 : 0 : if (AFDO_EINFO (e)->is_annotated ())
3496 : : {
3497 : 0 : if (dump_file)
3498 : 0 : fprintf (dump_file, " edge %i->%i count ",
3499 : 0 : b->index, e->dest->index);
3500 : 0 : add_scale (&scales, AFDO_EINFO (e)->get_count (), e->count ());
3501 : : }
3502 : 0 : else if (is_bb_annotated (e->dest, *annotated_bb))
3503 : : {
3504 : 0 : profile_count annotated_count = e->dest->count;
3505 : 0 : profile_count out_count = profile_count::zero ();
3506 : 0 : bool ok = true;
3507 : 0 : for (edge e2: e->dest->preds)
3508 : 0 : if (AFDO_EINFO (e2)->is_annotated ())
3509 : 0 : annotated_count -= AFDO_EINFO (e2)->get_count ();
3510 : 0 : else if (component[e->src->index] == component_id)
3511 : 0 : out_count += e->count ();
3512 : 0 : else if (e->probability.nonzero_p ())
3513 : : {
3514 : : ok = false;
3515 : : break;
3516 : : }
3517 : 0 : if (!ok)
3518 : 0 : continue;
3519 : 0 : if (dump_file)
3520 : 0 : fprintf (dump_file,
3521 : : " edge %i->%i has annotated sucessor; count ",
3522 : 0 : b->index, e->dest->index);
3523 : 0 : add_scale (&scales, annotated_count, e->count ());
3524 : : }
3525 : :
3526 : : }
3527 : :
3528 : : /* If we failed to find annotated entry or exit edge,
3529 : : look for exit edges and scale profile so the dest
3530 : : BB get all flow it needs. This is inprecise because
3531 : : the edge is not annotated and thus BB has more than
3532 : : one such predecessor. */
3533 : 0 : if (!scales.length ())
3534 : 0 : for (basic_block b : bbs)
3535 : 0 : if (b->count.nonzero_p ())
3536 : 0 : for (edge e: b->succs)
3537 : 0 : if (is_bb_annotated (e->dest, *annotated_bb))
3538 : : {
3539 : 0 : profile_count annotated_count = e->dest->count;
3540 : 0 : for (edge e2: e->dest->preds)
3541 : 0 : if (AFDO_EINFO (e2)->is_annotated ())
3542 : 0 : annotated_count -= AFDO_EINFO (e2)->get_count ();
3543 : 0 : if (dump_file)
3544 : 0 : fprintf (dump_file,
3545 : : " edge %i->%i has annotated sucessor;"
3546 : : " upper bound count ",
3547 : 0 : b->index, e->dest->index);
3548 : 0 : add_scale (&scales, annotated_count, e->count ());
3549 : : }
3550 : 0 : if (!scales.length ())
3551 : : {
3552 : 0 : if (dump_file)
3553 : 0 : fprintf (dump_file,
3554 : : " Can not determine count from the boundary; giving up");
3555 : 0 : continue;
3556 : : }
3557 : 0 : gcc_checking_assert (scales.length ());
3558 : 0 : scales.qsort (cmp);
3559 : 0 : scale_bbs (bbs, scales[scales.length () / 2]);
3560 : : }
3561 : 0 : }
3562 : :
3563 : : /* Propagate counts on control flow graph and calculate branch
3564 : : probabilities. */
3565 : :
3566 : : static void
3567 : 0 : afdo_calculate_branch_prob (bb_set *annotated_bb)
3568 : : {
3569 : 0 : edge e;
3570 : 0 : edge_iterator ei;
3571 : 0 : basic_block bb;
3572 : :
3573 : 0 : FOR_ALL_BB_FN (bb, cfun)
3574 : : {
3575 : 0 : gcc_assert (bb->aux == NULL);
3576 : 0 : FOR_EACH_EDGE (e, ei, bb->succs)
3577 : : {
3578 : 0 : gcc_assert (e->aux == NULL);
3579 : 0 : e->aux = new edge_info ();
3580 : : }
3581 : : }
3582 : :
3583 : 0 : afdo_find_equiv_class (annotated_bb);
3584 : 0 : afdo_propagate (annotated_bb);
3585 : :
3586 : 0 : FOR_EACH_BB_FN (bb, cfun)
3587 : 0 : if (is_bb_annotated (bb, *annotated_bb))
3588 : : {
3589 : 0 : bool all_known = true;
3590 : 0 : profile_count total_count = profile_count::zero ().afdo ();
3591 : :
3592 : 0 : FOR_EACH_EDGE (e, ei, bb->succs)
3593 : : {
3594 : 0 : gcc_assert (AFDO_EINFO (e) != NULL);
3595 : 0 : if (! AFDO_EINFO (e)->is_annotated ())
3596 : : {
3597 : : /* If by static profile this edge never happens,
3598 : : still propagate the rest. */
3599 : 0 : if (e->probability.nonzero_p ())
3600 : : {
3601 : : all_known = false;
3602 : : break;
3603 : : }
3604 : : }
3605 : : else
3606 : 0 : total_count += AFDO_EINFO (e)->get_count ();
3607 : : }
3608 : 0 : if (!all_known || !total_count.nonzero_p ())
3609 : 0 : continue;
3610 : :
3611 : 0 : FOR_EACH_EDGE (e, ei, bb->succs)
3612 : 0 : if (AFDO_EINFO (e)->is_annotated ())
3613 : : {
3614 : : /* If probability is 1, preserve reliable static prediction
3615 : : (This is, for example the case of single fallthru edge
3616 : : or single fallthru plus unlikely EH edge.) */
3617 : 0 : if (AFDO_EINFO (e)->get_count () == total_count
3618 : 0 : && e->probability == profile_probability::always ())
3619 : : ;
3620 : 0 : else if (AFDO_EINFO (e)->get_count ().nonzero_p ())
3621 : 0 : e->probability
3622 : 0 : = AFDO_EINFO (e)->get_count ().probability_in (total_count);
3623 : : /* If probability is zero, preserve reliable static
3624 : : prediction. */
3625 : 0 : else if (e->probability.nonzero_p ()
3626 : 0 : || e->probability.quality () == GUESSED)
3627 : 0 : e->probability = profile_probability::never ().afdo ();
3628 : : }
3629 : : }
3630 : 0 : afdo_adjust_guessed_profile (annotated_bb);
3631 : 0 : FOR_ALL_BB_FN (bb, cfun)
3632 : : {
3633 : 0 : bb->aux = NULL;
3634 : 0 : FOR_EACH_EDGE (e, ei, bb->succs)
3635 : 0 : if (AFDO_EINFO (e) != NULL)
3636 : : {
3637 : 0 : delete AFDO_EINFO (e);
3638 : 0 : e->aux = NULL;
3639 : : }
3640 : : }
3641 : 0 : }
3642 : :
3643 : : /* Annotate auto profile to the control flow graph. */
3644 : :
3645 : : static void
3646 : 0 : afdo_annotate_cfg (void)
3647 : : {
3648 : 0 : basic_block bb;
3649 : 0 : bb_set annotated_bb;
3650 : 0 : const function_instance *s
3651 : 0 : = afdo_source_profile->get_function_instance_by_decl (
3652 : : current_function_decl);
3653 : :
3654 : 0 : if (s == NULL)
3655 : : {
3656 : 0 : if (dump_file)
3657 : 0 : fprintf (dump_file, "No afdo profile for %s\n",
3658 : 0 : cgraph_node::get (current_function_decl)->dump_name ());
3659 : : /* create_gcov only dumps symbols with some samples in them.
3660 : : This means that we get nonempty zero_bbs only if some
3661 : : nonzero counts in profile were not matched with statements. */
3662 : 0 : if (!flag_profile_partial_training)
3663 : : {
3664 : 0 : FOR_ALL_BB_FN (bb, cfun)
3665 : 0 : if (bb->count.quality () == GUESSED_LOCAL)
3666 : 0 : bb->count = bb->count.global0afdo ();
3667 : 0 : update_max_bb_count ();
3668 : : }
3669 : 0 : return;
3670 : : }
3671 : :
3672 : 0 : calculate_dominance_info (CDI_POST_DOMINATORS);
3673 : 0 : calculate_dominance_info (CDI_DOMINATORS);
3674 : 0 : loop_optimizer_init (0);
3675 : :
3676 : 0 : if (dump_file)
3677 : : {
3678 : 0 : fprintf (dump_file, "\n\nAnnotating BB profile of %s\n",
3679 : 0 : cgraph_node::get (current_function_decl)->dump_name ());
3680 : 0 : fprintf (dump_file, "\n");
3681 : 0 : s->dump (dump_file);
3682 : 0 : fprintf (dump_file, "\n");
3683 : : }
3684 : :
3685 : : /* In the first pass only store non-zero counts. */
3686 : 0 : gcov_type head_count = s->head_count () * autofdo::afdo_count_scale;
3687 : 0 : bool profile_found = head_count > 0;
3688 : 0 : hash_set <basic_block> zero_bbs;
3689 : 0 : FOR_EACH_BB_FN (bb, cfun)
3690 : : {
3691 : 0 : if (afdo_set_bb_count (bb, zero_bbs))
3692 : : {
3693 : 0 : if (bb->count.quality () == AFDO)
3694 : : {
3695 : 0 : gcc_assert (bb->count.nonzero_p ());
3696 : : profile_found = true;
3697 : : }
3698 : 0 : set_bb_annotated (bb, &annotated_bb);
3699 : : }
3700 : : }
3701 : : /* We try to preserve static profile for BBs with 0
3702 : : afdo samples, but if even static profile agrees with 0,
3703 : : consider it final so propagation works better. */
3704 : 0 : for (basic_block bb : zero_bbs)
3705 : 0 : if (!bb->count.nonzero_p ())
3706 : : {
3707 : 0 : update_count_by_afdo_count (&bb->count, 0);
3708 : 0 : set_bb_annotated (bb, &annotated_bb);
3709 : 0 : if (dump_file)
3710 : : {
3711 : 0 : fprintf (dump_file, " Annotating bb %i with count ", bb->index);
3712 : 0 : bb->count.dump (dump_file);
3713 : 0 : fprintf (dump_file,
3714 : : " (has 0 count in both static and afdo profile)\n");
3715 : : }
3716 : : }
3717 : : /* Exit without clobbering static profile if there was no
3718 : : non-zero count. */
3719 : 0 : if (!profile_found)
3720 : : {
3721 : : /* create_gcov only dumps symbols with some samples in them.
3722 : : This means that we get nonempty zero_bbs only if some
3723 : : nonzero counts in profile were not matched with statements.
3724 : : ??? We can adjust create_gcov to also recordinfo
3725 : : about function with no samples. Then we can distinguish
3726 : : between lost profiles which should be kept local and
3727 : : real functions with 0 samples during train run. */
3728 : 0 : if (zero_bbs.is_empty ())
3729 : : {
3730 : 0 : if (dump_file)
3731 : 0 : fprintf (dump_file, "No afdo samples found"
3732 : : "; Setting global count to afdo0\n");
3733 : : }
3734 : : else
3735 : : {
3736 : 0 : if (dump_file)
3737 : 0 : fprintf (dump_file, "Setting global count to afdo0\n");
3738 : : }
3739 : 0 : if (!flag_profile_partial_training)
3740 : : {
3741 : 0 : FOR_ALL_BB_FN (bb, cfun)
3742 : 0 : if (bb->count.quality () == GUESSED_LOCAL)
3743 : 0 : bb->count = bb->count.global0afdo ();
3744 : 0 : update_max_bb_count ();
3745 : : }
3746 : :
3747 : 0 : loop_optimizer_finalize ();
3748 : 0 : free_dominance_info (CDI_DOMINATORS);
3749 : 0 : free_dominance_info (CDI_POST_DOMINATORS);
3750 : 0 : return;
3751 : : }
3752 : :
3753 : : /* Update profile. */
3754 : 0 : if (head_count > 0)
3755 : : {
3756 : 0 : update_count_by_afdo_count (&ENTRY_BLOCK_PTR_FOR_FN (cfun)->count,
3757 : : head_count);
3758 : 0 : set_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun), &annotated_bb);
3759 : 0 : if (!is_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, annotated_bb)
3760 : 0 : || ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
3761 : 0 : > ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count)
3762 : : {
3763 : 0 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->count
3764 : 0 : = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
3765 : 0 : set_bb_annotated (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
3766 : : &annotated_bb);
3767 : : }
3768 : 0 : if (!is_bb_annotated (EXIT_BLOCK_PTR_FOR_FN (cfun), annotated_bb)
3769 : 0 : || ENTRY_BLOCK_PTR_FOR_FN (cfun)->count
3770 : 0 : > EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count)
3771 : : {
3772 : 0 : EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->count
3773 : 0 : = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
3774 : 0 : set_bb_annotated (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, &annotated_bb);
3775 : : }
3776 : : }
3777 : :
3778 : : /* Calculate, propagate count and probability information on CFG. */
3779 : 0 : afdo_calculate_branch_prob (&annotated_bb);
3780 : :
3781 : : /* If we failed to turn some of original guessed profile to global,
3782 : : set basic blocks uninitialized. */
3783 : 0 : FOR_ALL_BB_FN (bb, cfun)
3784 : 0 : if (!bb->count.ipa_p ())
3785 : : {
3786 : : /* We skip annotating entry profile if it is 0
3787 : : in hope to be able to determine it better from the
3788 : : static profile.
3789 : :
3790 : : Now we know we can not derive it from other info,
3791 : : so set it since it is better than UNKNOWN. */
3792 : 0 : if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
3793 : 0 : bb->count = profile_count::zero ().afdo ();
3794 : : else
3795 : 0 : bb->count = profile_count::uninitialized ();
3796 : 0 : if (dump_file)
3797 : 0 : fprintf (dump_file, " Unknown count of bb %i\n", bb->index);
3798 : 0 : cfun->cfg->full_profile = false;
3799 : : }
3800 : :
3801 : 0 : cgraph_node::get (current_function_decl)->count
3802 : 0 : = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
3803 : 0 : update_max_bb_count ();
3804 : 0 : profile_status_for_fn (cfun) = PROFILE_READ;
3805 : 0 : if (flag_value_profile_transformations)
3806 : : {
3807 : 0 : gimple_value_profile_transformations ();
3808 : 0 : free_dominance_info (CDI_DOMINATORS);
3809 : 0 : free_dominance_info (CDI_POST_DOMINATORS);
3810 : 0 : update_ssa (TODO_update_ssa);
3811 : : }
3812 : :
3813 : 0 : loop_optimizer_finalize ();
3814 : 0 : free_dominance_info (CDI_DOMINATORS);
3815 : 0 : free_dominance_info (CDI_POST_DOMINATORS);
3816 : 0 : }
3817 : :
3818 : : /* Use AutoFDO profile to annoate the control flow graph.
3819 : : Return the todo flag. */
3820 : :
3821 : : static unsigned int
3822 : 0 : auto_profile (void)
3823 : : {
3824 : 0 : struct cgraph_node *node;
3825 : :
3826 : 0 : if (symtab->state == FINISHED || !afdo_source_profile)
3827 : : return 0;
3828 : :
3829 : 0 : init_node_map (true);
3830 : 0 : profile_info = autofdo::afdo_profile_info;
3831 : 0 : afdo_source_profile->offline_unrealized_inlines ();
3832 : :
3833 : 0 : FOR_EACH_FUNCTION (node)
3834 : : {
3835 : 0 : if (!gimple_has_body_p (node->decl))
3836 : 0 : continue;
3837 : :
3838 : : /* Don't profile functions produced for builtin stuff. */
3839 : 0 : if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
3840 : 0 : continue;
3841 : :
3842 : 0 : push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3843 : :
3844 : 0 : autofdo::afdo_annotate_cfg ();
3845 : 0 : compute_function_frequency ();
3846 : :
3847 : 0 : free_dominance_info (CDI_DOMINATORS);
3848 : 0 : free_dominance_info (CDI_POST_DOMINATORS);
3849 : 0 : cgraph_edge::rebuild_edges ();
3850 : 0 : pop_cfun ();
3851 : : }
3852 : :
3853 : : return 0;
3854 : : }
3855 : : } /* namespace autofdo. */
3856 : :
3857 : : /* Read the profile from the profile data file. */
3858 : :
3859 : : void
3860 : 0 : read_autofdo_file (void)
3861 : : {
3862 : 0 : if (auto_profile_file == NULL)
3863 : 0 : auto_profile_file = DEFAULT_AUTO_PROFILE_FILE;
3864 : :
3865 : 0 : autofdo::afdo_profile_info = XNEW (gcov_summary);
3866 : 0 : autofdo::afdo_profile_info->runs = 1;
3867 : 0 : autofdo::afdo_profile_info->sum_max = 0;
3868 : :
3869 : : /* Read the profile from the profile file. */
3870 : 0 : autofdo::read_profile ();
3871 : 0 : }
3872 : :
3873 : : /* Free the resources. */
3874 : :
3875 : : void
3876 : 0 : end_auto_profile (void)
3877 : : {
3878 : 0 : delete autofdo::afdo_source_profile;
3879 : 0 : delete autofdo::afdo_string_table;
3880 : 0 : profile_info = NULL;
3881 : 0 : }
3882 : :
3883 : : /* Returns TRUE if EDGE is hot enough to be inlined early. */
3884 : :
3885 : : bool
3886 : 0 : afdo_callsite_hot_enough_for_early_inline (struct cgraph_edge *edge)
3887 : : {
3888 : 0 : gcov_type count
3889 : 0 : = autofdo::afdo_source_profile->get_callsite_total_count (edge);
3890 : :
3891 : 0 : if (count > 0)
3892 : : {
3893 : 0 : bool is_hot;
3894 : 0 : profile_count pcount = profile_count::from_gcov_type (count).afdo ();
3895 : 0 : is_hot = maybe_hot_afdo_count_p (pcount);
3896 : 0 : if (dump_file)
3897 : : {
3898 : 0 : fprintf (dump_file, "Call %s -> %s has %s afdo profile count ",
3899 : 0 : edge->caller->dump_name (), edge->callee->dump_name (),
3900 : : is_hot ? "hot" : "cold");
3901 : 0 : pcount.dump (dump_file);
3902 : 0 : fprintf (dump_file, "\n");
3903 : : }
3904 : 0 : return is_hot;
3905 : : }
3906 : :
3907 : : return false;
3908 : : }
3909 : :
3910 : : /* Do indirect call promotion during early inlining to make the
3911 : : IR match the profiled binary before actual annotation.
3912 : :
3913 : : This is needed because an indirect call might have been promoted
3914 : : and inlined in the profiled binary. If we do not promote and
3915 : : inline these indirect calls before annotation, the profile for
3916 : : these promoted functions will be lost.
3917 : :
3918 : : e.g. foo() --indirect_call--> bar()
3919 : : In profiled binary, the callsite is promoted and inlined, making
3920 : : the profile look like:
3921 : :
3922 : : foo: {
3923 : : loc_foo_1: count_1
3924 : : bar@loc_foo_2: {
3925 : : loc_bar_1: count_2
3926 : : loc_bar_2: count_3
3927 : : }
3928 : : }
3929 : :
3930 : : Before AutoFDO pass, loc_foo_2 is not promoted thus not inlined.
3931 : : If we perform annotation on it, the profile inside bar@loc_foo2
3932 : : will be wasted.
3933 : :
3934 : : To avoid this, we promote loc_foo_2 and inline the promoted bar
3935 : : function before annotation, so the profile inside bar@loc_foo2
3936 : : will be useful. */
3937 : :
3938 : : bool
3939 : 0 : afdo_vpt_for_early_inline (cgraph_node *node)
3940 : : {
3941 : 0 : if (!node->indirect_calls)
3942 : : return false;
3943 : 0 : bool changed = false;
3944 : 0 : cgraph_node *outer = node->inlined_to ? node->inlined_to : node;
3945 : 0 : if (autofdo::afdo_source_profile->get_function_instance_by_decl
3946 : 0 : (outer->decl) == NULL)
3947 : : return false;
3948 : 0 : for (cgraph_edge *e = node->indirect_calls; e; e = e->next_callee)
3949 : : {
3950 : 0 : gcov_type bb_count = 0;
3951 : 0 : autofdo::count_info info;
3952 : 0 : basic_block bb = gimple_bb (e->call_stmt);
3953 : :
3954 : : /* TODO: This is quadratic; cache the value. */
3955 : 0 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3956 : 0 : !gsi_end_p (gsi); gsi_next (&gsi))
3957 : : {
3958 : 0 : autofdo::count_info info;
3959 : 0 : gimple *stmt = gsi_stmt (gsi);
3960 : 0 : if (autofdo::afdo_source_profile->get_count_info (stmt, &info, node))
3961 : 0 : bb_count = MAX (bb_count, info.count);
3962 : 0 : }
3963 : 0 : autofdo::afdo_source_profile->get_count_info (e->call_stmt, &info, node);
3964 : 0 : info.count = bb_count;
3965 : 0 : if (!autofdo::afdo_source_profile->update_inlined_ind_target
3966 : 0 : (e->call_stmt, &info, node))
3967 : 0 : continue;
3968 : 0 : changed |= autofdo::afdo_vpt (e->call_stmt, info.targets, true, e);
3969 : 0 : }
3970 : : return changed;
3971 : : }
3972 : :
3973 : : /* If speculation used during early inline, remove the target
3974 : : so we do not speculate the indirect edge again during afdo pass. */
3975 : :
3976 : : void
3977 : 0 : remove_afdo_speculative_target (cgraph_edge *e)
3978 : : {
3979 : 0 : autofdo::afdo_source_profile->remove_icall_target (e);
3980 : 0 : }
3981 : :
3982 : : namespace
3983 : : {
3984 : :
3985 : : const pass_data pass_data_ipa_auto_profile = {
3986 : : SIMPLE_IPA_PASS, "afdo", /* name */
3987 : : OPTGROUP_NONE, /* optinfo_flags */
3988 : : TV_IPA_AUTOFDO, /* tv_id */
3989 : : 0, /* properties_required */
3990 : : 0, /* properties_provided */
3991 : : 0, /* properties_destroyed */
3992 : : 0, /* todo_flags_start */
3993 : : 0, /* todo_flags_finish */
3994 : : };
3995 : :
3996 : : class pass_ipa_auto_profile : public simple_ipa_opt_pass
3997 : : {
3998 : : public:
3999 : 286682 : pass_ipa_auto_profile (gcc::context *ctxt)
4000 : 573364 : : simple_ipa_opt_pass (pass_data_ipa_auto_profile, ctxt)
4001 : : {
4002 : : }
4003 : :
4004 : : /* opt_pass methods: */
4005 : : bool
4006 : 230209 : gate (function *) final override
4007 : : {
4008 : 230209 : return flag_auto_profile;
4009 : : }
4010 : : unsigned int
4011 : 0 : execute (function *) final override
4012 : : {
4013 : 0 : return autofdo::auto_profile ();
4014 : : }
4015 : : }; // class pass_ipa_auto_profile
4016 : :
4017 : : } // anon namespace
4018 : :
4019 : : simple_ipa_opt_pass *
4020 : 286682 : make_pass_ipa_auto_profile (gcc::context *ctxt)
4021 : : {
4022 : 286682 : return new pass_ipa_auto_profile (ctxt);
4023 : : }
4024 : :
4025 : : namespace
4026 : : {
4027 : :
4028 : : const pass_data pass_data_ipa_auto_profile_offline = {
4029 : : SIMPLE_IPA_PASS, "afdo_offline", /* name */
4030 : : OPTGROUP_NONE, /* optinfo_flags */
4031 : : TV_IPA_AUTOFDO_OFFLINE, /* tv_id */
4032 : : 0, /* properties_required */
4033 : : 0, /* properties_provided */
4034 : : 0, /* properties_destroyed */
4035 : : 0, /* todo_flags_start */
4036 : : 0, /* todo_flags_finish */
4037 : : };
4038 : :
4039 : : class pass_ipa_auto_profile_offline : public simple_ipa_opt_pass
4040 : : {
4041 : : public:
4042 : 286682 : pass_ipa_auto_profile_offline (gcc::context *ctxt)
4043 : 573364 : : simple_ipa_opt_pass (pass_data_ipa_auto_profile_offline, ctxt)
4044 : : {
4045 : : }
4046 : :
4047 : : /* opt_pass methods: */
4048 : : bool
4049 : 230209 : gate (function *) final override
4050 : : {
4051 : 230209 : return flag_auto_profile;
4052 : : }
4053 : : unsigned int
4054 : 0 : execute (function *) final override
4055 : : {
4056 : 0 : read_autofdo_file ();
4057 : 0 : if (autofdo::afdo_source_profile)
4058 : 0 : autofdo::afdo_source_profile->offline_external_functions ();
4059 : 0 : return 0;
4060 : : }
4061 : : }; // class pass_ipa_auto_profile
4062 : :
4063 : : } // anon namespace
4064 : :
4065 : : simple_ipa_opt_pass *
4066 : 286682 : make_pass_ipa_auto_profile_offline (gcc::context *ctxt)
4067 : : {
4068 : 286682 : return new pass_ipa_auto_profile_offline (ctxt);
4069 : : }
|