]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-utils.c
New symbol_summary class introduced.
[thirdparty/gcc.git] / gcc / ipa-utils.c
CommitLineData
ea900239 1/* Utilities for ipa analysis.
23a5b65a 2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
ea900239
DB
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
ea900239
DB
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
ea900239
DB
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
60393bbc
AM
26#include "predict.h"
27#include "vec.h"
28#include "hashtab.h"
29#include "hash-set.h"
30#include "machmode.h"
31#include "hard-reg-set.h"
32#include "input.h"
33#include "function.h"
34#include "dominance.h"
35#include "cfg.h"
2fb9a547
AM
36#include "basic-block.h"
37#include "tree-ssa-alias.h"
38#include "internal-fn.h"
39#include "gimple-expr.h"
40#include "is-a.h"
8e9055ae 41#include "gimple.h"
ea900239 42#include "tree-inline.h"
7ee2468b 43#include "dumpfile.h"
ea900239 44#include "langhooks.h"
ea264ca5 45#include "splay-tree.h"
c582198b
AM
46#include "hash-map.h"
47#include "plugin-api.h"
48#include "ipa-ref.h"
49#include "cgraph.h"
ea900239 50#include "ipa-utils.h"
c582198b 51#include "bitmap.h"
ea900239 52#include "ipa-reference.h"
ea900239 53#include "flags.h"
ea900239
DB
54#include "diagnostic.h"
55#include "langhooks.h"
4843f032 56#include "lto-streamer.h"
c582198b
AM
57#include "alloc-pool.h"
58#include "ipa-prop.h"
4843f032 59#include "ipa-inline.h"
ea900239
DB
60
61/* Debugging function for postorder and inorder code. NOTE is a string
62 that is printed before the nodes are printed. ORDER is an array of
63 cgraph_nodes that has COUNT useful nodes in it. */
64
b8698a0f 65void
af8bca3c
MJ
66ipa_print_order (FILE* out,
67 const char * note,
68 struct cgraph_node** order,
69 int count)
ea900239
DB
70{
71 int i;
72 fprintf (out, "\n\n ordered call graph: %s\n", note);
b8698a0f 73
ea900239 74 for (i = count - 1; i >= 0; i--)
d52f5295 75 order[i]->dump (out);
ea900239 76 fprintf (out, "\n");
c3284718 77 fflush (out);
ea900239
DB
78}
79
d52f5295 80
ea900239
DB
81struct searchc_env {
82 struct cgraph_node **stack;
83 int stack_size;
84 struct cgraph_node **result;
85 int order_pos;
86 splay_tree nodes_marked_new;
87 bool reduce;
b6156cf2 88 bool allow_overwritable;
ea900239
DB
89 int count;
90};
91
92/* This is an implementation of Tarjan's strongly connected region
93 finder as reprinted in Aho Hopcraft and Ullman's The Design and
94 Analysis of Computer Programs (1975) pages 192-193. This version
95 has been customized for cgraph_nodes. The env parameter is because
96 it is recursive and there are no nested functions here. This
97 function should only be called from itself or
af8bca3c 98 ipa_reduced_postorder. ENV is a stack env and would be
ea900239
DB
99 unnecessary if C had nested functions. V is the node to start
100 searching from. */
101
102static void
2505c5ed
JH
103searchc (struct searchc_env* env, struct cgraph_node *v,
104 bool (*ignore_edge) (struct cgraph_edge *))
ea900239
DB
105{
106 struct cgraph_edge *edge;
67348ccc 107 struct ipa_dfs_info *v_info = (struct ipa_dfs_info *) v->aux;
b8698a0f 108
ea900239 109 /* mark node as old */
c5274326 110 v_info->new_node = false;
ea900239 111 splay_tree_remove (env->nodes_marked_new, v->uid);
b8698a0f 112
ea900239
DB
113 v_info->dfn_number = env->count;
114 v_info->low_link = env->count;
115 env->count++;
116 env->stack[(env->stack_size)++] = v;
117 v_info->on_stack = true;
b8698a0f 118
ea900239
DB
119 for (edge = v->callees; edge; edge = edge->next_callee)
120 {
121 struct ipa_dfs_info * w_info;
fede8efa 122 enum availability avail;
d52f5295 123 struct cgraph_node *w = edge->callee->ultimate_alias_target (&avail);
e2c9111c 124
fede8efa 125 if (!w || (ignore_edge && ignore_edge (edge)))
2505c5ed
JH
126 continue;
127
67348ccc 128 if (w->aux
d52f5295
ML
129 && (avail > AVAIL_INTERPOSABLE
130 || (env->allow_overwritable && avail == AVAIL_INTERPOSABLE)))
ea900239 131 {
67348ccc 132 w_info = (struct ipa_dfs_info *) w->aux;
b8698a0f 133 if (w_info->new_node)
ea900239 134 {
2505c5ed 135 searchc (env, w, ignore_edge);
ea900239
DB
136 v_info->low_link =
137 (v_info->low_link < w_info->low_link) ?
138 v_info->low_link : w_info->low_link;
b8698a0f
L
139 }
140 else
141 if ((w_info->dfn_number < v_info->dfn_number)
142 && (w_info->on_stack))
ea900239
DB
143 v_info->low_link =
144 (w_info->dfn_number < v_info->low_link) ?
145 w_info->dfn_number : v_info->low_link;
146 }
147 }
148
149
b8698a0f 150 if (v_info->low_link == v_info->dfn_number)
ea900239
DB
151 {
152 struct cgraph_node *last = NULL;
153 struct cgraph_node *x;
154 struct ipa_dfs_info *x_info;
155 do {
156 x = env->stack[--(env->stack_size)];
67348ccc 157 x_info = (struct ipa_dfs_info *) x->aux;
ea900239 158 x_info->on_stack = false;
11026b51 159 x_info->scc_no = v_info->dfn_number;
b8698a0f
L
160
161 if (env->reduce)
ea900239
DB
162 {
163 x_info->next_cycle = last;
164 last = x;
b8698a0f
L
165 }
166 else
ea900239 167 env->result[env->order_pos++] = x;
b8698a0f 168 }
ea900239 169 while (v != x);
b8698a0f 170 if (env->reduce)
ea900239
DB
171 env->result[env->order_pos++] = v;
172 }
173}
174
175/* Topsort the call graph by caller relation. Put the result in ORDER.
176
df92c640
SB
177 The REDUCE flag is true if you want the cycles reduced to single nodes.
178 You can use ipa_get_nodes_in_cycle to obtain a vector containing all real
179 call graph nodes in a reduced node.
180
181 Set ALLOW_OVERWRITABLE if nodes with such availability should be included.
af8bca3c
MJ
182 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
183 for the topological sort. */
ea900239
DB
184
185int
af8bca3c
MJ
186ipa_reduced_postorder (struct cgraph_node **order,
187 bool reduce, bool allow_overwritable,
188 bool (*ignore_edge) (struct cgraph_edge *))
ea900239
DB
189{
190 struct cgraph_node *node;
191 struct searchc_env env;
192 splay_tree_node result;
3dafb85c 193 env.stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
ea900239
DB
194 env.stack_size = 0;
195 env.result = order;
196 env.order_pos = 0;
197 env.nodes_marked_new = splay_tree_new (splay_tree_compare_ints, 0, 0);
198 env.count = 1;
199 env.reduce = reduce;
b6156cf2 200 env.allow_overwritable = allow_overwritable;
b8698a0f 201
65c70e6b 202 FOR_EACH_DEFINED_FUNCTION (node)
e2c9111c 203 {
d52f5295 204 enum availability avail = node->get_availability ();
e2c9111c 205
d52f5295 206 if (avail > AVAIL_INTERPOSABLE
b8698a0f 207 || (allow_overwritable
d52f5295 208 && (avail == AVAIL_INTERPOSABLE)))
e2c9111c
JH
209 {
210 /* Reuse the info if it is already there. */
67348ccc 211 struct ipa_dfs_info *info = (struct ipa_dfs_info *) node->aux;
e2c9111c
JH
212 if (!info)
213 info = XCNEW (struct ipa_dfs_info);
214 info->new_node = true;
215 info->on_stack = false;
216 info->next_cycle = NULL;
67348ccc 217 node->aux = info;
b8698a0f 218
e2c9111c 219 splay_tree_insert (env.nodes_marked_new,
b8698a0f 220 (splay_tree_key)node->uid,
e2c9111c 221 (splay_tree_value)node);
b8698a0f
L
222 }
223 else
67348ccc 224 node->aux = NULL;
e2c9111c 225 }
ea900239
DB
226 result = splay_tree_min (env.nodes_marked_new);
227 while (result)
228 {
229 node = (struct cgraph_node *)result->value;
2505c5ed 230 searchc (&env, node, ignore_edge);
ea900239
DB
231 result = splay_tree_min (env.nodes_marked_new);
232 }
233 splay_tree_delete (env.nodes_marked_new);
234 free (env.stack);
235
236 return env.order_pos;
237}
238
af8bca3c
MJ
239/* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
240 graph nodes. */
241
242void
243ipa_free_postorder_info (void)
244{
245 struct cgraph_node *node;
65c70e6b 246 FOR_EACH_DEFINED_FUNCTION (node)
af8bca3c
MJ
247 {
248 /* Get rid of the aux information. */
67348ccc 249 if (node->aux)
af8bca3c 250 {
67348ccc
DM
251 free (node->aux);
252 node->aux = NULL;
af8bca3c
MJ
253 }
254 }
255}
256
df92c640
SB
257/* Get the set of nodes for the cycle in the reduced call graph starting
258 from NODE. */
259
d52f5295 260vec<cgraph_node *>
df92c640
SB
261ipa_get_nodes_in_cycle (struct cgraph_node *node)
262{
d52f5295 263 vec<cgraph_node *> v = vNULL;
df92c640
SB
264 struct ipa_dfs_info *node_dfs_info;
265 while (node)
266 {
9771b263 267 v.safe_push (node);
67348ccc 268 node_dfs_info = (struct ipa_dfs_info *) node->aux;
df92c640
SB
269 node = node_dfs_info->next_cycle;
270 }
271 return v;
272}
273
4cb13597
MJ
274/* Return true iff the CS is an edge within a strongly connected component as
275 computed by ipa_reduced_postorder. */
276
277bool
278ipa_edge_within_scc (struct cgraph_edge *cs)
279{
67348ccc 280 struct ipa_dfs_info *caller_dfs = (struct ipa_dfs_info *) cs->caller->aux;
4cb13597 281 struct ipa_dfs_info *callee_dfs;
d52f5295 282 struct cgraph_node *callee = cs->callee->function_symbol ();
4cb13597 283
67348ccc 284 callee_dfs = (struct ipa_dfs_info *) callee->aux;
4cb13597
MJ
285 return (caller_dfs
286 && callee_dfs
287 && caller_dfs->scc_no == callee_dfs->scc_no);
288}
289
8775a18b
JH
290struct postorder_stack
291{
292 struct cgraph_node *node;
293 struct cgraph_edge *edge;
294 int ref;
295};
296
af8bca3c 297/* Fill array order with all nodes with output flag set in the reverse
39e2db00
JH
298 topological order. Return the number of elements in the array.
299 FIXME: While walking, consider aliases, too. */
af8bca3c
MJ
300
301int
302ipa_reverse_postorder (struct cgraph_node **order)
303{
304 struct cgraph_node *node, *node2;
305 int stack_size = 0;
306 int order_pos = 0;
8775a18b 307 struct cgraph_edge *edge;
af8bca3c 308 int pass;
d122681a 309 struct ipa_ref *ref = NULL;
af8bca3c 310
8775a18b 311 struct postorder_stack *stack =
3dafb85c 312 XCNEWVEC (struct postorder_stack, symtab->cgraph_count);
af8bca3c
MJ
313
314 /* We have to deal with cycles nicely, so use a depth first traversal
315 output algorithm. Ignore the fact that some functions won't need
316 to be output and put them into order as well, so we get dependencies
317 right through inline functions. */
65c70e6b 318 FOR_EACH_FUNCTION (node)
67348ccc 319 node->aux = NULL;
af8bca3c 320 for (pass = 0; pass < 2; pass++)
65c70e6b 321 FOR_EACH_FUNCTION (node)
67348ccc 322 if (!node->aux
af8bca3c 323 && (pass
67348ccc 324 || (!node->address_taken
af8bca3c 325 && !node->global.inlined_to
67348ccc 326 && !node->alias && !node->thunk.thunk_p
d52f5295 327 && !node->only_called_directly_p ())))
af8bca3c 328 {
8775a18b
JH
329 stack_size = 0;
330 stack[stack_size].node = node;
331 stack[stack_size].edge = node->callers;
332 stack[stack_size].ref = 0;
67348ccc 333 node->aux = (void *)(size_t)1;
8775a18b 334 while (stack_size >= 0)
af8bca3c 335 {
8775a18b 336 while (true)
af8bca3c 337 {
8775a18b
JH
338 node2 = NULL;
339 while (stack[stack_size].edge && !node2)
af8bca3c 340 {
8775a18b 341 edge = stack[stack_size].edge;
af8bca3c 342 node2 = edge->caller;
8775a18b
JH
343 stack[stack_size].edge = edge->next_caller;
344 /* Break possible cycles involving always-inline
345 functions by ignoring edges from always-inline
346 functions to non-always-inline functions. */
67348ccc 347 if (DECL_DISREGARD_INLINE_LIMITS (edge->caller->decl)
8775a18b 348 && !DECL_DISREGARD_INLINE_LIMITS
d52f5295 349 (edge->callee->function_symbol ()->decl))
8775a18b
JH
350 node2 = NULL;
351 }
d122681a 352 for (; stack[stack_size].node->iterate_referring (
8775a18b
JH
353 stack[stack_size].ref,
354 ref) && !node2;
355 stack[stack_size].ref++)
356 {
357 if (ref->use == IPA_REF_ALIAS)
d122681a 358 node2 = dyn_cast <cgraph_node *> (ref->referring);
8775a18b
JH
359 }
360 if (!node2)
361 break;
67348ccc 362 if (!node2->aux)
8775a18b
JH
363 {
364 stack[++stack_size].node = node2;
365 stack[stack_size].edge = node2->callers;
366 stack[stack_size].ref = 0;
67348ccc 367 node2->aux = (void *)(size_t)1;
af8bca3c
MJ
368 }
369 }
8775a18b 370 order[order_pos++] = stack[stack_size--].node;
af8bca3c
MJ
371 }
372 }
373 free (stack);
65c70e6b 374 FOR_EACH_FUNCTION (node)
67348ccc 375 node->aux = NULL;
af8bca3c
MJ
376 return order_pos;
377}
378
379
ea900239
DB
380
381/* Given a memory reference T, will return the variable at the bottom
073a8998 382 of the access. Unlike get_base_address, this will recurse through
ea900239
DB
383 INDIRECT_REFS. */
384
385tree
386get_base_var (tree t)
387{
b8698a0f 388 while (!SSA_VAR_P (t)
ea900239
DB
389 && (!CONSTANT_CLASS_P (t))
390 && TREE_CODE (t) != LABEL_DECL
391 && TREE_CODE (t) != FUNCTION_DECL
3baf459d
DN
392 && TREE_CODE (t) != CONST_DECL
393 && TREE_CODE (t) != CONSTRUCTOR)
ea900239
DB
394 {
395 t = TREE_OPERAND (t, 0);
396 }
397 return t;
b8698a0f 398}
ea900239 399
1cb1a99f 400
4843f032
JH
401/* SRC and DST are going to be merged. Take SRC's profile and merge it into
402 DST so it is not going to be lost. Destroy SRC's body on the way. */
403
404void
405ipa_merge_profiles (struct cgraph_node *dst,
406 struct cgraph_node *src)
407{
67348ccc 408 tree oldsrcdecl = src->decl;
4843f032
JH
409 struct function *srccfun, *dstcfun;
410 bool match = true;
411
67348ccc
DM
412 if (!src->definition
413 || !dst->definition)
4843f032
JH
414 return;
415 if (src->frequency < dst->frequency)
416 src->frequency = dst->frequency;
9cec31f4
ML
417
418 /* Time profiles are merged. */
419 if (dst->tp_first_run > src->tp_first_run && src->tp_first_run)
420 dst->tp_first_run = src->tp_first_run;
421
fd29c024
ML
422 if (src->profile_id && !dst->profile_id)
423 dst->profile_id = src->profile_id;
cb90235d 424
4843f032
JH
425 if (!dst->count)
426 return;
3dafb85c 427 if (symtab->dump_file)
4843f032 428 {
3dafb85c 429 fprintf (symtab->dump_file, "Merging profiles of %s/%i to %s/%i\n",
2a72a953
DM
430 xstrdup_for_dump (src->name ()), src->order,
431 xstrdup_for_dump (dst->name ()), dst->order);
4843f032
JH
432 }
433 dst->count += src->count;
434
435 /* This is ugly. We need to get both function bodies into memory.
436 If declaration is merged, we need to duplicate it to be able
437 to load body that is being replaced. This makes symbol table
438 temporarily inconsistent. */
67348ccc 439 if (src->decl == dst->decl)
4843f032 440 {
4843f032
JH
441 struct lto_in_decl_state temp;
442 struct lto_in_decl_state *state;
443
444 /* We are going to move the decl, we want to remove its file decl data.
445 and link these with the new decl. */
67348ccc 446 temp.fn_decl = src->decl;
907dadbd
TS
447 lto_in_decl_state **slot
448 = src->lto_file_data->function_decl_states->find_slot (&temp,
449 NO_INSERT);
450 state = *slot;
451 src->lto_file_data->function_decl_states->clear_slot (slot);
4843f032
JH
452 gcc_assert (state);
453
454 /* Duplicate the decl and be sure it does not link into body of DST. */
67348ccc
DM
455 src->decl = copy_node (src->decl);
456 DECL_STRUCT_FUNCTION (src->decl) = NULL;
457 DECL_ARGUMENTS (src->decl) = NULL;
458 DECL_INITIAL (src->decl) = NULL;
459 DECL_RESULT (src->decl) = NULL;
4843f032
JH
460
461 /* Associate the decl state with new declaration, so LTO streamer
462 can look it up. */
67348ccc 463 state->fn_decl = src->decl;
907dadbd
TS
464 slot
465 = src->lto_file_data->function_decl_states->find_slot (state, INSERT);
4843f032
JH
466 gcc_assert (!*slot);
467 *slot = state;
468 }
d52f5295
ML
469 src->get_body ();
470 dst->get_body ();
67348ccc
DM
471 srccfun = DECL_STRUCT_FUNCTION (src->decl);
472 dstcfun = DECL_STRUCT_FUNCTION (dst->decl);
0cae8d31
DM
473 if (n_basic_blocks_for_fn (srccfun)
474 != n_basic_blocks_for_fn (dstcfun))
4843f032 475 {
3dafb85c
ML
476 if (symtab->dump_file)
477 fprintf (symtab->dump_file,
4843f032
JH
478 "Giving up; number of basic block mismatch.\n");
479 match = false;
480 }
3986e690
DM
481 else if (last_basic_block_for_fn (srccfun)
482 != last_basic_block_for_fn (dstcfun))
4843f032 483 {
3dafb85c
ML
484 if (symtab->dump_file)
485 fprintf (symtab->dump_file,
4843f032
JH
486 "Giving up; last block mismatch.\n");
487 match = false;
488 }
489 else
490 {
491 basic_block srcbb, dstbb;
492
493 FOR_ALL_BB_FN (srcbb, srccfun)
494 {
495 unsigned int i;
496
bbd79259 497 dstbb = BASIC_BLOCK_FOR_FN (dstcfun, srcbb->index);
4843f032
JH
498 if (dstbb == NULL)
499 {
3dafb85c
ML
500 if (symtab->dump_file)
501 fprintf (symtab->dump_file,
4843f032
JH
502 "No matching block for bb %i.\n",
503 srcbb->index);
504 match = false;
505 break;
506 }
507 if (EDGE_COUNT (srcbb->succs) != EDGE_COUNT (dstbb->succs))
508 {
3dafb85c
ML
509 if (symtab->dump_file)
510 fprintf (symtab->dump_file,
4843f032
JH
511 "Edge count mistmatch for bb %i.\n",
512 srcbb->index);
513 match = false;
514 break;
515 }
516 for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
517 {
518 edge srce = EDGE_SUCC (srcbb, i);
519 edge dste = EDGE_SUCC (dstbb, i);
520 if (srce->dest->index != dste->dest->index)
521 {
3dafb85c
ML
522 if (symtab->dump_file)
523 fprintf (symtab->dump_file,
4843f032
JH
524 "Succ edge mistmatch for bb %i.\n",
525 srce->dest->index);
526 match = false;
527 break;
528 }
529 }
530 }
531 }
532 if (match)
533 {
534 struct cgraph_edge *e;
535 basic_block srcbb, dstbb;
536
537 /* TODO: merge also statement histograms. */
538 FOR_ALL_BB_FN (srcbb, srccfun)
539 {
540 unsigned int i;
541
bbd79259 542 dstbb = BASIC_BLOCK_FOR_FN (dstcfun, srcbb->index);
4843f032
JH
543 dstbb->count += srcbb->count;
544 for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
545 {
546 edge srce = EDGE_SUCC (srcbb, i);
547 edge dste = EDGE_SUCC (dstbb, i);
548 dste->count += srce->count;
549 }
550 }
551 push_cfun (dstcfun);
552 counts_to_freqs ();
553 compute_function_frequency ();
554 pop_cfun ();
555 for (e = dst->callees; e; e = e->next_callee)
556 {
557 gcc_assert (!e->speculative);
558 e->count = gimple_bb (e->call_stmt)->count;
559 e->frequency = compute_call_stmt_bb_frequency
67348ccc 560 (dst->decl,
4843f032
JH
561 gimple_bb (e->call_stmt));
562 }
563 for (e = dst->indirect_calls; e; e = e->next_callee)
564 {
565 gcc_assert (!e->speculative);
566 e->count = gimple_bb (e->call_stmt)->count;
567 e->frequency = compute_call_stmt_bb_frequency
67348ccc 568 (dst->decl,
4843f032
JH
569 gimple_bb (e->call_stmt));
570 }
d52f5295 571 src->release_body ();
4843f032
JH
572 inline_update_overall_summary (dst);
573 }
574 /* TODO: if there is no match, we can scale up. */
67348ccc 575 src->decl = oldsrcdecl;
4843f032
JH
576}
577
fc11f321
JH
578/* Return true if call to DEST is known to be self-recusive call withing FUNC. */
579
580bool
581recursive_call_p (tree func, tree dest)
582{
d52f5295
ML
583 struct cgraph_node *dest_node = cgraph_node::get_create (dest);
584 struct cgraph_node *cnode = cgraph_node::get_create (func);
fc11f321 585
d52f5295 586 return dest_node->semantically_equivalent_p (cnode);
fc11f321 587}