[doc][13/14] Document AArch64 target attributes and pragmas
[official-gcc.git] / gcc / value-prof.c
blob8aace8c872c9bf847c23c9cde1f65a39d9e8bfb2
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2015 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 "cfghooks.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "tree-nested.h"
32 #include "calls.h"
33 #include "flags.h"
34 #include "insn-config.h"
35 #include "expmed.h"
36 #include "dojump.h"
37 #include "explow.h"
38 #include "emit-rtl.h"
39 #include "varasm.h"
40 #include "stmt.h"
41 #include "expr.h"
42 #include "value-prof.h"
43 #include "recog.h"
44 #include "insn-codes.h"
45 #include "optabs.h"
46 #include "regs.h"
47 #include "internal-fn.h"
48 #include "tree-eh.h"
49 #include "gimplify.h"
50 #include "gimple-iterator.h"
51 #include "tree-cfg.h"
52 #include "diagnostic.h"
53 #include "gimple-pretty-print.h"
54 #include "coverage.h"
55 #include "gcov-io.h"
56 #include "timevar.h"
57 #include "dumpfile.h"
58 #include "profile.h"
59 #include "cgraph.h"
60 #include "data-streamer.h"
61 #include "builtins.h"
62 #include "params.h"
63 #include "tree-chkp.h"
65 /* In this file value profile based optimizations are placed. Currently the
66 following optimizations are implemented (for more detailed descriptions
67 see comments at value_profile_transformations):
69 1) Division/modulo specialization. Provided that we can determine that the
70 operands of the division have some special properties, we may use it to
71 produce more effective code.
73 2) Indirect/virtual call specialization. If we can determine most
74 common function callee in indirect/virtual call. We can use this
75 information to improve code effectiveness (especially info for
76 the inliner).
78 3) Speculative prefetching. If we are able to determine that the difference
79 between addresses accessed by a memory reference is usually constant, we
80 may add the prefetch instructions.
81 FIXME: This transformation was removed together with RTL based value
82 profiling.
85 Value profiling internals
86 ==========================
88 Every value profiling transformation starts with defining what values
89 to profile. There are different histogram types (see HIST_TYPE_* in
90 value-prof.h) and each transformation can request one or more histogram
91 types per GIMPLE statement. The function gimple_find_values_to_profile()
92 collects the values to profile in a vec, and adds the number of counters
93 required for the different histogram types.
95 For a -fprofile-generate run, the statements for which values should be
96 recorded, are instrumented in instrument_values(). The instrumentation
97 is done by helper functions that can be found in tree-profile.c, where
98 new types of histograms can be added if necessary.
100 After a -fprofile-use, the value profiling data is read back in by
101 compute_value_histograms() that translates the collected data to
102 histograms and attaches them to the profiled statements via
103 gimple_add_histogram_value(). Histograms are stored in a hash table
104 that is attached to every intrumented function, see VALUE_HISTOGRAMS
105 in function.h.
107 The value-profile transformations driver is the function
108 gimple_value_profile_transformations(). It traverses all statements in
109 the to-be-transformed function, and looks for statements with one or
110 more histograms attached to it. If a statement has histograms, the
111 transformation functions are called on the statement.
113 Limitations / FIXME / TODO:
114 * Only one histogram of each type can be associated with a statement.
115 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
116 (This type of histogram was originally used to implement a form of
117 stride profiling based speculative prefetching to improve SPEC2000
118 scores for memory-bound benchmarks, mcf and equake. However, this
119 was an RTL value-profiling transformation, and those have all been
120 removed.)
121 * Some value profile transformations are done in builtins.c (?!)
122 * Updating of histograms needs some TLC.
123 * The value profiling code could be used to record analysis results
124 from non-profiling (e.g. VRP).
125 * Adding new profilers should be simplified, starting with a cleanup
126 of what-happens-where andwith making gimple_find_values_to_profile
127 and gimple_value_profile_transformations table-driven, perhaps...
130 static tree gimple_divmod_fixed_value (gassign *, tree, int, gcov_type,
131 gcov_type);
132 static tree gimple_mod_pow2 (gassign *, int, gcov_type, gcov_type);
133 static tree gimple_mod_subtract (gassign *, int, int, int, gcov_type,
134 gcov_type, gcov_type);
135 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
136 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
137 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
138 static bool gimple_stringops_transform (gimple_stmt_iterator *);
139 static bool gimple_ic_transform (gimple_stmt_iterator *);
141 /* Allocate histogram value. */
143 histogram_value
144 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
145 enum hist_type type, gimple stmt, tree value)
147 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
148 hist->hvalue.value = value;
149 hist->hvalue.stmt = stmt;
150 hist->type = type;
151 return hist;
154 /* Hash value for histogram. */
156 static hashval_t
157 histogram_hash (const void *x)
159 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
162 /* Return nonzero if statement for histogram_value X is Y. */
164 static int
165 histogram_eq (const void *x, const void *y)
167 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
170 /* Set histogram for STMT. */
172 static void
173 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
175 void **loc;
176 if (!hist && !VALUE_HISTOGRAMS (fun))
177 return;
178 if (!VALUE_HISTOGRAMS (fun))
179 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
180 histogram_eq, NULL);
181 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
182 htab_hash_pointer (stmt),
183 hist ? INSERT : NO_INSERT);
184 if (!hist)
186 if (loc)
187 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
188 return;
190 *loc = hist;
193 /* Get histogram list for STMT. */
195 histogram_value
196 gimple_histogram_value (struct function *fun, gimple stmt)
198 if (!VALUE_HISTOGRAMS (fun))
199 return NULL;
200 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
201 htab_hash_pointer (stmt));
204 /* Add histogram for STMT. */
206 void
207 gimple_add_histogram_value (struct function *fun, gimple stmt,
208 histogram_value hist)
210 hist->hvalue.next = gimple_histogram_value (fun, stmt);
211 set_histogram_value (fun, stmt, hist);
212 hist->fun = fun;
216 /* Remove histogram HIST from STMT's histogram list. */
218 void
219 gimple_remove_histogram_value (struct function *fun, gimple stmt,
220 histogram_value hist)
222 histogram_value hist2 = gimple_histogram_value (fun, stmt);
223 if (hist == hist2)
225 set_histogram_value (fun, stmt, hist->hvalue.next);
227 else
229 while (hist2->hvalue.next != hist)
230 hist2 = hist2->hvalue.next;
231 hist2->hvalue.next = hist->hvalue.next;
233 free (hist->hvalue.counters);
234 #ifdef ENABLE_CHECKING
235 memset (hist, 0xab, sizeof (*hist));
236 #endif
237 free (hist);
241 /* Lookup histogram of type TYPE in the STMT. */
243 histogram_value
244 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
245 enum hist_type type)
247 histogram_value hist;
248 for (hist = gimple_histogram_value (fun, stmt); hist;
249 hist = hist->hvalue.next)
250 if (hist->type == type)
251 return hist;
252 return NULL;
255 /* Dump information about HIST to DUMP_FILE. */
257 static void
258 dump_histogram_value (FILE *dump_file, histogram_value hist)
260 switch (hist->type)
262 case HIST_TYPE_INTERVAL:
263 fprintf (dump_file, "Interval counter range %d -- %d",
264 hist->hdata.intvl.int_start,
265 (hist->hdata.intvl.int_start
266 + hist->hdata.intvl.steps - 1));
267 if (hist->hvalue.counters)
269 unsigned int i;
270 fprintf (dump_file, " [");
271 for (i = 0; i < hist->hdata.intvl.steps; i++)
272 fprintf (dump_file, " %d:%" PRId64,
273 hist->hdata.intvl.int_start + i,
274 (int64_t) hist->hvalue.counters[i]);
275 fprintf (dump_file, " ] outside range:%" PRId64,
276 (int64_t) hist->hvalue.counters[i]);
278 fprintf (dump_file, ".\n");
279 break;
281 case HIST_TYPE_POW2:
282 fprintf (dump_file, "Pow2 counter ");
283 if (hist->hvalue.counters)
285 fprintf (dump_file, "pow2:%" PRId64
286 " nonpow2:%" 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_SINGLE_VALUE:
294 fprintf (dump_file, "Single value ");
295 if (hist->hvalue.counters)
297 fprintf (dump_file, "value:%" PRId64
298 " match:%" PRId64
299 " wrong:%" PRId64,
300 (int64_t) hist->hvalue.counters[0],
301 (int64_t) hist->hvalue.counters[1],
302 (int64_t) hist->hvalue.counters[2]);
304 fprintf (dump_file, ".\n");
305 break;
307 case HIST_TYPE_AVERAGE:
308 fprintf (dump_file, "Average value ");
309 if (hist->hvalue.counters)
311 fprintf (dump_file, "sum:%" PRId64
312 " times:%" PRId64,
313 (int64_t) hist->hvalue.counters[0],
314 (int64_t) hist->hvalue.counters[1]);
316 fprintf (dump_file, ".\n");
317 break;
319 case HIST_TYPE_IOR:
320 fprintf (dump_file, "IOR value ");
321 if (hist->hvalue.counters)
323 fprintf (dump_file, "ior:%" PRId64,
324 (int64_t) hist->hvalue.counters[0]);
326 fprintf (dump_file, ".\n");
327 break;
329 case HIST_TYPE_CONST_DELTA:
330 fprintf (dump_file, "Constant delta ");
331 if (hist->hvalue.counters)
333 fprintf (dump_file, "value:%" PRId64
334 " match:%" PRId64
335 " wrong:%" PRId64,
336 (int64_t) hist->hvalue.counters[0],
337 (int64_t) hist->hvalue.counters[1],
338 (int64_t) hist->hvalue.counters[2]);
340 fprintf (dump_file, ".\n");
341 break;
342 case HIST_TYPE_INDIR_CALL:
343 fprintf (dump_file, "Indirect call ");
344 if (hist->hvalue.counters)
346 fprintf (dump_file, "value:%" PRId64
347 " match:%" PRId64
348 " all:%" PRId64,
349 (int64_t) hist->hvalue.counters[0],
350 (int64_t) hist->hvalue.counters[1],
351 (int64_t) hist->hvalue.counters[2]);
353 fprintf (dump_file, ".\n");
354 break;
355 case HIST_TYPE_TIME_PROFILE:
356 fprintf (dump_file, "Time profile ");
357 if (hist->hvalue.counters)
359 fprintf (dump_file, "time:%" PRId64,
360 (int64_t) hist->hvalue.counters[0]);
362 fprintf (dump_file, ".\n");
363 break;
364 case HIST_TYPE_INDIR_CALL_TOPN:
365 fprintf (dump_file, "Indirect call topn ");
366 if (hist->hvalue.counters)
368 int i;
370 fprintf (dump_file, "accu:%" PRId64, hist->hvalue.counters[0]);
371 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
373 fprintf (dump_file, " target:%" PRId64 " value:%" PRId64,
374 (int64_t) hist->hvalue.counters[i],
375 (int64_t) hist->hvalue.counters[i+1]);
378 fprintf (dump_file, ".\n");
379 break;
380 case HIST_TYPE_MAX:
381 gcc_unreachable ();
385 /* Dump information about HIST to DUMP_FILE. */
387 void
388 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
390 struct bitpack_d bp;
391 unsigned int i;
393 bp = bitpack_create (ob->main_stream);
394 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
395 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
396 streamer_write_bitpack (&bp);
397 switch (hist->type)
399 case HIST_TYPE_INTERVAL:
400 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
401 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
402 break;
403 default:
404 break;
406 for (i = 0; i < hist->n_counters; i++)
407 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
408 if (hist->hvalue.next)
409 stream_out_histogram_value (ob, hist->hvalue.next);
411 /* Dump information about HIST to DUMP_FILE. */
413 void
414 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
416 enum hist_type type;
417 unsigned int ncounters = 0;
418 struct bitpack_d bp;
419 unsigned int i;
420 histogram_value new_val;
421 bool next;
422 histogram_value *next_p = NULL;
426 bp = streamer_read_bitpack (ib);
427 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
428 next = bp_unpack_value (&bp, 1);
429 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
430 switch (type)
432 case HIST_TYPE_INTERVAL:
433 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
434 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
435 ncounters = new_val->hdata.intvl.steps + 2;
436 break;
438 case HIST_TYPE_POW2:
439 case HIST_TYPE_AVERAGE:
440 ncounters = 2;
441 break;
443 case HIST_TYPE_SINGLE_VALUE:
444 case HIST_TYPE_INDIR_CALL:
445 ncounters = 3;
446 break;
448 case HIST_TYPE_CONST_DELTA:
449 ncounters = 4;
450 break;
452 case HIST_TYPE_IOR:
453 case HIST_TYPE_TIME_PROFILE:
454 ncounters = 1;
455 break;
457 case HIST_TYPE_INDIR_CALL_TOPN:
458 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
459 break;
461 case HIST_TYPE_MAX:
462 gcc_unreachable ();
464 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
465 new_val->n_counters = ncounters;
466 for (i = 0; i < ncounters; i++)
467 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
468 if (!next_p)
469 gimple_add_histogram_value (cfun, stmt, new_val);
470 else
471 *next_p = new_val;
472 next_p = &new_val->hvalue.next;
474 while (next);
477 /* Dump all histograms attached to STMT to DUMP_FILE. */
479 void
480 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
482 histogram_value hist;
483 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
484 dump_histogram_value (dump_file, hist);
487 /* Remove all histograms associated with STMT. */
489 void
490 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
492 histogram_value val;
493 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
494 gimple_remove_histogram_value (fun, stmt, val);
497 /* Duplicate all histograms associates with OSTMT to STMT. */
499 void
500 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
501 struct function *ofun, gimple ostmt)
503 histogram_value val;
504 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
506 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
507 memcpy (new_val, val, sizeof (*val));
508 new_val->hvalue.stmt = stmt;
509 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
510 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
511 gimple_add_histogram_value (fun, stmt, new_val);
516 /* Move all histograms associated with OSTMT to STMT. */
518 void
519 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
521 histogram_value val = gimple_histogram_value (fun, ostmt);
522 if (val)
524 /* The following three statements can't be reordered,
525 because histogram hashtab relies on stmt field value
526 for finding the exact slot. */
527 set_histogram_value (fun, ostmt, NULL);
528 for (; val != NULL; val = val->hvalue.next)
529 val->hvalue.stmt = stmt;
530 set_histogram_value (fun, stmt, val);
534 static bool error_found = false;
536 /* Helper function for verify_histograms. For each histogram reachable via htab
537 walk verify that it was reached via statement walk. */
539 static int
540 visit_hist (void **slot, void *data)
542 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
543 histogram_value hist = *(histogram_value *) slot;
545 if (!visited->contains (hist)
546 && hist->type != HIST_TYPE_TIME_PROFILE)
548 error ("dead histogram");
549 dump_histogram_value (stderr, hist);
550 debug_gimple_stmt (hist->hvalue.stmt);
551 error_found = true;
553 return 1;
557 /* Verify sanity of the histograms. */
559 DEBUG_FUNCTION void
560 verify_histograms (void)
562 basic_block bb;
563 gimple_stmt_iterator gsi;
564 histogram_value hist;
566 error_found = false;
567 hash_set<histogram_value> visited_hists;
568 FOR_EACH_BB_FN (bb, cfun)
569 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
571 gimple stmt = gsi_stmt (gsi);
573 for (hist = gimple_histogram_value (cfun, stmt); hist;
574 hist = hist->hvalue.next)
576 if (hist->hvalue.stmt != stmt)
578 error ("Histogram value statement does not correspond to "
579 "the statement it is associated with");
580 debug_gimple_stmt (stmt);
581 dump_histogram_value (stderr, hist);
582 error_found = true;
584 visited_hists.add (hist);
587 if (VALUE_HISTOGRAMS (cfun))
588 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
589 if (error_found)
590 internal_error ("verify_histograms failed");
593 /* Helper function for verify_histograms. For each histogram reachable via htab
594 walk verify that it was reached via statement walk. */
596 static int
597 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
599 histogram_value hist = *(histogram_value *) slot;
600 free (hist->hvalue.counters);
601 #ifdef ENABLE_CHECKING
602 memset (hist, 0xab, sizeof (*hist));
603 #endif
604 free (hist);
605 return 1;
608 void
609 free_histograms (void)
611 if (VALUE_HISTOGRAMS (cfun))
613 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
614 htab_delete (VALUE_HISTOGRAMS (cfun));
615 VALUE_HISTOGRAMS (cfun) = NULL;
620 /* The overall number of invocations of the counter should match
621 execution count of basic block. Report it as error rather than
622 internal error as it might mean that user has misused the profile
623 somehow. */
625 static bool
626 check_counter (gimple stmt, const char * name,
627 gcov_type *count, gcov_type *all, gcov_type bb_count)
629 if (*all != bb_count || *count > *all)
631 location_t locus;
632 locus = (stmt != NULL)
633 ? gimple_location (stmt)
634 : DECL_SOURCE_LOCATION (current_function_decl);
635 if (flag_profile_correction)
637 if (dump_enabled_p ())
638 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
639 "correcting inconsistent value profile: %s "
640 "profiler overall count (%d) does not match BB "
641 "count (%d)\n", name, (int)*all, (int)bb_count);
642 *all = bb_count;
643 if (*count > *all)
644 *count = *all;
645 return false;
647 else
649 error_at (locus, "corrupted value profile: %s "
650 "profile counter (%d out of %d) inconsistent with "
651 "basic-block count (%d)",
652 name,
653 (int) *count,
654 (int) *all,
655 (int) bb_count);
656 return true;
660 return false;
664 /* GIMPLE based transformations. */
666 bool
667 gimple_value_profile_transformations (void)
669 basic_block bb;
670 gimple_stmt_iterator gsi;
671 bool changed = false;
673 FOR_EACH_BB_FN (bb, cfun)
675 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
677 gimple stmt = gsi_stmt (gsi);
678 histogram_value th = gimple_histogram_value (cfun, stmt);
679 if (!th)
680 continue;
682 if (dump_file)
684 fprintf (dump_file, "Trying transformations on stmt ");
685 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
686 dump_histograms_for_stmt (cfun, dump_file, stmt);
689 /* Transformations: */
690 /* The order of things in this conditional controls which
691 transformation is used when more than one is applicable. */
692 /* It is expected that any code added by the transformations
693 will be added before the current statement, and that the
694 current statement remain valid (although possibly
695 modified) upon return. */
696 if (gimple_mod_subtract_transform (&gsi)
697 || gimple_divmod_fixed_value_transform (&gsi)
698 || gimple_mod_pow2_value_transform (&gsi)
699 || gimple_stringops_transform (&gsi)
700 || gimple_ic_transform (&gsi))
702 stmt = gsi_stmt (gsi);
703 changed = true;
704 /* Original statement may no longer be in the same block. */
705 if (bb != gimple_bb (stmt))
707 bb = gimple_bb (stmt);
708 gsi = gsi_for_stmt (stmt);
714 if (changed)
716 counts_to_freqs ();
719 return changed;
723 /* Generate code for transformation 1 (with parent gimple assignment
724 STMT and probability of taking the optimal path PROB, which is
725 equivalent to COUNT/ALL within roundoff error). This generates the
726 result into a temp and returns the temp; it does not replace or
727 alter the original STMT. */
729 static tree
730 gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
731 gcov_type count, gcov_type all)
733 gassign *stmt1, *stmt2;
734 gcond *stmt3;
735 tree tmp0, tmp1, tmp2;
736 gimple bb1end, bb2end, bb3end;
737 basic_block bb, bb2, bb3, bb4;
738 tree optype, op1, op2;
739 edge e12, e13, e23, e24, e34;
740 gimple_stmt_iterator gsi;
742 gcc_assert (is_gimple_assign (stmt)
743 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
744 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
746 optype = TREE_TYPE (gimple_assign_lhs (stmt));
747 op1 = gimple_assign_rhs1 (stmt);
748 op2 = gimple_assign_rhs2 (stmt);
750 bb = gimple_bb (stmt);
751 gsi = gsi_for_stmt (stmt);
753 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
754 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
755 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
756 stmt2 = gimple_build_assign (tmp1, op2);
757 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
758 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
759 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
760 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
761 bb1end = stmt3;
763 tmp2 = create_tmp_reg (optype, "PROF");
764 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
765 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
766 bb2end = stmt1;
768 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
769 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
770 bb3end = stmt1;
772 /* Fix CFG. */
773 /* Edge e23 connects bb2 to bb3, etc. */
774 e12 = split_block (bb, bb1end);
775 bb2 = e12->dest;
776 bb2->count = count;
777 e23 = split_block (bb2, bb2end);
778 bb3 = e23->dest;
779 bb3->count = all - count;
780 e34 = split_block (bb3, bb3end);
781 bb4 = e34->dest;
782 bb4->count = all;
784 e12->flags &= ~EDGE_FALLTHRU;
785 e12->flags |= EDGE_FALSE_VALUE;
786 e12->probability = prob;
787 e12->count = count;
789 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
790 e13->probability = REG_BR_PROB_BASE - prob;
791 e13->count = all - count;
793 remove_edge (e23);
795 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
796 e24->probability = REG_BR_PROB_BASE;
797 e24->count = count;
799 e34->probability = REG_BR_PROB_BASE;
800 e34->count = all - count;
802 return tmp2;
806 /* Do transform 1) on INSN if applicable. */
808 static bool
809 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
811 histogram_value histogram;
812 enum tree_code code;
813 gcov_type val, count, all;
814 tree result, value, tree_val;
815 gcov_type prob;
816 gassign *stmt;
818 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
819 if (!stmt)
820 return false;
822 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
823 return false;
825 code = gimple_assign_rhs_code (stmt);
827 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
828 return false;
830 histogram = gimple_histogram_value_of_type (cfun, stmt,
831 HIST_TYPE_SINGLE_VALUE);
832 if (!histogram)
833 return false;
835 value = histogram->hvalue.value;
836 val = histogram->hvalue.counters[0];
837 count = histogram->hvalue.counters[1];
838 all = histogram->hvalue.counters[2];
839 gimple_remove_histogram_value (cfun, stmt, histogram);
841 /* We require that count is at least half of all; this means
842 that for the transformation to fire the value must be constant
843 at least 50% of time (and 75% gives the guarantee of usage). */
844 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
845 || 2 * count < all
846 || optimize_bb_for_size_p (gimple_bb (stmt)))
847 return false;
849 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
850 return false;
852 /* Compute probability of taking the optimal path. */
853 if (all > 0)
854 prob = GCOV_COMPUTE_SCALE (count, all);
855 else
856 prob = 0;
858 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
859 tree_val = build_int_cst (get_gcov_type (), val);
860 else
862 HOST_WIDE_INT a[2];
863 a[0] = (unsigned HOST_WIDE_INT) val;
864 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
866 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
867 TYPE_PRECISION (get_gcov_type ()), false));
869 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
871 if (dump_file)
873 fprintf (dump_file, "Div/mod by constant ");
874 print_generic_expr (dump_file, value, TDF_SLIM);
875 fprintf (dump_file, "=");
876 print_generic_expr (dump_file, tree_val, TDF_SLIM);
877 fprintf (dump_file, " transformation on insn ");
878 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
881 gimple_assign_set_rhs_from_tree (si, result);
882 update_stmt (gsi_stmt (*si));
884 return true;
887 /* Generate code for transformation 2 (with parent gimple assign STMT and
888 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
889 within roundoff error). This generates the result into a temp and returns
890 the temp; it does not replace or alter the original STMT. */
891 static tree
892 gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
894 gassign *stmt1, *stmt2, *stmt3;
895 gcond *stmt4;
896 tree tmp2, tmp3;
897 gimple bb1end, bb2end, bb3end;
898 basic_block bb, bb2, bb3, bb4;
899 tree optype, op1, op2;
900 edge e12, e13, e23, e24, e34;
901 gimple_stmt_iterator gsi;
902 tree result;
904 gcc_assert (is_gimple_assign (stmt)
905 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
907 optype = TREE_TYPE (gimple_assign_lhs (stmt));
908 op1 = gimple_assign_rhs1 (stmt);
909 op2 = gimple_assign_rhs2 (stmt);
911 bb = gimple_bb (stmt);
912 gsi = gsi_for_stmt (stmt);
914 result = create_tmp_reg (optype, "PROF");
915 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
916 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
917 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
918 build_int_cst (optype, -1));
919 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
920 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
921 NULL_TREE, NULL_TREE);
922 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
923 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
924 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
925 bb1end = stmt4;
927 /* tmp2 == op2-1 inherited from previous block. */
928 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
929 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
930 bb2end = stmt1;
932 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
933 op1, op2);
934 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
935 bb3end = stmt1;
937 /* Fix CFG. */
938 /* Edge e23 connects bb2 to bb3, etc. */
939 e12 = split_block (bb, bb1end);
940 bb2 = e12->dest;
941 bb2->count = count;
942 e23 = split_block (bb2, bb2end);
943 bb3 = e23->dest;
944 bb3->count = all - count;
945 e34 = split_block (bb3, bb3end);
946 bb4 = e34->dest;
947 bb4->count = all;
949 e12->flags &= ~EDGE_FALLTHRU;
950 e12->flags |= EDGE_FALSE_VALUE;
951 e12->probability = prob;
952 e12->count = count;
954 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
955 e13->probability = REG_BR_PROB_BASE - prob;
956 e13->count = all - count;
958 remove_edge (e23);
960 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
961 e24->probability = REG_BR_PROB_BASE;
962 e24->count = count;
964 e34->probability = REG_BR_PROB_BASE;
965 e34->count = all - count;
967 return result;
970 /* Do transform 2) on INSN if applicable. */
971 static bool
972 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
974 histogram_value histogram;
975 enum tree_code code;
976 gcov_type count, wrong_values, all;
977 tree lhs_type, result, value;
978 gcov_type prob;
979 gassign *stmt;
981 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
982 if (!stmt)
983 return false;
985 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
986 if (!INTEGRAL_TYPE_P (lhs_type))
987 return false;
989 code = gimple_assign_rhs_code (stmt);
991 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
992 return false;
994 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
995 if (!histogram)
996 return false;
998 value = histogram->hvalue.value;
999 wrong_values = histogram->hvalue.counters[0];
1000 count = histogram->hvalue.counters[1];
1002 gimple_remove_histogram_value (cfun, stmt, histogram);
1004 /* We require that we hit a power of 2 at least half of all evaluations. */
1005 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
1006 || count < wrong_values
1007 || optimize_bb_for_size_p (gimple_bb (stmt)))
1008 return false;
1010 if (dump_file)
1012 fprintf (dump_file, "Mod power of 2 transformation on insn ");
1013 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1016 /* Compute probability of taking the optimal path. */
1017 all = count + wrong_values;
1019 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
1020 return false;
1022 if (all > 0)
1023 prob = GCOV_COMPUTE_SCALE (count, all);
1024 else
1025 prob = 0;
1027 result = gimple_mod_pow2 (stmt, prob, count, all);
1029 gimple_assign_set_rhs_from_tree (si, result);
1030 update_stmt (gsi_stmt (*si));
1032 return true;
1035 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1036 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1037 supported and this is built into this interface. The probabilities of taking
1038 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1039 COUNT2/ALL respectively within roundoff error). This generates the
1040 result into a temp and returns the temp; it does not replace or alter
1041 the original STMT. */
1042 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1044 static tree
1045 gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
1046 gcov_type count1, gcov_type count2, gcov_type all)
1048 gassign *stmt1;
1049 gimple stmt2;
1050 gcond *stmt3;
1051 tree tmp1;
1052 gimple bb1end, bb2end = NULL, bb3end;
1053 basic_block bb, bb2, bb3, bb4;
1054 tree optype, op1, op2;
1055 edge e12, e23 = 0, e24, e34, e14;
1056 gimple_stmt_iterator gsi;
1057 tree result;
1059 gcc_assert (is_gimple_assign (stmt)
1060 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1062 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1063 op1 = gimple_assign_rhs1 (stmt);
1064 op2 = gimple_assign_rhs2 (stmt);
1066 bb = gimple_bb (stmt);
1067 gsi = gsi_for_stmt (stmt);
1069 result = create_tmp_reg (optype, "PROF");
1070 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1071 stmt1 = gimple_build_assign (result, op1);
1072 stmt2 = gimple_build_assign (tmp1, op2);
1073 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1074 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1075 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1076 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1077 bb1end = stmt3;
1079 if (ncounts) /* Assumed to be 0 or 1 */
1081 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1082 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1083 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1084 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1085 bb2end = stmt2;
1088 /* Fallback case. */
1089 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1090 result, tmp1);
1091 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1092 bb3end = stmt1;
1094 /* Fix CFG. */
1095 /* Edge e23 connects bb2 to bb3, etc. */
1096 /* However block 3 is optional; if it is not there, references
1097 to 3 really refer to block 2. */
1098 e12 = split_block (bb, bb1end);
1099 bb2 = e12->dest;
1100 bb2->count = all - count1;
1102 if (ncounts) /* Assumed to be 0 or 1. */
1104 e23 = split_block (bb2, bb2end);
1105 bb3 = e23->dest;
1106 bb3->count = all - count1 - count2;
1109 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1110 bb4 = e34->dest;
1111 bb4->count = all;
1113 e12->flags &= ~EDGE_FALLTHRU;
1114 e12->flags |= EDGE_FALSE_VALUE;
1115 e12->probability = REG_BR_PROB_BASE - prob1;
1116 e12->count = all - count1;
1118 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1119 e14->probability = prob1;
1120 e14->count = count1;
1122 if (ncounts) /* Assumed to be 0 or 1. */
1124 e23->flags &= ~EDGE_FALLTHRU;
1125 e23->flags |= EDGE_FALSE_VALUE;
1126 e23->count = all - count1 - count2;
1127 e23->probability = REG_BR_PROB_BASE - prob2;
1129 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1130 e24->probability = prob2;
1131 e24->count = count2;
1134 e34->probability = REG_BR_PROB_BASE;
1135 e34->count = all - count1 - count2;
1137 return result;
1141 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1143 static bool
1144 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1146 histogram_value histogram;
1147 enum tree_code code;
1148 gcov_type count, wrong_values, all;
1149 tree lhs_type, result;
1150 gcov_type prob1, prob2;
1151 unsigned int i, steps;
1152 gcov_type count1, count2;
1153 gassign *stmt;
1155 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1156 if (!stmt)
1157 return false;
1159 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1160 if (!INTEGRAL_TYPE_P (lhs_type))
1161 return false;
1163 code = gimple_assign_rhs_code (stmt);
1165 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1166 return false;
1168 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1169 if (!histogram)
1170 return false;
1172 all = 0;
1173 wrong_values = 0;
1174 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1175 all += histogram->hvalue.counters[i];
1177 wrong_values += histogram->hvalue.counters[i];
1178 wrong_values += histogram->hvalue.counters[i+1];
1179 steps = histogram->hdata.intvl.steps;
1180 all += wrong_values;
1181 count1 = histogram->hvalue.counters[0];
1182 count2 = histogram->hvalue.counters[1];
1184 /* Compute probability of taking the optimal path. */
1185 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1187 gimple_remove_histogram_value (cfun, stmt, histogram);
1188 return false;
1191 if (flag_profile_correction && count1 + count2 > all)
1192 all = count1 + count2;
1194 gcc_assert (count1 + count2 <= all);
1196 /* We require that we use just subtractions in at least 50% of all
1197 evaluations. */
1198 count = 0;
1199 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1201 count += histogram->hvalue.counters[i];
1202 if (count * 2 >= all)
1203 break;
1205 if (i == steps
1206 || optimize_bb_for_size_p (gimple_bb (stmt)))
1207 return false;
1209 gimple_remove_histogram_value (cfun, stmt, histogram);
1210 if (dump_file)
1212 fprintf (dump_file, "Mod subtract transformation on insn ");
1213 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1216 /* Compute probability of taking the optimal path(s). */
1217 if (all > 0)
1219 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1220 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1222 else
1224 prob1 = prob2 = 0;
1227 /* In practice, "steps" is always 2. This interface reflects this,
1228 and will need to be changed if "steps" can change. */
1229 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1231 gimple_assign_set_rhs_from_tree (si, result);
1232 update_stmt (gsi_stmt (*si));
1234 return true;
1237 typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
1239 static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
1241 /* Returns true if node graph is initialized. This
1242 is used to test if profile_id has been created
1243 for cgraph_nodes. */
1245 bool
1246 coverage_node_map_initialized_p (void)
1248 return cgraph_node_map != 0;
1251 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1252 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1253 that the PROFILE_IDs was already assigned. */
1255 void
1256 init_node_map (bool local)
1258 struct cgraph_node *n;
1259 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
1261 FOR_EACH_DEFINED_FUNCTION (n)
1262 if (n->has_gimple_body_p ())
1264 cgraph_node **val;
1265 if (local)
1267 n->profile_id = coverage_compute_profile_id (n);
1268 while ((val = cgraph_node_map->get (n->profile_id))
1269 || !n->profile_id)
1271 if (dump_file)
1272 fprintf (dump_file, "Local profile-id %i conflict"
1273 " with nodes %s/%i %s/%i\n",
1274 n->profile_id,
1275 n->name (),
1276 n->order,
1277 (*val)->name (),
1278 (*val)->order);
1279 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1282 else if (!n->profile_id)
1284 if (dump_file)
1285 fprintf (dump_file,
1286 "Node %s/%i has no profile-id"
1287 " (profile feedback missing?)\n",
1288 n->name (),
1289 n->order);
1290 continue;
1292 else if ((val = cgraph_node_map->get (n->profile_id)))
1294 if (dump_file)
1295 fprintf (dump_file,
1296 "Node %s/%i has IP profile-id %i conflict. "
1297 "Giving up.\n",
1298 n->name (),
1299 n->order,
1300 n->profile_id);
1301 *val = NULL;
1302 continue;
1304 cgraph_node_map->put (n->profile_id, n);
1308 /* Delete the CGRAPH_NODE_MAP. */
1310 void
1311 del_node_map (void)
1313 delete cgraph_node_map;
1316 /* Return cgraph node for function with pid */
1318 struct cgraph_node*
1319 find_func_by_profile_id (int profile_id)
1321 cgraph_node **val = cgraph_node_map->get (profile_id);
1322 if (val)
1323 return *val;
1324 else
1325 return NULL;
1328 /* Perform sanity check on the indirect call target. Due to race conditions,
1329 false function target may be attributed to an indirect call site. If the
1330 call expression type mismatches with the target function's type, expand_call
1331 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1332 Returns true if TARGET is considered ok for call CALL_STMT. */
1334 bool
1335 check_ic_target (gcall *call_stmt, struct cgraph_node *target)
1337 location_t locus;
1338 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1339 return true;
1341 locus = gimple_location (call_stmt);
1342 if (dump_enabled_p ())
1343 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1344 "Skipping target %s with mismatching types for icall\n",
1345 target->name ());
1346 return false;
1349 /* Do transformation
1351 if (actual_callee_address == address_of_most_common_function/method)
1352 do direct call
1353 else
1354 old call
1357 gcall *
1358 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1359 int prob, gcov_type count, gcov_type all)
1361 gcall *dcall_stmt;
1362 gassign *load_stmt;
1363 gcond *cond_stmt;
1364 gcall *iretbnd_stmt = NULL;
1365 tree tmp0, tmp1, tmp;
1366 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1367 tree optype = build_pointer_type (void_type_node);
1368 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1369 gimple_stmt_iterator gsi;
1370 int lp_nr, dflags;
1371 edge e_eh, e;
1372 edge_iterator ei;
1373 gimple_stmt_iterator psi;
1375 cond_bb = gimple_bb (icall_stmt);
1376 gsi = gsi_for_stmt (icall_stmt);
1378 if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
1379 iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
1381 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1382 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1383 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1384 load_stmt = gimple_build_assign (tmp0, tmp);
1385 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1387 tmp = fold_convert (optype, build_addr (direct_call->decl,
1388 current_function_decl));
1389 load_stmt = gimple_build_assign (tmp1, tmp);
1390 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1392 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1393 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1395 gimple_set_vdef (icall_stmt, NULL_TREE);
1396 gimple_set_vuse (icall_stmt, NULL_TREE);
1397 update_stmt (icall_stmt);
1398 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1399 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1400 dflags = flags_from_decl_or_type (direct_call->decl);
1401 if ((dflags & ECF_NORETURN) != 0)
1402 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1403 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1405 /* Fix CFG. */
1406 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1407 e_cd = split_block (cond_bb, cond_stmt);
1408 dcall_bb = e_cd->dest;
1409 dcall_bb->count = count;
1411 e_di = split_block (dcall_bb, dcall_stmt);
1412 icall_bb = e_di->dest;
1413 icall_bb->count = all - count;
1415 /* Do not disturb existing EH edges from the indirect call. */
1416 if (!stmt_ends_bb_p (icall_stmt))
1417 e_ij = split_block (icall_bb, icall_stmt);
1418 else
1420 e_ij = find_fallthru_edge (icall_bb->succs);
1421 /* The indirect call might be noreturn. */
1422 if (e_ij != NULL)
1424 e_ij->probability = REG_BR_PROB_BASE;
1425 e_ij->count = all - count;
1426 e_ij = single_pred_edge (split_edge (e_ij));
1429 if (e_ij != NULL)
1431 join_bb = e_ij->dest;
1432 join_bb->count = all;
1435 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1436 e_cd->probability = prob;
1437 e_cd->count = count;
1439 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1440 e_ci->probability = REG_BR_PROB_BASE - prob;
1441 e_ci->count = all - count;
1443 remove_edge (e_di);
1445 if (e_ij != NULL)
1447 if ((dflags & ECF_NORETURN) != 0)
1448 e_ij->count = all;
1449 else
1451 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1452 e_dj->probability = REG_BR_PROB_BASE;
1453 e_dj->count = count;
1455 e_ij->count = all - count;
1457 e_ij->probability = REG_BR_PROB_BASE;
1460 /* Insert PHI node for the call result if necessary. */
1461 if (gimple_call_lhs (icall_stmt)
1462 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1463 && (dflags & ECF_NORETURN) == 0)
1465 tree result = gimple_call_lhs (icall_stmt);
1466 gphi *phi = create_phi_node (result, join_bb);
1467 gimple_call_set_lhs (icall_stmt,
1468 duplicate_ssa_name (result, icall_stmt));
1469 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1470 gimple_call_set_lhs (dcall_stmt,
1471 duplicate_ssa_name (result, dcall_stmt));
1472 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1474 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1475 call then we need to make it's copy for the direct
1476 call. */
1477 if (iretbnd_stmt)
1479 if (gimple_call_lhs (iretbnd_stmt))
1481 gimple copy;
1483 gimple_set_vdef (iretbnd_stmt, NULL_TREE);
1484 gimple_set_vuse (iretbnd_stmt, NULL_TREE);
1485 update_stmt (iretbnd_stmt);
1487 result = gimple_call_lhs (iretbnd_stmt);
1488 phi = create_phi_node (result, join_bb);
1490 copy = gimple_copy (iretbnd_stmt);
1491 gimple_call_set_arg (copy, 0,
1492 gimple_call_lhs (dcall_stmt));
1493 gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
1494 gsi_insert_on_edge (e_dj, copy);
1495 add_phi_arg (phi, gimple_call_lhs (copy),
1496 e_dj, UNKNOWN_LOCATION);
1498 gimple_call_set_arg (iretbnd_stmt, 0,
1499 gimple_call_lhs (icall_stmt));
1500 gimple_call_set_lhs (iretbnd_stmt,
1501 duplicate_ssa_name (result, iretbnd_stmt));
1502 psi = gsi_for_stmt (iretbnd_stmt);
1503 gsi_remove (&psi, false);
1504 gsi_insert_on_edge (e_ij, iretbnd_stmt);
1505 add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
1506 e_ij, UNKNOWN_LOCATION);
1508 gsi_commit_one_edge_insert (e_dj, NULL);
1509 gsi_commit_one_edge_insert (e_ij, NULL);
1511 else
1513 psi = gsi_for_stmt (iretbnd_stmt);
1514 gsi_remove (&psi, true);
1519 /* Build an EH edge for the direct call if necessary. */
1520 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1521 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1523 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1526 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1527 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1529 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1530 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1531 !gsi_end_p (psi); gsi_next (&psi))
1533 gphi *phi = psi.phi ();
1534 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1535 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1538 if (!stmt_could_throw_p (dcall_stmt))
1539 gimple_purge_dead_eh_edges (dcall_bb);
1540 return dcall_stmt;
1544 For every checked indirect/virtual call determine if most common pid of
1545 function/class method has probability more than 50%. If yes modify code of
1546 this call to:
1549 static bool
1550 gimple_ic_transform (gimple_stmt_iterator *gsi)
1552 gcall *stmt;
1553 histogram_value histogram;
1554 gcov_type val, count, all, bb_all;
1555 struct cgraph_node *direct_call;
1557 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1558 if (!stmt)
1559 return false;
1561 if (gimple_call_fndecl (stmt) != NULL_TREE)
1562 return false;
1564 if (gimple_call_internal_p (stmt))
1565 return false;
1567 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1568 if (!histogram)
1569 return false;
1571 val = histogram->hvalue.counters [0];
1572 count = histogram->hvalue.counters [1];
1573 all = histogram->hvalue.counters [2];
1575 bb_all = gimple_bb (stmt)->count;
1576 /* The order of CHECK_COUNTER calls is important -
1577 since check_counter can correct the third parameter
1578 and we want to make count <= all <= bb_all. */
1579 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1580 || check_counter (stmt, "ic", &count, &all, all))
1582 gimple_remove_histogram_value (cfun, stmt, histogram);
1583 return false;
1586 if (4 * count <= 3 * all)
1587 return false;
1589 direct_call = find_func_by_profile_id ((int)val);
1591 if (direct_call == NULL)
1593 if (val)
1595 if (dump_file)
1597 fprintf (dump_file, "Indirect call -> direct call from other module");
1598 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1599 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1602 return false;
1605 if (!check_ic_target (stmt, direct_call))
1607 if (dump_file)
1609 fprintf (dump_file, "Indirect call -> direct call ");
1610 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1611 fprintf (dump_file, "=> ");
1612 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1613 fprintf (dump_file, " transformation skipped because of type mismatch");
1614 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1616 gimple_remove_histogram_value (cfun, stmt, histogram);
1617 return false;
1620 if (dump_file)
1622 fprintf (dump_file, "Indirect call -> direct call ");
1623 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1624 fprintf (dump_file, "=> ");
1625 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1626 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1627 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1628 fprintf (dump_file, "hist->count %" PRId64
1629 " hist->all %" PRId64"\n", count, all);
1632 return true;
1635 /* Return true if the stringop CALL with FNDECL shall be profiled.
1636 SIZE_ARG be set to the argument index for the size of the string
1637 operation.
1639 static bool
1640 interesting_stringop_to_profile_p (tree fndecl, gcall *call, int *size_arg)
1642 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1644 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1645 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1646 return false;
1648 switch (fcode)
1650 case BUILT_IN_MEMCPY:
1651 case BUILT_IN_MEMPCPY:
1652 *size_arg = 2;
1653 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1654 INTEGER_TYPE, VOID_TYPE);
1655 case BUILT_IN_MEMSET:
1656 *size_arg = 2;
1657 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1658 INTEGER_TYPE, VOID_TYPE);
1659 case BUILT_IN_BZERO:
1660 *size_arg = 1;
1661 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1662 VOID_TYPE);
1663 default:
1664 gcc_unreachable ();
1668 /* Convert stringop (..., vcall_size)
1669 into
1670 if (vcall_size == icall_size)
1671 stringop (..., icall_size);
1672 else
1673 stringop (..., vcall_size);
1674 assuming we'll propagate a true constant into ICALL_SIZE later. */
1676 static void
1677 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
1678 gcov_type count, gcov_type all)
1680 gassign *tmp_stmt;
1681 gcond *cond_stmt;
1682 gcall *icall_stmt;
1683 tree tmp0, tmp1, vcall_size, optype;
1684 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1685 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1686 gimple_stmt_iterator gsi;
1687 tree fndecl;
1688 int size_arg;
1690 fndecl = gimple_call_fndecl (vcall_stmt);
1691 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1692 gcc_unreachable ();
1694 cond_bb = gimple_bb (vcall_stmt);
1695 gsi = gsi_for_stmt (vcall_stmt);
1697 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1698 optype = TREE_TYPE (vcall_size);
1700 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1701 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1702 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1703 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1705 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1706 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1708 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1709 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1711 gimple_set_vdef (vcall_stmt, NULL);
1712 gimple_set_vuse (vcall_stmt, NULL);
1713 update_stmt (vcall_stmt);
1714 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1715 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1716 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1718 /* Fix CFG. */
1719 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1720 e_ci = split_block (cond_bb, cond_stmt);
1721 icall_bb = e_ci->dest;
1722 icall_bb->count = count;
1724 e_iv = split_block (icall_bb, icall_stmt);
1725 vcall_bb = e_iv->dest;
1726 vcall_bb->count = all - count;
1728 e_vj = split_block (vcall_bb, vcall_stmt);
1729 join_bb = e_vj->dest;
1730 join_bb->count = all;
1732 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1733 e_ci->probability = prob;
1734 e_ci->count = count;
1736 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1737 e_cv->probability = REG_BR_PROB_BASE - prob;
1738 e_cv->count = all - count;
1740 remove_edge (e_iv);
1742 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1743 e_ij->probability = REG_BR_PROB_BASE;
1744 e_ij->count = count;
1746 e_vj->probability = REG_BR_PROB_BASE;
1747 e_vj->count = all - count;
1749 /* Insert PHI node for the call result if necessary. */
1750 if (gimple_call_lhs (vcall_stmt)
1751 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1753 tree result = gimple_call_lhs (vcall_stmt);
1754 gphi *phi = create_phi_node (result, join_bb);
1755 gimple_call_set_lhs (vcall_stmt,
1756 duplicate_ssa_name (result, vcall_stmt));
1757 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1758 gimple_call_set_lhs (icall_stmt,
1759 duplicate_ssa_name (result, icall_stmt));
1760 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1763 /* Because these are all string op builtins, they're all nothrow. */
1764 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1765 gcc_assert (!stmt_could_throw_p (icall_stmt));
1768 /* Find values inside STMT for that we want to measure histograms for
1769 division/modulo optimization. */
1770 static bool
1771 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1773 gcall *stmt;
1774 tree fndecl;
1775 tree blck_size;
1776 enum built_in_function fcode;
1777 histogram_value histogram;
1778 gcov_type count, all, val;
1779 tree dest, src;
1780 unsigned int dest_align, src_align;
1781 gcov_type prob;
1782 tree tree_val;
1783 int size_arg;
1785 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1786 if (!stmt)
1787 return false;
1788 fndecl = gimple_call_fndecl (stmt);
1789 if (!fndecl)
1790 return false;
1791 fcode = DECL_FUNCTION_CODE (fndecl);
1792 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1793 return false;
1795 blck_size = gimple_call_arg (stmt, size_arg);
1796 if (TREE_CODE (blck_size) == INTEGER_CST)
1797 return false;
1799 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1800 if (!histogram)
1801 return false;
1802 val = histogram->hvalue.counters[0];
1803 count = histogram->hvalue.counters[1];
1804 all = histogram->hvalue.counters[2];
1805 gimple_remove_histogram_value (cfun, stmt, histogram);
1806 /* We require that count is at least half of all; this means
1807 that for the transformation to fire the value must be constant
1808 at least 80% of time. */
1809 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1810 return false;
1811 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1812 return false;
1813 if (all > 0)
1814 prob = GCOV_COMPUTE_SCALE (count, all);
1815 else
1816 prob = 0;
1817 dest = gimple_call_arg (stmt, 0);
1818 dest_align = get_pointer_alignment (dest);
1819 switch (fcode)
1821 case BUILT_IN_MEMCPY:
1822 case BUILT_IN_MEMPCPY:
1823 src = gimple_call_arg (stmt, 1);
1824 src_align = get_pointer_alignment (src);
1825 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1826 return false;
1827 break;
1828 case BUILT_IN_MEMSET:
1829 if (!can_store_by_pieces (val, builtin_memset_read_str,
1830 gimple_call_arg (stmt, 1),
1831 dest_align, true))
1832 return false;
1833 break;
1834 case BUILT_IN_BZERO:
1835 if (!can_store_by_pieces (val, builtin_memset_read_str,
1836 integer_zero_node,
1837 dest_align, true))
1838 return false;
1839 break;
1840 default:
1841 gcc_unreachable ();
1843 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1844 tree_val = build_int_cst (get_gcov_type (), val);
1845 else
1847 HOST_WIDE_INT a[2];
1848 a[0] = (unsigned HOST_WIDE_INT) val;
1849 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1851 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1852 TYPE_PRECISION (get_gcov_type ()), false));
1855 if (dump_file)
1857 fprintf (dump_file, "Single value %i stringop transformation on ",
1858 (int)val);
1859 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1861 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1863 return true;
1866 void
1867 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1868 HOST_WIDE_INT *expected_size)
1870 histogram_value histogram;
1871 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1872 if (!histogram)
1873 *expected_size = -1;
1874 else if (!histogram->hvalue.counters[1])
1876 *expected_size = -1;
1877 gimple_remove_histogram_value (cfun, stmt, histogram);
1879 else
1881 gcov_type size;
1882 size = ((histogram->hvalue.counters[0]
1883 + histogram->hvalue.counters[1] / 2)
1884 / histogram->hvalue.counters[1]);
1885 /* Even if we can hold bigger value in SIZE, INT_MAX
1886 is safe "infinity" for code generation strategies. */
1887 if (size > INT_MAX)
1888 size = INT_MAX;
1889 *expected_size = size;
1890 gimple_remove_histogram_value (cfun, stmt, histogram);
1892 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1893 if (!histogram)
1894 *expected_align = 0;
1895 else if (!histogram->hvalue.counters[0])
1897 gimple_remove_histogram_value (cfun, stmt, histogram);
1898 *expected_align = 0;
1900 else
1902 gcov_type count;
1903 int alignment;
1905 count = histogram->hvalue.counters[0];
1906 alignment = 1;
1907 while (!(count & alignment)
1908 && (alignment * 2 * BITS_PER_UNIT))
1909 alignment <<= 1;
1910 *expected_align = alignment * BITS_PER_UNIT;
1911 gimple_remove_histogram_value (cfun, stmt, histogram);
1916 /* Find values inside STMT for that we want to measure histograms for
1917 division/modulo optimization. */
1918 static void
1919 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1921 tree lhs, divisor, op0, type;
1922 histogram_value hist;
1924 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1925 return;
1927 lhs = gimple_assign_lhs (stmt);
1928 type = TREE_TYPE (lhs);
1929 if (!INTEGRAL_TYPE_P (type))
1930 return;
1932 switch (gimple_assign_rhs_code (stmt))
1934 case TRUNC_DIV_EXPR:
1935 case TRUNC_MOD_EXPR:
1936 divisor = gimple_assign_rhs2 (stmt);
1937 op0 = gimple_assign_rhs1 (stmt);
1939 values->reserve (3);
1941 if (TREE_CODE (divisor) == SSA_NAME)
1942 /* Check for the case where the divisor is the same value most
1943 of the time. */
1944 values->quick_push (gimple_alloc_histogram_value (cfun,
1945 HIST_TYPE_SINGLE_VALUE,
1946 stmt, divisor));
1948 /* For mod, check whether it is not often a noop (or replaceable by
1949 a few subtractions). */
1950 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1951 && TYPE_UNSIGNED (type))
1953 tree val;
1954 /* Check for a special case where the divisor is power of 2. */
1955 values->quick_push (gimple_alloc_histogram_value (cfun,
1956 HIST_TYPE_POW2,
1957 stmt, divisor));
1959 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1960 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1961 stmt, val);
1962 hist->hdata.intvl.int_start = 0;
1963 hist->hdata.intvl.steps = 2;
1964 values->quick_push (hist);
1966 return;
1968 default:
1969 return;
1973 /* Find calls inside STMT for that we want to measure histograms for
1974 indirect/virtual call optimization. */
1976 static void
1977 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1979 tree callee;
1981 if (gimple_code (stmt) != GIMPLE_CALL
1982 || gimple_call_internal_p (stmt)
1983 || gimple_call_fndecl (stmt) != NULL_TREE)
1984 return;
1986 callee = gimple_call_fn (stmt);
1988 values->reserve (3);
1990 values->quick_push (gimple_alloc_histogram_value (
1991 cfun,
1992 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
1993 HIST_TYPE_INDIR_CALL_TOPN :
1994 HIST_TYPE_INDIR_CALL,
1995 stmt, callee));
1997 return;
2000 /* Find values inside STMT for that we want to measure histograms for
2001 string operations. */
2002 static void
2003 gimple_stringops_values_to_profile (gimple gs, histogram_values *values)
2005 gcall *stmt;
2006 tree fndecl;
2007 tree blck_size;
2008 tree dest;
2009 int size_arg;
2011 stmt = dyn_cast <gcall *> (gs);
2012 if (!stmt)
2013 return;
2014 fndecl = gimple_call_fndecl (stmt);
2015 if (!fndecl)
2016 return;
2018 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
2019 return;
2021 dest = gimple_call_arg (stmt, 0);
2022 blck_size = gimple_call_arg (stmt, size_arg);
2024 if (TREE_CODE (blck_size) != INTEGER_CST)
2026 values->safe_push (gimple_alloc_histogram_value (cfun,
2027 HIST_TYPE_SINGLE_VALUE,
2028 stmt, blck_size));
2029 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2030 stmt, blck_size));
2032 if (TREE_CODE (blck_size) != INTEGER_CST)
2033 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2034 stmt, dest));
2037 /* Find values inside STMT for that we want to measure histograms and adds
2038 them to list VALUES. */
2040 static void
2041 gimple_values_to_profile (gimple stmt, histogram_values *values)
2043 gimple_divmod_values_to_profile (stmt, values);
2044 gimple_stringops_values_to_profile (stmt, values);
2045 gimple_indirect_call_to_profile (stmt, values);
2048 void
2049 gimple_find_values_to_profile (histogram_values *values)
2051 basic_block bb;
2052 gimple_stmt_iterator gsi;
2053 unsigned i;
2054 histogram_value hist = NULL;
2055 values->create (0);
2057 FOR_EACH_BB_FN (bb, cfun)
2058 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2059 gimple_values_to_profile (gsi_stmt (gsi), values);
2061 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2063 FOR_EACH_VEC_ELT (*values, i, hist)
2065 switch (hist->type)
2067 case HIST_TYPE_INTERVAL:
2068 hist->n_counters = hist->hdata.intvl.steps + 2;
2069 break;
2071 case HIST_TYPE_POW2:
2072 hist->n_counters = 2;
2073 break;
2075 case HIST_TYPE_SINGLE_VALUE:
2076 hist->n_counters = 3;
2077 break;
2079 case HIST_TYPE_CONST_DELTA:
2080 hist->n_counters = 4;
2081 break;
2083 case HIST_TYPE_INDIR_CALL:
2084 hist->n_counters = 3;
2085 break;
2087 case HIST_TYPE_TIME_PROFILE:
2088 hist->n_counters = 1;
2089 break;
2091 case HIST_TYPE_AVERAGE:
2092 hist->n_counters = 2;
2093 break;
2095 case HIST_TYPE_IOR:
2096 hist->n_counters = 1;
2097 break;
2099 case HIST_TYPE_INDIR_CALL_TOPN:
2100 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2101 break;
2103 default:
2104 gcc_unreachable ();
2106 if (dump_file)
2108 fprintf (dump_file, "Stmt ");
2109 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2110 dump_histogram_value (dump_file, hist);