Generate PADDI to add large constants if -mcpu=future.
[official-gcc.git] / gcc / value-prof.c
blobcc3542f02954010afd5c3327a359d3e690f42cb5
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "cfghooks.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "coverage.h"
31 #include "data-streamer.h"
32 #include "diagnostic.h"
33 #include "fold-const.h"
34 #include "tree-nested.h"
35 #include "calls.h"
36 #include "expr.h"
37 #include "value-prof.h"
38 #include "tree-eh.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "tree-cfg.h"
42 #include "gimple-pretty-print.h"
43 #include "dumpfile.h"
44 #include "builtins.h"
46 /* In this file value profile based optimizations are placed. Currently the
47 following optimizations are implemented (for more detailed descriptions
48 see comments at value_profile_transformations):
50 1) Division/modulo specialization. Provided that we can determine that the
51 operands of the division have some special properties, we may use it to
52 produce more effective code.
54 2) Indirect/virtual call specialization. If we can determine most
55 common function callee in indirect/virtual call. We can use this
56 information to improve code effectiveness (especially info for
57 the inliner).
59 3) Speculative prefetching. If we are able to determine that the difference
60 between addresses accessed by a memory reference is usually constant, we
61 may add the prefetch instructions.
62 FIXME: This transformation was removed together with RTL based value
63 profiling.
66 Value profiling internals
67 ==========================
69 Every value profiling transformation starts with defining what values
70 to profile. There are different histogram types (see HIST_TYPE_* in
71 value-prof.h) and each transformation can request one or more histogram
72 types per GIMPLE statement. The function gimple_find_values_to_profile()
73 collects the values to profile in a vec, and adds the number of counters
74 required for the different histogram types.
76 For a -fprofile-generate run, the statements for which values should be
77 recorded, are instrumented in instrument_values(). The instrumentation
78 is done by helper functions that can be found in tree-profile.c, where
79 new types of histograms can be added if necessary.
81 After a -fprofile-use, the value profiling data is read back in by
82 compute_value_histograms() that translates the collected data to
83 histograms and attaches them to the profiled statements via
84 gimple_add_histogram_value(). Histograms are stored in a hash table
85 that is attached to every intrumented function, see VALUE_HISTOGRAMS
86 in function.h.
88 The value-profile transformations driver is the function
89 gimple_value_profile_transformations(). It traverses all statements in
90 the to-be-transformed function, and looks for statements with one or
91 more histograms attached to it. If a statement has histograms, the
92 transformation functions are called on the statement.
94 Limitations / FIXME / TODO:
95 * Only one histogram of each type can be associated with a statement.
96 * Some value profile transformations are done in builtins.c (?!)
97 * Updating of histograms needs some TLC.
98 * The value profiling code could be used to record analysis results
99 from non-profiling (e.g. VRP).
100 * Adding new profilers should be simplified, starting with a cleanup
101 of what-happens-where and with making gimple_find_values_to_profile
102 and gimple_value_profile_transformations table-driven, perhaps...
105 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
106 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
107 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
108 static bool gimple_stringops_transform (gimple_stmt_iterator *);
109 static bool gimple_ic_transform (gimple_stmt_iterator *);
111 /* Allocate histogram value. */
113 histogram_value
114 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
115 enum hist_type type, gimple *stmt, tree value)
117 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
118 hist->hvalue.value = value;
119 hist->hvalue.stmt = stmt;
120 hist->type = type;
121 return hist;
124 /* Hash value for histogram. */
126 static hashval_t
127 histogram_hash (const void *x)
129 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
132 /* Return nonzero if statement for histogram_value X is Y. */
134 static int
135 histogram_eq (const void *x, const void *y)
137 return ((const_histogram_value) x)->hvalue.stmt == (const gimple *) y;
140 /* Set histogram for STMT. */
142 static void
143 set_histogram_value (struct function *fun, gimple *stmt, histogram_value hist)
145 void **loc;
146 if (!hist && !VALUE_HISTOGRAMS (fun))
147 return;
148 if (!VALUE_HISTOGRAMS (fun))
149 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
150 histogram_eq, NULL);
151 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
152 htab_hash_pointer (stmt),
153 hist ? INSERT : NO_INSERT);
154 if (!hist)
156 if (loc)
157 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
158 return;
160 *loc = hist;
163 /* Get histogram list for STMT. */
165 histogram_value
166 gimple_histogram_value (struct function *fun, gimple *stmt)
168 if (!VALUE_HISTOGRAMS (fun))
169 return NULL;
170 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
171 htab_hash_pointer (stmt));
174 /* Add histogram for STMT. */
176 void
177 gimple_add_histogram_value (struct function *fun, gimple *stmt,
178 histogram_value hist)
180 hist->hvalue.next = gimple_histogram_value (fun, stmt);
181 set_histogram_value (fun, stmt, hist);
182 hist->fun = fun;
185 /* Remove histogram HIST from STMT's histogram list. */
187 void
188 gimple_remove_histogram_value (struct function *fun, gimple *stmt,
189 histogram_value hist)
191 histogram_value hist2 = gimple_histogram_value (fun, stmt);
192 if (hist == hist2)
194 set_histogram_value (fun, stmt, hist->hvalue.next);
196 else
198 while (hist2->hvalue.next != hist)
199 hist2 = hist2->hvalue.next;
200 hist2->hvalue.next = hist->hvalue.next;
202 free (hist->hvalue.counters);
203 if (flag_checking)
204 memset (hist, 0xab, sizeof (*hist));
205 free (hist);
208 /* Lookup histogram of type TYPE in the STMT. */
210 histogram_value
211 gimple_histogram_value_of_type (struct function *fun, gimple *stmt,
212 enum hist_type type)
214 histogram_value hist;
215 for (hist = gimple_histogram_value (fun, stmt); hist;
216 hist = hist->hvalue.next)
217 if (hist->type == type)
218 return hist;
219 return NULL;
222 /* Dump information about HIST to DUMP_FILE. */
224 static void
225 dump_histogram_value (FILE *dump_file, histogram_value hist)
227 switch (hist->type)
229 case HIST_TYPE_INTERVAL:
230 if (hist->hvalue.counters)
232 fprintf (dump_file, "Interval counter range [%d,%d]: [",
233 hist->hdata.intvl.int_start,
234 (hist->hdata.intvl.int_start
235 + hist->hdata.intvl.steps - 1));
237 unsigned int i;
238 for (i = 0; i < hist->hdata.intvl.steps; i++)
240 fprintf (dump_file, "%d:%" PRId64,
241 hist->hdata.intvl.int_start + i,
242 (int64_t) hist->hvalue.counters[i]);
243 if (i != hist->hdata.intvl.steps - 1)
244 fprintf (dump_file, ", ");
246 fprintf (dump_file, "] outside range: %" PRId64 ".\n",
247 (int64_t) hist->hvalue.counters[i]);
249 break;
251 case HIST_TYPE_POW2:
252 if (hist->hvalue.counters)
253 fprintf (dump_file, "Pow2 counter pow2:%" PRId64
254 " nonpow2:%" PRId64 ".\n",
255 (int64_t) hist->hvalue.counters[1],
256 (int64_t) hist->hvalue.counters[0]);
257 break;
259 case HIST_TYPE_TOPN_VALUES:
260 case HIST_TYPE_INDIR_CALL:
261 if (hist->hvalue.counters)
263 fprintf (dump_file,
264 (hist->type == HIST_TYPE_TOPN_VALUES
265 ? "Top N value counter " : "Indirect call counter"));
266 if (hist->hvalue.counters)
268 fprintf (dump_file, "all: %" PRId64 ", values: ",
269 (int64_t) hist->hvalue.counters[0]);
270 for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++)
272 fprintf (dump_file, "[%" PRId64 ":%" PRId64 "]",
273 (int64_t) hist->hvalue.counters[2 * i + 1],
274 (int64_t) hist->hvalue.counters[2 * i + 2]);
275 if (i != GCOV_TOPN_VALUES - 1)
276 fprintf (dump_file, ", ");
278 fprintf (dump_file, ".\n");
281 break;
283 case HIST_TYPE_AVERAGE:
284 if (hist->hvalue.counters)
285 fprintf (dump_file, "Average value sum:%" PRId64
286 " times:%" PRId64 ".\n",
287 (int64_t) hist->hvalue.counters[0],
288 (int64_t) hist->hvalue.counters[1]);
289 break;
291 case HIST_TYPE_IOR:
292 if (hist->hvalue.counters)
293 fprintf (dump_file, "IOR value ior:%" PRId64 ".\n",
294 (int64_t) hist->hvalue.counters[0]);
295 break;
297 case HIST_TYPE_TIME_PROFILE:
298 if (hist->hvalue.counters)
299 fprintf (dump_file, "Time profile time:%" PRId64 ".\n",
300 (int64_t) hist->hvalue.counters[0]);
301 break;
302 default:
303 gcc_unreachable ();
307 /* Dump information about HIST to DUMP_FILE. */
309 void
310 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
312 struct bitpack_d bp;
313 unsigned int i;
315 bp = bitpack_create (ob->main_stream);
316 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
317 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
318 streamer_write_bitpack (&bp);
319 switch (hist->type)
321 case HIST_TYPE_INTERVAL:
322 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
323 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
324 break;
325 default:
326 break;
328 for (i = 0; i < hist->n_counters; i++)
330 /* When user uses an unsigned type with a big value, constant converted
331 to gcov_type (a signed type) can be negative. */
332 gcov_type value = hist->hvalue.counters[i];
333 if (hist->type == HIST_TYPE_TOPN_VALUES && i > 0)
335 else
336 gcc_assert (value >= 0);
338 streamer_write_gcov_count (ob, value);
340 if (hist->hvalue.next)
341 stream_out_histogram_value (ob, hist->hvalue.next);
344 /* Dump information about HIST to DUMP_FILE. */
346 void
347 stream_in_histogram_value (class lto_input_block *ib, gimple *stmt)
349 enum hist_type type;
350 unsigned int ncounters = 0;
351 struct bitpack_d bp;
352 unsigned int i;
353 histogram_value new_val;
354 bool next;
355 histogram_value *next_p = NULL;
359 bp = streamer_read_bitpack (ib);
360 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
361 next = bp_unpack_value (&bp, 1);
362 new_val = gimple_alloc_histogram_value (cfun, type, stmt);
363 switch (type)
365 case HIST_TYPE_INTERVAL:
366 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
367 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
368 ncounters = new_val->hdata.intvl.steps + 2;
369 break;
371 case HIST_TYPE_POW2:
372 case HIST_TYPE_AVERAGE:
373 ncounters = 2;
374 break;
376 case HIST_TYPE_TOPN_VALUES:
377 case HIST_TYPE_INDIR_CALL:
378 ncounters = GCOV_TOPN_VALUES_COUNTERS;
379 break;
381 case HIST_TYPE_IOR:
382 case HIST_TYPE_TIME_PROFILE:
383 ncounters = 1;
384 break;
386 default:
387 gcc_unreachable ();
389 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
390 new_val->n_counters = ncounters;
391 for (i = 0; i < ncounters; i++)
392 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
393 if (!next_p)
394 gimple_add_histogram_value (cfun, stmt, new_val);
395 else
396 *next_p = new_val;
397 next_p = &new_val->hvalue.next;
399 while (next);
402 /* Dump all histograms attached to STMT to DUMP_FILE. */
404 void
405 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple *stmt)
407 histogram_value hist;
408 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
409 dump_histogram_value (dump_file, hist);
412 /* Remove all histograms associated with STMT. */
414 void
415 gimple_remove_stmt_histograms (struct function *fun, gimple *stmt)
417 histogram_value val;
418 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
419 gimple_remove_histogram_value (fun, stmt, val);
422 /* Duplicate all histograms associates with OSTMT to STMT. */
424 void
425 gimple_duplicate_stmt_histograms (struct function *fun, gimple *stmt,
426 struct function *ofun, gimple *ostmt)
428 histogram_value val;
429 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
431 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type);
432 memcpy (new_val, val, sizeof (*val));
433 new_val->hvalue.stmt = stmt;
434 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
435 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
436 gimple_add_histogram_value (fun, stmt, new_val);
440 /* Move all histograms associated with OSTMT to STMT. */
442 void
443 gimple_move_stmt_histograms (struct function *fun, gimple *stmt, gimple *ostmt)
445 histogram_value val = gimple_histogram_value (fun, ostmt);
446 if (val)
448 /* The following three statements can't be reordered,
449 because histogram hashtab relies on stmt field value
450 for finding the exact slot. */
451 set_histogram_value (fun, ostmt, NULL);
452 for (; val != NULL; val = val->hvalue.next)
453 val->hvalue.stmt = stmt;
454 set_histogram_value (fun, stmt, val);
458 static bool error_found = false;
460 /* Helper function for verify_histograms. For each histogram reachable via htab
461 walk verify that it was reached via statement walk. */
463 static int
464 visit_hist (void **slot, void *data)
466 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
467 histogram_value hist = *(histogram_value *) slot;
469 if (!visited->contains (hist)
470 && hist->type != HIST_TYPE_TIME_PROFILE)
472 error ("dead histogram");
473 dump_histogram_value (stderr, hist);
474 debug_gimple_stmt (hist->hvalue.stmt);
475 error_found = true;
477 return 1;
480 /* Verify sanity of the histograms. */
482 DEBUG_FUNCTION void
483 verify_histograms (void)
485 basic_block bb;
486 gimple_stmt_iterator gsi;
487 histogram_value hist;
489 error_found = false;
490 hash_set<histogram_value> visited_hists;
491 FOR_EACH_BB_FN (bb, cfun)
492 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
494 gimple *stmt = gsi_stmt (gsi);
496 for (hist = gimple_histogram_value (cfun, stmt); hist;
497 hist = hist->hvalue.next)
499 if (hist->hvalue.stmt != stmt)
501 error ("histogram value statement does not correspond to "
502 "the statement it is associated with");
503 debug_gimple_stmt (stmt);
504 dump_histogram_value (stderr, hist);
505 error_found = true;
507 visited_hists.add (hist);
510 if (VALUE_HISTOGRAMS (cfun))
511 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
512 if (error_found)
513 internal_error ("%qs failed", __func__);
516 /* Helper function for verify_histograms. For each histogram reachable via htab
517 walk verify that it was reached via statement walk. */
519 static int
520 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
522 histogram_value hist = *(histogram_value *) slot;
523 free (hist->hvalue.counters);
524 free (hist);
525 return 1;
528 void
529 free_histograms (struct function *fn)
531 if (VALUE_HISTOGRAMS (fn))
533 htab_traverse (VALUE_HISTOGRAMS (fn), free_hist, NULL);
534 htab_delete (VALUE_HISTOGRAMS (fn));
535 VALUE_HISTOGRAMS (fn) = NULL;
539 /* The overall number of invocations of the counter should match
540 execution count of basic block. Report it as error rather than
541 internal error as it might mean that user has misused the profile
542 somehow. */
544 static bool
545 check_counter (gimple *stmt, const char * name,
546 gcov_type *count, gcov_type *all, profile_count bb_count_d)
548 gcov_type bb_count = bb_count_d.ipa ().to_gcov_type ();
549 if (*all != bb_count || *count > *all)
551 dump_user_location_t locus;
552 locus = ((stmt != NULL)
553 ? dump_user_location_t (stmt)
554 : dump_user_location_t::from_function_decl
555 (current_function_decl));
556 if (flag_profile_correction)
558 if (dump_enabled_p ())
559 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
560 "correcting inconsistent value profile: %s "
561 "profiler overall count (%d) does not match BB "
562 "count (%d)\n", name, (int)*all, (int)bb_count);
563 *all = bb_count;
564 if (*count > *all)
565 *count = *all;
566 return false;
568 else
570 error_at (locus.get_location_t (), "corrupted value profile: %s "
571 "profile counter (%d out of %d) inconsistent with "
572 "basic-block count (%d)",
573 name,
574 (int) *count,
575 (int) *all,
576 (int) bb_count);
577 return true;
581 return false;
584 /* GIMPLE based transformations. */
586 bool
587 gimple_value_profile_transformations (void)
589 basic_block bb;
590 gimple_stmt_iterator gsi;
591 bool changed = false;
593 FOR_EACH_BB_FN (bb, cfun)
595 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
597 gimple *stmt = gsi_stmt (gsi);
598 histogram_value th = gimple_histogram_value (cfun, stmt);
599 if (!th)
600 continue;
602 if (dump_file)
604 fprintf (dump_file, "Trying transformations on stmt ");
605 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
606 dump_histograms_for_stmt (cfun, dump_file, stmt);
609 /* Transformations: */
610 /* The order of things in this conditional controls which
611 transformation is used when more than one is applicable. */
612 /* It is expected that any code added by the transformations
613 will be added before the current statement, and that the
614 current statement remain valid (although possibly
615 modified) upon return. */
616 if (gimple_mod_subtract_transform (&gsi)
617 || gimple_divmod_fixed_value_transform (&gsi)
618 || gimple_mod_pow2_value_transform (&gsi)
619 || gimple_stringops_transform (&gsi)
620 || gimple_ic_transform (&gsi))
622 stmt = gsi_stmt (gsi);
623 changed = true;
624 /* Original statement may no longer be in the same block. */
625 if (bb != gimple_bb (stmt))
627 bb = gimple_bb (stmt);
628 gsi = gsi_for_stmt (stmt);
634 return changed;
637 /* Generate code for transformation 1 (with parent gimple assignment
638 STMT and probability of taking the optimal path PROB, which is
639 equivalent to COUNT/ALL within roundoff error). This generates the
640 result into a temp and returns the temp; it does not replace or
641 alter the original STMT. */
643 static tree
644 gimple_divmod_fixed_value (gassign *stmt, tree value, profile_probability prob,
645 gcov_type count, gcov_type all)
647 gassign *stmt1, *stmt2;
648 gcond *stmt3;
649 tree tmp0, tmp1, tmp2;
650 gimple *bb1end, *bb2end, *bb3end;
651 basic_block bb, bb2, bb3, bb4;
652 tree optype, op1, op2;
653 edge e12, e13, e23, e24, e34;
654 gimple_stmt_iterator gsi;
656 gcc_assert (is_gimple_assign (stmt)
657 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
658 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
660 optype = TREE_TYPE (gimple_assign_lhs (stmt));
661 op1 = gimple_assign_rhs1 (stmt);
662 op2 = gimple_assign_rhs2 (stmt);
664 bb = gimple_bb (stmt);
665 gsi = gsi_for_stmt (stmt);
667 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
668 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
669 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
670 stmt2 = gimple_build_assign (tmp1, op2);
671 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
672 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
673 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
674 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
675 bb1end = stmt3;
677 tmp2 = create_tmp_reg (optype, "PROF");
678 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
679 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
680 bb2end = stmt1;
682 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
683 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
684 bb3end = stmt1;
686 /* Fix CFG. */
687 /* Edge e23 connects bb2 to bb3, etc. */
688 e12 = split_block (bb, bb1end);
689 bb2 = e12->dest;
690 bb2->count = profile_count::from_gcov_type (count);
691 e23 = split_block (bb2, bb2end);
692 bb3 = e23->dest;
693 bb3->count = profile_count::from_gcov_type (all - count);
694 e34 = split_block (bb3, bb3end);
695 bb4 = e34->dest;
696 bb4->count = profile_count::from_gcov_type (all);
698 e12->flags &= ~EDGE_FALLTHRU;
699 e12->flags |= EDGE_FALSE_VALUE;
700 e12->probability = prob;
702 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
703 e13->probability = prob.invert ();
705 remove_edge (e23);
707 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
708 e24->probability = profile_probability::always ();
710 e34->probability = profile_probability::always ();
712 return tmp2;
715 /* Return the n-th value count of TOPN_VALUE histogram. If
716 there's a value, return true and set VALUE and COUNT
717 arguments. */
719 bool
720 get_nth_most_common_value (gimple *stmt, const char *counter_type,
721 histogram_value hist, gcov_type *value,
722 gcov_type *count, gcov_type *all, unsigned n)
724 if (hist->hvalue.counters[2] == -1)
725 return false;
727 gcc_assert (n < GCOV_TOPN_VALUES);
729 *count = 0;
730 *value = 0;
732 gcov_type read_all = hist->hvalue.counters[0];
734 gcov_type v = hist->hvalue.counters[2 * n + 1];
735 gcov_type c = hist->hvalue.counters[2 * n + 2];
737 /* Indirect calls can't be verified. */
738 if (stmt
739 && check_counter (stmt, counter_type, &c, &read_all,
740 gimple_bb (stmt)->count))
741 return false;
743 *all = read_all;
745 *value = v;
746 *count = c;
747 return true;
750 /* Do transform 1) on INSN if applicable. */
752 static bool
753 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
755 histogram_value histogram;
756 enum tree_code code;
757 gcov_type val, count, all;
758 tree result, value, tree_val;
759 profile_probability prob;
760 gassign *stmt;
762 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
763 if (!stmt)
764 return false;
766 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
767 return false;
769 code = gimple_assign_rhs_code (stmt);
771 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
772 return false;
774 histogram = gimple_histogram_value_of_type (cfun, stmt,
775 HIST_TYPE_TOPN_VALUES);
776 if (!histogram)
777 return false;
779 if (!get_nth_most_common_value (stmt, "divmod", histogram, &val, &count,
780 &all))
781 return false;
783 value = histogram->hvalue.value;
784 gimple_remove_histogram_value (cfun, stmt, histogram);
786 /* We require that count is at least half of all. */
787 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
788 || 2 * count < all
789 || optimize_bb_for_size_p (gimple_bb (stmt)))
790 return false;
792 /* Compute probability of taking the optimal path. */
793 if (all > 0)
794 prob = profile_probability::probability_in_gcov_type (count, all);
795 else
796 prob = profile_probability::never ();
798 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
799 tree_val = build_int_cst (get_gcov_type (), val);
800 else
802 HOST_WIDE_INT a[2];
803 a[0] = (unsigned HOST_WIDE_INT) val;
804 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
806 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
807 TYPE_PRECISION (get_gcov_type ()), false));
809 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
811 if (dump_enabled_p ())
812 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
813 "Transformation done: div/mod by constant %T\n", tree_val);
815 gimple_assign_set_rhs_from_tree (si, result);
816 update_stmt (gsi_stmt (*si));
818 return true;
821 /* Generate code for transformation 2 (with parent gimple assign STMT and
822 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
823 within roundoff error). This generates the result into a temp and returns
824 the temp; it does not replace or alter the original STMT. */
826 static tree
827 gimple_mod_pow2 (gassign *stmt, profile_probability prob, gcov_type count, gcov_type all)
829 gassign *stmt1, *stmt2, *stmt3;
830 gcond *stmt4;
831 tree tmp2, tmp3;
832 gimple *bb1end, *bb2end, *bb3end;
833 basic_block bb, bb2, bb3, bb4;
834 tree optype, op1, op2;
835 edge e12, e13, e23, e24, e34;
836 gimple_stmt_iterator gsi;
837 tree result;
839 gcc_assert (is_gimple_assign (stmt)
840 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
842 optype = TREE_TYPE (gimple_assign_lhs (stmt));
843 op1 = gimple_assign_rhs1 (stmt);
844 op2 = gimple_assign_rhs2 (stmt);
846 bb = gimple_bb (stmt);
847 gsi = gsi_for_stmt (stmt);
849 result = create_tmp_reg (optype, "PROF");
850 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
851 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
852 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
853 build_int_cst (optype, -1));
854 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
855 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
856 NULL_TREE, NULL_TREE);
857 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
858 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
859 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
860 bb1end = stmt4;
862 /* tmp2 == op2-1 inherited from previous block. */
863 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
864 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
865 bb2end = stmt1;
867 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
868 op1, op2);
869 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
870 bb3end = stmt1;
872 /* Fix CFG. */
873 /* Edge e23 connects bb2 to bb3, etc. */
874 e12 = split_block (bb, bb1end);
875 bb2 = e12->dest;
876 bb2->count = profile_count::from_gcov_type (count);
877 e23 = split_block (bb2, bb2end);
878 bb3 = e23->dest;
879 bb3->count = profile_count::from_gcov_type (all - count);
880 e34 = split_block (bb3, bb3end);
881 bb4 = e34->dest;
882 bb4->count = profile_count::from_gcov_type (all);
884 e12->flags &= ~EDGE_FALLTHRU;
885 e12->flags |= EDGE_FALSE_VALUE;
886 e12->probability = prob;
888 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
889 e13->probability = prob.invert ();
891 remove_edge (e23);
893 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
894 e24->probability = profile_probability::always ();
896 e34->probability = profile_probability::always ();
898 return result;
901 /* Do transform 2) on INSN if applicable. */
903 static bool
904 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
906 histogram_value histogram;
907 enum tree_code code;
908 gcov_type count, wrong_values, all;
909 tree lhs_type, result, value;
910 profile_probability prob;
911 gassign *stmt;
913 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
914 if (!stmt)
915 return false;
917 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
918 if (!INTEGRAL_TYPE_P (lhs_type))
919 return false;
921 code = gimple_assign_rhs_code (stmt);
923 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
924 return false;
926 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
927 if (!histogram)
928 return false;
930 value = histogram->hvalue.value;
931 wrong_values = histogram->hvalue.counters[0];
932 count = histogram->hvalue.counters[1];
934 gimple_remove_histogram_value (cfun, stmt, histogram);
936 /* We require that we hit a power of 2 at least half of all evaluations. */
937 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
938 || count < wrong_values
939 || optimize_bb_for_size_p (gimple_bb (stmt)))
940 return false;
942 /* Compute probability of taking the optimal path. */
943 all = count + wrong_values;
945 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
946 return false;
948 if (dump_enabled_p ())
949 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
950 "Transformation done: mod power of 2\n");
952 if (all > 0)
953 prob = profile_probability::probability_in_gcov_type (count, all);
954 else
955 prob = profile_probability::never ();
957 result = gimple_mod_pow2 (stmt, prob, count, all);
959 gimple_assign_set_rhs_from_tree (si, result);
960 update_stmt (gsi_stmt (*si));
962 return true;
965 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
966 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
967 supported and this is built into this interface. The probabilities of taking
968 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
969 COUNT2/ALL respectively within roundoff error). This generates the
970 result into a temp and returns the temp; it does not replace or alter
971 the original STMT. */
972 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
974 static tree
975 gimple_mod_subtract (gassign *stmt, profile_probability prob1,
976 profile_probability prob2, int ncounts,
977 gcov_type count1, gcov_type count2, gcov_type all)
979 gassign *stmt1;
980 gimple *stmt2;
981 gcond *stmt3;
982 tree tmp1;
983 gimple *bb1end, *bb2end = NULL, *bb3end;
984 basic_block bb, bb2, bb3, bb4;
985 tree optype, op1, op2;
986 edge e12, e23 = 0, e24, e34, e14;
987 gimple_stmt_iterator gsi;
988 tree result;
990 gcc_assert (is_gimple_assign (stmt)
991 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
993 optype = TREE_TYPE (gimple_assign_lhs (stmt));
994 op1 = gimple_assign_rhs1 (stmt);
995 op2 = gimple_assign_rhs2 (stmt);
997 bb = gimple_bb (stmt);
998 gsi = gsi_for_stmt (stmt);
1000 result = create_tmp_reg (optype, "PROF");
1001 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1002 stmt1 = gimple_build_assign (result, op1);
1003 stmt2 = gimple_build_assign (tmp1, op2);
1004 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1005 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1006 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1007 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1008 bb1end = stmt3;
1010 if (ncounts) /* Assumed to be 0 or 1 */
1012 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1013 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1014 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1015 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1016 bb2end = stmt2;
1019 /* Fallback case. */
1020 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1021 result, tmp1);
1022 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1023 bb3end = stmt1;
1025 /* Fix CFG. */
1026 /* Edge e23 connects bb2 to bb3, etc. */
1027 /* However block 3 is optional; if it is not there, references
1028 to 3 really refer to block 2. */
1029 e12 = split_block (bb, bb1end);
1030 bb2 = e12->dest;
1031 bb2->count = profile_count::from_gcov_type (all - count1);
1033 if (ncounts) /* Assumed to be 0 or 1. */
1035 e23 = split_block (bb2, bb2end);
1036 bb3 = e23->dest;
1037 bb3->count = profile_count::from_gcov_type (all - count1 - count2);
1040 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1041 bb4 = e34->dest;
1042 bb4->count = profile_count::from_gcov_type (all);
1044 e12->flags &= ~EDGE_FALLTHRU;
1045 e12->flags |= EDGE_FALSE_VALUE;
1046 e12->probability = prob1.invert ();
1048 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1049 e14->probability = prob1;
1051 if (ncounts) /* Assumed to be 0 or 1. */
1053 e23->flags &= ~EDGE_FALLTHRU;
1054 e23->flags |= EDGE_FALSE_VALUE;
1055 e23->probability = prob2.invert ();
1057 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1058 e24->probability = prob2;
1061 e34->probability = profile_probability::always ();
1063 return result;
1066 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1068 static bool
1069 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1071 histogram_value histogram;
1072 enum tree_code code;
1073 gcov_type count, wrong_values, all;
1074 tree lhs_type, result;
1075 profile_probability prob1, prob2;
1076 unsigned int i, steps;
1077 gcov_type count1, count2;
1078 gassign *stmt;
1079 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1080 if (!stmt)
1081 return false;
1083 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1084 if (!INTEGRAL_TYPE_P (lhs_type))
1085 return false;
1087 code = gimple_assign_rhs_code (stmt);
1089 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1090 return false;
1092 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1093 if (!histogram)
1094 return false;
1096 all = 0;
1097 wrong_values = 0;
1098 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1099 all += histogram->hvalue.counters[i];
1101 wrong_values += histogram->hvalue.counters[i];
1102 wrong_values += histogram->hvalue.counters[i+1];
1103 steps = histogram->hdata.intvl.steps;
1104 all += wrong_values;
1105 count1 = histogram->hvalue.counters[0];
1106 count2 = histogram->hvalue.counters[1];
1108 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1110 gimple_remove_histogram_value (cfun, stmt, histogram);
1111 return false;
1114 if (flag_profile_correction && count1 + count2 > all)
1115 all = count1 + count2;
1117 gcc_assert (count1 + count2 <= all);
1119 /* We require that we use just subtractions in at least 50% of all
1120 evaluations. */
1121 count = 0;
1122 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1124 count += histogram->hvalue.counters[i];
1125 if (count * 2 >= all)
1126 break;
1128 if (i == steps
1129 || optimize_bb_for_size_p (gimple_bb (stmt)))
1130 return false;
1132 gimple_remove_histogram_value (cfun, stmt, histogram);
1133 if (dump_enabled_p ())
1134 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
1135 "Transformation done: mod subtract\n");
1137 /* Compute probability of taking the optimal path(s). */
1138 if (all > 0)
1140 prob1 = profile_probability::probability_in_gcov_type (count1, all);
1141 prob2 = profile_probability::probability_in_gcov_type (count2, all);
1143 else
1145 prob1 = prob2 = profile_probability::never ();
1148 /* In practice, "steps" is always 2. This interface reflects this,
1149 and will need to be changed if "steps" can change. */
1150 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1152 gimple_assign_set_rhs_from_tree (si, result);
1153 update_stmt (gsi_stmt (*si));
1155 return true;
1158 typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
1160 static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
1162 /* Returns true if node graph is initialized. This
1163 is used to test if profile_id has been created
1164 for cgraph_nodes. */
1166 bool
1167 coverage_node_map_initialized_p (void)
1169 return cgraph_node_map != 0;
1172 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1173 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1174 that the PROFILE_IDs was already assigned. */
1176 void
1177 init_node_map (bool local)
1179 struct cgraph_node *n;
1180 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
1182 FOR_EACH_DEFINED_FUNCTION (n)
1183 if (n->has_gimple_body_p () || n->thunk.thunk_p)
1185 cgraph_node **val;
1186 dump_user_location_t loc
1187 = dump_user_location_t::from_function_decl (n->decl);
1188 if (local)
1190 n->profile_id = coverage_compute_profile_id (n);
1191 while ((val = cgraph_node_map->get (n->profile_id))
1192 || !n->profile_id)
1194 if (dump_enabled_p ())
1195 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
1196 "Local profile-id %i conflict"
1197 " with nodes %s %s\n",
1198 n->profile_id,
1199 n->dump_name (),
1200 (*val)->dump_name ());
1201 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1204 else if (!n->profile_id)
1206 if (dump_enabled_p ())
1207 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
1208 "Node %s has no profile-id"
1209 " (profile feedback missing?)\n",
1210 n->dump_name ());
1211 continue;
1213 else if ((val = cgraph_node_map->get (n->profile_id)))
1215 if (dump_enabled_p ())
1216 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
1217 "Node %s has IP profile-id %i conflict. "
1218 "Giving up.\n",
1219 n->dump_name (), n->profile_id);
1220 *val = NULL;
1221 continue;
1223 cgraph_node_map->put (n->profile_id, n);
1227 /* Delete the CGRAPH_NODE_MAP. */
1229 void
1230 del_node_map (void)
1232 delete cgraph_node_map;
1235 /* Return cgraph node for function with pid */
1237 struct cgraph_node*
1238 find_func_by_profile_id (int profile_id)
1240 cgraph_node **val = cgraph_node_map->get (profile_id);
1241 if (val)
1242 return *val;
1243 else
1244 return NULL;
1247 /* Do transformation
1249 if (actual_callee_address == address_of_most_common_function/method)
1250 do direct call
1251 else
1252 old call
1255 gcall *
1256 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1257 profile_probability prob)
1259 gcall *dcall_stmt;
1260 gassign *load_stmt;
1261 gcond *cond_stmt;
1262 tree tmp0, tmp1, tmp;
1263 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1264 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1265 gimple_stmt_iterator gsi;
1266 int lp_nr, dflags;
1267 edge e_eh, e;
1268 edge_iterator ei;
1270 cond_bb = gimple_bb (icall_stmt);
1271 gsi = gsi_for_stmt (icall_stmt);
1273 tmp0 = make_temp_ssa_name (ptr_type_node, NULL, "PROF");
1274 tmp1 = make_temp_ssa_name (ptr_type_node, NULL, "PROF");
1275 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1276 load_stmt = gimple_build_assign (tmp0, tmp);
1277 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1279 tmp = fold_convert (ptr_type_node, build_addr (direct_call->decl));
1280 load_stmt = gimple_build_assign (tmp1, tmp);
1281 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1283 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1284 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1286 if (TREE_CODE (gimple_vdef (icall_stmt)) == SSA_NAME)
1288 unlink_stmt_vdef (icall_stmt);
1289 release_ssa_name (gimple_vdef (icall_stmt));
1291 gimple_set_vdef (icall_stmt, NULL_TREE);
1292 gimple_set_vuse (icall_stmt, NULL_TREE);
1293 update_stmt (icall_stmt);
1294 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1295 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1296 dflags = flags_from_decl_or_type (direct_call->decl);
1297 if ((dflags & ECF_NORETURN) != 0
1298 && should_remove_lhs_p (gimple_call_lhs (dcall_stmt)))
1299 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1300 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1302 /* Fix CFG. */
1303 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1304 e_cd = split_block (cond_bb, cond_stmt);
1305 dcall_bb = e_cd->dest;
1306 dcall_bb->count = cond_bb->count.apply_probability (prob);
1308 e_di = split_block (dcall_bb, dcall_stmt);
1309 icall_bb = e_di->dest;
1310 icall_bb->count = cond_bb->count - dcall_bb->count;
1312 /* Do not disturb existing EH edges from the indirect call. */
1313 if (!stmt_ends_bb_p (icall_stmt))
1314 e_ij = split_block (icall_bb, icall_stmt);
1315 else
1317 e_ij = find_fallthru_edge (icall_bb->succs);
1318 /* The indirect call might be noreturn. */
1319 if (e_ij != NULL)
1321 e_ij->probability = profile_probability::always ();
1322 e_ij = single_pred_edge (split_edge (e_ij));
1325 if (e_ij != NULL)
1327 join_bb = e_ij->dest;
1328 join_bb->count = cond_bb->count;
1331 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1332 e_cd->probability = prob;
1334 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1335 e_ci->probability = prob.invert ();
1337 remove_edge (e_di);
1339 if (e_ij != NULL)
1341 if ((dflags & ECF_NORETURN) == 0)
1343 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1344 e_dj->probability = profile_probability::always ();
1346 e_ij->probability = profile_probability::always ();
1349 /* Insert PHI node for the call result if necessary. */
1350 if (gimple_call_lhs (icall_stmt)
1351 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1352 && (dflags & ECF_NORETURN) == 0)
1354 tree result = gimple_call_lhs (icall_stmt);
1355 gphi *phi = create_phi_node (result, join_bb);
1356 gimple_call_set_lhs (icall_stmt,
1357 duplicate_ssa_name (result, icall_stmt));
1358 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1359 gimple_call_set_lhs (dcall_stmt,
1360 duplicate_ssa_name (result, dcall_stmt));
1361 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1364 /* Build an EH edge for the direct call if necessary. */
1365 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1366 if (lp_nr > 0 && stmt_could_throw_p (cfun, dcall_stmt))
1368 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1371 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1372 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1374 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1375 e->probability = e_eh->probability;
1376 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1377 !gsi_end_p (psi); gsi_next (&psi))
1379 gphi *phi = psi.phi ();
1380 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1381 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1384 if (!stmt_could_throw_p (cfun, dcall_stmt))
1385 gimple_purge_dead_eh_edges (dcall_bb);
1386 return dcall_stmt;
1390 For every checked indirect/virtual call determine if most common pid of
1391 function/class method has probability more than 50%. If yes modify code of
1392 this call to:
1395 static bool
1396 gimple_ic_transform (gimple_stmt_iterator *gsi)
1398 gcall *stmt;
1399 histogram_value histogram;
1400 gcov_type val, count, all;
1401 struct cgraph_node *direct_call;
1403 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1404 if (!stmt)
1405 return false;
1407 if (gimple_call_fndecl (stmt) != NULL_TREE)
1408 return false;
1410 if (gimple_call_internal_p (stmt))
1411 return false;
1413 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1414 if (!histogram)
1415 return false;
1417 if (!get_nth_most_common_value (NULL, "indirect call", histogram, &val,
1418 &count, &all))
1419 return false;
1421 if (4 * count <= 3 * all)
1422 return false;
1424 direct_call = find_func_by_profile_id ((int)val);
1426 if (direct_call == NULL)
1428 if (val)
1430 if (dump_enabled_p ())
1431 dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmt,
1432 "Indirect call -> direct call from other "
1433 "module %T=> %i (will resolve only with LTO)\n",
1434 gimple_call_fn (stmt), (int)val);
1436 return false;
1439 if (dump_enabled_p ())
1441 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
1442 "Indirect call -> direct call "
1443 "%T => %T transformation on insn postponed\n",
1444 gimple_call_fn (stmt), direct_call->decl);
1445 dump_printf_loc (MSG_NOTE, stmt,
1446 "hist->count %" PRId64
1447 " hist->all %" PRId64"\n", count, all);
1450 return true;
1453 /* Return true if the stringop CALL shall be profiled. SIZE_ARG be
1454 set to the argument index for the size of the string operation. */
1456 static bool
1457 interesting_stringop_to_profile_p (gcall *call, int *size_arg)
1459 enum built_in_function fcode;
1461 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (call));
1462 switch (fcode)
1464 case BUILT_IN_MEMCPY:
1465 case BUILT_IN_MEMPCPY:
1466 case BUILT_IN_MEMMOVE:
1467 *size_arg = 2;
1468 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1469 INTEGER_TYPE, VOID_TYPE);
1470 case BUILT_IN_MEMSET:
1471 *size_arg = 2;
1472 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1473 INTEGER_TYPE, VOID_TYPE);
1474 case BUILT_IN_BZERO:
1475 *size_arg = 1;
1476 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1477 VOID_TYPE);
1478 default:
1479 return false;
1483 /* Convert stringop (..., vcall_size)
1484 into
1485 if (vcall_size == icall_size)
1486 stringop (..., icall_size);
1487 else
1488 stringop (..., vcall_size);
1489 assuming we'll propagate a true constant into ICALL_SIZE later. */
1491 static void
1492 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, profile_probability prob,
1493 gcov_type count, gcov_type all)
1495 gassign *tmp_stmt;
1496 gcond *cond_stmt;
1497 gcall *icall_stmt;
1498 tree tmp0, tmp1, vcall_size, optype;
1499 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1500 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1501 gimple_stmt_iterator gsi;
1502 int size_arg;
1504 if (!interesting_stringop_to_profile_p (vcall_stmt, &size_arg))
1505 gcc_unreachable ();
1507 cond_bb = gimple_bb (vcall_stmt);
1508 gsi = gsi_for_stmt (vcall_stmt);
1510 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1511 optype = TREE_TYPE (vcall_size);
1513 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1514 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1515 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1516 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1518 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1519 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1521 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1522 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1524 if (TREE_CODE (gimple_vdef (vcall_stmt)) == SSA_NAME)
1526 unlink_stmt_vdef (vcall_stmt);
1527 release_ssa_name (gimple_vdef (vcall_stmt));
1529 gimple_set_vdef (vcall_stmt, NULL);
1530 gimple_set_vuse (vcall_stmt, NULL);
1531 update_stmt (vcall_stmt);
1532 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1533 gimple_call_set_arg (icall_stmt, size_arg,
1534 fold_convert (optype, icall_size));
1535 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1537 /* Fix CFG. */
1538 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1539 e_ci = split_block (cond_bb, cond_stmt);
1540 icall_bb = e_ci->dest;
1541 icall_bb->count = profile_count::from_gcov_type (count);
1543 e_iv = split_block (icall_bb, icall_stmt);
1544 vcall_bb = e_iv->dest;
1545 vcall_bb->count = profile_count::from_gcov_type (all - count);
1547 e_vj = split_block (vcall_bb, vcall_stmt);
1548 join_bb = e_vj->dest;
1549 join_bb->count = profile_count::from_gcov_type (all);
1551 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1552 e_ci->probability = prob;
1554 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1555 e_cv->probability = prob.invert ();
1557 remove_edge (e_iv);
1559 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1560 e_ij->probability = profile_probability::always ();
1562 e_vj->probability = profile_probability::always ();
1564 /* Insert PHI node for the call result if necessary. */
1565 if (gimple_call_lhs (vcall_stmt)
1566 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1568 tree result = gimple_call_lhs (vcall_stmt);
1569 gphi *phi = create_phi_node (result, join_bb);
1570 gimple_call_set_lhs (vcall_stmt,
1571 duplicate_ssa_name (result, vcall_stmt));
1572 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1573 gimple_call_set_lhs (icall_stmt,
1574 duplicate_ssa_name (result, icall_stmt));
1575 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1578 /* Because these are all string op builtins, they're all nothrow. */
1579 gcc_assert (!stmt_could_throw_p (cfun, vcall_stmt));
1580 gcc_assert (!stmt_could_throw_p (cfun, icall_stmt));
1583 /* Find values inside STMT for that we want to measure histograms for
1584 division/modulo optimization. */
1586 static bool
1587 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1589 gcall *stmt;
1590 tree blck_size;
1591 enum built_in_function fcode;
1592 histogram_value histogram;
1593 gcov_type count, all, val;
1594 tree dest, src;
1595 unsigned int dest_align, src_align;
1596 profile_probability prob;
1597 tree tree_val;
1598 int size_arg;
1600 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1601 if (!stmt)
1602 return false;
1604 if (!gimple_call_builtin_p (gsi_stmt (*gsi), BUILT_IN_NORMAL))
1605 return false;
1607 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
1608 return false;
1610 blck_size = gimple_call_arg (stmt, size_arg);
1611 if (TREE_CODE (blck_size) == INTEGER_CST)
1612 return false;
1614 histogram = gimple_histogram_value_of_type (cfun, stmt,
1615 HIST_TYPE_TOPN_VALUES);
1616 if (!histogram)
1617 return false;
1619 if (!get_nth_most_common_value (stmt, "stringops", histogram, &val, &count,
1620 &all))
1621 return false;
1623 gimple_remove_histogram_value (cfun, stmt, histogram);
1625 /* We require that count is at least half of all. */
1626 if (2 * count < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1627 return false;
1628 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1629 return false;
1630 if (all > 0)
1631 prob = profile_probability::probability_in_gcov_type (count, all);
1632 else
1633 prob = profile_probability::never ();
1635 dest = gimple_call_arg (stmt, 0);
1636 dest_align = get_pointer_alignment (dest);
1637 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (stmt));
1638 switch (fcode)
1640 case BUILT_IN_MEMCPY:
1641 case BUILT_IN_MEMPCPY:
1642 case BUILT_IN_MEMMOVE:
1643 src = gimple_call_arg (stmt, 1);
1644 src_align = get_pointer_alignment (src);
1645 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1646 return false;
1647 break;
1648 case BUILT_IN_MEMSET:
1649 if (!can_store_by_pieces (val, builtin_memset_read_str,
1650 gimple_call_arg (stmt, 1),
1651 dest_align, true))
1652 return false;
1653 break;
1654 case BUILT_IN_BZERO:
1655 if (!can_store_by_pieces (val, builtin_memset_read_str,
1656 integer_zero_node,
1657 dest_align, true))
1658 return false;
1659 break;
1660 default:
1661 gcc_unreachable ();
1664 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1665 tree_val = build_int_cst (get_gcov_type (), val);
1666 else
1668 HOST_WIDE_INT a[2];
1669 a[0] = (unsigned HOST_WIDE_INT) val;
1670 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1672 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1673 TYPE_PRECISION (get_gcov_type ()), false));
1676 if (dump_enabled_p ())
1677 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
1678 "Transformation done: single value %i stringop for %s\n",
1679 (int)val, built_in_names[(int)fcode]);
1681 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1683 return true;
1686 void
1687 stringop_block_profile (gimple *stmt, unsigned int *expected_align,
1688 HOST_WIDE_INT *expected_size)
1690 histogram_value histogram;
1691 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1693 if (!histogram)
1694 *expected_size = -1;
1695 else if (!histogram->hvalue.counters[1])
1697 *expected_size = -1;
1698 gimple_remove_histogram_value (cfun, stmt, histogram);
1700 else
1702 gcov_type size;
1703 size = ((histogram->hvalue.counters[0]
1704 + histogram->hvalue.counters[1] / 2)
1705 / histogram->hvalue.counters[1]);
1706 /* Even if we can hold bigger value in SIZE, INT_MAX
1707 is safe "infinity" for code generation strategies. */
1708 if (size > INT_MAX)
1709 size = INT_MAX;
1710 *expected_size = size;
1711 gimple_remove_histogram_value (cfun, stmt, histogram);
1714 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1716 if (!histogram)
1717 *expected_align = 0;
1718 else if (!histogram->hvalue.counters[0])
1720 gimple_remove_histogram_value (cfun, stmt, histogram);
1721 *expected_align = 0;
1723 else
1725 gcov_type count;
1726 unsigned int alignment;
1728 count = histogram->hvalue.counters[0];
1729 alignment = 1;
1730 while (!(count & alignment)
1731 && (alignment <= UINT_MAX / 2 / BITS_PER_UNIT))
1732 alignment <<= 1;
1733 *expected_align = alignment * BITS_PER_UNIT;
1734 gimple_remove_histogram_value (cfun, stmt, histogram);
1739 /* Find values inside STMT for that we want to measure histograms for
1740 division/modulo optimization. */
1742 static void
1743 gimple_divmod_values_to_profile (gimple *stmt, histogram_values *values)
1745 tree lhs, divisor, op0, type;
1746 histogram_value hist;
1748 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1749 return;
1751 lhs = gimple_assign_lhs (stmt);
1752 type = TREE_TYPE (lhs);
1753 if (!INTEGRAL_TYPE_P (type))
1754 return;
1756 switch (gimple_assign_rhs_code (stmt))
1758 case TRUNC_DIV_EXPR:
1759 case TRUNC_MOD_EXPR:
1760 divisor = gimple_assign_rhs2 (stmt);
1761 op0 = gimple_assign_rhs1 (stmt);
1763 if (TREE_CODE (divisor) == SSA_NAME)
1764 /* Check for the case where the divisor is the same value most
1765 of the time. */
1766 values->safe_push (gimple_alloc_histogram_value (cfun,
1767 HIST_TYPE_TOPN_VALUES,
1768 stmt, divisor));
1770 /* For mod, check whether it is not often a noop (or replaceable by
1771 a few subtractions). */
1772 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1773 && TYPE_UNSIGNED (type)
1774 && TREE_CODE (divisor) == SSA_NAME)
1776 tree val;
1777 /* Check for a special case where the divisor is power of 2. */
1778 values->safe_push (gimple_alloc_histogram_value (cfun,
1779 HIST_TYPE_POW2,
1780 stmt, divisor));
1781 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1782 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1783 stmt, val);
1784 hist->hdata.intvl.int_start = 0;
1785 hist->hdata.intvl.steps = 2;
1786 values->safe_push (hist);
1788 return;
1790 default:
1791 return;
1795 /* Find calls inside STMT for that we want to measure histograms for
1796 indirect/virtual call optimization. */
1798 static void
1799 gimple_indirect_call_to_profile (gimple *stmt, histogram_values *values)
1801 tree callee;
1803 if (gimple_code (stmt) != GIMPLE_CALL
1804 || gimple_call_internal_p (stmt)
1805 || gimple_call_fndecl (stmt) != NULL_TREE)
1806 return;
1808 callee = gimple_call_fn (stmt);
1809 histogram_value v = gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1810 stmt, callee);
1811 values->safe_push (v);
1813 return;
1816 /* Find values inside STMT for that we want to measure histograms for
1817 string operations. */
1819 static void
1820 gimple_stringops_values_to_profile (gimple *gs, histogram_values *values)
1822 gcall *stmt;
1823 tree blck_size;
1824 tree dest;
1825 int size_arg;
1827 stmt = dyn_cast <gcall *> (gs);
1828 if (!stmt)
1829 return;
1831 if (!gimple_call_builtin_p (gs, BUILT_IN_NORMAL))
1832 return;
1834 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
1835 return;
1837 dest = gimple_call_arg (stmt, 0);
1838 blck_size = gimple_call_arg (stmt, size_arg);
1840 if (TREE_CODE (blck_size) != INTEGER_CST)
1842 values->safe_push (gimple_alloc_histogram_value (cfun,
1843 HIST_TYPE_TOPN_VALUES,
1844 stmt, blck_size));
1845 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1846 stmt, blck_size));
1849 if (TREE_CODE (blck_size) != INTEGER_CST)
1850 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1851 stmt, dest));
1854 /* Find values inside STMT for that we want to measure histograms and adds
1855 them to list VALUES. */
1857 static void
1858 gimple_values_to_profile (gimple *stmt, histogram_values *values)
1860 gimple_divmod_values_to_profile (stmt, values);
1861 gimple_stringops_values_to_profile (stmt, values);
1862 gimple_indirect_call_to_profile (stmt, values);
1865 void
1866 gimple_find_values_to_profile (histogram_values *values)
1868 basic_block bb;
1869 gimple_stmt_iterator gsi;
1870 unsigned i;
1871 histogram_value hist = NULL;
1872 values->create (0);
1874 FOR_EACH_BB_FN (bb, cfun)
1875 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1876 gimple_values_to_profile (gsi_stmt (gsi), values);
1878 values->safe_push (gimple_alloc_histogram_value (cfun,
1879 HIST_TYPE_TIME_PROFILE));
1881 FOR_EACH_VEC_ELT (*values, i, hist)
1883 switch (hist->type)
1885 case HIST_TYPE_INTERVAL:
1886 hist->n_counters = hist->hdata.intvl.steps + 2;
1887 break;
1889 case HIST_TYPE_POW2:
1890 hist->n_counters = 2;
1891 break;
1893 case HIST_TYPE_TOPN_VALUES:
1894 case HIST_TYPE_INDIR_CALL:
1895 hist->n_counters = GCOV_TOPN_VALUES_COUNTERS;
1896 break;
1898 case HIST_TYPE_TIME_PROFILE:
1899 hist->n_counters = 1;
1900 break;
1902 case HIST_TYPE_AVERAGE:
1903 hist->n_counters = 2;
1904 break;
1906 case HIST_TYPE_IOR:
1907 hist->n_counters = 1;
1908 break;
1910 default:
1911 gcc_unreachable ();
1913 if (dump_file && hist->hvalue.stmt != NULL)
1915 fprintf (dump_file, "Stmt ");
1916 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
1917 dump_histogram_value (dump_file, hist);