]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-profile.c
In PR70010, a function is marked with target(no-vsx) to disable VSX code
[thirdparty/gcc.git] / gcc / tree-profile.c
CommitLineData
4ee9c684 1/* Calculate branch probabilities, and basic block execution counts.
fbd26352 2 Copyright (C) 1990-2019 Free Software Foundation, Inc.
4ee9c684 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.
7
8This file is part of GCC.
9
10GCC is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free
8c4c00c1 12Software Foundation; either version 3, or (at your option) any later
4ee9c684 13version.
14
15GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
19
20You should have received a copy of the GNU General Public License
8c4c00c1 21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
4ee9c684 23
24/* Generate basic block profile instrumentation and auxiliary files.
d2971487 25 Tree-based version. See profile.c for overview. */
4ee9c684 26
27#include "config.h"
28#include "system.h"
29#include "coretypes.h"
ad7b10a2 30#include "memmodel.h"
9ef16211 31#include "backend.h"
7c29e30e 32#include "target.h"
9ef16211 33#include "tree.h"
34#include "gimple.h"
7c29e30e 35#include "cfghooks.h"
36#include "tree-pass.h"
9ef16211 37#include "ssa.h"
7c29e30e 38#include "cgraph.h"
4ee9c684 39#include "coverage.h"
7c29e30e 40#include "diagnostic-core.h"
b20a8bb4 41#include "fold-const.h"
9ed99284 42#include "varasm.h"
43#include "tree-nested.h"
a8783bee 44#include "gimplify.h"
dcf1a1ec 45#include "gimple-iterator.h"
e795d6e1 46#include "gimplify-me.h"
073c1fd5 47#include "tree-cfg.h"
073c1fd5 48#include "tree-into-ssa.h"
4ee9c684 49#include "value-prof.h"
1ad3e14c 50#include "profile.h"
424a4a92 51#include "tree-cfgcleanup.h"
b74245ec 52#include "params.h"
30a86690 53#include "stringpool.h"
54#include "attribs.h"
b9078d9f 55#include "tree-pretty-print.h"
604e9a8b 56#include "langhooks.h"
57#include "stor-layout.h"
aed74248 58#include "xregex.h"
4ee9c684 59
d7683f13 60static GTY(()) tree gcov_type_node;
61static GTY(()) tree tree_interval_profiler_fn;
62static GTY(()) tree tree_pow2_profiler_fn;
ac65c341 63static GTY(()) tree tree_topn_values_profiler_fn;
167b550b 64static GTY(()) tree tree_indirect_call_profiler_fn;
162719b3 65static GTY(()) tree tree_average_profiler_fn;
66static GTY(()) tree tree_ior_profiler_fn;
36ffeeaf 67static GTY(()) tree tree_time_profiler_counter;
38fe12e3 68
4b0a9554 69
604e9a8b 70static GTY(()) tree ic_tuple_var;
71static GTY(()) tree ic_tuple_counters_field;
72static GTY(()) tree ic_tuple_callee_field;
167b550b 73
4b0a9554 74/* Do initialization work for the edge profiler. */
75
167b550b 76/* Add code:
fe37be54 77 __thread gcov* __gcov_indirect_call_counters; // pointer to actual counter
38fe12e3 78 __thread void* __gcov_indirect_call_callee; // actual callee address
79 __thread int __gcov_function_counter; // time profiler function counter
167b550b 80*/
81static void
fc49fbc1 82init_ic_make_global_vars (void)
167b550b 83{
36ffeeaf 84 tree gcov_type_ptr;
167b550b 85
604e9a8b 86 gcov_type_ptr = build_pointer_type (get_gcov_type ());
48e1416a 87
604e9a8b 88 tree tuple_type = lang_hooks.types.make_type (RECORD_TYPE);
109cfbe4 89
604e9a8b 90 /* callee */
91 ic_tuple_callee_field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
92 ptr_type_node);
ca13b0e8 93
604e9a8b 94 /* counters */
95 ic_tuple_counters_field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
96 NULL_TREE, gcov_type_ptr);
97 DECL_CHAIN (ic_tuple_counters_field) = ic_tuple_callee_field;
98
99 finish_builtin_struct (tuple_type, "indirect_call_tuple",
100 ic_tuple_counters_field, NULL_TREE);
101
102 ic_tuple_var
ca13b0e8 103 = build_decl (UNKNOWN_LOCATION, VAR_DECL,
5cb221f2 104 get_identifier ("__gcov_indirect_call"), tuple_type);
604e9a8b 105 TREE_PUBLIC (ic_tuple_var) = 1;
106 DECL_ARTIFICIAL (ic_tuple_var) = 1;
107 DECL_INITIAL (ic_tuple_var) = NULL;
108 DECL_EXTERNAL (ic_tuple_var) = 1;
109cfbe4 109 if (targetm.have_tls)
07f113ea 110 set_decl_tls_model (ic_tuple_var, decl_default_tls_model (ic_tuple_var));
167b550b 111}
112
3e7f455b 113/* Create the type and function decls for the interface with gcov. */
114
fc49fbc1 115void
36ffeeaf 116gimple_init_gcov_profiler (void)
4b0a9554 117{
d7683f13 118 tree interval_profiler_fn_type;
119 tree pow2_profiler_fn_type;
ac65c341 120 tree topn_values_profiler_fn_type;
d7683f13 121 tree gcov_type_ptr;
167b550b 122 tree ic_profiler_fn_type;
162719b3 123 tree average_profiler_fn_type;
7132b755 124 const char *profiler_fn_name;
2d2b4107 125 const char *fn_name;
d7683f13 126
127 if (!gcov_type_node)
128 {
2d2b4107 129 const char *fn_suffix
130 = flag_profile_update == PROFILE_UPDATE_ATOMIC ? "_atomic" : "";
131
d7683f13 132 gcov_type_node = get_gcov_type ();
133 gcov_type_ptr = build_pointer_type (gcov_type_node);
134
135 /* void (*) (gcov_type *, gcov_type, int, unsigned) */
136 interval_profiler_fn_type
137 = build_function_type_list (void_type_node,
138 gcov_type_ptr, gcov_type_node,
139 integer_type_node,
140 unsigned_type_node, NULL_TREE);
2d2b4107 141 fn_name = concat ("__gcov_interval_profiler", fn_suffix, NULL);
142 tree_interval_profiler_fn = build_fn_decl (fn_name,
143 interval_profiler_fn_type);
144 free (CONST_CAST (char *, fn_name));
85344eeb 145 TREE_NOTHROW (tree_interval_profiler_fn) = 1;
146 DECL_ATTRIBUTES (tree_interval_profiler_fn)
147 = tree_cons (get_identifier ("leaf"), NULL,
148 DECL_ATTRIBUTES (tree_interval_profiler_fn));
d7683f13 149
150 /* void (*) (gcov_type *, gcov_type) */
151 pow2_profiler_fn_type
152 = build_function_type_list (void_type_node,
153 gcov_type_ptr, gcov_type_node,
154 NULL_TREE);
2d2b4107 155 fn_name = concat ("__gcov_pow2_profiler", fn_suffix, NULL);
156 tree_pow2_profiler_fn = build_fn_decl (fn_name, pow2_profiler_fn_type);
157 free (CONST_CAST (char *, fn_name));
85344eeb 158 TREE_NOTHROW (tree_pow2_profiler_fn) = 1;
159 DECL_ATTRIBUTES (tree_pow2_profiler_fn)
160 = tree_cons (get_identifier ("leaf"), NULL,
161 DECL_ATTRIBUTES (tree_pow2_profiler_fn));
d7683f13 162
163 /* void (*) (gcov_type *, gcov_type) */
ac65c341 164 topn_values_profiler_fn_type
d7683f13 165 = build_function_type_list (void_type_node,
166 gcov_type_ptr, gcov_type_node,
167 NULL_TREE);
ac65c341 168 fn_name = concat ("__gcov_topn_values_profiler", fn_suffix, NULL);
169 tree_topn_values_profiler_fn
170 = build_fn_decl (fn_name, topn_values_profiler_fn_type);
1a15313c 171
ac65c341 172 TREE_NOTHROW (tree_topn_values_profiler_fn) = 1;
173 DECL_ATTRIBUTES (tree_topn_values_profiler_fn)
85344eeb 174 = tree_cons (get_identifier ("leaf"), NULL,
ac65c341 175 DECL_ATTRIBUTES (tree_topn_values_profiler_fn));
167b550b 176
fc49fbc1 177 init_ic_make_global_vars ();
48e1416a 178
ca13b0e8 179 /* void (*) (gcov_type, void *) */
180 ic_profiler_fn_type
181 = build_function_type_list (void_type_node,
182 gcov_type_node,
604e9a8b 183 ptr_type_node,
ca13b0e8 184 NULL_TREE);
1a15313c 185 profiler_fn_name = "__gcov_indirect_call_profiler_v4";
7132b755 186
ca13b0e8 187 tree_indirect_call_profiler_fn
7132b755 188 = build_fn_decl (profiler_fn_name, ic_profiler_fn_type);
ca13b0e8 189
85344eeb 190 TREE_NOTHROW (tree_indirect_call_profiler_fn) = 1;
191 DECL_ATTRIBUTES (tree_indirect_call_profiler_fn)
192 = tree_cons (get_identifier ("leaf"), NULL,
193 DECL_ATTRIBUTES (tree_indirect_call_profiler_fn));
194
36ffeeaf 195 tree_time_profiler_counter
196 = build_decl (UNKNOWN_LOCATION, VAR_DECL,
197 get_identifier ("__gcov_time_profiler_counter"),
198 get_gcov_type ());
199 TREE_PUBLIC (tree_time_profiler_counter) = 1;
200 DECL_EXTERNAL (tree_time_profiler_counter) = 1;
201 TREE_STATIC (tree_time_profiler_counter) = 1;
202 DECL_ARTIFICIAL (tree_time_profiler_counter) = 1;
203 DECL_INITIAL (tree_time_profiler_counter) = NULL;
204
162719b3 205 /* void (*) (gcov_type *, gcov_type) */
206 average_profiler_fn_type
207 = build_function_type_list (void_type_node,
208 gcov_type_ptr, gcov_type_node, NULL_TREE);
2d2b4107 209 fn_name = concat ("__gcov_average_profiler", fn_suffix, NULL);
210 tree_average_profiler_fn = build_fn_decl (fn_name,
211 average_profiler_fn_type);
212 free (CONST_CAST (char *, fn_name));
85344eeb 213 TREE_NOTHROW (tree_average_profiler_fn) = 1;
214 DECL_ATTRIBUTES (tree_average_profiler_fn)
215 = tree_cons (get_identifier ("leaf"), NULL,
216 DECL_ATTRIBUTES (tree_average_profiler_fn));
2d2b4107 217 fn_name = concat ("__gcov_ior_profiler", fn_suffix, NULL);
218 tree_ior_profiler_fn = build_fn_decl (fn_name, average_profiler_fn_type);
219 free (CONST_CAST (char *, fn_name));
85344eeb 220 TREE_NOTHROW (tree_ior_profiler_fn) = 1;
221 DECL_ATTRIBUTES (tree_ior_profiler_fn)
222 = tree_cons (get_identifier ("leaf"), NULL,
223 DECL_ATTRIBUTES (tree_ior_profiler_fn));
224
6c0782b1 225 /* LTO streamer needs assembler names. Because we create these decls
226 late, we need to initialize them by hand. */
227 DECL_ASSEMBLER_NAME (tree_interval_profiler_fn);
228 DECL_ASSEMBLER_NAME (tree_pow2_profiler_fn);
ac65c341 229 DECL_ASSEMBLER_NAME (tree_topn_values_profiler_fn);
6c0782b1 230 DECL_ASSEMBLER_NAME (tree_indirect_call_profiler_fn);
231 DECL_ASSEMBLER_NAME (tree_average_profiler_fn);
232 DECL_ASSEMBLER_NAME (tree_ior_profiler_fn);
d7683f13 233 }
4b0a9554 234}
235
48e1416a 236/* Output instructions as GIMPLE trees to increment the edge
237 execution count, and insert them on E. We rely on
75a70cf9 238 gsi_insert_on_edge to preserve the order. */
4ee9c684 239
fc49fbc1 240void
241gimple_gen_edge_profiler (int edgeno, edge e)
4ee9c684 242{
7132b755 243 tree one;
f81207a7 244
f81207a7 245 one = build_int_cst (gcov_type_node, 1);
7132b755 246
247 if (flag_profile_update == PROFILE_UPDATE_ATOMIC)
248 {
249 /* __atomic_fetch_add (&counter, 1, MEMMODEL_RELAXED); */
250 tree addr = tree_coverage_counter_addr (GCOV_COUNTER_ARCS, edgeno);
b277c58e 251 tree f = builtin_decl_explicit (LONG_LONG_TYPE_SIZE > 32
252 ? BUILT_IN_ATOMIC_FETCH_ADD_8:
253 BUILT_IN_ATOMIC_FETCH_ADD_4);
254 gcall *stmt = gimple_build_call (f, 3, addr, one,
255 build_int_cst (integer_type_node,
256 MEMMODEL_RELAXED));
7132b755 257 gsi_insert_on_edge (e, stmt);
258 }
259 else
260 {
261 tree ref = tree_coverage_counter_ref (GCOV_COUNTER_ARCS, edgeno);
262 tree gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
263 NULL, "PROF_edge_counter");
264 gassign *stmt1 = gimple_build_assign (gcov_type_tmp_var, ref);
265 gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
266 NULL, "PROF_edge_counter");
267 gassign *stmt2 = gimple_build_assign (gcov_type_tmp_var, PLUS_EXPR,
268 gimple_assign_lhs (stmt1), one);
269 gassign *stmt3 = gimple_build_assign (unshare_expr (ref),
270 gimple_assign_lhs (stmt2));
271 gsi_insert_on_edge (e, stmt1);
272 gsi_insert_on_edge (e, stmt2);
273 gsi_insert_on_edge (e, stmt3);
274 }
4ee9c684 275}
276
75a70cf9 277/* Emits code to get VALUE to instrument at GSI, and returns the
d7683f13 278 variable containing the value. */
279
280static tree
75a70cf9 281prepare_instrumented_value (gimple_stmt_iterator *gsi, histogram_value value)
d7683f13 282{
ed4294da 283 tree val = value->hvalue.value;
c821ef7d 284 if (POINTER_TYPE_P (TREE_TYPE (val)))
a0553bff 285 val = fold_convert (build_nonstandard_integer_type
286 (TYPE_PRECISION (TREE_TYPE (val)), 1), val);
75a70cf9 287 return force_gimple_operand_gsi (gsi, fold_convert (gcov_type_node, val),
288 true, NULL_TREE, true, GSI_SAME_STMT);
d7683f13 289}
290
48e1416a 291/* Output instructions as GIMPLE trees to increment the interval histogram
292 counter. VALUE is the expression whose value is profiled. TAG is the
4ee9c684 293 tag of the section for counters, BASE is offset of the counter position. */
294
fc49fbc1 295void
4ad5dc1e 296gimple_gen_interval_profiler (histogram_value value, unsigned tag)
4ee9c684 297{
42acab1c 298 gimple *stmt = value->hvalue.stmt;
75a70cf9 299 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4ad5dc1e 300 tree ref = tree_coverage_counter_ref (tag, 0), ref_ptr;
1a91d914 301 gcall *call;
75a70cf9 302 tree val;
303 tree start = build_int_cst_type (integer_type_node,
304 value->hdata.intvl.int_start);
305 tree steps = build_int_cst_type (unsigned_type_node,
306 value->hdata.intvl.steps);
48e1416a 307
75a70cf9 308 ref_ptr = force_gimple_operand_gsi (&gsi,
0e49e441 309 build_addr (ref),
75a70cf9 310 true, NULL_TREE, true, GSI_SAME_STMT);
311 val = prepare_instrumented_value (&gsi, value);
312 call = gimple_build_call (tree_interval_profiler_fn, 4,
313 ref_ptr, val, start, steps);
77fca8b5 314 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
4ee9c684 315}
316
48e1416a 317/* Output instructions as GIMPLE trees to increment the power of two histogram
318 counter. VALUE is the expression whose value is profiled. TAG is the tag
4ad5dc1e 319 of the section for counters. */
4ee9c684 320
fc49fbc1 321void
4ad5dc1e 322gimple_gen_pow2_profiler (histogram_value value, unsigned tag)
4ee9c684 323{
42acab1c 324 gimple *stmt = value->hvalue.stmt;
75a70cf9 325 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4ad5dc1e 326 tree ref_ptr = tree_coverage_counter_addr (tag, 0);
1a91d914 327 gcall *call;
75a70cf9 328 tree val;
48e1416a 329
75a70cf9 330 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
331 true, NULL_TREE, true, GSI_SAME_STMT);
332 val = prepare_instrumented_value (&gsi, value);
333 call = gimple_build_call (tree_pow2_profiler_fn, 2, ref_ptr, val);
77fca8b5 334 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
4ee9c684 335}
336
ac65c341 337/* Output instructions as GIMPLE trees for code to find the most N common
338 values. VALUE is the expression whose value is profiled. TAG is the tag
4ad5dc1e 339 of the section for counters. */
4ee9c684 340
fc49fbc1 341void
4ad5dc1e 342gimple_gen_topn_values_profiler (histogram_value value, unsigned tag)
4ee9c684 343{
42acab1c 344 gimple *stmt = value->hvalue.stmt;
75a70cf9 345 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4ad5dc1e 346 tree ref_ptr = tree_coverage_counter_addr (tag, 0);
1a91d914 347 gcall *call;
75a70cf9 348 tree val;
48e1416a 349
75a70cf9 350 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
351 true, NULL_TREE, true, GSI_SAME_STMT);
352 val = prepare_instrumented_value (&gsi, value);
ac65c341 353 call = gimple_build_call (tree_topn_values_profiler_fn, 2, ref_ptr, val);
77fca8b5 354 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
4ee9c684 355}
356
167b550b 357
358/* Output instructions as GIMPLE trees for code to find the most
48e1416a 359 common called function in indirect call.
0d424440 360 VALUE is the call expression whose indirect callee is profiled.
4ad5dc1e 361 TAG is the tag of the section for counters. */
167b550b 362
fc49fbc1 363void
4ad5dc1e 364gimple_gen_ic_profiler (histogram_value value, unsigned tag)
167b550b 365{
75a70cf9 366 tree tmp1;
1a91d914 367 gassign *stmt1, *stmt2, *stmt3;
42acab1c 368 gimple *stmt = value->hvalue.stmt;
75a70cf9 369 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4ad5dc1e 370 tree ref_ptr = tree_coverage_counter_addr (tag, 0);
167b550b 371
75a70cf9 372 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
373 true, NULL_TREE, true, GSI_SAME_STMT);
167b550b 374
375 /* Insert code:
48e1416a 376
604e9a8b 377 stmt1: __gcov_indirect_call.counters = get_relevant_counter_ptr ();
3e7f455b 378 stmt2: tmp1 = (void *) (indirect call argument value)
604e9a8b 379 stmt3: __gcov_indirect_call.callee = tmp1;
696ee0c7 380
381 Example:
382 f_1 = foo;
604e9a8b 383 __gcov_indirect_call.counters = &__gcov4.main[0];
696ee0c7 384 PROF_9 = f_1;
385 __gcov_indirect_call_callee = PROF_9;
386 _4 = f_1 ();
167b550b 387 */
388
604e9a8b 389 tree gcov_type_ptr = build_pointer_type (get_gcov_type ());
390
391 tree counter_ref = build3 (COMPONENT_REF, gcov_type_ptr,
392 ic_tuple_var, ic_tuple_counters_field, NULL_TREE);
393
394 stmt1 = gimple_build_assign (counter_ref, ref_ptr);
395 tmp1 = make_temp_ssa_name (ptr_type_node, NULL, "PROF");
75a70cf9 396 stmt2 = gimple_build_assign (tmp1, unshare_expr (value->hvalue.value));
604e9a8b 397 tree callee_ref = build3 (COMPONENT_REF, ptr_type_node,
398 ic_tuple_var, ic_tuple_callee_field, NULL_TREE);
399 stmt3 = gimple_build_assign (callee_ref, tmp1);
167b550b 400
75a70cf9 401 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
402 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
403 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
167b550b 404}
405
406
407/* Output instructions as GIMPLE trees for code to find the most
408 common called function in indirect call. Insert instructions at the
0d424440 409 beginning of every possible called function.
167b550b 410 */
411
fc49fbc1 412void
413gimple_gen_ic_func_profiler (void)
167b550b 414{
415d1b9a 415 struct cgraph_node * c_node = cgraph_node::get (current_function_decl);
1a91d914 416 gcall *stmt1;
fe37be54 417 tree tree_uid, cur_func, void0;
167b550b 418
415d1b9a 419 if (c_node->only_called_directly_p ())
6329636b 420 return;
48e1416a 421
36ffeeaf 422 gimple_init_gcov_profiler ();
48e1416a 423
696ee0c7 424 basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
425 basic_block cond_bb = split_edge (single_succ_edge (entry));
426 basic_block update_bb = split_edge (single_succ_edge (cond_bb));
427
d43192fe 428 /* We need to do an extra split in order to not create an input
429 for a possible PHI node. */
430 split_edge (single_succ_edge (update_bb));
431
696ee0c7 432 edge true_edge = single_succ_edge (cond_bb);
433 true_edge->flags = EDGE_TRUE_VALUE;
434
720cfc43 435 profile_probability probability;
696ee0c7 436 if (DECL_VIRTUAL_P (current_function_decl))
720cfc43 437 probability = profile_probability::very_likely ();
696ee0c7 438 else
720cfc43 439 probability = profile_probability::unlikely ();
696ee0c7 440
441 true_edge->probability = probability;
442 edge e = make_edge (cond_bb, single_succ_edge (update_bb)->dest,
443 EDGE_FALSE_VALUE);
720cfc43 444 e->probability = true_edge->probability.invert ();
696ee0c7 445
3e7f455b 446 /* Insert code:
447
696ee0c7 448 if (__gcov_indirect_call_callee != NULL)
c099f1dd 449 __gcov_indirect_call_profiler_v3 (profile_id, &current_function_decl);
696ee0c7 450
c099f1dd 451 The function __gcov_indirect_call_profiler_v3 is responsible for
696ee0c7 452 resetting __gcov_indirect_call_callee to NULL. */
453
454 gimple_stmt_iterator gsi = gsi_start_bb (cond_bb);
604e9a8b 455 void0 = build_int_cst (ptr_type_node, 0);
456
457 tree callee_ref = build3 (COMPONENT_REF, ptr_type_node,
458 ic_tuple_var, ic_tuple_callee_field, NULL_TREE);
696ee0c7 459
604e9a8b 460 tree ref = force_gimple_operand_gsi (&gsi, callee_ref, true, NULL_TREE,
696ee0c7 461 true, GSI_SAME_STMT);
462
463 gcond *cond = gimple_build_cond (NE_EXPR, ref,
464 void0, NULL, NULL);
465 gsi_insert_before (&gsi, cond, GSI_NEW_STMT);
466
467 gsi = gsi_after_labels (update_bb);
85344eeb 468
469 cur_func = force_gimple_operand_gsi (&gsi,
0e49e441 470 build_addr (current_function_decl),
85344eeb 471 true, NULL_TREE,
472 true, GSI_SAME_STMT);
fe37be54 473 tree_uid = build_int_cst
ca13b0e8 474 (gcov_type_node,
475 cgraph_node::get (current_function_decl)->profile_id);
476 stmt1 = gimple_build_call (tree_indirect_call_profiler_fn, 2,
477 tree_uid, cur_func);
85344eeb 478 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
167b550b 479}
480
38fe12e3 481/* Output instructions as GIMPLE tree at the beginning for each function.
482 TAG is the tag of the section for counters, BASE is offset of the
483 counter position and GSI is the iterator we place the counter. */
484
485void
4ad5dc1e 486gimple_gen_time_profiler (unsigned tag)
38fe12e3 487{
36ffeeaf 488 tree type = get_gcov_type ();
3f3e14c0 489 basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
490 basic_block cond_bb = split_edge (single_succ_edge (entry));
36ffeeaf 491 basic_block update_bb = split_edge (single_succ_edge (cond_bb));
492
d43192fe 493 /* We need to do an extra split in order to not create an input
494 for a possible PHI node. */
495 split_edge (single_succ_edge (update_bb));
496
36ffeeaf 497 edge true_edge = single_succ_edge (cond_bb);
498 true_edge->flags = EDGE_TRUE_VALUE;
720cfc43 499 true_edge->probability = profile_probability::unlikely ();
36ffeeaf 500 edge e
501 = make_edge (cond_bb, single_succ_edge (update_bb)->dest, EDGE_FALSE_VALUE);
720cfc43 502 e->probability = true_edge->probability.invert ();
36ffeeaf 503
504 gimple_stmt_iterator gsi = gsi_start_bb (cond_bb);
4ad5dc1e 505 tree original_ref = tree_coverage_counter_ref (tag, 0);
36ffeeaf 506 tree ref = force_gimple_operand_gsi (&gsi, original_ref, true, NULL_TREE,
507 true, GSI_SAME_STMT);
508 tree one = build_int_cst (type, 1);
38fe12e3 509
36ffeeaf 510 /* Emit: if (counters[0] != 0). */
511 gcond *cond = gimple_build_cond (EQ_EXPR, ref, build_int_cst (type, 0),
512 NULL, NULL);
513 gsi_insert_before (&gsi, cond, GSI_NEW_STMT);
514
515 gsi = gsi_start_bb (update_bb);
516
517 /* Emit: counters[0] = ++__gcov_time_profiler_counter. */
518 if (flag_profile_update == PROFILE_UPDATE_ATOMIC)
519 {
32837f2d 520 tree ptr = make_temp_ssa_name (build_pointer_type (type), NULL,
521 "time_profiler_counter_ptr");
522 tree addr = build1 (ADDR_EXPR, TREE_TYPE (ptr),
36ffeeaf 523 tree_time_profiler_counter);
524 gassign *assign = gimple_build_assign (ptr, NOP_EXPR, addr);
525 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
526 tree f = builtin_decl_explicit (LONG_LONG_TYPE_SIZE > 32
527 ? BUILT_IN_ATOMIC_ADD_FETCH_8:
528 BUILT_IN_ATOMIC_ADD_FETCH_4);
529 gcall *stmt = gimple_build_call (f, 3, ptr, one,
530 build_int_cst (integer_type_node,
531 MEMMODEL_RELAXED));
532 tree result_type = TREE_TYPE (TREE_TYPE (f));
533 tree tmp = make_temp_ssa_name (result_type, NULL, "time_profile");
534 gimple_set_lhs (stmt, tmp);
535 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
536 tmp = make_temp_ssa_name (type, NULL, "time_profile");
537 assign = gimple_build_assign (tmp, NOP_EXPR,
538 gimple_call_lhs (stmt));
539 gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
540 assign = gimple_build_assign (original_ref, tmp);
541 gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
542 }
543 else
544 {
545 tree tmp = make_temp_ssa_name (type, NULL, "time_profile");
546 gassign *assign = gimple_build_assign (tmp, tree_time_profiler_counter);
547 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
548
549 tmp = make_temp_ssa_name (type, NULL, "time_profile");
550 assign = gimple_build_assign (tmp, PLUS_EXPR, gimple_assign_lhs (assign),
551 one);
552 gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
553 assign = gimple_build_assign (original_ref, tmp);
554 gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
555 assign = gimple_build_assign (tree_time_profiler_counter, tmp);
556 gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
557 }
38fe12e3 558}
559
48e1416a 560/* Output instructions as GIMPLE trees to increment the average histogram
561 counter. VALUE is the expression whose value is profiled. TAG is the
162719b3 562 tag of the section for counters, BASE is offset of the counter position. */
563
fc49fbc1 564void
4ad5dc1e 565gimple_gen_average_profiler (histogram_value value, unsigned tag)
162719b3 566{
42acab1c 567 gimple *stmt = value->hvalue.stmt;
75a70cf9 568 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4ad5dc1e 569 tree ref_ptr = tree_coverage_counter_addr (tag, 0);
1a91d914 570 gcall *call;
75a70cf9 571 tree val;
48e1416a 572
75a70cf9 573 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
0d734975 574 true, NULL_TREE,
75a70cf9 575 true, GSI_SAME_STMT);
576 val = prepare_instrumented_value (&gsi, value);
577 call = gimple_build_call (tree_average_profiler_fn, 2, ref_ptr, val);
77fca8b5 578 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
162719b3 579}
580
48e1416a 581/* Output instructions as GIMPLE trees to increment the ior histogram
582 counter. VALUE is the expression whose value is profiled. TAG is the
162719b3 583 tag of the section for counters, BASE is offset of the counter position. */
584
fc49fbc1 585void
4ad5dc1e 586gimple_gen_ior_profiler (histogram_value value, unsigned tag)
162719b3 587{
42acab1c 588 gimple *stmt = value->hvalue.stmt;
75a70cf9 589 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4ad5dc1e 590 tree ref_ptr = tree_coverage_counter_addr (tag, 0);
1a91d914 591 gcall *call;
75a70cf9 592 tree val;
48e1416a 593
75a70cf9 594 ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
595 true, NULL_TREE, true, GSI_SAME_STMT);
596 val = prepare_instrumented_value (&gsi, value);
597 call = gimple_build_call (tree_ior_profiler_fn, 2, ref_ptr, val);
77fca8b5 598 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
162719b3 599}
600
aed74248 601static vec<regex_t> profile_filter_files;
602static vec<regex_t> profile_exclude_files;
603
604/* Parse list of provided REGEX (separated with semi-collon) and
605 create expressions (of type regex_t) and save them into V vector.
606 If there is a regular expression parsing error, error message is
607 printed for FLAG_NAME. */
608
609static void
610parse_profile_filter (const char *regex, vec<regex_t> *v,
611 const char *flag_name)
612{
613 v->create (4);
614 if (regex != NULL)
615 {
616 char *str = xstrdup (regex);
617 for (char *p = strtok (str, ";"); p != NULL; p = strtok (NULL, ";"))
618 {
619 regex_t r;
620 if (regcomp (&r, p, REG_EXTENDED | REG_NOSUB) != 0)
621 {
a0b58a70 622 error ("invalid regular expression %qs in %qs",
aed74248 623 p, flag_name);
624 return;
625 }
626
627 v->safe_push (r);
628 }
629 }
630}
631
632/* Parse values of -fprofile-filter-files and -fprofile-exclude-files
633 options. */
634
635static void
636parse_profile_file_filtering ()
637{
638 parse_profile_filter (flag_profile_filter_files, &profile_filter_files,
639 "-fprofile-filter-files");
640 parse_profile_filter (flag_profile_exclude_files, &profile_exclude_files,
641 "-fprofile-exclude-files");
642}
643
644/* Parse vectors of regular expressions. */
645
646static void
647release_profile_file_filtering ()
648{
649 profile_filter_files.release ();
650 profile_exclude_files.release ();
651}
652
653/* Return true when FILENAME should be instrumented based on
654 -fprofile-filter-files and -fprofile-exclude-files options. */
655
656static bool
657include_source_file_for_profile (const char *filename)
658{
659 /* First check whether file is included in flag_profile_exclude_files. */
660 for (unsigned i = 0; i < profile_exclude_files.length (); i++)
661 if (regexec (&profile_exclude_files[i],
662 filename, 0, NULL, 0) == REG_NOERROR)
663 return false;
664
665 /* For non-empty flag_profile_filter_files include only files matching a
666 regex in the flag. */
667 if (profile_filter_files.is_empty ())
668 return true;
669
670 for (unsigned i = 0; i < profile_filter_files.length (); i++)
671 if (regexec (&profile_filter_files[i], filename, 0, NULL, 0) == REG_NOERROR)
672 return true;
673
674 return false;
675}
676
4c790ba8 677#ifndef HAVE_sync_compare_and_swapsi
678#define HAVE_sync_compare_and_swapsi 0
679#endif
680#ifndef HAVE_atomic_compare_and_swapsi
681#define HAVE_atomic_compare_and_swapsi 0
682#endif
683
684#ifndef HAVE_sync_compare_and_swapdi
685#define HAVE_sync_compare_and_swapdi 0
686#endif
687#ifndef HAVE_atomic_compare_and_swapdi
688#define HAVE_atomic_compare_and_swapdi 0
689#endif
690
85344eeb 691/* Profile all functions in the callgraph. */
4ee9c684 692
2a1990e9 693static unsigned int
d2971487 694tree_profiling (void)
695{
85344eeb 696 struct cgraph_node *node;
697
4c790ba8 698 /* Verify whether we can utilize atomic update operations. */
3af33bcf 699 bool can_support_atomic = false;
700 unsigned HOST_WIDE_INT gcov_type_size
701 = tree_to_uhwi (TYPE_SIZE_UNIT (get_gcov_type ()));
702 if (gcov_type_size == 4)
703 can_support_atomic
704 = HAVE_sync_compare_and_swapsi || HAVE_atomic_compare_and_swapsi;
705 else if (gcov_type_size == 8)
706 can_support_atomic
707 = HAVE_sync_compare_and_swapdi || HAVE_atomic_compare_and_swapdi;
708
709 if (flag_profile_update == PROFILE_UPDATE_ATOMIC
710 && !can_support_atomic)
4c790ba8 711 {
3af33bcf 712 warning (0, "target does not support atomic profile update, "
713 "single mode is selected");
714 flag_profile_update = PROFILE_UPDATE_SINGLE;
4c790ba8 715 }
3af33bcf 716 else if (flag_profile_update == PROFILE_UPDATE_PREFER_ATOMIC)
717 flag_profile_update = can_support_atomic
718 ? PROFILE_UPDATE_ATOMIC : PROFILE_UPDATE_SINGLE;
4c790ba8 719
3e7f455b 720 /* This is a small-ipa pass that gets called only once, from
721 cgraphunit.c:ipa_passes(). */
35ee1c66 722 gcc_assert (symtab->state == IPA_SSA);
f81207a7 723
fe37be54 724 init_node_map (true);
aed74248 725 parse_profile_file_filtering ();
1ad3e14c 726
7c455d87 727 FOR_EACH_DEFINED_FUNCTION (node)
85344eeb 728 {
e9780462 729 bool thunk = false;
730 if (!gimple_has_body_p (node->decl) && !node->thunk.thunk_p)
85344eeb 731 continue;
732
733 /* Don't profile functions produced for builtin stuff. */
02774f2d 734 if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
85344eeb 735 continue;
736
595e387a 737 if (lookup_attribute ("no_profile_instrument_function",
738 DECL_ATTRIBUTES (node->decl)))
739 continue;
1a382068 740 /* Do not instrument extern inline functions when testing coverage.
741 While this is not perfectly consistent (early inlined extern inlines
742 will get acocunted), testsuite expects that. */
743 if (DECL_EXTERNAL (node->decl)
744 && flag_test_coverage)
745 continue;
746
aed74248 747 const char *file = LOCATION_FILE (DECL_SOURCE_LOCATION (node->decl));
748 if (!include_source_file_for_profile (file))
749 continue;
750
e9780462 751 if (node->thunk.thunk_p)
752 {
f4d3c071 753 /* We cannot expand variadic thunks to Gimple. */
e9780462 754 if (stdarg_p (TREE_TYPE (node->decl)))
755 continue;
756 thunk = true;
757 /* When generate profile, expand thunk to gimple so it can be
758 instrumented same way as other functions. */
759 if (profile_arc_flag)
760 node->expand_thunk (false, true);
761 /* Read cgraph profile but keep function as thunk at profile-use
762 time. */
763 else
764 {
765 read_thunk_profile (node);
766 continue;
767 }
768 }
769
02774f2d 770 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
85344eeb 771
b9078d9f 772 if (dump_file)
773 dump_function_header (dump_file, cfun->decl, dump_flags);
774
8c1fce46 775 /* Local pure-const may imply need to fixup the cfg. */
e9780462 776 if (gimple_has_body_p (node->decl)
777 && (execute_fixup_cfg () & TODO_cleanup_cfg))
141de90e 778 cleanup_tree_cfg ();
3e7f455b 779
e9780462 780 branch_prob (thunk);
85344eeb 781
782 if (! flag_branch_probabilities
783 && flag_profile_values)
fc49fbc1 784 gimple_gen_ic_func_profiler ();
85344eeb 785
786 if (flag_branch_probabilities
e9780462 787 && !thunk
85344eeb 788 && flag_profile_values
789 && flag_value_profile_transformations)
fc49fbc1 790 gimple_value_profile_transformations ();
85344eeb 791
792 /* The above could hose dominator info. Currently there is
793 none coming in, this is a safety valve. It should be
794 easy to adjust it, if and when there is some. */
795 free_dominance_info (CDI_DOMINATORS);
796 free_dominance_info (CDI_POST_DOMINATORS);
85344eeb 797 pop_cfun ();
798 }
799
aed74248 800 release_profile_file_filtering ();
801
85344eeb 802 /* Drop pure/const flags from instrumented functions. */
1059fe86 803 if (profile_arc_flag || flag_test_coverage)
804 FOR_EACH_DEFINED_FUNCTION (node)
805 {
806 if (!gimple_has_body_p (node->decl)
807 || !(!node->clone_of
808 || node->decl != node->clone_of->decl))
809 continue;
810
811 /* Don't profile functions produced for builtin stuff. */
812 if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
813 continue;
814
815 node->set_const_flag (false, false);
816 node->set_pure_flag (false, false);
817 }
85344eeb 818
819 /* Update call statements and rebuild the cgraph. */
7c455d87 820 FOR_EACH_DEFINED_FUNCTION (node)
85344eeb 821 {
822 basic_block bb;
823
02774f2d 824 if (!gimple_has_body_p (node->decl)
7d0d0ce1 825 || !(!node->clone_of
02774f2d 826 || node->decl != node->clone_of->decl))
85344eeb 827 continue;
828
829 /* Don't profile functions produced for builtin stuff. */
02774f2d 830 if (DECL_SOURCE_LOCATION (node->decl) == BUILTINS_LOCATION)
85344eeb 831 continue;
832
02774f2d 833 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
85344eeb 834
fc00614f 835 FOR_EACH_BB_FN (bb, cfun)
85344eeb 836 {
837 gimple_stmt_iterator gsi;
838 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
839 {
42acab1c 840 gimple *stmt = gsi_stmt (gsi);
85344eeb 841 if (is_gimple_call (stmt))
842 update_stmt (stmt);
843 }
844 }
845
2701a3fd 846 /* re-merge split blocks. */
847 cleanup_tree_cfg ();
85344eeb 848 update_ssa (TODO_update_ssa);
849
35ee1c66 850 cgraph_edge::rebuild_edges ();
85344eeb 851
85344eeb 852 pop_cfun ();
853 }
fdc47e9a 854
38a65d4e 855 handle_missing_profiles ();
856
9af5ce0c 857 del_node_map ();
2a1990e9 858 return 0;
d2971487 859}
860
cbe8bda8 861namespace {
862
863const pass_data pass_data_ipa_tree_profile =
4ee9c684 864{
cbe8bda8 865 SIMPLE_IPA_PASS, /* type */
866 "profile", /* name */
867 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 868 TV_IPA_PROFILE, /* tv_id */
869 0, /* properties_required */
870 0, /* properties_provided */
871 0, /* properties_destroyed */
872 0, /* todo_flags_start */
1059fe86 873 TODO_dump_symtab, /* todo_flags_finish */
4ee9c684 874};
875
cbe8bda8 876class pass_ipa_tree_profile : public simple_ipa_opt_pass
877{
878public:
9af5ce0c 879 pass_ipa_tree_profile (gcc::context *ctxt)
880 : simple_ipa_opt_pass (pass_data_ipa_tree_profile, ctxt)
cbe8bda8 881 {}
882
883 /* opt_pass methods: */
31315c24 884 virtual bool gate (function *);
65b0537f 885 virtual unsigned int execute (function *) { return tree_profiling (); }
cbe8bda8 886
887}; // class pass_ipa_tree_profile
888
31315c24 889bool
890pass_ipa_tree_profile::gate (function *)
891{
94bed7c3 892 /* When profile instrumentation, use or test coverage shall be performed.
893 But for AutoFDO, this there is no instrumentation, thus this pass is
894 diabled. */
895 return (!in_lto_p && !flag_auto_profile
31315c24 896 && (flag_branch_probabilities || flag_test_coverage
897 || profile_arc_flag));
898}
899
cbe8bda8 900} // anon namespace
901
902simple_ipa_opt_pass *
903make_pass_ipa_tree_profile (gcc::context *ctxt)
904{
905 return new pass_ipa_tree_profile (ctxt);
906}
907
d7683f13 908#include "gt-tree-profile.h"