1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2013 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"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "value-prof.h"
30 #include "insn-config.h"
36 #include "tree-flow-inline.h"
37 #include "diagnostic.h"
38 #include "gimple-pretty-print.h"
45 #include "pointer-set.h"
47 #include "data-streamer.h"
49 /* In this file value profile based optimizations are placed. Currently the
50 following optimizations are implemented (for more detailed descriptions
51 see comments at value_profile_transformations):
53 1) Division/modulo specialization. Provided that we can determine that the
54 operands of the division have some special properties, we may use it to
55 produce more effective code.
57 2) Indirect/virtual call specialization. If we can determine most
58 common function callee in indirect/virtual call. We can use this
59 information to improve code effectiveness (especially info for
62 3) Speculative prefetching. If we are able to determine that the difference
63 between addresses accessed by a memory reference is usually constant, we
64 may add the prefetch instructions.
65 FIXME: This transformation was removed together with RTL based value
69 Value profiling internals
70 ==========================
72 Every value profiling transformation starts with defining what values
73 to profile. There are different histogram types (see HIST_TYPE_* in
74 value-prof.h) and each transformation can request one or more histogram
75 types per GIMPLE statement. The function gimple_find_values_to_profile()
76 collects the values to profile in a vec, and adds the number of counters
77 required for the different histogram types.
79 For a -fprofile-generate run, the statements for which values should be
80 recorded, are instrumented in instrument_values(). The instrumentation
81 is done by helper functions that can be found in tree-profile.c, where
82 new types of histograms can be added if necessary.
84 After a -fprofile-use, the value profiling data is read back in by
85 compute_value_histograms() that translates the collected data to
86 histograms and attaches them to the profiled statements via
87 gimple_add_histogram_value(). Histograms are stored in a hash table
88 that is attached to every intrumented function, see VALUE_HISTOGRAMS
91 The value-profile transformations driver is the function
92 gimple_value_profile_transformations(). It traverses all statements in
93 the to-be-transformed function, and looks for statements with one or
94 more histograms attached to it. If a statement has histograms, the
95 transformation functions are called on the statement.
97 Limitations / FIXME / TODO:
98 * Only one histogram of each type can be associated with a statement.
99 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
100 (This type of histogram was originally used to implement a form of
101 stride profiling based speculative prefetching to improve SPEC2000
102 scores for memory-bound benchmarks, mcf and equake. However, this
103 was an RTL value-profiling transformation, and those have all been
105 * Some value profile transformations are done in builtins.c (?!)
106 * Updating of histograms needs some TLC.
107 * The value profiling code could be used to record analysis results
108 from non-profiling (e.g. VRP).
109 * Adding new profilers should be simplified, starting with a cleanup
110 of what-happens-where andwith making gimple_find_values_to_profile
111 and gimple_value_profile_transformations table-driven, perhaps...
114 static tree
gimple_divmod_fixed_value (gimple
, tree
, int, gcov_type
, gcov_type
);
115 static tree
gimple_mod_pow2 (gimple
, int, gcov_type
, gcov_type
);
116 static tree
gimple_mod_subtract (gimple
, int, int, int, gcov_type
, gcov_type
,
118 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*);
119 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator
*);
120 static bool gimple_mod_subtract_transform (gimple_stmt_iterator
*);
121 static bool gimple_stringops_transform (gimple_stmt_iterator
*);
122 static bool gimple_ic_transform (gimple_stmt_iterator
*);
124 /* Allocate histogram value. */
126 static histogram_value
127 gimple_alloc_histogram_value (struct function
*fun ATTRIBUTE_UNUSED
,
128 enum hist_type type
, gimple stmt
, tree value
)
130 histogram_value hist
= (histogram_value
) xcalloc (1, sizeof (*hist
));
131 hist
->hvalue
.value
= value
;
132 hist
->hvalue
.stmt
= stmt
;
137 /* Hash value for histogram. */
140 histogram_hash (const void *x
)
142 return htab_hash_pointer (((const_histogram_value
)x
)->hvalue
.stmt
);
145 /* Return nonzero if statement for histogram_value X is Y. */
148 histogram_eq (const void *x
, const void *y
)
150 return ((const_histogram_value
) x
)->hvalue
.stmt
== (const_gimple
) y
;
153 /* Set histogram for STMT. */
156 set_histogram_value (struct function
*fun
, gimple stmt
, histogram_value hist
)
159 if (!hist
&& !VALUE_HISTOGRAMS (fun
))
161 if (!VALUE_HISTOGRAMS (fun
))
162 VALUE_HISTOGRAMS (fun
) = htab_create (1, histogram_hash
,
164 loc
= htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
165 htab_hash_pointer (stmt
),
166 hist
? INSERT
: NO_INSERT
);
170 htab_clear_slot (VALUE_HISTOGRAMS (fun
), loc
);
176 /* Get histogram list for STMT. */
179 gimple_histogram_value (struct function
*fun
, gimple stmt
)
181 if (!VALUE_HISTOGRAMS (fun
))
183 return (histogram_value
) htab_find_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
184 htab_hash_pointer (stmt
));
187 /* Add histogram for STMT. */
190 gimple_add_histogram_value (struct function
*fun
, gimple stmt
,
191 histogram_value hist
)
193 hist
->hvalue
.next
= gimple_histogram_value (fun
, stmt
);
194 set_histogram_value (fun
, stmt
, hist
);
198 /* Remove histogram HIST from STMT's histogram list. */
201 gimple_remove_histogram_value (struct function
*fun
, gimple stmt
,
202 histogram_value hist
)
204 histogram_value hist2
= gimple_histogram_value (fun
, stmt
);
207 set_histogram_value (fun
, stmt
, hist
->hvalue
.next
);
211 while (hist2
->hvalue
.next
!= hist
)
212 hist2
= hist2
->hvalue
.next
;
213 hist2
->hvalue
.next
= hist
->hvalue
.next
;
215 free (hist
->hvalue
.counters
);
216 #ifdef ENABLE_CHECKING
217 memset (hist
, 0xab, sizeof (*hist
));
223 /* Lookup histogram of type TYPE in the STMT. */
226 gimple_histogram_value_of_type (struct function
*fun
, gimple stmt
,
229 histogram_value hist
;
230 for (hist
= gimple_histogram_value (fun
, stmt
); hist
;
231 hist
= hist
->hvalue
.next
)
232 if (hist
->type
== type
)
237 /* Dump information about HIST to DUMP_FILE. */
240 dump_histogram_value (FILE *dump_file
, histogram_value hist
)
244 case HIST_TYPE_INTERVAL
:
245 fprintf (dump_file
, "Interval counter range %d -- %d",
246 hist
->hdata
.intvl
.int_start
,
247 (hist
->hdata
.intvl
.int_start
248 + hist
->hdata
.intvl
.steps
- 1));
249 if (hist
->hvalue
.counters
)
252 fprintf(dump_file
, " [");
253 for (i
= 0; i
< hist
->hdata
.intvl
.steps
; i
++)
254 fprintf (dump_file
, " %d:"HOST_WIDEST_INT_PRINT_DEC
,
255 hist
->hdata
.intvl
.int_start
+ i
,
256 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[i
]);
257 fprintf (dump_file
, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC
,
258 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[i
]);
260 fprintf (dump_file
, ".\n");
264 fprintf (dump_file
, "Pow2 counter ");
265 if (hist
->hvalue
.counters
)
267 fprintf (dump_file
, "pow2:"HOST_WIDEST_INT_PRINT_DEC
268 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC
,
269 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
270 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1]);
272 fprintf (dump_file
, ".\n");
275 case HIST_TYPE_SINGLE_VALUE
:
276 fprintf (dump_file
, "Single value ");
277 if (hist
->hvalue
.counters
)
279 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
280 " match:"HOST_WIDEST_INT_PRINT_DEC
281 " wrong:"HOST_WIDEST_INT_PRINT_DEC
,
282 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
283 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
284 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
286 fprintf (dump_file
, ".\n");
289 case HIST_TYPE_AVERAGE
:
290 fprintf (dump_file
, "Average value ");
291 if (hist
->hvalue
.counters
)
293 fprintf (dump_file
, "sum:"HOST_WIDEST_INT_PRINT_DEC
294 " times:"HOST_WIDEST_INT_PRINT_DEC
,
295 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
296 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1]);
298 fprintf (dump_file
, ".\n");
302 fprintf (dump_file
, "IOR value ");
303 if (hist
->hvalue
.counters
)
305 fprintf (dump_file
, "ior:"HOST_WIDEST_INT_PRINT_DEC
,
306 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0]);
308 fprintf (dump_file
, ".\n");
311 case HIST_TYPE_CONST_DELTA
:
312 fprintf (dump_file
, "Constant delta ");
313 if (hist
->hvalue
.counters
)
315 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
316 " match:"HOST_WIDEST_INT_PRINT_DEC
317 " wrong:"HOST_WIDEST_INT_PRINT_DEC
,
318 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
319 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
320 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
322 fprintf (dump_file
, ".\n");
324 case HIST_TYPE_INDIR_CALL
:
325 fprintf (dump_file
, "Indirect call ");
326 if (hist
->hvalue
.counters
)
328 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
329 " match:"HOST_WIDEST_INT_PRINT_DEC
330 " all:"HOST_WIDEST_INT_PRINT_DEC
,
331 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
332 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
333 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
335 fprintf (dump_file
, ".\n");
342 /* Dump information about HIST to DUMP_FILE. */
345 stream_out_histogram_value (struct output_block
*ob
, histogram_value hist
)
350 bp
= bitpack_create (ob
->main_stream
);
351 bp_pack_enum (&bp
, hist_type
, HIST_TYPE_MAX
, hist
->type
);
352 bp_pack_value (&bp
, hist
->hvalue
.next
!= NULL
, 1);
353 streamer_write_bitpack (&bp
);
356 case HIST_TYPE_INTERVAL
:
357 streamer_write_hwi (ob
, hist
->hdata
.intvl
.int_start
);
358 streamer_write_uhwi (ob
, hist
->hdata
.intvl
.steps
);
363 for (i
= 0; i
< hist
->n_counters
; i
++)
364 streamer_write_gcov_count (ob
, hist
->hvalue
.counters
[i
]);
365 if (hist
->hvalue
.next
)
366 stream_out_histogram_value (ob
, hist
->hvalue
.next
);
368 /* Dump information about HIST to DUMP_FILE. */
371 stream_in_histogram_value (struct lto_input_block
*ib
, gimple stmt
)
374 unsigned int ncounters
= 0;
377 histogram_value new_val
;
379 histogram_value
*next_p
= NULL
;
383 bp
= streamer_read_bitpack (ib
);
384 type
= bp_unpack_enum (&bp
, hist_type
, HIST_TYPE_MAX
);
385 next
= bp_unpack_value (&bp
, 1);
386 new_val
= gimple_alloc_histogram_value (cfun
, type
, stmt
, NULL
);
389 case HIST_TYPE_INTERVAL
:
390 new_val
->hdata
.intvl
.int_start
= streamer_read_hwi (ib
);
391 new_val
->hdata
.intvl
.steps
= streamer_read_uhwi (ib
);
392 ncounters
= new_val
->hdata
.intvl
.steps
+ 2;
396 case HIST_TYPE_AVERAGE
:
400 case HIST_TYPE_SINGLE_VALUE
:
401 case HIST_TYPE_INDIR_CALL
:
405 case HIST_TYPE_CONST_DELTA
:
415 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * ncounters
);
416 new_val
->n_counters
= ncounters
;
417 for (i
= 0; i
< ncounters
; i
++)
418 new_val
->hvalue
.counters
[i
] = streamer_read_gcov_count (ib
);
420 gimple_add_histogram_value (cfun
, stmt
, new_val
);
423 next_p
= &new_val
->hvalue
.next
;
428 /* Dump all histograms attached to STMT to DUMP_FILE. */
431 dump_histograms_for_stmt (struct function
*fun
, FILE *dump_file
, gimple stmt
)
433 histogram_value hist
;
434 for (hist
= gimple_histogram_value (fun
, stmt
); hist
; hist
= hist
->hvalue
.next
)
435 dump_histogram_value (dump_file
, hist
);
438 /* Remove all histograms associated with STMT. */
441 gimple_remove_stmt_histograms (struct function
*fun
, gimple stmt
)
444 while ((val
= gimple_histogram_value (fun
, stmt
)) != NULL
)
445 gimple_remove_histogram_value (fun
, stmt
, val
);
448 /* Duplicate all histograms associates with OSTMT to STMT. */
451 gimple_duplicate_stmt_histograms (struct function
*fun
, gimple stmt
,
452 struct function
*ofun
, gimple ostmt
)
455 for (val
= gimple_histogram_value (ofun
, ostmt
); val
!= NULL
; val
= val
->hvalue
.next
)
457 histogram_value new_val
= gimple_alloc_histogram_value (fun
, val
->type
, NULL
, NULL
);
458 memcpy (new_val
, val
, sizeof (*val
));
459 new_val
->hvalue
.stmt
= stmt
;
460 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
461 memcpy (new_val
->hvalue
.counters
, val
->hvalue
.counters
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
462 gimple_add_histogram_value (fun
, stmt
, new_val
);
467 /* Move all histograms associated with OSTMT to STMT. */
470 gimple_move_stmt_histograms (struct function
*fun
, gimple stmt
, gimple ostmt
)
472 histogram_value val
= gimple_histogram_value (fun
, ostmt
);
475 /* The following three statements can't be reordered,
476 because histogram hashtab relies on stmt field value
477 for finding the exact slot. */
478 set_histogram_value (fun
, ostmt
, NULL
);
479 for (; val
!= NULL
; val
= val
->hvalue
.next
)
480 val
->hvalue
.stmt
= stmt
;
481 set_histogram_value (fun
, stmt
, val
);
485 static bool error_found
= false;
487 /* Helper function for verify_histograms. For each histogram reachable via htab
488 walk verify that it was reached via statement walk. */
491 visit_hist (void **slot
, void *data
)
493 struct pointer_set_t
*visited
= (struct pointer_set_t
*) data
;
494 histogram_value hist
= *(histogram_value
*) slot
;
495 if (!pointer_set_contains (visited
, hist
))
497 error ("dead histogram");
498 dump_histogram_value (stderr
, hist
);
499 debug_gimple_stmt (hist
->hvalue
.stmt
);
506 /* Verify sanity of the histograms. */
509 verify_histograms (void)
512 gimple_stmt_iterator gsi
;
513 histogram_value hist
;
514 struct pointer_set_t
*visited_hists
;
517 visited_hists
= pointer_set_create ();
519 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
521 gimple stmt
= gsi_stmt (gsi
);
523 for (hist
= gimple_histogram_value (cfun
, stmt
); hist
;
524 hist
= hist
->hvalue
.next
)
526 if (hist
->hvalue
.stmt
!= stmt
)
528 error ("Histogram value statement does not correspond to "
529 "the statement it is associated with");
530 debug_gimple_stmt (stmt
);
531 dump_histogram_value (stderr
, hist
);
534 pointer_set_insert (visited_hists
, hist
);
537 if (VALUE_HISTOGRAMS (cfun
))
538 htab_traverse (VALUE_HISTOGRAMS (cfun
), visit_hist
, visited_hists
);
539 pointer_set_destroy (visited_hists
);
541 internal_error ("verify_histograms failed");
544 /* Helper function for verify_histograms. For each histogram reachable via htab
545 walk verify that it was reached via statement walk. */
548 free_hist (void **slot
, void *data ATTRIBUTE_UNUSED
)
550 histogram_value hist
= *(histogram_value
*) slot
;
551 free (hist
->hvalue
.counters
);
552 #ifdef ENABLE_CHECKING
553 memset (hist
, 0xab, sizeof (*hist
));
560 free_histograms (void)
562 if (VALUE_HISTOGRAMS (cfun
))
564 htab_traverse (VALUE_HISTOGRAMS (cfun
), free_hist
, NULL
);
565 htab_delete (VALUE_HISTOGRAMS (cfun
));
566 VALUE_HISTOGRAMS (cfun
) = NULL
;
571 /* The overall number of invocations of the counter should match
572 execution count of basic block. Report it as error rather than
573 internal error as it might mean that user has misused the profile
577 check_counter (gimple stmt
, const char * name
,
578 gcov_type
*count
, gcov_type
*all
, gcov_type bb_count
)
580 if (*all
!= bb_count
|| *count
> *all
)
583 locus
= (stmt
!= NULL
)
584 ? gimple_location (stmt
)
585 : DECL_SOURCE_LOCATION (current_function_decl
);
586 if (flag_profile_correction
)
588 if (dump_enabled_p ())
589 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
590 "correcting inconsistent value profile: %s "
591 "profiler overall count (%d) does not match BB "
592 "count (%d)\n", name
, (int)*all
, (int)bb_count
);
600 error_at (locus
, "corrupted value profile: %s "
601 "profile counter (%d out of %d) inconsistent with "
602 "basic-block count (%d)",
615 /* GIMPLE based transformations. */
618 gimple_value_profile_transformations (void)
621 gimple_stmt_iterator gsi
;
622 bool changed
= false;
626 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
628 gimple stmt
= gsi_stmt (gsi
);
629 histogram_value th
= gimple_histogram_value (cfun
, stmt
);
635 fprintf (dump_file
, "Trying transformations on stmt ");
636 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
637 dump_histograms_for_stmt (cfun
, dump_file
, stmt
);
640 /* Transformations: */
641 /* The order of things in this conditional controls which
642 transformation is used when more than one is applicable. */
643 /* It is expected that any code added by the transformations
644 will be added before the current statement, and that the
645 current statement remain valid (although possibly
646 modified) upon return. */
647 if (gimple_mod_subtract_transform (&gsi
)
648 || gimple_divmod_fixed_value_transform (&gsi
)
649 || gimple_mod_pow2_value_transform (&gsi
)
650 || gimple_stringops_transform (&gsi
)
651 || gimple_ic_transform (&gsi
))
653 stmt
= gsi_stmt (gsi
);
655 /* Original statement may no longer be in the same block. */
656 if (bb
!= gimple_bb (stmt
))
658 bb
= gimple_bb (stmt
);
659 gsi
= gsi_for_stmt (stmt
);
674 /* Generate code for transformation 1 (with parent gimple assignment
675 STMT and probability of taking the optimal path PROB, which is
676 equivalent to COUNT/ALL within roundoff error). This generates the
677 result into a temp and returns the temp; it does not replace or
678 alter the original STMT. */
681 gimple_divmod_fixed_value (gimple stmt
, tree value
, int prob
, gcov_type count
,
684 gimple stmt1
, stmt2
, stmt3
;
685 tree tmp0
, tmp1
, tmp2
;
686 gimple bb1end
, bb2end
, bb3end
;
687 basic_block bb
, bb2
, bb3
, bb4
;
688 tree optype
, op1
, op2
;
689 edge e12
, e13
, e23
, e24
, e34
;
690 gimple_stmt_iterator gsi
;
692 gcc_assert (is_gimple_assign (stmt
)
693 && (gimple_assign_rhs_code (stmt
) == TRUNC_DIV_EXPR
694 || gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
));
696 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
697 op1
= gimple_assign_rhs1 (stmt
);
698 op2
= gimple_assign_rhs2 (stmt
);
700 bb
= gimple_bb (stmt
);
701 gsi
= gsi_for_stmt (stmt
);
703 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
704 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
705 stmt1
= gimple_build_assign (tmp0
, fold_convert (optype
, value
));
706 stmt2
= gimple_build_assign (tmp1
, op2
);
707 stmt3
= gimple_build_cond (NE_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
708 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
709 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
710 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
713 tmp2
= create_tmp_reg (optype
, "PROF");
714 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), tmp2
,
716 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
719 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), tmp2
,
721 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
725 /* Edge e23 connects bb2 to bb3, etc. */
726 e12
= split_block (bb
, bb1end
);
729 e23
= split_block (bb2
, bb2end
);
731 bb3
->count
= all
- count
;
732 e34
= split_block (bb3
, bb3end
);
736 e12
->flags
&= ~EDGE_FALLTHRU
;
737 e12
->flags
|= EDGE_FALSE_VALUE
;
738 e12
->probability
= prob
;
741 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
742 e13
->probability
= REG_BR_PROB_BASE
- prob
;
743 e13
->count
= all
- count
;
747 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
748 e24
->probability
= REG_BR_PROB_BASE
;
751 e34
->probability
= REG_BR_PROB_BASE
;
752 e34
->count
= all
- count
;
758 /* Do transform 1) on INSN if applicable. */
761 gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*si
)
763 histogram_value histogram
;
765 gcov_type val
, count
, all
;
766 tree result
, value
, tree_val
;
770 stmt
= gsi_stmt (*si
);
771 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
774 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
))))
777 code
= gimple_assign_rhs_code (stmt
);
779 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
782 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
783 HIST_TYPE_SINGLE_VALUE
);
787 value
= histogram
->hvalue
.value
;
788 val
= histogram
->hvalue
.counters
[0];
789 count
= histogram
->hvalue
.counters
[1];
790 all
= histogram
->hvalue
.counters
[2];
791 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
793 /* We require that count is at least half of all; this means
794 that for the transformation to fire the value must be constant
795 at least 50% of time (and 75% gives the guarantee of usage). */
796 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
798 || optimize_bb_for_size_p (gimple_bb (stmt
)))
801 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
804 /* Compute probability of taking the optimal path. */
806 prob
= GCOV_COMPUTE_SCALE (count
, all
);
810 tree_val
= build_int_cst_wide (get_gcov_type (),
811 (unsigned HOST_WIDE_INT
) val
,
812 val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1);
813 result
= gimple_divmod_fixed_value (stmt
, tree_val
, prob
, count
, all
);
817 fprintf (dump_file
, "Div/mod by constant ");
818 print_generic_expr (dump_file
, value
, TDF_SLIM
);
819 fprintf (dump_file
, "=");
820 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
821 fprintf (dump_file
, " transformation on insn ");
822 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
825 gimple_assign_set_rhs_from_tree (si
, result
);
826 update_stmt (gsi_stmt (*si
));
831 /* Generate code for transformation 2 (with parent gimple assign STMT and
832 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
833 within roundoff error). This generates the result into a temp and returns
834 the temp; it does not replace or alter the original STMT. */
836 gimple_mod_pow2 (gimple stmt
, int prob
, gcov_type count
, gcov_type all
)
838 gimple stmt1
, stmt2
, stmt3
, stmt4
;
840 gimple bb1end
, bb2end
, bb3end
;
841 basic_block bb
, bb2
, bb3
, bb4
;
842 tree optype
, op1
, op2
;
843 edge e12
, e13
, e23
, e24
, e34
;
844 gimple_stmt_iterator gsi
;
847 gcc_assert (is_gimple_assign (stmt
)
848 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
850 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
851 op1
= gimple_assign_rhs1 (stmt
);
852 op2
= gimple_assign_rhs2 (stmt
);
854 bb
= gimple_bb (stmt
);
855 gsi
= gsi_for_stmt (stmt
);
857 result
= create_tmp_reg (optype
, "PROF");
858 tmp2
= make_temp_ssa_name (optype
, NULL
, "PROF");
859 tmp3
= make_temp_ssa_name (optype
, NULL
, "PROF");
860 stmt2
= gimple_build_assign_with_ops (PLUS_EXPR
, tmp2
, op2
,
861 build_int_cst (optype
, -1));
862 stmt3
= gimple_build_assign_with_ops (BIT_AND_EXPR
, tmp3
, tmp2
, op2
);
863 stmt4
= gimple_build_cond (NE_EXPR
, tmp3
, build_int_cst (optype
, 0),
864 NULL_TREE
, NULL_TREE
);
865 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
866 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
867 gsi_insert_before (&gsi
, stmt4
, GSI_SAME_STMT
);
870 /* tmp2 == op2-1 inherited from previous block. */
871 stmt1
= gimple_build_assign_with_ops (BIT_AND_EXPR
, result
, op1
, tmp2
);
872 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
875 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), result
,
877 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
881 /* Edge e23 connects bb2 to bb3, etc. */
882 e12
= split_block (bb
, bb1end
);
885 e23
= split_block (bb2
, bb2end
);
887 bb3
->count
= all
- count
;
888 e34
= split_block (bb3
, bb3end
);
892 e12
->flags
&= ~EDGE_FALLTHRU
;
893 e12
->flags
|= EDGE_FALSE_VALUE
;
894 e12
->probability
= prob
;
897 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
898 e13
->probability
= REG_BR_PROB_BASE
- prob
;
899 e13
->count
= all
- count
;
903 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
904 e24
->probability
= REG_BR_PROB_BASE
;
907 e34
->probability
= REG_BR_PROB_BASE
;
908 e34
->count
= all
- count
;
913 /* Do transform 2) on INSN if applicable. */
915 gimple_mod_pow2_value_transform (gimple_stmt_iterator
*si
)
917 histogram_value histogram
;
919 gcov_type count
, wrong_values
, all
;
920 tree lhs_type
, result
, value
;
924 stmt
= gsi_stmt (*si
);
925 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
928 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
929 if (!INTEGRAL_TYPE_P (lhs_type
))
932 code
= gimple_assign_rhs_code (stmt
);
934 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
937 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_POW2
);
941 value
= histogram
->hvalue
.value
;
942 wrong_values
= histogram
->hvalue
.counters
[0];
943 count
= histogram
->hvalue
.counters
[1];
945 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
947 /* We require that we hit a power of 2 at least half of all evaluations. */
948 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
949 || count
< wrong_values
950 || optimize_bb_for_size_p (gimple_bb (stmt
)))
955 fprintf (dump_file
, "Mod power of 2 transformation on insn ");
956 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
959 /* Compute probability of taking the optimal path. */
960 all
= count
+ wrong_values
;
962 if (check_counter (stmt
, "pow2", &count
, &all
, gimple_bb (stmt
)->count
))
966 prob
= GCOV_COMPUTE_SCALE (count
, all
);
970 result
= gimple_mod_pow2 (stmt
, prob
, count
, all
);
972 gimple_assign_set_rhs_from_tree (si
, result
);
973 update_stmt (gsi_stmt (*si
));
978 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
979 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
980 supported and this is built into this interface. The probabilities of taking
981 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
982 COUNT2/ALL respectively within roundoff error). This generates the
983 result into a temp and returns the temp; it does not replace or alter
984 the original STMT. */
985 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
988 gimple_mod_subtract (gimple stmt
, int prob1
, int prob2
, int ncounts
,
989 gcov_type count1
, gcov_type count2
, gcov_type all
)
991 gimple stmt1
, stmt2
, stmt3
;
993 gimple bb1end
, bb2end
= NULL
, bb3end
;
994 basic_block bb
, bb2
, bb3
, bb4
;
995 tree optype
, op1
, op2
;
996 edge e12
, e23
= 0, e24
, e34
, e14
;
997 gimple_stmt_iterator gsi
;
1000 gcc_assert (is_gimple_assign (stmt
)
1001 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
1003 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1004 op1
= gimple_assign_rhs1 (stmt
);
1005 op2
= gimple_assign_rhs2 (stmt
);
1007 bb
= gimple_bb (stmt
);
1008 gsi
= gsi_for_stmt (stmt
);
1010 result
= create_tmp_reg (optype
, "PROF");
1011 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1012 stmt1
= gimple_build_assign (result
, op1
);
1013 stmt2
= gimple_build_assign (tmp1
, op2
);
1014 stmt3
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1015 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1016 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1017 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
1020 if (ncounts
) /* Assumed to be 0 or 1 */
1022 stmt1
= gimple_build_assign_with_ops (MINUS_EXPR
, result
, result
, tmp1
);
1023 stmt2
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1024 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1025 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1029 /* Fallback case. */
1030 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), result
,
1032 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1036 /* Edge e23 connects bb2 to bb3, etc. */
1037 /* However block 3 is optional; if it is not there, references
1038 to 3 really refer to block 2. */
1039 e12
= split_block (bb
, bb1end
);
1041 bb2
->count
= all
- count1
;
1043 if (ncounts
) /* Assumed to be 0 or 1. */
1045 e23
= split_block (bb2
, bb2end
);
1047 bb3
->count
= all
- count1
- count2
;
1050 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
1054 e12
->flags
&= ~EDGE_FALLTHRU
;
1055 e12
->flags
|= EDGE_FALSE_VALUE
;
1056 e12
->probability
= REG_BR_PROB_BASE
- prob1
;
1057 e12
->count
= all
- count1
;
1059 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
1060 e14
->probability
= prob1
;
1061 e14
->count
= count1
;
1063 if (ncounts
) /* Assumed to be 0 or 1. */
1065 e23
->flags
&= ~EDGE_FALLTHRU
;
1066 e23
->flags
|= EDGE_FALSE_VALUE
;
1067 e23
->count
= all
- count1
- count2
;
1068 e23
->probability
= REG_BR_PROB_BASE
- prob2
;
1070 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
1071 e24
->probability
= prob2
;
1072 e24
->count
= count2
;
1075 e34
->probability
= REG_BR_PROB_BASE
;
1076 e34
->count
= all
- count1
- count2
;
1082 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1085 gimple_mod_subtract_transform (gimple_stmt_iterator
*si
)
1087 histogram_value histogram
;
1088 enum tree_code code
;
1089 gcov_type count
, wrong_values
, all
;
1090 tree lhs_type
, result
;
1091 gcov_type prob1
, prob2
;
1092 unsigned int i
, steps
;
1093 gcov_type count1
, count2
;
1096 stmt
= gsi_stmt (*si
);
1097 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1100 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1101 if (!INTEGRAL_TYPE_P (lhs_type
))
1104 code
= gimple_assign_rhs_code (stmt
);
1106 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1109 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INTERVAL
);
1115 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1116 all
+= histogram
->hvalue
.counters
[i
];
1118 wrong_values
+= histogram
->hvalue
.counters
[i
];
1119 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
1120 steps
= histogram
->hdata
.intvl
.steps
;
1121 all
+= wrong_values
;
1122 count1
= histogram
->hvalue
.counters
[0];
1123 count2
= histogram
->hvalue
.counters
[1];
1125 /* Compute probability of taking the optimal path. */
1126 if (check_counter (stmt
, "interval", &count1
, &all
, gimple_bb (stmt
)->count
))
1128 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1132 if (flag_profile_correction
&& count1
+ count2
> all
)
1133 all
= count1
+ count2
;
1135 gcc_assert (count1
+ count2
<= all
);
1137 /* We require that we use just subtractions in at least 50% of all
1140 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1142 count
+= histogram
->hvalue
.counters
[i
];
1143 if (count
* 2 >= all
)
1147 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1150 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1153 fprintf (dump_file
, "Mod subtract transformation on insn ");
1154 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1157 /* Compute probability of taking the optimal path(s). */
1160 prob1
= GCOV_COMPUTE_SCALE (count1
, all
);
1161 prob2
= GCOV_COMPUTE_SCALE (count2
, all
);
1168 /* In practice, "steps" is always 2. This interface reflects this,
1169 and will need to be changed if "steps" can change. */
1170 result
= gimple_mod_subtract (stmt
, prob1
, prob2
, i
, count1
, count2
, all
);
1172 gimple_assign_set_rhs_from_tree (si
, result
);
1173 update_stmt (gsi_stmt (*si
));
1178 static pointer_map_t
*cgraph_node_map
;
1180 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1181 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1182 that the PROFILE_IDs was already assigned. */
1185 init_node_map (bool local
)
1187 struct cgraph_node
*n
;
1188 cgraph_node_map
= pointer_map_create ();
1190 FOR_EACH_DEFINED_FUNCTION (n
)
1191 if (cgraph_function_with_gimple_body_p (n
)
1192 && !cgraph_only_called_directly_p (n
))
1197 n
->profile_id
= coverage_compute_profile_id (n
);
1198 while ((val
= pointer_map_contains (cgraph_node_map
,
1199 (void *)(size_t)n
->profile_id
))
1203 fprintf (dump_file
, "Local profile-id %i conflict"
1204 " with nodes %s/%i %s/%i\n",
1206 cgraph_node_name (n
),
1208 symtab_node_name (*(symtab_node
*)val
),
1209 (*(symtab_node
*)val
)->symbol
.order
);
1210 n
->profile_id
= (n
->profile_id
+ 1) & 0x7fffffff;
1213 else if (!n
->profile_id
)
1217 "Node %s/%i has no profile-id"
1218 " (profile feedback missing?)\n",
1219 cgraph_node_name (n
),
1223 else if ((val
= pointer_map_contains (cgraph_node_map
,
1224 (void *)(size_t)n
->profile_id
)))
1228 "Node %s/%i has IP profile-id %i conflict. "
1230 cgraph_node_name (n
),
1236 *pointer_map_insert (cgraph_node_map
,
1237 (void *)(size_t)n
->profile_id
) = (void *)n
;
1241 /* Delete the CGRAPH_NODE_MAP. */
1246 pointer_map_destroy (cgraph_node_map
);
1249 /* Return cgraph node for function with pid */
1252 find_func_by_profile_id (int profile_id
)
1254 void **val
= pointer_map_contains (cgraph_node_map
,
1255 (void *)(size_t)profile_id
);
1257 return (struct cgraph_node
*)*val
;
1262 /* Perform sanity check on the indirect call target. Due to race conditions,
1263 false function target may be attributed to an indirect call site. If the
1264 call expression type mismatches with the target function's type, expand_call
1265 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1266 Returns true if TARGET is considered ok for call CALL_STMT. */
1269 check_ic_target (gimple call_stmt
, struct cgraph_node
*target
)
1272 if (gimple_check_call_matching_types (call_stmt
, target
->symbol
.decl
, true))
1275 locus
= gimple_location (call_stmt
);
1276 if (dump_enabled_p ())
1277 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
1278 "Skipping target %s with mismatching types for icall\n",
1279 cgraph_node_name (target
));
1283 /* Do transformation
1285 if (actual_callee_address == address_of_most_common_function/method)
1292 gimple_ic (gimple icall_stmt
, struct cgraph_node
*direct_call
,
1293 int prob
, gcov_type count
, gcov_type all
)
1295 gimple dcall_stmt
, load_stmt
, cond_stmt
;
1296 tree tmp0
, tmp1
, tmp
;
1297 basic_block cond_bb
, dcall_bb
, icall_bb
, join_bb
= NULL
;
1298 tree optype
= build_pointer_type (void_type_node
);
1299 edge e_cd
, e_ci
, e_di
, e_dj
= NULL
, e_ij
;
1300 gimple_stmt_iterator gsi
;
1304 gimple_stmt_iterator psi
;
1306 cond_bb
= gimple_bb (icall_stmt
);
1307 gsi
= gsi_for_stmt (icall_stmt
);
1309 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1310 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1311 tmp
= unshare_expr (gimple_call_fn (icall_stmt
));
1312 load_stmt
= gimple_build_assign (tmp0
, tmp
);
1313 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1315 tmp
= fold_convert (optype
, build_addr (direct_call
->symbol
.decl
,
1316 current_function_decl
));
1317 load_stmt
= gimple_build_assign (tmp1
, tmp
);
1318 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1320 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1321 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1323 gimple_set_vdef (icall_stmt
, NULL_TREE
);
1324 gimple_set_vuse (icall_stmt
, NULL_TREE
);
1325 update_stmt (icall_stmt
);
1326 dcall_stmt
= gimple_copy (icall_stmt
);
1327 gimple_call_set_fndecl (dcall_stmt
, direct_call
->symbol
.decl
);
1328 dflags
= flags_from_decl_or_type (direct_call
->symbol
.decl
);
1329 if ((dflags
& ECF_NORETURN
) != 0)
1330 gimple_call_set_lhs (dcall_stmt
, NULL_TREE
);
1331 gsi_insert_before (&gsi
, dcall_stmt
, GSI_SAME_STMT
);
1334 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1335 e_cd
= split_block (cond_bb
, cond_stmt
);
1336 dcall_bb
= e_cd
->dest
;
1337 dcall_bb
->count
= count
;
1339 e_di
= split_block (dcall_bb
, dcall_stmt
);
1340 icall_bb
= e_di
->dest
;
1341 icall_bb
->count
= all
- count
;
1343 /* Do not disturb existing EH edges from the indirect call. */
1344 if (!stmt_ends_bb_p (icall_stmt
))
1345 e_ij
= split_block (icall_bb
, icall_stmt
);
1348 e_ij
= find_fallthru_edge (icall_bb
->succs
);
1349 /* The indirect call might be noreturn. */
1352 e_ij
->probability
= REG_BR_PROB_BASE
;
1353 e_ij
->count
= all
- count
;
1354 e_ij
= single_pred_edge (split_edge (e_ij
));
1359 join_bb
= e_ij
->dest
;
1360 join_bb
->count
= all
;
1363 e_cd
->flags
= (e_cd
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1364 e_cd
->probability
= prob
;
1365 e_cd
->count
= count
;
1367 e_ci
= make_edge (cond_bb
, icall_bb
, EDGE_FALSE_VALUE
);
1368 e_ci
->probability
= REG_BR_PROB_BASE
- prob
;
1369 e_ci
->count
= all
- count
;
1375 if ((dflags
& ECF_NORETURN
) != 0)
1379 e_dj
= make_edge (dcall_bb
, join_bb
, EDGE_FALLTHRU
);
1380 e_dj
->probability
= REG_BR_PROB_BASE
;
1381 e_dj
->count
= count
;
1383 e_ij
->count
= all
- count
;
1385 e_ij
->probability
= REG_BR_PROB_BASE
;
1388 /* Insert PHI node for the call result if necessary. */
1389 if (gimple_call_lhs (icall_stmt
)
1390 && TREE_CODE (gimple_call_lhs (icall_stmt
)) == SSA_NAME
1391 && (dflags
& ECF_NORETURN
) == 0)
1393 tree result
= gimple_call_lhs (icall_stmt
);
1394 gimple phi
= create_phi_node (result
, join_bb
);
1395 gimple_call_set_lhs (icall_stmt
,
1396 duplicate_ssa_name (result
, icall_stmt
));
1397 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1398 gimple_call_set_lhs (dcall_stmt
,
1399 duplicate_ssa_name (result
, dcall_stmt
));
1400 add_phi_arg (phi
, gimple_call_lhs (dcall_stmt
), e_dj
, UNKNOWN_LOCATION
);
1403 /* Build an EH edge for the direct call if necessary. */
1404 lp_nr
= lookup_stmt_eh_lp (icall_stmt
);
1405 if (lp_nr
> 0 && stmt_could_throw_p (dcall_stmt
))
1407 add_stmt_to_eh_lp (dcall_stmt
, lp_nr
);
1410 FOR_EACH_EDGE (e_eh
, ei
, icall_bb
->succs
)
1411 if (e_eh
->flags
& (EDGE_EH
| EDGE_ABNORMAL
))
1413 e
= make_edge (dcall_bb
, e_eh
->dest
, e_eh
->flags
);
1414 for (psi
= gsi_start_phis (e_eh
->dest
);
1415 !gsi_end_p (psi
); gsi_next (&psi
))
1417 gimple phi
= gsi_stmt (psi
);
1418 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
),
1419 PHI_ARG_DEF_FROM_EDGE (phi
, e_eh
));
1426 For every checked indirect/virtual call determine if most common pid of
1427 function/class method has probability more than 50%. If yes modify code of
1432 gimple_ic_transform (gimple_stmt_iterator
*gsi
)
1434 gimple stmt
= gsi_stmt (*gsi
);
1435 histogram_value histogram
;
1436 gcov_type val
, count
, all
, bb_all
;
1437 struct cgraph_node
*direct_call
;
1439 if (gimple_code (stmt
) != GIMPLE_CALL
)
1442 if (gimple_call_fndecl (stmt
) != NULL_TREE
)
1445 if (gimple_call_internal_p (stmt
))
1448 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INDIR_CALL
);
1452 val
= histogram
->hvalue
.counters
[0];
1453 count
= histogram
->hvalue
.counters
[1];
1454 all
= histogram
->hvalue
.counters
[2];
1456 bb_all
= gimple_bb (stmt
)->count
;
1457 /* The order of CHECK_COUNTER calls is important -
1458 since check_counter can correct the third parameter
1459 and we want to make count <= all <= bb_all. */
1460 if ( check_counter (stmt
, "ic", &all
, &bb_all
, bb_all
)
1461 || check_counter (stmt
, "ic", &count
, &all
, all
))
1463 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1467 if (4 * count
<= 3 * all
)
1470 direct_call
= find_func_by_profile_id ((int)val
);
1472 if (direct_call
== NULL
)
1478 fprintf (dump_file
, "Indirect call -> direct call from other module");
1479 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1480 fprintf (dump_file
, "=> %i (will resolve only with LTO)\n", (int)val
);
1486 if (!check_ic_target (stmt
, direct_call
))
1490 fprintf (dump_file
, "Indirect call -> direct call ");
1491 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1492 fprintf (dump_file
, "=> ");
1493 print_generic_expr (dump_file
, direct_call
->symbol
.decl
, TDF_SLIM
);
1494 fprintf (dump_file
, " transformation skipped because of type mismatch");
1495 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1497 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1503 fprintf (dump_file
, "Indirect call -> direct call ");
1504 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1505 fprintf (dump_file
, "=> ");
1506 print_generic_expr (dump_file
, direct_call
->symbol
.decl
, TDF_SLIM
);
1507 fprintf (dump_file
, " transformation on insn postponned to ipa-profile");
1508 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1509 fprintf (dump_file
, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1510 " hist->all "HOST_WIDEST_INT_PRINT_DEC
"\n", count
, all
);
1516 /* Return true if the stringop CALL with FNDECL shall be profiled.
1517 SIZE_ARG be set to the argument index for the size of the string
1521 interesting_stringop_to_profile_p (tree fndecl
, gimple call
, int *size_arg
)
1523 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
1525 if (fcode
!= BUILT_IN_MEMCPY
&& fcode
!= BUILT_IN_MEMPCPY
1526 && fcode
!= BUILT_IN_MEMSET
&& fcode
!= BUILT_IN_BZERO
)
1531 case BUILT_IN_MEMCPY
:
1532 case BUILT_IN_MEMPCPY
:
1534 return validate_gimple_arglist (call
, POINTER_TYPE
, POINTER_TYPE
,
1535 INTEGER_TYPE
, VOID_TYPE
);
1536 case BUILT_IN_MEMSET
:
1538 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1539 INTEGER_TYPE
, VOID_TYPE
);
1540 case BUILT_IN_BZERO
:
1542 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1549 /* Convert stringop (..., vcall_size)
1551 if (vcall_size == icall_size)
1552 stringop (..., icall_size);
1554 stringop (..., vcall_size);
1555 assuming we'll propagate a true constant into ICALL_SIZE later. */
1558 gimple_stringop_fixed_value (gimple vcall_stmt
, tree icall_size
, int prob
,
1559 gcov_type count
, gcov_type all
)
1561 gimple tmp_stmt
, cond_stmt
, icall_stmt
;
1562 tree tmp0
, tmp1
, vcall_size
, optype
;
1563 basic_block cond_bb
, icall_bb
, vcall_bb
, join_bb
;
1564 edge e_ci
, e_cv
, e_iv
, e_ij
, e_vj
;
1565 gimple_stmt_iterator gsi
;
1569 fndecl
= gimple_call_fndecl (vcall_stmt
);
1570 if (!interesting_stringop_to_profile_p (fndecl
, vcall_stmt
, &size_arg
))
1573 cond_bb
= gimple_bb (vcall_stmt
);
1574 gsi
= gsi_for_stmt (vcall_stmt
);
1576 vcall_size
= gimple_call_arg (vcall_stmt
, size_arg
);
1577 optype
= TREE_TYPE (vcall_size
);
1579 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1580 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1581 tmp_stmt
= gimple_build_assign (tmp0
, fold_convert (optype
, icall_size
));
1582 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1584 tmp_stmt
= gimple_build_assign (tmp1
, vcall_size
);
1585 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1587 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1588 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1590 gimple_set_vdef (vcall_stmt
, NULL
);
1591 gimple_set_vuse (vcall_stmt
, NULL
);
1592 update_stmt (vcall_stmt
);
1593 icall_stmt
= gimple_copy (vcall_stmt
);
1594 gimple_call_set_arg (icall_stmt
, size_arg
, icall_size
);
1595 gsi_insert_before (&gsi
, icall_stmt
, GSI_SAME_STMT
);
1598 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1599 e_ci
= split_block (cond_bb
, cond_stmt
);
1600 icall_bb
= e_ci
->dest
;
1601 icall_bb
->count
= count
;
1603 e_iv
= split_block (icall_bb
, icall_stmt
);
1604 vcall_bb
= e_iv
->dest
;
1605 vcall_bb
->count
= all
- count
;
1607 e_vj
= split_block (vcall_bb
, vcall_stmt
);
1608 join_bb
= e_vj
->dest
;
1609 join_bb
->count
= all
;
1611 e_ci
->flags
= (e_ci
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1612 e_ci
->probability
= prob
;
1613 e_ci
->count
= count
;
1615 e_cv
= make_edge (cond_bb
, vcall_bb
, EDGE_FALSE_VALUE
);
1616 e_cv
->probability
= REG_BR_PROB_BASE
- prob
;
1617 e_cv
->count
= all
- count
;
1621 e_ij
= make_edge (icall_bb
, join_bb
, EDGE_FALLTHRU
);
1622 e_ij
->probability
= REG_BR_PROB_BASE
;
1623 e_ij
->count
= count
;
1625 e_vj
->probability
= REG_BR_PROB_BASE
;
1626 e_vj
->count
= all
- count
;
1628 /* Insert PHI node for the call result if necessary. */
1629 if (gimple_call_lhs (vcall_stmt
)
1630 && TREE_CODE (gimple_call_lhs (vcall_stmt
)) == SSA_NAME
)
1632 tree result
= gimple_call_lhs (vcall_stmt
);
1633 gimple phi
= create_phi_node (result
, join_bb
);
1634 gimple_call_set_lhs (vcall_stmt
,
1635 duplicate_ssa_name (result
, vcall_stmt
));
1636 add_phi_arg (phi
, gimple_call_lhs (vcall_stmt
), e_vj
, UNKNOWN_LOCATION
);
1637 gimple_call_set_lhs (icall_stmt
,
1638 duplicate_ssa_name (result
, icall_stmt
));
1639 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1642 /* Because these are all string op builtins, they're all nothrow. */
1643 gcc_assert (!stmt_could_throw_p (vcall_stmt
));
1644 gcc_assert (!stmt_could_throw_p (icall_stmt
));
1647 /* Find values inside STMT for that we want to measure histograms for
1648 division/modulo optimization. */
1650 gimple_stringops_transform (gimple_stmt_iterator
*gsi
)
1652 gimple stmt
= gsi_stmt (*gsi
);
1655 enum built_in_function fcode
;
1656 histogram_value histogram
;
1657 gcov_type count
, all
, val
;
1659 unsigned int dest_align
, src_align
;
1664 if (gimple_code (stmt
) != GIMPLE_CALL
)
1666 fndecl
= gimple_call_fndecl (stmt
);
1669 fcode
= DECL_FUNCTION_CODE (fndecl
);
1670 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1673 blck_size
= gimple_call_arg (stmt
, size_arg
);
1674 if (TREE_CODE (blck_size
) == INTEGER_CST
)
1677 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_SINGLE_VALUE
);
1680 val
= histogram
->hvalue
.counters
[0];
1681 count
= histogram
->hvalue
.counters
[1];
1682 all
= histogram
->hvalue
.counters
[2];
1683 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1684 /* We require that count is at least half of all; this means
1685 that for the transformation to fire the value must be constant
1686 at least 80% of time. */
1687 if ((6 * count
/ 5) < all
|| optimize_bb_for_size_p (gimple_bb (stmt
)))
1689 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
1692 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1695 dest
= gimple_call_arg (stmt
, 0);
1696 dest_align
= get_pointer_alignment (dest
);
1699 case BUILT_IN_MEMCPY
:
1700 case BUILT_IN_MEMPCPY
:
1701 src
= gimple_call_arg (stmt
, 1);
1702 src_align
= get_pointer_alignment (src
);
1703 if (!can_move_by_pieces (val
, MIN (dest_align
, src_align
)))
1706 case BUILT_IN_MEMSET
:
1707 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1708 gimple_call_arg (stmt
, 1),
1712 case BUILT_IN_BZERO
:
1713 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1721 tree_val
= build_int_cst_wide (get_gcov_type (),
1722 (unsigned HOST_WIDE_INT
) val
,
1723 val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1);
1726 fprintf (dump_file
, "Single value %i stringop transformation on ",
1728 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1730 gimple_stringop_fixed_value (stmt
, tree_val
, prob
, count
, all
);
1736 stringop_block_profile (gimple stmt
, unsigned int *expected_align
,
1737 HOST_WIDE_INT
*expected_size
)
1739 histogram_value histogram
;
1740 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_AVERAGE
);
1742 *expected_size
= -1;
1743 else if (!histogram
->hvalue
.counters
[1])
1745 *expected_size
= -1;
1746 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1751 size
= ((histogram
->hvalue
.counters
[0]
1752 + histogram
->hvalue
.counters
[1] / 2)
1753 / histogram
->hvalue
.counters
[1]);
1754 /* Even if we can hold bigger value in SIZE, INT_MAX
1755 is safe "infinity" for code generation strategies. */
1758 *expected_size
= size
;
1759 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1761 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_IOR
);
1763 *expected_align
= 0;
1764 else if (!histogram
->hvalue
.counters
[0])
1766 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1767 *expected_align
= 0;
1774 count
= histogram
->hvalue
.counters
[0];
1776 while (!(count
& alignment
)
1777 && (alignment
* 2 * BITS_PER_UNIT
))
1779 *expected_align
= alignment
* BITS_PER_UNIT
;
1780 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1785 /* Find values inside STMT for that we want to measure histograms for
1786 division/modulo optimization. */
1788 gimple_divmod_values_to_profile (gimple stmt
, histogram_values
*values
)
1790 tree lhs
, divisor
, op0
, type
;
1791 histogram_value hist
;
1793 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1796 lhs
= gimple_assign_lhs (stmt
);
1797 type
= TREE_TYPE (lhs
);
1798 if (!INTEGRAL_TYPE_P (type
))
1801 switch (gimple_assign_rhs_code (stmt
))
1803 case TRUNC_DIV_EXPR
:
1804 case TRUNC_MOD_EXPR
:
1805 divisor
= gimple_assign_rhs2 (stmt
);
1806 op0
= gimple_assign_rhs1 (stmt
);
1808 values
->reserve (3);
1810 if (TREE_CODE (divisor
) == SSA_NAME
)
1811 /* Check for the case where the divisor is the same value most
1813 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1814 HIST_TYPE_SINGLE_VALUE
,
1817 /* For mod, check whether it is not often a noop (or replaceable by
1818 a few subtractions). */
1819 if (gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
1820 && TYPE_UNSIGNED (type
))
1823 /* Check for a special case where the divisor is power of 2. */
1824 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1828 val
= build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
1829 hist
= gimple_alloc_histogram_value (cfun
, HIST_TYPE_INTERVAL
,
1831 hist
->hdata
.intvl
.int_start
= 0;
1832 hist
->hdata
.intvl
.steps
= 2;
1833 values
->quick_push (hist
);
1842 /* Find calls inside STMT for that we want to measure histograms for
1843 indirect/virtual call optimization. */
1846 gimple_indirect_call_to_profile (gimple stmt
, histogram_values
*values
)
1850 if (gimple_code (stmt
) != GIMPLE_CALL
1851 || gimple_call_internal_p (stmt
)
1852 || gimple_call_fndecl (stmt
) != NULL_TREE
)
1855 callee
= gimple_call_fn (stmt
);
1857 values
->reserve (3);
1859 values
->quick_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_INDIR_CALL
,
1865 /* Find values inside STMT for that we want to measure histograms for
1866 string operations. */
1868 gimple_stringops_values_to_profile (gimple stmt
, histogram_values
*values
)
1875 if (gimple_code (stmt
) != GIMPLE_CALL
)
1877 fndecl
= gimple_call_fndecl (stmt
);
1881 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1884 dest
= gimple_call_arg (stmt
, 0);
1885 blck_size
= gimple_call_arg (stmt
, size_arg
);
1887 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1889 values
->safe_push (gimple_alloc_histogram_value (cfun
,
1890 HIST_TYPE_SINGLE_VALUE
,
1892 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_AVERAGE
,
1895 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1896 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_IOR
,
1900 /* Find values inside STMT for that we want to measure histograms and adds
1901 them to list VALUES. */
1904 gimple_values_to_profile (gimple stmt
, histogram_values
*values
)
1906 gimple_divmod_values_to_profile (stmt
, values
);
1907 gimple_stringops_values_to_profile (stmt
, values
);
1908 gimple_indirect_call_to_profile (stmt
, values
);
1912 gimple_find_values_to_profile (histogram_values
*values
)
1915 gimple_stmt_iterator gsi
;
1917 histogram_value hist
= NULL
;
1921 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1922 gimple_values_to_profile (gsi_stmt (gsi
), values
);
1924 FOR_EACH_VEC_ELT (*values
, i
, hist
)
1928 case HIST_TYPE_INTERVAL
:
1929 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
1932 case HIST_TYPE_POW2
:
1933 hist
->n_counters
= 2;
1936 case HIST_TYPE_SINGLE_VALUE
:
1937 hist
->n_counters
= 3;
1940 case HIST_TYPE_CONST_DELTA
:
1941 hist
->n_counters
= 4;
1944 case HIST_TYPE_INDIR_CALL
:
1945 hist
->n_counters
= 3;
1948 case HIST_TYPE_AVERAGE
:
1949 hist
->n_counters
= 2;
1953 hist
->n_counters
= 1;
1961 fprintf (dump_file
, "Stmt ");
1962 print_gimple_stmt (dump_file
, hist
->hvalue
.stmt
, 0, TDF_SLIM
);
1963 dump_histogram_value (dump_file
, hist
);