1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2019 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
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
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/>. */
22 #include "coretypes.h"
31 #include "data-streamer.h"
32 #include "diagnostic.h"
33 #include "fold-const.h"
34 #include "tree-nested.h"
37 #include "value-prof.h"
40 #include "gimple-iterator.h"
42 #include "gimple-pretty-print.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
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
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
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. */
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
;
125 /* Hash value for histogram. */
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. */
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. */
144 set_histogram_value (struct function
*fun
, gimple
*stmt
, histogram_value hist
)
147 if (!hist
&& !VALUE_HISTOGRAMS (fun
))
149 if (!VALUE_HISTOGRAMS (fun
))
150 VALUE_HISTOGRAMS (fun
) = htab_create (1, histogram_hash
,
152 loc
= htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
153 htab_hash_pointer (stmt
),
154 hist
? INSERT
: NO_INSERT
);
158 htab_clear_slot (VALUE_HISTOGRAMS (fun
), loc
);
164 /* Get histogram list for STMT. */
167 gimple_histogram_value (struct function
*fun
, gimple
*stmt
)
169 if (!VALUE_HISTOGRAMS (fun
))
171 return (histogram_value
) htab_find_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
172 htab_hash_pointer (stmt
));
175 /* Add histogram for STMT. */
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
);
186 /* Remove histogram HIST from STMT's histogram list. */
189 gimple_remove_histogram_value (struct function
*fun
, gimple
*stmt
,
190 histogram_value hist
)
192 histogram_value hist2
= gimple_histogram_value (fun
, stmt
);
195 set_histogram_value (fun
, stmt
, hist
->hvalue
.next
);
199 while (hist2
->hvalue
.next
!= hist
)
200 hist2
= hist2
->hvalue
.next
;
201 hist2
->hvalue
.next
= hist
->hvalue
.next
;
203 free (hist
->hvalue
.counters
);
205 memset (hist
, 0xab, sizeof (*hist
));
209 /* Lookup histogram of type TYPE in the STMT. */
212 gimple_histogram_value_of_type (struct function
*fun
, gimple
*stmt
,
215 histogram_value hist
;
216 for (hist
= gimple_histogram_value (fun
, stmt
); hist
;
217 hist
= hist
->hvalue
.next
)
218 if (hist
->type
== type
)
223 /* Dump information about HIST to DUMP_FILE. */
226 dump_histogram_value (FILE *dump_file
, histogram_value hist
)
230 case HIST_TYPE_INTERVAL
:
231 if (hist
->hvalue
.counters
)
233 fprintf (dump_file
, "Interval counter range [%d,%d]: [",
234 hist
->hdata
.intvl
.int_start
,
235 (hist
->hdata
.intvl
.int_start
236 + hist
->hdata
.intvl
.steps
- 1));
239 for (i
= 0; i
< hist
->hdata
.intvl
.steps
; i
++)
241 fprintf (dump_file
, "%d:%" PRId64
,
242 hist
->hdata
.intvl
.int_start
+ i
,
243 (int64_t) hist
->hvalue
.counters
[i
]);
244 if (i
!= hist
->hdata
.intvl
.steps
- 1)
245 fprintf (dump_file
, ", ");
247 fprintf (dump_file
, "] outside range: %" PRId64
".\n",
248 (int64_t) hist
->hvalue
.counters
[i
]);
253 if (hist
->hvalue
.counters
)
254 fprintf (dump_file
, "Pow2 counter pow2:%" PRId64
255 " nonpow2:%" PRId64
".\n",
256 (int64_t) hist
->hvalue
.counters
[1],
257 (int64_t) hist
->hvalue
.counters
[0]);
260 case HIST_TYPE_TOPN_VALUES
:
261 case HIST_TYPE_INDIR_CALL
:
262 if (hist
->hvalue
.counters
)
265 (hist
->type
== HIST_TYPE_TOPN_VALUES
266 ? "Top N value counter " : "Indirect call counter"));
267 if (hist
->hvalue
.counters
)
269 fprintf (dump_file
, "all: %" PRId64
", values: ",
270 (int64_t) hist
->hvalue
.counters
[0]);
271 for (unsigned i
= 0; i
< GCOV_TOPN_VALUES
; i
++)
273 fprintf (dump_file
, "[%" PRId64
":%" PRId64
"]",
274 (int64_t) hist
->hvalue
.counters
[2 * i
+ 1],
275 (int64_t) hist
->hvalue
.counters
[2 * i
+ 2]);
276 if (i
!= GCOV_TOPN_VALUES
- 1)
277 fprintf (dump_file
, ", ");
279 fprintf (dump_file
, ".\n");
284 case HIST_TYPE_AVERAGE
:
285 if (hist
->hvalue
.counters
)
286 fprintf (dump_file
, "Average value sum:%" PRId64
287 " times:%" PRId64
".\n",
288 (int64_t) hist
->hvalue
.counters
[0],
289 (int64_t) hist
->hvalue
.counters
[1]);
293 if (hist
->hvalue
.counters
)
294 fprintf (dump_file
, "IOR value ior:%" PRId64
".\n",
295 (int64_t) hist
->hvalue
.counters
[0]);
298 case HIST_TYPE_TIME_PROFILE
:
299 if (hist
->hvalue
.counters
)
300 fprintf (dump_file
, "Time profile time:%" PRId64
".\n",
301 (int64_t) hist
->hvalue
.counters
[0]);
308 /* Dump information about HIST to DUMP_FILE. */
311 stream_out_histogram_value (struct output_block
*ob
, histogram_value hist
)
316 bp
= bitpack_create (ob
->main_stream
);
317 bp_pack_enum (&bp
, hist_type
, HIST_TYPE_MAX
, hist
->type
);
318 bp_pack_value (&bp
, hist
->hvalue
.next
!= NULL
, 1);
319 streamer_write_bitpack (&bp
);
322 case HIST_TYPE_INTERVAL
:
323 streamer_write_hwi (ob
, hist
->hdata
.intvl
.int_start
);
324 streamer_write_uhwi (ob
, hist
->hdata
.intvl
.steps
);
329 for (i
= 0; i
< hist
->n_counters
; i
++)
331 /* When user uses an unsigned type with a big value, constant converted
332 to gcov_type (a signed type) can be negative. */
333 gcov_type value
= hist
->hvalue
.counters
[i
];
334 if (hist
->type
== HIST_TYPE_TOPN_VALUES
&& i
> 0)
337 gcc_assert (value
>= 0);
339 streamer_write_gcov_count (ob
, value
);
341 if (hist
->hvalue
.next
)
342 stream_out_histogram_value (ob
, hist
->hvalue
.next
);
345 /* Dump information about HIST to DUMP_FILE. */
348 stream_in_histogram_value (struct lto_input_block
*ib
, gimple
*stmt
)
351 unsigned int ncounters
= 0;
354 histogram_value new_val
;
356 histogram_value
*next_p
= NULL
;
360 bp
= streamer_read_bitpack (ib
);
361 type
= bp_unpack_enum (&bp
, hist_type
, HIST_TYPE_MAX
);
362 next
= bp_unpack_value (&bp
, 1);
363 new_val
= gimple_alloc_histogram_value (cfun
, type
, stmt
, NULL
);
366 case HIST_TYPE_INTERVAL
:
367 new_val
->hdata
.intvl
.int_start
= streamer_read_hwi (ib
);
368 new_val
->hdata
.intvl
.steps
= streamer_read_uhwi (ib
);
369 ncounters
= new_val
->hdata
.intvl
.steps
+ 2;
373 case HIST_TYPE_AVERAGE
:
377 case HIST_TYPE_TOPN_VALUES
:
378 case HIST_TYPE_INDIR_CALL
:
379 ncounters
= GCOV_TOPN_VALUES_COUNTERS
;
383 case HIST_TYPE_TIME_PROFILE
:
390 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * ncounters
);
391 new_val
->n_counters
= ncounters
;
392 for (i
= 0; i
< ncounters
; i
++)
393 new_val
->hvalue
.counters
[i
] = streamer_read_gcov_count (ib
);
395 gimple_add_histogram_value (cfun
, stmt
, new_val
);
398 next_p
= &new_val
->hvalue
.next
;
403 /* Dump all histograms attached to STMT to DUMP_FILE. */
406 dump_histograms_for_stmt (struct function
*fun
, FILE *dump_file
, gimple
*stmt
)
408 histogram_value hist
;
409 for (hist
= gimple_histogram_value (fun
, stmt
); hist
; hist
= hist
->hvalue
.next
)
410 dump_histogram_value (dump_file
, hist
);
413 /* Remove all histograms associated with STMT. */
416 gimple_remove_stmt_histograms (struct function
*fun
, gimple
*stmt
)
419 while ((val
= gimple_histogram_value (fun
, stmt
)) != NULL
)
420 gimple_remove_histogram_value (fun
, stmt
, val
);
423 /* Duplicate all histograms associates with OSTMT to STMT. */
426 gimple_duplicate_stmt_histograms (struct function
*fun
, gimple
*stmt
,
427 struct function
*ofun
, gimple
*ostmt
)
430 for (val
= gimple_histogram_value (ofun
, ostmt
); val
!= NULL
; val
= val
->hvalue
.next
)
432 histogram_value new_val
= gimple_alloc_histogram_value (fun
, val
->type
, NULL
, NULL
);
433 memcpy (new_val
, val
, sizeof (*val
));
434 new_val
->hvalue
.stmt
= stmt
;
435 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
436 memcpy (new_val
->hvalue
.counters
, val
->hvalue
.counters
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
437 gimple_add_histogram_value (fun
, stmt
, new_val
);
441 /* Move all histograms associated with OSTMT to STMT. */
444 gimple_move_stmt_histograms (struct function
*fun
, gimple
*stmt
, gimple
*ostmt
)
446 histogram_value val
= gimple_histogram_value (fun
, ostmt
);
449 /* The following three statements can't be reordered,
450 because histogram hashtab relies on stmt field value
451 for finding the exact slot. */
452 set_histogram_value (fun
, ostmt
, NULL
);
453 for (; val
!= NULL
; val
= val
->hvalue
.next
)
454 val
->hvalue
.stmt
= stmt
;
455 set_histogram_value (fun
, stmt
, val
);
459 static bool error_found
= false;
461 /* Helper function for verify_histograms. For each histogram reachable via htab
462 walk verify that it was reached via statement walk. */
465 visit_hist (void **slot
, void *data
)
467 hash_set
<histogram_value
> *visited
= (hash_set
<histogram_value
> *) data
;
468 histogram_value hist
= *(histogram_value
*) slot
;
470 if (!visited
->contains (hist
)
471 && hist
->type
!= HIST_TYPE_TIME_PROFILE
)
473 error ("dead histogram");
474 dump_histogram_value (stderr
, hist
);
475 debug_gimple_stmt (hist
->hvalue
.stmt
);
481 /* Verify sanity of the histograms. */
484 verify_histograms (void)
487 gimple_stmt_iterator gsi
;
488 histogram_value hist
;
491 hash_set
<histogram_value
> visited_hists
;
492 FOR_EACH_BB_FN (bb
, cfun
)
493 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
495 gimple
*stmt
= gsi_stmt (gsi
);
497 for (hist
= gimple_histogram_value (cfun
, stmt
); hist
;
498 hist
= hist
->hvalue
.next
)
500 if (hist
->hvalue
.stmt
!= stmt
)
502 error ("histogram value statement does not correspond to "
503 "the statement it is associated with");
504 debug_gimple_stmt (stmt
);
505 dump_histogram_value (stderr
, hist
);
508 visited_hists
.add (hist
);
511 if (VALUE_HISTOGRAMS (cfun
))
512 htab_traverse (VALUE_HISTOGRAMS (cfun
), visit_hist
, &visited_hists
);
514 internal_error ("%qs failed", __func__
);
517 /* Helper function for verify_histograms. For each histogram reachable via htab
518 walk verify that it was reached via statement walk. */
521 free_hist (void **slot
, void *data ATTRIBUTE_UNUSED
)
523 histogram_value hist
= *(histogram_value
*) slot
;
524 free (hist
->hvalue
.counters
);
530 free_histograms (struct function
*fn
)
532 if (VALUE_HISTOGRAMS (fn
))
534 htab_traverse (VALUE_HISTOGRAMS (fn
), free_hist
, NULL
);
535 htab_delete (VALUE_HISTOGRAMS (fn
));
536 VALUE_HISTOGRAMS (fn
) = NULL
;
540 /* The overall number of invocations of the counter should match
541 execution count of basic block. Report it as error rather than
542 internal error as it might mean that user has misused the profile
546 check_counter (gimple
*stmt
, const char * name
,
547 gcov_type
*count
, gcov_type
*all
, profile_count bb_count_d
)
549 gcov_type bb_count
= bb_count_d
.ipa ().to_gcov_type ();
550 if (*all
!= bb_count
|| *count
> *all
)
552 dump_user_location_t locus
;
553 locus
= ((stmt
!= NULL
)
554 ? dump_user_location_t (stmt
)
555 : dump_user_location_t::from_function_decl
556 (current_function_decl
));
557 if (flag_profile_correction
)
559 if (dump_enabled_p ())
560 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
561 "correcting inconsistent value profile: %s "
562 "profiler overall count (%d) does not match BB "
563 "count (%d)\n", name
, (int)*all
, (int)bb_count
);
571 error_at (locus
.get_location_t (), "corrupted value profile: %s "
572 "profile counter (%d out of %d) inconsistent with "
573 "basic-block count (%d)",
585 /* GIMPLE based transformations. */
588 gimple_value_profile_transformations (void)
591 gimple_stmt_iterator gsi
;
592 bool changed
= false;
594 FOR_EACH_BB_FN (bb
, cfun
)
596 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
598 gimple
*stmt
= gsi_stmt (gsi
);
599 histogram_value th
= gimple_histogram_value (cfun
, stmt
);
605 fprintf (dump_file
, "Trying transformations on stmt ");
606 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
607 dump_histograms_for_stmt (cfun
, dump_file
, stmt
);
610 /* Transformations: */
611 /* The order of things in this conditional controls which
612 transformation is used when more than one is applicable. */
613 /* It is expected that any code added by the transformations
614 will be added before the current statement, and that the
615 current statement remain valid (although possibly
616 modified) upon return. */
617 if (gimple_mod_subtract_transform (&gsi
)
618 || gimple_divmod_fixed_value_transform (&gsi
)
619 || gimple_mod_pow2_value_transform (&gsi
)
620 || gimple_stringops_transform (&gsi
)
621 || gimple_ic_transform (&gsi
))
623 stmt
= gsi_stmt (gsi
);
625 /* Original statement may no longer be in the same block. */
626 if (bb
!= gimple_bb (stmt
))
628 bb
= gimple_bb (stmt
);
629 gsi
= gsi_for_stmt (stmt
);
638 /* Generate code for transformation 1 (with parent gimple assignment
639 STMT and probability of taking the optimal path PROB, which is
640 equivalent to COUNT/ALL within roundoff error). This generates the
641 result into a temp and returns the temp; it does not replace or
642 alter the original STMT. */
645 gimple_divmod_fixed_value (gassign
*stmt
, tree value
, profile_probability prob
,
646 gcov_type count
, gcov_type all
)
648 gassign
*stmt1
, *stmt2
;
650 tree tmp0
, tmp1
, tmp2
;
651 gimple
*bb1end
, *bb2end
, *bb3end
;
652 basic_block bb
, bb2
, bb3
, bb4
;
653 tree optype
, op1
, op2
;
654 edge e12
, e13
, e23
, e24
, e34
;
655 gimple_stmt_iterator gsi
;
657 gcc_assert (is_gimple_assign (stmt
)
658 && (gimple_assign_rhs_code (stmt
) == TRUNC_DIV_EXPR
659 || gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
));
661 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
662 op1
= gimple_assign_rhs1 (stmt
);
663 op2
= gimple_assign_rhs2 (stmt
);
665 bb
= gimple_bb (stmt
);
666 gsi
= gsi_for_stmt (stmt
);
668 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
669 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
670 stmt1
= gimple_build_assign (tmp0
, fold_convert (optype
, value
));
671 stmt2
= gimple_build_assign (tmp1
, op2
);
672 stmt3
= gimple_build_cond (NE_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
673 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
674 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
675 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
678 tmp2
= create_tmp_reg (optype
, "PROF");
679 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, tmp0
);
680 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
683 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, op2
);
684 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
688 /* Edge e23 connects bb2 to bb3, etc. */
689 e12
= split_block (bb
, bb1end
);
691 bb2
->count
= profile_count::from_gcov_type (count
);
692 e23
= split_block (bb2
, bb2end
);
694 bb3
->count
= profile_count::from_gcov_type (all
- count
);
695 e34
= split_block (bb3
, bb3end
);
697 bb4
->count
= profile_count::from_gcov_type (all
);
699 e12
->flags
&= ~EDGE_FALLTHRU
;
700 e12
->flags
|= EDGE_FALSE_VALUE
;
701 e12
->probability
= prob
;
703 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
704 e13
->probability
= prob
.invert ();
708 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
709 e24
->probability
= profile_probability::always ();
711 e34
->probability
= profile_probability::always ();
716 /* Return most common value of TOPN_VALUE histogram. If
717 there's a unique value, return true and set VALUE and COUNT
721 get_most_common_single_value (gimple
*stmt
, const char *counter_type
,
722 histogram_value hist
,
723 gcov_type
*value
, gcov_type
*count
,
726 if (hist
->hvalue
.counters
[2] == -1)
732 gcov_type read_all
= hist
->hvalue
.counters
[0];
734 for (unsigned i
= 0; i
< GCOV_TOPN_VALUES
; i
++)
736 gcov_type v
= hist
->hvalue
.counters
[2 * i
+ 1];
737 gcov_type c
= hist
->hvalue
.counters
[2 * i
+ 2];
739 /* Indirect calls can't be vereified. */
740 if (stmt
&& check_counter (stmt
, counter_type
, &c
, &read_all
,
741 gimple_bb (stmt
)->count
))
751 else if (c
== *count
&& v
> *value
)
758 /* Do transform 1) on INSN if applicable. */
761 gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*si
)
763 histogram_value histogram
;
765 gcov_type val
, count
, all
;
766 tree result
, value
, tree_val
;
767 profile_probability prob
;
770 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
774 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
))))
777 code
= gimple_assign_rhs_code (stmt
);
779 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
782 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
783 HIST_TYPE_TOPN_VALUES
);
787 if (!get_most_common_single_value (stmt
, "divmod", histogram
, &val
, &count
,
791 value
= histogram
->hvalue
.value
;
792 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
794 /* We require that count is at least half of all. */
795 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
797 || optimize_bb_for_size_p (gimple_bb (stmt
)))
800 /* Compute probability of taking the optimal path. */
802 prob
= profile_probability::probability_in_gcov_type (count
, all
);
804 prob
= profile_probability::never ();
806 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
807 tree_val
= build_int_cst (get_gcov_type (), val
);
811 a
[0] = (unsigned HOST_WIDE_INT
) val
;
812 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
814 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
815 TYPE_PRECISION (get_gcov_type ()), false));
817 result
= gimple_divmod_fixed_value (stmt
, tree_val
, prob
, count
, all
);
821 fprintf (dump_file
, "Transformation done: div/mod by constant ");
822 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
823 fprintf (dump_file
, "\n");
826 gimple_assign_set_rhs_from_tree (si
, result
);
827 update_stmt (gsi_stmt (*si
));
832 /* Generate code for transformation 2 (with parent gimple assign STMT and
833 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
834 within roundoff error). This generates the result into a temp and returns
835 the temp; it does not replace or alter the original STMT. */
838 gimple_mod_pow2 (gassign
*stmt
, profile_probability prob
, gcov_type count
, gcov_type all
)
840 gassign
*stmt1
, *stmt2
, *stmt3
;
843 gimple
*bb1end
, *bb2end
, *bb3end
;
844 basic_block bb
, bb2
, bb3
, bb4
;
845 tree optype
, op1
, op2
;
846 edge e12
, e13
, e23
, e24
, e34
;
847 gimple_stmt_iterator gsi
;
850 gcc_assert (is_gimple_assign (stmt
)
851 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
853 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
854 op1
= gimple_assign_rhs1 (stmt
);
855 op2
= gimple_assign_rhs2 (stmt
);
857 bb
= gimple_bb (stmt
);
858 gsi
= gsi_for_stmt (stmt
);
860 result
= create_tmp_reg (optype
, "PROF");
861 tmp2
= make_temp_ssa_name (optype
, NULL
, "PROF");
862 tmp3
= make_temp_ssa_name (optype
, NULL
, "PROF");
863 stmt2
= gimple_build_assign (tmp2
, PLUS_EXPR
, op2
,
864 build_int_cst (optype
, -1));
865 stmt3
= gimple_build_assign (tmp3
, BIT_AND_EXPR
, tmp2
, op2
);
866 stmt4
= gimple_build_cond (NE_EXPR
, tmp3
, build_int_cst (optype
, 0),
867 NULL_TREE
, NULL_TREE
);
868 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
869 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
870 gsi_insert_before (&gsi
, stmt4
, GSI_SAME_STMT
);
873 /* tmp2 == op2-1 inherited from previous block. */
874 stmt1
= gimple_build_assign (result
, BIT_AND_EXPR
, op1
, tmp2
);
875 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
878 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
880 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
884 /* Edge e23 connects bb2 to bb3, etc. */
885 e12
= split_block (bb
, bb1end
);
887 bb2
->count
= profile_count::from_gcov_type (count
);
888 e23
= split_block (bb2
, bb2end
);
890 bb3
->count
= profile_count::from_gcov_type (all
- count
);
891 e34
= split_block (bb3
, bb3end
);
893 bb4
->count
= profile_count::from_gcov_type (all
);
895 e12
->flags
&= ~EDGE_FALLTHRU
;
896 e12
->flags
|= EDGE_FALSE_VALUE
;
897 e12
->probability
= prob
;
899 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
900 e13
->probability
= prob
.invert ();
904 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
905 e24
->probability
= profile_probability::always ();
907 e34
->probability
= profile_probability::always ();
912 /* Do transform 2) on INSN if applicable. */
915 gimple_mod_pow2_value_transform (gimple_stmt_iterator
*si
)
917 histogram_value histogram
;
919 gcov_type count
, wrong_values
, all
;
920 tree lhs_type
, result
, value
;
921 profile_probability prob
;
924 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
928 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
929 if (!INTEGRAL_TYPE_P (lhs_type
))
932 code
= gimple_assign_rhs_code (stmt
);
934 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
937 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_POW2
);
941 value
= histogram
->hvalue
.value
;
942 wrong_values
= histogram
->hvalue
.counters
[0];
943 count
= histogram
->hvalue
.counters
[1];
945 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
947 /* We require that we hit a power of 2 at least half of all evaluations. */
948 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
949 || count
< wrong_values
950 || optimize_bb_for_size_p (gimple_bb (stmt
)))
953 /* Compute probability of taking the optimal path. */
954 all
= count
+ wrong_values
;
956 if (check_counter (stmt
, "pow2", &count
, &all
, gimple_bb (stmt
)->count
))
960 fprintf (dump_file
, "Transformation done: mod power of 2\n");
963 prob
= profile_probability::probability_in_gcov_type (count
, all
);
965 prob
= profile_probability::never ();
967 result
= gimple_mod_pow2 (stmt
, prob
, count
, all
);
969 gimple_assign_set_rhs_from_tree (si
, result
);
970 update_stmt (gsi_stmt (*si
));
975 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
976 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
977 supported and this is built into this interface. The probabilities of taking
978 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
979 COUNT2/ALL respectively within roundoff error). This generates the
980 result into a temp and returns the temp; it does not replace or alter
981 the original STMT. */
982 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
985 gimple_mod_subtract (gassign
*stmt
, profile_probability prob1
,
986 profile_probability prob2
, int ncounts
,
987 gcov_type count1
, gcov_type count2
, gcov_type all
)
993 gimple
*bb1end
, *bb2end
= NULL
, *bb3end
;
994 basic_block bb
, bb2
, bb3
, bb4
;
995 tree optype
, op1
, op2
;
996 edge e12
, e23
= 0, e24
, e34
, e14
;
997 gimple_stmt_iterator gsi
;
1000 gcc_assert (is_gimple_assign (stmt
)
1001 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
1003 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1004 op1
= gimple_assign_rhs1 (stmt
);
1005 op2
= gimple_assign_rhs2 (stmt
);
1007 bb
= gimple_bb (stmt
);
1008 gsi
= gsi_for_stmt (stmt
);
1010 result
= create_tmp_reg (optype
, "PROF");
1011 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1012 stmt1
= gimple_build_assign (result
, op1
);
1013 stmt2
= gimple_build_assign (tmp1
, op2
);
1014 stmt3
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1015 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1016 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1017 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
1020 if (ncounts
) /* Assumed to be 0 or 1 */
1022 stmt1
= gimple_build_assign (result
, MINUS_EXPR
, result
, tmp1
);
1023 stmt2
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1024 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1025 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1029 /* Fallback case. */
1030 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
1032 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1036 /* Edge e23 connects bb2 to bb3, etc. */
1037 /* However block 3 is optional; if it is not there, references
1038 to 3 really refer to block 2. */
1039 e12
= split_block (bb
, bb1end
);
1041 bb2
->count
= profile_count::from_gcov_type (all
- count1
);
1043 if (ncounts
) /* Assumed to be 0 or 1. */
1045 e23
= split_block (bb2
, bb2end
);
1047 bb3
->count
= profile_count::from_gcov_type (all
- count1
- count2
);
1050 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
1052 bb4
->count
= profile_count::from_gcov_type (all
);
1054 e12
->flags
&= ~EDGE_FALLTHRU
;
1055 e12
->flags
|= EDGE_FALSE_VALUE
;
1056 e12
->probability
= prob1
.invert ();
1058 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
1059 e14
->probability
= prob1
;
1061 if (ncounts
) /* Assumed to be 0 or 1. */
1063 e23
->flags
&= ~EDGE_FALLTHRU
;
1064 e23
->flags
|= EDGE_FALSE_VALUE
;
1065 e23
->probability
= prob2
.invert ();
1067 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
1068 e24
->probability
= prob2
;
1071 e34
->probability
= profile_probability::always ();
1076 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1079 gimple_mod_subtract_transform (gimple_stmt_iterator
*si
)
1081 histogram_value histogram
;
1082 enum tree_code code
;
1083 gcov_type count
, wrong_values
, all
;
1084 tree lhs_type
, result
;
1085 profile_probability prob1
, prob2
;
1086 unsigned int i
, steps
;
1087 gcov_type count1
, count2
;
1089 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
1093 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1094 if (!INTEGRAL_TYPE_P (lhs_type
))
1097 code
= gimple_assign_rhs_code (stmt
);
1099 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1102 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INTERVAL
);
1108 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1109 all
+= histogram
->hvalue
.counters
[i
];
1111 wrong_values
+= histogram
->hvalue
.counters
[i
];
1112 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
1113 steps
= histogram
->hdata
.intvl
.steps
;
1114 all
+= wrong_values
;
1115 count1
= histogram
->hvalue
.counters
[0];
1116 count2
= histogram
->hvalue
.counters
[1];
1118 if (check_counter (stmt
, "interval", &count1
, &all
, gimple_bb (stmt
)->count
))
1120 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1124 if (flag_profile_correction
&& count1
+ count2
> all
)
1125 all
= count1
+ count2
;
1127 gcc_assert (count1
+ count2
<= all
);
1129 /* We require that we use just subtractions in at least 50% of all
1132 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1134 count
+= histogram
->hvalue
.counters
[i
];
1135 if (count
* 2 >= all
)
1139 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1142 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1144 fprintf (dump_file
, "Transformation done: mod subtract\n");
1146 /* Compute probability of taking the optimal path(s). */
1149 prob1
= profile_probability::probability_in_gcov_type (count1
, all
);
1150 prob2
= profile_probability::probability_in_gcov_type (count2
, all
);
1154 prob1
= prob2
= profile_probability::never ();
1157 /* In practice, "steps" is always 2. This interface reflects this,
1158 and will need to be changed if "steps" can change. */
1159 result
= gimple_mod_subtract (stmt
, prob1
, prob2
, i
, count1
, count2
, all
);
1161 gimple_assign_set_rhs_from_tree (si
, result
);
1162 update_stmt (gsi_stmt (*si
));
1167 typedef int_hash
<unsigned int, 0, UINT_MAX
> profile_id_hash
;
1169 static hash_map
<profile_id_hash
, cgraph_node
*> *cgraph_node_map
= 0;
1171 /* Returns true if node graph is initialized. This
1172 is used to test if profile_id has been created
1173 for cgraph_nodes. */
1176 coverage_node_map_initialized_p (void)
1178 return cgraph_node_map
!= 0;
1181 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1182 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1183 that the PROFILE_IDs was already assigned. */
1186 init_node_map (bool local
)
1188 struct cgraph_node
*n
;
1189 cgraph_node_map
= new hash_map
<profile_id_hash
, cgraph_node
*>;
1191 FOR_EACH_DEFINED_FUNCTION (n
)
1192 if (n
->has_gimple_body_p () || n
->thunk
.thunk_p
)
1197 n
->profile_id
= coverage_compute_profile_id (n
);
1198 while ((val
= cgraph_node_map
->get (n
->profile_id
))
1202 fprintf (dump_file
, "Local profile-id %i conflict"
1203 " with nodes %s %s\n",
1206 (*val
)->dump_name ());
1207 n
->profile_id
= (n
->profile_id
+ 1) & 0x7fffffff;
1210 else if (!n
->profile_id
)
1214 "Node %s has no profile-id"
1215 " (profile feedback missing?)\n",
1219 else if ((val
= cgraph_node_map
->get (n
->profile_id
)))
1223 "Node %s has IP profile-id %i conflict. "
1225 n
->dump_name (), n
->profile_id
);
1229 cgraph_node_map
->put (n
->profile_id
, n
);
1233 /* Delete the CGRAPH_NODE_MAP. */
1238 delete cgraph_node_map
;
1241 /* Return cgraph node for function with pid */
1244 find_func_by_profile_id (int profile_id
)
1246 cgraph_node
**val
= cgraph_node_map
->get (profile_id
);
1253 /* Perform sanity check on the indirect call target. Due to race conditions,
1254 false function target may be attributed to an indirect call site. If the
1255 call expression type mismatches with the target function's type, expand_call
1256 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1257 Returns true if TARGET is considered ok for call CALL_STMT. */
1260 check_ic_target (gcall
*call_stmt
, struct cgraph_node
*target
)
1262 if (gimple_check_call_matching_types (call_stmt
, target
->decl
, true))
1265 if (dump_enabled_p ())
1266 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, call_stmt
,
1267 "Skipping target %s with mismatching types for icall\n",
1272 /* Do transformation
1274 if (actual_callee_address == address_of_most_common_function/method)
1281 gimple_ic (gcall
*icall_stmt
, struct cgraph_node
*direct_call
,
1282 profile_probability prob
)
1287 tree tmp0
, tmp1
, tmp
;
1288 basic_block cond_bb
, dcall_bb
, icall_bb
, join_bb
= NULL
;
1289 edge e_cd
, e_ci
, e_di
, e_dj
= NULL
, e_ij
;
1290 gimple_stmt_iterator gsi
;
1295 cond_bb
= gimple_bb (icall_stmt
);
1296 gsi
= gsi_for_stmt (icall_stmt
);
1298 tmp0
= make_temp_ssa_name (ptr_type_node
, NULL
, "PROF");
1299 tmp1
= make_temp_ssa_name (ptr_type_node
, NULL
, "PROF");
1300 tmp
= unshare_expr (gimple_call_fn (icall_stmt
));
1301 load_stmt
= gimple_build_assign (tmp0
, tmp
);
1302 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1304 tmp
= fold_convert (ptr_type_node
, build_addr (direct_call
->decl
));
1305 load_stmt
= gimple_build_assign (tmp1
, tmp
);
1306 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1308 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1309 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1311 if (TREE_CODE (gimple_vdef (icall_stmt
)) == SSA_NAME
)
1313 unlink_stmt_vdef (icall_stmt
);
1314 release_ssa_name (gimple_vdef (icall_stmt
));
1316 gimple_set_vdef (icall_stmt
, NULL_TREE
);
1317 gimple_set_vuse (icall_stmt
, NULL_TREE
);
1318 update_stmt (icall_stmt
);
1319 dcall_stmt
= as_a
<gcall
*> (gimple_copy (icall_stmt
));
1320 gimple_call_set_fndecl (dcall_stmt
, direct_call
->decl
);
1321 dflags
= flags_from_decl_or_type (direct_call
->decl
);
1322 if ((dflags
& ECF_NORETURN
) != 0
1323 && should_remove_lhs_p (gimple_call_lhs (dcall_stmt
)))
1324 gimple_call_set_lhs (dcall_stmt
, NULL_TREE
);
1325 gsi_insert_before (&gsi
, dcall_stmt
, GSI_SAME_STMT
);
1328 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1329 e_cd
= split_block (cond_bb
, cond_stmt
);
1330 dcall_bb
= e_cd
->dest
;
1331 dcall_bb
->count
= cond_bb
->count
.apply_probability (prob
);
1333 e_di
= split_block (dcall_bb
, dcall_stmt
);
1334 icall_bb
= e_di
->dest
;
1335 icall_bb
->count
= cond_bb
->count
- dcall_bb
->count
;
1337 /* Do not disturb existing EH edges from the indirect call. */
1338 if (!stmt_ends_bb_p (icall_stmt
))
1339 e_ij
= split_block (icall_bb
, icall_stmt
);
1342 e_ij
= find_fallthru_edge (icall_bb
->succs
);
1343 /* The indirect call might be noreturn. */
1346 e_ij
->probability
= profile_probability::always ();
1347 e_ij
= single_pred_edge (split_edge (e_ij
));
1352 join_bb
= e_ij
->dest
;
1353 join_bb
->count
= cond_bb
->count
;
1356 e_cd
->flags
= (e_cd
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1357 e_cd
->probability
= prob
;
1359 e_ci
= make_edge (cond_bb
, icall_bb
, EDGE_FALSE_VALUE
);
1360 e_ci
->probability
= prob
.invert ();
1366 if ((dflags
& ECF_NORETURN
) == 0)
1368 e_dj
= make_edge (dcall_bb
, join_bb
, EDGE_FALLTHRU
);
1369 e_dj
->probability
= profile_probability::always ();
1371 e_ij
->probability
= profile_probability::always ();
1374 /* Insert PHI node for the call result if necessary. */
1375 if (gimple_call_lhs (icall_stmt
)
1376 && TREE_CODE (gimple_call_lhs (icall_stmt
)) == SSA_NAME
1377 && (dflags
& ECF_NORETURN
) == 0)
1379 tree result
= gimple_call_lhs (icall_stmt
);
1380 gphi
*phi
= create_phi_node (result
, join_bb
);
1381 gimple_call_set_lhs (icall_stmt
,
1382 duplicate_ssa_name (result
, icall_stmt
));
1383 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1384 gimple_call_set_lhs (dcall_stmt
,
1385 duplicate_ssa_name (result
, dcall_stmt
));
1386 add_phi_arg (phi
, gimple_call_lhs (dcall_stmt
), e_dj
, UNKNOWN_LOCATION
);
1389 /* Build an EH edge for the direct call if necessary. */
1390 lp_nr
= lookup_stmt_eh_lp (icall_stmt
);
1391 if (lp_nr
> 0 && stmt_could_throw_p (cfun
, dcall_stmt
))
1393 add_stmt_to_eh_lp (dcall_stmt
, lp_nr
);
1396 FOR_EACH_EDGE (e_eh
, ei
, icall_bb
->succs
)
1397 if (e_eh
->flags
& (EDGE_EH
| EDGE_ABNORMAL
))
1399 e
= make_edge (dcall_bb
, e_eh
->dest
, e_eh
->flags
);
1400 e
->probability
= e_eh
->probability
;
1401 for (gphi_iterator psi
= gsi_start_phis (e_eh
->dest
);
1402 !gsi_end_p (psi
); gsi_next (&psi
))
1404 gphi
*phi
= psi
.phi ();
1405 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
),
1406 PHI_ARG_DEF_FROM_EDGE (phi
, e_eh
));
1409 if (!stmt_could_throw_p (cfun
, dcall_stmt
))
1410 gimple_purge_dead_eh_edges (dcall_bb
);
1415 For every checked indirect/virtual call determine if most common pid of
1416 function/class method has probability more than 50%. If yes modify code of
1421 gimple_ic_transform (gimple_stmt_iterator
*gsi
)
1424 histogram_value histogram
;
1425 gcov_type val
, count
, all
;
1426 struct cgraph_node
*direct_call
;
1428 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1432 if (gimple_call_fndecl (stmt
) != NULL_TREE
)
1435 if (gimple_call_internal_p (stmt
))
1438 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INDIR_CALL
);
1442 if (!get_most_common_single_value (NULL
, "indirect call", histogram
, &val
,
1446 if (4 * count
<= 3 * all
)
1449 direct_call
= find_func_by_profile_id ((int)val
);
1451 if (direct_call
== NULL
)
1457 fprintf (dump_file
, "Indirect call -> direct call from other module");
1458 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1459 fprintf (dump_file
, "=> %i (will resolve only with LTO)\n", (int)val
);
1465 if (!check_ic_target (stmt
, direct_call
))
1469 fprintf (dump_file
, "Indirect call -> direct call ");
1470 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1471 fprintf (dump_file
, "=> ");
1472 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1473 fprintf (dump_file
, " transformation skipped because of type mismatch");
1474 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1476 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1482 fprintf (dump_file
, "Indirect call -> direct call ");
1483 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1484 fprintf (dump_file
, "=> ");
1485 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1486 fprintf (dump_file
, " transformation on insn postponned to ipa-profile");
1487 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1488 fprintf (dump_file
, "hist->count %" PRId64
1489 " hist->all %" PRId64
"\n", count
, all
);
1495 /* Return true if the stringop CALL shall be profiled. SIZE_ARG be
1496 set to the argument index for the size of the string operation. */
1499 interesting_stringop_to_profile_p (gcall
*call
, int *size_arg
)
1501 enum built_in_function fcode
;
1503 fcode
= DECL_FUNCTION_CODE (gimple_call_fndecl (call
));
1506 case BUILT_IN_MEMCPY
:
1507 case BUILT_IN_MEMPCPY
:
1508 case BUILT_IN_MEMMOVE
:
1510 return validate_gimple_arglist (call
, POINTER_TYPE
, POINTER_TYPE
,
1511 INTEGER_TYPE
, VOID_TYPE
);
1512 case BUILT_IN_MEMSET
:
1514 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1515 INTEGER_TYPE
, VOID_TYPE
);
1516 case BUILT_IN_BZERO
:
1518 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1525 /* Convert stringop (..., vcall_size)
1527 if (vcall_size == icall_size)
1528 stringop (..., icall_size);
1530 stringop (..., vcall_size);
1531 assuming we'll propagate a true constant into ICALL_SIZE later. */
1534 gimple_stringop_fixed_value (gcall
*vcall_stmt
, tree icall_size
, profile_probability prob
,
1535 gcov_type count
, gcov_type all
)
1540 tree tmp0
, tmp1
, vcall_size
, optype
;
1541 basic_block cond_bb
, icall_bb
, vcall_bb
, join_bb
;
1542 edge e_ci
, e_cv
, e_iv
, e_ij
, e_vj
;
1543 gimple_stmt_iterator gsi
;
1546 if (!interesting_stringop_to_profile_p (vcall_stmt
, &size_arg
))
1549 cond_bb
= gimple_bb (vcall_stmt
);
1550 gsi
= gsi_for_stmt (vcall_stmt
);
1552 vcall_size
= gimple_call_arg (vcall_stmt
, size_arg
);
1553 optype
= TREE_TYPE (vcall_size
);
1555 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1556 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1557 tmp_stmt
= gimple_build_assign (tmp0
, fold_convert (optype
, icall_size
));
1558 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1560 tmp_stmt
= gimple_build_assign (tmp1
, vcall_size
);
1561 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1563 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1564 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1566 if (TREE_CODE (gimple_vdef (vcall_stmt
)) == SSA_NAME
)
1568 unlink_stmt_vdef (vcall_stmt
);
1569 release_ssa_name (gimple_vdef (vcall_stmt
));
1571 gimple_set_vdef (vcall_stmt
, NULL
);
1572 gimple_set_vuse (vcall_stmt
, NULL
);
1573 update_stmt (vcall_stmt
);
1574 icall_stmt
= as_a
<gcall
*> (gimple_copy (vcall_stmt
));
1575 gimple_call_set_arg (icall_stmt
, size_arg
,
1576 fold_convert (optype
, icall_size
));
1577 gsi_insert_before (&gsi
, icall_stmt
, GSI_SAME_STMT
);
1580 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1581 e_ci
= split_block (cond_bb
, cond_stmt
);
1582 icall_bb
= e_ci
->dest
;
1583 icall_bb
->count
= profile_count::from_gcov_type (count
);
1585 e_iv
= split_block (icall_bb
, icall_stmt
);
1586 vcall_bb
= e_iv
->dest
;
1587 vcall_bb
->count
= profile_count::from_gcov_type (all
- count
);
1589 e_vj
= split_block (vcall_bb
, vcall_stmt
);
1590 join_bb
= e_vj
->dest
;
1591 join_bb
->count
= profile_count::from_gcov_type (all
);
1593 e_ci
->flags
= (e_ci
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1594 e_ci
->probability
= prob
;
1596 e_cv
= make_edge (cond_bb
, vcall_bb
, EDGE_FALSE_VALUE
);
1597 e_cv
->probability
= prob
.invert ();
1601 e_ij
= make_edge (icall_bb
, join_bb
, EDGE_FALLTHRU
);
1602 e_ij
->probability
= profile_probability::always ();
1604 e_vj
->probability
= profile_probability::always ();
1606 /* Insert PHI node for the call result if necessary. */
1607 if (gimple_call_lhs (vcall_stmt
)
1608 && TREE_CODE (gimple_call_lhs (vcall_stmt
)) == SSA_NAME
)
1610 tree result
= gimple_call_lhs (vcall_stmt
);
1611 gphi
*phi
= create_phi_node (result
, join_bb
);
1612 gimple_call_set_lhs (vcall_stmt
,
1613 duplicate_ssa_name (result
, vcall_stmt
));
1614 add_phi_arg (phi
, gimple_call_lhs (vcall_stmt
), e_vj
, UNKNOWN_LOCATION
);
1615 gimple_call_set_lhs (icall_stmt
,
1616 duplicate_ssa_name (result
, icall_stmt
));
1617 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1620 /* Because these are all string op builtins, they're all nothrow. */
1621 gcc_assert (!stmt_could_throw_p (cfun
, vcall_stmt
));
1622 gcc_assert (!stmt_could_throw_p (cfun
, icall_stmt
));
1625 /* Find values inside STMT for that we want to measure histograms for
1626 division/modulo optimization. */
1629 gimple_stringops_transform (gimple_stmt_iterator
*gsi
)
1633 enum built_in_function fcode
;
1634 histogram_value histogram
;
1635 gcov_type count
, all
, val
;
1637 unsigned int dest_align
, src_align
;
1638 profile_probability prob
;
1642 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1646 if (!gimple_call_builtin_p (gsi_stmt (*gsi
), BUILT_IN_NORMAL
))
1649 if (!interesting_stringop_to_profile_p (stmt
, &size_arg
))
1652 blck_size
= gimple_call_arg (stmt
, size_arg
);
1653 if (TREE_CODE (blck_size
) == INTEGER_CST
)
1656 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
1657 HIST_TYPE_TOPN_VALUES
);
1661 if (!get_most_common_single_value (stmt
, "stringops", histogram
, &val
,
1665 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1667 /* We require that count is at least half of all. */
1668 if (2 * count
< all
|| optimize_bb_for_size_p (gimple_bb (stmt
)))
1670 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
1673 prob
= profile_probability::probability_in_gcov_type (count
, all
);
1675 prob
= profile_probability::never ();
1677 dest
= gimple_call_arg (stmt
, 0);
1678 dest_align
= get_pointer_alignment (dest
);
1679 fcode
= DECL_FUNCTION_CODE (gimple_call_fndecl (stmt
));
1682 case BUILT_IN_MEMCPY
:
1683 case BUILT_IN_MEMPCPY
:
1684 case BUILT_IN_MEMMOVE
:
1685 src
= gimple_call_arg (stmt
, 1);
1686 src_align
= get_pointer_alignment (src
);
1687 if (!can_move_by_pieces (val
, MIN (dest_align
, src_align
)))
1690 case BUILT_IN_MEMSET
:
1691 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1692 gimple_call_arg (stmt
, 1),
1696 case BUILT_IN_BZERO
:
1697 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1706 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
1707 tree_val
= build_int_cst (get_gcov_type (), val
);
1711 a
[0] = (unsigned HOST_WIDE_INT
) val
;
1712 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
1714 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
1715 TYPE_PRECISION (get_gcov_type ()), false));
1720 "Transformation done: single value %i stringop for %s\n",
1721 (int)val
, built_in_names
[(int)fcode
]);
1723 gimple_stringop_fixed_value (stmt
, tree_val
, prob
, count
, all
);
1729 stringop_block_profile (gimple
*stmt
, unsigned int *expected_align
,
1730 HOST_WIDE_INT
*expected_size
)
1732 histogram_value histogram
;
1733 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_AVERAGE
);
1736 *expected_size
= -1;
1737 else if (!histogram
->hvalue
.counters
[1])
1739 *expected_size
= -1;
1740 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1745 size
= ((histogram
->hvalue
.counters
[0]
1746 + histogram
->hvalue
.counters
[1] / 2)
1747 / histogram
->hvalue
.counters
[1]);
1748 /* Even if we can hold bigger value in SIZE, INT_MAX
1749 is safe "infinity" for code generation strategies. */
1752 *expected_size
= size
;
1753 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1756 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_IOR
);
1759 *expected_align
= 0;
1760 else if (!histogram
->hvalue
.counters
[0])
1762 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1763 *expected_align
= 0;
1768 unsigned int alignment
;
1770 count
= histogram
->hvalue
.counters
[0];
1772 while (!(count
& alignment
)
1773 && (alignment
<= UINT_MAX
/ 2 / BITS_PER_UNIT
))
1775 *expected_align
= alignment
* BITS_PER_UNIT
;
1776 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1781 /* Find values inside STMT for that we want to measure histograms for
1782 division/modulo optimization. */
1785 gimple_divmod_values_to_profile (gimple
*stmt
, histogram_values
*values
)
1787 tree lhs
, divisor
, op0
, type
;
1788 histogram_value hist
;
1790 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1793 lhs
= gimple_assign_lhs (stmt
);
1794 type
= TREE_TYPE (lhs
);
1795 if (!INTEGRAL_TYPE_P (type
))
1798 switch (gimple_assign_rhs_code (stmt
))
1800 case TRUNC_DIV_EXPR
:
1801 case TRUNC_MOD_EXPR
:
1802 divisor
= gimple_assign_rhs2 (stmt
);
1803 op0
= gimple_assign_rhs1 (stmt
);
1805 values
->reserve (3);
1807 if (TREE_CODE (divisor
) == SSA_NAME
)
1808 /* Check for the case where the divisor is the same value most
1810 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1811 HIST_TYPE_TOPN_VALUES
,
1814 /* For mod, check whether it is not often a noop (or replaceable by
1815 a few subtractions). */
1816 if (gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
1817 && TYPE_UNSIGNED (type
)
1818 && TREE_CODE (divisor
) == SSA_NAME
)
1821 /* Check for a special case where the divisor is power of 2. */
1822 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1826 val
= build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
1827 hist
= gimple_alloc_histogram_value (cfun
, HIST_TYPE_INTERVAL
,
1829 hist
->hdata
.intvl
.int_start
= 0;
1830 hist
->hdata
.intvl
.steps
= 2;
1831 values
->quick_push (hist
);
1840 /* Find calls inside STMT for that we want to measure histograms for
1841 indirect/virtual call optimization. */
1844 gimple_indirect_call_to_profile (gimple
*stmt
, histogram_values
*values
)
1848 if (gimple_code (stmt
) != GIMPLE_CALL
1849 || gimple_call_internal_p (stmt
)
1850 || gimple_call_fndecl (stmt
) != NULL_TREE
)
1853 callee
= gimple_call_fn (stmt
);
1855 values
->reserve (3);
1857 values
->quick_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_INDIR_CALL
,
1863 /* Find values inside STMT for that we want to measure histograms for
1864 string operations. */
1867 gimple_stringops_values_to_profile (gimple
*gs
, histogram_values
*values
)
1874 stmt
= dyn_cast
<gcall
*> (gs
);
1878 if (!gimple_call_builtin_p (gs
, BUILT_IN_NORMAL
))
1881 if (!interesting_stringop_to_profile_p (stmt
, &size_arg
))
1884 dest
= gimple_call_arg (stmt
, 0);
1885 blck_size
= gimple_call_arg (stmt
, size_arg
);
1887 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1889 values
->safe_push (gimple_alloc_histogram_value (cfun
,
1890 HIST_TYPE_TOPN_VALUES
,
1892 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_AVERAGE
,
1896 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1897 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_IOR
,
1901 /* Find values inside STMT for that we want to measure histograms and adds
1902 them to list VALUES. */
1905 gimple_values_to_profile (gimple
*stmt
, histogram_values
*values
)
1907 gimple_divmod_values_to_profile (stmt
, values
);
1908 gimple_stringops_values_to_profile (stmt
, values
);
1909 gimple_indirect_call_to_profile (stmt
, values
);
1913 gimple_find_values_to_profile (histogram_values
*values
)
1916 gimple_stmt_iterator gsi
;
1918 histogram_value hist
= NULL
;
1921 FOR_EACH_BB_FN (bb
, cfun
)
1922 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1923 gimple_values_to_profile (gsi_stmt (gsi
), values
);
1925 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_TIME_PROFILE
, 0, 0));
1927 FOR_EACH_VEC_ELT (*values
, i
, hist
)
1931 case HIST_TYPE_INTERVAL
:
1932 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
1935 case HIST_TYPE_POW2
:
1936 hist
->n_counters
= 2;
1939 case HIST_TYPE_TOPN_VALUES
:
1940 case HIST_TYPE_INDIR_CALL
:
1941 hist
->n_counters
= GCOV_TOPN_VALUES_COUNTERS
;
1944 case HIST_TYPE_TIME_PROFILE
:
1945 hist
->n_counters
= 1;
1948 case HIST_TYPE_AVERAGE
:
1949 hist
->n_counters
= 2;
1953 hist
->n_counters
= 1;
1959 if (dump_file
&& hist
->hvalue
.stmt
!= NULL
)
1961 fprintf (dump_file
, "Stmt ");
1962 print_gimple_stmt (dump_file
, hist
->hvalue
.stmt
, 0, TDF_SLIM
);
1963 dump_histogram_value (dump_file
, hist
);