[AArch64] PR target/79913: VEC_SELECT bugs in aarch64 patterns
[official-gcc.git] / gcc / value-prof.c
blob22dc2c9e257019d0d8b7dd0dc8b6d7382585f3c3
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++)
369 /* When user uses an unsigned type with a big value, constant converted
370 to gcov_type (a signed type) can be negative. */
371 gcov_type value = hist->hvalue.counters[i];
372 if (hist->type == HIST_TYPE_SINGLE_VALUE && i == 0)
374 else
375 gcc_assert (value >= 0);
377 streamer_write_gcov_count (ob, value);
379 if (hist->hvalue.next)
380 stream_out_histogram_value (ob, hist->hvalue.next);
383 /* Dump information about HIST to DUMP_FILE. */
385 void
386 stream_in_histogram_value (struct lto_input_block *ib, gimple *stmt)
388 enum hist_type type;
389 unsigned int ncounters = 0;
390 struct bitpack_d bp;
391 unsigned int i;
392 histogram_value new_val;
393 bool next;
394 histogram_value *next_p = NULL;
398 bp = streamer_read_bitpack (ib);
399 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
400 next = bp_unpack_value (&bp, 1);
401 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
402 switch (type)
404 case HIST_TYPE_INTERVAL:
405 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
406 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
407 ncounters = new_val->hdata.intvl.steps + 2;
408 break;
410 case HIST_TYPE_POW2:
411 case HIST_TYPE_AVERAGE:
412 ncounters = 2;
413 break;
415 case HIST_TYPE_SINGLE_VALUE:
416 case HIST_TYPE_INDIR_CALL:
417 ncounters = 3;
418 break;
420 case HIST_TYPE_IOR:
421 case HIST_TYPE_TIME_PROFILE:
422 ncounters = 1;
423 break;
425 case HIST_TYPE_INDIR_CALL_TOPN:
426 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
427 break;
429 case HIST_TYPE_MAX:
430 gcc_unreachable ();
432 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
433 new_val->n_counters = ncounters;
434 for (i = 0; i < ncounters; i++)
435 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
436 if (!next_p)
437 gimple_add_histogram_value (cfun, stmt, new_val);
438 else
439 *next_p = new_val;
440 next_p = &new_val->hvalue.next;
442 while (next);
445 /* Dump all histograms attached to STMT to DUMP_FILE. */
447 void
448 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple *stmt)
450 histogram_value hist;
451 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
452 dump_histogram_value (dump_file, hist);
455 /* Remove all histograms associated with STMT. */
457 void
458 gimple_remove_stmt_histograms (struct function *fun, gimple *stmt)
460 histogram_value val;
461 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
462 gimple_remove_histogram_value (fun, stmt, val);
465 /* Duplicate all histograms associates with OSTMT to STMT. */
467 void
468 gimple_duplicate_stmt_histograms (struct function *fun, gimple *stmt,
469 struct function *ofun, gimple *ostmt)
471 histogram_value val;
472 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
474 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
475 memcpy (new_val, val, sizeof (*val));
476 new_val->hvalue.stmt = stmt;
477 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
478 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
479 gimple_add_histogram_value (fun, stmt, new_val);
483 /* Move all histograms associated with OSTMT to STMT. */
485 void
486 gimple_move_stmt_histograms (struct function *fun, gimple *stmt, gimple *ostmt)
488 histogram_value val = gimple_histogram_value (fun, ostmt);
489 if (val)
491 /* The following three statements can't be reordered,
492 because histogram hashtab relies on stmt field value
493 for finding the exact slot. */
494 set_histogram_value (fun, ostmt, NULL);
495 for (; val != NULL; val = val->hvalue.next)
496 val->hvalue.stmt = stmt;
497 set_histogram_value (fun, stmt, val);
501 static bool error_found = false;
503 /* Helper function for verify_histograms. For each histogram reachable via htab
504 walk verify that it was reached via statement walk. */
506 static int
507 visit_hist (void **slot, void *data)
509 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
510 histogram_value hist = *(histogram_value *) slot;
512 if (!visited->contains (hist)
513 && hist->type != HIST_TYPE_TIME_PROFILE)
515 error ("dead histogram");
516 dump_histogram_value (stderr, hist);
517 debug_gimple_stmt (hist->hvalue.stmt);
518 error_found = true;
520 return 1;
523 /* Verify sanity of the histograms. */
525 DEBUG_FUNCTION void
526 verify_histograms (void)
528 basic_block bb;
529 gimple_stmt_iterator gsi;
530 histogram_value hist;
532 error_found = false;
533 hash_set<histogram_value> visited_hists;
534 FOR_EACH_BB_FN (bb, cfun)
535 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
537 gimple *stmt = gsi_stmt (gsi);
539 for (hist = gimple_histogram_value (cfun, stmt); hist;
540 hist = hist->hvalue.next)
542 if (hist->hvalue.stmt != stmt)
544 error ("Histogram value statement does not correspond to "
545 "the statement it is associated with");
546 debug_gimple_stmt (stmt);
547 dump_histogram_value (stderr, hist);
548 error_found = true;
550 visited_hists.add (hist);
553 if (VALUE_HISTOGRAMS (cfun))
554 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
555 if (error_found)
556 internal_error ("verify_histograms failed");
559 /* Helper function for verify_histograms. For each histogram reachable via htab
560 walk verify that it was reached via statement walk. */
562 static int
563 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
565 histogram_value hist = *(histogram_value *) slot;
566 free (hist->hvalue.counters);
567 if (flag_checking)
568 memset (hist, 0xab, sizeof (*hist));
569 free (hist);
570 return 1;
573 void
574 free_histograms (struct function *fn)
576 if (VALUE_HISTOGRAMS (fn))
578 htab_traverse (VALUE_HISTOGRAMS (fn), free_hist, NULL);
579 htab_delete (VALUE_HISTOGRAMS (fn));
580 VALUE_HISTOGRAMS (fn) = NULL;
584 /* The overall number of invocations of the counter should match
585 execution count of basic block. Report it as error rather than
586 internal error as it might mean that user has misused the profile
587 somehow. */
589 static bool
590 check_counter (gimple *stmt, const char * name,
591 gcov_type *count, gcov_type *all, gcov_type bb_count)
593 if (*all != bb_count || *count > *all)
595 location_t locus;
596 locus = (stmt != NULL)
597 ? gimple_location (stmt)
598 : DECL_SOURCE_LOCATION (current_function_decl);
599 if (flag_profile_correction)
601 if (dump_enabled_p ())
602 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
603 "correcting inconsistent value profile: %s "
604 "profiler overall count (%d) does not match BB "
605 "count (%d)\n", name, (int)*all, (int)bb_count);
606 *all = bb_count;
607 if (*count > *all)
608 *count = *all;
609 return false;
611 else
613 error_at (locus, "corrupted value profile: %s "
614 "profile counter (%d out of %d) inconsistent with "
615 "basic-block count (%d)",
616 name,
617 (int) *count,
618 (int) *all,
619 (int) bb_count);
620 return true;
624 return false;
627 /* GIMPLE based transformations. */
629 bool
630 gimple_value_profile_transformations (void)
632 basic_block bb;
633 gimple_stmt_iterator gsi;
634 bool changed = false;
636 /* Autofdo does its own transformations for indirect calls,
637 and otherwise does not support value profiling. */
638 if (flag_auto_profile)
639 return false;
641 FOR_EACH_BB_FN (bb, cfun)
643 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
645 gimple *stmt = gsi_stmt (gsi);
646 histogram_value th = gimple_histogram_value (cfun, stmt);
647 if (!th)
648 continue;
650 if (dump_file)
652 fprintf (dump_file, "Trying transformations on stmt ");
653 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
654 dump_histograms_for_stmt (cfun, dump_file, stmt);
657 /* Transformations: */
658 /* The order of things in this conditional controls which
659 transformation is used when more than one is applicable. */
660 /* It is expected that any code added by the transformations
661 will be added before the current statement, and that the
662 current statement remain valid (although possibly
663 modified) upon return. */
664 if (gimple_mod_subtract_transform (&gsi)
665 || gimple_divmod_fixed_value_transform (&gsi)
666 || gimple_mod_pow2_value_transform (&gsi)
667 || gimple_stringops_transform (&gsi)
668 || gimple_ic_transform (&gsi))
670 stmt = gsi_stmt (gsi);
671 changed = true;
672 /* Original statement may no longer be in the same block. */
673 if (bb != gimple_bb (stmt))
675 bb = gimple_bb (stmt);
676 gsi = gsi_for_stmt (stmt);
682 if (changed)
684 counts_to_freqs ();
687 return changed;
690 /* Generate code for transformation 1 (with parent gimple assignment
691 STMT and probability of taking the optimal path PROB, which is
692 equivalent to COUNT/ALL within roundoff error). This generates the
693 result into a temp and returns the temp; it does not replace or
694 alter the original STMT. */
696 static tree
697 gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
698 gcov_type count, gcov_type all)
700 gassign *stmt1, *stmt2;
701 gcond *stmt3;
702 tree tmp0, tmp1, tmp2;
703 gimple *bb1end, *bb2end, *bb3end;
704 basic_block bb, bb2, bb3, bb4;
705 tree optype, op1, op2;
706 edge e12, e13, e23, e24, e34;
707 gimple_stmt_iterator gsi;
709 gcc_assert (is_gimple_assign (stmt)
710 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
711 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
713 optype = TREE_TYPE (gimple_assign_lhs (stmt));
714 op1 = gimple_assign_rhs1 (stmt);
715 op2 = gimple_assign_rhs2 (stmt);
717 bb = gimple_bb (stmt);
718 gsi = gsi_for_stmt (stmt);
720 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
721 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
722 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
723 stmt2 = gimple_build_assign (tmp1, op2);
724 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
725 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
726 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
727 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
728 bb1end = stmt3;
730 tmp2 = create_tmp_reg (optype, "PROF");
731 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
732 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
733 bb2end = stmt1;
735 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
736 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
737 bb3end = stmt1;
739 /* Fix CFG. */
740 /* Edge e23 connects bb2 to bb3, etc. */
741 e12 = split_block (bb, bb1end);
742 bb2 = e12->dest;
743 bb2->count = count;
744 e23 = split_block (bb2, bb2end);
745 bb3 = e23->dest;
746 bb3->count = all - count;
747 e34 = split_block (bb3, bb3end);
748 bb4 = e34->dest;
749 bb4->count = all;
751 e12->flags &= ~EDGE_FALLTHRU;
752 e12->flags |= EDGE_FALSE_VALUE;
753 e12->probability = prob;
754 e12->count = count;
756 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
757 e13->probability = REG_BR_PROB_BASE - prob;
758 e13->count = all - count;
760 remove_edge (e23);
762 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
763 e24->probability = REG_BR_PROB_BASE;
764 e24->count = count;
766 e34->probability = REG_BR_PROB_BASE;
767 e34->count = all - count;
769 return tmp2;
772 /* Do transform 1) on INSN if applicable. */
774 static bool
775 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
777 histogram_value histogram;
778 enum tree_code code;
779 gcov_type val, count, all;
780 tree result, value, tree_val;
781 gcov_type prob;
782 gassign *stmt;
784 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
785 if (!stmt)
786 return false;
788 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
789 return false;
791 code = gimple_assign_rhs_code (stmt);
793 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
794 return false;
796 histogram = gimple_histogram_value_of_type (cfun, stmt,
797 HIST_TYPE_SINGLE_VALUE);
798 if (!histogram)
799 return false;
801 value = histogram->hvalue.value;
802 val = histogram->hvalue.counters[0];
803 count = histogram->hvalue.counters[1];
804 all = histogram->hvalue.counters[2];
805 gimple_remove_histogram_value (cfun, stmt, histogram);
807 /* We require that count is at least half of all; this means
808 that for the transformation to fire the value must be constant
809 at least 50% of time (and 75% gives the guarantee of usage). */
810 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
811 || 2 * count < all
812 || optimize_bb_for_size_p (gimple_bb (stmt)))
813 return false;
815 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
816 return false;
818 /* Compute probability of taking the optimal path. */
819 if (all > 0)
820 prob = GCOV_COMPUTE_SCALE (count, all);
821 else
822 prob = 0;
824 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
825 tree_val = build_int_cst (get_gcov_type (), val);
826 else
828 HOST_WIDE_INT a[2];
829 a[0] = (unsigned HOST_WIDE_INT) val;
830 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
832 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
833 TYPE_PRECISION (get_gcov_type ()), false));
835 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
837 if (dump_file)
839 fprintf (dump_file, "Div/mod by constant ");
840 print_generic_expr (dump_file, value, TDF_SLIM);
841 fprintf (dump_file, "=");
842 print_generic_expr (dump_file, tree_val, TDF_SLIM);
843 fprintf (dump_file, " transformation on insn ");
844 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
847 gimple_assign_set_rhs_from_tree (si, result);
848 update_stmt (gsi_stmt (*si));
850 return true;
853 /* Generate code for transformation 2 (with parent gimple assign STMT and
854 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
855 within roundoff error). This generates the result into a temp and returns
856 the temp; it does not replace or alter the original STMT. */
858 static tree
859 gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
861 gassign *stmt1, *stmt2, *stmt3;
862 gcond *stmt4;
863 tree tmp2, tmp3;
864 gimple *bb1end, *bb2end, *bb3end;
865 basic_block bb, bb2, bb3, bb4;
866 tree optype, op1, op2;
867 edge e12, e13, e23, e24, e34;
868 gimple_stmt_iterator gsi;
869 tree result;
871 gcc_assert (is_gimple_assign (stmt)
872 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
874 optype = TREE_TYPE (gimple_assign_lhs (stmt));
875 op1 = gimple_assign_rhs1 (stmt);
876 op2 = gimple_assign_rhs2 (stmt);
878 bb = gimple_bb (stmt);
879 gsi = gsi_for_stmt (stmt);
881 result = create_tmp_reg (optype, "PROF");
882 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
883 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
884 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
885 build_int_cst (optype, -1));
886 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
887 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
888 NULL_TREE, NULL_TREE);
889 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
890 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
891 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
892 bb1end = stmt4;
894 /* tmp2 == op2-1 inherited from previous block. */
895 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
896 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
897 bb2end = stmt1;
899 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
900 op1, op2);
901 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
902 bb3end = stmt1;
904 /* Fix CFG. */
905 /* Edge e23 connects bb2 to bb3, etc. */
906 e12 = split_block (bb, bb1end);
907 bb2 = e12->dest;
908 bb2->count = count;
909 e23 = split_block (bb2, bb2end);
910 bb3 = e23->dest;
911 bb3->count = all - count;
912 e34 = split_block (bb3, bb3end);
913 bb4 = e34->dest;
914 bb4->count = all;
916 e12->flags &= ~EDGE_FALLTHRU;
917 e12->flags |= EDGE_FALSE_VALUE;
918 e12->probability = prob;
919 e12->count = count;
921 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
922 e13->probability = REG_BR_PROB_BASE - prob;
923 e13->count = all - count;
925 remove_edge (e23);
927 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
928 e24->probability = REG_BR_PROB_BASE;
929 e24->count = count;
931 e34->probability = REG_BR_PROB_BASE;
932 e34->count = all - count;
934 return result;
937 /* Do transform 2) on INSN if applicable. */
939 static bool
940 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
942 histogram_value histogram;
943 enum tree_code code;
944 gcov_type count, wrong_values, all;
945 tree lhs_type, result, value;
946 gcov_type prob;
947 gassign *stmt;
949 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
950 if (!stmt)
951 return false;
953 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
954 if (!INTEGRAL_TYPE_P (lhs_type))
955 return false;
957 code = gimple_assign_rhs_code (stmt);
959 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
960 return false;
962 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
963 if (!histogram)
964 return false;
966 value = histogram->hvalue.value;
967 wrong_values = histogram->hvalue.counters[0];
968 count = histogram->hvalue.counters[1];
970 gimple_remove_histogram_value (cfun, stmt, histogram);
972 /* We require that we hit a power of 2 at least half of all evaluations. */
973 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
974 || count < wrong_values
975 || optimize_bb_for_size_p (gimple_bb (stmt)))
976 return false;
978 if (dump_file)
980 fprintf (dump_file, "Mod power of 2 transformation on insn ");
981 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
984 /* Compute probability of taking the optimal path. */
985 all = count + wrong_values;
987 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
988 return false;
990 if (all > 0)
991 prob = GCOV_COMPUTE_SCALE (count, all);
992 else
993 prob = 0;
995 result = gimple_mod_pow2 (stmt, prob, count, all);
997 gimple_assign_set_rhs_from_tree (si, result);
998 update_stmt (gsi_stmt (*si));
1000 return true;
1003 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1004 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1005 supported and this is built into this interface. The probabilities of taking
1006 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1007 COUNT2/ALL respectively within roundoff error). This generates the
1008 result into a temp and returns the temp; it does not replace or alter
1009 the original STMT. */
1010 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1012 static tree
1013 gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
1014 gcov_type count1, gcov_type count2, gcov_type all)
1016 gassign *stmt1;
1017 gimple *stmt2;
1018 gcond *stmt3;
1019 tree tmp1;
1020 gimple *bb1end, *bb2end = NULL, *bb3end;
1021 basic_block bb, bb2, bb3, bb4;
1022 tree optype, op1, op2;
1023 edge e12, e23 = 0, e24, e34, e14;
1024 gimple_stmt_iterator gsi;
1025 tree result;
1027 gcc_assert (is_gimple_assign (stmt)
1028 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1030 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1031 op1 = gimple_assign_rhs1 (stmt);
1032 op2 = gimple_assign_rhs2 (stmt);
1034 bb = gimple_bb (stmt);
1035 gsi = gsi_for_stmt (stmt);
1037 result = create_tmp_reg (optype, "PROF");
1038 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1039 stmt1 = gimple_build_assign (result, op1);
1040 stmt2 = gimple_build_assign (tmp1, op2);
1041 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1042 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1043 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1044 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1045 bb1end = stmt3;
1047 if (ncounts) /* Assumed to be 0 or 1 */
1049 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1050 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1051 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1052 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1053 bb2end = stmt2;
1056 /* Fallback case. */
1057 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1058 result, tmp1);
1059 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1060 bb3end = stmt1;
1062 /* Fix CFG. */
1063 /* Edge e23 connects bb2 to bb3, etc. */
1064 /* However block 3 is optional; if it is not there, references
1065 to 3 really refer to block 2. */
1066 e12 = split_block (bb, bb1end);
1067 bb2 = e12->dest;
1068 bb2->count = all - count1;
1070 if (ncounts) /* Assumed to be 0 or 1. */
1072 e23 = split_block (bb2, bb2end);
1073 bb3 = e23->dest;
1074 bb3->count = all - count1 - count2;
1077 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1078 bb4 = e34->dest;
1079 bb4->count = all;
1081 e12->flags &= ~EDGE_FALLTHRU;
1082 e12->flags |= EDGE_FALSE_VALUE;
1083 e12->probability = REG_BR_PROB_BASE - prob1;
1084 e12->count = all - count1;
1086 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1087 e14->probability = prob1;
1088 e14->count = count1;
1090 if (ncounts) /* Assumed to be 0 or 1. */
1092 e23->flags &= ~EDGE_FALLTHRU;
1093 e23->flags |= EDGE_FALSE_VALUE;
1094 e23->count = all - count1 - count2;
1095 e23->probability = REG_BR_PROB_BASE - prob2;
1097 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1098 e24->probability = prob2;
1099 e24->count = count2;
1102 e34->probability = REG_BR_PROB_BASE;
1103 e34->count = all - count1 - count2;
1105 return result;
1108 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1110 static bool
1111 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1113 histogram_value histogram;
1114 enum tree_code code;
1115 gcov_type count, wrong_values, all;
1116 tree lhs_type, result;
1117 gcov_type prob1, prob2;
1118 unsigned int i, steps;
1119 gcov_type count1, count2;
1120 gassign *stmt;
1121 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1122 if (!stmt)
1123 return false;
1125 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1126 if (!INTEGRAL_TYPE_P (lhs_type))
1127 return false;
1129 code = gimple_assign_rhs_code (stmt);
1131 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1132 return false;
1134 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1135 if (!histogram)
1136 return false;
1138 all = 0;
1139 wrong_values = 0;
1140 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1141 all += histogram->hvalue.counters[i];
1143 wrong_values += histogram->hvalue.counters[i];
1144 wrong_values += histogram->hvalue.counters[i+1];
1145 steps = histogram->hdata.intvl.steps;
1146 all += wrong_values;
1147 count1 = histogram->hvalue.counters[0];
1148 count2 = histogram->hvalue.counters[1];
1150 /* Compute probability of taking the optimal path. */
1151 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1153 gimple_remove_histogram_value (cfun, stmt, histogram);
1154 return false;
1157 if (flag_profile_correction && count1 + count2 > all)
1158 all = count1 + count2;
1160 gcc_assert (count1 + count2 <= all);
1162 /* We require that we use just subtractions in at least 50% of all
1163 evaluations. */
1164 count = 0;
1165 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1167 count += histogram->hvalue.counters[i];
1168 if (count * 2 >= all)
1169 break;
1171 if (i == steps
1172 || optimize_bb_for_size_p (gimple_bb (stmt)))
1173 return false;
1175 gimple_remove_histogram_value (cfun, stmt, histogram);
1176 if (dump_file)
1178 fprintf (dump_file, "Mod subtract transformation on insn ");
1179 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1182 /* Compute probability of taking the optimal path(s). */
1183 if (all > 0)
1185 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1186 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1188 else
1190 prob1 = prob2 = 0;
1193 /* In practice, "steps" is always 2. This interface reflects this,
1194 and will need to be changed if "steps" can change. */
1195 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1197 gimple_assign_set_rhs_from_tree (si, result);
1198 update_stmt (gsi_stmt (*si));
1200 return true;
1203 typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
1205 static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
1207 /* Returns true if node graph is initialized. This
1208 is used to test if profile_id has been created
1209 for cgraph_nodes. */
1211 bool
1212 coverage_node_map_initialized_p (void)
1214 return cgraph_node_map != 0;
1217 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1218 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1219 that the PROFILE_IDs was already assigned. */
1221 void
1222 init_node_map (bool local)
1224 struct cgraph_node *n;
1225 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
1227 FOR_EACH_DEFINED_FUNCTION (n)
1228 if (n->has_gimple_body_p ())
1230 cgraph_node **val;
1231 if (local)
1233 n->profile_id = coverage_compute_profile_id (n);
1234 while ((val = cgraph_node_map->get (n->profile_id))
1235 || !n->profile_id)
1237 if (dump_file)
1238 fprintf (dump_file, "Local profile-id %i conflict"
1239 " with nodes %s/%i %s/%i\n",
1240 n->profile_id,
1241 n->name (),
1242 n->order,
1243 (*val)->name (),
1244 (*val)->order);
1245 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1248 else if (!n->profile_id)
1250 if (dump_file)
1251 fprintf (dump_file,
1252 "Node %s/%i has no profile-id"
1253 " (profile feedback missing?)\n",
1254 n->name (),
1255 n->order);
1256 continue;
1258 else if ((val = cgraph_node_map->get (n->profile_id)))
1260 if (dump_file)
1261 fprintf (dump_file,
1262 "Node %s/%i has IP profile-id %i conflict. "
1263 "Giving up.\n",
1264 n->name (),
1265 n->order,
1266 n->profile_id);
1267 *val = NULL;
1268 continue;
1270 cgraph_node_map->put (n->profile_id, n);
1274 /* Delete the CGRAPH_NODE_MAP. */
1276 void
1277 del_node_map (void)
1279 delete cgraph_node_map;
1282 /* Return cgraph node for function with pid */
1284 struct cgraph_node*
1285 find_func_by_profile_id (int profile_id)
1287 cgraph_node **val = cgraph_node_map->get (profile_id);
1288 if (val)
1289 return *val;
1290 else
1291 return NULL;
1294 /* Perform sanity check on the indirect call target. Due to race conditions,
1295 false function target may be attributed to an indirect call site. If the
1296 call expression type mismatches with the target function's type, expand_call
1297 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1298 Returns true if TARGET is considered ok for call CALL_STMT. */
1300 bool
1301 check_ic_target (gcall *call_stmt, struct cgraph_node *target)
1303 location_t locus;
1304 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1305 return true;
1307 locus = gimple_location (call_stmt);
1308 if (dump_enabled_p ())
1309 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1310 "Skipping target %s with mismatching types for icall\n",
1311 target->name ());
1312 return false;
1315 /* Do transformation
1317 if (actual_callee_address == address_of_most_common_function/method)
1318 do direct call
1319 else
1320 old call
1323 gcall *
1324 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1325 int prob, gcov_type count, gcov_type all)
1327 gcall *dcall_stmt;
1328 gassign *load_stmt;
1329 gcond *cond_stmt;
1330 gcall *iretbnd_stmt = NULL;
1331 tree tmp0, tmp1, tmp;
1332 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1333 tree optype = build_pointer_type (void_type_node);
1334 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1335 gimple_stmt_iterator gsi;
1336 int lp_nr, dflags;
1337 edge e_eh, e;
1338 edge_iterator ei;
1339 gimple_stmt_iterator psi;
1341 cond_bb = gimple_bb (icall_stmt);
1342 gsi = gsi_for_stmt (icall_stmt);
1344 if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
1345 iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
1347 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1348 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1349 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1350 load_stmt = gimple_build_assign (tmp0, tmp);
1351 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1353 tmp = fold_convert (optype, build_addr (direct_call->decl));
1354 load_stmt = gimple_build_assign (tmp1, tmp);
1355 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1357 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1358 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1360 if (TREE_CODE (gimple_vdef (icall_stmt)) == SSA_NAME)
1362 unlink_stmt_vdef (icall_stmt);
1363 release_ssa_name (gimple_vdef (icall_stmt));
1365 gimple_set_vdef (icall_stmt, NULL_TREE);
1366 gimple_set_vuse (icall_stmt, NULL_TREE);
1367 update_stmt (icall_stmt);
1368 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1369 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1370 dflags = flags_from_decl_or_type (direct_call->decl);
1371 if ((dflags & ECF_NORETURN) != 0
1372 && should_remove_lhs_p (gimple_call_lhs (dcall_stmt)))
1373 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1374 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1376 /* Fix CFG. */
1377 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1378 e_cd = split_block (cond_bb, cond_stmt);
1379 dcall_bb = e_cd->dest;
1380 dcall_bb->count = count;
1382 e_di = split_block (dcall_bb, dcall_stmt);
1383 icall_bb = e_di->dest;
1384 icall_bb->count = all - count;
1386 /* Do not disturb existing EH edges from the indirect call. */
1387 if (!stmt_ends_bb_p (icall_stmt))
1388 e_ij = split_block (icall_bb, icall_stmt);
1389 else
1391 e_ij = find_fallthru_edge (icall_bb->succs);
1392 /* The indirect call might be noreturn. */
1393 if (e_ij != NULL)
1395 e_ij->probability = REG_BR_PROB_BASE;
1396 e_ij->count = all - count;
1397 e_ij = single_pred_edge (split_edge (e_ij));
1400 if (e_ij != NULL)
1402 join_bb = e_ij->dest;
1403 join_bb->count = all;
1406 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1407 e_cd->probability = prob;
1408 e_cd->count = count;
1410 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1411 e_ci->probability = REG_BR_PROB_BASE - prob;
1412 e_ci->count = all - count;
1414 remove_edge (e_di);
1416 if (e_ij != NULL)
1418 if ((dflags & ECF_NORETURN) != 0)
1419 e_ij->count = all;
1420 else
1422 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1423 e_dj->probability = REG_BR_PROB_BASE;
1424 e_dj->count = count;
1426 e_ij->count = all - count;
1428 e_ij->probability = REG_BR_PROB_BASE;
1431 /* Insert PHI node for the call result if necessary. */
1432 if (gimple_call_lhs (icall_stmt)
1433 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1434 && (dflags & ECF_NORETURN) == 0)
1436 tree result = gimple_call_lhs (icall_stmt);
1437 gphi *phi = create_phi_node (result, join_bb);
1438 gimple_call_set_lhs (icall_stmt,
1439 duplicate_ssa_name (result, icall_stmt));
1440 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1441 gimple_call_set_lhs (dcall_stmt,
1442 duplicate_ssa_name (result, dcall_stmt));
1443 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1445 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1446 call then we need to make it's copy for the direct
1447 call. */
1448 if (iretbnd_stmt)
1450 if (gimple_call_lhs (iretbnd_stmt))
1452 gimple *copy;
1454 if (TREE_CODE (gimple_vdef (iretbnd_stmt)) == SSA_NAME)
1456 unlink_stmt_vdef (iretbnd_stmt);
1457 release_ssa_name (gimple_vdef (iretbnd_stmt));
1459 gimple_set_vdef (iretbnd_stmt, NULL_TREE);
1460 gimple_set_vuse (iretbnd_stmt, NULL_TREE);
1461 update_stmt (iretbnd_stmt);
1463 result = gimple_call_lhs (iretbnd_stmt);
1464 phi = create_phi_node (result, join_bb);
1466 copy = gimple_copy (iretbnd_stmt);
1467 gimple_call_set_arg (copy, 0,
1468 gimple_call_lhs (dcall_stmt));
1469 gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
1470 gsi_insert_on_edge (e_dj, copy);
1471 add_phi_arg (phi, gimple_call_lhs (copy),
1472 e_dj, UNKNOWN_LOCATION);
1474 gimple_call_set_arg (iretbnd_stmt, 0,
1475 gimple_call_lhs (icall_stmt));
1476 gimple_call_set_lhs (iretbnd_stmt,
1477 duplicate_ssa_name (result, iretbnd_stmt));
1478 psi = gsi_for_stmt (iretbnd_stmt);
1479 gsi_remove (&psi, false);
1480 gsi_insert_on_edge (e_ij, iretbnd_stmt);
1481 add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
1482 e_ij, UNKNOWN_LOCATION);
1484 gsi_commit_one_edge_insert (e_dj, NULL);
1485 gsi_commit_one_edge_insert (e_ij, NULL);
1487 else
1489 psi = gsi_for_stmt (iretbnd_stmt);
1490 gsi_remove (&psi, true);
1495 /* Build an EH edge for the direct call if necessary. */
1496 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1497 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1499 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1502 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1503 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1505 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1506 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1507 !gsi_end_p (psi); gsi_next (&psi))
1509 gphi *phi = psi.phi ();
1510 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1511 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1514 if (!stmt_could_throw_p (dcall_stmt))
1515 gimple_purge_dead_eh_edges (dcall_bb);
1516 return dcall_stmt;
1520 For every checked indirect/virtual call determine if most common pid of
1521 function/class method has probability more than 50%. If yes modify code of
1522 this call to:
1525 static bool
1526 gimple_ic_transform (gimple_stmt_iterator *gsi)
1528 gcall *stmt;
1529 histogram_value histogram;
1530 gcov_type val, count, all, bb_all;
1531 struct cgraph_node *direct_call;
1533 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1534 if (!stmt)
1535 return false;
1537 if (gimple_call_fndecl (stmt) != NULL_TREE)
1538 return false;
1540 if (gimple_call_internal_p (stmt))
1541 return false;
1543 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1544 if (!histogram)
1545 return false;
1547 val = histogram->hvalue.counters [0];
1548 count = histogram->hvalue.counters [1];
1549 all = histogram->hvalue.counters [2];
1551 bb_all = gimple_bb (stmt)->count;
1552 /* The order of CHECK_COUNTER calls is important -
1553 since check_counter can correct the third parameter
1554 and we want to make count <= all <= bb_all. */
1555 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1556 || check_counter (stmt, "ic", &count, &all, all))
1558 gimple_remove_histogram_value (cfun, stmt, histogram);
1559 return false;
1562 if (4 * count <= 3 * all)
1563 return false;
1565 direct_call = find_func_by_profile_id ((int)val);
1567 if (direct_call == NULL)
1569 if (val)
1571 if (dump_file)
1573 fprintf (dump_file, "Indirect call -> direct call from other module");
1574 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1575 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1578 return false;
1581 if (!check_ic_target (stmt, direct_call))
1583 if (dump_file)
1585 fprintf (dump_file, "Indirect call -> direct call ");
1586 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1587 fprintf (dump_file, "=> ");
1588 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1589 fprintf (dump_file, " transformation skipped because of type mismatch");
1590 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1592 gimple_remove_histogram_value (cfun, stmt, histogram);
1593 return false;
1596 if (dump_file)
1598 fprintf (dump_file, "Indirect call -> direct call ");
1599 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1600 fprintf (dump_file, "=> ");
1601 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1602 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1603 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1604 fprintf (dump_file, "hist->count %" PRId64
1605 " hist->all %" PRId64"\n", count, all);
1608 return true;
1611 /* Return true if the stringop CALL shall be profiled. SIZE_ARG be
1612 set to the argument index for the size of the string operation. */
1614 static bool
1615 interesting_stringop_to_profile_p (gcall *call, int *size_arg)
1617 enum built_in_function fcode;
1619 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (call));
1620 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1621 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1622 return false;
1624 switch (fcode)
1626 case BUILT_IN_MEMCPY:
1627 case BUILT_IN_MEMPCPY:
1628 *size_arg = 2;
1629 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1630 INTEGER_TYPE, VOID_TYPE);
1631 case BUILT_IN_MEMSET:
1632 *size_arg = 2;
1633 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1634 INTEGER_TYPE, VOID_TYPE);
1635 case BUILT_IN_BZERO:
1636 *size_arg = 1;
1637 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1638 VOID_TYPE);
1639 default:
1640 gcc_unreachable ();
1644 /* Convert stringop (..., vcall_size)
1645 into
1646 if (vcall_size == icall_size)
1647 stringop (..., icall_size);
1648 else
1649 stringop (..., vcall_size);
1650 assuming we'll propagate a true constant into ICALL_SIZE later. */
1652 static void
1653 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
1654 gcov_type count, gcov_type all)
1656 gassign *tmp_stmt;
1657 gcond *cond_stmt;
1658 gcall *icall_stmt;
1659 tree tmp0, tmp1, vcall_size, optype;
1660 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1661 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1662 gimple_stmt_iterator gsi;
1663 int size_arg;
1665 if (!interesting_stringop_to_profile_p (vcall_stmt, &size_arg))
1666 gcc_unreachable ();
1668 cond_bb = gimple_bb (vcall_stmt);
1669 gsi = gsi_for_stmt (vcall_stmt);
1671 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1672 optype = TREE_TYPE (vcall_size);
1674 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1675 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1676 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1677 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1679 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1680 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1682 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1683 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1685 if (TREE_CODE (gimple_vdef (vcall_stmt)) == SSA_NAME)
1687 unlink_stmt_vdef (vcall_stmt);
1688 release_ssa_name (gimple_vdef (vcall_stmt));
1690 gimple_set_vdef (vcall_stmt, NULL);
1691 gimple_set_vuse (vcall_stmt, NULL);
1692 update_stmt (vcall_stmt);
1693 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1694 gimple_call_set_arg (icall_stmt, size_arg,
1695 fold_convert (optype, icall_size));
1696 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1698 /* Fix CFG. */
1699 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1700 e_ci = split_block (cond_bb, cond_stmt);
1701 icall_bb = e_ci->dest;
1702 icall_bb->count = count;
1704 e_iv = split_block (icall_bb, icall_stmt);
1705 vcall_bb = e_iv->dest;
1706 vcall_bb->count = all - count;
1708 e_vj = split_block (vcall_bb, vcall_stmt);
1709 join_bb = e_vj->dest;
1710 join_bb->count = all;
1712 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1713 e_ci->probability = prob;
1714 e_ci->count = count;
1716 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1717 e_cv->probability = REG_BR_PROB_BASE - prob;
1718 e_cv->count = all - count;
1720 remove_edge (e_iv);
1722 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1723 e_ij->probability = REG_BR_PROB_BASE;
1724 e_ij->count = count;
1726 e_vj->probability = REG_BR_PROB_BASE;
1727 e_vj->count = all - count;
1729 /* Insert PHI node for the call result if necessary. */
1730 if (gimple_call_lhs (vcall_stmt)
1731 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1733 tree result = gimple_call_lhs (vcall_stmt);
1734 gphi *phi = create_phi_node (result, join_bb);
1735 gimple_call_set_lhs (vcall_stmt,
1736 duplicate_ssa_name (result, vcall_stmt));
1737 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1738 gimple_call_set_lhs (icall_stmt,
1739 duplicate_ssa_name (result, icall_stmt));
1740 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1743 /* Because these are all string op builtins, they're all nothrow. */
1744 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1745 gcc_assert (!stmt_could_throw_p (icall_stmt));
1748 /* Find values inside STMT for that we want to measure histograms for
1749 division/modulo optimization. */
1751 static bool
1752 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1754 gcall *stmt;
1755 tree blck_size;
1756 enum built_in_function fcode;
1757 histogram_value histogram;
1758 gcov_type count, all, val;
1759 tree dest, src;
1760 unsigned int dest_align, src_align;
1761 gcov_type prob;
1762 tree tree_val;
1763 int size_arg;
1765 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1766 if (!stmt)
1767 return false;
1769 if (!gimple_call_builtin_p (gsi_stmt (*gsi), BUILT_IN_NORMAL))
1770 return false;
1772 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
1773 return false;
1775 blck_size = gimple_call_arg (stmt, size_arg);
1776 if (TREE_CODE (blck_size) == INTEGER_CST)
1777 return false;
1779 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1780 if (!histogram)
1781 return false;
1783 val = histogram->hvalue.counters[0];
1784 count = histogram->hvalue.counters[1];
1785 all = histogram->hvalue.counters[2];
1786 gimple_remove_histogram_value (cfun, stmt, histogram);
1788 /* We require that count is at least half of all; this means
1789 that for the transformation to fire the value must be constant
1790 at least 80% of time. */
1791 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1792 return false;
1793 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1794 return false;
1795 if (all > 0)
1796 prob = GCOV_COMPUTE_SCALE (count, all);
1797 else
1798 prob = 0;
1800 dest = gimple_call_arg (stmt, 0);
1801 dest_align = get_pointer_alignment (dest);
1802 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (stmt));
1803 switch (fcode)
1805 case BUILT_IN_MEMCPY:
1806 case BUILT_IN_MEMPCPY:
1807 src = gimple_call_arg (stmt, 1);
1808 src_align = get_pointer_alignment (src);
1809 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1810 return false;
1811 break;
1812 case BUILT_IN_MEMSET:
1813 if (!can_store_by_pieces (val, builtin_memset_read_str,
1814 gimple_call_arg (stmt, 1),
1815 dest_align, true))
1816 return false;
1817 break;
1818 case BUILT_IN_BZERO:
1819 if (!can_store_by_pieces (val, builtin_memset_read_str,
1820 integer_zero_node,
1821 dest_align, true))
1822 return false;
1823 break;
1824 default:
1825 gcc_unreachable ();
1828 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1829 tree_val = build_int_cst (get_gcov_type (), val);
1830 else
1832 HOST_WIDE_INT a[2];
1833 a[0] = (unsigned HOST_WIDE_INT) val;
1834 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1836 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1837 TYPE_PRECISION (get_gcov_type ()), false));
1840 if (dump_file)
1842 fprintf (dump_file, "Single value %i stringop transformation on ",
1843 (int)val);
1844 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1847 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1849 return true;
1852 void
1853 stringop_block_profile (gimple *stmt, unsigned int *expected_align,
1854 HOST_WIDE_INT *expected_size)
1856 histogram_value histogram;
1857 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1859 if (!histogram)
1860 *expected_size = -1;
1861 else if (!histogram->hvalue.counters[1])
1863 *expected_size = -1;
1864 gimple_remove_histogram_value (cfun, stmt, histogram);
1866 else
1868 gcov_type size;
1869 size = ((histogram->hvalue.counters[0]
1870 + histogram->hvalue.counters[1] / 2)
1871 / histogram->hvalue.counters[1]);
1872 /* Even if we can hold bigger value in SIZE, INT_MAX
1873 is safe "infinity" for code generation strategies. */
1874 if (size > INT_MAX)
1875 size = INT_MAX;
1876 *expected_size = size;
1877 gimple_remove_histogram_value (cfun, stmt, histogram);
1880 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1882 if (!histogram)
1883 *expected_align = 0;
1884 else if (!histogram->hvalue.counters[0])
1886 gimple_remove_histogram_value (cfun, stmt, histogram);
1887 *expected_align = 0;
1889 else
1891 gcov_type count;
1892 unsigned int alignment;
1894 count = histogram->hvalue.counters[0];
1895 alignment = 1;
1896 while (!(count & alignment)
1897 && (alignment <= UINT_MAX / 2 / BITS_PER_UNIT))
1898 alignment <<= 1;
1899 *expected_align = alignment * BITS_PER_UNIT;
1900 gimple_remove_histogram_value (cfun, stmt, histogram);
1905 /* Find values inside STMT for that we want to measure histograms for
1906 division/modulo optimization. */
1908 static void
1909 gimple_divmod_values_to_profile (gimple *stmt, histogram_values *values)
1911 tree lhs, divisor, op0, type;
1912 histogram_value hist;
1914 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1915 return;
1917 lhs = gimple_assign_lhs (stmt);
1918 type = TREE_TYPE (lhs);
1919 if (!INTEGRAL_TYPE_P (type))
1920 return;
1922 switch (gimple_assign_rhs_code (stmt))
1924 case TRUNC_DIV_EXPR:
1925 case TRUNC_MOD_EXPR:
1926 divisor = gimple_assign_rhs2 (stmt);
1927 op0 = gimple_assign_rhs1 (stmt);
1929 values->reserve (3);
1931 if (TREE_CODE (divisor) == SSA_NAME)
1932 /* Check for the case where the divisor is the same value most
1933 of the time. */
1934 values->quick_push (gimple_alloc_histogram_value (cfun,
1935 HIST_TYPE_SINGLE_VALUE,
1936 stmt, divisor));
1938 /* For mod, check whether it is not often a noop (or replaceable by
1939 a few subtractions). */
1940 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1941 && TYPE_UNSIGNED (type)
1942 && TREE_CODE (divisor) == SSA_NAME)
1944 tree val;
1945 /* Check for a special case where the divisor is power of 2. */
1946 values->quick_push (gimple_alloc_histogram_value (cfun,
1947 HIST_TYPE_POW2,
1948 stmt, divisor));
1950 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1951 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1952 stmt, val);
1953 hist->hdata.intvl.int_start = 0;
1954 hist->hdata.intvl.steps = 2;
1955 values->quick_push (hist);
1957 return;
1959 default:
1960 return;
1964 /* Find calls inside STMT for that we want to measure histograms for
1965 indirect/virtual call optimization. */
1967 static void
1968 gimple_indirect_call_to_profile (gimple *stmt, histogram_values *values)
1970 tree callee;
1972 if (gimple_code (stmt) != GIMPLE_CALL
1973 || gimple_call_internal_p (stmt)
1974 || gimple_call_fndecl (stmt) != NULL_TREE)
1975 return;
1977 callee = gimple_call_fn (stmt);
1979 values->reserve (3);
1981 values->quick_push (gimple_alloc_histogram_value (
1982 cfun,
1983 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
1984 HIST_TYPE_INDIR_CALL_TOPN :
1985 HIST_TYPE_INDIR_CALL,
1986 stmt, callee));
1988 return;
1991 /* Find values inside STMT for that we want to measure histograms for
1992 string operations. */
1994 static void
1995 gimple_stringops_values_to_profile (gimple *gs, histogram_values *values)
1997 gcall *stmt;
1998 tree blck_size;
1999 tree dest;
2000 int size_arg;
2002 stmt = dyn_cast <gcall *> (gs);
2003 if (!stmt)
2004 return;
2006 if (!gimple_call_builtin_p (gs, BUILT_IN_NORMAL))
2007 return;
2009 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
2010 return;
2012 dest = gimple_call_arg (stmt, 0);
2013 blck_size = gimple_call_arg (stmt, size_arg);
2015 if (TREE_CODE (blck_size) != INTEGER_CST)
2017 values->safe_push (gimple_alloc_histogram_value (cfun,
2018 HIST_TYPE_SINGLE_VALUE,
2019 stmt, blck_size));
2020 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2021 stmt, blck_size));
2024 if (TREE_CODE (blck_size) != INTEGER_CST)
2025 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2026 stmt, dest));
2029 /* Find values inside STMT for that we want to measure histograms and adds
2030 them to list VALUES. */
2032 static void
2033 gimple_values_to_profile (gimple *stmt, histogram_values *values)
2035 gimple_divmod_values_to_profile (stmt, values);
2036 gimple_stringops_values_to_profile (stmt, values);
2037 gimple_indirect_call_to_profile (stmt, values);
2040 void
2041 gimple_find_values_to_profile (histogram_values *values)
2043 basic_block bb;
2044 gimple_stmt_iterator gsi;
2045 unsigned i;
2046 histogram_value hist = NULL;
2047 values->create (0);
2049 FOR_EACH_BB_FN (bb, cfun)
2050 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2051 gimple_values_to_profile (gsi_stmt (gsi), values);
2053 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2055 FOR_EACH_VEC_ELT (*values, i, hist)
2057 switch (hist->type)
2059 case HIST_TYPE_INTERVAL:
2060 hist->n_counters = hist->hdata.intvl.steps + 2;
2061 break;
2063 case HIST_TYPE_POW2:
2064 hist->n_counters = 2;
2065 break;
2067 case HIST_TYPE_SINGLE_VALUE:
2068 hist->n_counters = 3;
2069 break;
2071 case HIST_TYPE_INDIR_CALL:
2072 hist->n_counters = 3;
2073 break;
2075 case HIST_TYPE_TIME_PROFILE:
2076 hist->n_counters = 1;
2077 break;
2079 case HIST_TYPE_AVERAGE:
2080 hist->n_counters = 2;
2081 break;
2083 case HIST_TYPE_IOR:
2084 hist->n_counters = 1;
2085 break;
2087 case HIST_TYPE_INDIR_CALL_TOPN:
2088 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2089 break;
2091 default:
2092 gcc_unreachable ();
2094 if (dump_file)
2096 fprintf (dump_file, "Stmt ");
2097 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2098 dump_histogram_value (dump_file, hist);