* gcc.dg/vect/slp-9.c: Disable einline.
[official-gcc.git] / gcc / value-prof.c
blob82a144fd21ef0e1f9ba6f16f7aa11d468f5f06eb
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2014 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 "tree.h"
25 #include "tree-nested.h"
26 #include "calls.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "hard-reg-set.h"
30 #include "predict.h"
31 #include "vec.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "basic-block.h"
40 #include "value-prof.h"
41 #include "flags.h"
42 #include "insn-config.h"
43 #include "recog.h"
44 #include "insn-codes.h"
45 #include "optabs.h"
46 #include "regs.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "tree-eh.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimplify.h"
54 #include "gimple-iterator.h"
55 #include "gimple-ssa.h"
56 #include "tree-cfg.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
59 #include "stringpool.h"
60 #include "tree-ssanames.h"
61 #include "diagnostic.h"
62 #include "gimple-pretty-print.h"
63 #include "coverage.h"
64 #include "tree.h"
65 #include "gcov-io.h"
66 #include "timevar.h"
67 #include "dumpfile.h"
68 #include "profile.h"
69 #include "hash-map.h"
70 #include "plugin-api.h"
71 #include "ipa-ref.h"
72 #include "cgraph.h"
73 #include "data-streamer.h"
74 #include "builtins.h"
75 #include "tree-nested.h"
76 #include "params.h"
77 #include "tree-chkp.h"
79 /* In this file value profile based optimizations are placed. Currently the
80 following optimizations are implemented (for more detailed descriptions
81 see comments at value_profile_transformations):
83 1) Division/modulo specialization. Provided that we can determine that the
84 operands of the division have some special properties, we may use it to
85 produce more effective code.
87 2) Indirect/virtual call specialization. If we can determine most
88 common function callee in indirect/virtual call. We can use this
89 information to improve code effectiveness (especially info for
90 the inliner).
92 3) Speculative prefetching. If we are able to determine that the difference
93 between addresses accessed by a memory reference is usually constant, we
94 may add the prefetch instructions.
95 FIXME: This transformation was removed together with RTL based value
96 profiling.
99 Value profiling internals
100 ==========================
102 Every value profiling transformation starts with defining what values
103 to profile. There are different histogram types (see HIST_TYPE_* in
104 value-prof.h) and each transformation can request one or more histogram
105 types per GIMPLE statement. The function gimple_find_values_to_profile()
106 collects the values to profile in a vec, and adds the number of counters
107 required for the different histogram types.
109 For a -fprofile-generate run, the statements for which values should be
110 recorded, are instrumented in instrument_values(). The instrumentation
111 is done by helper functions that can be found in tree-profile.c, where
112 new types of histograms can be added if necessary.
114 After a -fprofile-use, the value profiling data is read back in by
115 compute_value_histograms() that translates the collected data to
116 histograms and attaches them to the profiled statements via
117 gimple_add_histogram_value(). Histograms are stored in a hash table
118 that is attached to every intrumented function, see VALUE_HISTOGRAMS
119 in function.h.
121 The value-profile transformations driver is the function
122 gimple_value_profile_transformations(). It traverses all statements in
123 the to-be-transformed function, and looks for statements with one or
124 more histograms attached to it. If a statement has histograms, the
125 transformation functions are called on the statement.
127 Limitations / FIXME / TODO:
128 * Only one histogram of each type can be associated with a statement.
129 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
130 (This type of histogram was originally used to implement a form of
131 stride profiling based speculative prefetching to improve SPEC2000
132 scores for memory-bound benchmarks, mcf and equake. However, this
133 was an RTL value-profiling transformation, and those have all been
134 removed.)
135 * Some value profile transformations are done in builtins.c (?!)
136 * Updating of histograms needs some TLC.
137 * The value profiling code could be used to record analysis results
138 from non-profiling (e.g. VRP).
139 * Adding new profilers should be simplified, starting with a cleanup
140 of what-happens-where andwith making gimple_find_values_to_profile
141 and gimple_value_profile_transformations table-driven, perhaps...
144 static tree gimple_divmod_fixed_value (gassign *, tree, int, gcov_type,
145 gcov_type);
146 static tree gimple_mod_pow2 (gassign *, int, gcov_type, gcov_type);
147 static tree gimple_mod_subtract (gassign *, int, int, int, gcov_type,
148 gcov_type, gcov_type);
149 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
150 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
151 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
152 static bool gimple_stringops_transform (gimple_stmt_iterator *);
153 static bool gimple_ic_transform (gimple_stmt_iterator *);
155 /* Allocate histogram value. */
157 histogram_value
158 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
159 enum hist_type type, gimple stmt, tree value)
161 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
162 hist->hvalue.value = value;
163 hist->hvalue.stmt = stmt;
164 hist->type = type;
165 return hist;
168 /* Hash value for histogram. */
170 static hashval_t
171 histogram_hash (const void *x)
173 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
176 /* Return nonzero if statement for histogram_value X is Y. */
178 static int
179 histogram_eq (const void *x, const void *y)
181 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
184 /* Set histogram for STMT. */
186 static void
187 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
189 void **loc;
190 if (!hist && !VALUE_HISTOGRAMS (fun))
191 return;
192 if (!VALUE_HISTOGRAMS (fun))
193 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
194 histogram_eq, NULL);
195 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
196 htab_hash_pointer (stmt),
197 hist ? INSERT : NO_INSERT);
198 if (!hist)
200 if (loc)
201 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
202 return;
204 *loc = hist;
207 /* Get histogram list for STMT. */
209 histogram_value
210 gimple_histogram_value (struct function *fun, gimple stmt)
212 if (!VALUE_HISTOGRAMS (fun))
213 return NULL;
214 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
215 htab_hash_pointer (stmt));
218 /* Add histogram for STMT. */
220 void
221 gimple_add_histogram_value (struct function *fun, gimple stmt,
222 histogram_value hist)
224 hist->hvalue.next = gimple_histogram_value (fun, stmt);
225 set_histogram_value (fun, stmt, hist);
226 hist->fun = fun;
230 /* Remove histogram HIST from STMT's histogram list. */
232 void
233 gimple_remove_histogram_value (struct function *fun, gimple stmt,
234 histogram_value hist)
236 histogram_value hist2 = gimple_histogram_value (fun, stmt);
237 if (hist == hist2)
239 set_histogram_value (fun, stmt, hist->hvalue.next);
241 else
243 while (hist2->hvalue.next != hist)
244 hist2 = hist2->hvalue.next;
245 hist2->hvalue.next = hist->hvalue.next;
247 free (hist->hvalue.counters);
248 #ifdef ENABLE_CHECKING
249 memset (hist, 0xab, sizeof (*hist));
250 #endif
251 free (hist);
255 /* Lookup histogram of type TYPE in the STMT. */
257 histogram_value
258 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
259 enum hist_type type)
261 histogram_value hist;
262 for (hist = gimple_histogram_value (fun, stmt); hist;
263 hist = hist->hvalue.next)
264 if (hist->type == type)
265 return hist;
266 return NULL;
269 /* Dump information about HIST to DUMP_FILE. */
271 static void
272 dump_histogram_value (FILE *dump_file, histogram_value hist)
274 switch (hist->type)
276 case HIST_TYPE_INTERVAL:
277 fprintf (dump_file, "Interval counter range %d -- %d",
278 hist->hdata.intvl.int_start,
279 (hist->hdata.intvl.int_start
280 + hist->hdata.intvl.steps - 1));
281 if (hist->hvalue.counters)
283 unsigned int i;
284 fprintf (dump_file, " [");
285 for (i = 0; i < hist->hdata.intvl.steps; i++)
286 fprintf (dump_file, " %d:%"PRId64,
287 hist->hdata.intvl.int_start + i,
288 (int64_t) hist->hvalue.counters[i]);
289 fprintf (dump_file, " ] outside range:%"PRId64,
290 (int64_t) hist->hvalue.counters[i]);
292 fprintf (dump_file, ".\n");
293 break;
295 case HIST_TYPE_POW2:
296 fprintf (dump_file, "Pow2 counter ");
297 if (hist->hvalue.counters)
299 fprintf (dump_file, "pow2:%"PRId64
300 " nonpow2:%"PRId64,
301 (int64_t) hist->hvalue.counters[0],
302 (int64_t) hist->hvalue.counters[1]);
304 fprintf (dump_file, ".\n");
305 break;
307 case HIST_TYPE_SINGLE_VALUE:
308 fprintf (dump_file, "Single value ");
309 if (hist->hvalue.counters)
311 fprintf (dump_file, "value:%"PRId64
312 " match:%"PRId64
313 " wrong:%"PRId64,
314 (int64_t) hist->hvalue.counters[0],
315 (int64_t) hist->hvalue.counters[1],
316 (int64_t) hist->hvalue.counters[2]);
318 fprintf (dump_file, ".\n");
319 break;
321 case HIST_TYPE_AVERAGE:
322 fprintf (dump_file, "Average value ");
323 if (hist->hvalue.counters)
325 fprintf (dump_file, "sum:%"PRId64
326 " times:%"PRId64,
327 (int64_t) hist->hvalue.counters[0],
328 (int64_t) hist->hvalue.counters[1]);
330 fprintf (dump_file, ".\n");
331 break;
333 case HIST_TYPE_IOR:
334 fprintf (dump_file, "IOR value ");
335 if (hist->hvalue.counters)
337 fprintf (dump_file, "ior:%"PRId64,
338 (int64_t) hist->hvalue.counters[0]);
340 fprintf (dump_file, ".\n");
341 break;
343 case HIST_TYPE_CONST_DELTA:
344 fprintf (dump_file, "Constant delta ");
345 if (hist->hvalue.counters)
347 fprintf (dump_file, "value:%"PRId64
348 " match:%"PRId64
349 " wrong:%"PRId64,
350 (int64_t) hist->hvalue.counters[0],
351 (int64_t) hist->hvalue.counters[1],
352 (int64_t) hist->hvalue.counters[2]);
354 fprintf (dump_file, ".\n");
355 break;
356 case HIST_TYPE_INDIR_CALL:
357 fprintf (dump_file, "Indirect call ");
358 if (hist->hvalue.counters)
360 fprintf (dump_file, "value:%"PRId64
361 " match:%"PRId64
362 " all:%"PRId64,
363 (int64_t) hist->hvalue.counters[0],
364 (int64_t) hist->hvalue.counters[1],
365 (int64_t) hist->hvalue.counters[2]);
367 fprintf (dump_file, ".\n");
368 break;
369 case HIST_TYPE_TIME_PROFILE:
370 fprintf (dump_file, "Time profile ");
371 if (hist->hvalue.counters)
373 fprintf (dump_file, "time:%"PRId64,
374 (int64_t) hist->hvalue.counters[0]);
376 fprintf (dump_file, ".\n");
377 break;
378 case HIST_TYPE_INDIR_CALL_TOPN:
379 fprintf (dump_file, "Indirect call topn ");
380 if (hist->hvalue.counters)
382 int i;
384 fprintf (dump_file, "accu:%"PRId64, hist->hvalue.counters[0]);
385 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
387 fprintf (dump_file, " target:%"PRId64 " value:%"PRId64,
388 (int64_t) hist->hvalue.counters[i],
389 (int64_t) hist->hvalue.counters[i+1]);
392 fprintf (dump_file, ".\n");
393 break;
394 case HIST_TYPE_MAX:
395 gcc_unreachable ();
399 /* Dump information about HIST to DUMP_FILE. */
401 void
402 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
404 struct bitpack_d bp;
405 unsigned int i;
407 bp = bitpack_create (ob->main_stream);
408 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
409 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
410 streamer_write_bitpack (&bp);
411 switch (hist->type)
413 case HIST_TYPE_INTERVAL:
414 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
415 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
416 break;
417 default:
418 break;
420 for (i = 0; i < hist->n_counters; i++)
421 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
422 if (hist->hvalue.next)
423 stream_out_histogram_value (ob, hist->hvalue.next);
425 /* Dump information about HIST to DUMP_FILE. */
427 void
428 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
430 enum hist_type type;
431 unsigned int ncounters = 0;
432 struct bitpack_d bp;
433 unsigned int i;
434 histogram_value new_val;
435 bool next;
436 histogram_value *next_p = NULL;
440 bp = streamer_read_bitpack (ib);
441 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
442 next = bp_unpack_value (&bp, 1);
443 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
444 switch (type)
446 case HIST_TYPE_INTERVAL:
447 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
448 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
449 ncounters = new_val->hdata.intvl.steps + 2;
450 break;
452 case HIST_TYPE_POW2:
453 case HIST_TYPE_AVERAGE:
454 ncounters = 2;
455 break;
457 case HIST_TYPE_SINGLE_VALUE:
458 case HIST_TYPE_INDIR_CALL:
459 ncounters = 3;
460 break;
462 case HIST_TYPE_CONST_DELTA:
463 ncounters = 4;
464 break;
466 case HIST_TYPE_IOR:
467 case HIST_TYPE_TIME_PROFILE:
468 ncounters = 1;
469 break;
471 case HIST_TYPE_INDIR_CALL_TOPN:
472 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
473 break;
475 case HIST_TYPE_MAX:
476 gcc_unreachable ();
478 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
479 new_val->n_counters = ncounters;
480 for (i = 0; i < ncounters; i++)
481 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
482 if (!next_p)
483 gimple_add_histogram_value (cfun, stmt, new_val);
484 else
485 *next_p = new_val;
486 next_p = &new_val->hvalue.next;
488 while (next);
491 /* Dump all histograms attached to STMT to DUMP_FILE. */
493 void
494 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
496 histogram_value hist;
497 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
498 dump_histogram_value (dump_file, hist);
501 /* Remove all histograms associated with STMT. */
503 void
504 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
506 histogram_value val;
507 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
508 gimple_remove_histogram_value (fun, stmt, val);
511 /* Duplicate all histograms associates with OSTMT to STMT. */
513 void
514 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
515 struct function *ofun, gimple ostmt)
517 histogram_value val;
518 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
520 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
521 memcpy (new_val, val, sizeof (*val));
522 new_val->hvalue.stmt = stmt;
523 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
524 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
525 gimple_add_histogram_value (fun, stmt, new_val);
530 /* Move all histograms associated with OSTMT to STMT. */
532 void
533 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
535 histogram_value val = gimple_histogram_value (fun, ostmt);
536 if (val)
538 /* The following three statements can't be reordered,
539 because histogram hashtab relies on stmt field value
540 for finding the exact slot. */
541 set_histogram_value (fun, ostmt, NULL);
542 for (; val != NULL; val = val->hvalue.next)
543 val->hvalue.stmt = stmt;
544 set_histogram_value (fun, stmt, val);
548 static bool error_found = false;
550 /* Helper function for verify_histograms. For each histogram reachable via htab
551 walk verify that it was reached via statement walk. */
553 static int
554 visit_hist (void **slot, void *data)
556 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
557 histogram_value hist = *(histogram_value *) slot;
559 if (!visited->contains (hist)
560 && hist->type != HIST_TYPE_TIME_PROFILE)
562 error ("dead histogram");
563 dump_histogram_value (stderr, hist);
564 debug_gimple_stmt (hist->hvalue.stmt);
565 error_found = true;
567 return 1;
571 /* Verify sanity of the histograms. */
573 DEBUG_FUNCTION void
574 verify_histograms (void)
576 basic_block bb;
577 gimple_stmt_iterator gsi;
578 histogram_value hist;
580 error_found = false;
581 hash_set<histogram_value> visited_hists;
582 FOR_EACH_BB_FN (bb, cfun)
583 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
585 gimple stmt = gsi_stmt (gsi);
587 for (hist = gimple_histogram_value (cfun, stmt); hist;
588 hist = hist->hvalue.next)
590 if (hist->hvalue.stmt != stmt)
592 error ("Histogram value statement does not correspond to "
593 "the statement it is associated with");
594 debug_gimple_stmt (stmt);
595 dump_histogram_value (stderr, hist);
596 error_found = true;
598 visited_hists.add (hist);
601 if (VALUE_HISTOGRAMS (cfun))
602 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
603 if (error_found)
604 internal_error ("verify_histograms failed");
607 /* Helper function for verify_histograms. For each histogram reachable via htab
608 walk verify that it was reached via statement walk. */
610 static int
611 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
613 histogram_value hist = *(histogram_value *) slot;
614 free (hist->hvalue.counters);
615 #ifdef ENABLE_CHECKING
616 memset (hist, 0xab, sizeof (*hist));
617 #endif
618 free (hist);
619 return 1;
622 void
623 free_histograms (void)
625 if (VALUE_HISTOGRAMS (cfun))
627 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
628 htab_delete (VALUE_HISTOGRAMS (cfun));
629 VALUE_HISTOGRAMS (cfun) = NULL;
634 /* The overall number of invocations of the counter should match
635 execution count of basic block. Report it as error rather than
636 internal error as it might mean that user has misused the profile
637 somehow. */
639 static bool
640 check_counter (gimple stmt, const char * name,
641 gcov_type *count, gcov_type *all, gcov_type bb_count)
643 if (*all != bb_count || *count > *all)
645 location_t locus;
646 locus = (stmt != NULL)
647 ? gimple_location (stmt)
648 : DECL_SOURCE_LOCATION (current_function_decl);
649 if (flag_profile_correction)
651 if (dump_enabled_p ())
652 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
653 "correcting inconsistent value profile: %s "
654 "profiler overall count (%d) does not match BB "
655 "count (%d)\n", name, (int)*all, (int)bb_count);
656 *all = bb_count;
657 if (*count > *all)
658 *count = *all;
659 return false;
661 else
663 error_at (locus, "corrupted value profile: %s "
664 "profile counter (%d out of %d) inconsistent with "
665 "basic-block count (%d)",
666 name,
667 (int) *count,
668 (int) *all,
669 (int) bb_count);
670 return true;
674 return false;
678 /* GIMPLE based transformations. */
680 bool
681 gimple_value_profile_transformations (void)
683 basic_block bb;
684 gimple_stmt_iterator gsi;
685 bool changed = false;
687 FOR_EACH_BB_FN (bb, cfun)
689 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
691 gimple stmt = gsi_stmt (gsi);
692 histogram_value th = gimple_histogram_value (cfun, stmt);
693 if (!th)
694 continue;
696 if (dump_file)
698 fprintf (dump_file, "Trying transformations on stmt ");
699 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
700 dump_histograms_for_stmt (cfun, dump_file, stmt);
703 /* Transformations: */
704 /* The order of things in this conditional controls which
705 transformation is used when more than one is applicable. */
706 /* It is expected that any code added by the transformations
707 will be added before the current statement, and that the
708 current statement remain valid (although possibly
709 modified) upon return. */
710 if (gimple_mod_subtract_transform (&gsi)
711 || gimple_divmod_fixed_value_transform (&gsi)
712 || gimple_mod_pow2_value_transform (&gsi)
713 || gimple_stringops_transform (&gsi)
714 || gimple_ic_transform (&gsi))
716 stmt = gsi_stmt (gsi);
717 changed = true;
718 /* Original statement may no longer be in the same block. */
719 if (bb != gimple_bb (stmt))
721 bb = gimple_bb (stmt);
722 gsi = gsi_for_stmt (stmt);
728 if (changed)
730 counts_to_freqs ();
733 return changed;
737 /* Generate code for transformation 1 (with parent gimple assignment
738 STMT and probability of taking the optimal path PROB, which is
739 equivalent to COUNT/ALL within roundoff error). This generates the
740 result into a temp and returns the temp; it does not replace or
741 alter the original STMT. */
743 static tree
744 gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
745 gcov_type count, gcov_type all)
747 gassign *stmt1, *stmt2;
748 gcond *stmt3;
749 tree tmp0, tmp1, tmp2;
750 gimple bb1end, bb2end, bb3end;
751 basic_block bb, bb2, bb3, bb4;
752 tree optype, op1, op2;
753 edge e12, e13, e23, e24, e34;
754 gimple_stmt_iterator gsi;
756 gcc_assert (is_gimple_assign (stmt)
757 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
758 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
760 optype = TREE_TYPE (gimple_assign_lhs (stmt));
761 op1 = gimple_assign_rhs1 (stmt);
762 op2 = gimple_assign_rhs2 (stmt);
764 bb = gimple_bb (stmt);
765 gsi = gsi_for_stmt (stmt);
767 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
768 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
769 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
770 stmt2 = gimple_build_assign (tmp1, op2);
771 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
772 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
773 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
774 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
775 bb1end = stmt3;
777 tmp2 = create_tmp_reg (optype, "PROF");
778 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
779 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
780 bb2end = stmt1;
782 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
783 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
784 bb3end = stmt1;
786 /* Fix CFG. */
787 /* Edge e23 connects bb2 to bb3, etc. */
788 e12 = split_block (bb, bb1end);
789 bb2 = e12->dest;
790 bb2->count = count;
791 e23 = split_block (bb2, bb2end);
792 bb3 = e23->dest;
793 bb3->count = all - count;
794 e34 = split_block (bb3, bb3end);
795 bb4 = e34->dest;
796 bb4->count = all;
798 e12->flags &= ~EDGE_FALLTHRU;
799 e12->flags |= EDGE_FALSE_VALUE;
800 e12->probability = prob;
801 e12->count = count;
803 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
804 e13->probability = REG_BR_PROB_BASE - prob;
805 e13->count = all - count;
807 remove_edge (e23);
809 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
810 e24->probability = REG_BR_PROB_BASE;
811 e24->count = count;
813 e34->probability = REG_BR_PROB_BASE;
814 e34->count = all - count;
816 return tmp2;
820 /* Do transform 1) on INSN if applicable. */
822 static bool
823 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
825 histogram_value histogram;
826 enum tree_code code;
827 gcov_type val, count, all;
828 tree result, value, tree_val;
829 gcov_type prob;
830 gassign *stmt;
832 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
833 if (!stmt)
834 return false;
836 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
837 return false;
839 code = gimple_assign_rhs_code (stmt);
841 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
842 return false;
844 histogram = gimple_histogram_value_of_type (cfun, stmt,
845 HIST_TYPE_SINGLE_VALUE);
846 if (!histogram)
847 return false;
849 value = histogram->hvalue.value;
850 val = histogram->hvalue.counters[0];
851 count = histogram->hvalue.counters[1];
852 all = histogram->hvalue.counters[2];
853 gimple_remove_histogram_value (cfun, stmt, histogram);
855 /* We require that count is at least half of all; this means
856 that for the transformation to fire the value must be constant
857 at least 50% of time (and 75% gives the guarantee of usage). */
858 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
859 || 2 * count < all
860 || optimize_bb_for_size_p (gimple_bb (stmt)))
861 return false;
863 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
864 return false;
866 /* Compute probability of taking the optimal path. */
867 if (all > 0)
868 prob = GCOV_COMPUTE_SCALE (count, all);
869 else
870 prob = 0;
872 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
873 tree_val = build_int_cst (get_gcov_type (), val);
874 else
876 HOST_WIDE_INT a[2];
877 a[0] = (unsigned HOST_WIDE_INT) val;
878 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
880 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
881 TYPE_PRECISION (get_gcov_type ()), false));
883 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
885 if (dump_file)
887 fprintf (dump_file, "Div/mod by constant ");
888 print_generic_expr (dump_file, value, TDF_SLIM);
889 fprintf (dump_file, "=");
890 print_generic_expr (dump_file, tree_val, TDF_SLIM);
891 fprintf (dump_file, " transformation on insn ");
892 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
895 gimple_assign_set_rhs_from_tree (si, result);
896 update_stmt (gsi_stmt (*si));
898 return true;
901 /* Generate code for transformation 2 (with parent gimple assign STMT and
902 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
903 within roundoff error). This generates the result into a temp and returns
904 the temp; it does not replace or alter the original STMT. */
905 static tree
906 gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
908 gassign *stmt1, *stmt2, *stmt3;
909 gcond *stmt4;
910 tree tmp2, tmp3;
911 gimple bb1end, bb2end, bb3end;
912 basic_block bb, bb2, bb3, bb4;
913 tree optype, op1, op2;
914 edge e12, e13, e23, e24, e34;
915 gimple_stmt_iterator gsi;
916 tree result;
918 gcc_assert (is_gimple_assign (stmt)
919 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
921 optype = TREE_TYPE (gimple_assign_lhs (stmt));
922 op1 = gimple_assign_rhs1 (stmt);
923 op2 = gimple_assign_rhs2 (stmt);
925 bb = gimple_bb (stmt);
926 gsi = gsi_for_stmt (stmt);
928 result = create_tmp_reg (optype, "PROF");
929 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
930 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
931 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
932 build_int_cst (optype, -1));
933 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
934 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
935 NULL_TREE, NULL_TREE);
936 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
937 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
938 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
939 bb1end = stmt4;
941 /* tmp2 == op2-1 inherited from previous block. */
942 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
943 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
944 bb2end = stmt1;
946 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
947 op1, op2);
948 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
949 bb3end = stmt1;
951 /* Fix CFG. */
952 /* Edge e23 connects bb2 to bb3, etc. */
953 e12 = split_block (bb, bb1end);
954 bb2 = e12->dest;
955 bb2->count = count;
956 e23 = split_block (bb2, bb2end);
957 bb3 = e23->dest;
958 bb3->count = all - count;
959 e34 = split_block (bb3, bb3end);
960 bb4 = e34->dest;
961 bb4->count = all;
963 e12->flags &= ~EDGE_FALLTHRU;
964 e12->flags |= EDGE_FALSE_VALUE;
965 e12->probability = prob;
966 e12->count = count;
968 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
969 e13->probability = REG_BR_PROB_BASE - prob;
970 e13->count = all - count;
972 remove_edge (e23);
974 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
975 e24->probability = REG_BR_PROB_BASE;
976 e24->count = count;
978 e34->probability = REG_BR_PROB_BASE;
979 e34->count = all - count;
981 return result;
984 /* Do transform 2) on INSN if applicable. */
985 static bool
986 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
988 histogram_value histogram;
989 enum tree_code code;
990 gcov_type count, wrong_values, all;
991 tree lhs_type, result, value;
992 gcov_type prob;
993 gassign *stmt;
995 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
996 if (!stmt)
997 return false;
999 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1000 if (!INTEGRAL_TYPE_P (lhs_type))
1001 return false;
1003 code = gimple_assign_rhs_code (stmt);
1005 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1006 return false;
1008 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
1009 if (!histogram)
1010 return false;
1012 value = histogram->hvalue.value;
1013 wrong_values = histogram->hvalue.counters[0];
1014 count = histogram->hvalue.counters[1];
1016 gimple_remove_histogram_value (cfun, stmt, histogram);
1018 /* We require that we hit a power of 2 at least half of all evaluations. */
1019 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
1020 || count < wrong_values
1021 || optimize_bb_for_size_p (gimple_bb (stmt)))
1022 return false;
1024 if (dump_file)
1026 fprintf (dump_file, "Mod power of 2 transformation on insn ");
1027 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1030 /* Compute probability of taking the optimal path. */
1031 all = count + wrong_values;
1033 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
1034 return false;
1036 if (all > 0)
1037 prob = GCOV_COMPUTE_SCALE (count, all);
1038 else
1039 prob = 0;
1041 result = gimple_mod_pow2 (stmt, prob, count, all);
1043 gimple_assign_set_rhs_from_tree (si, result);
1044 update_stmt (gsi_stmt (*si));
1046 return true;
1049 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1050 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1051 supported and this is built into this interface. The probabilities of taking
1052 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1053 COUNT2/ALL respectively within roundoff error). This generates the
1054 result into a temp and returns the temp; it does not replace or alter
1055 the original STMT. */
1056 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1058 static tree
1059 gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
1060 gcov_type count1, gcov_type count2, gcov_type all)
1062 gassign *stmt1;
1063 gimple stmt2;
1064 gcond *stmt3;
1065 tree tmp1;
1066 gimple bb1end, bb2end = NULL, bb3end;
1067 basic_block bb, bb2, bb3, bb4;
1068 tree optype, op1, op2;
1069 edge e12, e23 = 0, e24, e34, e14;
1070 gimple_stmt_iterator gsi;
1071 tree result;
1073 gcc_assert (is_gimple_assign (stmt)
1074 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1076 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1077 op1 = gimple_assign_rhs1 (stmt);
1078 op2 = gimple_assign_rhs2 (stmt);
1080 bb = gimple_bb (stmt);
1081 gsi = gsi_for_stmt (stmt);
1083 result = create_tmp_reg (optype, "PROF");
1084 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1085 stmt1 = gimple_build_assign (result, op1);
1086 stmt2 = gimple_build_assign (tmp1, op2);
1087 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1088 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1089 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1090 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1091 bb1end = stmt3;
1093 if (ncounts) /* Assumed to be 0 or 1 */
1095 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1096 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1097 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1098 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1099 bb2end = stmt2;
1102 /* Fallback case. */
1103 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1104 result, tmp1);
1105 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1106 bb3end = stmt1;
1108 /* Fix CFG. */
1109 /* Edge e23 connects bb2 to bb3, etc. */
1110 /* However block 3 is optional; if it is not there, references
1111 to 3 really refer to block 2. */
1112 e12 = split_block (bb, bb1end);
1113 bb2 = e12->dest;
1114 bb2->count = all - count1;
1116 if (ncounts) /* Assumed to be 0 or 1. */
1118 e23 = split_block (bb2, bb2end);
1119 bb3 = e23->dest;
1120 bb3->count = all - count1 - count2;
1123 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1124 bb4 = e34->dest;
1125 bb4->count = all;
1127 e12->flags &= ~EDGE_FALLTHRU;
1128 e12->flags |= EDGE_FALSE_VALUE;
1129 e12->probability = REG_BR_PROB_BASE - prob1;
1130 e12->count = all - count1;
1132 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1133 e14->probability = prob1;
1134 e14->count = count1;
1136 if (ncounts) /* Assumed to be 0 or 1. */
1138 e23->flags &= ~EDGE_FALLTHRU;
1139 e23->flags |= EDGE_FALSE_VALUE;
1140 e23->count = all - count1 - count2;
1141 e23->probability = REG_BR_PROB_BASE - prob2;
1143 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1144 e24->probability = prob2;
1145 e24->count = count2;
1148 e34->probability = REG_BR_PROB_BASE;
1149 e34->count = all - count1 - count2;
1151 return result;
1155 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1157 static bool
1158 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1160 histogram_value histogram;
1161 enum tree_code code;
1162 gcov_type count, wrong_values, all;
1163 tree lhs_type, result;
1164 gcov_type prob1, prob2;
1165 unsigned int i, steps;
1166 gcov_type count1, count2;
1167 gassign *stmt;
1169 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1170 if (!stmt)
1171 return false;
1173 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1174 if (!INTEGRAL_TYPE_P (lhs_type))
1175 return false;
1177 code = gimple_assign_rhs_code (stmt);
1179 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1180 return false;
1182 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1183 if (!histogram)
1184 return false;
1186 all = 0;
1187 wrong_values = 0;
1188 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1189 all += histogram->hvalue.counters[i];
1191 wrong_values += histogram->hvalue.counters[i];
1192 wrong_values += histogram->hvalue.counters[i+1];
1193 steps = histogram->hdata.intvl.steps;
1194 all += wrong_values;
1195 count1 = histogram->hvalue.counters[0];
1196 count2 = histogram->hvalue.counters[1];
1198 /* Compute probability of taking the optimal path. */
1199 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1201 gimple_remove_histogram_value (cfun, stmt, histogram);
1202 return false;
1205 if (flag_profile_correction && count1 + count2 > all)
1206 all = count1 + count2;
1208 gcc_assert (count1 + count2 <= all);
1210 /* We require that we use just subtractions in at least 50% of all
1211 evaluations. */
1212 count = 0;
1213 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1215 count += histogram->hvalue.counters[i];
1216 if (count * 2 >= all)
1217 break;
1219 if (i == steps
1220 || optimize_bb_for_size_p (gimple_bb (stmt)))
1221 return false;
1223 gimple_remove_histogram_value (cfun, stmt, histogram);
1224 if (dump_file)
1226 fprintf (dump_file, "Mod subtract transformation on insn ");
1227 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1230 /* Compute probability of taking the optimal path(s). */
1231 if (all > 0)
1233 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1234 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1236 else
1238 prob1 = prob2 = 0;
1241 /* In practice, "steps" is always 2. This interface reflects this,
1242 and will need to be changed if "steps" can change. */
1243 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1245 gimple_assign_set_rhs_from_tree (si, result);
1246 update_stmt (gsi_stmt (*si));
1248 return true;
1251 struct profile_id_traits : default_hashmap_traits
1253 template<typename T>
1254 static bool
1255 is_deleted (T &e)
1257 return e.m_key == UINT_MAX;
1260 template<typename T> static bool is_empty (T &e) { return e.m_key == 0; }
1261 template<typename T> static void mark_deleted (T &e) { e.m_key = UINT_MAX; }
1262 template<typename T> static void mark_empty (T &e) { e.m_key = 0; }
1265 static hash_map<unsigned int, cgraph_node *, profile_id_traits> *
1266 cgraph_node_map = 0;
1268 /* Returns true if node graph is initialized. This
1269 is used to test if profile_id has been created
1270 for cgraph_nodes. */
1272 bool
1273 coverage_node_map_initialized_p (void)
1275 return cgraph_node_map != 0;
1278 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1279 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1280 that the PROFILE_IDs was already assigned. */
1282 void
1283 init_node_map (bool local)
1285 struct cgraph_node *n;
1286 cgraph_node_map
1287 = new hash_map<unsigned int, cgraph_node *, profile_id_traits>;
1289 FOR_EACH_DEFINED_FUNCTION (n)
1290 if (n->has_gimple_body_p ())
1292 cgraph_node **val;
1293 if (local)
1295 n->profile_id = coverage_compute_profile_id (n);
1296 while ((val = cgraph_node_map->get (n->profile_id))
1297 || !n->profile_id)
1299 if (dump_file)
1300 fprintf (dump_file, "Local profile-id %i conflict"
1301 " with nodes %s/%i %s/%i\n",
1302 n->profile_id,
1303 n->name (),
1304 n->order,
1305 (*val)->name (),
1306 (*val)->order);
1307 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1310 else if (!n->profile_id)
1312 if (dump_file)
1313 fprintf (dump_file,
1314 "Node %s/%i has no profile-id"
1315 " (profile feedback missing?)\n",
1316 n->name (),
1317 n->order);
1318 continue;
1320 else if ((val = cgraph_node_map->get (n->profile_id)))
1322 if (dump_file)
1323 fprintf (dump_file,
1324 "Node %s/%i has IP profile-id %i conflict. "
1325 "Giving up.\n",
1326 n->name (),
1327 n->order,
1328 n->profile_id);
1329 *val = NULL;
1330 continue;
1332 cgraph_node_map->put (n->profile_id, n);
1336 /* Delete the CGRAPH_NODE_MAP. */
1338 void
1339 del_node_map (void)
1341 delete cgraph_node_map;
1344 /* Return cgraph node for function with pid */
1346 struct cgraph_node*
1347 find_func_by_profile_id (int profile_id)
1349 cgraph_node **val = cgraph_node_map->get (profile_id);
1350 if (val)
1351 return *val;
1352 else
1353 return NULL;
1356 /* Perform sanity check on the indirect call target. Due to race conditions,
1357 false function target may be attributed to an indirect call site. If the
1358 call expression type mismatches with the target function's type, expand_call
1359 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1360 Returns true if TARGET is considered ok for call CALL_STMT. */
1362 bool
1363 check_ic_target (gcall *call_stmt, struct cgraph_node *target)
1365 location_t locus;
1366 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1367 return true;
1369 locus = gimple_location (call_stmt);
1370 if (dump_enabled_p ())
1371 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1372 "Skipping target %s with mismatching types for icall\n",
1373 target->name ());
1374 return false;
1377 /* Do transformation
1379 if (actual_callee_address == address_of_most_common_function/method)
1380 do direct call
1381 else
1382 old call
1385 gcall *
1386 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1387 int prob, gcov_type count, gcov_type all)
1389 gcall *dcall_stmt;
1390 gassign *load_stmt;
1391 gcond *cond_stmt;
1392 gcall *iretbnd_stmt = NULL;
1393 tree tmp0, tmp1, tmp;
1394 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1395 tree optype = build_pointer_type (void_type_node);
1396 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1397 gimple_stmt_iterator gsi;
1398 int lp_nr, dflags;
1399 edge e_eh, e;
1400 edge_iterator ei;
1401 gimple_stmt_iterator psi;
1403 cond_bb = gimple_bb (icall_stmt);
1404 gsi = gsi_for_stmt (icall_stmt);
1406 if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
1407 iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
1409 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1410 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1411 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1412 load_stmt = gimple_build_assign (tmp0, tmp);
1413 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1415 tmp = fold_convert (optype, build_addr (direct_call->decl,
1416 current_function_decl));
1417 load_stmt = gimple_build_assign (tmp1, tmp);
1418 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1420 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1421 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1423 gimple_set_vdef (icall_stmt, NULL_TREE);
1424 gimple_set_vuse (icall_stmt, NULL_TREE);
1425 update_stmt (icall_stmt);
1426 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1427 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1428 dflags = flags_from_decl_or_type (direct_call->decl);
1429 if ((dflags & ECF_NORETURN) != 0)
1430 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1431 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1433 /* Fix CFG. */
1434 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1435 e_cd = split_block (cond_bb, cond_stmt);
1436 dcall_bb = e_cd->dest;
1437 dcall_bb->count = count;
1439 e_di = split_block (dcall_bb, dcall_stmt);
1440 icall_bb = e_di->dest;
1441 icall_bb->count = all - count;
1443 /* Do not disturb existing EH edges from the indirect call. */
1444 if (!stmt_ends_bb_p (icall_stmt))
1445 e_ij = split_block (icall_bb, icall_stmt);
1446 else
1448 e_ij = find_fallthru_edge (icall_bb->succs);
1449 /* The indirect call might be noreturn. */
1450 if (e_ij != NULL)
1452 e_ij->probability = REG_BR_PROB_BASE;
1453 e_ij->count = all - count;
1454 e_ij = single_pred_edge (split_edge (e_ij));
1457 if (e_ij != NULL)
1459 join_bb = e_ij->dest;
1460 join_bb->count = all;
1463 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1464 e_cd->probability = prob;
1465 e_cd->count = count;
1467 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1468 e_ci->probability = REG_BR_PROB_BASE - prob;
1469 e_ci->count = all - count;
1471 remove_edge (e_di);
1473 if (e_ij != NULL)
1475 if ((dflags & ECF_NORETURN) != 0)
1476 e_ij->count = all;
1477 else
1479 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1480 e_dj->probability = REG_BR_PROB_BASE;
1481 e_dj->count = count;
1483 e_ij->count = all - count;
1485 e_ij->probability = REG_BR_PROB_BASE;
1488 /* Insert PHI node for the call result if necessary. */
1489 if (gimple_call_lhs (icall_stmt)
1490 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1491 && (dflags & ECF_NORETURN) == 0)
1493 tree result = gimple_call_lhs (icall_stmt);
1494 gphi *phi = create_phi_node (result, join_bb);
1495 gimple_call_set_lhs (icall_stmt,
1496 duplicate_ssa_name (result, icall_stmt));
1497 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1498 gimple_call_set_lhs (dcall_stmt,
1499 duplicate_ssa_name (result, dcall_stmt));
1500 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1502 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1503 call then we need to make it's copy for the direct
1504 call. */
1505 if (iretbnd_stmt)
1507 if (gimple_call_lhs (iretbnd_stmt))
1509 gimple copy;
1511 gimple_set_vdef (iretbnd_stmt, NULL_TREE);
1512 gimple_set_vuse (iretbnd_stmt, NULL_TREE);
1513 update_stmt (iretbnd_stmt);
1515 result = gimple_call_lhs (iretbnd_stmt);
1516 phi = create_phi_node (result, join_bb);
1518 copy = gimple_copy (iretbnd_stmt);
1519 gimple_call_set_arg (copy, 0,
1520 gimple_call_lhs (dcall_stmt));
1521 gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
1522 gsi_insert_on_edge (e_dj, copy);
1523 add_phi_arg (phi, gimple_call_lhs (copy),
1524 e_dj, UNKNOWN_LOCATION);
1526 gimple_call_set_arg (iretbnd_stmt, 0,
1527 gimple_call_lhs (icall_stmt));
1528 gimple_call_set_lhs (iretbnd_stmt,
1529 duplicate_ssa_name (result, iretbnd_stmt));
1530 psi = gsi_for_stmt (iretbnd_stmt);
1531 gsi_remove (&psi, false);
1532 gsi_insert_on_edge (e_ij, iretbnd_stmt);
1533 add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
1534 e_ij, UNKNOWN_LOCATION);
1536 gsi_commit_one_edge_insert (e_dj, NULL);
1537 gsi_commit_one_edge_insert (e_ij, NULL);
1539 else
1541 psi = gsi_for_stmt (iretbnd_stmt);
1542 gsi_remove (&psi, true);
1547 /* Build an EH edge for the direct call if necessary. */
1548 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1549 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1551 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1554 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1555 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1557 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1558 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1559 !gsi_end_p (psi); gsi_next (&psi))
1561 gphi *phi = psi.phi ();
1562 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1563 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1566 return dcall_stmt;
1570 For every checked indirect/virtual call determine if most common pid of
1571 function/class method has probability more than 50%. If yes modify code of
1572 this call to:
1575 static bool
1576 gimple_ic_transform (gimple_stmt_iterator *gsi)
1578 gcall *stmt;
1579 histogram_value histogram;
1580 gcov_type val, count, all, bb_all;
1581 struct cgraph_node *direct_call;
1583 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1584 if (!stmt)
1585 return false;
1587 if (gimple_call_fndecl (stmt) != NULL_TREE)
1588 return false;
1590 if (gimple_call_internal_p (stmt))
1591 return false;
1593 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1594 if (!histogram)
1595 return false;
1597 val = histogram->hvalue.counters [0];
1598 count = histogram->hvalue.counters [1];
1599 all = histogram->hvalue.counters [2];
1601 bb_all = gimple_bb (stmt)->count;
1602 /* The order of CHECK_COUNTER calls is important -
1603 since check_counter can correct the third parameter
1604 and we want to make count <= all <= bb_all. */
1605 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1606 || check_counter (stmt, "ic", &count, &all, all))
1608 gimple_remove_histogram_value (cfun, stmt, histogram);
1609 return false;
1612 if (4 * count <= 3 * all)
1613 return false;
1615 direct_call = find_func_by_profile_id ((int)val);
1617 if (direct_call == NULL)
1619 if (val)
1621 if (dump_file)
1623 fprintf (dump_file, "Indirect call -> direct call from other module");
1624 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1625 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1628 return false;
1631 if (!check_ic_target (stmt, direct_call))
1633 if (dump_file)
1635 fprintf (dump_file, "Indirect call -> direct call ");
1636 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1637 fprintf (dump_file, "=> ");
1638 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1639 fprintf (dump_file, " transformation skipped because of type mismatch");
1640 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1642 gimple_remove_histogram_value (cfun, stmt, histogram);
1643 return false;
1646 if (dump_file)
1648 fprintf (dump_file, "Indirect call -> direct call ");
1649 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1650 fprintf (dump_file, "=> ");
1651 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1652 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1653 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1654 fprintf (dump_file, "hist->count %"PRId64
1655 " hist->all %"PRId64"\n", count, all);
1658 return true;
1661 /* Return true if the stringop CALL with FNDECL shall be profiled.
1662 SIZE_ARG be set to the argument index for the size of the string
1663 operation.
1665 static bool
1666 interesting_stringop_to_profile_p (tree fndecl, gcall *call, int *size_arg)
1668 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1670 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1671 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1672 return false;
1674 switch (fcode)
1676 case BUILT_IN_MEMCPY:
1677 case BUILT_IN_MEMPCPY:
1678 *size_arg = 2;
1679 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1680 INTEGER_TYPE, VOID_TYPE);
1681 case BUILT_IN_MEMSET:
1682 *size_arg = 2;
1683 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1684 INTEGER_TYPE, VOID_TYPE);
1685 case BUILT_IN_BZERO:
1686 *size_arg = 1;
1687 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1688 VOID_TYPE);
1689 default:
1690 gcc_unreachable ();
1694 /* Convert stringop (..., vcall_size)
1695 into
1696 if (vcall_size == icall_size)
1697 stringop (..., icall_size);
1698 else
1699 stringop (..., vcall_size);
1700 assuming we'll propagate a true constant into ICALL_SIZE later. */
1702 static void
1703 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
1704 gcov_type count, gcov_type all)
1706 gassign *tmp_stmt;
1707 gcond *cond_stmt;
1708 gcall *icall_stmt;
1709 tree tmp0, tmp1, vcall_size, optype;
1710 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1711 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1712 gimple_stmt_iterator gsi;
1713 tree fndecl;
1714 int size_arg;
1716 fndecl = gimple_call_fndecl (vcall_stmt);
1717 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1718 gcc_unreachable ();
1720 cond_bb = gimple_bb (vcall_stmt);
1721 gsi = gsi_for_stmt (vcall_stmt);
1723 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1724 optype = TREE_TYPE (vcall_size);
1726 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1727 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1728 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1729 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1731 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1732 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1734 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1735 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1737 gimple_set_vdef (vcall_stmt, NULL);
1738 gimple_set_vuse (vcall_stmt, NULL);
1739 update_stmt (vcall_stmt);
1740 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1741 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1742 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1744 /* Fix CFG. */
1745 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1746 e_ci = split_block (cond_bb, cond_stmt);
1747 icall_bb = e_ci->dest;
1748 icall_bb->count = count;
1750 e_iv = split_block (icall_bb, icall_stmt);
1751 vcall_bb = e_iv->dest;
1752 vcall_bb->count = all - count;
1754 e_vj = split_block (vcall_bb, vcall_stmt);
1755 join_bb = e_vj->dest;
1756 join_bb->count = all;
1758 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1759 e_ci->probability = prob;
1760 e_ci->count = count;
1762 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1763 e_cv->probability = REG_BR_PROB_BASE - prob;
1764 e_cv->count = all - count;
1766 remove_edge (e_iv);
1768 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1769 e_ij->probability = REG_BR_PROB_BASE;
1770 e_ij->count = count;
1772 e_vj->probability = REG_BR_PROB_BASE;
1773 e_vj->count = all - count;
1775 /* Insert PHI node for the call result if necessary. */
1776 if (gimple_call_lhs (vcall_stmt)
1777 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1779 tree result = gimple_call_lhs (vcall_stmt);
1780 gphi *phi = create_phi_node (result, join_bb);
1781 gimple_call_set_lhs (vcall_stmt,
1782 duplicate_ssa_name (result, vcall_stmt));
1783 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1784 gimple_call_set_lhs (icall_stmt,
1785 duplicate_ssa_name (result, icall_stmt));
1786 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1789 /* Because these are all string op builtins, they're all nothrow. */
1790 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1791 gcc_assert (!stmt_could_throw_p (icall_stmt));
1794 /* Find values inside STMT for that we want to measure histograms for
1795 division/modulo optimization. */
1796 static bool
1797 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1799 gcall *stmt;
1800 tree fndecl;
1801 tree blck_size;
1802 enum built_in_function fcode;
1803 histogram_value histogram;
1804 gcov_type count, all, val;
1805 tree dest, src;
1806 unsigned int dest_align, src_align;
1807 gcov_type prob;
1808 tree tree_val;
1809 int size_arg;
1811 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1812 if (!stmt)
1813 return false;
1814 fndecl = gimple_call_fndecl (stmt);
1815 if (!fndecl)
1816 return false;
1817 fcode = DECL_FUNCTION_CODE (fndecl);
1818 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1819 return false;
1821 blck_size = gimple_call_arg (stmt, size_arg);
1822 if (TREE_CODE (blck_size) == INTEGER_CST)
1823 return false;
1825 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1826 if (!histogram)
1827 return false;
1828 val = histogram->hvalue.counters[0];
1829 count = histogram->hvalue.counters[1];
1830 all = histogram->hvalue.counters[2];
1831 gimple_remove_histogram_value (cfun, stmt, histogram);
1832 /* We require that count is at least half of all; this means
1833 that for the transformation to fire the value must be constant
1834 at least 80% of time. */
1835 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1836 return false;
1837 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1838 return false;
1839 if (all > 0)
1840 prob = GCOV_COMPUTE_SCALE (count, all);
1841 else
1842 prob = 0;
1843 dest = gimple_call_arg (stmt, 0);
1844 dest_align = get_pointer_alignment (dest);
1845 switch (fcode)
1847 case BUILT_IN_MEMCPY:
1848 case BUILT_IN_MEMPCPY:
1849 src = gimple_call_arg (stmt, 1);
1850 src_align = get_pointer_alignment (src);
1851 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1852 return false;
1853 break;
1854 case BUILT_IN_MEMSET:
1855 if (!can_store_by_pieces (val, builtin_memset_read_str,
1856 gimple_call_arg (stmt, 1),
1857 dest_align, true))
1858 return false;
1859 break;
1860 case BUILT_IN_BZERO:
1861 if (!can_store_by_pieces (val, builtin_memset_read_str,
1862 integer_zero_node,
1863 dest_align, true))
1864 return false;
1865 break;
1866 default:
1867 gcc_unreachable ();
1869 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1870 tree_val = build_int_cst (get_gcov_type (), val);
1871 else
1873 HOST_WIDE_INT a[2];
1874 a[0] = (unsigned HOST_WIDE_INT) val;
1875 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1877 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1878 TYPE_PRECISION (get_gcov_type ()), false));
1881 if (dump_file)
1883 fprintf (dump_file, "Single value %i stringop transformation on ",
1884 (int)val);
1885 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1887 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1889 return true;
1892 void
1893 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1894 HOST_WIDE_INT *expected_size)
1896 histogram_value histogram;
1897 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1898 if (!histogram)
1899 *expected_size = -1;
1900 else if (!histogram->hvalue.counters[1])
1902 *expected_size = -1;
1903 gimple_remove_histogram_value (cfun, stmt, histogram);
1905 else
1907 gcov_type size;
1908 size = ((histogram->hvalue.counters[0]
1909 + histogram->hvalue.counters[1] / 2)
1910 / histogram->hvalue.counters[1]);
1911 /* Even if we can hold bigger value in SIZE, INT_MAX
1912 is safe "infinity" for code generation strategies. */
1913 if (size > INT_MAX)
1914 size = INT_MAX;
1915 *expected_size = size;
1916 gimple_remove_histogram_value (cfun, stmt, histogram);
1918 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1919 if (!histogram)
1920 *expected_align = 0;
1921 else if (!histogram->hvalue.counters[0])
1923 gimple_remove_histogram_value (cfun, stmt, histogram);
1924 *expected_align = 0;
1926 else
1928 gcov_type count;
1929 int alignment;
1931 count = histogram->hvalue.counters[0];
1932 alignment = 1;
1933 while (!(count & alignment)
1934 && (alignment * 2 * BITS_PER_UNIT))
1935 alignment <<= 1;
1936 *expected_align = alignment * BITS_PER_UNIT;
1937 gimple_remove_histogram_value (cfun, stmt, histogram);
1942 /* Find values inside STMT for that we want to measure histograms for
1943 division/modulo optimization. */
1944 static void
1945 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1947 tree lhs, divisor, op0, type;
1948 histogram_value hist;
1950 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1951 return;
1953 lhs = gimple_assign_lhs (stmt);
1954 type = TREE_TYPE (lhs);
1955 if (!INTEGRAL_TYPE_P (type))
1956 return;
1958 switch (gimple_assign_rhs_code (stmt))
1960 case TRUNC_DIV_EXPR:
1961 case TRUNC_MOD_EXPR:
1962 divisor = gimple_assign_rhs2 (stmt);
1963 op0 = gimple_assign_rhs1 (stmt);
1965 values->reserve (3);
1967 if (TREE_CODE (divisor) == SSA_NAME)
1968 /* Check for the case where the divisor is the same value most
1969 of the time. */
1970 values->quick_push (gimple_alloc_histogram_value (cfun,
1971 HIST_TYPE_SINGLE_VALUE,
1972 stmt, divisor));
1974 /* For mod, check whether it is not often a noop (or replaceable by
1975 a few subtractions). */
1976 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1977 && TYPE_UNSIGNED (type))
1979 tree val;
1980 /* Check for a special case where the divisor is power of 2. */
1981 values->quick_push (gimple_alloc_histogram_value (cfun,
1982 HIST_TYPE_POW2,
1983 stmt, divisor));
1985 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1986 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1987 stmt, val);
1988 hist->hdata.intvl.int_start = 0;
1989 hist->hdata.intvl.steps = 2;
1990 values->quick_push (hist);
1992 return;
1994 default:
1995 return;
1999 /* Find calls inside STMT for that we want to measure histograms for
2000 indirect/virtual call optimization. */
2002 static void
2003 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
2005 tree callee;
2007 if (gimple_code (stmt) != GIMPLE_CALL
2008 || gimple_call_internal_p (stmt)
2009 || gimple_call_fndecl (stmt) != NULL_TREE)
2010 return;
2012 callee = gimple_call_fn (stmt);
2014 values->reserve (3);
2016 values->quick_push (gimple_alloc_histogram_value (
2017 cfun,
2018 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
2019 HIST_TYPE_INDIR_CALL_TOPN :
2020 HIST_TYPE_INDIR_CALL,
2021 stmt, callee));
2023 return;
2026 /* Find values inside STMT for that we want to measure histograms for
2027 string operations. */
2028 static void
2029 gimple_stringops_values_to_profile (gimple gs, histogram_values *values)
2031 gcall *stmt;
2032 tree fndecl;
2033 tree blck_size;
2034 tree dest;
2035 int size_arg;
2037 stmt = dyn_cast <gcall *> (gs);
2038 if (!stmt)
2039 return;
2040 fndecl = gimple_call_fndecl (stmt);
2041 if (!fndecl)
2042 return;
2044 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
2045 return;
2047 dest = gimple_call_arg (stmt, 0);
2048 blck_size = gimple_call_arg (stmt, size_arg);
2050 if (TREE_CODE (blck_size) != INTEGER_CST)
2052 values->safe_push (gimple_alloc_histogram_value (cfun,
2053 HIST_TYPE_SINGLE_VALUE,
2054 stmt, blck_size));
2055 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2056 stmt, blck_size));
2058 if (TREE_CODE (blck_size) != INTEGER_CST)
2059 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2060 stmt, dest));
2063 /* Find values inside STMT for that we want to measure histograms and adds
2064 them to list VALUES. */
2066 static void
2067 gimple_values_to_profile (gimple stmt, histogram_values *values)
2069 gimple_divmod_values_to_profile (stmt, values);
2070 gimple_stringops_values_to_profile (stmt, values);
2071 gimple_indirect_call_to_profile (stmt, values);
2074 void
2075 gimple_find_values_to_profile (histogram_values *values)
2077 basic_block bb;
2078 gimple_stmt_iterator gsi;
2079 unsigned i;
2080 histogram_value hist = NULL;
2081 values->create (0);
2083 FOR_EACH_BB_FN (bb, cfun)
2084 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2085 gimple_values_to_profile (gsi_stmt (gsi), values);
2087 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2089 FOR_EACH_VEC_ELT (*values, i, hist)
2091 switch (hist->type)
2093 case HIST_TYPE_INTERVAL:
2094 hist->n_counters = hist->hdata.intvl.steps + 2;
2095 break;
2097 case HIST_TYPE_POW2:
2098 hist->n_counters = 2;
2099 break;
2101 case HIST_TYPE_SINGLE_VALUE:
2102 hist->n_counters = 3;
2103 break;
2105 case HIST_TYPE_CONST_DELTA:
2106 hist->n_counters = 4;
2107 break;
2109 case HIST_TYPE_INDIR_CALL:
2110 hist->n_counters = 3;
2111 break;
2113 case HIST_TYPE_TIME_PROFILE:
2114 hist->n_counters = 1;
2115 break;
2117 case HIST_TYPE_AVERAGE:
2118 hist->n_counters = 2;
2119 break;
2121 case HIST_TYPE_IOR:
2122 hist->n_counters = 1;
2123 break;
2125 case HIST_TYPE_INDIR_CALL_TOPN:
2126 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2127 break;
2129 default:
2130 gcc_unreachable ();
2132 if (dump_file)
2134 fprintf (dump_file, "Stmt ");
2135 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2136 dump_histogram_value (dump_file, hist);