Use -fbuilding-libgcc for more target macros used in libgcc.
[official-gcc.git] / gcc / value-prof.c
blobe3579678b71cc44f0d534dde6415089703206517
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "tree-nested.h"
26 #include "calls.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "value-prof.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "optabs.h"
36 #include "regs.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "tree-eh.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimplify.h"
44 #include "gimple-iterator.h"
45 #include "gimple-ssa.h"
46 #include "tree-cfg.h"
47 #include "tree-phinodes.h"
48 #include "ssa-iterators.h"
49 #include "stringpool.h"
50 #include "tree-ssanames.h"
51 #include "diagnostic.h"
52 #include "gimple-pretty-print.h"
53 #include "coverage.h"
54 #include "tree.h"
55 #include "gcov-io.h"
56 #include "timevar.h"
57 #include "dumpfile.h"
58 #include "profile.h"
59 #include "data-streamer.h"
60 #include "builtins.h"
61 #include "tree-nested.h"
62 #include "hash-set.h"
64 /* In this file value profile based optimizations are placed. Currently the
65 following optimizations are implemented (for more detailed descriptions
66 see comments at value_profile_transformations):
68 1) Division/modulo specialization. Provided that we can determine that the
69 operands of the division have some special properties, we may use it to
70 produce more effective code.
72 2) Indirect/virtual call specialization. If we can determine most
73 common function callee in indirect/virtual call. We can use this
74 information to improve code effectiveness (especially info for
75 the inliner).
77 3) Speculative prefetching. If we are able to determine that the difference
78 between addresses accessed by a memory reference is usually constant, we
79 may add the prefetch instructions.
80 FIXME: This transformation was removed together with RTL based value
81 profiling.
84 Value profiling internals
85 ==========================
87 Every value profiling transformation starts with defining what values
88 to profile. There are different histogram types (see HIST_TYPE_* in
89 value-prof.h) and each transformation can request one or more histogram
90 types per GIMPLE statement. The function gimple_find_values_to_profile()
91 collects the values to profile in a vec, and adds the number of counters
92 required for the different histogram types.
94 For a -fprofile-generate run, the statements for which values should be
95 recorded, are instrumented in instrument_values(). The instrumentation
96 is done by helper functions that can be found in tree-profile.c, where
97 new types of histograms can be added if necessary.
99 After a -fprofile-use, the value profiling data is read back in by
100 compute_value_histograms() that translates the collected data to
101 histograms and attaches them to the profiled statements via
102 gimple_add_histogram_value(). Histograms are stored in a hash table
103 that is attached to every intrumented function, see VALUE_HISTOGRAMS
104 in function.h.
106 The value-profile transformations driver is the function
107 gimple_value_profile_transformations(). It traverses all statements in
108 the to-be-transformed function, and looks for statements with one or
109 more histograms attached to it. If a statement has histograms, the
110 transformation functions are called on the statement.
112 Limitations / FIXME / TODO:
113 * Only one histogram of each type can be associated with a statement.
114 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
115 (This type of histogram was originally used to implement a form of
116 stride profiling based speculative prefetching to improve SPEC2000
117 scores for memory-bound benchmarks, mcf and equake. However, this
118 was an RTL value-profiling transformation, and those have all been
119 removed.)
120 * Some value profile transformations are done in builtins.c (?!)
121 * Updating of histograms needs some TLC.
122 * The value profiling code could be used to record analysis results
123 from non-profiling (e.g. VRP).
124 * Adding new profilers should be simplified, starting with a cleanup
125 of what-happens-where andwith making gimple_find_values_to_profile
126 and gimple_value_profile_transformations table-driven, perhaps...
129 static tree gimple_divmod_fixed_value (gimple, tree, int, gcov_type, gcov_type);
130 static tree gimple_mod_pow2 (gimple, int, gcov_type, gcov_type);
131 static tree gimple_mod_subtract (gimple, int, int, int, gcov_type, gcov_type,
132 gcov_type);
133 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
134 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
135 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
136 static bool gimple_stringops_transform (gimple_stmt_iterator *);
137 static bool gimple_ic_transform (gimple_stmt_iterator *);
139 /* Allocate histogram value. */
141 static histogram_value
142 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
143 enum hist_type type, gimple stmt, tree value)
145 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
146 hist->hvalue.value = value;
147 hist->hvalue.stmt = stmt;
148 hist->type = type;
149 return hist;
152 /* Hash value for histogram. */
154 static hashval_t
155 histogram_hash (const void *x)
157 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
160 /* Return nonzero if statement for histogram_value X is Y. */
162 static int
163 histogram_eq (const void *x, const void *y)
165 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
168 /* Set histogram for STMT. */
170 static void
171 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
173 void **loc;
174 if (!hist && !VALUE_HISTOGRAMS (fun))
175 return;
176 if (!VALUE_HISTOGRAMS (fun))
177 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
178 histogram_eq, NULL);
179 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
180 htab_hash_pointer (stmt),
181 hist ? INSERT : NO_INSERT);
182 if (!hist)
184 if (loc)
185 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
186 return;
188 *loc = hist;
191 /* Get histogram list for STMT. */
193 histogram_value
194 gimple_histogram_value (struct function *fun, gimple stmt)
196 if (!VALUE_HISTOGRAMS (fun))
197 return NULL;
198 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
199 htab_hash_pointer (stmt));
202 /* Add histogram for STMT. */
204 void
205 gimple_add_histogram_value (struct function *fun, gimple stmt,
206 histogram_value hist)
208 hist->hvalue.next = gimple_histogram_value (fun, stmt);
209 set_histogram_value (fun, stmt, hist);
210 hist->fun = fun;
214 /* Remove histogram HIST from STMT's histogram list. */
216 void
217 gimple_remove_histogram_value (struct function *fun, gimple stmt,
218 histogram_value hist)
220 histogram_value hist2 = gimple_histogram_value (fun, stmt);
221 if (hist == hist2)
223 set_histogram_value (fun, stmt, hist->hvalue.next);
225 else
227 while (hist2->hvalue.next != hist)
228 hist2 = hist2->hvalue.next;
229 hist2->hvalue.next = hist->hvalue.next;
231 free (hist->hvalue.counters);
232 #ifdef ENABLE_CHECKING
233 memset (hist, 0xab, sizeof (*hist));
234 #endif
235 free (hist);
239 /* Lookup histogram of type TYPE in the STMT. */
241 histogram_value
242 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
243 enum hist_type type)
245 histogram_value hist;
246 for (hist = gimple_histogram_value (fun, stmt); hist;
247 hist = hist->hvalue.next)
248 if (hist->type == type)
249 return hist;
250 return NULL;
253 /* Dump information about HIST to DUMP_FILE. */
255 static void
256 dump_histogram_value (FILE *dump_file, histogram_value hist)
258 switch (hist->type)
260 case HIST_TYPE_INTERVAL:
261 fprintf (dump_file, "Interval counter range %d -- %d",
262 hist->hdata.intvl.int_start,
263 (hist->hdata.intvl.int_start
264 + hist->hdata.intvl.steps - 1));
265 if (hist->hvalue.counters)
267 unsigned int i;
268 fprintf (dump_file, " [");
269 for (i = 0; i < hist->hdata.intvl.steps; i++)
270 fprintf (dump_file, " %d:%"PRId64,
271 hist->hdata.intvl.int_start + i,
272 (int64_t) hist->hvalue.counters[i]);
273 fprintf (dump_file, " ] outside range:%"PRId64,
274 (int64_t) hist->hvalue.counters[i]);
276 fprintf (dump_file, ".\n");
277 break;
279 case HIST_TYPE_POW2:
280 fprintf (dump_file, "Pow2 counter ");
281 if (hist->hvalue.counters)
283 fprintf (dump_file, "pow2:%"PRId64
284 " nonpow2:%"PRId64,
285 (int64_t) hist->hvalue.counters[0],
286 (int64_t) hist->hvalue.counters[1]);
288 fprintf (dump_file, ".\n");
289 break;
291 case HIST_TYPE_SINGLE_VALUE:
292 fprintf (dump_file, "Single value ");
293 if (hist->hvalue.counters)
295 fprintf (dump_file, "value:%"PRId64
296 " match:%"PRId64
297 " wrong:%"PRId64,
298 (int64_t) hist->hvalue.counters[0],
299 (int64_t) hist->hvalue.counters[1],
300 (int64_t) hist->hvalue.counters[2]);
302 fprintf (dump_file, ".\n");
303 break;
305 case HIST_TYPE_AVERAGE:
306 fprintf (dump_file, "Average value ");
307 if (hist->hvalue.counters)
309 fprintf (dump_file, "sum:%"PRId64
310 " times:%"PRId64,
311 (int64_t) hist->hvalue.counters[0],
312 (int64_t) hist->hvalue.counters[1]);
314 fprintf (dump_file, ".\n");
315 break;
317 case HIST_TYPE_IOR:
318 fprintf (dump_file, "IOR value ");
319 if (hist->hvalue.counters)
321 fprintf (dump_file, "ior:%"PRId64,
322 (int64_t) hist->hvalue.counters[0]);
324 fprintf (dump_file, ".\n");
325 break;
327 case HIST_TYPE_CONST_DELTA:
328 fprintf (dump_file, "Constant delta ");
329 if (hist->hvalue.counters)
331 fprintf (dump_file, "value:%"PRId64
332 " match:%"PRId64
333 " wrong:%"PRId64,
334 (int64_t) hist->hvalue.counters[0],
335 (int64_t) hist->hvalue.counters[1],
336 (int64_t) hist->hvalue.counters[2]);
338 fprintf (dump_file, ".\n");
339 break;
340 case HIST_TYPE_INDIR_CALL:
341 fprintf (dump_file, "Indirect call ");
342 if (hist->hvalue.counters)
344 fprintf (dump_file, "value:%"PRId64
345 " match:%"PRId64
346 " all:%"PRId64,
347 (int64_t) hist->hvalue.counters[0],
348 (int64_t) hist->hvalue.counters[1],
349 (int64_t) hist->hvalue.counters[2]);
351 fprintf (dump_file, ".\n");
352 break;
353 case HIST_TYPE_TIME_PROFILE:
354 fprintf (dump_file, "Time profile ");
355 if (hist->hvalue.counters)
357 fprintf (dump_file, "time:%"PRId64,
358 (int64_t) hist->hvalue.counters[0]);
360 fprintf (dump_file, ".\n");
361 break;
362 case HIST_TYPE_MAX:
363 gcc_unreachable ();
367 /* Dump information about HIST to DUMP_FILE. */
369 void
370 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
372 struct bitpack_d bp;
373 unsigned int i;
375 bp = bitpack_create (ob->main_stream);
376 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
377 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
378 streamer_write_bitpack (&bp);
379 switch (hist->type)
381 case HIST_TYPE_INTERVAL:
382 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
383 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
384 break;
385 default:
386 break;
388 for (i = 0; i < hist->n_counters; i++)
389 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
390 if (hist->hvalue.next)
391 stream_out_histogram_value (ob, hist->hvalue.next);
393 /* Dump information about HIST to DUMP_FILE. */
395 void
396 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
398 enum hist_type type;
399 unsigned int ncounters = 0;
400 struct bitpack_d bp;
401 unsigned int i;
402 histogram_value new_val;
403 bool next;
404 histogram_value *next_p = NULL;
408 bp = streamer_read_bitpack (ib);
409 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
410 next = bp_unpack_value (&bp, 1);
411 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
412 switch (type)
414 case HIST_TYPE_INTERVAL:
415 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
416 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
417 ncounters = new_val->hdata.intvl.steps + 2;
418 break;
420 case HIST_TYPE_POW2:
421 case HIST_TYPE_AVERAGE:
422 ncounters = 2;
423 break;
425 case HIST_TYPE_SINGLE_VALUE:
426 case HIST_TYPE_INDIR_CALL:
427 ncounters = 3;
428 break;
430 case HIST_TYPE_CONST_DELTA:
431 ncounters = 4;
432 break;
434 case HIST_TYPE_IOR:
435 case HIST_TYPE_TIME_PROFILE:
436 ncounters = 1;
437 break;
438 case HIST_TYPE_MAX:
439 gcc_unreachable ();
441 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
442 new_val->n_counters = ncounters;
443 for (i = 0; i < ncounters; i++)
444 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
445 if (!next_p)
446 gimple_add_histogram_value (cfun, stmt, new_val);
447 else
448 *next_p = new_val;
449 next_p = &new_val->hvalue.next;
451 while (next);
454 /* Dump all histograms attached to STMT to DUMP_FILE. */
456 void
457 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
459 histogram_value hist;
460 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
461 dump_histogram_value (dump_file, hist);
464 /* Remove all histograms associated with STMT. */
466 void
467 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
469 histogram_value val;
470 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
471 gimple_remove_histogram_value (fun, stmt, val);
474 /* Duplicate all histograms associates with OSTMT to STMT. */
476 void
477 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
478 struct function *ofun, gimple ostmt)
480 histogram_value val;
481 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
483 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
484 memcpy (new_val, val, sizeof (*val));
485 new_val->hvalue.stmt = stmt;
486 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
487 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
488 gimple_add_histogram_value (fun, stmt, new_val);
493 /* Move all histograms associated with OSTMT to STMT. */
495 void
496 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
498 histogram_value val = gimple_histogram_value (fun, ostmt);
499 if (val)
501 /* The following three statements can't be reordered,
502 because histogram hashtab relies on stmt field value
503 for finding the exact slot. */
504 set_histogram_value (fun, ostmt, NULL);
505 for (; val != NULL; val = val->hvalue.next)
506 val->hvalue.stmt = stmt;
507 set_histogram_value (fun, stmt, val);
511 static bool error_found = false;
513 /* Helper function for verify_histograms. For each histogram reachable via htab
514 walk verify that it was reached via statement walk. */
516 static int
517 visit_hist (void **slot, void *data)
519 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
520 histogram_value hist = *(histogram_value *) slot;
522 if (!visited->contains (hist)
523 && hist->type != HIST_TYPE_TIME_PROFILE)
525 error ("dead histogram");
526 dump_histogram_value (stderr, hist);
527 debug_gimple_stmt (hist->hvalue.stmt);
528 error_found = true;
530 return 1;
534 /* Verify sanity of the histograms. */
536 DEBUG_FUNCTION void
537 verify_histograms (void)
539 basic_block bb;
540 gimple_stmt_iterator gsi;
541 histogram_value hist;
543 error_found = false;
544 hash_set<histogram_value> visited_hists;
545 FOR_EACH_BB_FN (bb, cfun)
546 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
548 gimple stmt = gsi_stmt (gsi);
550 for (hist = gimple_histogram_value (cfun, stmt); hist;
551 hist = hist->hvalue.next)
553 if (hist->hvalue.stmt != stmt)
555 error ("Histogram value statement does not correspond to "
556 "the statement it is associated with");
557 debug_gimple_stmt (stmt);
558 dump_histogram_value (stderr, hist);
559 error_found = true;
561 visited_hists.add (hist);
564 if (VALUE_HISTOGRAMS (cfun))
565 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
566 if (error_found)
567 internal_error ("verify_histograms failed");
570 /* Helper function for verify_histograms. For each histogram reachable via htab
571 walk verify that it was reached via statement walk. */
573 static int
574 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
576 histogram_value hist = *(histogram_value *) slot;
577 free (hist->hvalue.counters);
578 #ifdef ENABLE_CHECKING
579 memset (hist, 0xab, sizeof (*hist));
580 #endif
581 free (hist);
582 return 1;
585 void
586 free_histograms (void)
588 if (VALUE_HISTOGRAMS (cfun))
590 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
591 htab_delete (VALUE_HISTOGRAMS (cfun));
592 VALUE_HISTOGRAMS (cfun) = NULL;
597 /* The overall number of invocations of the counter should match
598 execution count of basic block. Report it as error rather than
599 internal error as it might mean that user has misused the profile
600 somehow. */
602 static bool
603 check_counter (gimple stmt, const char * name,
604 gcov_type *count, gcov_type *all, gcov_type bb_count)
606 if (*all != bb_count || *count > *all)
608 location_t locus;
609 locus = (stmt != NULL)
610 ? gimple_location (stmt)
611 : DECL_SOURCE_LOCATION (current_function_decl);
612 if (flag_profile_correction)
614 if (dump_enabled_p ())
615 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
616 "correcting inconsistent value profile: %s "
617 "profiler overall count (%d) does not match BB "
618 "count (%d)\n", name, (int)*all, (int)bb_count);
619 *all = bb_count;
620 if (*count > *all)
621 *count = *all;
622 return false;
624 else
626 error_at (locus, "corrupted value profile: %s "
627 "profile counter (%d out of %d) inconsistent with "
628 "basic-block count (%d)",
629 name,
630 (int) *count,
631 (int) *all,
632 (int) bb_count);
633 return true;
637 return false;
641 /* GIMPLE based transformations. */
643 bool
644 gimple_value_profile_transformations (void)
646 basic_block bb;
647 gimple_stmt_iterator gsi;
648 bool changed = false;
650 FOR_EACH_BB_FN (bb, cfun)
652 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
654 gimple stmt = gsi_stmt (gsi);
655 histogram_value th = gimple_histogram_value (cfun, stmt);
656 if (!th)
657 continue;
659 if (dump_file)
661 fprintf (dump_file, "Trying transformations on stmt ");
662 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
663 dump_histograms_for_stmt (cfun, dump_file, stmt);
666 /* Transformations: */
667 /* The order of things in this conditional controls which
668 transformation is used when more than one is applicable. */
669 /* It is expected that any code added by the transformations
670 will be added before the current statement, and that the
671 current statement remain valid (although possibly
672 modified) upon return. */
673 if (gimple_mod_subtract_transform (&gsi)
674 || gimple_divmod_fixed_value_transform (&gsi)
675 || gimple_mod_pow2_value_transform (&gsi)
676 || gimple_stringops_transform (&gsi)
677 || gimple_ic_transform (&gsi))
679 stmt = gsi_stmt (gsi);
680 changed = true;
681 /* Original statement may no longer be in the same block. */
682 if (bb != gimple_bb (stmt))
684 bb = gimple_bb (stmt);
685 gsi = gsi_for_stmt (stmt);
691 if (changed)
693 counts_to_freqs ();
696 return changed;
700 /* Generate code for transformation 1 (with parent gimple assignment
701 STMT and probability of taking the optimal path PROB, which is
702 equivalent to COUNT/ALL within roundoff error). This generates the
703 result into a temp and returns the temp; it does not replace or
704 alter the original STMT. */
706 static tree
707 gimple_divmod_fixed_value (gimple stmt, tree value, int prob, gcov_type count,
708 gcov_type all)
710 gimple stmt1, stmt2, stmt3;
711 tree tmp0, tmp1, tmp2;
712 gimple bb1end, bb2end, bb3end;
713 basic_block bb, bb2, bb3, bb4;
714 tree optype, op1, op2;
715 edge e12, e13, e23, e24, e34;
716 gimple_stmt_iterator gsi;
718 gcc_assert (is_gimple_assign (stmt)
719 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
720 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
722 optype = TREE_TYPE (gimple_assign_lhs (stmt));
723 op1 = gimple_assign_rhs1 (stmt);
724 op2 = gimple_assign_rhs2 (stmt);
726 bb = gimple_bb (stmt);
727 gsi = gsi_for_stmt (stmt);
729 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
730 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
731 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
732 stmt2 = gimple_build_assign (tmp1, op2);
733 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
734 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
735 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
736 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
737 bb1end = stmt3;
739 tmp2 = create_tmp_reg (optype, "PROF");
740 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
741 op1, tmp0);
742 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
743 bb2end = stmt1;
745 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
746 op1, op2);
747 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
748 bb3end = stmt1;
750 /* Fix CFG. */
751 /* Edge e23 connects bb2 to bb3, etc. */
752 e12 = split_block (bb, bb1end);
753 bb2 = e12->dest;
754 bb2->count = count;
755 e23 = split_block (bb2, bb2end);
756 bb3 = e23->dest;
757 bb3->count = all - count;
758 e34 = split_block (bb3, bb3end);
759 bb4 = e34->dest;
760 bb4->count = all;
762 e12->flags &= ~EDGE_FALLTHRU;
763 e12->flags |= EDGE_FALSE_VALUE;
764 e12->probability = prob;
765 e12->count = count;
767 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
768 e13->probability = REG_BR_PROB_BASE - prob;
769 e13->count = all - count;
771 remove_edge (e23);
773 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
774 e24->probability = REG_BR_PROB_BASE;
775 e24->count = count;
777 e34->probability = REG_BR_PROB_BASE;
778 e34->count = all - count;
780 return tmp2;
784 /* Do transform 1) on INSN if applicable. */
786 static bool
787 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
789 histogram_value histogram;
790 enum tree_code code;
791 gcov_type val, count, all;
792 tree result, value, tree_val;
793 gcov_type prob;
794 gimple stmt;
796 stmt = gsi_stmt (*si);
797 if (gimple_code (stmt) != GIMPLE_ASSIGN)
798 return false;
800 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
801 return false;
803 code = gimple_assign_rhs_code (stmt);
805 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
806 return false;
808 histogram = gimple_histogram_value_of_type (cfun, stmt,
809 HIST_TYPE_SINGLE_VALUE);
810 if (!histogram)
811 return false;
813 value = histogram->hvalue.value;
814 val = histogram->hvalue.counters[0];
815 count = histogram->hvalue.counters[1];
816 all = histogram->hvalue.counters[2];
817 gimple_remove_histogram_value (cfun, stmt, histogram);
819 /* We require that count is at least half of all; this means
820 that for the transformation to fire the value must be constant
821 at least 50% of time (and 75% gives the guarantee of usage). */
822 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
823 || 2 * count < all
824 || optimize_bb_for_size_p (gimple_bb (stmt)))
825 return false;
827 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
828 return false;
830 /* Compute probability of taking the optimal path. */
831 if (all > 0)
832 prob = GCOV_COMPUTE_SCALE (count, all);
833 else
834 prob = 0;
836 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
837 tree_val = build_int_cst (get_gcov_type (), val);
838 else
840 HOST_WIDE_INT a[2];
841 a[0] = (unsigned HOST_WIDE_INT) val;
842 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
844 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
845 TYPE_PRECISION (get_gcov_type ()), false));
847 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
849 if (dump_file)
851 fprintf (dump_file, "Div/mod by constant ");
852 print_generic_expr (dump_file, value, TDF_SLIM);
853 fprintf (dump_file, "=");
854 print_generic_expr (dump_file, tree_val, TDF_SLIM);
855 fprintf (dump_file, " transformation on insn ");
856 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
859 gimple_assign_set_rhs_from_tree (si, result);
860 update_stmt (gsi_stmt (*si));
862 return true;
865 /* Generate code for transformation 2 (with parent gimple assign STMT and
866 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
867 within roundoff error). This generates the result into a temp and returns
868 the temp; it does not replace or alter the original STMT. */
869 static tree
870 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
872 gimple stmt1, stmt2, stmt3, stmt4;
873 tree tmp2, tmp3;
874 gimple bb1end, bb2end, bb3end;
875 basic_block bb, bb2, bb3, bb4;
876 tree optype, op1, op2;
877 edge e12, e13, e23, e24, e34;
878 gimple_stmt_iterator gsi;
879 tree result;
881 gcc_assert (is_gimple_assign (stmt)
882 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
884 optype = TREE_TYPE (gimple_assign_lhs (stmt));
885 op1 = gimple_assign_rhs1 (stmt);
886 op2 = gimple_assign_rhs2 (stmt);
888 bb = gimple_bb (stmt);
889 gsi = gsi_for_stmt (stmt);
891 result = create_tmp_reg (optype, "PROF");
892 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
893 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
894 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
895 build_int_cst (optype, -1));
896 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
897 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
898 NULL_TREE, NULL_TREE);
899 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
900 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
901 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
902 bb1end = stmt4;
904 /* tmp2 == op2-1 inherited from previous block. */
905 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
906 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
907 bb2end = stmt1;
909 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
910 op1, op2);
911 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
912 bb3end = stmt1;
914 /* Fix CFG. */
915 /* Edge e23 connects bb2 to bb3, etc. */
916 e12 = split_block (bb, bb1end);
917 bb2 = e12->dest;
918 bb2->count = count;
919 e23 = split_block (bb2, bb2end);
920 bb3 = e23->dest;
921 bb3->count = all - count;
922 e34 = split_block (bb3, bb3end);
923 bb4 = e34->dest;
924 bb4->count = all;
926 e12->flags &= ~EDGE_FALLTHRU;
927 e12->flags |= EDGE_FALSE_VALUE;
928 e12->probability = prob;
929 e12->count = count;
931 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
932 e13->probability = REG_BR_PROB_BASE - prob;
933 e13->count = all - count;
935 remove_edge (e23);
937 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
938 e24->probability = REG_BR_PROB_BASE;
939 e24->count = count;
941 e34->probability = REG_BR_PROB_BASE;
942 e34->count = all - count;
944 return result;
947 /* Do transform 2) on INSN if applicable. */
948 static bool
949 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
951 histogram_value histogram;
952 enum tree_code code;
953 gcov_type count, wrong_values, all;
954 tree lhs_type, result, value;
955 gcov_type prob;
956 gimple stmt;
958 stmt = gsi_stmt (*si);
959 if (gimple_code (stmt) != GIMPLE_ASSIGN)
960 return false;
962 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
963 if (!INTEGRAL_TYPE_P (lhs_type))
964 return false;
966 code = gimple_assign_rhs_code (stmt);
968 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
969 return false;
971 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
972 if (!histogram)
973 return false;
975 value = histogram->hvalue.value;
976 wrong_values = histogram->hvalue.counters[0];
977 count = histogram->hvalue.counters[1];
979 gimple_remove_histogram_value (cfun, stmt, histogram);
981 /* We require that we hit a power of 2 at least half of all evaluations. */
982 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
983 || count < wrong_values
984 || optimize_bb_for_size_p (gimple_bb (stmt)))
985 return false;
987 if (dump_file)
989 fprintf (dump_file, "Mod power of 2 transformation on insn ");
990 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
993 /* Compute probability of taking the optimal path. */
994 all = count + wrong_values;
996 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
997 return false;
999 if (all > 0)
1000 prob = GCOV_COMPUTE_SCALE (count, all);
1001 else
1002 prob = 0;
1004 result = gimple_mod_pow2 (stmt, prob, count, all);
1006 gimple_assign_set_rhs_from_tree (si, result);
1007 update_stmt (gsi_stmt (*si));
1009 return true;
1012 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1013 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1014 supported and this is built into this interface. The probabilities of taking
1015 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1016 COUNT2/ALL respectively within roundoff error). This generates the
1017 result into a temp and returns the temp; it does not replace or alter
1018 the original STMT. */
1019 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1021 static tree
1022 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
1023 gcov_type count1, gcov_type count2, gcov_type all)
1025 gimple stmt1, stmt2, stmt3;
1026 tree tmp1;
1027 gimple bb1end, bb2end = NULL, bb3end;
1028 basic_block bb, bb2, bb3, bb4;
1029 tree optype, op1, op2;
1030 edge e12, e23 = 0, e24, e34, e14;
1031 gimple_stmt_iterator gsi;
1032 tree result;
1034 gcc_assert (is_gimple_assign (stmt)
1035 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1037 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1038 op1 = gimple_assign_rhs1 (stmt);
1039 op2 = gimple_assign_rhs2 (stmt);
1041 bb = gimple_bb (stmt);
1042 gsi = gsi_for_stmt (stmt);
1044 result = create_tmp_reg (optype, "PROF");
1045 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1046 stmt1 = gimple_build_assign (result, op1);
1047 stmt2 = gimple_build_assign (tmp1, op2);
1048 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1049 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1050 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1051 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1052 bb1end = stmt3;
1054 if (ncounts) /* Assumed to be 0 or 1 */
1056 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
1057 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1058 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1059 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1060 bb2end = stmt2;
1063 /* Fallback case. */
1064 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
1065 result, tmp1);
1066 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1067 bb3end = stmt1;
1069 /* Fix CFG. */
1070 /* Edge e23 connects bb2 to bb3, etc. */
1071 /* However block 3 is optional; if it is not there, references
1072 to 3 really refer to block 2. */
1073 e12 = split_block (bb, bb1end);
1074 bb2 = e12->dest;
1075 bb2->count = all - count1;
1077 if (ncounts) /* Assumed to be 0 or 1. */
1079 e23 = split_block (bb2, bb2end);
1080 bb3 = e23->dest;
1081 bb3->count = all - count1 - count2;
1084 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1085 bb4 = e34->dest;
1086 bb4->count = all;
1088 e12->flags &= ~EDGE_FALLTHRU;
1089 e12->flags |= EDGE_FALSE_VALUE;
1090 e12->probability = REG_BR_PROB_BASE - prob1;
1091 e12->count = all - count1;
1093 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1094 e14->probability = prob1;
1095 e14->count = count1;
1097 if (ncounts) /* Assumed to be 0 or 1. */
1099 e23->flags &= ~EDGE_FALLTHRU;
1100 e23->flags |= EDGE_FALSE_VALUE;
1101 e23->count = all - count1 - count2;
1102 e23->probability = REG_BR_PROB_BASE - prob2;
1104 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1105 e24->probability = prob2;
1106 e24->count = count2;
1109 e34->probability = REG_BR_PROB_BASE;
1110 e34->count = all - count1 - count2;
1112 return result;
1116 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1118 static bool
1119 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1121 histogram_value histogram;
1122 enum tree_code code;
1123 gcov_type count, wrong_values, all;
1124 tree lhs_type, result;
1125 gcov_type prob1, prob2;
1126 unsigned int i, steps;
1127 gcov_type count1, count2;
1128 gimple stmt;
1130 stmt = gsi_stmt (*si);
1131 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1132 return false;
1134 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1135 if (!INTEGRAL_TYPE_P (lhs_type))
1136 return false;
1138 code = gimple_assign_rhs_code (stmt);
1140 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1141 return false;
1143 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1144 if (!histogram)
1145 return false;
1147 all = 0;
1148 wrong_values = 0;
1149 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1150 all += histogram->hvalue.counters[i];
1152 wrong_values += histogram->hvalue.counters[i];
1153 wrong_values += histogram->hvalue.counters[i+1];
1154 steps = histogram->hdata.intvl.steps;
1155 all += wrong_values;
1156 count1 = histogram->hvalue.counters[0];
1157 count2 = histogram->hvalue.counters[1];
1159 /* Compute probability of taking the optimal path. */
1160 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1162 gimple_remove_histogram_value (cfun, stmt, histogram);
1163 return false;
1166 if (flag_profile_correction && count1 + count2 > all)
1167 all = count1 + count2;
1169 gcc_assert (count1 + count2 <= all);
1171 /* We require that we use just subtractions in at least 50% of all
1172 evaluations. */
1173 count = 0;
1174 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1176 count += histogram->hvalue.counters[i];
1177 if (count * 2 >= all)
1178 break;
1180 if (i == steps
1181 || optimize_bb_for_size_p (gimple_bb (stmt)))
1182 return false;
1184 gimple_remove_histogram_value (cfun, stmt, histogram);
1185 if (dump_file)
1187 fprintf (dump_file, "Mod subtract transformation on insn ");
1188 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1191 /* Compute probability of taking the optimal path(s). */
1192 if (all > 0)
1194 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1195 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1197 else
1199 prob1 = prob2 = 0;
1202 /* In practice, "steps" is always 2. This interface reflects this,
1203 and will need to be changed if "steps" can change. */
1204 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1206 gimple_assign_set_rhs_from_tree (si, result);
1207 update_stmt (gsi_stmt (*si));
1209 return true;
1212 struct profile_id_traits : default_hashmap_traits
1214 template<typename T>
1215 static bool
1216 is_deleted (T &e)
1218 return e.m_key == UINT_MAX;
1221 template<typename T> static bool is_empty (T &e) { return e.m_key == 0; }
1222 template<typename T> static void mark_deleted (T &e) { e.m_key = UINT_MAX; }
1223 template<typename T> static void mark_empty (T &e) { e.m_key = 0; }
1226 static hash_map<unsigned int, cgraph_node *, profile_id_traits> *
1227 cgraph_node_map = 0;
1229 /* Returns true if node graph is initialized. This
1230 is used to test if profile_id has been created
1231 for cgraph_nodes. */
1233 bool
1234 coverage_node_map_initialized_p (void)
1236 return cgraph_node_map != 0;
1239 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1240 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1241 that the PROFILE_IDs was already assigned. */
1243 void
1244 init_node_map (bool local)
1246 struct cgraph_node *n;
1247 cgraph_node_map
1248 = new hash_map<unsigned int, cgraph_node *, profile_id_traits>;
1250 FOR_EACH_DEFINED_FUNCTION (n)
1251 if (n->has_gimple_body_p ())
1253 cgraph_node **val;
1254 if (local)
1256 n->profile_id = coverage_compute_profile_id (n);
1257 while ((val = cgraph_node_map->get (n->profile_id))
1258 || !n->profile_id)
1260 if (dump_file)
1261 fprintf (dump_file, "Local profile-id %i conflict"
1262 " with nodes %s/%i %s/%i\n",
1263 n->profile_id,
1264 n->name (),
1265 n->order,
1266 (*val)->name (),
1267 (*val)->order);
1268 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1271 else if (!n->profile_id)
1273 if (dump_file)
1274 fprintf (dump_file,
1275 "Node %s/%i has no profile-id"
1276 " (profile feedback missing?)\n",
1277 n->name (),
1278 n->order);
1279 continue;
1281 else if ((val = cgraph_node_map->get (n->profile_id)))
1283 if (dump_file)
1284 fprintf (dump_file,
1285 "Node %s/%i has IP profile-id %i conflict. "
1286 "Giving up.\n",
1287 n->name (),
1288 n->order,
1289 n->profile_id);
1290 *val = NULL;
1291 continue;
1293 cgraph_node_map->put (n->profile_id, n);
1297 /* Delete the CGRAPH_NODE_MAP. */
1299 void
1300 del_node_map (void)
1302 delete cgraph_node_map;
1305 /* Return cgraph node for function with pid */
1307 struct cgraph_node*
1308 find_func_by_profile_id (int profile_id)
1310 cgraph_node **val = cgraph_node_map->get (profile_id);
1311 if (val)
1312 return *val;
1313 else
1314 return NULL;
1317 /* Perform sanity check on the indirect call target. Due to race conditions,
1318 false function target may be attributed to an indirect call site. If the
1319 call expression type mismatches with the target function's type, expand_call
1320 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1321 Returns true if TARGET is considered ok for call CALL_STMT. */
1323 static bool
1324 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1326 location_t locus;
1327 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1328 return true;
1330 locus = gimple_location (call_stmt);
1331 if (dump_enabled_p ())
1332 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1333 "Skipping target %s with mismatching types for icall\n",
1334 target->name ());
1335 return false;
1338 /* Do transformation
1340 if (actual_callee_address == address_of_most_common_function/method)
1341 do direct call
1342 else
1343 old call
1346 gimple
1347 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1348 int prob, gcov_type count, gcov_type all)
1350 gimple dcall_stmt, load_stmt, cond_stmt;
1351 tree tmp0, tmp1, tmp;
1352 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1353 tree optype = build_pointer_type (void_type_node);
1354 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1355 gimple_stmt_iterator gsi;
1356 int lp_nr, dflags;
1357 edge e_eh, e;
1358 edge_iterator ei;
1359 gimple_stmt_iterator psi;
1361 cond_bb = gimple_bb (icall_stmt);
1362 gsi = gsi_for_stmt (icall_stmt);
1364 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1365 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1366 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1367 load_stmt = gimple_build_assign (tmp0, tmp);
1368 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1370 tmp = fold_convert (optype, build_addr (direct_call->decl,
1371 current_function_decl));
1372 load_stmt = gimple_build_assign (tmp1, tmp);
1373 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1375 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1376 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1378 gimple_set_vdef (icall_stmt, NULL_TREE);
1379 gimple_set_vuse (icall_stmt, NULL_TREE);
1380 update_stmt (icall_stmt);
1381 dcall_stmt = gimple_copy (icall_stmt);
1382 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1383 dflags = flags_from_decl_or_type (direct_call->decl);
1384 if ((dflags & ECF_NORETURN) != 0)
1385 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1386 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1388 /* Fix CFG. */
1389 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1390 e_cd = split_block (cond_bb, cond_stmt);
1391 dcall_bb = e_cd->dest;
1392 dcall_bb->count = count;
1394 e_di = split_block (dcall_bb, dcall_stmt);
1395 icall_bb = e_di->dest;
1396 icall_bb->count = all - count;
1398 /* Do not disturb existing EH edges from the indirect call. */
1399 if (!stmt_ends_bb_p (icall_stmt))
1400 e_ij = split_block (icall_bb, icall_stmt);
1401 else
1403 e_ij = find_fallthru_edge (icall_bb->succs);
1404 /* The indirect call might be noreturn. */
1405 if (e_ij != NULL)
1407 e_ij->probability = REG_BR_PROB_BASE;
1408 e_ij->count = all - count;
1409 e_ij = single_pred_edge (split_edge (e_ij));
1412 if (e_ij != NULL)
1414 join_bb = e_ij->dest;
1415 join_bb->count = all;
1418 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1419 e_cd->probability = prob;
1420 e_cd->count = count;
1422 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1423 e_ci->probability = REG_BR_PROB_BASE - prob;
1424 e_ci->count = all - count;
1426 remove_edge (e_di);
1428 if (e_ij != NULL)
1430 if ((dflags & ECF_NORETURN) != 0)
1431 e_ij->count = all;
1432 else
1434 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1435 e_dj->probability = REG_BR_PROB_BASE;
1436 e_dj->count = count;
1438 e_ij->count = all - count;
1440 e_ij->probability = REG_BR_PROB_BASE;
1443 /* Insert PHI node for the call result if necessary. */
1444 if (gimple_call_lhs (icall_stmt)
1445 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1446 && (dflags & ECF_NORETURN) == 0)
1448 tree result = gimple_call_lhs (icall_stmt);
1449 gimple phi = create_phi_node (result, join_bb);
1450 gimple_call_set_lhs (icall_stmt,
1451 duplicate_ssa_name (result, icall_stmt));
1452 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1453 gimple_call_set_lhs (dcall_stmt,
1454 duplicate_ssa_name (result, dcall_stmt));
1455 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1458 /* Build an EH edge for the direct call if necessary. */
1459 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1460 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1462 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1465 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1466 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1468 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1469 for (psi = gsi_start_phis (e_eh->dest);
1470 !gsi_end_p (psi); gsi_next (&psi))
1472 gimple phi = gsi_stmt (psi);
1473 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1474 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1477 return dcall_stmt;
1481 For every checked indirect/virtual call determine if most common pid of
1482 function/class method has probability more than 50%. If yes modify code of
1483 this call to:
1486 static bool
1487 gimple_ic_transform (gimple_stmt_iterator *gsi)
1489 gimple stmt = gsi_stmt (*gsi);
1490 histogram_value histogram;
1491 gcov_type val, count, all, bb_all;
1492 struct cgraph_node *direct_call;
1494 if (gimple_code (stmt) != GIMPLE_CALL)
1495 return false;
1497 if (gimple_call_fndecl (stmt) != NULL_TREE)
1498 return false;
1500 if (gimple_call_internal_p (stmt))
1501 return false;
1503 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1504 if (!histogram)
1505 return false;
1507 val = histogram->hvalue.counters [0];
1508 count = histogram->hvalue.counters [1];
1509 all = histogram->hvalue.counters [2];
1511 bb_all = gimple_bb (stmt)->count;
1512 /* The order of CHECK_COUNTER calls is important -
1513 since check_counter can correct the third parameter
1514 and we want to make count <= all <= bb_all. */
1515 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1516 || check_counter (stmt, "ic", &count, &all, all))
1518 gimple_remove_histogram_value (cfun, stmt, histogram);
1519 return false;
1522 if (4 * count <= 3 * all)
1523 return false;
1525 direct_call = find_func_by_profile_id ((int)val);
1527 if (direct_call == NULL)
1529 if (val)
1531 if (dump_file)
1533 fprintf (dump_file, "Indirect call -> direct call from other module");
1534 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1535 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1538 return false;
1541 if (!check_ic_target (stmt, direct_call))
1543 if (dump_file)
1545 fprintf (dump_file, "Indirect call -> direct call ");
1546 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1547 fprintf (dump_file, "=> ");
1548 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1549 fprintf (dump_file, " transformation skipped because of type mismatch");
1550 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1552 gimple_remove_histogram_value (cfun, stmt, histogram);
1553 return false;
1556 if (dump_file)
1558 fprintf (dump_file, "Indirect call -> direct call ");
1559 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1560 fprintf (dump_file, "=> ");
1561 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1562 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1563 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1564 fprintf (dump_file, "hist->count %"PRId64
1565 " hist->all %"PRId64"\n", count, all);
1568 return true;
1571 /* Return true if the stringop CALL with FNDECL shall be profiled.
1572 SIZE_ARG be set to the argument index for the size of the string
1573 operation.
1575 static bool
1576 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1578 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1580 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1581 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1582 return false;
1584 switch (fcode)
1586 case BUILT_IN_MEMCPY:
1587 case BUILT_IN_MEMPCPY:
1588 *size_arg = 2;
1589 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1590 INTEGER_TYPE, VOID_TYPE);
1591 case BUILT_IN_MEMSET:
1592 *size_arg = 2;
1593 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1594 INTEGER_TYPE, VOID_TYPE);
1595 case BUILT_IN_BZERO:
1596 *size_arg = 1;
1597 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1598 VOID_TYPE);
1599 default:
1600 gcc_unreachable ();
1604 /* Convert stringop (..., vcall_size)
1605 into
1606 if (vcall_size == icall_size)
1607 stringop (..., icall_size);
1608 else
1609 stringop (..., vcall_size);
1610 assuming we'll propagate a true constant into ICALL_SIZE later. */
1612 static void
1613 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1614 gcov_type count, gcov_type all)
1616 gimple tmp_stmt, cond_stmt, icall_stmt;
1617 tree tmp0, tmp1, vcall_size, optype;
1618 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1619 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1620 gimple_stmt_iterator gsi;
1621 tree fndecl;
1622 int size_arg;
1624 fndecl = gimple_call_fndecl (vcall_stmt);
1625 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1626 gcc_unreachable ();
1628 cond_bb = gimple_bb (vcall_stmt);
1629 gsi = gsi_for_stmt (vcall_stmt);
1631 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1632 optype = TREE_TYPE (vcall_size);
1634 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1635 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1636 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1637 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1639 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1640 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1642 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1643 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1645 gimple_set_vdef (vcall_stmt, NULL);
1646 gimple_set_vuse (vcall_stmt, NULL);
1647 update_stmt (vcall_stmt);
1648 icall_stmt = gimple_copy (vcall_stmt);
1649 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1650 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1652 /* Fix CFG. */
1653 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1654 e_ci = split_block (cond_bb, cond_stmt);
1655 icall_bb = e_ci->dest;
1656 icall_bb->count = count;
1658 e_iv = split_block (icall_bb, icall_stmt);
1659 vcall_bb = e_iv->dest;
1660 vcall_bb->count = all - count;
1662 e_vj = split_block (vcall_bb, vcall_stmt);
1663 join_bb = e_vj->dest;
1664 join_bb->count = all;
1666 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1667 e_ci->probability = prob;
1668 e_ci->count = count;
1670 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1671 e_cv->probability = REG_BR_PROB_BASE - prob;
1672 e_cv->count = all - count;
1674 remove_edge (e_iv);
1676 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1677 e_ij->probability = REG_BR_PROB_BASE;
1678 e_ij->count = count;
1680 e_vj->probability = REG_BR_PROB_BASE;
1681 e_vj->count = all - count;
1683 /* Insert PHI node for the call result if necessary. */
1684 if (gimple_call_lhs (vcall_stmt)
1685 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1687 tree result = gimple_call_lhs (vcall_stmt);
1688 gimple phi = create_phi_node (result, join_bb);
1689 gimple_call_set_lhs (vcall_stmt,
1690 duplicate_ssa_name (result, vcall_stmt));
1691 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1692 gimple_call_set_lhs (icall_stmt,
1693 duplicate_ssa_name (result, icall_stmt));
1694 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1697 /* Because these are all string op builtins, they're all nothrow. */
1698 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1699 gcc_assert (!stmt_could_throw_p (icall_stmt));
1702 /* Find values inside STMT for that we want to measure histograms for
1703 division/modulo optimization. */
1704 static bool
1705 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1707 gimple stmt = gsi_stmt (*gsi);
1708 tree fndecl;
1709 tree blck_size;
1710 enum built_in_function fcode;
1711 histogram_value histogram;
1712 gcov_type count, all, val;
1713 tree dest, src;
1714 unsigned int dest_align, src_align;
1715 gcov_type prob;
1716 tree tree_val;
1717 int size_arg;
1719 if (gimple_code (stmt) != GIMPLE_CALL)
1720 return false;
1721 fndecl = gimple_call_fndecl (stmt);
1722 if (!fndecl)
1723 return false;
1724 fcode = DECL_FUNCTION_CODE (fndecl);
1725 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1726 return false;
1728 blck_size = gimple_call_arg (stmt, size_arg);
1729 if (TREE_CODE (blck_size) == INTEGER_CST)
1730 return false;
1732 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1733 if (!histogram)
1734 return false;
1735 val = histogram->hvalue.counters[0];
1736 count = histogram->hvalue.counters[1];
1737 all = histogram->hvalue.counters[2];
1738 gimple_remove_histogram_value (cfun, stmt, histogram);
1739 /* We require that count is at least half of all; this means
1740 that for the transformation to fire the value must be constant
1741 at least 80% of time. */
1742 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1743 return false;
1744 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1745 return false;
1746 if (all > 0)
1747 prob = GCOV_COMPUTE_SCALE (count, all);
1748 else
1749 prob = 0;
1750 dest = gimple_call_arg (stmt, 0);
1751 dest_align = get_pointer_alignment (dest);
1752 switch (fcode)
1754 case BUILT_IN_MEMCPY:
1755 case BUILT_IN_MEMPCPY:
1756 src = gimple_call_arg (stmt, 1);
1757 src_align = get_pointer_alignment (src);
1758 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1759 return false;
1760 break;
1761 case BUILT_IN_MEMSET:
1762 if (!can_store_by_pieces (val, builtin_memset_read_str,
1763 gimple_call_arg (stmt, 1),
1764 dest_align, true))
1765 return false;
1766 break;
1767 case BUILT_IN_BZERO:
1768 if (!can_store_by_pieces (val, builtin_memset_read_str,
1769 integer_zero_node,
1770 dest_align, true))
1771 return false;
1772 break;
1773 default:
1774 gcc_unreachable ();
1776 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1777 tree_val = build_int_cst (get_gcov_type (), val);
1778 else
1780 HOST_WIDE_INT a[2];
1781 a[0] = (unsigned HOST_WIDE_INT) val;
1782 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1784 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1785 TYPE_PRECISION (get_gcov_type ()), false));
1788 if (dump_file)
1790 fprintf (dump_file, "Single value %i stringop transformation on ",
1791 (int)val);
1792 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1794 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1796 return true;
1799 void
1800 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1801 HOST_WIDE_INT *expected_size)
1803 histogram_value histogram;
1804 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1805 if (!histogram)
1806 *expected_size = -1;
1807 else if (!histogram->hvalue.counters[1])
1809 *expected_size = -1;
1810 gimple_remove_histogram_value (cfun, stmt, histogram);
1812 else
1814 gcov_type size;
1815 size = ((histogram->hvalue.counters[0]
1816 + histogram->hvalue.counters[1] / 2)
1817 / histogram->hvalue.counters[1]);
1818 /* Even if we can hold bigger value in SIZE, INT_MAX
1819 is safe "infinity" for code generation strategies. */
1820 if (size > INT_MAX)
1821 size = INT_MAX;
1822 *expected_size = size;
1823 gimple_remove_histogram_value (cfun, stmt, histogram);
1825 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1826 if (!histogram)
1827 *expected_align = 0;
1828 else if (!histogram->hvalue.counters[0])
1830 gimple_remove_histogram_value (cfun, stmt, histogram);
1831 *expected_align = 0;
1833 else
1835 gcov_type count;
1836 int alignment;
1838 count = histogram->hvalue.counters[0];
1839 alignment = 1;
1840 while (!(count & alignment)
1841 && (alignment * 2 * BITS_PER_UNIT))
1842 alignment <<= 1;
1843 *expected_align = alignment * BITS_PER_UNIT;
1844 gimple_remove_histogram_value (cfun, stmt, histogram);
1849 /* Find values inside STMT for that we want to measure histograms for
1850 division/modulo optimization. */
1851 static void
1852 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1854 tree lhs, divisor, op0, type;
1855 histogram_value hist;
1857 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1858 return;
1860 lhs = gimple_assign_lhs (stmt);
1861 type = TREE_TYPE (lhs);
1862 if (!INTEGRAL_TYPE_P (type))
1863 return;
1865 switch (gimple_assign_rhs_code (stmt))
1867 case TRUNC_DIV_EXPR:
1868 case TRUNC_MOD_EXPR:
1869 divisor = gimple_assign_rhs2 (stmt);
1870 op0 = gimple_assign_rhs1 (stmt);
1872 values->reserve (3);
1874 if (TREE_CODE (divisor) == SSA_NAME)
1875 /* Check for the case where the divisor is the same value most
1876 of the time. */
1877 values->quick_push (gimple_alloc_histogram_value (cfun,
1878 HIST_TYPE_SINGLE_VALUE,
1879 stmt, divisor));
1881 /* For mod, check whether it is not often a noop (or replaceable by
1882 a few subtractions). */
1883 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1884 && TYPE_UNSIGNED (type))
1886 tree val;
1887 /* Check for a special case where the divisor is power of 2. */
1888 values->quick_push (gimple_alloc_histogram_value (cfun,
1889 HIST_TYPE_POW2,
1890 stmt, divisor));
1892 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1893 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1894 stmt, val);
1895 hist->hdata.intvl.int_start = 0;
1896 hist->hdata.intvl.steps = 2;
1897 values->quick_push (hist);
1899 return;
1901 default:
1902 return;
1906 /* Find calls inside STMT for that we want to measure histograms for
1907 indirect/virtual call optimization. */
1909 static void
1910 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1912 tree callee;
1914 if (gimple_code (stmt) != GIMPLE_CALL
1915 || gimple_call_internal_p (stmt)
1916 || gimple_call_fndecl (stmt) != NULL_TREE)
1917 return;
1919 callee = gimple_call_fn (stmt);
1921 values->reserve (3);
1923 values->quick_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1924 stmt, callee));
1926 return;
1929 /* Find values inside STMT for that we want to measure histograms for
1930 string operations. */
1931 static void
1932 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1934 tree fndecl;
1935 tree blck_size;
1936 tree dest;
1937 int size_arg;
1939 if (gimple_code (stmt) != GIMPLE_CALL)
1940 return;
1941 fndecl = gimple_call_fndecl (stmt);
1942 if (!fndecl)
1943 return;
1945 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1946 return;
1948 dest = gimple_call_arg (stmt, 0);
1949 blck_size = gimple_call_arg (stmt, size_arg);
1951 if (TREE_CODE (blck_size) != INTEGER_CST)
1953 values->safe_push (gimple_alloc_histogram_value (cfun,
1954 HIST_TYPE_SINGLE_VALUE,
1955 stmt, blck_size));
1956 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1957 stmt, blck_size));
1959 if (TREE_CODE (blck_size) != INTEGER_CST)
1960 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1961 stmt, dest));
1964 /* Find values inside STMT for that we want to measure histograms and adds
1965 them to list VALUES. */
1967 static void
1968 gimple_values_to_profile (gimple stmt, histogram_values *values)
1970 gimple_divmod_values_to_profile (stmt, values);
1971 gimple_stringops_values_to_profile (stmt, values);
1972 gimple_indirect_call_to_profile (stmt, values);
1975 void
1976 gimple_find_values_to_profile (histogram_values *values)
1978 basic_block bb;
1979 gimple_stmt_iterator gsi;
1980 unsigned i;
1981 histogram_value hist = NULL;
1982 values->create (0);
1984 FOR_EACH_BB_FN (bb, cfun)
1985 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1986 gimple_values_to_profile (gsi_stmt (gsi), values);
1988 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
1990 FOR_EACH_VEC_ELT (*values, i, hist)
1992 switch (hist->type)
1994 case HIST_TYPE_INTERVAL:
1995 hist->n_counters = hist->hdata.intvl.steps + 2;
1996 break;
1998 case HIST_TYPE_POW2:
1999 hist->n_counters = 2;
2000 break;
2002 case HIST_TYPE_SINGLE_VALUE:
2003 hist->n_counters = 3;
2004 break;
2006 case HIST_TYPE_CONST_DELTA:
2007 hist->n_counters = 4;
2008 break;
2010 case HIST_TYPE_INDIR_CALL:
2011 hist->n_counters = 3;
2012 break;
2014 case HIST_TYPE_TIME_PROFILE:
2015 hist->n_counters = 1;
2016 break;
2018 case HIST_TYPE_AVERAGE:
2019 hist->n_counters = 2;
2020 break;
2022 case HIST_TYPE_IOR:
2023 hist->n_counters = 1;
2024 break;
2026 default:
2027 gcc_unreachable ();
2029 if (dump_file)
2031 fprintf (dump_file, "Stmt ");
2032 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2033 dump_histogram_value (dump_file, hist);