* altivec.md: Delete UNSPEC_VMLADDUHM.
[official-gcc.git] / gcc / value-prof.c
blob462659b5695720fff3513ca1854e9b6570210f8a
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 "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "tree-nested.h"
30 #include "calls.h"
31 #include "rtl.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "expmed.h"
37 #include "dojump.h"
38 #include "explow.h"
39 #include "emit-rtl.h"
40 #include "varasm.h"
41 #include "stmt.h"
42 #include "expr.h"
43 #include "predict.h"
44 #include "dominance.h"
45 #include "cfg.h"
46 #include "basic-block.h"
47 #include "value-prof.h"
48 #include "recog.h"
49 #include "insn-codes.h"
50 #include "optabs.h"
51 #include "regs.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "tree-eh.h"
55 #include "gimple-expr.h"
56 #include "is-a.h"
57 #include "gimple.h"
58 #include "gimplify.h"
59 #include "gimple-iterator.h"
60 #include "gimple-ssa.h"
61 #include "tree-cfg.h"
62 #include "tree-phinodes.h"
63 #include "ssa-iterators.h"
64 #include "stringpool.h"
65 #include "tree-ssanames.h"
66 #include "diagnostic.h"
67 #include "gimple-pretty-print.h"
68 #include "coverage.h"
69 #include "gcov-io.h"
70 #include "timevar.h"
71 #include "dumpfile.h"
72 #include "profile.h"
73 #include "plugin-api.h"
74 #include "ipa-ref.h"
75 #include "cgraph.h"
76 #include "data-streamer.h"
77 #include "builtins.h"
78 #include "params.h"
79 #include "tree-chkp.h"
81 /* In this file value profile based optimizations are placed. Currently the
82 following optimizations are implemented (for more detailed descriptions
83 see comments at value_profile_transformations):
85 1) Division/modulo specialization. Provided that we can determine that the
86 operands of the division have some special properties, we may use it to
87 produce more effective code.
89 2) Indirect/virtual call specialization. If we can determine most
90 common function callee in indirect/virtual call. We can use this
91 information to improve code effectiveness (especially info for
92 the inliner).
94 3) Speculative prefetching. If we are able to determine that the difference
95 between addresses accessed by a memory reference is usually constant, we
96 may add the prefetch instructions.
97 FIXME: This transformation was removed together with RTL based value
98 profiling.
101 Value profiling internals
102 ==========================
104 Every value profiling transformation starts with defining what values
105 to profile. There are different histogram types (see HIST_TYPE_* in
106 value-prof.h) and each transformation can request one or more histogram
107 types per GIMPLE statement. The function gimple_find_values_to_profile()
108 collects the values to profile in a vec, and adds the number of counters
109 required for the different histogram types.
111 For a -fprofile-generate run, the statements for which values should be
112 recorded, are instrumented in instrument_values(). The instrumentation
113 is done by helper functions that can be found in tree-profile.c, where
114 new types of histograms can be added if necessary.
116 After a -fprofile-use, the value profiling data is read back in by
117 compute_value_histograms() that translates the collected data to
118 histograms and attaches them to the profiled statements via
119 gimple_add_histogram_value(). Histograms are stored in a hash table
120 that is attached to every intrumented function, see VALUE_HISTOGRAMS
121 in function.h.
123 The value-profile transformations driver is the function
124 gimple_value_profile_transformations(). It traverses all statements in
125 the to-be-transformed function, and looks for statements with one or
126 more histograms attached to it. If a statement has histograms, the
127 transformation functions are called on the statement.
129 Limitations / FIXME / TODO:
130 * Only one histogram of each type can be associated with a statement.
131 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
132 (This type of histogram was originally used to implement a form of
133 stride profiling based speculative prefetching to improve SPEC2000
134 scores for memory-bound benchmarks, mcf and equake. However, this
135 was an RTL value-profiling transformation, and those have all been
136 removed.)
137 * Some value profile transformations are done in builtins.c (?!)
138 * Updating of histograms needs some TLC.
139 * The value profiling code could be used to record analysis results
140 from non-profiling (e.g. VRP).
141 * Adding new profilers should be simplified, starting with a cleanup
142 of what-happens-where andwith making gimple_find_values_to_profile
143 and gimple_value_profile_transformations table-driven, perhaps...
146 static tree gimple_divmod_fixed_value (gassign *, tree, int, gcov_type,
147 gcov_type);
148 static tree gimple_mod_pow2 (gassign *, int, gcov_type, gcov_type);
149 static tree gimple_mod_subtract (gassign *, int, int, int, gcov_type,
150 gcov_type, gcov_type);
151 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
152 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
153 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
154 static bool gimple_stringops_transform (gimple_stmt_iterator *);
155 static bool gimple_ic_transform (gimple_stmt_iterator *);
157 /* Allocate histogram value. */
159 histogram_value
160 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
161 enum hist_type type, gimple stmt, tree value)
163 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
164 hist->hvalue.value = value;
165 hist->hvalue.stmt = stmt;
166 hist->type = type;
167 return hist;
170 /* Hash value for histogram. */
172 static hashval_t
173 histogram_hash (const void *x)
175 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
178 /* Return nonzero if statement for histogram_value X is Y. */
180 static int
181 histogram_eq (const void *x, const void *y)
183 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
186 /* Set histogram for STMT. */
188 static void
189 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
191 void **loc;
192 if (!hist && !VALUE_HISTOGRAMS (fun))
193 return;
194 if (!VALUE_HISTOGRAMS (fun))
195 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
196 histogram_eq, NULL);
197 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
198 htab_hash_pointer (stmt),
199 hist ? INSERT : NO_INSERT);
200 if (!hist)
202 if (loc)
203 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
204 return;
206 *loc = hist;
209 /* Get histogram list for STMT. */
211 histogram_value
212 gimple_histogram_value (struct function *fun, gimple stmt)
214 if (!VALUE_HISTOGRAMS (fun))
215 return NULL;
216 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
217 htab_hash_pointer (stmt));
220 /* Add histogram for STMT. */
222 void
223 gimple_add_histogram_value (struct function *fun, gimple stmt,
224 histogram_value hist)
226 hist->hvalue.next = gimple_histogram_value (fun, stmt);
227 set_histogram_value (fun, stmt, hist);
228 hist->fun = fun;
232 /* Remove histogram HIST from STMT's histogram list. */
234 void
235 gimple_remove_histogram_value (struct function *fun, gimple stmt,
236 histogram_value hist)
238 histogram_value hist2 = gimple_histogram_value (fun, stmt);
239 if (hist == hist2)
241 set_histogram_value (fun, stmt, hist->hvalue.next);
243 else
245 while (hist2->hvalue.next != hist)
246 hist2 = hist2->hvalue.next;
247 hist2->hvalue.next = hist->hvalue.next;
249 free (hist->hvalue.counters);
250 #ifdef ENABLE_CHECKING
251 memset (hist, 0xab, sizeof (*hist));
252 #endif
253 free (hist);
257 /* Lookup histogram of type TYPE in the STMT. */
259 histogram_value
260 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
261 enum hist_type type)
263 histogram_value hist;
264 for (hist = gimple_histogram_value (fun, stmt); hist;
265 hist = hist->hvalue.next)
266 if (hist->type == type)
267 return hist;
268 return NULL;
271 /* Dump information about HIST to DUMP_FILE. */
273 static void
274 dump_histogram_value (FILE *dump_file, histogram_value hist)
276 switch (hist->type)
278 case HIST_TYPE_INTERVAL:
279 fprintf (dump_file, "Interval counter range %d -- %d",
280 hist->hdata.intvl.int_start,
281 (hist->hdata.intvl.int_start
282 + hist->hdata.intvl.steps - 1));
283 if (hist->hvalue.counters)
285 unsigned int i;
286 fprintf (dump_file, " [");
287 for (i = 0; i < hist->hdata.intvl.steps; i++)
288 fprintf (dump_file, " %d:%" PRId64,
289 hist->hdata.intvl.int_start + i,
290 (int64_t) hist->hvalue.counters[i]);
291 fprintf (dump_file, " ] outside range:%" PRId64,
292 (int64_t) hist->hvalue.counters[i]);
294 fprintf (dump_file, ".\n");
295 break;
297 case HIST_TYPE_POW2:
298 fprintf (dump_file, "Pow2 counter ");
299 if (hist->hvalue.counters)
301 fprintf (dump_file, "pow2:%" PRId64
302 " nonpow2:%" PRId64,
303 (int64_t) hist->hvalue.counters[0],
304 (int64_t) hist->hvalue.counters[1]);
306 fprintf (dump_file, ".\n");
307 break;
309 case HIST_TYPE_SINGLE_VALUE:
310 fprintf (dump_file, "Single value ");
311 if (hist->hvalue.counters)
313 fprintf (dump_file, "value:%" PRId64
314 " match:%" PRId64
315 " wrong:%" PRId64,
316 (int64_t) hist->hvalue.counters[0],
317 (int64_t) hist->hvalue.counters[1],
318 (int64_t) hist->hvalue.counters[2]);
320 fprintf (dump_file, ".\n");
321 break;
323 case HIST_TYPE_AVERAGE:
324 fprintf (dump_file, "Average value ");
325 if (hist->hvalue.counters)
327 fprintf (dump_file, "sum:%" PRId64
328 " times:%" PRId64,
329 (int64_t) hist->hvalue.counters[0],
330 (int64_t) hist->hvalue.counters[1]);
332 fprintf (dump_file, ".\n");
333 break;
335 case HIST_TYPE_IOR:
336 fprintf (dump_file, "IOR value ");
337 if (hist->hvalue.counters)
339 fprintf (dump_file, "ior:%" PRId64,
340 (int64_t) hist->hvalue.counters[0]);
342 fprintf (dump_file, ".\n");
343 break;
345 case HIST_TYPE_CONST_DELTA:
346 fprintf (dump_file, "Constant delta ");
347 if (hist->hvalue.counters)
349 fprintf (dump_file, "value:%" PRId64
350 " match:%" PRId64
351 " wrong:%" PRId64,
352 (int64_t) hist->hvalue.counters[0],
353 (int64_t) hist->hvalue.counters[1],
354 (int64_t) hist->hvalue.counters[2]);
356 fprintf (dump_file, ".\n");
357 break;
358 case HIST_TYPE_INDIR_CALL:
359 fprintf (dump_file, "Indirect call ");
360 if (hist->hvalue.counters)
362 fprintf (dump_file, "value:%" PRId64
363 " match:%" PRId64
364 " all:%" PRId64,
365 (int64_t) hist->hvalue.counters[0],
366 (int64_t) hist->hvalue.counters[1],
367 (int64_t) hist->hvalue.counters[2]);
369 fprintf (dump_file, ".\n");
370 break;
371 case HIST_TYPE_TIME_PROFILE:
372 fprintf (dump_file, "Time profile ");
373 if (hist->hvalue.counters)
375 fprintf (dump_file, "time:%" PRId64,
376 (int64_t) hist->hvalue.counters[0]);
378 fprintf (dump_file, ".\n");
379 break;
380 case HIST_TYPE_INDIR_CALL_TOPN:
381 fprintf (dump_file, "Indirect call topn ");
382 if (hist->hvalue.counters)
384 int i;
386 fprintf (dump_file, "accu:%" PRId64, hist->hvalue.counters[0]);
387 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
389 fprintf (dump_file, " target:%" PRId64 " value:%" PRId64,
390 (int64_t) hist->hvalue.counters[i],
391 (int64_t) hist->hvalue.counters[i+1]);
394 fprintf (dump_file, ".\n");
395 break;
396 case HIST_TYPE_MAX:
397 gcc_unreachable ();
401 /* Dump information about HIST to DUMP_FILE. */
403 void
404 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
406 struct bitpack_d bp;
407 unsigned int i;
409 bp = bitpack_create (ob->main_stream);
410 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
411 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
412 streamer_write_bitpack (&bp);
413 switch (hist->type)
415 case HIST_TYPE_INTERVAL:
416 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
417 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
418 break;
419 default:
420 break;
422 for (i = 0; i < hist->n_counters; i++)
423 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
424 if (hist->hvalue.next)
425 stream_out_histogram_value (ob, hist->hvalue.next);
427 /* Dump information about HIST to DUMP_FILE. */
429 void
430 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
432 enum hist_type type;
433 unsigned int ncounters = 0;
434 struct bitpack_d bp;
435 unsigned int i;
436 histogram_value new_val;
437 bool next;
438 histogram_value *next_p = NULL;
442 bp = streamer_read_bitpack (ib);
443 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
444 next = bp_unpack_value (&bp, 1);
445 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
446 switch (type)
448 case HIST_TYPE_INTERVAL:
449 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
450 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
451 ncounters = new_val->hdata.intvl.steps + 2;
452 break;
454 case HIST_TYPE_POW2:
455 case HIST_TYPE_AVERAGE:
456 ncounters = 2;
457 break;
459 case HIST_TYPE_SINGLE_VALUE:
460 case HIST_TYPE_INDIR_CALL:
461 ncounters = 3;
462 break;
464 case HIST_TYPE_CONST_DELTA:
465 ncounters = 4;
466 break;
468 case HIST_TYPE_IOR:
469 case HIST_TYPE_TIME_PROFILE:
470 ncounters = 1;
471 break;
473 case HIST_TYPE_INDIR_CALL_TOPN:
474 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
475 break;
477 case HIST_TYPE_MAX:
478 gcc_unreachable ();
480 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
481 new_val->n_counters = ncounters;
482 for (i = 0; i < ncounters; i++)
483 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
484 if (!next_p)
485 gimple_add_histogram_value (cfun, stmt, new_val);
486 else
487 *next_p = new_val;
488 next_p = &new_val->hvalue.next;
490 while (next);
493 /* Dump all histograms attached to STMT to DUMP_FILE. */
495 void
496 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
498 histogram_value hist;
499 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
500 dump_histogram_value (dump_file, hist);
503 /* Remove all histograms associated with STMT. */
505 void
506 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
508 histogram_value val;
509 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
510 gimple_remove_histogram_value (fun, stmt, val);
513 /* Duplicate all histograms associates with OSTMT to STMT. */
515 void
516 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
517 struct function *ofun, gimple ostmt)
519 histogram_value val;
520 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
522 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
523 memcpy (new_val, val, sizeof (*val));
524 new_val->hvalue.stmt = stmt;
525 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
526 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
527 gimple_add_histogram_value (fun, stmt, new_val);
532 /* Move all histograms associated with OSTMT to STMT. */
534 void
535 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
537 histogram_value val = gimple_histogram_value (fun, ostmt);
538 if (val)
540 /* The following three statements can't be reordered,
541 because histogram hashtab relies on stmt field value
542 for finding the exact slot. */
543 set_histogram_value (fun, ostmt, NULL);
544 for (; val != NULL; val = val->hvalue.next)
545 val->hvalue.stmt = stmt;
546 set_histogram_value (fun, stmt, val);
550 static bool error_found = false;
552 /* Helper function for verify_histograms. For each histogram reachable via htab
553 walk verify that it was reached via statement walk. */
555 static int
556 visit_hist (void **slot, void *data)
558 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
559 histogram_value hist = *(histogram_value *) slot;
561 if (!visited->contains (hist)
562 && hist->type != HIST_TYPE_TIME_PROFILE)
564 error ("dead histogram");
565 dump_histogram_value (stderr, hist);
566 debug_gimple_stmt (hist->hvalue.stmt);
567 error_found = true;
569 return 1;
573 /* Verify sanity of the histograms. */
575 DEBUG_FUNCTION void
576 verify_histograms (void)
578 basic_block bb;
579 gimple_stmt_iterator gsi;
580 histogram_value hist;
582 error_found = false;
583 hash_set<histogram_value> visited_hists;
584 FOR_EACH_BB_FN (bb, cfun)
585 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
587 gimple stmt = gsi_stmt (gsi);
589 for (hist = gimple_histogram_value (cfun, stmt); hist;
590 hist = hist->hvalue.next)
592 if (hist->hvalue.stmt != stmt)
594 error ("Histogram value statement does not correspond to "
595 "the statement it is associated with");
596 debug_gimple_stmt (stmt);
597 dump_histogram_value (stderr, hist);
598 error_found = true;
600 visited_hists.add (hist);
603 if (VALUE_HISTOGRAMS (cfun))
604 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
605 if (error_found)
606 internal_error ("verify_histograms failed");
609 /* Helper function for verify_histograms. For each histogram reachable via htab
610 walk verify that it was reached via statement walk. */
612 static int
613 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
615 histogram_value hist = *(histogram_value *) slot;
616 free (hist->hvalue.counters);
617 #ifdef ENABLE_CHECKING
618 memset (hist, 0xab, sizeof (*hist));
619 #endif
620 free (hist);
621 return 1;
624 void
625 free_histograms (void)
627 if (VALUE_HISTOGRAMS (cfun))
629 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
630 htab_delete (VALUE_HISTOGRAMS (cfun));
631 VALUE_HISTOGRAMS (cfun) = NULL;
636 /* The overall number of invocations of the counter should match
637 execution count of basic block. Report it as error rather than
638 internal error as it might mean that user has misused the profile
639 somehow. */
641 static bool
642 check_counter (gimple stmt, const char * name,
643 gcov_type *count, gcov_type *all, gcov_type bb_count)
645 if (*all != bb_count || *count > *all)
647 location_t locus;
648 locus = (stmt != NULL)
649 ? gimple_location (stmt)
650 : DECL_SOURCE_LOCATION (current_function_decl);
651 if (flag_profile_correction)
653 if (dump_enabled_p ())
654 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
655 "correcting inconsistent value profile: %s "
656 "profiler overall count (%d) does not match BB "
657 "count (%d)\n", name, (int)*all, (int)bb_count);
658 *all = bb_count;
659 if (*count > *all)
660 *count = *all;
661 return false;
663 else
665 error_at (locus, "corrupted value profile: %s "
666 "profile counter (%d out of %d) inconsistent with "
667 "basic-block count (%d)",
668 name,
669 (int) *count,
670 (int) *all,
671 (int) bb_count);
672 return true;
676 return false;
680 /* GIMPLE based transformations. */
682 bool
683 gimple_value_profile_transformations (void)
685 basic_block bb;
686 gimple_stmt_iterator gsi;
687 bool changed = false;
689 FOR_EACH_BB_FN (bb, cfun)
691 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
693 gimple stmt = gsi_stmt (gsi);
694 histogram_value th = gimple_histogram_value (cfun, stmt);
695 if (!th)
696 continue;
698 if (dump_file)
700 fprintf (dump_file, "Trying transformations on stmt ");
701 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
702 dump_histograms_for_stmt (cfun, dump_file, stmt);
705 /* Transformations: */
706 /* The order of things in this conditional controls which
707 transformation is used when more than one is applicable. */
708 /* It is expected that any code added by the transformations
709 will be added before the current statement, and that the
710 current statement remain valid (although possibly
711 modified) upon return. */
712 if (gimple_mod_subtract_transform (&gsi)
713 || gimple_divmod_fixed_value_transform (&gsi)
714 || gimple_mod_pow2_value_transform (&gsi)
715 || gimple_stringops_transform (&gsi)
716 || gimple_ic_transform (&gsi))
718 stmt = gsi_stmt (gsi);
719 changed = true;
720 /* Original statement may no longer be in the same block. */
721 if (bb != gimple_bb (stmt))
723 bb = gimple_bb (stmt);
724 gsi = gsi_for_stmt (stmt);
730 if (changed)
732 counts_to_freqs ();
735 return changed;
739 /* Generate code for transformation 1 (with parent gimple assignment
740 STMT and probability of taking the optimal path PROB, which is
741 equivalent to COUNT/ALL within roundoff error). This generates the
742 result into a temp and returns the temp; it does not replace or
743 alter the original STMT. */
745 static tree
746 gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
747 gcov_type count, gcov_type all)
749 gassign *stmt1, *stmt2;
750 gcond *stmt3;
751 tree tmp0, tmp1, tmp2;
752 gimple bb1end, bb2end, bb3end;
753 basic_block bb, bb2, bb3, bb4;
754 tree optype, op1, op2;
755 edge e12, e13, e23, e24, e34;
756 gimple_stmt_iterator gsi;
758 gcc_assert (is_gimple_assign (stmt)
759 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
760 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
762 optype = TREE_TYPE (gimple_assign_lhs (stmt));
763 op1 = gimple_assign_rhs1 (stmt);
764 op2 = gimple_assign_rhs2 (stmt);
766 bb = gimple_bb (stmt);
767 gsi = gsi_for_stmt (stmt);
769 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
770 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
771 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
772 stmt2 = gimple_build_assign (tmp1, op2);
773 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
774 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
775 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
776 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
777 bb1end = stmt3;
779 tmp2 = create_tmp_reg (optype, "PROF");
780 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
781 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
782 bb2end = stmt1;
784 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
785 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
786 bb3end = stmt1;
788 /* Fix CFG. */
789 /* Edge e23 connects bb2 to bb3, etc. */
790 e12 = split_block (bb, bb1end);
791 bb2 = e12->dest;
792 bb2->count = count;
793 e23 = split_block (bb2, bb2end);
794 bb3 = e23->dest;
795 bb3->count = all - count;
796 e34 = split_block (bb3, bb3end);
797 bb4 = e34->dest;
798 bb4->count = all;
800 e12->flags &= ~EDGE_FALLTHRU;
801 e12->flags |= EDGE_FALSE_VALUE;
802 e12->probability = prob;
803 e12->count = count;
805 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
806 e13->probability = REG_BR_PROB_BASE - prob;
807 e13->count = all - count;
809 remove_edge (e23);
811 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
812 e24->probability = REG_BR_PROB_BASE;
813 e24->count = count;
815 e34->probability = REG_BR_PROB_BASE;
816 e34->count = all - count;
818 return tmp2;
822 /* Do transform 1) on INSN if applicable. */
824 static bool
825 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
827 histogram_value histogram;
828 enum tree_code code;
829 gcov_type val, count, all;
830 tree result, value, tree_val;
831 gcov_type prob;
832 gassign *stmt;
834 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
835 if (!stmt)
836 return false;
838 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
839 return false;
841 code = gimple_assign_rhs_code (stmt);
843 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
844 return false;
846 histogram = gimple_histogram_value_of_type (cfun, stmt,
847 HIST_TYPE_SINGLE_VALUE);
848 if (!histogram)
849 return false;
851 value = histogram->hvalue.value;
852 val = histogram->hvalue.counters[0];
853 count = histogram->hvalue.counters[1];
854 all = histogram->hvalue.counters[2];
855 gimple_remove_histogram_value (cfun, stmt, histogram);
857 /* We require that count is at least half of all; this means
858 that for the transformation to fire the value must be constant
859 at least 50% of time (and 75% gives the guarantee of usage). */
860 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
861 || 2 * count < all
862 || optimize_bb_for_size_p (gimple_bb (stmt)))
863 return false;
865 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
866 return false;
868 /* Compute probability of taking the optimal path. */
869 if (all > 0)
870 prob = GCOV_COMPUTE_SCALE (count, all);
871 else
872 prob = 0;
874 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
875 tree_val = build_int_cst (get_gcov_type (), val);
876 else
878 HOST_WIDE_INT a[2];
879 a[0] = (unsigned HOST_WIDE_INT) val;
880 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
882 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
883 TYPE_PRECISION (get_gcov_type ()), false));
885 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
887 if (dump_file)
889 fprintf (dump_file, "Div/mod by constant ");
890 print_generic_expr (dump_file, value, TDF_SLIM);
891 fprintf (dump_file, "=");
892 print_generic_expr (dump_file, tree_val, TDF_SLIM);
893 fprintf (dump_file, " transformation on insn ");
894 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
897 gimple_assign_set_rhs_from_tree (si, result);
898 update_stmt (gsi_stmt (*si));
900 return true;
903 /* Generate code for transformation 2 (with parent gimple assign STMT and
904 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
905 within roundoff error). This generates the result into a temp and returns
906 the temp; it does not replace or alter the original STMT. */
907 static tree
908 gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
910 gassign *stmt1, *stmt2, *stmt3;
911 gcond *stmt4;
912 tree tmp2, tmp3;
913 gimple bb1end, bb2end, bb3end;
914 basic_block bb, bb2, bb3, bb4;
915 tree optype, op1, op2;
916 edge e12, e13, e23, e24, e34;
917 gimple_stmt_iterator gsi;
918 tree result;
920 gcc_assert (is_gimple_assign (stmt)
921 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
923 optype = TREE_TYPE (gimple_assign_lhs (stmt));
924 op1 = gimple_assign_rhs1 (stmt);
925 op2 = gimple_assign_rhs2 (stmt);
927 bb = gimple_bb (stmt);
928 gsi = gsi_for_stmt (stmt);
930 result = create_tmp_reg (optype, "PROF");
931 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
932 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
933 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
934 build_int_cst (optype, -1));
935 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
936 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
937 NULL_TREE, NULL_TREE);
938 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
939 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
940 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
941 bb1end = stmt4;
943 /* tmp2 == op2-1 inherited from previous block. */
944 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
945 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
946 bb2end = stmt1;
948 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
949 op1, op2);
950 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
951 bb3end = stmt1;
953 /* Fix CFG. */
954 /* Edge e23 connects bb2 to bb3, etc. */
955 e12 = split_block (bb, bb1end);
956 bb2 = e12->dest;
957 bb2->count = count;
958 e23 = split_block (bb2, bb2end);
959 bb3 = e23->dest;
960 bb3->count = all - count;
961 e34 = split_block (bb3, bb3end);
962 bb4 = e34->dest;
963 bb4->count = all;
965 e12->flags &= ~EDGE_FALLTHRU;
966 e12->flags |= EDGE_FALSE_VALUE;
967 e12->probability = prob;
968 e12->count = count;
970 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
971 e13->probability = REG_BR_PROB_BASE - prob;
972 e13->count = all - count;
974 remove_edge (e23);
976 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
977 e24->probability = REG_BR_PROB_BASE;
978 e24->count = count;
980 e34->probability = REG_BR_PROB_BASE;
981 e34->count = all - count;
983 return result;
986 /* Do transform 2) on INSN if applicable. */
987 static bool
988 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
990 histogram_value histogram;
991 enum tree_code code;
992 gcov_type count, wrong_values, all;
993 tree lhs_type, result, value;
994 gcov_type prob;
995 gassign *stmt;
997 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
998 if (!stmt)
999 return false;
1001 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1002 if (!INTEGRAL_TYPE_P (lhs_type))
1003 return false;
1005 code = gimple_assign_rhs_code (stmt);
1007 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1008 return false;
1010 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
1011 if (!histogram)
1012 return false;
1014 value = histogram->hvalue.value;
1015 wrong_values = histogram->hvalue.counters[0];
1016 count = histogram->hvalue.counters[1];
1018 gimple_remove_histogram_value (cfun, stmt, histogram);
1020 /* We require that we hit a power of 2 at least half of all evaluations. */
1021 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
1022 || count < wrong_values
1023 || optimize_bb_for_size_p (gimple_bb (stmt)))
1024 return false;
1026 if (dump_file)
1028 fprintf (dump_file, "Mod power of 2 transformation on insn ");
1029 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1032 /* Compute probability of taking the optimal path. */
1033 all = count + wrong_values;
1035 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
1036 return false;
1038 if (all > 0)
1039 prob = GCOV_COMPUTE_SCALE (count, all);
1040 else
1041 prob = 0;
1043 result = gimple_mod_pow2 (stmt, prob, count, all);
1045 gimple_assign_set_rhs_from_tree (si, result);
1046 update_stmt (gsi_stmt (*si));
1048 return true;
1051 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1052 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1053 supported and this is built into this interface. The probabilities of taking
1054 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1055 COUNT2/ALL respectively within roundoff error). This generates the
1056 result into a temp and returns the temp; it does not replace or alter
1057 the original STMT. */
1058 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1060 static tree
1061 gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
1062 gcov_type count1, gcov_type count2, gcov_type all)
1064 gassign *stmt1;
1065 gimple stmt2;
1066 gcond *stmt3;
1067 tree tmp1;
1068 gimple bb1end, bb2end = NULL, bb3end;
1069 basic_block bb, bb2, bb3, bb4;
1070 tree optype, op1, op2;
1071 edge e12, e23 = 0, e24, e34, e14;
1072 gimple_stmt_iterator gsi;
1073 tree result;
1075 gcc_assert (is_gimple_assign (stmt)
1076 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1078 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1079 op1 = gimple_assign_rhs1 (stmt);
1080 op2 = gimple_assign_rhs2 (stmt);
1082 bb = gimple_bb (stmt);
1083 gsi = gsi_for_stmt (stmt);
1085 result = create_tmp_reg (optype, "PROF");
1086 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1087 stmt1 = gimple_build_assign (result, op1);
1088 stmt2 = gimple_build_assign (tmp1, op2);
1089 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1090 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1091 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1092 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1093 bb1end = stmt3;
1095 if (ncounts) /* Assumed to be 0 or 1 */
1097 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1098 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1099 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1100 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1101 bb2end = stmt2;
1104 /* Fallback case. */
1105 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1106 result, tmp1);
1107 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1108 bb3end = stmt1;
1110 /* Fix CFG. */
1111 /* Edge e23 connects bb2 to bb3, etc. */
1112 /* However block 3 is optional; if it is not there, references
1113 to 3 really refer to block 2. */
1114 e12 = split_block (bb, bb1end);
1115 bb2 = e12->dest;
1116 bb2->count = all - count1;
1118 if (ncounts) /* Assumed to be 0 or 1. */
1120 e23 = split_block (bb2, bb2end);
1121 bb3 = e23->dest;
1122 bb3->count = all - count1 - count2;
1125 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1126 bb4 = e34->dest;
1127 bb4->count = all;
1129 e12->flags &= ~EDGE_FALLTHRU;
1130 e12->flags |= EDGE_FALSE_VALUE;
1131 e12->probability = REG_BR_PROB_BASE - prob1;
1132 e12->count = all - count1;
1134 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1135 e14->probability = prob1;
1136 e14->count = count1;
1138 if (ncounts) /* Assumed to be 0 or 1. */
1140 e23->flags &= ~EDGE_FALLTHRU;
1141 e23->flags |= EDGE_FALSE_VALUE;
1142 e23->count = all - count1 - count2;
1143 e23->probability = REG_BR_PROB_BASE - prob2;
1145 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1146 e24->probability = prob2;
1147 e24->count = count2;
1150 e34->probability = REG_BR_PROB_BASE;
1151 e34->count = all - count1 - count2;
1153 return result;
1157 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1159 static bool
1160 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1162 histogram_value histogram;
1163 enum tree_code code;
1164 gcov_type count, wrong_values, all;
1165 tree lhs_type, result;
1166 gcov_type prob1, prob2;
1167 unsigned int i, steps;
1168 gcov_type count1, count2;
1169 gassign *stmt;
1171 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1172 if (!stmt)
1173 return false;
1175 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1176 if (!INTEGRAL_TYPE_P (lhs_type))
1177 return false;
1179 code = gimple_assign_rhs_code (stmt);
1181 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1182 return false;
1184 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1185 if (!histogram)
1186 return false;
1188 all = 0;
1189 wrong_values = 0;
1190 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1191 all += histogram->hvalue.counters[i];
1193 wrong_values += histogram->hvalue.counters[i];
1194 wrong_values += histogram->hvalue.counters[i+1];
1195 steps = histogram->hdata.intvl.steps;
1196 all += wrong_values;
1197 count1 = histogram->hvalue.counters[0];
1198 count2 = histogram->hvalue.counters[1];
1200 /* Compute probability of taking the optimal path. */
1201 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1203 gimple_remove_histogram_value (cfun, stmt, histogram);
1204 return false;
1207 if (flag_profile_correction && count1 + count2 > all)
1208 all = count1 + count2;
1210 gcc_assert (count1 + count2 <= all);
1212 /* We require that we use just subtractions in at least 50% of all
1213 evaluations. */
1214 count = 0;
1215 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1217 count += histogram->hvalue.counters[i];
1218 if (count * 2 >= all)
1219 break;
1221 if (i == steps
1222 || optimize_bb_for_size_p (gimple_bb (stmt)))
1223 return false;
1225 gimple_remove_histogram_value (cfun, stmt, histogram);
1226 if (dump_file)
1228 fprintf (dump_file, "Mod subtract transformation on insn ");
1229 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1232 /* Compute probability of taking the optimal path(s). */
1233 if (all > 0)
1235 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1236 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1238 else
1240 prob1 = prob2 = 0;
1243 /* In practice, "steps" is always 2. This interface reflects this,
1244 and will need to be changed if "steps" can change. */
1245 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1247 gimple_assign_set_rhs_from_tree (si, result);
1248 update_stmt (gsi_stmt (*si));
1250 return true;
1253 struct profile_id_traits : default_hashmap_traits
1255 template<typename T>
1256 static bool
1257 is_deleted (T &e)
1259 return e.m_key == UINT_MAX;
1262 template<typename T> static bool is_empty (T &e) { return e.m_key == 0; }
1263 template<typename T> static void mark_deleted (T &e) { e.m_key = UINT_MAX; }
1264 template<typename T> static void mark_empty (T &e) { e.m_key = 0; }
1267 static hash_map<unsigned int, cgraph_node *, profile_id_traits> *
1268 cgraph_node_map = 0;
1270 /* Returns true if node graph is initialized. This
1271 is used to test if profile_id has been created
1272 for cgraph_nodes. */
1274 bool
1275 coverage_node_map_initialized_p (void)
1277 return cgraph_node_map != 0;
1280 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1281 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1282 that the PROFILE_IDs was already assigned. */
1284 void
1285 init_node_map (bool local)
1287 struct cgraph_node *n;
1288 cgraph_node_map
1289 = new hash_map<unsigned int, cgraph_node *, profile_id_traits>;
1291 FOR_EACH_DEFINED_FUNCTION (n)
1292 if (n->has_gimple_body_p ())
1294 cgraph_node **val;
1295 if (local)
1297 n->profile_id = coverage_compute_profile_id (n);
1298 while ((val = cgraph_node_map->get (n->profile_id))
1299 || !n->profile_id)
1301 if (dump_file)
1302 fprintf (dump_file, "Local profile-id %i conflict"
1303 " with nodes %s/%i %s/%i\n",
1304 n->profile_id,
1305 n->name (),
1306 n->order,
1307 (*val)->name (),
1308 (*val)->order);
1309 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1312 else if (!n->profile_id)
1314 if (dump_file)
1315 fprintf (dump_file,
1316 "Node %s/%i has no profile-id"
1317 " (profile feedback missing?)\n",
1318 n->name (),
1319 n->order);
1320 continue;
1322 else if ((val = cgraph_node_map->get (n->profile_id)))
1324 if (dump_file)
1325 fprintf (dump_file,
1326 "Node %s/%i has IP profile-id %i conflict. "
1327 "Giving up.\n",
1328 n->name (),
1329 n->order,
1330 n->profile_id);
1331 *val = NULL;
1332 continue;
1334 cgraph_node_map->put (n->profile_id, n);
1338 /* Delete the CGRAPH_NODE_MAP. */
1340 void
1341 del_node_map (void)
1343 delete cgraph_node_map;
1346 /* Return cgraph node for function with pid */
1348 struct cgraph_node*
1349 find_func_by_profile_id (int profile_id)
1351 cgraph_node **val = cgraph_node_map->get (profile_id);
1352 if (val)
1353 return *val;
1354 else
1355 return NULL;
1358 /* Perform sanity check on the indirect call target. Due to race conditions,
1359 false function target may be attributed to an indirect call site. If the
1360 call expression type mismatches with the target function's type, expand_call
1361 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1362 Returns true if TARGET is considered ok for call CALL_STMT. */
1364 bool
1365 check_ic_target (gcall *call_stmt, struct cgraph_node *target)
1367 location_t locus;
1368 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1369 return true;
1371 locus = gimple_location (call_stmt);
1372 if (dump_enabled_p ())
1373 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1374 "Skipping target %s with mismatching types for icall\n",
1375 target->name ());
1376 return false;
1379 /* Do transformation
1381 if (actual_callee_address == address_of_most_common_function/method)
1382 do direct call
1383 else
1384 old call
1387 gcall *
1388 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1389 int prob, gcov_type count, gcov_type all)
1391 gcall *dcall_stmt;
1392 gassign *load_stmt;
1393 gcond *cond_stmt;
1394 gcall *iretbnd_stmt = NULL;
1395 tree tmp0, tmp1, tmp;
1396 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1397 tree optype = build_pointer_type (void_type_node);
1398 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1399 gimple_stmt_iterator gsi;
1400 int lp_nr, dflags;
1401 edge e_eh, e;
1402 edge_iterator ei;
1403 gimple_stmt_iterator psi;
1405 cond_bb = gimple_bb (icall_stmt);
1406 gsi = gsi_for_stmt (icall_stmt);
1408 if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
1409 iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
1411 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1412 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1413 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1414 load_stmt = gimple_build_assign (tmp0, tmp);
1415 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1417 tmp = fold_convert (optype, build_addr (direct_call->decl,
1418 current_function_decl));
1419 load_stmt = gimple_build_assign (tmp1, tmp);
1420 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1422 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1423 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1425 gimple_set_vdef (icall_stmt, NULL_TREE);
1426 gimple_set_vuse (icall_stmt, NULL_TREE);
1427 update_stmt (icall_stmt);
1428 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1429 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1430 dflags = flags_from_decl_or_type (direct_call->decl);
1431 if ((dflags & ECF_NORETURN) != 0)
1432 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1433 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1435 /* Fix CFG. */
1436 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1437 e_cd = split_block (cond_bb, cond_stmt);
1438 dcall_bb = e_cd->dest;
1439 dcall_bb->count = count;
1441 e_di = split_block (dcall_bb, dcall_stmt);
1442 icall_bb = e_di->dest;
1443 icall_bb->count = all - count;
1445 /* Do not disturb existing EH edges from the indirect call. */
1446 if (!stmt_ends_bb_p (icall_stmt))
1447 e_ij = split_block (icall_bb, icall_stmt);
1448 else
1450 e_ij = find_fallthru_edge (icall_bb->succs);
1451 /* The indirect call might be noreturn. */
1452 if (e_ij != NULL)
1454 e_ij->probability = REG_BR_PROB_BASE;
1455 e_ij->count = all - count;
1456 e_ij = single_pred_edge (split_edge (e_ij));
1459 if (e_ij != NULL)
1461 join_bb = e_ij->dest;
1462 join_bb->count = all;
1465 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1466 e_cd->probability = prob;
1467 e_cd->count = count;
1469 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1470 e_ci->probability = REG_BR_PROB_BASE - prob;
1471 e_ci->count = all - count;
1473 remove_edge (e_di);
1475 if (e_ij != NULL)
1477 if ((dflags & ECF_NORETURN) != 0)
1478 e_ij->count = all;
1479 else
1481 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1482 e_dj->probability = REG_BR_PROB_BASE;
1483 e_dj->count = count;
1485 e_ij->count = all - count;
1487 e_ij->probability = REG_BR_PROB_BASE;
1490 /* Insert PHI node for the call result if necessary. */
1491 if (gimple_call_lhs (icall_stmt)
1492 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1493 && (dflags & ECF_NORETURN) == 0)
1495 tree result = gimple_call_lhs (icall_stmt);
1496 gphi *phi = create_phi_node (result, join_bb);
1497 gimple_call_set_lhs (icall_stmt,
1498 duplicate_ssa_name (result, icall_stmt));
1499 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1500 gimple_call_set_lhs (dcall_stmt,
1501 duplicate_ssa_name (result, dcall_stmt));
1502 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1504 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1505 call then we need to make it's copy for the direct
1506 call. */
1507 if (iretbnd_stmt)
1509 if (gimple_call_lhs (iretbnd_stmt))
1511 gimple copy;
1513 gimple_set_vdef (iretbnd_stmt, NULL_TREE);
1514 gimple_set_vuse (iretbnd_stmt, NULL_TREE);
1515 update_stmt (iretbnd_stmt);
1517 result = gimple_call_lhs (iretbnd_stmt);
1518 phi = create_phi_node (result, join_bb);
1520 copy = gimple_copy (iretbnd_stmt);
1521 gimple_call_set_arg (copy, 0,
1522 gimple_call_lhs (dcall_stmt));
1523 gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
1524 gsi_insert_on_edge (e_dj, copy);
1525 add_phi_arg (phi, gimple_call_lhs (copy),
1526 e_dj, UNKNOWN_LOCATION);
1528 gimple_call_set_arg (iretbnd_stmt, 0,
1529 gimple_call_lhs (icall_stmt));
1530 gimple_call_set_lhs (iretbnd_stmt,
1531 duplicate_ssa_name (result, iretbnd_stmt));
1532 psi = gsi_for_stmt (iretbnd_stmt);
1533 gsi_remove (&psi, false);
1534 gsi_insert_on_edge (e_ij, iretbnd_stmt);
1535 add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
1536 e_ij, UNKNOWN_LOCATION);
1538 gsi_commit_one_edge_insert (e_dj, NULL);
1539 gsi_commit_one_edge_insert (e_ij, NULL);
1541 else
1543 psi = gsi_for_stmt (iretbnd_stmt);
1544 gsi_remove (&psi, true);
1549 /* Build an EH edge for the direct call if necessary. */
1550 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1551 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1553 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1556 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1557 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1559 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1560 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1561 !gsi_end_p (psi); gsi_next (&psi))
1563 gphi *phi = psi.phi ();
1564 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1565 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1568 if (!stmt_could_throw_p (dcall_stmt))
1569 gimple_purge_dead_eh_edges (dcall_bb);
1570 return dcall_stmt;
1574 For every checked indirect/virtual call determine if most common pid of
1575 function/class method has probability more than 50%. If yes modify code of
1576 this call to:
1579 static bool
1580 gimple_ic_transform (gimple_stmt_iterator *gsi)
1582 gcall *stmt;
1583 histogram_value histogram;
1584 gcov_type val, count, all, bb_all;
1585 struct cgraph_node *direct_call;
1587 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1588 if (!stmt)
1589 return false;
1591 if (gimple_call_fndecl (stmt) != NULL_TREE)
1592 return false;
1594 if (gimple_call_internal_p (stmt))
1595 return false;
1597 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1598 if (!histogram)
1599 return false;
1601 val = histogram->hvalue.counters [0];
1602 count = histogram->hvalue.counters [1];
1603 all = histogram->hvalue.counters [2];
1605 bb_all = gimple_bb (stmt)->count;
1606 /* The order of CHECK_COUNTER calls is important -
1607 since check_counter can correct the third parameter
1608 and we want to make count <= all <= bb_all. */
1609 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1610 || check_counter (stmt, "ic", &count, &all, all))
1612 gimple_remove_histogram_value (cfun, stmt, histogram);
1613 return false;
1616 if (4 * count <= 3 * all)
1617 return false;
1619 direct_call = find_func_by_profile_id ((int)val);
1621 if (direct_call == NULL)
1623 if (val)
1625 if (dump_file)
1627 fprintf (dump_file, "Indirect call -> direct call from other module");
1628 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1629 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1632 return false;
1635 if (!check_ic_target (stmt, direct_call))
1637 if (dump_file)
1639 fprintf (dump_file, "Indirect call -> direct call ");
1640 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1641 fprintf (dump_file, "=> ");
1642 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1643 fprintf (dump_file, " transformation skipped because of type mismatch");
1644 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1646 gimple_remove_histogram_value (cfun, stmt, histogram);
1647 return false;
1650 if (dump_file)
1652 fprintf (dump_file, "Indirect call -> direct call ");
1653 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1654 fprintf (dump_file, "=> ");
1655 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1656 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1657 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1658 fprintf (dump_file, "hist->count %" PRId64
1659 " hist->all %" PRId64"\n", count, all);
1662 return true;
1665 /* Return true if the stringop CALL with FNDECL shall be profiled.
1666 SIZE_ARG be set to the argument index for the size of the string
1667 operation.
1669 static bool
1670 interesting_stringop_to_profile_p (tree fndecl, gcall *call, int *size_arg)
1672 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1674 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1675 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1676 return false;
1678 switch (fcode)
1680 case BUILT_IN_MEMCPY:
1681 case BUILT_IN_MEMPCPY:
1682 *size_arg = 2;
1683 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1684 INTEGER_TYPE, VOID_TYPE);
1685 case BUILT_IN_MEMSET:
1686 *size_arg = 2;
1687 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1688 INTEGER_TYPE, VOID_TYPE);
1689 case BUILT_IN_BZERO:
1690 *size_arg = 1;
1691 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1692 VOID_TYPE);
1693 default:
1694 gcc_unreachable ();
1698 /* Convert stringop (..., vcall_size)
1699 into
1700 if (vcall_size == icall_size)
1701 stringop (..., icall_size);
1702 else
1703 stringop (..., vcall_size);
1704 assuming we'll propagate a true constant into ICALL_SIZE later. */
1706 static void
1707 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
1708 gcov_type count, gcov_type all)
1710 gassign *tmp_stmt;
1711 gcond *cond_stmt;
1712 gcall *icall_stmt;
1713 tree tmp0, tmp1, vcall_size, optype;
1714 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1715 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1716 gimple_stmt_iterator gsi;
1717 tree fndecl;
1718 int size_arg;
1720 fndecl = gimple_call_fndecl (vcall_stmt);
1721 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1722 gcc_unreachable ();
1724 cond_bb = gimple_bb (vcall_stmt);
1725 gsi = gsi_for_stmt (vcall_stmt);
1727 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1728 optype = TREE_TYPE (vcall_size);
1730 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1731 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1732 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1733 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1735 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1736 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1738 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1739 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1741 gimple_set_vdef (vcall_stmt, NULL);
1742 gimple_set_vuse (vcall_stmt, NULL);
1743 update_stmt (vcall_stmt);
1744 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1745 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1746 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1748 /* Fix CFG. */
1749 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1750 e_ci = split_block (cond_bb, cond_stmt);
1751 icall_bb = e_ci->dest;
1752 icall_bb->count = count;
1754 e_iv = split_block (icall_bb, icall_stmt);
1755 vcall_bb = e_iv->dest;
1756 vcall_bb->count = all - count;
1758 e_vj = split_block (vcall_bb, vcall_stmt);
1759 join_bb = e_vj->dest;
1760 join_bb->count = all;
1762 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1763 e_ci->probability = prob;
1764 e_ci->count = count;
1766 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1767 e_cv->probability = REG_BR_PROB_BASE - prob;
1768 e_cv->count = all - count;
1770 remove_edge (e_iv);
1772 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1773 e_ij->probability = REG_BR_PROB_BASE;
1774 e_ij->count = count;
1776 e_vj->probability = REG_BR_PROB_BASE;
1777 e_vj->count = all - count;
1779 /* Insert PHI node for the call result if necessary. */
1780 if (gimple_call_lhs (vcall_stmt)
1781 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1783 tree result = gimple_call_lhs (vcall_stmt);
1784 gphi *phi = create_phi_node (result, join_bb);
1785 gimple_call_set_lhs (vcall_stmt,
1786 duplicate_ssa_name (result, vcall_stmt));
1787 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1788 gimple_call_set_lhs (icall_stmt,
1789 duplicate_ssa_name (result, icall_stmt));
1790 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1793 /* Because these are all string op builtins, they're all nothrow. */
1794 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1795 gcc_assert (!stmt_could_throw_p (icall_stmt));
1798 /* Find values inside STMT for that we want to measure histograms for
1799 division/modulo optimization. */
1800 static bool
1801 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1803 gcall *stmt;
1804 tree fndecl;
1805 tree blck_size;
1806 enum built_in_function fcode;
1807 histogram_value histogram;
1808 gcov_type count, all, val;
1809 tree dest, src;
1810 unsigned int dest_align, src_align;
1811 gcov_type prob;
1812 tree tree_val;
1813 int size_arg;
1815 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1816 if (!stmt)
1817 return false;
1818 fndecl = gimple_call_fndecl (stmt);
1819 if (!fndecl)
1820 return false;
1821 fcode = DECL_FUNCTION_CODE (fndecl);
1822 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1823 return false;
1825 blck_size = gimple_call_arg (stmt, size_arg);
1826 if (TREE_CODE (blck_size) == INTEGER_CST)
1827 return false;
1829 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1830 if (!histogram)
1831 return false;
1832 val = histogram->hvalue.counters[0];
1833 count = histogram->hvalue.counters[1];
1834 all = histogram->hvalue.counters[2];
1835 gimple_remove_histogram_value (cfun, stmt, histogram);
1836 /* We require that count is at least half of all; this means
1837 that for the transformation to fire the value must be constant
1838 at least 80% of time. */
1839 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1840 return false;
1841 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1842 return false;
1843 if (all > 0)
1844 prob = GCOV_COMPUTE_SCALE (count, all);
1845 else
1846 prob = 0;
1847 dest = gimple_call_arg (stmt, 0);
1848 dest_align = get_pointer_alignment (dest);
1849 switch (fcode)
1851 case BUILT_IN_MEMCPY:
1852 case BUILT_IN_MEMPCPY:
1853 src = gimple_call_arg (stmt, 1);
1854 src_align = get_pointer_alignment (src);
1855 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1856 return false;
1857 break;
1858 case BUILT_IN_MEMSET:
1859 if (!can_store_by_pieces (val, builtin_memset_read_str,
1860 gimple_call_arg (stmt, 1),
1861 dest_align, true))
1862 return false;
1863 break;
1864 case BUILT_IN_BZERO:
1865 if (!can_store_by_pieces (val, builtin_memset_read_str,
1866 integer_zero_node,
1867 dest_align, true))
1868 return false;
1869 break;
1870 default:
1871 gcc_unreachable ();
1873 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1874 tree_val = build_int_cst (get_gcov_type (), val);
1875 else
1877 HOST_WIDE_INT a[2];
1878 a[0] = (unsigned HOST_WIDE_INT) val;
1879 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1881 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1882 TYPE_PRECISION (get_gcov_type ()), false));
1885 if (dump_file)
1887 fprintf (dump_file, "Single value %i stringop transformation on ",
1888 (int)val);
1889 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1891 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1893 return true;
1896 void
1897 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1898 HOST_WIDE_INT *expected_size)
1900 histogram_value histogram;
1901 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1902 if (!histogram)
1903 *expected_size = -1;
1904 else if (!histogram->hvalue.counters[1])
1906 *expected_size = -1;
1907 gimple_remove_histogram_value (cfun, stmt, histogram);
1909 else
1911 gcov_type size;
1912 size = ((histogram->hvalue.counters[0]
1913 + histogram->hvalue.counters[1] / 2)
1914 / histogram->hvalue.counters[1]);
1915 /* Even if we can hold bigger value in SIZE, INT_MAX
1916 is safe "infinity" for code generation strategies. */
1917 if (size > INT_MAX)
1918 size = INT_MAX;
1919 *expected_size = size;
1920 gimple_remove_histogram_value (cfun, stmt, histogram);
1922 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1923 if (!histogram)
1924 *expected_align = 0;
1925 else if (!histogram->hvalue.counters[0])
1927 gimple_remove_histogram_value (cfun, stmt, histogram);
1928 *expected_align = 0;
1930 else
1932 gcov_type count;
1933 int alignment;
1935 count = histogram->hvalue.counters[0];
1936 alignment = 1;
1937 while (!(count & alignment)
1938 && (alignment * 2 * BITS_PER_UNIT))
1939 alignment <<= 1;
1940 *expected_align = alignment * BITS_PER_UNIT;
1941 gimple_remove_histogram_value (cfun, stmt, histogram);
1946 /* Find values inside STMT for that we want to measure histograms for
1947 division/modulo optimization. */
1948 static void
1949 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1951 tree lhs, divisor, op0, type;
1952 histogram_value hist;
1954 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1955 return;
1957 lhs = gimple_assign_lhs (stmt);
1958 type = TREE_TYPE (lhs);
1959 if (!INTEGRAL_TYPE_P (type))
1960 return;
1962 switch (gimple_assign_rhs_code (stmt))
1964 case TRUNC_DIV_EXPR:
1965 case TRUNC_MOD_EXPR:
1966 divisor = gimple_assign_rhs2 (stmt);
1967 op0 = gimple_assign_rhs1 (stmt);
1969 values->reserve (3);
1971 if (TREE_CODE (divisor) == SSA_NAME)
1972 /* Check for the case where the divisor is the same value most
1973 of the time. */
1974 values->quick_push (gimple_alloc_histogram_value (cfun,
1975 HIST_TYPE_SINGLE_VALUE,
1976 stmt, divisor));
1978 /* For mod, check whether it is not often a noop (or replaceable by
1979 a few subtractions). */
1980 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1981 && TYPE_UNSIGNED (type))
1983 tree val;
1984 /* Check for a special case where the divisor is power of 2. */
1985 values->quick_push (gimple_alloc_histogram_value (cfun,
1986 HIST_TYPE_POW2,
1987 stmt, divisor));
1989 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1990 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1991 stmt, val);
1992 hist->hdata.intvl.int_start = 0;
1993 hist->hdata.intvl.steps = 2;
1994 values->quick_push (hist);
1996 return;
1998 default:
1999 return;
2003 /* Find calls inside STMT for that we want to measure histograms for
2004 indirect/virtual call optimization. */
2006 static void
2007 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
2009 tree callee;
2011 if (gimple_code (stmt) != GIMPLE_CALL
2012 || gimple_call_internal_p (stmt)
2013 || gimple_call_fndecl (stmt) != NULL_TREE)
2014 return;
2016 callee = gimple_call_fn (stmt);
2018 values->reserve (3);
2020 values->quick_push (gimple_alloc_histogram_value (
2021 cfun,
2022 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
2023 HIST_TYPE_INDIR_CALL_TOPN :
2024 HIST_TYPE_INDIR_CALL,
2025 stmt, callee));
2027 return;
2030 /* Find values inside STMT for that we want to measure histograms for
2031 string operations. */
2032 static void
2033 gimple_stringops_values_to_profile (gimple gs, histogram_values *values)
2035 gcall *stmt;
2036 tree fndecl;
2037 tree blck_size;
2038 tree dest;
2039 int size_arg;
2041 stmt = dyn_cast <gcall *> (gs);
2042 if (!stmt)
2043 return;
2044 fndecl = gimple_call_fndecl (stmt);
2045 if (!fndecl)
2046 return;
2048 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
2049 return;
2051 dest = gimple_call_arg (stmt, 0);
2052 blck_size = gimple_call_arg (stmt, size_arg);
2054 if (TREE_CODE (blck_size) != INTEGER_CST)
2056 values->safe_push (gimple_alloc_histogram_value (cfun,
2057 HIST_TYPE_SINGLE_VALUE,
2058 stmt, blck_size));
2059 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2060 stmt, blck_size));
2062 if (TREE_CODE (blck_size) != INTEGER_CST)
2063 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2064 stmt, dest));
2067 /* Find values inside STMT for that we want to measure histograms and adds
2068 them to list VALUES. */
2070 static void
2071 gimple_values_to_profile (gimple stmt, histogram_values *values)
2073 gimple_divmod_values_to_profile (stmt, values);
2074 gimple_stringops_values_to_profile (stmt, values);
2075 gimple_indirect_call_to_profile (stmt, values);
2078 void
2079 gimple_find_values_to_profile (histogram_values *values)
2081 basic_block bb;
2082 gimple_stmt_iterator gsi;
2083 unsigned i;
2084 histogram_value hist = NULL;
2085 values->create (0);
2087 FOR_EACH_BB_FN (bb, cfun)
2088 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2089 gimple_values_to_profile (gsi_stmt (gsi), values);
2091 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2093 FOR_EACH_VEC_ELT (*values, i, hist)
2095 switch (hist->type)
2097 case HIST_TYPE_INTERVAL:
2098 hist->n_counters = hist->hdata.intvl.steps + 2;
2099 break;
2101 case HIST_TYPE_POW2:
2102 hist->n_counters = 2;
2103 break;
2105 case HIST_TYPE_SINGLE_VALUE:
2106 hist->n_counters = 3;
2107 break;
2109 case HIST_TYPE_CONST_DELTA:
2110 hist->n_counters = 4;
2111 break;
2113 case HIST_TYPE_INDIR_CALL:
2114 hist->n_counters = 3;
2115 break;
2117 case HIST_TYPE_TIME_PROFILE:
2118 hist->n_counters = 1;
2119 break;
2121 case HIST_TYPE_AVERAGE:
2122 hist->n_counters = 2;
2123 break;
2125 case HIST_TYPE_IOR:
2126 hist->n_counters = 1;
2127 break;
2129 case HIST_TYPE_INDIR_CALL_TOPN:
2130 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2131 break;
2133 default:
2134 gcc_unreachable ();
2136 if (dump_file)
2138 fprintf (dump_file, "Stmt ");
2139 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2140 dump_histogram_value (dump_file, hist);