2017-02-20 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / value-prof.c
blob097e4094095b1bf9c328b2af976206b28f44180e
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 andwith making gimple_find_values_to_profile
104 and gimple_value_profile_transformations table-driven, perhaps...
107 static tree gimple_divmod_fixed_value (gassign *, tree, int, gcov_type,
108 gcov_type);
109 static tree gimple_mod_pow2 (gassign *, int, gcov_type, gcov_type);
110 static tree gimple_mod_subtract (gassign *, int, int, int, gcov_type,
111 gcov_type, gcov_type);
112 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
113 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
114 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
115 static bool gimple_stringops_transform (gimple_stmt_iterator *);
116 static bool gimple_ic_transform (gimple_stmt_iterator *);
118 /* Allocate histogram value. */
120 histogram_value
121 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
122 enum hist_type type, gimple *stmt, tree value)
124 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
125 hist->hvalue.value = value;
126 hist->hvalue.stmt = stmt;
127 hist->type = type;
128 return hist;
131 /* Hash value for histogram. */
133 static hashval_t
134 histogram_hash (const void *x)
136 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
139 /* Return nonzero if statement for histogram_value X is Y. */
141 static int
142 histogram_eq (const void *x, const void *y)
144 return ((const_histogram_value) x)->hvalue.stmt == (const gimple *) y;
147 /* Set histogram for STMT. */
149 static void
150 set_histogram_value (struct function *fun, gimple *stmt, histogram_value hist)
152 void **loc;
153 if (!hist && !VALUE_HISTOGRAMS (fun))
154 return;
155 if (!VALUE_HISTOGRAMS (fun))
156 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
157 histogram_eq, NULL);
158 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
159 htab_hash_pointer (stmt),
160 hist ? INSERT : NO_INSERT);
161 if (!hist)
163 if (loc)
164 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
165 return;
167 *loc = hist;
170 /* Get histogram list for STMT. */
172 histogram_value
173 gimple_histogram_value (struct function *fun, gimple *stmt)
175 if (!VALUE_HISTOGRAMS (fun))
176 return NULL;
177 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
178 htab_hash_pointer (stmt));
181 /* Add histogram for STMT. */
183 void
184 gimple_add_histogram_value (struct function *fun, gimple *stmt,
185 histogram_value hist)
187 hist->hvalue.next = gimple_histogram_value (fun, stmt);
188 set_histogram_value (fun, stmt, hist);
189 hist->fun = fun;
192 /* Remove histogram HIST from STMT's histogram list. */
194 void
195 gimple_remove_histogram_value (struct function *fun, gimple *stmt,
196 histogram_value hist)
198 histogram_value hist2 = gimple_histogram_value (fun, stmt);
199 if (hist == hist2)
201 set_histogram_value (fun, stmt, hist->hvalue.next);
203 else
205 while (hist2->hvalue.next != hist)
206 hist2 = hist2->hvalue.next;
207 hist2->hvalue.next = hist->hvalue.next;
209 free (hist->hvalue.counters);
210 if (flag_checking)
211 memset (hist, 0xab, sizeof (*hist));
212 free (hist);
215 /* Lookup histogram of type TYPE in the STMT. */
217 histogram_value
218 gimple_histogram_value_of_type (struct function *fun, gimple *stmt,
219 enum hist_type type)
221 histogram_value hist;
222 for (hist = gimple_histogram_value (fun, stmt); hist;
223 hist = hist->hvalue.next)
224 if (hist->type == type)
225 return hist;
226 return NULL;
229 /* Dump information about HIST to DUMP_FILE. */
231 static void
232 dump_histogram_value (FILE *dump_file, histogram_value hist)
234 switch (hist->type)
236 case HIST_TYPE_INTERVAL:
237 fprintf (dump_file, "Interval counter range %d -- %d",
238 hist->hdata.intvl.int_start,
239 (hist->hdata.intvl.int_start
240 + hist->hdata.intvl.steps - 1));
241 if (hist->hvalue.counters)
243 unsigned int i;
244 fprintf (dump_file, " [");
245 for (i = 0; i < hist->hdata.intvl.steps; i++)
246 fprintf (dump_file, " %d:%" PRId64,
247 hist->hdata.intvl.int_start + i,
248 (int64_t) hist->hvalue.counters[i]);
249 fprintf (dump_file, " ] outside range:%" PRId64,
250 (int64_t) hist->hvalue.counters[i]);
252 fprintf (dump_file, ".\n");
253 break;
255 case HIST_TYPE_POW2:
256 fprintf (dump_file, "Pow2 counter ");
257 if (hist->hvalue.counters)
259 fprintf (dump_file, "pow2:%" PRId64
260 " nonpow2:%" PRId64,
261 (int64_t) hist->hvalue.counters[1],
262 (int64_t) hist->hvalue.counters[0]);
264 fprintf (dump_file, ".\n");
265 break;
267 case HIST_TYPE_SINGLE_VALUE:
268 fprintf (dump_file, "Single value ");
269 if (hist->hvalue.counters)
271 fprintf (dump_file, "value:%" PRId64
272 " match:%" PRId64
273 " wrong:%" PRId64,
274 (int64_t) hist->hvalue.counters[0],
275 (int64_t) hist->hvalue.counters[1],
276 (int64_t) hist->hvalue.counters[2]);
278 fprintf (dump_file, ".\n");
279 break;
281 case HIST_TYPE_AVERAGE:
282 fprintf (dump_file, "Average value ");
283 if (hist->hvalue.counters)
285 fprintf (dump_file, "sum:%" PRId64
286 " times:%" PRId64,
287 (int64_t) hist->hvalue.counters[0],
288 (int64_t) hist->hvalue.counters[1]);
290 fprintf (dump_file, ".\n");
291 break;
293 case HIST_TYPE_IOR:
294 fprintf (dump_file, "IOR value ");
295 if (hist->hvalue.counters)
297 fprintf (dump_file, "ior:%" PRId64,
298 (int64_t) hist->hvalue.counters[0]);
300 fprintf (dump_file, ".\n");
301 break;
303 case HIST_TYPE_INDIR_CALL:
304 fprintf (dump_file, "Indirect call ");
305 if (hist->hvalue.counters)
307 fprintf (dump_file, "value:%" PRId64
308 " match:%" PRId64
309 " all:%" PRId64,
310 (int64_t) hist->hvalue.counters[0],
311 (int64_t) hist->hvalue.counters[1],
312 (int64_t) hist->hvalue.counters[2]);
314 fprintf (dump_file, ".\n");
315 break;
316 case HIST_TYPE_TIME_PROFILE:
317 fprintf (dump_file, "Time profile ");
318 if (hist->hvalue.counters)
320 fprintf (dump_file, "time:%" PRId64,
321 (int64_t) hist->hvalue.counters[0]);
323 fprintf (dump_file, ".\n");
324 break;
325 case HIST_TYPE_INDIR_CALL_TOPN:
326 fprintf (dump_file, "Indirect call topn ");
327 if (hist->hvalue.counters)
329 int i;
331 fprintf (dump_file, "accu:%" PRId64, hist->hvalue.counters[0]);
332 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
334 fprintf (dump_file, " target:%" PRId64 " value:%" PRId64,
335 (int64_t) hist->hvalue.counters[i],
336 (int64_t) hist->hvalue.counters[i+1]);
339 fprintf (dump_file, ".\n");
340 break;
341 case HIST_TYPE_MAX:
342 gcc_unreachable ();
346 /* Dump information about HIST to DUMP_FILE. */
348 void
349 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
351 struct bitpack_d bp;
352 unsigned int i;
354 bp = bitpack_create (ob->main_stream);
355 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
356 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
357 streamer_write_bitpack (&bp);
358 switch (hist->type)
360 case HIST_TYPE_INTERVAL:
361 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
362 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
363 break;
364 default:
365 break;
367 for (i = 0; i < hist->n_counters; i++)
368 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
369 if (hist->hvalue.next)
370 stream_out_histogram_value (ob, hist->hvalue.next);
373 /* Dump information about HIST to DUMP_FILE. */
375 void
376 stream_in_histogram_value (struct lto_input_block *ib, gimple *stmt)
378 enum hist_type type;
379 unsigned int ncounters = 0;
380 struct bitpack_d bp;
381 unsigned int i;
382 histogram_value new_val;
383 bool next;
384 histogram_value *next_p = NULL;
388 bp = streamer_read_bitpack (ib);
389 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
390 next = bp_unpack_value (&bp, 1);
391 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
392 switch (type)
394 case HIST_TYPE_INTERVAL:
395 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
396 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
397 ncounters = new_val->hdata.intvl.steps + 2;
398 break;
400 case HIST_TYPE_POW2:
401 case HIST_TYPE_AVERAGE:
402 ncounters = 2;
403 break;
405 case HIST_TYPE_SINGLE_VALUE:
406 case HIST_TYPE_INDIR_CALL:
407 ncounters = 3;
408 break;
410 case HIST_TYPE_IOR:
411 case HIST_TYPE_TIME_PROFILE:
412 ncounters = 1;
413 break;
415 case HIST_TYPE_INDIR_CALL_TOPN:
416 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
417 break;
419 case HIST_TYPE_MAX:
420 gcc_unreachable ();
422 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
423 new_val->n_counters = ncounters;
424 for (i = 0; i < ncounters; i++)
425 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
426 if (!next_p)
427 gimple_add_histogram_value (cfun, stmt, new_val);
428 else
429 *next_p = new_val;
430 next_p = &new_val->hvalue.next;
432 while (next);
435 /* Dump all histograms attached to STMT to DUMP_FILE. */
437 void
438 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple *stmt)
440 histogram_value hist;
441 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
442 dump_histogram_value (dump_file, hist);
445 /* Remove all histograms associated with STMT. */
447 void
448 gimple_remove_stmt_histograms (struct function *fun, gimple *stmt)
450 histogram_value val;
451 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
452 gimple_remove_histogram_value (fun, stmt, val);
455 /* Duplicate all histograms associates with OSTMT to STMT. */
457 void
458 gimple_duplicate_stmt_histograms (struct function *fun, gimple *stmt,
459 struct function *ofun, gimple *ostmt)
461 histogram_value val;
462 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
464 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
465 memcpy (new_val, val, sizeof (*val));
466 new_val->hvalue.stmt = stmt;
467 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
468 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
469 gimple_add_histogram_value (fun, stmt, new_val);
473 /* Move all histograms associated with OSTMT to STMT. */
475 void
476 gimple_move_stmt_histograms (struct function *fun, gimple *stmt, gimple *ostmt)
478 histogram_value val = gimple_histogram_value (fun, ostmt);
479 if (val)
481 /* The following three statements can't be reordered,
482 because histogram hashtab relies on stmt field value
483 for finding the exact slot. */
484 set_histogram_value (fun, ostmt, NULL);
485 for (; val != NULL; val = val->hvalue.next)
486 val->hvalue.stmt = stmt;
487 set_histogram_value (fun, stmt, val);
491 static bool error_found = false;
493 /* Helper function for verify_histograms. For each histogram reachable via htab
494 walk verify that it was reached via statement walk. */
496 static int
497 visit_hist (void **slot, void *data)
499 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
500 histogram_value hist = *(histogram_value *) slot;
502 if (!visited->contains (hist)
503 && hist->type != HIST_TYPE_TIME_PROFILE)
505 error ("dead histogram");
506 dump_histogram_value (stderr, hist);
507 debug_gimple_stmt (hist->hvalue.stmt);
508 error_found = true;
510 return 1;
513 /* Verify sanity of the histograms. */
515 DEBUG_FUNCTION void
516 verify_histograms (void)
518 basic_block bb;
519 gimple_stmt_iterator gsi;
520 histogram_value hist;
522 error_found = false;
523 hash_set<histogram_value> visited_hists;
524 FOR_EACH_BB_FN (bb, cfun)
525 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
527 gimple *stmt = gsi_stmt (gsi);
529 for (hist = gimple_histogram_value (cfun, stmt); hist;
530 hist = hist->hvalue.next)
532 if (hist->hvalue.stmt != stmt)
534 error ("Histogram value statement does not correspond to "
535 "the statement it is associated with");
536 debug_gimple_stmt (stmt);
537 dump_histogram_value (stderr, hist);
538 error_found = true;
540 visited_hists.add (hist);
543 if (VALUE_HISTOGRAMS (cfun))
544 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
545 if (error_found)
546 internal_error ("verify_histograms failed");
549 /* Helper function for verify_histograms. For each histogram reachable via htab
550 walk verify that it was reached via statement walk. */
552 static int
553 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
555 histogram_value hist = *(histogram_value *) slot;
556 free (hist->hvalue.counters);
557 if (flag_checking)
558 memset (hist, 0xab, sizeof (*hist));
559 free (hist);
560 return 1;
563 void
564 free_histograms (struct function *fn)
566 if (VALUE_HISTOGRAMS (fn))
568 htab_traverse (VALUE_HISTOGRAMS (fn), free_hist, NULL);
569 htab_delete (VALUE_HISTOGRAMS (fn));
570 VALUE_HISTOGRAMS (fn) = NULL;
574 /* The overall number of invocations of the counter should match
575 execution count of basic block. Report it as error rather than
576 internal error as it might mean that user has misused the profile
577 somehow. */
579 static bool
580 check_counter (gimple *stmt, const char * name,
581 gcov_type *count, gcov_type *all, gcov_type bb_count)
583 if (*all != bb_count || *count > *all)
585 location_t locus;
586 locus = (stmt != NULL)
587 ? gimple_location (stmt)
588 : DECL_SOURCE_LOCATION (current_function_decl);
589 if (flag_profile_correction)
591 if (dump_enabled_p ())
592 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
593 "correcting inconsistent value profile: %s "
594 "profiler overall count (%d) does not match BB "
595 "count (%d)\n", name, (int)*all, (int)bb_count);
596 *all = bb_count;
597 if (*count > *all)
598 *count = *all;
599 return false;
601 else
603 error_at (locus, "corrupted value profile: %s "
604 "profile counter (%d out of %d) inconsistent with "
605 "basic-block count (%d)",
606 name,
607 (int) *count,
608 (int) *all,
609 (int) bb_count);
610 return true;
614 return false;
617 /* GIMPLE based transformations. */
619 bool
620 gimple_value_profile_transformations (void)
622 basic_block bb;
623 gimple_stmt_iterator gsi;
624 bool changed = false;
626 /* Autofdo does its own transformations for indirect calls,
627 and otherwise does not support value profiling. */
628 if (flag_auto_profile)
629 return false;
631 FOR_EACH_BB_FN (bb, cfun)
633 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
635 gimple *stmt = gsi_stmt (gsi);
636 histogram_value th = gimple_histogram_value (cfun, stmt);
637 if (!th)
638 continue;
640 if (dump_file)
642 fprintf (dump_file, "Trying transformations on stmt ");
643 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
644 dump_histograms_for_stmt (cfun, dump_file, stmt);
647 /* Transformations: */
648 /* The order of things in this conditional controls which
649 transformation is used when more than one is applicable. */
650 /* It is expected that any code added by the transformations
651 will be added before the current statement, and that the
652 current statement remain valid (although possibly
653 modified) upon return. */
654 if (gimple_mod_subtract_transform (&gsi)
655 || gimple_divmod_fixed_value_transform (&gsi)
656 || gimple_mod_pow2_value_transform (&gsi)
657 || gimple_stringops_transform (&gsi)
658 || gimple_ic_transform (&gsi))
660 stmt = gsi_stmt (gsi);
661 changed = true;
662 /* Original statement may no longer be in the same block. */
663 if (bb != gimple_bb (stmt))
665 bb = gimple_bb (stmt);
666 gsi = gsi_for_stmt (stmt);
672 if (changed)
674 counts_to_freqs ();
677 return changed;
680 /* Generate code for transformation 1 (with parent gimple assignment
681 STMT and probability of taking the optimal path PROB, which is
682 equivalent to COUNT/ALL within roundoff error). This generates the
683 result into a temp and returns the temp; it does not replace or
684 alter the original STMT. */
686 static tree
687 gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
688 gcov_type count, gcov_type all)
690 gassign *stmt1, *stmt2;
691 gcond *stmt3;
692 tree tmp0, tmp1, tmp2;
693 gimple *bb1end, *bb2end, *bb3end;
694 basic_block bb, bb2, bb3, bb4;
695 tree optype, op1, op2;
696 edge e12, e13, e23, e24, e34;
697 gimple_stmt_iterator gsi;
699 gcc_assert (is_gimple_assign (stmt)
700 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
701 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
703 optype = TREE_TYPE (gimple_assign_lhs (stmt));
704 op1 = gimple_assign_rhs1 (stmt);
705 op2 = gimple_assign_rhs2 (stmt);
707 bb = gimple_bb (stmt);
708 gsi = gsi_for_stmt (stmt);
710 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
711 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
712 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
713 stmt2 = gimple_build_assign (tmp1, op2);
714 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
715 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
716 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
717 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
718 bb1end = stmt3;
720 tmp2 = create_tmp_reg (optype, "PROF");
721 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
722 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
723 bb2end = stmt1;
725 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
726 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
727 bb3end = stmt1;
729 /* Fix CFG. */
730 /* Edge e23 connects bb2 to bb3, etc. */
731 e12 = split_block (bb, bb1end);
732 bb2 = e12->dest;
733 bb2->count = count;
734 e23 = split_block (bb2, bb2end);
735 bb3 = e23->dest;
736 bb3->count = all - count;
737 e34 = split_block (bb3, bb3end);
738 bb4 = e34->dest;
739 bb4->count = all;
741 e12->flags &= ~EDGE_FALLTHRU;
742 e12->flags |= EDGE_FALSE_VALUE;
743 e12->probability = prob;
744 e12->count = count;
746 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
747 e13->probability = REG_BR_PROB_BASE - prob;
748 e13->count = all - count;
750 remove_edge (e23);
752 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
753 e24->probability = REG_BR_PROB_BASE;
754 e24->count = count;
756 e34->probability = REG_BR_PROB_BASE;
757 e34->count = all - count;
759 return tmp2;
762 /* Do transform 1) on INSN if applicable. */
764 static bool
765 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
767 histogram_value histogram;
768 enum tree_code code;
769 gcov_type val, count, all;
770 tree result, value, tree_val;
771 gcov_type prob;
772 gassign *stmt;
774 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
775 if (!stmt)
776 return false;
778 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
779 return false;
781 code = gimple_assign_rhs_code (stmt);
783 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
784 return false;
786 histogram = gimple_histogram_value_of_type (cfun, stmt,
787 HIST_TYPE_SINGLE_VALUE);
788 if (!histogram)
789 return false;
791 value = histogram->hvalue.value;
792 val = histogram->hvalue.counters[0];
793 count = histogram->hvalue.counters[1];
794 all = histogram->hvalue.counters[2];
795 gimple_remove_histogram_value (cfun, stmt, histogram);
797 /* We require that count is at least half of all; this means
798 that for the transformation to fire the value must be constant
799 at least 50% of time (and 75% gives the guarantee of usage). */
800 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
801 || 2 * count < all
802 || optimize_bb_for_size_p (gimple_bb (stmt)))
803 return false;
805 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
806 return false;
808 /* Compute probability of taking the optimal path. */
809 if (all > 0)
810 prob = GCOV_COMPUTE_SCALE (count, all);
811 else
812 prob = 0;
814 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
815 tree_val = build_int_cst (get_gcov_type (), val);
816 else
818 HOST_WIDE_INT a[2];
819 a[0] = (unsigned HOST_WIDE_INT) val;
820 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
822 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
823 TYPE_PRECISION (get_gcov_type ()), false));
825 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
827 if (dump_file)
829 fprintf (dump_file, "Div/mod by constant ");
830 print_generic_expr (dump_file, value, TDF_SLIM);
831 fprintf (dump_file, "=");
832 print_generic_expr (dump_file, tree_val, TDF_SLIM);
833 fprintf (dump_file, " transformation on insn ");
834 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
837 gimple_assign_set_rhs_from_tree (si, result);
838 update_stmt (gsi_stmt (*si));
840 return true;
843 /* Generate code for transformation 2 (with parent gimple assign STMT and
844 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
845 within roundoff error). This generates the result into a temp and returns
846 the temp; it does not replace or alter the original STMT. */
848 static tree
849 gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
851 gassign *stmt1, *stmt2, *stmt3;
852 gcond *stmt4;
853 tree tmp2, tmp3;
854 gimple *bb1end, *bb2end, *bb3end;
855 basic_block bb, bb2, bb3, bb4;
856 tree optype, op1, op2;
857 edge e12, e13, e23, e24, e34;
858 gimple_stmt_iterator gsi;
859 tree result;
861 gcc_assert (is_gimple_assign (stmt)
862 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
864 optype = TREE_TYPE (gimple_assign_lhs (stmt));
865 op1 = gimple_assign_rhs1 (stmt);
866 op2 = gimple_assign_rhs2 (stmt);
868 bb = gimple_bb (stmt);
869 gsi = gsi_for_stmt (stmt);
871 result = create_tmp_reg (optype, "PROF");
872 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
873 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
874 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
875 build_int_cst (optype, -1));
876 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
877 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
878 NULL_TREE, NULL_TREE);
879 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
880 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
881 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
882 bb1end = stmt4;
884 /* tmp2 == op2-1 inherited from previous block. */
885 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
886 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
887 bb2end = stmt1;
889 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
890 op1, op2);
891 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
892 bb3end = stmt1;
894 /* Fix CFG. */
895 /* Edge e23 connects bb2 to bb3, etc. */
896 e12 = split_block (bb, bb1end);
897 bb2 = e12->dest;
898 bb2->count = count;
899 e23 = split_block (bb2, bb2end);
900 bb3 = e23->dest;
901 bb3->count = all - count;
902 e34 = split_block (bb3, bb3end);
903 bb4 = e34->dest;
904 bb4->count = all;
906 e12->flags &= ~EDGE_FALLTHRU;
907 e12->flags |= EDGE_FALSE_VALUE;
908 e12->probability = prob;
909 e12->count = count;
911 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
912 e13->probability = REG_BR_PROB_BASE - prob;
913 e13->count = all - count;
915 remove_edge (e23);
917 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
918 e24->probability = REG_BR_PROB_BASE;
919 e24->count = count;
921 e34->probability = REG_BR_PROB_BASE;
922 e34->count = all - count;
924 return result;
927 /* Do transform 2) on INSN if applicable. */
929 static bool
930 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
932 histogram_value histogram;
933 enum tree_code code;
934 gcov_type count, wrong_values, all;
935 tree lhs_type, result, value;
936 gcov_type prob;
937 gassign *stmt;
939 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
940 if (!stmt)
941 return false;
943 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
944 if (!INTEGRAL_TYPE_P (lhs_type))
945 return false;
947 code = gimple_assign_rhs_code (stmt);
949 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
950 return false;
952 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
953 if (!histogram)
954 return false;
956 value = histogram->hvalue.value;
957 wrong_values = histogram->hvalue.counters[0];
958 count = histogram->hvalue.counters[1];
960 gimple_remove_histogram_value (cfun, stmt, histogram);
962 /* We require that we hit a power of 2 at least half of all evaluations. */
963 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
964 || count < wrong_values
965 || optimize_bb_for_size_p (gimple_bb (stmt)))
966 return false;
968 if (dump_file)
970 fprintf (dump_file, "Mod power of 2 transformation on insn ");
971 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
974 /* Compute probability of taking the optimal path. */
975 all = count + wrong_values;
977 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
978 return false;
980 if (all > 0)
981 prob = GCOV_COMPUTE_SCALE (count, all);
982 else
983 prob = 0;
985 result = gimple_mod_pow2 (stmt, prob, count, all);
987 gimple_assign_set_rhs_from_tree (si, result);
988 update_stmt (gsi_stmt (*si));
990 return true;
993 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
994 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
995 supported and this is built into this interface. The probabilities of taking
996 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
997 COUNT2/ALL respectively within roundoff error). This generates the
998 result into a temp and returns the temp; it does not replace or alter
999 the original STMT. */
1000 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1002 static tree
1003 gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
1004 gcov_type count1, gcov_type count2, gcov_type all)
1006 gassign *stmt1;
1007 gimple *stmt2;
1008 gcond *stmt3;
1009 tree tmp1;
1010 gimple *bb1end, *bb2end = NULL, *bb3end;
1011 basic_block bb, bb2, bb3, bb4;
1012 tree optype, op1, op2;
1013 edge e12, e23 = 0, e24, e34, e14;
1014 gimple_stmt_iterator gsi;
1015 tree result;
1017 gcc_assert (is_gimple_assign (stmt)
1018 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1020 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1021 op1 = gimple_assign_rhs1 (stmt);
1022 op2 = gimple_assign_rhs2 (stmt);
1024 bb = gimple_bb (stmt);
1025 gsi = gsi_for_stmt (stmt);
1027 result = create_tmp_reg (optype, "PROF");
1028 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1029 stmt1 = gimple_build_assign (result, op1);
1030 stmt2 = gimple_build_assign (tmp1, op2);
1031 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1032 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1033 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1034 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1035 bb1end = stmt3;
1037 if (ncounts) /* Assumed to be 0 or 1 */
1039 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1040 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1041 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1042 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1043 bb2end = stmt2;
1046 /* Fallback case. */
1047 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1048 result, tmp1);
1049 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1050 bb3end = stmt1;
1052 /* Fix CFG. */
1053 /* Edge e23 connects bb2 to bb3, etc. */
1054 /* However block 3 is optional; if it is not there, references
1055 to 3 really refer to block 2. */
1056 e12 = split_block (bb, bb1end);
1057 bb2 = e12->dest;
1058 bb2->count = all - count1;
1060 if (ncounts) /* Assumed to be 0 or 1. */
1062 e23 = split_block (bb2, bb2end);
1063 bb3 = e23->dest;
1064 bb3->count = all - count1 - count2;
1067 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1068 bb4 = e34->dest;
1069 bb4->count = all;
1071 e12->flags &= ~EDGE_FALLTHRU;
1072 e12->flags |= EDGE_FALSE_VALUE;
1073 e12->probability = REG_BR_PROB_BASE - prob1;
1074 e12->count = all - count1;
1076 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1077 e14->probability = prob1;
1078 e14->count = count1;
1080 if (ncounts) /* Assumed to be 0 or 1. */
1082 e23->flags &= ~EDGE_FALLTHRU;
1083 e23->flags |= EDGE_FALSE_VALUE;
1084 e23->count = all - count1 - count2;
1085 e23->probability = REG_BR_PROB_BASE - prob2;
1087 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1088 e24->probability = prob2;
1089 e24->count = count2;
1092 e34->probability = REG_BR_PROB_BASE;
1093 e34->count = all - count1 - count2;
1095 return result;
1098 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1100 static bool
1101 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1103 histogram_value histogram;
1104 enum tree_code code;
1105 gcov_type count, wrong_values, all;
1106 tree lhs_type, result;
1107 gcov_type prob1, prob2;
1108 unsigned int i, steps;
1109 gcov_type count1, count2;
1110 gassign *stmt;
1111 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1112 if (!stmt)
1113 return false;
1115 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1116 if (!INTEGRAL_TYPE_P (lhs_type))
1117 return false;
1119 code = gimple_assign_rhs_code (stmt);
1121 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1122 return false;
1124 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1125 if (!histogram)
1126 return false;
1128 all = 0;
1129 wrong_values = 0;
1130 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1131 all += histogram->hvalue.counters[i];
1133 wrong_values += histogram->hvalue.counters[i];
1134 wrong_values += histogram->hvalue.counters[i+1];
1135 steps = histogram->hdata.intvl.steps;
1136 all += wrong_values;
1137 count1 = histogram->hvalue.counters[0];
1138 count2 = histogram->hvalue.counters[1];
1140 /* Compute probability of taking the optimal path. */
1141 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1143 gimple_remove_histogram_value (cfun, stmt, histogram);
1144 return false;
1147 if (flag_profile_correction && count1 + count2 > all)
1148 all = count1 + count2;
1150 gcc_assert (count1 + count2 <= all);
1152 /* We require that we use just subtractions in at least 50% of all
1153 evaluations. */
1154 count = 0;
1155 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1157 count += histogram->hvalue.counters[i];
1158 if (count * 2 >= all)
1159 break;
1161 if (i == steps
1162 || optimize_bb_for_size_p (gimple_bb (stmt)))
1163 return false;
1165 gimple_remove_histogram_value (cfun, stmt, histogram);
1166 if (dump_file)
1168 fprintf (dump_file, "Mod subtract transformation on insn ");
1169 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1172 /* Compute probability of taking the optimal path(s). */
1173 if (all > 0)
1175 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1176 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1178 else
1180 prob1 = prob2 = 0;
1183 /* In practice, "steps" is always 2. This interface reflects this,
1184 and will need to be changed if "steps" can change. */
1185 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1187 gimple_assign_set_rhs_from_tree (si, result);
1188 update_stmt (gsi_stmt (*si));
1190 return true;
1193 typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
1195 static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
1197 /* Returns true if node graph is initialized. This
1198 is used to test if profile_id has been created
1199 for cgraph_nodes. */
1201 bool
1202 coverage_node_map_initialized_p (void)
1204 return cgraph_node_map != 0;
1207 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1208 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1209 that the PROFILE_IDs was already assigned. */
1211 void
1212 init_node_map (bool local)
1214 struct cgraph_node *n;
1215 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
1217 FOR_EACH_DEFINED_FUNCTION (n)
1218 if (n->has_gimple_body_p ())
1220 cgraph_node **val;
1221 if (local)
1223 n->profile_id = coverage_compute_profile_id (n);
1224 while ((val = cgraph_node_map->get (n->profile_id))
1225 || !n->profile_id)
1227 if (dump_file)
1228 fprintf (dump_file, "Local profile-id %i conflict"
1229 " with nodes %s/%i %s/%i\n",
1230 n->profile_id,
1231 n->name (),
1232 n->order,
1233 (*val)->name (),
1234 (*val)->order);
1235 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1238 else if (!n->profile_id)
1240 if (dump_file)
1241 fprintf (dump_file,
1242 "Node %s/%i has no profile-id"
1243 " (profile feedback missing?)\n",
1244 n->name (),
1245 n->order);
1246 continue;
1248 else if ((val = cgraph_node_map->get (n->profile_id)))
1250 if (dump_file)
1251 fprintf (dump_file,
1252 "Node %s/%i has IP profile-id %i conflict. "
1253 "Giving up.\n",
1254 n->name (),
1255 n->order,
1256 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 int prob, gcov_type count, gcov_type 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 = REG_BR_PROB_BASE;
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 = REG_BR_PROB_BASE - prob;
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 = REG_BR_PROB_BASE;
1414 e_dj->count = count;
1416 e_ij->count = all - count;
1418 e_ij->probability = REG_BR_PROB_BASE;
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 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1497 !gsi_end_p (psi); gsi_next (&psi))
1499 gphi *phi = psi.phi ();
1500 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1501 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1504 if (!stmt_could_throw_p (dcall_stmt))
1505 gimple_purge_dead_eh_edges (dcall_bb);
1506 return dcall_stmt;
1510 For every checked indirect/virtual call determine if most common pid of
1511 function/class method has probability more than 50%. If yes modify code of
1512 this call to:
1515 static bool
1516 gimple_ic_transform (gimple_stmt_iterator *gsi)
1518 gcall *stmt;
1519 histogram_value histogram;
1520 gcov_type val, count, all, bb_all;
1521 struct cgraph_node *direct_call;
1523 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1524 if (!stmt)
1525 return false;
1527 if (gimple_call_fndecl (stmt) != NULL_TREE)
1528 return false;
1530 if (gimple_call_internal_p (stmt))
1531 return false;
1533 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1534 if (!histogram)
1535 return false;
1537 val = histogram->hvalue.counters [0];
1538 count = histogram->hvalue.counters [1];
1539 all = histogram->hvalue.counters [2];
1541 bb_all = gimple_bb (stmt)->count;
1542 /* The order of CHECK_COUNTER calls is important -
1543 since check_counter can correct the third parameter
1544 and we want to make count <= all <= bb_all. */
1545 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1546 || check_counter (stmt, "ic", &count, &all, all))
1548 gimple_remove_histogram_value (cfun, stmt, histogram);
1549 return false;
1552 if (4 * count <= 3 * all)
1553 return false;
1555 direct_call = find_func_by_profile_id ((int)val);
1557 if (direct_call == NULL)
1559 if (val)
1561 if (dump_file)
1563 fprintf (dump_file, "Indirect call -> direct call from other module");
1564 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1565 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1568 return false;
1571 if (!check_ic_target (stmt, direct_call))
1573 if (dump_file)
1575 fprintf (dump_file, "Indirect call -> direct call ");
1576 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1577 fprintf (dump_file, "=> ");
1578 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1579 fprintf (dump_file, " transformation skipped because of type mismatch");
1580 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1582 gimple_remove_histogram_value (cfun, stmt, histogram);
1583 return false;
1586 if (dump_file)
1588 fprintf (dump_file, "Indirect call -> direct call ");
1589 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1590 fprintf (dump_file, "=> ");
1591 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1592 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1593 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1594 fprintf (dump_file, "hist->count %" PRId64
1595 " hist->all %" PRId64"\n", count, all);
1598 return true;
1601 /* Return true if the stringop CALL shall be profiled. SIZE_ARG be
1602 set to the argument index for the size of the string operation. */
1604 static bool
1605 interesting_stringop_to_profile_p (gcall *call, int *size_arg)
1607 enum built_in_function fcode;
1609 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (call));
1610 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1611 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1612 return false;
1614 switch (fcode)
1616 case BUILT_IN_MEMCPY:
1617 case BUILT_IN_MEMPCPY:
1618 *size_arg = 2;
1619 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1620 INTEGER_TYPE, VOID_TYPE);
1621 case BUILT_IN_MEMSET:
1622 *size_arg = 2;
1623 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1624 INTEGER_TYPE, VOID_TYPE);
1625 case BUILT_IN_BZERO:
1626 *size_arg = 1;
1627 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1628 VOID_TYPE);
1629 default:
1630 gcc_unreachable ();
1634 /* Convert stringop (..., vcall_size)
1635 into
1636 if (vcall_size == icall_size)
1637 stringop (..., icall_size);
1638 else
1639 stringop (..., vcall_size);
1640 assuming we'll propagate a true constant into ICALL_SIZE later. */
1642 static void
1643 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
1644 gcov_type count, gcov_type all)
1646 gassign *tmp_stmt;
1647 gcond *cond_stmt;
1648 gcall *icall_stmt;
1649 tree tmp0, tmp1, vcall_size, optype;
1650 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1651 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1652 gimple_stmt_iterator gsi;
1653 int size_arg;
1655 if (!interesting_stringop_to_profile_p (vcall_stmt, &size_arg))
1656 gcc_unreachable ();
1658 cond_bb = gimple_bb (vcall_stmt);
1659 gsi = gsi_for_stmt (vcall_stmt);
1661 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1662 optype = TREE_TYPE (vcall_size);
1664 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1665 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1666 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1667 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1669 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1670 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1672 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1673 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1675 if (TREE_CODE (gimple_vdef (vcall_stmt)) == SSA_NAME)
1677 unlink_stmt_vdef (vcall_stmt);
1678 release_ssa_name (gimple_vdef (vcall_stmt));
1680 gimple_set_vdef (vcall_stmt, NULL);
1681 gimple_set_vuse (vcall_stmt, NULL);
1682 update_stmt (vcall_stmt);
1683 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1684 gimple_call_set_arg (icall_stmt, size_arg,
1685 fold_convert (optype, icall_size));
1686 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1688 /* Fix CFG. */
1689 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1690 e_ci = split_block (cond_bb, cond_stmt);
1691 icall_bb = e_ci->dest;
1692 icall_bb->count = count;
1694 e_iv = split_block (icall_bb, icall_stmt);
1695 vcall_bb = e_iv->dest;
1696 vcall_bb->count = all - count;
1698 e_vj = split_block (vcall_bb, vcall_stmt);
1699 join_bb = e_vj->dest;
1700 join_bb->count = all;
1702 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1703 e_ci->probability = prob;
1704 e_ci->count = count;
1706 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1707 e_cv->probability = REG_BR_PROB_BASE - prob;
1708 e_cv->count = all - count;
1710 remove_edge (e_iv);
1712 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1713 e_ij->probability = REG_BR_PROB_BASE;
1714 e_ij->count = count;
1716 e_vj->probability = REG_BR_PROB_BASE;
1717 e_vj->count = all - count;
1719 /* Insert PHI node for the call result if necessary. */
1720 if (gimple_call_lhs (vcall_stmt)
1721 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1723 tree result = gimple_call_lhs (vcall_stmt);
1724 gphi *phi = create_phi_node (result, join_bb);
1725 gimple_call_set_lhs (vcall_stmt,
1726 duplicate_ssa_name (result, vcall_stmt));
1727 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1728 gimple_call_set_lhs (icall_stmt,
1729 duplicate_ssa_name (result, icall_stmt));
1730 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1733 /* Because these are all string op builtins, they're all nothrow. */
1734 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1735 gcc_assert (!stmt_could_throw_p (icall_stmt));
1738 /* Find values inside STMT for that we want to measure histograms for
1739 division/modulo optimization. */
1741 static bool
1742 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1744 gcall *stmt;
1745 tree blck_size;
1746 enum built_in_function fcode;
1747 histogram_value histogram;
1748 gcov_type count, all, val;
1749 tree dest, src;
1750 unsigned int dest_align, src_align;
1751 gcov_type prob;
1752 tree tree_val;
1753 int size_arg;
1755 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1756 if (!stmt)
1757 return false;
1759 if (!gimple_call_builtin_p (gsi_stmt (*gsi), BUILT_IN_NORMAL))
1760 return false;
1762 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
1763 return false;
1765 blck_size = gimple_call_arg (stmt, size_arg);
1766 if (TREE_CODE (blck_size) == INTEGER_CST)
1767 return false;
1769 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1770 if (!histogram)
1771 return false;
1773 val = histogram->hvalue.counters[0];
1774 count = histogram->hvalue.counters[1];
1775 all = histogram->hvalue.counters[2];
1776 gimple_remove_histogram_value (cfun, stmt, histogram);
1778 /* We require that count is at least half of all; this means
1779 that for the transformation to fire the value must be constant
1780 at least 80% of time. */
1781 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1782 return false;
1783 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1784 return false;
1785 if (all > 0)
1786 prob = GCOV_COMPUTE_SCALE (count, all);
1787 else
1788 prob = 0;
1790 dest = gimple_call_arg (stmt, 0);
1791 dest_align = get_pointer_alignment (dest);
1792 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (stmt));
1793 switch (fcode)
1795 case BUILT_IN_MEMCPY:
1796 case BUILT_IN_MEMPCPY:
1797 src = gimple_call_arg (stmt, 1);
1798 src_align = get_pointer_alignment (src);
1799 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1800 return false;
1801 break;
1802 case BUILT_IN_MEMSET:
1803 if (!can_store_by_pieces (val, builtin_memset_read_str,
1804 gimple_call_arg (stmt, 1),
1805 dest_align, true))
1806 return false;
1807 break;
1808 case BUILT_IN_BZERO:
1809 if (!can_store_by_pieces (val, builtin_memset_read_str,
1810 integer_zero_node,
1811 dest_align, true))
1812 return false;
1813 break;
1814 default:
1815 gcc_unreachable ();
1818 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1819 tree_val = build_int_cst (get_gcov_type (), val);
1820 else
1822 HOST_WIDE_INT a[2];
1823 a[0] = (unsigned HOST_WIDE_INT) val;
1824 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1826 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1827 TYPE_PRECISION (get_gcov_type ()), false));
1830 if (dump_file)
1832 fprintf (dump_file, "Single value %i stringop transformation on ",
1833 (int)val);
1834 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1837 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1839 return true;
1842 void
1843 stringop_block_profile (gimple *stmt, unsigned int *expected_align,
1844 HOST_WIDE_INT *expected_size)
1846 histogram_value histogram;
1847 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1849 if (!histogram)
1850 *expected_size = -1;
1851 else if (!histogram->hvalue.counters[1])
1853 *expected_size = -1;
1854 gimple_remove_histogram_value (cfun, stmt, histogram);
1856 else
1858 gcov_type size;
1859 size = ((histogram->hvalue.counters[0]
1860 + histogram->hvalue.counters[1] / 2)
1861 / histogram->hvalue.counters[1]);
1862 /* Even if we can hold bigger value in SIZE, INT_MAX
1863 is safe "infinity" for code generation strategies. */
1864 if (size > INT_MAX)
1865 size = INT_MAX;
1866 *expected_size = size;
1867 gimple_remove_histogram_value (cfun, stmt, histogram);
1870 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1872 if (!histogram)
1873 *expected_align = 0;
1874 else if (!histogram->hvalue.counters[0])
1876 gimple_remove_histogram_value (cfun, stmt, histogram);
1877 *expected_align = 0;
1879 else
1881 gcov_type count;
1882 unsigned int alignment;
1884 count = histogram->hvalue.counters[0];
1885 alignment = 1;
1886 while (!(count & alignment)
1887 && (alignment <= UINT_MAX / 2 / BITS_PER_UNIT))
1888 alignment <<= 1;
1889 *expected_align = alignment * BITS_PER_UNIT;
1890 gimple_remove_histogram_value (cfun, stmt, histogram);
1895 /* Find values inside STMT for that we want to measure histograms for
1896 division/modulo optimization. */
1898 static void
1899 gimple_divmod_values_to_profile (gimple *stmt, histogram_values *values)
1901 tree lhs, divisor, op0, type;
1902 histogram_value hist;
1904 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1905 return;
1907 lhs = gimple_assign_lhs (stmt);
1908 type = TREE_TYPE (lhs);
1909 if (!INTEGRAL_TYPE_P (type))
1910 return;
1912 switch (gimple_assign_rhs_code (stmt))
1914 case TRUNC_DIV_EXPR:
1915 case TRUNC_MOD_EXPR:
1916 divisor = gimple_assign_rhs2 (stmt);
1917 op0 = gimple_assign_rhs1 (stmt);
1919 values->reserve (3);
1921 if (TREE_CODE (divisor) == SSA_NAME)
1922 /* Check for the case where the divisor is the same value most
1923 of the time. */
1924 values->quick_push (gimple_alloc_histogram_value (cfun,
1925 HIST_TYPE_SINGLE_VALUE,
1926 stmt, divisor));
1928 /* For mod, check whether it is not often a noop (or replaceable by
1929 a few subtractions). */
1930 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1931 && TYPE_UNSIGNED (type)
1932 && TREE_CODE (divisor) == SSA_NAME)
1934 tree val;
1935 /* Check for a special case where the divisor is power of 2. */
1936 values->quick_push (gimple_alloc_histogram_value (cfun,
1937 HIST_TYPE_POW2,
1938 stmt, divisor));
1940 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1941 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1942 stmt, val);
1943 hist->hdata.intvl.int_start = 0;
1944 hist->hdata.intvl.steps = 2;
1945 values->quick_push (hist);
1947 return;
1949 default:
1950 return;
1954 /* Find calls inside STMT for that we want to measure histograms for
1955 indirect/virtual call optimization. */
1957 static void
1958 gimple_indirect_call_to_profile (gimple *stmt, histogram_values *values)
1960 tree callee;
1962 if (gimple_code (stmt) != GIMPLE_CALL
1963 || gimple_call_internal_p (stmt)
1964 || gimple_call_fndecl (stmt) != NULL_TREE)
1965 return;
1967 callee = gimple_call_fn (stmt);
1969 values->reserve (3);
1971 values->quick_push (gimple_alloc_histogram_value (
1972 cfun,
1973 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
1974 HIST_TYPE_INDIR_CALL_TOPN :
1975 HIST_TYPE_INDIR_CALL,
1976 stmt, callee));
1978 return;
1981 /* Find values inside STMT for that we want to measure histograms for
1982 string operations. */
1984 static void
1985 gimple_stringops_values_to_profile (gimple *gs, histogram_values *values)
1987 gcall *stmt;
1988 tree blck_size;
1989 tree dest;
1990 int size_arg;
1992 stmt = dyn_cast <gcall *> (gs);
1993 if (!stmt)
1994 return;
1996 if (!gimple_call_builtin_p (gs, BUILT_IN_NORMAL))
1997 return;
1999 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
2000 return;
2002 dest = gimple_call_arg (stmt, 0);
2003 blck_size = gimple_call_arg (stmt, size_arg);
2005 if (TREE_CODE (blck_size) != INTEGER_CST)
2007 values->safe_push (gimple_alloc_histogram_value (cfun,
2008 HIST_TYPE_SINGLE_VALUE,
2009 stmt, blck_size));
2010 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2011 stmt, blck_size));
2014 if (TREE_CODE (blck_size) != INTEGER_CST)
2015 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2016 stmt, dest));
2019 /* Find values inside STMT for that we want to measure histograms and adds
2020 them to list VALUES. */
2022 static void
2023 gimple_values_to_profile (gimple *stmt, histogram_values *values)
2025 gimple_divmod_values_to_profile (stmt, values);
2026 gimple_stringops_values_to_profile (stmt, values);
2027 gimple_indirect_call_to_profile (stmt, values);
2030 void
2031 gimple_find_values_to_profile (histogram_values *values)
2033 basic_block bb;
2034 gimple_stmt_iterator gsi;
2035 unsigned i;
2036 histogram_value hist = NULL;
2037 values->create (0);
2039 FOR_EACH_BB_FN (bb, cfun)
2040 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2041 gimple_values_to_profile (gsi_stmt (gsi), values);
2043 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2045 FOR_EACH_VEC_ELT (*values, i, hist)
2047 switch (hist->type)
2049 case HIST_TYPE_INTERVAL:
2050 hist->n_counters = hist->hdata.intvl.steps + 2;
2051 break;
2053 case HIST_TYPE_POW2:
2054 hist->n_counters = 2;
2055 break;
2057 case HIST_TYPE_SINGLE_VALUE:
2058 hist->n_counters = 3;
2059 break;
2061 case HIST_TYPE_INDIR_CALL:
2062 hist->n_counters = 3;
2063 break;
2065 case HIST_TYPE_TIME_PROFILE:
2066 hist->n_counters = 1;
2067 break;
2069 case HIST_TYPE_AVERAGE:
2070 hist->n_counters = 2;
2071 break;
2073 case HIST_TYPE_IOR:
2074 hist->n_counters = 1;
2075 break;
2077 case HIST_TYPE_INDIR_CALL_TOPN:
2078 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2079 break;
2081 default:
2082 gcc_unreachable ();
2084 if (dump_file)
2086 fprintf (dump_file, "Stmt ");
2087 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2088 dump_histogram_value (dump_file, hist);