1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2013 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
6 Converted to use trees by Dale Johannesen, Apple Computer.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Generate basic block profile instrumentation and auxiliary files.
25 Tree-based version. See profile.c for overview. */
29 #include "coretypes.h"
33 #include "basic-block.h"
34 #include "diagnostic-core.h"
37 #include "tree-flow.h"
38 #include "tree-pass.h"
39 #include "value-prof.h"
44 static GTY(()) tree gcov_type_node;
45 static GTY(()) tree tree_interval_profiler_fn;
46 static GTY(()) tree tree_pow2_profiler_fn;
47 static GTY(()) tree tree_one_value_profiler_fn;
48 static GTY(()) tree tree_indirect_call_profiler_fn;
49 static GTY(()) tree tree_average_profiler_fn;
50 static GTY(()) tree tree_ior_profiler_fn;
53 static GTY(()) tree ic_void_ptr_var;
54 static GTY(()) tree ic_gcov_type_ptr_var;
55 static GTY(()) tree ptr_void;
57 /* Do initialization work for the edge profiler. */
60 __thread gcov* __gcov_indirect_call_counters; // pointer to actual counter
61 __thread void* __gcov_indirect_call_callee; // actual callee address
64 init_ic_make_global_vars (void)
68 ptr_void = build_pointer_type (void_type_node);
71 = build_decl (UNKNOWN_LOCATION, VAR_DECL,
72 get_identifier ("__gcov_indirect_call_callee"),
74 TREE_STATIC (ic_void_ptr_var) = 1;
75 TREE_PUBLIC (ic_void_ptr_var) = 1;
76 DECL_EXTERNAL (ic_void_ptr_var) = 1;
77 DECL_ARTIFICIAL (ic_void_ptr_var) = 1;
78 DECL_INITIAL (ic_void_ptr_var) = NULL;
80 DECL_TLS_MODEL (ic_void_ptr_var) =
81 decl_default_tls_model (ic_void_ptr_var);
83 varpool_finalize_decl (ic_void_ptr_var);
85 gcov_type_ptr = build_pointer_type (get_gcov_type ());
87 = build_decl (UNKNOWN_LOCATION, VAR_DECL,
88 get_identifier ("__gcov_indirect_call_counters"),
90 TREE_STATIC (ic_gcov_type_ptr_var) = 1;
91 TREE_PUBLIC (ic_gcov_type_ptr_var) = 1;
92 DECL_EXTERNAL (ic_gcov_type_ptr_var) = 1;
93 DECL_ARTIFICIAL (ic_gcov_type_ptr_var) = 1;
94 DECL_INITIAL (ic_gcov_type_ptr_var) = NULL;
96 DECL_TLS_MODEL (ic_gcov_type_ptr_var) =
97 decl_default_tls_model (ic_gcov_type_ptr_var);
99 varpool_finalize_decl (ic_gcov_type_ptr_var);
102 /* Create the type and function decls for the interface with gcov. */
105 gimple_init_edge_profiler (void)
107 tree interval_profiler_fn_type;
108 tree pow2_profiler_fn_type;
109 tree one_value_profiler_fn_type;
111 tree ic_profiler_fn_type;
112 tree average_profiler_fn_type;
116 gcov_type_node = get_gcov_type ();
117 gcov_type_ptr = build_pointer_type (gcov_type_node);
119 /* void (*) (gcov_type *, gcov_type, int, unsigned) */
120 interval_profiler_fn_type
121 = build_function_type_list (void_type_node,
122 gcov_type_ptr, gcov_type_node,
124 unsigned_type_node, NULL_TREE);
125 tree_interval_profiler_fn
126 = build_fn_decl ("__gcov_interval_profiler",
127 interval_profiler_fn_type);
128 TREE_NOTHROW (tree_interval_profiler_fn) = 1;
129 DECL_ATTRIBUTES (tree_interval_profiler_fn)
130 = tree_cons (get_identifier ("leaf"), NULL,
131 DECL_ATTRIBUTES (tree_interval_profiler_fn));
133 /* void (*) (gcov_type *, gcov_type) */
134 pow2_profiler_fn_type
135 = build_function_type_list (void_type_node,
136 gcov_type_ptr, gcov_type_node,
138 tree_pow2_profiler_fn = build_fn_decl ("__gcov_pow2_profiler",
139 pow2_profiler_fn_type);
140 TREE_NOTHROW (tree_pow2_profiler_fn) = 1;
141 DECL_ATTRIBUTES (tree_pow2_profiler_fn)
142 = tree_cons (get_identifier ("leaf"), NULL,
143 DECL_ATTRIBUTES (tree_pow2_profiler_fn));
145 /* void (*) (gcov_type *, gcov_type) */
146 one_value_profiler_fn_type
147 = build_function_type_list (void_type_node,
148 gcov_type_ptr, gcov_type_node,
150 tree_one_value_profiler_fn
151 = build_fn_decl ("__gcov_one_value_profiler",
152 one_value_profiler_fn_type);
153 TREE_NOTHROW (tree_one_value_profiler_fn) = 1;
154 DECL_ATTRIBUTES (tree_one_value_profiler_fn)
155 = tree_cons (get_identifier ("leaf"), NULL,
156 DECL_ATTRIBUTES (tree_one_value_profiler_fn));
158 init_ic_make_global_vars ();
160 /* void (*) (gcov_type, void *) */
162 = build_function_type_list (void_type_node,
166 tree_indirect_call_profiler_fn
167 = build_fn_decl ("__gcov_indirect_call_profiler_v2",
168 ic_profiler_fn_type);
169 TREE_NOTHROW (tree_indirect_call_profiler_fn) = 1;
170 DECL_ATTRIBUTES (tree_indirect_call_profiler_fn)
171 = tree_cons (get_identifier ("leaf"), NULL,
172 DECL_ATTRIBUTES (tree_indirect_call_profiler_fn));
174 /* void (*) (gcov_type *, gcov_type) */
175 average_profiler_fn_type
176 = build_function_type_list (void_type_node,
177 gcov_type_ptr, gcov_type_node, NULL_TREE);
178 tree_average_profiler_fn
179 = build_fn_decl ("__gcov_average_profiler",
180 average_profiler_fn_type);
181 TREE_NOTHROW (tree_average_profiler_fn) = 1;
182 DECL_ATTRIBUTES (tree_average_profiler_fn)
183 = tree_cons (get_identifier ("leaf"), NULL,
184 DECL_ATTRIBUTES (tree_average_profiler_fn));
186 = build_fn_decl ("__gcov_ior_profiler",
187 average_profiler_fn_type);
188 TREE_NOTHROW (tree_ior_profiler_fn) = 1;
189 DECL_ATTRIBUTES (tree_ior_profiler_fn)
190 = tree_cons (get_identifier ("leaf"), NULL,
191 DECL_ATTRIBUTES (tree_ior_profiler_fn));
193 /* LTO streamer needs assembler names. Because we create these decls
194 late, we need to initialize them by hand. */
195 DECL_ASSEMBLER_NAME (tree_interval_profiler_fn);
196 DECL_ASSEMBLER_NAME (tree_pow2_profiler_fn);
197 DECL_ASSEMBLER_NAME (tree_one_value_profiler_fn);
198 DECL_ASSEMBLER_NAME (tree_indirect_call_profiler_fn);
199 DECL_ASSEMBLER_NAME (tree_average_profiler_fn);
200 DECL_ASSEMBLER_NAME (tree_ior_profiler_fn);
204 /* Output instructions as GIMPLE trees to increment the edge
205 execution count, and insert them on E. We rely on
206 gsi_insert_on_edge to preserve the order. */
209 gimple_gen_edge_profiler (int edgeno, edge e)
211 tree ref, one, gcov_type_tmp_var;
212 gimple stmt1, stmt2, stmt3;
214 ref = tree_coverage_counter_ref (GCOV_COUNTER_ARCS, edgeno);
215 one = build_int_cst (gcov_type_node, 1);
216 gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
217 NULL, "PROF_edge_counter");
218 stmt1 = gimple_build_assign (gcov_type_tmp_var, ref);
219 gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
220 NULL, "PROF_edge_counter");
221 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, gcov_type_tmp_var,
222 gimple_assign_lhs (stmt1), one);
223 stmt3 = gimple_build_assign (unshare_expr (ref), gimple_assign_lhs (stmt2));
224 gsi_insert_on_edge (e, stmt1);
225 gsi_insert_on_edge (e, stmt2);
226 gsi_insert_on_edge (e, stmt3);
229 /* Emits code to get VALUE to instrument at GSI, and returns the
230 variable containing the value. */
233 prepare_instrumented_value (gimple_stmt_iterator *gsi, histogram_value value)
235 tree val = value->hvalue.value;
236 if (POINTER_TYPE_P (TREE_TYPE (val)))
237 val = fold_convert (build_nonstandard_integer_type
238 (TYPE_PRECISION (TREE_TYPE (val)), 1), val);
239 return force_gimple_operand_gsi (gsi, fold_convert (gcov_type_node, val),
240 true, NULL_TREE, true, GSI_SAME_STMT);
243 /* Output instructions as GIMPLE trees to increment the interval histogram
244 counter. VALUE is the expression whose value is profiled. TAG is the
245 tag of the section for counters, BASE is offset of the counter position. */
248 gimple_gen_interval_profiler (histogram_value value, unsigned tag, unsigned base)
250 gimple stmt = value->hvalue.stmt;
251 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
252 tree ref = tree_coverage_counter_ref (tag, base), ref_ptr;
255 tree start = build_int_cst_type (integer_type_node,
256 value->hdata.intvl.int_start);
257 tree steps = build_int_cst_type (unsigned_type_node,
258 value->hdata.intvl.steps);
260 ref_ptr = force_gimple_operand_gsi (&gsi,
261 build_addr (ref, current_function_decl),
262 true, NULL_TREE, true, GSI_SAME_STMT);
263 val = prepare_instrumented_value (&gsi, value);
264 call = gimple_build_call (tree_interval_profiler_fn, 4,
265 ref_ptr, val, start, steps);
266 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
269 /* Output instructions as GIMPLE trees to increment the power of two histogram
270 counter. VALUE is the expression whose value is profiled. TAG is the tag
271 of the section for counters, BASE is offset of the counter position. */
274 gimple_gen_pow2_profiler (histogram_value value, unsigned tag, unsigned base)
276 gimple stmt = value->hvalue.stmt;
277 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
278 tree ref_ptr = tree_coverage_counter_addr (tag, base);
282 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
283 true, NULL_TREE, true, GSI_SAME_STMT);
284 val = prepare_instrumented_value (&gsi, value);
285 call = gimple_build_call (tree_pow2_profiler_fn, 2, ref_ptr, val);
286 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
289 /* Output instructions as GIMPLE trees for code to find the most common value.
290 VALUE is the expression whose value is profiled. TAG is the tag of the
291 section for counters, BASE is offset of the counter position. */
294 gimple_gen_one_value_profiler (histogram_value value, unsigned tag, unsigned base)
296 gimple stmt = value->hvalue.stmt;
297 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
298 tree ref_ptr = tree_coverage_counter_addr (tag, base);
302 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
303 true, NULL_TREE, true, GSI_SAME_STMT);
304 val = prepare_instrumented_value (&gsi, value);
305 call = gimple_build_call (tree_one_value_profiler_fn, 2, ref_ptr, val);
306 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
310 /* Output instructions as GIMPLE trees for code to find the most
311 common called function in indirect call.
312 VALUE is the call expression whose indirect callee is profiled.
313 TAG is the tag of the section for counters, BASE is offset of the
317 gimple_gen_ic_profiler (histogram_value value, unsigned tag, unsigned base)
320 gimple stmt1, stmt2, stmt3;
321 gimple stmt = value->hvalue.stmt;
322 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
323 tree ref_ptr = tree_coverage_counter_addr (tag, base);
325 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
326 true, NULL_TREE, true, GSI_SAME_STMT);
330 stmt1: __gcov_indirect_call_counters = get_relevant_counter_ptr ();
331 stmt2: tmp1 = (void *) (indirect call argument value)
332 stmt3: __gcov_indirect_call_callee = tmp1;
335 stmt1 = gimple_build_assign (ic_gcov_type_ptr_var, ref_ptr);
336 tmp1 = make_temp_ssa_name (ptr_void, NULL, "PROF");
337 stmt2 = gimple_build_assign (tmp1, unshare_expr (value->hvalue.value));
338 stmt3 = gimple_build_assign (ic_void_ptr_var, gimple_assign_lhs (stmt2));
340 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
341 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
342 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
346 /* Output instructions as GIMPLE trees for code to find the most
347 common called function in indirect call. Insert instructions at the
348 beginning of every possible called function.
352 gimple_gen_ic_func_profiler (void)
354 struct cgraph_node * c_node = cgraph_get_node (current_function_decl);
355 gimple_stmt_iterator gsi;
357 tree tree_uid, cur_func, void0;
359 if (cgraph_only_called_directly_p (c_node))
362 gimple_init_edge_profiler ();
366 stmt1: __gcov_indirect_call_profiler_v2 (profile_id,
367 ¤t_function_decl)
369 gsi = gsi_after_labels (split_edge (single_succ_edge (ENTRY_BLOCK_PTR)));
371 cur_func = force_gimple_operand_gsi (&gsi,
372 build_addr (current_function_decl,
373 current_function_decl),
375 true, GSI_SAME_STMT);
376 tree_uid = build_int_cst
377 (gcov_type_node, cgraph_get_node (current_function_decl)->profile_id);
378 stmt1 = gimple_build_call (tree_indirect_call_profiler_fn, 2,
380 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
382 /* Set __gcov_indirect_call_callee to 0,
383 so that calls from other modules won't get misattributed
384 to the last caller of the current callee. */
385 void0 = build_int_cst (build_pointer_type (void_type_node), 0);
386 stmt2 = gimple_build_assign (ic_void_ptr_var, void0);
387 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
390 /* Output instructions as GIMPLE trees for code to find the most common value
391 of a difference between two evaluations of an expression.
392 VALUE is the expression whose value is profiled. TAG is the tag of the
393 section for counters, BASE is offset of the counter position. */
396 gimple_gen_const_delta_profiler (histogram_value value ATTRIBUTE_UNUSED,
397 unsigned tag ATTRIBUTE_UNUSED,
398 unsigned base ATTRIBUTE_UNUSED)
400 /* FIXME implement this. */
401 #ifdef ENABLE_CHECKING
402 internal_error ("unimplemented functionality");
407 /* Output instructions as GIMPLE trees to increment the average histogram
408 counter. VALUE is the expression whose value is profiled. TAG is the
409 tag of the section for counters, BASE is offset of the counter position. */
412 gimple_gen_average_profiler (histogram_value value, unsigned tag, unsigned base)
414 gimple stmt = value->hvalue.stmt;
415 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
416 tree ref_ptr = tree_coverage_counter_addr (tag, base);
420 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
422 true, GSI_SAME_STMT);
423 val = prepare_instrumented_value (&gsi, value);
424 call = gimple_build_call (tree_average_profiler_fn, 2, ref_ptr, val);
425 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
428 /* Output instructions as GIMPLE trees to increment the ior histogram
429 counter. VALUE is the expression whose value is profiled. TAG is the
430 tag of the section for counters, BASE is offset of the counter position. */
433 gimple_gen_ior_profiler (histogram_value value, unsigned tag, unsigned base)
435 gimple stmt = value->hvalue.stmt;
436 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
437 tree ref_ptr = tree_coverage_counter_addr (tag, base);
441 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
442 true, NULL_TREE, true, GSI_SAME_STMT);
443 val = prepare_instrumented_value (&gsi, value);
444 call = gimple_build_call (tree_ior_profiler_fn, 2, ref_ptr, val);
445 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
448 /* Profile all functions in the callgraph. */
451 tree_profiling (void)
453 struct cgraph_node *node;
455 /* This is a small-ipa pass that gets called only once, from
456 cgraphunit.c:ipa_passes(). */
457 gcc_assert (cgraph_state == CGRAPH_STATE_IPA_SSA);
459 init_node_map (true);
461 FOR_EACH_DEFINED_FUNCTION (node)
463 if (!gimple_has_body_p (node->symbol.decl))
466 /* Don't profile functions produced for builtin stuff. */
467 if (DECL_SOURCE_LOCATION (node->symbol.decl) == BUILTINS_LOCATION)
470 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
472 /* Local pure-const may imply need to fixup the cfg. */
473 if (execute_fixup_cfg () & TODO_cleanup_cfg)
478 if (! flag_branch_probabilities
479 && flag_profile_values)
480 gimple_gen_ic_func_profiler ();
482 if (flag_branch_probabilities
483 && flag_profile_values
484 && flag_value_profile_transformations)
485 gimple_value_profile_transformations ();
487 /* The above could hose dominator info. Currently there is
488 none coming in, this is a safety valve. It should be
489 easy to adjust it, if and when there is some. */
490 free_dominance_info (CDI_DOMINATORS);
491 free_dominance_info (CDI_POST_DOMINATORS);
495 /* Drop pure/const flags from instrumented functions. */
496 FOR_EACH_DEFINED_FUNCTION (node)
498 if (!gimple_has_body_p (node->symbol.decl)
500 || node->symbol.decl != node->clone_of->symbol.decl))
503 /* Don't profile functions produced for builtin stuff. */
504 if (DECL_SOURCE_LOCATION (node->symbol.decl) == BUILTINS_LOCATION)
507 cgraph_set_const_flag (node, false, false);
508 cgraph_set_pure_flag (node, false, false);
511 /* Update call statements and rebuild the cgraph. */
512 FOR_EACH_DEFINED_FUNCTION (node)
516 if (!gimple_has_body_p (node->symbol.decl)
518 || node->symbol.decl != node->clone_of->symbol.decl))
521 /* Don't profile functions produced for builtin stuff. */
522 if (DECL_SOURCE_LOCATION (node->symbol.decl) == BUILTINS_LOCATION)
525 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
529 gimple_stmt_iterator gsi;
530 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
532 gimple stmt = gsi_stmt (gsi);
533 if (is_gimple_call (stmt))
538 update_ssa (TODO_update_ssa);
540 rebuild_cgraph_edges ();
549 /* When profile instrumentation, use or test coverage shall be performed. */
552 gate_tree_profile_ipa (void)
555 && (flag_branch_probabilities || flag_test_coverage
556 || profile_arc_flag));
561 const pass_data pass_data_ipa_tree_profile =
563 SIMPLE_IPA_PASS, /* type */
564 "profile", /* name */
565 OPTGROUP_NONE, /* optinfo_flags */
567 true, /* has_execute */
568 TV_IPA_PROFILE, /* tv_id */
569 0, /* properties_required */
570 0, /* properties_provided */
571 0, /* properties_destroyed */
572 0, /* todo_flags_start */
573 0, /* todo_flags_finish */
576 class pass_ipa_tree_profile : public simple_ipa_opt_pass
579 pass_ipa_tree_profile(gcc::context *ctxt)
580 : simple_ipa_opt_pass(pass_data_ipa_tree_profile, ctxt)
583 /* opt_pass methods: */
584 bool gate () { return gate_tree_profile_ipa (); }
585 unsigned int execute () { return tree_profiling (); }
587 }; // class pass_ipa_tree_profile
591 simple_ipa_opt_pass *
592 make_pass_ipa_tree_profile (gcc::context *ctxt)
594 return new pass_ipa_tree_profile (ctxt);
597 #include "gt-tree-profile.h"