Simplify convert_modes, ignoring invalid old modes for CONST_INTs.
[official-gcc.git] / gcc / value-prof.c
blob29f51e7457acb6a5e8da5948c25a0323fcd71a29
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-ssa.h"
36 #include "diagnostic.h"
37 #include "gimple-pretty-print.h"
38 #include "coverage.h"
39 #include "tree.h"
40 #include "gcov-io.h"
41 #include "cgraph.h"
42 #include "timevar.h"
43 #include "dumpfile.h"
44 #include "pointer-set.h"
45 #include "profile.h"
46 #include "data-streamer.h"
48 /* In this file value profile based optimizations are placed. Currently the
49 following optimizations are implemented (for more detailed descriptions
50 see comments at value_profile_transformations):
52 1) Division/modulo specialization. Provided that we can determine that the
53 operands of the division have some special properties, we may use it to
54 produce more effective code.
56 2) Indirect/virtual call specialization. If we can determine most
57 common function callee in indirect/virtual call. We can use this
58 information to improve code effectiveness (especially info for
59 the inliner).
61 3) Speculative prefetching. If we are able to determine that the difference
62 between addresses accessed by a memory reference is usually constant, we
63 may add the prefetch instructions.
64 FIXME: This transformation was removed together with RTL based value
65 profiling.
68 Value profiling internals
69 ==========================
71 Every value profiling transformation starts with defining what values
72 to profile. There are different histogram types (see HIST_TYPE_* in
73 value-prof.h) and each transformation can request one or more histogram
74 types per GIMPLE statement. The function gimple_find_values_to_profile()
75 collects the values to profile in a vec, and adds the number of counters
76 required for the different histogram types.
78 For a -fprofile-generate run, the statements for which values should be
79 recorded, are instrumented in instrument_values(). The instrumentation
80 is done by helper functions that can be found in tree-profile.c, where
81 new types of histograms can be added if necessary.
83 After a -fprofile-use, the value profiling data is read back in by
84 compute_value_histograms() that translates the collected data to
85 histograms and attaches them to the profiled statements via
86 gimple_add_histogram_value(). Histograms are stored in a hash table
87 that is attached to every intrumented function, see VALUE_HISTOGRAMS
88 in function.h.
90 The value-profile transformations driver is the function
91 gimple_value_profile_transformations(). It traverses all statements in
92 the to-be-transformed function, and looks for statements with one or
93 more histograms attached to it. If a statement has histograms, the
94 transformation functions are called on the statement.
96 Limitations / FIXME / TODO:
97 * Only one histogram of each type can be associated with a statement.
98 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
99 (This type of histogram was originally used to implement a form of
100 stride profiling based speculative prefetching to improve SPEC2000
101 scores for memory-bound benchmarks, mcf and equake. However, this
102 was an RTL value-profiling transformation, and those have all been
103 removed.)
104 * Some value profile transformations are done in builtins.c (?!)
105 * Updating of histograms needs some TLC.
106 * The value profiling code could be used to record analysis results
107 from non-profiling (e.g. VRP).
108 * Adding new profilers should be simplified, starting with a cleanup
109 of what-happens-where andwith making gimple_find_values_to_profile
110 and gimple_value_profile_transformations table-driven, perhaps...
113 static tree gimple_divmod_fixed_value (gimple, tree, int, gcov_type, gcov_type);
114 static tree gimple_mod_pow2 (gimple, int, gcov_type, gcov_type);
115 static tree gimple_mod_subtract (gimple, int, int, int, gcov_type, gcov_type,
116 gcov_type);
117 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
118 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
119 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
120 static bool gimple_stringops_transform (gimple_stmt_iterator *);
121 static bool gimple_ic_transform (gimple_stmt_iterator *);
123 /* Allocate histogram value. */
125 static histogram_value
126 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
127 enum hist_type type, gimple stmt, tree value)
129 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
130 hist->hvalue.value = value;
131 hist->hvalue.stmt = stmt;
132 hist->type = type;
133 return hist;
136 /* Hash value for histogram. */
138 static hashval_t
139 histogram_hash (const void *x)
141 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
144 /* Return nonzero if statement for histogram_value X is Y. */
146 static int
147 histogram_eq (const void *x, const void *y)
149 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
152 /* Set histogram for STMT. */
154 static void
155 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
157 void **loc;
158 if (!hist && !VALUE_HISTOGRAMS (fun))
159 return;
160 if (!VALUE_HISTOGRAMS (fun))
161 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
162 histogram_eq, NULL);
163 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
164 htab_hash_pointer (stmt),
165 hist ? INSERT : NO_INSERT);
166 if (!hist)
168 if (loc)
169 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
170 return;
172 *loc = hist;
175 /* Get histogram list for STMT. */
177 histogram_value
178 gimple_histogram_value (struct function *fun, gimple stmt)
180 if (!VALUE_HISTOGRAMS (fun))
181 return NULL;
182 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
183 htab_hash_pointer (stmt));
186 /* Add histogram for STMT. */
188 void
189 gimple_add_histogram_value (struct function *fun, gimple stmt,
190 histogram_value hist)
192 hist->hvalue.next = gimple_histogram_value (fun, stmt);
193 set_histogram_value (fun, stmt, hist);
197 /* Remove histogram HIST from STMT's histogram list. */
199 void
200 gimple_remove_histogram_value (struct function *fun, gimple stmt,
201 histogram_value hist)
203 histogram_value hist2 = gimple_histogram_value (fun, stmt);
204 if (hist == hist2)
206 set_histogram_value (fun, stmt, hist->hvalue.next);
208 else
210 while (hist2->hvalue.next != hist)
211 hist2 = hist2->hvalue.next;
212 hist2->hvalue.next = hist->hvalue.next;
214 free (hist->hvalue.counters);
215 #ifdef ENABLE_CHECKING
216 memset (hist, 0xab, sizeof (*hist));
217 #endif
218 free (hist);
222 /* Lookup histogram of type TYPE in the STMT. */
224 histogram_value
225 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
226 enum hist_type type)
228 histogram_value hist;
229 for (hist = gimple_histogram_value (fun, stmt); hist;
230 hist = hist->hvalue.next)
231 if (hist->type == type)
232 return hist;
233 return NULL;
236 /* Dump information about HIST to DUMP_FILE. */
238 static void
239 dump_histogram_value (FILE *dump_file, histogram_value hist)
241 switch (hist->type)
243 case HIST_TYPE_INTERVAL:
244 fprintf (dump_file, "Interval counter range %d -- %d",
245 hist->hdata.intvl.int_start,
246 (hist->hdata.intvl.int_start
247 + hist->hdata.intvl.steps - 1));
248 if (hist->hvalue.counters)
250 unsigned int i;
251 fprintf (dump_file, " [");
252 for (i = 0; i < hist->hdata.intvl.steps; i++)
253 fprintf (dump_file, " %d:"HOST_WIDEST_INT_PRINT_DEC,
254 hist->hdata.intvl.int_start + i,
255 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
256 fprintf (dump_file, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC,
257 (HOST_WIDEST_INT) hist->hvalue.counters[i]);
259 fprintf (dump_file, ".\n");
260 break;
262 case HIST_TYPE_POW2:
263 fprintf (dump_file, "Pow2 counter ");
264 if (hist->hvalue.counters)
266 fprintf (dump_file, "pow2:"HOST_WIDEST_INT_PRINT_DEC
267 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC,
268 (HOST_WIDEST_INT) hist->hvalue.counters[0],
269 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
271 fprintf (dump_file, ".\n");
272 break;
274 case HIST_TYPE_SINGLE_VALUE:
275 fprintf (dump_file, "Single value ");
276 if (hist->hvalue.counters)
278 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
279 " match:"HOST_WIDEST_INT_PRINT_DEC
280 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
281 (HOST_WIDEST_INT) hist->hvalue.counters[0],
282 (HOST_WIDEST_INT) hist->hvalue.counters[1],
283 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
285 fprintf (dump_file, ".\n");
286 break;
288 case HIST_TYPE_AVERAGE:
289 fprintf (dump_file, "Average value ");
290 if (hist->hvalue.counters)
292 fprintf (dump_file, "sum:"HOST_WIDEST_INT_PRINT_DEC
293 " times:"HOST_WIDEST_INT_PRINT_DEC,
294 (HOST_WIDEST_INT) hist->hvalue.counters[0],
295 (HOST_WIDEST_INT) hist->hvalue.counters[1]);
297 fprintf (dump_file, ".\n");
298 break;
300 case HIST_TYPE_IOR:
301 fprintf (dump_file, "IOR value ");
302 if (hist->hvalue.counters)
304 fprintf (dump_file, "ior:"HOST_WIDEST_INT_PRINT_DEC,
305 (HOST_WIDEST_INT) hist->hvalue.counters[0]);
307 fprintf (dump_file, ".\n");
308 break;
310 case HIST_TYPE_CONST_DELTA:
311 fprintf (dump_file, "Constant delta ");
312 if (hist->hvalue.counters)
314 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
315 " match:"HOST_WIDEST_INT_PRINT_DEC
316 " wrong:"HOST_WIDEST_INT_PRINT_DEC,
317 (HOST_WIDEST_INT) hist->hvalue.counters[0],
318 (HOST_WIDEST_INT) hist->hvalue.counters[1],
319 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
321 fprintf (dump_file, ".\n");
322 break;
323 case HIST_TYPE_INDIR_CALL:
324 fprintf (dump_file, "Indirect call ");
325 if (hist->hvalue.counters)
327 fprintf (dump_file, "value:"HOST_WIDEST_INT_PRINT_DEC
328 " match:"HOST_WIDEST_INT_PRINT_DEC
329 " all:"HOST_WIDEST_INT_PRINT_DEC,
330 (HOST_WIDEST_INT) hist->hvalue.counters[0],
331 (HOST_WIDEST_INT) hist->hvalue.counters[1],
332 (HOST_WIDEST_INT) hist->hvalue.counters[2]);
334 fprintf (dump_file, ".\n");
335 break;
336 case HIST_TYPE_MAX:
337 gcc_unreachable ();
341 /* Dump information about HIST to DUMP_FILE. */
343 void
344 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
346 struct bitpack_d bp;
347 unsigned int i;
349 bp = bitpack_create (ob->main_stream);
350 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
351 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
352 streamer_write_bitpack (&bp);
353 switch (hist->type)
355 case HIST_TYPE_INTERVAL:
356 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
357 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
358 break;
359 default:
360 break;
362 for (i = 0; i < hist->n_counters; i++)
363 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
364 if (hist->hvalue.next)
365 stream_out_histogram_value (ob, hist->hvalue.next);
367 /* Dump information about HIST to DUMP_FILE. */
369 void
370 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
372 enum hist_type type;
373 unsigned int ncounters = 0;
374 struct bitpack_d bp;
375 unsigned int i;
376 histogram_value new_val;
377 bool next;
378 histogram_value *next_p = NULL;
382 bp = streamer_read_bitpack (ib);
383 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
384 next = bp_unpack_value (&bp, 1);
385 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
386 switch (type)
388 case HIST_TYPE_INTERVAL:
389 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
390 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
391 ncounters = new_val->hdata.intvl.steps + 2;
392 break;
394 case HIST_TYPE_POW2:
395 case HIST_TYPE_AVERAGE:
396 ncounters = 2;
397 break;
399 case HIST_TYPE_SINGLE_VALUE:
400 case HIST_TYPE_INDIR_CALL:
401 ncounters = 3;
402 break;
404 case HIST_TYPE_CONST_DELTA:
405 ncounters = 4;
406 break;
408 case HIST_TYPE_IOR:
409 ncounters = 1;
410 break;
411 case HIST_TYPE_MAX:
412 gcc_unreachable ();
414 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
415 new_val->n_counters = ncounters;
416 for (i = 0; i < ncounters; i++)
417 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
418 if (!next_p)
419 gimple_add_histogram_value (cfun, stmt, new_val);
420 else
421 *next_p = new_val;
422 next_p = &new_val->hvalue.next;
424 while (next);
427 /* Dump all histograms attached to STMT to DUMP_FILE. */
429 void
430 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
432 histogram_value hist;
433 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
434 dump_histogram_value (dump_file, hist);
437 /* Remove all histograms associated with STMT. */
439 void
440 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
442 histogram_value val;
443 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
444 gimple_remove_histogram_value (fun, stmt, val);
447 /* Duplicate all histograms associates with OSTMT to STMT. */
449 void
450 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
451 struct function *ofun, gimple ostmt)
453 histogram_value val;
454 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
456 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
457 memcpy (new_val, val, sizeof (*val));
458 new_val->hvalue.stmt = stmt;
459 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
460 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
461 gimple_add_histogram_value (fun, stmt, new_val);
466 /* Move all histograms associated with OSTMT to STMT. */
468 void
469 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
471 histogram_value val = gimple_histogram_value (fun, ostmt);
472 if (val)
474 /* The following three statements can't be reordered,
475 because histogram hashtab relies on stmt field value
476 for finding the exact slot. */
477 set_histogram_value (fun, ostmt, NULL);
478 for (; val != NULL; val = val->hvalue.next)
479 val->hvalue.stmt = stmt;
480 set_histogram_value (fun, stmt, val);
484 static bool error_found = false;
486 /* Helper function for verify_histograms. For each histogram reachable via htab
487 walk verify that it was reached via statement walk. */
489 static int
490 visit_hist (void **slot, void *data)
492 struct pointer_set_t *visited = (struct pointer_set_t *) data;
493 histogram_value hist = *(histogram_value *) slot;
494 if (!pointer_set_contains (visited, hist))
496 error ("dead histogram");
497 dump_histogram_value (stderr, hist);
498 debug_gimple_stmt (hist->hvalue.stmt);
499 error_found = true;
501 return 1;
505 /* Verify sanity of the histograms. */
507 DEBUG_FUNCTION void
508 verify_histograms (void)
510 basic_block bb;
511 gimple_stmt_iterator gsi;
512 histogram_value hist;
513 struct pointer_set_t *visited_hists;
515 error_found = false;
516 visited_hists = pointer_set_create ();
517 FOR_EACH_BB (bb)
518 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
520 gimple stmt = gsi_stmt (gsi);
522 for (hist = gimple_histogram_value (cfun, stmt); hist;
523 hist = hist->hvalue.next)
525 if (hist->hvalue.stmt != stmt)
527 error ("Histogram value statement does not correspond to "
528 "the statement it is associated with");
529 debug_gimple_stmt (stmt);
530 dump_histogram_value (stderr, hist);
531 error_found = true;
533 pointer_set_insert (visited_hists, hist);
536 if (VALUE_HISTOGRAMS (cfun))
537 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, visited_hists);
538 pointer_set_destroy (visited_hists);
539 if (error_found)
540 internal_error ("verify_histograms failed");
543 /* Helper function for verify_histograms. For each histogram reachable via htab
544 walk verify that it was reached via statement walk. */
546 static int
547 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
549 histogram_value hist = *(histogram_value *) slot;
550 free (hist->hvalue.counters);
551 #ifdef ENABLE_CHECKING
552 memset (hist, 0xab, sizeof (*hist));
553 #endif
554 free (hist);
555 return 1;
558 void
559 free_histograms (void)
561 if (VALUE_HISTOGRAMS (cfun))
563 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
564 htab_delete (VALUE_HISTOGRAMS (cfun));
565 VALUE_HISTOGRAMS (cfun) = NULL;
570 /* The overall number of invocations of the counter should match
571 execution count of basic block. Report it as error rather than
572 internal error as it might mean that user has misused the profile
573 somehow. */
575 static bool
576 check_counter (gimple stmt, const char * name,
577 gcov_type *count, gcov_type *all, gcov_type bb_count)
579 if (*all != bb_count || *count > *all)
581 location_t locus;
582 locus = (stmt != NULL)
583 ? gimple_location (stmt)
584 : DECL_SOURCE_LOCATION (current_function_decl);
585 if (flag_profile_correction)
587 if (dump_enabled_p ())
588 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
589 "correcting inconsistent value profile: %s "
590 "profiler overall count (%d) does not match BB "
591 "count (%d)\n", 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 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
810 tree_val = build_int_cst (get_gcov_type (), val);
811 else
813 HOST_WIDE_INT a[2];
814 a[0] = (unsigned HOST_WIDE_INT) val;
815 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
817 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
818 TYPE_PRECISION (get_gcov_type ()), false));
820 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
822 if (dump_file)
824 fprintf (dump_file, "Div/mod by constant ");
825 print_generic_expr (dump_file, value, TDF_SLIM);
826 fprintf (dump_file, "=");
827 print_generic_expr (dump_file, tree_val, TDF_SLIM);
828 fprintf (dump_file, " transformation on insn ");
829 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
832 gimple_assign_set_rhs_from_tree (si, result);
833 update_stmt (gsi_stmt (*si));
835 return true;
838 /* Generate code for transformation 2 (with parent gimple assign STMT and
839 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
840 within roundoff error). This generates the result into a temp and returns
841 the temp; it does not replace or alter the original STMT. */
842 static tree
843 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
845 gimple stmt1, stmt2, stmt3, stmt4;
846 tree tmp2, tmp3;
847 gimple bb1end, bb2end, bb3end;
848 basic_block bb, bb2, bb3, bb4;
849 tree optype, op1, op2;
850 edge e12, e13, e23, e24, e34;
851 gimple_stmt_iterator gsi;
852 tree result;
854 gcc_assert (is_gimple_assign (stmt)
855 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
857 optype = TREE_TYPE (gimple_assign_lhs (stmt));
858 op1 = gimple_assign_rhs1 (stmt);
859 op2 = gimple_assign_rhs2 (stmt);
861 bb = gimple_bb (stmt);
862 gsi = gsi_for_stmt (stmt);
864 result = create_tmp_reg (optype, "PROF");
865 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
866 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
867 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
868 build_int_cst (optype, -1));
869 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
870 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
871 NULL_TREE, NULL_TREE);
872 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
873 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
874 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
875 bb1end = stmt4;
877 /* tmp2 == op2-1 inherited from previous block. */
878 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
879 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
880 bb2end = stmt1;
882 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
883 op1, op2);
884 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
885 bb3end = stmt1;
887 /* Fix CFG. */
888 /* Edge e23 connects bb2 to bb3, etc. */
889 e12 = split_block (bb, bb1end);
890 bb2 = e12->dest;
891 bb2->count = count;
892 e23 = split_block (bb2, bb2end);
893 bb3 = e23->dest;
894 bb3->count = all - count;
895 e34 = split_block (bb3, bb3end);
896 bb4 = e34->dest;
897 bb4->count = all;
899 e12->flags &= ~EDGE_FALLTHRU;
900 e12->flags |= EDGE_FALSE_VALUE;
901 e12->probability = prob;
902 e12->count = count;
904 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
905 e13->probability = REG_BR_PROB_BASE - prob;
906 e13->count = all - count;
908 remove_edge (e23);
910 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
911 e24->probability = REG_BR_PROB_BASE;
912 e24->count = count;
914 e34->probability = REG_BR_PROB_BASE;
915 e34->count = all - count;
917 return result;
920 /* Do transform 2) on INSN if applicable. */
921 static bool
922 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
924 histogram_value histogram;
925 enum tree_code code;
926 gcov_type count, wrong_values, all;
927 tree lhs_type, result, value;
928 gcov_type prob;
929 gimple stmt;
931 stmt = gsi_stmt (*si);
932 if (gimple_code (stmt) != GIMPLE_ASSIGN)
933 return false;
935 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
936 if (!INTEGRAL_TYPE_P (lhs_type))
937 return false;
939 code = gimple_assign_rhs_code (stmt);
941 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
942 return false;
944 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
945 if (!histogram)
946 return false;
948 value = histogram->hvalue.value;
949 wrong_values = histogram->hvalue.counters[0];
950 count = histogram->hvalue.counters[1];
952 gimple_remove_histogram_value (cfun, stmt, histogram);
954 /* We require that we hit a power of 2 at least half of all evaluations. */
955 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
956 || count < wrong_values
957 || optimize_bb_for_size_p (gimple_bb (stmt)))
958 return false;
960 if (dump_file)
962 fprintf (dump_file, "Mod power of 2 transformation on insn ");
963 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
966 /* Compute probability of taking the optimal path. */
967 all = count + wrong_values;
969 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
970 return false;
972 if (all > 0)
973 prob = GCOV_COMPUTE_SCALE (count, all);
974 else
975 prob = 0;
977 result = gimple_mod_pow2 (stmt, prob, count, all);
979 gimple_assign_set_rhs_from_tree (si, result);
980 update_stmt (gsi_stmt (*si));
982 return true;
985 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
986 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
987 supported and this is built into this interface. The probabilities of taking
988 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
989 COUNT2/ALL respectively within roundoff error). This generates the
990 result into a temp and returns the temp; it does not replace or alter
991 the original STMT. */
992 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
994 static tree
995 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
996 gcov_type count1, gcov_type count2, gcov_type all)
998 gimple stmt1, stmt2, stmt3;
999 tree tmp1;
1000 gimple bb1end, bb2end = NULL, bb3end;
1001 basic_block bb, bb2, bb3, bb4;
1002 tree optype, op1, op2;
1003 edge e12, e23 = 0, e24, e34, e14;
1004 gimple_stmt_iterator gsi;
1005 tree result;
1007 gcc_assert (is_gimple_assign (stmt)
1008 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1010 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1011 op1 = gimple_assign_rhs1 (stmt);
1012 op2 = gimple_assign_rhs2 (stmt);
1014 bb = gimple_bb (stmt);
1015 gsi = gsi_for_stmt (stmt);
1017 result = create_tmp_reg (optype, "PROF");
1018 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1019 stmt1 = gimple_build_assign (result, op1);
1020 stmt2 = gimple_build_assign (tmp1, op2);
1021 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1022 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1023 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1024 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1025 bb1end = stmt3;
1027 if (ncounts) /* Assumed to be 0 or 1 */
1029 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
1030 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1031 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1032 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1033 bb2end = stmt2;
1036 /* Fallback case. */
1037 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
1038 result, tmp1);
1039 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1040 bb3end = stmt1;
1042 /* Fix CFG. */
1043 /* Edge e23 connects bb2 to bb3, etc. */
1044 /* However block 3 is optional; if it is not there, references
1045 to 3 really refer to block 2. */
1046 e12 = split_block (bb, bb1end);
1047 bb2 = e12->dest;
1048 bb2->count = all - count1;
1050 if (ncounts) /* Assumed to be 0 or 1. */
1052 e23 = split_block (bb2, bb2end);
1053 bb3 = e23->dest;
1054 bb3->count = all - count1 - count2;
1057 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1058 bb4 = e34->dest;
1059 bb4->count = all;
1061 e12->flags &= ~EDGE_FALLTHRU;
1062 e12->flags |= EDGE_FALSE_VALUE;
1063 e12->probability = REG_BR_PROB_BASE - prob1;
1064 e12->count = all - count1;
1066 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1067 e14->probability = prob1;
1068 e14->count = count1;
1070 if (ncounts) /* Assumed to be 0 or 1. */
1072 e23->flags &= ~EDGE_FALLTHRU;
1073 e23->flags |= EDGE_FALSE_VALUE;
1074 e23->count = all - count1 - count2;
1075 e23->probability = REG_BR_PROB_BASE - prob2;
1077 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1078 e24->probability = prob2;
1079 e24->count = count2;
1082 e34->probability = REG_BR_PROB_BASE;
1083 e34->count = all - count1 - count2;
1085 return result;
1089 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1091 static bool
1092 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1094 histogram_value histogram;
1095 enum tree_code code;
1096 gcov_type count, wrong_values, all;
1097 tree lhs_type, result;
1098 gcov_type prob1, prob2;
1099 unsigned int i, steps;
1100 gcov_type count1, count2;
1101 gimple stmt;
1103 stmt = gsi_stmt (*si);
1104 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1105 return false;
1107 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1108 if (!INTEGRAL_TYPE_P (lhs_type))
1109 return false;
1111 code = gimple_assign_rhs_code (stmt);
1113 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1114 return false;
1116 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1117 if (!histogram)
1118 return false;
1120 all = 0;
1121 wrong_values = 0;
1122 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1123 all += histogram->hvalue.counters[i];
1125 wrong_values += histogram->hvalue.counters[i];
1126 wrong_values += histogram->hvalue.counters[i+1];
1127 steps = histogram->hdata.intvl.steps;
1128 all += wrong_values;
1129 count1 = histogram->hvalue.counters[0];
1130 count2 = histogram->hvalue.counters[1];
1132 /* Compute probability of taking the optimal path. */
1133 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1135 gimple_remove_histogram_value (cfun, stmt, histogram);
1136 return false;
1139 if (flag_profile_correction && count1 + count2 > all)
1140 all = count1 + count2;
1142 gcc_assert (count1 + count2 <= all);
1144 /* We require that we use just subtractions in at least 50% of all
1145 evaluations. */
1146 count = 0;
1147 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1149 count += histogram->hvalue.counters[i];
1150 if (count * 2 >= all)
1151 break;
1153 if (i == steps
1154 || optimize_bb_for_size_p (gimple_bb (stmt)))
1155 return false;
1157 gimple_remove_histogram_value (cfun, stmt, histogram);
1158 if (dump_file)
1160 fprintf (dump_file, "Mod subtract transformation on insn ");
1161 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1164 /* Compute probability of taking the optimal path(s). */
1165 if (all > 0)
1167 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1168 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1170 else
1172 prob1 = prob2 = 0;
1175 /* In practice, "steps" is always 2. This interface reflects this,
1176 and will need to be changed if "steps" can change. */
1177 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1179 gimple_assign_set_rhs_from_tree (si, result);
1180 update_stmt (gsi_stmt (*si));
1182 return true;
1185 static pointer_map_t *cgraph_node_map;
1187 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1188 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1189 that the PROFILE_IDs was already assigned. */
1191 void
1192 init_node_map (bool local)
1194 struct cgraph_node *n;
1195 cgraph_node_map = pointer_map_create ();
1197 FOR_EACH_DEFINED_FUNCTION (n)
1198 if (cgraph_function_with_gimple_body_p (n)
1199 && !cgraph_only_called_directly_p (n))
1201 void **val;
1202 if (local)
1204 n->profile_id = coverage_compute_profile_id (n);
1205 while ((val = pointer_map_contains (cgraph_node_map,
1206 (void *)(size_t)n->profile_id))
1207 || !n->profile_id)
1209 if (dump_file)
1210 fprintf (dump_file, "Local profile-id %i conflict"
1211 " with nodes %s/%i %s/%i\n",
1212 n->profile_id,
1213 cgraph_node_name (n),
1214 n->symbol.order,
1215 symtab_node_name (*(symtab_node*)val),
1216 (*(symtab_node *)val)->symbol.order);
1217 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1220 else if (!n->profile_id)
1222 if (dump_file)
1223 fprintf (dump_file,
1224 "Node %s/%i has no profile-id"
1225 " (profile feedback missing?)\n",
1226 cgraph_node_name (n),
1227 n->symbol.order);
1228 continue;
1230 else if ((val = pointer_map_contains (cgraph_node_map,
1231 (void *)(size_t)n->profile_id)))
1233 if (dump_file)
1234 fprintf (dump_file,
1235 "Node %s/%i has IP profile-id %i conflict. "
1236 "Giving up.\n",
1237 cgraph_node_name (n),
1238 n->symbol.order,
1239 n->profile_id);
1240 *val = NULL;
1241 continue;
1243 *pointer_map_insert (cgraph_node_map,
1244 (void *)(size_t)n->profile_id) = (void *)n;
1248 /* Delete the CGRAPH_NODE_MAP. */
1250 void
1251 del_node_map (void)
1253 pointer_map_destroy (cgraph_node_map);
1256 /* Return cgraph node for function with pid */
1258 struct cgraph_node*
1259 find_func_by_profile_id (int profile_id)
1261 void **val = pointer_map_contains (cgraph_node_map,
1262 (void *)(size_t)profile_id);
1263 if (val)
1264 return (struct cgraph_node *)*val;
1265 else
1266 return NULL;
1269 /* Perform sanity check on the indirect call target. Due to race conditions,
1270 false function target may be attributed to an indirect call site. If the
1271 call expression type mismatches with the target function's type, expand_call
1272 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1273 Returns true if TARGET is considered ok for call CALL_STMT. */
1275 static bool
1276 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1278 location_t locus;
1279 if (gimple_check_call_matching_types (call_stmt, target->symbol.decl, true))
1280 return true;
1282 locus = gimple_location (call_stmt);
1283 if (dump_enabled_p ())
1284 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1285 "Skipping target %s with mismatching types for icall\n",
1286 cgraph_node_name (target));
1287 return false;
1290 /* Do transformation
1292 if (actual_callee_address == address_of_most_common_function/method)
1293 do direct call
1294 else
1295 old call
1298 gimple
1299 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1300 int prob, gcov_type count, gcov_type all)
1302 gimple dcall_stmt, load_stmt, cond_stmt;
1303 tree tmp0, tmp1, tmp;
1304 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1305 tree optype = build_pointer_type (void_type_node);
1306 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1307 gimple_stmt_iterator gsi;
1308 int lp_nr, dflags;
1309 edge e_eh, e;
1310 edge_iterator ei;
1311 gimple_stmt_iterator psi;
1313 cond_bb = gimple_bb (icall_stmt);
1314 gsi = gsi_for_stmt (icall_stmt);
1316 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1317 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1318 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1319 load_stmt = gimple_build_assign (tmp0, tmp);
1320 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1322 tmp = fold_convert (optype, build_addr (direct_call->symbol.decl,
1323 current_function_decl));
1324 load_stmt = gimple_build_assign (tmp1, tmp);
1325 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1327 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1328 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1330 gimple_set_vdef (icall_stmt, NULL_TREE);
1331 gimple_set_vuse (icall_stmt, NULL_TREE);
1332 update_stmt (icall_stmt);
1333 dcall_stmt = gimple_copy (icall_stmt);
1334 gimple_call_set_fndecl (dcall_stmt, direct_call->symbol.decl);
1335 dflags = flags_from_decl_or_type (direct_call->symbol.decl);
1336 if ((dflags & ECF_NORETURN) != 0)
1337 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1338 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1340 /* Fix CFG. */
1341 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1342 e_cd = split_block (cond_bb, cond_stmt);
1343 dcall_bb = e_cd->dest;
1344 dcall_bb->count = count;
1346 e_di = split_block (dcall_bb, dcall_stmt);
1347 icall_bb = e_di->dest;
1348 icall_bb->count = all - count;
1350 /* Do not disturb existing EH edges from the indirect call. */
1351 if (!stmt_ends_bb_p (icall_stmt))
1352 e_ij = split_block (icall_bb, icall_stmt);
1353 else
1355 e_ij = find_fallthru_edge (icall_bb->succs);
1356 /* The indirect call might be noreturn. */
1357 if (e_ij != NULL)
1359 e_ij->probability = REG_BR_PROB_BASE;
1360 e_ij->count = all - count;
1361 e_ij = single_pred_edge (split_edge (e_ij));
1364 if (e_ij != NULL)
1366 join_bb = e_ij->dest;
1367 join_bb->count = all;
1370 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1371 e_cd->probability = prob;
1372 e_cd->count = count;
1374 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1375 e_ci->probability = REG_BR_PROB_BASE - prob;
1376 e_ci->count = all - count;
1378 remove_edge (e_di);
1380 if (e_ij != NULL)
1382 if ((dflags & ECF_NORETURN) != 0)
1383 e_ij->count = all;
1384 else
1386 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1387 e_dj->probability = REG_BR_PROB_BASE;
1388 e_dj->count = count;
1390 e_ij->count = all - count;
1392 e_ij->probability = REG_BR_PROB_BASE;
1395 /* Insert PHI node for the call result if necessary. */
1396 if (gimple_call_lhs (icall_stmt)
1397 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1398 && (dflags & ECF_NORETURN) == 0)
1400 tree result = gimple_call_lhs (icall_stmt);
1401 gimple phi = create_phi_node (result, join_bb);
1402 gimple_call_set_lhs (icall_stmt,
1403 duplicate_ssa_name (result, icall_stmt));
1404 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1405 gimple_call_set_lhs (dcall_stmt,
1406 duplicate_ssa_name (result, dcall_stmt));
1407 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1410 /* Build an EH edge for the direct call if necessary. */
1411 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1412 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1414 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1417 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1418 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1420 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1421 for (psi = gsi_start_phis (e_eh->dest);
1422 !gsi_end_p (psi); gsi_next (&psi))
1424 gimple phi = gsi_stmt (psi);
1425 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1426 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1429 return dcall_stmt;
1433 For every checked indirect/virtual call determine if most common pid of
1434 function/class method has probability more than 50%. If yes modify code of
1435 this call to:
1438 static bool
1439 gimple_ic_transform (gimple_stmt_iterator *gsi)
1441 gimple stmt = gsi_stmt (*gsi);
1442 histogram_value histogram;
1443 gcov_type val, count, all, bb_all;
1444 struct cgraph_node *direct_call;
1446 if (gimple_code (stmt) != GIMPLE_CALL)
1447 return false;
1449 if (gimple_call_fndecl (stmt) != NULL_TREE)
1450 return false;
1452 if (gimple_call_internal_p (stmt))
1453 return false;
1455 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1456 if (!histogram)
1457 return false;
1459 val = histogram->hvalue.counters [0];
1460 count = histogram->hvalue.counters [1];
1461 all = histogram->hvalue.counters [2];
1463 bb_all = gimple_bb (stmt)->count;
1464 /* The order of CHECK_COUNTER calls is important -
1465 since check_counter can correct the third parameter
1466 and we want to make count <= all <= bb_all. */
1467 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1468 || check_counter (stmt, "ic", &count, &all, all))
1470 gimple_remove_histogram_value (cfun, stmt, histogram);
1471 return false;
1474 if (4 * count <= 3 * all)
1475 return false;
1477 direct_call = find_func_by_profile_id ((int)val);
1479 if (direct_call == NULL)
1481 if (val)
1483 if (dump_file)
1485 fprintf (dump_file, "Indirect call -> direct call from other module");
1486 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1487 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1490 return false;
1493 if (!check_ic_target (stmt, direct_call))
1495 if (dump_file)
1497 fprintf (dump_file, "Indirect call -> direct call ");
1498 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1499 fprintf (dump_file, "=> ");
1500 print_generic_expr (dump_file, direct_call->symbol.decl, TDF_SLIM);
1501 fprintf (dump_file, " transformation skipped because of type mismatch");
1502 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1504 gimple_remove_histogram_value (cfun, stmt, histogram);
1505 return false;
1508 if (dump_file)
1510 fprintf (dump_file, "Indirect call -> direct call ");
1511 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1512 fprintf (dump_file, "=> ");
1513 print_generic_expr (dump_file, direct_call->symbol.decl, TDF_SLIM);
1514 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1515 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1516 fprintf (dump_file, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1517 " hist->all "HOST_WIDEST_INT_PRINT_DEC"\n", count, all);
1520 return true;
1523 /* Return true if the stringop CALL with FNDECL shall be profiled.
1524 SIZE_ARG be set to the argument index for the size of the string
1525 operation.
1527 static bool
1528 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1530 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1532 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1533 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1534 return false;
1536 switch (fcode)
1538 case BUILT_IN_MEMCPY:
1539 case BUILT_IN_MEMPCPY:
1540 *size_arg = 2;
1541 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1542 INTEGER_TYPE, VOID_TYPE);
1543 case BUILT_IN_MEMSET:
1544 *size_arg = 2;
1545 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1546 INTEGER_TYPE, VOID_TYPE);
1547 case BUILT_IN_BZERO:
1548 *size_arg = 1;
1549 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1550 VOID_TYPE);
1551 default:
1552 gcc_unreachable ();
1556 /* Convert stringop (..., vcall_size)
1557 into
1558 if (vcall_size == icall_size)
1559 stringop (..., icall_size);
1560 else
1561 stringop (..., vcall_size);
1562 assuming we'll propagate a true constant into ICALL_SIZE later. */
1564 static void
1565 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1566 gcov_type count, gcov_type all)
1568 gimple tmp_stmt, cond_stmt, icall_stmt;
1569 tree tmp0, tmp1, vcall_size, optype;
1570 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1571 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1572 gimple_stmt_iterator gsi;
1573 tree fndecl;
1574 int size_arg;
1576 fndecl = gimple_call_fndecl (vcall_stmt);
1577 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1578 gcc_unreachable ();
1580 cond_bb = gimple_bb (vcall_stmt);
1581 gsi = gsi_for_stmt (vcall_stmt);
1583 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1584 optype = TREE_TYPE (vcall_size);
1586 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1587 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1588 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1589 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1591 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1592 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1594 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1595 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1597 gimple_set_vdef (vcall_stmt, NULL);
1598 gimple_set_vuse (vcall_stmt, NULL);
1599 update_stmt (vcall_stmt);
1600 icall_stmt = gimple_copy (vcall_stmt);
1601 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1602 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1604 /* Fix CFG. */
1605 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1606 e_ci = split_block (cond_bb, cond_stmt);
1607 icall_bb = e_ci->dest;
1608 icall_bb->count = count;
1610 e_iv = split_block (icall_bb, icall_stmt);
1611 vcall_bb = e_iv->dest;
1612 vcall_bb->count = all - count;
1614 e_vj = split_block (vcall_bb, vcall_stmt);
1615 join_bb = e_vj->dest;
1616 join_bb->count = all;
1618 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1619 e_ci->probability = prob;
1620 e_ci->count = count;
1622 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1623 e_cv->probability = REG_BR_PROB_BASE - prob;
1624 e_cv->count = all - count;
1626 remove_edge (e_iv);
1628 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1629 e_ij->probability = REG_BR_PROB_BASE;
1630 e_ij->count = count;
1632 e_vj->probability = REG_BR_PROB_BASE;
1633 e_vj->count = all - count;
1635 /* Insert PHI node for the call result if necessary. */
1636 if (gimple_call_lhs (vcall_stmt)
1637 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1639 tree result = gimple_call_lhs (vcall_stmt);
1640 gimple phi = create_phi_node (result, join_bb);
1641 gimple_call_set_lhs (vcall_stmt,
1642 duplicate_ssa_name (result, vcall_stmt));
1643 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1644 gimple_call_set_lhs (icall_stmt,
1645 duplicate_ssa_name (result, icall_stmt));
1646 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1649 /* Because these are all string op builtins, they're all nothrow. */
1650 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1651 gcc_assert (!stmt_could_throw_p (icall_stmt));
1654 /* Find values inside STMT for that we want to measure histograms for
1655 division/modulo optimization. */
1656 static bool
1657 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1659 gimple stmt = gsi_stmt (*gsi);
1660 tree fndecl;
1661 tree blck_size;
1662 enum built_in_function fcode;
1663 histogram_value histogram;
1664 gcov_type count, all, val;
1665 tree dest, src;
1666 unsigned int dest_align, src_align;
1667 gcov_type prob;
1668 tree tree_val;
1669 int size_arg;
1671 if (gimple_code (stmt) != GIMPLE_CALL)
1672 return false;
1673 fndecl = gimple_call_fndecl (stmt);
1674 if (!fndecl)
1675 return false;
1676 fcode = DECL_FUNCTION_CODE (fndecl);
1677 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1678 return false;
1680 blck_size = gimple_call_arg (stmt, size_arg);
1681 if (TREE_CODE (blck_size) == INTEGER_CST)
1682 return false;
1684 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1685 if (!histogram)
1686 return false;
1687 val = histogram->hvalue.counters[0];
1688 count = histogram->hvalue.counters[1];
1689 all = histogram->hvalue.counters[2];
1690 gimple_remove_histogram_value (cfun, stmt, histogram);
1691 /* We require that count is at least half of all; this means
1692 that for the transformation to fire the value must be constant
1693 at least 80% of time. */
1694 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1695 return false;
1696 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1697 return false;
1698 if (all > 0)
1699 prob = GCOV_COMPUTE_SCALE (count, all);
1700 else
1701 prob = 0;
1702 dest = gimple_call_arg (stmt, 0);
1703 dest_align = get_pointer_alignment (dest);
1704 switch (fcode)
1706 case BUILT_IN_MEMCPY:
1707 case BUILT_IN_MEMPCPY:
1708 src = gimple_call_arg (stmt, 1);
1709 src_align = get_pointer_alignment (src);
1710 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1711 return false;
1712 break;
1713 case BUILT_IN_MEMSET:
1714 if (!can_store_by_pieces (val, builtin_memset_read_str,
1715 gimple_call_arg (stmt, 1),
1716 dest_align, true))
1717 return false;
1718 break;
1719 case BUILT_IN_BZERO:
1720 if (!can_store_by_pieces (val, builtin_memset_read_str,
1721 integer_zero_node,
1722 dest_align, true))
1723 return false;
1724 break;
1725 default:
1726 gcc_unreachable ();
1728 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1729 tree_val = build_int_cst (get_gcov_type (), val);
1730 else
1732 HOST_WIDE_INT a[2];
1733 a[0] = (unsigned HOST_WIDE_INT) val;
1734 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1736 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1737 TYPE_PRECISION (get_gcov_type ()), false));
1740 if (dump_file)
1742 fprintf (dump_file, "Single value %i stringop transformation on ",
1743 (int)val);
1744 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1746 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1748 return true;
1751 void
1752 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1753 HOST_WIDE_INT *expected_size)
1755 histogram_value histogram;
1756 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1757 if (!histogram)
1758 *expected_size = -1;
1759 else if (!histogram->hvalue.counters[1])
1761 *expected_size = -1;
1762 gimple_remove_histogram_value (cfun, stmt, histogram);
1764 else
1766 gcov_type size;
1767 size = ((histogram->hvalue.counters[0]
1768 + histogram->hvalue.counters[1] / 2)
1769 / histogram->hvalue.counters[1]);
1770 /* Even if we can hold bigger value in SIZE, INT_MAX
1771 is safe "infinity" for code generation strategies. */
1772 if (size > INT_MAX)
1773 size = INT_MAX;
1774 *expected_size = size;
1775 gimple_remove_histogram_value (cfun, stmt, histogram);
1777 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1778 if (!histogram)
1779 *expected_align = 0;
1780 else if (!histogram->hvalue.counters[0])
1782 gimple_remove_histogram_value (cfun, stmt, histogram);
1783 *expected_align = 0;
1785 else
1787 gcov_type count;
1788 int alignment;
1790 count = histogram->hvalue.counters[0];
1791 alignment = 1;
1792 while (!(count & alignment)
1793 && (alignment * 2 * BITS_PER_UNIT))
1794 alignment <<= 1;
1795 *expected_align = alignment * BITS_PER_UNIT;
1796 gimple_remove_histogram_value (cfun, stmt, histogram);
1801 /* Find values inside STMT for that we want to measure histograms for
1802 division/modulo optimization. */
1803 static void
1804 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1806 tree lhs, divisor, op0, type;
1807 histogram_value hist;
1809 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1810 return;
1812 lhs = gimple_assign_lhs (stmt);
1813 type = TREE_TYPE (lhs);
1814 if (!INTEGRAL_TYPE_P (type))
1815 return;
1817 switch (gimple_assign_rhs_code (stmt))
1819 case TRUNC_DIV_EXPR:
1820 case TRUNC_MOD_EXPR:
1821 divisor = gimple_assign_rhs2 (stmt);
1822 op0 = gimple_assign_rhs1 (stmt);
1824 values->reserve (3);
1826 if (TREE_CODE (divisor) == SSA_NAME)
1827 /* Check for the case where the divisor is the same value most
1828 of the time. */
1829 values->quick_push (gimple_alloc_histogram_value (cfun,
1830 HIST_TYPE_SINGLE_VALUE,
1831 stmt, divisor));
1833 /* For mod, check whether it is not often a noop (or replaceable by
1834 a few subtractions). */
1835 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1836 && TYPE_UNSIGNED (type))
1838 tree val;
1839 /* Check for a special case where the divisor is power of 2. */
1840 values->quick_push (gimple_alloc_histogram_value (cfun,
1841 HIST_TYPE_POW2,
1842 stmt, divisor));
1844 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1845 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1846 stmt, val);
1847 hist->hdata.intvl.int_start = 0;
1848 hist->hdata.intvl.steps = 2;
1849 values->quick_push (hist);
1851 return;
1853 default:
1854 return;
1858 /* Find calls inside STMT for that we want to measure histograms for
1859 indirect/virtual call optimization. */
1861 static void
1862 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1864 tree callee;
1866 if (gimple_code (stmt) != GIMPLE_CALL
1867 || gimple_call_internal_p (stmt)
1868 || gimple_call_fndecl (stmt) != NULL_TREE)
1869 return;
1871 callee = gimple_call_fn (stmt);
1873 values->reserve (3);
1875 values->quick_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_INDIR_CALL,
1876 stmt, callee));
1878 return;
1881 /* Find values inside STMT for that we want to measure histograms for
1882 string operations. */
1883 static void
1884 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1886 tree fndecl;
1887 tree blck_size;
1888 tree dest;
1889 int size_arg;
1891 if (gimple_code (stmt) != GIMPLE_CALL)
1892 return;
1893 fndecl = gimple_call_fndecl (stmt);
1894 if (!fndecl)
1895 return;
1897 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1898 return;
1900 dest = gimple_call_arg (stmt, 0);
1901 blck_size = gimple_call_arg (stmt, size_arg);
1903 if (TREE_CODE (blck_size) != INTEGER_CST)
1905 values->safe_push (gimple_alloc_histogram_value (cfun,
1906 HIST_TYPE_SINGLE_VALUE,
1907 stmt, blck_size));
1908 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1909 stmt, blck_size));
1911 if (TREE_CODE (blck_size) != INTEGER_CST)
1912 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1913 stmt, dest));
1916 /* Find values inside STMT for that we want to measure histograms and adds
1917 them to list VALUES. */
1919 static void
1920 gimple_values_to_profile (gimple stmt, histogram_values *values)
1922 gimple_divmod_values_to_profile (stmt, values);
1923 gimple_stringops_values_to_profile (stmt, values);
1924 gimple_indirect_call_to_profile (stmt, values);
1927 void
1928 gimple_find_values_to_profile (histogram_values *values)
1930 basic_block bb;
1931 gimple_stmt_iterator gsi;
1932 unsigned i;
1933 histogram_value hist = NULL;
1935 values->create (0);
1936 FOR_EACH_BB (bb)
1937 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1938 gimple_values_to_profile (gsi_stmt (gsi), values);
1940 FOR_EACH_VEC_ELT (*values, i, hist)
1942 switch (hist->type)
1944 case HIST_TYPE_INTERVAL:
1945 hist->n_counters = hist->hdata.intvl.steps + 2;
1946 break;
1948 case HIST_TYPE_POW2:
1949 hist->n_counters = 2;
1950 break;
1952 case HIST_TYPE_SINGLE_VALUE:
1953 hist->n_counters = 3;
1954 break;
1956 case HIST_TYPE_CONST_DELTA:
1957 hist->n_counters = 4;
1958 break;
1960 case HIST_TYPE_INDIR_CALL:
1961 hist->n_counters = 3;
1962 break;
1964 case HIST_TYPE_AVERAGE:
1965 hist->n_counters = 2;
1966 break;
1968 case HIST_TYPE_IOR:
1969 hist->n_counters = 1;
1970 break;
1972 default:
1973 gcc_unreachable ();
1975 if (dump_file)
1977 fprintf (dump_file, "Stmt ");
1978 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
1979 dump_histogram_value (dump_file, hist);