PR middle-end/66633
[official-gcc.git] / gcc / value-prof.c
blob0424149471bca8956bc3e282a9dac9a8f4c1b6fd
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 "tm.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "tree-nested.h"
29 #include "calls.h"
30 #include "rtl.h"
31 #include "hard-reg-set.h"
32 #include "function.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 "predict.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "basic-block.h"
46 #include "value-prof.h"
47 #include "recog.h"
48 #include "insn-codes.h"
49 #include "optabs.h"
50 #include "regs.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "tree-eh.h"
54 #include "gimple-expr.h"
55 #include "gimple.h"
56 #include "gimplify.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
59 #include "tree-cfg.h"
60 #include "tree-phinodes.h"
61 #include "ssa-iterators.h"
62 #include "stringpool.h"
63 #include "tree-ssanames.h"
64 #include "diagnostic.h"
65 #include "gimple-pretty-print.h"
66 #include "coverage.h"
67 #include "gcov-io.h"
68 #include "timevar.h"
69 #include "dumpfile.h"
70 #include "profile.h"
71 #include "cgraph.h"
72 #include "data-streamer.h"
73 #include "builtins.h"
74 #include "params.h"
75 #include "tree-chkp.h"
77 /* In this file value profile based optimizations are placed. Currently the
78 following optimizations are implemented (for more detailed descriptions
79 see comments at value_profile_transformations):
81 1) Division/modulo specialization. Provided that we can determine that the
82 operands of the division have some special properties, we may use it to
83 produce more effective code.
85 2) Indirect/virtual call specialization. If we can determine most
86 common function callee in indirect/virtual call. We can use this
87 information to improve code effectiveness (especially info for
88 the inliner).
90 3) Speculative prefetching. If we are able to determine that the difference
91 between addresses accessed by a memory reference is usually constant, we
92 may add the prefetch instructions.
93 FIXME: This transformation was removed together with RTL based value
94 profiling.
97 Value profiling internals
98 ==========================
100 Every value profiling transformation starts with defining what values
101 to profile. There are different histogram types (see HIST_TYPE_* in
102 value-prof.h) and each transformation can request one or more histogram
103 types per GIMPLE statement. The function gimple_find_values_to_profile()
104 collects the values to profile in a vec, and adds the number of counters
105 required for the different histogram types.
107 For a -fprofile-generate run, the statements for which values should be
108 recorded, are instrumented in instrument_values(). The instrumentation
109 is done by helper functions that can be found in tree-profile.c, where
110 new types of histograms can be added if necessary.
112 After a -fprofile-use, the value profiling data is read back in by
113 compute_value_histograms() that translates the collected data to
114 histograms and attaches them to the profiled statements via
115 gimple_add_histogram_value(). Histograms are stored in a hash table
116 that is attached to every intrumented function, see VALUE_HISTOGRAMS
117 in function.h.
119 The value-profile transformations driver is the function
120 gimple_value_profile_transformations(). It traverses all statements in
121 the to-be-transformed function, and looks for statements with one or
122 more histograms attached to it. If a statement has histograms, the
123 transformation functions are called on the statement.
125 Limitations / FIXME / TODO:
126 * Only one histogram of each type can be associated with a statement.
127 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
128 (This type of histogram was originally used to implement a form of
129 stride profiling based speculative prefetching to improve SPEC2000
130 scores for memory-bound benchmarks, mcf and equake. However, this
131 was an RTL value-profiling transformation, and those have all been
132 removed.)
133 * Some value profile transformations are done in builtins.c (?!)
134 * Updating of histograms needs some TLC.
135 * The value profiling code could be used to record analysis results
136 from non-profiling (e.g. VRP).
137 * Adding new profilers should be simplified, starting with a cleanup
138 of what-happens-where andwith making gimple_find_values_to_profile
139 and gimple_value_profile_transformations table-driven, perhaps...
142 static tree gimple_divmod_fixed_value (gassign *, tree, int, gcov_type,
143 gcov_type);
144 static tree gimple_mod_pow2 (gassign *, int, gcov_type, gcov_type);
145 static tree gimple_mod_subtract (gassign *, int, int, int, gcov_type,
146 gcov_type, gcov_type);
147 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
148 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
149 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
150 static bool gimple_stringops_transform (gimple_stmt_iterator *);
151 static bool gimple_ic_transform (gimple_stmt_iterator *);
153 /* Allocate histogram value. */
155 histogram_value
156 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
157 enum hist_type type, gimple stmt, tree value)
159 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
160 hist->hvalue.value = value;
161 hist->hvalue.stmt = stmt;
162 hist->type = type;
163 return hist;
166 /* Hash value for histogram. */
168 static hashval_t
169 histogram_hash (const void *x)
171 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
174 /* Return nonzero if statement for histogram_value X is Y. */
176 static int
177 histogram_eq (const void *x, const void *y)
179 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
182 /* Set histogram for STMT. */
184 static void
185 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
187 void **loc;
188 if (!hist && !VALUE_HISTOGRAMS (fun))
189 return;
190 if (!VALUE_HISTOGRAMS (fun))
191 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
192 histogram_eq, NULL);
193 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
194 htab_hash_pointer (stmt),
195 hist ? INSERT : NO_INSERT);
196 if (!hist)
198 if (loc)
199 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
200 return;
202 *loc = hist;
205 /* Get histogram list for STMT. */
207 histogram_value
208 gimple_histogram_value (struct function *fun, gimple stmt)
210 if (!VALUE_HISTOGRAMS (fun))
211 return NULL;
212 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
213 htab_hash_pointer (stmt));
216 /* Add histogram for STMT. */
218 void
219 gimple_add_histogram_value (struct function *fun, gimple stmt,
220 histogram_value hist)
222 hist->hvalue.next = gimple_histogram_value (fun, stmt);
223 set_histogram_value (fun, stmt, hist);
224 hist->fun = fun;
228 /* Remove histogram HIST from STMT's histogram list. */
230 void
231 gimple_remove_histogram_value (struct function *fun, gimple stmt,
232 histogram_value hist)
234 histogram_value hist2 = gimple_histogram_value (fun, stmt);
235 if (hist == hist2)
237 set_histogram_value (fun, stmt, hist->hvalue.next);
239 else
241 while (hist2->hvalue.next != hist)
242 hist2 = hist2->hvalue.next;
243 hist2->hvalue.next = hist->hvalue.next;
245 free (hist->hvalue.counters);
246 #ifdef ENABLE_CHECKING
247 memset (hist, 0xab, sizeof (*hist));
248 #endif
249 free (hist);
253 /* Lookup histogram of type TYPE in the STMT. */
255 histogram_value
256 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
257 enum hist_type type)
259 histogram_value hist;
260 for (hist = gimple_histogram_value (fun, stmt); hist;
261 hist = hist->hvalue.next)
262 if (hist->type == type)
263 return hist;
264 return NULL;
267 /* Dump information about HIST to DUMP_FILE. */
269 static void
270 dump_histogram_value (FILE *dump_file, histogram_value hist)
272 switch (hist->type)
274 case HIST_TYPE_INTERVAL:
275 fprintf (dump_file, "Interval counter range %d -- %d",
276 hist->hdata.intvl.int_start,
277 (hist->hdata.intvl.int_start
278 + hist->hdata.intvl.steps - 1));
279 if (hist->hvalue.counters)
281 unsigned int i;
282 fprintf (dump_file, " [");
283 for (i = 0; i < hist->hdata.intvl.steps; i++)
284 fprintf (dump_file, " %d:%" PRId64,
285 hist->hdata.intvl.int_start + i,
286 (int64_t) hist->hvalue.counters[i]);
287 fprintf (dump_file, " ] outside range:%" PRId64,
288 (int64_t) hist->hvalue.counters[i]);
290 fprintf (dump_file, ".\n");
291 break;
293 case HIST_TYPE_POW2:
294 fprintf (dump_file, "Pow2 counter ");
295 if (hist->hvalue.counters)
297 fprintf (dump_file, "pow2:%" PRId64
298 " nonpow2:%" PRId64,
299 (int64_t) hist->hvalue.counters[0],
300 (int64_t) hist->hvalue.counters[1]);
302 fprintf (dump_file, ".\n");
303 break;
305 case HIST_TYPE_SINGLE_VALUE:
306 fprintf (dump_file, "Single value ");
307 if (hist->hvalue.counters)
309 fprintf (dump_file, "value:%" PRId64
310 " match:%" PRId64
311 " wrong:%" PRId64,
312 (int64_t) hist->hvalue.counters[0],
313 (int64_t) hist->hvalue.counters[1],
314 (int64_t) hist->hvalue.counters[2]);
316 fprintf (dump_file, ".\n");
317 break;
319 case HIST_TYPE_AVERAGE:
320 fprintf (dump_file, "Average value ");
321 if (hist->hvalue.counters)
323 fprintf (dump_file, "sum:%" PRId64
324 " times:%" PRId64,
325 (int64_t) hist->hvalue.counters[0],
326 (int64_t) hist->hvalue.counters[1]);
328 fprintf (dump_file, ".\n");
329 break;
331 case HIST_TYPE_IOR:
332 fprintf (dump_file, "IOR value ");
333 if (hist->hvalue.counters)
335 fprintf (dump_file, "ior:%" PRId64,
336 (int64_t) hist->hvalue.counters[0]);
338 fprintf (dump_file, ".\n");
339 break;
341 case HIST_TYPE_CONST_DELTA:
342 fprintf (dump_file, "Constant delta ");
343 if (hist->hvalue.counters)
345 fprintf (dump_file, "value:%" PRId64
346 " match:%" PRId64
347 " wrong:%" PRId64,
348 (int64_t) hist->hvalue.counters[0],
349 (int64_t) hist->hvalue.counters[1],
350 (int64_t) hist->hvalue.counters[2]);
352 fprintf (dump_file, ".\n");
353 break;
354 case HIST_TYPE_INDIR_CALL:
355 fprintf (dump_file, "Indirect call ");
356 if (hist->hvalue.counters)
358 fprintf (dump_file, "value:%" PRId64
359 " match:%" PRId64
360 " all:%" PRId64,
361 (int64_t) hist->hvalue.counters[0],
362 (int64_t) hist->hvalue.counters[1],
363 (int64_t) hist->hvalue.counters[2]);
365 fprintf (dump_file, ".\n");
366 break;
367 case HIST_TYPE_TIME_PROFILE:
368 fprintf (dump_file, "Time profile ");
369 if (hist->hvalue.counters)
371 fprintf (dump_file, "time:%" PRId64,
372 (int64_t) hist->hvalue.counters[0]);
374 fprintf (dump_file, ".\n");
375 break;
376 case HIST_TYPE_INDIR_CALL_TOPN:
377 fprintf (dump_file, "Indirect call topn ");
378 if (hist->hvalue.counters)
380 int i;
382 fprintf (dump_file, "accu:%" PRId64, hist->hvalue.counters[0]);
383 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
385 fprintf (dump_file, " target:%" PRId64 " value:%" PRId64,
386 (int64_t) hist->hvalue.counters[i],
387 (int64_t) hist->hvalue.counters[i+1]);
390 fprintf (dump_file, ".\n");
391 break;
392 case HIST_TYPE_MAX:
393 gcc_unreachable ();
397 /* Dump information about HIST to DUMP_FILE. */
399 void
400 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
402 struct bitpack_d bp;
403 unsigned int i;
405 bp = bitpack_create (ob->main_stream);
406 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
407 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
408 streamer_write_bitpack (&bp);
409 switch (hist->type)
411 case HIST_TYPE_INTERVAL:
412 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
413 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
414 break;
415 default:
416 break;
418 for (i = 0; i < hist->n_counters; i++)
419 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
420 if (hist->hvalue.next)
421 stream_out_histogram_value (ob, hist->hvalue.next);
423 /* Dump information about HIST to DUMP_FILE. */
425 void
426 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
428 enum hist_type type;
429 unsigned int ncounters = 0;
430 struct bitpack_d bp;
431 unsigned int i;
432 histogram_value new_val;
433 bool next;
434 histogram_value *next_p = NULL;
438 bp = streamer_read_bitpack (ib);
439 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
440 next = bp_unpack_value (&bp, 1);
441 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
442 switch (type)
444 case HIST_TYPE_INTERVAL:
445 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
446 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
447 ncounters = new_val->hdata.intvl.steps + 2;
448 break;
450 case HIST_TYPE_POW2:
451 case HIST_TYPE_AVERAGE:
452 ncounters = 2;
453 break;
455 case HIST_TYPE_SINGLE_VALUE:
456 case HIST_TYPE_INDIR_CALL:
457 ncounters = 3;
458 break;
460 case HIST_TYPE_CONST_DELTA:
461 ncounters = 4;
462 break;
464 case HIST_TYPE_IOR:
465 case HIST_TYPE_TIME_PROFILE:
466 ncounters = 1;
467 break;
469 case HIST_TYPE_INDIR_CALL_TOPN:
470 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
471 break;
473 case HIST_TYPE_MAX:
474 gcc_unreachable ();
476 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
477 new_val->n_counters = ncounters;
478 for (i = 0; i < ncounters; i++)
479 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
480 if (!next_p)
481 gimple_add_histogram_value (cfun, stmt, new_val);
482 else
483 *next_p = new_val;
484 next_p = &new_val->hvalue.next;
486 while (next);
489 /* Dump all histograms attached to STMT to DUMP_FILE. */
491 void
492 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
494 histogram_value hist;
495 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
496 dump_histogram_value (dump_file, hist);
499 /* Remove all histograms associated with STMT. */
501 void
502 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
504 histogram_value val;
505 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
506 gimple_remove_histogram_value (fun, stmt, val);
509 /* Duplicate all histograms associates with OSTMT to STMT. */
511 void
512 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
513 struct function *ofun, gimple ostmt)
515 histogram_value val;
516 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
518 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
519 memcpy (new_val, val, sizeof (*val));
520 new_val->hvalue.stmt = stmt;
521 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
522 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
523 gimple_add_histogram_value (fun, stmt, new_val);
528 /* Move all histograms associated with OSTMT to STMT. */
530 void
531 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
533 histogram_value val = gimple_histogram_value (fun, ostmt);
534 if (val)
536 /* The following three statements can't be reordered,
537 because histogram hashtab relies on stmt field value
538 for finding the exact slot. */
539 set_histogram_value (fun, ostmt, NULL);
540 for (; val != NULL; val = val->hvalue.next)
541 val->hvalue.stmt = stmt;
542 set_histogram_value (fun, stmt, val);
546 static bool error_found = false;
548 /* Helper function for verify_histograms. For each histogram reachable via htab
549 walk verify that it was reached via statement walk. */
551 static int
552 visit_hist (void **slot, void *data)
554 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
555 histogram_value hist = *(histogram_value *) slot;
557 if (!visited->contains (hist)
558 && hist->type != HIST_TYPE_TIME_PROFILE)
560 error ("dead histogram");
561 dump_histogram_value (stderr, hist);
562 debug_gimple_stmt (hist->hvalue.stmt);
563 error_found = true;
565 return 1;
569 /* Verify sanity of the histograms. */
571 DEBUG_FUNCTION void
572 verify_histograms (void)
574 basic_block bb;
575 gimple_stmt_iterator gsi;
576 histogram_value hist;
578 error_found = false;
579 hash_set<histogram_value> visited_hists;
580 FOR_EACH_BB_FN (bb, cfun)
581 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
583 gimple stmt = gsi_stmt (gsi);
585 for (hist = gimple_histogram_value (cfun, stmt); hist;
586 hist = hist->hvalue.next)
588 if (hist->hvalue.stmt != stmt)
590 error ("Histogram value statement does not correspond to "
591 "the statement it is associated with");
592 debug_gimple_stmt (stmt);
593 dump_histogram_value (stderr, hist);
594 error_found = true;
596 visited_hists.add (hist);
599 if (VALUE_HISTOGRAMS (cfun))
600 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
601 if (error_found)
602 internal_error ("verify_histograms failed");
605 /* Helper function for verify_histograms. For each histogram reachable via htab
606 walk verify that it was reached via statement walk. */
608 static int
609 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
611 histogram_value hist = *(histogram_value *) slot;
612 free (hist->hvalue.counters);
613 #ifdef ENABLE_CHECKING
614 memset (hist, 0xab, sizeof (*hist));
615 #endif
616 free (hist);
617 return 1;
620 void
621 free_histograms (void)
623 if (VALUE_HISTOGRAMS (cfun))
625 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
626 htab_delete (VALUE_HISTOGRAMS (cfun));
627 VALUE_HISTOGRAMS (cfun) = NULL;
632 /* The overall number of invocations of the counter should match
633 execution count of basic block. Report it as error rather than
634 internal error as it might mean that user has misused the profile
635 somehow. */
637 static bool
638 check_counter (gimple stmt, const char * name,
639 gcov_type *count, gcov_type *all, gcov_type bb_count)
641 if (*all != bb_count || *count > *all)
643 location_t locus;
644 locus = (stmt != NULL)
645 ? gimple_location (stmt)
646 : DECL_SOURCE_LOCATION (current_function_decl);
647 if (flag_profile_correction)
649 if (dump_enabled_p ())
650 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
651 "correcting inconsistent value profile: %s "
652 "profiler overall count (%d) does not match BB "
653 "count (%d)\n", name, (int)*all, (int)bb_count);
654 *all = bb_count;
655 if (*count > *all)
656 *count = *all;
657 return false;
659 else
661 error_at (locus, "corrupted value profile: %s "
662 "profile counter (%d out of %d) inconsistent with "
663 "basic-block count (%d)",
664 name,
665 (int) *count,
666 (int) *all,
667 (int) bb_count);
668 return true;
672 return false;
676 /* GIMPLE based transformations. */
678 bool
679 gimple_value_profile_transformations (void)
681 basic_block bb;
682 gimple_stmt_iterator gsi;
683 bool changed = false;
685 FOR_EACH_BB_FN (bb, cfun)
687 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
689 gimple stmt = gsi_stmt (gsi);
690 histogram_value th = gimple_histogram_value (cfun, stmt);
691 if (!th)
692 continue;
694 if (dump_file)
696 fprintf (dump_file, "Trying transformations on stmt ");
697 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
698 dump_histograms_for_stmt (cfun, dump_file, stmt);
701 /* Transformations: */
702 /* The order of things in this conditional controls which
703 transformation is used when more than one is applicable. */
704 /* It is expected that any code added by the transformations
705 will be added before the current statement, and that the
706 current statement remain valid (although possibly
707 modified) upon return. */
708 if (gimple_mod_subtract_transform (&gsi)
709 || gimple_divmod_fixed_value_transform (&gsi)
710 || gimple_mod_pow2_value_transform (&gsi)
711 || gimple_stringops_transform (&gsi)
712 || gimple_ic_transform (&gsi))
714 stmt = gsi_stmt (gsi);
715 changed = true;
716 /* Original statement may no longer be in the same block. */
717 if (bb != gimple_bb (stmt))
719 bb = gimple_bb (stmt);
720 gsi = gsi_for_stmt (stmt);
726 if (changed)
728 counts_to_freqs ();
731 return changed;
735 /* Generate code for transformation 1 (with parent gimple assignment
736 STMT and probability of taking the optimal path PROB, which is
737 equivalent to COUNT/ALL within roundoff error). This generates the
738 result into a temp and returns the temp; it does not replace or
739 alter the original STMT. */
741 static tree
742 gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
743 gcov_type count, gcov_type all)
745 gassign *stmt1, *stmt2;
746 gcond *stmt3;
747 tree tmp0, tmp1, tmp2;
748 gimple bb1end, bb2end, bb3end;
749 basic_block bb, bb2, bb3, bb4;
750 tree optype, op1, op2;
751 edge e12, e13, e23, e24, e34;
752 gimple_stmt_iterator gsi;
754 gcc_assert (is_gimple_assign (stmt)
755 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
756 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
758 optype = TREE_TYPE (gimple_assign_lhs (stmt));
759 op1 = gimple_assign_rhs1 (stmt);
760 op2 = gimple_assign_rhs2 (stmt);
762 bb = gimple_bb (stmt);
763 gsi = gsi_for_stmt (stmt);
765 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
766 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
767 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
768 stmt2 = gimple_build_assign (tmp1, op2);
769 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
770 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
771 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
772 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
773 bb1end = stmt3;
775 tmp2 = create_tmp_reg (optype, "PROF");
776 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
777 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
778 bb2end = stmt1;
780 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
781 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
782 bb3end = stmt1;
784 /* Fix CFG. */
785 /* Edge e23 connects bb2 to bb3, etc. */
786 e12 = split_block (bb, bb1end);
787 bb2 = e12->dest;
788 bb2->count = count;
789 e23 = split_block (bb2, bb2end);
790 bb3 = e23->dest;
791 bb3->count = all - count;
792 e34 = split_block (bb3, bb3end);
793 bb4 = e34->dest;
794 bb4->count = all;
796 e12->flags &= ~EDGE_FALLTHRU;
797 e12->flags |= EDGE_FALSE_VALUE;
798 e12->probability = prob;
799 e12->count = count;
801 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
802 e13->probability = REG_BR_PROB_BASE - prob;
803 e13->count = all - count;
805 remove_edge (e23);
807 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
808 e24->probability = REG_BR_PROB_BASE;
809 e24->count = count;
811 e34->probability = REG_BR_PROB_BASE;
812 e34->count = all - count;
814 return tmp2;
818 /* Do transform 1) on INSN if applicable. */
820 static bool
821 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
823 histogram_value histogram;
824 enum tree_code code;
825 gcov_type val, count, all;
826 tree result, value, tree_val;
827 gcov_type prob;
828 gassign *stmt;
830 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
831 if (!stmt)
832 return false;
834 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
835 return false;
837 code = gimple_assign_rhs_code (stmt);
839 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
840 return false;
842 histogram = gimple_histogram_value_of_type (cfun, stmt,
843 HIST_TYPE_SINGLE_VALUE);
844 if (!histogram)
845 return false;
847 value = histogram->hvalue.value;
848 val = histogram->hvalue.counters[0];
849 count = histogram->hvalue.counters[1];
850 all = histogram->hvalue.counters[2];
851 gimple_remove_histogram_value (cfun, stmt, histogram);
853 /* We require that count is at least half of all; this means
854 that for the transformation to fire the value must be constant
855 at least 50% of time (and 75% gives the guarantee of usage). */
856 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
857 || 2 * count < all
858 || optimize_bb_for_size_p (gimple_bb (stmt)))
859 return false;
861 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
862 return false;
864 /* Compute probability of taking the optimal path. */
865 if (all > 0)
866 prob = GCOV_COMPUTE_SCALE (count, all);
867 else
868 prob = 0;
870 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
871 tree_val = build_int_cst (get_gcov_type (), val);
872 else
874 HOST_WIDE_INT a[2];
875 a[0] = (unsigned HOST_WIDE_INT) val;
876 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
878 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
879 TYPE_PRECISION (get_gcov_type ()), false));
881 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
883 if (dump_file)
885 fprintf (dump_file, "Div/mod by constant ");
886 print_generic_expr (dump_file, value, TDF_SLIM);
887 fprintf (dump_file, "=");
888 print_generic_expr (dump_file, tree_val, TDF_SLIM);
889 fprintf (dump_file, " transformation on insn ");
890 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
893 gimple_assign_set_rhs_from_tree (si, result);
894 update_stmt (gsi_stmt (*si));
896 return true;
899 /* Generate code for transformation 2 (with parent gimple assign STMT and
900 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
901 within roundoff error). This generates the result into a temp and returns
902 the temp; it does not replace or alter the original STMT. */
903 static tree
904 gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
906 gassign *stmt1, *stmt2, *stmt3;
907 gcond *stmt4;
908 tree tmp2, tmp3;
909 gimple bb1end, bb2end, bb3end;
910 basic_block bb, bb2, bb3, bb4;
911 tree optype, op1, op2;
912 edge e12, e13, e23, e24, e34;
913 gimple_stmt_iterator gsi;
914 tree result;
916 gcc_assert (is_gimple_assign (stmt)
917 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
919 optype = TREE_TYPE (gimple_assign_lhs (stmt));
920 op1 = gimple_assign_rhs1 (stmt);
921 op2 = gimple_assign_rhs2 (stmt);
923 bb = gimple_bb (stmt);
924 gsi = gsi_for_stmt (stmt);
926 result = create_tmp_reg (optype, "PROF");
927 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
928 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
929 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
930 build_int_cst (optype, -1));
931 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
932 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
933 NULL_TREE, NULL_TREE);
934 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
935 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
936 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
937 bb1end = stmt4;
939 /* tmp2 == op2-1 inherited from previous block. */
940 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
941 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
942 bb2end = stmt1;
944 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
945 op1, op2);
946 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
947 bb3end = stmt1;
949 /* Fix CFG. */
950 /* Edge e23 connects bb2 to bb3, etc. */
951 e12 = split_block (bb, bb1end);
952 bb2 = e12->dest;
953 bb2->count = count;
954 e23 = split_block (bb2, bb2end);
955 bb3 = e23->dest;
956 bb3->count = all - count;
957 e34 = split_block (bb3, bb3end);
958 bb4 = e34->dest;
959 bb4->count = all;
961 e12->flags &= ~EDGE_FALLTHRU;
962 e12->flags |= EDGE_FALSE_VALUE;
963 e12->probability = prob;
964 e12->count = count;
966 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
967 e13->probability = REG_BR_PROB_BASE - prob;
968 e13->count = all - count;
970 remove_edge (e23);
972 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
973 e24->probability = REG_BR_PROB_BASE;
974 e24->count = count;
976 e34->probability = REG_BR_PROB_BASE;
977 e34->count = all - count;
979 return result;
982 /* Do transform 2) on INSN if applicable. */
983 static bool
984 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
986 histogram_value histogram;
987 enum tree_code code;
988 gcov_type count, wrong_values, all;
989 tree lhs_type, result, value;
990 gcov_type prob;
991 gassign *stmt;
993 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
994 if (!stmt)
995 return false;
997 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
998 if (!INTEGRAL_TYPE_P (lhs_type))
999 return false;
1001 code = gimple_assign_rhs_code (stmt);
1003 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1004 return false;
1006 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
1007 if (!histogram)
1008 return false;
1010 value = histogram->hvalue.value;
1011 wrong_values = histogram->hvalue.counters[0];
1012 count = histogram->hvalue.counters[1];
1014 gimple_remove_histogram_value (cfun, stmt, histogram);
1016 /* We require that we hit a power of 2 at least half of all evaluations. */
1017 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
1018 || count < wrong_values
1019 || optimize_bb_for_size_p (gimple_bb (stmt)))
1020 return false;
1022 if (dump_file)
1024 fprintf (dump_file, "Mod power of 2 transformation on insn ");
1025 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1028 /* Compute probability of taking the optimal path. */
1029 all = count + wrong_values;
1031 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
1032 return false;
1034 if (all > 0)
1035 prob = GCOV_COMPUTE_SCALE (count, all);
1036 else
1037 prob = 0;
1039 result = gimple_mod_pow2 (stmt, prob, count, all);
1041 gimple_assign_set_rhs_from_tree (si, result);
1042 update_stmt (gsi_stmt (*si));
1044 return true;
1047 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1048 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1049 supported and this is built into this interface. The probabilities of taking
1050 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1051 COUNT2/ALL respectively within roundoff error). This generates the
1052 result into a temp and returns the temp; it does not replace or alter
1053 the original STMT. */
1054 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1056 static tree
1057 gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
1058 gcov_type count1, gcov_type count2, gcov_type all)
1060 gassign *stmt1;
1061 gimple stmt2;
1062 gcond *stmt3;
1063 tree tmp1;
1064 gimple bb1end, bb2end = NULL, bb3end;
1065 basic_block bb, bb2, bb3, bb4;
1066 tree optype, op1, op2;
1067 edge e12, e23 = 0, e24, e34, e14;
1068 gimple_stmt_iterator gsi;
1069 tree result;
1071 gcc_assert (is_gimple_assign (stmt)
1072 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1074 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1075 op1 = gimple_assign_rhs1 (stmt);
1076 op2 = gimple_assign_rhs2 (stmt);
1078 bb = gimple_bb (stmt);
1079 gsi = gsi_for_stmt (stmt);
1081 result = create_tmp_reg (optype, "PROF");
1082 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1083 stmt1 = gimple_build_assign (result, op1);
1084 stmt2 = gimple_build_assign (tmp1, op2);
1085 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1086 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1087 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1088 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1089 bb1end = stmt3;
1091 if (ncounts) /* Assumed to be 0 or 1 */
1093 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1094 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1095 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1096 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1097 bb2end = stmt2;
1100 /* Fallback case. */
1101 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1102 result, tmp1);
1103 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1104 bb3end = stmt1;
1106 /* Fix CFG. */
1107 /* Edge e23 connects bb2 to bb3, etc. */
1108 /* However block 3 is optional; if it is not there, references
1109 to 3 really refer to block 2. */
1110 e12 = split_block (bb, bb1end);
1111 bb2 = e12->dest;
1112 bb2->count = all - count1;
1114 if (ncounts) /* Assumed to be 0 or 1. */
1116 e23 = split_block (bb2, bb2end);
1117 bb3 = e23->dest;
1118 bb3->count = all - count1 - count2;
1121 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1122 bb4 = e34->dest;
1123 bb4->count = all;
1125 e12->flags &= ~EDGE_FALLTHRU;
1126 e12->flags |= EDGE_FALSE_VALUE;
1127 e12->probability = REG_BR_PROB_BASE - prob1;
1128 e12->count = all - count1;
1130 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1131 e14->probability = prob1;
1132 e14->count = count1;
1134 if (ncounts) /* Assumed to be 0 or 1. */
1136 e23->flags &= ~EDGE_FALLTHRU;
1137 e23->flags |= EDGE_FALSE_VALUE;
1138 e23->count = all - count1 - count2;
1139 e23->probability = REG_BR_PROB_BASE - prob2;
1141 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1142 e24->probability = prob2;
1143 e24->count = count2;
1146 e34->probability = REG_BR_PROB_BASE;
1147 e34->count = all - count1 - count2;
1149 return result;
1153 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1155 static bool
1156 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1158 histogram_value histogram;
1159 enum tree_code code;
1160 gcov_type count, wrong_values, all;
1161 tree lhs_type, result;
1162 gcov_type prob1, prob2;
1163 unsigned int i, steps;
1164 gcov_type count1, count2;
1165 gassign *stmt;
1167 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1168 if (!stmt)
1169 return false;
1171 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1172 if (!INTEGRAL_TYPE_P (lhs_type))
1173 return false;
1175 code = gimple_assign_rhs_code (stmt);
1177 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1178 return false;
1180 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1181 if (!histogram)
1182 return false;
1184 all = 0;
1185 wrong_values = 0;
1186 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1187 all += histogram->hvalue.counters[i];
1189 wrong_values += histogram->hvalue.counters[i];
1190 wrong_values += histogram->hvalue.counters[i+1];
1191 steps = histogram->hdata.intvl.steps;
1192 all += wrong_values;
1193 count1 = histogram->hvalue.counters[0];
1194 count2 = histogram->hvalue.counters[1];
1196 /* Compute probability of taking the optimal path. */
1197 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1199 gimple_remove_histogram_value (cfun, stmt, histogram);
1200 return false;
1203 if (flag_profile_correction && count1 + count2 > all)
1204 all = count1 + count2;
1206 gcc_assert (count1 + count2 <= all);
1208 /* We require that we use just subtractions in at least 50% of all
1209 evaluations. */
1210 count = 0;
1211 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1213 count += histogram->hvalue.counters[i];
1214 if (count * 2 >= all)
1215 break;
1217 if (i == steps
1218 || optimize_bb_for_size_p (gimple_bb (stmt)))
1219 return false;
1221 gimple_remove_histogram_value (cfun, stmt, histogram);
1222 if (dump_file)
1224 fprintf (dump_file, "Mod subtract transformation on insn ");
1225 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1228 /* Compute probability of taking the optimal path(s). */
1229 if (all > 0)
1231 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1232 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1234 else
1236 prob1 = prob2 = 0;
1239 /* In practice, "steps" is always 2. This interface reflects this,
1240 and will need to be changed if "steps" can change. */
1241 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1243 gimple_assign_set_rhs_from_tree (si, result);
1244 update_stmt (gsi_stmt (*si));
1246 return true;
1249 typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
1251 static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
1253 /* Returns true if node graph is initialized. This
1254 is used to test if profile_id has been created
1255 for cgraph_nodes. */
1257 bool
1258 coverage_node_map_initialized_p (void)
1260 return cgraph_node_map != 0;
1263 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1264 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1265 that the PROFILE_IDs was already assigned. */
1267 void
1268 init_node_map (bool local)
1270 struct cgraph_node *n;
1271 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
1273 FOR_EACH_DEFINED_FUNCTION (n)
1274 if (n->has_gimple_body_p ())
1276 cgraph_node **val;
1277 if (local)
1279 n->profile_id = coverage_compute_profile_id (n);
1280 while ((val = cgraph_node_map->get (n->profile_id))
1281 || !n->profile_id)
1283 if (dump_file)
1284 fprintf (dump_file, "Local profile-id %i conflict"
1285 " with nodes %s/%i %s/%i\n",
1286 n->profile_id,
1287 n->name (),
1288 n->order,
1289 (*val)->name (),
1290 (*val)->order);
1291 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1294 else if (!n->profile_id)
1296 if (dump_file)
1297 fprintf (dump_file,
1298 "Node %s/%i has no profile-id"
1299 " (profile feedback missing?)\n",
1300 n->name (),
1301 n->order);
1302 continue;
1304 else if ((val = cgraph_node_map->get (n->profile_id)))
1306 if (dump_file)
1307 fprintf (dump_file,
1308 "Node %s/%i has IP profile-id %i conflict. "
1309 "Giving up.\n",
1310 n->name (),
1311 n->order,
1312 n->profile_id);
1313 *val = NULL;
1314 continue;
1316 cgraph_node_map->put (n->profile_id, n);
1320 /* Delete the CGRAPH_NODE_MAP. */
1322 void
1323 del_node_map (void)
1325 delete cgraph_node_map;
1328 /* Return cgraph node for function with pid */
1330 struct cgraph_node*
1331 find_func_by_profile_id (int profile_id)
1333 cgraph_node **val = cgraph_node_map->get (profile_id);
1334 if (val)
1335 return *val;
1336 else
1337 return NULL;
1340 /* Perform sanity check on the indirect call target. Due to race conditions,
1341 false function target may be attributed to an indirect call site. If the
1342 call expression type mismatches with the target function's type, expand_call
1343 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1344 Returns true if TARGET is considered ok for call CALL_STMT. */
1346 bool
1347 check_ic_target (gcall *call_stmt, struct cgraph_node *target)
1349 location_t locus;
1350 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1351 return true;
1353 locus = gimple_location (call_stmt);
1354 if (dump_enabled_p ())
1355 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1356 "Skipping target %s with mismatching types for icall\n",
1357 target->name ());
1358 return false;
1361 /* Do transformation
1363 if (actual_callee_address == address_of_most_common_function/method)
1364 do direct call
1365 else
1366 old call
1369 gcall *
1370 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1371 int prob, gcov_type count, gcov_type all)
1373 gcall *dcall_stmt;
1374 gassign *load_stmt;
1375 gcond *cond_stmt;
1376 gcall *iretbnd_stmt = NULL;
1377 tree tmp0, tmp1, tmp;
1378 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1379 tree optype = build_pointer_type (void_type_node);
1380 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1381 gimple_stmt_iterator gsi;
1382 int lp_nr, dflags;
1383 edge e_eh, e;
1384 edge_iterator ei;
1385 gimple_stmt_iterator psi;
1387 cond_bb = gimple_bb (icall_stmt);
1388 gsi = gsi_for_stmt (icall_stmt);
1390 if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
1391 iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
1393 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1394 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1395 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1396 load_stmt = gimple_build_assign (tmp0, tmp);
1397 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1399 tmp = fold_convert (optype, build_addr (direct_call->decl,
1400 current_function_decl));
1401 load_stmt = gimple_build_assign (tmp1, tmp);
1402 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1404 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1405 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1407 gimple_set_vdef (icall_stmt, NULL_TREE);
1408 gimple_set_vuse (icall_stmt, NULL_TREE);
1409 update_stmt (icall_stmt);
1410 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1411 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1412 dflags = flags_from_decl_or_type (direct_call->decl);
1413 if ((dflags & ECF_NORETURN) != 0)
1414 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1415 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1417 /* Fix CFG. */
1418 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1419 e_cd = split_block (cond_bb, cond_stmt);
1420 dcall_bb = e_cd->dest;
1421 dcall_bb->count = count;
1423 e_di = split_block (dcall_bb, dcall_stmt);
1424 icall_bb = e_di->dest;
1425 icall_bb->count = all - count;
1427 /* Do not disturb existing EH edges from the indirect call. */
1428 if (!stmt_ends_bb_p (icall_stmt))
1429 e_ij = split_block (icall_bb, icall_stmt);
1430 else
1432 e_ij = find_fallthru_edge (icall_bb->succs);
1433 /* The indirect call might be noreturn. */
1434 if (e_ij != NULL)
1436 e_ij->probability = REG_BR_PROB_BASE;
1437 e_ij->count = all - count;
1438 e_ij = single_pred_edge (split_edge (e_ij));
1441 if (e_ij != NULL)
1443 join_bb = e_ij->dest;
1444 join_bb->count = all;
1447 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1448 e_cd->probability = prob;
1449 e_cd->count = count;
1451 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1452 e_ci->probability = REG_BR_PROB_BASE - prob;
1453 e_ci->count = all - count;
1455 remove_edge (e_di);
1457 if (e_ij != NULL)
1459 if ((dflags & ECF_NORETURN) != 0)
1460 e_ij->count = all;
1461 else
1463 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1464 e_dj->probability = REG_BR_PROB_BASE;
1465 e_dj->count = count;
1467 e_ij->count = all - count;
1469 e_ij->probability = REG_BR_PROB_BASE;
1472 /* Insert PHI node for the call result if necessary. */
1473 if (gimple_call_lhs (icall_stmt)
1474 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1475 && (dflags & ECF_NORETURN) == 0)
1477 tree result = gimple_call_lhs (icall_stmt);
1478 gphi *phi = create_phi_node (result, join_bb);
1479 gimple_call_set_lhs (icall_stmt,
1480 duplicate_ssa_name (result, icall_stmt));
1481 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1482 gimple_call_set_lhs (dcall_stmt,
1483 duplicate_ssa_name (result, dcall_stmt));
1484 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1486 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1487 call then we need to make it's copy for the direct
1488 call. */
1489 if (iretbnd_stmt)
1491 if (gimple_call_lhs (iretbnd_stmt))
1493 gimple copy;
1495 gimple_set_vdef (iretbnd_stmt, NULL_TREE);
1496 gimple_set_vuse (iretbnd_stmt, NULL_TREE);
1497 update_stmt (iretbnd_stmt);
1499 result = gimple_call_lhs (iretbnd_stmt);
1500 phi = create_phi_node (result, join_bb);
1502 copy = gimple_copy (iretbnd_stmt);
1503 gimple_call_set_arg (copy, 0,
1504 gimple_call_lhs (dcall_stmt));
1505 gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
1506 gsi_insert_on_edge (e_dj, copy);
1507 add_phi_arg (phi, gimple_call_lhs (copy),
1508 e_dj, UNKNOWN_LOCATION);
1510 gimple_call_set_arg (iretbnd_stmt, 0,
1511 gimple_call_lhs (icall_stmt));
1512 gimple_call_set_lhs (iretbnd_stmt,
1513 duplicate_ssa_name (result, iretbnd_stmt));
1514 psi = gsi_for_stmt (iretbnd_stmt);
1515 gsi_remove (&psi, false);
1516 gsi_insert_on_edge (e_ij, iretbnd_stmt);
1517 add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
1518 e_ij, UNKNOWN_LOCATION);
1520 gsi_commit_one_edge_insert (e_dj, NULL);
1521 gsi_commit_one_edge_insert (e_ij, NULL);
1523 else
1525 psi = gsi_for_stmt (iretbnd_stmt);
1526 gsi_remove (&psi, true);
1531 /* Build an EH edge for the direct call if necessary. */
1532 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1533 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1535 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1538 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1539 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1541 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1542 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1543 !gsi_end_p (psi); gsi_next (&psi))
1545 gphi *phi = psi.phi ();
1546 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1547 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1550 if (!stmt_could_throw_p (dcall_stmt))
1551 gimple_purge_dead_eh_edges (dcall_bb);
1552 return dcall_stmt;
1556 For every checked indirect/virtual call determine if most common pid of
1557 function/class method has probability more than 50%. If yes modify code of
1558 this call to:
1561 static bool
1562 gimple_ic_transform (gimple_stmt_iterator *gsi)
1564 gcall *stmt;
1565 histogram_value histogram;
1566 gcov_type val, count, all, bb_all;
1567 struct cgraph_node *direct_call;
1569 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1570 if (!stmt)
1571 return false;
1573 if (gimple_call_fndecl (stmt) != NULL_TREE)
1574 return false;
1576 if (gimple_call_internal_p (stmt))
1577 return false;
1579 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1580 if (!histogram)
1581 return false;
1583 val = histogram->hvalue.counters [0];
1584 count = histogram->hvalue.counters [1];
1585 all = histogram->hvalue.counters [2];
1587 bb_all = gimple_bb (stmt)->count;
1588 /* The order of CHECK_COUNTER calls is important -
1589 since check_counter can correct the third parameter
1590 and we want to make count <= all <= bb_all. */
1591 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1592 || check_counter (stmt, "ic", &count, &all, all))
1594 gimple_remove_histogram_value (cfun, stmt, histogram);
1595 return false;
1598 if (4 * count <= 3 * all)
1599 return false;
1601 direct_call = find_func_by_profile_id ((int)val);
1603 if (direct_call == NULL)
1605 if (val)
1607 if (dump_file)
1609 fprintf (dump_file, "Indirect call -> direct call from other module");
1610 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1611 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1614 return false;
1617 if (!check_ic_target (stmt, direct_call))
1619 if (dump_file)
1621 fprintf (dump_file, "Indirect call -> direct call ");
1622 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1623 fprintf (dump_file, "=> ");
1624 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1625 fprintf (dump_file, " transformation skipped because of type mismatch");
1626 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1628 gimple_remove_histogram_value (cfun, stmt, histogram);
1629 return false;
1632 if (dump_file)
1634 fprintf (dump_file, "Indirect call -> direct call ");
1635 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1636 fprintf (dump_file, "=> ");
1637 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1638 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1639 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1640 fprintf (dump_file, "hist->count %" PRId64
1641 " hist->all %" PRId64"\n", count, all);
1644 return true;
1647 /* Return true if the stringop CALL with FNDECL shall be profiled.
1648 SIZE_ARG be set to the argument index for the size of the string
1649 operation.
1651 static bool
1652 interesting_stringop_to_profile_p (tree fndecl, gcall *call, int *size_arg)
1654 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1656 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1657 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1658 return false;
1660 switch (fcode)
1662 case BUILT_IN_MEMCPY:
1663 case BUILT_IN_MEMPCPY:
1664 *size_arg = 2;
1665 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1666 INTEGER_TYPE, VOID_TYPE);
1667 case BUILT_IN_MEMSET:
1668 *size_arg = 2;
1669 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1670 INTEGER_TYPE, VOID_TYPE);
1671 case BUILT_IN_BZERO:
1672 *size_arg = 1;
1673 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1674 VOID_TYPE);
1675 default:
1676 gcc_unreachable ();
1680 /* Convert stringop (..., vcall_size)
1681 into
1682 if (vcall_size == icall_size)
1683 stringop (..., icall_size);
1684 else
1685 stringop (..., vcall_size);
1686 assuming we'll propagate a true constant into ICALL_SIZE later. */
1688 static void
1689 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
1690 gcov_type count, gcov_type all)
1692 gassign *tmp_stmt;
1693 gcond *cond_stmt;
1694 gcall *icall_stmt;
1695 tree tmp0, tmp1, vcall_size, optype;
1696 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1697 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1698 gimple_stmt_iterator gsi;
1699 tree fndecl;
1700 int size_arg;
1702 fndecl = gimple_call_fndecl (vcall_stmt);
1703 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1704 gcc_unreachable ();
1706 cond_bb = gimple_bb (vcall_stmt);
1707 gsi = gsi_for_stmt (vcall_stmt);
1709 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1710 optype = TREE_TYPE (vcall_size);
1712 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1713 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1714 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1715 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1717 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1718 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1720 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1721 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1723 gimple_set_vdef (vcall_stmt, NULL);
1724 gimple_set_vuse (vcall_stmt, NULL);
1725 update_stmt (vcall_stmt);
1726 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1727 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1728 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1730 /* Fix CFG. */
1731 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1732 e_ci = split_block (cond_bb, cond_stmt);
1733 icall_bb = e_ci->dest;
1734 icall_bb->count = count;
1736 e_iv = split_block (icall_bb, icall_stmt);
1737 vcall_bb = e_iv->dest;
1738 vcall_bb->count = all - count;
1740 e_vj = split_block (vcall_bb, vcall_stmt);
1741 join_bb = e_vj->dest;
1742 join_bb->count = all;
1744 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1745 e_ci->probability = prob;
1746 e_ci->count = count;
1748 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1749 e_cv->probability = REG_BR_PROB_BASE - prob;
1750 e_cv->count = all - count;
1752 remove_edge (e_iv);
1754 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1755 e_ij->probability = REG_BR_PROB_BASE;
1756 e_ij->count = count;
1758 e_vj->probability = REG_BR_PROB_BASE;
1759 e_vj->count = all - count;
1761 /* Insert PHI node for the call result if necessary. */
1762 if (gimple_call_lhs (vcall_stmt)
1763 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1765 tree result = gimple_call_lhs (vcall_stmt);
1766 gphi *phi = create_phi_node (result, join_bb);
1767 gimple_call_set_lhs (vcall_stmt,
1768 duplicate_ssa_name (result, vcall_stmt));
1769 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1770 gimple_call_set_lhs (icall_stmt,
1771 duplicate_ssa_name (result, icall_stmt));
1772 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1775 /* Because these are all string op builtins, they're all nothrow. */
1776 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1777 gcc_assert (!stmt_could_throw_p (icall_stmt));
1780 /* Find values inside STMT for that we want to measure histograms for
1781 division/modulo optimization. */
1782 static bool
1783 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1785 gcall *stmt;
1786 tree fndecl;
1787 tree blck_size;
1788 enum built_in_function fcode;
1789 histogram_value histogram;
1790 gcov_type count, all, val;
1791 tree dest, src;
1792 unsigned int dest_align, src_align;
1793 gcov_type prob;
1794 tree tree_val;
1795 int size_arg;
1797 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1798 if (!stmt)
1799 return false;
1800 fndecl = gimple_call_fndecl (stmt);
1801 if (!fndecl)
1802 return false;
1803 fcode = DECL_FUNCTION_CODE (fndecl);
1804 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1805 return false;
1807 blck_size = gimple_call_arg (stmt, size_arg);
1808 if (TREE_CODE (blck_size) == INTEGER_CST)
1809 return false;
1811 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1812 if (!histogram)
1813 return false;
1814 val = histogram->hvalue.counters[0];
1815 count = histogram->hvalue.counters[1];
1816 all = histogram->hvalue.counters[2];
1817 gimple_remove_histogram_value (cfun, stmt, histogram);
1818 /* We require that count is at least half of all; this means
1819 that for the transformation to fire the value must be constant
1820 at least 80% of time. */
1821 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1822 return false;
1823 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1824 return false;
1825 if (all > 0)
1826 prob = GCOV_COMPUTE_SCALE (count, all);
1827 else
1828 prob = 0;
1829 dest = gimple_call_arg (stmt, 0);
1830 dest_align = get_pointer_alignment (dest);
1831 switch (fcode)
1833 case BUILT_IN_MEMCPY:
1834 case BUILT_IN_MEMPCPY:
1835 src = gimple_call_arg (stmt, 1);
1836 src_align = get_pointer_alignment (src);
1837 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1838 return false;
1839 break;
1840 case BUILT_IN_MEMSET:
1841 if (!can_store_by_pieces (val, builtin_memset_read_str,
1842 gimple_call_arg (stmt, 1),
1843 dest_align, true))
1844 return false;
1845 break;
1846 case BUILT_IN_BZERO:
1847 if (!can_store_by_pieces (val, builtin_memset_read_str,
1848 integer_zero_node,
1849 dest_align, true))
1850 return false;
1851 break;
1852 default:
1853 gcc_unreachable ();
1855 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1856 tree_val = build_int_cst (get_gcov_type (), val);
1857 else
1859 HOST_WIDE_INT a[2];
1860 a[0] = (unsigned HOST_WIDE_INT) val;
1861 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1863 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1864 TYPE_PRECISION (get_gcov_type ()), false));
1867 if (dump_file)
1869 fprintf (dump_file, "Single value %i stringop transformation on ",
1870 (int)val);
1871 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1873 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1875 return true;
1878 void
1879 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1880 HOST_WIDE_INT *expected_size)
1882 histogram_value histogram;
1883 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1884 if (!histogram)
1885 *expected_size = -1;
1886 else if (!histogram->hvalue.counters[1])
1888 *expected_size = -1;
1889 gimple_remove_histogram_value (cfun, stmt, histogram);
1891 else
1893 gcov_type size;
1894 size = ((histogram->hvalue.counters[0]
1895 + histogram->hvalue.counters[1] / 2)
1896 / histogram->hvalue.counters[1]);
1897 /* Even if we can hold bigger value in SIZE, INT_MAX
1898 is safe "infinity" for code generation strategies. */
1899 if (size > INT_MAX)
1900 size = INT_MAX;
1901 *expected_size = size;
1902 gimple_remove_histogram_value (cfun, stmt, histogram);
1904 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1905 if (!histogram)
1906 *expected_align = 0;
1907 else if (!histogram->hvalue.counters[0])
1909 gimple_remove_histogram_value (cfun, stmt, histogram);
1910 *expected_align = 0;
1912 else
1914 gcov_type count;
1915 int alignment;
1917 count = histogram->hvalue.counters[0];
1918 alignment = 1;
1919 while (!(count & alignment)
1920 && (alignment * 2 * BITS_PER_UNIT))
1921 alignment <<= 1;
1922 *expected_align = alignment * BITS_PER_UNIT;
1923 gimple_remove_histogram_value (cfun, stmt, histogram);
1928 /* Find values inside STMT for that we want to measure histograms for
1929 division/modulo optimization. */
1930 static void
1931 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1933 tree lhs, divisor, op0, type;
1934 histogram_value hist;
1936 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1937 return;
1939 lhs = gimple_assign_lhs (stmt);
1940 type = TREE_TYPE (lhs);
1941 if (!INTEGRAL_TYPE_P (type))
1942 return;
1944 switch (gimple_assign_rhs_code (stmt))
1946 case TRUNC_DIV_EXPR:
1947 case TRUNC_MOD_EXPR:
1948 divisor = gimple_assign_rhs2 (stmt);
1949 op0 = gimple_assign_rhs1 (stmt);
1951 values->reserve (3);
1953 if (TREE_CODE (divisor) == SSA_NAME)
1954 /* Check for the case where the divisor is the same value most
1955 of the time. */
1956 values->quick_push (gimple_alloc_histogram_value (cfun,
1957 HIST_TYPE_SINGLE_VALUE,
1958 stmt, divisor));
1960 /* For mod, check whether it is not often a noop (or replaceable by
1961 a few subtractions). */
1962 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1963 && TYPE_UNSIGNED (type))
1965 tree val;
1966 /* Check for a special case where the divisor is power of 2. */
1967 values->quick_push (gimple_alloc_histogram_value (cfun,
1968 HIST_TYPE_POW2,
1969 stmt, divisor));
1971 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1972 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1973 stmt, val);
1974 hist->hdata.intvl.int_start = 0;
1975 hist->hdata.intvl.steps = 2;
1976 values->quick_push (hist);
1978 return;
1980 default:
1981 return;
1985 /* Find calls inside STMT for that we want to measure histograms for
1986 indirect/virtual call optimization. */
1988 static void
1989 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1991 tree callee;
1993 if (gimple_code (stmt) != GIMPLE_CALL
1994 || gimple_call_internal_p (stmt)
1995 || gimple_call_fndecl (stmt) != NULL_TREE)
1996 return;
1998 callee = gimple_call_fn (stmt);
2000 values->reserve (3);
2002 values->quick_push (gimple_alloc_histogram_value (
2003 cfun,
2004 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
2005 HIST_TYPE_INDIR_CALL_TOPN :
2006 HIST_TYPE_INDIR_CALL,
2007 stmt, callee));
2009 return;
2012 /* Find values inside STMT for that we want to measure histograms for
2013 string operations. */
2014 static void
2015 gimple_stringops_values_to_profile (gimple gs, histogram_values *values)
2017 gcall *stmt;
2018 tree fndecl;
2019 tree blck_size;
2020 tree dest;
2021 int size_arg;
2023 stmt = dyn_cast <gcall *> (gs);
2024 if (!stmt)
2025 return;
2026 fndecl = gimple_call_fndecl (stmt);
2027 if (!fndecl)
2028 return;
2030 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
2031 return;
2033 dest = gimple_call_arg (stmt, 0);
2034 blck_size = gimple_call_arg (stmt, size_arg);
2036 if (TREE_CODE (blck_size) != INTEGER_CST)
2038 values->safe_push (gimple_alloc_histogram_value (cfun,
2039 HIST_TYPE_SINGLE_VALUE,
2040 stmt, blck_size));
2041 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2042 stmt, blck_size));
2044 if (TREE_CODE (blck_size) != INTEGER_CST)
2045 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2046 stmt, dest));
2049 /* Find values inside STMT for that we want to measure histograms and adds
2050 them to list VALUES. */
2052 static void
2053 gimple_values_to_profile (gimple stmt, histogram_values *values)
2055 gimple_divmod_values_to_profile (stmt, values);
2056 gimple_stringops_values_to_profile (stmt, values);
2057 gimple_indirect_call_to_profile (stmt, values);
2060 void
2061 gimple_find_values_to_profile (histogram_values *values)
2063 basic_block bb;
2064 gimple_stmt_iterator gsi;
2065 unsigned i;
2066 histogram_value hist = NULL;
2067 values->create (0);
2069 FOR_EACH_BB_FN (bb, cfun)
2070 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2071 gimple_values_to_profile (gsi_stmt (gsi), values);
2073 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2075 FOR_EACH_VEC_ELT (*values, i, hist)
2077 switch (hist->type)
2079 case HIST_TYPE_INTERVAL:
2080 hist->n_counters = hist->hdata.intvl.steps + 2;
2081 break;
2083 case HIST_TYPE_POW2:
2084 hist->n_counters = 2;
2085 break;
2087 case HIST_TYPE_SINGLE_VALUE:
2088 hist->n_counters = 3;
2089 break;
2091 case HIST_TYPE_CONST_DELTA:
2092 hist->n_counters = 4;
2093 break;
2095 case HIST_TYPE_INDIR_CALL:
2096 hist->n_counters = 3;
2097 break;
2099 case HIST_TYPE_TIME_PROFILE:
2100 hist->n_counters = 1;
2101 break;
2103 case HIST_TYPE_AVERAGE:
2104 hist->n_counters = 2;
2105 break;
2107 case HIST_TYPE_IOR:
2108 hist->n_counters = 1;
2109 break;
2111 case HIST_TYPE_INDIR_CALL_TOPN:
2112 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2113 break;
2115 default:
2116 gcc_unreachable ();
2118 if (dump_file)
2120 fprintf (dump_file, "Stmt ");
2121 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2122 dump_histogram_value (dump_file, hist);