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