1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2014 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"
25 #include "tree-nested.h"
29 #include "hard-reg-set.h"
37 #include "dominance.h"
39 #include "basic-block.h"
40 #include "value-prof.h"
42 #include "insn-config.h"
44 #include "insn-codes.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
50 #include "gimple-expr.h"
54 #include "gimple-iterator.h"
55 #include "gimple-ssa.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
59 #include "stringpool.h"
60 #include "tree-ssanames.h"
61 #include "diagnostic.h"
62 #include "gimple-pretty-print.h"
70 #include "plugin-api.h"
73 #include "data-streamer.h"
75 #include "tree-nested.h"
77 #include "tree-chkp.h"
79 /* In this file value profile based optimizations are placed. Currently the
80 following optimizations are implemented (for more detailed descriptions
81 see comments at value_profile_transformations):
83 1) Division/modulo specialization. Provided that we can determine that the
84 operands of the division have some special properties, we may use it to
85 produce more effective code.
87 2) Indirect/virtual call specialization. If we can determine most
88 common function callee in indirect/virtual call. We can use this
89 information to improve code effectiveness (especially info for
92 3) Speculative prefetching. If we are able to determine that the difference
93 between addresses accessed by a memory reference is usually constant, we
94 may add the prefetch instructions.
95 FIXME: This transformation was removed together with RTL based value
99 Value profiling internals
100 ==========================
102 Every value profiling transformation starts with defining what values
103 to profile. There are different histogram types (see HIST_TYPE_* in
104 value-prof.h) and each transformation can request one or more histogram
105 types per GIMPLE statement. The function gimple_find_values_to_profile()
106 collects the values to profile in a vec, and adds the number of counters
107 required for the different histogram types.
109 For a -fprofile-generate run, the statements for which values should be
110 recorded, are instrumented in instrument_values(). The instrumentation
111 is done by helper functions that can be found in tree-profile.c, where
112 new types of histograms can be added if necessary.
114 After a -fprofile-use, the value profiling data is read back in by
115 compute_value_histograms() that translates the collected data to
116 histograms and attaches them to the profiled statements via
117 gimple_add_histogram_value(). Histograms are stored in a hash table
118 that is attached to every intrumented function, see VALUE_HISTOGRAMS
121 The value-profile transformations driver is the function
122 gimple_value_profile_transformations(). It traverses all statements in
123 the to-be-transformed function, and looks for statements with one or
124 more histograms attached to it. If a statement has histograms, the
125 transformation functions are called on the statement.
127 Limitations / FIXME / TODO:
128 * Only one histogram of each type can be associated with a statement.
129 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
130 (This type of histogram was originally used to implement a form of
131 stride profiling based speculative prefetching to improve SPEC2000
132 scores for memory-bound benchmarks, mcf and equake. However, this
133 was an RTL value-profiling transformation, and those have all been
135 * Some value profile transformations are done in builtins.c (?!)
136 * Updating of histograms needs some TLC.
137 * The value profiling code could be used to record analysis results
138 from non-profiling (e.g. VRP).
139 * Adding new profilers should be simplified, starting with a cleanup
140 of what-happens-where andwith making gimple_find_values_to_profile
141 and gimple_value_profile_transformations table-driven, perhaps...
144 static tree
gimple_divmod_fixed_value (gassign
*, tree
, int, gcov_type
,
146 static tree
gimple_mod_pow2 (gassign
*, int, gcov_type
, gcov_type
);
147 static tree
gimple_mod_subtract (gassign
*, int, int, int, gcov_type
,
148 gcov_type
, gcov_type
);
149 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*);
150 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator
*);
151 static bool gimple_mod_subtract_transform (gimple_stmt_iterator
*);
152 static bool gimple_stringops_transform (gimple_stmt_iterator
*);
153 static bool gimple_ic_transform (gimple_stmt_iterator
*);
155 /* Allocate histogram value. */
158 gimple_alloc_histogram_value (struct function
*fun ATTRIBUTE_UNUSED
,
159 enum hist_type type
, gimple stmt
, tree value
)
161 histogram_value hist
= (histogram_value
) xcalloc (1, sizeof (*hist
));
162 hist
->hvalue
.value
= value
;
163 hist
->hvalue
.stmt
= stmt
;
168 /* Hash value for histogram. */
171 histogram_hash (const void *x
)
173 return htab_hash_pointer (((const_histogram_value
)x
)->hvalue
.stmt
);
176 /* Return nonzero if statement for histogram_value X is Y. */
179 histogram_eq (const void *x
, const void *y
)
181 return ((const_histogram_value
) x
)->hvalue
.stmt
== (const_gimple
) y
;
184 /* Set histogram for STMT. */
187 set_histogram_value (struct function
*fun
, gimple stmt
, histogram_value hist
)
190 if (!hist
&& !VALUE_HISTOGRAMS (fun
))
192 if (!VALUE_HISTOGRAMS (fun
))
193 VALUE_HISTOGRAMS (fun
) = htab_create (1, histogram_hash
,
195 loc
= htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
196 htab_hash_pointer (stmt
),
197 hist
? INSERT
: NO_INSERT
);
201 htab_clear_slot (VALUE_HISTOGRAMS (fun
), loc
);
207 /* Get histogram list for STMT. */
210 gimple_histogram_value (struct function
*fun
, gimple stmt
)
212 if (!VALUE_HISTOGRAMS (fun
))
214 return (histogram_value
) htab_find_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
215 htab_hash_pointer (stmt
));
218 /* Add histogram for STMT. */
221 gimple_add_histogram_value (struct function
*fun
, gimple stmt
,
222 histogram_value hist
)
224 hist
->hvalue
.next
= gimple_histogram_value (fun
, stmt
);
225 set_histogram_value (fun
, stmt
, hist
);
230 /* Remove histogram HIST from STMT's histogram list. */
233 gimple_remove_histogram_value (struct function
*fun
, gimple stmt
,
234 histogram_value hist
)
236 histogram_value hist2
= gimple_histogram_value (fun
, stmt
);
239 set_histogram_value (fun
, stmt
, hist
->hvalue
.next
);
243 while (hist2
->hvalue
.next
!= hist
)
244 hist2
= hist2
->hvalue
.next
;
245 hist2
->hvalue
.next
= hist
->hvalue
.next
;
247 free (hist
->hvalue
.counters
);
248 #ifdef ENABLE_CHECKING
249 memset (hist
, 0xab, sizeof (*hist
));
255 /* Lookup histogram of type TYPE in the STMT. */
258 gimple_histogram_value_of_type (struct function
*fun
, gimple stmt
,
261 histogram_value hist
;
262 for (hist
= gimple_histogram_value (fun
, stmt
); hist
;
263 hist
= hist
->hvalue
.next
)
264 if (hist
->type
== type
)
269 /* Dump information about HIST to DUMP_FILE. */
272 dump_histogram_value (FILE *dump_file
, histogram_value hist
)
276 case HIST_TYPE_INTERVAL
:
277 fprintf (dump_file
, "Interval counter range %d -- %d",
278 hist
->hdata
.intvl
.int_start
,
279 (hist
->hdata
.intvl
.int_start
280 + hist
->hdata
.intvl
.steps
- 1));
281 if (hist
->hvalue
.counters
)
284 fprintf (dump_file
, " [");
285 for (i
= 0; i
< hist
->hdata
.intvl
.steps
; i
++)
286 fprintf (dump_file
, " %d:%"PRId64
,
287 hist
->hdata
.intvl
.int_start
+ i
,
288 (int64_t) hist
->hvalue
.counters
[i
]);
289 fprintf (dump_file
, " ] outside range:%"PRId64
,
290 (int64_t) hist
->hvalue
.counters
[i
]);
292 fprintf (dump_file
, ".\n");
296 fprintf (dump_file
, "Pow2 counter ");
297 if (hist
->hvalue
.counters
)
299 fprintf (dump_file
, "pow2:%"PRId64
301 (int64_t) hist
->hvalue
.counters
[0],
302 (int64_t) hist
->hvalue
.counters
[1]);
304 fprintf (dump_file
, ".\n");
307 case HIST_TYPE_SINGLE_VALUE
:
308 fprintf (dump_file
, "Single value ");
309 if (hist
->hvalue
.counters
)
311 fprintf (dump_file
, "value:%"PRId64
314 (int64_t) hist
->hvalue
.counters
[0],
315 (int64_t) hist
->hvalue
.counters
[1],
316 (int64_t) hist
->hvalue
.counters
[2]);
318 fprintf (dump_file
, ".\n");
321 case HIST_TYPE_AVERAGE
:
322 fprintf (dump_file
, "Average value ");
323 if (hist
->hvalue
.counters
)
325 fprintf (dump_file
, "sum:%"PRId64
327 (int64_t) hist
->hvalue
.counters
[0],
328 (int64_t) hist
->hvalue
.counters
[1]);
330 fprintf (dump_file
, ".\n");
334 fprintf (dump_file
, "IOR value ");
335 if (hist
->hvalue
.counters
)
337 fprintf (dump_file
, "ior:%"PRId64
,
338 (int64_t) hist
->hvalue
.counters
[0]);
340 fprintf (dump_file
, ".\n");
343 case HIST_TYPE_CONST_DELTA
:
344 fprintf (dump_file
, "Constant delta ");
345 if (hist
->hvalue
.counters
)
347 fprintf (dump_file
, "value:%"PRId64
350 (int64_t) hist
->hvalue
.counters
[0],
351 (int64_t) hist
->hvalue
.counters
[1],
352 (int64_t) hist
->hvalue
.counters
[2]);
354 fprintf (dump_file
, ".\n");
356 case HIST_TYPE_INDIR_CALL
:
357 fprintf (dump_file
, "Indirect call ");
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_TIME_PROFILE
:
370 fprintf (dump_file
, "Time profile ");
371 if (hist
->hvalue
.counters
)
373 fprintf (dump_file
, "time:%"PRId64
,
374 (int64_t) hist
->hvalue
.counters
[0]);
376 fprintf (dump_file
, ".\n");
378 case HIST_TYPE_INDIR_CALL_TOPN
:
379 fprintf (dump_file
, "Indirect call topn ");
380 if (hist
->hvalue
.counters
)
384 fprintf (dump_file
, "accu:%"PRId64
, hist
->hvalue
.counters
[0]);
385 for (i
= 1; i
< (GCOV_ICALL_TOPN_VAL
<< 2); i
+= 2)
387 fprintf (dump_file
, " target:%"PRId64
" value:%"PRId64
,
388 (int64_t) hist
->hvalue
.counters
[i
],
389 (int64_t) hist
->hvalue
.counters
[i
+1]);
392 fprintf (dump_file
, ".\n");
399 /* Dump information about HIST to DUMP_FILE. */
402 stream_out_histogram_value (struct output_block
*ob
, histogram_value hist
)
407 bp
= bitpack_create (ob
->main_stream
);
408 bp_pack_enum (&bp
, hist_type
, HIST_TYPE_MAX
, hist
->type
);
409 bp_pack_value (&bp
, hist
->hvalue
.next
!= NULL
, 1);
410 streamer_write_bitpack (&bp
);
413 case HIST_TYPE_INTERVAL
:
414 streamer_write_hwi (ob
, hist
->hdata
.intvl
.int_start
);
415 streamer_write_uhwi (ob
, hist
->hdata
.intvl
.steps
);
420 for (i
= 0; i
< hist
->n_counters
; i
++)
421 streamer_write_gcov_count (ob
, hist
->hvalue
.counters
[i
]);
422 if (hist
->hvalue
.next
)
423 stream_out_histogram_value (ob
, hist
->hvalue
.next
);
425 /* Dump information about HIST to DUMP_FILE. */
428 stream_in_histogram_value (struct lto_input_block
*ib
, gimple stmt
)
431 unsigned int ncounters
= 0;
434 histogram_value new_val
;
436 histogram_value
*next_p
= NULL
;
440 bp
= streamer_read_bitpack (ib
);
441 type
= bp_unpack_enum (&bp
, hist_type
, HIST_TYPE_MAX
);
442 next
= bp_unpack_value (&bp
, 1);
443 new_val
= gimple_alloc_histogram_value (cfun
, type
, stmt
, NULL
);
446 case HIST_TYPE_INTERVAL
:
447 new_val
->hdata
.intvl
.int_start
= streamer_read_hwi (ib
);
448 new_val
->hdata
.intvl
.steps
= streamer_read_uhwi (ib
);
449 ncounters
= new_val
->hdata
.intvl
.steps
+ 2;
453 case HIST_TYPE_AVERAGE
:
457 case HIST_TYPE_SINGLE_VALUE
:
458 case HIST_TYPE_INDIR_CALL
:
462 case HIST_TYPE_CONST_DELTA
:
467 case HIST_TYPE_TIME_PROFILE
:
471 case HIST_TYPE_INDIR_CALL_TOPN
:
472 ncounters
= (GCOV_ICALL_TOPN_VAL
<< 2) + 1;
478 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * ncounters
);
479 new_val
->n_counters
= ncounters
;
480 for (i
= 0; i
< ncounters
; i
++)
481 new_val
->hvalue
.counters
[i
] = streamer_read_gcov_count (ib
);
483 gimple_add_histogram_value (cfun
, stmt
, new_val
);
486 next_p
= &new_val
->hvalue
.next
;
491 /* Dump all histograms attached to STMT to DUMP_FILE. */
494 dump_histograms_for_stmt (struct function
*fun
, FILE *dump_file
, gimple stmt
)
496 histogram_value hist
;
497 for (hist
= gimple_histogram_value (fun
, stmt
); hist
; hist
= hist
->hvalue
.next
)
498 dump_histogram_value (dump_file
, hist
);
501 /* Remove all histograms associated with STMT. */
504 gimple_remove_stmt_histograms (struct function
*fun
, gimple stmt
)
507 while ((val
= gimple_histogram_value (fun
, stmt
)) != NULL
)
508 gimple_remove_histogram_value (fun
, stmt
, val
);
511 /* Duplicate all histograms associates with OSTMT to STMT. */
514 gimple_duplicate_stmt_histograms (struct function
*fun
, gimple stmt
,
515 struct function
*ofun
, gimple ostmt
)
518 for (val
= gimple_histogram_value (ofun
, ostmt
); val
!= NULL
; val
= val
->hvalue
.next
)
520 histogram_value new_val
= gimple_alloc_histogram_value (fun
, val
->type
, NULL
, NULL
);
521 memcpy (new_val
, val
, sizeof (*val
));
522 new_val
->hvalue
.stmt
= stmt
;
523 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
524 memcpy (new_val
->hvalue
.counters
, val
->hvalue
.counters
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
525 gimple_add_histogram_value (fun
, stmt
, new_val
);
530 /* Move all histograms associated with OSTMT to STMT. */
533 gimple_move_stmt_histograms (struct function
*fun
, gimple stmt
, gimple ostmt
)
535 histogram_value val
= gimple_histogram_value (fun
, ostmt
);
538 /* The following three statements can't be reordered,
539 because histogram hashtab relies on stmt field value
540 for finding the exact slot. */
541 set_histogram_value (fun
, ostmt
, NULL
);
542 for (; val
!= NULL
; val
= val
->hvalue
.next
)
543 val
->hvalue
.stmt
= stmt
;
544 set_histogram_value (fun
, stmt
, val
);
548 static bool error_found
= false;
550 /* Helper function for verify_histograms. For each histogram reachable via htab
551 walk verify that it was reached via statement walk. */
554 visit_hist (void **slot
, void *data
)
556 hash_set
<histogram_value
> *visited
= (hash_set
<histogram_value
> *) data
;
557 histogram_value hist
= *(histogram_value
*) slot
;
559 if (!visited
->contains (hist
)
560 && hist
->type
!= HIST_TYPE_TIME_PROFILE
)
562 error ("dead histogram");
563 dump_histogram_value (stderr
, hist
);
564 debug_gimple_stmt (hist
->hvalue
.stmt
);
571 /* Verify sanity of the histograms. */
574 verify_histograms (void)
577 gimple_stmt_iterator gsi
;
578 histogram_value hist
;
581 hash_set
<histogram_value
> visited_hists
;
582 FOR_EACH_BB_FN (bb
, cfun
)
583 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
585 gimple stmt
= gsi_stmt (gsi
);
587 for (hist
= gimple_histogram_value (cfun
, stmt
); hist
;
588 hist
= hist
->hvalue
.next
)
590 if (hist
->hvalue
.stmt
!= stmt
)
592 error ("Histogram value statement does not correspond to "
593 "the statement it is associated with");
594 debug_gimple_stmt (stmt
);
595 dump_histogram_value (stderr
, hist
);
598 visited_hists
.add (hist
);
601 if (VALUE_HISTOGRAMS (cfun
))
602 htab_traverse (VALUE_HISTOGRAMS (cfun
), visit_hist
, &visited_hists
);
604 internal_error ("verify_histograms failed");
607 /* Helper function for verify_histograms. For each histogram reachable via htab
608 walk verify that it was reached via statement walk. */
611 free_hist (void **slot
, void *data ATTRIBUTE_UNUSED
)
613 histogram_value hist
= *(histogram_value
*) slot
;
614 free (hist
->hvalue
.counters
);
615 #ifdef ENABLE_CHECKING
616 memset (hist
, 0xab, sizeof (*hist
));
623 free_histograms (void)
625 if (VALUE_HISTOGRAMS (cfun
))
627 htab_traverse (VALUE_HISTOGRAMS (cfun
), free_hist
, NULL
);
628 htab_delete (VALUE_HISTOGRAMS (cfun
));
629 VALUE_HISTOGRAMS (cfun
) = NULL
;
634 /* The overall number of invocations of the counter should match
635 execution count of basic block. Report it as error rather than
636 internal error as it might mean that user has misused the profile
640 check_counter (gimple stmt
, const char * name
,
641 gcov_type
*count
, gcov_type
*all
, gcov_type bb_count
)
643 if (*all
!= bb_count
|| *count
> *all
)
646 locus
= (stmt
!= NULL
)
647 ? gimple_location (stmt
)
648 : DECL_SOURCE_LOCATION (current_function_decl
);
649 if (flag_profile_correction
)
651 if (dump_enabled_p ())
652 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
653 "correcting inconsistent value profile: %s "
654 "profiler overall count (%d) does not match BB "
655 "count (%d)\n", name
, (int)*all
, (int)bb_count
);
663 error_at (locus
, "corrupted value profile: %s "
664 "profile counter (%d out of %d) inconsistent with "
665 "basic-block count (%d)",
678 /* GIMPLE based transformations. */
681 gimple_value_profile_transformations (void)
684 gimple_stmt_iterator gsi
;
685 bool changed
= false;
687 FOR_EACH_BB_FN (bb
, cfun
)
689 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
691 gimple stmt
= gsi_stmt (gsi
);
692 histogram_value th
= gimple_histogram_value (cfun
, stmt
);
698 fprintf (dump_file
, "Trying transformations on stmt ");
699 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
700 dump_histograms_for_stmt (cfun
, dump_file
, stmt
);
703 /* Transformations: */
704 /* The order of things in this conditional controls which
705 transformation is used when more than one is applicable. */
706 /* It is expected that any code added by the transformations
707 will be added before the current statement, and that the
708 current statement remain valid (although possibly
709 modified) upon return. */
710 if (gimple_mod_subtract_transform (&gsi
)
711 || gimple_divmod_fixed_value_transform (&gsi
)
712 || gimple_mod_pow2_value_transform (&gsi
)
713 || gimple_stringops_transform (&gsi
)
714 || gimple_ic_transform (&gsi
))
716 stmt
= gsi_stmt (gsi
);
718 /* Original statement may no longer be in the same block. */
719 if (bb
!= gimple_bb (stmt
))
721 bb
= gimple_bb (stmt
);
722 gsi
= gsi_for_stmt (stmt
);
737 /* Generate code for transformation 1 (with parent gimple assignment
738 STMT and probability of taking the optimal path PROB, which is
739 equivalent to COUNT/ALL within roundoff error). This generates the
740 result into a temp and returns the temp; it does not replace or
741 alter the original STMT. */
744 gimple_divmod_fixed_value (gassign
*stmt
, tree value
, int prob
,
745 gcov_type count
, gcov_type all
)
747 gassign
*stmt1
, *stmt2
;
749 tree tmp0
, tmp1
, tmp2
;
750 gimple bb1end
, bb2end
, bb3end
;
751 basic_block bb
, bb2
, bb3
, bb4
;
752 tree optype
, op1
, op2
;
753 edge e12
, e13
, e23
, e24
, e34
;
754 gimple_stmt_iterator gsi
;
756 gcc_assert (is_gimple_assign (stmt
)
757 && (gimple_assign_rhs_code (stmt
) == TRUNC_DIV_EXPR
758 || gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
));
760 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
761 op1
= gimple_assign_rhs1 (stmt
);
762 op2
= gimple_assign_rhs2 (stmt
);
764 bb
= gimple_bb (stmt
);
765 gsi
= gsi_for_stmt (stmt
);
767 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
768 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
769 stmt1
= gimple_build_assign (tmp0
, fold_convert (optype
, value
));
770 stmt2
= gimple_build_assign (tmp1
, op2
);
771 stmt3
= gimple_build_cond (NE_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
772 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
773 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
774 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
777 tmp2
= create_tmp_reg (optype
, "PROF");
778 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, tmp0
);
779 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
782 stmt1
= gimple_build_assign (tmp2
, gimple_assign_rhs_code (stmt
), op1
, op2
);
783 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
787 /* Edge e23 connects bb2 to bb3, etc. */
788 e12
= split_block (bb
, bb1end
);
791 e23
= split_block (bb2
, bb2end
);
793 bb3
->count
= all
- count
;
794 e34
= split_block (bb3
, bb3end
);
798 e12
->flags
&= ~EDGE_FALLTHRU
;
799 e12
->flags
|= EDGE_FALSE_VALUE
;
800 e12
->probability
= prob
;
803 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
804 e13
->probability
= REG_BR_PROB_BASE
- prob
;
805 e13
->count
= all
- count
;
809 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
810 e24
->probability
= REG_BR_PROB_BASE
;
813 e34
->probability
= REG_BR_PROB_BASE
;
814 e34
->count
= all
- count
;
820 /* Do transform 1) on INSN if applicable. */
823 gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*si
)
825 histogram_value histogram
;
827 gcov_type val
, count
, all
;
828 tree result
, value
, tree_val
;
832 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
836 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
))))
839 code
= gimple_assign_rhs_code (stmt
);
841 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
844 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
845 HIST_TYPE_SINGLE_VALUE
);
849 value
= histogram
->hvalue
.value
;
850 val
= histogram
->hvalue
.counters
[0];
851 count
= histogram
->hvalue
.counters
[1];
852 all
= histogram
->hvalue
.counters
[2];
853 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
855 /* We require that count is at least half of all; this means
856 that for the transformation to fire the value must be constant
857 at least 50% of time (and 75% gives the guarantee of usage). */
858 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
860 || optimize_bb_for_size_p (gimple_bb (stmt
)))
863 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
866 /* Compute probability of taking the optimal path. */
868 prob
= GCOV_COMPUTE_SCALE (count
, all
);
872 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
873 tree_val
= build_int_cst (get_gcov_type (), val
);
877 a
[0] = (unsigned HOST_WIDE_INT
) val
;
878 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
880 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
881 TYPE_PRECISION (get_gcov_type ()), false));
883 result
= gimple_divmod_fixed_value (stmt
, tree_val
, prob
, count
, all
);
887 fprintf (dump_file
, "Div/mod by constant ");
888 print_generic_expr (dump_file
, value
, TDF_SLIM
);
889 fprintf (dump_file
, "=");
890 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
891 fprintf (dump_file
, " transformation on insn ");
892 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
895 gimple_assign_set_rhs_from_tree (si
, result
);
896 update_stmt (gsi_stmt (*si
));
901 /* Generate code for transformation 2 (with parent gimple assign STMT and
902 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
903 within roundoff error). This generates the result into a temp and returns
904 the temp; it does not replace or alter the original STMT. */
906 gimple_mod_pow2 (gassign
*stmt
, int prob
, gcov_type count
, gcov_type all
)
908 gassign
*stmt1
, *stmt2
, *stmt3
;
911 gimple bb1end
, bb2end
, bb3end
;
912 basic_block bb
, bb2
, bb3
, bb4
;
913 tree optype
, op1
, op2
;
914 edge e12
, e13
, e23
, e24
, e34
;
915 gimple_stmt_iterator gsi
;
918 gcc_assert (is_gimple_assign (stmt
)
919 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
921 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
922 op1
= gimple_assign_rhs1 (stmt
);
923 op2
= gimple_assign_rhs2 (stmt
);
925 bb
= gimple_bb (stmt
);
926 gsi
= gsi_for_stmt (stmt
);
928 result
= create_tmp_reg (optype
, "PROF");
929 tmp2
= make_temp_ssa_name (optype
, NULL
, "PROF");
930 tmp3
= make_temp_ssa_name (optype
, NULL
, "PROF");
931 stmt2
= gimple_build_assign (tmp2
, PLUS_EXPR
, op2
,
932 build_int_cst (optype
, -1));
933 stmt3
= gimple_build_assign (tmp3
, BIT_AND_EXPR
, tmp2
, op2
);
934 stmt4
= gimple_build_cond (NE_EXPR
, tmp3
, build_int_cst (optype
, 0),
935 NULL_TREE
, NULL_TREE
);
936 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
937 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
938 gsi_insert_before (&gsi
, stmt4
, GSI_SAME_STMT
);
941 /* tmp2 == op2-1 inherited from previous block. */
942 stmt1
= gimple_build_assign (result
, BIT_AND_EXPR
, op1
, tmp2
);
943 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
946 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
948 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
952 /* Edge e23 connects bb2 to bb3, etc. */
953 e12
= split_block (bb
, bb1end
);
956 e23
= split_block (bb2
, bb2end
);
958 bb3
->count
= all
- count
;
959 e34
= split_block (bb3
, bb3end
);
963 e12
->flags
&= ~EDGE_FALLTHRU
;
964 e12
->flags
|= EDGE_FALSE_VALUE
;
965 e12
->probability
= prob
;
968 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
969 e13
->probability
= REG_BR_PROB_BASE
- prob
;
970 e13
->count
= all
- count
;
974 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
975 e24
->probability
= REG_BR_PROB_BASE
;
978 e34
->probability
= REG_BR_PROB_BASE
;
979 e34
->count
= all
- count
;
984 /* Do transform 2) on INSN if applicable. */
986 gimple_mod_pow2_value_transform (gimple_stmt_iterator
*si
)
988 histogram_value histogram
;
990 gcov_type count
, wrong_values
, all
;
991 tree lhs_type
, result
, value
;
995 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
999 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1000 if (!INTEGRAL_TYPE_P (lhs_type
))
1003 code
= gimple_assign_rhs_code (stmt
);
1005 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1008 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_POW2
);
1012 value
= histogram
->hvalue
.value
;
1013 wrong_values
= histogram
->hvalue
.counters
[0];
1014 count
= histogram
->hvalue
.counters
[1];
1016 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1018 /* We require that we hit a power of 2 at least half of all evaluations. */
1019 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
1020 || count
< wrong_values
1021 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1026 fprintf (dump_file
, "Mod power of 2 transformation on insn ");
1027 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1030 /* Compute probability of taking the optimal path. */
1031 all
= count
+ wrong_values
;
1033 if (check_counter (stmt
, "pow2", &count
, &all
, gimple_bb (stmt
)->count
))
1037 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1041 result
= gimple_mod_pow2 (stmt
, prob
, count
, all
);
1043 gimple_assign_set_rhs_from_tree (si
, result
);
1044 update_stmt (gsi_stmt (*si
));
1049 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1050 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1051 supported and this is built into this interface. The probabilities of taking
1052 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1053 COUNT2/ALL respectively within roundoff error). This generates the
1054 result into a temp and returns the temp; it does not replace or alter
1055 the original STMT. */
1056 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1059 gimple_mod_subtract (gassign
*stmt
, int prob1
, int prob2
, int ncounts
,
1060 gcov_type count1
, gcov_type count2
, gcov_type all
)
1066 gimple bb1end
, bb2end
= NULL
, bb3end
;
1067 basic_block bb
, bb2
, bb3
, bb4
;
1068 tree optype
, op1
, op2
;
1069 edge e12
, e23
= 0, e24
, e34
, e14
;
1070 gimple_stmt_iterator gsi
;
1073 gcc_assert (is_gimple_assign (stmt
)
1074 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
1076 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1077 op1
= gimple_assign_rhs1 (stmt
);
1078 op2
= gimple_assign_rhs2 (stmt
);
1080 bb
= gimple_bb (stmt
);
1081 gsi
= gsi_for_stmt (stmt
);
1083 result
= create_tmp_reg (optype
, "PROF");
1084 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1085 stmt1
= gimple_build_assign (result
, op1
);
1086 stmt2
= gimple_build_assign (tmp1
, op2
);
1087 stmt3
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1088 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1089 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1090 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
1093 if (ncounts
) /* Assumed to be 0 or 1 */
1095 stmt1
= gimple_build_assign (result
, MINUS_EXPR
, result
, tmp1
);
1096 stmt2
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1097 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1098 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1102 /* Fallback case. */
1103 stmt1
= gimple_build_assign (result
, gimple_assign_rhs_code (stmt
),
1105 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1109 /* Edge e23 connects bb2 to bb3, etc. */
1110 /* However block 3 is optional; if it is not there, references
1111 to 3 really refer to block 2. */
1112 e12
= split_block (bb
, bb1end
);
1114 bb2
->count
= all
- count1
;
1116 if (ncounts
) /* Assumed to be 0 or 1. */
1118 e23
= split_block (bb2
, bb2end
);
1120 bb3
->count
= all
- count1
- count2
;
1123 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
1127 e12
->flags
&= ~EDGE_FALLTHRU
;
1128 e12
->flags
|= EDGE_FALSE_VALUE
;
1129 e12
->probability
= REG_BR_PROB_BASE
- prob1
;
1130 e12
->count
= all
- count1
;
1132 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
1133 e14
->probability
= prob1
;
1134 e14
->count
= count1
;
1136 if (ncounts
) /* Assumed to be 0 or 1. */
1138 e23
->flags
&= ~EDGE_FALLTHRU
;
1139 e23
->flags
|= EDGE_FALSE_VALUE
;
1140 e23
->count
= all
- count1
- count2
;
1141 e23
->probability
= REG_BR_PROB_BASE
- prob2
;
1143 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
1144 e24
->probability
= prob2
;
1145 e24
->count
= count2
;
1148 e34
->probability
= REG_BR_PROB_BASE
;
1149 e34
->count
= all
- count1
- count2
;
1155 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1158 gimple_mod_subtract_transform (gimple_stmt_iterator
*si
)
1160 histogram_value histogram
;
1161 enum tree_code code
;
1162 gcov_type count
, wrong_values
, all
;
1163 tree lhs_type
, result
;
1164 gcov_type prob1
, prob2
;
1165 unsigned int i
, steps
;
1166 gcov_type count1
, count2
;
1169 stmt
= dyn_cast
<gassign
*> (gsi_stmt (*si
));
1173 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1174 if (!INTEGRAL_TYPE_P (lhs_type
))
1177 code
= gimple_assign_rhs_code (stmt
);
1179 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1182 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INTERVAL
);
1188 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1189 all
+= histogram
->hvalue
.counters
[i
];
1191 wrong_values
+= histogram
->hvalue
.counters
[i
];
1192 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
1193 steps
= histogram
->hdata
.intvl
.steps
;
1194 all
+= wrong_values
;
1195 count1
= histogram
->hvalue
.counters
[0];
1196 count2
= histogram
->hvalue
.counters
[1];
1198 /* Compute probability of taking the optimal path. */
1199 if (check_counter (stmt
, "interval", &count1
, &all
, gimple_bb (stmt
)->count
))
1201 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1205 if (flag_profile_correction
&& count1
+ count2
> all
)
1206 all
= count1
+ count2
;
1208 gcc_assert (count1
+ count2
<= all
);
1210 /* We require that we use just subtractions in at least 50% of all
1213 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1215 count
+= histogram
->hvalue
.counters
[i
];
1216 if (count
* 2 >= all
)
1220 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1223 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1226 fprintf (dump_file
, "Mod subtract transformation on insn ");
1227 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1230 /* Compute probability of taking the optimal path(s). */
1233 prob1
= GCOV_COMPUTE_SCALE (count1
, all
);
1234 prob2
= GCOV_COMPUTE_SCALE (count2
, all
);
1241 /* In practice, "steps" is always 2. This interface reflects this,
1242 and will need to be changed if "steps" can change. */
1243 result
= gimple_mod_subtract (stmt
, prob1
, prob2
, i
, count1
, count2
, all
);
1245 gimple_assign_set_rhs_from_tree (si
, result
);
1246 update_stmt (gsi_stmt (*si
));
1251 struct profile_id_traits
: default_hashmap_traits
1253 template<typename T
>
1257 return e
.m_key
== UINT_MAX
;
1260 template<typename T
> static bool is_empty (T
&e
) { return e
.m_key
== 0; }
1261 template<typename T
> static void mark_deleted (T
&e
) { e
.m_key
= UINT_MAX
; }
1262 template<typename T
> static void mark_empty (T
&e
) { e
.m_key
= 0; }
1265 static hash_map
<unsigned int, cgraph_node
*, profile_id_traits
> *
1266 cgraph_node_map
= 0;
1268 /* Returns true if node graph is initialized. This
1269 is used to test if profile_id has been created
1270 for cgraph_nodes. */
1273 coverage_node_map_initialized_p (void)
1275 return cgraph_node_map
!= 0;
1278 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1279 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1280 that the PROFILE_IDs was already assigned. */
1283 init_node_map (bool local
)
1285 struct cgraph_node
*n
;
1287 = new hash_map
<unsigned int, cgraph_node
*, profile_id_traits
>;
1289 FOR_EACH_DEFINED_FUNCTION (n
)
1290 if (n
->has_gimple_body_p ())
1295 n
->profile_id
= coverage_compute_profile_id (n
);
1296 while ((val
= cgraph_node_map
->get (n
->profile_id
))
1300 fprintf (dump_file
, "Local profile-id %i conflict"
1301 " with nodes %s/%i %s/%i\n",
1307 n
->profile_id
= (n
->profile_id
+ 1) & 0x7fffffff;
1310 else if (!n
->profile_id
)
1314 "Node %s/%i has no profile-id"
1315 " (profile feedback missing?)\n",
1320 else if ((val
= cgraph_node_map
->get (n
->profile_id
)))
1324 "Node %s/%i has IP profile-id %i conflict. "
1332 cgraph_node_map
->put (n
->profile_id
, n
);
1336 /* Delete the CGRAPH_NODE_MAP. */
1341 delete cgraph_node_map
;
1344 /* Return cgraph node for function with pid */
1347 find_func_by_profile_id (int profile_id
)
1349 cgraph_node
**val
= cgraph_node_map
->get (profile_id
);
1356 /* Perform sanity check on the indirect call target. Due to race conditions,
1357 false function target may be attributed to an indirect call site. If the
1358 call expression type mismatches with the target function's type, expand_call
1359 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1360 Returns true if TARGET is considered ok for call CALL_STMT. */
1363 check_ic_target (gcall
*call_stmt
, struct cgraph_node
*target
)
1366 if (gimple_check_call_matching_types (call_stmt
, target
->decl
, true))
1369 locus
= gimple_location (call_stmt
);
1370 if (dump_enabled_p ())
1371 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
1372 "Skipping target %s with mismatching types for icall\n",
1377 /* Do transformation
1379 if (actual_callee_address == address_of_most_common_function/method)
1386 gimple_ic (gcall
*icall_stmt
, struct cgraph_node
*direct_call
,
1387 int prob
, gcov_type count
, gcov_type all
)
1392 gcall
*iretbnd_stmt
= NULL
;
1393 tree tmp0
, tmp1
, tmp
;
1394 basic_block cond_bb
, dcall_bb
, icall_bb
, join_bb
= NULL
;
1395 tree optype
= build_pointer_type (void_type_node
);
1396 edge e_cd
, e_ci
, e_di
, e_dj
= NULL
, e_ij
;
1397 gimple_stmt_iterator gsi
;
1401 gimple_stmt_iterator psi
;
1403 cond_bb
= gimple_bb (icall_stmt
);
1404 gsi
= gsi_for_stmt (icall_stmt
);
1406 if (gimple_call_with_bounds_p (icall_stmt
) && gimple_call_lhs (icall_stmt
))
1407 iretbnd_stmt
= chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt
));
1409 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1410 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1411 tmp
= unshare_expr (gimple_call_fn (icall_stmt
));
1412 load_stmt
= gimple_build_assign (tmp0
, tmp
);
1413 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1415 tmp
= fold_convert (optype
, build_addr (direct_call
->decl
,
1416 current_function_decl
));
1417 load_stmt
= gimple_build_assign (tmp1
, tmp
);
1418 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1420 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1421 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1423 gimple_set_vdef (icall_stmt
, NULL_TREE
);
1424 gimple_set_vuse (icall_stmt
, NULL_TREE
);
1425 update_stmt (icall_stmt
);
1426 dcall_stmt
= as_a
<gcall
*> (gimple_copy (icall_stmt
));
1427 gimple_call_set_fndecl (dcall_stmt
, direct_call
->decl
);
1428 dflags
= flags_from_decl_or_type (direct_call
->decl
);
1429 if ((dflags
& ECF_NORETURN
) != 0)
1430 gimple_call_set_lhs (dcall_stmt
, NULL_TREE
);
1431 gsi_insert_before (&gsi
, dcall_stmt
, GSI_SAME_STMT
);
1434 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1435 e_cd
= split_block (cond_bb
, cond_stmt
);
1436 dcall_bb
= e_cd
->dest
;
1437 dcall_bb
->count
= count
;
1439 e_di
= split_block (dcall_bb
, dcall_stmt
);
1440 icall_bb
= e_di
->dest
;
1441 icall_bb
->count
= all
- count
;
1443 /* Do not disturb existing EH edges from the indirect call. */
1444 if (!stmt_ends_bb_p (icall_stmt
))
1445 e_ij
= split_block (icall_bb
, icall_stmt
);
1448 e_ij
= find_fallthru_edge (icall_bb
->succs
);
1449 /* The indirect call might be noreturn. */
1452 e_ij
->probability
= REG_BR_PROB_BASE
;
1453 e_ij
->count
= all
- count
;
1454 e_ij
= single_pred_edge (split_edge (e_ij
));
1459 join_bb
= e_ij
->dest
;
1460 join_bb
->count
= all
;
1463 e_cd
->flags
= (e_cd
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1464 e_cd
->probability
= prob
;
1465 e_cd
->count
= count
;
1467 e_ci
= make_edge (cond_bb
, icall_bb
, EDGE_FALSE_VALUE
);
1468 e_ci
->probability
= REG_BR_PROB_BASE
- prob
;
1469 e_ci
->count
= all
- count
;
1475 if ((dflags
& ECF_NORETURN
) != 0)
1479 e_dj
= make_edge (dcall_bb
, join_bb
, EDGE_FALLTHRU
);
1480 e_dj
->probability
= REG_BR_PROB_BASE
;
1481 e_dj
->count
= count
;
1483 e_ij
->count
= all
- count
;
1485 e_ij
->probability
= REG_BR_PROB_BASE
;
1488 /* Insert PHI node for the call result if necessary. */
1489 if (gimple_call_lhs (icall_stmt
)
1490 && TREE_CODE (gimple_call_lhs (icall_stmt
)) == SSA_NAME
1491 && (dflags
& ECF_NORETURN
) == 0)
1493 tree result
= gimple_call_lhs (icall_stmt
);
1494 gphi
*phi
= create_phi_node (result
, join_bb
);
1495 gimple_call_set_lhs (icall_stmt
,
1496 duplicate_ssa_name (result
, icall_stmt
));
1497 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1498 gimple_call_set_lhs (dcall_stmt
,
1499 duplicate_ssa_name (result
, dcall_stmt
));
1500 add_phi_arg (phi
, gimple_call_lhs (dcall_stmt
), e_dj
, UNKNOWN_LOCATION
);
1502 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1503 call then we need to make it's copy for the direct
1507 if (gimple_call_lhs (iretbnd_stmt
))
1511 gimple_set_vdef (iretbnd_stmt
, NULL_TREE
);
1512 gimple_set_vuse (iretbnd_stmt
, NULL_TREE
);
1513 update_stmt (iretbnd_stmt
);
1515 result
= gimple_call_lhs (iretbnd_stmt
);
1516 phi
= create_phi_node (result
, join_bb
);
1518 copy
= gimple_copy (iretbnd_stmt
);
1519 gimple_call_set_arg (copy
, 0,
1520 gimple_call_lhs (dcall_stmt
));
1521 gimple_call_set_lhs (copy
, duplicate_ssa_name (result
, copy
));
1522 gsi_insert_on_edge (e_dj
, copy
);
1523 add_phi_arg (phi
, gimple_call_lhs (copy
),
1524 e_dj
, UNKNOWN_LOCATION
);
1526 gimple_call_set_arg (iretbnd_stmt
, 0,
1527 gimple_call_lhs (icall_stmt
));
1528 gimple_call_set_lhs (iretbnd_stmt
,
1529 duplicate_ssa_name (result
, iretbnd_stmt
));
1530 psi
= gsi_for_stmt (iretbnd_stmt
);
1531 gsi_remove (&psi
, false);
1532 gsi_insert_on_edge (e_ij
, iretbnd_stmt
);
1533 add_phi_arg (phi
, gimple_call_lhs (iretbnd_stmt
),
1534 e_ij
, UNKNOWN_LOCATION
);
1536 gsi_commit_one_edge_insert (e_dj
, NULL
);
1537 gsi_commit_one_edge_insert (e_ij
, NULL
);
1541 psi
= gsi_for_stmt (iretbnd_stmt
);
1542 gsi_remove (&psi
, true);
1547 /* Build an EH edge for the direct call if necessary. */
1548 lp_nr
= lookup_stmt_eh_lp (icall_stmt
);
1549 if (lp_nr
> 0 && stmt_could_throw_p (dcall_stmt
))
1551 add_stmt_to_eh_lp (dcall_stmt
, lp_nr
);
1554 FOR_EACH_EDGE (e_eh
, ei
, icall_bb
->succs
)
1555 if (e_eh
->flags
& (EDGE_EH
| EDGE_ABNORMAL
))
1557 e
= make_edge (dcall_bb
, e_eh
->dest
, e_eh
->flags
);
1558 for (gphi_iterator psi
= gsi_start_phis (e_eh
->dest
);
1559 !gsi_end_p (psi
); gsi_next (&psi
))
1561 gphi
*phi
= psi
.phi ();
1562 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
),
1563 PHI_ARG_DEF_FROM_EDGE (phi
, e_eh
));
1570 For every checked indirect/virtual call determine if most common pid of
1571 function/class method has probability more than 50%. If yes modify code of
1576 gimple_ic_transform (gimple_stmt_iterator
*gsi
)
1579 histogram_value histogram
;
1580 gcov_type val
, count
, all
, bb_all
;
1581 struct cgraph_node
*direct_call
;
1583 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1587 if (gimple_call_fndecl (stmt
) != NULL_TREE
)
1590 if (gimple_call_internal_p (stmt
))
1593 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INDIR_CALL
);
1597 val
= histogram
->hvalue
.counters
[0];
1598 count
= histogram
->hvalue
.counters
[1];
1599 all
= histogram
->hvalue
.counters
[2];
1601 bb_all
= gimple_bb (stmt
)->count
;
1602 /* The order of CHECK_COUNTER calls is important -
1603 since check_counter can correct the third parameter
1604 and we want to make count <= all <= bb_all. */
1605 if ( check_counter (stmt
, "ic", &all
, &bb_all
, bb_all
)
1606 || check_counter (stmt
, "ic", &count
, &all
, all
))
1608 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1612 if (4 * count
<= 3 * all
)
1615 direct_call
= find_func_by_profile_id ((int)val
);
1617 if (direct_call
== NULL
)
1623 fprintf (dump_file
, "Indirect call -> direct call from other module");
1624 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1625 fprintf (dump_file
, "=> %i (will resolve only with LTO)\n", (int)val
);
1631 if (!check_ic_target (stmt
, direct_call
))
1635 fprintf (dump_file
, "Indirect call -> direct call ");
1636 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1637 fprintf (dump_file
, "=> ");
1638 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1639 fprintf (dump_file
, " transformation skipped because of type mismatch");
1640 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1642 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1648 fprintf (dump_file
, "Indirect call -> direct call ");
1649 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1650 fprintf (dump_file
, "=> ");
1651 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1652 fprintf (dump_file
, " transformation on insn postponned to ipa-profile");
1653 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1654 fprintf (dump_file
, "hist->count %"PRId64
1655 " hist->all %"PRId64
"\n", count
, all
);
1661 /* Return true if the stringop CALL with FNDECL shall be profiled.
1662 SIZE_ARG be set to the argument index for the size of the string
1666 interesting_stringop_to_profile_p (tree fndecl
, gcall
*call
, int *size_arg
)
1668 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
1670 if (fcode
!= BUILT_IN_MEMCPY
&& fcode
!= BUILT_IN_MEMPCPY
1671 && fcode
!= BUILT_IN_MEMSET
&& fcode
!= BUILT_IN_BZERO
)
1676 case BUILT_IN_MEMCPY
:
1677 case BUILT_IN_MEMPCPY
:
1679 return validate_gimple_arglist (call
, POINTER_TYPE
, POINTER_TYPE
,
1680 INTEGER_TYPE
, VOID_TYPE
);
1681 case BUILT_IN_MEMSET
:
1683 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1684 INTEGER_TYPE
, VOID_TYPE
);
1685 case BUILT_IN_BZERO
:
1687 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1694 /* Convert stringop (..., vcall_size)
1696 if (vcall_size == icall_size)
1697 stringop (..., icall_size);
1699 stringop (..., vcall_size);
1700 assuming we'll propagate a true constant into ICALL_SIZE later. */
1703 gimple_stringop_fixed_value (gcall
*vcall_stmt
, tree icall_size
, int prob
,
1704 gcov_type count
, gcov_type all
)
1709 tree tmp0
, tmp1
, vcall_size
, optype
;
1710 basic_block cond_bb
, icall_bb
, vcall_bb
, join_bb
;
1711 edge e_ci
, e_cv
, e_iv
, e_ij
, e_vj
;
1712 gimple_stmt_iterator gsi
;
1716 fndecl
= gimple_call_fndecl (vcall_stmt
);
1717 if (!interesting_stringop_to_profile_p (fndecl
, vcall_stmt
, &size_arg
))
1720 cond_bb
= gimple_bb (vcall_stmt
);
1721 gsi
= gsi_for_stmt (vcall_stmt
);
1723 vcall_size
= gimple_call_arg (vcall_stmt
, size_arg
);
1724 optype
= TREE_TYPE (vcall_size
);
1726 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1727 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1728 tmp_stmt
= gimple_build_assign (tmp0
, fold_convert (optype
, icall_size
));
1729 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1731 tmp_stmt
= gimple_build_assign (tmp1
, vcall_size
);
1732 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1734 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1735 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1737 gimple_set_vdef (vcall_stmt
, NULL
);
1738 gimple_set_vuse (vcall_stmt
, NULL
);
1739 update_stmt (vcall_stmt
);
1740 icall_stmt
= as_a
<gcall
*> (gimple_copy (vcall_stmt
));
1741 gimple_call_set_arg (icall_stmt
, size_arg
, icall_size
);
1742 gsi_insert_before (&gsi
, icall_stmt
, GSI_SAME_STMT
);
1745 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1746 e_ci
= split_block (cond_bb
, cond_stmt
);
1747 icall_bb
= e_ci
->dest
;
1748 icall_bb
->count
= count
;
1750 e_iv
= split_block (icall_bb
, icall_stmt
);
1751 vcall_bb
= e_iv
->dest
;
1752 vcall_bb
->count
= all
- count
;
1754 e_vj
= split_block (vcall_bb
, vcall_stmt
);
1755 join_bb
= e_vj
->dest
;
1756 join_bb
->count
= all
;
1758 e_ci
->flags
= (e_ci
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1759 e_ci
->probability
= prob
;
1760 e_ci
->count
= count
;
1762 e_cv
= make_edge (cond_bb
, vcall_bb
, EDGE_FALSE_VALUE
);
1763 e_cv
->probability
= REG_BR_PROB_BASE
- prob
;
1764 e_cv
->count
= all
- count
;
1768 e_ij
= make_edge (icall_bb
, join_bb
, EDGE_FALLTHRU
);
1769 e_ij
->probability
= REG_BR_PROB_BASE
;
1770 e_ij
->count
= count
;
1772 e_vj
->probability
= REG_BR_PROB_BASE
;
1773 e_vj
->count
= all
- count
;
1775 /* Insert PHI node for the call result if necessary. */
1776 if (gimple_call_lhs (vcall_stmt
)
1777 && TREE_CODE (gimple_call_lhs (vcall_stmt
)) == SSA_NAME
)
1779 tree result
= gimple_call_lhs (vcall_stmt
);
1780 gphi
*phi
= create_phi_node (result
, join_bb
);
1781 gimple_call_set_lhs (vcall_stmt
,
1782 duplicate_ssa_name (result
, vcall_stmt
));
1783 add_phi_arg (phi
, gimple_call_lhs (vcall_stmt
), e_vj
, UNKNOWN_LOCATION
);
1784 gimple_call_set_lhs (icall_stmt
,
1785 duplicate_ssa_name (result
, icall_stmt
));
1786 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1789 /* Because these are all string op builtins, they're all nothrow. */
1790 gcc_assert (!stmt_could_throw_p (vcall_stmt
));
1791 gcc_assert (!stmt_could_throw_p (icall_stmt
));
1794 /* Find values inside STMT for that we want to measure histograms for
1795 division/modulo optimization. */
1797 gimple_stringops_transform (gimple_stmt_iterator
*gsi
)
1802 enum built_in_function fcode
;
1803 histogram_value histogram
;
1804 gcov_type count
, all
, val
;
1806 unsigned int dest_align
, src_align
;
1811 stmt
= dyn_cast
<gcall
*> (gsi_stmt (*gsi
));
1814 fndecl
= gimple_call_fndecl (stmt
);
1817 fcode
= DECL_FUNCTION_CODE (fndecl
);
1818 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1821 blck_size
= gimple_call_arg (stmt
, size_arg
);
1822 if (TREE_CODE (blck_size
) == INTEGER_CST
)
1825 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_SINGLE_VALUE
);
1828 val
= histogram
->hvalue
.counters
[0];
1829 count
= histogram
->hvalue
.counters
[1];
1830 all
= histogram
->hvalue
.counters
[2];
1831 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1832 /* We require that count is at least half of all; this means
1833 that for the transformation to fire the value must be constant
1834 at least 80% of time. */
1835 if ((6 * count
/ 5) < all
|| optimize_bb_for_size_p (gimple_bb (stmt
)))
1837 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
1840 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1843 dest
= gimple_call_arg (stmt
, 0);
1844 dest_align
= get_pointer_alignment (dest
);
1847 case BUILT_IN_MEMCPY
:
1848 case BUILT_IN_MEMPCPY
:
1849 src
= gimple_call_arg (stmt
, 1);
1850 src_align
= get_pointer_alignment (src
);
1851 if (!can_move_by_pieces (val
, MIN (dest_align
, src_align
)))
1854 case BUILT_IN_MEMSET
:
1855 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1856 gimple_call_arg (stmt
, 1),
1860 case BUILT_IN_BZERO
:
1861 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1869 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
1870 tree_val
= build_int_cst (get_gcov_type (), val
);
1874 a
[0] = (unsigned HOST_WIDE_INT
) val
;
1875 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
1877 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
1878 TYPE_PRECISION (get_gcov_type ()), false));
1883 fprintf (dump_file
, "Single value %i stringop transformation on ",
1885 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1887 gimple_stringop_fixed_value (stmt
, tree_val
, prob
, count
, all
);
1893 stringop_block_profile (gimple stmt
, unsigned int *expected_align
,
1894 HOST_WIDE_INT
*expected_size
)
1896 histogram_value histogram
;
1897 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_AVERAGE
);
1899 *expected_size
= -1;
1900 else if (!histogram
->hvalue
.counters
[1])
1902 *expected_size
= -1;
1903 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1908 size
= ((histogram
->hvalue
.counters
[0]
1909 + histogram
->hvalue
.counters
[1] / 2)
1910 / histogram
->hvalue
.counters
[1]);
1911 /* Even if we can hold bigger value in SIZE, INT_MAX
1912 is safe "infinity" for code generation strategies. */
1915 *expected_size
= size
;
1916 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1918 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_IOR
);
1920 *expected_align
= 0;
1921 else if (!histogram
->hvalue
.counters
[0])
1923 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1924 *expected_align
= 0;
1931 count
= histogram
->hvalue
.counters
[0];
1933 while (!(count
& alignment
)
1934 && (alignment
* 2 * BITS_PER_UNIT
))
1936 *expected_align
= alignment
* BITS_PER_UNIT
;
1937 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1942 /* Find values inside STMT for that we want to measure histograms for
1943 division/modulo optimization. */
1945 gimple_divmod_values_to_profile (gimple stmt
, histogram_values
*values
)
1947 tree lhs
, divisor
, op0
, type
;
1948 histogram_value hist
;
1950 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1953 lhs
= gimple_assign_lhs (stmt
);
1954 type
= TREE_TYPE (lhs
);
1955 if (!INTEGRAL_TYPE_P (type
))
1958 switch (gimple_assign_rhs_code (stmt
))
1960 case TRUNC_DIV_EXPR
:
1961 case TRUNC_MOD_EXPR
:
1962 divisor
= gimple_assign_rhs2 (stmt
);
1963 op0
= gimple_assign_rhs1 (stmt
);
1965 values
->reserve (3);
1967 if (TREE_CODE (divisor
) == SSA_NAME
)
1968 /* Check for the case where the divisor is the same value most
1970 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1971 HIST_TYPE_SINGLE_VALUE
,
1974 /* For mod, check whether it is not often a noop (or replaceable by
1975 a few subtractions). */
1976 if (gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
1977 && TYPE_UNSIGNED (type
))
1980 /* Check for a special case where the divisor is power of 2. */
1981 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1985 val
= build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
1986 hist
= gimple_alloc_histogram_value (cfun
, HIST_TYPE_INTERVAL
,
1988 hist
->hdata
.intvl
.int_start
= 0;
1989 hist
->hdata
.intvl
.steps
= 2;
1990 values
->quick_push (hist
);
1999 /* Find calls inside STMT for that we want to measure histograms for
2000 indirect/virtual call optimization. */
2003 gimple_indirect_call_to_profile (gimple stmt
, histogram_values
*values
)
2007 if (gimple_code (stmt
) != GIMPLE_CALL
2008 || gimple_call_internal_p (stmt
)
2009 || gimple_call_fndecl (stmt
) != NULL_TREE
)
2012 callee
= gimple_call_fn (stmt
);
2014 values
->reserve (3);
2016 values
->quick_push (gimple_alloc_histogram_value (
2018 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE
) ?
2019 HIST_TYPE_INDIR_CALL_TOPN
:
2020 HIST_TYPE_INDIR_CALL
,
2026 /* Find values inside STMT for that we want to measure histograms for
2027 string operations. */
2029 gimple_stringops_values_to_profile (gimple gs
, histogram_values
*values
)
2037 stmt
= dyn_cast
<gcall
*> (gs
);
2040 fndecl
= gimple_call_fndecl (stmt
);
2044 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
2047 dest
= gimple_call_arg (stmt
, 0);
2048 blck_size
= gimple_call_arg (stmt
, size_arg
);
2050 if (TREE_CODE (blck_size
) != INTEGER_CST
)
2052 values
->safe_push (gimple_alloc_histogram_value (cfun
,
2053 HIST_TYPE_SINGLE_VALUE
,
2055 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_AVERAGE
,
2058 if (TREE_CODE (blck_size
) != INTEGER_CST
)
2059 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_IOR
,
2063 /* Find values inside STMT for that we want to measure histograms and adds
2064 them to list VALUES. */
2067 gimple_values_to_profile (gimple stmt
, histogram_values
*values
)
2069 gimple_divmod_values_to_profile (stmt
, values
);
2070 gimple_stringops_values_to_profile (stmt
, values
);
2071 gimple_indirect_call_to_profile (stmt
, values
);
2075 gimple_find_values_to_profile (histogram_values
*values
)
2078 gimple_stmt_iterator gsi
;
2080 histogram_value hist
= NULL
;
2083 FOR_EACH_BB_FN (bb
, cfun
)
2084 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2085 gimple_values_to_profile (gsi_stmt (gsi
), values
);
2087 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_TIME_PROFILE
, 0, 0));
2089 FOR_EACH_VEC_ELT (*values
, i
, hist
)
2093 case HIST_TYPE_INTERVAL
:
2094 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
2097 case HIST_TYPE_POW2
:
2098 hist
->n_counters
= 2;
2101 case HIST_TYPE_SINGLE_VALUE
:
2102 hist
->n_counters
= 3;
2105 case HIST_TYPE_CONST_DELTA
:
2106 hist
->n_counters
= 4;
2109 case HIST_TYPE_INDIR_CALL
:
2110 hist
->n_counters
= 3;
2113 case HIST_TYPE_TIME_PROFILE
:
2114 hist
->n_counters
= 1;
2117 case HIST_TYPE_AVERAGE
:
2118 hist
->n_counters
= 2;
2122 hist
->n_counters
= 1;
2125 case HIST_TYPE_INDIR_CALL_TOPN
:
2126 hist
->n_counters
= GCOV_ICALL_TOPN_NCOUNTS
;
2134 fprintf (dump_file
, "Stmt ");
2135 print_gimple_stmt (dump_file
, hist
->hvalue
.stmt
, 0, TDF_SLIM
);
2136 dump_histogram_value (dump_file
, hist
);