* gcc.dg/c11-complex-1.c: Use dg-add-options ieee.
[official-gcc.git] / gcc / value-prof.c
blobf21ff00f2cbed5ac9747f7f64e5b0ce8a35191f8
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2013 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 "rtl.h"
26 #include "expr.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "value-prof.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "optabs.h"
34 #include "regs.h"
35 #include "ggc.h"
36 #include "gimple.h"
37 #include "gimplify.h"
38 #include "gimple-iterator.h"
39 #include "gimple-ssa.h"
40 #include "tree-cfg.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "tree-ssanames.h"
44 #include "diagnostic.h"
45 #include "gimple-pretty-print.h"
46 #include "coverage.h"
47 #include "tree.h"
48 #include "gcov-io.h"
49 #include "timevar.h"
50 #include "dumpfile.h"
51 #include "pointer-set.h"
52 #include "profile.h"
53 #include "data-streamer.h"
54 #include "builtins.h"
55 #include "tree-nested.h"
57 /* In this file value profile based optimizations are placed. Currently the
58 following optimizations are implemented (for more detailed descriptions
59 see comments at value_profile_transformations):
61 1) Division/modulo specialization. Provided that we can determine that the
62 operands of the division have some special properties, we may use it to
63 produce more effective code.
65 2) Indirect/virtual call specialization. If we can determine most
66 common function callee in indirect/virtual call. We can use this
67 information to improve code effectiveness (especially info for
68 the inliner).
70 3) Speculative prefetching. If we are able to determine that the difference
71 between addresses accessed by a memory reference is usually constant, we
72 may add the prefetch instructions.
73 FIXME: This transformation was removed together with RTL based value
74 profiling.
77 Value profiling internals
78 ==========================
80 Every value profiling transformation starts with defining what values
81 to profile. There are different histogram types (see HIST_TYPE_* in
82 value-prof.h) and each transformation can request one or more histogram
83 types per GIMPLE statement. The function gimple_find_values_to_profile()
84 collects the values to profile in a vec, and adds the number of counters
85 required for the different histogram types.
87 For a -fprofile-generate run, the statements for which values should be
88 recorded, are instrumented in instrument_values(). The instrumentation
89 is done by helper functions that can be found in tree-profile.c, where
90 new types of histograms can be added if necessary.
92 After a -fprofile-use, the value profiling data is read back in by
93 compute_value_histograms() that translates the collected data to
94 histograms and attaches them to the profiled statements via
95 gimple_add_histogram_value(). Histograms are stored in a hash table
96 that is attached to every intrumented function, see VALUE_HISTOGRAMS
97 in function.h.
99 The value-profile transformations driver is the function
100 gimple_value_profile_transformations(). It traverses all statements in
101 the to-be-transformed function, and looks for statements with one or
102 more histograms attached to it. If a statement has histograms, the
103 transformation functions are called on the statement.
105 Limitations / FIXME / TODO:
106 * Only one histogram of each type can be associated with a statement.
107 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
108 (This type of histogram was originally used to implement a form of
109 stride profiling based speculative prefetching to improve SPEC2000
110 scores for memory-bound benchmarks, mcf and equake. However, this
111 was an RTL value-profiling transformation, and those have all been
112 removed.)
113 * Some value profile transformations are done in builtins.c (?!)
114 * Updating of histograms needs some TLC.
115 * The value profiling code could be used to record analysis results
116 from non-profiling (e.g. VRP).
117 * Adding new profilers should be simplified, starting with a cleanup
118 of what-happens-where andwith making gimple_find_values_to_profile
119 and gimple_value_profile_transformations table-driven, perhaps...
122 static tree gimple_divmod_fixed_value (gimple, tree, int, gcov_type, gcov_type);
123 static tree gimple_mod_pow2 (gimple, int, gcov_type, gcov_type);
124 static tree gimple_mod_subtract (gimple, int, int, int, gcov_type, gcov_type,
125 gcov_type);
126 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
127 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
128 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
129 static bool gimple_stringops_transform (gimple_stmt_iterator *);
130 static bool gimple_ic_transform (gimple_stmt_iterator *);
132 /* Allocate histogram value. */
134 static histogram_value
135 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
136 enum hist_type type, gimple stmt, tree value)
138 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
139 hist->hvalue.value = value;
140 hist->hvalue.stmt = stmt;
141 hist->type = type;
142 return hist;
145 /* Hash value for histogram. */
147 static hashval_t
148 histogram_hash (const void *x)
150 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
153 /* Return nonzero if statement for histogram_value X is Y. */
155 static int
156 histogram_eq (const void *x, const void *y)
158 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
161 /* Set histogram for STMT. */
163 static void
164 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
166 void **loc;
167 if (!hist && !VALUE_HISTOGRAMS (fun))
168 return;
169 if (!VALUE_HISTOGRAMS (fun))
170 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
171 histogram_eq, NULL);
172 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
173 htab_hash_pointer (stmt),
174 hist ? INSERT : NO_INSERT);
175 if (!hist)
177 if (loc)
178 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
179 return;
181 *loc = hist;
184 /* Get histogram list for STMT. */
186 histogram_value
187 gimple_histogram_value (struct function *fun, gimple stmt)
189 if (!VALUE_HISTOGRAMS (fun))
190 return NULL;
191 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
192 htab_hash_pointer (stmt));
195 /* Add histogram for STMT. */
197 void
198 gimple_add_histogram_value (struct function *fun, gimple stmt,
199 histogram_value hist)
201 hist->hvalue.next = gimple_histogram_value (fun, stmt);
202 set_histogram_value (fun, stmt, hist);
203 hist->fun = fun;
207 /* Remove histogram HIST from STMT's histogram list. */
209 void
210 gimple_remove_histogram_value (struct function *fun, gimple stmt,
211 histogram_value hist)
213 histogram_value hist2 = gimple_histogram_value (fun, stmt);
214 if (hist == hist2)
216 set_histogram_value (fun, stmt, hist->hvalue.next);
218 else
220 while (hist2->hvalue.next != hist)
221 hist2 = hist2->hvalue.next;
222 hist2->hvalue.next = hist->hvalue.next;
224 free (hist->hvalue.counters);
225 #ifdef ENABLE_CHECKING
226 memset (hist, 0xab, sizeof (*hist));
227 #endif
228 free (hist);
232 /* Lookup histogram of type TYPE in the STMT. */
234 histogram_value
235 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
236 enum hist_type type)
238 histogram_value hist;
239 for (hist = gimple_histogram_value (fun, stmt); hist;
240 hist = hist->hvalue.next)
241 if (hist->type == type)
242 return hist;
243 return NULL;
246 /* Dump information about HIST to DUMP_FILE. */
248 static void
249 dump_histogram_value (FILE *dump_file, histogram_value hist)
251 switch (hist->type)
253 case HIST_TYPE_INTERVAL:
254 fprintf (dump_file, "Interval counter range %d -- %d",
255 hist->hdata.intvl.int_start,
256 (hist->hdata.intvl.int_start
257 + hist->hdata.intvl.steps - 1));
258 if (hist->hvalue.counters)
260 unsigned int i;
261 fprintf (dump_file, " [");
262 for (i = 0; i < hist->hdata.intvl.steps; i++)
263 fprintf (dump_file, " %d:"HOST_WIDEST_INT_PRINT_DEC,
264 hist->hdata.intvl.int_start + i,
265 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
266 fprintf (dump_file, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC,
267 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
269 fprintf (dump_file, ".\n");
270 break;
272 case HIST_TYPE_POW2:
273 fprintf (dump_file, "Pow2 counter ");
274 if (hist->hvalue.counters)
276 fprintf (dump_file, "pow2:"HOST_WIDEST_INT_PRINT_DEC
277 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC,
278 (HOST_WIDEST_INT) hist->hvalue.counters[0],
279 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
281 fprintf (dump_file, ".\n");
282 break;
284 case HIST_TYPE_SINGLE_VALUE:
285 fprintf (dump_file, "Single value ");
286 if (hist->hvalue.counters)
288 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
289 " match:"HOST_WIDEST_INT_PRINT_DEC
290 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
291 (HOST_WIDEST_INT) hist->hvalue.counters[0],
292 (HOST_WIDEST_INT) hist->hvalue.counters[1],
293 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
295 fprintf (dump_file, ".\n");
296 break;
298 case HIST_TYPE_AVERAGE:
299 fprintf (dump_file, "Average value ");
300 if (hist->hvalue.counters)
302 fprintf (dump_file, "sum:"HOST_WIDEST_INT_PRINT_DEC
303 " times:"HOST_WIDEST_INT_PRINT_DEC,
304 (HOST_WIDEST_INT) hist->hvalue.counters[0],
305 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
307 fprintf (dump_file, ".\n");
308 break;
310 case HIST_TYPE_IOR:
311 fprintf (dump_file, "IOR value ");
312 if (hist->hvalue.counters)
314 fprintf (dump_file, "ior:"HOST_WIDEST_INT_PRINT_DEC,
315 (HOST_WIDEST_INT) hist->hvalue.counters[0]);
317 fprintf (dump_file, ".\n");
318 break;
320 case HIST_TYPE_CONST_DELTA:
321 fprintf (dump_file, "Constant delta ");
322 if (hist->hvalue.counters)
324 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
325 " match:"HOST_WIDEST_INT_PRINT_DEC
326 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
327 (HOST_WIDEST_INT) hist->hvalue.counters[0],
328 (HOST_WIDEST_INT) hist->hvalue.counters[1],
329 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
331 fprintf (dump_file, ".\n");
332 break;
333 case HIST_TYPE_INDIR_CALL:
334 fprintf (dump_file, "Indirect call ");
335 if (hist->hvalue.counters)
337 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
338 " match:"HOST_WIDEST_INT_PRINT_DEC
339 " all:"HOST_WIDEST_INT_PRINT_DEC,
340 (HOST_WIDEST_INT) hist->hvalue.counters[0],
341 (HOST_WIDEST_INT) hist->hvalue.counters[1],
342 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
344 fprintf (dump_file, ".\n");
345 break;
346 case HIST_TYPE_TIME_PROFILE:
347 fprintf (dump_file, "Time profile ");
348 if (hist->hvalue.counters)
350 fprintf (dump_file, "time:"HOST_WIDEST_INT_PRINT_DEC,
351 (HOST_WIDEST_INT) hist->hvalue.counters[0]);
353 fprintf (dump_file, ".\n");
354 break;
355 case HIST_TYPE_MAX:
356 gcc_unreachable ();
360 /* Dump information about HIST to DUMP_FILE. */
362 void
363 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
365 struct bitpack_d bp;
366 unsigned int i;
368 bp = bitpack_create (ob->main_stream);
369 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
370 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
371 streamer_write_bitpack (&bp);
372 switch (hist->type)
374 case HIST_TYPE_INTERVAL:
375 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
376 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
377 break;
378 default:
379 break;
381 for (i = 0; i < hist->n_counters; i++)
382 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
383 if (hist->hvalue.next)
384 stream_out_histogram_value (ob, hist->hvalue.next);
386 /* Dump information about HIST to DUMP_FILE. */
388 void
389 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
391 enum hist_type type;
392 unsigned int ncounters = 0;
393 struct bitpack_d bp;
394 unsigned int i;
395 histogram_value new_val;
396 bool next;
397 histogram_value *next_p = NULL;
401 bp = streamer_read_bitpack (ib);
402 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
403 next = bp_unpack_value (&bp, 1);
404 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
405 switch (type)
407 case HIST_TYPE_INTERVAL:
408 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
409 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
410 ncounters = new_val->hdata.intvl.steps + 2;
411 break;
413 case HIST_TYPE_POW2:
414 case HIST_TYPE_AVERAGE:
415 ncounters = 2;
416 break;
418 case HIST_TYPE_SINGLE_VALUE:
419 case HIST_TYPE_INDIR_CALL:
420 ncounters = 3;
421 break;
423 case HIST_TYPE_CONST_DELTA:
424 ncounters = 4;
425 break;
427 case HIST_TYPE_IOR:
428 case HIST_TYPE_TIME_PROFILE:
429 ncounters = 1;
430 break;
431 case HIST_TYPE_MAX:
432 gcc_unreachable ();
434 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
435 new_val->n_counters = ncounters;
436 for (i = 0; i < ncounters; i++)
437 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
438 if (!next_p)
439 gimple_add_histogram_value (cfun, stmt, new_val);
440 else
441 *next_p = new_val;
442 next_p = &new_val->hvalue.next;
444 while (next);
447 /* Dump all histograms attached to STMT to DUMP_FILE. */
449 void
450 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
452 histogram_value hist;
453 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
454 dump_histogram_value (dump_file, hist);
457 /* Remove all histograms associated with STMT. */
459 void
460 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
462 histogram_value val;
463 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
464 gimple_remove_histogram_value (fun, stmt, val);
467 /* Duplicate all histograms associates with OSTMT to STMT. */
469 void
470 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
471 struct function *ofun, gimple ostmt)
473 histogram_value val;
474 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
476 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
477 memcpy (new_val, val, sizeof (*val));
478 new_val->hvalue.stmt = stmt;
479 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
480 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
481 gimple_add_histogram_value (fun, stmt, new_val);
486 /* Move all histograms associated with OSTMT to STMT. */
488 void
489 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
491 histogram_value val = gimple_histogram_value (fun, ostmt);
492 if (val)
494 /* The following three statements can't be reordered,
495 because histogram hashtab relies on stmt field value
496 for finding the exact slot. */
497 set_histogram_value (fun, ostmt, NULL);
498 for (; val != NULL; val = val->hvalue.next)
499 val->hvalue.stmt = stmt;
500 set_histogram_value (fun, stmt, val);
504 static bool error_found = false;
506 /* Helper function for verify_histograms. For each histogram reachable via htab
507 walk verify that it was reached via statement walk. */
509 static int
510 visit_hist (void **slot, void *data)
512 struct pointer_set_t *visited = (struct pointer_set_t *) data;
513 histogram_value hist = *(histogram_value *) slot;
515 if (!pointer_set_contains (visited, hist)
516 && hist->type != HIST_TYPE_TIME_PROFILE)
518 error ("dead histogram");
519 dump_histogram_value (stderr, hist);
520 debug_gimple_stmt (hist->hvalue.stmt);
521 error_found = true;
523 return 1;
527 /* Verify sanity of the histograms. */
529 DEBUG_FUNCTION void
530 verify_histograms (void)
532 basic_block bb;
533 gimple_stmt_iterator gsi;
534 histogram_value hist;
535 struct pointer_set_t *visited_hists;
537 error_found = false;
538 visited_hists = pointer_set_create ();
539 FOR_EACH_BB (bb)
540 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
542 gimple stmt = gsi_stmt (gsi);
544 for (hist = gimple_histogram_value (cfun, stmt); hist;
545 hist = hist->hvalue.next)
547 if (hist->hvalue.stmt != stmt)
549 error ("Histogram value statement does not correspond to "
550 "the statement it is associated with");
551 debug_gimple_stmt (stmt);
552 dump_histogram_value (stderr, hist);
553 error_found = true;
555 pointer_set_insert (visited_hists, hist);
558 if (VALUE_HISTOGRAMS (cfun))
559 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, visited_hists);
560 pointer_set_destroy (visited_hists);
561 if (error_found)
562 internal_error ("verify_histograms failed");
565 /* Helper function for verify_histograms. For each histogram reachable via htab
566 walk verify that it was reached via statement walk. */
568 static int
569 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
571 histogram_value hist = *(histogram_value *) slot;
572 free (hist->hvalue.counters);
573 #ifdef ENABLE_CHECKING
574 memset (hist, 0xab, sizeof (*hist));
575 #endif
576 free (hist);
577 return 1;
580 void
581 free_histograms (void)
583 if (VALUE_HISTOGRAMS (cfun))
585 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
586 htab_delete (VALUE_HISTOGRAMS (cfun));
587 VALUE_HISTOGRAMS (cfun) = NULL;
592 /* The overall number of invocations of the counter should match
593 execution count of basic block. Report it as error rather than
594 internal error as it might mean that user has misused the profile
595 somehow. */
597 static bool
598 check_counter (gimple stmt, const char * name,
599 gcov_type *count, gcov_type *all, gcov_type bb_count)
601 if (*all != bb_count || *count > *all)
603 location_t locus;
604 locus = (stmt != NULL)
605 ? gimple_location (stmt)
606 : DECL_SOURCE_LOCATION (current_function_decl);
607 if (flag_profile_correction)
609 if (dump_enabled_p ())
610 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
611 "correcting inconsistent value profile: %s "
612 "profiler overall count (%d) does not match BB "
613 "count (%d)\n", name, (int)*all, (int)bb_count);
614 *all = bb_count;
615 if (*count > *all)
616 *count = *all;
617 return false;
619 else
621 error_at (locus, "corrupted value profile: %s "
622 "profile counter (%d out of %d) inconsistent with "
623 "basic-block count (%d)",
624 name,
625 (int) *count,
626 (int) *all,
627 (int) bb_count);
628 return true;
632 return false;
636 /* GIMPLE based transformations. */
638 bool
639 gimple_value_profile_transformations (void)
641 basic_block bb;
642 gimple_stmt_iterator gsi;
643 bool changed = false;
645 FOR_EACH_BB (bb)
647 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
649 gimple stmt = gsi_stmt (gsi);
650 histogram_value th = gimple_histogram_value (cfun, stmt);
651 if (!th)
652 continue;
654 if (dump_file)
656 fprintf (dump_file, "Trying transformations on stmt ");
657 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
658 dump_histograms_for_stmt (cfun, dump_file, stmt);
661 /* Transformations: */
662 /* The order of things in this conditional controls which
663 transformation is used when more than one is applicable. */
664 /* It is expected that any code added by the transformations
665 will be added before the current statement, and that the
666 current statement remain valid (although possibly
667 modified) upon return. */
668 if (gimple_mod_subtract_transform (&gsi)
669 || gimple_divmod_fixed_value_transform (&gsi)
670 || gimple_mod_pow2_value_transform (&gsi)
671 || gimple_stringops_transform (&gsi)
672 || gimple_ic_transform (&gsi))
674 stmt = gsi_stmt (gsi);
675 changed = true;
676 /* Original statement may no longer be in the same block. */
677 if (bb != gimple_bb (stmt))
679 bb = gimple_bb (stmt);
680 gsi = gsi_for_stmt (stmt);
686 if (changed)
688 counts_to_freqs ();
691 return changed;
695 /* Generate code for transformation 1 (with parent gimple assignment
696 STMT and probability of taking the optimal path PROB, which is
697 equivalent to COUNT/ALL within roundoff error). This generates the
698 result into a temp and returns the temp; it does not replace or
699 alter the original STMT. */
701 static tree
702 gimple_divmod_fixed_value (gimple stmt, tree value, int prob, gcov_type count,
703 gcov_type all)
705 gimple stmt1, stmt2, stmt3;
706 tree tmp0, tmp1, tmp2;
707 gimple bb1end, bb2end, bb3end;
708 basic_block bb, bb2, bb3, bb4;
709 tree optype, op1, op2;
710 edge e12, e13, e23, e24, e34;
711 gimple_stmt_iterator gsi;
713 gcc_assert (is_gimple_assign (stmt)
714 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
715 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
717 optype = TREE_TYPE (gimple_assign_lhs (stmt));
718 op1 = gimple_assign_rhs1 (stmt);
719 op2 = gimple_assign_rhs2 (stmt);
721 bb = gimple_bb (stmt);
722 gsi = gsi_for_stmt (stmt);
724 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
725 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
726 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
727 stmt2 = gimple_build_assign (tmp1, op2);
728 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
729 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
730 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
731 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
732 bb1end = stmt3;
734 tmp2 = create_tmp_reg (optype, "PROF");
735 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
736 op1, tmp0);
737 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
738 bb2end = stmt1;
740 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
741 op1, op2);
742 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
743 bb3end = stmt1;
745 /* Fix CFG. */
746 /* Edge e23 connects bb2 to bb3, etc. */
747 e12 = split_block (bb, bb1end);
748 bb2 = e12->dest;
749 bb2->count = count;
750 e23 = split_block (bb2, bb2end);
751 bb3 = e23->dest;
752 bb3->count = all - count;
753 e34 = split_block (bb3, bb3end);
754 bb4 = e34->dest;
755 bb4->count = all;
757 e12->flags &= ~EDGE_FALLTHRU;
758 e12->flags |= EDGE_FALSE_VALUE;
759 e12->probability = prob;
760 e12->count = count;
762 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
763 e13->probability = REG_BR_PROB_BASE - prob;
764 e13->count = all - count;
766 remove_edge (e23);
768 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
769 e24->probability = REG_BR_PROB_BASE;
770 e24->count = count;
772 e34->probability = REG_BR_PROB_BASE;
773 e34->count = all - count;
775 return tmp2;
779 /* Do transform 1) on INSN if applicable. */
781 static bool
782 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
784 histogram_value histogram;
785 enum tree_code code;
786 gcov_type val, count, all;
787 tree result, value, tree_val;
788 gcov_type prob;
789 gimple stmt;
791 stmt = gsi_stmt (*si);
792 if (gimple_code (stmt) != GIMPLE_ASSIGN)
793 return false;
795 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
796 return false;
798 code = gimple_assign_rhs_code (stmt);
800 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
801 return false;
803 histogram = gimple_histogram_value_of_type (cfun, stmt,
804 HIST_TYPE_SINGLE_VALUE);
805 if (!histogram)
806 return false;
808 value = histogram->hvalue.value;
809 val = histogram->hvalue.counters[0];
810 count = histogram->hvalue.counters[1];
811 all = histogram->hvalue.counters[2];
812 gimple_remove_histogram_value (cfun, stmt, histogram);
814 /* We require that count is at least half of all; this means
815 that for the transformation to fire the value must be constant
816 at least 50% of time (and 75% gives the guarantee of usage). */
817 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
818 || 2 * count < all
819 || optimize_bb_for_size_p (gimple_bb (stmt)))
820 return false;
822 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
823 return false;
825 /* Compute probability of taking the optimal path. */
826 if (all > 0)
827 prob = GCOV_COMPUTE_SCALE (count, all);
828 else
829 prob = 0;
831 tree_val = build_int_cst_wide (get_gcov_type (),
832 (unsigned HOST_WIDE_INT) val,
833 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
834 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
836 if (dump_file)
838 fprintf (dump_file, "Div/mod by constant ");
839 print_generic_expr (dump_file, value, TDF_SLIM);
840 fprintf (dump_file, "=");
841 print_generic_expr (dump_file, tree_val, TDF_SLIM);
842 fprintf (dump_file, " transformation on insn ");
843 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
846 gimple_assign_set_rhs_from_tree (si, result);
847 update_stmt (gsi_stmt (*si));
849 return true;
852 /* Generate code for transformation 2 (with parent gimple assign STMT and
853 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
854 within roundoff error). This generates the result into a temp and returns
855 the temp; it does not replace or alter the original STMT. */
856 static tree
857 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
859 gimple stmt1, stmt2, stmt3, stmt4;
860 tree tmp2, tmp3;
861 gimple bb1end, bb2end, bb3end;
862 basic_block bb, bb2, bb3, bb4;
863 tree optype, op1, op2;
864 edge e12, e13, e23, e24, e34;
865 gimple_stmt_iterator gsi;
866 tree result;
868 gcc_assert (is_gimple_assign (stmt)
869 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
871 optype = TREE_TYPE (gimple_assign_lhs (stmt));
872 op1 = gimple_assign_rhs1 (stmt);
873 op2 = gimple_assign_rhs2 (stmt);
875 bb = gimple_bb (stmt);
876 gsi = gsi_for_stmt (stmt);
878 result = create_tmp_reg (optype, "PROF");
879 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
880 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
881 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
882 build_int_cst (optype, -1));
883 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
884 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
885 NULL_TREE, NULL_TREE);
886 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
887 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
888 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
889 bb1end = stmt4;
891 /* tmp2 == op2-1 inherited from previous block. */
892 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
893 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
894 bb2end = stmt1;
896 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
897 op1, op2);
898 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
899 bb3end = stmt1;
901 /* Fix CFG. */
902 /* Edge e23 connects bb2 to bb3, etc. */
903 e12 = split_block (bb, bb1end);
904 bb2 = e12->dest;
905 bb2->count = count;
906 e23 = split_block (bb2, bb2end);
907 bb3 = e23->dest;
908 bb3->count = all - count;
909 e34 = split_block (bb3, bb3end);
910 bb4 = e34->dest;
911 bb4->count = all;
913 e12->flags &= ~EDGE_FALLTHRU;
914 e12->flags |= EDGE_FALSE_VALUE;
915 e12->probability = prob;
916 e12->count = count;
918 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
919 e13->probability = REG_BR_PROB_BASE - prob;
920 e13->count = all - count;
922 remove_edge (e23);
924 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
925 e24->probability = REG_BR_PROB_BASE;
926 e24->count = count;
928 e34->probability = REG_BR_PROB_BASE;
929 e34->count = all - count;
931 return result;
934 /* Do transform 2) on INSN if applicable. */
935 static bool
936 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
938 histogram_value histogram;
939 enum tree_code code;
940 gcov_type count, wrong_values, all;
941 tree lhs_type, result, value;
942 gcov_type prob;
943 gimple stmt;
945 stmt = gsi_stmt (*si);
946 if (gimple_code (stmt) != GIMPLE_ASSIGN)
947 return false;
949 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
950 if (!INTEGRAL_TYPE_P (lhs_type))
951 return false;
953 code = gimple_assign_rhs_code (stmt);
955 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
956 return false;
958 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
959 if (!histogram)
960 return false;
962 value = histogram->hvalue.value;
963 wrong_values = histogram->hvalue.counters[0];
964 count = histogram->hvalue.counters[1];
966 gimple_remove_histogram_value (cfun, stmt, histogram);
968 /* We require that we hit a power of 2 at least half of all evaluations. */
969 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
970 || count < wrong_values
971 || optimize_bb_for_size_p (gimple_bb (stmt)))
972 return false;
974 if (dump_file)
976 fprintf (dump_file, "Mod power of 2 transformation on insn ");
977 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
980 /* Compute probability of taking the optimal path. */
981 all = count + wrong_values;
983 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
984 return false;
986 if (all > 0)
987 prob = GCOV_COMPUTE_SCALE (count, all);
988 else
989 prob = 0;
991 result = gimple_mod_pow2 (stmt, prob, count, all);
993 gimple_assign_set_rhs_from_tree (si, result);
994 update_stmt (gsi_stmt (*si));
996 return true;
999 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1000 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1001 supported and this is built into this interface. The probabilities of taking
1002 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1003 COUNT2/ALL respectively within roundoff error). This generates the
1004 result into a temp and returns the temp; it does not replace or alter
1005 the original STMT. */
1006 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1008 static tree
1009 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
1010 gcov_type count1, gcov_type count2, gcov_type all)
1012 gimple stmt1, stmt2, stmt3;
1013 tree tmp1;
1014 gimple bb1end, bb2end = NULL, bb3end;
1015 basic_block bb, bb2, bb3, bb4;
1016 tree optype, op1, op2;
1017 edge e12, e23 = 0, e24, e34, e14;
1018 gimple_stmt_iterator gsi;
1019 tree result;
1021 gcc_assert (is_gimple_assign (stmt)
1022 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1024 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1025 op1 = gimple_assign_rhs1 (stmt);
1026 op2 = gimple_assign_rhs2 (stmt);
1028 bb = gimple_bb (stmt);
1029 gsi = gsi_for_stmt (stmt);
1031 result = create_tmp_reg (optype, "PROF");
1032 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1033 stmt1 = gimple_build_assign (result, op1);
1034 stmt2 = gimple_build_assign (tmp1, op2);
1035 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1036 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1037 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1038 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1039 bb1end = stmt3;
1041 if (ncounts) /* Assumed to be 0 or 1 */
1043 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
1044 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1045 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1046 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1047 bb2end = stmt2;
1050 /* Fallback case. */
1051 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
1052 result, tmp1);
1053 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1054 bb3end = stmt1;
1056 /* Fix CFG. */
1057 /* Edge e23 connects bb2 to bb3, etc. */
1058 /* However block 3 is optional; if it is not there, references
1059 to 3 really refer to block 2. */
1060 e12 = split_block (bb, bb1end);
1061 bb2 = e12->dest;
1062 bb2->count = all - count1;
1064 if (ncounts) /* Assumed to be 0 or 1. */
1066 e23 = split_block (bb2, bb2end);
1067 bb3 = e23->dest;
1068 bb3->count = all - count1 - count2;
1071 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1072 bb4 = e34->dest;
1073 bb4->count = all;
1075 e12->flags &= ~EDGE_FALLTHRU;
1076 e12->flags |= EDGE_FALSE_VALUE;
1077 e12->probability = REG_BR_PROB_BASE - prob1;
1078 e12->count = all - count1;
1080 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1081 e14->probability = prob1;
1082 e14->count = count1;
1084 if (ncounts) /* Assumed to be 0 or 1. */
1086 e23->flags &= ~EDGE_FALLTHRU;
1087 e23->flags |= EDGE_FALSE_VALUE;
1088 e23->count = all - count1 - count2;
1089 e23->probability = REG_BR_PROB_BASE - prob2;
1091 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1092 e24->probability = prob2;
1093 e24->count = count2;
1096 e34->probability = REG_BR_PROB_BASE;
1097 e34->count = all - count1 - count2;
1099 return result;
1103 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1105 static bool
1106 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1108 histogram_value histogram;
1109 enum tree_code code;
1110 gcov_type count, wrong_values, all;
1111 tree lhs_type, result;
1112 gcov_type prob1, prob2;
1113 unsigned int i, steps;
1114 gcov_type count1, count2;
1115 gimple stmt;
1117 stmt = gsi_stmt (*si);
1118 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1119 return false;
1121 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1122 if (!INTEGRAL_TYPE_P (lhs_type))
1123 return false;
1125 code = gimple_assign_rhs_code (stmt);
1127 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1128 return false;
1130 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1131 if (!histogram)
1132 return false;
1134 all = 0;
1135 wrong_values = 0;
1136 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1137 all += histogram->hvalue.counters[i];
1139 wrong_values += histogram->hvalue.counters[i];
1140 wrong_values += histogram->hvalue.counters[i+1];
1141 steps = histogram->hdata.intvl.steps;
1142 all += wrong_values;
1143 count1 = histogram->hvalue.counters[0];
1144 count2 = histogram->hvalue.counters[1];
1146 /* Compute probability of taking the optimal path. */
1147 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1149 gimple_remove_histogram_value (cfun, stmt, histogram);
1150 return false;
1153 if (flag_profile_correction && count1 + count2 > all)
1154 all = count1 + count2;
1156 gcc_assert (count1 + count2 <= all);
1158 /* We require that we use just subtractions in at least 50% of all
1159 evaluations. */
1160 count = 0;
1161 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1163 count += histogram->hvalue.counters[i];
1164 if (count * 2 >= all)
1165 break;
1167 if (i == steps
1168 || optimize_bb_for_size_p (gimple_bb (stmt)))
1169 return false;
1171 gimple_remove_histogram_value (cfun, stmt, histogram);
1172 if (dump_file)
1174 fprintf (dump_file, "Mod subtract transformation on insn ");
1175 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1178 /* Compute probability of taking the optimal path(s). */
1179 if (all > 0)
1181 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1182 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1184 else
1186 prob1 = prob2 = 0;
1189 /* In practice, "steps" is always 2. This interface reflects this,
1190 and will need to be changed if "steps" can change. */
1191 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1193 gimple_assign_set_rhs_from_tree (si, result);
1194 update_stmt (gsi_stmt (*si));
1196 return true;
1199 static pointer_map_t *cgraph_node_map;
1201 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1202 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1203 that the PROFILE_IDs was already assigned. */
1205 void
1206 init_node_map (bool local)
1208 struct cgraph_node *n;
1209 cgraph_node_map = pointer_map_create ();
1211 FOR_EACH_DEFINED_FUNCTION (n)
1212 if (cgraph_function_with_gimple_body_p (n)
1213 && !cgraph_only_called_directly_p (n))
1215 void **val;
1216 if (local)
1218 n->profile_id = coverage_compute_profile_id (n);
1219 while ((val = pointer_map_contains (cgraph_node_map,
1220 (void *)(size_t)n->profile_id))
1221 || !n->profile_id)
1223 if (dump_file)
1224 fprintf (dump_file, "Local profile-id %i conflict"
1225 " with nodes %s/%i %s/%i\n",
1226 n->profile_id,
1227 n->name (),
1228 n->order,
1229 (*(symtab_node **)val)->name (),
1230 (*(symtab_node **)val)->order);
1231 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1234 else if (!n->profile_id)
1236 if (dump_file)
1237 fprintf (dump_file,
1238 "Node %s/%i has no profile-id"
1239 " (profile feedback missing?)\n",
1240 n->name (),
1241 n->order);
1242 continue;
1244 else if ((val = pointer_map_contains (cgraph_node_map,
1245 (void *)(size_t)n->profile_id)))
1247 if (dump_file)
1248 fprintf (dump_file,
1249 "Node %s/%i has IP profile-id %i conflict. "
1250 "Giving up.\n",
1251 n->name (),
1252 n->order,
1253 n->profile_id);
1254 *val = NULL;
1255 continue;
1257 *pointer_map_insert (cgraph_node_map,
1258 (void *)(size_t)n->profile_id) = (void *)n;
1262 /* Delete the CGRAPH_NODE_MAP. */
1264 void
1265 del_node_map (void)
1267 pointer_map_destroy (cgraph_node_map);
1270 /* Return cgraph node for function with pid */
1272 struct cgraph_node*
1273 find_func_by_profile_id (int profile_id)
1275 void **val = pointer_map_contains (cgraph_node_map,
1276 (void *)(size_t)profile_id);
1277 if (val)
1278 return (struct cgraph_node *)*val;
1279 else
1280 return NULL;
1283 /* Perform sanity check on the indirect call target. Due to race conditions,
1284 false function target may be attributed to an indirect call site. If the
1285 call expression type mismatches with the target function's type, expand_call
1286 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1287 Returns true if TARGET is considered ok for call CALL_STMT. */
1289 static bool
1290 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1292 location_t locus;
1293 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1294 return true;
1296 locus = gimple_location (call_stmt);
1297 if (dump_enabled_p ())
1298 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1299 "Skipping target %s with mismatching types for icall\n",
1300 target->name ());
1301 return false;
1304 /* Do transformation
1306 if (actual_callee_address == address_of_most_common_function/method)
1307 do direct call
1308 else
1309 old call
1312 gimple
1313 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1314 int prob, gcov_type count, gcov_type all)
1316 gimple dcall_stmt, load_stmt, cond_stmt;
1317 tree tmp0, tmp1, tmp;
1318 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1319 tree optype = build_pointer_type (void_type_node);
1320 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1321 gimple_stmt_iterator gsi;
1322 int lp_nr, dflags;
1323 edge e_eh, e;
1324 edge_iterator ei;
1325 gimple_stmt_iterator psi;
1327 cond_bb = gimple_bb (icall_stmt);
1328 gsi = gsi_for_stmt (icall_stmt);
1330 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1331 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1332 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1333 load_stmt = gimple_build_assign (tmp0, tmp);
1334 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1336 tmp = fold_convert (optype, build_addr (direct_call->decl,
1337 current_function_decl));
1338 load_stmt = gimple_build_assign (tmp1, tmp);
1339 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1341 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1342 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1344 gimple_set_vdef (icall_stmt, NULL_TREE);
1345 gimple_set_vuse (icall_stmt, NULL_TREE);
1346 update_stmt (icall_stmt);
1347 dcall_stmt = gimple_copy (icall_stmt);
1348 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1349 dflags = flags_from_decl_or_type (direct_call->decl);
1350 if ((dflags & ECF_NORETURN) != 0)
1351 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1352 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1354 /* Fix CFG. */
1355 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1356 e_cd = split_block (cond_bb, cond_stmt);
1357 dcall_bb = e_cd->dest;
1358 dcall_bb->count = count;
1360 e_di = split_block (dcall_bb, dcall_stmt);
1361 icall_bb = e_di->dest;
1362 icall_bb->count = all - count;
1364 /* Do not disturb existing EH edges from the indirect call. */
1365 if (!stmt_ends_bb_p (icall_stmt))
1366 e_ij = split_block (icall_bb, icall_stmt);
1367 else
1369 e_ij = find_fallthru_edge (icall_bb->succs);
1370 /* The indirect call might be noreturn. */
1371 if (e_ij != NULL)
1373 e_ij->probability = REG_BR_PROB_BASE;
1374 e_ij->count = all - count;
1375 e_ij = single_pred_edge (split_edge (e_ij));
1378 if (e_ij != NULL)
1380 join_bb = e_ij->dest;
1381 join_bb->count = all;
1384 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1385 e_cd->probability = prob;
1386 e_cd->count = count;
1388 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1389 e_ci->probability = REG_BR_PROB_BASE - prob;
1390 e_ci->count = all - count;
1392 remove_edge (e_di);
1394 if (e_ij != NULL)
1396 if ((dflags & ECF_NORETURN) != 0)
1397 e_ij->count = all;
1398 else
1400 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1401 e_dj->probability = REG_BR_PROB_BASE;
1402 e_dj->count = count;
1404 e_ij->count = all - count;
1406 e_ij->probability = REG_BR_PROB_BASE;
1409 /* Insert PHI node for the call result if necessary. */
1410 if (gimple_call_lhs (icall_stmt)
1411 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1412 && (dflags & ECF_NORETURN) == 0)
1414 tree result = gimple_call_lhs (icall_stmt);
1415 gimple phi = create_phi_node (result, join_bb);
1416 gimple_call_set_lhs (icall_stmt,
1417 duplicate_ssa_name (result, icall_stmt));
1418 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1419 gimple_call_set_lhs (dcall_stmt,
1420 duplicate_ssa_name (result, dcall_stmt));
1421 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1424 /* Build an EH edge for the direct call if necessary. */
1425 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1426 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1428 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1431 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1432 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1434 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1435 for (psi = gsi_start_phis (e_eh->dest);
1436 !gsi_end_p (psi); gsi_next (&psi))
1438 gimple phi = gsi_stmt (psi);
1439 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1440 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1443 return dcall_stmt;
1447 For every checked indirect/virtual call determine if most common pid of
1448 function/class method has probability more than 50%. If yes modify code of
1449 this call to:
1452 static bool
1453 gimple_ic_transform (gimple_stmt_iterator *gsi)
1455 gimple stmt = gsi_stmt (*gsi);
1456 histogram_value histogram;
1457 gcov_type val, count, all, bb_all;
1458 struct cgraph_node *direct_call;
1460 if (gimple_code (stmt) != GIMPLE_CALL)
1461 return false;
1463 if (gimple_call_fndecl (stmt) != NULL_TREE)
1464 return false;
1466 if (gimple_call_internal_p (stmt))
1467 return false;
1469 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1470 if (!histogram)
1471 return false;
1473 val = histogram->hvalue.counters [0];
1474 count = histogram->hvalue.counters [1];
1475 all = histogram->hvalue.counters [2];
1477 bb_all = gimple_bb (stmt)->count;
1478 /* The order of CHECK_COUNTER calls is important -
1479 since check_counter can correct the third parameter
1480 and we want to make count <= all <= bb_all. */
1481 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1482 || check_counter (stmt, "ic", &count, &all, all))
1484 gimple_remove_histogram_value (cfun, stmt, histogram);
1485 return false;
1488 if (4 * count <= 3 * all)
1489 return false;
1491 direct_call = find_func_by_profile_id ((int)val);
1493 if (direct_call == NULL)
1495 if (val)
1497 if (dump_file)
1499 fprintf (dump_file, "Indirect call -> direct call from other module");
1500 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1501 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1504 return false;
1507 if (!check_ic_target (stmt, direct_call))
1509 if (dump_file)
1511 fprintf (dump_file, "Indirect call -> direct call ");
1512 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1513 fprintf (dump_file, "=> ");
1514 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1515 fprintf (dump_file, " transformation skipped because of type mismatch");
1516 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1518 gimple_remove_histogram_value (cfun, stmt, histogram);
1519 return false;
1522 if (dump_file)
1524 fprintf (dump_file, "Indirect call -> direct call ");
1525 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1526 fprintf (dump_file, "=> ");
1527 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1528 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1529 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1530 fprintf (dump_file, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1531 " hist->all "HOST_WIDEST_INT_PRINT_DEC"\n", count, all);
1534 return true;
1537 /* Return true if the stringop CALL with FNDECL shall be profiled.
1538 SIZE_ARG be set to the argument index for the size of the string
1539 operation.
1541 static bool
1542 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1544 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1546 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1547 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1548 return false;
1550 switch (fcode)
1552 case BUILT_IN_MEMCPY:
1553 case BUILT_IN_MEMPCPY:
1554 *size_arg = 2;
1555 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1556 INTEGER_TYPE, VOID_TYPE);
1557 case BUILT_IN_MEMSET:
1558 *size_arg = 2;
1559 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1560 INTEGER_TYPE, VOID_TYPE);
1561 case BUILT_IN_BZERO:
1562 *size_arg = 1;
1563 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1564 VOID_TYPE);
1565 default:
1566 gcc_unreachable ();
1570 /* Convert stringop (..., vcall_size)
1571 into
1572 if (vcall_size == icall_size)
1573 stringop (..., icall_size);
1574 else
1575 stringop (..., vcall_size);
1576 assuming we'll propagate a true constant into ICALL_SIZE later. */
1578 static void
1579 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1580 gcov_type count, gcov_type all)
1582 gimple tmp_stmt, cond_stmt, icall_stmt;
1583 tree tmp0, tmp1, vcall_size, optype;
1584 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1585 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1586 gimple_stmt_iterator gsi;
1587 tree fndecl;
1588 int size_arg;
1590 fndecl = gimple_call_fndecl (vcall_stmt);
1591 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1592 gcc_unreachable ();
1594 cond_bb = gimple_bb (vcall_stmt);
1595 gsi = gsi_for_stmt (vcall_stmt);
1597 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1598 optype = TREE_TYPE (vcall_size);
1600 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1601 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1602 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1603 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1605 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1606 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1608 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1609 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1611 gimple_set_vdef (vcall_stmt, NULL);
1612 gimple_set_vuse (vcall_stmt, NULL);
1613 update_stmt (vcall_stmt);
1614 icall_stmt = gimple_copy (vcall_stmt);
1615 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1616 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1618 /* Fix CFG. */
1619 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1620 e_ci = split_block (cond_bb, cond_stmt);
1621 icall_bb = e_ci->dest;
1622 icall_bb->count = count;
1624 e_iv = split_block (icall_bb, icall_stmt);
1625 vcall_bb = e_iv->dest;
1626 vcall_bb->count = all - count;
1628 e_vj = split_block (vcall_bb, vcall_stmt);
1629 join_bb = e_vj->dest;
1630 join_bb->count = all;
1632 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1633 e_ci->probability = prob;
1634 e_ci->count = count;
1636 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1637 e_cv->probability = REG_BR_PROB_BASE - prob;
1638 e_cv->count = all - count;
1640 remove_edge (e_iv);
1642 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1643 e_ij->probability = REG_BR_PROB_BASE;
1644 e_ij->count = count;
1646 e_vj->probability = REG_BR_PROB_BASE;
1647 e_vj->count = all - count;
1649 /* Insert PHI node for the call result if necessary. */
1650 if (gimple_call_lhs (vcall_stmt)
1651 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1653 tree result = gimple_call_lhs (vcall_stmt);
1654 gimple phi = create_phi_node (result, join_bb);
1655 gimple_call_set_lhs (vcall_stmt,
1656 duplicate_ssa_name (result, vcall_stmt));
1657 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1658 gimple_call_set_lhs (icall_stmt,
1659 duplicate_ssa_name (result, icall_stmt));
1660 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1663 /* Because these are all string op builtins, they're all nothrow. */
1664 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1665 gcc_assert (!stmt_could_throw_p (icall_stmt));
1668 /* Find values inside STMT for that we want to measure histograms for
1669 division/modulo optimization. */
1670 static bool
1671 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1673 gimple stmt = gsi_stmt (*gsi);
1674 tree fndecl;
1675 tree blck_size;
1676 enum built_in_function fcode;
1677 histogram_value histogram;
1678 gcov_type count, all, val;
1679 tree dest, src;
1680 unsigned int dest_align, src_align;
1681 gcov_type prob;
1682 tree tree_val;
1683 int size_arg;
1685 if (gimple_code (stmt) != GIMPLE_CALL)
1686 return false;
1687 fndecl = gimple_call_fndecl (stmt);
1688 if (!fndecl)
1689 return false;
1690 fcode = DECL_FUNCTION_CODE (fndecl);
1691 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1692 return false;
1694 blck_size = gimple_call_arg (stmt, size_arg);
1695 if (TREE_CODE (blck_size) == INTEGER_CST)
1696 return false;
1698 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1699 if (!histogram)
1700 return false;
1701 val = histogram->hvalue.counters[0];
1702 count = histogram->hvalue.counters[1];
1703 all = histogram->hvalue.counters[2];
1704 gimple_remove_histogram_value (cfun, stmt, histogram);
1705 /* We require that count is at least half of all; this means
1706 that for the transformation to fire the value must be constant
1707 at least 80% of time. */
1708 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1709 return false;
1710 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1711 return false;
1712 if (all > 0)
1713 prob = GCOV_COMPUTE_SCALE (count, all);
1714 else
1715 prob = 0;
1716 dest = gimple_call_arg (stmt, 0);
1717 dest_align = get_pointer_alignment (dest);
1718 switch (fcode)
1720 case BUILT_IN_MEMCPY:
1721 case BUILT_IN_MEMPCPY:
1722 src = gimple_call_arg (stmt, 1);
1723 src_align = get_pointer_alignment (src);
1724 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1725 return false;
1726 break;
1727 case BUILT_IN_MEMSET:
1728 if (!can_store_by_pieces (val, builtin_memset_read_str,
1729 gimple_call_arg (stmt, 1),
1730 dest_align, true))
1731 return false;
1732 break;
1733 case BUILT_IN_BZERO:
1734 if (!can_store_by_pieces (val, builtin_memset_read_str,
1735 integer_zero_node,
1736 dest_align, true))
1737 return false;
1738 break;
1739 default:
1740 gcc_unreachable ();
1742 tree_val = build_int_cst_wide (get_gcov_type (),
1743 (unsigned HOST_WIDE_INT) val,
1744 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
1745 if (dump_file)
1747 fprintf (dump_file, "Single value %i stringop transformation on ",
1748 (int)val);
1749 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1751 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1753 return true;
1756 void
1757 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1758 HOST_WIDE_INT *expected_size)
1760 histogram_value histogram;
1761 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1762 if (!histogram)
1763 *expected_size = -1;
1764 else if (!histogram->hvalue.counters[1])
1766 *expected_size = -1;
1767 gimple_remove_histogram_value (cfun, stmt, histogram);
1769 else
1771 gcov_type size;
1772 size = ((histogram->hvalue.counters[0]
1773 + histogram->hvalue.counters[1] / 2)
1774 / histogram->hvalue.counters[1]);
1775 /* Even if we can hold bigger value in SIZE, INT_MAX
1776 is safe "infinity" for code generation strategies. */
1777 if (size > INT_MAX)
1778 size = INT_MAX;
1779 *expected_size = size;
1780 gimple_remove_histogram_value (cfun, stmt, histogram);
1782 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1783 if (!histogram)
1784 *expected_align = 0;
1785 else if (!histogram->hvalue.counters[0])
1787 gimple_remove_histogram_value (cfun, stmt, histogram);
1788 *expected_align = 0;
1790 else
1792 gcov_type count;
1793 int alignment;
1795 count = histogram->hvalue.counters[0];
1796 alignment = 1;
1797 while (!(count & alignment)
1798 && (alignment * 2 * BITS_PER_UNIT))
1799 alignment <<= 1;
1800 *expected_align = alignment * BITS_PER_UNIT;
1801 gimple_remove_histogram_value (cfun, stmt, histogram);
1806 /* Find values inside STMT for that we want to measure histograms for
1807 division/modulo optimization. */
1808 static void
1809 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1811 tree lhs, divisor, op0, type;
1812 histogram_value hist;
1814 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1815 return;
1817 lhs = gimple_assign_lhs (stmt);
1818 type = TREE_TYPE (lhs);
1819 if (!INTEGRAL_TYPE_P (type))
1820 return;
1822 switch (gimple_assign_rhs_code (stmt))
1824 case TRUNC_DIV_EXPR:
1825 case TRUNC_MOD_EXPR:
1826 divisor = gimple_assign_rhs2 (stmt);
1827 op0 = gimple_assign_rhs1 (stmt);
1829 values->reserve (3);
1831 if (TREE_CODE (divisor) == SSA_NAME)
1832 /* Check for the case where the divisor is the same value most
1833 of the time. */
1834 values->quick_push (gimple_alloc_histogram_value (cfun,
1835 HIST_TYPE_SINGLE_VALUE,
1836 stmt, divisor));
1838 /* For mod, check whether it is not often a noop (or replaceable by
1839 a few subtractions). */
1840 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1841 && TYPE_UNSIGNED (type))
1843 tree val;
1844 /* Check for a special case where the divisor is power of 2. */
1845 values->quick_push (gimple_alloc_histogram_value (cfun,
1846 HIST_TYPE_POW2,
1847 stmt, divisor));
1849 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1850 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1851 stmt, val);
1852 hist->hdata.intvl.int_start = 0;
1853 hist->hdata.intvl.steps = 2;
1854 values->quick_push (hist);
1856 return;
1858 default:
1859 return;
1863 /* Find calls inside STMT for that we want to measure histograms for
1864 indirect/virtual call optimization. */
1866 static void
1867 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1869 tree callee;
1871 if (gimple_code (stmt) != GIMPLE_CALL
1872 || gimple_call_internal_p (stmt)
1873 || gimple_call_fndecl (stmt) != NULL_TREE)
1874 return;
1876 callee = gimple_call_fn (stmt);
1878 values->reserve (3);
1880 values->quick_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1881 stmt, callee));
1883 return;
1886 /* Find values inside STMT for that we want to measure histograms for
1887 string operations. */
1888 static void
1889 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1891 tree fndecl;
1892 tree blck_size;
1893 tree dest;
1894 int size_arg;
1896 if (gimple_code (stmt) != GIMPLE_CALL)
1897 return;
1898 fndecl = gimple_call_fndecl (stmt);
1899 if (!fndecl)
1900 return;
1902 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1903 return;
1905 dest = gimple_call_arg (stmt, 0);
1906 blck_size = gimple_call_arg (stmt, size_arg);
1908 if (TREE_CODE (blck_size) != INTEGER_CST)
1910 values->safe_push (gimple_alloc_histogram_value (cfun,
1911 HIST_TYPE_SINGLE_VALUE,
1912 stmt, blck_size));
1913 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1914 stmt, blck_size));
1916 if (TREE_CODE (blck_size) != INTEGER_CST)
1917 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1918 stmt, dest));
1921 /* Find values inside STMT for that we want to measure histograms and adds
1922 them to list VALUES. */
1924 static void
1925 gimple_values_to_profile (gimple stmt, histogram_values *values)
1927 gimple_divmod_values_to_profile (stmt, values);
1928 gimple_stringops_values_to_profile (stmt, values);
1929 gimple_indirect_call_to_profile (stmt, values);
1932 void
1933 gimple_find_values_to_profile (histogram_values *values)
1935 basic_block bb;
1936 gimple_stmt_iterator gsi;
1937 unsigned i;
1938 histogram_value hist = NULL;
1939 values->create (0);
1941 FOR_EACH_BB (bb)
1942 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1943 gimple_values_to_profile (gsi_stmt (gsi), values);
1945 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
1947 FOR_EACH_VEC_ELT (*values, i, hist)
1949 switch (hist->type)
1951 case HIST_TYPE_INTERVAL:
1952 hist->n_counters = hist->hdata.intvl.steps + 2;
1953 break;
1955 case HIST_TYPE_POW2:
1956 hist->n_counters = 2;
1957 break;
1959 case HIST_TYPE_SINGLE_VALUE:
1960 hist->n_counters = 3;
1961 break;
1963 case HIST_TYPE_CONST_DELTA:
1964 hist->n_counters = 4;
1965 break;
1967 case HIST_TYPE_INDIR_CALL:
1968 hist->n_counters = 3;
1969 break;
1971 case HIST_TYPE_TIME_PROFILE:
1972 hist->n_counters = 1;
1973 break;
1975 case HIST_TYPE_AVERAGE:
1976 hist->n_counters = 2;
1977 break;
1979 case HIST_TYPE_IOR:
1980 hist->n_counters = 1;
1981 break;
1983 default:
1984 gcc_unreachable ();
1986 if (dump_file)
1988 fprintf (dump_file, "Stmt ");
1989 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
1990 dump_histogram_value (dump_file, hist);