2013-11-08 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / value-prof.c
blob0a9388285bb69b6515d76888f7b9155404b114dc
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 "gimple-ssa.h"
38 #include "tree-cfg.h"
39 #include "tree-phinodes.h"
40 #include "ssa-iterators.h"
41 #include "tree-ssanames.h"
42 #include "diagnostic.h"
43 #include "gimple-pretty-print.h"
44 #include "coverage.h"
45 #include "tree.h"
46 #include "gcov-io.h"
47 #include "timevar.h"
48 #include "dumpfile.h"
49 #include "pointer-set.h"
50 #include "profile.h"
51 #include "data-streamer.h"
53 /* In this file value profile based optimizations are placed. Currently the
54 following optimizations are implemented (for more detailed descriptions
55 see comments at value_profile_transformations):
57 1) Division/modulo specialization. Provided that we can determine that the
58 operands of the division have some special properties, we may use it to
59 produce more effective code.
61 2) Indirect/virtual call specialization. If we can determine most
62 common function callee in indirect/virtual call. We can use this
63 information to improve code effectiveness (especially info for
64 the inliner).
66 3) Speculative prefetching. If we are able to determine that the difference
67 between addresses accessed by a memory reference is usually constant, we
68 may add the prefetch instructions.
69 FIXME: This transformation was removed together with RTL based value
70 profiling.
73 Value profiling internals
74 ==========================
76 Every value profiling transformation starts with defining what values
77 to profile. There are different histogram types (see HIST_TYPE_* in
78 value-prof.h) and each transformation can request one or more histogram
79 types per GIMPLE statement. The function gimple_find_values_to_profile()
80 collects the values to profile in a vec, and adds the number of counters
81 required for the different histogram types.
83 For a -fprofile-generate run, the statements for which values should be
84 recorded, are instrumented in instrument_values(). The instrumentation
85 is done by helper functions that can be found in tree-profile.c, where
86 new types of histograms can be added if necessary.
88 After a -fprofile-use, the value profiling data is read back in by
89 compute_value_histograms() that translates the collected data to
90 histograms and attaches them to the profiled statements via
91 gimple_add_histogram_value(). Histograms are stored in a hash table
92 that is attached to every intrumented function, see VALUE_HISTOGRAMS
93 in function.h.
95 The value-profile transformations driver is the function
96 gimple_value_profile_transformations(). It traverses all statements in
97 the to-be-transformed function, and looks for statements with one or
98 more histograms attached to it. If a statement has histograms, the
99 transformation functions are called on the statement.
101 Limitations / FIXME / TODO:
102 * Only one histogram of each type can be associated with a statement.
103 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
104 (This type of histogram was originally used to implement a form of
105 stride profiling based speculative prefetching to improve SPEC2000
106 scores for memory-bound benchmarks, mcf and equake. However, this
107 was an RTL value-profiling transformation, and those have all been
108 removed.)
109 * Some value profile transformations are done in builtins.c (?!)
110 * Updating of histograms needs some TLC.
111 * The value profiling code could be used to record analysis results
112 from non-profiling (e.g. VRP).
113 * Adding new profilers should be simplified, starting with a cleanup
114 of what-happens-where andwith making gimple_find_values_to_profile
115 and gimple_value_profile_transformations table-driven, perhaps...
118 static tree gimple_divmod_fixed_value (gimple, tree, int, gcov_type, gcov_type);
119 static tree gimple_mod_pow2 (gimple, int, gcov_type, gcov_type);
120 static tree gimple_mod_subtract (gimple, int, int, int, gcov_type, gcov_type,
121 gcov_type);
122 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
123 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
124 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
125 static bool gimple_stringops_transform (gimple_stmt_iterator *);
126 static bool gimple_ic_transform (gimple_stmt_iterator *);
128 /* Allocate histogram value. */
130 static histogram_value
131 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
132 enum hist_type type, gimple stmt, tree value)
134 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
135 hist->hvalue.value = value;
136 hist->hvalue.stmt = stmt;
137 hist->type = type;
138 return hist;
141 /* Hash value for histogram. */
143 static hashval_t
144 histogram_hash (const void *x)
146 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
149 /* Return nonzero if statement for histogram_value X is Y. */
151 static int
152 histogram_eq (const void *x, const void *y)
154 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
157 /* Set histogram for STMT. */
159 static void
160 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
162 void **loc;
163 if (!hist && !VALUE_HISTOGRAMS (fun))
164 return;
165 if (!VALUE_HISTOGRAMS (fun))
166 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
167 histogram_eq, NULL);
168 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
169 htab_hash_pointer (stmt),
170 hist ? INSERT : NO_INSERT);
171 if (!hist)
173 if (loc)
174 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
175 return;
177 *loc = hist;
180 /* Get histogram list for STMT. */
182 histogram_value
183 gimple_histogram_value (struct function *fun, gimple stmt)
185 if (!VALUE_HISTOGRAMS (fun))
186 return NULL;
187 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
188 htab_hash_pointer (stmt));
191 /* Add histogram for STMT. */
193 void
194 gimple_add_histogram_value (struct function *fun, gimple stmt,
195 histogram_value hist)
197 hist->hvalue.next = gimple_histogram_value (fun, stmt);
198 set_histogram_value (fun, stmt, hist);
202 /* Remove histogram HIST from STMT's histogram list. */
204 void
205 gimple_remove_histogram_value (struct function *fun, gimple stmt,
206 histogram_value hist)
208 histogram_value hist2 = gimple_histogram_value (fun, stmt);
209 if (hist == hist2)
211 set_histogram_value (fun, stmt, hist->hvalue.next);
213 else
215 while (hist2->hvalue.next != hist)
216 hist2 = hist2->hvalue.next;
217 hist2->hvalue.next = hist->hvalue.next;
219 free (hist->hvalue.counters);
220 #ifdef ENABLE_CHECKING
221 memset (hist, 0xab, sizeof (*hist));
222 #endif
223 free (hist);
227 /* Lookup histogram of type TYPE in the STMT. */
229 histogram_value
230 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
231 enum hist_type type)
233 histogram_value hist;
234 for (hist = gimple_histogram_value (fun, stmt); hist;
235 hist = hist->hvalue.next)
236 if (hist->type == type)
237 return hist;
238 return NULL;
241 /* Dump information about HIST to DUMP_FILE. */
243 static void
244 dump_histogram_value (FILE *dump_file, histogram_value hist)
246 switch (hist->type)
248 case HIST_TYPE_INTERVAL:
249 fprintf (dump_file, "Interval counter range %d -- %d",
250 hist->hdata.intvl.int_start,
251 (hist->hdata.intvl.int_start
252 + hist->hdata.intvl.steps - 1));
253 if (hist->hvalue.counters)
255 unsigned int i;
256 fprintf (dump_file, " [");
257 for (i = 0; i < hist->hdata.intvl.steps; i++)
258 fprintf (dump_file, " %d:"HOST_WIDEST_INT_PRINT_DEC,
259 hist->hdata.intvl.int_start + i,
260 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
261 fprintf (dump_file, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC,
262 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
264 fprintf (dump_file, ".\n");
265 break;
267 case HIST_TYPE_POW2:
268 fprintf (dump_file, "Pow2 counter ");
269 if (hist->hvalue.counters)
271 fprintf (dump_file, "pow2:"HOST_WIDEST_INT_PRINT_DEC
272 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC,
273 (HOST_WIDEST_INT) hist->hvalue.counters[0],
274 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
276 fprintf (dump_file, ".\n");
277 break;
279 case HIST_TYPE_SINGLE_VALUE:
280 fprintf (dump_file, "Single value ");
281 if (hist->hvalue.counters)
283 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
284 " match:"HOST_WIDEST_INT_PRINT_DEC
285 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
286 (HOST_WIDEST_INT) hist->hvalue.counters[0],
287 (HOST_WIDEST_INT) hist->hvalue.counters[1],
288 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
290 fprintf (dump_file, ".\n");
291 break;
293 case HIST_TYPE_AVERAGE:
294 fprintf (dump_file, "Average value ");
295 if (hist->hvalue.counters)
297 fprintf (dump_file, "sum:"HOST_WIDEST_INT_PRINT_DEC
298 " times:"HOST_WIDEST_INT_PRINT_DEC,
299 (HOST_WIDEST_INT) hist->hvalue.counters[0],
300 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
302 fprintf (dump_file, ".\n");
303 break;
305 case HIST_TYPE_IOR:
306 fprintf (dump_file, "IOR value ");
307 if (hist->hvalue.counters)
309 fprintf (dump_file, "ior:"HOST_WIDEST_INT_PRINT_DEC,
310 (HOST_WIDEST_INT) hist->hvalue.counters[0]);
312 fprintf (dump_file, ".\n");
313 break;
315 case HIST_TYPE_CONST_DELTA:
316 fprintf (dump_file, "Constant delta ");
317 if (hist->hvalue.counters)
319 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
320 " match:"HOST_WIDEST_INT_PRINT_DEC
321 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
322 (HOST_WIDEST_INT) hist->hvalue.counters[0],
323 (HOST_WIDEST_INT) hist->hvalue.counters[1],
324 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
326 fprintf (dump_file, ".\n");
327 break;
328 case HIST_TYPE_INDIR_CALL:
329 fprintf (dump_file, "Indirect call ");
330 if (hist->hvalue.counters)
332 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
333 " match:"HOST_WIDEST_INT_PRINT_DEC
334 " all:"HOST_WIDEST_INT_PRINT_DEC,
335 (HOST_WIDEST_INT) hist->hvalue.counters[0],
336 (HOST_WIDEST_INT) hist->hvalue.counters[1],
337 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
339 fprintf (dump_file, ".\n");
340 break;
341 case HIST_TYPE_MAX:
342 gcc_unreachable ();
346 /* Dump information about HIST to DUMP_FILE. */
348 void
349 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
351 struct bitpack_d bp;
352 unsigned int i;
354 bp = bitpack_create (ob->main_stream);
355 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
356 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
357 streamer_write_bitpack (&bp);
358 switch (hist->type)
360 case HIST_TYPE_INTERVAL:
361 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
362 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
363 break;
364 default:
365 break;
367 for (i = 0; i < hist->n_counters; i++)
368 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
369 if (hist->hvalue.next)
370 stream_out_histogram_value (ob, hist->hvalue.next);
372 /* Dump information about HIST to DUMP_FILE. */
374 void
375 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
377 enum hist_type type;
378 unsigned int ncounters = 0;
379 struct bitpack_d bp;
380 unsigned int i;
381 histogram_value new_val;
382 bool next;
383 histogram_value *next_p = NULL;
387 bp = streamer_read_bitpack (ib);
388 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
389 next = bp_unpack_value (&bp, 1);
390 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
391 switch (type)
393 case HIST_TYPE_INTERVAL:
394 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
395 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
396 ncounters = new_val->hdata.intvl.steps + 2;
397 break;
399 case HIST_TYPE_POW2:
400 case HIST_TYPE_AVERAGE:
401 ncounters = 2;
402 break;
404 case HIST_TYPE_SINGLE_VALUE:
405 case HIST_TYPE_INDIR_CALL:
406 ncounters = 3;
407 break;
409 case HIST_TYPE_CONST_DELTA:
410 ncounters = 4;
411 break;
413 case HIST_TYPE_IOR:
414 ncounters = 1;
415 break;
416 case HIST_TYPE_MAX:
417 gcc_unreachable ();
419 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
420 new_val->n_counters = ncounters;
421 for (i = 0; i < ncounters; i++)
422 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
423 if (!next_p)
424 gimple_add_histogram_value (cfun, stmt, new_val);
425 else
426 *next_p = new_val;
427 next_p = &new_val->hvalue.next;
429 while (next);
432 /* Dump all histograms attached to STMT to DUMP_FILE. */
434 void
435 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
437 histogram_value hist;
438 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
439 dump_histogram_value (dump_file, hist);
442 /* Remove all histograms associated with STMT. */
444 void
445 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
447 histogram_value val;
448 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
449 gimple_remove_histogram_value (fun, stmt, val);
452 /* Duplicate all histograms associates with OSTMT to STMT. */
454 void
455 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
456 struct function *ofun, gimple ostmt)
458 histogram_value val;
459 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
461 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
462 memcpy (new_val, val, sizeof (*val));
463 new_val->hvalue.stmt = stmt;
464 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
465 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
466 gimple_add_histogram_value (fun, stmt, new_val);
471 /* Move all histograms associated with OSTMT to STMT. */
473 void
474 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
476 histogram_value val = gimple_histogram_value (fun, ostmt);
477 if (val)
479 /* The following three statements can't be reordered,
480 because histogram hashtab relies on stmt field value
481 for finding the exact slot. */
482 set_histogram_value (fun, ostmt, NULL);
483 for (; val != NULL; val = val->hvalue.next)
484 val->hvalue.stmt = stmt;
485 set_histogram_value (fun, stmt, val);
489 static bool error_found = false;
491 /* Helper function for verify_histograms. For each histogram reachable via htab
492 walk verify that it was reached via statement walk. */
494 static int
495 visit_hist (void **slot, void *data)
497 struct pointer_set_t *visited = (struct pointer_set_t *) data;
498 histogram_value hist = *(histogram_value *) slot;
499 if (!pointer_set_contains (visited, hist))
501 error ("dead histogram");
502 dump_histogram_value (stderr, hist);
503 debug_gimple_stmt (hist->hvalue.stmt);
504 error_found = true;
506 return 1;
510 /* Verify sanity of the histograms. */
512 DEBUG_FUNCTION void
513 verify_histograms (void)
515 basic_block bb;
516 gimple_stmt_iterator gsi;
517 histogram_value hist;
518 struct pointer_set_t *visited_hists;
520 error_found = false;
521 visited_hists = pointer_set_create ();
522 FOR_EACH_BB (bb)
523 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
525 gimple stmt = gsi_stmt (gsi);
527 for (hist = gimple_histogram_value (cfun, stmt); hist;
528 hist = hist->hvalue.next)
530 if (hist->hvalue.stmt != stmt)
532 error ("Histogram value statement does not correspond to "
533 "the statement it is associated with");
534 debug_gimple_stmt (stmt);
535 dump_histogram_value (stderr, hist);
536 error_found = true;
538 pointer_set_insert (visited_hists, hist);
541 if (VALUE_HISTOGRAMS (cfun))
542 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, visited_hists);
543 pointer_set_destroy (visited_hists);
544 if (error_found)
545 internal_error ("verify_histograms failed");
548 /* Helper function for verify_histograms. For each histogram reachable via htab
549 walk verify that it was reached via statement walk. */
551 static int
552 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
554 histogram_value hist = *(histogram_value *) slot;
555 free (hist->hvalue.counters);
556 #ifdef ENABLE_CHECKING
557 memset (hist, 0xab, sizeof (*hist));
558 #endif
559 free (hist);
560 return 1;
563 void
564 free_histograms (void)
566 if (VALUE_HISTOGRAMS (cfun))
568 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
569 htab_delete (VALUE_HISTOGRAMS (cfun));
570 VALUE_HISTOGRAMS (cfun) = NULL;
575 /* The overall number of invocations of the counter should match
576 execution count of basic block. Report it as error rather than
577 internal error as it might mean that user has misused the profile
578 somehow. */
580 static bool
581 check_counter (gimple stmt, const char * name,
582 gcov_type *count, gcov_type *all, gcov_type bb_count)
584 if (*all != bb_count || *count > *all)
586 location_t locus;
587 locus = (stmt != NULL)
588 ? gimple_location (stmt)
589 : DECL_SOURCE_LOCATION (current_function_decl);
590 if (flag_profile_correction)
592 if (dump_enabled_p ())
593 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
594 "correcting inconsistent value profile: %s "
595 "profiler overall count (%d) does not match BB "
596 "count (%d)\n", name, (int)*all, (int)bb_count);
597 *all = bb_count;
598 if (*count > *all)
599 *count = *all;
600 return false;
602 else
604 error_at (locus, "corrupted value profile: %s "
605 "profile counter (%d out of %d) inconsistent with "
606 "basic-block count (%d)",
607 name,
608 (int) *count,
609 (int) *all,
610 (int) bb_count);
611 return true;
615 return false;
619 /* GIMPLE based transformations. */
621 bool
622 gimple_value_profile_transformations (void)
624 basic_block bb;
625 gimple_stmt_iterator gsi;
626 bool changed = false;
628 FOR_EACH_BB (bb)
630 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
632 gimple stmt = gsi_stmt (gsi);
633 histogram_value th = gimple_histogram_value (cfun, stmt);
634 if (!th)
635 continue;
637 if (dump_file)
639 fprintf (dump_file, "Trying transformations on stmt ");
640 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
641 dump_histograms_for_stmt (cfun, dump_file, stmt);
644 /* Transformations: */
645 /* The order of things in this conditional controls which
646 transformation is used when more than one is applicable. */
647 /* It is expected that any code added by the transformations
648 will be added before the current statement, and that the
649 current statement remain valid (although possibly
650 modified) upon return. */
651 if (gimple_mod_subtract_transform (&gsi)
652 || gimple_divmod_fixed_value_transform (&gsi)
653 || gimple_mod_pow2_value_transform (&gsi)
654 || gimple_stringops_transform (&gsi)
655 || gimple_ic_transform (&gsi))
657 stmt = gsi_stmt (gsi);
658 changed = true;
659 /* Original statement may no longer be in the same block. */
660 if (bb != gimple_bb (stmt))
662 bb = gimple_bb (stmt);
663 gsi = gsi_for_stmt (stmt);
669 if (changed)
671 counts_to_freqs ();
674 return changed;
678 /* Generate code for transformation 1 (with parent gimple assignment
679 STMT and probability of taking the optimal path PROB, which is
680 equivalent to COUNT/ALL within roundoff error). This generates the
681 result into a temp and returns the temp; it does not replace or
682 alter the original STMT. */
684 static tree
685 gimple_divmod_fixed_value (gimple stmt, tree value, int prob, gcov_type count,
686 gcov_type all)
688 gimple stmt1, stmt2, stmt3;
689 tree tmp0, tmp1, tmp2;
690 gimple bb1end, bb2end, bb3end;
691 basic_block bb, bb2, bb3, bb4;
692 tree optype, op1, op2;
693 edge e12, e13, e23, e24, e34;
694 gimple_stmt_iterator gsi;
696 gcc_assert (is_gimple_assign (stmt)
697 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
698 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
700 optype = TREE_TYPE (gimple_assign_lhs (stmt));
701 op1 = gimple_assign_rhs1 (stmt);
702 op2 = gimple_assign_rhs2 (stmt);
704 bb = gimple_bb (stmt);
705 gsi = gsi_for_stmt (stmt);
707 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
708 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
709 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
710 stmt2 = gimple_build_assign (tmp1, op2);
711 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
712 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
713 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
714 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
715 bb1end = stmt3;
717 tmp2 = create_tmp_reg (optype, "PROF");
718 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
719 op1, tmp0);
720 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
721 bb2end = stmt1;
723 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
724 op1, op2);
725 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
726 bb3end = stmt1;
728 /* Fix CFG. */
729 /* Edge e23 connects bb2 to bb3, etc. */
730 e12 = split_block (bb, bb1end);
731 bb2 = e12->dest;
732 bb2->count = count;
733 e23 = split_block (bb2, bb2end);
734 bb3 = e23->dest;
735 bb3->count = all - count;
736 e34 = split_block (bb3, bb3end);
737 bb4 = e34->dest;
738 bb4->count = all;
740 e12->flags &= ~EDGE_FALLTHRU;
741 e12->flags |= EDGE_FALSE_VALUE;
742 e12->probability = prob;
743 e12->count = count;
745 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
746 e13->probability = REG_BR_PROB_BASE - prob;
747 e13->count = all - count;
749 remove_edge (e23);
751 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
752 e24->probability = REG_BR_PROB_BASE;
753 e24->count = count;
755 e34->probability = REG_BR_PROB_BASE;
756 e34->count = all - count;
758 return tmp2;
762 /* Do transform 1) on INSN if applicable. */
764 static bool
765 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
767 histogram_value histogram;
768 enum tree_code code;
769 gcov_type val, count, all;
770 tree result, value, tree_val;
771 gcov_type prob;
772 gimple stmt;
774 stmt = gsi_stmt (*si);
775 if (gimple_code (stmt) != GIMPLE_ASSIGN)
776 return false;
778 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
779 return false;
781 code = gimple_assign_rhs_code (stmt);
783 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
784 return false;
786 histogram = gimple_histogram_value_of_type (cfun, stmt,
787 HIST_TYPE_SINGLE_VALUE);
788 if (!histogram)
789 return false;
791 value = histogram->hvalue.value;
792 val = histogram->hvalue.counters[0];
793 count = histogram->hvalue.counters[1];
794 all = histogram->hvalue.counters[2];
795 gimple_remove_histogram_value (cfun, stmt, histogram);
797 /* We require that count is at least half of all; this means
798 that for the transformation to fire the value must be constant
799 at least 50% of time (and 75% gives the guarantee of usage). */
800 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
801 || 2 * count < all
802 || optimize_bb_for_size_p (gimple_bb (stmt)))
803 return false;
805 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
806 return false;
808 /* Compute probability of taking the optimal path. */
809 if (all > 0)
810 prob = GCOV_COMPUTE_SCALE (count, all);
811 else
812 prob = 0;
814 tree_val = build_int_cst_wide (get_gcov_type (),
815 (unsigned HOST_WIDE_INT) val,
816 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
817 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
819 if (dump_file)
821 fprintf (dump_file, "Div/mod by constant ");
822 print_generic_expr (dump_file, value, TDF_SLIM);
823 fprintf (dump_file, "=");
824 print_generic_expr (dump_file, tree_val, TDF_SLIM);
825 fprintf (dump_file, " transformation on insn ");
826 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
829 gimple_assign_set_rhs_from_tree (si, result);
830 update_stmt (gsi_stmt (*si));
832 return true;
835 /* Generate code for transformation 2 (with parent gimple assign STMT and
836 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
837 within roundoff error). This generates the result into a temp and returns
838 the temp; it does not replace or alter the original STMT. */
839 static tree
840 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
842 gimple stmt1, stmt2, stmt3, stmt4;
843 tree tmp2, tmp3;
844 gimple bb1end, bb2end, bb3end;
845 basic_block bb, bb2, bb3, bb4;
846 tree optype, op1, op2;
847 edge e12, e13, e23, e24, e34;
848 gimple_stmt_iterator gsi;
849 tree result;
851 gcc_assert (is_gimple_assign (stmt)
852 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
854 optype = TREE_TYPE (gimple_assign_lhs (stmt));
855 op1 = gimple_assign_rhs1 (stmt);
856 op2 = gimple_assign_rhs2 (stmt);
858 bb = gimple_bb (stmt);
859 gsi = gsi_for_stmt (stmt);
861 result = create_tmp_reg (optype, "PROF");
862 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
863 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
864 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
865 build_int_cst (optype, -1));
866 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
867 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
868 NULL_TREE, NULL_TREE);
869 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
870 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
871 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
872 bb1end = stmt4;
874 /* tmp2 == op2-1 inherited from previous block. */
875 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
876 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
877 bb2end = stmt1;
879 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
880 op1, op2);
881 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
882 bb3end = stmt1;
884 /* Fix CFG. */
885 /* Edge e23 connects bb2 to bb3, etc. */
886 e12 = split_block (bb, bb1end);
887 bb2 = e12->dest;
888 bb2->count = count;
889 e23 = split_block (bb2, bb2end);
890 bb3 = e23->dest;
891 bb3->count = all - count;
892 e34 = split_block (bb3, bb3end);
893 bb4 = e34->dest;
894 bb4->count = all;
896 e12->flags &= ~EDGE_FALLTHRU;
897 e12->flags |= EDGE_FALSE_VALUE;
898 e12->probability = prob;
899 e12->count = count;
901 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
902 e13->probability = REG_BR_PROB_BASE - prob;
903 e13->count = all - count;
905 remove_edge (e23);
907 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
908 e24->probability = REG_BR_PROB_BASE;
909 e24->count = count;
911 e34->probability = REG_BR_PROB_BASE;
912 e34->count = all - count;
914 return result;
917 /* Do transform 2) on INSN if applicable. */
918 static bool
919 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
921 histogram_value histogram;
922 enum tree_code code;
923 gcov_type count, wrong_values, all;
924 tree lhs_type, result, value;
925 gcov_type prob;
926 gimple stmt;
928 stmt = gsi_stmt (*si);
929 if (gimple_code (stmt) != GIMPLE_ASSIGN)
930 return false;
932 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
933 if (!INTEGRAL_TYPE_P (lhs_type))
934 return false;
936 code = gimple_assign_rhs_code (stmt);
938 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
939 return false;
941 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
942 if (!histogram)
943 return false;
945 value = histogram->hvalue.value;
946 wrong_values = histogram->hvalue.counters[0];
947 count = histogram->hvalue.counters[1];
949 gimple_remove_histogram_value (cfun, stmt, histogram);
951 /* We require that we hit a power of 2 at least half of all evaluations. */
952 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
953 || count < wrong_values
954 || optimize_bb_for_size_p (gimple_bb (stmt)))
955 return false;
957 if (dump_file)
959 fprintf (dump_file, "Mod power of 2 transformation on insn ");
960 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
963 /* Compute probability of taking the optimal path. */
964 all = count + wrong_values;
966 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
967 return false;
969 if (all > 0)
970 prob = GCOV_COMPUTE_SCALE (count, all);
971 else
972 prob = 0;
974 result = gimple_mod_pow2 (stmt, prob, count, all);
976 gimple_assign_set_rhs_from_tree (si, result);
977 update_stmt (gsi_stmt (*si));
979 return true;
982 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
983 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
984 supported and this is built into this interface. The probabilities of taking
985 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
986 COUNT2/ALL respectively within roundoff error). This generates the
987 result into a temp and returns the temp; it does not replace or alter
988 the original STMT. */
989 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
991 static tree
992 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
993 gcov_type count1, gcov_type count2, gcov_type all)
995 gimple stmt1, stmt2, stmt3;
996 tree tmp1;
997 gimple bb1end, bb2end = NULL, bb3end;
998 basic_block bb, bb2, bb3, bb4;
999 tree optype, op1, op2;
1000 edge e12, e23 = 0, e24, e34, e14;
1001 gimple_stmt_iterator gsi;
1002 tree result;
1004 gcc_assert (is_gimple_assign (stmt)
1005 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1007 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1008 op1 = gimple_assign_rhs1 (stmt);
1009 op2 = gimple_assign_rhs2 (stmt);
1011 bb = gimple_bb (stmt);
1012 gsi = gsi_for_stmt (stmt);
1014 result = create_tmp_reg (optype, "PROF");
1015 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1016 stmt1 = gimple_build_assign (result, op1);
1017 stmt2 = gimple_build_assign (tmp1, op2);
1018 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1019 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1020 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1021 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1022 bb1end = stmt3;
1024 if (ncounts) /* Assumed to be 0 or 1 */
1026 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
1027 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1028 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1029 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1030 bb2end = stmt2;
1033 /* Fallback case. */
1034 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
1035 result, tmp1);
1036 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1037 bb3end = stmt1;
1039 /* Fix CFG. */
1040 /* Edge e23 connects bb2 to bb3, etc. */
1041 /* However block 3 is optional; if it is not there, references
1042 to 3 really refer to block 2. */
1043 e12 = split_block (bb, bb1end);
1044 bb2 = e12->dest;
1045 bb2->count = all - count1;
1047 if (ncounts) /* Assumed to be 0 or 1. */
1049 e23 = split_block (bb2, bb2end);
1050 bb3 = e23->dest;
1051 bb3->count = all - count1 - count2;
1054 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1055 bb4 = e34->dest;
1056 bb4->count = all;
1058 e12->flags &= ~EDGE_FALLTHRU;
1059 e12->flags |= EDGE_FALSE_VALUE;
1060 e12->probability = REG_BR_PROB_BASE - prob1;
1061 e12->count = all - count1;
1063 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1064 e14->probability = prob1;
1065 e14->count = count1;
1067 if (ncounts) /* Assumed to be 0 or 1. */
1069 e23->flags &= ~EDGE_FALLTHRU;
1070 e23->flags |= EDGE_FALSE_VALUE;
1071 e23->count = all - count1 - count2;
1072 e23->probability = REG_BR_PROB_BASE - prob2;
1074 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1075 e24->probability = prob2;
1076 e24->count = count2;
1079 e34->probability = REG_BR_PROB_BASE;
1080 e34->count = all - count1 - count2;
1082 return result;
1086 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1088 static bool
1089 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1091 histogram_value histogram;
1092 enum tree_code code;
1093 gcov_type count, wrong_values, all;
1094 tree lhs_type, result;
1095 gcov_type prob1, prob2;
1096 unsigned int i, steps;
1097 gcov_type count1, count2;
1098 gimple stmt;
1100 stmt = gsi_stmt (*si);
1101 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1102 return false;
1104 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1105 if (!INTEGRAL_TYPE_P (lhs_type))
1106 return false;
1108 code = gimple_assign_rhs_code (stmt);
1110 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1111 return false;
1113 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1114 if (!histogram)
1115 return false;
1117 all = 0;
1118 wrong_values = 0;
1119 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1120 all += histogram->hvalue.counters[i];
1122 wrong_values += histogram->hvalue.counters[i];
1123 wrong_values += histogram->hvalue.counters[i+1];
1124 steps = histogram->hdata.intvl.steps;
1125 all += wrong_values;
1126 count1 = histogram->hvalue.counters[0];
1127 count2 = histogram->hvalue.counters[1];
1129 /* Compute probability of taking the optimal path. */
1130 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1132 gimple_remove_histogram_value (cfun, stmt, histogram);
1133 return false;
1136 if (flag_profile_correction && count1 + count2 > all)
1137 all = count1 + count2;
1139 gcc_assert (count1 + count2 <= all);
1141 /* We require that we use just subtractions in at least 50% of all
1142 evaluations. */
1143 count = 0;
1144 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1146 count += histogram->hvalue.counters[i];
1147 if (count * 2 >= all)
1148 break;
1150 if (i == steps
1151 || optimize_bb_for_size_p (gimple_bb (stmt)))
1152 return false;
1154 gimple_remove_histogram_value (cfun, stmt, histogram);
1155 if (dump_file)
1157 fprintf (dump_file, "Mod subtract transformation on insn ");
1158 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1161 /* Compute probability of taking the optimal path(s). */
1162 if (all > 0)
1164 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1165 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1167 else
1169 prob1 = prob2 = 0;
1172 /* In practice, "steps" is always 2. This interface reflects this,
1173 and will need to be changed if "steps" can change. */
1174 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1176 gimple_assign_set_rhs_from_tree (si, result);
1177 update_stmt (gsi_stmt (*si));
1179 return true;
1182 static pointer_map_t *cgraph_node_map;
1184 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1185 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1186 that the PROFILE_IDs was already assigned. */
1188 void
1189 init_node_map (bool local)
1191 struct cgraph_node *n;
1192 cgraph_node_map = pointer_map_create ();
1194 FOR_EACH_DEFINED_FUNCTION (n)
1195 if (cgraph_function_with_gimple_body_p (n)
1196 && !cgraph_only_called_directly_p (n))
1198 void **val;
1199 if (local)
1201 n->profile_id = coverage_compute_profile_id (n);
1202 while ((val = pointer_map_contains (cgraph_node_map,
1203 (void *)(size_t)n->profile_id))
1204 || !n->profile_id)
1206 if (dump_file)
1207 fprintf (dump_file, "Local profile-id %i conflict"
1208 " with nodes %s/%i %s/%i\n",
1209 n->profile_id,
1210 cgraph_node_name (n),
1211 n->order,
1212 symtab_node_name (*(symtab_node **)val),
1213 (*(symtab_node **)val)->order);
1214 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1217 else if (!n->profile_id)
1219 if (dump_file)
1220 fprintf (dump_file,
1221 "Node %s/%i has no profile-id"
1222 " (profile feedback missing?)\n",
1223 cgraph_node_name (n),
1224 n->order);
1225 continue;
1227 else if ((val = pointer_map_contains (cgraph_node_map,
1228 (void *)(size_t)n->profile_id)))
1230 if (dump_file)
1231 fprintf (dump_file,
1232 "Node %s/%i has IP profile-id %i conflict. "
1233 "Giving up.\n",
1234 cgraph_node_name (n),
1235 n->order,
1236 n->profile_id);
1237 *val = NULL;
1238 continue;
1240 *pointer_map_insert (cgraph_node_map,
1241 (void *)(size_t)n->profile_id) = (void *)n;
1245 /* Delete the CGRAPH_NODE_MAP. */
1247 void
1248 del_node_map (void)
1250 pointer_map_destroy (cgraph_node_map);
1253 /* Return cgraph node for function with pid */
1255 struct cgraph_node*
1256 find_func_by_profile_id (int profile_id)
1258 void **val = pointer_map_contains (cgraph_node_map,
1259 (void *)(size_t)profile_id);
1260 if (val)
1261 return (struct cgraph_node *)*val;
1262 else
1263 return NULL;
1266 /* Perform sanity check on the indirect call target. Due to race conditions,
1267 false function target may be attributed to an indirect call site. If the
1268 call expression type mismatches with the target function's type, expand_call
1269 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1270 Returns true if TARGET is considered ok for call CALL_STMT. */
1272 static bool
1273 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1275 location_t locus;
1276 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1277 return true;
1279 locus = gimple_location (call_stmt);
1280 if (dump_enabled_p ())
1281 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1282 "Skipping target %s with mismatching types for icall\n",
1283 cgraph_node_name (target));
1284 return false;
1287 /* Do transformation
1289 if (actual_callee_address == address_of_most_common_function/method)
1290 do direct call
1291 else
1292 old call
1295 gimple
1296 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1297 int prob, gcov_type count, gcov_type all)
1299 gimple dcall_stmt, load_stmt, cond_stmt;
1300 tree tmp0, tmp1, tmp;
1301 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1302 tree optype = build_pointer_type (void_type_node);
1303 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1304 gimple_stmt_iterator gsi;
1305 int lp_nr, dflags;
1306 edge e_eh, e;
1307 edge_iterator ei;
1308 gimple_stmt_iterator psi;
1310 cond_bb = gimple_bb (icall_stmt);
1311 gsi = gsi_for_stmt (icall_stmt);
1313 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1314 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1315 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1316 load_stmt = gimple_build_assign (tmp0, tmp);
1317 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1319 tmp = fold_convert (optype, build_addr (direct_call->decl,
1320 current_function_decl));
1321 load_stmt = gimple_build_assign (tmp1, tmp);
1322 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1324 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1325 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1327 gimple_set_vdef (icall_stmt, NULL_TREE);
1328 gimple_set_vuse (icall_stmt, NULL_TREE);
1329 update_stmt (icall_stmt);
1330 dcall_stmt = gimple_copy (icall_stmt);
1331 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1332 dflags = flags_from_decl_or_type (direct_call->decl);
1333 if ((dflags & ECF_NORETURN) != 0)
1334 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1335 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1337 /* Fix CFG. */
1338 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1339 e_cd = split_block (cond_bb, cond_stmt);
1340 dcall_bb = e_cd->dest;
1341 dcall_bb->count = count;
1343 e_di = split_block (dcall_bb, dcall_stmt);
1344 icall_bb = e_di->dest;
1345 icall_bb->count = all - count;
1347 /* Do not disturb existing EH edges from the indirect call. */
1348 if (!stmt_ends_bb_p (icall_stmt))
1349 e_ij = split_block (icall_bb, icall_stmt);
1350 else
1352 e_ij = find_fallthru_edge (icall_bb->succs);
1353 /* The indirect call might be noreturn. */
1354 if (e_ij != NULL)
1356 e_ij->probability = REG_BR_PROB_BASE;
1357 e_ij->count = all - count;
1358 e_ij = single_pred_edge (split_edge (e_ij));
1361 if (e_ij != NULL)
1363 join_bb = e_ij->dest;
1364 join_bb->count = all;
1367 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1368 e_cd->probability = prob;
1369 e_cd->count = count;
1371 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1372 e_ci->probability = REG_BR_PROB_BASE - prob;
1373 e_ci->count = all - count;
1375 remove_edge (e_di);
1377 if (e_ij != NULL)
1379 if ((dflags & ECF_NORETURN) != 0)
1380 e_ij->count = all;
1381 else
1383 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1384 e_dj->probability = REG_BR_PROB_BASE;
1385 e_dj->count = count;
1387 e_ij->count = all - count;
1389 e_ij->probability = REG_BR_PROB_BASE;
1392 /* Insert PHI node for the call result if necessary. */
1393 if (gimple_call_lhs (icall_stmt)
1394 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1395 && (dflags & ECF_NORETURN) == 0)
1397 tree result = gimple_call_lhs (icall_stmt);
1398 gimple phi = create_phi_node (result, join_bb);
1399 gimple_call_set_lhs (icall_stmt,
1400 duplicate_ssa_name (result, icall_stmt));
1401 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1402 gimple_call_set_lhs (dcall_stmt,
1403 duplicate_ssa_name (result, dcall_stmt));
1404 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1407 /* Build an EH edge for the direct call if necessary. */
1408 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1409 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1411 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1414 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1415 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1417 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1418 for (psi = gsi_start_phis (e_eh->dest);
1419 !gsi_end_p (psi); gsi_next (&psi))
1421 gimple phi = gsi_stmt (psi);
1422 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1423 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1426 return dcall_stmt;
1430 For every checked indirect/virtual call determine if most common pid of
1431 function/class method has probability more than 50%. If yes modify code of
1432 this call to:
1435 static bool
1436 gimple_ic_transform (gimple_stmt_iterator *gsi)
1438 gimple stmt = gsi_stmt (*gsi);
1439 histogram_value histogram;
1440 gcov_type val, count, all, bb_all;
1441 struct cgraph_node *direct_call;
1443 if (gimple_code (stmt) != GIMPLE_CALL)
1444 return false;
1446 if (gimple_call_fndecl (stmt) != NULL_TREE)
1447 return false;
1449 if (gimple_call_internal_p (stmt))
1450 return false;
1452 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1453 if (!histogram)
1454 return false;
1456 val = histogram->hvalue.counters [0];
1457 count = histogram->hvalue.counters [1];
1458 all = histogram->hvalue.counters [2];
1460 bb_all = gimple_bb (stmt)->count;
1461 /* The order of CHECK_COUNTER calls is important -
1462 since check_counter can correct the third parameter
1463 and we want to make count <= all <= bb_all. */
1464 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1465 || check_counter (stmt, "ic", &count, &all, all))
1467 gimple_remove_histogram_value (cfun, stmt, histogram);
1468 return false;
1471 if (4 * count <= 3 * all)
1472 return false;
1474 direct_call = find_func_by_profile_id ((int)val);
1476 if (direct_call == NULL)
1478 if (val)
1480 if (dump_file)
1482 fprintf (dump_file, "Indirect call -> direct call from other module");
1483 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1484 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1487 return false;
1490 if (!check_ic_target (stmt, direct_call))
1492 if (dump_file)
1494 fprintf (dump_file, "Indirect call -> direct call ");
1495 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1496 fprintf (dump_file, "=> ");
1497 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1498 fprintf (dump_file, " transformation skipped because of type mismatch");
1499 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1501 gimple_remove_histogram_value (cfun, stmt, histogram);
1502 return false;
1505 if (dump_file)
1507 fprintf (dump_file, "Indirect call -> direct call ");
1508 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1509 fprintf (dump_file, "=> ");
1510 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1511 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1512 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1513 fprintf (dump_file, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1514 " hist->all "HOST_WIDEST_INT_PRINT_DEC"\n", count, all);
1517 return true;
1520 /* Return true if the stringop CALL with FNDECL shall be profiled.
1521 SIZE_ARG be set to the argument index for the size of the string
1522 operation.
1524 static bool
1525 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1527 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1529 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1530 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1531 return false;
1533 switch (fcode)
1535 case BUILT_IN_MEMCPY:
1536 case BUILT_IN_MEMPCPY:
1537 *size_arg = 2;
1538 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1539 INTEGER_TYPE, VOID_TYPE);
1540 case BUILT_IN_MEMSET:
1541 *size_arg = 2;
1542 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1543 INTEGER_TYPE, VOID_TYPE);
1544 case BUILT_IN_BZERO:
1545 *size_arg = 1;
1546 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1547 VOID_TYPE);
1548 default:
1549 gcc_unreachable ();
1553 /* Convert stringop (..., vcall_size)
1554 into
1555 if (vcall_size == icall_size)
1556 stringop (..., icall_size);
1557 else
1558 stringop (..., vcall_size);
1559 assuming we'll propagate a true constant into ICALL_SIZE later. */
1561 static void
1562 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1563 gcov_type count, gcov_type all)
1565 gimple tmp_stmt, cond_stmt, icall_stmt;
1566 tree tmp0, tmp1, vcall_size, optype;
1567 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1568 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1569 gimple_stmt_iterator gsi;
1570 tree fndecl;
1571 int size_arg;
1573 fndecl = gimple_call_fndecl (vcall_stmt);
1574 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1575 gcc_unreachable ();
1577 cond_bb = gimple_bb (vcall_stmt);
1578 gsi = gsi_for_stmt (vcall_stmt);
1580 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1581 optype = TREE_TYPE (vcall_size);
1583 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1584 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1585 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1586 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1588 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1589 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1591 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1592 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1594 gimple_set_vdef (vcall_stmt, NULL);
1595 gimple_set_vuse (vcall_stmt, NULL);
1596 update_stmt (vcall_stmt);
1597 icall_stmt = gimple_copy (vcall_stmt);
1598 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1599 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1601 /* Fix CFG. */
1602 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1603 e_ci = split_block (cond_bb, cond_stmt);
1604 icall_bb = e_ci->dest;
1605 icall_bb->count = count;
1607 e_iv = split_block (icall_bb, icall_stmt);
1608 vcall_bb = e_iv->dest;
1609 vcall_bb->count = all - count;
1611 e_vj = split_block (vcall_bb, vcall_stmt);
1612 join_bb = e_vj->dest;
1613 join_bb->count = all;
1615 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1616 e_ci->probability = prob;
1617 e_ci->count = count;
1619 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1620 e_cv->probability = REG_BR_PROB_BASE - prob;
1621 e_cv->count = all - count;
1623 remove_edge (e_iv);
1625 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1626 e_ij->probability = REG_BR_PROB_BASE;
1627 e_ij->count = count;
1629 e_vj->probability = REG_BR_PROB_BASE;
1630 e_vj->count = all - count;
1632 /* Insert PHI node for the call result if necessary. */
1633 if (gimple_call_lhs (vcall_stmt)
1634 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1636 tree result = gimple_call_lhs (vcall_stmt);
1637 gimple phi = create_phi_node (result, join_bb);
1638 gimple_call_set_lhs (vcall_stmt,
1639 duplicate_ssa_name (result, vcall_stmt));
1640 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1641 gimple_call_set_lhs (icall_stmt,
1642 duplicate_ssa_name (result, icall_stmt));
1643 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1646 /* Because these are all string op builtins, they're all nothrow. */
1647 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1648 gcc_assert (!stmt_could_throw_p (icall_stmt));
1651 /* Find values inside STMT for that we want to measure histograms for
1652 division/modulo optimization. */
1653 static bool
1654 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1656 gimple stmt = gsi_stmt (*gsi);
1657 tree fndecl;
1658 tree blck_size;
1659 enum built_in_function fcode;
1660 histogram_value histogram;
1661 gcov_type count, all, val;
1662 tree dest, src;
1663 unsigned int dest_align, src_align;
1664 gcov_type prob;
1665 tree tree_val;
1666 int size_arg;
1668 if (gimple_code (stmt) != GIMPLE_CALL)
1669 return false;
1670 fndecl = gimple_call_fndecl (stmt);
1671 if (!fndecl)
1672 return false;
1673 fcode = DECL_FUNCTION_CODE (fndecl);
1674 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1675 return false;
1677 blck_size = gimple_call_arg (stmt, size_arg);
1678 if (TREE_CODE (blck_size) == INTEGER_CST)
1679 return false;
1681 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1682 if (!histogram)
1683 return false;
1684 val = histogram->hvalue.counters[0];
1685 count = histogram->hvalue.counters[1];
1686 all = histogram->hvalue.counters[2];
1687 gimple_remove_histogram_value (cfun, stmt, histogram);
1688 /* We require that count is at least half of all; this means
1689 that for the transformation to fire the value must be constant
1690 at least 80% of time. */
1691 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1692 return false;
1693 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1694 return false;
1695 if (all > 0)
1696 prob = GCOV_COMPUTE_SCALE (count, all);
1697 else
1698 prob = 0;
1699 dest = gimple_call_arg (stmt, 0);
1700 dest_align = get_pointer_alignment (dest);
1701 switch (fcode)
1703 case BUILT_IN_MEMCPY:
1704 case BUILT_IN_MEMPCPY:
1705 src = gimple_call_arg (stmt, 1);
1706 src_align = get_pointer_alignment (src);
1707 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1708 return false;
1709 break;
1710 case BUILT_IN_MEMSET:
1711 if (!can_store_by_pieces (val, builtin_memset_read_str,
1712 gimple_call_arg (stmt, 1),
1713 dest_align, true))
1714 return false;
1715 break;
1716 case BUILT_IN_BZERO:
1717 if (!can_store_by_pieces (val, builtin_memset_read_str,
1718 integer_zero_node,
1719 dest_align, true))
1720 return false;
1721 break;
1722 default:
1723 gcc_unreachable ();
1725 tree_val = build_int_cst_wide (get_gcov_type (),
1726 (unsigned HOST_WIDE_INT) val,
1727 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
1728 if (dump_file)
1730 fprintf (dump_file, "Single value %i stringop transformation on ",
1731 (int)val);
1732 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1734 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1736 return true;
1739 void
1740 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1741 HOST_WIDE_INT *expected_size)
1743 histogram_value histogram;
1744 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1745 if (!histogram)
1746 *expected_size = -1;
1747 else if (!histogram->hvalue.counters[1])
1749 *expected_size = -1;
1750 gimple_remove_histogram_value (cfun, stmt, histogram);
1752 else
1754 gcov_type size;
1755 size = ((histogram->hvalue.counters[0]
1756 + histogram->hvalue.counters[1] / 2)
1757 / histogram->hvalue.counters[1]);
1758 /* Even if we can hold bigger value in SIZE, INT_MAX
1759 is safe "infinity" for code generation strategies. */
1760 if (size > INT_MAX)
1761 size = INT_MAX;
1762 *expected_size = size;
1763 gimple_remove_histogram_value (cfun, stmt, histogram);
1765 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1766 if (!histogram)
1767 *expected_align = 0;
1768 else if (!histogram->hvalue.counters[0])
1770 gimple_remove_histogram_value (cfun, stmt, histogram);
1771 *expected_align = 0;
1773 else
1775 gcov_type count;
1776 int alignment;
1778 count = histogram->hvalue.counters[0];
1779 alignment = 1;
1780 while (!(count & alignment)
1781 && (alignment * 2 * BITS_PER_UNIT))
1782 alignment <<= 1;
1783 *expected_align = alignment * BITS_PER_UNIT;
1784 gimple_remove_histogram_value (cfun, stmt, histogram);
1789 /* Find values inside STMT for that we want to measure histograms for
1790 division/modulo optimization. */
1791 static void
1792 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1794 tree lhs, divisor, op0, type;
1795 histogram_value hist;
1797 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1798 return;
1800 lhs = gimple_assign_lhs (stmt);
1801 type = TREE_TYPE (lhs);
1802 if (!INTEGRAL_TYPE_P (type))
1803 return;
1805 switch (gimple_assign_rhs_code (stmt))
1807 case TRUNC_DIV_EXPR:
1808 case TRUNC_MOD_EXPR:
1809 divisor = gimple_assign_rhs2 (stmt);
1810 op0 = gimple_assign_rhs1 (stmt);
1812 values->reserve (3);
1814 if (TREE_CODE (divisor) == SSA_NAME)
1815 /* Check for the case where the divisor is the same value most
1816 of the time. */
1817 values->quick_push (gimple_alloc_histogram_value (cfun,
1818 HIST_TYPE_SINGLE_VALUE,
1819 stmt, divisor));
1821 /* For mod, check whether it is not often a noop (or replaceable by
1822 a few subtractions). */
1823 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1824 && TYPE_UNSIGNED (type))
1826 tree val;
1827 /* Check for a special case where the divisor is power of 2. */
1828 values->quick_push (gimple_alloc_histogram_value (cfun,
1829 HIST_TYPE_POW2,
1830 stmt, divisor));
1832 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1833 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1834 stmt, val);
1835 hist->hdata.intvl.int_start = 0;
1836 hist->hdata.intvl.steps = 2;
1837 values->quick_push (hist);
1839 return;
1841 default:
1842 return;
1846 /* Find calls inside STMT for that we want to measure histograms for
1847 indirect/virtual call optimization. */
1849 static void
1850 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1852 tree callee;
1854 if (gimple_code (stmt) != GIMPLE_CALL
1855 || gimple_call_internal_p (stmt)
1856 || gimple_call_fndecl (stmt) != NULL_TREE)
1857 return;
1859 callee = gimple_call_fn (stmt);
1861 values->reserve (3);
1863 values->quick_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1864 stmt, callee));
1866 return;
1869 /* Find values inside STMT for that we want to measure histograms for
1870 string operations. */
1871 static void
1872 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1874 tree fndecl;
1875 tree blck_size;
1876 tree dest;
1877 int size_arg;
1879 if (gimple_code (stmt) != GIMPLE_CALL)
1880 return;
1881 fndecl = gimple_call_fndecl (stmt);
1882 if (!fndecl)
1883 return;
1885 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1886 return;
1888 dest = gimple_call_arg (stmt, 0);
1889 blck_size = gimple_call_arg (stmt, size_arg);
1891 if (TREE_CODE (blck_size) != INTEGER_CST)
1893 values->safe_push (gimple_alloc_histogram_value (cfun,
1894 HIST_TYPE_SINGLE_VALUE,
1895 stmt, blck_size));
1896 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1897 stmt, blck_size));
1899 if (TREE_CODE (blck_size) != INTEGER_CST)
1900 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1901 stmt, dest));
1904 /* Find values inside STMT for that we want to measure histograms and adds
1905 them to list VALUES. */
1907 static void
1908 gimple_values_to_profile (gimple stmt, histogram_values *values)
1910 gimple_divmod_values_to_profile (stmt, values);
1911 gimple_stringops_values_to_profile (stmt, values);
1912 gimple_indirect_call_to_profile (stmt, values);
1915 void
1916 gimple_find_values_to_profile (histogram_values *values)
1918 basic_block bb;
1919 gimple_stmt_iterator gsi;
1920 unsigned i;
1921 histogram_value hist = NULL;
1923 values->create (0);
1924 FOR_EACH_BB (bb)
1925 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1926 gimple_values_to_profile (gsi_stmt (gsi), values);
1928 FOR_EACH_VEC_ELT (*values, i, hist)
1930 switch (hist->type)
1932 case HIST_TYPE_INTERVAL:
1933 hist->n_counters = hist->hdata.intvl.steps + 2;
1934 break;
1936 case HIST_TYPE_POW2:
1937 hist->n_counters = 2;
1938 break;
1940 case HIST_TYPE_SINGLE_VALUE:
1941 hist->n_counters = 3;
1942 break;
1944 case HIST_TYPE_CONST_DELTA:
1945 hist->n_counters = 4;
1946 break;
1948 case HIST_TYPE_INDIR_CALL:
1949 hist->n_counters = 3;
1950 break;
1952 case HIST_TYPE_AVERAGE:
1953 hist->n_counters = 2;
1954 break;
1956 case HIST_TYPE_IOR:
1957 hist->n_counters = 1;
1958 break;
1960 default:
1961 gcc_unreachable ();
1963 if (dump_file)
1965 fprintf (dump_file, "Stmt ");
1966 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
1967 dump_histogram_value (dump_file, hist);