LWG 3035. std::allocator's constructors should be constexpr
[official-gcc.git] / gcc / value-prof.c
blobd50a179b27d55f5f5494fb2b0e71f9ae3187eae8
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2018 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 "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "cfghooks.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "coverage.h"
31 #include "data-streamer.h"
32 #include "diagnostic.h"
33 #include "fold-const.h"
34 #include "tree-nested.h"
35 #include "calls.h"
36 #include "expr.h"
37 #include "value-prof.h"
38 #include "tree-eh.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "tree-cfg.h"
42 #include "gimple-pretty-print.h"
43 #include "dumpfile.h"
44 #include "builtins.h"
45 #include "params.h"
47 /* In this file value profile based optimizations are placed. Currently the
48 following optimizations are implemented (for more detailed descriptions
49 see comments at value_profile_transformations):
51 1) Division/modulo specialization. Provided that we can determine that the
52 operands of the division have some special properties, we may use it to
53 produce more effective code.
55 2) Indirect/virtual call specialization. If we can determine most
56 common function callee in indirect/virtual call. We can use this
57 information to improve code effectiveness (especially info for
58 the inliner).
60 3) Speculative prefetching. If we are able to determine that the difference
61 between addresses accessed by a memory reference is usually constant, we
62 may add the prefetch instructions.
63 FIXME: This transformation was removed together with RTL based value
64 profiling.
67 Value profiling internals
68 ==========================
70 Every value profiling transformation starts with defining what values
71 to profile. There are different histogram types (see HIST_TYPE_* in
72 value-prof.h) and each transformation can request one or more histogram
73 types per GIMPLE statement. The function gimple_find_values_to_profile()
74 collects the values to profile in a vec, and adds the number of counters
75 required for the different histogram types.
77 For a -fprofile-generate run, the statements for which values should be
78 recorded, are instrumented in instrument_values(). The instrumentation
79 is done by helper functions that can be found in tree-profile.c, where
80 new types of histograms can be added if necessary.
82 After a -fprofile-use, the value profiling data is read back in by
83 compute_value_histograms() that translates the collected data to
84 histograms and attaches them to the profiled statements via
85 gimple_add_histogram_value(). Histograms are stored in a hash table
86 that is attached to every intrumented function, see VALUE_HISTOGRAMS
87 in function.h.
89 The value-profile transformations driver is the function
90 gimple_value_profile_transformations(). It traverses all statements in
91 the to-be-transformed function, and looks for statements with one or
92 more histograms attached to it. If a statement has histograms, the
93 transformation functions are called on the statement.
95 Limitations / FIXME / TODO:
96 * Only one histogram of each type can be associated with a statement.
97 * Some value profile transformations are done in builtins.c (?!)
98 * Updating of histograms needs some TLC.
99 * The value profiling code could be used to record analysis results
100 from non-profiling (e.g. VRP).
101 * Adding new profilers should be simplified, starting with a cleanup
102 of what-happens-where and with making gimple_find_values_to_profile
103 and gimple_value_profile_transformations table-driven, perhaps...
106 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
107 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
108 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
109 static bool gimple_stringops_transform (gimple_stmt_iterator *);
110 static bool gimple_ic_transform (gimple_stmt_iterator *);
112 /* Allocate histogram value. */
114 histogram_value
115 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
116 enum hist_type type, gimple *stmt, tree value)
118 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
119 hist->hvalue.value = value;
120 hist->hvalue.stmt = stmt;
121 hist->type = type;
122 return hist;
125 /* Hash value for histogram. */
127 static hashval_t
128 histogram_hash (const void *x)
130 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
133 /* Return nonzero if statement for histogram_value X is Y. */
135 static int
136 histogram_eq (const void *x, const void *y)
138 return ((const_histogram_value) x)->hvalue.stmt == (const gimple *) y;
141 /* Set histogram for STMT. */
143 static void
144 set_histogram_value (struct function *fun, gimple *stmt, histogram_value hist)
146 void **loc;
147 if (!hist && !VALUE_HISTOGRAMS (fun))
148 return;
149 if (!VALUE_HISTOGRAMS (fun))
150 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
151 histogram_eq, NULL);
152 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
153 htab_hash_pointer (stmt),
154 hist ? INSERT : NO_INSERT);
155 if (!hist)
157 if (loc)
158 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
159 return;
161 *loc = hist;
164 /* Get histogram list for STMT. */
166 histogram_value
167 gimple_histogram_value (struct function *fun, gimple *stmt)
169 if (!VALUE_HISTOGRAMS (fun))
170 return NULL;
171 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
172 htab_hash_pointer (stmt));
175 /* Add histogram for STMT. */
177 void
178 gimple_add_histogram_value (struct function *fun, gimple *stmt,
179 histogram_value hist)
181 hist->hvalue.next = gimple_histogram_value (fun, stmt);
182 set_histogram_value (fun, stmt, hist);
183 hist->fun = fun;
186 /* Remove histogram HIST from STMT's histogram list. */
188 void
189 gimple_remove_histogram_value (struct function *fun, gimple *stmt,
190 histogram_value hist)
192 histogram_value hist2 = gimple_histogram_value (fun, stmt);
193 if (hist == hist2)
195 set_histogram_value (fun, stmt, hist->hvalue.next);
197 else
199 while (hist2->hvalue.next != hist)
200 hist2 = hist2->hvalue.next;
201 hist2->hvalue.next = hist->hvalue.next;
203 free (hist->hvalue.counters);
204 if (flag_checking)
205 memset (hist, 0xab, sizeof (*hist));
206 free (hist);
209 /* Lookup histogram of type TYPE in the STMT. */
211 histogram_value
212 gimple_histogram_value_of_type (struct function *fun, gimple *stmt,
213 enum hist_type type)
215 histogram_value hist;
216 for (hist = gimple_histogram_value (fun, stmt); hist;
217 hist = hist->hvalue.next)
218 if (hist->type == type)
219 return hist;
220 return NULL;
223 /* Dump information about HIST to DUMP_FILE. */
225 static void
226 dump_histogram_value (FILE *dump_file, histogram_value hist)
228 switch (hist->type)
230 case HIST_TYPE_INTERVAL:
231 fprintf (dump_file, "Interval counter range %d -- %d",
232 hist->hdata.intvl.int_start,
233 (hist->hdata.intvl.int_start
234 + hist->hdata.intvl.steps - 1));
235 if (hist->hvalue.counters)
237 unsigned int i;
238 fprintf (dump_file, " [");
239 for (i = 0; i < hist->hdata.intvl.steps; i++)
240 fprintf (dump_file, " %d:%" PRId64,
241 hist->hdata.intvl.int_start + i,
242 (int64_t) hist->hvalue.counters[i]);
243 fprintf (dump_file, " ] outside range:%" PRId64,
244 (int64_t) hist->hvalue.counters[i]);
246 fprintf (dump_file, ".\n");
247 break;
249 case HIST_TYPE_POW2:
250 fprintf (dump_file, "Pow2 counter ");
251 if (hist->hvalue.counters)
253 fprintf (dump_file, "pow2:%" PRId64
254 " nonpow2:%" PRId64,
255 (int64_t) hist->hvalue.counters[1],
256 (int64_t) hist->hvalue.counters[0]);
258 fprintf (dump_file, ".\n");
259 break;
261 case HIST_TYPE_SINGLE_VALUE:
262 fprintf (dump_file, "Single value ");
263 if (hist->hvalue.counters)
265 fprintf (dump_file, "value:%" PRId64
266 " match:%" PRId64
267 " wrong:%" PRId64,
268 (int64_t) hist->hvalue.counters[0],
269 (int64_t) hist->hvalue.counters[1],
270 (int64_t) hist->hvalue.counters[2]);
272 fprintf (dump_file, ".\n");
273 break;
275 case HIST_TYPE_AVERAGE:
276 fprintf (dump_file, "Average value ");
277 if (hist->hvalue.counters)
279 fprintf (dump_file, "sum:%" PRId64
280 " times:%" PRId64,
281 (int64_t) hist->hvalue.counters[0],
282 (int64_t) hist->hvalue.counters[1]);
284 fprintf (dump_file, ".\n");
285 break;
287 case HIST_TYPE_IOR:
288 fprintf (dump_file, "IOR value ");
289 if (hist->hvalue.counters)
291 fprintf (dump_file, "ior:%" PRId64,
292 (int64_t) hist->hvalue.counters[0]);
294 fprintf (dump_file, ".\n");
295 break;
297 case HIST_TYPE_INDIR_CALL:
298 fprintf (dump_file, "Indirect call ");
299 if (hist->hvalue.counters)
301 fprintf (dump_file, "value:%" PRId64
302 " match:%" PRId64
303 " all:%" PRId64,
304 (int64_t) hist->hvalue.counters[0],
305 (int64_t) hist->hvalue.counters[1],
306 (int64_t) hist->hvalue.counters[2]);
308 fprintf (dump_file, ".\n");
309 break;
310 case HIST_TYPE_TIME_PROFILE:
311 fprintf (dump_file, "Time profile ");
312 if (hist->hvalue.counters)
314 fprintf (dump_file, "time:%" PRId64,
315 (int64_t) hist->hvalue.counters[0]);
317 fprintf (dump_file, ".\n");
318 break;
319 case HIST_TYPE_INDIR_CALL_TOPN:
320 fprintf (dump_file, "Indirect call topn ");
321 if (hist->hvalue.counters)
323 int i;
325 fprintf (dump_file, "accu:%" PRId64, hist->hvalue.counters[0]);
326 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
328 fprintf (dump_file, " target:%" PRId64 " value:%" PRId64,
329 (int64_t) hist->hvalue.counters[i],
330 (int64_t) hist->hvalue.counters[i+1]);
333 fprintf (dump_file, ".\n");
334 break;
335 case HIST_TYPE_MAX:
336 gcc_unreachable ();
340 /* Dump information about HIST to DUMP_FILE. */
342 void
343 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
345 struct bitpack_d bp;
346 unsigned int i;
348 bp = bitpack_create (ob->main_stream);
349 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
350 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
351 streamer_write_bitpack (&bp);
352 switch (hist->type)
354 case HIST_TYPE_INTERVAL:
355 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
356 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
357 break;
358 default:
359 break;
361 for (i = 0; i < hist->n_counters; i++)
363 /* When user uses an unsigned type with a big value, constant converted
364 to gcov_type (a signed type) can be negative. */
365 gcov_type value = hist->hvalue.counters[i];
366 if (hist->type == HIST_TYPE_SINGLE_VALUE && i == 0)
368 else
369 gcc_assert (value >= 0);
371 streamer_write_gcov_count (ob, value);
373 if (hist->hvalue.next)
374 stream_out_histogram_value (ob, hist->hvalue.next);
377 /* Dump information about HIST to DUMP_FILE. */
379 void
380 stream_in_histogram_value (struct lto_input_block *ib, gimple *stmt)
382 enum hist_type type;
383 unsigned int ncounters = 0;
384 struct bitpack_d bp;
385 unsigned int i;
386 histogram_value new_val;
387 bool next;
388 histogram_value *next_p = NULL;
392 bp = streamer_read_bitpack (ib);
393 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
394 next = bp_unpack_value (&bp, 1);
395 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
396 switch (type)
398 case HIST_TYPE_INTERVAL:
399 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
400 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
401 ncounters = new_val->hdata.intvl.steps + 2;
402 break;
404 case HIST_TYPE_POW2:
405 case HIST_TYPE_AVERAGE:
406 ncounters = 2;
407 break;
409 case HIST_TYPE_SINGLE_VALUE:
410 case HIST_TYPE_INDIR_CALL:
411 ncounters = 3;
412 break;
414 case HIST_TYPE_IOR:
415 case HIST_TYPE_TIME_PROFILE:
416 ncounters = 1;
417 break;
419 case HIST_TYPE_INDIR_CALL_TOPN:
420 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
421 break;
423 case HIST_TYPE_MAX:
424 gcc_unreachable ();
426 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
427 new_val->n_counters = ncounters;
428 for (i = 0; i < ncounters; i++)
429 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
430 if (!next_p)
431 gimple_add_histogram_value (cfun, stmt, new_val);
432 else
433 *next_p = new_val;
434 next_p = &new_val->hvalue.next;
436 while (next);
439 /* Dump all histograms attached to STMT to DUMP_FILE. */
441 void
442 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple *stmt)
444 histogram_value hist;
445 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
446 dump_histogram_value (dump_file, hist);
449 /* Remove all histograms associated with STMT. */
451 void
452 gimple_remove_stmt_histograms (struct function *fun, gimple *stmt)
454 histogram_value val;
455 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
456 gimple_remove_histogram_value (fun, stmt, val);
459 /* Duplicate all histograms associates with OSTMT to STMT. */
461 void
462 gimple_duplicate_stmt_histograms (struct function *fun, gimple *stmt,
463 struct function *ofun, gimple *ostmt)
465 histogram_value val;
466 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
468 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
469 memcpy (new_val, val, sizeof (*val));
470 new_val->hvalue.stmt = stmt;
471 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
472 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
473 gimple_add_histogram_value (fun, stmt, new_val);
477 /* Move all histograms associated with OSTMT to STMT. */
479 void
480 gimple_move_stmt_histograms (struct function *fun, gimple *stmt, gimple *ostmt)
482 histogram_value val = gimple_histogram_value (fun, ostmt);
483 if (val)
485 /* The following three statements can't be reordered,
486 because histogram hashtab relies on stmt field value
487 for finding the exact slot. */
488 set_histogram_value (fun, ostmt, NULL);
489 for (; val != NULL; val = val->hvalue.next)
490 val->hvalue.stmt = stmt;
491 set_histogram_value (fun, stmt, val);
495 static bool error_found = false;
497 /* Helper function for verify_histograms. For each histogram reachable via htab
498 walk verify that it was reached via statement walk. */
500 static int
501 visit_hist (void **slot, void *data)
503 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
504 histogram_value hist = *(histogram_value *) slot;
506 if (!visited->contains (hist)
507 && hist->type != HIST_TYPE_TIME_PROFILE)
509 error ("dead histogram");
510 dump_histogram_value (stderr, hist);
511 debug_gimple_stmt (hist->hvalue.stmt);
512 error_found = true;
514 return 1;
517 /* Verify sanity of the histograms. */
519 DEBUG_FUNCTION void
520 verify_histograms (void)
522 basic_block bb;
523 gimple_stmt_iterator gsi;
524 histogram_value hist;
526 error_found = false;
527 hash_set<histogram_value> visited_hists;
528 FOR_EACH_BB_FN (bb, cfun)
529 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
531 gimple *stmt = gsi_stmt (gsi);
533 for (hist = gimple_histogram_value (cfun, stmt); hist;
534 hist = hist->hvalue.next)
536 if (hist->hvalue.stmt != stmt)
538 error ("Histogram value statement does not correspond to "
539 "the statement it is associated with");
540 debug_gimple_stmt (stmt);
541 dump_histogram_value (stderr, hist);
542 error_found = true;
544 visited_hists.add (hist);
547 if (VALUE_HISTOGRAMS (cfun))
548 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
549 if (error_found)
550 internal_error ("verify_histograms failed");
553 /* Helper function for verify_histograms. For each histogram reachable via htab
554 walk verify that it was reached via statement walk. */
556 static int
557 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
559 histogram_value hist = *(histogram_value *) slot;
560 free (hist->hvalue.counters);
561 free (hist);
562 return 1;
565 void
566 free_histograms (struct function *fn)
568 if (VALUE_HISTOGRAMS (fn))
570 htab_traverse (VALUE_HISTOGRAMS (fn), free_hist, NULL);
571 htab_delete (VALUE_HISTOGRAMS (fn));
572 VALUE_HISTOGRAMS (fn) = NULL;
576 /* The overall number of invocations of the counter should match
577 execution count of basic block. Report it as error rather than
578 internal error as it might mean that user has misused the profile
579 somehow. */
581 static bool
582 check_counter (gimple *stmt, const char * name,
583 gcov_type *count, gcov_type *all, profile_count bb_count_d)
585 gcov_type bb_count = bb_count_d.ipa ().to_gcov_type ();
586 if (*all != bb_count || *count > *all)
588 location_t locus;
589 locus = (stmt != NULL)
590 ? gimple_location (stmt)
591 : DECL_SOURCE_LOCATION (current_function_decl);
592 if (flag_profile_correction)
594 if (dump_enabled_p ())
595 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
596 "correcting inconsistent value profile: %s "
597 "profiler overall count (%d) does not match BB "
598 "count (%d)\n", name, (int)*all, (int)bb_count);
599 *all = bb_count;
600 if (*count > *all)
601 *count = *all;
602 return false;
604 else
606 error_at (locus, "corrupted value profile: %s "
607 "profile counter (%d out of %d) inconsistent with "
608 "basic-block count (%d)",
609 name,
610 (int) *count,
611 (int) *all,
612 (int) bb_count);
613 return true;
617 return false;
620 /* GIMPLE based transformations. */
622 bool
623 gimple_value_profile_transformations (void)
625 basic_block bb;
626 gimple_stmt_iterator gsi;
627 bool changed = false;
629 /* Autofdo does its own transformations for indirect calls,
630 and otherwise does not support value profiling. */
631 if (flag_auto_profile)
632 return false;
634 FOR_EACH_BB_FN (bb, cfun)
636 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
638 gimple *stmt = gsi_stmt (gsi);
639 histogram_value th = gimple_histogram_value (cfun, stmt);
640 if (!th)
641 continue;
643 if (dump_file)
645 fprintf (dump_file, "Trying transformations on stmt ");
646 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
647 dump_histograms_for_stmt (cfun, dump_file, stmt);
650 /* Transformations: */
651 /* The order of things in this conditional controls which
652 transformation is used when more than one is applicable. */
653 /* It is expected that any code added by the transformations
654 will be added before the current statement, and that the
655 current statement remain valid (although possibly
656 modified) upon return. */
657 if (gimple_mod_subtract_transform (&gsi)
658 || gimple_divmod_fixed_value_transform (&gsi)
659 || gimple_mod_pow2_value_transform (&gsi)
660 || gimple_stringops_transform (&gsi)
661 || gimple_ic_transform (&gsi))
663 stmt = gsi_stmt (gsi);
664 changed = true;
665 /* Original statement may no longer be in the same block. */
666 if (bb != gimple_bb (stmt))
668 bb = gimple_bb (stmt);
669 gsi = gsi_for_stmt (stmt);
675 return changed;
678 /* Generate code for transformation 1 (with parent gimple assignment
679 STMT and probability of taking the optimal path PROB, which is
680 equivalent to COUNT/ALL within roundoff error). This generates the
681 result into a temp and returns the temp; it does not replace or
682 alter the original STMT. */
684 static tree
685 gimple_divmod_fixed_value (gassign *stmt, tree value, profile_probability prob,
686 gcov_type count, gcov_type all)
688 gassign *stmt1, *stmt2;
689 gcond *stmt3;
690 tree tmp0, tmp1, tmp2;
691 gimple *bb1end, *bb2end, *bb3end;
692 basic_block bb, bb2, bb3, bb4;
693 tree optype, op1, op2;
694 edge e12, e13, e23, e24, e34;
695 gimple_stmt_iterator gsi;
697 gcc_assert (is_gimple_assign (stmt)
698 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
699 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
701 optype = TREE_TYPE (gimple_assign_lhs (stmt));
702 op1 = gimple_assign_rhs1 (stmt);
703 op2 = gimple_assign_rhs2 (stmt);
705 bb = gimple_bb (stmt);
706 gsi = gsi_for_stmt (stmt);
708 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
709 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
710 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
711 stmt2 = gimple_build_assign (tmp1, op2);
712 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
713 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
714 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
715 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
716 bb1end = stmt3;
718 tmp2 = create_tmp_reg (optype, "PROF");
719 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
720 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
721 bb2end = stmt1;
723 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
724 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
725 bb3end = stmt1;
727 /* Fix CFG. */
728 /* Edge e23 connects bb2 to bb3, etc. */
729 e12 = split_block (bb, bb1end);
730 bb2 = e12->dest;
731 bb2->count = profile_count::from_gcov_type (count);
732 e23 = split_block (bb2, bb2end);
733 bb3 = e23->dest;
734 bb3->count = profile_count::from_gcov_type (all - count);
735 e34 = split_block (bb3, bb3end);
736 bb4 = e34->dest;
737 bb4->count = profile_count::from_gcov_type (all);
739 e12->flags &= ~EDGE_FALLTHRU;
740 e12->flags |= EDGE_FALSE_VALUE;
741 e12->probability = prob;
743 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
744 e13->probability = prob.invert ();
746 remove_edge (e23);
748 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
749 e24->probability = profile_probability::always ();
751 e34->probability = profile_probability::always ();
753 return tmp2;
756 /* Do transform 1) on INSN if applicable. */
758 static bool
759 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
761 histogram_value histogram;
762 enum tree_code code;
763 gcov_type val, count, all;
764 tree result, value, tree_val;
765 profile_probability prob;
766 gassign *stmt;
768 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
769 if (!stmt)
770 return false;
772 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
773 return false;
775 code = gimple_assign_rhs_code (stmt);
777 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
778 return false;
780 histogram = gimple_histogram_value_of_type (cfun, stmt,
781 HIST_TYPE_SINGLE_VALUE);
782 if (!histogram)
783 return false;
785 value = histogram->hvalue.value;
786 val = histogram->hvalue.counters[0];
787 count = histogram->hvalue.counters[1];
788 all = histogram->hvalue.counters[2];
789 gimple_remove_histogram_value (cfun, stmt, histogram);
791 /* We require that count is at least half of all; this means
792 that for the transformation to fire the value must be constant
793 at least 50% of time (and 75% gives the guarantee of usage). */
794 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
795 || 2 * count < all
796 || optimize_bb_for_size_p (gimple_bb (stmt)))
797 return false;
799 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
800 return false;
802 /* Compute probability of taking the optimal path. */
803 if (all > 0)
804 prob = profile_probability::probability_in_gcov_type (count, all);
805 else
806 prob = profile_probability::never ();
808 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
809 tree_val = build_int_cst (get_gcov_type (), val);
810 else
812 HOST_WIDE_INT a[2];
813 a[0] = (unsigned HOST_WIDE_INT) val;
814 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
816 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
817 TYPE_PRECISION (get_gcov_type ()), false));
819 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
821 if (dump_file)
823 fprintf (dump_file, "Div/mod by constant ");
824 print_generic_expr (dump_file, value, TDF_SLIM);
825 fprintf (dump_file, "=");
826 print_generic_expr (dump_file, tree_val, TDF_SLIM);
827 fprintf (dump_file, " transformation on insn ");
828 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
831 gimple_assign_set_rhs_from_tree (si, result);
832 update_stmt (gsi_stmt (*si));
834 return true;
837 /* Generate code for transformation 2 (with parent gimple assign STMT and
838 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
839 within roundoff error). This generates the result into a temp and returns
840 the temp; it does not replace or alter the original STMT. */
842 static tree
843 gimple_mod_pow2 (gassign *stmt, profile_probability prob, gcov_type count, gcov_type all)
845 gassign *stmt1, *stmt2, *stmt3;
846 gcond *stmt4;
847 tree tmp2, tmp3;
848 gimple *bb1end, *bb2end, *bb3end;
849 basic_block bb, bb2, bb3, bb4;
850 tree optype, op1, op2;
851 edge e12, e13, e23, e24, e34;
852 gimple_stmt_iterator gsi;
853 tree result;
855 gcc_assert (is_gimple_assign (stmt)
856 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
858 optype = TREE_TYPE (gimple_assign_lhs (stmt));
859 op1 = gimple_assign_rhs1 (stmt);
860 op2 = gimple_assign_rhs2 (stmt);
862 bb = gimple_bb (stmt);
863 gsi = gsi_for_stmt (stmt);
865 result = create_tmp_reg (optype, "PROF");
866 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
867 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
868 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
869 build_int_cst (optype, -1));
870 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
871 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
872 NULL_TREE, NULL_TREE);
873 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
874 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
875 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
876 bb1end = stmt4;
878 /* tmp2 == op2-1 inherited from previous block. */
879 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
880 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
881 bb2end = stmt1;
883 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
884 op1, op2);
885 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
886 bb3end = stmt1;
888 /* Fix CFG. */
889 /* Edge e23 connects bb2 to bb3, etc. */
890 e12 = split_block (bb, bb1end);
891 bb2 = e12->dest;
892 bb2->count = profile_count::from_gcov_type (count);
893 e23 = split_block (bb2, bb2end);
894 bb3 = e23->dest;
895 bb3->count = profile_count::from_gcov_type (all - count);
896 e34 = split_block (bb3, bb3end);
897 bb4 = e34->dest;
898 bb4->count = profile_count::from_gcov_type (all);
900 e12->flags &= ~EDGE_FALLTHRU;
901 e12->flags |= EDGE_FALSE_VALUE;
902 e12->probability = prob;
904 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
905 e13->probability = prob.invert ();
907 remove_edge (e23);
909 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
910 e24->probability = profile_probability::always ();
912 e34->probability = profile_probability::always ();
914 return result;
917 /* Do transform 2) on INSN if applicable. */
919 static bool
920 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
922 histogram_value histogram;
923 enum tree_code code;
924 gcov_type count, wrong_values, all;
925 tree lhs_type, result, value;
926 profile_probability prob;
927 gassign *stmt;
929 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
930 if (!stmt)
931 return false;
933 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
934 if (!INTEGRAL_TYPE_P (lhs_type))
935 return false;
937 code = gimple_assign_rhs_code (stmt);
939 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
940 return false;
942 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
943 if (!histogram)
944 return false;
946 value = histogram->hvalue.value;
947 wrong_values = histogram->hvalue.counters[0];
948 count = histogram->hvalue.counters[1];
950 gimple_remove_histogram_value (cfun, stmt, histogram);
952 /* We require that we hit a power of 2 at least half of all evaluations. */
953 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
954 || count < wrong_values
955 || optimize_bb_for_size_p (gimple_bb (stmt)))
956 return false;
958 if (dump_file)
960 fprintf (dump_file, "Mod power of 2 transformation on insn ");
961 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
964 /* Compute probability of taking the optimal path. */
965 all = count + wrong_values;
967 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
968 return false;
970 if (all > 0)
971 prob = profile_probability::probability_in_gcov_type (count, all);
972 else
973 prob = profile_probability::never ();
975 result = gimple_mod_pow2 (stmt, prob, count, all);
977 gimple_assign_set_rhs_from_tree (si, result);
978 update_stmt (gsi_stmt (*si));
980 return true;
983 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
984 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
985 supported and this is built into this interface. The probabilities of taking
986 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
987 COUNT2/ALL respectively within roundoff error). This generates the
988 result into a temp and returns the temp; it does not replace or alter
989 the original STMT. */
990 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
992 static tree
993 gimple_mod_subtract (gassign *stmt, profile_probability prob1,
994 profile_probability prob2, int ncounts,
995 gcov_type count1, gcov_type count2, gcov_type all)
997 gassign *stmt1;
998 gimple *stmt2;
999 gcond *stmt3;
1000 tree tmp1;
1001 gimple *bb1end, *bb2end = NULL, *bb3end;
1002 basic_block bb, bb2, bb3, bb4;
1003 tree optype, op1, op2;
1004 edge e12, e23 = 0, e24, e34, e14;
1005 gimple_stmt_iterator gsi;
1006 tree result;
1008 gcc_assert (is_gimple_assign (stmt)
1009 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1011 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1012 op1 = gimple_assign_rhs1 (stmt);
1013 op2 = gimple_assign_rhs2 (stmt);
1015 bb = gimple_bb (stmt);
1016 gsi = gsi_for_stmt (stmt);
1018 result = create_tmp_reg (optype, "PROF");
1019 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1020 stmt1 = gimple_build_assign (result, op1);
1021 stmt2 = gimple_build_assign (tmp1, op2);
1022 stmt3 = 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 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1026 bb1end = stmt3;
1028 if (ncounts) /* Assumed to be 0 or 1 */
1030 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
1031 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1032 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1033 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1034 bb2end = stmt2;
1037 /* Fallback case. */
1038 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1039 result, tmp1);
1040 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1041 bb3end = stmt1;
1043 /* Fix CFG. */
1044 /* Edge e23 connects bb2 to bb3, etc. */
1045 /* However block 3 is optional; if it is not there, references
1046 to 3 really refer to block 2. */
1047 e12 = split_block (bb, bb1end);
1048 bb2 = e12->dest;
1049 bb2->count = profile_count::from_gcov_type (all - count1);
1051 if (ncounts) /* Assumed to be 0 or 1. */
1053 e23 = split_block (bb2, bb2end);
1054 bb3 = e23->dest;
1055 bb3->count = profile_count::from_gcov_type (all - count1 - count2);
1058 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1059 bb4 = e34->dest;
1060 bb4->count = profile_count::from_gcov_type (all);
1062 e12->flags &= ~EDGE_FALLTHRU;
1063 e12->flags |= EDGE_FALSE_VALUE;
1064 e12->probability = prob1.invert ();
1066 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1067 e14->probability = prob1;
1069 if (ncounts) /* Assumed to be 0 or 1. */
1071 e23->flags &= ~EDGE_FALLTHRU;
1072 e23->flags |= EDGE_FALSE_VALUE;
1073 e23->probability = prob2.invert ();
1075 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1076 e24->probability = prob2;
1079 e34->probability = profile_probability::always ();
1081 return result;
1084 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1086 static bool
1087 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1089 histogram_value histogram;
1090 enum tree_code code;
1091 gcov_type count, wrong_values, all;
1092 tree lhs_type, result;
1093 profile_probability prob1, prob2;
1094 unsigned int i, steps;
1095 gcov_type count1, count2;
1096 gassign *stmt;
1097 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1098 if (!stmt)
1099 return false;
1101 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1102 if (!INTEGRAL_TYPE_P (lhs_type))
1103 return false;
1105 code = gimple_assign_rhs_code (stmt);
1107 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1108 return false;
1110 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1111 if (!histogram)
1112 return false;
1114 all = 0;
1115 wrong_values = 0;
1116 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1117 all += histogram->hvalue.counters[i];
1119 wrong_values += histogram->hvalue.counters[i];
1120 wrong_values += histogram->hvalue.counters[i+1];
1121 steps = histogram->hdata.intvl.steps;
1122 all += wrong_values;
1123 count1 = histogram->hvalue.counters[0];
1124 count2 = histogram->hvalue.counters[1];
1126 /* Compute probability of taking the optimal path. */
1127 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1129 gimple_remove_histogram_value (cfun, stmt, histogram);
1130 return false;
1133 if (flag_profile_correction && count1 + count2 > all)
1134 all = count1 + count2;
1136 gcc_assert (count1 + count2 <= all);
1138 /* We require that we use just subtractions in at least 50% of all
1139 evaluations. */
1140 count = 0;
1141 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1143 count += histogram->hvalue.counters[i];
1144 if (count * 2 >= all)
1145 break;
1147 if (i == steps
1148 || optimize_bb_for_size_p (gimple_bb (stmt)))
1149 return false;
1151 gimple_remove_histogram_value (cfun, stmt, histogram);
1152 if (dump_file)
1154 fprintf (dump_file, "Mod subtract transformation on insn ");
1155 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1158 /* Compute probability of taking the optimal path(s). */
1159 if (all > 0)
1161 prob1 = profile_probability::probability_in_gcov_type (count1, all);
1162 prob2 = profile_probability::probability_in_gcov_type (count2, all);
1164 else
1166 prob1 = prob2 = profile_probability::never ();
1169 /* In practice, "steps" is always 2. This interface reflects this,
1170 and will need to be changed if "steps" can change. */
1171 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1173 gimple_assign_set_rhs_from_tree (si, result);
1174 update_stmt (gsi_stmt (*si));
1176 return true;
1179 typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
1181 static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
1183 /* Returns true if node graph is initialized. This
1184 is used to test if profile_id has been created
1185 for cgraph_nodes. */
1187 bool
1188 coverage_node_map_initialized_p (void)
1190 return cgraph_node_map != 0;
1193 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1194 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1195 that the PROFILE_IDs was already assigned. */
1197 void
1198 init_node_map (bool local)
1200 struct cgraph_node *n;
1201 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
1203 FOR_EACH_DEFINED_FUNCTION (n)
1204 if (n->has_gimple_body_p ())
1206 cgraph_node **val;
1207 if (local)
1209 n->profile_id = coverage_compute_profile_id (n);
1210 while ((val = cgraph_node_map->get (n->profile_id))
1211 || !n->profile_id)
1213 if (dump_file)
1214 fprintf (dump_file, "Local profile-id %i conflict"
1215 " with nodes %s %s\n",
1216 n->profile_id,
1217 n->dump_name (),
1218 (*val)->dump_name ());
1219 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1222 else if (!n->profile_id)
1224 if (dump_file)
1225 fprintf (dump_file,
1226 "Node %s has no profile-id"
1227 " (profile feedback missing?)\n",
1228 n->dump_name ());
1229 continue;
1231 else if ((val = cgraph_node_map->get (n->profile_id)))
1233 if (dump_file)
1234 fprintf (dump_file,
1235 "Node %s has IP profile-id %i conflict. "
1236 "Giving up.\n",
1237 n->dump_name (), n->profile_id);
1238 *val = NULL;
1239 continue;
1241 cgraph_node_map->put (n->profile_id, n);
1245 /* Delete the CGRAPH_NODE_MAP. */
1247 void
1248 del_node_map (void)
1250 delete cgraph_node_map;
1253 /* Return cgraph node for function with pid */
1255 struct cgraph_node*
1256 find_func_by_profile_id (int profile_id)
1258 cgraph_node **val = cgraph_node_map->get (profile_id);
1259 if (val)
1260 return *val;
1261 else
1262 return NULL;
1265 /* Perform sanity check on the indirect call target. Due to race conditions,
1266 false function target may be attributed to an indirect call site. If the
1267 call expression type mismatches with the target function's type, expand_call
1268 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1269 Returns true if TARGET is considered ok for call CALL_STMT. */
1271 bool
1272 check_ic_target (gcall *call_stmt, struct cgraph_node *target)
1274 location_t locus;
1275 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1276 return true;
1278 locus = gimple_location (call_stmt);
1279 if (dump_enabled_p ())
1280 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1281 "Skipping target %s with mismatching types for icall\n",
1282 target->name ());
1283 return false;
1286 /* Do transformation
1288 if (actual_callee_address == address_of_most_common_function/method)
1289 do direct call
1290 else
1291 old call
1294 gcall *
1295 gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
1296 profile_probability prob)
1298 gcall *dcall_stmt;
1299 gassign *load_stmt;
1300 gcond *cond_stmt;
1301 tree tmp0, tmp1, tmp;
1302 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1303 tree optype = build_pointer_type (void_type_node);
1304 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1305 gimple_stmt_iterator gsi;
1306 int lp_nr, dflags;
1307 edge e_eh, e;
1308 edge_iterator ei;
1310 cond_bb = gimple_bb (icall_stmt);
1311 gsi = gsi_for_stmt (icall_stmt);
1313 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1314 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1315 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1316 load_stmt = gimple_build_assign (tmp0, tmp);
1317 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1319 tmp = fold_convert (optype, build_addr (direct_call->decl));
1320 load_stmt = gimple_build_assign (tmp1, tmp);
1321 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1323 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1324 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1326 if (TREE_CODE (gimple_vdef (icall_stmt)) == SSA_NAME)
1328 unlink_stmt_vdef (icall_stmt);
1329 release_ssa_name (gimple_vdef (icall_stmt));
1331 gimple_set_vdef (icall_stmt, NULL_TREE);
1332 gimple_set_vuse (icall_stmt, NULL_TREE);
1333 update_stmt (icall_stmt);
1334 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
1335 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1336 dflags = flags_from_decl_or_type (direct_call->decl);
1337 if ((dflags & ECF_NORETURN) != 0
1338 && should_remove_lhs_p (gimple_call_lhs (dcall_stmt)))
1339 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1340 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1342 /* Fix CFG. */
1343 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1344 e_cd = split_block (cond_bb, cond_stmt);
1345 dcall_bb = e_cd->dest;
1346 dcall_bb->count = cond_bb->count.apply_probability (prob);
1348 e_di = split_block (dcall_bb, dcall_stmt);
1349 icall_bb = e_di->dest;
1350 icall_bb->count = cond_bb->count - dcall_bb->count;
1352 /* Do not disturb existing EH edges from the indirect call. */
1353 if (!stmt_ends_bb_p (icall_stmt))
1354 e_ij = split_block (icall_bb, icall_stmt);
1355 else
1357 e_ij = find_fallthru_edge (icall_bb->succs);
1358 /* The indirect call might be noreturn. */
1359 if (e_ij != NULL)
1361 e_ij->probability = profile_probability::always ();
1362 e_ij = single_pred_edge (split_edge (e_ij));
1365 if (e_ij != NULL)
1367 join_bb = e_ij->dest;
1368 join_bb->count = cond_bb->count;
1371 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1372 e_cd->probability = prob;
1374 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1375 e_ci->probability = prob.invert ();
1377 remove_edge (e_di);
1379 if (e_ij != NULL)
1381 if ((dflags & ECF_NORETURN) == 0)
1383 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1384 e_dj->probability = profile_probability::always ();
1386 e_ij->probability = profile_probability::always ();
1389 /* Insert PHI node for the call result if necessary. */
1390 if (gimple_call_lhs (icall_stmt)
1391 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1392 && (dflags & ECF_NORETURN) == 0)
1394 tree result = gimple_call_lhs (icall_stmt);
1395 gphi *phi = create_phi_node (result, join_bb);
1396 gimple_call_set_lhs (icall_stmt,
1397 duplicate_ssa_name (result, icall_stmt));
1398 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1399 gimple_call_set_lhs (dcall_stmt,
1400 duplicate_ssa_name (result, dcall_stmt));
1401 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1404 /* Build an EH edge for the direct call if necessary. */
1405 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1406 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1408 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1411 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1412 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1414 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1415 e->probability = e_eh->probability;
1416 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
1417 !gsi_end_p (psi); gsi_next (&psi))
1419 gphi *phi = psi.phi ();
1420 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1421 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1424 if (!stmt_could_throw_p (dcall_stmt))
1425 gimple_purge_dead_eh_edges (dcall_bb);
1426 return dcall_stmt;
1430 For every checked indirect/virtual call determine if most common pid of
1431 function/class method has probability more than 50%. If yes modify code of
1432 this call to:
1435 static bool
1436 gimple_ic_transform (gimple_stmt_iterator *gsi)
1438 gcall *stmt;
1439 histogram_value histogram;
1440 gcov_type val, count, all, bb_all;
1441 struct cgraph_node *direct_call;
1443 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1444 if (!stmt)
1445 return false;
1447 if (gimple_call_fndecl (stmt) != NULL_TREE)
1448 return false;
1450 if (gimple_call_internal_p (stmt))
1451 return false;
1453 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1454 if (!histogram)
1455 return false;
1457 val = histogram->hvalue.counters [0];
1458 count = histogram->hvalue.counters [1];
1459 all = histogram->hvalue.counters [2];
1461 bb_all = gimple_bb (stmt)->count.ipa ().to_gcov_type ();
1462 /* The order of CHECK_COUNTER calls is important -
1463 since check_counter can correct the third parameter
1464 and we want to make count <= all <= bb_all. */
1465 if (check_counter (stmt, "ic", &all, &bb_all, gimple_bb (stmt)->count)
1466 || check_counter (stmt, "ic", &count, &all,
1467 profile_count::from_gcov_type (all)))
1469 gimple_remove_histogram_value (cfun, stmt, histogram);
1470 return false;
1473 if (4 * count <= 3 * all)
1474 return false;
1476 direct_call = find_func_by_profile_id ((int)val);
1478 if (direct_call == NULL)
1480 if (val)
1482 if (dump_file)
1484 fprintf (dump_file, "Indirect call -> direct call from other module");
1485 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1486 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1489 return false;
1492 if (!check_ic_target (stmt, direct_call))
1494 if (dump_file)
1496 fprintf (dump_file, "Indirect call -> direct call ");
1497 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1498 fprintf (dump_file, "=> ");
1499 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1500 fprintf (dump_file, " transformation skipped because of type mismatch");
1501 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1503 gimple_remove_histogram_value (cfun, stmt, histogram);
1504 return false;
1507 if (dump_file)
1509 fprintf (dump_file, "Indirect call -> direct call ");
1510 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1511 fprintf (dump_file, "=> ");
1512 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1513 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1514 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1515 fprintf (dump_file, "hist->count %" PRId64
1516 " hist->all %" PRId64"\n", count, all);
1519 return true;
1522 /* Return true if the stringop CALL shall be profiled. SIZE_ARG be
1523 set to the argument index for the size of the string operation. */
1525 static bool
1526 interesting_stringop_to_profile_p (gcall *call, int *size_arg)
1528 enum built_in_function fcode;
1530 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (call));
1531 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1532 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1533 return false;
1535 switch (fcode)
1537 case BUILT_IN_MEMCPY:
1538 case BUILT_IN_MEMPCPY:
1539 *size_arg = 2;
1540 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1541 INTEGER_TYPE, VOID_TYPE);
1542 case BUILT_IN_MEMSET:
1543 *size_arg = 2;
1544 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1545 INTEGER_TYPE, VOID_TYPE);
1546 case BUILT_IN_BZERO:
1547 *size_arg = 1;
1548 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1549 VOID_TYPE);
1550 default:
1551 gcc_unreachable ();
1555 /* Convert stringop (..., vcall_size)
1556 into
1557 if (vcall_size == icall_size)
1558 stringop (..., icall_size);
1559 else
1560 stringop (..., vcall_size);
1561 assuming we'll propagate a true constant into ICALL_SIZE later. */
1563 static void
1564 gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, profile_probability prob,
1565 gcov_type count, gcov_type all)
1567 gassign *tmp_stmt;
1568 gcond *cond_stmt;
1569 gcall *icall_stmt;
1570 tree tmp0, tmp1, vcall_size, optype;
1571 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1572 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1573 gimple_stmt_iterator gsi;
1574 int size_arg;
1576 if (!interesting_stringop_to_profile_p (vcall_stmt, &size_arg))
1577 gcc_unreachable ();
1579 cond_bb = gimple_bb (vcall_stmt);
1580 gsi = gsi_for_stmt (vcall_stmt);
1582 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1583 optype = TREE_TYPE (vcall_size);
1585 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1586 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1587 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1588 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1590 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1591 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1593 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1594 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1596 if (TREE_CODE (gimple_vdef (vcall_stmt)) == SSA_NAME)
1598 unlink_stmt_vdef (vcall_stmt);
1599 release_ssa_name (gimple_vdef (vcall_stmt));
1601 gimple_set_vdef (vcall_stmt, NULL);
1602 gimple_set_vuse (vcall_stmt, NULL);
1603 update_stmt (vcall_stmt);
1604 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
1605 gimple_call_set_arg (icall_stmt, size_arg,
1606 fold_convert (optype, icall_size));
1607 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1609 /* Fix CFG. */
1610 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1611 e_ci = split_block (cond_bb, cond_stmt);
1612 icall_bb = e_ci->dest;
1613 icall_bb->count = profile_count::from_gcov_type (count);
1615 e_iv = split_block (icall_bb, icall_stmt);
1616 vcall_bb = e_iv->dest;
1617 vcall_bb->count = profile_count::from_gcov_type (all - count);
1619 e_vj = split_block (vcall_bb, vcall_stmt);
1620 join_bb = e_vj->dest;
1621 join_bb->count = profile_count::from_gcov_type (all);
1623 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1624 e_ci->probability = prob;
1626 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1627 e_cv->probability = prob.invert ();
1629 remove_edge (e_iv);
1631 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1632 e_ij->probability = profile_probability::always ();
1634 e_vj->probability = profile_probability::always ();
1636 /* Insert PHI node for the call result if necessary. */
1637 if (gimple_call_lhs (vcall_stmt)
1638 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1640 tree result = gimple_call_lhs (vcall_stmt);
1641 gphi *phi = create_phi_node (result, join_bb);
1642 gimple_call_set_lhs (vcall_stmt,
1643 duplicate_ssa_name (result, vcall_stmt));
1644 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1645 gimple_call_set_lhs (icall_stmt,
1646 duplicate_ssa_name (result, icall_stmt));
1647 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1650 /* Because these are all string op builtins, they're all nothrow. */
1651 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1652 gcc_assert (!stmt_could_throw_p (icall_stmt));
1655 /* Find values inside STMT for that we want to measure histograms for
1656 division/modulo optimization. */
1658 static bool
1659 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1661 gcall *stmt;
1662 tree blck_size;
1663 enum built_in_function fcode;
1664 histogram_value histogram;
1665 gcov_type count, all, val;
1666 tree dest, src;
1667 unsigned int dest_align, src_align;
1668 profile_probability prob;
1669 tree tree_val;
1670 int size_arg;
1672 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1673 if (!stmt)
1674 return false;
1676 if (!gimple_call_builtin_p (gsi_stmt (*gsi), BUILT_IN_NORMAL))
1677 return false;
1679 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
1680 return false;
1682 blck_size = gimple_call_arg (stmt, size_arg);
1683 if (TREE_CODE (blck_size) == INTEGER_CST)
1684 return false;
1686 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1687 if (!histogram)
1688 return false;
1690 val = histogram->hvalue.counters[0];
1691 count = histogram->hvalue.counters[1];
1692 all = histogram->hvalue.counters[2];
1693 gimple_remove_histogram_value (cfun, stmt, histogram);
1695 /* We require that count is at least half of all; this means
1696 that for the transformation to fire the value must be constant
1697 at least 80% of time. */
1698 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1699 return false;
1700 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1701 return false;
1702 if (all > 0)
1703 prob = profile_probability::probability_in_gcov_type (count, all);
1704 else
1705 prob = profile_probability::never ();
1707 dest = gimple_call_arg (stmt, 0);
1708 dest_align = get_pointer_alignment (dest);
1709 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (stmt));
1710 switch (fcode)
1712 case BUILT_IN_MEMCPY:
1713 case BUILT_IN_MEMPCPY:
1714 src = gimple_call_arg (stmt, 1);
1715 src_align = get_pointer_alignment (src);
1716 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1717 return false;
1718 break;
1719 case BUILT_IN_MEMSET:
1720 if (!can_store_by_pieces (val, builtin_memset_read_str,
1721 gimple_call_arg (stmt, 1),
1722 dest_align, true))
1723 return false;
1724 break;
1725 case BUILT_IN_BZERO:
1726 if (!can_store_by_pieces (val, builtin_memset_read_str,
1727 integer_zero_node,
1728 dest_align, true))
1729 return false;
1730 break;
1731 default:
1732 gcc_unreachable ();
1735 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1736 tree_val = build_int_cst (get_gcov_type (), val);
1737 else
1739 HOST_WIDE_INT a[2];
1740 a[0] = (unsigned HOST_WIDE_INT) val;
1741 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1743 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1744 TYPE_PRECISION (get_gcov_type ()), false));
1747 if (dump_file)
1749 fprintf (dump_file, "Single value %i stringop transformation on ",
1750 (int)val);
1751 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1754 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1756 return true;
1759 void
1760 stringop_block_profile (gimple *stmt, unsigned int *expected_align,
1761 HOST_WIDE_INT *expected_size)
1763 histogram_value histogram;
1764 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1766 if (!histogram)
1767 *expected_size = -1;
1768 else if (!histogram->hvalue.counters[1])
1770 *expected_size = -1;
1771 gimple_remove_histogram_value (cfun, stmt, histogram);
1773 else
1775 gcov_type size;
1776 size = ((histogram->hvalue.counters[0]
1777 + histogram->hvalue.counters[1] / 2)
1778 / histogram->hvalue.counters[1]);
1779 /* Even if we can hold bigger value in SIZE, INT_MAX
1780 is safe "infinity" for code generation strategies. */
1781 if (size > INT_MAX)
1782 size = INT_MAX;
1783 *expected_size = size;
1784 gimple_remove_histogram_value (cfun, stmt, histogram);
1787 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1789 if (!histogram)
1790 *expected_align = 0;
1791 else if (!histogram->hvalue.counters[0])
1793 gimple_remove_histogram_value (cfun, stmt, histogram);
1794 *expected_align = 0;
1796 else
1798 gcov_type count;
1799 unsigned int alignment;
1801 count = histogram->hvalue.counters[0];
1802 alignment = 1;
1803 while (!(count & alignment)
1804 && (alignment <= UINT_MAX / 2 / BITS_PER_UNIT))
1805 alignment <<= 1;
1806 *expected_align = alignment * BITS_PER_UNIT;
1807 gimple_remove_histogram_value (cfun, stmt, histogram);
1812 /* Find values inside STMT for that we want to measure histograms for
1813 division/modulo optimization. */
1815 static void
1816 gimple_divmod_values_to_profile (gimple *stmt, histogram_values *values)
1818 tree lhs, divisor, op0, type;
1819 histogram_value hist;
1821 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1822 return;
1824 lhs = gimple_assign_lhs (stmt);
1825 type = TREE_TYPE (lhs);
1826 if (!INTEGRAL_TYPE_P (type))
1827 return;
1829 switch (gimple_assign_rhs_code (stmt))
1831 case TRUNC_DIV_EXPR:
1832 case TRUNC_MOD_EXPR:
1833 divisor = gimple_assign_rhs2 (stmt);
1834 op0 = gimple_assign_rhs1 (stmt);
1836 values->reserve (3);
1838 if (TREE_CODE (divisor) == SSA_NAME)
1839 /* Check for the case where the divisor is the same value most
1840 of the time. */
1841 values->quick_push (gimple_alloc_histogram_value (cfun,
1842 HIST_TYPE_SINGLE_VALUE,
1843 stmt, divisor));
1845 /* For mod, check whether it is not often a noop (or replaceable by
1846 a few subtractions). */
1847 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1848 && TYPE_UNSIGNED (type)
1849 && TREE_CODE (divisor) == SSA_NAME)
1851 tree val;
1852 /* Check for a special case where the divisor is power of 2. */
1853 values->quick_push (gimple_alloc_histogram_value (cfun,
1854 HIST_TYPE_POW2,
1855 stmt, divisor));
1857 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1858 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1859 stmt, val);
1860 hist->hdata.intvl.int_start = 0;
1861 hist->hdata.intvl.steps = 2;
1862 values->quick_push (hist);
1864 return;
1866 default:
1867 return;
1871 /* Find calls inside STMT for that we want to measure histograms for
1872 indirect/virtual call optimization. */
1874 static void
1875 gimple_indirect_call_to_profile (gimple *stmt, histogram_values *values)
1877 tree callee;
1879 if (gimple_code (stmt) != GIMPLE_CALL
1880 || gimple_call_internal_p (stmt)
1881 || gimple_call_fndecl (stmt) != NULL_TREE)
1882 return;
1884 callee = gimple_call_fn (stmt);
1886 values->reserve (3);
1888 values->quick_push (gimple_alloc_histogram_value (
1889 cfun,
1890 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
1891 HIST_TYPE_INDIR_CALL_TOPN :
1892 HIST_TYPE_INDIR_CALL,
1893 stmt, callee));
1895 return;
1898 /* Find values inside STMT for that we want to measure histograms for
1899 string operations. */
1901 static void
1902 gimple_stringops_values_to_profile (gimple *gs, histogram_values *values)
1904 gcall *stmt;
1905 tree blck_size;
1906 tree dest;
1907 int size_arg;
1909 stmt = dyn_cast <gcall *> (gs);
1910 if (!stmt)
1911 return;
1913 if (!gimple_call_builtin_p (gs, BUILT_IN_NORMAL))
1914 return;
1916 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
1917 return;
1919 dest = gimple_call_arg (stmt, 0);
1920 blck_size = gimple_call_arg (stmt, size_arg);
1922 if (TREE_CODE (blck_size) != INTEGER_CST)
1924 values->safe_push (gimple_alloc_histogram_value (cfun,
1925 HIST_TYPE_SINGLE_VALUE,
1926 stmt, blck_size));
1927 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1928 stmt, blck_size));
1931 if (TREE_CODE (blck_size) != INTEGER_CST)
1932 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1933 stmt, dest));
1936 /* Find values inside STMT for that we want to measure histograms and adds
1937 them to list VALUES. */
1939 static void
1940 gimple_values_to_profile (gimple *stmt, histogram_values *values)
1942 gimple_divmod_values_to_profile (stmt, values);
1943 gimple_stringops_values_to_profile (stmt, values);
1944 gimple_indirect_call_to_profile (stmt, values);
1947 void
1948 gimple_find_values_to_profile (histogram_values *values)
1950 basic_block bb;
1951 gimple_stmt_iterator gsi;
1952 unsigned i;
1953 histogram_value hist = NULL;
1954 values->create (0);
1956 FOR_EACH_BB_FN (bb, cfun)
1957 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1958 gimple_values_to_profile (gsi_stmt (gsi), values);
1960 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
1962 FOR_EACH_VEC_ELT (*values, i, hist)
1964 switch (hist->type)
1966 case HIST_TYPE_INTERVAL:
1967 hist->n_counters = hist->hdata.intvl.steps + 2;
1968 break;
1970 case HIST_TYPE_POW2:
1971 hist->n_counters = 2;
1972 break;
1974 case HIST_TYPE_SINGLE_VALUE:
1975 hist->n_counters = 3;
1976 break;
1978 case HIST_TYPE_INDIR_CALL:
1979 hist->n_counters = 3;
1980 break;
1982 case HIST_TYPE_TIME_PROFILE:
1983 hist->n_counters = 1;
1984 break;
1986 case HIST_TYPE_AVERAGE:
1987 hist->n_counters = 2;
1988 break;
1990 case HIST_TYPE_IOR:
1991 hist->n_counters = 1;
1992 break;
1994 case HIST_TYPE_INDIR_CALL_TOPN:
1995 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
1996 break;
1998 default:
1999 gcc_unreachable ();
2001 if (dump_file && hist->hvalue.stmt != NULL)
2003 fprintf (dump_file, "Stmt ");
2004 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2005 dump_histogram_value (dump_file, hist);