* rtl.h (CONST_FIXED_P): New predicate macro.
[official-gcc.git] / gcc / value-prof.c
blob25445a10924845cadee14edef4271ab2bf39f4e3
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.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 "tree-flow.h"
37 #include "tree-flow-inline.h"
38 #include "diagnostic.h"
39 #include "gimple-pretty-print.h"
40 #include "coverage.h"
41 #include "tree.h"
42 #include "gcov-io.h"
43 #include "cgraph.h"
44 #include "timevar.h"
45 #include "dumpfile.h"
46 #include "pointer-set.h"
47 #include "profile.h"
49 /* In this file value profile based optimizations are placed. Currently the
50 following optimizations are implemented (for more detailed descriptions
51 see comments at value_profile_transformations):
53 1) Division/modulo specialization. Provided that we can determine that the
54 operands of the division have some special properties, we may use it to
55 produce more effective code.
57 2) Indirect/virtual call specialization. If we can determine most
58 common function callee in indirect/virtual call. We can use this
59 information to improve code effectiveness (especially info for
60 the inliner).
62 3) Speculative prefetching. If we are able to determine that the difference
63 between addresses accessed by a memory reference is usually constant, we
64 may add the prefetch instructions.
65 FIXME: This transformation was removed together with RTL based value
66 profiling.
69 Value profiling internals
70 ==========================
72 Every value profiling transformation starts with defining what values
73 to profile. There are different histogram types (see HIST_TYPE_* in
74 value-prof.h) and each transformation can request one or more histogram
75 types per GIMPLE statement. The function gimple_find_values_to_profile()
76 collects the values to profile in a VEC, and adds the number of counters
77 required for the different histogram types.
79 For a -fprofile-generate run, the statements for which values should be
80 recorded, are instrumented in instrument_values(). The instrumentation
81 is done by helper functions that can be found in tree-profile.c, where
82 new types of histograms can be added if necessary.
84 After a -fprofile-use, the value profiling data is read back in by
85 compute_value_histograms() that translates the collected data to
86 histograms and attaches them to the profiled statements via
87 gimple_add_histogram_value(). Histograms are stored in a hash table
88 that is attached to every intrumented function, see VALUE_HISTOGRAMS
89 in function.h.
91 The value-profile transformations driver is the function
92 gimple_value_profile_transformations(). It traverses all statements in
93 the to-be-transformed function, and looks for statements with one or
94 more histograms attached to it. If a statement has histograms, the
95 transformation functions are called on the statement.
97 Limitations / FIXME / TODO:
98 * Only one histogram of each type can be associated with a statement.
99 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
100 (This type of histogram was originally used to implement a form of
101 stride profiling based speculative prefetching to improve SPEC2000
102 scores for memory-bound benchmarks, mcf and equake. However, this
103 was an RTL value-profiling transformation, and those have all been
104 removed.)
105 * Some value profile transformations are done in builtins.c (?!)
106 * Updating of histograms needs some TLC.
107 * The value profiling code could be used to record analysis results
108 from non-profiling (e.g. VRP).
109 * Adding new profilers should be simplified, starting with a cleanup
110 of what-happens-where andwith making gimple_find_values_to_profile
111 and gimple_value_profile_transformations table-driven, perhaps...
114 static tree gimple_divmod_fixed_value (gimple, tree, int, gcov_type, gcov_type);
115 static tree gimple_mod_pow2 (gimple, int, gcov_type, gcov_type);
116 static tree gimple_mod_subtract (gimple, int, int, int, gcov_type, gcov_type,
117 gcov_type);
118 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
119 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
120 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
121 static bool gimple_stringops_transform (gimple_stmt_iterator *);
122 static bool gimple_ic_transform (gimple_stmt_iterator *);
124 /* Allocate histogram value. */
126 static histogram_value
127 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
128 enum hist_type type, gimple stmt, tree value)
130 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
131 hist->hvalue.value = value;
132 hist->hvalue.stmt = stmt;
133 hist->type = type;
134 return hist;
137 /* Hash value for histogram. */
139 static hashval_t
140 histogram_hash (const void *x)
142 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
145 /* Return nonzero if statement for histogram_value X is Y. */
147 static int
148 histogram_eq (const void *x, const void *y)
150 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
153 /* Set histogram for STMT. */
155 static void
156 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
158 void **loc;
159 if (!hist && !VALUE_HISTOGRAMS (fun))
160 return;
161 if (!VALUE_HISTOGRAMS (fun))
162 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
163 histogram_eq, NULL);
164 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
165 htab_hash_pointer (stmt),
166 hist ? INSERT : NO_INSERT);
167 if (!hist)
169 if (loc)
170 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
171 return;
173 *loc = hist;
176 /* Get histogram list for STMT. */
178 histogram_value
179 gimple_histogram_value (struct function *fun, gimple stmt)
181 if (!VALUE_HISTOGRAMS (fun))
182 return NULL;
183 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
184 htab_hash_pointer (stmt));
187 /* Add histogram for STMT. */
189 void
190 gimple_add_histogram_value (struct function *fun, gimple stmt,
191 histogram_value hist)
193 hist->hvalue.next = gimple_histogram_value (fun, stmt);
194 set_histogram_value (fun, stmt, hist);
198 /* Remove histogram HIST from STMT's histogram list. */
200 void
201 gimple_remove_histogram_value (struct function *fun, gimple stmt,
202 histogram_value hist)
204 histogram_value hist2 = gimple_histogram_value (fun, stmt);
205 if (hist == hist2)
207 set_histogram_value (fun, stmt, hist->hvalue.next);
209 else
211 while (hist2->hvalue.next != hist)
212 hist2 = hist2->hvalue.next;
213 hist2->hvalue.next = hist->hvalue.next;
215 free (hist->hvalue.counters);
216 #ifdef ENABLE_CHECKING
217 memset (hist, 0xab, sizeof (*hist));
218 #endif
219 free (hist);
223 /* Lookup histogram of type TYPE in the STMT. */
225 histogram_value
226 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
227 enum hist_type type)
229 histogram_value hist;
230 for (hist = gimple_histogram_value (fun, stmt); hist;
231 hist = hist->hvalue.next)
232 if (hist->type == type)
233 return hist;
234 return NULL;
237 /* Dump information about HIST to DUMP_FILE. */
239 static void
240 dump_histogram_value (FILE *dump_file, histogram_value hist)
242 switch (hist->type)
244 case HIST_TYPE_INTERVAL:
245 fprintf (dump_file, "Interval counter range %d -- %d",
246 hist->hdata.intvl.int_start,
247 (hist->hdata.intvl.int_start
248 + hist->hdata.intvl.steps - 1));
249 if (hist->hvalue.counters)
251 unsigned int i;
252 fprintf(dump_file, " [");
253 for (i = 0; i < hist->hdata.intvl.steps; i++)
254 fprintf (dump_file, " %d:"HOST_WIDEST_INT_PRINT_DEC,
255 hist->hdata.intvl.int_start + i,
256 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
257 fprintf (dump_file, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC,
258 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
260 fprintf (dump_file, ".\n");
261 break;
263 case HIST_TYPE_POW2:
264 fprintf (dump_file, "Pow2 counter ");
265 if (hist->hvalue.counters)
267 fprintf (dump_file, "pow2:"HOST_WIDEST_INT_PRINT_DEC
268 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC,
269 (HOST_WIDEST_INT) hist->hvalue.counters[0],
270 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
272 fprintf (dump_file, ".\n");
273 break;
275 case HIST_TYPE_SINGLE_VALUE:
276 fprintf (dump_file, "Single value ");
277 if (hist->hvalue.counters)
279 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
280 " match:"HOST_WIDEST_INT_PRINT_DEC
281 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
282 (HOST_WIDEST_INT) hist->hvalue.counters[0],
283 (HOST_WIDEST_INT) hist->hvalue.counters[1],
284 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
286 fprintf (dump_file, ".\n");
287 break;
289 case HIST_TYPE_AVERAGE:
290 fprintf (dump_file, "Average value ");
291 if (hist->hvalue.counters)
293 fprintf (dump_file, "sum:"HOST_WIDEST_INT_PRINT_DEC
294 " times:"HOST_WIDEST_INT_PRINT_DEC,
295 (HOST_WIDEST_INT) hist->hvalue.counters[0],
296 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
298 fprintf (dump_file, ".\n");
299 break;
301 case HIST_TYPE_IOR:
302 fprintf (dump_file, "IOR value ");
303 if (hist->hvalue.counters)
305 fprintf (dump_file, "ior:"HOST_WIDEST_INT_PRINT_DEC,
306 (HOST_WIDEST_INT) hist->hvalue.counters[0]);
308 fprintf (dump_file, ".\n");
309 break;
311 case HIST_TYPE_CONST_DELTA:
312 fprintf (dump_file, "Constant delta ");
313 if (hist->hvalue.counters)
315 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
316 " match:"HOST_WIDEST_INT_PRINT_DEC
317 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
318 (HOST_WIDEST_INT) hist->hvalue.counters[0],
319 (HOST_WIDEST_INT) hist->hvalue.counters[1],
320 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
322 fprintf (dump_file, ".\n");
323 break;
324 case HIST_TYPE_INDIR_CALL:
325 fprintf (dump_file, "Indirect call ");
326 if (hist->hvalue.counters)
328 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
329 " match:"HOST_WIDEST_INT_PRINT_DEC
330 " all:"HOST_WIDEST_INT_PRINT_DEC,
331 (HOST_WIDEST_INT) hist->hvalue.counters[0],
332 (HOST_WIDEST_INT) hist->hvalue.counters[1],
333 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
335 fprintf (dump_file, ".\n");
336 break;
340 /* Dump all histograms attached to STMT to DUMP_FILE. */
342 void
343 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
345 histogram_value hist;
346 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
347 dump_histogram_value (dump_file, hist);
350 /* Remove all histograms associated with STMT. */
352 void
353 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
355 histogram_value val;
356 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
357 gimple_remove_histogram_value (fun, stmt, val);
360 /* Duplicate all histograms associates with OSTMT to STMT. */
362 void
363 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
364 struct function *ofun, gimple ostmt)
366 histogram_value val;
367 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
369 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
370 memcpy (new_val, val, sizeof (*val));
371 new_val->hvalue.stmt = stmt;
372 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
373 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
374 gimple_add_histogram_value (fun, stmt, new_val);
379 /* Move all histograms associated with OSTMT to STMT. */
381 void
382 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
384 histogram_value val = gimple_histogram_value (fun, ostmt);
385 if (val)
387 /* The following three statements can't be reordered,
388 because histogram hashtab relies on stmt field value
389 for finding the exact slot. */
390 set_histogram_value (fun, ostmt, NULL);
391 for (; val != NULL; val = val->hvalue.next)
392 val->hvalue.stmt = stmt;
393 set_histogram_value (fun, stmt, val);
397 static bool error_found = false;
399 /* Helper function for verify_histograms. For each histogram reachable via htab
400 walk verify that it was reached via statement walk. */
402 static int
403 visit_hist (void **slot, void *data)
405 struct pointer_set_t *visited = (struct pointer_set_t *) data;
406 histogram_value hist = *(histogram_value *) slot;
407 if (!pointer_set_contains (visited, hist))
409 error ("dead histogram");
410 dump_histogram_value (stderr, hist);
411 debug_gimple_stmt (hist->hvalue.stmt);
412 error_found = true;
414 return 1;
418 /* Verify sanity of the histograms. */
420 DEBUG_FUNCTION void
421 verify_histograms (void)
423 basic_block bb;
424 gimple_stmt_iterator gsi;
425 histogram_value hist;
426 struct pointer_set_t *visited_hists;
428 error_found = false;
429 visited_hists = pointer_set_create ();
430 FOR_EACH_BB (bb)
431 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
433 gimple stmt = gsi_stmt (gsi);
435 for (hist = gimple_histogram_value (cfun, stmt); hist;
436 hist = hist->hvalue.next)
438 if (hist->hvalue.stmt != stmt)
440 error ("Histogram value statement does not correspond to "
441 "the statement it is associated with");
442 debug_gimple_stmt (stmt);
443 dump_histogram_value (stderr, hist);
444 error_found = true;
446 pointer_set_insert (visited_hists, hist);
449 if (VALUE_HISTOGRAMS (cfun))
450 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, visited_hists);
451 pointer_set_destroy (visited_hists);
452 if (error_found)
453 internal_error ("verify_histograms failed");
456 /* Helper function for verify_histograms. For each histogram reachable via htab
457 walk verify that it was reached via statement walk. */
459 static int
460 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
462 histogram_value hist = *(histogram_value *) slot;
463 free (hist->hvalue.counters);
464 #ifdef ENABLE_CHECKING
465 memset (hist, 0xab, sizeof (*hist));
466 #endif
467 free (hist);
468 return 1;
471 void
472 free_histograms (void)
474 if (VALUE_HISTOGRAMS (cfun))
476 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
477 htab_delete (VALUE_HISTOGRAMS (cfun));
478 VALUE_HISTOGRAMS (cfun) = NULL;
483 /* The overall number of invocations of the counter should match
484 execution count of basic block. Report it as error rather than
485 internal error as it might mean that user has misused the profile
486 somehow. */
488 static bool
489 check_counter (gimple stmt, const char * name,
490 gcov_type *count, gcov_type *all, gcov_type bb_count)
492 if (*all != bb_count || *count > *all)
494 location_t locus;
495 locus = (stmt != NULL)
496 ? gimple_location (stmt)
497 : DECL_SOURCE_LOCATION (current_function_decl);
498 if (flag_profile_correction)
500 inform (locus, "correcting inconsistent value profile: "
501 "%s profiler overall count (%d) does not match BB count "
502 "(%d)", name, (int)*all, (int)bb_count);
503 *all = bb_count;
504 if (*count > *all)
505 *count = *all;
506 return false;
508 else
510 error_at (locus, "corrupted value profile: %s "
511 "profile counter (%d out of %d) inconsistent with "
512 "basic-block count (%d)",
513 name,
514 (int) *count,
515 (int) *all,
516 (int) bb_count);
517 return true;
521 return false;
525 /* GIMPLE based transformations. */
527 bool
528 gimple_value_profile_transformations (void)
530 basic_block bb;
531 gimple_stmt_iterator gsi;
532 bool changed = false;
534 FOR_EACH_BB (bb)
536 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
538 gimple stmt = gsi_stmt (gsi);
539 histogram_value th = gimple_histogram_value (cfun, stmt);
540 if (!th)
541 continue;
543 if (dump_file)
545 fprintf (dump_file, "Trying transformations on stmt ");
546 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
547 dump_histograms_for_stmt (cfun, dump_file, stmt);
550 /* Transformations: */
551 /* The order of things in this conditional controls which
552 transformation is used when more than one is applicable. */
553 /* It is expected that any code added by the transformations
554 will be added before the current statement, and that the
555 current statement remain valid (although possibly
556 modified) upon return. */
557 if (gimple_mod_subtract_transform (&gsi)
558 || gimple_divmod_fixed_value_transform (&gsi)
559 || gimple_mod_pow2_value_transform (&gsi)
560 || gimple_stringops_transform (&gsi)
561 || gimple_ic_transform (&gsi))
563 stmt = gsi_stmt (gsi);
564 changed = true;
565 /* Original statement may no longer be in the same block. */
566 if (bb != gimple_bb (stmt))
568 bb = gimple_bb (stmt);
569 gsi = gsi_for_stmt (stmt);
575 if (changed)
577 counts_to_freqs ();
580 return changed;
584 /* Generate code for transformation 1 (with parent gimple assignment
585 STMT and probability of taking the optimal path PROB, which is
586 equivalent to COUNT/ALL within roundoff error). This generates the
587 result into a temp and returns the temp; it does not replace or
588 alter the original STMT. */
590 static tree
591 gimple_divmod_fixed_value (gimple stmt, tree value, int prob, gcov_type count,
592 gcov_type all)
594 gimple stmt1, stmt2, stmt3;
595 tree tmp0, tmp1, tmp2;
596 gimple bb1end, bb2end, bb3end;
597 basic_block bb, bb2, bb3, bb4;
598 tree optype, op1, op2;
599 edge e12, e13, e23, e24, e34;
600 gimple_stmt_iterator gsi;
602 gcc_assert (is_gimple_assign (stmt)
603 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
604 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
606 optype = TREE_TYPE (gimple_assign_lhs (stmt));
607 op1 = gimple_assign_rhs1 (stmt);
608 op2 = gimple_assign_rhs2 (stmt);
610 bb = gimple_bb (stmt);
611 gsi = gsi_for_stmt (stmt);
613 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
614 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
615 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
616 stmt2 = gimple_build_assign (tmp1, op2);
617 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
618 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
619 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
620 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
621 bb1end = stmt3;
623 tmp2 = create_tmp_reg (optype, "PROF");
624 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
625 op1, tmp0);
626 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
627 bb2end = stmt1;
629 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
630 op1, op2);
631 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
632 bb3end = stmt1;
634 /* Fix CFG. */
635 /* Edge e23 connects bb2 to bb3, etc. */
636 e12 = split_block (bb, bb1end);
637 bb2 = e12->dest;
638 bb2->count = count;
639 e23 = split_block (bb2, bb2end);
640 bb3 = e23->dest;
641 bb3->count = all - count;
642 e34 = split_block (bb3, bb3end);
643 bb4 = e34->dest;
644 bb4->count = all;
646 e12->flags &= ~EDGE_FALLTHRU;
647 e12->flags |= EDGE_FALSE_VALUE;
648 e12->probability = prob;
649 e12->count = count;
651 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
652 e13->probability = REG_BR_PROB_BASE - prob;
653 e13->count = all - count;
655 remove_edge (e23);
657 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
658 e24->probability = REG_BR_PROB_BASE;
659 e24->count = count;
661 e34->probability = REG_BR_PROB_BASE;
662 e34->count = all - count;
664 return tmp2;
668 /* Do transform 1) on INSN if applicable. */
670 static bool
671 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
673 histogram_value histogram;
674 enum tree_code code;
675 gcov_type val, count, all;
676 tree result, value, tree_val;
677 gcov_type prob;
678 gimple stmt;
680 stmt = gsi_stmt (*si);
681 if (gimple_code (stmt) != GIMPLE_ASSIGN)
682 return false;
684 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
685 return false;
687 code = gimple_assign_rhs_code (stmt);
689 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
690 return false;
692 histogram = gimple_histogram_value_of_type (cfun, stmt,
693 HIST_TYPE_SINGLE_VALUE);
694 if (!histogram)
695 return false;
697 value = histogram->hvalue.value;
698 val = histogram->hvalue.counters[0];
699 count = histogram->hvalue.counters[1];
700 all = histogram->hvalue.counters[2];
701 gimple_remove_histogram_value (cfun, stmt, histogram);
703 /* We require that count is at least half of all; this means
704 that for the transformation to fire the value must be constant
705 at least 50% of time (and 75% gives the guarantee of usage). */
706 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
707 || 2 * count < all
708 || optimize_bb_for_size_p (gimple_bb (stmt)))
709 return false;
711 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
712 return false;
714 /* Compute probability of taking the optimal path. */
715 if (all > 0)
716 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
717 else
718 prob = 0;
720 tree_val = build_int_cst_wide (get_gcov_type (),
721 (unsigned HOST_WIDE_INT) val,
722 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
723 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
725 if (dump_file)
727 fprintf (dump_file, "Div/mod by constant ");
728 print_generic_expr (dump_file, value, TDF_SLIM);
729 fprintf (dump_file, "=");
730 print_generic_expr (dump_file, tree_val, TDF_SLIM);
731 fprintf (dump_file, " transformation on insn ");
732 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
735 gimple_assign_set_rhs_from_tree (si, result);
736 update_stmt (gsi_stmt (*si));
738 return true;
741 /* Generate code for transformation 2 (with parent gimple assign STMT and
742 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
743 within roundoff error). This generates the result into a temp and returns
744 the temp; it does not replace or alter the original STMT. */
745 static tree
746 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
748 gimple stmt1, stmt2, stmt3, stmt4;
749 tree tmp2, tmp3;
750 gimple bb1end, bb2end, bb3end;
751 basic_block bb, bb2, bb3, bb4;
752 tree optype, op1, op2;
753 edge e12, e13, e23, e24, e34;
754 gimple_stmt_iterator gsi;
755 tree result;
757 gcc_assert (is_gimple_assign (stmt)
758 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
760 optype = TREE_TYPE (gimple_assign_lhs (stmt));
761 op1 = gimple_assign_rhs1 (stmt);
762 op2 = gimple_assign_rhs2 (stmt);
764 bb = gimple_bb (stmt);
765 gsi = gsi_for_stmt (stmt);
767 result = create_tmp_reg (optype, "PROF");
768 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
769 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
770 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
771 build_int_cst (optype, -1));
772 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
773 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
774 NULL_TREE, NULL_TREE);
775 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
776 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
777 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
778 bb1end = stmt4;
780 /* tmp2 == op2-1 inherited from previous block. */
781 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
782 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
783 bb2end = stmt1;
785 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
786 op1, op2);
787 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
788 bb3end = stmt1;
790 /* Fix CFG. */
791 /* Edge e23 connects bb2 to bb3, etc. */
792 e12 = split_block (bb, bb1end);
793 bb2 = e12->dest;
794 bb2->count = count;
795 e23 = split_block (bb2, bb2end);
796 bb3 = e23->dest;
797 bb3->count = all - count;
798 e34 = split_block (bb3, bb3end);
799 bb4 = e34->dest;
800 bb4->count = all;
802 e12->flags &= ~EDGE_FALLTHRU;
803 e12->flags |= EDGE_FALSE_VALUE;
804 e12->probability = prob;
805 e12->count = count;
807 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
808 e13->probability = REG_BR_PROB_BASE - prob;
809 e13->count = all - count;
811 remove_edge (e23);
813 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
814 e24->probability = REG_BR_PROB_BASE;
815 e24->count = count;
817 e34->probability = REG_BR_PROB_BASE;
818 e34->count = all - count;
820 return result;
823 /* Do transform 2) on INSN if applicable. */
824 static bool
825 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
827 histogram_value histogram;
828 enum tree_code code;
829 gcov_type count, wrong_values, all;
830 tree lhs_type, result, value;
831 gcov_type prob;
832 gimple stmt;
834 stmt = gsi_stmt (*si);
835 if (gimple_code (stmt) != GIMPLE_ASSIGN)
836 return false;
838 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
839 if (!INTEGRAL_TYPE_P (lhs_type))
840 return false;
842 code = gimple_assign_rhs_code (stmt);
844 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
845 return false;
847 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
848 if (!histogram)
849 return false;
851 value = histogram->hvalue.value;
852 wrong_values = histogram->hvalue.counters[0];
853 count = histogram->hvalue.counters[1];
855 gimple_remove_histogram_value (cfun, stmt, histogram);
857 /* We require that we hit a power of 2 at least half of all evaluations. */
858 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
859 || count < wrong_values
860 || optimize_bb_for_size_p (gimple_bb (stmt)))
861 return false;
863 if (dump_file)
865 fprintf (dump_file, "Mod power of 2 transformation on insn ");
866 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
869 /* Compute probability of taking the optimal path. */
870 all = count + wrong_values;
872 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
873 return false;
875 if (all > 0)
876 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
877 else
878 prob = 0;
880 result = gimple_mod_pow2 (stmt, prob, count, all);
882 gimple_assign_set_rhs_from_tree (si, result);
883 update_stmt (gsi_stmt (*si));
885 return true;
888 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
889 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
890 supported and this is built into this interface. The probabilities of taking
891 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
892 COUNT2/ALL respectively within roundoff error). This generates the
893 result into a temp and returns the temp; it does not replace or alter
894 the original STMT. */
895 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
897 static tree
898 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
899 gcov_type count1, gcov_type count2, gcov_type all)
901 gimple stmt1, stmt2, stmt3;
902 tree tmp1;
903 gimple bb1end, bb2end = NULL, bb3end;
904 basic_block bb, bb2, bb3, bb4;
905 tree optype, op1, op2;
906 edge e12, e23 = 0, e24, e34, e14;
907 gimple_stmt_iterator gsi;
908 tree result;
910 gcc_assert (is_gimple_assign (stmt)
911 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
913 optype = TREE_TYPE (gimple_assign_lhs (stmt));
914 op1 = gimple_assign_rhs1 (stmt);
915 op2 = gimple_assign_rhs2 (stmt);
917 bb = gimple_bb (stmt);
918 gsi = gsi_for_stmt (stmt);
920 result = create_tmp_reg (optype, "PROF");
921 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
922 stmt1 = gimple_build_assign (result, op1);
923 stmt2 = gimple_build_assign (tmp1, op2);
924 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
925 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
926 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
927 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
928 bb1end = stmt3;
930 if (ncounts) /* Assumed to be 0 or 1 */
932 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
933 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
934 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
935 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
936 bb2end = stmt2;
939 /* Fallback case. */
940 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
941 result, tmp1);
942 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
943 bb3end = stmt1;
945 /* Fix CFG. */
946 /* Edge e23 connects bb2 to bb3, etc. */
947 /* However block 3 is optional; if it is not there, references
948 to 3 really refer to block 2. */
949 e12 = split_block (bb, bb1end);
950 bb2 = e12->dest;
951 bb2->count = all - count1;
953 if (ncounts) /* Assumed to be 0 or 1. */
955 e23 = split_block (bb2, bb2end);
956 bb3 = e23->dest;
957 bb3->count = all - count1 - count2;
960 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
961 bb4 = e34->dest;
962 bb4->count = all;
964 e12->flags &= ~EDGE_FALLTHRU;
965 e12->flags |= EDGE_FALSE_VALUE;
966 e12->probability = REG_BR_PROB_BASE - prob1;
967 e12->count = all - count1;
969 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
970 e14->probability = prob1;
971 e14->count = count1;
973 if (ncounts) /* Assumed to be 0 or 1. */
975 e23->flags &= ~EDGE_FALLTHRU;
976 e23->flags |= EDGE_FALSE_VALUE;
977 e23->count = all - count1 - count2;
978 e23->probability = REG_BR_PROB_BASE - prob2;
980 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
981 e24->probability = prob2;
982 e24->count = count2;
985 e34->probability = REG_BR_PROB_BASE;
986 e34->count = all - count1 - count2;
988 return result;
992 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
994 static bool
995 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
997 histogram_value histogram;
998 enum tree_code code;
999 gcov_type count, wrong_values, all;
1000 tree lhs_type, result;
1001 gcov_type prob1, prob2;
1002 unsigned int i, steps;
1003 gcov_type count1, count2;
1004 gimple stmt;
1006 stmt = gsi_stmt (*si);
1007 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1008 return false;
1010 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1011 if (!INTEGRAL_TYPE_P (lhs_type))
1012 return false;
1014 code = gimple_assign_rhs_code (stmt);
1016 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1017 return false;
1019 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1020 if (!histogram)
1021 return false;
1023 all = 0;
1024 wrong_values = 0;
1025 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1026 all += histogram->hvalue.counters[i];
1028 wrong_values += histogram->hvalue.counters[i];
1029 wrong_values += histogram->hvalue.counters[i+1];
1030 steps = histogram->hdata.intvl.steps;
1031 all += wrong_values;
1032 count1 = histogram->hvalue.counters[0];
1033 count2 = histogram->hvalue.counters[1];
1035 /* Compute probability of taking the optimal path. */
1036 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1038 gimple_remove_histogram_value (cfun, stmt, histogram);
1039 return false;
1042 if (flag_profile_correction && count1 + count2 > all)
1043 all = count1 + count2;
1045 gcc_assert (count1 + count2 <= all);
1047 /* We require that we use just subtractions in at least 50% of all
1048 evaluations. */
1049 count = 0;
1050 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1052 count += histogram->hvalue.counters[i];
1053 if (count * 2 >= all)
1054 break;
1056 if (i == steps
1057 || optimize_bb_for_size_p (gimple_bb (stmt)))
1058 return false;
1060 gimple_remove_histogram_value (cfun, stmt, histogram);
1061 if (dump_file)
1063 fprintf (dump_file, "Mod subtract transformation on insn ");
1064 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1067 /* Compute probability of taking the optimal path(s). */
1068 if (all > 0)
1070 prob1 = (count1 * REG_BR_PROB_BASE + all / 2) / all;
1071 prob2 = (count2 * REG_BR_PROB_BASE + all / 2) / all;
1073 else
1075 prob1 = prob2 = 0;
1078 /* In practice, "steps" is always 2. This interface reflects this,
1079 and will need to be changed if "steps" can change. */
1080 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1082 gimple_assign_set_rhs_from_tree (si, result);
1083 update_stmt (gsi_stmt (*si));
1085 return true;
1088 static VEC(cgraph_node_ptr, heap) *cgraph_node_map = NULL;
1090 /* Initialize map from FUNCDEF_NO to CGRAPH_NODE. */
1092 void
1093 init_node_map (void)
1095 struct cgraph_node *n;
1097 if (get_last_funcdef_no ())
1098 VEC_safe_grow_cleared (cgraph_node_ptr, heap,
1099 cgraph_node_map, get_last_funcdef_no ());
1101 FOR_EACH_FUNCTION (n)
1103 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
1104 VEC_replace (cgraph_node_ptr, cgraph_node_map,
1105 DECL_STRUCT_FUNCTION (n->symbol.decl)->funcdef_no, n);
1109 /* Delete the CGRAPH_NODE_MAP. */
1111 void
1112 del_node_map (void)
1114 VEC_free (cgraph_node_ptr, heap, cgraph_node_map);
1115 cgraph_node_map = NULL;
1118 /* Return cgraph node for function with pid */
1120 static inline struct cgraph_node*
1121 find_func_by_funcdef_no (int func_id)
1123 int max_id = get_last_funcdef_no ();
1124 if (func_id >= max_id || VEC_index (cgraph_node_ptr,
1125 cgraph_node_map,
1126 func_id) == NULL)
1128 if (flag_profile_correction)
1129 inform (DECL_SOURCE_LOCATION (current_function_decl),
1130 "Inconsistent profile: indirect call target (%d) does not exist", func_id);
1131 else
1132 error ("Inconsistent profile: indirect call target (%d) does not exist", func_id);
1134 return NULL;
1137 return VEC_index (cgraph_node_ptr, cgraph_node_map, func_id);
1140 /* Perform sanity check on the indirect call target. Due to race conditions,
1141 false function target may be attributed to an indirect call site. If the
1142 call expression type mismatches with the target function's type, expand_call
1143 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1144 Returns true if TARGET is considered ok for call CALL_STMT. */
1146 static bool
1147 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1149 location_t locus;
1150 if (gimple_check_call_matching_types (call_stmt, target->symbol.decl))
1151 return true;
1153 locus = gimple_location (call_stmt);
1154 inform (locus, "Skipping target %s with mismatching types for icall ",
1155 cgraph_node_name (target));
1156 return false;
1159 /* Do transformation
1161 if (actual_callee_address == address_of_most_common_function/method)
1162 do direct call
1163 else
1164 old call
1167 static gimple
1168 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1169 int prob, gcov_type count, gcov_type all)
1171 gimple dcall_stmt, load_stmt, cond_stmt;
1172 tree tmp0, tmp1, tmp;
1173 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1174 tree optype = build_pointer_type (void_type_node);
1175 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1176 gimple_stmt_iterator gsi;
1177 int lp_nr, dflags;
1179 cond_bb = gimple_bb (icall_stmt);
1180 gsi = gsi_for_stmt (icall_stmt);
1182 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1183 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1184 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1185 load_stmt = gimple_build_assign (tmp0, tmp);
1186 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1188 tmp = fold_convert (optype, build_addr (direct_call->symbol.decl,
1189 current_function_decl));
1190 load_stmt = gimple_build_assign (tmp1, tmp);
1191 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1193 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1194 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1196 gimple_set_vdef (icall_stmt, NULL_TREE);
1197 gimple_set_vuse (icall_stmt, NULL_TREE);
1198 update_stmt (icall_stmt);
1199 dcall_stmt = gimple_copy (icall_stmt);
1200 gimple_call_set_fndecl (dcall_stmt, direct_call->symbol.decl);
1201 dflags = flags_from_decl_or_type (direct_call->symbol.decl);
1202 if ((dflags & ECF_NORETURN) != 0)
1203 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1204 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1206 /* Fix CFG. */
1207 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1208 e_cd = split_block (cond_bb, cond_stmt);
1209 dcall_bb = e_cd->dest;
1210 dcall_bb->count = count;
1212 e_di = split_block (dcall_bb, dcall_stmt);
1213 icall_bb = e_di->dest;
1214 icall_bb->count = all - count;
1216 /* Do not disturb existing EH edges from the indirect call. */
1217 if (!stmt_ends_bb_p (icall_stmt))
1218 e_ij = split_block (icall_bb, icall_stmt);
1219 else
1221 e_ij = find_fallthru_edge (icall_bb->succs);
1222 /* The indirect call might be noreturn. */
1223 if (e_ij != NULL)
1225 e_ij->probability = REG_BR_PROB_BASE;
1226 e_ij->count = all - count;
1227 e_ij = single_pred_edge (split_edge (e_ij));
1230 if (e_ij != NULL)
1232 join_bb = e_ij->dest;
1233 join_bb->count = all;
1236 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1237 e_cd->probability = prob;
1238 e_cd->count = count;
1240 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1241 e_ci->probability = REG_BR_PROB_BASE - prob;
1242 e_ci->count = all - count;
1244 remove_edge (e_di);
1246 if (e_ij != NULL)
1248 if ((dflags & ECF_NORETURN) != 0)
1249 e_ij->count = all;
1250 else
1252 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1253 e_dj->probability = REG_BR_PROB_BASE;
1254 e_dj->count = count;
1256 e_ij->count = all - count;
1258 e_ij->probability = REG_BR_PROB_BASE;
1261 /* Insert PHI node for the call result if necessary. */
1262 if (gimple_call_lhs (icall_stmt)
1263 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1264 && (dflags & ECF_NORETURN) == 0)
1266 tree result = gimple_call_lhs (icall_stmt);
1267 gimple phi = create_phi_node (result, join_bb);
1268 gimple_call_set_lhs (icall_stmt,
1269 duplicate_ssa_name (result, icall_stmt));
1270 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1271 gimple_call_set_lhs (dcall_stmt,
1272 duplicate_ssa_name (result, dcall_stmt));
1273 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1276 /* Build an EH edge for the direct call if necessary. */
1277 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1278 if (lp_nr != 0
1279 && stmt_could_throw_p (dcall_stmt))
1281 edge e_eh, e;
1282 edge_iterator ei;
1283 gimple_stmt_iterator psi;
1285 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1286 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1287 if (e_eh->flags & EDGE_EH)
1288 break;
1289 e = make_edge (dcall_bb, e_eh->dest, EDGE_EH);
1290 for (psi = gsi_start_phis (e_eh->dest);
1291 !gsi_end_p (psi); gsi_next (&psi))
1293 gimple phi = gsi_stmt (psi);
1294 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1295 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1299 return dcall_stmt;
1303 For every checked indirect/virtual call determine if most common pid of
1304 function/class method has probability more than 50%. If yes modify code of
1305 this call to:
1308 static bool
1309 gimple_ic_transform (gimple_stmt_iterator *gsi)
1311 gimple stmt = gsi_stmt (*gsi);
1312 histogram_value histogram;
1313 gcov_type val, count, all, bb_all;
1314 gcov_type prob;
1315 gimple modify;
1316 struct cgraph_node *direct_call;
1318 if (gimple_code (stmt) != GIMPLE_CALL)
1319 return false;
1321 if (gimple_call_fndecl (stmt) != NULL_TREE)
1322 return false;
1324 if (gimple_call_internal_p (stmt))
1325 return false;
1327 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1328 if (!histogram)
1329 return false;
1331 val = histogram->hvalue.counters [0];
1332 count = histogram->hvalue.counters [1];
1333 all = histogram->hvalue.counters [2];
1334 gimple_remove_histogram_value (cfun, stmt, histogram);
1336 if (4 * count <= 3 * all)
1337 return false;
1339 bb_all = gimple_bb (stmt)->count;
1340 /* The order of CHECK_COUNTER calls is important -
1341 since check_counter can correct the third parameter
1342 and we want to make count <= all <= bb_all. */
1343 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1344 || check_counter (stmt, "ic", &count, &all, all))
1345 return false;
1347 if (all > 0)
1348 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
1349 else
1350 prob = 0;
1351 direct_call = find_func_by_funcdef_no ((int)val);
1353 if (direct_call == NULL)
1354 return false;
1356 if (!check_ic_target (stmt, direct_call))
1357 return false;
1359 modify = gimple_ic (stmt, direct_call, prob, count, all);
1361 if (dump_file)
1363 fprintf (dump_file, "Indirect call -> direct call ");
1364 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1365 fprintf (dump_file, "=> ");
1366 print_generic_expr (dump_file, direct_call->symbol.decl, TDF_SLIM);
1367 fprintf (dump_file, " transformation on insn ");
1368 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1369 fprintf (dump_file, " to ");
1370 print_gimple_stmt (dump_file, modify, 0, TDF_SLIM);
1371 fprintf (dump_file, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1372 " hist->all "HOST_WIDEST_INT_PRINT_DEC"\n", count, all);
1375 return true;
1378 /* Return true if the stringop CALL with FNDECL shall be profiled.
1379 SIZE_ARG be set to the argument index for the size of the string
1380 operation.
1382 static bool
1383 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1385 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1387 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1388 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1389 return false;
1391 switch (fcode)
1393 case BUILT_IN_MEMCPY:
1394 case BUILT_IN_MEMPCPY:
1395 *size_arg = 2;
1396 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1397 INTEGER_TYPE, VOID_TYPE);
1398 case BUILT_IN_MEMSET:
1399 *size_arg = 2;
1400 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1401 INTEGER_TYPE, VOID_TYPE);
1402 case BUILT_IN_BZERO:
1403 *size_arg = 1;
1404 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1405 VOID_TYPE);
1406 default:
1407 gcc_unreachable ();
1411 /* Convert stringop (..., vcall_size)
1412 into
1413 if (vcall_size == icall_size)
1414 stringop (..., icall_size);
1415 else
1416 stringop (..., vcall_size);
1417 assuming we'll propagate a true constant into ICALL_SIZE later. */
1419 static void
1420 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1421 gcov_type count, gcov_type all)
1423 gimple tmp_stmt, cond_stmt, icall_stmt;
1424 tree tmp0, tmp1, vcall_size, optype;
1425 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1426 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1427 gimple_stmt_iterator gsi;
1428 tree fndecl;
1429 int size_arg;
1431 fndecl = gimple_call_fndecl (vcall_stmt);
1432 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1433 gcc_unreachable();
1435 cond_bb = gimple_bb (vcall_stmt);
1436 gsi = gsi_for_stmt (vcall_stmt);
1438 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1439 optype = TREE_TYPE (vcall_size);
1441 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1442 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1443 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1444 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1446 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1447 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1449 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1450 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1452 gimple_set_vdef (vcall_stmt, NULL);
1453 gimple_set_vuse (vcall_stmt, NULL);
1454 update_stmt (vcall_stmt);
1455 icall_stmt = gimple_copy (vcall_stmt);
1456 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1457 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1459 /* Fix CFG. */
1460 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1461 e_ci = split_block (cond_bb, cond_stmt);
1462 icall_bb = e_ci->dest;
1463 icall_bb->count = count;
1465 e_iv = split_block (icall_bb, icall_stmt);
1466 vcall_bb = e_iv->dest;
1467 vcall_bb->count = all - count;
1469 e_vj = split_block (vcall_bb, vcall_stmt);
1470 join_bb = e_vj->dest;
1471 join_bb->count = all;
1473 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1474 e_ci->probability = prob;
1475 e_ci->count = count;
1477 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1478 e_cv->probability = REG_BR_PROB_BASE - prob;
1479 e_cv->count = all - count;
1481 remove_edge (e_iv);
1483 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1484 e_ij->probability = REG_BR_PROB_BASE;
1485 e_ij->count = count;
1487 e_vj->probability = REG_BR_PROB_BASE;
1488 e_vj->count = all - count;
1490 /* Insert PHI node for the call result if necessary. */
1491 if (gimple_call_lhs (vcall_stmt)
1492 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1494 tree result = gimple_call_lhs (vcall_stmt);
1495 gimple phi = create_phi_node (result, join_bb);
1496 gimple_call_set_lhs (vcall_stmt,
1497 duplicate_ssa_name (result, vcall_stmt));
1498 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1499 gimple_call_set_lhs (icall_stmt,
1500 duplicate_ssa_name (result, icall_stmt));
1501 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1504 /* Because these are all string op builtins, they're all nothrow. */
1505 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1506 gcc_assert (!stmt_could_throw_p (icall_stmt));
1509 /* Find values inside STMT for that we want to measure histograms for
1510 division/modulo optimization. */
1511 static bool
1512 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1514 gimple stmt = gsi_stmt (*gsi);
1515 tree fndecl;
1516 tree blck_size;
1517 enum built_in_function fcode;
1518 histogram_value histogram;
1519 gcov_type count, all, val;
1520 tree dest, src;
1521 unsigned int dest_align, src_align;
1522 gcov_type prob;
1523 tree tree_val;
1524 int size_arg;
1526 if (gimple_code (stmt) != GIMPLE_CALL)
1527 return false;
1528 fndecl = gimple_call_fndecl (stmt);
1529 if (!fndecl)
1530 return false;
1531 fcode = DECL_FUNCTION_CODE (fndecl);
1532 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1533 return false;
1535 blck_size = gimple_call_arg (stmt, size_arg);
1536 if (TREE_CODE (blck_size) == INTEGER_CST)
1537 return false;
1539 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1540 if (!histogram)
1541 return false;
1542 val = histogram->hvalue.counters[0];
1543 count = histogram->hvalue.counters[1];
1544 all = histogram->hvalue.counters[2];
1545 gimple_remove_histogram_value (cfun, stmt, histogram);
1546 /* We require that count is at least half of all; this means
1547 that for the transformation to fire the value must be constant
1548 at least 80% of time. */
1549 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1550 return false;
1551 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1552 return false;
1553 if (all > 0)
1554 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
1555 else
1556 prob = 0;
1557 dest = gimple_call_arg (stmt, 0);
1558 dest_align = get_pointer_alignment (dest);
1559 switch (fcode)
1561 case BUILT_IN_MEMCPY:
1562 case BUILT_IN_MEMPCPY:
1563 src = gimple_call_arg (stmt, 1);
1564 src_align = get_pointer_alignment (src);
1565 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1566 return false;
1567 break;
1568 case BUILT_IN_MEMSET:
1569 if (!can_store_by_pieces (val, builtin_memset_read_str,
1570 gimple_call_arg (stmt, 1),
1571 dest_align, true))
1572 return false;
1573 break;
1574 case BUILT_IN_BZERO:
1575 if (!can_store_by_pieces (val, builtin_memset_read_str,
1576 integer_zero_node,
1577 dest_align, true))
1578 return false;
1579 break;
1580 default:
1581 gcc_unreachable ();
1583 tree_val = build_int_cst_wide (get_gcov_type (),
1584 (unsigned HOST_WIDE_INT) val,
1585 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
1586 if (dump_file)
1588 fprintf (dump_file, "Single value %i stringop transformation on ",
1589 (int)val);
1590 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1592 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1594 return true;
1597 void
1598 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1599 HOST_WIDE_INT *expected_size)
1601 histogram_value histogram;
1602 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1603 if (!histogram)
1604 *expected_size = -1;
1605 else if (!histogram->hvalue.counters[1])
1607 *expected_size = -1;
1608 gimple_remove_histogram_value (cfun, stmt, histogram);
1610 else
1612 gcov_type size;
1613 size = ((histogram->hvalue.counters[0]
1614 + histogram->hvalue.counters[1] / 2)
1615 / histogram->hvalue.counters[1]);
1616 /* Even if we can hold bigger value in SIZE, INT_MAX
1617 is safe "infinity" for code generation strategies. */
1618 if (size > INT_MAX)
1619 size = INT_MAX;
1620 *expected_size = size;
1621 gimple_remove_histogram_value (cfun, stmt, histogram);
1623 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1624 if (!histogram)
1625 *expected_align = 0;
1626 else if (!histogram->hvalue.counters[0])
1628 gimple_remove_histogram_value (cfun, stmt, histogram);
1629 *expected_align = 0;
1631 else
1633 gcov_type count;
1634 int alignment;
1636 count = histogram->hvalue.counters[0];
1637 alignment = 1;
1638 while (!(count & alignment)
1639 && (alignment * 2 * BITS_PER_UNIT))
1640 alignment <<= 1;
1641 *expected_align = alignment * BITS_PER_UNIT;
1642 gimple_remove_histogram_value (cfun, stmt, histogram);
1647 /* Find values inside STMT for that we want to measure histograms for
1648 division/modulo optimization. */
1649 static void
1650 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1652 tree lhs, divisor, op0, type;
1653 histogram_value hist;
1655 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1656 return;
1658 lhs = gimple_assign_lhs (stmt);
1659 type = TREE_TYPE (lhs);
1660 if (!INTEGRAL_TYPE_P (type))
1661 return;
1663 switch (gimple_assign_rhs_code (stmt))
1665 case TRUNC_DIV_EXPR:
1666 case TRUNC_MOD_EXPR:
1667 divisor = gimple_assign_rhs2 (stmt);
1668 op0 = gimple_assign_rhs1 (stmt);
1670 VEC_reserve (histogram_value, heap, *values, 3);
1672 if (TREE_CODE (divisor) == SSA_NAME)
1673 /* Check for the case where the divisor is the same value most
1674 of the time. */
1675 VEC_quick_push (histogram_value, *values,
1676 gimple_alloc_histogram_value (cfun,
1677 HIST_TYPE_SINGLE_VALUE,
1678 stmt, divisor));
1680 /* For mod, check whether it is not often a noop (or replaceable by
1681 a few subtractions). */
1682 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1683 && TYPE_UNSIGNED (type))
1685 tree val;
1686 /* Check for a special case where the divisor is power of 2. */
1687 VEC_quick_push (histogram_value, *values,
1688 gimple_alloc_histogram_value (cfun, HIST_TYPE_POW2,
1689 stmt, divisor));
1691 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1692 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1693 stmt, val);
1694 hist->hdata.intvl.int_start = 0;
1695 hist->hdata.intvl.steps = 2;
1696 VEC_quick_push (histogram_value, *values, hist);
1698 return;
1700 default:
1701 return;
1705 /* Find calls inside STMT for that we want to measure histograms for
1706 indirect/virtual call optimization. */
1708 static void
1709 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1711 tree callee;
1713 if (gimple_code (stmt) != GIMPLE_CALL
1714 || gimple_call_internal_p (stmt)
1715 || gimple_call_fndecl (stmt) != NULL_TREE)
1716 return;
1718 callee = gimple_call_fn (stmt);
1720 VEC_reserve (histogram_value, heap, *values, 3);
1722 VEC_quick_push (histogram_value, *values,
1723 gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1724 stmt, callee));
1726 return;
1729 /* Find values inside STMT for that we want to measure histograms for
1730 string operations. */
1731 static void
1732 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1734 tree fndecl;
1735 tree blck_size;
1736 tree dest;
1737 int size_arg;
1739 if (gimple_code (stmt) != GIMPLE_CALL)
1740 return;
1741 fndecl = gimple_call_fndecl (stmt);
1742 if (!fndecl)
1743 return;
1745 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1746 return;
1748 dest = gimple_call_arg (stmt, 0);
1749 blck_size = gimple_call_arg (stmt, size_arg);
1751 if (TREE_CODE (blck_size) != INTEGER_CST)
1753 VEC_safe_push (histogram_value, heap, *values,
1754 gimple_alloc_histogram_value (cfun, HIST_TYPE_SINGLE_VALUE,
1755 stmt, blck_size));
1756 VEC_safe_push (histogram_value, heap, *values,
1757 gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1758 stmt, blck_size));
1760 if (TREE_CODE (blck_size) != INTEGER_CST)
1761 VEC_safe_push (histogram_value, heap, *values,
1762 gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1763 stmt, dest));
1766 /* Find values inside STMT for that we want to measure histograms and adds
1767 them to list VALUES. */
1769 static void
1770 gimple_values_to_profile (gimple stmt, histogram_values *values)
1772 gimple_divmod_values_to_profile (stmt, values);
1773 gimple_stringops_values_to_profile (stmt, values);
1774 gimple_indirect_call_to_profile (stmt, values);
1777 void
1778 gimple_find_values_to_profile (histogram_values *values)
1780 basic_block bb;
1781 gimple_stmt_iterator gsi;
1782 unsigned i;
1783 histogram_value hist = NULL;
1785 *values = NULL;
1786 FOR_EACH_BB (bb)
1787 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1788 gimple_values_to_profile (gsi_stmt (gsi), values);
1790 FOR_EACH_VEC_ELT (histogram_value, *values, i, hist)
1792 switch (hist->type)
1794 case HIST_TYPE_INTERVAL:
1795 hist->n_counters = hist->hdata.intvl.steps + 2;
1796 break;
1798 case HIST_TYPE_POW2:
1799 hist->n_counters = 2;
1800 break;
1802 case HIST_TYPE_SINGLE_VALUE:
1803 hist->n_counters = 3;
1804 break;
1806 case HIST_TYPE_CONST_DELTA:
1807 hist->n_counters = 4;
1808 break;
1810 case HIST_TYPE_INDIR_CALL:
1811 hist->n_counters = 3;
1812 break;
1814 case HIST_TYPE_AVERAGE:
1815 hist->n_counters = 2;
1816 break;
1818 case HIST_TYPE_IOR:
1819 hist->n_counters = 1;
1820 break;
1822 default:
1823 gcc_unreachable ();
1825 if (dump_file)
1827 fprintf (dump_file, "Stmt ");
1828 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
1829 dump_histogram_value (dump_file, hist);