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 "diagnostic.h"
37 #include "gimple-pretty-print.h"
44 #include "pointer-set.h"
46 #include "data-streamer.h"
48 /* In this file value profile based optimizations are placed. Currently the
49 following optimizations are implemented (for more detailed descriptions
50 see comments at value_profile_transformations):
52 1) Division/modulo specialization. Provided that we can determine that the
53 operands of the division have some special properties, we may use it to
54 produce more effective code.
56 2) Indirect/virtual call specialization. If we can determine most
57 common function callee in indirect/virtual call. We can use this
58 information to improve code effectiveness (especially info for
61 3) Speculative prefetching. If we are able to determine that the difference
62 between addresses accessed by a memory reference is usually constant, we
63 may add the prefetch instructions.
64 FIXME: This transformation was removed together with RTL based value
68 Value profiling internals
69 ==========================
71 Every value profiling transformation starts with defining what values
72 to profile. There are different histogram types (see HIST_TYPE_* in
73 value-prof.h) and each transformation can request one or more histogram
74 types per GIMPLE statement. The function gimple_find_values_to_profile()
75 collects the values to profile in a vec, and adds the number of counters
76 required for the different histogram types.
78 For a -fprofile-generate run, the statements for which values should be
79 recorded, are instrumented in instrument_values(). The instrumentation
80 is done by helper functions that can be found in tree-profile.c, where
81 new types of histograms can be added if necessary.
83 After a -fprofile-use, the value profiling data is read back in by
84 compute_value_histograms() that translates the collected data to
85 histograms and attaches them to the profiled statements via
86 gimple_add_histogram_value(). Histograms are stored in a hash table
87 that is attached to every intrumented function, see VALUE_HISTOGRAMS
90 The value-profile transformations driver is the function
91 gimple_value_profile_transformations(). It traverses all statements in
92 the to-be-transformed function, and looks for statements with one or
93 more histograms attached to it. If a statement has histograms, the
94 transformation functions are called on the statement.
96 Limitations / FIXME / TODO:
97 * Only one histogram of each type can be associated with a statement.
98 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
99 (This type of histogram was originally used to implement a form of
100 stride profiling based speculative prefetching to improve SPEC2000
101 scores for memory-bound benchmarks, mcf and equake. However, this
102 was an RTL value-profiling transformation, and those have all been
104 * Some value profile transformations are done in builtins.c (?!)
105 * Updating of histograms needs some TLC.
106 * The value profiling code could be used to record analysis results
107 from non-profiling (e.g. VRP).
108 * Adding new profilers should be simplified, starting with a cleanup
109 of what-happens-where andwith making gimple_find_values_to_profile
110 and gimple_value_profile_transformations table-driven, perhaps...
113 static tree
gimple_divmod_fixed_value (gimple
, tree
, int, gcov_type
, gcov_type
);
114 static tree
gimple_mod_pow2 (gimple
, int, gcov_type
, gcov_type
);
115 static tree
gimple_mod_subtract (gimple
, int, int, int, gcov_type
, gcov_type
,
117 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*);
118 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator
*);
119 static bool gimple_mod_subtract_transform (gimple_stmt_iterator
*);
120 static bool gimple_stringops_transform (gimple_stmt_iterator
*);
121 static bool gimple_ic_transform (gimple_stmt_iterator
*);
123 /* Allocate histogram value. */
125 static histogram_value
126 gimple_alloc_histogram_value (struct function
*fun ATTRIBUTE_UNUSED
,
127 enum hist_type type
, gimple stmt
, tree value
)
129 histogram_value hist
= (histogram_value
) xcalloc (1, sizeof (*hist
));
130 hist
->hvalue
.value
= value
;
131 hist
->hvalue
.stmt
= stmt
;
136 /* Hash value for histogram. */
139 histogram_hash (const void *x
)
141 return htab_hash_pointer (((const_histogram_value
)x
)->hvalue
.stmt
);
144 /* Return nonzero if statement for histogram_value X is Y. */
147 histogram_eq (const void *x
, const void *y
)
149 return ((const_histogram_value
) x
)->hvalue
.stmt
== (const_gimple
) y
;
152 /* Set histogram for STMT. */
155 set_histogram_value (struct function
*fun
, gimple stmt
, histogram_value hist
)
158 if (!hist
&& !VALUE_HISTOGRAMS (fun
))
160 if (!VALUE_HISTOGRAMS (fun
))
161 VALUE_HISTOGRAMS (fun
) = htab_create (1, histogram_hash
,
163 loc
= htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
164 htab_hash_pointer (stmt
),
165 hist
? INSERT
: NO_INSERT
);
169 htab_clear_slot (VALUE_HISTOGRAMS (fun
), loc
);
175 /* Get histogram list for STMT. */
178 gimple_histogram_value (struct function
*fun
, gimple stmt
)
180 if (!VALUE_HISTOGRAMS (fun
))
182 return (histogram_value
) htab_find_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
183 htab_hash_pointer (stmt
));
186 /* Add histogram for STMT. */
189 gimple_add_histogram_value (struct function
*fun
, gimple stmt
,
190 histogram_value hist
)
192 hist
->hvalue
.next
= gimple_histogram_value (fun
, stmt
);
193 set_histogram_value (fun
, stmt
, hist
);
197 /* Remove histogram HIST from STMT's histogram list. */
200 gimple_remove_histogram_value (struct function
*fun
, gimple stmt
,
201 histogram_value hist
)
203 histogram_value hist2
= gimple_histogram_value (fun
, stmt
);
206 set_histogram_value (fun
, stmt
, hist
->hvalue
.next
);
210 while (hist2
->hvalue
.next
!= hist
)
211 hist2
= hist2
->hvalue
.next
;
212 hist2
->hvalue
.next
= hist
->hvalue
.next
;
214 free (hist
->hvalue
.counters
);
215 #ifdef ENABLE_CHECKING
216 memset (hist
, 0xab, sizeof (*hist
));
222 /* Lookup histogram of type TYPE in the STMT. */
225 gimple_histogram_value_of_type (struct function
*fun
, gimple stmt
,
228 histogram_value hist
;
229 for (hist
= gimple_histogram_value (fun
, stmt
); hist
;
230 hist
= hist
->hvalue
.next
)
231 if (hist
->type
== type
)
236 /* Dump information about HIST to DUMP_FILE. */
239 dump_histogram_value (FILE *dump_file
, histogram_value hist
)
243 case HIST_TYPE_INTERVAL
:
244 fprintf (dump_file
, "Interval counter range %d -- %d",
245 hist
->hdata
.intvl
.int_start
,
246 (hist
->hdata
.intvl
.int_start
247 + hist
->hdata
.intvl
.steps
- 1));
248 if (hist
->hvalue
.counters
)
251 fprintf (dump_file
, " [");
252 for (i
= 0; i
< hist
->hdata
.intvl
.steps
; i
++)
253 fprintf (dump_file
, " %d:"HOST_WIDEST_INT_PRINT_DEC
,
254 hist
->hdata
.intvl
.int_start
+ i
,
255 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[i
]);
256 fprintf (dump_file
, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC
,
257 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[i
]);
259 fprintf (dump_file
, ".\n");
263 fprintf (dump_file
, "Pow2 counter ");
264 if (hist
->hvalue
.counters
)
266 fprintf (dump_file
, "pow2:"HOST_WIDEST_INT_PRINT_DEC
267 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC
,
268 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
269 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1]);
271 fprintf (dump_file
, ".\n");
274 case HIST_TYPE_SINGLE_VALUE
:
275 fprintf (dump_file
, "Single value ");
276 if (hist
->hvalue
.counters
)
278 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
279 " match:"HOST_WIDEST_INT_PRINT_DEC
280 " wrong:"HOST_WIDEST_INT_PRINT_DEC
,
281 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
282 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
283 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
285 fprintf (dump_file
, ".\n");
288 case HIST_TYPE_AVERAGE
:
289 fprintf (dump_file
, "Average value ");
290 if (hist
->hvalue
.counters
)
292 fprintf (dump_file
, "sum:"HOST_WIDEST_INT_PRINT_DEC
293 " times:"HOST_WIDEST_INT_PRINT_DEC
,
294 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
295 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1]);
297 fprintf (dump_file
, ".\n");
301 fprintf (dump_file
, "IOR value ");
302 if (hist
->hvalue
.counters
)
304 fprintf (dump_file
, "ior:"HOST_WIDEST_INT_PRINT_DEC
,
305 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0]);
307 fprintf (dump_file
, ".\n");
310 case HIST_TYPE_CONST_DELTA
:
311 fprintf (dump_file
, "Constant delta ");
312 if (hist
->hvalue
.counters
)
314 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
315 " match:"HOST_WIDEST_INT_PRINT_DEC
316 " wrong:"HOST_WIDEST_INT_PRINT_DEC
,
317 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
318 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
319 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
321 fprintf (dump_file
, ".\n");
323 case HIST_TYPE_INDIR_CALL
:
324 fprintf (dump_file
, "Indirect call ");
325 if (hist
->hvalue
.counters
)
327 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
328 " match:"HOST_WIDEST_INT_PRINT_DEC
329 " all:"HOST_WIDEST_INT_PRINT_DEC
,
330 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
331 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
332 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
334 fprintf (dump_file
, ".\n");
341 /* Dump information about HIST to DUMP_FILE. */
344 stream_out_histogram_value (struct output_block
*ob
, histogram_value hist
)
349 bp
= bitpack_create (ob
->main_stream
);
350 bp_pack_enum (&bp
, hist_type
, HIST_TYPE_MAX
, hist
->type
);
351 bp_pack_value (&bp
, hist
->hvalue
.next
!= NULL
, 1);
352 streamer_write_bitpack (&bp
);
355 case HIST_TYPE_INTERVAL
:
356 streamer_write_hwi (ob
, hist
->hdata
.intvl
.int_start
);
357 streamer_write_uhwi (ob
, hist
->hdata
.intvl
.steps
);
362 for (i
= 0; i
< hist
->n_counters
; i
++)
363 streamer_write_gcov_count (ob
, hist
->hvalue
.counters
[i
]);
364 if (hist
->hvalue
.next
)
365 stream_out_histogram_value (ob
, hist
->hvalue
.next
);
367 /* Dump information about HIST to DUMP_FILE. */
370 stream_in_histogram_value (struct lto_input_block
*ib
, gimple stmt
)
373 unsigned int ncounters
= 0;
376 histogram_value new_val
;
378 histogram_value
*next_p
= NULL
;
382 bp
= streamer_read_bitpack (ib
);
383 type
= bp_unpack_enum (&bp
, hist_type
, HIST_TYPE_MAX
);
384 next
= bp_unpack_value (&bp
, 1);
385 new_val
= gimple_alloc_histogram_value (cfun
, type
, stmt
, NULL
);
388 case HIST_TYPE_INTERVAL
:
389 new_val
->hdata
.intvl
.int_start
= streamer_read_hwi (ib
);
390 new_val
->hdata
.intvl
.steps
= streamer_read_uhwi (ib
);
391 ncounters
= new_val
->hdata
.intvl
.steps
+ 2;
395 case HIST_TYPE_AVERAGE
:
399 case HIST_TYPE_SINGLE_VALUE
:
400 case HIST_TYPE_INDIR_CALL
:
404 case HIST_TYPE_CONST_DELTA
:
414 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * ncounters
);
415 new_val
->n_counters
= ncounters
;
416 for (i
= 0; i
< ncounters
; i
++)
417 new_val
->hvalue
.counters
[i
] = streamer_read_gcov_count (ib
);
419 gimple_add_histogram_value (cfun
, stmt
, new_val
);
422 next_p
= &new_val
->hvalue
.next
;
427 /* Dump all histograms attached to STMT to DUMP_FILE. */
430 dump_histograms_for_stmt (struct function
*fun
, FILE *dump_file
, gimple stmt
)
432 histogram_value hist
;
433 for (hist
= gimple_histogram_value (fun
, stmt
); hist
; hist
= hist
->hvalue
.next
)
434 dump_histogram_value (dump_file
, hist
);
437 /* Remove all histograms associated with STMT. */
440 gimple_remove_stmt_histograms (struct function
*fun
, gimple stmt
)
443 while ((val
= gimple_histogram_value (fun
, stmt
)) != NULL
)
444 gimple_remove_histogram_value (fun
, stmt
, val
);
447 /* Duplicate all histograms associates with OSTMT to STMT. */
450 gimple_duplicate_stmt_histograms (struct function
*fun
, gimple stmt
,
451 struct function
*ofun
, gimple ostmt
)
454 for (val
= gimple_histogram_value (ofun
, ostmt
); val
!= NULL
; val
= val
->hvalue
.next
)
456 histogram_value new_val
= gimple_alloc_histogram_value (fun
, val
->type
, NULL
, NULL
);
457 memcpy (new_val
, val
, sizeof (*val
));
458 new_val
->hvalue
.stmt
= stmt
;
459 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
460 memcpy (new_val
->hvalue
.counters
, val
->hvalue
.counters
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
461 gimple_add_histogram_value (fun
, stmt
, new_val
);
466 /* Move all histograms associated with OSTMT to STMT. */
469 gimple_move_stmt_histograms (struct function
*fun
, gimple stmt
, gimple ostmt
)
471 histogram_value val
= gimple_histogram_value (fun
, ostmt
);
474 /* The following three statements can't be reordered,
475 because histogram hashtab relies on stmt field value
476 for finding the exact slot. */
477 set_histogram_value (fun
, ostmt
, NULL
);
478 for (; val
!= NULL
; val
= val
->hvalue
.next
)
479 val
->hvalue
.stmt
= stmt
;
480 set_histogram_value (fun
, stmt
, val
);
484 static bool error_found
= false;
486 /* Helper function for verify_histograms. For each histogram reachable via htab
487 walk verify that it was reached via statement walk. */
490 visit_hist (void **slot
, void *data
)
492 struct pointer_set_t
*visited
= (struct pointer_set_t
*) data
;
493 histogram_value hist
= *(histogram_value
*) slot
;
494 if (!pointer_set_contains (visited
, hist
))
496 error ("dead histogram");
497 dump_histogram_value (stderr
, hist
);
498 debug_gimple_stmt (hist
->hvalue
.stmt
);
505 /* Verify sanity of the histograms. */
508 verify_histograms (void)
511 gimple_stmt_iterator gsi
;
512 histogram_value hist
;
513 struct pointer_set_t
*visited_hists
;
516 visited_hists
= pointer_set_create ();
518 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
520 gimple stmt
= gsi_stmt (gsi
);
522 for (hist
= gimple_histogram_value (cfun
, stmt
); hist
;
523 hist
= hist
->hvalue
.next
)
525 if (hist
->hvalue
.stmt
!= stmt
)
527 error ("Histogram value statement does not correspond to "
528 "the statement it is associated with");
529 debug_gimple_stmt (stmt
);
530 dump_histogram_value (stderr
, hist
);
533 pointer_set_insert (visited_hists
, hist
);
536 if (VALUE_HISTOGRAMS (cfun
))
537 htab_traverse (VALUE_HISTOGRAMS (cfun
), visit_hist
, visited_hists
);
538 pointer_set_destroy (visited_hists
);
540 internal_error ("verify_histograms failed");
543 /* Helper function for verify_histograms. For each histogram reachable via htab
544 walk verify that it was reached via statement walk. */
547 free_hist (void **slot
, void *data ATTRIBUTE_UNUSED
)
549 histogram_value hist
= *(histogram_value
*) slot
;
550 free (hist
->hvalue
.counters
);
551 #ifdef ENABLE_CHECKING
552 memset (hist
, 0xab, sizeof (*hist
));
559 free_histograms (void)
561 if (VALUE_HISTOGRAMS (cfun
))
563 htab_traverse (VALUE_HISTOGRAMS (cfun
), free_hist
, NULL
);
564 htab_delete (VALUE_HISTOGRAMS (cfun
));
565 VALUE_HISTOGRAMS (cfun
) = NULL
;
570 /* The overall number of invocations of the counter should match
571 execution count of basic block. Report it as error rather than
572 internal error as it might mean that user has misused the profile
576 check_counter (gimple stmt
, const char * name
,
577 gcov_type
*count
, gcov_type
*all
, gcov_type bb_count
)
579 if (*all
!= bb_count
|| *count
> *all
)
582 locus
= (stmt
!= NULL
)
583 ? gimple_location (stmt
)
584 : DECL_SOURCE_LOCATION (current_function_decl
);
585 if (flag_profile_correction
)
587 if (dump_enabled_p ())
588 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
589 "correcting inconsistent value profile: %s "
590 "profiler overall count (%d) does not match BB "
591 "count (%d)\n", name
, (int)*all
, (int)bb_count
);
599 error_at (locus
, "corrupted value profile: %s "
600 "profile counter (%d out of %d) inconsistent with "
601 "basic-block count (%d)",
614 /* GIMPLE based transformations. */
617 gimple_value_profile_transformations (void)
620 gimple_stmt_iterator gsi
;
621 bool changed
= false;
625 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
627 gimple stmt
= gsi_stmt (gsi
);
628 histogram_value th
= gimple_histogram_value (cfun
, stmt
);
634 fprintf (dump_file
, "Trying transformations on stmt ");
635 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
636 dump_histograms_for_stmt (cfun
, dump_file
, stmt
);
639 /* Transformations: */
640 /* The order of things in this conditional controls which
641 transformation is used when more than one is applicable. */
642 /* It is expected that any code added by the transformations
643 will be added before the current statement, and that the
644 current statement remain valid (although possibly
645 modified) upon return. */
646 if (gimple_mod_subtract_transform (&gsi
)
647 || gimple_divmod_fixed_value_transform (&gsi
)
648 || gimple_mod_pow2_value_transform (&gsi
)
649 || gimple_stringops_transform (&gsi
)
650 || gimple_ic_transform (&gsi
))
652 stmt
= gsi_stmt (gsi
);
654 /* Original statement may no longer be in the same block. */
655 if (bb
!= gimple_bb (stmt
))
657 bb
= gimple_bb (stmt
);
658 gsi
= gsi_for_stmt (stmt
);
673 /* Generate code for transformation 1 (with parent gimple assignment
674 STMT and probability of taking the optimal path PROB, which is
675 equivalent to COUNT/ALL within roundoff error). This generates the
676 result into a temp and returns the temp; it does not replace or
677 alter the original STMT. */
680 gimple_divmod_fixed_value (gimple stmt
, tree value
, int prob
, gcov_type count
,
683 gimple stmt1
, stmt2
, stmt3
;
684 tree tmp0
, tmp1
, tmp2
;
685 gimple bb1end
, bb2end
, bb3end
;
686 basic_block bb
, bb2
, bb3
, bb4
;
687 tree optype
, op1
, op2
;
688 edge e12
, e13
, e23
, e24
, e34
;
689 gimple_stmt_iterator gsi
;
691 gcc_assert (is_gimple_assign (stmt
)
692 && (gimple_assign_rhs_code (stmt
) == TRUNC_DIV_EXPR
693 || gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
));
695 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
696 op1
= gimple_assign_rhs1 (stmt
);
697 op2
= gimple_assign_rhs2 (stmt
);
699 bb
= gimple_bb (stmt
);
700 gsi
= gsi_for_stmt (stmt
);
702 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
703 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
704 stmt1
= gimple_build_assign (tmp0
, fold_convert (optype
, value
));
705 stmt2
= gimple_build_assign (tmp1
, op2
);
706 stmt3
= gimple_build_cond (NE_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
707 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
708 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
709 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
712 tmp2
= create_tmp_reg (optype
, "PROF");
713 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), tmp2
,
715 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
718 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), tmp2
,
720 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
724 /* Edge e23 connects bb2 to bb3, etc. */
725 e12
= split_block (bb
, bb1end
);
728 e23
= split_block (bb2
, bb2end
);
730 bb3
->count
= all
- count
;
731 e34
= split_block (bb3
, bb3end
);
735 e12
->flags
&= ~EDGE_FALLTHRU
;
736 e12
->flags
|= EDGE_FALSE_VALUE
;
737 e12
->probability
= prob
;
740 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
741 e13
->probability
= REG_BR_PROB_BASE
- prob
;
742 e13
->count
= all
- count
;
746 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
747 e24
->probability
= REG_BR_PROB_BASE
;
750 e34
->probability
= REG_BR_PROB_BASE
;
751 e34
->count
= all
- count
;
757 /* Do transform 1) on INSN if applicable. */
760 gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*si
)
762 histogram_value histogram
;
764 gcov_type val
, count
, all
;
765 tree result
, value
, tree_val
;
769 stmt
= gsi_stmt (*si
);
770 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
773 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
))))
776 code
= gimple_assign_rhs_code (stmt
);
778 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
781 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
782 HIST_TYPE_SINGLE_VALUE
);
786 value
= histogram
->hvalue
.value
;
787 val
= histogram
->hvalue
.counters
[0];
788 count
= histogram
->hvalue
.counters
[1];
789 all
= histogram
->hvalue
.counters
[2];
790 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
792 /* We require that count is at least half of all; this means
793 that for the transformation to fire the value must be constant
794 at least 50% of time (and 75% gives the guarantee of usage). */
795 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
797 || optimize_bb_for_size_p (gimple_bb (stmt
)))
800 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
803 /* Compute probability of taking the optimal path. */
805 prob
= GCOV_COMPUTE_SCALE (count
, all
);
809 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
810 tree_val
= build_int_cst (get_gcov_type (), val
);
814 a
[0] = (unsigned HOST_WIDE_INT
) val
;
815 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
817 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
818 TYPE_PRECISION (get_gcov_type ()), false));
820 result
= gimple_divmod_fixed_value (stmt
, tree_val
, prob
, count
, all
);
824 fprintf (dump_file
, "Div/mod by constant ");
825 print_generic_expr (dump_file
, value
, TDF_SLIM
);
826 fprintf (dump_file
, "=");
827 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
828 fprintf (dump_file
, " transformation on insn ");
829 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
832 gimple_assign_set_rhs_from_tree (si
, result
);
833 update_stmt (gsi_stmt (*si
));
838 /* Generate code for transformation 2 (with parent gimple assign STMT and
839 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
840 within roundoff error). This generates the result into a temp and returns
841 the temp; it does not replace or alter the original STMT. */
843 gimple_mod_pow2 (gimple stmt
, int prob
, gcov_type count
, gcov_type all
)
845 gimple stmt1
, stmt2
, stmt3
, stmt4
;
847 gimple bb1end
, bb2end
, bb3end
;
848 basic_block bb
, bb2
, bb3
, bb4
;
849 tree optype
, op1
, op2
;
850 edge e12
, e13
, e23
, e24
, e34
;
851 gimple_stmt_iterator gsi
;
854 gcc_assert (is_gimple_assign (stmt
)
855 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
857 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
858 op1
= gimple_assign_rhs1 (stmt
);
859 op2
= gimple_assign_rhs2 (stmt
);
861 bb
= gimple_bb (stmt
);
862 gsi
= gsi_for_stmt (stmt
);
864 result
= create_tmp_reg (optype
, "PROF");
865 tmp2
= make_temp_ssa_name (optype
, NULL
, "PROF");
866 tmp3
= make_temp_ssa_name (optype
, NULL
, "PROF");
867 stmt2
= gimple_build_assign_with_ops (PLUS_EXPR
, tmp2
, op2
,
868 build_int_cst (optype
, -1));
869 stmt3
= gimple_build_assign_with_ops (BIT_AND_EXPR
, tmp3
, tmp2
, op2
);
870 stmt4
= gimple_build_cond (NE_EXPR
, tmp3
, build_int_cst (optype
, 0),
871 NULL_TREE
, NULL_TREE
);
872 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
873 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
874 gsi_insert_before (&gsi
, stmt4
, GSI_SAME_STMT
);
877 /* tmp2 == op2-1 inherited from previous block. */
878 stmt1
= gimple_build_assign_with_ops (BIT_AND_EXPR
, result
, op1
, tmp2
);
879 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
882 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), result
,
884 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
888 /* Edge e23 connects bb2 to bb3, etc. */
889 e12
= split_block (bb
, bb1end
);
892 e23
= split_block (bb2
, bb2end
);
894 bb3
->count
= all
- count
;
895 e34
= split_block (bb3
, bb3end
);
899 e12
->flags
&= ~EDGE_FALLTHRU
;
900 e12
->flags
|= EDGE_FALSE_VALUE
;
901 e12
->probability
= prob
;
904 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
905 e13
->probability
= REG_BR_PROB_BASE
- prob
;
906 e13
->count
= all
- count
;
910 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
911 e24
->probability
= REG_BR_PROB_BASE
;
914 e34
->probability
= REG_BR_PROB_BASE
;
915 e34
->count
= all
- count
;
920 /* Do transform 2) on INSN if applicable. */
922 gimple_mod_pow2_value_transform (gimple_stmt_iterator
*si
)
924 histogram_value histogram
;
926 gcov_type count
, wrong_values
, all
;
927 tree lhs_type
, result
, value
;
931 stmt
= gsi_stmt (*si
);
932 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
935 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
936 if (!INTEGRAL_TYPE_P (lhs_type
))
939 code
= gimple_assign_rhs_code (stmt
);
941 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
944 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_POW2
);
948 value
= histogram
->hvalue
.value
;
949 wrong_values
= histogram
->hvalue
.counters
[0];
950 count
= histogram
->hvalue
.counters
[1];
952 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
954 /* We require that we hit a power of 2 at least half of all evaluations. */
955 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
956 || count
< wrong_values
957 || optimize_bb_for_size_p (gimple_bb (stmt
)))
962 fprintf (dump_file
, "Mod power of 2 transformation on insn ");
963 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
966 /* Compute probability of taking the optimal path. */
967 all
= count
+ wrong_values
;
969 if (check_counter (stmt
, "pow2", &count
, &all
, gimple_bb (stmt
)->count
))
973 prob
= GCOV_COMPUTE_SCALE (count
, all
);
977 result
= gimple_mod_pow2 (stmt
, prob
, count
, all
);
979 gimple_assign_set_rhs_from_tree (si
, result
);
980 update_stmt (gsi_stmt (*si
));
985 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
986 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
987 supported and this is built into this interface. The probabilities of taking
988 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
989 COUNT2/ALL respectively within roundoff error). This generates the
990 result into a temp and returns the temp; it does not replace or alter
991 the original STMT. */
992 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
995 gimple_mod_subtract (gimple stmt
, int prob1
, int prob2
, int ncounts
,
996 gcov_type count1
, gcov_type count2
, gcov_type all
)
998 gimple stmt1
, stmt2
, stmt3
;
1000 gimple bb1end
, bb2end
= NULL
, bb3end
;
1001 basic_block bb
, bb2
, bb3
, bb4
;
1002 tree optype
, op1
, op2
;
1003 edge e12
, e23
= 0, e24
, e34
, e14
;
1004 gimple_stmt_iterator gsi
;
1007 gcc_assert (is_gimple_assign (stmt
)
1008 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
1010 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1011 op1
= gimple_assign_rhs1 (stmt
);
1012 op2
= gimple_assign_rhs2 (stmt
);
1014 bb
= gimple_bb (stmt
);
1015 gsi
= gsi_for_stmt (stmt
);
1017 result
= create_tmp_reg (optype
, "PROF");
1018 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1019 stmt1
= gimple_build_assign (result
, op1
);
1020 stmt2
= gimple_build_assign (tmp1
, op2
);
1021 stmt3
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1022 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1023 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1024 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
1027 if (ncounts
) /* Assumed to be 0 or 1 */
1029 stmt1
= gimple_build_assign_with_ops (MINUS_EXPR
, result
, result
, tmp1
);
1030 stmt2
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1031 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1032 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1036 /* Fallback case. */
1037 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), result
,
1039 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1043 /* Edge e23 connects bb2 to bb3, etc. */
1044 /* However block 3 is optional; if it is not there, references
1045 to 3 really refer to block 2. */
1046 e12
= split_block (bb
, bb1end
);
1048 bb2
->count
= all
- count1
;
1050 if (ncounts
) /* Assumed to be 0 or 1. */
1052 e23
= split_block (bb2
, bb2end
);
1054 bb3
->count
= all
- count1
- count2
;
1057 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
1061 e12
->flags
&= ~EDGE_FALLTHRU
;
1062 e12
->flags
|= EDGE_FALSE_VALUE
;
1063 e12
->probability
= REG_BR_PROB_BASE
- prob1
;
1064 e12
->count
= all
- count1
;
1066 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
1067 e14
->probability
= prob1
;
1068 e14
->count
= count1
;
1070 if (ncounts
) /* Assumed to be 0 or 1. */
1072 e23
->flags
&= ~EDGE_FALLTHRU
;
1073 e23
->flags
|= EDGE_FALSE_VALUE
;
1074 e23
->count
= all
- count1
- count2
;
1075 e23
->probability
= REG_BR_PROB_BASE
- prob2
;
1077 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
1078 e24
->probability
= prob2
;
1079 e24
->count
= count2
;
1082 e34
->probability
= REG_BR_PROB_BASE
;
1083 e34
->count
= all
- count1
- count2
;
1089 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1092 gimple_mod_subtract_transform (gimple_stmt_iterator
*si
)
1094 histogram_value histogram
;
1095 enum tree_code code
;
1096 gcov_type count
, wrong_values
, all
;
1097 tree lhs_type
, result
;
1098 gcov_type prob1
, prob2
;
1099 unsigned int i
, steps
;
1100 gcov_type count1
, count2
;
1103 stmt
= gsi_stmt (*si
);
1104 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1107 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1108 if (!INTEGRAL_TYPE_P (lhs_type
))
1111 code
= gimple_assign_rhs_code (stmt
);
1113 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1116 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INTERVAL
);
1122 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1123 all
+= histogram
->hvalue
.counters
[i
];
1125 wrong_values
+= histogram
->hvalue
.counters
[i
];
1126 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
1127 steps
= histogram
->hdata
.intvl
.steps
;
1128 all
+= wrong_values
;
1129 count1
= histogram
->hvalue
.counters
[0];
1130 count2
= histogram
->hvalue
.counters
[1];
1132 /* Compute probability of taking the optimal path. */
1133 if (check_counter (stmt
, "interval", &count1
, &all
, gimple_bb (stmt
)->count
))
1135 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1139 if (flag_profile_correction
&& count1
+ count2
> all
)
1140 all
= count1
+ count2
;
1142 gcc_assert (count1
+ count2
<= all
);
1144 /* We require that we use just subtractions in at least 50% of all
1147 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1149 count
+= histogram
->hvalue
.counters
[i
];
1150 if (count
* 2 >= all
)
1154 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1157 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1160 fprintf (dump_file
, "Mod subtract transformation on insn ");
1161 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1164 /* Compute probability of taking the optimal path(s). */
1167 prob1
= GCOV_COMPUTE_SCALE (count1
, all
);
1168 prob2
= GCOV_COMPUTE_SCALE (count2
, all
);
1175 /* In practice, "steps" is always 2. This interface reflects this,
1176 and will need to be changed if "steps" can change. */
1177 result
= gimple_mod_subtract (stmt
, prob1
, prob2
, i
, count1
, count2
, all
);
1179 gimple_assign_set_rhs_from_tree (si
, result
);
1180 update_stmt (gsi_stmt (*si
));
1185 static pointer_map_t
*cgraph_node_map
;
1187 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1188 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1189 that the PROFILE_IDs was already assigned. */
1192 init_node_map (bool local
)
1194 struct cgraph_node
*n
;
1195 cgraph_node_map
= pointer_map_create ();
1197 FOR_EACH_DEFINED_FUNCTION (n
)
1198 if (cgraph_function_with_gimple_body_p (n
)
1199 && !cgraph_only_called_directly_p (n
))
1204 n
->profile_id
= coverage_compute_profile_id (n
);
1205 while ((val
= pointer_map_contains (cgraph_node_map
,
1206 (void *)(size_t)n
->profile_id
))
1210 fprintf (dump_file
, "Local profile-id %i conflict"
1211 " with nodes %s/%i %s/%i\n",
1213 cgraph_node_name (n
),
1215 symtab_node_name (*(symtab_node
*)val
),
1216 (*(symtab_node
*)val
)->symbol
.order
);
1217 n
->profile_id
= (n
->profile_id
+ 1) & 0x7fffffff;
1220 else if (!n
->profile_id
)
1224 "Node %s/%i has no profile-id"
1225 " (profile feedback missing?)\n",
1226 cgraph_node_name (n
),
1230 else if ((val
= pointer_map_contains (cgraph_node_map
,
1231 (void *)(size_t)n
->profile_id
)))
1235 "Node %s/%i has IP profile-id %i conflict. "
1237 cgraph_node_name (n
),
1243 *pointer_map_insert (cgraph_node_map
,
1244 (void *)(size_t)n
->profile_id
) = (void *)n
;
1248 /* Delete the CGRAPH_NODE_MAP. */
1253 pointer_map_destroy (cgraph_node_map
);
1256 /* Return cgraph node for function with pid */
1259 find_func_by_profile_id (int profile_id
)
1261 void **val
= pointer_map_contains (cgraph_node_map
,
1262 (void *)(size_t)profile_id
);
1264 return (struct cgraph_node
*)*val
;
1269 /* Perform sanity check on the indirect call target. Due to race conditions,
1270 false function target may be attributed to an indirect call site. If the
1271 call expression type mismatches with the target function's type, expand_call
1272 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1273 Returns true if TARGET is considered ok for call CALL_STMT. */
1276 check_ic_target (gimple call_stmt
, struct cgraph_node
*target
)
1279 if (gimple_check_call_matching_types (call_stmt
, target
->symbol
.decl
, true))
1282 locus
= gimple_location (call_stmt
);
1283 if (dump_enabled_p ())
1284 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
1285 "Skipping target %s with mismatching types for icall\n",
1286 cgraph_node_name (target
));
1290 /* Do transformation
1292 if (actual_callee_address == address_of_most_common_function/method)
1299 gimple_ic (gimple icall_stmt
, struct cgraph_node
*direct_call
,
1300 int prob
, gcov_type count
, gcov_type all
)
1302 gimple dcall_stmt
, load_stmt
, cond_stmt
;
1303 tree tmp0
, tmp1
, tmp
;
1304 basic_block cond_bb
, dcall_bb
, icall_bb
, join_bb
= NULL
;
1305 tree optype
= build_pointer_type (void_type_node
);
1306 edge e_cd
, e_ci
, e_di
, e_dj
= NULL
, e_ij
;
1307 gimple_stmt_iterator gsi
;
1311 gimple_stmt_iterator psi
;
1313 cond_bb
= gimple_bb (icall_stmt
);
1314 gsi
= gsi_for_stmt (icall_stmt
);
1316 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1317 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1318 tmp
= unshare_expr (gimple_call_fn (icall_stmt
));
1319 load_stmt
= gimple_build_assign (tmp0
, tmp
);
1320 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1322 tmp
= fold_convert (optype
, build_addr (direct_call
->symbol
.decl
,
1323 current_function_decl
));
1324 load_stmt
= gimple_build_assign (tmp1
, tmp
);
1325 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1327 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1328 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1330 gimple_set_vdef (icall_stmt
, NULL_TREE
);
1331 gimple_set_vuse (icall_stmt
, NULL_TREE
);
1332 update_stmt (icall_stmt
);
1333 dcall_stmt
= gimple_copy (icall_stmt
);
1334 gimple_call_set_fndecl (dcall_stmt
, direct_call
->symbol
.decl
);
1335 dflags
= flags_from_decl_or_type (direct_call
->symbol
.decl
);
1336 if ((dflags
& ECF_NORETURN
) != 0)
1337 gimple_call_set_lhs (dcall_stmt
, NULL_TREE
);
1338 gsi_insert_before (&gsi
, dcall_stmt
, GSI_SAME_STMT
);
1341 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1342 e_cd
= split_block (cond_bb
, cond_stmt
);
1343 dcall_bb
= e_cd
->dest
;
1344 dcall_bb
->count
= count
;
1346 e_di
= split_block (dcall_bb
, dcall_stmt
);
1347 icall_bb
= e_di
->dest
;
1348 icall_bb
->count
= all
- count
;
1350 /* Do not disturb existing EH edges from the indirect call. */
1351 if (!stmt_ends_bb_p (icall_stmt
))
1352 e_ij
= split_block (icall_bb
, icall_stmt
);
1355 e_ij
= find_fallthru_edge (icall_bb
->succs
);
1356 /* The indirect call might be noreturn. */
1359 e_ij
->probability
= REG_BR_PROB_BASE
;
1360 e_ij
->count
= all
- count
;
1361 e_ij
= single_pred_edge (split_edge (e_ij
));
1366 join_bb
= e_ij
->dest
;
1367 join_bb
->count
= all
;
1370 e_cd
->flags
= (e_cd
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1371 e_cd
->probability
= prob
;
1372 e_cd
->count
= count
;
1374 e_ci
= make_edge (cond_bb
, icall_bb
, EDGE_FALSE_VALUE
);
1375 e_ci
->probability
= REG_BR_PROB_BASE
- prob
;
1376 e_ci
->count
= all
- count
;
1382 if ((dflags
& ECF_NORETURN
) != 0)
1386 e_dj
= make_edge (dcall_bb
, join_bb
, EDGE_FALLTHRU
);
1387 e_dj
->probability
= REG_BR_PROB_BASE
;
1388 e_dj
->count
= count
;
1390 e_ij
->count
= all
- count
;
1392 e_ij
->probability
= REG_BR_PROB_BASE
;
1395 /* Insert PHI node for the call result if necessary. */
1396 if (gimple_call_lhs (icall_stmt
)
1397 && TREE_CODE (gimple_call_lhs (icall_stmt
)) == SSA_NAME
1398 && (dflags
& ECF_NORETURN
) == 0)
1400 tree result
= gimple_call_lhs (icall_stmt
);
1401 gimple phi
= create_phi_node (result
, join_bb
);
1402 gimple_call_set_lhs (icall_stmt
,
1403 duplicate_ssa_name (result
, icall_stmt
));
1404 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1405 gimple_call_set_lhs (dcall_stmt
,
1406 duplicate_ssa_name (result
, dcall_stmt
));
1407 add_phi_arg (phi
, gimple_call_lhs (dcall_stmt
), e_dj
, UNKNOWN_LOCATION
);
1410 /* Build an EH edge for the direct call if necessary. */
1411 lp_nr
= lookup_stmt_eh_lp (icall_stmt
);
1412 if (lp_nr
> 0 && stmt_could_throw_p (dcall_stmt
))
1414 add_stmt_to_eh_lp (dcall_stmt
, lp_nr
);
1417 FOR_EACH_EDGE (e_eh
, ei
, icall_bb
->succs
)
1418 if (e_eh
->flags
& (EDGE_EH
| EDGE_ABNORMAL
))
1420 e
= make_edge (dcall_bb
, e_eh
->dest
, e_eh
->flags
);
1421 for (psi
= gsi_start_phis (e_eh
->dest
);
1422 !gsi_end_p (psi
); gsi_next (&psi
))
1424 gimple phi
= gsi_stmt (psi
);
1425 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
),
1426 PHI_ARG_DEF_FROM_EDGE (phi
, e_eh
));
1433 For every checked indirect/virtual call determine if most common pid of
1434 function/class method has probability more than 50%. If yes modify code of
1439 gimple_ic_transform (gimple_stmt_iterator
*gsi
)
1441 gimple stmt
= gsi_stmt (*gsi
);
1442 histogram_value histogram
;
1443 gcov_type val
, count
, all
, bb_all
;
1444 struct cgraph_node
*direct_call
;
1446 if (gimple_code (stmt
) != GIMPLE_CALL
)
1449 if (gimple_call_fndecl (stmt
) != NULL_TREE
)
1452 if (gimple_call_internal_p (stmt
))
1455 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INDIR_CALL
);
1459 val
= histogram
->hvalue
.counters
[0];
1460 count
= histogram
->hvalue
.counters
[1];
1461 all
= histogram
->hvalue
.counters
[2];
1463 bb_all
= gimple_bb (stmt
)->count
;
1464 /* The order of CHECK_COUNTER calls is important -
1465 since check_counter can correct the third parameter
1466 and we want to make count <= all <= bb_all. */
1467 if ( check_counter (stmt
, "ic", &all
, &bb_all
, bb_all
)
1468 || check_counter (stmt
, "ic", &count
, &all
, all
))
1470 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1474 if (4 * count
<= 3 * all
)
1477 direct_call
= find_func_by_profile_id ((int)val
);
1479 if (direct_call
== NULL
)
1485 fprintf (dump_file
, "Indirect call -> direct call from other module");
1486 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1487 fprintf (dump_file
, "=> %i (will resolve only with LTO)\n", (int)val
);
1493 if (!check_ic_target (stmt
, direct_call
))
1497 fprintf (dump_file
, "Indirect call -> direct call ");
1498 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1499 fprintf (dump_file
, "=> ");
1500 print_generic_expr (dump_file
, direct_call
->symbol
.decl
, TDF_SLIM
);
1501 fprintf (dump_file
, " transformation skipped because of type mismatch");
1502 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1504 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1510 fprintf (dump_file
, "Indirect call -> direct call ");
1511 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1512 fprintf (dump_file
, "=> ");
1513 print_generic_expr (dump_file
, direct_call
->symbol
.decl
, TDF_SLIM
);
1514 fprintf (dump_file
, " transformation on insn postponned to ipa-profile");
1515 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1516 fprintf (dump_file
, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1517 " hist->all "HOST_WIDEST_INT_PRINT_DEC
"\n", count
, all
);
1523 /* Return true if the stringop CALL with FNDECL shall be profiled.
1524 SIZE_ARG be set to the argument index for the size of the string
1528 interesting_stringop_to_profile_p (tree fndecl
, gimple call
, int *size_arg
)
1530 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
1532 if (fcode
!= BUILT_IN_MEMCPY
&& fcode
!= BUILT_IN_MEMPCPY
1533 && fcode
!= BUILT_IN_MEMSET
&& fcode
!= BUILT_IN_BZERO
)
1538 case BUILT_IN_MEMCPY
:
1539 case BUILT_IN_MEMPCPY
:
1541 return validate_gimple_arglist (call
, POINTER_TYPE
, POINTER_TYPE
,
1542 INTEGER_TYPE
, VOID_TYPE
);
1543 case BUILT_IN_MEMSET
:
1545 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1546 INTEGER_TYPE
, VOID_TYPE
);
1547 case BUILT_IN_BZERO
:
1549 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1556 /* Convert stringop (..., vcall_size)
1558 if (vcall_size == icall_size)
1559 stringop (..., icall_size);
1561 stringop (..., vcall_size);
1562 assuming we'll propagate a true constant into ICALL_SIZE later. */
1565 gimple_stringop_fixed_value (gimple vcall_stmt
, tree icall_size
, int prob
,
1566 gcov_type count
, gcov_type all
)
1568 gimple tmp_stmt
, cond_stmt
, icall_stmt
;
1569 tree tmp0
, tmp1
, vcall_size
, optype
;
1570 basic_block cond_bb
, icall_bb
, vcall_bb
, join_bb
;
1571 edge e_ci
, e_cv
, e_iv
, e_ij
, e_vj
;
1572 gimple_stmt_iterator gsi
;
1576 fndecl
= gimple_call_fndecl (vcall_stmt
);
1577 if (!interesting_stringop_to_profile_p (fndecl
, vcall_stmt
, &size_arg
))
1580 cond_bb
= gimple_bb (vcall_stmt
);
1581 gsi
= gsi_for_stmt (vcall_stmt
);
1583 vcall_size
= gimple_call_arg (vcall_stmt
, size_arg
);
1584 optype
= TREE_TYPE (vcall_size
);
1586 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1587 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1588 tmp_stmt
= gimple_build_assign (tmp0
, fold_convert (optype
, icall_size
));
1589 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1591 tmp_stmt
= gimple_build_assign (tmp1
, vcall_size
);
1592 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1594 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1595 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1597 gimple_set_vdef (vcall_stmt
, NULL
);
1598 gimple_set_vuse (vcall_stmt
, NULL
);
1599 update_stmt (vcall_stmt
);
1600 icall_stmt
= gimple_copy (vcall_stmt
);
1601 gimple_call_set_arg (icall_stmt
, size_arg
, icall_size
);
1602 gsi_insert_before (&gsi
, icall_stmt
, GSI_SAME_STMT
);
1605 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1606 e_ci
= split_block (cond_bb
, cond_stmt
);
1607 icall_bb
= e_ci
->dest
;
1608 icall_bb
->count
= count
;
1610 e_iv
= split_block (icall_bb
, icall_stmt
);
1611 vcall_bb
= e_iv
->dest
;
1612 vcall_bb
->count
= all
- count
;
1614 e_vj
= split_block (vcall_bb
, vcall_stmt
);
1615 join_bb
= e_vj
->dest
;
1616 join_bb
->count
= all
;
1618 e_ci
->flags
= (e_ci
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1619 e_ci
->probability
= prob
;
1620 e_ci
->count
= count
;
1622 e_cv
= make_edge (cond_bb
, vcall_bb
, EDGE_FALSE_VALUE
);
1623 e_cv
->probability
= REG_BR_PROB_BASE
- prob
;
1624 e_cv
->count
= all
- count
;
1628 e_ij
= make_edge (icall_bb
, join_bb
, EDGE_FALLTHRU
);
1629 e_ij
->probability
= REG_BR_PROB_BASE
;
1630 e_ij
->count
= count
;
1632 e_vj
->probability
= REG_BR_PROB_BASE
;
1633 e_vj
->count
= all
- count
;
1635 /* Insert PHI node for the call result if necessary. */
1636 if (gimple_call_lhs (vcall_stmt
)
1637 && TREE_CODE (gimple_call_lhs (vcall_stmt
)) == SSA_NAME
)
1639 tree result
= gimple_call_lhs (vcall_stmt
);
1640 gimple phi
= create_phi_node (result
, join_bb
);
1641 gimple_call_set_lhs (vcall_stmt
,
1642 duplicate_ssa_name (result
, vcall_stmt
));
1643 add_phi_arg (phi
, gimple_call_lhs (vcall_stmt
), e_vj
, UNKNOWN_LOCATION
);
1644 gimple_call_set_lhs (icall_stmt
,
1645 duplicate_ssa_name (result
, icall_stmt
));
1646 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1649 /* Because these are all string op builtins, they're all nothrow. */
1650 gcc_assert (!stmt_could_throw_p (vcall_stmt
));
1651 gcc_assert (!stmt_could_throw_p (icall_stmt
));
1654 /* Find values inside STMT for that we want to measure histograms for
1655 division/modulo optimization. */
1657 gimple_stringops_transform (gimple_stmt_iterator
*gsi
)
1659 gimple stmt
= gsi_stmt (*gsi
);
1662 enum built_in_function fcode
;
1663 histogram_value histogram
;
1664 gcov_type count
, all
, val
;
1666 unsigned int dest_align
, src_align
;
1671 if (gimple_code (stmt
) != GIMPLE_CALL
)
1673 fndecl
= gimple_call_fndecl (stmt
);
1676 fcode
= DECL_FUNCTION_CODE (fndecl
);
1677 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1680 blck_size
= gimple_call_arg (stmt
, size_arg
);
1681 if (TREE_CODE (blck_size
) == INTEGER_CST
)
1684 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_SINGLE_VALUE
);
1687 val
= histogram
->hvalue
.counters
[0];
1688 count
= histogram
->hvalue
.counters
[1];
1689 all
= histogram
->hvalue
.counters
[2];
1690 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1691 /* We require that count is at least half of all; this means
1692 that for the transformation to fire the value must be constant
1693 at least 80% of time. */
1694 if ((6 * count
/ 5) < all
|| optimize_bb_for_size_p (gimple_bb (stmt
)))
1696 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
1699 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1702 dest
= gimple_call_arg (stmt
, 0);
1703 dest_align
= get_pointer_alignment (dest
);
1706 case BUILT_IN_MEMCPY
:
1707 case BUILT_IN_MEMPCPY
:
1708 src
= gimple_call_arg (stmt
, 1);
1709 src_align
= get_pointer_alignment (src
);
1710 if (!can_move_by_pieces (val
, MIN (dest_align
, src_align
)))
1713 case BUILT_IN_MEMSET
:
1714 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1715 gimple_call_arg (stmt
, 1),
1719 case BUILT_IN_BZERO
:
1720 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1728 if (sizeof (gcov_type
) == sizeof (HOST_WIDE_INT
))
1729 tree_val
= build_int_cst (get_gcov_type (), val
);
1733 a
[0] = (unsigned HOST_WIDE_INT
) val
;
1734 a
[1] = val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1;
1736 tree_val
= wide_int_to_tree (get_gcov_type (), wide_int::from_array (a
, 2,
1737 TYPE_PRECISION (get_gcov_type ()), false));
1742 fprintf (dump_file
, "Single value %i stringop transformation on ",
1744 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1746 gimple_stringop_fixed_value (stmt
, tree_val
, prob
, count
, all
);
1752 stringop_block_profile (gimple stmt
, unsigned int *expected_align
,
1753 HOST_WIDE_INT
*expected_size
)
1755 histogram_value histogram
;
1756 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_AVERAGE
);
1758 *expected_size
= -1;
1759 else if (!histogram
->hvalue
.counters
[1])
1761 *expected_size
= -1;
1762 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1767 size
= ((histogram
->hvalue
.counters
[0]
1768 + histogram
->hvalue
.counters
[1] / 2)
1769 / histogram
->hvalue
.counters
[1]);
1770 /* Even if we can hold bigger value in SIZE, INT_MAX
1771 is safe "infinity" for code generation strategies. */
1774 *expected_size
= size
;
1775 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1777 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_IOR
);
1779 *expected_align
= 0;
1780 else if (!histogram
->hvalue
.counters
[0])
1782 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1783 *expected_align
= 0;
1790 count
= histogram
->hvalue
.counters
[0];
1792 while (!(count
& alignment
)
1793 && (alignment
* 2 * BITS_PER_UNIT
))
1795 *expected_align
= alignment
* BITS_PER_UNIT
;
1796 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1801 /* Find values inside STMT for that we want to measure histograms for
1802 division/modulo optimization. */
1804 gimple_divmod_values_to_profile (gimple stmt
, histogram_values
*values
)
1806 tree lhs
, divisor
, op0
, type
;
1807 histogram_value hist
;
1809 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1812 lhs
= gimple_assign_lhs (stmt
);
1813 type
= TREE_TYPE (lhs
);
1814 if (!INTEGRAL_TYPE_P (type
))
1817 switch (gimple_assign_rhs_code (stmt
))
1819 case TRUNC_DIV_EXPR
:
1820 case TRUNC_MOD_EXPR
:
1821 divisor
= gimple_assign_rhs2 (stmt
);
1822 op0
= gimple_assign_rhs1 (stmt
);
1824 values
->reserve (3);
1826 if (TREE_CODE (divisor
) == SSA_NAME
)
1827 /* Check for the case where the divisor is the same value most
1829 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1830 HIST_TYPE_SINGLE_VALUE
,
1833 /* For mod, check whether it is not often a noop (or replaceable by
1834 a few subtractions). */
1835 if (gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
1836 && TYPE_UNSIGNED (type
))
1839 /* Check for a special case where the divisor is power of 2. */
1840 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1844 val
= build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
1845 hist
= gimple_alloc_histogram_value (cfun
, HIST_TYPE_INTERVAL
,
1847 hist
->hdata
.intvl
.int_start
= 0;
1848 hist
->hdata
.intvl
.steps
= 2;
1849 values
->quick_push (hist
);
1858 /* Find calls inside STMT for that we want to measure histograms for
1859 indirect/virtual call optimization. */
1862 gimple_indirect_call_to_profile (gimple stmt
, histogram_values
*values
)
1866 if (gimple_code (stmt
) != GIMPLE_CALL
1867 || gimple_call_internal_p (stmt
)
1868 || gimple_call_fndecl (stmt
) != NULL_TREE
)
1871 callee
= gimple_call_fn (stmt
);
1873 values
->reserve (3);
1875 values
->quick_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_INDIR_CALL
,
1881 /* Find values inside STMT for that we want to measure histograms for
1882 string operations. */
1884 gimple_stringops_values_to_profile (gimple stmt
, histogram_values
*values
)
1891 if (gimple_code (stmt
) != GIMPLE_CALL
)
1893 fndecl
= gimple_call_fndecl (stmt
);
1897 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1900 dest
= gimple_call_arg (stmt
, 0);
1901 blck_size
= gimple_call_arg (stmt
, size_arg
);
1903 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1905 values
->safe_push (gimple_alloc_histogram_value (cfun
,
1906 HIST_TYPE_SINGLE_VALUE
,
1908 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_AVERAGE
,
1911 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1912 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_IOR
,
1916 /* Find values inside STMT for that we want to measure histograms and adds
1917 them to list VALUES. */
1920 gimple_values_to_profile (gimple stmt
, histogram_values
*values
)
1922 gimple_divmod_values_to_profile (stmt
, values
);
1923 gimple_stringops_values_to_profile (stmt
, values
);
1924 gimple_indirect_call_to_profile (stmt
, values
);
1928 gimple_find_values_to_profile (histogram_values
*values
)
1931 gimple_stmt_iterator gsi
;
1933 histogram_value hist
= NULL
;
1937 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1938 gimple_values_to_profile (gsi_stmt (gsi
), values
);
1940 FOR_EACH_VEC_ELT (*values
, i
, hist
)
1944 case HIST_TYPE_INTERVAL
:
1945 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
1948 case HIST_TYPE_POW2
:
1949 hist
->n_counters
= 2;
1952 case HIST_TYPE_SINGLE_VALUE
:
1953 hist
->n_counters
= 3;
1956 case HIST_TYPE_CONST_DELTA
:
1957 hist
->n_counters
= 4;
1960 case HIST_TYPE_INDIR_CALL
:
1961 hist
->n_counters
= 3;
1964 case HIST_TYPE_AVERAGE
:
1965 hist
->n_counters
= 2;
1969 hist
->n_counters
= 1;
1977 fprintf (dump_file
, "Stmt ");
1978 print_gimple_stmt (dump_file
, hist
->hvalue
.stmt
, 0, TDF_SLIM
);
1979 dump_histogram_value (dump_file
, hist
);