* i386.c (has_dispatch): Disable for Ryzen.
[official-gcc.git] / gcc / value-prof.c
blob23b8dc26471c6c38c270a9d1777b6eea8d6db80e
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "cfghooks.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "coverage.h"
31 #include "data-streamer.h"
32 #include "diagnostic.h"
33 #include "fold-const.h"
34 #include "tree-nested.h"
35 #include "calls.h"
36 #include "expr.h"
37 #include "value-prof.h"
38 #include "tree-eh.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "tree-cfg.h"
42 #include "gimple-pretty-print.h"
43 #include "dumpfile.h"
44 #include "builtins.h"
45 #include "params.h"
46 #include "tree-chkp.h"
48 /* In this file value profile based optimizations are placed. Currently the
49 following optimizations are implemented (for more detailed descriptions
50 see comments at value_profile_transformations):
52 1) Division/modulo specialization. Provided that we can determine that the
53 operands of the division have some special properties, we may use it to
54 produce more effective code.
56 2) Indirect/virtual call specialization. If we can determine most
57 common function callee in indirect/virtual call. We can use this
58 information to improve code effectiveness (especially info for
59 the inliner).
61 3) Speculative prefetching. If we are able to determine that the difference
62 between addresses accessed by a memory reference is usually constant, we
63 may add the prefetch instructions.
64 FIXME: This transformation was removed together with RTL based value
65 profiling.
68 Value profiling internals
69 ==========================
71 Every value profiling transformation starts with defining what values
72 to profile. There are different histogram types (see HIST_TYPE_* in
73 value-prof.h) and each transformation can request one or more histogram
74 types per GIMPLE statement. The function gimple_find_values_to_profile()
75 collects the values to profile in a vec, and adds the number of counters
76 required for the different histogram types.
78 For a -fprofile-generate run, the statements for which values should be
79 recorded, are instrumented in instrument_values(). The instrumentation
80 is done by helper functions that can be found in tree-profile.c, where
81 new types of histograms can be added if necessary.
83 After a -fprofile-use, the value profiling data is read back in by
84 compute_value_histograms() that translates the collected data to
85 histograms and attaches them to the profiled statements via
86 gimple_add_histogram_value(). Histograms are stored in a hash table
87 that is attached to every intrumented function, see VALUE_HISTOGRAMS
88 in function.h.
90 The value-profile transformations driver is the function
91 gimple_value_profile_transformations(). It traverses all statements in
92 the to-be-transformed function, and looks for statements with one or
93 more histograms attached to it. If a statement has histograms, the
94 transformation functions are called on the statement.
96 Limitations / FIXME / TODO:
97 * Only one histogram of each type can be associated with a statement.
98 * Some value profile transformations are done in builtins.c (?!)
99 * Updating of histograms needs some TLC.
100 * The value profiling code could be used to record analysis results
101 from non-profiling (e.g. VRP).
102 * Adding new profilers should be simplified, starting with a cleanup
103 of what-happens-where and with making gimple_find_values_to_profile
104 and gimple_value_profile_transformations table-driven, perhaps...
107 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
108 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
109 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
110 static bool gimple_stringops_transform (gimple_stmt_iterator *);
111 static bool gimple_ic_transform (gimple_stmt_iterator *);
113 /* Allocate histogram value. */
115 histogram_value
116 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
117 enum hist_type type, gimple *stmt, tree value)
119 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
120 hist->hvalue.value = value;
121 hist->hvalue.stmt = stmt;
122 hist->type = type;
123 return hist;
126 /* Hash value for histogram. */
128 static hashval_t
129 histogram_hash (const void *x)
131 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
134 /* Return nonzero if statement for histogram_value X is Y. */
136 static int
137 histogram_eq (const void *x, const void *y)
139 return ((const_histogram_value) x)->hvalue.stmt == (const gimple *) y;
142 /* Set histogram for STMT. */
144 static void
145 set_histogram_value (struct function *fun, gimple *stmt, histogram_value hist)
147 void **loc;
148 if (!hist && !VALUE_HISTOGRAMS (fun))
149 return;
150 if (!VALUE_HISTOGRAMS (fun))
151 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
152 histogram_eq, NULL);
153 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
154 htab_hash_pointer (stmt),
155 hist ? INSERT : NO_INSERT);
156 if (!hist)
158 if (loc)
159 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
160 return;
162 *loc = hist;
165 /* Get histogram list for STMT. */
167 histogram_value
168 gimple_histogram_value (struct function *fun, gimple *stmt)
170 if (!VALUE_HISTOGRAMS (fun))
171 return NULL;
172 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
173 htab_hash_pointer (stmt));
176 /* Add histogram for STMT. */
178 void
179 gimple_add_histogram_value (struct function *fun, gimple *stmt,
180 histogram_value hist)
182 hist->hvalue.next = gimple_histogram_value (fun, stmt);
183 set_histogram_value (fun, stmt, hist);
184 hist->fun = fun;
187 /* Remove histogram HIST from STMT's histogram list. */
189 void
190 gimple_remove_histogram_value (struct function *fun, gimple *stmt,
191 histogram_value hist)
193 histogram_value hist2 = gimple_histogram_value (fun, stmt);
194 if (hist == hist2)
196 set_histogram_value (fun, stmt, hist->hvalue.next);
198 else
200 while (hist2->hvalue.next != hist)
201 hist2 = hist2->hvalue.next;
202 hist2->hvalue.next = hist->hvalue.next;
204 free (hist->hvalue.counters);
205 if (flag_checking)
206 memset (hist, 0xab, sizeof (*hist));
207 free (hist);
210 /* Lookup histogram of type TYPE in the STMT. */
212 histogram_value
213 gimple_histogram_value_of_type (struct function *fun, gimple *stmt,
214 enum hist_type type)
216 histogram_value hist;
217 for (hist = gimple_histogram_value (fun, stmt); hist;
218 hist = hist->hvalue.next)
219 if (hist->type == type)
220 return hist;
221 return NULL;
224 /* Dump information about HIST to DUMP_FILE. */
226 static void
227 dump_histogram_value (FILE *dump_file, histogram_value hist)
229 switch (hist->type)
231 case HIST_TYPE_INTERVAL:
232 fprintf (dump_file, "Interval counter range %d -- %d",
233 hist->hdata.intvl.int_start,
234 (hist->hdata.intvl.int_start
235 + hist->hdata.intvl.steps - 1));
236 if (hist->hvalue.counters)
238 unsigned int i;
239 fprintf (dump_file, " [");
240 for (i = 0; i < hist->hdata.intvl.steps; i++)
241 fprintf (dump_file, " %d:%" PRId64,
242 hist->hdata.intvl.int_start + i,
243 (int64_t) hist->hvalue.counters[i]);
244 fprintf (dump_file, " ] outside range:%" PRId64,
245 (int64_t) hist->hvalue.counters[i]);
247 fprintf (dump_file, ".\n");
248 break;
250 case HIST_TYPE_POW2:
251 fprintf (dump_file, "Pow2 counter ");
252 if (hist->hvalue.counters)
254 fprintf (dump_file, "pow2:%" PRId64
255 " nonpow2:%" PRId64,
256 (int64_t) hist->hvalue.counters[1],
257 (int64_t) hist->hvalue.counters[0]);
259 fprintf (dump_file, ".\n");
260 break;
262 case HIST_TYPE_SINGLE_VALUE:
263 fprintf (dump_file, "Single value ");
264 if (hist->hvalue.counters)
266 fprintf (dump_file, "value:%" PRId64
267 " match:%" PRId64
268 " wrong:%" PRId64,
269 (int64_t) hist->hvalue.counters[0],
270 (int64_t) hist->hvalue.counters[1],
271 (int64_t) hist->hvalue.counters[2]);
273 fprintf (dump_file, ".\n");
274 break;
276 case HIST_TYPE_AVERAGE:
277 fprintf (dump_file, "Average value ");
278 if (hist->hvalue.counters)
280 fprintf (dump_file, "sum:%" PRId64
281 " times:%" PRId64,
282 (int64_t) hist->hvalue.counters[0],
283 (int64_t) hist->hvalue.counters[1]);
285 fprintf (dump_file, ".\n");
286 break;
288 case HIST_TYPE_IOR:
289 fprintf (dump_file, "IOR value ");
290 if (hist->hvalue.counters)
292 fprintf (dump_file, "ior:%" PRId64,
293 (int64_t) hist->hvalue.counters[0]);
295 fprintf (dump_file, ".\n");
296 break;
298 case HIST_TYPE_INDIR_CALL:
299 fprintf (dump_file, "Indirect call ");
300 if (hist->hvalue.counters)
302 fprintf (dump_file, "value:%" PRId64
303 " match:%" PRId64
304 " all:%" PRId64,
305 (int64_t) hist->hvalue.counters[0],
306 (int64_t) hist->hvalue.counters[1],
307 (int64_t) hist->hvalue.counters[2]);
309 fprintf (dump_file, ".\n");
310 break;
311 case HIST_TYPE_TIME_PROFILE:
312 fprintf (dump_file, "Time profile ");
313 if (hist->hvalue.counters)
315 fprintf (dump_file, "time:%" PRId64,
316 (int64_t) hist->hvalue.counters[0]);
318 fprintf (dump_file, ".\n");
319 break;
320 case HIST_TYPE_INDIR_CALL_TOPN:
321 fprintf (dump_file, "Indirect call topn ");
322 if (hist->hvalue.counters)
324 int i;
326 fprintf (dump_file, "accu:%" PRId64, hist->hvalue.counters[0]);
327 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
329 fprintf (dump_file, " target:%" PRId64 " value:%" PRId64,
330 (int64_t) hist->hvalue.counters[i],
331 (int64_t) hist->hvalue.counters[i+1]);
334 fprintf (dump_file, ".\n");
335 break;
336 case HIST_TYPE_MAX:
337 gcc_unreachable ();
341 /* Dump information about HIST to DUMP_FILE. */
343 void
344 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
346 struct bitpack_d bp;
347 unsigned int i;
349 bp = bitpack_create (ob->main_stream);
350 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
351 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
352 streamer_write_bitpack (&bp);
353 switch (hist->type)
355 case HIST_TYPE_INTERVAL:
356 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
357 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
358 break;
359 default:
360 break;
362 for (i = 0; i < hist->n_counters; i++)
364 /* When user uses an unsigned type with a big value, constant converted
365 to gcov_type (a signed type) can be negative. */
366 gcov_type value = hist->hvalue.counters[i];
367 if (hist->type == HIST_TYPE_SINGLE_VALUE && i == 0)
369 else
370 gcc_assert (value >= 0);
372 streamer_write_gcov_count (ob, value);
374 if (hist->hvalue.next)
375 stream_out_histogram_value (ob, hist->hvalue.next);
378 /* Dump information about HIST to DUMP_FILE. */
380 void
381 stream_in_histogram_value (struct lto_input_block *ib, gimple *stmt)
383 enum hist_type type;
384 unsigned int ncounters = 0;
385 struct bitpack_d bp;
386 unsigned int i;
387 histogram_value new_val;
388 bool next;
389 histogram_value *next_p = NULL;
393 bp = streamer_read_bitpack (ib);
394 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
395 next = bp_unpack_value (&bp, 1);
396 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
397 switch (type)
399 case HIST_TYPE_INTERVAL:
400 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
401 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
402 ncounters = new_val->hdata.intvl.steps + 2;
403 break;
405 case HIST_TYPE_POW2:
406 case HIST_TYPE_AVERAGE:
407 ncounters = 2;
408 break;
410 case HIST_TYPE_SINGLE_VALUE:
411 case HIST_TYPE_INDIR_CALL:
412 ncounters = 3;
413 break;
415 case HIST_TYPE_IOR:
416 case HIST_TYPE_TIME_PROFILE:
417 ncounters = 1;
418 break;
420 case HIST_TYPE_INDIR_CALL_TOPN:
421 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
422 break;
424 case HIST_TYPE_MAX:
425 gcc_unreachable ();
427 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
428 new_val->n_counters = ncounters;
429 for (i = 0; i < ncounters; i++)
430 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
431 if (!next_p)
432 gimple_add_histogram_value (cfun, stmt, new_val);
433 else
434 *next_p = new_val;
435 next_p = &new_val->hvalue.next;
437 while (next);
440 /* Dump all histograms attached to STMT to DUMP_FILE. */
442 void
443 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple *stmt)
445 histogram_value hist;
446 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
447 dump_histogram_value (dump_file, hist);
450 /* Remove all histograms associated with STMT. */
452 void
453 gimple_remove_stmt_histograms (struct function *fun, gimple *stmt)
455 histogram_value val;
456 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
457 gimple_remove_histogram_value (fun, stmt, val);
460 /* Duplicate all histograms associates with OSTMT to STMT. */
462 void
463 gimple_duplicate_stmt_histograms (struct function *fun, gimple *stmt,
464 struct function *ofun, gimple *ostmt)
466 histogram_value val;
467 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
469 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
470 memcpy (new_val, val, sizeof (*val));
471 new_val->hvalue.stmt = stmt;
472 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
473 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
474 gimple_add_histogram_value (fun, stmt, new_val);
478 /* Move all histograms associated with OSTMT to STMT. */
480 void
481 gimple_move_stmt_histograms (struct function *fun, gimple *stmt, gimple *ostmt)
483 histogram_value val = gimple_histogram_value (fun, ostmt);
484 if (val)
486 /* The following three statements can't be reordered,
487 because histogram hashtab relies on stmt field value
488 for finding the exact slot. */
489 set_histogram_value (fun, ostmt, NULL);
490 for (; val != NULL; val = val->hvalue.next)
491 val->hvalue.stmt = stmt;
492 set_histogram_value (fun, stmt, val);
496 static bool error_found = false;
498 /* Helper function for verify_histograms. For each histogram reachable via htab
499 walk verify that it was reached via statement walk. */
501 static int
502 visit_hist (void **slot, void *data)
504 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
505 histogram_value hist = *(histogram_value *) slot;
507 if (!visited->contains (hist)
508 && hist->type != HIST_TYPE_TIME_PROFILE)
510 error ("dead histogram");
511 dump_histogram_value (stderr, hist);
512 debug_gimple_stmt (hist->hvalue.stmt);
513 error_found = true;
515 return 1;
518 /* Verify sanity of the histograms. */
520 DEBUG_FUNCTION void
521 verify_histograms (void)
523 basic_block bb;
524 gimple_stmt_iterator gsi;
525 histogram_value hist;
527 error_found = false;
528 hash_set<histogram_value> visited_hists;
529 FOR_EACH_BB_FN (bb, cfun)
530 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
532 gimple *stmt = gsi_stmt (gsi);
534 for (hist = gimple_histogram_value (cfun, stmt); hist;
535 hist = hist->hvalue.next)
537 if (hist->hvalue.stmt != stmt)
539 error ("Histogram value statement does not correspond to "
540 "the statement it is associated with");
541 debug_gimple_stmt (stmt);
542 dump_histogram_value (stderr, hist);
543 error_found = true;
545 visited_hists.add (hist);
548 if (VALUE_HISTOGRAMS (cfun))
549 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
550 if (error_found)
551 internal_error ("verify_histograms failed");
554 /* Helper function for verify_histograms. For each histogram reachable via htab
555 walk verify that it was reached via statement walk. */
557 static int
558 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
560 histogram_value hist = *(histogram_value *) slot;
561 free (hist->hvalue.counters);
562 free (hist);
563 return 1;
566 void
567 free_histograms (struct function *fn)
569 if (VALUE_HISTOGRAMS (fn))
571 htab_traverse (VALUE_HISTOGRAMS (fn), free_hist, NULL);
572 htab_delete (VALUE_HISTOGRAMS (fn));
573 VALUE_HISTOGRAMS (fn) = NULL;
577 /* The overall number of invocations of the counter should match
578 execution count of basic block. Report it as error rather than
579 internal error as it might mean that user has misused the profile
580 somehow. */
582 static bool
583 check_counter (gimple *stmt, const char * name,
584 gcov_type *count, gcov_type *all, profile_count bb_count_d)
586 gcov_type bb_count = bb_count_d.to_gcov_type ();
587 if (*all != bb_count || *count > *all)
589 location_t locus;
590 locus = (stmt != NULL)
591 ? gimple_location (stmt)
592 : DECL_SOURCE_LOCATION (current_function_decl);
593 if (flag_profile_correction)
595 if (dump_enabled_p ())
596 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
597 "correcting inconsistent value profile: %s "
598 "profiler overall count (%d) does not match BB "
599 "count (%d)\n", name, (int)*all, (int)bb_count);
600 *all = bb_count;
601 if (*count > *all)
602 *count = *all;
603 return false;
605 else
607 error_at (locus, "corrupted value profile: %s "
608 "profile counter (%d out of %d) inconsistent with "
609 "basic-block count (%d)",
610 name,
611 (int) *count,
612 (int) *all,
613 (int) bb_count);
614 return true;
618 return false;
621 /* GIMPLE based transformations. */
623 bool
624 gimple_value_profile_transformations (void)
626 basic_block bb;
627 gimple_stmt_iterator gsi;
628 bool changed = false;
630 /* Autofdo does its own transformations for indirect calls,
631 and otherwise does not support value profiling. */
632 if (flag_auto_profile)
633 return false;
635 FOR_EACH_BB_FN (bb, cfun)
637 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
639 gimple *stmt = gsi_stmt (gsi);
640 histogram_value th = gimple_histogram_value (cfun, stmt);
641 if (!th)
642 continue;
644 if (dump_file)
646 fprintf (dump_file, "Trying transformations on stmt ");
647 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
648 dump_histograms_for_stmt (cfun, dump_file, stmt);
651 /* Transformations: */
652 /* The order of things in this conditional controls which
653 transformation is used when more than one is applicable. */
654 /* It is expected that any code added by the transformations
655 will be added before the current statement, and that the
656 current statement remain valid (although possibly
657 modified) upon return. */
658 if (gimple_mod_subtract_transform (&gsi)
659 || gimple_divmod_fixed_value_transform (&gsi)
660 || gimple_mod_pow2_value_transform (&gsi)
661 || gimple_stringops_transform (&gsi)
662 || gimple_ic_transform (&gsi))
664 stmt = gsi_stmt (gsi);
665 changed = true;
666 /* Original statement may no longer be in the same block. */
667 if (bb != gimple_bb (stmt))
669 bb = gimple_bb (stmt);
670 gsi = gsi_for_stmt (stmt);
676 if (changed)
678 counts_to_freqs ();
681 return changed;
684 /* Generate code for transformation 1 (with parent gimple assignment
685 STMT and probability of taking the optimal path PROB, which is
686 equivalent to COUNT/ALL within roundoff error). This generates the
687 result into a temp and returns the temp; it does not replace or
688 alter the original STMT. */
690 static tree
691 gimple_divmod_fixed_value (gassign *stmt, tree value, profile_probability prob,
692 gcov_type count, gcov_type all)
694 gassign *stmt1, *stmt2;
695 gcond *stmt3;
696 tree tmp0, tmp1, tmp2;
697 gimple *bb1end, *bb2end, *bb3end;
698 basic_block bb, bb2, bb3, bb4;
699 tree optype, op1, op2;
700 edge e12, e13, e23, e24, e34;
701 gimple_stmt_iterator gsi;
703 gcc_assert (is_gimple_assign (stmt)
704 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
705 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
707 optype = TREE_TYPE (gimple_assign_lhs (stmt));
708 op1 = gimple_assign_rhs1 (stmt);
709 op2 = gimple_assign_rhs2 (stmt);
711 bb = gimple_bb (stmt);
712 gsi = gsi_for_stmt (stmt);
714 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
715 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
716 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
717 stmt2 = gimple_build_assign (tmp1, op2);
718 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
719 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
720 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
721 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
722 bb1end = stmt3;
724 tmp2 = create_tmp_reg (optype, "PROF");
725 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
726 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
727 bb2end = stmt1;
729 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
730 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
731 bb3end = stmt1;
733 /* Fix CFG. */
734 /* Edge e23 connects bb2 to bb3, etc. */
735 e12 = split_block (bb, bb1end);
736 bb2 = e12->dest;
737 bb2->count = profile_count::from_gcov_type (count);
738 e23 = split_block (bb2, bb2end);
739 bb3 = e23->dest;
740 bb3->count = profile_count::from_gcov_type (all - count);
741 e34 = split_block (bb3, bb3end);
742 bb4 = e34->dest;
743 bb4->count = profile_count::from_gcov_type (all);
745 e12->flags &= ~EDGE_FALLTHRU;
746 e12->flags |= EDGE_FALSE_VALUE;
747 e12->probability = prob;
748 e12->count = profile_count::from_gcov_type (count);
750 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
751 e13->probability = prob.invert ();
752 e13->count = profile_count::from_gcov_type (all - count);
754 remove_edge (e23);
756 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
757 e24->probability = profile_probability::always ();
758 e24->count = profile_count::from_gcov_type (count);
760 e34->probability = profile_probability::always ();
761 e34->count = profile_count::from_gcov_type (all - count);
763 return tmp2;
766 /* Do transform 1) on INSN if applicable. */
768 static bool
769 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
771 histogram_value histogram;
772 enum tree_code code;
773 gcov_type val, count, all;
774 tree result, value, tree_val;
775 profile_probability prob;
776 gassign *stmt;
778 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
779 if (!stmt)
780 return false;
782 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
783 return false;
785 code = gimple_assign_rhs_code (stmt);
787 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
788 return false;
790 histogram = gimple_histogram_value_of_type (cfun, stmt,
791 HIST_TYPE_SINGLE_VALUE);
792 if (!histogram)
793 return false;
795 value = histogram->hvalue.value;
796 val = histogram->hvalue.counters[0];
797 count = histogram->hvalue.counters[1];
798 all = histogram->hvalue.counters[2];
799 gimple_remove_histogram_value (cfun, stmt, histogram);
801 /* We require that count is at least half of all; this means
802 that for the transformation to fire the value must be constant
803 at least 50% of time (and 75% gives the guarantee of usage). */
804 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
805 || 2 * count < all
806 || optimize_bb_for_size_p (gimple_bb (stmt)))
807 return false;
809 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
810 return false;
812 /* Compute probability of taking the optimal path. */
813 if (all > 0)
814 prob = profile_probability::probability_in_gcov_type (count, all);
815 else
816 prob = profile_probability::never ();
818 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
819 tree_val = build_int_cst (get_gcov_type (), val);
820 else
822 HOST_WIDE_INT a[2];
823 a[0] = (unsigned HOST_WIDE_INT) val;
824 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
826 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
827 TYPE_PRECISION (get_gcov_type ()), false));
829 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
831 if (dump_file)
833 fprintf (dump_file, "Div/mod by constant ");
834 print_generic_expr (dump_file, value, TDF_SLIM);
835 fprintf (dump_file, "=");
836 print_generic_expr (dump_file, tree_val, TDF_SLIM);
837 fprintf (dump_file, " transformation on insn ");
838 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
841 gimple_assign_set_rhs_from_tree (si, result);
842 update_stmt (gsi_stmt (*si));
844 return true;
847 /* Generate code for transformation 2 (with parent gimple assign STMT and
848 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
849 within roundoff error). This generates the result into a temp and returns
850 the temp; it does not replace or alter the original STMT. */
852 static tree
853 gimple_mod_pow2 (gassign *stmt, profile_probability prob, gcov_type count, gcov_type all)
855 gassign *stmt1, *stmt2, *stmt3;
856 gcond *stmt4;
857 tree tmp2, tmp3;
858 gimple *bb1end, *bb2end, *bb3end;
859 basic_block bb, bb2, bb3, bb4;
860 tree optype, op1, op2;
861 edge e12, e13, e23, e24, e34;
862 gimple_stmt_iterator gsi;
863 tree result;
865 gcc_assert (is_gimple_assign (stmt)
866 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
868 optype = TREE_TYPE (gimple_assign_lhs (stmt));
869 op1 = gimple_assign_rhs1 (stmt);
870 op2 = gimple_assign_rhs2 (stmt);
872 bb = gimple_bb (stmt);
873 gsi = gsi_for_stmt (stmt);
875 result = create_tmp_reg (optype, "PROF");
876 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
877 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
878 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
879 build_int_cst (optype, -1));
880 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
881 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
882 NULL_TREE, NULL_TREE);
883 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
884 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
885 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
886 bb1end = stmt4;
888 /* tmp2 == op2-1 inherited from previous block. */
889 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
890 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
891 bb2end = stmt1;
893 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
894 op1, op2);
895 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
896 bb3end = stmt1;
898 /* Fix CFG. */
899 /* Edge e23 connects bb2 to bb3, etc. */
900 e12 = split_block (bb, bb1end);
901 bb2 = e12->dest;
902 bb2->count = profile_count::from_gcov_type (count);
903 e23 = split_block (bb2, bb2end);
904 bb3 = e23->dest;
905 bb3->count = profile_count::from_gcov_type (all - count);
906 e34 = split_block (bb3, bb3end);
907 bb4 = e34->dest;
908 bb4->count = profile_count::from_gcov_type (all);
910 e12->flags &= ~EDGE_FALLTHRU;
911 e12->flags |= EDGE_FALSE_VALUE;
912 e12->probability = prob;
913 e12->count = profile_count::from_gcov_type (count);
915 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
916 e13->probability = prob.invert ();
917 e13->count = profile_count::from_gcov_type (all - count);
919 remove_edge (e23);
921 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
922 e24->probability = profile_probability::always ();
923 e24->count = profile_count::from_gcov_type (count);
925 e34->probability = profile_probability::always ();
926 e34->count = profile_count::from_gcov_type (all - count);
928 return result;
931 /* Do transform 2) on INSN if applicable. */
933 static bool
934 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
936 histogram_value histogram;
937 enum tree_code code;
938 gcov_type count, wrong_values, all;
939 tree lhs_type, result, value;
940 profile_probability prob;
941 gassign *stmt;
943 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
944 if (!stmt)
945 return false;
947 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
948 if (!INTEGRAL_TYPE_P (lhs_type))
949 return false;
951 code = gimple_assign_rhs_code (stmt);
953 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
954 return false;
956 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
957 if (!histogram)
958 return false;
960 value = histogram->hvalue.value;
961 wrong_values = histogram->hvalue.counters[0];
962 count = histogram->hvalue.counters[1];
964 gimple_remove_histogram_value (cfun, stmt, histogram);
966 /* We require that we hit a power of 2 at least half of all evaluations. */
967 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
968 || count < wrong_values
969 || optimize_bb_for_size_p (gimple_bb (stmt)))
970 return false;
972 if (dump_file)
974 fprintf (dump_file, "Mod power of 2 transformation on insn ");
975 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
978 /* Compute probability of taking the optimal path. */
979 all = count + wrong_values;
981 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
982 return false;
984 if (all > 0)
985 prob = profile_probability::probability_in_gcov_type (count, all);
986 else
987 prob = profile_probability::never ();
989 result = gimple_mod_pow2 (stmt, prob, count, all);
991 gimple_assign_set_rhs_from_tree (si, result);
992 update_stmt (gsi_stmt (*si));
994 return true;
997 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
998 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
999 supported and this is built into this interface. The probabilities of taking
1000 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1001 COUNT2/ALL respectively within roundoff error). This generates the
1002 result into a temp and returns the temp; it does not replace or alter
1003 the original STMT. */
1004 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1006 static tree
1007 gimple_mod_subtract (gassign *stmt, profile_probability prob1,
1008 profile_probability prob2, int ncounts,
1009 gcov_type count1, gcov_type count2, gcov_type all)
1011 gassign *stmt1;
1012 gimple *stmt2;
1013 gcond *stmt3;
1014 tree tmp1;
1015 gimple *bb1end, *bb2end = NULL, *bb3end;
1016 basic_block bb, bb2, bb3, bb4;
1017 tree optype, op1, op2;
1018 edge e12, e23 = 0, e24, e34, e14;
1019 gimple_stmt_iterator gsi;
1020 tree result;
1022 gcc_assert (is_gimple_assign (stmt)
1023 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1025 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1026 op1 = gimple_assign_rhs1 (stmt);
1027 op2 = gimple_assign_rhs2 (stmt);
1029 bb = gimple_bb (stmt);
1030 gsi = gsi_for_stmt (stmt);
1032 result = create_tmp_reg (optype, "PROF");
1033 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1034 stmt1 = gimple_build_assign (result, op1);
1035 stmt2 = gimple_build_assign (tmp1, op2);
1036 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1037 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1038 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1039 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1040 bb1end = stmt3;
1042 if (ncounts) /* Assumed to be 0 or 1 */
1044 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1045 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1046 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1047 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1048 bb2end = stmt2;
1051 /* Fallback case. */
1052 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1053 result, tmp1);
1054 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1055 bb3end = stmt1;
1057 /* Fix CFG. */
1058 /* Edge e23 connects bb2 to bb3, etc. */
1059 /* However block 3 is optional; if it is not there, references
1060 to 3 really refer to block 2. */
1061 e12 = split_block (bb, bb1end);
1062 bb2 = e12->dest;
1063 bb2->count = profile_count::from_gcov_type (all - count1);
1065 if (ncounts) /* Assumed to be 0 or 1. */
1067 e23 = split_block (bb2, bb2end);
1068 bb3 = e23->dest;
1069 bb3->count = profile_count::from_gcov_type (all - count1 - count2);
1072 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1073 bb4 = e34->dest;
1074 bb4->count = profile_count::from_gcov_type (all);
1076 e12->flags &= ~EDGE_FALLTHRU;
1077 e12->flags |= EDGE_FALSE_VALUE;
1078 e12->probability = prob1.invert ();
1079 e12->count = profile_count::from_gcov_type (all - count1);
1081 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1082 e14->probability = prob1;
1083 e14->count = profile_count::from_gcov_type (count1);
1085 if (ncounts) /* Assumed to be 0 or 1. */
1087 e23->flags &= ~EDGE_FALLTHRU;
1088 e23->flags |= EDGE_FALSE_VALUE;
1089 e23->count = profile_count::from_gcov_type (all - count1 - count2);
1090 e23->probability = prob2.invert ();
1092 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1093 e24->probability = prob2;
1094 e24->count = profile_count::from_gcov_type (count2);
1097 e34->probability = profile_probability::always ();
1098 e34->count = profile_count::from_gcov_type (all - count1 - count2);
1100 return result;
1103 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1105 static bool
1106 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1108 histogram_value histogram;
1109 enum tree_code code;
1110 gcov_type count, wrong_values, all;
1111 tree lhs_type, result;
1112 profile_probability prob1, prob2;
1113 unsigned int i, steps;
1114 gcov_type count1, count2;
1115 gassign *stmt;
1116 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1117 if (!stmt)
1118 return false;
1120 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1121 if (!INTEGRAL_TYPE_P (lhs_type))
1122 return false;
1124 code = gimple_assign_rhs_code (stmt);
1126 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1127 return false;
1129 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1130 if (!histogram)
1131 return false;
1133 all = 0;
1134 wrong_values = 0;
1135 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1136 all += histogram->hvalue.counters[i];
1138 wrong_values += histogram->hvalue.counters[i];
1139 wrong_values += histogram->hvalue.counters[i+1];
1140 steps = histogram->hdata.intvl.steps;
1141 all += wrong_values;
1142 count1 = histogram->hvalue.counters[0];
1143 count2 = histogram->hvalue.counters[1];
1145 /* Compute probability of taking the optimal path. */
1146 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1148 gimple_remove_histogram_value (cfun, stmt, histogram);
1149 return false;
1152 if (flag_profile_correction && count1 + count2 > all)
1153 all = count1 + count2;
1155 gcc_assert (count1 + count2 <= all);
1157 /* We require that we use just subtractions in at least 50% of all
1158 evaluations. */
1159 count = 0;
1160 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1162 count += histogram->hvalue.counters[i];
1163 if (count * 2 >= all)
1164 break;
1166 if (i == steps
1167 || optimize_bb_for_size_p (gimple_bb (stmt)))
1168 return false;
1170 gimple_remove_histogram_value (cfun, stmt, histogram);
1171 if (dump_file)
1173 fprintf (dump_file, "Mod subtract transformation on insn ");
1174 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1177 /* Compute probability of taking the optimal path(s). */
1178 if (all > 0)
1180 prob1 = profile_probability::probability_in_gcov_type (count1, all);
1181 prob2 = profile_probability::probability_in_gcov_type (count2, all);
1183 else
1185 prob1 = prob2 = profile_probability::never ();
1188 /* In practice, "steps" is always 2. This interface reflects this,
1189 and will need to be changed if "steps" can change. */
1190 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1192 gimple_assign_set_rhs_from_tree (si, result);
1193 update_stmt (gsi_stmt (*si));
1195 return true;
1198 typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
1200 static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
1202 /* Returns true if node graph is initialized. This
1203 is used to test if profile_id has been created
1204 for cgraph_nodes. */
1206 bool
1207 coverage_node_map_initialized_p (void)
1209 return cgraph_node_map != 0;
1212 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1213 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1214 that the PROFILE_IDs was already assigned. */
1216 void
1217 init_node_map (bool local)
1219 struct cgraph_node *n;
1220 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
1222 FOR_EACH_DEFINED_FUNCTION (n)
1223 if (n->has_gimple_body_p ())
1225 cgraph_node **val;
1226 if (local)
1228 n->profile_id = coverage_compute_profile_id (n);
1229 while ((val = cgraph_node_map->get (n->profile_id))
1230 || !n->profile_id)
1232 if (dump_file)
1233 fprintf (dump_file, "Local profile-id %i conflict"
1234 " with nodes %s %s\n",
1235 n->profile_id,
1236 n->dump_name (),
1237 (*val)->dump_name ());
1238 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1241 else if (!n->profile_id)
1243 if (dump_file)
1244 fprintf (dump_file,
1245 "Node %s has no profile-id"
1246 " (profile feedback missing?)\n",
1247 n->dump_name ());
1248 continue;
1250 else if ((val = cgraph_node_map->get (n->profile_id)))
1252 if (dump_file)
1253 fprintf (dump_file,
1254 "Node %s has IP profile-id %i conflict. "
1255 "Giving up.\n",
1256 n->dump_name (), n->profile_id);
1257 *val = NULL;
1258 continue;
1260 cgraph_node_map->put (n->profile_id, n);
1264 /* Delete the CGRAPH_NODE_MAP. */
1266 void
1267 del_node_map (void)
1269 delete cgraph_node_map;
1272 /* Return cgraph node for function with pid */
1274 struct cgraph_node*
1275 find_func_by_profile_id (int profile_id)
1277 cgraph_node **val = cgraph_node_map->get (profile_id);
1278 if (val)
1279 return *val;
1280 else
1281 return NULL;
1284 /* Perform sanity check on the indirect call target. Due to race conditions,
1285 false function target may be attributed to an indirect call site. If the
1286 call expression type mismatches with the target function's type, expand_call
1287 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1288 Returns true if TARGET is considered ok for call CALL_STMT. */
1290 bool
1291 check_ic_target (gcall *call_stmt, struct cgraph_node *target)
1293 location_t locus;
1294 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1295 return true;
1297 locus = gimple_location (call_stmt);
1298 if (dump_enabled_p ())
1299 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1300 "Skipping target %s with mismatching types for icall\n",
1301 target->name ());
1302 return false;
1305 /* Do transformation
1307 if (actual_callee_address == address_of_most_common_function/method)
1308 do direct call
1309 else
1310 old call
1313 gcall *
1314 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1315 profile_probability prob, profile_count count, profile_count all)
1317 gcall *dcall_stmt;
1318 gassign *load_stmt;
1319 gcond *cond_stmt;
1320 gcall *iretbnd_stmt = NULL;
1321 tree tmp0, tmp1, tmp;
1322 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1323 tree optype = build_pointer_type (void_type_node);
1324 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1325 gimple_stmt_iterator gsi;
1326 int lp_nr, dflags;
1327 edge e_eh, e;
1328 edge_iterator ei;
1329 gimple_stmt_iterator psi;
1331 cond_bb = gimple_bb (icall_stmt);
1332 gsi = gsi_for_stmt (icall_stmt);
1334 if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
1335 iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
1337 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1338 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1339 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1340 load_stmt = gimple_build_assign (tmp0, tmp);
1341 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1343 tmp = fold_convert (optype, build_addr (direct_call->decl));
1344 load_stmt = gimple_build_assign (tmp1, tmp);
1345 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1347 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1348 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1350 if (TREE_CODE (gimple_vdef (icall_stmt)) == SSA_NAME)
1352 unlink_stmt_vdef (icall_stmt);
1353 release_ssa_name (gimple_vdef (icall_stmt));
1355 gimple_set_vdef (icall_stmt, NULL_TREE);
1356 gimple_set_vuse (icall_stmt, NULL_TREE);
1357 update_stmt (icall_stmt);
1358 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1359 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1360 dflags = flags_from_decl_or_type (direct_call->decl);
1361 if ((dflags & ECF_NORETURN) != 0
1362 && should_remove_lhs_p (gimple_call_lhs (dcall_stmt)))
1363 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1364 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1366 /* Fix CFG. */
1367 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1368 e_cd = split_block (cond_bb, cond_stmt);
1369 dcall_bb = e_cd->dest;
1370 dcall_bb->count = count;
1372 e_di = split_block (dcall_bb, dcall_stmt);
1373 icall_bb = e_di->dest;
1374 icall_bb->count = all - count;
1376 /* Do not disturb existing EH edges from the indirect call. */
1377 if (!stmt_ends_bb_p (icall_stmt))
1378 e_ij = split_block (icall_bb, icall_stmt);
1379 else
1381 e_ij = find_fallthru_edge (icall_bb->succs);
1382 /* The indirect call might be noreturn. */
1383 if (e_ij != NULL)
1385 e_ij->probability = profile_probability::always ();
1386 e_ij->count = all - count;
1387 e_ij = single_pred_edge (split_edge (e_ij));
1390 if (e_ij != NULL)
1392 join_bb = e_ij->dest;
1393 join_bb->count = all;
1396 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1397 e_cd->probability = prob;
1398 e_cd->count = count;
1400 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1401 e_ci->probability = prob.invert ();
1402 e_ci->count = all - count;
1404 remove_edge (e_di);
1406 if (e_ij != NULL)
1408 if ((dflags & ECF_NORETURN) != 0)
1409 e_ij->count = all;
1410 else
1412 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1413 e_dj->probability = profile_probability::always ();
1414 e_dj->count = count;
1416 e_ij->count = all - count;
1418 e_ij->probability = profile_probability::always ();
1421 /* Insert PHI node for the call result if necessary. */
1422 if (gimple_call_lhs (icall_stmt)
1423 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1424 && (dflags & ECF_NORETURN) == 0)
1426 tree result = gimple_call_lhs (icall_stmt);
1427 gphi *phi = create_phi_node (result, join_bb);
1428 gimple_call_set_lhs (icall_stmt,
1429 duplicate_ssa_name (result, icall_stmt));
1430 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1431 gimple_call_set_lhs (dcall_stmt,
1432 duplicate_ssa_name (result, dcall_stmt));
1433 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1435 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1436 call then we need to make it's copy for the direct
1437 call. */
1438 if (iretbnd_stmt)
1440 if (gimple_call_lhs (iretbnd_stmt))
1442 gimple *copy;
1444 if (TREE_CODE (gimple_vdef (iretbnd_stmt)) == SSA_NAME)
1446 unlink_stmt_vdef (iretbnd_stmt);
1447 release_ssa_name (gimple_vdef (iretbnd_stmt));
1449 gimple_set_vdef (iretbnd_stmt, NULL_TREE);
1450 gimple_set_vuse (iretbnd_stmt, NULL_TREE);
1451 update_stmt (iretbnd_stmt);
1453 result = gimple_call_lhs (iretbnd_stmt);
1454 phi = create_phi_node (result, join_bb);
1456 copy = gimple_copy (iretbnd_stmt);
1457 gimple_call_set_arg (copy, 0,
1458 gimple_call_lhs (dcall_stmt));
1459 gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
1460 gsi_insert_on_edge (e_dj, copy);
1461 add_phi_arg (phi, gimple_call_lhs (copy),
1462 e_dj, UNKNOWN_LOCATION);
1464 gimple_call_set_arg (iretbnd_stmt, 0,
1465 gimple_call_lhs (icall_stmt));
1466 gimple_call_set_lhs (iretbnd_stmt,
1467 duplicate_ssa_name (result, iretbnd_stmt));
1468 psi = gsi_for_stmt (iretbnd_stmt);
1469 gsi_remove (&psi, false);
1470 gsi_insert_on_edge (e_ij, iretbnd_stmt);
1471 add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
1472 e_ij, UNKNOWN_LOCATION);
1474 gsi_commit_one_edge_insert (e_dj, NULL);
1475 gsi_commit_one_edge_insert (e_ij, NULL);
1477 else
1479 psi = gsi_for_stmt (iretbnd_stmt);
1480 gsi_remove (&psi, true);
1485 /* Build an EH edge for the direct call if necessary. */
1486 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1487 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1489 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1492 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1493 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1495 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1496 e->probability = e_eh->probability;
1497 e->count = e_eh->count;
1498 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1499 !gsi_end_p (psi); gsi_next (&psi))
1501 gphi *phi = psi.phi ();
1502 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1503 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1506 if (!stmt_could_throw_p (dcall_stmt))
1507 gimple_purge_dead_eh_edges (dcall_bb);
1508 return dcall_stmt;
1512 For every checked indirect/virtual call determine if most common pid of
1513 function/class method has probability more than 50%. If yes modify code of
1514 this call to:
1517 static bool
1518 gimple_ic_transform (gimple_stmt_iterator *gsi)
1520 gcall *stmt;
1521 histogram_value histogram;
1522 gcov_type val, count, all, bb_all;
1523 struct cgraph_node *direct_call;
1525 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1526 if (!stmt)
1527 return false;
1529 if (gimple_call_fndecl (stmt) != NULL_TREE)
1530 return false;
1532 if (gimple_call_internal_p (stmt))
1533 return false;
1535 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1536 if (!histogram)
1537 return false;
1539 val = histogram->hvalue.counters [0];
1540 count = histogram->hvalue.counters [1];
1541 all = histogram->hvalue.counters [2];
1543 bb_all = gimple_bb (stmt)->count.to_gcov_type ();
1544 /* The order of CHECK_COUNTER calls is important -
1545 since check_counter can correct the third parameter
1546 and we want to make count <= all <= bb_all. */
1547 if (check_counter (stmt, "ic", &all, &bb_all, gimple_bb (stmt)->count)
1548 || check_counter (stmt, "ic", &count, &all,
1549 profile_count::from_gcov_type (all)))
1551 gimple_remove_histogram_value (cfun, stmt, histogram);
1552 return false;
1555 if (4 * count <= 3 * all)
1556 return false;
1558 direct_call = find_func_by_profile_id ((int)val);
1560 if (direct_call == NULL)
1562 if (val)
1564 if (dump_file)
1566 fprintf (dump_file, "Indirect call -> direct call from other module");
1567 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1568 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1571 return false;
1574 if (!check_ic_target (stmt, direct_call))
1576 if (dump_file)
1578 fprintf (dump_file, "Indirect call -> direct call ");
1579 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1580 fprintf (dump_file, "=> ");
1581 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1582 fprintf (dump_file, " transformation skipped because of type mismatch");
1583 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1585 gimple_remove_histogram_value (cfun, stmt, histogram);
1586 return false;
1589 if (dump_file)
1591 fprintf (dump_file, "Indirect call -> direct call ");
1592 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1593 fprintf (dump_file, "=> ");
1594 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1595 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1596 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1597 fprintf (dump_file, "hist->count %" PRId64
1598 " hist->all %" PRId64"\n", count, all);
1601 return true;
1604 /* Return true if the stringop CALL shall be profiled. SIZE_ARG be
1605 set to the argument index for the size of the string operation. */
1607 static bool
1608 interesting_stringop_to_profile_p (gcall *call, int *size_arg)
1610 enum built_in_function fcode;
1612 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (call));
1613 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1614 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1615 return false;
1617 switch (fcode)
1619 case BUILT_IN_MEMCPY:
1620 case BUILT_IN_MEMPCPY:
1621 *size_arg = 2;
1622 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1623 INTEGER_TYPE, VOID_TYPE);
1624 case BUILT_IN_MEMSET:
1625 *size_arg = 2;
1626 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1627 INTEGER_TYPE, VOID_TYPE);
1628 case BUILT_IN_BZERO:
1629 *size_arg = 1;
1630 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1631 VOID_TYPE);
1632 default:
1633 gcc_unreachable ();
1637 /* Convert stringop (..., vcall_size)
1638 into
1639 if (vcall_size == icall_size)
1640 stringop (..., icall_size);
1641 else
1642 stringop (..., vcall_size);
1643 assuming we'll propagate a true constant into ICALL_SIZE later. */
1645 static void
1646 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, profile_probability prob,
1647 gcov_type count, gcov_type all)
1649 gassign *tmp_stmt;
1650 gcond *cond_stmt;
1651 gcall *icall_stmt;
1652 tree tmp0, tmp1, vcall_size, optype;
1653 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1654 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1655 gimple_stmt_iterator gsi;
1656 int size_arg;
1658 if (!interesting_stringop_to_profile_p (vcall_stmt, &size_arg))
1659 gcc_unreachable ();
1661 cond_bb = gimple_bb (vcall_stmt);
1662 gsi = gsi_for_stmt (vcall_stmt);
1664 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1665 optype = TREE_TYPE (vcall_size);
1667 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1668 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1669 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1670 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1672 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1673 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1675 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1676 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1678 if (TREE_CODE (gimple_vdef (vcall_stmt)) == SSA_NAME)
1680 unlink_stmt_vdef (vcall_stmt);
1681 release_ssa_name (gimple_vdef (vcall_stmt));
1683 gimple_set_vdef (vcall_stmt, NULL);
1684 gimple_set_vuse (vcall_stmt, NULL);
1685 update_stmt (vcall_stmt);
1686 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1687 gimple_call_set_arg (icall_stmt, size_arg,
1688 fold_convert (optype, icall_size));
1689 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1691 /* Fix CFG. */
1692 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1693 e_ci = split_block (cond_bb, cond_stmt);
1694 icall_bb = e_ci->dest;
1695 icall_bb->count = profile_count::from_gcov_type (count);
1697 e_iv = split_block (icall_bb, icall_stmt);
1698 vcall_bb = e_iv->dest;
1699 vcall_bb->count = profile_count::from_gcov_type (all - count);
1701 e_vj = split_block (vcall_bb, vcall_stmt);
1702 join_bb = e_vj->dest;
1703 join_bb->count = profile_count::from_gcov_type (all);
1705 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1706 e_ci->probability = prob;
1707 e_ci->count = profile_count::from_gcov_type (count);
1709 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1710 e_cv->probability = prob.invert ();
1711 e_cv->count = profile_count::from_gcov_type (all - count);
1713 remove_edge (e_iv);
1715 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1716 e_ij->probability = profile_probability::always ();
1717 e_ij->count = profile_count::from_gcov_type (count);
1719 e_vj->probability = profile_probability::always ();
1720 e_vj->count = profile_count::from_gcov_type (all - count);
1722 /* Insert PHI node for the call result if necessary. */
1723 if (gimple_call_lhs (vcall_stmt)
1724 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1726 tree result = gimple_call_lhs (vcall_stmt);
1727 gphi *phi = create_phi_node (result, join_bb);
1728 gimple_call_set_lhs (vcall_stmt,
1729 duplicate_ssa_name (result, vcall_stmt));
1730 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1731 gimple_call_set_lhs (icall_stmt,
1732 duplicate_ssa_name (result, icall_stmt));
1733 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1736 /* Because these are all string op builtins, they're all nothrow. */
1737 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1738 gcc_assert (!stmt_could_throw_p (icall_stmt));
1741 /* Find values inside STMT for that we want to measure histograms for
1742 division/modulo optimization. */
1744 static bool
1745 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1747 gcall *stmt;
1748 tree blck_size;
1749 enum built_in_function fcode;
1750 histogram_value histogram;
1751 gcov_type count, all, val;
1752 tree dest, src;
1753 unsigned int dest_align, src_align;
1754 profile_probability prob;
1755 tree tree_val;
1756 int size_arg;
1758 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1759 if (!stmt)
1760 return false;
1762 if (!gimple_call_builtin_p (gsi_stmt (*gsi), BUILT_IN_NORMAL))
1763 return false;
1765 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
1766 return false;
1768 blck_size = gimple_call_arg (stmt, size_arg);
1769 if (TREE_CODE (blck_size) == INTEGER_CST)
1770 return false;
1772 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1773 if (!histogram)
1774 return false;
1776 val = histogram->hvalue.counters[0];
1777 count = histogram->hvalue.counters[1];
1778 all = histogram->hvalue.counters[2];
1779 gimple_remove_histogram_value (cfun, stmt, histogram);
1781 /* We require that count is at least half of all; this means
1782 that for the transformation to fire the value must be constant
1783 at least 80% of time. */
1784 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1785 return false;
1786 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1787 return false;
1788 if (all > 0)
1789 prob = profile_probability::probability_in_gcov_type (count, all);
1790 else
1791 prob = profile_probability::never ();
1793 dest = gimple_call_arg (stmt, 0);
1794 dest_align = get_pointer_alignment (dest);
1795 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (stmt));
1796 switch (fcode)
1798 case BUILT_IN_MEMCPY:
1799 case BUILT_IN_MEMPCPY:
1800 src = gimple_call_arg (stmt, 1);
1801 src_align = get_pointer_alignment (src);
1802 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1803 return false;
1804 break;
1805 case BUILT_IN_MEMSET:
1806 if (!can_store_by_pieces (val, builtin_memset_read_str,
1807 gimple_call_arg (stmt, 1),
1808 dest_align, true))
1809 return false;
1810 break;
1811 case BUILT_IN_BZERO:
1812 if (!can_store_by_pieces (val, builtin_memset_read_str,
1813 integer_zero_node,
1814 dest_align, true))
1815 return false;
1816 break;
1817 default:
1818 gcc_unreachable ();
1821 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1822 tree_val = build_int_cst (get_gcov_type (), val);
1823 else
1825 HOST_WIDE_INT a[2];
1826 a[0] = (unsigned HOST_WIDE_INT) val;
1827 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1829 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1830 TYPE_PRECISION (get_gcov_type ()), false));
1833 if (dump_file)
1835 fprintf (dump_file, "Single value %i stringop transformation on ",
1836 (int)val);
1837 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1840 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1842 return true;
1845 void
1846 stringop_block_profile (gimple *stmt, unsigned int *expected_align,
1847 HOST_WIDE_INT *expected_size)
1849 histogram_value histogram;
1850 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1852 if (!histogram)
1853 *expected_size = -1;
1854 else if (!histogram->hvalue.counters[1])
1856 *expected_size = -1;
1857 gimple_remove_histogram_value (cfun, stmt, histogram);
1859 else
1861 gcov_type size;
1862 size = ((histogram->hvalue.counters[0]
1863 + histogram->hvalue.counters[1] / 2)
1864 / histogram->hvalue.counters[1]);
1865 /* Even if we can hold bigger value in SIZE, INT_MAX
1866 is safe "infinity" for code generation strategies. */
1867 if (size > INT_MAX)
1868 size = INT_MAX;
1869 *expected_size = size;
1870 gimple_remove_histogram_value (cfun, stmt, histogram);
1873 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1875 if (!histogram)
1876 *expected_align = 0;
1877 else if (!histogram->hvalue.counters[0])
1879 gimple_remove_histogram_value (cfun, stmt, histogram);
1880 *expected_align = 0;
1882 else
1884 gcov_type count;
1885 unsigned int alignment;
1887 count = histogram->hvalue.counters[0];
1888 alignment = 1;
1889 while (!(count & alignment)
1890 && (alignment <= UINT_MAX / 2 / BITS_PER_UNIT))
1891 alignment <<= 1;
1892 *expected_align = alignment * BITS_PER_UNIT;
1893 gimple_remove_histogram_value (cfun, stmt, histogram);
1898 /* Find values inside STMT for that we want to measure histograms for
1899 division/modulo optimization. */
1901 static void
1902 gimple_divmod_values_to_profile (gimple *stmt, histogram_values *values)
1904 tree lhs, divisor, op0, type;
1905 histogram_value hist;
1907 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1908 return;
1910 lhs = gimple_assign_lhs (stmt);
1911 type = TREE_TYPE (lhs);
1912 if (!INTEGRAL_TYPE_P (type))
1913 return;
1915 switch (gimple_assign_rhs_code (stmt))
1917 case TRUNC_DIV_EXPR:
1918 case TRUNC_MOD_EXPR:
1919 divisor = gimple_assign_rhs2 (stmt);
1920 op0 = gimple_assign_rhs1 (stmt);
1922 values->reserve (3);
1924 if (TREE_CODE (divisor) == SSA_NAME)
1925 /* Check for the case where the divisor is the same value most
1926 of the time. */
1927 values->quick_push (gimple_alloc_histogram_value (cfun,
1928 HIST_TYPE_SINGLE_VALUE,
1929 stmt, divisor));
1931 /* For mod, check whether it is not often a noop (or replaceable by
1932 a few subtractions). */
1933 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1934 && TYPE_UNSIGNED (type)
1935 && TREE_CODE (divisor) == SSA_NAME)
1937 tree val;
1938 /* Check for a special case where the divisor is power of 2. */
1939 values->quick_push (gimple_alloc_histogram_value (cfun,
1940 HIST_TYPE_POW2,
1941 stmt, divisor));
1943 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1944 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1945 stmt, val);
1946 hist->hdata.intvl.int_start = 0;
1947 hist->hdata.intvl.steps = 2;
1948 values->quick_push (hist);
1950 return;
1952 default:
1953 return;
1957 /* Find calls inside STMT for that we want to measure histograms for
1958 indirect/virtual call optimization. */
1960 static void
1961 gimple_indirect_call_to_profile (gimple *stmt, histogram_values *values)
1963 tree callee;
1965 if (gimple_code (stmt) != GIMPLE_CALL
1966 || gimple_call_internal_p (stmt)
1967 || gimple_call_fndecl (stmt) != NULL_TREE)
1968 return;
1970 callee = gimple_call_fn (stmt);
1972 values->reserve (3);
1974 values->quick_push (gimple_alloc_histogram_value (
1975 cfun,
1976 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
1977 HIST_TYPE_INDIR_CALL_TOPN :
1978 HIST_TYPE_INDIR_CALL,
1979 stmt, callee));
1981 return;
1984 /* Find values inside STMT for that we want to measure histograms for
1985 string operations. */
1987 static void
1988 gimple_stringops_values_to_profile (gimple *gs, histogram_values *values)
1990 gcall *stmt;
1991 tree blck_size;
1992 tree dest;
1993 int size_arg;
1995 stmt = dyn_cast <gcall *> (gs);
1996 if (!stmt)
1997 return;
1999 if (!gimple_call_builtin_p (gs, BUILT_IN_NORMAL))
2000 return;
2002 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
2003 return;
2005 dest = gimple_call_arg (stmt, 0);
2006 blck_size = gimple_call_arg (stmt, size_arg);
2008 if (TREE_CODE (blck_size) != INTEGER_CST)
2010 values->safe_push (gimple_alloc_histogram_value (cfun,
2011 HIST_TYPE_SINGLE_VALUE,
2012 stmt, blck_size));
2013 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2014 stmt, blck_size));
2017 if (TREE_CODE (blck_size) != INTEGER_CST)
2018 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2019 stmt, dest));
2022 /* Find values inside STMT for that we want to measure histograms and adds
2023 them to list VALUES. */
2025 static void
2026 gimple_values_to_profile (gimple *stmt, histogram_values *values)
2028 gimple_divmod_values_to_profile (stmt, values);
2029 gimple_stringops_values_to_profile (stmt, values);
2030 gimple_indirect_call_to_profile (stmt, values);
2033 void
2034 gimple_find_values_to_profile (histogram_values *values)
2036 basic_block bb;
2037 gimple_stmt_iterator gsi;
2038 unsigned i;
2039 histogram_value hist = NULL;
2040 values->create (0);
2042 FOR_EACH_BB_FN (bb, cfun)
2043 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2044 gimple_values_to_profile (gsi_stmt (gsi), values);
2046 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2048 FOR_EACH_VEC_ELT (*values, i, hist)
2050 switch (hist->type)
2052 case HIST_TYPE_INTERVAL:
2053 hist->n_counters = hist->hdata.intvl.steps + 2;
2054 break;
2056 case HIST_TYPE_POW2:
2057 hist->n_counters = 2;
2058 break;
2060 case HIST_TYPE_SINGLE_VALUE:
2061 hist->n_counters = 3;
2062 break;
2064 case HIST_TYPE_INDIR_CALL:
2065 hist->n_counters = 3;
2066 break;
2068 case HIST_TYPE_TIME_PROFILE:
2069 hist->n_counters = 1;
2070 break;
2072 case HIST_TYPE_AVERAGE:
2073 hist->n_counters = 2;
2074 break;
2076 case HIST_TYPE_IOR:
2077 hist->n_counters = 1;
2078 break;
2080 case HIST_TYPE_INDIR_CALL_TOPN:
2081 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2082 break;
2084 default:
2085 gcc_unreachable ();
2087 if (dump_file)
2089 fprintf (dump_file, "Stmt ");
2090 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2091 dump_histogram_value (dump_file, hist);