1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2015 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"
27 #include "fold-const.h"
28 #include "tree-nested.h"
31 #include "hard-reg-set.h"
34 #include "insn-config.h"
43 #include "dominance.h"
45 #include "basic-block.h"
46 #include "value-prof.h"
48 #include "insn-codes.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
54 #include "gimple-expr.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
60 #include "tree-phinodes.h"
61 #include "ssa-iterators.h"
62 #include "stringpool.h"
63 #include "tree-ssanames.h"
64 #include "diagnostic.h"
65 #include "gimple-pretty-print.h"
72 #include "data-streamer.h"
75 #include "tree-chkp.h"
77 /* In this file value profile based optimizations are placed. Currently the
78 following optimizations are implemented (for more detailed descriptions
79 see comments at value_profile_transformations):
81 1) Division/modulo specialization. Provided that we can determine that the
82 operands of the division have some special properties, we may use it to
83 produce more effective code.
85 2) Indirect/virtual call specialization. If we can determine most
86 common function callee in indirect/virtual call. We can use this
87 information to improve code effectiveness (especially info for
90 3) Speculative prefetching. If we are able to determine that the difference
91 between addresses accessed by a memory reference is usually constant, we
92 may add the prefetch instructions.
93 FIXME: This transformation was removed together with RTL based value
97 Value profiling internals
98 ==========================
100 Every value profiling transformation starts with defining what values
101 to profile. There are different histogram types (see HIST_TYPE_* in
102 value-prof.h) and each transformation can request one or more histogram
103 types per GIMPLE statement. The function gimple_find_values_to_profile()
104 collects the values to profile in a vec, and adds the number of counters
105 required for the different histogram types.
107 For a -fprofile-generate run, the statements for which values should be
108 recorded, are instrumented in instrument_values(). The instrumentation
109 is done by helper functions that can be found in tree-profile.c, where
110 new types of histograms can be added if necessary.
112 After a -fprofile-use, the value profiling data is read back in by
113 compute_value_histograms() that translates the collected data to
114 histograms and attaches them to the profiled statements via
115 gimple_add_histogram_value(). Histograms are stored in a hash table
116 that is attached to every intrumented function, see VALUE_HISTOGRAMS
119 The value-profile transformations driver is the function
120 gimple_value_profile_transformations(). It traverses all statements in
121 the to-be-transformed function, and looks for statements with one or
122 more histograms attached to it. If a statement has histograms, the
123 transformation functions are called on the statement.
125 Limitations / FIXME / TODO:
126 * Only one histogram of each type can be associated with a statement.
127 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
128 (This type of histogram was originally used to implement a form of
129 stride profiling based speculative prefetching to improve SPEC2000
130 scores for memory-bound benchmarks, mcf and equake. However, this
131 was an RTL value-profiling transformation, and those have all been
133 * Some value profile transformations are done in builtins.c (?!)
134 * Updating of histograms needs some TLC.
135 * The value profiling code could be used to record analysis results
136 from non-profiling (e.g. VRP).
137 * Adding new profilers should be simplified, starting with a cleanup
138 of what-happens-where andwith making gimple_find_values_to_profile
139 and gimple_value_profile_transformations table-driven, perhaps...
142 static tree
gimple_divmod_fixed_value (gassign
*, tree
, int, gcov_type
,
144 static tree
gimple_mod_pow2 (gassign
*, int, gcov_type
, gcov_type
);
145 static tree
gimple_mod_subtract (gassign
*, int, int, int, gcov_type
,
146 gcov_type
, gcov_type
);
147 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*);
148 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator
*);
149 static bool gimple_mod_subtract_transform (gimple_stmt_iterator
*);
150 static bool gimple_stringops_transform (gimple_stmt_iterator
*);
151 static bool gimple_ic_transform (gimple_stmt_iterator
*);
153 /* Allocate histogram value. */
156 gimple_alloc_histogram_value (struct function
*fun ATTRIBUTE_UNUSED
,
157 enum hist_type type
, gimple stmt
, tree value
)
159 histogram_value hist
= (histogram_value
) xcalloc (1, sizeof (*hist
));
160 hist
->hvalue
.value
= value
;
161 hist
->hvalue
.stmt
= stmt
;
166 /* Hash value for histogram. */
169 histogram_hash (const void *x
)
171 return htab_hash_pointer (((const_histogram_value
)x
)->hvalue
.stmt
);
174 /* Return nonzero if statement for histogram_value X is Y. */
177 histogram_eq (const void *x
, const void *y
)
179 return ((const_histogram_value
) x
)->hvalue
.stmt
== (const_gimple
) y
;
182 /* Set histogram for STMT. */
185 set_histogram_value (struct function
*fun
, gimple stmt
, histogram_value hist
)
188 if (!hist
&& !VALUE_HISTOGRAMS (fun
))
190 if (!VALUE_HISTOGRAMS (fun
))
191 VALUE_HISTOGRAMS (fun
) = htab_create (1, histogram_hash
,
193 loc
= htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
194 htab_hash_pointer (stmt
),
195 hist
? INSERT
: NO_INSERT
);
199 htab_clear_slot (VALUE_HISTOGRAMS (fun
), loc
);
205 /* Get histogram list for STMT. */
208 gimple_histogram_value (struct function
*fun
, gimple stmt
)
210 if (!VALUE_HISTOGRAMS (fun
))
212 return (histogram_value
) htab_find_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
213 htab_hash_pointer (stmt
));
216 /* Add histogram for STMT. */
219 gimple_add_histogram_value (struct function
*fun
, gimple stmt
,
220 histogram_value hist
)
222 hist
->hvalue
.next
= gimple_histogram_value (fun
, stmt
);
223 set_histogram_value (fun
, stmt
, hist
);
228 /* Remove histogram HIST from STMT's histogram list. */
231 gimple_remove_histogram_value (struct function
*fun
, gimple stmt
,
232 histogram_value hist
)
234 histogram_value hist2
= gimple_histogram_value (fun
, stmt
);
237 set_histogram_value (fun
, stmt
, hist
->hvalue
.next
);
241 while (hist2
->hvalue
.next
!= hist
)
242 hist2
= hist2
->hvalue
.next
;
243 hist2
->hvalue
.next
= hist
->hvalue
.next
;
245 free (hist
->hvalue
.counters
);
246 #ifdef ENABLE_CHECKING
247 memset (hist
, 0xab, sizeof (*hist
));
253 /* Lookup histogram of type TYPE in the STMT. */
256 gimple_histogram_value_of_type (struct function
*fun
, gimple stmt
,
259 histogram_value hist
;
260 for (hist
= gimple_histogram_value (fun
, stmt
); hist
;
261 hist
= hist
->hvalue
.next
)
262 if (hist
->type
== type
)
267 /* Dump information about HIST to DUMP_FILE. */
270 dump_histogram_value (FILE *dump_file
, histogram_value hist
)
274 case HIST_TYPE_INTERVAL
:
275 fprintf (dump_file
, "Interval counter range %d -- %d",
276 hist
->hdata
.intvl
.int_start
,
277 (hist
->hdata
.intvl
.int_start
278 + hist
->hdata
.intvl
.steps
- 1));
279 if (hist
->hvalue
.counters
)
282 fprintf (dump_file
, " [");
283 for (i
= 0; i
< hist
->hdata
.intvl
.steps
; i
++)
284 fprintf (dump_file
, " %d:%" PRId64
,
285 hist
->hdata
.intvl
.int_start
+ i
,
286 (int64_t) hist
->hvalue
.counters
[i
]);
287 fprintf (dump_file
, " ] outside range:%" PRId64
,
288 (int64_t) hist
->hvalue
.counters
[i
]);
290 fprintf (dump_file
, ".\n");
294 fprintf (dump_file
, "Pow2 counter ");
295 if (hist
->hvalue
.counters
)
297 fprintf (dump_file
, "pow2:%" PRId64
299 (int64_t) hist
->hvalue
.counters
[0],
300 (int64_t) hist
->hvalue
.counters
[1]);
302 fprintf (dump_file
, ".\n");
305 case HIST_TYPE_SINGLE_VALUE
:
306 fprintf (dump_file
, "Single value ");
307 if (hist
->hvalue
.counters
)
309 fprintf (dump_file
, "value:%" PRId64
312 (int64_t) hist
->hvalue
.counters
[0],
313 (int64_t) hist
->hvalue
.counters
[1],
314 (int64_t) hist
->hvalue
.counters
[2]);
316 fprintf (dump_file
, ".\n");
319 case HIST_TYPE_AVERAGE
:
320 fprintf (dump_file
, "Average value ");
321 if (hist
->hvalue
.counters
)
323 fprintf (dump_file
, "sum:%" PRId64
325 (int64_t) hist
->hvalue
.counters
[0],
326 (int64_t) hist
->hvalue
.counters
[1]);
328 fprintf (dump_file
, ".\n");
332 fprintf (dump_file
, "IOR value ");
333 if (hist
->hvalue
.counters
)
335 fprintf (dump_file
, "ior:%" PRId64
,
336 (int64_t) hist
->hvalue
.counters
[0]);
338 fprintf (dump_file
, ".\n");
341 case HIST_TYPE_CONST_DELTA
:
342 fprintf (dump_file
, "Constant delta ");
343 if (hist
->hvalue
.counters
)
345 fprintf (dump_file
, "value:%" PRId64
348 (int64_t) hist
->hvalue
.counters
[0],
349 (int64_t) hist
->hvalue
.counters
[1],
350 (int64_t) hist
->hvalue
.counters
[2]);
352 fprintf (dump_file
, ".\n");
354 case HIST_TYPE_INDIR_CALL
:
355 fprintf (dump_file
, "Indirect call ");
356 if (hist
->hvalue
.counters
)
358 fprintf (dump_file
, "value:%" PRId64
361 (int64_t) hist
->hvalue
.counters
[0],
362 (int64_t) hist
->hvalue
.counters
[1],
363 (int64_t) hist
->hvalue
.counters
[2]);
365 fprintf (dump_file
, ".\n");
367 case HIST_TYPE_TIME_PROFILE
:
368 fprintf (dump_file
, "Time profile ");
369 if (hist
->hvalue
.counters
)
371 fprintf (dump_file
, "time:%" PRId64
,
372 (int64_t) hist
->hvalue
.counters
[0]);
374 fprintf (dump_file
, ".\n");
376 case HIST_TYPE_INDIR_CALL_TOPN
:
377 fprintf (dump_file
, "Indirect call topn ");
378 if (hist
->hvalue
.counters
)
382 fprintf (dump_file
, "accu:%" PRId64
, hist
->hvalue
.counters
[0]);
383 for (i
= 1; i
< (GCOV_ICALL_TOPN_VAL
<< 2); i
+= 2)
385 fprintf (dump_file
, " target:%" PRId64
" value:%" PRId64
,
386 (int64_t) hist
->hvalue
.counters
[i
],
387 (int64_t) hist
->hvalue
.counters
[i
+1]);
390 fprintf (dump_file
, ".\n");
397 /* Dump information about HIST to DUMP_FILE. */
400 stream_out_histogram_value (struct output_block
*ob
, histogram_value hist
)
405 bp
= bitpack_create (ob
->main_stream
);
406 bp_pack_enum (&bp
, hist_type
, HIST_TYPE_MAX
, hist
->type
);
407 bp_pack_value (&bp
, hist
->hvalue
.next
!= NULL
, 1);
408 streamer_write_bitpack (&bp
);
411 case HIST_TYPE_INTERVAL
:
412 streamer_write_hwi (ob
, hist
->hdata
.intvl
.int_start
);
413 streamer_write_uhwi (ob
, hist
->hdata
.intvl
.steps
);
418 for (i
= 0; i
< hist
->n_counters
; i
++)
419 streamer_write_gcov_count (ob
, hist
->hvalue
.counters
[i
]);
420 if (hist
->hvalue
.next
)
421 stream_out_histogram_value (ob
, hist
->hvalue
.next
);
423 /* Dump information about HIST to DUMP_FILE. */
426 stream_in_histogram_value (struct lto_input_block
*ib
, gimple stmt
)
429 unsigned int ncounters
= 0;
432 histogram_value new_val
;
434 histogram_value
*next_p
= NULL
;
438 bp
= streamer_read_bitpack (ib
);
439 type
= bp_unpack_enum (&bp
, hist_type
, HIST_TYPE_MAX
);
440 next
= bp_unpack_value (&bp
, 1);
441 new_val
= gimple_alloc_histogram_value (cfun
, type
, stmt
, NULL
);
444 case HIST_TYPE_INTERVAL
:
445 new_val
->hdata
.intvl
.int_start
= streamer_read_hwi (ib
);
446 new_val
->hdata
.intvl
.steps
= streamer_read_uhwi (ib
);
447 ncounters
= new_val
->hdata
.intvl
.steps
+ 2;
451 case HIST_TYPE_AVERAGE
:
455 case HIST_TYPE_SINGLE_VALUE
:
456 case HIST_TYPE_INDIR_CALL
:
460 case HIST_TYPE_CONST_DELTA
:
465 case HIST_TYPE_TIME_PROFILE
:
469 case HIST_TYPE_INDIR_CALL_TOPN
:
470 ncounters
= (GCOV_ICALL_TOPN_VAL
<< 2) + 1;
476 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * ncounters
);
477 new_val
->n_counters
= ncounters
;
478 for (i
= 0; i
< ncounters
; i
++)
479 new_val
->hvalue
.counters
[i
] = streamer_read_gcov_count (ib
);
481 gimple_add_histogram_value (cfun
, stmt
, new_val
);
484 next_p
= &new_val
->hvalue
.next
;
489 /* Dump all histograms attached to STMT to DUMP_FILE. */
492 dump_histograms_for_stmt (struct function
*fun
, FILE *dump_file
, gimple stmt
)
494 histogram_value hist
;
495 for (hist
= gimple_histogram_value (fun
, stmt
); hist
; hist
= hist
->hvalue
.next
)
496 dump_histogram_value (dump_file
, hist
);
499 /* Remove all histograms associated with STMT. */
502 gimple_remove_stmt_histograms (struct function
*fun
, gimple stmt
)
505 while ((val
= gimple_histogram_value (fun
, stmt
)) != NULL
)
506 gimple_remove_histogram_value (fun
, stmt
, val
);
509 /* Duplicate all histograms associates with OSTMT to STMT. */
512 gimple_duplicate_stmt_histograms (struct function
*fun
, gimple stmt
,
513 struct function
*ofun
, gimple ostmt
)
516 for (val
= gimple_histogram_value (ofun
, ostmt
); val
!= NULL
; val
= val
->hvalue
.next
)
518 histogram_value new_val
= gimple_alloc_histogram_value (fun
, val
->type
, NULL
, NULL
);
519 memcpy (new_val
, val
, sizeof (*val
));
520 new_val
->hvalue
.stmt
= stmt
;
521 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
522 memcpy (new_val
->hvalue
.counters
, val
->hvalue
.counters
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
523 gimple_add_histogram_value (fun
, stmt
, new_val
);
528 /* Move all histograms associated with OSTMT to STMT. */
531 gimple_move_stmt_histograms (struct function
*fun
, gimple stmt
, gimple ostmt
)
533 histogram_value val
= gimple_histogram_value (fun
, ostmt
);
536 /* The following three statements can't be reordered,
537 because histogram hashtab relies on stmt field value
538 for finding the exact slot. */
539 set_histogram_value (fun
, ostmt
, NULL
);
540 for (; val
!= NULL
; val
= val
->hvalue
.next
)
541 val
->hvalue
.stmt
= stmt
;
542 set_histogram_value (fun
, stmt
, val
);
546 static bool error_found
= false;
548 /* Helper function for verify_histograms. For each histogram reachable via htab
549 walk verify that it was reached via statement walk. */
552 visit_hist (void **slot
, void *data
)
554 hash_set
<histogram_value
> *visited
= (hash_set
<histogram_value
> *) data
;
555 histogram_value hist
= *(histogram_value
*) slot
;
557 if (!visited
->contains (hist
)
558 && hist
->type
!= HIST_TYPE_TIME_PROFILE
)
560 error ("dead histogram");
561 dump_histogram_value (stderr
, hist
);
562 debug_gimple_stmt (hist
->hvalue
.stmt
);
569 /* Verify sanity of the histograms. */
572 verify_histograms (void)
575 gimple_stmt_iterator gsi
;
576 histogram_value hist
;
579 hash_set
<histogram_value
> visited_hists
;
580 FOR_EACH_BB_FN (bb
, cfun
)
581 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
583 gimple stmt
= gsi_stmt (gsi
);
585 for (hist
= gimple_histogram_value (cfun
, stmt
); hist
;
586 hist
= hist
->hvalue
.next
)
588 if (hist
->hvalue
.stmt
!= stmt
)
590 error ("Histogram value statement does not correspond to "
591 "the statement it is associated with");
592 debug_gimple_stmt (stmt
);
593 dump_histogram_value (stderr
, hist
);
596 visited_hists
.add (hist
);
599 if (VALUE_HISTOGRAMS (cfun
))
600 htab_traverse (VALUE_HISTOGRAMS (cfun
), visit_hist
, &visited_hists
);
602 internal_error ("verify_histograms failed");
605 /* Helper function for verify_histograms. For each histogram reachable via htab
606 walk verify that it was reached via statement walk. */
609 free_hist (void **slot
, void *data ATTRIBUTE_UNUSED
)
611 histogram_value hist
= *(histogram_value
*) slot
;
612 free (hist
->hvalue
.counters
);
613 #ifdef ENABLE_CHECKING
614 memset (hist
, 0xab, sizeof (*hist
));
621 free_histograms (void)
623 if (VALUE_HISTOGRAMS (cfun
))
625 htab_traverse (VALUE_HISTOGRAMS (cfun
), free_hist
, NULL
);
626 htab_delete (VALUE_HISTOGRAMS (cfun
));
627 VALUE_HISTOGRAMS (cfun
) = NULL
;
632 /* The overall number of invocations of the counter should match
633 execution count of basic block. Report it as error rather than
634 internal error as it might mean that user has misused the profile
638 check_counter (gimple stmt
, const char * name
,
639 gcov_type
*count
, gcov_type
*all
, gcov_type bb_count
)
641 if (*all
!= bb_count
|| *count
> *all
)
644 locus
= (stmt
!= NULL
)
645 ? gimple_location (stmt
)
646 : DECL_SOURCE_LOCATION (current_function_decl
);
647 if (flag_profile_correction
)
649 if (dump_enabled_p ())
650 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
651 "correcting inconsistent value profile: %s "
652 "profiler overall count (%d) does not match BB "
653 "count (%d)\n", name
, (int)*all
, (int)bb_count
);
661 error_at (locus
, "corrupted value profile: %s "
662 "profile counter (%d out of %d) inconsistent with "
663 "basic-block count (%d)",
676 /* GIMPLE based transformations. */
679 gimple_value_profile_transformations (void)
682 gimple_stmt_iterator gsi
;
683 bool changed
= false;
685 FOR_EACH_BB_FN (bb
, cfun
)
687 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
689 gimple stmt
= gsi_stmt (gsi
);
690 histogram_value th
= gimple_histogram_value (cfun
, stmt
);
696 fprintf (dump_file
, "Trying transformations on stmt ");
697 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
698 dump_histograms_for_stmt (cfun
, dump_file
, stmt
);
701 /* Transformations: */
702 /* The order of things in this conditional controls which
703 transformation is used when more than one is applicable. */
704 /* It is expected that any code added by the transformations
705 will be added before the current statement, and that the
706 current statement remain valid (although possibly
707 modified) upon return. */
708 if (gimple_mod_subtract_transform (&gsi
)
709 || gimple_divmod_fixed_value_transform (&gsi
)
710 || gimple_mod_pow2_value_transform (&gsi
)
711 || gimple_stringops_transform (&gsi
)
712 || gimple_ic_transform (&gsi
))
714 stmt
= gsi_stmt (gsi
);
716 /* Original statement may no longer be in the same block. */
717 if (bb
!= gimple_bb (stmt
))
719 bb
= gimple_bb (stmt
);
720 gsi
= gsi_for_stmt (stmt
);
735 /* Generate code for transformation 1 (with parent gimple assignment
736 STMT and probability of taking the optimal path PROB, which is
737 equivalent to COUNT/ALL within roundoff error). This generates the
738 result into a temp and returns the temp; it does not replace or
739 alter the original STMT. */
742 gimple_divmod_fixed_value (gassign
*stmt
, tree value
, int prob
,
743 gcov_type count
, gcov_type all
)
745 gassign
*stmt1
, *stmt2
;
747 tree tmp0
, tmp1
, tmp2
;
748 gimple bb1end
, bb2end
, bb3end
;
749 basic_block bb
, bb2
, bb3
, bb4
;
750 tree optype
, op1
, op2
;
751 edge e12
, e13
, e23
, e24
, e34
;
752 gimple_stmt_iterator gsi
;
754 gcc_assert (is_gimple_assign (stmt
)
755 && (gimple_assign_rhs_code (stmt
) == TRUNC_DIV_EXPR
756 || gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
));
758 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
759 op1
= gimple_assign_rhs1 (stmt
);
760 op2
= gimple_assign_rhs2 (stmt
);
762 bb
= gimple_bb (stmt
);
763 gsi
= gsi_for_stmt (stmt
);
765 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
766 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
767 stmt1
= gimple_build_assign (tmp0
, fold_convert (optype
, value
));
768 stmt2
= gimple_build_assign (tmp1
, op2
);
769 stmt3
= gimple_build_cond (NE_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
770 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
771 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
772 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
775 tmp2
= create_tmp_reg (optype
, "PROF");
776 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, tmp0
);
777 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
780 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, op2
);
781 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
785 /* Edge e23 connects bb2 to bb3, etc. */
786 e12
= split_block (bb
, bb1end
);
789 e23
= split_block (bb2
, bb2end
);
791 bb3
->count
= all
- count
;
792 e34
= split_block (bb3
, bb3end
);
796 e12
->flags
&= ~EDGE_FALLTHRU
;
797 e12
->flags
|= EDGE_FALSE_VALUE
;
798 e12
->probability
= prob
;
801 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
802 e13
->probability
= REG_BR_PROB_BASE
- prob
;
803 e13
->count
= all
- count
;
807 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
808 e24
->probability
= REG_BR_PROB_BASE
;
811 e34
->probability
= REG_BR_PROB_BASE
;
812 e34
->count
= all
- count
;
818 /* Do transform 1) on INSN if applicable. */
821 gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*si
)
823 histogram_value histogram
;
825 gcov_type val
, count
, all
;
826 tree result
, value
, tree_val
;
830 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
834 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
))))
837 code
= gimple_assign_rhs_code (stmt
);
839 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
842 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
843 HIST_TYPE_SINGLE_VALUE
);
847 value
= histogram
->hvalue
.value
;
848 val
= histogram
->hvalue
.counters
[0];
849 count
= histogram
->hvalue
.counters
[1];
850 all
= histogram
->hvalue
.counters
[2];
851 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
853 /* We require that count is at least half of all; this means
854 that for the transformation to fire the value must be constant
855 at least 50% of time (and 75% gives the guarantee of usage). */
856 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
858 || optimize_bb_for_size_p (gimple_bb (stmt
)))
861 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
864 /* Compute probability of taking the optimal path. */
866 prob
= GCOV_COMPUTE_SCALE (count
, all
);
870 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
871 tree_val
= build_int_cst (get_gcov_type (), val
);
875 a
[0] = (unsigned HOST_WIDE_INT
) val
;
876 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
878 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
879 TYPE_PRECISION (get_gcov_type ()), false));
881 result
= gimple_divmod_fixed_value (stmt
, tree_val
, prob
, count
, all
);
885 fprintf (dump_file
, "Div/mod by constant ");
886 print_generic_expr (dump_file
, value
, TDF_SLIM
);
887 fprintf (dump_file
, "=");
888 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
889 fprintf (dump_file
, " transformation on insn ");
890 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
893 gimple_assign_set_rhs_from_tree (si
, result
);
894 update_stmt (gsi_stmt (*si
));
899 /* Generate code for transformation 2 (with parent gimple assign STMT and
900 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
901 within roundoff error). This generates the result into a temp and returns
902 the temp; it does not replace or alter the original STMT. */
904 gimple_mod_pow2 (gassign
*stmt
, int prob
, gcov_type count
, gcov_type all
)
906 gassign
*stmt1
, *stmt2
, *stmt3
;
909 gimple bb1end
, bb2end
, bb3end
;
910 basic_block bb
, bb2
, bb3
, bb4
;
911 tree optype
, op1
, op2
;
912 edge e12
, e13
, e23
, e24
, e34
;
913 gimple_stmt_iterator gsi
;
916 gcc_assert (is_gimple_assign (stmt
)
917 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
919 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
920 op1
= gimple_assign_rhs1 (stmt
);
921 op2
= gimple_assign_rhs2 (stmt
);
923 bb
= gimple_bb (stmt
);
924 gsi
= gsi_for_stmt (stmt
);
926 result
= create_tmp_reg (optype
, "PROF");
927 tmp2
= make_temp_ssa_name (optype
, NULL
, "PROF");
928 tmp3
= make_temp_ssa_name (optype
, NULL
, "PROF");
929 stmt2
= gimple_build_assign (tmp2
, PLUS_EXPR
, op2
,
930 build_int_cst (optype
, -1));
931 stmt3
= gimple_build_assign (tmp3
, BIT_AND_EXPR
, tmp2
, op2
);
932 stmt4
= gimple_build_cond (NE_EXPR
, tmp3
, build_int_cst (optype
, 0),
933 NULL_TREE
, NULL_TREE
);
934 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
935 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
936 gsi_insert_before (&gsi
, stmt4
, GSI_SAME_STMT
);
939 /* tmp2 == op2-1 inherited from previous block. */
940 stmt1
= gimple_build_assign (result
, BIT_AND_EXPR
, op1
, tmp2
);
941 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
944 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
946 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
950 /* Edge e23 connects bb2 to bb3, etc. */
951 e12
= split_block (bb
, bb1end
);
954 e23
= split_block (bb2
, bb2end
);
956 bb3
->count
= all
- count
;
957 e34
= split_block (bb3
, bb3end
);
961 e12
->flags
&= ~EDGE_FALLTHRU
;
962 e12
->flags
|= EDGE_FALSE_VALUE
;
963 e12
->probability
= prob
;
966 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
967 e13
->probability
= REG_BR_PROB_BASE
- prob
;
968 e13
->count
= all
- count
;
972 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
973 e24
->probability
= REG_BR_PROB_BASE
;
976 e34
->probability
= REG_BR_PROB_BASE
;
977 e34
->count
= all
- count
;
982 /* Do transform 2) on INSN if applicable. */
984 gimple_mod_pow2_value_transform (gimple_stmt_iterator
*si
)
986 histogram_value histogram
;
988 gcov_type count
, wrong_values
, all
;
989 tree lhs_type
, result
, value
;
993 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
997 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
998 if (!INTEGRAL_TYPE_P (lhs_type
))
1001 code
= gimple_assign_rhs_code (stmt
);
1003 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1006 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_POW2
);
1010 value
= histogram
->hvalue
.value
;
1011 wrong_values
= histogram
->hvalue
.counters
[0];
1012 count
= histogram
->hvalue
.counters
[1];
1014 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1016 /* We require that we hit a power of 2 at least half of all evaluations. */
1017 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
1018 || count
< wrong_values
1019 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1024 fprintf (dump_file
, "Mod power of 2 transformation on insn ");
1025 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1028 /* Compute probability of taking the optimal path. */
1029 all
= count
+ wrong_values
;
1031 if (check_counter (stmt
, "pow2", &count
, &all
, gimple_bb (stmt
)->count
))
1035 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1039 result
= gimple_mod_pow2 (stmt
, prob
, count
, all
);
1041 gimple_assign_set_rhs_from_tree (si
, result
);
1042 update_stmt (gsi_stmt (*si
));
1047 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1048 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1049 supported and this is built into this interface. The probabilities of taking
1050 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1051 COUNT2/ALL respectively within roundoff error). This generates the
1052 result into a temp and returns the temp; it does not replace or alter
1053 the original STMT. */
1054 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1057 gimple_mod_subtract (gassign
*stmt
, int prob1
, int prob2
, int ncounts
,
1058 gcov_type count1
, gcov_type count2
, gcov_type all
)
1064 gimple bb1end
, bb2end
= NULL
, bb3end
;
1065 basic_block bb
, bb2
, bb3
, bb4
;
1066 tree optype
, op1
, op2
;
1067 edge e12
, e23
= 0, e24
, e34
, e14
;
1068 gimple_stmt_iterator gsi
;
1071 gcc_assert (is_gimple_assign (stmt
)
1072 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
1074 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1075 op1
= gimple_assign_rhs1 (stmt
);
1076 op2
= gimple_assign_rhs2 (stmt
);
1078 bb
= gimple_bb (stmt
);
1079 gsi
= gsi_for_stmt (stmt
);
1081 result
= create_tmp_reg (optype
, "PROF");
1082 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1083 stmt1
= gimple_build_assign (result
, op1
);
1084 stmt2
= gimple_build_assign (tmp1
, op2
);
1085 stmt3
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1086 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1087 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1088 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
1091 if (ncounts
) /* Assumed to be 0 or 1 */
1093 stmt1
= gimple_build_assign (result
, MINUS_EXPR
, result
, tmp1
);
1094 stmt2
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1095 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1096 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1100 /* Fallback case. */
1101 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
1103 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1107 /* Edge e23 connects bb2 to bb3, etc. */
1108 /* However block 3 is optional; if it is not there, references
1109 to 3 really refer to block 2. */
1110 e12
= split_block (bb
, bb1end
);
1112 bb2
->count
= all
- count1
;
1114 if (ncounts
) /* Assumed to be 0 or 1. */
1116 e23
= split_block (bb2
, bb2end
);
1118 bb3
->count
= all
- count1
- count2
;
1121 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
1125 e12
->flags
&= ~EDGE_FALLTHRU
;
1126 e12
->flags
|= EDGE_FALSE_VALUE
;
1127 e12
->probability
= REG_BR_PROB_BASE
- prob1
;
1128 e12
->count
= all
- count1
;
1130 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
1131 e14
->probability
= prob1
;
1132 e14
->count
= count1
;
1134 if (ncounts
) /* Assumed to be 0 or 1. */
1136 e23
->flags
&= ~EDGE_FALLTHRU
;
1137 e23
->flags
|= EDGE_FALSE_VALUE
;
1138 e23
->count
= all
- count1
- count2
;
1139 e23
->probability
= REG_BR_PROB_BASE
- prob2
;
1141 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
1142 e24
->probability
= prob2
;
1143 e24
->count
= count2
;
1146 e34
->probability
= REG_BR_PROB_BASE
;
1147 e34
->count
= all
- count1
- count2
;
1153 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1156 gimple_mod_subtract_transform (gimple_stmt_iterator
*si
)
1158 histogram_value histogram
;
1159 enum tree_code code
;
1160 gcov_type count
, wrong_values
, all
;
1161 tree lhs_type
, result
;
1162 gcov_type prob1
, prob2
;
1163 unsigned int i
, steps
;
1164 gcov_type count1
, count2
;
1167 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
1171 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1172 if (!INTEGRAL_TYPE_P (lhs_type
))
1175 code
= gimple_assign_rhs_code (stmt
);
1177 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1180 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INTERVAL
);
1186 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1187 all
+= histogram
->hvalue
.counters
[i
];
1189 wrong_values
+= histogram
->hvalue
.counters
[i
];
1190 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
1191 steps
= histogram
->hdata
.intvl
.steps
;
1192 all
+= wrong_values
;
1193 count1
= histogram
->hvalue
.counters
[0];
1194 count2
= histogram
->hvalue
.counters
[1];
1196 /* Compute probability of taking the optimal path. */
1197 if (check_counter (stmt
, "interval", &count1
, &all
, gimple_bb (stmt
)->count
))
1199 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1203 if (flag_profile_correction
&& count1
+ count2
> all
)
1204 all
= count1
+ count2
;
1206 gcc_assert (count1
+ count2
<= all
);
1208 /* We require that we use just subtractions in at least 50% of all
1211 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1213 count
+= histogram
->hvalue
.counters
[i
];
1214 if (count
* 2 >= all
)
1218 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1221 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1224 fprintf (dump_file
, "Mod subtract transformation on insn ");
1225 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1228 /* Compute probability of taking the optimal path(s). */
1231 prob1
= GCOV_COMPUTE_SCALE (count1
, all
);
1232 prob2
= GCOV_COMPUTE_SCALE (count2
, all
);
1239 /* In practice, "steps" is always 2. This interface reflects this,
1240 and will need to be changed if "steps" can change. */
1241 result
= gimple_mod_subtract (stmt
, prob1
, prob2
, i
, count1
, count2
, all
);
1243 gimple_assign_set_rhs_from_tree (si
, result
);
1244 update_stmt (gsi_stmt (*si
));
1249 typedef int_hash
<unsigned int, 0, UINT_MAX
> profile_id_hash
;
1251 static hash_map
<profile_id_hash
, cgraph_node
*> *cgraph_node_map
= 0;
1253 /* Returns true if node graph is initialized. This
1254 is used to test if profile_id has been created
1255 for cgraph_nodes. */
1258 coverage_node_map_initialized_p (void)
1260 return cgraph_node_map
!= 0;
1263 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1264 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1265 that the PROFILE_IDs was already assigned. */
1268 init_node_map (bool local
)
1270 struct cgraph_node
*n
;
1271 cgraph_node_map
= new hash_map
<profile_id_hash
, cgraph_node
*>;
1273 FOR_EACH_DEFINED_FUNCTION (n
)
1274 if (n
->has_gimple_body_p ())
1279 n
->profile_id
= coverage_compute_profile_id (n
);
1280 while ((val
= cgraph_node_map
->get (n
->profile_id
))
1284 fprintf (dump_file
, "Local profile-id %i conflict"
1285 " with nodes %s/%i %s/%i\n",
1291 n
->profile_id
= (n
->profile_id
+ 1) & 0x7fffffff;
1294 else if (!n
->profile_id
)
1298 "Node %s/%i has no profile-id"
1299 " (profile feedback missing?)\n",
1304 else if ((val
= cgraph_node_map
->get (n
->profile_id
)))
1308 "Node %s/%i has IP profile-id %i conflict. "
1316 cgraph_node_map
->put (n
->profile_id
, n
);
1320 /* Delete the CGRAPH_NODE_MAP. */
1325 delete cgraph_node_map
;
1328 /* Return cgraph node for function with pid */
1331 find_func_by_profile_id (int profile_id
)
1333 cgraph_node
**val
= cgraph_node_map
->get (profile_id
);
1340 /* Perform sanity check on the indirect call target. Due to race conditions,
1341 false function target may be attributed to an indirect call site. If the
1342 call expression type mismatches with the target function's type, expand_call
1343 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1344 Returns true if TARGET is considered ok for call CALL_STMT. */
1347 check_ic_target (gcall
*call_stmt
, struct cgraph_node
*target
)
1350 if (gimple_check_call_matching_types (call_stmt
, target
->decl
, true))
1353 locus
= gimple_location (call_stmt
);
1354 if (dump_enabled_p ())
1355 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
1356 "Skipping target %s with mismatching types for icall\n",
1361 /* Do transformation
1363 if (actual_callee_address == address_of_most_common_function/method)
1370 gimple_ic (gcall
*icall_stmt
, struct cgraph_node
*direct_call
,
1371 int prob
, gcov_type count
, gcov_type all
)
1376 gcall
*iretbnd_stmt
= NULL
;
1377 tree tmp0
, tmp1
, tmp
;
1378 basic_block cond_bb
, dcall_bb
, icall_bb
, join_bb
= NULL
;
1379 tree optype
= build_pointer_type (void_type_node
);
1380 edge e_cd
, e_ci
, e_di
, e_dj
= NULL
, e_ij
;
1381 gimple_stmt_iterator gsi
;
1385 gimple_stmt_iterator psi
;
1387 cond_bb
= gimple_bb (icall_stmt
);
1388 gsi
= gsi_for_stmt (icall_stmt
);
1390 if (gimple_call_with_bounds_p (icall_stmt
) && gimple_call_lhs (icall_stmt
))
1391 iretbnd_stmt
= chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt
));
1393 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1394 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1395 tmp
= unshare_expr (gimple_call_fn (icall_stmt
));
1396 load_stmt
= gimple_build_assign (tmp0
, tmp
);
1397 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1399 tmp
= fold_convert (optype
, build_addr (direct_call
->decl
,
1400 current_function_decl
));
1401 load_stmt
= gimple_build_assign (tmp1
, tmp
);
1402 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1404 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1405 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1407 gimple_set_vdef (icall_stmt
, NULL_TREE
);
1408 gimple_set_vuse (icall_stmt
, NULL_TREE
);
1409 update_stmt (icall_stmt
);
1410 dcall_stmt
= as_a
<gcall
*> (gimple_copy (icall_stmt
));
1411 gimple_call_set_fndecl (dcall_stmt
, direct_call
->decl
);
1412 dflags
= flags_from_decl_or_type (direct_call
->decl
);
1413 if ((dflags
& ECF_NORETURN
) != 0)
1414 gimple_call_set_lhs (dcall_stmt
, NULL_TREE
);
1415 gsi_insert_before (&gsi
, dcall_stmt
, GSI_SAME_STMT
);
1418 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1419 e_cd
= split_block (cond_bb
, cond_stmt
);
1420 dcall_bb
= e_cd
->dest
;
1421 dcall_bb
->count
= count
;
1423 e_di
= split_block (dcall_bb
, dcall_stmt
);
1424 icall_bb
= e_di
->dest
;
1425 icall_bb
->count
= all
- count
;
1427 /* Do not disturb existing EH edges from the indirect call. */
1428 if (!stmt_ends_bb_p (icall_stmt
))
1429 e_ij
= split_block (icall_bb
, icall_stmt
);
1432 e_ij
= find_fallthru_edge (icall_bb
->succs
);
1433 /* The indirect call might be noreturn. */
1436 e_ij
->probability
= REG_BR_PROB_BASE
;
1437 e_ij
->count
= all
- count
;
1438 e_ij
= single_pred_edge (split_edge (e_ij
));
1443 join_bb
= e_ij
->dest
;
1444 join_bb
->count
= all
;
1447 e_cd
->flags
= (e_cd
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1448 e_cd
->probability
= prob
;
1449 e_cd
->count
= count
;
1451 e_ci
= make_edge (cond_bb
, icall_bb
, EDGE_FALSE_VALUE
);
1452 e_ci
->probability
= REG_BR_PROB_BASE
- prob
;
1453 e_ci
->count
= all
- count
;
1459 if ((dflags
& ECF_NORETURN
) != 0)
1463 e_dj
= make_edge (dcall_bb
, join_bb
, EDGE_FALLTHRU
);
1464 e_dj
->probability
= REG_BR_PROB_BASE
;
1465 e_dj
->count
= count
;
1467 e_ij
->count
= all
- count
;
1469 e_ij
->probability
= REG_BR_PROB_BASE
;
1472 /* Insert PHI node for the call result if necessary. */
1473 if (gimple_call_lhs (icall_stmt
)
1474 && TREE_CODE (gimple_call_lhs (icall_stmt
)) == SSA_NAME
1475 && (dflags
& ECF_NORETURN
) == 0)
1477 tree result
= gimple_call_lhs (icall_stmt
);
1478 gphi
*phi
= create_phi_node (result
, join_bb
);
1479 gimple_call_set_lhs (icall_stmt
,
1480 duplicate_ssa_name (result
, icall_stmt
));
1481 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1482 gimple_call_set_lhs (dcall_stmt
,
1483 duplicate_ssa_name (result
, dcall_stmt
));
1484 add_phi_arg (phi
, gimple_call_lhs (dcall_stmt
), e_dj
, UNKNOWN_LOCATION
);
1486 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1487 call then we need to make it's copy for the direct
1491 if (gimple_call_lhs (iretbnd_stmt
))
1495 gimple_set_vdef (iretbnd_stmt
, NULL_TREE
);
1496 gimple_set_vuse (iretbnd_stmt
, NULL_TREE
);
1497 update_stmt (iretbnd_stmt
);
1499 result
= gimple_call_lhs (iretbnd_stmt
);
1500 phi
= create_phi_node (result
, join_bb
);
1502 copy
= gimple_copy (iretbnd_stmt
);
1503 gimple_call_set_arg (copy
, 0,
1504 gimple_call_lhs (dcall_stmt
));
1505 gimple_call_set_lhs (copy
, duplicate_ssa_name (result
, copy
));
1506 gsi_insert_on_edge (e_dj
, copy
);
1507 add_phi_arg (phi
, gimple_call_lhs (copy
),
1508 e_dj
, UNKNOWN_LOCATION
);
1510 gimple_call_set_arg (iretbnd_stmt
, 0,
1511 gimple_call_lhs (icall_stmt
));
1512 gimple_call_set_lhs (iretbnd_stmt
,
1513 duplicate_ssa_name (result
, iretbnd_stmt
));
1514 psi
= gsi_for_stmt (iretbnd_stmt
);
1515 gsi_remove (&psi
, false);
1516 gsi_insert_on_edge (e_ij
, iretbnd_stmt
);
1517 add_phi_arg (phi
, gimple_call_lhs (iretbnd_stmt
),
1518 e_ij
, UNKNOWN_LOCATION
);
1520 gsi_commit_one_edge_insert (e_dj
, NULL
);
1521 gsi_commit_one_edge_insert (e_ij
, NULL
);
1525 psi
= gsi_for_stmt (iretbnd_stmt
);
1526 gsi_remove (&psi
, true);
1531 /* Build an EH edge for the direct call if necessary. */
1532 lp_nr
= lookup_stmt_eh_lp (icall_stmt
);
1533 if (lp_nr
> 0 && stmt_could_throw_p (dcall_stmt
))
1535 add_stmt_to_eh_lp (dcall_stmt
, lp_nr
);
1538 FOR_EACH_EDGE (e_eh
, ei
, icall_bb
->succs
)
1539 if (e_eh
->flags
& (EDGE_EH
| EDGE_ABNORMAL
))
1541 e
= make_edge (dcall_bb
, e_eh
->dest
, e_eh
->flags
);
1542 for (gphi_iterator psi
= gsi_start_phis (e_eh
->dest
);
1543 !gsi_end_p (psi
); gsi_next (&psi
))
1545 gphi
*phi
= psi
.phi ();
1546 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
),
1547 PHI_ARG_DEF_FROM_EDGE (phi
, e_eh
));
1550 if (!stmt_could_throw_p (dcall_stmt
))
1551 gimple_purge_dead_eh_edges (dcall_bb
);
1556 For every checked indirect/virtual call determine if most common pid of
1557 function/class method has probability more than 50%. If yes modify code of
1562 gimple_ic_transform (gimple_stmt_iterator
*gsi
)
1565 histogram_value histogram
;
1566 gcov_type val
, count
, all
, bb_all
;
1567 struct cgraph_node
*direct_call
;
1569 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1573 if (gimple_call_fndecl (stmt
) != NULL_TREE
)
1576 if (gimple_call_internal_p (stmt
))
1579 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INDIR_CALL
);
1583 val
= histogram
->hvalue
.counters
[0];
1584 count
= histogram
->hvalue
.counters
[1];
1585 all
= histogram
->hvalue
.counters
[2];
1587 bb_all
= gimple_bb (stmt
)->count
;
1588 /* The order of CHECK_COUNTER calls is important -
1589 since check_counter can correct the third parameter
1590 and we want to make count <= all <= bb_all. */
1591 if ( check_counter (stmt
, "ic", &all
, &bb_all
, bb_all
)
1592 || check_counter (stmt
, "ic", &count
, &all
, all
))
1594 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1598 if (4 * count
<= 3 * all
)
1601 direct_call
= find_func_by_profile_id ((int)val
);
1603 if (direct_call
== NULL
)
1609 fprintf (dump_file
, "Indirect call -> direct call from other module");
1610 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1611 fprintf (dump_file
, "=> %i (will resolve only with LTO)\n", (int)val
);
1617 if (!check_ic_target (stmt
, direct_call
))
1621 fprintf (dump_file
, "Indirect call -> direct call ");
1622 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1623 fprintf (dump_file
, "=> ");
1624 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1625 fprintf (dump_file
, " transformation skipped because of type mismatch");
1626 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1628 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1634 fprintf (dump_file
, "Indirect call -> direct call ");
1635 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1636 fprintf (dump_file
, "=> ");
1637 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1638 fprintf (dump_file
, " transformation on insn postponned to ipa-profile");
1639 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1640 fprintf (dump_file
, "hist->count %" PRId64
1641 " hist->all %" PRId64
"\n", count
, all
);
1647 /* Return true if the stringop CALL with FNDECL shall be profiled.
1648 SIZE_ARG be set to the argument index for the size of the string
1652 interesting_stringop_to_profile_p (tree fndecl
, gcall
*call
, int *size_arg
)
1654 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
1656 if (fcode
!= BUILT_IN_MEMCPY
&& fcode
!= BUILT_IN_MEMPCPY
1657 && fcode
!= BUILT_IN_MEMSET
&& fcode
!= BUILT_IN_BZERO
)
1662 case BUILT_IN_MEMCPY
:
1663 case BUILT_IN_MEMPCPY
:
1665 return validate_gimple_arglist (call
, POINTER_TYPE
, POINTER_TYPE
,
1666 INTEGER_TYPE
, VOID_TYPE
);
1667 case BUILT_IN_MEMSET
:
1669 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1670 INTEGER_TYPE
, VOID_TYPE
);
1671 case BUILT_IN_BZERO
:
1673 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1680 /* Convert stringop (..., vcall_size)
1682 if (vcall_size == icall_size)
1683 stringop (..., icall_size);
1685 stringop (..., vcall_size);
1686 assuming we'll propagate a true constant into ICALL_SIZE later. */
1689 gimple_stringop_fixed_value (gcall
*vcall_stmt
, tree icall_size
, int prob
,
1690 gcov_type count
, gcov_type all
)
1695 tree tmp0
, tmp1
, vcall_size
, optype
;
1696 basic_block cond_bb
, icall_bb
, vcall_bb
, join_bb
;
1697 edge e_ci
, e_cv
, e_iv
, e_ij
, e_vj
;
1698 gimple_stmt_iterator gsi
;
1702 fndecl
= gimple_call_fndecl (vcall_stmt
);
1703 if (!interesting_stringop_to_profile_p (fndecl
, vcall_stmt
, &size_arg
))
1706 cond_bb
= gimple_bb (vcall_stmt
);
1707 gsi
= gsi_for_stmt (vcall_stmt
);
1709 vcall_size
= gimple_call_arg (vcall_stmt
, size_arg
);
1710 optype
= TREE_TYPE (vcall_size
);
1712 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1713 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1714 tmp_stmt
= gimple_build_assign (tmp0
, fold_convert (optype
, icall_size
));
1715 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1717 tmp_stmt
= gimple_build_assign (tmp1
, vcall_size
);
1718 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1720 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1721 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1723 gimple_set_vdef (vcall_stmt
, NULL
);
1724 gimple_set_vuse (vcall_stmt
, NULL
);
1725 update_stmt (vcall_stmt
);
1726 icall_stmt
= as_a
<gcall
*> (gimple_copy (vcall_stmt
));
1727 gimple_call_set_arg (icall_stmt
, size_arg
, icall_size
);
1728 gsi_insert_before (&gsi
, icall_stmt
, GSI_SAME_STMT
);
1731 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1732 e_ci
= split_block (cond_bb
, cond_stmt
);
1733 icall_bb
= e_ci
->dest
;
1734 icall_bb
->count
= count
;
1736 e_iv
= split_block (icall_bb
, icall_stmt
);
1737 vcall_bb
= e_iv
->dest
;
1738 vcall_bb
->count
= all
- count
;
1740 e_vj
= split_block (vcall_bb
, vcall_stmt
);
1741 join_bb
= e_vj
->dest
;
1742 join_bb
->count
= all
;
1744 e_ci
->flags
= (e_ci
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1745 e_ci
->probability
= prob
;
1746 e_ci
->count
= count
;
1748 e_cv
= make_edge (cond_bb
, vcall_bb
, EDGE_FALSE_VALUE
);
1749 e_cv
->probability
= REG_BR_PROB_BASE
- prob
;
1750 e_cv
->count
= all
- count
;
1754 e_ij
= make_edge (icall_bb
, join_bb
, EDGE_FALLTHRU
);
1755 e_ij
->probability
= REG_BR_PROB_BASE
;
1756 e_ij
->count
= count
;
1758 e_vj
->probability
= REG_BR_PROB_BASE
;
1759 e_vj
->count
= all
- count
;
1761 /* Insert PHI node for the call result if necessary. */
1762 if (gimple_call_lhs (vcall_stmt
)
1763 && TREE_CODE (gimple_call_lhs (vcall_stmt
)) == SSA_NAME
)
1765 tree result
= gimple_call_lhs (vcall_stmt
);
1766 gphi
*phi
= create_phi_node (result
, join_bb
);
1767 gimple_call_set_lhs (vcall_stmt
,
1768 duplicate_ssa_name (result
, vcall_stmt
));
1769 add_phi_arg (phi
, gimple_call_lhs (vcall_stmt
), e_vj
, UNKNOWN_LOCATION
);
1770 gimple_call_set_lhs (icall_stmt
,
1771 duplicate_ssa_name (result
, icall_stmt
));
1772 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1775 /* Because these are all string op builtins, they're all nothrow. */
1776 gcc_assert (!stmt_could_throw_p (vcall_stmt
));
1777 gcc_assert (!stmt_could_throw_p (icall_stmt
));
1780 /* Find values inside STMT for that we want to measure histograms for
1781 division/modulo optimization. */
1783 gimple_stringops_transform (gimple_stmt_iterator
*gsi
)
1788 enum built_in_function fcode
;
1789 histogram_value histogram
;
1790 gcov_type count
, all
, val
;
1792 unsigned int dest_align
, src_align
;
1797 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1800 fndecl
= gimple_call_fndecl (stmt
);
1803 fcode
= DECL_FUNCTION_CODE (fndecl
);
1804 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1807 blck_size
= gimple_call_arg (stmt
, size_arg
);
1808 if (TREE_CODE (blck_size
) == INTEGER_CST
)
1811 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_SINGLE_VALUE
);
1814 val
= histogram
->hvalue
.counters
[0];
1815 count
= histogram
->hvalue
.counters
[1];
1816 all
= histogram
->hvalue
.counters
[2];
1817 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1818 /* We require that count is at least half of all; this means
1819 that for the transformation to fire the value must be constant
1820 at least 80% of time. */
1821 if ((6 * count
/ 5) < all
|| optimize_bb_for_size_p (gimple_bb (stmt
)))
1823 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
1826 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1829 dest
= gimple_call_arg (stmt
, 0);
1830 dest_align
= get_pointer_alignment (dest
);
1833 case BUILT_IN_MEMCPY
:
1834 case BUILT_IN_MEMPCPY
:
1835 src
= gimple_call_arg (stmt
, 1);
1836 src_align
= get_pointer_alignment (src
);
1837 if (!can_move_by_pieces (val
, MIN (dest_align
, src_align
)))
1840 case BUILT_IN_MEMSET
:
1841 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1842 gimple_call_arg (stmt
, 1),
1846 case BUILT_IN_BZERO
:
1847 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1855 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
1856 tree_val
= build_int_cst (get_gcov_type (), val
);
1860 a
[0] = (unsigned HOST_WIDE_INT
) val
;
1861 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
1863 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
1864 TYPE_PRECISION (get_gcov_type ()), false));
1869 fprintf (dump_file
, "Single value %i stringop transformation on ",
1871 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1873 gimple_stringop_fixed_value (stmt
, tree_val
, prob
, count
, all
);
1879 stringop_block_profile (gimple stmt
, unsigned int *expected_align
,
1880 HOST_WIDE_INT
*expected_size
)
1882 histogram_value histogram
;
1883 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_AVERAGE
);
1885 *expected_size
= -1;
1886 else if (!histogram
->hvalue
.counters
[1])
1888 *expected_size
= -1;
1889 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1894 size
= ((histogram
->hvalue
.counters
[0]
1895 + histogram
->hvalue
.counters
[1] / 2)
1896 / histogram
->hvalue
.counters
[1]);
1897 /* Even if we can hold bigger value in SIZE, INT_MAX
1898 is safe "infinity" for code generation strategies. */
1901 *expected_size
= size
;
1902 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1904 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_IOR
);
1906 *expected_align
= 0;
1907 else if (!histogram
->hvalue
.counters
[0])
1909 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1910 *expected_align
= 0;
1917 count
= histogram
->hvalue
.counters
[0];
1919 while (!(count
& alignment
)
1920 && (alignment
* 2 * BITS_PER_UNIT
))
1922 *expected_align
= alignment
* BITS_PER_UNIT
;
1923 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1928 /* Find values inside STMT for that we want to measure histograms for
1929 division/modulo optimization. */
1931 gimple_divmod_values_to_profile (gimple stmt
, histogram_values
*values
)
1933 tree lhs
, divisor
, op0
, type
;
1934 histogram_value hist
;
1936 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1939 lhs
= gimple_assign_lhs (stmt
);
1940 type
= TREE_TYPE (lhs
);
1941 if (!INTEGRAL_TYPE_P (type
))
1944 switch (gimple_assign_rhs_code (stmt
))
1946 case TRUNC_DIV_EXPR
:
1947 case TRUNC_MOD_EXPR
:
1948 divisor
= gimple_assign_rhs2 (stmt
);
1949 op0
= gimple_assign_rhs1 (stmt
);
1951 values
->reserve (3);
1953 if (TREE_CODE (divisor
) == SSA_NAME
)
1954 /* Check for the case where the divisor is the same value most
1956 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1957 HIST_TYPE_SINGLE_VALUE
,
1960 /* For mod, check whether it is not often a noop (or replaceable by
1961 a few subtractions). */
1962 if (gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
1963 && TYPE_UNSIGNED (type
))
1966 /* Check for a special case where the divisor is power of 2. */
1967 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1971 val
= build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
1972 hist
= gimple_alloc_histogram_value (cfun
, HIST_TYPE_INTERVAL
,
1974 hist
->hdata
.intvl
.int_start
= 0;
1975 hist
->hdata
.intvl
.steps
= 2;
1976 values
->quick_push (hist
);
1985 /* Find calls inside STMT for that we want to measure histograms for
1986 indirect/virtual call optimization. */
1989 gimple_indirect_call_to_profile (gimple stmt
, histogram_values
*values
)
1993 if (gimple_code (stmt
) != GIMPLE_CALL
1994 || gimple_call_internal_p (stmt
)
1995 || gimple_call_fndecl (stmt
) != NULL_TREE
)
1998 callee
= gimple_call_fn (stmt
);
2000 values
->reserve (3);
2002 values
->quick_push (gimple_alloc_histogram_value (
2004 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE
) ?
2005 HIST_TYPE_INDIR_CALL_TOPN
:
2006 HIST_TYPE_INDIR_CALL
,
2012 /* Find values inside STMT for that we want to measure histograms for
2013 string operations. */
2015 gimple_stringops_values_to_profile (gimple gs
, histogram_values
*values
)
2023 stmt
= dyn_cast
<gcall
*> (gs
);
2026 fndecl
= gimple_call_fndecl (stmt
);
2030 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
2033 dest
= gimple_call_arg (stmt
, 0);
2034 blck_size
= gimple_call_arg (stmt
, size_arg
);
2036 if (TREE_CODE (blck_size
) != INTEGER_CST
)
2038 values
->safe_push (gimple_alloc_histogram_value (cfun
,
2039 HIST_TYPE_SINGLE_VALUE
,
2041 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_AVERAGE
,
2044 if (TREE_CODE (blck_size
) != INTEGER_CST
)
2045 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_IOR
,
2049 /* Find values inside STMT for that we want to measure histograms and adds
2050 them to list VALUES. */
2053 gimple_values_to_profile (gimple stmt
, histogram_values
*values
)
2055 gimple_divmod_values_to_profile (stmt
, values
);
2056 gimple_stringops_values_to_profile (stmt
, values
);
2057 gimple_indirect_call_to_profile (stmt
, values
);
2061 gimple_find_values_to_profile (histogram_values
*values
)
2064 gimple_stmt_iterator gsi
;
2066 histogram_value hist
= NULL
;
2069 FOR_EACH_BB_FN (bb
, cfun
)
2070 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2071 gimple_values_to_profile (gsi_stmt (gsi
), values
);
2073 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_TIME_PROFILE
, 0, 0));
2075 FOR_EACH_VEC_ELT (*values
, i
, hist
)
2079 case HIST_TYPE_INTERVAL
:
2080 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
2083 case HIST_TYPE_POW2
:
2084 hist
->n_counters
= 2;
2087 case HIST_TYPE_SINGLE_VALUE
:
2088 hist
->n_counters
= 3;
2091 case HIST_TYPE_CONST_DELTA
:
2092 hist
->n_counters
= 4;
2095 case HIST_TYPE_INDIR_CALL
:
2096 hist
->n_counters
= 3;
2099 case HIST_TYPE_TIME_PROFILE
:
2100 hist
->n_counters
= 1;
2103 case HIST_TYPE_AVERAGE
:
2104 hist
->n_counters
= 2;
2108 hist
->n_counters
= 1;
2111 case HIST_TYPE_INDIR_CALL_TOPN
:
2112 hist
->n_counters
= GCOV_ICALL_TOPN_NCOUNTS
;
2120 fprintf (dump_file
, "Stmt ");
2121 print_gimple_stmt (dump_file
, hist
->hvalue
.stmt
, 0, TDF_SLIM
);
2122 dump_histogram_value (dump_file
, hist
);