* cp-tree.h (struct deferred_access_check): Add location.
[official-gcc.git] / gcc / value-prof.c
blob29c3e92b28928c8f926311dd63ae5beed453743f
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.
56 2) Speculative prefetching. If we are able to determine that the difference
57 between addresses accessed by a memory reference is usually constant, we
58 may add the prefetch instructions.
59 FIXME: This transformation was removed together with RTL based value
60 profiling.
62 3) Indirect/virtual call specialization. If we can determine most
63 common function callee in indirect/virtual call. We can use this
64 information to improve code effectiveness (especially info for
65 inliner).
67 Every such optimization should add its requirements for profiled values to
68 insn_values_to_profile function. This function is called from branch_prob
69 in profile.c and the requested values are instrumented by it in the first
70 compilation with -fprofile-arcs. The optimization may then read the
71 gathered data in the second compilation with -fbranch-probabilities.
73 The measured data is pointed to from the histograms
74 field of the statement annotation of the instrumented insns. It is
75 kept as a linked list of struct histogram_value_t's, which contain the
76 same information as above. */
79 static tree gimple_divmod_fixed_value (gimple, tree, int, gcov_type, gcov_type);
80 static tree gimple_mod_pow2 (gimple, int, gcov_type, gcov_type);
81 static tree gimple_mod_subtract (gimple, int, int, int, gcov_type, gcov_type,
82 gcov_type);
83 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
84 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
85 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
86 static bool gimple_stringops_transform (gimple_stmt_iterator *);
87 static bool gimple_ic_transform (gimple);
89 /* Allocate histogram value. */
91 static histogram_value
92 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
93 enum hist_type type, gimple stmt, tree value)
95 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
96 hist->hvalue.value = value;
97 hist->hvalue.stmt = stmt;
98 hist->type = type;
99 return hist;
102 /* Hash value for histogram. */
104 static hashval_t
105 histogram_hash (const void *x)
107 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
110 /* Return nonzero if statement for histogram_value X is Y. */
112 static int
113 histogram_eq (const void *x, const void *y)
115 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
118 /* Set histogram for STMT. */
120 static void
121 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
123 void **loc;
124 if (!hist && !VALUE_HISTOGRAMS (fun))
125 return;
126 if (!VALUE_HISTOGRAMS (fun))
127 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
128 histogram_eq, NULL);
129 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
130 htab_hash_pointer (stmt),
131 hist ? INSERT : NO_INSERT);
132 if (!hist)
134 if (loc)
135 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
136 return;
138 *loc = hist;
141 /* Get histogram list for STMT. */
143 histogram_value
144 gimple_histogram_value (struct function *fun, gimple stmt)
146 if (!VALUE_HISTOGRAMS (fun))
147 return NULL;
148 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
149 htab_hash_pointer (stmt));
152 /* Add histogram for STMT. */
154 void
155 gimple_add_histogram_value (struct function *fun, gimple stmt,
156 histogram_value hist)
158 hist->hvalue.next = gimple_histogram_value (fun, stmt);
159 set_histogram_value (fun, stmt, hist);
163 /* Remove histogram HIST from STMT's histogram list. */
165 void
166 gimple_remove_histogram_value (struct function *fun, gimple stmt,
167 histogram_value hist)
169 histogram_value hist2 = gimple_histogram_value (fun, stmt);
170 if (hist == hist2)
172 set_histogram_value (fun, stmt, hist->hvalue.next);
174 else
176 while (hist2->hvalue.next != hist)
177 hist2 = hist2->hvalue.next;
178 hist2->hvalue.next = hist->hvalue.next;
180 free (hist->hvalue.counters);
181 #ifdef ENABLE_CHECKING
182 memset (hist, 0xab, sizeof (*hist));
183 #endif
184 free (hist);
188 /* Lookup histogram of type TYPE in the STMT. */
190 histogram_value
191 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
192 enum hist_type type)
194 histogram_value hist;
195 for (hist = gimple_histogram_value (fun, stmt); hist;
196 hist = hist->hvalue.next)
197 if (hist->type == type)
198 return hist;
199 return NULL;
202 /* Dump information about HIST to DUMP_FILE. */
204 static void
205 dump_histogram_value (FILE *dump_file, histogram_value hist)
207 switch (hist->type)
209 case HIST_TYPE_INTERVAL:
210 fprintf (dump_file, "Interval counter range %d -- %d",
211 hist->hdata.intvl.int_start,
212 (hist->hdata.intvl.int_start
213 + hist->hdata.intvl.steps - 1));
214 if (hist->hvalue.counters)
216 unsigned int i;
217 fprintf(dump_file, " [");
218 for (i = 0; i < hist->hdata.intvl.steps; i++)
219 fprintf (dump_file, " %d:"HOST_WIDEST_INT_PRINT_DEC,
220 hist->hdata.intvl.int_start + i,
221 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
222 fprintf (dump_file, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC,
223 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
225 fprintf (dump_file, ".\n");
226 break;
228 case HIST_TYPE_POW2:
229 fprintf (dump_file, "Pow2 counter ");
230 if (hist->hvalue.counters)
232 fprintf (dump_file, "pow2:"HOST_WIDEST_INT_PRINT_DEC
233 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC,
234 (HOST_WIDEST_INT) hist->hvalue.counters[0],
235 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
237 fprintf (dump_file, ".\n");
238 break;
240 case HIST_TYPE_SINGLE_VALUE:
241 fprintf (dump_file, "Single value ");
242 if (hist->hvalue.counters)
244 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
245 " match:"HOST_WIDEST_INT_PRINT_DEC
246 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
247 (HOST_WIDEST_INT) hist->hvalue.counters[0],
248 (HOST_WIDEST_INT) hist->hvalue.counters[1],
249 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
251 fprintf (dump_file, ".\n");
252 break;
254 case HIST_TYPE_AVERAGE:
255 fprintf (dump_file, "Average value ");
256 if (hist->hvalue.counters)
258 fprintf (dump_file, "sum:"HOST_WIDEST_INT_PRINT_DEC
259 " times:"HOST_WIDEST_INT_PRINT_DEC,
260 (HOST_WIDEST_INT) hist->hvalue.counters[0],
261 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
263 fprintf (dump_file, ".\n");
264 break;
266 case HIST_TYPE_IOR:
267 fprintf (dump_file, "IOR value ");
268 if (hist->hvalue.counters)
270 fprintf (dump_file, "ior:"HOST_WIDEST_INT_PRINT_DEC,
271 (HOST_WIDEST_INT) hist->hvalue.counters[0]);
273 fprintf (dump_file, ".\n");
274 break;
276 case HIST_TYPE_CONST_DELTA:
277 fprintf (dump_file, "Constant delta ");
278 if (hist->hvalue.counters)
280 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
281 " match:"HOST_WIDEST_INT_PRINT_DEC
282 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
283 (HOST_WIDEST_INT) hist->hvalue.counters[0],
284 (HOST_WIDEST_INT) hist->hvalue.counters[1],
285 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
287 fprintf (dump_file, ".\n");
288 break;
289 case HIST_TYPE_INDIR_CALL:
290 fprintf (dump_file, "Indirect call ");
291 if (hist->hvalue.counters)
293 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
294 " match:"HOST_WIDEST_INT_PRINT_DEC
295 " all:"HOST_WIDEST_INT_PRINT_DEC,
296 (HOST_WIDEST_INT) hist->hvalue.counters[0],
297 (HOST_WIDEST_INT) hist->hvalue.counters[1],
298 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
300 fprintf (dump_file, ".\n");
301 break;
305 /* Dump all histograms attached to STMT to DUMP_FILE. */
307 void
308 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
310 histogram_value hist;
311 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
312 dump_histogram_value (dump_file, hist);
315 /* Remove all histograms associated with STMT. */
317 void
318 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
320 histogram_value val;
321 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
322 gimple_remove_histogram_value (fun, stmt, val);
325 /* Duplicate all histograms associates with OSTMT to STMT. */
327 void
328 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
329 struct function *ofun, gimple ostmt)
331 histogram_value val;
332 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
334 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
335 memcpy (new_val, val, sizeof (*val));
336 new_val->hvalue.stmt = stmt;
337 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
338 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
339 gimple_add_histogram_value (fun, stmt, new_val);
344 /* Move all histograms associated with OSTMT to STMT. */
346 void
347 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
349 histogram_value val = gimple_histogram_value (fun, ostmt);
350 if (val)
352 /* The following three statements can't be reordered,
353 because histogram hashtab relies on stmt field value
354 for finding the exact slot. */
355 set_histogram_value (fun, ostmt, NULL);
356 for (; val != NULL; val = val->hvalue.next)
357 val->hvalue.stmt = stmt;
358 set_histogram_value (fun, stmt, val);
362 static bool error_found = false;
364 /* Helper function for verify_histograms. For each histogram reachable via htab
365 walk verify that it was reached via statement walk. */
367 static int
368 visit_hist (void **slot, void *data)
370 struct pointer_set_t *visited = (struct pointer_set_t *) data;
371 histogram_value hist = *(histogram_value *) slot;
372 if (!pointer_set_contains (visited, hist))
374 error ("dead histogram");
375 dump_histogram_value (stderr, hist);
376 debug_gimple_stmt (hist->hvalue.stmt);
377 error_found = true;
379 return 1;
383 /* Verify sanity of the histograms. */
385 DEBUG_FUNCTION void
386 verify_histograms (void)
388 basic_block bb;
389 gimple_stmt_iterator gsi;
390 histogram_value hist;
391 struct pointer_set_t *visited_hists;
393 error_found = false;
394 visited_hists = pointer_set_create ();
395 FOR_EACH_BB (bb)
396 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
398 gimple stmt = gsi_stmt (gsi);
400 for (hist = gimple_histogram_value (cfun, stmt); hist;
401 hist = hist->hvalue.next)
403 if (hist->hvalue.stmt != stmt)
405 error ("Histogram value statement does not correspond to "
406 "the statement it is associated with");
407 debug_gimple_stmt (stmt);
408 dump_histogram_value (stderr, hist);
409 error_found = true;
411 pointer_set_insert (visited_hists, hist);
414 if (VALUE_HISTOGRAMS (cfun))
415 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, visited_hists);
416 pointer_set_destroy (visited_hists);
417 if (error_found)
418 internal_error ("verify_histograms failed");
421 /* Helper function for verify_histograms. For each histogram reachable via htab
422 walk verify that it was reached via statement walk. */
424 static int
425 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
427 histogram_value hist = *(histogram_value *) slot;
428 free (hist->hvalue.counters);
429 #ifdef ENABLE_CHECKING
430 memset (hist, 0xab, sizeof (*hist));
431 #endif
432 free (hist);
433 return 1;
436 void
437 free_histograms (void)
439 if (VALUE_HISTOGRAMS (cfun))
441 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
442 htab_delete (VALUE_HISTOGRAMS (cfun));
443 VALUE_HISTOGRAMS (cfun) = NULL;
448 /* The overall number of invocations of the counter should match
449 execution count of basic block. Report it as error rather than
450 internal error as it might mean that user has misused the profile
451 somehow. */
453 static bool
454 check_counter (gimple stmt, const char * name,
455 gcov_type *count, gcov_type *all, gcov_type bb_count)
457 if (*all != bb_count || *count > *all)
459 location_t locus;
460 locus = (stmt != NULL)
461 ? gimple_location (stmt)
462 : DECL_SOURCE_LOCATION (current_function_decl);
463 if (flag_profile_correction)
465 inform (locus, "correcting inconsistent value profile: "
466 "%s profiler overall count (%d) does not match BB count "
467 "(%d)", name, (int)*all, (int)bb_count);
468 *all = bb_count;
469 if (*count > *all)
470 *count = *all;
471 return false;
473 else
475 error_at (locus, "corrupted value profile: %s "
476 "profile counter (%d out of %d) inconsistent with "
477 "basic-block count (%d)",
478 name,
479 (int) *count,
480 (int) *all,
481 (int) bb_count);
482 return true;
486 return false;
490 /* GIMPLE based transformations. */
492 bool
493 gimple_value_profile_transformations (void)
495 basic_block bb;
496 gimple_stmt_iterator gsi;
497 bool changed = false;
499 FOR_EACH_BB (bb)
501 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
503 gimple stmt = gsi_stmt (gsi);
504 histogram_value th = gimple_histogram_value (cfun, stmt);
505 if (!th)
506 continue;
508 if (dump_file)
510 fprintf (dump_file, "Trying transformations on stmt ");
511 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
512 dump_histograms_for_stmt (cfun, dump_file, stmt);
515 /* Transformations: */
516 /* The order of things in this conditional controls which
517 transformation is used when more than one is applicable. */
518 /* It is expected that any code added by the transformations
519 will be added before the current statement, and that the
520 current statement remain valid (although possibly
521 modified) upon return. */
522 if (flag_value_profile_transformations
523 && (gimple_mod_subtract_transform (&gsi)
524 || gimple_divmod_fixed_value_transform (&gsi)
525 || gimple_mod_pow2_value_transform (&gsi)
526 || gimple_stringops_transform (&gsi)
527 || gimple_ic_transform (stmt)))
529 stmt = gsi_stmt (gsi);
530 changed = true;
531 /* Original statement may no longer be in the same block. */
532 if (bb != gimple_bb (stmt))
534 bb = gimple_bb (stmt);
535 gsi = gsi_for_stmt (stmt);
541 if (changed)
543 counts_to_freqs ();
546 return changed;
550 /* Generate code for transformation 1 (with parent gimple assignment
551 STMT and probability of taking the optimal path PROB, which is
552 equivalent to COUNT/ALL within roundoff error). This generates the
553 result into a temp and returns the temp; it does not replace or
554 alter the original STMT. */
556 static tree
557 gimple_divmod_fixed_value (gimple stmt, tree value, int prob, gcov_type count,
558 gcov_type all)
560 gimple stmt1, stmt2, stmt3;
561 tree tmp0, tmp1, tmp2, tmpv;
562 gimple bb1end, bb2end, bb3end;
563 basic_block bb, bb2, bb3, bb4;
564 tree optype, op1, op2;
565 edge e12, e13, e23, e24, e34;
566 gimple_stmt_iterator gsi;
568 gcc_assert (is_gimple_assign (stmt)
569 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
570 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
572 optype = TREE_TYPE (gimple_assign_lhs (stmt));
573 op1 = gimple_assign_rhs1 (stmt);
574 op2 = gimple_assign_rhs2 (stmt);
576 bb = gimple_bb (stmt);
577 gsi = gsi_for_stmt (stmt);
579 tmpv = create_tmp_reg (optype, "PROF");
580 tmp0 = make_ssa_name (tmpv, NULL);
581 tmp1 = make_ssa_name (tmpv, NULL);
582 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
583 SSA_NAME_DEF_STMT (tmp0) = stmt1;
584 stmt2 = gimple_build_assign (tmp1, op2);
585 SSA_NAME_DEF_STMT (tmp1) = stmt2;
586 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
587 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
588 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
589 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
590 bb1end = stmt3;
592 tmp2 = make_rename_temp (optype, "PROF");
593 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
594 op1, tmp0);
595 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
596 bb2end = stmt1;
598 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
599 op1, op2);
600 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
601 bb3end = stmt1;
603 /* Fix CFG. */
604 /* Edge e23 connects bb2 to bb3, etc. */
605 e12 = split_block (bb, bb1end);
606 bb2 = e12->dest;
607 bb2->count = count;
608 e23 = split_block (bb2, bb2end);
609 bb3 = e23->dest;
610 bb3->count = all - count;
611 e34 = split_block (bb3, bb3end);
612 bb4 = e34->dest;
613 bb4->count = all;
615 e12->flags &= ~EDGE_FALLTHRU;
616 e12->flags |= EDGE_FALSE_VALUE;
617 e12->probability = prob;
618 e12->count = count;
620 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
621 e13->probability = REG_BR_PROB_BASE - prob;
622 e13->count = all - count;
624 remove_edge (e23);
626 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
627 e24->probability = REG_BR_PROB_BASE;
628 e24->count = count;
630 e34->probability = REG_BR_PROB_BASE;
631 e34->count = all - count;
633 return tmp2;
637 /* Do transform 1) on INSN if applicable. */
639 static bool
640 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
642 histogram_value histogram;
643 enum tree_code code;
644 gcov_type val, count, all;
645 tree result, value, tree_val;
646 gcov_type prob;
647 gimple stmt;
649 stmt = gsi_stmt (*si);
650 if (gimple_code (stmt) != GIMPLE_ASSIGN)
651 return false;
653 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
654 return false;
656 code = gimple_assign_rhs_code (stmt);
658 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
659 return false;
661 histogram = gimple_histogram_value_of_type (cfun, stmt,
662 HIST_TYPE_SINGLE_VALUE);
663 if (!histogram)
664 return false;
666 value = histogram->hvalue.value;
667 val = histogram->hvalue.counters[0];
668 count = histogram->hvalue.counters[1];
669 all = histogram->hvalue.counters[2];
670 gimple_remove_histogram_value (cfun, stmt, histogram);
672 /* We require that count is at least half of all; this means
673 that for the transformation to fire the value must be constant
674 at least 50% of time (and 75% gives the guarantee of usage). */
675 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
676 || 2 * count < all
677 || optimize_bb_for_size_p (gimple_bb (stmt)))
678 return false;
680 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
681 return false;
683 /* Compute probability of taking the optimal path. */
684 if (all > 0)
685 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
686 else
687 prob = 0;
689 tree_val = build_int_cst_wide (get_gcov_type (),
690 (unsigned HOST_WIDE_INT) val,
691 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
692 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
694 if (dump_file)
696 fprintf (dump_file, "Div/mod by constant ");
697 print_generic_expr (dump_file, value, TDF_SLIM);
698 fprintf (dump_file, "=");
699 print_generic_expr (dump_file, tree_val, TDF_SLIM);
700 fprintf (dump_file, " transformation on insn ");
701 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
704 gimple_assign_set_rhs_from_tree (si, result);
705 update_stmt (gsi_stmt (*si));
707 return true;
710 /* Generate code for transformation 2 (with parent gimple assign STMT and
711 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
712 within roundoff error). This generates the result into a temp and returns
713 the temp; it does not replace or alter the original STMT. */
714 static tree
715 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
717 gimple stmt1, stmt2, stmt3, stmt4;
718 tree tmp2, tmp3, tmpv;
719 gimple bb1end, bb2end, bb3end;
720 basic_block bb, bb2, bb3, bb4;
721 tree optype, op1, op2;
722 edge e12, e13, e23, e24, e34;
723 gimple_stmt_iterator gsi;
724 tree result;
726 gcc_assert (is_gimple_assign (stmt)
727 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
729 optype = TREE_TYPE (gimple_assign_lhs (stmt));
730 op1 = gimple_assign_rhs1 (stmt);
731 op2 = gimple_assign_rhs2 (stmt);
733 bb = gimple_bb (stmt);
734 gsi = gsi_for_stmt (stmt);
736 result = make_rename_temp (optype, "PROF");
737 tmpv = create_tmp_var (optype, "PROF");
738 tmp2 = make_ssa_name (tmpv, NULL);
739 tmp3 = make_ssa_name (tmpv, NULL);
740 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
741 build_int_cst (optype, -1));
742 SSA_NAME_DEF_STMT (tmp2) = stmt2;
743 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
744 SSA_NAME_DEF_STMT (tmp3) = stmt3;
745 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
746 NULL_TREE, NULL_TREE);
747 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
748 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
749 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
750 bb1end = stmt4;
752 /* tmp2 == op2-1 inherited from previous block. */
753 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
754 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
755 bb2end = stmt1;
757 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
758 op1, op2);
759 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
760 bb3end = stmt1;
762 /* Fix CFG. */
763 /* Edge e23 connects bb2 to bb3, etc. */
764 e12 = split_block (bb, bb1end);
765 bb2 = e12->dest;
766 bb2->count = count;
767 e23 = split_block (bb2, bb2end);
768 bb3 = e23->dest;
769 bb3->count = all - count;
770 e34 = split_block (bb3, bb3end);
771 bb4 = e34->dest;
772 bb4->count = all;
774 e12->flags &= ~EDGE_FALLTHRU;
775 e12->flags |= EDGE_FALSE_VALUE;
776 e12->probability = prob;
777 e12->count = count;
779 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
780 e13->probability = REG_BR_PROB_BASE - prob;
781 e13->count = all - count;
783 remove_edge (e23);
785 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
786 e24->probability = REG_BR_PROB_BASE;
787 e24->count = count;
789 e34->probability = REG_BR_PROB_BASE;
790 e34->count = all - count;
792 return result;
795 /* Do transform 2) on INSN if applicable. */
796 static bool
797 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
799 histogram_value histogram;
800 enum tree_code code;
801 gcov_type count, wrong_values, all;
802 tree lhs_type, result, value;
803 gcov_type prob;
804 gimple stmt;
806 stmt = gsi_stmt (*si);
807 if (gimple_code (stmt) != GIMPLE_ASSIGN)
808 return false;
810 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
811 if (!INTEGRAL_TYPE_P (lhs_type))
812 return false;
814 code = gimple_assign_rhs_code (stmt);
816 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
817 return false;
819 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
820 if (!histogram)
821 return false;
823 value = histogram->hvalue.value;
824 wrong_values = histogram->hvalue.counters[0];
825 count = histogram->hvalue.counters[1];
827 gimple_remove_histogram_value (cfun, stmt, histogram);
829 /* We require that we hit a power of 2 at least half of all evaluations. */
830 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
831 || count < wrong_values
832 || optimize_bb_for_size_p (gimple_bb (stmt)))
833 return false;
835 if (dump_file)
837 fprintf (dump_file, "Mod power of 2 transformation on insn ");
838 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
841 /* Compute probability of taking the optimal path. */
842 all = count + wrong_values;
844 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
845 return false;
847 if (all > 0)
848 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
849 else
850 prob = 0;
852 result = gimple_mod_pow2 (stmt, prob, count, all);
854 gimple_assign_set_rhs_from_tree (si, result);
855 update_stmt (gsi_stmt (*si));
857 return true;
860 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
861 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
862 supported and this is built into this interface. The probabilities of taking
863 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
864 COUNT2/ALL respectively within roundoff error). This generates the
865 result into a temp and returns the temp; it does not replace or alter
866 the original STMT. */
867 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
869 static tree
870 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
871 gcov_type count1, gcov_type count2, gcov_type all)
873 gimple stmt1, stmt2, stmt3;
874 tree tmp1;
875 gimple bb1end, bb2end = NULL, bb3end;
876 basic_block bb, bb2, bb3, bb4;
877 tree optype, op1, op2;
878 edge e12, e23 = 0, e24, e34, e14;
879 gimple_stmt_iterator gsi;
880 tree result;
882 gcc_assert (is_gimple_assign (stmt)
883 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
885 optype = TREE_TYPE (gimple_assign_lhs (stmt));
886 op1 = gimple_assign_rhs1 (stmt);
887 op2 = gimple_assign_rhs2 (stmt);
889 bb = gimple_bb (stmt);
890 gsi = gsi_for_stmt (stmt);
892 result = make_rename_temp (optype, "PROF");
893 tmp1 = make_ssa_name (create_tmp_var (optype, "PROF"), NULL);
894 stmt1 = gimple_build_assign (result, op1);
895 stmt2 = gimple_build_assign (tmp1, op2);
896 SSA_NAME_DEF_STMT (tmp1) = stmt2;
897 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
898 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
899 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
900 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
901 bb1end = stmt3;
903 if (ncounts) /* Assumed to be 0 or 1 */
905 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
906 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
907 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
908 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
909 bb2end = stmt2;
912 /* Fallback case. */
913 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
914 result, tmp1);
915 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
916 bb3end = stmt1;
918 /* Fix CFG. */
919 /* Edge e23 connects bb2 to bb3, etc. */
920 /* However block 3 is optional; if it is not there, references
921 to 3 really refer to block 2. */
922 e12 = split_block (bb, bb1end);
923 bb2 = e12->dest;
924 bb2->count = all - count1;
926 if (ncounts) /* Assumed to be 0 or 1. */
928 e23 = split_block (bb2, bb2end);
929 bb3 = e23->dest;
930 bb3->count = all - count1 - count2;
933 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
934 bb4 = e34->dest;
935 bb4->count = all;
937 e12->flags &= ~EDGE_FALLTHRU;
938 e12->flags |= EDGE_FALSE_VALUE;
939 e12->probability = REG_BR_PROB_BASE - prob1;
940 e12->count = all - count1;
942 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
943 e14->probability = prob1;
944 e14->count = count1;
946 if (ncounts) /* Assumed to be 0 or 1. */
948 e23->flags &= ~EDGE_FALLTHRU;
949 e23->flags |= EDGE_FALSE_VALUE;
950 e23->count = all - count1 - count2;
951 e23->probability = REG_BR_PROB_BASE - prob2;
953 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
954 e24->probability = prob2;
955 e24->count = count2;
958 e34->probability = REG_BR_PROB_BASE;
959 e34->count = all - count1 - count2;
961 return result;
965 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
967 static bool
968 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
970 histogram_value histogram;
971 enum tree_code code;
972 gcov_type count, wrong_values, all;
973 tree lhs_type, result;
974 gcov_type prob1, prob2;
975 unsigned int i, steps;
976 gcov_type count1, count2;
977 gimple stmt;
979 stmt = gsi_stmt (*si);
980 if (gimple_code (stmt) != GIMPLE_ASSIGN)
981 return false;
983 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
984 if (!INTEGRAL_TYPE_P (lhs_type))
985 return false;
987 code = gimple_assign_rhs_code (stmt);
989 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
990 return false;
992 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
993 if (!histogram)
994 return false;
996 all = 0;
997 wrong_values = 0;
998 for (i = 0; i < histogram->hdata.intvl.steps; i++)
999 all += histogram->hvalue.counters[i];
1001 wrong_values += histogram->hvalue.counters[i];
1002 wrong_values += histogram->hvalue.counters[i+1];
1003 steps = histogram->hdata.intvl.steps;
1004 all += wrong_values;
1005 count1 = histogram->hvalue.counters[0];
1006 count2 = histogram->hvalue.counters[1];
1008 /* Compute probability of taking the optimal path. */
1009 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1011 gimple_remove_histogram_value (cfun, stmt, histogram);
1012 return false;
1015 if (flag_profile_correction && count1 + count2 > all)
1016 all = count1 + count2;
1018 gcc_assert (count1 + count2 <= all);
1020 /* We require that we use just subtractions in at least 50% of all
1021 evaluations. */
1022 count = 0;
1023 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1025 count += histogram->hvalue.counters[i];
1026 if (count * 2 >= all)
1027 break;
1029 if (i == steps
1030 || optimize_bb_for_size_p (gimple_bb (stmt)))
1031 return false;
1033 gimple_remove_histogram_value (cfun, stmt, histogram);
1034 if (dump_file)
1036 fprintf (dump_file, "Mod subtract transformation on insn ");
1037 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1040 /* Compute probability of taking the optimal path(s). */
1041 if (all > 0)
1043 prob1 = (count1 * REG_BR_PROB_BASE + all / 2) / all;
1044 prob2 = (count2 * REG_BR_PROB_BASE + all / 2) / all;
1046 else
1048 prob1 = prob2 = 0;
1051 /* In practice, "steps" is always 2. This interface reflects this,
1052 and will need to be changed if "steps" can change. */
1053 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1055 gimple_assign_set_rhs_from_tree (si, result);
1056 update_stmt (gsi_stmt (*si));
1058 return true;
1061 static VEC(cgraph_node_ptr, heap) *cgraph_node_map = NULL;
1063 /* Initialize map from FUNCDEF_NO to CGRAPH_NODE. */
1065 void
1066 init_node_map (void)
1068 struct cgraph_node *n;
1070 if (get_last_funcdef_no ())
1071 VEC_safe_grow_cleared (cgraph_node_ptr, heap,
1072 cgraph_node_map, get_last_funcdef_no ());
1074 FOR_EACH_FUNCTION (n)
1076 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
1077 VEC_replace (cgraph_node_ptr, cgraph_node_map,
1078 DECL_STRUCT_FUNCTION (n->symbol.decl)->funcdef_no, n);
1082 /* Delete the CGRAPH_NODE_MAP. */
1084 void
1085 del_node_map (void)
1087 VEC_free (cgraph_node_ptr, heap, cgraph_node_map);
1088 cgraph_node_map = NULL;
1091 /* Return cgraph node for function with pid */
1093 static inline struct cgraph_node*
1094 find_func_by_funcdef_no (int func_id)
1096 int max_id = get_last_funcdef_no ();
1097 if (func_id >= max_id || VEC_index (cgraph_node_ptr,
1098 cgraph_node_map,
1099 func_id) == NULL)
1101 if (flag_profile_correction)
1102 inform (DECL_SOURCE_LOCATION (current_function_decl),
1103 "Inconsistent profile: indirect call target (%d) does not exist", func_id);
1104 else
1105 error ("Inconsistent profile: indirect call target (%d) does not exist", func_id);
1107 return NULL;
1110 return VEC_index (cgraph_node_ptr, cgraph_node_map, func_id);
1113 /* Perform sanity check on the indirect call target. Due to race conditions,
1114 false function target may be attributed to an indirect call site. If the
1115 call expression type mismatches with the target function's type, expand_call
1116 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1117 Returns true if TARGET is considered ok for call CALL_STMT. */
1119 static bool
1120 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1122 location_t locus;
1123 if (gimple_check_call_matching_types (call_stmt, target->symbol.decl))
1124 return true;
1126 locus = gimple_location (call_stmt);
1127 inform (locus, "Skipping target %s with mismatching types for icall ",
1128 cgraph_node_name (target));
1129 return false;
1132 /* Do transformation
1134 if (actual_callee_address == address_of_most_common_function/method)
1135 do direct call
1136 else
1137 old call
1140 static gimple
1141 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1142 int prob, gcov_type count, gcov_type all)
1144 gimple dcall_stmt, load_stmt, cond_stmt;
1145 tree tmp0, tmp1, tmpv, tmp;
1146 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1147 tree optype = build_pointer_type (void_type_node);
1148 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1149 gimple_stmt_iterator gsi;
1150 int lp_nr, dflags;
1152 cond_bb = gimple_bb (icall_stmt);
1153 gsi = gsi_for_stmt (icall_stmt);
1155 tmpv = create_tmp_reg (optype, "PROF");
1156 tmp0 = make_ssa_name (tmpv, NULL);
1157 tmp1 = make_ssa_name (tmpv, NULL);
1158 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1159 load_stmt = gimple_build_assign (tmp0, tmp);
1160 SSA_NAME_DEF_STMT (tmp0) = load_stmt;
1161 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1163 tmp = fold_convert (optype, build_addr (direct_call->symbol.decl,
1164 current_function_decl));
1165 load_stmt = gimple_build_assign (tmp1, tmp);
1166 SSA_NAME_DEF_STMT (tmp1) = load_stmt;
1167 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1169 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1170 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1172 gimple_set_vdef (icall_stmt, NULL_TREE);
1173 gimple_set_vuse (icall_stmt, NULL_TREE);
1174 update_stmt (icall_stmt);
1175 dcall_stmt = gimple_copy (icall_stmt);
1176 gimple_call_set_fndecl (dcall_stmt, direct_call->symbol.decl);
1177 dflags = flags_from_decl_or_type (direct_call->symbol.decl);
1178 if ((dflags & ECF_NORETURN) != 0)
1179 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1180 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1182 /* Fix CFG. */
1183 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1184 e_cd = split_block (cond_bb, cond_stmt);
1185 dcall_bb = e_cd->dest;
1186 dcall_bb->count = count;
1188 e_di = split_block (dcall_bb, dcall_stmt);
1189 icall_bb = e_di->dest;
1190 icall_bb->count = all - count;
1192 /* Do not disturb existing EH edges from the indirect call. */
1193 if (!stmt_ends_bb_p (icall_stmt))
1194 e_ij = split_block (icall_bb, icall_stmt);
1195 else
1197 e_ij = find_fallthru_edge (icall_bb->succs);
1198 /* The indirect call might be noreturn. */
1199 if (e_ij != NULL)
1201 e_ij->probability = REG_BR_PROB_BASE;
1202 e_ij->count = all - count;
1203 e_ij = single_pred_edge (split_edge (e_ij));
1206 if (e_ij != NULL)
1208 join_bb = e_ij->dest;
1209 join_bb->count = all;
1212 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1213 e_cd->probability = prob;
1214 e_cd->count = count;
1216 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1217 e_ci->probability = REG_BR_PROB_BASE - prob;
1218 e_ci->count = all - count;
1220 remove_edge (e_di);
1222 if (e_ij != NULL)
1224 if ((dflags & ECF_NORETURN) != 0)
1225 e_ij->count = all;
1226 else
1228 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1229 e_dj->probability = REG_BR_PROB_BASE;
1230 e_dj->count = count;
1232 e_ij->count = all - count;
1234 e_ij->probability = REG_BR_PROB_BASE;
1237 /* Insert PHI node for the call result if necessary. */
1238 if (gimple_call_lhs (icall_stmt)
1239 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1240 && (dflags & ECF_NORETURN) == 0)
1242 tree result = gimple_call_lhs (icall_stmt);
1243 gimple phi = create_phi_node (result, join_bb);
1244 SSA_NAME_DEF_STMT (result) = phi;
1245 gimple_call_set_lhs (icall_stmt,
1246 make_ssa_name (SSA_NAME_VAR (result), icall_stmt));
1247 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1248 gimple_call_set_lhs (dcall_stmt,
1249 make_ssa_name (SSA_NAME_VAR (result), dcall_stmt));
1250 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1253 /* Build an EH edge for the direct call if necessary. */
1254 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1255 if (lp_nr != 0
1256 && stmt_could_throw_p (dcall_stmt))
1258 edge e_eh, e;
1259 edge_iterator ei;
1260 gimple_stmt_iterator psi;
1262 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1263 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1264 if (e_eh->flags & EDGE_EH)
1265 break;
1266 e = make_edge (dcall_bb, e_eh->dest, EDGE_EH);
1267 for (psi = gsi_start_phis (e_eh->dest);
1268 !gsi_end_p (psi); gsi_next (&psi))
1270 gimple phi = gsi_stmt (psi);
1271 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1272 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1276 return dcall_stmt;
1280 For every checked indirect/virtual call determine if most common pid of
1281 function/class method has probability more than 50%. If yes modify code of
1282 this call to:
1285 static bool
1286 gimple_ic_transform (gimple stmt)
1288 histogram_value histogram;
1289 gcov_type val, count, all, bb_all;
1290 gcov_type prob;
1291 gimple modify;
1292 struct cgraph_node *direct_call;
1294 if (gimple_code (stmt) != GIMPLE_CALL)
1295 return false;
1297 if (gimple_call_fndecl (stmt) != NULL_TREE)
1298 return false;
1300 if (gimple_call_internal_p (stmt))
1301 return false;
1303 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1304 if (!histogram)
1305 return false;
1307 val = histogram->hvalue.counters [0];
1308 count = histogram->hvalue.counters [1];
1309 all = histogram->hvalue.counters [2];
1310 gimple_remove_histogram_value (cfun, stmt, histogram);
1312 if (4 * count <= 3 * all)
1313 return false;
1315 bb_all = gimple_bb (stmt)->count;
1316 /* The order of CHECK_COUNTER calls is important -
1317 since check_counter can correct the third parameter
1318 and we want to make count <= all <= bb_all. */
1319 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1320 || check_counter (stmt, "ic", &count, &all, all))
1321 return false;
1323 if (all > 0)
1324 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
1325 else
1326 prob = 0;
1327 direct_call = find_func_by_funcdef_no ((int)val);
1329 if (direct_call == NULL)
1330 return false;
1332 if (!check_ic_target (stmt, direct_call))
1333 return false;
1335 modify = gimple_ic (stmt, direct_call, prob, count, all);
1337 if (dump_file)
1339 fprintf (dump_file, "Indirect call -> direct call ");
1340 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1341 fprintf (dump_file, "=> ");
1342 print_generic_expr (dump_file, direct_call->symbol.decl, TDF_SLIM);
1343 fprintf (dump_file, " transformation on insn ");
1344 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1345 fprintf (dump_file, " to ");
1346 print_gimple_stmt (dump_file, modify, 0, TDF_SLIM);
1347 fprintf (dump_file, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1348 " hist->all "HOST_WIDEST_INT_PRINT_DEC"\n", count, all);
1351 return true;
1354 /* Return true if the stringop CALL with FNDECL shall be profiled.
1355 SIZE_ARG be set to the argument index for the size of the string
1356 operation.
1358 static bool
1359 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1361 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1363 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1364 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1365 return false;
1367 switch (fcode)
1369 case BUILT_IN_MEMCPY:
1370 case BUILT_IN_MEMPCPY:
1371 *size_arg = 2;
1372 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1373 INTEGER_TYPE, VOID_TYPE);
1374 case BUILT_IN_MEMSET:
1375 *size_arg = 2;
1376 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1377 INTEGER_TYPE, VOID_TYPE);
1378 case BUILT_IN_BZERO:
1379 *size_arg = 1;
1380 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1381 VOID_TYPE);
1382 default:
1383 gcc_unreachable ();
1387 /* Convert stringop (..., vcall_size)
1388 into
1389 if (vcall_size == icall_size)
1390 stringop (..., icall_size);
1391 else
1392 stringop (..., vcall_size);
1393 assuming we'll propagate a true constant into ICALL_SIZE later. */
1395 static void
1396 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1397 gcov_type count, gcov_type all)
1399 gimple tmp_stmt, cond_stmt, icall_stmt;
1400 tree tmp0, tmp1, tmpv, vcall_size, optype;
1401 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1402 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1403 gimple_stmt_iterator gsi;
1404 tree fndecl;
1405 int size_arg;
1407 fndecl = gimple_call_fndecl (vcall_stmt);
1408 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1409 gcc_unreachable();
1411 cond_bb = gimple_bb (vcall_stmt);
1412 gsi = gsi_for_stmt (vcall_stmt);
1414 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1415 optype = TREE_TYPE (vcall_size);
1417 tmpv = create_tmp_var (optype, "PROF");
1418 tmp0 = make_ssa_name (tmpv, NULL);
1419 tmp1 = make_ssa_name (tmpv, NULL);
1420 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1421 SSA_NAME_DEF_STMT (tmp0) = tmp_stmt;
1422 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1424 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1425 SSA_NAME_DEF_STMT (tmp1) = tmp_stmt;
1426 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1428 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1429 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1431 gimple_set_vdef (vcall_stmt, NULL);
1432 gimple_set_vuse (vcall_stmt, NULL);
1433 update_stmt (vcall_stmt);
1434 icall_stmt = gimple_copy (vcall_stmt);
1435 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1436 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1438 /* Fix CFG. */
1439 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1440 e_ci = split_block (cond_bb, cond_stmt);
1441 icall_bb = e_ci->dest;
1442 icall_bb->count = count;
1444 e_iv = split_block (icall_bb, icall_stmt);
1445 vcall_bb = e_iv->dest;
1446 vcall_bb->count = all - count;
1448 e_vj = split_block (vcall_bb, vcall_stmt);
1449 join_bb = e_vj->dest;
1450 join_bb->count = all;
1452 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1453 e_ci->probability = prob;
1454 e_ci->count = count;
1456 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1457 e_cv->probability = REG_BR_PROB_BASE - prob;
1458 e_cv->count = all - count;
1460 remove_edge (e_iv);
1462 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1463 e_ij->probability = REG_BR_PROB_BASE;
1464 e_ij->count = count;
1466 e_vj->probability = REG_BR_PROB_BASE;
1467 e_vj->count = all - count;
1469 /* Insert PHI node for the call result if necessary. */
1470 if (gimple_call_lhs (vcall_stmt)
1471 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1473 tree result = gimple_call_lhs (vcall_stmt);
1474 gimple phi = create_phi_node (result, join_bb);
1475 SSA_NAME_DEF_STMT (result) = phi;
1476 gimple_call_set_lhs (vcall_stmt,
1477 make_ssa_name (SSA_NAME_VAR (result), vcall_stmt));
1478 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1479 gimple_call_set_lhs (icall_stmt,
1480 make_ssa_name (SSA_NAME_VAR (result), icall_stmt));
1481 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1484 /* Because these are all string op builtins, they're all nothrow. */
1485 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1486 gcc_assert (!stmt_could_throw_p (icall_stmt));
1489 /* Find values inside STMT for that we want to measure histograms for
1490 division/modulo optimization. */
1491 static bool
1492 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1494 gimple stmt = gsi_stmt (*gsi);
1495 tree fndecl;
1496 tree blck_size;
1497 enum built_in_function fcode;
1498 histogram_value histogram;
1499 gcov_type count, all, val;
1500 tree dest, src;
1501 unsigned int dest_align, src_align;
1502 gcov_type prob;
1503 tree tree_val;
1504 int size_arg;
1506 if (gimple_code (stmt) != GIMPLE_CALL)
1507 return false;
1508 fndecl = gimple_call_fndecl (stmt);
1509 if (!fndecl)
1510 return false;
1511 fcode = DECL_FUNCTION_CODE (fndecl);
1512 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1513 return false;
1515 blck_size = gimple_call_arg (stmt, size_arg);
1516 if (TREE_CODE (blck_size) == INTEGER_CST)
1517 return false;
1519 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1520 if (!histogram)
1521 return false;
1522 val = histogram->hvalue.counters[0];
1523 count = histogram->hvalue.counters[1];
1524 all = histogram->hvalue.counters[2];
1525 gimple_remove_histogram_value (cfun, stmt, histogram);
1526 /* We require that count is at least half of all; this means
1527 that for the transformation to fire the value must be constant
1528 at least 80% of time. */
1529 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1530 return false;
1531 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1532 return false;
1533 if (all > 0)
1534 prob = (count * REG_BR_PROB_BASE + all / 2) / all;
1535 else
1536 prob = 0;
1537 dest = gimple_call_arg (stmt, 0);
1538 dest_align = get_pointer_alignment (dest);
1539 switch (fcode)
1541 case BUILT_IN_MEMCPY:
1542 case BUILT_IN_MEMPCPY:
1543 src = gimple_call_arg (stmt, 1);
1544 src_align = get_pointer_alignment (src);
1545 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1546 return false;
1547 break;
1548 case BUILT_IN_MEMSET:
1549 if (!can_store_by_pieces (val, builtin_memset_read_str,
1550 gimple_call_arg (stmt, 1),
1551 dest_align, true))
1552 return false;
1553 break;
1554 case BUILT_IN_BZERO:
1555 if (!can_store_by_pieces (val, builtin_memset_read_str,
1556 integer_zero_node,
1557 dest_align, true))
1558 return false;
1559 break;
1560 default:
1561 gcc_unreachable ();
1563 tree_val = build_int_cst_wide (get_gcov_type (),
1564 (unsigned HOST_WIDE_INT) val,
1565 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
1566 if (dump_file)
1568 fprintf (dump_file, "Single value %i stringop transformation on ",
1569 (int)val);
1570 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1572 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1574 return true;
1577 void
1578 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1579 HOST_WIDE_INT *expected_size)
1581 histogram_value histogram;
1582 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1583 if (!histogram)
1584 *expected_size = -1;
1585 else if (!histogram->hvalue.counters[1])
1587 *expected_size = -1;
1588 gimple_remove_histogram_value (cfun, stmt, histogram);
1590 else
1592 gcov_type size;
1593 size = ((histogram->hvalue.counters[0]
1594 + histogram->hvalue.counters[1] / 2)
1595 / histogram->hvalue.counters[1]);
1596 /* Even if we can hold bigger value in SIZE, INT_MAX
1597 is safe "infinity" for code generation strategies. */
1598 if (size > INT_MAX)
1599 size = INT_MAX;
1600 *expected_size = size;
1601 gimple_remove_histogram_value (cfun, stmt, histogram);
1603 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1604 if (!histogram)
1605 *expected_align = 0;
1606 else if (!histogram->hvalue.counters[0])
1608 gimple_remove_histogram_value (cfun, stmt, histogram);
1609 *expected_align = 0;
1611 else
1613 gcov_type count;
1614 int alignment;
1616 count = histogram->hvalue.counters[0];
1617 alignment = 1;
1618 while (!(count & alignment)
1619 && (alignment * 2 * BITS_PER_UNIT))
1620 alignment <<= 1;
1621 *expected_align = alignment * BITS_PER_UNIT;
1622 gimple_remove_histogram_value (cfun, stmt, histogram);
1627 /* Find values inside STMT for that we want to measure histograms for
1628 division/modulo optimization. */
1629 static void
1630 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1632 tree lhs, divisor, op0, type;
1633 histogram_value hist;
1635 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1636 return;
1638 lhs = gimple_assign_lhs (stmt);
1639 type = TREE_TYPE (lhs);
1640 if (!INTEGRAL_TYPE_P (type))
1641 return;
1643 switch (gimple_assign_rhs_code (stmt))
1645 case TRUNC_DIV_EXPR:
1646 case TRUNC_MOD_EXPR:
1647 divisor = gimple_assign_rhs2 (stmt);
1648 op0 = gimple_assign_rhs1 (stmt);
1650 VEC_reserve (histogram_value, heap, *values, 3);
1652 if (is_gimple_reg (divisor))
1653 /* Check for the case where the divisor is the same value most
1654 of the time. */
1655 VEC_quick_push (histogram_value, *values,
1656 gimple_alloc_histogram_value (cfun,
1657 HIST_TYPE_SINGLE_VALUE,
1658 stmt, divisor));
1660 /* For mod, check whether it is not often a noop (or replaceable by
1661 a few subtractions). */
1662 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1663 && TYPE_UNSIGNED (type))
1665 tree val;
1666 /* Check for a special case where the divisor is power of 2. */
1667 VEC_quick_push (histogram_value, *values,
1668 gimple_alloc_histogram_value (cfun, HIST_TYPE_POW2,
1669 stmt, divisor));
1671 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1672 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1673 stmt, val);
1674 hist->hdata.intvl.int_start = 0;
1675 hist->hdata.intvl.steps = 2;
1676 VEC_quick_push (histogram_value, *values, hist);
1678 return;
1680 default:
1681 return;
1685 /* Find calls inside STMT for that we want to measure histograms for
1686 indirect/virtual call optimization. */
1688 static void
1689 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1691 tree callee;
1693 if (gimple_code (stmt) != GIMPLE_CALL
1694 || gimple_call_internal_p (stmt)
1695 || gimple_call_fndecl (stmt) != NULL_TREE)
1696 return;
1698 callee = gimple_call_fn (stmt);
1700 VEC_reserve (histogram_value, heap, *values, 3);
1702 VEC_quick_push (histogram_value, *values,
1703 gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1704 stmt, callee));
1706 return;
1709 /* Find values inside STMT for that we want to measure histograms for
1710 string operations. */
1711 static void
1712 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1714 tree fndecl;
1715 tree blck_size;
1716 tree dest;
1717 int size_arg;
1719 if (gimple_code (stmt) != GIMPLE_CALL)
1720 return;
1721 fndecl = gimple_call_fndecl (stmt);
1722 if (!fndecl)
1723 return;
1725 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1726 return;
1728 dest = gimple_call_arg (stmt, 0);
1729 blck_size = gimple_call_arg (stmt, size_arg);
1731 if (TREE_CODE (blck_size) != INTEGER_CST)
1733 VEC_safe_push (histogram_value, heap, *values,
1734 gimple_alloc_histogram_value (cfun, HIST_TYPE_SINGLE_VALUE,
1735 stmt, blck_size));
1736 VEC_safe_push (histogram_value, heap, *values,
1737 gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1738 stmt, blck_size));
1740 if (TREE_CODE (blck_size) != INTEGER_CST)
1741 VEC_safe_push (histogram_value, heap, *values,
1742 gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1743 stmt, dest));
1746 /* Find values inside STMT for that we want to measure histograms and adds
1747 them to list VALUES. */
1749 static void
1750 gimple_values_to_profile (gimple stmt, histogram_values *values)
1752 if (flag_value_profile_transformations)
1754 gimple_divmod_values_to_profile (stmt, values);
1755 gimple_stringops_values_to_profile (stmt, values);
1756 gimple_indirect_call_to_profile (stmt, values);
1760 void
1761 gimple_find_values_to_profile (histogram_values *values)
1763 basic_block bb;
1764 gimple_stmt_iterator gsi;
1765 unsigned i;
1766 histogram_value hist = NULL;
1768 *values = NULL;
1769 FOR_EACH_BB (bb)
1770 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1771 gimple_values_to_profile (gsi_stmt (gsi), values);
1773 FOR_EACH_VEC_ELT (histogram_value, *values, i, hist)
1775 switch (hist->type)
1777 case HIST_TYPE_INTERVAL:
1778 hist->n_counters = hist->hdata.intvl.steps + 2;
1779 break;
1781 case HIST_TYPE_POW2:
1782 hist->n_counters = 2;
1783 break;
1785 case HIST_TYPE_SINGLE_VALUE:
1786 hist->n_counters = 3;
1787 break;
1789 case HIST_TYPE_CONST_DELTA:
1790 hist->n_counters = 4;
1791 break;
1793 case HIST_TYPE_INDIR_CALL:
1794 hist->n_counters = 3;
1795 break;
1797 case HIST_TYPE_AVERAGE:
1798 hist->n_counters = 2;
1799 break;
1801 case HIST_TYPE_IOR:
1802 hist->n_counters = 1;
1803 break;
1805 default:
1806 gcc_unreachable ();
1808 if (dump_file)
1810 fprintf (dump_file, "Stmt ");
1811 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
1812 dump_histogram_value (dump_file, hist);