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