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"
25 #include "tree-nested.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "value-prof.h"
33 #include "insn-config.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
45 #include "stringpool.h"
46 #include "tree-ssanames.h"
47 #include "diagnostic.h"
48 #include "gimple-pretty-print.h"
54 #include "pointer-set.h"
56 #include "data-streamer.h"
58 #include "tree-nested.h"
60 /* In this file value profile based optimizations are placed. Currently the
61 following optimizations are implemented (for more detailed descriptions
62 see comments at value_profile_transformations):
64 1) Division/modulo specialization. Provided that we can determine that the
65 operands of the division have some special properties, we may use it to
66 produce more effective code.
68 2) Indirect/virtual call specialization. If we can determine most
69 common function callee in indirect/virtual call. We can use this
70 information to improve code effectiveness (especially info for
73 3) Speculative prefetching. If we are able to determine that the difference
74 between addresses accessed by a memory reference is usually constant, we
75 may add the prefetch instructions.
76 FIXME: This transformation was removed together with RTL based value
80 Value profiling internals
81 ==========================
83 Every value profiling transformation starts with defining what values
84 to profile. There are different histogram types (see HIST_TYPE_* in
85 value-prof.h) and each transformation can request one or more histogram
86 types per GIMPLE statement. The function gimple_find_values_to_profile()
87 collects the values to profile in a vec, and adds the number of counters
88 required for the different histogram types.
90 For a -fprofile-generate run, the statements for which values should be
91 recorded, are instrumented in instrument_values(). The instrumentation
92 is done by helper functions that can be found in tree-profile.c, where
93 new types of histograms can be added if necessary.
95 After a -fprofile-use, the value profiling data is read back in by
96 compute_value_histograms() that translates the collected data to
97 histograms and attaches them to the profiled statements via
98 gimple_add_histogram_value(). Histograms are stored in a hash table
99 that is attached to every intrumented function, see VALUE_HISTOGRAMS
102 The value-profile transformations driver is the function
103 gimple_value_profile_transformations(). It traverses all statements in
104 the to-be-transformed function, and looks for statements with one or
105 more histograms attached to it. If a statement has histograms, the
106 transformation functions are called on the statement.
108 Limitations / FIXME / TODO:
109 * Only one histogram of each type can be associated with a statement.
110 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
111 (This type of histogram was originally used to implement a form of
112 stride profiling based speculative prefetching to improve SPEC2000
113 scores for memory-bound benchmarks, mcf and equake. However, this
114 was an RTL value-profiling transformation, and those have all been
116 * Some value profile transformations are done in builtins.c (?!)
117 * Updating of histograms needs some TLC.
118 * The value profiling code could be used to record analysis results
119 from non-profiling (e.g. VRP).
120 * Adding new profilers should be simplified, starting with a cleanup
121 of what-happens-where andwith making gimple_find_values_to_profile
122 and gimple_value_profile_transformations table-driven, perhaps...
125 static tree
gimple_divmod_fixed_value (gimple
, tree
, int, gcov_type
, gcov_type
);
126 static tree
gimple_mod_pow2 (gimple
, int, gcov_type
, gcov_type
);
127 static tree
gimple_mod_subtract (gimple
, int, int, int, gcov_type
, gcov_type
,
129 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*);
130 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator
*);
131 static bool gimple_mod_subtract_transform (gimple_stmt_iterator
*);
132 static bool gimple_stringops_transform (gimple_stmt_iterator
*);
133 static bool gimple_ic_transform (gimple_stmt_iterator
*);
135 /* Allocate histogram value. */
137 static histogram_value
138 gimple_alloc_histogram_value (struct function
*fun ATTRIBUTE_UNUSED
,
139 enum hist_type type
, gimple stmt
, tree value
)
141 histogram_value hist
= (histogram_value
) xcalloc (1, sizeof (*hist
));
142 hist
->hvalue
.value
= value
;
143 hist
->hvalue
.stmt
= stmt
;
148 /* Hash value for histogram. */
151 histogram_hash (const void *x
)
153 return htab_hash_pointer (((const_histogram_value
)x
)->hvalue
.stmt
);
156 /* Return nonzero if statement for histogram_value X is Y. */
159 histogram_eq (const void *x
, const void *y
)
161 return ((const_histogram_value
) x
)->hvalue
.stmt
== (const_gimple
) y
;
164 /* Set histogram for STMT. */
167 set_histogram_value (struct function
*fun
, gimple stmt
, histogram_value hist
)
170 if (!hist
&& !VALUE_HISTOGRAMS (fun
))
172 if (!VALUE_HISTOGRAMS (fun
))
173 VALUE_HISTOGRAMS (fun
) = htab_create (1, histogram_hash
,
175 loc
= htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
176 htab_hash_pointer (stmt
),
177 hist
? INSERT
: NO_INSERT
);
181 htab_clear_slot (VALUE_HISTOGRAMS (fun
), loc
);
187 /* Get histogram list for STMT. */
190 gimple_histogram_value (struct function
*fun
, gimple stmt
)
192 if (!VALUE_HISTOGRAMS (fun
))
194 return (histogram_value
) htab_find_with_hash (VALUE_HISTOGRAMS (fun
), stmt
,
195 htab_hash_pointer (stmt
));
198 /* Add histogram for STMT. */
201 gimple_add_histogram_value (struct function
*fun
, gimple stmt
,
202 histogram_value hist
)
204 hist
->hvalue
.next
= gimple_histogram_value (fun
, stmt
);
205 set_histogram_value (fun
, stmt
, hist
);
210 /* Remove histogram HIST from STMT's histogram list. */
213 gimple_remove_histogram_value (struct function
*fun
, gimple stmt
,
214 histogram_value hist
)
216 histogram_value hist2
= gimple_histogram_value (fun
, stmt
);
219 set_histogram_value (fun
, stmt
, hist
->hvalue
.next
);
223 while (hist2
->hvalue
.next
!= hist
)
224 hist2
= hist2
->hvalue
.next
;
225 hist2
->hvalue
.next
= hist
->hvalue
.next
;
227 free (hist
->hvalue
.counters
);
228 #ifdef ENABLE_CHECKING
229 memset (hist
, 0xab, sizeof (*hist
));
235 /* Lookup histogram of type TYPE in the STMT. */
238 gimple_histogram_value_of_type (struct function
*fun
, gimple stmt
,
241 histogram_value hist
;
242 for (hist
= gimple_histogram_value (fun
, stmt
); hist
;
243 hist
= hist
->hvalue
.next
)
244 if (hist
->type
== type
)
249 /* Dump information about HIST to DUMP_FILE. */
252 dump_histogram_value (FILE *dump_file
, histogram_value hist
)
256 case HIST_TYPE_INTERVAL
:
257 fprintf (dump_file
, "Interval counter range %d -- %d",
258 hist
->hdata
.intvl
.int_start
,
259 (hist
->hdata
.intvl
.int_start
260 + hist
->hdata
.intvl
.steps
- 1));
261 if (hist
->hvalue
.counters
)
264 fprintf (dump_file
, " [");
265 for (i
= 0; i
< hist
->hdata
.intvl
.steps
; i
++)
266 fprintf (dump_file
, " %d:"HOST_WIDEST_INT_PRINT_DEC
,
267 hist
->hdata
.intvl
.int_start
+ i
,
268 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[i
]);
269 fprintf (dump_file
, " ] outside range:"HOST_WIDEST_INT_PRINT_DEC
,
270 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[i
]);
272 fprintf (dump_file
, ".\n");
276 fprintf (dump_file
, "Pow2 counter ");
277 if (hist
->hvalue
.counters
)
279 fprintf (dump_file
, "pow2:"HOST_WIDEST_INT_PRINT_DEC
280 " nonpow2:"HOST_WIDEST_INT_PRINT_DEC
,
281 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
282 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1]);
284 fprintf (dump_file
, ".\n");
287 case HIST_TYPE_SINGLE_VALUE
:
288 fprintf (dump_file
, "Single value ");
289 if (hist
->hvalue
.counters
)
291 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
292 " match:"HOST_WIDEST_INT_PRINT_DEC
293 " wrong:"HOST_WIDEST_INT_PRINT_DEC
,
294 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
295 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
296 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
298 fprintf (dump_file
, ".\n");
301 case HIST_TYPE_AVERAGE
:
302 fprintf (dump_file
, "Average value ");
303 if (hist
->hvalue
.counters
)
305 fprintf (dump_file
, "sum:"HOST_WIDEST_INT_PRINT_DEC
306 " times:"HOST_WIDEST_INT_PRINT_DEC
,
307 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
308 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1]);
310 fprintf (dump_file
, ".\n");
314 fprintf (dump_file
, "IOR value ");
315 if (hist
->hvalue
.counters
)
317 fprintf (dump_file
, "ior:"HOST_WIDEST_INT_PRINT_DEC
,
318 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0]);
320 fprintf (dump_file
, ".\n");
323 case HIST_TYPE_CONST_DELTA
:
324 fprintf (dump_file
, "Constant delta ");
325 if (hist
->hvalue
.counters
)
327 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
328 " match:"HOST_WIDEST_INT_PRINT_DEC
329 " wrong:"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");
336 case HIST_TYPE_INDIR_CALL
:
337 fprintf (dump_file
, "Indirect call ");
338 if (hist
->hvalue
.counters
)
340 fprintf (dump_file
, "value:"HOST_WIDEST_INT_PRINT_DEC
341 " match:"HOST_WIDEST_INT_PRINT_DEC
342 " all:"HOST_WIDEST_INT_PRINT_DEC
,
343 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0],
344 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[1],
345 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[2]);
347 fprintf (dump_file
, ".\n");
349 case HIST_TYPE_TIME_PROFILE
:
350 fprintf (dump_file
, "Time profile ");
351 if (hist
->hvalue
.counters
)
353 fprintf (dump_file
, "time:"HOST_WIDEST_INT_PRINT_DEC
,
354 (HOST_WIDEST_INT
) hist
->hvalue
.counters
[0]);
356 fprintf (dump_file
, ".\n");
363 /* Dump information about HIST to DUMP_FILE. */
366 stream_out_histogram_value (struct output_block
*ob
, histogram_value hist
)
371 bp
= bitpack_create (ob
->main_stream
);
372 bp_pack_enum (&bp
, hist_type
, HIST_TYPE_MAX
, hist
->type
);
373 bp_pack_value (&bp
, hist
->hvalue
.next
!= NULL
, 1);
374 streamer_write_bitpack (&bp
);
377 case HIST_TYPE_INTERVAL
:
378 streamer_write_hwi (ob
, hist
->hdata
.intvl
.int_start
);
379 streamer_write_uhwi (ob
, hist
->hdata
.intvl
.steps
);
384 for (i
= 0; i
< hist
->n_counters
; i
++)
385 streamer_write_gcov_count (ob
, hist
->hvalue
.counters
[i
]);
386 if (hist
->hvalue
.next
)
387 stream_out_histogram_value (ob
, hist
->hvalue
.next
);
389 /* Dump information about HIST to DUMP_FILE. */
392 stream_in_histogram_value (struct lto_input_block
*ib
, gimple stmt
)
395 unsigned int ncounters
= 0;
398 histogram_value new_val
;
400 histogram_value
*next_p
= NULL
;
404 bp
= streamer_read_bitpack (ib
);
405 type
= bp_unpack_enum (&bp
, hist_type
, HIST_TYPE_MAX
);
406 next
= bp_unpack_value (&bp
, 1);
407 new_val
= gimple_alloc_histogram_value (cfun
, type
, stmt
, NULL
);
410 case HIST_TYPE_INTERVAL
:
411 new_val
->hdata
.intvl
.int_start
= streamer_read_hwi (ib
);
412 new_val
->hdata
.intvl
.steps
= streamer_read_uhwi (ib
);
413 ncounters
= new_val
->hdata
.intvl
.steps
+ 2;
417 case HIST_TYPE_AVERAGE
:
421 case HIST_TYPE_SINGLE_VALUE
:
422 case HIST_TYPE_INDIR_CALL
:
426 case HIST_TYPE_CONST_DELTA
:
431 case HIST_TYPE_TIME_PROFILE
:
437 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * ncounters
);
438 new_val
->n_counters
= ncounters
;
439 for (i
= 0; i
< ncounters
; i
++)
440 new_val
->hvalue
.counters
[i
] = streamer_read_gcov_count (ib
);
442 gimple_add_histogram_value (cfun
, stmt
, new_val
);
445 next_p
= &new_val
->hvalue
.next
;
450 /* Dump all histograms attached to STMT to DUMP_FILE. */
453 dump_histograms_for_stmt (struct function
*fun
, FILE *dump_file
, gimple stmt
)
455 histogram_value hist
;
456 for (hist
= gimple_histogram_value (fun
, stmt
); hist
; hist
= hist
->hvalue
.next
)
457 dump_histogram_value (dump_file
, hist
);
460 /* Remove all histograms associated with STMT. */
463 gimple_remove_stmt_histograms (struct function
*fun
, gimple stmt
)
466 while ((val
= gimple_histogram_value (fun
, stmt
)) != NULL
)
467 gimple_remove_histogram_value (fun
, stmt
, val
);
470 /* Duplicate all histograms associates with OSTMT to STMT. */
473 gimple_duplicate_stmt_histograms (struct function
*fun
, gimple stmt
,
474 struct function
*ofun
, gimple ostmt
)
477 for (val
= gimple_histogram_value (ofun
, ostmt
); val
!= NULL
; val
= val
->hvalue
.next
)
479 histogram_value new_val
= gimple_alloc_histogram_value (fun
, val
->type
, NULL
, NULL
);
480 memcpy (new_val
, val
, sizeof (*val
));
481 new_val
->hvalue
.stmt
= stmt
;
482 new_val
->hvalue
.counters
= XNEWVAR (gcov_type
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
483 memcpy (new_val
->hvalue
.counters
, val
->hvalue
.counters
, sizeof (*new_val
->hvalue
.counters
) * new_val
->n_counters
);
484 gimple_add_histogram_value (fun
, stmt
, new_val
);
489 /* Move all histograms associated with OSTMT to STMT. */
492 gimple_move_stmt_histograms (struct function
*fun
, gimple stmt
, gimple ostmt
)
494 histogram_value val
= gimple_histogram_value (fun
, ostmt
);
497 /* The following three statements can't be reordered,
498 because histogram hashtab relies on stmt field value
499 for finding the exact slot. */
500 set_histogram_value (fun
, ostmt
, NULL
);
501 for (; val
!= NULL
; val
= val
->hvalue
.next
)
502 val
->hvalue
.stmt
= stmt
;
503 set_histogram_value (fun
, stmt
, val
);
507 static bool error_found
= false;
509 /* Helper function for verify_histograms. For each histogram reachable via htab
510 walk verify that it was reached via statement walk. */
513 visit_hist (void **slot
, void *data
)
515 struct pointer_set_t
*visited
= (struct pointer_set_t
*) data
;
516 histogram_value hist
= *(histogram_value
*) slot
;
518 if (!pointer_set_contains (visited
, hist
)
519 && hist
->type
!= HIST_TYPE_TIME_PROFILE
)
521 error ("dead histogram");
522 dump_histogram_value (stderr
, hist
);
523 debug_gimple_stmt (hist
->hvalue
.stmt
);
530 /* Verify sanity of the histograms. */
533 verify_histograms (void)
536 gimple_stmt_iterator gsi
;
537 histogram_value hist
;
538 struct pointer_set_t
*visited_hists
;
541 visited_hists
= pointer_set_create ();
543 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
545 gimple stmt
= gsi_stmt (gsi
);
547 for (hist
= gimple_histogram_value (cfun
, stmt
); hist
;
548 hist
= hist
->hvalue
.next
)
550 if (hist
->hvalue
.stmt
!= stmt
)
552 error ("Histogram value statement does not correspond to "
553 "the statement it is associated with");
554 debug_gimple_stmt (stmt
);
555 dump_histogram_value (stderr
, hist
);
558 pointer_set_insert (visited_hists
, hist
);
561 if (VALUE_HISTOGRAMS (cfun
))
562 htab_traverse (VALUE_HISTOGRAMS (cfun
), visit_hist
, visited_hists
);
563 pointer_set_destroy (visited_hists
);
565 internal_error ("verify_histograms failed");
568 /* Helper function for verify_histograms. For each histogram reachable via htab
569 walk verify that it was reached via statement walk. */
572 free_hist (void **slot
, void *data ATTRIBUTE_UNUSED
)
574 histogram_value hist
= *(histogram_value
*) slot
;
575 free (hist
->hvalue
.counters
);
576 #ifdef ENABLE_CHECKING
577 memset (hist
, 0xab, sizeof (*hist
));
584 free_histograms (void)
586 if (VALUE_HISTOGRAMS (cfun
))
588 htab_traverse (VALUE_HISTOGRAMS (cfun
), free_hist
, NULL
);
589 htab_delete (VALUE_HISTOGRAMS (cfun
));
590 VALUE_HISTOGRAMS (cfun
) = NULL
;
595 /* The overall number of invocations of the counter should match
596 execution count of basic block. Report it as error rather than
597 internal error as it might mean that user has misused the profile
601 check_counter (gimple stmt
, const char * name
,
602 gcov_type
*count
, gcov_type
*all
, gcov_type bb_count
)
604 if (*all
!= bb_count
|| *count
> *all
)
607 locus
= (stmt
!= NULL
)
608 ? gimple_location (stmt
)
609 : DECL_SOURCE_LOCATION (current_function_decl
);
610 if (flag_profile_correction
)
612 if (dump_enabled_p ())
613 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
614 "correcting inconsistent value profile: %s "
615 "profiler overall count (%d) does not match BB "
616 "count (%d)\n", name
, (int)*all
, (int)bb_count
);
624 error_at (locus
, "corrupted value profile: %s "
625 "profile counter (%d out of %d) inconsistent with "
626 "basic-block count (%d)",
639 /* GIMPLE based transformations. */
642 gimple_value_profile_transformations (void)
645 gimple_stmt_iterator gsi
;
646 bool changed
= false;
650 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
652 gimple stmt
= gsi_stmt (gsi
);
653 histogram_value th
= gimple_histogram_value (cfun
, stmt
);
659 fprintf (dump_file
, "Trying transformations on stmt ");
660 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
661 dump_histograms_for_stmt (cfun
, dump_file
, stmt
);
664 /* Transformations: */
665 /* The order of things in this conditional controls which
666 transformation is used when more than one is applicable. */
667 /* It is expected that any code added by the transformations
668 will be added before the current statement, and that the
669 current statement remain valid (although possibly
670 modified) upon return. */
671 if (gimple_mod_subtract_transform (&gsi
)
672 || gimple_divmod_fixed_value_transform (&gsi
)
673 || gimple_mod_pow2_value_transform (&gsi
)
674 || gimple_stringops_transform (&gsi
)
675 || gimple_ic_transform (&gsi
))
677 stmt
= gsi_stmt (gsi
);
679 /* Original statement may no longer be in the same block. */
680 if (bb
!= gimple_bb (stmt
))
682 bb
= gimple_bb (stmt
);
683 gsi
= gsi_for_stmt (stmt
);
698 /* Generate code for transformation 1 (with parent gimple assignment
699 STMT and probability of taking the optimal path PROB, which is
700 equivalent to COUNT/ALL within roundoff error). This generates the
701 result into a temp and returns the temp; it does not replace or
702 alter the original STMT. */
705 gimple_divmod_fixed_value (gimple stmt
, tree value
, int prob
, gcov_type count
,
708 gimple stmt1
, stmt2
, stmt3
;
709 tree tmp0
, tmp1
, tmp2
;
710 gimple bb1end
, bb2end
, bb3end
;
711 basic_block bb
, bb2
, bb3
, bb4
;
712 tree optype
, op1
, op2
;
713 edge e12
, e13
, e23
, e24
, e34
;
714 gimple_stmt_iterator gsi
;
716 gcc_assert (is_gimple_assign (stmt
)
717 && (gimple_assign_rhs_code (stmt
) == TRUNC_DIV_EXPR
718 || gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
));
720 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
721 op1
= gimple_assign_rhs1 (stmt
);
722 op2
= gimple_assign_rhs2 (stmt
);
724 bb
= gimple_bb (stmt
);
725 gsi
= gsi_for_stmt (stmt
);
727 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
728 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
729 stmt1
= gimple_build_assign (tmp0
, fold_convert (optype
, value
));
730 stmt2
= gimple_build_assign (tmp1
, op2
);
731 stmt3
= gimple_build_cond (NE_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
732 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
733 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
734 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
737 tmp2
= create_tmp_reg (optype
, "PROF");
738 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), tmp2
,
740 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
743 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), tmp2
,
745 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
749 /* Edge e23 connects bb2 to bb3, etc. */
750 e12
= split_block (bb
, bb1end
);
753 e23
= split_block (bb2
, bb2end
);
755 bb3
->count
= all
- count
;
756 e34
= split_block (bb3
, bb3end
);
760 e12
->flags
&= ~EDGE_FALLTHRU
;
761 e12
->flags
|= EDGE_FALSE_VALUE
;
762 e12
->probability
= prob
;
765 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
766 e13
->probability
= REG_BR_PROB_BASE
- prob
;
767 e13
->count
= all
- count
;
771 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
772 e24
->probability
= REG_BR_PROB_BASE
;
775 e34
->probability
= REG_BR_PROB_BASE
;
776 e34
->count
= all
- count
;
782 /* Do transform 1) on INSN if applicable. */
785 gimple_divmod_fixed_value_transform (gimple_stmt_iterator
*si
)
787 histogram_value histogram
;
789 gcov_type val
, count
, all
;
790 tree result
, value
, tree_val
;
794 stmt
= gsi_stmt (*si
);
795 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
798 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
))))
801 code
= gimple_assign_rhs_code (stmt
);
803 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
806 histogram
= gimple_histogram_value_of_type (cfun
, stmt
,
807 HIST_TYPE_SINGLE_VALUE
);
811 value
= histogram
->hvalue
.value
;
812 val
= histogram
->hvalue
.counters
[0];
813 count
= histogram
->hvalue
.counters
[1];
814 all
= histogram
->hvalue
.counters
[2];
815 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
817 /* We require that count is at least half of all; this means
818 that for the transformation to fire the value must be constant
819 at least 50% of time (and 75% gives the guarantee of usage). */
820 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
822 || optimize_bb_for_size_p (gimple_bb (stmt
)))
825 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
828 /* Compute probability of taking the optimal path. */
830 prob
= GCOV_COMPUTE_SCALE (count
, all
);
834 tree_val
= build_int_cst_wide (get_gcov_type (),
835 (unsigned HOST_WIDE_INT
) val
,
836 val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1);
837 result
= gimple_divmod_fixed_value (stmt
, tree_val
, prob
, count
, all
);
841 fprintf (dump_file
, "Div/mod by constant ");
842 print_generic_expr (dump_file
, value
, TDF_SLIM
);
843 fprintf (dump_file
, "=");
844 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
845 fprintf (dump_file
, " transformation on insn ");
846 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
849 gimple_assign_set_rhs_from_tree (si
, result
);
850 update_stmt (gsi_stmt (*si
));
855 /* Generate code for transformation 2 (with parent gimple assign STMT and
856 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
857 within roundoff error). This generates the result into a temp and returns
858 the temp; it does not replace or alter the original STMT. */
860 gimple_mod_pow2 (gimple stmt
, int prob
, gcov_type count
, gcov_type all
)
862 gimple stmt1
, stmt2
, stmt3
, stmt4
;
864 gimple bb1end
, bb2end
, bb3end
;
865 basic_block bb
, bb2
, bb3
, bb4
;
866 tree optype
, op1
, op2
;
867 edge e12
, e13
, e23
, e24
, e34
;
868 gimple_stmt_iterator gsi
;
871 gcc_assert (is_gimple_assign (stmt
)
872 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
874 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
875 op1
= gimple_assign_rhs1 (stmt
);
876 op2
= gimple_assign_rhs2 (stmt
);
878 bb
= gimple_bb (stmt
);
879 gsi
= gsi_for_stmt (stmt
);
881 result
= create_tmp_reg (optype
, "PROF");
882 tmp2
= make_temp_ssa_name (optype
, NULL
, "PROF");
883 tmp3
= make_temp_ssa_name (optype
, NULL
, "PROF");
884 stmt2
= gimple_build_assign_with_ops (PLUS_EXPR
, tmp2
, op2
,
885 build_int_cst (optype
, -1));
886 stmt3
= gimple_build_assign_with_ops (BIT_AND_EXPR
, tmp3
, tmp2
, op2
);
887 stmt4
= gimple_build_cond (NE_EXPR
, tmp3
, build_int_cst (optype
, 0),
888 NULL_TREE
, NULL_TREE
);
889 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
890 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
891 gsi_insert_before (&gsi
, stmt4
, GSI_SAME_STMT
);
894 /* tmp2 == op2-1 inherited from previous block. */
895 stmt1
= gimple_build_assign_with_ops (BIT_AND_EXPR
, result
, op1
, tmp2
);
896 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
899 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), result
,
901 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
905 /* Edge e23 connects bb2 to bb3, etc. */
906 e12
= split_block (bb
, bb1end
);
909 e23
= split_block (bb2
, bb2end
);
911 bb3
->count
= all
- count
;
912 e34
= split_block (bb3
, bb3end
);
916 e12
->flags
&= ~EDGE_FALLTHRU
;
917 e12
->flags
|= EDGE_FALSE_VALUE
;
918 e12
->probability
= prob
;
921 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
922 e13
->probability
= REG_BR_PROB_BASE
- prob
;
923 e13
->count
= all
- count
;
927 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
928 e24
->probability
= REG_BR_PROB_BASE
;
931 e34
->probability
= REG_BR_PROB_BASE
;
932 e34
->count
= all
- count
;
937 /* Do transform 2) on INSN if applicable. */
939 gimple_mod_pow2_value_transform (gimple_stmt_iterator
*si
)
941 histogram_value histogram
;
943 gcov_type count
, wrong_values
, all
;
944 tree lhs_type
, result
, value
;
948 stmt
= gsi_stmt (*si
);
949 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
952 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
953 if (!INTEGRAL_TYPE_P (lhs_type
))
956 code
= gimple_assign_rhs_code (stmt
);
958 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
961 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_POW2
);
965 value
= histogram
->hvalue
.value
;
966 wrong_values
= histogram
->hvalue
.counters
[0];
967 count
= histogram
->hvalue
.counters
[1];
969 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
971 /* We require that we hit a power of 2 at least half of all evaluations. */
972 if (simple_cst_equal (gimple_assign_rhs2 (stmt
), value
) != 1
973 || count
< wrong_values
974 || optimize_bb_for_size_p (gimple_bb (stmt
)))
979 fprintf (dump_file
, "Mod power of 2 transformation on insn ");
980 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
983 /* Compute probability of taking the optimal path. */
984 all
= count
+ wrong_values
;
986 if (check_counter (stmt
, "pow2", &count
, &all
, gimple_bb (stmt
)->count
))
990 prob
= GCOV_COMPUTE_SCALE (count
, all
);
994 result
= gimple_mod_pow2 (stmt
, prob
, count
, all
);
996 gimple_assign_set_rhs_from_tree (si
, result
);
997 update_stmt (gsi_stmt (*si
));
1002 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1003 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1004 supported and this is built into this interface. The probabilities of taking
1005 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1006 COUNT2/ALL respectively within roundoff error). This generates the
1007 result into a temp and returns the temp; it does not replace or alter
1008 the original STMT. */
1009 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1012 gimple_mod_subtract (gimple stmt
, int prob1
, int prob2
, int ncounts
,
1013 gcov_type count1
, gcov_type count2
, gcov_type all
)
1015 gimple stmt1
, stmt2
, stmt3
;
1017 gimple bb1end
, bb2end
= NULL
, bb3end
;
1018 basic_block bb
, bb2
, bb3
, bb4
;
1019 tree optype
, op1
, op2
;
1020 edge e12
, e23
= 0, e24
, e34
, e14
;
1021 gimple_stmt_iterator gsi
;
1024 gcc_assert (is_gimple_assign (stmt
)
1025 && gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
);
1027 optype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1028 op1
= gimple_assign_rhs1 (stmt
);
1029 op2
= gimple_assign_rhs2 (stmt
);
1031 bb
= gimple_bb (stmt
);
1032 gsi
= gsi_for_stmt (stmt
);
1034 result
= create_tmp_reg (optype
, "PROF");
1035 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1036 stmt1
= gimple_build_assign (result
, op1
);
1037 stmt2
= gimple_build_assign (tmp1
, op2
);
1038 stmt3
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1039 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1040 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1041 gsi_insert_before (&gsi
, stmt3
, GSI_SAME_STMT
);
1044 if (ncounts
) /* Assumed to be 0 or 1 */
1046 stmt1
= gimple_build_assign_with_ops (MINUS_EXPR
, result
, result
, tmp1
);
1047 stmt2
= gimple_build_cond (LT_EXPR
, result
, tmp1
, NULL_TREE
, NULL_TREE
);
1048 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1049 gsi_insert_before (&gsi
, stmt2
, GSI_SAME_STMT
);
1053 /* Fallback case. */
1054 stmt1
= gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), result
,
1056 gsi_insert_before (&gsi
, stmt1
, GSI_SAME_STMT
);
1060 /* Edge e23 connects bb2 to bb3, etc. */
1061 /* However block 3 is optional; if it is not there, references
1062 to 3 really refer to block 2. */
1063 e12
= split_block (bb
, bb1end
);
1065 bb2
->count
= all
- count1
;
1067 if (ncounts
) /* Assumed to be 0 or 1. */
1069 e23
= split_block (bb2
, bb2end
);
1071 bb3
->count
= all
- count1
- count2
;
1074 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
1078 e12
->flags
&= ~EDGE_FALLTHRU
;
1079 e12
->flags
|= EDGE_FALSE_VALUE
;
1080 e12
->probability
= REG_BR_PROB_BASE
- prob1
;
1081 e12
->count
= all
- count1
;
1083 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
1084 e14
->probability
= prob1
;
1085 e14
->count
= count1
;
1087 if (ncounts
) /* Assumed to be 0 or 1. */
1089 e23
->flags
&= ~EDGE_FALLTHRU
;
1090 e23
->flags
|= EDGE_FALSE_VALUE
;
1091 e23
->count
= all
- count1
- count2
;
1092 e23
->probability
= REG_BR_PROB_BASE
- prob2
;
1094 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
1095 e24
->probability
= prob2
;
1096 e24
->count
= count2
;
1099 e34
->probability
= REG_BR_PROB_BASE
;
1100 e34
->count
= all
- count1
- count2
;
1106 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1109 gimple_mod_subtract_transform (gimple_stmt_iterator
*si
)
1111 histogram_value histogram
;
1112 enum tree_code code
;
1113 gcov_type count
, wrong_values
, all
;
1114 tree lhs_type
, result
;
1115 gcov_type prob1
, prob2
;
1116 unsigned int i
, steps
;
1117 gcov_type count1
, count2
;
1120 stmt
= gsi_stmt (*si
);
1121 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1124 lhs_type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1125 if (!INTEGRAL_TYPE_P (lhs_type
))
1128 code
= gimple_assign_rhs_code (stmt
);
1130 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (lhs_type
))
1133 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INTERVAL
);
1139 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1140 all
+= histogram
->hvalue
.counters
[i
];
1142 wrong_values
+= histogram
->hvalue
.counters
[i
];
1143 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
1144 steps
= histogram
->hdata
.intvl
.steps
;
1145 all
+= wrong_values
;
1146 count1
= histogram
->hvalue
.counters
[0];
1147 count2
= histogram
->hvalue
.counters
[1];
1149 /* Compute probability of taking the optimal path. */
1150 if (check_counter (stmt
, "interval", &count1
, &all
, gimple_bb (stmt
)->count
))
1152 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1156 if (flag_profile_correction
&& count1
+ count2
> all
)
1157 all
= count1
+ count2
;
1159 gcc_assert (count1
+ count2
<= all
);
1161 /* We require that we use just subtractions in at least 50% of all
1164 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
1166 count
+= histogram
->hvalue
.counters
[i
];
1167 if (count
* 2 >= all
)
1171 || optimize_bb_for_size_p (gimple_bb (stmt
)))
1174 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1177 fprintf (dump_file
, "Mod subtract transformation on insn ");
1178 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1181 /* Compute probability of taking the optimal path(s). */
1184 prob1
= GCOV_COMPUTE_SCALE (count1
, all
);
1185 prob2
= GCOV_COMPUTE_SCALE (count2
, all
);
1192 /* In practice, "steps" is always 2. This interface reflects this,
1193 and will need to be changed if "steps" can change. */
1194 result
= gimple_mod_subtract (stmt
, prob1
, prob2
, i
, count1
, count2
, all
);
1196 gimple_assign_set_rhs_from_tree (si
, result
);
1197 update_stmt (gsi_stmt (*si
));
1202 static pointer_map_t
*cgraph_node_map
;
1204 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1205 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1206 that the PROFILE_IDs was already assigned. */
1209 init_node_map (bool local
)
1211 struct cgraph_node
*n
;
1212 cgraph_node_map
= pointer_map_create ();
1214 FOR_EACH_DEFINED_FUNCTION (n
)
1215 if (cgraph_function_with_gimple_body_p (n
)
1216 && !cgraph_only_called_directly_p (n
))
1221 n
->profile_id
= coverage_compute_profile_id (n
);
1222 while ((val
= pointer_map_contains (cgraph_node_map
,
1223 (void *)(size_t)n
->profile_id
))
1227 fprintf (dump_file
, "Local profile-id %i conflict"
1228 " with nodes %s/%i %s/%i\n",
1232 (*(symtab_node
**)val
)->name (),
1233 (*(symtab_node
**)val
)->order
);
1234 n
->profile_id
= (n
->profile_id
+ 1) & 0x7fffffff;
1237 else if (!n
->profile_id
)
1241 "Node %s/%i has no profile-id"
1242 " (profile feedback missing?)\n",
1247 else if ((val
= pointer_map_contains (cgraph_node_map
,
1248 (void *)(size_t)n
->profile_id
)))
1252 "Node %s/%i has IP profile-id %i conflict. "
1260 *pointer_map_insert (cgraph_node_map
,
1261 (void *)(size_t)n
->profile_id
) = (void *)n
;
1265 /* Delete the CGRAPH_NODE_MAP. */
1270 pointer_map_destroy (cgraph_node_map
);
1273 /* Return cgraph node for function with pid */
1276 find_func_by_profile_id (int profile_id
)
1278 void **val
= pointer_map_contains (cgraph_node_map
,
1279 (void *)(size_t)profile_id
);
1281 return (struct cgraph_node
*)*val
;
1286 /* Perform sanity check on the indirect call target. Due to race conditions,
1287 false function target may be attributed to an indirect call site. If the
1288 call expression type mismatches with the target function's type, expand_call
1289 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1290 Returns true if TARGET is considered ok for call CALL_STMT. */
1293 check_ic_target (gimple call_stmt
, struct cgraph_node
*target
)
1296 if (gimple_check_call_matching_types (call_stmt
, target
->decl
, true))
1299 locus
= gimple_location (call_stmt
);
1300 if (dump_enabled_p ())
1301 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, locus
,
1302 "Skipping target %s with mismatching types for icall\n",
1307 /* Do transformation
1309 if (actual_callee_address == address_of_most_common_function/method)
1316 gimple_ic (gimple icall_stmt
, struct cgraph_node
*direct_call
,
1317 int prob
, gcov_type count
, gcov_type all
)
1319 gimple dcall_stmt
, load_stmt
, cond_stmt
;
1320 tree tmp0
, tmp1
, tmp
;
1321 basic_block cond_bb
, dcall_bb
, icall_bb
, join_bb
= NULL
;
1322 tree optype
= build_pointer_type (void_type_node
);
1323 edge e_cd
, e_ci
, e_di
, e_dj
= NULL
, e_ij
;
1324 gimple_stmt_iterator gsi
;
1328 gimple_stmt_iterator psi
;
1330 cond_bb
= gimple_bb (icall_stmt
);
1331 gsi
= gsi_for_stmt (icall_stmt
);
1333 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1334 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1335 tmp
= unshare_expr (gimple_call_fn (icall_stmt
));
1336 load_stmt
= gimple_build_assign (tmp0
, tmp
);
1337 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1339 tmp
= fold_convert (optype
, build_addr (direct_call
->decl
,
1340 current_function_decl
));
1341 load_stmt
= gimple_build_assign (tmp1
, tmp
);
1342 gsi_insert_before (&gsi
, load_stmt
, GSI_SAME_STMT
);
1344 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1345 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1347 gimple_set_vdef (icall_stmt
, NULL_TREE
);
1348 gimple_set_vuse (icall_stmt
, NULL_TREE
);
1349 update_stmt (icall_stmt
);
1350 dcall_stmt
= gimple_copy (icall_stmt
);
1351 gimple_call_set_fndecl (dcall_stmt
, direct_call
->decl
);
1352 dflags
= flags_from_decl_or_type (direct_call
->decl
);
1353 if ((dflags
& ECF_NORETURN
) != 0)
1354 gimple_call_set_lhs (dcall_stmt
, NULL_TREE
);
1355 gsi_insert_before (&gsi
, dcall_stmt
, GSI_SAME_STMT
);
1358 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1359 e_cd
= split_block (cond_bb
, cond_stmt
);
1360 dcall_bb
= e_cd
->dest
;
1361 dcall_bb
->count
= count
;
1363 e_di
= split_block (dcall_bb
, dcall_stmt
);
1364 icall_bb
= e_di
->dest
;
1365 icall_bb
->count
= all
- count
;
1367 /* Do not disturb existing EH edges from the indirect call. */
1368 if (!stmt_ends_bb_p (icall_stmt
))
1369 e_ij
= split_block (icall_bb
, icall_stmt
);
1372 e_ij
= find_fallthru_edge (icall_bb
->succs
);
1373 /* The indirect call might be noreturn. */
1376 e_ij
->probability
= REG_BR_PROB_BASE
;
1377 e_ij
->count
= all
- count
;
1378 e_ij
= single_pred_edge (split_edge (e_ij
));
1383 join_bb
= e_ij
->dest
;
1384 join_bb
->count
= all
;
1387 e_cd
->flags
= (e_cd
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1388 e_cd
->probability
= prob
;
1389 e_cd
->count
= count
;
1391 e_ci
= make_edge (cond_bb
, icall_bb
, EDGE_FALSE_VALUE
);
1392 e_ci
->probability
= REG_BR_PROB_BASE
- prob
;
1393 e_ci
->count
= all
- count
;
1399 if ((dflags
& ECF_NORETURN
) != 0)
1403 e_dj
= make_edge (dcall_bb
, join_bb
, EDGE_FALLTHRU
);
1404 e_dj
->probability
= REG_BR_PROB_BASE
;
1405 e_dj
->count
= count
;
1407 e_ij
->count
= all
- count
;
1409 e_ij
->probability
= REG_BR_PROB_BASE
;
1412 /* Insert PHI node for the call result if necessary. */
1413 if (gimple_call_lhs (icall_stmt
)
1414 && TREE_CODE (gimple_call_lhs (icall_stmt
)) == SSA_NAME
1415 && (dflags
& ECF_NORETURN
) == 0)
1417 tree result
= gimple_call_lhs (icall_stmt
);
1418 gimple phi
= create_phi_node (result
, join_bb
);
1419 gimple_call_set_lhs (icall_stmt
,
1420 duplicate_ssa_name (result
, icall_stmt
));
1421 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1422 gimple_call_set_lhs (dcall_stmt
,
1423 duplicate_ssa_name (result
, dcall_stmt
));
1424 add_phi_arg (phi
, gimple_call_lhs (dcall_stmt
), e_dj
, UNKNOWN_LOCATION
);
1427 /* Build an EH edge for the direct call if necessary. */
1428 lp_nr
= lookup_stmt_eh_lp (icall_stmt
);
1429 if (lp_nr
> 0 && stmt_could_throw_p (dcall_stmt
))
1431 add_stmt_to_eh_lp (dcall_stmt
, lp_nr
);
1434 FOR_EACH_EDGE (e_eh
, ei
, icall_bb
->succs
)
1435 if (e_eh
->flags
& (EDGE_EH
| EDGE_ABNORMAL
))
1437 e
= make_edge (dcall_bb
, e_eh
->dest
, e_eh
->flags
);
1438 for (psi
= gsi_start_phis (e_eh
->dest
);
1439 !gsi_end_p (psi
); gsi_next (&psi
))
1441 gimple phi
= gsi_stmt (psi
);
1442 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
),
1443 PHI_ARG_DEF_FROM_EDGE (phi
, e_eh
));
1450 For every checked indirect/virtual call determine if most common pid of
1451 function/class method has probability more than 50%. If yes modify code of
1456 gimple_ic_transform (gimple_stmt_iterator
*gsi
)
1458 gimple stmt
= gsi_stmt (*gsi
);
1459 histogram_value histogram
;
1460 gcov_type val
, count
, all
, bb_all
;
1461 struct cgraph_node
*direct_call
;
1463 if (gimple_code (stmt
) != GIMPLE_CALL
)
1466 if (gimple_call_fndecl (stmt
) != NULL_TREE
)
1469 if (gimple_call_internal_p (stmt
))
1472 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_INDIR_CALL
);
1476 val
= histogram
->hvalue
.counters
[0];
1477 count
= histogram
->hvalue
.counters
[1];
1478 all
= histogram
->hvalue
.counters
[2];
1480 bb_all
= gimple_bb (stmt
)->count
;
1481 /* The order of CHECK_COUNTER calls is important -
1482 since check_counter can correct the third parameter
1483 and we want to make count <= all <= bb_all. */
1484 if ( check_counter (stmt
, "ic", &all
, &bb_all
, bb_all
)
1485 || check_counter (stmt
, "ic", &count
, &all
, all
))
1487 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1491 if (4 * count
<= 3 * all
)
1494 direct_call
= find_func_by_profile_id ((int)val
);
1496 if (direct_call
== NULL
)
1502 fprintf (dump_file
, "Indirect call -> direct call from other module");
1503 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1504 fprintf (dump_file
, "=> %i (will resolve only with LTO)\n", (int)val
);
1510 if (!check_ic_target (stmt
, direct_call
))
1514 fprintf (dump_file
, "Indirect call -> direct call ");
1515 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1516 fprintf (dump_file
, "=> ");
1517 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1518 fprintf (dump_file
, " transformation skipped because of type mismatch");
1519 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1521 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1527 fprintf (dump_file
, "Indirect call -> direct call ");
1528 print_generic_expr (dump_file
, gimple_call_fn (stmt
), TDF_SLIM
);
1529 fprintf (dump_file
, "=> ");
1530 print_generic_expr (dump_file
, direct_call
->decl
, TDF_SLIM
);
1531 fprintf (dump_file
, " transformation on insn postponned to ipa-profile");
1532 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1533 fprintf (dump_file
, "hist->count "HOST_WIDEST_INT_PRINT_DEC
1534 " hist->all "HOST_WIDEST_INT_PRINT_DEC
"\n", count
, all
);
1540 /* Return true if the stringop CALL with FNDECL shall be profiled.
1541 SIZE_ARG be set to the argument index for the size of the string
1545 interesting_stringop_to_profile_p (tree fndecl
, gimple call
, int *size_arg
)
1547 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
1549 if (fcode
!= BUILT_IN_MEMCPY
&& fcode
!= BUILT_IN_MEMPCPY
1550 && fcode
!= BUILT_IN_MEMSET
&& fcode
!= BUILT_IN_BZERO
)
1555 case BUILT_IN_MEMCPY
:
1556 case BUILT_IN_MEMPCPY
:
1558 return validate_gimple_arglist (call
, POINTER_TYPE
, POINTER_TYPE
,
1559 INTEGER_TYPE
, VOID_TYPE
);
1560 case BUILT_IN_MEMSET
:
1562 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1563 INTEGER_TYPE
, VOID_TYPE
);
1564 case BUILT_IN_BZERO
:
1566 return validate_gimple_arglist (call
, POINTER_TYPE
, INTEGER_TYPE
,
1573 /* Convert stringop (..., vcall_size)
1575 if (vcall_size == icall_size)
1576 stringop (..., icall_size);
1578 stringop (..., vcall_size);
1579 assuming we'll propagate a true constant into ICALL_SIZE later. */
1582 gimple_stringop_fixed_value (gimple vcall_stmt
, tree icall_size
, int prob
,
1583 gcov_type count
, gcov_type all
)
1585 gimple tmp_stmt
, cond_stmt
, icall_stmt
;
1586 tree tmp0
, tmp1
, vcall_size
, optype
;
1587 basic_block cond_bb
, icall_bb
, vcall_bb
, join_bb
;
1588 edge e_ci
, e_cv
, e_iv
, e_ij
, e_vj
;
1589 gimple_stmt_iterator gsi
;
1593 fndecl
= gimple_call_fndecl (vcall_stmt
);
1594 if (!interesting_stringop_to_profile_p (fndecl
, vcall_stmt
, &size_arg
))
1597 cond_bb
= gimple_bb (vcall_stmt
);
1598 gsi
= gsi_for_stmt (vcall_stmt
);
1600 vcall_size
= gimple_call_arg (vcall_stmt
, size_arg
);
1601 optype
= TREE_TYPE (vcall_size
);
1603 tmp0
= make_temp_ssa_name (optype
, NULL
, "PROF");
1604 tmp1
= make_temp_ssa_name (optype
, NULL
, "PROF");
1605 tmp_stmt
= gimple_build_assign (tmp0
, fold_convert (optype
, icall_size
));
1606 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1608 tmp_stmt
= gimple_build_assign (tmp1
, vcall_size
);
1609 gsi_insert_before (&gsi
, tmp_stmt
, GSI_SAME_STMT
);
1611 cond_stmt
= gimple_build_cond (EQ_EXPR
, tmp1
, tmp0
, NULL_TREE
, NULL_TREE
);
1612 gsi_insert_before (&gsi
, cond_stmt
, GSI_SAME_STMT
);
1614 gimple_set_vdef (vcall_stmt
, NULL
);
1615 gimple_set_vuse (vcall_stmt
, NULL
);
1616 update_stmt (vcall_stmt
);
1617 icall_stmt
= gimple_copy (vcall_stmt
);
1618 gimple_call_set_arg (icall_stmt
, size_arg
, icall_size
);
1619 gsi_insert_before (&gsi
, icall_stmt
, GSI_SAME_STMT
);
1622 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1623 e_ci
= split_block (cond_bb
, cond_stmt
);
1624 icall_bb
= e_ci
->dest
;
1625 icall_bb
->count
= count
;
1627 e_iv
= split_block (icall_bb
, icall_stmt
);
1628 vcall_bb
= e_iv
->dest
;
1629 vcall_bb
->count
= all
- count
;
1631 e_vj
= split_block (vcall_bb
, vcall_stmt
);
1632 join_bb
= e_vj
->dest
;
1633 join_bb
->count
= all
;
1635 e_ci
->flags
= (e_ci
->flags
& ~EDGE_FALLTHRU
) | EDGE_TRUE_VALUE
;
1636 e_ci
->probability
= prob
;
1637 e_ci
->count
= count
;
1639 e_cv
= make_edge (cond_bb
, vcall_bb
, EDGE_FALSE_VALUE
);
1640 e_cv
->probability
= REG_BR_PROB_BASE
- prob
;
1641 e_cv
->count
= all
- count
;
1645 e_ij
= make_edge (icall_bb
, join_bb
, EDGE_FALLTHRU
);
1646 e_ij
->probability
= REG_BR_PROB_BASE
;
1647 e_ij
->count
= count
;
1649 e_vj
->probability
= REG_BR_PROB_BASE
;
1650 e_vj
->count
= all
- count
;
1652 /* Insert PHI node for the call result if necessary. */
1653 if (gimple_call_lhs (vcall_stmt
)
1654 && TREE_CODE (gimple_call_lhs (vcall_stmt
)) == SSA_NAME
)
1656 tree result
= gimple_call_lhs (vcall_stmt
);
1657 gimple phi
= create_phi_node (result
, join_bb
);
1658 gimple_call_set_lhs (vcall_stmt
,
1659 duplicate_ssa_name (result
, vcall_stmt
));
1660 add_phi_arg (phi
, gimple_call_lhs (vcall_stmt
), e_vj
, UNKNOWN_LOCATION
);
1661 gimple_call_set_lhs (icall_stmt
,
1662 duplicate_ssa_name (result
, icall_stmt
));
1663 add_phi_arg (phi
, gimple_call_lhs (icall_stmt
), e_ij
, UNKNOWN_LOCATION
);
1666 /* Because these are all string op builtins, they're all nothrow. */
1667 gcc_assert (!stmt_could_throw_p (vcall_stmt
));
1668 gcc_assert (!stmt_could_throw_p (icall_stmt
));
1671 /* Find values inside STMT for that we want to measure histograms for
1672 division/modulo optimization. */
1674 gimple_stringops_transform (gimple_stmt_iterator
*gsi
)
1676 gimple stmt
= gsi_stmt (*gsi
);
1679 enum built_in_function fcode
;
1680 histogram_value histogram
;
1681 gcov_type count
, all
, val
;
1683 unsigned int dest_align
, src_align
;
1688 if (gimple_code (stmt
) != GIMPLE_CALL
)
1690 fndecl
= gimple_call_fndecl (stmt
);
1693 fcode
= DECL_FUNCTION_CODE (fndecl
);
1694 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1697 blck_size
= gimple_call_arg (stmt
, size_arg
);
1698 if (TREE_CODE (blck_size
) == INTEGER_CST
)
1701 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_SINGLE_VALUE
);
1704 val
= histogram
->hvalue
.counters
[0];
1705 count
= histogram
->hvalue
.counters
[1];
1706 all
= histogram
->hvalue
.counters
[2];
1707 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1708 /* We require that count is at least half of all; this means
1709 that for the transformation to fire the value must be constant
1710 at least 80% of time. */
1711 if ((6 * count
/ 5) < all
|| optimize_bb_for_size_p (gimple_bb (stmt
)))
1713 if (check_counter (stmt
, "value", &count
, &all
, gimple_bb (stmt
)->count
))
1716 prob
= GCOV_COMPUTE_SCALE (count
, all
);
1719 dest
= gimple_call_arg (stmt
, 0);
1720 dest_align
= get_pointer_alignment (dest
);
1723 case BUILT_IN_MEMCPY
:
1724 case BUILT_IN_MEMPCPY
:
1725 src
= gimple_call_arg (stmt
, 1);
1726 src_align
= get_pointer_alignment (src
);
1727 if (!can_move_by_pieces (val
, MIN (dest_align
, src_align
)))
1730 case BUILT_IN_MEMSET
:
1731 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1732 gimple_call_arg (stmt
, 1),
1736 case BUILT_IN_BZERO
:
1737 if (!can_store_by_pieces (val
, builtin_memset_read_str
,
1745 tree_val
= build_int_cst_wide (get_gcov_type (),
1746 (unsigned HOST_WIDE_INT
) val
,
1747 val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1);
1750 fprintf (dump_file
, "Single value %i stringop transformation on ",
1752 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1754 gimple_stringop_fixed_value (stmt
, tree_val
, prob
, count
, all
);
1760 stringop_block_profile (gimple stmt
, unsigned int *expected_align
,
1761 HOST_WIDE_INT
*expected_size
)
1763 histogram_value histogram
;
1764 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_AVERAGE
);
1766 *expected_size
= -1;
1767 else if (!histogram
->hvalue
.counters
[1])
1769 *expected_size
= -1;
1770 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1775 size
= ((histogram
->hvalue
.counters
[0]
1776 + histogram
->hvalue
.counters
[1] / 2)
1777 / histogram
->hvalue
.counters
[1]);
1778 /* Even if we can hold bigger value in SIZE, INT_MAX
1779 is safe "infinity" for code generation strategies. */
1782 *expected_size
= size
;
1783 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1785 histogram
= gimple_histogram_value_of_type (cfun
, stmt
, HIST_TYPE_IOR
);
1787 *expected_align
= 0;
1788 else if (!histogram
->hvalue
.counters
[0])
1790 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1791 *expected_align
= 0;
1798 count
= histogram
->hvalue
.counters
[0];
1800 while (!(count
& alignment
)
1801 && (alignment
* 2 * BITS_PER_UNIT
))
1803 *expected_align
= alignment
* BITS_PER_UNIT
;
1804 gimple_remove_histogram_value (cfun
, stmt
, histogram
);
1809 /* Find values inside STMT for that we want to measure histograms for
1810 division/modulo optimization. */
1812 gimple_divmod_values_to_profile (gimple stmt
, histogram_values
*values
)
1814 tree lhs
, divisor
, op0
, type
;
1815 histogram_value hist
;
1817 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
1820 lhs
= gimple_assign_lhs (stmt
);
1821 type
= TREE_TYPE (lhs
);
1822 if (!INTEGRAL_TYPE_P (type
))
1825 switch (gimple_assign_rhs_code (stmt
))
1827 case TRUNC_DIV_EXPR
:
1828 case TRUNC_MOD_EXPR
:
1829 divisor
= gimple_assign_rhs2 (stmt
);
1830 op0
= gimple_assign_rhs1 (stmt
);
1832 values
->reserve (3);
1834 if (TREE_CODE (divisor
) == SSA_NAME
)
1835 /* Check for the case where the divisor is the same value most
1837 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1838 HIST_TYPE_SINGLE_VALUE
,
1841 /* For mod, check whether it is not often a noop (or replaceable by
1842 a few subtractions). */
1843 if (gimple_assign_rhs_code (stmt
) == TRUNC_MOD_EXPR
1844 && TYPE_UNSIGNED (type
))
1847 /* Check for a special case where the divisor is power of 2. */
1848 values
->quick_push (gimple_alloc_histogram_value (cfun
,
1852 val
= build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
1853 hist
= gimple_alloc_histogram_value (cfun
, HIST_TYPE_INTERVAL
,
1855 hist
->hdata
.intvl
.int_start
= 0;
1856 hist
->hdata
.intvl
.steps
= 2;
1857 values
->quick_push (hist
);
1866 /* Find calls inside STMT for that we want to measure histograms for
1867 indirect/virtual call optimization. */
1870 gimple_indirect_call_to_profile (gimple stmt
, histogram_values
*values
)
1874 if (gimple_code (stmt
) != GIMPLE_CALL
1875 || gimple_call_internal_p (stmt
)
1876 || gimple_call_fndecl (stmt
) != NULL_TREE
)
1879 callee
= gimple_call_fn (stmt
);
1881 values
->reserve (3);
1883 values
->quick_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_INDIR_CALL
,
1889 /* Find values inside STMT for that we want to measure histograms for
1890 string operations. */
1892 gimple_stringops_values_to_profile (gimple stmt
, histogram_values
*values
)
1899 if (gimple_code (stmt
) != GIMPLE_CALL
)
1901 fndecl
= gimple_call_fndecl (stmt
);
1905 if (!interesting_stringop_to_profile_p (fndecl
, stmt
, &size_arg
))
1908 dest
= gimple_call_arg (stmt
, 0);
1909 blck_size
= gimple_call_arg (stmt
, size_arg
);
1911 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1913 values
->safe_push (gimple_alloc_histogram_value (cfun
,
1914 HIST_TYPE_SINGLE_VALUE
,
1916 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_AVERAGE
,
1919 if (TREE_CODE (blck_size
) != INTEGER_CST
)
1920 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_IOR
,
1924 /* Find values inside STMT for that we want to measure histograms and adds
1925 them to list VALUES. */
1928 gimple_values_to_profile (gimple stmt
, histogram_values
*values
)
1930 gimple_divmod_values_to_profile (stmt
, values
);
1931 gimple_stringops_values_to_profile (stmt
, values
);
1932 gimple_indirect_call_to_profile (stmt
, values
);
1936 gimple_find_values_to_profile (histogram_values
*values
)
1939 gimple_stmt_iterator gsi
;
1941 histogram_value hist
= NULL
;
1945 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1946 gimple_values_to_profile (gsi_stmt (gsi
), values
);
1948 values
->safe_push (gimple_alloc_histogram_value (cfun
, HIST_TYPE_TIME_PROFILE
, 0, 0));
1950 FOR_EACH_VEC_ELT (*values
, i
, hist
)
1954 case HIST_TYPE_INTERVAL
:
1955 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
1958 case HIST_TYPE_POW2
:
1959 hist
->n_counters
= 2;
1962 case HIST_TYPE_SINGLE_VALUE
:
1963 hist
->n_counters
= 3;
1966 case HIST_TYPE_CONST_DELTA
:
1967 hist
->n_counters
= 4;
1970 case HIST_TYPE_INDIR_CALL
:
1971 hist
->n_counters
= 3;
1974 case HIST_TYPE_TIME_PROFILE
:
1975 hist
->n_counters
= 1;
1978 case HIST_TYPE_AVERAGE
:
1979 hist
->n_counters
= 2;
1983 hist
->n_counters
= 1;
1991 fprintf (dump_file
, "Stmt ");
1992 print_gimple_stmt (dump_file
, hist
->hvalue
.stmt
, 0, TDF_SLIM
);
1993 dump_histogram_value (dump_file
, hist
);