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 "double-int.h"
34 #include "fold-const.h"
35 #include "tree-nested.h"
39 #include "hard-reg-set.h"
42 #include "statistics.h"
44 #include "fixed-value.h"
45 #include "insn-config.h"
54 #include "dominance.h"
56 #include "basic-block.h"
57 #include "value-prof.h"
59 #include "insn-codes.h"
62 #include "tree-ssa-alias.h"
63 #include "internal-fn.h"
65 #include "gimple-expr.h"
69 #include "gimple-iterator.h"
70 #include "gimple-ssa.h"
72 #include "tree-phinodes.h"
73 #include "ssa-iterators.h"
74 #include "stringpool.h"
75 #include "tree-ssanames.h"
76 #include "diagnostic.h"
77 #include "gimple-pretty-print.h"
84 #include "plugin-api.h"
87 #include "data-streamer.h"
90 #include "tree-chkp.h"
92 /* In this file value profile based optimizations are placed. Currently the
93 following optimizations are implemented (for more detailed descriptions
94 see comments at value_profile_transformations):
96 1) Division/modulo specialization. Provided that we can determine that the
97 operands of the division have some special properties, we may use it to
98 produce more effective code.
100 2) Indirect/virtual call specialization. If we can determine most
101 common function callee in indirect/virtual call. We can use this
102 information to improve code effectiveness (especially info for
105 3) Speculative prefetching. If we are able to determine that the difference
106 between addresses accessed by a memory reference is usually constant, we
107 may add the prefetch instructions.
108 FIXME: This transformation was removed together with RTL based value
112 Value profiling internals
113 ==========================
115 Every value profiling transformation starts with defining what values
116 to profile. There are different histogram types (see HIST_TYPE_* in
117 value-prof.h) and each transformation can request one or more histogram
118 types per GIMPLE statement. The function gimple_find_values_to_profile()
119 collects the values to profile in a vec, and adds the number of counters
120 required for the different histogram types.
122 For a -fprofile-generate run, the statements for which values should be
123 recorded, are instrumented in instrument_values(). The instrumentation
124 is done by helper functions that can be found in tree-profile.c, where
125 new types of histograms can be added if necessary.
127 After a -fprofile-use, the value profiling data is read back in by
128 compute_value_histograms() that translates the collected data to
129 histograms and attaches them to the profiled statements via
130 gimple_add_histogram_value(). Histograms are stored in a hash table
131 that is attached to every intrumented function, see VALUE_HISTOGRAMS
134 The value-profile transformations driver is the function
135 gimple_value_profile_transformations(). It traverses all statements in
136 the to-be-transformed function, and looks for statements with one or
137 more histograms attached to it. If a statement has histograms, the
138 transformation functions are called on the statement.
140 Limitations / FIXME / TODO:
141 * Only one histogram of each type can be associated with a statement.
142 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
143 (This type of histogram was originally used to implement a form of
144 stride profiling based speculative prefetching to improve SPEC2000
145 scores for memory-bound benchmarks, mcf and equake. However, this
146 was an RTL value-profiling transformation, and those have all been
148 * Some value profile transformations are done in builtins.c (?!)
149 * Updating of histograms needs some TLC.
150 * The value profiling code could be used to record analysis results
151 from non-profiling (e.g. VRP).
152 * Adding new profilers should be simplified, starting with a cleanup
153 of what-happens-where andwith making gimple_find_values_to_profile
154 and gimple_value_profile_transformations table-driven, perhaps...
157 static tree
gimple_divmod_fixed_value (gassign
*, tree
, int, gcov_type
,
159 static tree
gimple_mod_pow2 (gassign
*, int, gcov_type
, gcov_type
);
160 static tree
gimple_mod_subtract (gassign
*, int, int, int, gcov_type
,
161 gcov_type
, gcov_type
);
162 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*);
163 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator
*);
164 static bool gimple_mod_subtract_transform (gimple_stmt_iterator
*);
165 static bool gimple_stringops_transform (gimple_stmt_iterator
*);
166 static bool gimple_ic_transform (gimple_stmt_iterator
*);
168 /* Allocate histogram value. */
171 gimple_alloc_histogram_value (struct function
*fun ATTRIBUTE_UNUSED
,
172 enum hist_type type
, gimple stmt
, tree value
)
174 histogram_value hist
= (histogram_value
) xcalloc (1, sizeof (*hist
));
175 hist
->hvalue
.value
= value
;
176 hist
->hvalue
.stmt
= stmt
;
181 /* Hash value for histogram. */
184 histogram_hash (const void *x
)
186 return htab_hash_pointer (((const_histogram_value
)x
)->hvalue
.stmt
);
189 /* Return nonzero if statement for histogram_value X is Y. */
192 histogram_eq (const void *x
, const void *y
)
194 return ((const_histogram_value
) x
)->hvalue
.stmt
== (const_gimple
) y
;
197 /* Set histogram for STMT. */
200 set_histogram_value (struct function
*fun
, gimple stmt
, histogram_value hist
)
203 if (!hist
&& !VALUE_HISTOGRAMS (fun
))
205 if (!VALUE_HISTOGRAMS (fun
))
206 VALUE_HISTOGRAMS (fun
) = htab_create (1, histogram_hash
,
208 loc
= htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
209 htab_hash_pointer (stmt
),
210 hist
? INSERT
: NO_INSERT
);
214 htab_clear_slot (VALUE_HISTOGRAMS (fun
), loc
);
220 /* Get histogram list for STMT. */
223 gimple_histogram_value (struct function
*fun
, gimple stmt
)
225 if (!VALUE_HISTOGRAMS (fun
))
227 return (histogram_value
) htab_find_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
228 htab_hash_pointer (stmt
));
231 /* Add histogram for STMT. */
234 gimple_add_histogram_value (struct function
*fun
, gimple stmt
,
235 histogram_value hist
)
237 hist
->hvalue
.next
= gimple_histogram_value (fun
, stmt
);
238 set_histogram_value (fun
, stmt
, hist
);
243 /* Remove histogram HIST from STMT's histogram list. */
246 gimple_remove_histogram_value (struct function
*fun
, gimple stmt
,
247 histogram_value hist
)
249 histogram_value hist2
= gimple_histogram_value (fun
, stmt
);
252 set_histogram_value (fun
, stmt
, hist
->hvalue
.next
);
256 while (hist2
->hvalue
.next
!= hist
)
257 hist2
= hist2
->hvalue
.next
;
258 hist2
->hvalue
.next
= hist
->hvalue
.next
;
260 free (hist
->hvalue
.counters
);
261 #ifdef ENABLE_CHECKING
262 memset (hist
, 0xab, sizeof (*hist
));
268 /* Lookup histogram of type TYPE in the STMT. */
271 gimple_histogram_value_of_type (struct function
*fun
, gimple stmt
,
274 histogram_value hist
;
275 for (hist
= gimple_histogram_value (fun
, stmt
); hist
;
276 hist
= hist
->hvalue
.next
)
277 if (hist
->type
== type
)
282 /* Dump information about HIST to DUMP_FILE. */
285 dump_histogram_value (FILE *dump_file
, histogram_value hist
)
289 case HIST_TYPE_INTERVAL
:
290 fprintf (dump_file
, "Interval counter range %d -- %d",
291 hist
->hdata
.intvl
.int_start
,
292 (hist
->hdata
.intvl
.int_start
293 + hist
->hdata
.intvl
.steps
- 1));
294 if (hist
->hvalue
.counters
)
297 fprintf (dump_file
, " [");
298 for (i
= 0; i
< hist
->hdata
.intvl
.steps
; i
++)
299 fprintf (dump_file
, " %d:%"PRId64
,
300 hist
->hdata
.intvl
.int_start
+ i
,
301 (int64_t) hist
->hvalue
.counters
[i
]);
302 fprintf (dump_file
, " ] outside range:%"PRId64
,
303 (int64_t) hist
->hvalue
.counters
[i
]);
305 fprintf (dump_file
, ".\n");
309 fprintf (dump_file
, "Pow2 counter ");
310 if (hist
->hvalue
.counters
)
312 fprintf (dump_file
, "pow2:%"PRId64
314 (int64_t) hist
->hvalue
.counters
[0],
315 (int64_t) hist
->hvalue
.counters
[1]);
317 fprintf (dump_file
, ".\n");
320 case HIST_TYPE_SINGLE_VALUE
:
321 fprintf (dump_file
, "Single value ");
322 if (hist
->hvalue
.counters
)
324 fprintf (dump_file
, "value:%"PRId64
327 (int64_t) hist
->hvalue
.counters
[0],
328 (int64_t) hist
->hvalue
.counters
[1],
329 (int64_t) hist
->hvalue
.counters
[2]);
331 fprintf (dump_file
, ".\n");
334 case HIST_TYPE_AVERAGE
:
335 fprintf (dump_file
, "Average value ");
336 if (hist
->hvalue
.counters
)
338 fprintf (dump_file
, "sum:%"PRId64
340 (int64_t) hist
->hvalue
.counters
[0],
341 (int64_t) hist
->hvalue
.counters
[1]);
343 fprintf (dump_file
, ".\n");
347 fprintf (dump_file
, "IOR value ");
348 if (hist
->hvalue
.counters
)
350 fprintf (dump_file
, "ior:%"PRId64
,
351 (int64_t) hist
->hvalue
.counters
[0]);
353 fprintf (dump_file
, ".\n");
356 case HIST_TYPE_CONST_DELTA
:
357 fprintf (dump_file
, "Constant delta ");
358 if (hist
->hvalue
.counters
)
360 fprintf (dump_file
, "value:%"PRId64
363 (int64_t) hist
->hvalue
.counters
[0],
364 (int64_t) hist
->hvalue
.counters
[1],
365 (int64_t) hist
->hvalue
.counters
[2]);
367 fprintf (dump_file
, ".\n");
369 case HIST_TYPE_INDIR_CALL
:
370 fprintf (dump_file
, "Indirect call ");
371 if (hist
->hvalue
.counters
)
373 fprintf (dump_file
, "value:%"PRId64
376 (int64_t) hist
->hvalue
.counters
[0],
377 (int64_t) hist
->hvalue
.counters
[1],
378 (int64_t) hist
->hvalue
.counters
[2]);
380 fprintf (dump_file
, ".\n");
382 case HIST_TYPE_TIME_PROFILE
:
383 fprintf (dump_file
, "Time profile ");
384 if (hist
->hvalue
.counters
)
386 fprintf (dump_file
, "time:%"PRId64
,
387 (int64_t) hist
->hvalue
.counters
[0]);
389 fprintf (dump_file
, ".\n");
391 case HIST_TYPE_INDIR_CALL_TOPN
:
392 fprintf (dump_file
, "Indirect call topn ");
393 if (hist
->hvalue
.counters
)
397 fprintf (dump_file
, "accu:%"PRId64
, hist
->hvalue
.counters
[0]);
398 for (i
= 1; i
< (GCOV_ICALL_TOPN_VAL
<< 2); i
+= 2)
400 fprintf (dump_file
, " target:%"PRId64
" value:%"PRId64
,
401 (int64_t) hist
->hvalue
.counters
[i
],
402 (int64_t) hist
->hvalue
.counters
[i
+1]);
405 fprintf (dump_file
, ".\n");
412 /* Dump information about HIST to DUMP_FILE. */
415 stream_out_histogram_value (struct output_block
*ob
, histogram_value hist
)
420 bp
= bitpack_create (ob
->main_stream
);
421 bp_pack_enum (&bp
, hist_type
, HIST_TYPE_MAX
, hist
->type
);
422 bp_pack_value (&bp
, hist
->hvalue
.next
!= NULL
, 1);
423 streamer_write_bitpack (&bp
);
426 case HIST_TYPE_INTERVAL
:
427 streamer_write_hwi (ob
, hist
->hdata
.intvl
.int_start
);
428 streamer_write_uhwi (ob
, hist
->hdata
.intvl
.steps
);
433 for (i
= 0; i
< hist
->n_counters
; i
++)
434 streamer_write_gcov_count (ob
, hist
->hvalue
.counters
[i
]);
435 if (hist
->hvalue
.next
)
436 stream_out_histogram_value (ob
, hist
->hvalue
.next
);
438 /* Dump information about HIST to DUMP_FILE. */
441 stream_in_histogram_value (struct lto_input_block
*ib
, gimple stmt
)
444 unsigned int ncounters
= 0;
447 histogram_value new_val
;
449 histogram_value
*next_p
= NULL
;
453 bp
= streamer_read_bitpack (ib
);
454 type
= bp_unpack_enum (&bp
, hist_type
, HIST_TYPE_MAX
);
455 next
= bp_unpack_value (&bp
, 1);
456 new_val
= gimple_alloc_histogram_value (cfun
, type
, stmt
, NULL
);
459 case HIST_TYPE_INTERVAL
:
460 new_val
->hdata
.intvl
.int_start
= streamer_read_hwi (ib
);
461 new_val
->hdata
.intvl
.steps
= streamer_read_uhwi (ib
);
462 ncounters
= new_val
->hdata
.intvl
.steps
+ 2;
466 case HIST_TYPE_AVERAGE
:
470 case HIST_TYPE_SINGLE_VALUE
:
471 case HIST_TYPE_INDIR_CALL
:
475 case HIST_TYPE_CONST_DELTA
:
480 case HIST_TYPE_TIME_PROFILE
:
484 case HIST_TYPE_INDIR_CALL_TOPN
:
485 ncounters
= (GCOV_ICALL_TOPN_VAL
<< 2) + 1;
491 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * ncounters
);
492 new_val
->n_counters
= ncounters
;
493 for (i
= 0; i
< ncounters
; i
++)
494 new_val
->hvalue
.counters
[i
] = streamer_read_gcov_count (ib
);
496 gimple_add_histogram_value (cfun
, stmt
, new_val
);
499 next_p
= &new_val
->hvalue
.next
;
504 /* Dump all histograms attached to STMT to DUMP_FILE. */
507 dump_histograms_for_stmt (struct function
*fun
, FILE *dump_file
, gimple stmt
)
509 histogram_value hist
;
510 for (hist
= gimple_histogram_value (fun
, stmt
); hist
; hist
= hist
->hvalue
.next
)
511 dump_histogram_value (dump_file
, hist
);
514 /* Remove all histograms associated with STMT. */
517 gimple_remove_stmt_histograms (struct function
*fun
, gimple stmt
)
520 while ((val
= gimple_histogram_value (fun
, stmt
)) != NULL
)
521 gimple_remove_histogram_value (fun
, stmt
, val
);
524 /* Duplicate all histograms associates with OSTMT to STMT. */
527 gimple_duplicate_stmt_histograms (struct function
*fun
, gimple stmt
,
528 struct function
*ofun
, gimple ostmt
)
531 for (val
= gimple_histogram_value (ofun
, ostmt
); val
!= NULL
; val
= val
->hvalue
.next
)
533 histogram_value new_val
= gimple_alloc_histogram_value (fun
, val
->type
, NULL
, NULL
);
534 memcpy (new_val
, val
, sizeof (*val
));
535 new_val
->hvalue
.stmt
= stmt
;
536 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
537 memcpy (new_val
->hvalue
.counters
, val
->hvalue
.counters
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
538 gimple_add_histogram_value (fun
, stmt
, new_val
);
543 /* Move all histograms associated with OSTMT to STMT. */
546 gimple_move_stmt_histograms (struct function
*fun
, gimple stmt
, gimple ostmt
)
548 histogram_value val
= gimple_histogram_value (fun
, ostmt
);
551 /* The following three statements can't be reordered,
552 because histogram hashtab relies on stmt field value
553 for finding the exact slot. */
554 set_histogram_value (fun
, ostmt
, NULL
);
555 for (; val
!= NULL
; val
= val
->hvalue
.next
)
556 val
->hvalue
.stmt
= stmt
;
557 set_histogram_value (fun
, stmt
, val
);
561 static bool error_found
= false;
563 /* Helper function for verify_histograms. For each histogram reachable via htab
564 walk verify that it was reached via statement walk. */
567 visit_hist (void **slot
, void *data
)
569 hash_set
<histogram_value
> *visited
= (hash_set
<histogram_value
> *) data
;
570 histogram_value hist
= *(histogram_value
*) slot
;
572 if (!visited
->contains (hist
)
573 && hist
->type
!= HIST_TYPE_TIME_PROFILE
)
575 error ("dead histogram");
576 dump_histogram_value (stderr
, hist
);
577 debug_gimple_stmt (hist
->hvalue
.stmt
);
584 /* Verify sanity of the histograms. */
587 verify_histograms (void)
590 gimple_stmt_iterator gsi
;
591 histogram_value hist
;
594 hash_set
<histogram_value
> visited_hists
;
595 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
);
600 for (hist
= gimple_histogram_value (cfun
, stmt
); hist
;
601 hist
= hist
->hvalue
.next
)
603 if (hist
->hvalue
.stmt
!= stmt
)
605 error ("Histogram value statement does not correspond to "
606 "the statement it is associated with");
607 debug_gimple_stmt (stmt
);
608 dump_histogram_value (stderr
, hist
);
611 visited_hists
.add (hist
);
614 if (VALUE_HISTOGRAMS (cfun
))
615 htab_traverse (VALUE_HISTOGRAMS (cfun
), visit_hist
, &visited_hists
);
617 internal_error ("verify_histograms failed");
620 /* Helper function for verify_histograms. For each histogram reachable via htab
621 walk verify that it was reached via statement walk. */
624 free_hist (void **slot
, void *data ATTRIBUTE_UNUSED
)
626 histogram_value hist
= *(histogram_value
*) slot
;
627 free (hist
->hvalue
.counters
);
628 #ifdef ENABLE_CHECKING
629 memset (hist
, 0xab, sizeof (*hist
));
636 free_histograms (void)
638 if (VALUE_HISTOGRAMS (cfun
))
640 htab_traverse (VALUE_HISTOGRAMS (cfun
), free_hist
, NULL
);
641 htab_delete (VALUE_HISTOGRAMS (cfun
));
642 VALUE_HISTOGRAMS (cfun
) = NULL
;
647 /* The overall number of invocations of the counter should match
648 execution count of basic block. Report it as error rather than
649 internal error as it might mean that user has misused the profile
653 check_counter (gimple stmt
, const char * name
,
654 gcov_type
*count
, gcov_type
*all
, gcov_type bb_count
)
656 if (*all
!= bb_count
|| *count
> *all
)
659 locus
= (stmt
!= NULL
)
660 ? gimple_location (stmt
)
661 : DECL_SOURCE_LOCATION (current_function_decl
);
662 if (flag_profile_correction
)
664 if (dump_enabled_p ())
665 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
666 "correcting inconsistent value profile: %s "
667 "profiler overall count (%d) does not match BB "
668 "count (%d)\n", name
, (int)*all
, (int)bb_count
);
676 error_at (locus
, "corrupted value profile: %s "
677 "profile counter (%d out of %d) inconsistent with "
678 "basic-block count (%d)",
691 /* GIMPLE based transformations. */
694 gimple_value_profile_transformations (void)
697 gimple_stmt_iterator gsi
;
698 bool changed
= false;
700 FOR_EACH_BB_FN (bb
, cfun
)
702 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
704 gimple stmt
= gsi_stmt (gsi
);
705 histogram_value th
= gimple_histogram_value (cfun
, stmt
);
711 fprintf (dump_file
, "Trying transformations on stmt ");
712 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
713 dump_histograms_for_stmt (cfun
, dump_file
, stmt
);
716 /* Transformations: */
717 /* The order of things in this conditional controls which
718 transformation is used when more than one is applicable. */
719 /* It is expected that any code added by the transformations
720 will be added before the current statement, and that the
721 current statement remain valid (although possibly
722 modified) upon return. */
723 if (gimple_mod_subtract_transform (&gsi
)
724 || gimple_divmod_fixed_value_transform (&gsi
)
725 || gimple_mod_pow2_value_transform (&gsi
)
726 || gimple_stringops_transform (&gsi
)
727 || gimple_ic_transform (&gsi
))
729 stmt
= gsi_stmt (gsi
);
731 /* Original statement may no longer be in the same block. */
732 if (bb
!= gimple_bb (stmt
))
734 bb
= gimple_bb (stmt
);
735 gsi
= gsi_for_stmt (stmt
);
750 /* Generate code for transformation 1 (with parent gimple assignment
751 STMT and probability of taking the optimal path PROB, which is
752 equivalent to COUNT/ALL within roundoff error). This generates the
753 result into a temp and returns the temp; it does not replace or
754 alter the original STMT. */
757 gimple_divmod_fixed_value (gassign
*stmt
, tree value
, int prob
,
758 gcov_type count
, gcov_type all
)
760 gassign
*stmt1
, *stmt2
;
762 tree tmp0
, tmp1
, tmp2
;
763 gimple bb1end
, bb2end
, bb3end
;
764 basic_block bb
, bb2
, bb3
, bb4
;
765 tree optype
, op1
, op2
;
766 edge e12
, e13
, e23
, e24
, e34
;
767 gimple_stmt_iterator gsi
;
769 gcc_assert (is_gimple_assign (stmt
)
770 && (gimple_assign_rhs_code (stmt
) == TRUNC_DIV_EXPR
771 || gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
));
773 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
774 op1
= gimple_assign_rhs1 (stmt
);
775 op2
= gimple_assign_rhs2 (stmt
);
777 bb
= gimple_bb (stmt
);
778 gsi
= gsi_for_stmt (stmt
);
780 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
781 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
782 stmt1
= gimple_build_assign (tmp0
, fold_convert (optype
, value
));
783 stmt2
= gimple_build_assign (tmp1
, op2
);
784 stmt3
= gimple_build_cond (NE_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
785 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
786 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
787 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
790 tmp2
= create_tmp_reg (optype
, "PROF");
791 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, tmp0
);
792 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
795 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, op2
);
796 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
800 /* Edge e23 connects bb2 to bb3, etc. */
801 e12
= split_block (bb
, bb1end
);
804 e23
= split_block (bb2
, bb2end
);
806 bb3
->count
= all
- count
;
807 e34
= split_block (bb3
, bb3end
);
811 e12
->flags
&= ~EDGE_FALLTHRU
;
812 e12
->flags
|= EDGE_FALSE_VALUE
;
813 e12
->probability
= prob
;
816 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
817 e13
->probability
= REG_BR_PROB_BASE
- prob
;
818 e13
->count
= all
- count
;
822 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
823 e24
->probability
= REG_BR_PROB_BASE
;
826 e34
->probability
= REG_BR_PROB_BASE
;
827 e34
->count
= all
- count
;
833 /* Do transform 1) on INSN if applicable. */
836 gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*si
)
838 histogram_value histogram
;
840 gcov_type val
, count
, all
;
841 tree result
, value
, tree_val
;
845 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
849 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
))))
852 code
= gimple_assign_rhs_code (stmt
);
854 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
857 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
858 HIST_TYPE_SINGLE_VALUE
);
862 value
= histogram
->hvalue
.value
;
863 val
= histogram
->hvalue
.counters
[0];
864 count
= histogram
->hvalue
.counters
[1];
865 all
= histogram
->hvalue
.counters
[2];
866 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
868 /* We require that count is at least half of all; this means
869 that for the transformation to fire the value must be constant
870 at least 50% of time (and 75% gives the guarantee of usage). */
871 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
873 || optimize_bb_for_size_p (gimple_bb (stmt
)))
876 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
879 /* Compute probability of taking the optimal path. */
881 prob
= GCOV_COMPUTE_SCALE (count
, all
);
885 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
886 tree_val
= build_int_cst (get_gcov_type (), val
);
890 a
[0] = (unsigned HOST_WIDE_INT
) val
;
891 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
893 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
894 TYPE_PRECISION (get_gcov_type ()), false));
896 result
= gimple_divmod_fixed_value (stmt
, tree_val
, prob
, count
, all
);
900 fprintf (dump_file
, "Div/mod by constant ");
901 print_generic_expr (dump_file
, value
, TDF_SLIM
);
902 fprintf (dump_file
, "=");
903 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
904 fprintf (dump_file
, " transformation on insn ");
905 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
908 gimple_assign_set_rhs_from_tree (si
, result
);
909 update_stmt (gsi_stmt (*si
));
914 /* Generate code for transformation 2 (with parent gimple assign STMT and
915 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
916 within roundoff error). This generates the result into a temp and returns
917 the temp; it does not replace or alter the original STMT. */
919 gimple_mod_pow2 (gassign
*stmt
, int prob
, gcov_type count
, gcov_type all
)
921 gassign
*stmt1
, *stmt2
, *stmt3
;
924 gimple bb1end
, bb2end
, bb3end
;
925 basic_block bb
, bb2
, bb3
, bb4
;
926 tree optype
, op1
, op2
;
927 edge e12
, e13
, e23
, e24
, e34
;
928 gimple_stmt_iterator gsi
;
931 gcc_assert (is_gimple_assign (stmt
)
932 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
934 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
935 op1
= gimple_assign_rhs1 (stmt
);
936 op2
= gimple_assign_rhs2 (stmt
);
938 bb
= gimple_bb (stmt
);
939 gsi
= gsi_for_stmt (stmt
);
941 result
= create_tmp_reg (optype
, "PROF");
942 tmp2
= make_temp_ssa_name (optype
, NULL
, "PROF");
943 tmp3
= make_temp_ssa_name (optype
, NULL
, "PROF");
944 stmt2
= gimple_build_assign (tmp2
, PLUS_EXPR
, op2
,
945 build_int_cst (optype
, -1));
946 stmt3
= gimple_build_assign (tmp3
, BIT_AND_EXPR
, tmp2
, op2
);
947 stmt4
= gimple_build_cond (NE_EXPR
, tmp3
, build_int_cst (optype
, 0),
948 NULL_TREE
, NULL_TREE
);
949 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
950 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
951 gsi_insert_before (&gsi
, stmt4
, GSI_SAME_STMT
);
954 /* tmp2 == op2-1 inherited from previous block. */
955 stmt1
= gimple_build_assign (result
, BIT_AND_EXPR
, op1
, tmp2
);
956 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
959 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
961 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
965 /* Edge e23 connects bb2 to bb3, etc. */
966 e12
= split_block (bb
, bb1end
);
969 e23
= split_block (bb2
, bb2end
);
971 bb3
->count
= all
- count
;
972 e34
= split_block (bb3
, bb3end
);
976 e12
->flags
&= ~EDGE_FALLTHRU
;
977 e12
->flags
|= EDGE_FALSE_VALUE
;
978 e12
->probability
= prob
;
981 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
982 e13
->probability
= REG_BR_PROB_BASE
- prob
;
983 e13
->count
= all
- count
;
987 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
988 e24
->probability
= REG_BR_PROB_BASE
;
991 e34
->probability
= REG_BR_PROB_BASE
;
992 e34
->count
= all
- count
;
997 /* Do transform 2) on INSN if applicable. */
999 gimple_mod_pow2_value_transform (gimple_stmt_iterator
*si
)
1001 histogram_value histogram
;
1002 enum tree_code code
;
1003 gcov_type count
, wrong_values
, all
;
1004 tree lhs_type
, result
, value
;
1008 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
1012 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1013 if (!INTEGRAL_TYPE_P (lhs_type
))
1016 code
= gimple_assign_rhs_code (stmt
);
1018 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1021 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_POW2
);
1025 value
= histogram
->hvalue
.value
;
1026 wrong_values
= histogram
->hvalue
.counters
[0];
1027 count
= histogram
->hvalue
.counters
[1];
1029 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1031 /* We require that we hit a power of 2 at least half of all evaluations. */
1032 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
1033 || count
< wrong_values
1034 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1039 fprintf (dump_file
, "Mod power of 2 transformation on insn ");
1040 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1043 /* Compute probability of taking the optimal path. */
1044 all
= count
+ wrong_values
;
1046 if (check_counter (stmt
, "pow2", &count
, &all
, gimple_bb (stmt
)->count
))
1050 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1054 result
= gimple_mod_pow2 (stmt
, prob
, count
, all
);
1056 gimple_assign_set_rhs_from_tree (si
, result
);
1057 update_stmt (gsi_stmt (*si
));
1062 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1063 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1064 supported and this is built into this interface. The probabilities of taking
1065 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1066 COUNT2/ALL respectively within roundoff error). This generates the
1067 result into a temp and returns the temp; it does not replace or alter
1068 the original STMT. */
1069 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1072 gimple_mod_subtract (gassign
*stmt
, int prob1
, int prob2
, int ncounts
,
1073 gcov_type count1
, gcov_type count2
, gcov_type all
)
1079 gimple bb1end
, bb2end
= NULL
, bb3end
;
1080 basic_block bb
, bb2
, bb3
, bb4
;
1081 tree optype
, op1
, op2
;
1082 edge e12
, e23
= 0, e24
, e34
, e14
;
1083 gimple_stmt_iterator gsi
;
1086 gcc_assert (is_gimple_assign (stmt
)
1087 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
1089 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1090 op1
= gimple_assign_rhs1 (stmt
);
1091 op2
= gimple_assign_rhs2 (stmt
);
1093 bb
= gimple_bb (stmt
);
1094 gsi
= gsi_for_stmt (stmt
);
1096 result
= create_tmp_reg (optype
, "PROF");
1097 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1098 stmt1
= gimple_build_assign (result
, op1
);
1099 stmt2
= gimple_build_assign (tmp1
, op2
);
1100 stmt3
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1101 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1102 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1103 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
1106 if (ncounts
) /* Assumed to be 0 or 1 */
1108 stmt1
= gimple_build_assign (result
, MINUS_EXPR
, result
, tmp1
);
1109 stmt2
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1110 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1111 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1115 /* Fallback case. */
1116 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
1118 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1122 /* Edge e23 connects bb2 to bb3, etc. */
1123 /* However block 3 is optional; if it is not there, references
1124 to 3 really refer to block 2. */
1125 e12
= split_block (bb
, bb1end
);
1127 bb2
->count
= all
- count1
;
1129 if (ncounts
) /* Assumed to be 0 or 1. */
1131 e23
= split_block (bb2
, bb2end
);
1133 bb3
->count
= all
- count1
- count2
;
1136 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
1140 e12
->flags
&= ~EDGE_FALLTHRU
;
1141 e12
->flags
|= EDGE_FALSE_VALUE
;
1142 e12
->probability
= REG_BR_PROB_BASE
- prob1
;
1143 e12
->count
= all
- count1
;
1145 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
1146 e14
->probability
= prob1
;
1147 e14
->count
= count1
;
1149 if (ncounts
) /* Assumed to be 0 or 1. */
1151 e23
->flags
&= ~EDGE_FALLTHRU
;
1152 e23
->flags
|= EDGE_FALSE_VALUE
;
1153 e23
->count
= all
- count1
- count2
;
1154 e23
->probability
= REG_BR_PROB_BASE
- prob2
;
1156 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
1157 e24
->probability
= prob2
;
1158 e24
->count
= count2
;
1161 e34
->probability
= REG_BR_PROB_BASE
;
1162 e34
->count
= all
- count1
- count2
;
1168 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1171 gimple_mod_subtract_transform (gimple_stmt_iterator
*si
)
1173 histogram_value histogram
;
1174 enum tree_code code
;
1175 gcov_type count
, wrong_values
, all
;
1176 tree lhs_type
, result
;
1177 gcov_type prob1
, prob2
;
1178 unsigned int i
, steps
;
1179 gcov_type count1
, count2
;
1182 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
1186 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1187 if (!INTEGRAL_TYPE_P (lhs_type
))
1190 code
= gimple_assign_rhs_code (stmt
);
1192 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1195 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INTERVAL
);
1201 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1202 all
+= histogram
->hvalue
.counters
[i
];
1204 wrong_values
+= histogram
->hvalue
.counters
[i
];
1205 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
1206 steps
= histogram
->hdata
.intvl
.steps
;
1207 all
+= wrong_values
;
1208 count1
= histogram
->hvalue
.counters
[0];
1209 count2
= histogram
->hvalue
.counters
[1];
1211 /* Compute probability of taking the optimal path. */
1212 if (check_counter (stmt
, "interval", &count1
, &all
, gimple_bb (stmt
)->count
))
1214 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1218 if (flag_profile_correction
&& count1
+ count2
> all
)
1219 all
= count1
+ count2
;
1221 gcc_assert (count1
+ count2
<= all
);
1223 /* We require that we use just subtractions in at least 50% of all
1226 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1228 count
+= histogram
->hvalue
.counters
[i
];
1229 if (count
* 2 >= all
)
1233 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1236 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1239 fprintf (dump_file
, "Mod subtract transformation on insn ");
1240 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1243 /* Compute probability of taking the optimal path(s). */
1246 prob1
= GCOV_COMPUTE_SCALE (count1
, all
);
1247 prob2
= GCOV_COMPUTE_SCALE (count2
, all
);
1254 /* In practice, "steps" is always 2. This interface reflects this,
1255 and will need to be changed if "steps" can change. */
1256 result
= gimple_mod_subtract (stmt
, prob1
, prob2
, i
, count1
, count2
, all
);
1258 gimple_assign_set_rhs_from_tree (si
, result
);
1259 update_stmt (gsi_stmt (*si
));
1264 struct profile_id_traits
: default_hashmap_traits
1266 template<typename T
>
1270 return e
.m_key
== UINT_MAX
;
1273 template<typename T
> static bool is_empty (T
&e
) { return e
.m_key
== 0; }
1274 template<typename T
> static void mark_deleted (T
&e
) { e
.m_key
= UINT_MAX
; }
1275 template<typename T
> static void mark_empty (T
&e
) { e
.m_key
= 0; }
1278 static hash_map
<unsigned int, cgraph_node
*, profile_id_traits
> *
1279 cgraph_node_map
= 0;
1281 /* Returns true if node graph is initialized. This
1282 is used to test if profile_id has been created
1283 for cgraph_nodes. */
1286 coverage_node_map_initialized_p (void)
1288 return cgraph_node_map
!= 0;
1291 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1292 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1293 that the PROFILE_IDs was already assigned. */
1296 init_node_map (bool local
)
1298 struct cgraph_node
*n
;
1300 = new hash_map
<unsigned int, cgraph_node
*, profile_id_traits
>;
1302 FOR_EACH_DEFINED_FUNCTION (n
)
1303 if (n
->has_gimple_body_p ())
1308 n
->profile_id
= coverage_compute_profile_id (n
);
1309 while ((val
= cgraph_node_map
->get (n
->profile_id
))
1313 fprintf (dump_file
, "Local profile-id %i conflict"
1314 " with nodes %s/%i %s/%i\n",
1320 n
->profile_id
= (n
->profile_id
+ 1) & 0x7fffffff;
1323 else if (!n
->profile_id
)
1327 "Node %s/%i has no profile-id"
1328 " (profile feedback missing?)\n",
1333 else if ((val
= cgraph_node_map
->get (n
->profile_id
)))
1337 "Node %s/%i has IP profile-id %i conflict. "
1345 cgraph_node_map
->put (n
->profile_id
, n
);
1349 /* Delete the CGRAPH_NODE_MAP. */
1354 delete cgraph_node_map
;
1357 /* Return cgraph node for function with pid */
1360 find_func_by_profile_id (int profile_id
)
1362 cgraph_node
**val
= cgraph_node_map
->get (profile_id
);
1369 /* Perform sanity check on the indirect call target. Due to race conditions,
1370 false function target may be attributed to an indirect call site. If the
1371 call expression type mismatches with the target function's type, expand_call
1372 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1373 Returns true if TARGET is considered ok for call CALL_STMT. */
1376 check_ic_target (gcall
*call_stmt
, struct cgraph_node
*target
)
1379 if (gimple_check_call_matching_types (call_stmt
, target
->decl
, true))
1382 locus
= gimple_location (call_stmt
);
1383 if (dump_enabled_p ())
1384 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
1385 "Skipping target %s with mismatching types for icall\n",
1390 /* Do transformation
1392 if (actual_callee_address == address_of_most_common_function/method)
1399 gimple_ic (gcall
*icall_stmt
, struct cgraph_node
*direct_call
,
1400 int prob
, gcov_type count
, gcov_type all
)
1405 gcall
*iretbnd_stmt
= NULL
;
1406 tree tmp0
, tmp1
, tmp
;
1407 basic_block cond_bb
, dcall_bb
, icall_bb
, join_bb
= NULL
;
1408 tree optype
= build_pointer_type (void_type_node
);
1409 edge e_cd
, e_ci
, e_di
, e_dj
= NULL
, e_ij
;
1410 gimple_stmt_iterator gsi
;
1414 gimple_stmt_iterator psi
;
1416 cond_bb
= gimple_bb (icall_stmt
);
1417 gsi
= gsi_for_stmt (icall_stmt
);
1419 if (gimple_call_with_bounds_p (icall_stmt
) && gimple_call_lhs (icall_stmt
))
1420 iretbnd_stmt
= chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt
));
1422 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1423 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1424 tmp
= unshare_expr (gimple_call_fn (icall_stmt
));
1425 load_stmt
= gimple_build_assign (tmp0
, tmp
);
1426 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1428 tmp
= fold_convert (optype
, build_addr (direct_call
->decl
,
1429 current_function_decl
));
1430 load_stmt
= gimple_build_assign (tmp1
, tmp
);
1431 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1433 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1434 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1436 gimple_set_vdef (icall_stmt
, NULL_TREE
);
1437 gimple_set_vuse (icall_stmt
, NULL_TREE
);
1438 update_stmt (icall_stmt
);
1439 dcall_stmt
= as_a
<gcall
*> (gimple_copy (icall_stmt
));
1440 gimple_call_set_fndecl (dcall_stmt
, direct_call
->decl
);
1441 dflags
= flags_from_decl_or_type (direct_call
->decl
);
1442 if ((dflags
& ECF_NORETURN
) != 0)
1443 gimple_call_set_lhs (dcall_stmt
, NULL_TREE
);
1444 gsi_insert_before (&gsi
, dcall_stmt
, GSI_SAME_STMT
);
1447 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1448 e_cd
= split_block (cond_bb
, cond_stmt
);
1449 dcall_bb
= e_cd
->dest
;
1450 dcall_bb
->count
= count
;
1452 e_di
= split_block (dcall_bb
, dcall_stmt
);
1453 icall_bb
= e_di
->dest
;
1454 icall_bb
->count
= all
- count
;
1456 /* Do not disturb existing EH edges from the indirect call. */
1457 if (!stmt_ends_bb_p (icall_stmt
))
1458 e_ij
= split_block (icall_bb
, icall_stmt
);
1461 e_ij
= find_fallthru_edge (icall_bb
->succs
);
1462 /* The indirect call might be noreturn. */
1465 e_ij
->probability
= REG_BR_PROB_BASE
;
1466 e_ij
->count
= all
- count
;
1467 e_ij
= single_pred_edge (split_edge (e_ij
));
1472 join_bb
= e_ij
->dest
;
1473 join_bb
->count
= all
;
1476 e_cd
->flags
= (e_cd
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1477 e_cd
->probability
= prob
;
1478 e_cd
->count
= count
;
1480 e_ci
= make_edge (cond_bb
, icall_bb
, EDGE_FALSE_VALUE
);
1481 e_ci
->probability
= REG_BR_PROB_BASE
- prob
;
1482 e_ci
->count
= all
- count
;
1488 if ((dflags
& ECF_NORETURN
) != 0)
1492 e_dj
= make_edge (dcall_bb
, join_bb
, EDGE_FALLTHRU
);
1493 e_dj
->probability
= REG_BR_PROB_BASE
;
1494 e_dj
->count
= count
;
1496 e_ij
->count
= all
- count
;
1498 e_ij
->probability
= REG_BR_PROB_BASE
;
1501 /* Insert PHI node for the call result if necessary. */
1502 if (gimple_call_lhs (icall_stmt
)
1503 && TREE_CODE (gimple_call_lhs (icall_stmt
)) == SSA_NAME
1504 && (dflags
& ECF_NORETURN
) == 0)
1506 tree result
= gimple_call_lhs (icall_stmt
);
1507 gphi
*phi
= create_phi_node (result
, join_bb
);
1508 gimple_call_set_lhs (icall_stmt
,
1509 duplicate_ssa_name (result
, icall_stmt
));
1510 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1511 gimple_call_set_lhs (dcall_stmt
,
1512 duplicate_ssa_name (result
, dcall_stmt
));
1513 add_phi_arg (phi
, gimple_call_lhs (dcall_stmt
), e_dj
, UNKNOWN_LOCATION
);
1515 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1516 call then we need to make it's copy for the direct
1520 if (gimple_call_lhs (iretbnd_stmt
))
1524 gimple_set_vdef (iretbnd_stmt
, NULL_TREE
);
1525 gimple_set_vuse (iretbnd_stmt
, NULL_TREE
);
1526 update_stmt (iretbnd_stmt
);
1528 result
= gimple_call_lhs (iretbnd_stmt
);
1529 phi
= create_phi_node (result
, join_bb
);
1531 copy
= gimple_copy (iretbnd_stmt
);
1532 gimple_call_set_arg (copy
, 0,
1533 gimple_call_lhs (dcall_stmt
));
1534 gimple_call_set_lhs (copy
, duplicate_ssa_name (result
, copy
));
1535 gsi_insert_on_edge (e_dj
, copy
);
1536 add_phi_arg (phi
, gimple_call_lhs (copy
),
1537 e_dj
, UNKNOWN_LOCATION
);
1539 gimple_call_set_arg (iretbnd_stmt
, 0,
1540 gimple_call_lhs (icall_stmt
));
1541 gimple_call_set_lhs (iretbnd_stmt
,
1542 duplicate_ssa_name (result
, iretbnd_stmt
));
1543 psi
= gsi_for_stmt (iretbnd_stmt
);
1544 gsi_remove (&psi
, false);
1545 gsi_insert_on_edge (e_ij
, iretbnd_stmt
);
1546 add_phi_arg (phi
, gimple_call_lhs (iretbnd_stmt
),
1547 e_ij
, UNKNOWN_LOCATION
);
1549 gsi_commit_one_edge_insert (e_dj
, NULL
);
1550 gsi_commit_one_edge_insert (e_ij
, NULL
);
1554 psi
= gsi_for_stmt (iretbnd_stmt
);
1555 gsi_remove (&psi
, true);
1560 /* Build an EH edge for the direct call if necessary. */
1561 lp_nr
= lookup_stmt_eh_lp (icall_stmt
);
1562 if (lp_nr
> 0 && stmt_could_throw_p (dcall_stmt
))
1564 add_stmt_to_eh_lp (dcall_stmt
, lp_nr
);
1567 FOR_EACH_EDGE (e_eh
, ei
, icall_bb
->succs
)
1568 if (e_eh
->flags
& (EDGE_EH
| EDGE_ABNORMAL
))
1570 e
= make_edge (dcall_bb
, e_eh
->dest
, e_eh
->flags
);
1571 for (gphi_iterator psi
= gsi_start_phis (e_eh
->dest
);
1572 !gsi_end_p (psi
); gsi_next (&psi
))
1574 gphi
*phi
= psi
.phi ();
1575 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
),
1576 PHI_ARG_DEF_FROM_EDGE (phi
, e_eh
));
1579 if (!stmt_could_throw_p (dcall_stmt
))
1580 gimple_purge_dead_eh_edges (dcall_bb
);
1585 For every checked indirect/virtual call determine if most common pid of
1586 function/class method has probability more than 50%. If yes modify code of
1591 gimple_ic_transform (gimple_stmt_iterator
*gsi
)
1594 histogram_value histogram
;
1595 gcov_type val
, count
, all
, bb_all
;
1596 struct cgraph_node
*direct_call
;
1598 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1602 if (gimple_call_fndecl (stmt
) != NULL_TREE
)
1605 if (gimple_call_internal_p (stmt
))
1608 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INDIR_CALL
);
1612 val
= histogram
->hvalue
.counters
[0];
1613 count
= histogram
->hvalue
.counters
[1];
1614 all
= histogram
->hvalue
.counters
[2];
1616 bb_all
= gimple_bb (stmt
)->count
;
1617 /* The order of CHECK_COUNTER calls is important -
1618 since check_counter can correct the third parameter
1619 and we want to make count <= all <= bb_all. */
1620 if ( check_counter (stmt
, "ic", &all
, &bb_all
, bb_all
)
1621 || check_counter (stmt
, "ic", &count
, &all
, all
))
1623 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1627 if (4 * count
<= 3 * all
)
1630 direct_call
= find_func_by_profile_id ((int)val
);
1632 if (direct_call
== NULL
)
1638 fprintf (dump_file
, "Indirect call -> direct call from other module");
1639 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1640 fprintf (dump_file
, "=> %i (will resolve only with LTO)\n", (int)val
);
1646 if (!check_ic_target (stmt
, direct_call
))
1650 fprintf (dump_file
, "Indirect call -> direct call ");
1651 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1652 fprintf (dump_file
, "=> ");
1653 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1654 fprintf (dump_file
, " transformation skipped because of type mismatch");
1655 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1657 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1663 fprintf (dump_file
, "Indirect call -> direct call ");
1664 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1665 fprintf (dump_file
, "=> ");
1666 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1667 fprintf (dump_file
, " transformation on insn postponned to ipa-profile");
1668 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1669 fprintf (dump_file
, "hist->count %"PRId64
1670 " hist->all %"PRId64
"\n", count
, all
);
1676 /* Return true if the stringop CALL with FNDECL shall be profiled.
1677 SIZE_ARG be set to the argument index for the size of the string
1681 interesting_stringop_to_profile_p (tree fndecl
, gcall
*call
, int *size_arg
)
1683 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
1685 if (fcode
!= BUILT_IN_MEMCPY
&& fcode
!= BUILT_IN_MEMPCPY
1686 && fcode
!= BUILT_IN_MEMSET
&& fcode
!= BUILT_IN_BZERO
)
1691 case BUILT_IN_MEMCPY
:
1692 case BUILT_IN_MEMPCPY
:
1694 return validate_gimple_arglist (call
, POINTER_TYPE
, POINTER_TYPE
,
1695 INTEGER_TYPE
, VOID_TYPE
);
1696 case BUILT_IN_MEMSET
:
1698 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1699 INTEGER_TYPE
, VOID_TYPE
);
1700 case BUILT_IN_BZERO
:
1702 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1709 /* Convert stringop (..., vcall_size)
1711 if (vcall_size == icall_size)
1712 stringop (..., icall_size);
1714 stringop (..., vcall_size);
1715 assuming we'll propagate a true constant into ICALL_SIZE later. */
1718 gimple_stringop_fixed_value (gcall
*vcall_stmt
, tree icall_size
, int prob
,
1719 gcov_type count
, gcov_type all
)
1724 tree tmp0
, tmp1
, vcall_size
, optype
;
1725 basic_block cond_bb
, icall_bb
, vcall_bb
, join_bb
;
1726 edge e_ci
, e_cv
, e_iv
, e_ij
, e_vj
;
1727 gimple_stmt_iterator gsi
;
1731 fndecl
= gimple_call_fndecl (vcall_stmt
);
1732 if (!interesting_stringop_to_profile_p (fndecl
, vcall_stmt
, &size_arg
))
1735 cond_bb
= gimple_bb (vcall_stmt
);
1736 gsi
= gsi_for_stmt (vcall_stmt
);
1738 vcall_size
= gimple_call_arg (vcall_stmt
, size_arg
);
1739 optype
= TREE_TYPE (vcall_size
);
1741 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1742 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1743 tmp_stmt
= gimple_build_assign (tmp0
, fold_convert (optype
, icall_size
));
1744 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1746 tmp_stmt
= gimple_build_assign (tmp1
, vcall_size
);
1747 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1749 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1750 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1752 gimple_set_vdef (vcall_stmt
, NULL
);
1753 gimple_set_vuse (vcall_stmt
, NULL
);
1754 update_stmt (vcall_stmt
);
1755 icall_stmt
= as_a
<gcall
*> (gimple_copy (vcall_stmt
));
1756 gimple_call_set_arg (icall_stmt
, size_arg
, icall_size
);
1757 gsi_insert_before (&gsi
, icall_stmt
, GSI_SAME_STMT
);
1760 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1761 e_ci
= split_block (cond_bb
, cond_stmt
);
1762 icall_bb
= e_ci
->dest
;
1763 icall_bb
->count
= count
;
1765 e_iv
= split_block (icall_bb
, icall_stmt
);
1766 vcall_bb
= e_iv
->dest
;
1767 vcall_bb
->count
= all
- count
;
1769 e_vj
= split_block (vcall_bb
, vcall_stmt
);
1770 join_bb
= e_vj
->dest
;
1771 join_bb
->count
= all
;
1773 e_ci
->flags
= (e_ci
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1774 e_ci
->probability
= prob
;
1775 e_ci
->count
= count
;
1777 e_cv
= make_edge (cond_bb
, vcall_bb
, EDGE_FALSE_VALUE
);
1778 e_cv
->probability
= REG_BR_PROB_BASE
- prob
;
1779 e_cv
->count
= all
- count
;
1783 e_ij
= make_edge (icall_bb
, join_bb
, EDGE_FALLTHRU
);
1784 e_ij
->probability
= REG_BR_PROB_BASE
;
1785 e_ij
->count
= count
;
1787 e_vj
->probability
= REG_BR_PROB_BASE
;
1788 e_vj
->count
= all
- count
;
1790 /* Insert PHI node for the call result if necessary. */
1791 if (gimple_call_lhs (vcall_stmt
)
1792 && TREE_CODE (gimple_call_lhs (vcall_stmt
)) == SSA_NAME
)
1794 tree result
= gimple_call_lhs (vcall_stmt
);
1795 gphi
*phi
= create_phi_node (result
, join_bb
);
1796 gimple_call_set_lhs (vcall_stmt
,
1797 duplicate_ssa_name (result
, vcall_stmt
));
1798 add_phi_arg (phi
, gimple_call_lhs (vcall_stmt
), e_vj
, UNKNOWN_LOCATION
);
1799 gimple_call_set_lhs (icall_stmt
,
1800 duplicate_ssa_name (result
, icall_stmt
));
1801 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1804 /* Because these are all string op builtins, they're all nothrow. */
1805 gcc_assert (!stmt_could_throw_p (vcall_stmt
));
1806 gcc_assert (!stmt_could_throw_p (icall_stmt
));
1809 /* Find values inside STMT for that we want to measure histograms for
1810 division/modulo optimization. */
1812 gimple_stringops_transform (gimple_stmt_iterator
*gsi
)
1817 enum built_in_function fcode
;
1818 histogram_value histogram
;
1819 gcov_type count
, all
, val
;
1821 unsigned int dest_align
, src_align
;
1826 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1829 fndecl
= gimple_call_fndecl (stmt
);
1832 fcode
= DECL_FUNCTION_CODE (fndecl
);
1833 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1836 blck_size
= gimple_call_arg (stmt
, size_arg
);
1837 if (TREE_CODE (blck_size
) == INTEGER_CST
)
1840 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_SINGLE_VALUE
);
1843 val
= histogram
->hvalue
.counters
[0];
1844 count
= histogram
->hvalue
.counters
[1];
1845 all
= histogram
->hvalue
.counters
[2];
1846 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1847 /* We require that count is at least half of all; this means
1848 that for the transformation to fire the value must be constant
1849 at least 80% of time. */
1850 if ((6 * count
/ 5) < all
|| optimize_bb_for_size_p (gimple_bb (stmt
)))
1852 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
1855 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1858 dest
= gimple_call_arg (stmt
, 0);
1859 dest_align
= get_pointer_alignment (dest
);
1862 case BUILT_IN_MEMCPY
:
1863 case BUILT_IN_MEMPCPY
:
1864 src
= gimple_call_arg (stmt
, 1);
1865 src_align
= get_pointer_alignment (src
);
1866 if (!can_move_by_pieces (val
, MIN (dest_align
, src_align
)))
1869 case BUILT_IN_MEMSET
:
1870 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1871 gimple_call_arg (stmt
, 1),
1875 case BUILT_IN_BZERO
:
1876 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1884 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
1885 tree_val
= build_int_cst (get_gcov_type (), val
);
1889 a
[0] = (unsigned HOST_WIDE_INT
) val
;
1890 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
1892 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
1893 TYPE_PRECISION (get_gcov_type ()), false));
1898 fprintf (dump_file
, "Single value %i stringop transformation on ",
1900 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1902 gimple_stringop_fixed_value (stmt
, tree_val
, prob
, count
, all
);
1908 stringop_block_profile (gimple stmt
, unsigned int *expected_align
,
1909 HOST_WIDE_INT
*expected_size
)
1911 histogram_value histogram
;
1912 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_AVERAGE
);
1914 *expected_size
= -1;
1915 else if (!histogram
->hvalue
.counters
[1])
1917 *expected_size
= -1;
1918 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1923 size
= ((histogram
->hvalue
.counters
[0]
1924 + histogram
->hvalue
.counters
[1] / 2)
1925 / histogram
->hvalue
.counters
[1]);
1926 /* Even if we can hold bigger value in SIZE, INT_MAX
1927 is safe "infinity" for code generation strategies. */
1930 *expected_size
= size
;
1931 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1933 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_IOR
);
1935 *expected_align
= 0;
1936 else if (!histogram
->hvalue
.counters
[0])
1938 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1939 *expected_align
= 0;
1946 count
= histogram
->hvalue
.counters
[0];
1948 while (!(count
& alignment
)
1949 && (alignment
* 2 * BITS_PER_UNIT
))
1951 *expected_align
= alignment
* BITS_PER_UNIT
;
1952 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1957 /* Find values inside STMT for that we want to measure histograms for
1958 division/modulo optimization. */
1960 gimple_divmod_values_to_profile (gimple stmt
, histogram_values
*values
)
1962 tree lhs
, divisor
, op0
, type
;
1963 histogram_value hist
;
1965 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1968 lhs
= gimple_assign_lhs (stmt
);
1969 type
= TREE_TYPE (lhs
);
1970 if (!INTEGRAL_TYPE_P (type
))
1973 switch (gimple_assign_rhs_code (stmt
))
1975 case TRUNC_DIV_EXPR
:
1976 case TRUNC_MOD_EXPR
:
1977 divisor
= gimple_assign_rhs2 (stmt
);
1978 op0
= gimple_assign_rhs1 (stmt
);
1980 values
->reserve (3);
1982 if (TREE_CODE (divisor
) == SSA_NAME
)
1983 /* Check for the case where the divisor is the same value most
1985 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1986 HIST_TYPE_SINGLE_VALUE
,
1989 /* For mod, check whether it is not often a noop (or replaceable by
1990 a few subtractions). */
1991 if (gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
1992 && TYPE_UNSIGNED (type
))
1995 /* Check for a special case where the divisor is power of 2. */
1996 values
->quick_push (gimple_alloc_histogram_value (cfun
,
2000 val
= build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
2001 hist
= gimple_alloc_histogram_value (cfun
, HIST_TYPE_INTERVAL
,
2003 hist
->hdata
.intvl
.int_start
= 0;
2004 hist
->hdata
.intvl
.steps
= 2;
2005 values
->quick_push (hist
);
2014 /* Find calls inside STMT for that we want to measure histograms for
2015 indirect/virtual call optimization. */
2018 gimple_indirect_call_to_profile (gimple stmt
, histogram_values
*values
)
2022 if (gimple_code (stmt
) != GIMPLE_CALL
2023 || gimple_call_internal_p (stmt
)
2024 || gimple_call_fndecl (stmt
) != NULL_TREE
)
2027 callee
= gimple_call_fn (stmt
);
2029 values
->reserve (3);
2031 values
->quick_push (gimple_alloc_histogram_value (
2033 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE
) ?
2034 HIST_TYPE_INDIR_CALL_TOPN
:
2035 HIST_TYPE_INDIR_CALL
,
2041 /* Find values inside STMT for that we want to measure histograms for
2042 string operations. */
2044 gimple_stringops_values_to_profile (gimple gs
, histogram_values
*values
)
2052 stmt
= dyn_cast
<gcall
*> (gs
);
2055 fndecl
= gimple_call_fndecl (stmt
);
2059 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
2062 dest
= gimple_call_arg (stmt
, 0);
2063 blck_size
= gimple_call_arg (stmt
, size_arg
);
2065 if (TREE_CODE (blck_size
) != INTEGER_CST
)
2067 values
->safe_push (gimple_alloc_histogram_value (cfun
,
2068 HIST_TYPE_SINGLE_VALUE
,
2070 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_AVERAGE
,
2073 if (TREE_CODE (blck_size
) != INTEGER_CST
)
2074 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_IOR
,
2078 /* Find values inside STMT for that we want to measure histograms and adds
2079 them to list VALUES. */
2082 gimple_values_to_profile (gimple stmt
, histogram_values
*values
)
2084 gimple_divmod_values_to_profile (stmt
, values
);
2085 gimple_stringops_values_to_profile (stmt
, values
);
2086 gimple_indirect_call_to_profile (stmt
, values
);
2090 gimple_find_values_to_profile (histogram_values
*values
)
2093 gimple_stmt_iterator gsi
;
2095 histogram_value hist
= NULL
;
2098 FOR_EACH_BB_FN (bb
, cfun
)
2099 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2100 gimple_values_to_profile (gsi_stmt (gsi
), values
);
2102 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_TIME_PROFILE
, 0, 0));
2104 FOR_EACH_VEC_ELT (*values
, i
, hist
)
2108 case HIST_TYPE_INTERVAL
:
2109 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
2112 case HIST_TYPE_POW2
:
2113 hist
->n_counters
= 2;
2116 case HIST_TYPE_SINGLE_VALUE
:
2117 hist
->n_counters
= 3;
2120 case HIST_TYPE_CONST_DELTA
:
2121 hist
->n_counters
= 4;
2124 case HIST_TYPE_INDIR_CALL
:
2125 hist
->n_counters
= 3;
2128 case HIST_TYPE_TIME_PROFILE
:
2129 hist
->n_counters
= 1;
2132 case HIST_TYPE_AVERAGE
:
2133 hist
->n_counters
= 2;
2137 hist
->n_counters
= 1;
2140 case HIST_TYPE_INDIR_CALL_TOPN
:
2141 hist
->n_counters
= GCOV_ICALL_TOPN_NCOUNTS
;
2149 fprintf (dump_file
, "Stmt ");
2150 print_gimple_stmt (dump_file
, hist
->hvalue
.stmt
, 0, TDF_SLIM
);
2151 dump_histogram_value (dump_file
, hist
);