Preserve loops in tail-merge
[official-gcc.git] / gcc / value-prof.c
blob3348d7f58c82534b621553f4973f6ea8f6749bfa
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "expr.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "value-prof.h"
29 #include "flags.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "optabs.h"
33 #include "regs.h"
34 #include "ggc.h"
35 #include "tree-flow.h"
36 #include "tree-flow-inline.h"
37 #include "diagnostic.h"
38 #include "gimple-pretty-print.h"
39 #include "coverage.h"
40 #include "tree.h"
41 #include "gcov-io.h"
42 #include "cgraph.h"
43 #include "timevar.h"
44 #include "dumpfile.h"
45 #include "pointer-set.h"
46 #include "profile.h"
47 #include "data-streamer.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;
337 case HIST_TYPE_MAX:
338 gcc_unreachable ();
342 /* Dump information about HIST to DUMP_FILE. */
344 void
345 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
347 struct bitpack_d bp;
348 unsigned int i;
350 bp = bitpack_create (ob->main_stream);
351 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
352 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
353 streamer_write_bitpack (&bp);
354 switch (hist->type)
356 case HIST_TYPE_INTERVAL:
357 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
358 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
359 break;
360 default:
361 break;
363 for (i = 0; i < hist->n_counters; i++)
364 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
365 if (hist->hvalue.next)
366 stream_out_histogram_value (ob, hist->hvalue.next);
368 /* Dump information about HIST to DUMP_FILE. */
370 void
371 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
373 enum hist_type type;
374 unsigned int ncounters = 0;
375 struct bitpack_d bp;
376 unsigned int i;
377 histogram_value new_val;
378 bool next;
379 histogram_value *next_p = NULL;
383 bp = streamer_read_bitpack (ib);
384 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
385 next = bp_unpack_value (&bp, 1);
386 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
387 switch (type)
389 case HIST_TYPE_INTERVAL:
390 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
391 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
392 ncounters = new_val->hdata.intvl.steps + 2;
393 break;
395 case HIST_TYPE_POW2:
396 case HIST_TYPE_AVERAGE:
397 ncounters = 2;
398 break;
400 case HIST_TYPE_SINGLE_VALUE:
401 case HIST_TYPE_INDIR_CALL:
402 ncounters = 3;
403 break;
405 case HIST_TYPE_CONST_DELTA:
406 ncounters = 4;
407 break;
409 case HIST_TYPE_IOR:
410 ncounters = 1;
411 break;
412 case HIST_TYPE_MAX:
413 gcc_unreachable ();
415 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
416 new_val->n_counters = ncounters;
417 for (i = 0; i < ncounters; i++)
418 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
419 debug_gimple_stmt (stmt);
420 if (!next_p)
421 gimple_add_histogram_value (cfun, stmt, new_val);
422 else
423 *next_p = new_val;
424 next_p = &new_val->hvalue.next;
426 while (next);
429 /* Dump all histograms attached to STMT to DUMP_FILE. */
431 void
432 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
434 histogram_value hist;
435 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
436 dump_histogram_value (dump_file, hist);
439 /* Remove all histograms associated with STMT. */
441 void
442 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
444 histogram_value val;
445 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
446 gimple_remove_histogram_value (fun, stmt, val);
449 /* Duplicate all histograms associates with OSTMT to STMT. */
451 void
452 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
453 struct function *ofun, gimple ostmt)
455 histogram_value val;
456 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
458 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
459 memcpy (new_val, val, sizeof (*val));
460 new_val->hvalue.stmt = stmt;
461 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
462 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
463 gimple_add_histogram_value (fun, stmt, new_val);
468 /* Move all histograms associated with OSTMT to STMT. */
470 void
471 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
473 histogram_value val = gimple_histogram_value (fun, ostmt);
474 if (val)
476 /* The following three statements can't be reordered,
477 because histogram hashtab relies on stmt field value
478 for finding the exact slot. */
479 set_histogram_value (fun, ostmt, NULL);
480 for (; val != NULL; val = val->hvalue.next)
481 val->hvalue.stmt = stmt;
482 set_histogram_value (fun, stmt, val);
486 static bool error_found = false;
488 /* Helper function for verify_histograms. For each histogram reachable via htab
489 walk verify that it was reached via statement walk. */
491 static int
492 visit_hist (void **slot, void *data)
494 struct pointer_set_t *visited = (struct pointer_set_t *) data;
495 histogram_value hist = *(histogram_value *) slot;
496 if (!pointer_set_contains (visited, hist))
498 error ("dead histogram");
499 dump_histogram_value (stderr, hist);
500 debug_gimple_stmt (hist->hvalue.stmt);
501 error_found = true;
503 return 1;
507 /* Verify sanity of the histograms. */
509 DEBUG_FUNCTION void
510 verify_histograms (void)
512 basic_block bb;
513 gimple_stmt_iterator gsi;
514 histogram_value hist;
515 struct pointer_set_t *visited_hists;
517 error_found = false;
518 visited_hists = pointer_set_create ();
519 FOR_EACH_BB (bb)
520 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
522 gimple stmt = gsi_stmt (gsi);
524 for (hist = gimple_histogram_value (cfun, stmt); hist;
525 hist = hist->hvalue.next)
527 if (hist->hvalue.stmt != stmt)
529 error ("Histogram value statement does not correspond to "
530 "the statement it is associated with");
531 debug_gimple_stmt (stmt);
532 dump_histogram_value (stderr, hist);
533 error_found = true;
535 pointer_set_insert (visited_hists, hist);
538 if (VALUE_HISTOGRAMS (cfun))
539 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, visited_hists);
540 pointer_set_destroy (visited_hists);
541 if (error_found)
542 internal_error ("verify_histograms failed");
545 /* Helper function for verify_histograms. For each histogram reachable via htab
546 walk verify that it was reached via statement walk. */
548 static int
549 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
551 histogram_value hist = *(histogram_value *) slot;
552 free (hist->hvalue.counters);
553 #ifdef ENABLE_CHECKING
554 memset (hist, 0xab, sizeof (*hist));
555 #endif
556 free (hist);
557 return 1;
560 void
561 free_histograms (void)
563 if (VALUE_HISTOGRAMS (cfun))
565 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
566 htab_delete (VALUE_HISTOGRAMS (cfun));
567 VALUE_HISTOGRAMS (cfun) = NULL;
572 /* The overall number of invocations of the counter should match
573 execution count of basic block. Report it as error rather than
574 internal error as it might mean that user has misused the profile
575 somehow. */
577 static bool
578 check_counter (gimple stmt, const char * name,
579 gcov_type *count, gcov_type *all, gcov_type bb_count)
581 if (*all != bb_count || *count > *all)
583 location_t locus;
584 locus = (stmt != NULL)
585 ? gimple_location (stmt)
586 : DECL_SOURCE_LOCATION (current_function_decl);
587 if (flag_profile_correction)
589 inform (locus, "correcting inconsistent value profile: "
590 "%s profiler overall count (%d) does not match BB count "
591 "(%d)", name, (int)*all, (int)bb_count);
592 *all = bb_count;
593 if (*count > *all)
594 *count = *all;
595 return false;
597 else
599 error_at (locus, "corrupted value profile: %s "
600 "profile counter (%d out of %d) inconsistent with "
601 "basic-block count (%d)",
602 name,
603 (int) *count,
604 (int) *all,
605 (int) bb_count);
606 return true;
610 return false;
614 /* GIMPLE based transformations. */
616 bool
617 gimple_value_profile_transformations (void)
619 basic_block bb;
620 gimple_stmt_iterator gsi;
621 bool changed = false;
623 FOR_EACH_BB (bb)
625 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
627 gimple stmt = gsi_stmt (gsi);
628 histogram_value th = gimple_histogram_value (cfun, stmt);
629 if (!th)
630 continue;
632 if (dump_file)
634 fprintf (dump_file, "Trying transformations on stmt ");
635 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
636 dump_histograms_for_stmt (cfun, dump_file, stmt);
639 /* Transformations: */
640 /* The order of things in this conditional controls which
641 transformation is used when more than one is applicable. */
642 /* It is expected that any code added by the transformations
643 will be added before the current statement, and that the
644 current statement remain valid (although possibly
645 modified) upon return. */
646 if (gimple_mod_subtract_transform (&gsi)
647 || gimple_divmod_fixed_value_transform (&gsi)
648 || gimple_mod_pow2_value_transform (&gsi)
649 || gimple_stringops_transform (&gsi)
650 || gimple_ic_transform (&gsi))
652 stmt = gsi_stmt (gsi);
653 changed = true;
654 /* Original statement may no longer be in the same block. */
655 if (bb != gimple_bb (stmt))
657 bb = gimple_bb (stmt);
658 gsi = gsi_for_stmt (stmt);
664 if (changed)
666 counts_to_freqs ();
669 return changed;
673 /* Generate code for transformation 1 (with parent gimple assignment
674 STMT and probability of taking the optimal path PROB, which is
675 equivalent to COUNT/ALL within roundoff error). This generates the
676 result into a temp and returns the temp; it does not replace or
677 alter the original STMT. */
679 static tree
680 gimple_divmod_fixed_value (gimple stmt, tree value, int prob, gcov_type count,
681 gcov_type all)
683 gimple stmt1, stmt2, stmt3;
684 tree tmp0, tmp1, tmp2;
685 gimple bb1end, bb2end, bb3end;
686 basic_block bb, bb2, bb3, bb4;
687 tree optype, op1, op2;
688 edge e12, e13, e23, e24, e34;
689 gimple_stmt_iterator gsi;
691 gcc_assert (is_gimple_assign (stmt)
692 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
693 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
695 optype = TREE_TYPE (gimple_assign_lhs (stmt));
696 op1 = gimple_assign_rhs1 (stmt);
697 op2 = gimple_assign_rhs2 (stmt);
699 bb = gimple_bb (stmt);
700 gsi = gsi_for_stmt (stmt);
702 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
703 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
704 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
705 stmt2 = gimple_build_assign (tmp1, op2);
706 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
707 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
708 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
709 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
710 bb1end = stmt3;
712 tmp2 = create_tmp_reg (optype, "PROF");
713 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
714 op1, tmp0);
715 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
716 bb2end = stmt1;
718 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
719 op1, op2);
720 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
721 bb3end = stmt1;
723 /* Fix CFG. */
724 /* Edge e23 connects bb2 to bb3, etc. */
725 e12 = split_block (bb, bb1end);
726 bb2 = e12->dest;
727 bb2->count = count;
728 e23 = split_block (bb2, bb2end);
729 bb3 = e23->dest;
730 bb3->count = all - count;
731 e34 = split_block (bb3, bb3end);
732 bb4 = e34->dest;
733 bb4->count = all;
735 e12->flags &= ~EDGE_FALLTHRU;
736 e12->flags |= EDGE_FALSE_VALUE;
737 e12->probability = prob;
738 e12->count = count;
740 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
741 e13->probability = REG_BR_PROB_BASE - prob;
742 e13->count = all - count;
744 remove_edge (e23);
746 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
747 e24->probability = REG_BR_PROB_BASE;
748 e24->count = count;
750 e34->probability = REG_BR_PROB_BASE;
751 e34->count = all - count;
753 return tmp2;
757 /* Do transform 1) on INSN if applicable. */
759 static bool
760 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
762 histogram_value histogram;
763 enum tree_code code;
764 gcov_type val, count, all;
765 tree result, value, tree_val;
766 gcov_type prob;
767 gimple stmt;
769 stmt = gsi_stmt (*si);
770 if (gimple_code (stmt) != GIMPLE_ASSIGN)
771 return false;
773 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
774 return false;
776 code = gimple_assign_rhs_code (stmt);
778 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
779 return false;
781 histogram = gimple_histogram_value_of_type (cfun, stmt,
782 HIST_TYPE_SINGLE_VALUE);
783 if (!histogram)
784 return false;
786 value = histogram->hvalue.value;
787 val = histogram->hvalue.counters[0];
788 count = histogram->hvalue.counters[1];
789 all = histogram->hvalue.counters[2];
790 gimple_remove_histogram_value (cfun, stmt, histogram);
792 /* We require that count is at least half of all; this means
793 that for the transformation to fire the value must be constant
794 at least 50% of time (and 75% gives the guarantee of usage). */
795 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
796 || 2 * count < all
797 || optimize_bb_for_size_p (gimple_bb (stmt)))
798 return false;
800 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
801 return false;
803 /* Compute probability of taking the optimal path. */
804 if (all > 0)
805 prob = GCOV_COMPUTE_SCALE (count, all);
806 else
807 prob = 0;
809 tree_val = build_int_cst_wide (get_gcov_type (),
810 (unsigned HOST_WIDE_INT) val,
811 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
812 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
814 if (dump_file)
816 fprintf (dump_file, "Div/mod by constant ");
817 print_generic_expr (dump_file, value, TDF_SLIM);
818 fprintf (dump_file, "=");
819 print_generic_expr (dump_file, tree_val, TDF_SLIM);
820 fprintf (dump_file, " transformation on insn ");
821 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
824 gimple_assign_set_rhs_from_tree (si, result);
825 update_stmt (gsi_stmt (*si));
827 return true;
830 /* Generate code for transformation 2 (with parent gimple assign STMT and
831 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
832 within roundoff error). This generates the result into a temp and returns
833 the temp; it does not replace or alter the original STMT. */
834 static tree
835 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
837 gimple stmt1, stmt2, stmt3, stmt4;
838 tree tmp2, tmp3;
839 gimple bb1end, bb2end, bb3end;
840 basic_block bb, bb2, bb3, bb4;
841 tree optype, op1, op2;
842 edge e12, e13, e23, e24, e34;
843 gimple_stmt_iterator gsi;
844 tree result;
846 gcc_assert (is_gimple_assign (stmt)
847 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
849 optype = TREE_TYPE (gimple_assign_lhs (stmt));
850 op1 = gimple_assign_rhs1 (stmt);
851 op2 = gimple_assign_rhs2 (stmt);
853 bb = gimple_bb (stmt);
854 gsi = gsi_for_stmt (stmt);
856 result = create_tmp_reg (optype, "PROF");
857 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
858 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
859 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
860 build_int_cst (optype, -1));
861 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
862 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
863 NULL_TREE, NULL_TREE);
864 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
865 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
866 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
867 bb1end = stmt4;
869 /* tmp2 == op2-1 inherited from previous block. */
870 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
871 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
872 bb2end = stmt1;
874 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
875 op1, op2);
876 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
877 bb3end = stmt1;
879 /* Fix CFG. */
880 /* Edge e23 connects bb2 to bb3, etc. */
881 e12 = split_block (bb, bb1end);
882 bb2 = e12->dest;
883 bb2->count = count;
884 e23 = split_block (bb2, bb2end);
885 bb3 = e23->dest;
886 bb3->count = all - count;
887 e34 = split_block (bb3, bb3end);
888 bb4 = e34->dest;
889 bb4->count = all;
891 e12->flags &= ~EDGE_FALLTHRU;
892 e12->flags |= EDGE_FALSE_VALUE;
893 e12->probability = prob;
894 e12->count = count;
896 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
897 e13->probability = REG_BR_PROB_BASE - prob;
898 e13->count = all - count;
900 remove_edge (e23);
902 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
903 e24->probability = REG_BR_PROB_BASE;
904 e24->count = count;
906 e34->probability = REG_BR_PROB_BASE;
907 e34->count = all - count;
909 return result;
912 /* Do transform 2) on INSN if applicable. */
913 static bool
914 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
916 histogram_value histogram;
917 enum tree_code code;
918 gcov_type count, wrong_values, all;
919 tree lhs_type, result, value;
920 gcov_type prob;
921 gimple stmt;
923 stmt = gsi_stmt (*si);
924 if (gimple_code (stmt) != GIMPLE_ASSIGN)
925 return false;
927 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
928 if (!INTEGRAL_TYPE_P (lhs_type))
929 return false;
931 code = gimple_assign_rhs_code (stmt);
933 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
934 return false;
936 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
937 if (!histogram)
938 return false;
940 value = histogram->hvalue.value;
941 wrong_values = histogram->hvalue.counters[0];
942 count = histogram->hvalue.counters[1];
944 gimple_remove_histogram_value (cfun, stmt, histogram);
946 /* We require that we hit a power of 2 at least half of all evaluations. */
947 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
948 || count < wrong_values
949 || optimize_bb_for_size_p (gimple_bb (stmt)))
950 return false;
952 if (dump_file)
954 fprintf (dump_file, "Mod power of 2 transformation on insn ");
955 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
958 /* Compute probability of taking the optimal path. */
959 all = count + wrong_values;
961 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
962 return false;
964 if (all > 0)
965 prob = GCOV_COMPUTE_SCALE (count, all);
966 else
967 prob = 0;
969 result = gimple_mod_pow2 (stmt, prob, count, all);
971 gimple_assign_set_rhs_from_tree (si, result);
972 update_stmt (gsi_stmt (*si));
974 return true;
977 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
978 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
979 supported and this is built into this interface. The probabilities of taking
980 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
981 COUNT2/ALL respectively within roundoff error). This generates the
982 result into a temp and returns the temp; it does not replace or alter
983 the original STMT. */
984 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
986 static tree
987 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
988 gcov_type count1, gcov_type count2, gcov_type all)
990 gimple stmt1, stmt2, stmt3;
991 tree tmp1;
992 gimple bb1end, bb2end = NULL, bb3end;
993 basic_block bb, bb2, bb3, bb4;
994 tree optype, op1, op2;
995 edge e12, e23 = 0, e24, e34, e14;
996 gimple_stmt_iterator gsi;
997 tree result;
999 gcc_assert (is_gimple_assign (stmt)
1000 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1002 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1003 op1 = gimple_assign_rhs1 (stmt);
1004 op2 = gimple_assign_rhs2 (stmt);
1006 bb = gimple_bb (stmt);
1007 gsi = gsi_for_stmt (stmt);
1009 result = create_tmp_reg (optype, "PROF");
1010 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1011 stmt1 = gimple_build_assign (result, op1);
1012 stmt2 = gimple_build_assign (tmp1, op2);
1013 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1014 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1015 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1016 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1017 bb1end = stmt3;
1019 if (ncounts) /* Assumed to be 0 or 1 */
1021 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
1022 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1023 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1024 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1025 bb2end = stmt2;
1028 /* Fallback case. */
1029 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
1030 result, tmp1);
1031 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1032 bb3end = stmt1;
1034 /* Fix CFG. */
1035 /* Edge e23 connects bb2 to bb3, etc. */
1036 /* However block 3 is optional; if it is not there, references
1037 to 3 really refer to block 2. */
1038 e12 = split_block (bb, bb1end);
1039 bb2 = e12->dest;
1040 bb2->count = all - count1;
1042 if (ncounts) /* Assumed to be 0 or 1. */
1044 e23 = split_block (bb2, bb2end);
1045 bb3 = e23->dest;
1046 bb3->count = all - count1 - count2;
1049 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1050 bb4 = e34->dest;
1051 bb4->count = all;
1053 e12->flags &= ~EDGE_FALLTHRU;
1054 e12->flags |= EDGE_FALSE_VALUE;
1055 e12->probability = REG_BR_PROB_BASE - prob1;
1056 e12->count = all - count1;
1058 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1059 e14->probability = prob1;
1060 e14->count = count1;
1062 if (ncounts) /* Assumed to be 0 or 1. */
1064 e23->flags &= ~EDGE_FALLTHRU;
1065 e23->flags |= EDGE_FALSE_VALUE;
1066 e23->count = all - count1 - count2;
1067 e23->probability = REG_BR_PROB_BASE - prob2;
1069 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1070 e24->probability = prob2;
1071 e24->count = count2;
1074 e34->probability = REG_BR_PROB_BASE;
1075 e34->count = all - count1 - count2;
1077 return result;
1081 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1083 static bool
1084 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1086 histogram_value histogram;
1087 enum tree_code code;
1088 gcov_type count, wrong_values, all;
1089 tree lhs_type, result;
1090 gcov_type prob1, prob2;
1091 unsigned int i, steps;
1092 gcov_type count1, count2;
1093 gimple stmt;
1095 stmt = gsi_stmt (*si);
1096 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1097 return false;
1099 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1100 if (!INTEGRAL_TYPE_P (lhs_type))
1101 return false;
1103 code = gimple_assign_rhs_code (stmt);
1105 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1106 return false;
1108 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1109 if (!histogram)
1110 return false;
1112 all = 0;
1113 wrong_values = 0;
1114 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1115 all += histogram->hvalue.counters[i];
1117 wrong_values += histogram->hvalue.counters[i];
1118 wrong_values += histogram->hvalue.counters[i+1];
1119 steps = histogram->hdata.intvl.steps;
1120 all += wrong_values;
1121 count1 = histogram->hvalue.counters[0];
1122 count2 = histogram->hvalue.counters[1];
1124 /* Compute probability of taking the optimal path. */
1125 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1127 gimple_remove_histogram_value (cfun, stmt, histogram);
1128 return false;
1131 if (flag_profile_correction && count1 + count2 > all)
1132 all = count1 + count2;
1134 gcc_assert (count1 + count2 <= all);
1136 /* We require that we use just subtractions in at least 50% of all
1137 evaluations. */
1138 count = 0;
1139 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1141 count += histogram->hvalue.counters[i];
1142 if (count * 2 >= all)
1143 break;
1145 if (i == steps
1146 || optimize_bb_for_size_p (gimple_bb (stmt)))
1147 return false;
1149 gimple_remove_histogram_value (cfun, stmt, histogram);
1150 if (dump_file)
1152 fprintf (dump_file, "Mod subtract transformation on insn ");
1153 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1156 /* Compute probability of taking the optimal path(s). */
1157 if (all > 0)
1159 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1160 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1162 else
1164 prob1 = prob2 = 0;
1167 /* In practice, "steps" is always 2. This interface reflects this,
1168 and will need to be changed if "steps" can change. */
1169 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1171 gimple_assign_set_rhs_from_tree (si, result);
1172 update_stmt (gsi_stmt (*si));
1174 return true;
1177 static vec<cgraph_node_ptr> cgraph_node_map
1178 = vNULL;
1180 /* Initialize map from FUNCDEF_NO to CGRAPH_NODE. */
1182 void
1183 init_node_map (void)
1185 struct cgraph_node *n;
1187 if (get_last_funcdef_no ())
1188 cgraph_node_map.safe_grow_cleared (get_last_funcdef_no ());
1190 FOR_EACH_FUNCTION (n)
1192 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
1193 cgraph_node_map[DECL_STRUCT_FUNCTION (n->symbol.decl)->funcdef_no] = n;
1197 /* Delete the CGRAPH_NODE_MAP. */
1199 void
1200 del_node_map (void)
1202 cgraph_node_map.release ();
1205 /* Return cgraph node for function with pid */
1207 static inline struct cgraph_node*
1208 find_func_by_funcdef_no (int func_id)
1210 int max_id = get_last_funcdef_no ();
1211 if (func_id >= max_id || cgraph_node_map[func_id] == NULL)
1213 if (flag_profile_correction)
1214 inform (DECL_SOURCE_LOCATION (current_function_decl),
1215 "Inconsistent profile: indirect call target (%d) does not exist", func_id);
1216 else
1217 error ("Inconsistent profile: indirect call target (%d) does not exist", func_id);
1219 return NULL;
1222 return cgraph_node_map[func_id];
1225 /* Perform sanity check on the indirect call target. Due to race conditions,
1226 false function target may be attributed to an indirect call site. If the
1227 call expression type mismatches with the target function's type, expand_call
1228 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1229 Returns true if TARGET is considered ok for call CALL_STMT. */
1231 static bool
1232 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1234 location_t locus;
1235 if (gimple_check_call_matching_types (call_stmt, target->symbol.decl))
1236 return true;
1238 locus = gimple_location (call_stmt);
1239 inform (locus, "Skipping target %s with mismatching types for icall ",
1240 cgraph_node_name (target));
1241 return false;
1244 /* Do transformation
1246 if (actual_callee_address == address_of_most_common_function/method)
1247 do direct call
1248 else
1249 old call
1252 static gimple
1253 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1254 int prob, gcov_type count, gcov_type all)
1256 gimple dcall_stmt, load_stmt, cond_stmt;
1257 tree tmp0, tmp1, tmp;
1258 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1259 tree optype = build_pointer_type (void_type_node);
1260 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1261 gimple_stmt_iterator gsi;
1262 int lp_nr, dflags;
1264 cond_bb = gimple_bb (icall_stmt);
1265 gsi = gsi_for_stmt (icall_stmt);
1267 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1268 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1269 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1270 load_stmt = gimple_build_assign (tmp0, tmp);
1271 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1273 tmp = fold_convert (optype, build_addr (direct_call->symbol.decl,
1274 current_function_decl));
1275 load_stmt = gimple_build_assign (tmp1, tmp);
1276 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1278 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1279 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1281 gimple_set_vdef (icall_stmt, NULL_TREE);
1282 gimple_set_vuse (icall_stmt, NULL_TREE);
1283 update_stmt (icall_stmt);
1284 dcall_stmt = gimple_copy (icall_stmt);
1285 gimple_call_set_fndecl (dcall_stmt, direct_call->symbol.decl);
1286 dflags = flags_from_decl_or_type (direct_call->symbol.decl);
1287 if ((dflags & ECF_NORETURN) != 0)
1288 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1289 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1291 /* Fix CFG. */
1292 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1293 e_cd = split_block (cond_bb, cond_stmt);
1294 dcall_bb = e_cd->dest;
1295 dcall_bb->count = count;
1297 e_di = split_block (dcall_bb, dcall_stmt);
1298 icall_bb = e_di->dest;
1299 icall_bb->count = all - count;
1301 /* Do not disturb existing EH edges from the indirect call. */
1302 if (!stmt_ends_bb_p (icall_stmt))
1303 e_ij = split_block (icall_bb, icall_stmt);
1304 else
1306 e_ij = find_fallthru_edge (icall_bb->succs);
1307 /* The indirect call might be noreturn. */
1308 if (e_ij != NULL)
1310 e_ij->probability = REG_BR_PROB_BASE;
1311 e_ij->count = all - count;
1312 e_ij = single_pred_edge (split_edge (e_ij));
1315 if (e_ij != NULL)
1317 join_bb = e_ij->dest;
1318 join_bb->count = all;
1321 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1322 e_cd->probability = prob;
1323 e_cd->count = count;
1325 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1326 e_ci->probability = REG_BR_PROB_BASE - prob;
1327 e_ci->count = all - count;
1329 remove_edge (e_di);
1331 if (e_ij != NULL)
1333 if ((dflags & ECF_NORETURN) != 0)
1334 e_ij->count = all;
1335 else
1337 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1338 e_dj->probability = REG_BR_PROB_BASE;
1339 e_dj->count = count;
1341 e_ij->count = all - count;
1343 e_ij->probability = REG_BR_PROB_BASE;
1346 /* Insert PHI node for the call result if necessary. */
1347 if (gimple_call_lhs (icall_stmt)
1348 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1349 && (dflags & ECF_NORETURN) == 0)
1351 tree result = gimple_call_lhs (icall_stmt);
1352 gimple phi = create_phi_node (result, join_bb);
1353 gimple_call_set_lhs (icall_stmt,
1354 duplicate_ssa_name (result, icall_stmt));
1355 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1356 gimple_call_set_lhs (dcall_stmt,
1357 duplicate_ssa_name (result, dcall_stmt));
1358 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1361 /* Build an EH edge for the direct call if necessary. */
1362 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1363 if (lp_nr != 0
1364 && stmt_could_throw_p (dcall_stmt))
1366 edge e_eh, e;
1367 edge_iterator ei;
1368 gimple_stmt_iterator psi;
1370 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1371 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1372 if (e_eh->flags & EDGE_EH)
1373 break;
1374 e = make_edge (dcall_bb, e_eh->dest, EDGE_EH);
1375 for (psi = gsi_start_phis (e_eh->dest);
1376 !gsi_end_p (psi); gsi_next (&psi))
1378 gimple phi = gsi_stmt (psi);
1379 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1380 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1384 return dcall_stmt;
1388 For every checked indirect/virtual call determine if most common pid of
1389 function/class method has probability more than 50%. If yes modify code of
1390 this call to:
1393 static bool
1394 gimple_ic_transform (gimple_stmt_iterator *gsi)
1396 gimple stmt = gsi_stmt (*gsi);
1397 histogram_value histogram;
1398 gcov_type val, count, all, bb_all;
1399 gcov_type prob;
1400 gimple modify;
1401 struct cgraph_node *direct_call;
1403 if (gimple_code (stmt) != GIMPLE_CALL)
1404 return false;
1406 if (gimple_call_fndecl (stmt) != NULL_TREE)
1407 return false;
1409 if (gimple_call_internal_p (stmt))
1410 return false;
1412 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1413 if (!histogram)
1414 return false;
1416 val = histogram->hvalue.counters [0];
1417 count = histogram->hvalue.counters [1];
1418 all = histogram->hvalue.counters [2];
1419 gimple_remove_histogram_value (cfun, stmt, histogram);
1421 if (4 * count <= 3 * all)
1422 return false;
1424 bb_all = gimple_bb (stmt)->count;
1425 /* The order of CHECK_COUNTER calls is important -
1426 since check_counter can correct the third parameter
1427 and we want to make count <= all <= bb_all. */
1428 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1429 || check_counter (stmt, "ic", &count, &all, all))
1430 return false;
1432 if (all > 0)
1433 prob = GCOV_COMPUTE_SCALE (count, all);
1434 else
1435 prob = 0;
1436 direct_call = find_func_by_funcdef_no ((int)val);
1438 if (direct_call == NULL)
1439 return false;
1441 if (!check_ic_target (stmt, direct_call))
1442 return false;
1444 modify = gimple_ic (stmt, direct_call, prob, count, all);
1446 if (dump_file)
1448 fprintf (dump_file, "Indirect call -> direct call ");
1449 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1450 fprintf (dump_file, "=> ");
1451 print_generic_expr (dump_file, direct_call->symbol.decl, TDF_SLIM);
1452 fprintf (dump_file, " transformation on insn ");
1453 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1454 fprintf (dump_file, " to ");
1455 print_gimple_stmt (dump_file, modify, 0, TDF_SLIM);
1456 fprintf (dump_file, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1457 " hist->all "HOST_WIDEST_INT_PRINT_DEC"\n", count, all);
1460 return true;
1463 /* Return true if the stringop CALL with FNDECL shall be profiled.
1464 SIZE_ARG be set to the argument index for the size of the string
1465 operation.
1467 static bool
1468 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1470 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1472 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1473 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1474 return false;
1476 switch (fcode)
1478 case BUILT_IN_MEMCPY:
1479 case BUILT_IN_MEMPCPY:
1480 *size_arg = 2;
1481 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1482 INTEGER_TYPE, VOID_TYPE);
1483 case BUILT_IN_MEMSET:
1484 *size_arg = 2;
1485 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1486 INTEGER_TYPE, VOID_TYPE);
1487 case BUILT_IN_BZERO:
1488 *size_arg = 1;
1489 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1490 VOID_TYPE);
1491 default:
1492 gcc_unreachable ();
1496 /* Convert stringop (..., vcall_size)
1497 into
1498 if (vcall_size == icall_size)
1499 stringop (..., icall_size);
1500 else
1501 stringop (..., vcall_size);
1502 assuming we'll propagate a true constant into ICALL_SIZE later. */
1504 static void
1505 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1506 gcov_type count, gcov_type all)
1508 gimple tmp_stmt, cond_stmt, icall_stmt;
1509 tree tmp0, tmp1, vcall_size, optype;
1510 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1511 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1512 gimple_stmt_iterator gsi;
1513 tree fndecl;
1514 int size_arg;
1516 fndecl = gimple_call_fndecl (vcall_stmt);
1517 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1518 gcc_unreachable();
1520 cond_bb = gimple_bb (vcall_stmt);
1521 gsi = gsi_for_stmt (vcall_stmt);
1523 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1524 optype = TREE_TYPE (vcall_size);
1526 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1527 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1528 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1529 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1531 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1532 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1534 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1535 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1537 gimple_set_vdef (vcall_stmt, NULL);
1538 gimple_set_vuse (vcall_stmt, NULL);
1539 update_stmt (vcall_stmt);
1540 icall_stmt = gimple_copy (vcall_stmt);
1541 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1542 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1544 /* Fix CFG. */
1545 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1546 e_ci = split_block (cond_bb, cond_stmt);
1547 icall_bb = e_ci->dest;
1548 icall_bb->count = count;
1550 e_iv = split_block (icall_bb, icall_stmt);
1551 vcall_bb = e_iv->dest;
1552 vcall_bb->count = all - count;
1554 e_vj = split_block (vcall_bb, vcall_stmt);
1555 join_bb = e_vj->dest;
1556 join_bb->count = all;
1558 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1559 e_ci->probability = prob;
1560 e_ci->count = count;
1562 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1563 e_cv->probability = REG_BR_PROB_BASE - prob;
1564 e_cv->count = all - count;
1566 remove_edge (e_iv);
1568 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1569 e_ij->probability = REG_BR_PROB_BASE;
1570 e_ij->count = count;
1572 e_vj->probability = REG_BR_PROB_BASE;
1573 e_vj->count = all - count;
1575 /* Insert PHI node for the call result if necessary. */
1576 if (gimple_call_lhs (vcall_stmt)
1577 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1579 tree result = gimple_call_lhs (vcall_stmt);
1580 gimple phi = create_phi_node (result, join_bb);
1581 gimple_call_set_lhs (vcall_stmt,
1582 duplicate_ssa_name (result, vcall_stmt));
1583 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1584 gimple_call_set_lhs (icall_stmt,
1585 duplicate_ssa_name (result, icall_stmt));
1586 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1589 /* Because these are all string op builtins, they're all nothrow. */
1590 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1591 gcc_assert (!stmt_could_throw_p (icall_stmt));
1594 /* Find values inside STMT for that we want to measure histograms for
1595 division/modulo optimization. */
1596 static bool
1597 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1599 gimple stmt = gsi_stmt (*gsi);
1600 tree fndecl;
1601 tree blck_size;
1602 enum built_in_function fcode;
1603 histogram_value histogram;
1604 gcov_type count, all, val;
1605 tree dest, src;
1606 unsigned int dest_align, src_align;
1607 gcov_type prob;
1608 tree tree_val;
1609 int size_arg;
1611 if (gimple_code (stmt) != GIMPLE_CALL)
1612 return false;
1613 fndecl = gimple_call_fndecl (stmt);
1614 if (!fndecl)
1615 return false;
1616 fcode = DECL_FUNCTION_CODE (fndecl);
1617 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1618 return false;
1620 blck_size = gimple_call_arg (stmt, size_arg);
1621 if (TREE_CODE (blck_size) == INTEGER_CST)
1622 return false;
1624 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1625 if (!histogram)
1626 return false;
1627 val = histogram->hvalue.counters[0];
1628 count = histogram->hvalue.counters[1];
1629 all = histogram->hvalue.counters[2];
1630 gimple_remove_histogram_value (cfun, stmt, histogram);
1631 /* We require that count is at least half of all; this means
1632 that for the transformation to fire the value must be constant
1633 at least 80% of time. */
1634 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1635 return false;
1636 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1637 return false;
1638 if (all > 0)
1639 prob = GCOV_COMPUTE_SCALE (count, all);
1640 else
1641 prob = 0;
1642 dest = gimple_call_arg (stmt, 0);
1643 dest_align = get_pointer_alignment (dest);
1644 switch (fcode)
1646 case BUILT_IN_MEMCPY:
1647 case BUILT_IN_MEMPCPY:
1648 src = gimple_call_arg (stmt, 1);
1649 src_align = get_pointer_alignment (src);
1650 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1651 return false;
1652 break;
1653 case BUILT_IN_MEMSET:
1654 if (!can_store_by_pieces (val, builtin_memset_read_str,
1655 gimple_call_arg (stmt, 1),
1656 dest_align, true))
1657 return false;
1658 break;
1659 case BUILT_IN_BZERO:
1660 if (!can_store_by_pieces (val, builtin_memset_read_str,
1661 integer_zero_node,
1662 dest_align, true))
1663 return false;
1664 break;
1665 default:
1666 gcc_unreachable ();
1668 tree_val = build_int_cst_wide (get_gcov_type (),
1669 (unsigned HOST_WIDE_INT) val,
1670 val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1);
1671 if (dump_file)
1673 fprintf (dump_file, "Single value %i stringop transformation on ",
1674 (int)val);
1675 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1677 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1679 return true;
1682 void
1683 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1684 HOST_WIDE_INT *expected_size)
1686 histogram_value histogram;
1687 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1688 if (!histogram)
1689 *expected_size = -1;
1690 else if (!histogram->hvalue.counters[1])
1692 *expected_size = -1;
1693 gimple_remove_histogram_value (cfun, stmt, histogram);
1695 else
1697 gcov_type size;
1698 size = ((histogram->hvalue.counters[0]
1699 + histogram->hvalue.counters[1] / 2)
1700 / histogram->hvalue.counters[1]);
1701 /* Even if we can hold bigger value in SIZE, INT_MAX
1702 is safe "infinity" for code generation strategies. */
1703 if (size > INT_MAX)
1704 size = INT_MAX;
1705 *expected_size = size;
1706 gimple_remove_histogram_value (cfun, stmt, histogram);
1708 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1709 if (!histogram)
1710 *expected_align = 0;
1711 else if (!histogram->hvalue.counters[0])
1713 gimple_remove_histogram_value (cfun, stmt, histogram);
1714 *expected_align = 0;
1716 else
1718 gcov_type count;
1719 int alignment;
1721 count = histogram->hvalue.counters[0];
1722 alignment = 1;
1723 while (!(count & alignment)
1724 && (alignment * 2 * BITS_PER_UNIT))
1725 alignment <<= 1;
1726 *expected_align = alignment * BITS_PER_UNIT;
1727 gimple_remove_histogram_value (cfun, stmt, histogram);
1732 /* Find values inside STMT for that we want to measure histograms for
1733 division/modulo optimization. */
1734 static void
1735 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1737 tree lhs, divisor, op0, type;
1738 histogram_value hist;
1740 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1741 return;
1743 lhs = gimple_assign_lhs (stmt);
1744 type = TREE_TYPE (lhs);
1745 if (!INTEGRAL_TYPE_P (type))
1746 return;
1748 switch (gimple_assign_rhs_code (stmt))
1750 case TRUNC_DIV_EXPR:
1751 case TRUNC_MOD_EXPR:
1752 divisor = gimple_assign_rhs2 (stmt);
1753 op0 = gimple_assign_rhs1 (stmt);
1755 values->reserve (3);
1757 if (TREE_CODE (divisor) == SSA_NAME)
1758 /* Check for the case where the divisor is the same value most
1759 of the time. */
1760 values->quick_push (gimple_alloc_histogram_value (cfun,
1761 HIST_TYPE_SINGLE_VALUE,
1762 stmt, divisor));
1764 /* For mod, check whether it is not often a noop (or replaceable by
1765 a few subtractions). */
1766 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1767 && TYPE_UNSIGNED (type))
1769 tree val;
1770 /* Check for a special case where the divisor is power of 2. */
1771 values->quick_push (gimple_alloc_histogram_value (cfun,
1772 HIST_TYPE_POW2,
1773 stmt, divisor));
1775 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1776 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1777 stmt, val);
1778 hist->hdata.intvl.int_start = 0;
1779 hist->hdata.intvl.steps = 2;
1780 values->quick_push (hist);
1782 return;
1784 default:
1785 return;
1789 /* Find calls inside STMT for that we want to measure histograms for
1790 indirect/virtual call optimization. */
1792 static void
1793 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1795 tree callee;
1797 if (gimple_code (stmt) != GIMPLE_CALL
1798 || gimple_call_internal_p (stmt)
1799 || gimple_call_fndecl (stmt) != NULL_TREE)
1800 return;
1802 callee = gimple_call_fn (stmt);
1804 values->reserve (3);
1806 values->quick_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1807 stmt, callee));
1809 return;
1812 /* Find values inside STMT for that we want to measure histograms for
1813 string operations. */
1814 static void
1815 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1817 tree fndecl;
1818 tree blck_size;
1819 tree dest;
1820 int size_arg;
1822 if (gimple_code (stmt) != GIMPLE_CALL)
1823 return;
1824 fndecl = gimple_call_fndecl (stmt);
1825 if (!fndecl)
1826 return;
1828 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1829 return;
1831 dest = gimple_call_arg (stmt, 0);
1832 blck_size = gimple_call_arg (stmt, size_arg);
1834 if (TREE_CODE (blck_size) != INTEGER_CST)
1836 values->safe_push (gimple_alloc_histogram_value (cfun,
1837 HIST_TYPE_SINGLE_VALUE,
1838 stmt, blck_size));
1839 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1840 stmt, blck_size));
1842 if (TREE_CODE (blck_size) != INTEGER_CST)
1843 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1844 stmt, dest));
1847 /* Find values inside STMT for that we want to measure histograms and adds
1848 them to list VALUES. */
1850 static void
1851 gimple_values_to_profile (gimple stmt, histogram_values *values)
1853 gimple_divmod_values_to_profile (stmt, values);
1854 gimple_stringops_values_to_profile (stmt, values);
1855 gimple_indirect_call_to_profile (stmt, values);
1858 void
1859 gimple_find_values_to_profile (histogram_values *values)
1861 basic_block bb;
1862 gimple_stmt_iterator gsi;
1863 unsigned i;
1864 histogram_value hist = NULL;
1866 values->create (0);
1867 FOR_EACH_BB (bb)
1868 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1869 gimple_values_to_profile (gsi_stmt (gsi), values);
1871 FOR_EACH_VEC_ELT (*values, i, hist)
1873 switch (hist->type)
1875 case HIST_TYPE_INTERVAL:
1876 hist->n_counters = hist->hdata.intvl.steps + 2;
1877 break;
1879 case HIST_TYPE_POW2:
1880 hist->n_counters = 2;
1881 break;
1883 case HIST_TYPE_SINGLE_VALUE:
1884 hist->n_counters = 3;
1885 break;
1887 case HIST_TYPE_CONST_DELTA:
1888 hist->n_counters = 4;
1889 break;
1891 case HIST_TYPE_INDIR_CALL:
1892 hist->n_counters = 3;
1893 break;
1895 case HIST_TYPE_AVERAGE:
1896 hist->n_counters = 2;
1897 break;
1899 case HIST_TYPE_IOR:
1900 hist->n_counters = 1;
1901 break;
1903 default:
1904 gcc_unreachable ();
1906 if (dump_file)
1908 fprintf (dump_file, "Stmt ");
1909 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
1910 dump_histogram_value (dump_file, hist);