1 /* Utilities for ipa analysis.
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 #include "fold-const.h"
32 #include "hard-reg-set.h"
35 #include "dominance.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
43 #include "tree-inline.h"
45 #include "langhooks.h"
46 #include "splay-tree.h"
47 #include "plugin-api.h"
50 #include "ipa-utils.h"
52 #include "ipa-reference.h"
54 #include "diagnostic.h"
55 #include "langhooks.h"
56 #include "lto-streamer.h"
57 #include "alloc-pool.h"
58 #include "symbol-summary.h"
60 #include "ipa-inline.h"
62 /* Debugging function for postorder and inorder code. NOTE is a string
63 that is printed before the nodes are printed. ORDER is an array of
64 cgraph_nodes that has COUNT useful nodes in it. */
67 ipa_print_order (FILE* out
,
69 struct cgraph_node
** order
,
73 fprintf (out
, "\n\n ordered call graph: %s\n", note
);
75 for (i
= count
- 1; i
>= 0; i
--)
83 struct cgraph_node
**stack
;
85 struct cgraph_node
**result
;
87 splay_tree nodes_marked_new
;
89 bool allow_overwritable
;
93 /* This is an implementation of Tarjan's strongly connected region
94 finder as reprinted in Aho Hopcraft and Ullman's The Design and
95 Analysis of Computer Programs (1975) pages 192-193. This version
96 has been customized for cgraph_nodes. The env parameter is because
97 it is recursive and there are no nested functions here. This
98 function should only be called from itself or
99 ipa_reduced_postorder. ENV is a stack env and would be
100 unnecessary if C had nested functions. V is the node to start
104 searchc (struct searchc_env
* env
, struct cgraph_node
*v
,
105 bool (*ignore_edge
) (struct cgraph_edge
*))
107 struct cgraph_edge
*edge
;
108 struct ipa_dfs_info
*v_info
= (struct ipa_dfs_info
*) v
->aux
;
110 /* mark node as old */
111 v_info
->new_node
= false;
112 splay_tree_remove (env
->nodes_marked_new
, v
->uid
);
114 v_info
->dfn_number
= env
->count
;
115 v_info
->low_link
= env
->count
;
117 env
->stack
[(env
->stack_size
)++] = v
;
118 v_info
->on_stack
= true;
120 for (edge
= v
->callees
; edge
; edge
= edge
->next_callee
)
122 struct ipa_dfs_info
* w_info
;
123 enum availability avail
;
124 struct cgraph_node
*w
= edge
->callee
->ultimate_alias_target (&avail
);
126 if (!w
|| (ignore_edge
&& ignore_edge (edge
)))
130 && (avail
> AVAIL_INTERPOSABLE
131 || (env
->allow_overwritable
&& avail
== AVAIL_INTERPOSABLE
)))
133 w_info
= (struct ipa_dfs_info
*) w
->aux
;
134 if (w_info
->new_node
)
136 searchc (env
, w
, ignore_edge
);
138 (v_info
->low_link
< w_info
->low_link
) ?
139 v_info
->low_link
: w_info
->low_link
;
142 if ((w_info
->dfn_number
< v_info
->dfn_number
)
143 && (w_info
->on_stack
))
145 (w_info
->dfn_number
< v_info
->low_link
) ?
146 w_info
->dfn_number
: v_info
->low_link
;
151 if (v_info
->low_link
== v_info
->dfn_number
)
153 struct cgraph_node
*last
= NULL
;
154 struct cgraph_node
*x
;
155 struct ipa_dfs_info
*x_info
;
157 x
= env
->stack
[--(env
->stack_size
)];
158 x_info
= (struct ipa_dfs_info
*) x
->aux
;
159 x_info
->on_stack
= false;
160 x_info
->scc_no
= v_info
->dfn_number
;
164 x_info
->next_cycle
= last
;
168 env
->result
[env
->order_pos
++] = x
;
172 env
->result
[env
->order_pos
++] = v
;
176 /* Topsort the call graph by caller relation. Put the result in ORDER.
178 The REDUCE flag is true if you want the cycles reduced to single nodes.
179 You can use ipa_get_nodes_in_cycle to obtain a vector containing all real
180 call graph nodes in a reduced node.
182 Set ALLOW_OVERWRITABLE if nodes with such availability should be included.
183 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
184 for the topological sort. */
187 ipa_reduced_postorder (struct cgraph_node
**order
,
188 bool reduce
, bool allow_overwritable
,
189 bool (*ignore_edge
) (struct cgraph_edge
*))
191 struct cgraph_node
*node
;
192 struct searchc_env env
;
193 splay_tree_node result
;
194 env
.stack
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
198 env
.nodes_marked_new
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
201 env
.allow_overwritable
= allow_overwritable
;
203 FOR_EACH_DEFINED_FUNCTION (node
)
205 enum availability avail
= node
->get_availability ();
207 if (avail
> AVAIL_INTERPOSABLE
208 || (allow_overwritable
209 && (avail
== AVAIL_INTERPOSABLE
)))
211 /* Reuse the info if it is already there. */
212 struct ipa_dfs_info
*info
= (struct ipa_dfs_info
*) node
->aux
;
214 info
= XCNEW (struct ipa_dfs_info
);
215 info
->new_node
= true;
216 info
->on_stack
= false;
217 info
->next_cycle
= NULL
;
220 splay_tree_insert (env
.nodes_marked_new
,
221 (splay_tree_key
)node
->uid
,
222 (splay_tree_value
)node
);
227 result
= splay_tree_min (env
.nodes_marked_new
);
230 node
= (struct cgraph_node
*)result
->value
;
231 searchc (&env
, node
, ignore_edge
);
232 result
= splay_tree_min (env
.nodes_marked_new
);
234 splay_tree_delete (env
.nodes_marked_new
);
237 return env
.order_pos
;
240 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
244 ipa_free_postorder_info (void)
246 struct cgraph_node
*node
;
247 FOR_EACH_DEFINED_FUNCTION (node
)
249 /* Get rid of the aux information. */
258 /* Get the set of nodes for the cycle in the reduced call graph starting
262 ipa_get_nodes_in_cycle (struct cgraph_node
*node
)
264 vec
<cgraph_node
*> v
= vNULL
;
265 struct ipa_dfs_info
*node_dfs_info
;
269 node_dfs_info
= (struct ipa_dfs_info
*) node
->aux
;
270 node
= node_dfs_info
->next_cycle
;
275 /* Return true iff the CS is an edge within a strongly connected component as
276 computed by ipa_reduced_postorder. */
279 ipa_edge_within_scc (struct cgraph_edge
*cs
)
281 struct ipa_dfs_info
*caller_dfs
= (struct ipa_dfs_info
*) cs
->caller
->aux
;
282 struct ipa_dfs_info
*callee_dfs
;
283 struct cgraph_node
*callee
= cs
->callee
->function_symbol ();
285 callee_dfs
= (struct ipa_dfs_info
*) callee
->aux
;
288 && caller_dfs
->scc_no
== callee_dfs
->scc_no
);
291 struct postorder_stack
293 struct cgraph_node
*node
;
294 struct cgraph_edge
*edge
;
298 /* Fill array order with all nodes with output flag set in the reverse
299 topological order. Return the number of elements in the array.
300 FIXME: While walking, consider aliases, too. */
303 ipa_reverse_postorder (struct cgraph_node
**order
)
305 struct cgraph_node
*node
, *node2
;
308 struct cgraph_edge
*edge
;
310 struct ipa_ref
*ref
= NULL
;
312 struct postorder_stack
*stack
=
313 XCNEWVEC (struct postorder_stack
, symtab
->cgraph_count
);
315 /* We have to deal with cycles nicely, so use a depth first traversal
316 output algorithm. Ignore the fact that some functions won't need
317 to be output and put them into order as well, so we get dependencies
318 right through inline functions. */
319 FOR_EACH_FUNCTION (node
)
321 for (pass
= 0; pass
< 2; pass
++)
322 FOR_EACH_FUNCTION (node
)
325 || (!node
->address_taken
326 && !node
->global
.inlined_to
327 && !node
->alias
&& !node
->thunk
.thunk_p
328 && !node
->only_called_directly_p ())))
331 stack
[stack_size
].node
= node
;
332 stack
[stack_size
].edge
= node
->callers
;
333 stack
[stack_size
].ref
= 0;
334 node
->aux
= (void *)(size_t)1;
335 while (stack_size
>= 0)
340 while (stack
[stack_size
].edge
&& !node2
)
342 edge
= stack
[stack_size
].edge
;
343 node2
= edge
->caller
;
344 stack
[stack_size
].edge
= edge
->next_caller
;
345 /* Break possible cycles involving always-inline
346 functions by ignoring edges from always-inline
347 functions to non-always-inline functions. */
348 if (DECL_DISREGARD_INLINE_LIMITS (edge
->caller
->decl
)
349 && !DECL_DISREGARD_INLINE_LIMITS
350 (edge
->callee
->function_symbol ()->decl
))
353 for (; stack
[stack_size
].node
->iterate_referring (
354 stack
[stack_size
].ref
,
356 stack
[stack_size
].ref
++)
358 if (ref
->use
== IPA_REF_ALIAS
)
359 node2
= dyn_cast
<cgraph_node
*> (ref
->referring
);
365 stack
[++stack_size
].node
= node2
;
366 stack
[stack_size
].edge
= node2
->callers
;
367 stack
[stack_size
].ref
= 0;
368 node2
->aux
= (void *)(size_t)1;
371 order
[order_pos
++] = stack
[stack_size
--].node
;
375 FOR_EACH_FUNCTION (node
)
382 /* Given a memory reference T, will return the variable at the bottom
383 of the access. Unlike get_base_address, this will recurse through
387 get_base_var (tree t
)
389 while (!SSA_VAR_P (t
)
390 && (!CONSTANT_CLASS_P (t
))
391 && TREE_CODE (t
) != LABEL_DECL
392 && TREE_CODE (t
) != FUNCTION_DECL
393 && TREE_CODE (t
) != CONST_DECL
394 && TREE_CODE (t
) != CONSTRUCTOR
)
396 t
= TREE_OPERAND (t
, 0);
402 /* SRC and DST are going to be merged. Take SRC's profile and merge it into
403 DST so it is not going to be lost. Possibly destroy SRC's body on the way
404 unless PRESERVE_BODY is set. */
407 ipa_merge_profiles (struct cgraph_node
*dst
,
408 struct cgraph_node
*src
,
411 tree oldsrcdecl
= src
->decl
;
412 struct function
*srccfun
, *dstcfun
;
418 if (src
->frequency
< dst
->frequency
)
419 src
->frequency
= dst
->frequency
;
421 /* Time profiles are merged. */
422 if (dst
->tp_first_run
> src
->tp_first_run
&& src
->tp_first_run
)
423 dst
->tp_first_run
= src
->tp_first_run
;
425 if (src
->profile_id
&& !dst
->profile_id
)
426 dst
->profile_id
= src
->profile_id
;
430 if (symtab
->dump_file
)
432 fprintf (symtab
->dump_file
, "Merging profiles of %s/%i to %s/%i\n",
433 xstrdup_for_dump (src
->name ()), src
->order
,
434 xstrdup_for_dump (dst
->name ()), dst
->order
);
436 dst
->count
+= src
->count
;
438 /* This is ugly. We need to get both function bodies into memory.
439 If declaration is merged, we need to duplicate it to be able
440 to load body that is being replaced. This makes symbol table
441 temporarily inconsistent. */
442 if (src
->decl
== dst
->decl
)
444 struct lto_in_decl_state temp
;
445 struct lto_in_decl_state
*state
;
447 /* We are going to move the decl, we want to remove its file decl data.
448 and link these with the new decl. */
449 temp
.fn_decl
= src
->decl
;
450 lto_in_decl_state
**slot
451 = src
->lto_file_data
->function_decl_states
->find_slot (&temp
,
454 src
->lto_file_data
->function_decl_states
->clear_slot (slot
);
457 /* Duplicate the decl and be sure it does not link into body of DST. */
458 src
->decl
= copy_node (src
->decl
);
459 DECL_STRUCT_FUNCTION (src
->decl
) = NULL
;
460 DECL_ARGUMENTS (src
->decl
) = NULL
;
461 DECL_INITIAL (src
->decl
) = NULL
;
462 DECL_RESULT (src
->decl
) = NULL
;
464 /* Associate the decl state with new declaration, so LTO streamer
466 state
->fn_decl
= src
->decl
;
468 = src
->lto_file_data
->function_decl_states
->find_slot (state
, INSERT
);
472 src
->get_untransformed_body ();
473 dst
->get_untransformed_body ();
474 srccfun
= DECL_STRUCT_FUNCTION (src
->decl
);
475 dstcfun
= DECL_STRUCT_FUNCTION (dst
->decl
);
476 if (n_basic_blocks_for_fn (srccfun
)
477 != n_basic_blocks_for_fn (dstcfun
))
479 if (symtab
->dump_file
)
480 fprintf (symtab
->dump_file
,
481 "Giving up; number of basic block mismatch.\n");
484 else if (last_basic_block_for_fn (srccfun
)
485 != last_basic_block_for_fn (dstcfun
))
487 if (symtab
->dump_file
)
488 fprintf (symtab
->dump_file
,
489 "Giving up; last block mismatch.\n");
494 basic_block srcbb
, dstbb
;
496 FOR_ALL_BB_FN (srcbb
, srccfun
)
500 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
503 if (symtab
->dump_file
)
504 fprintf (symtab
->dump_file
,
505 "No matching block for bb %i.\n",
510 if (EDGE_COUNT (srcbb
->succs
) != EDGE_COUNT (dstbb
->succs
))
512 if (symtab
->dump_file
)
513 fprintf (symtab
->dump_file
,
514 "Edge count mistmatch for bb %i.\n",
519 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
521 edge srce
= EDGE_SUCC (srcbb
, i
);
522 edge dste
= EDGE_SUCC (dstbb
, i
);
523 if (srce
->dest
->index
!= dste
->dest
->index
)
525 if (symtab
->dump_file
)
526 fprintf (symtab
->dump_file
,
527 "Succ edge mistmatch for bb %i.\n",
537 struct cgraph_edge
*e
, *e2
;
538 basic_block srcbb
, dstbb
;
540 /* TODO: merge also statement histograms. */
541 FOR_ALL_BB_FN (srcbb
, srccfun
)
545 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
546 dstbb
->count
+= srcbb
->count
;
547 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
549 edge srce
= EDGE_SUCC (srcbb
, i
);
550 edge dste
= EDGE_SUCC (dstbb
, i
);
551 dste
->count
+= srce
->count
;
556 compute_function_frequency ();
558 for (e
= dst
->callees
; e
; e
= e
->next_callee
)
562 e
->count
= gimple_bb (e
->call_stmt
)->count
;
563 e
->frequency
= compute_call_stmt_bb_frequency
565 gimple_bb (e
->call_stmt
));
567 for (e
= dst
->indirect_calls
, e2
= src
->indirect_calls
; e
;
568 e2
= (e2
? e2
->next_callee
: NULL
), e
= e
->next_callee
)
570 gcov_type count
= gimple_bb (e
->call_stmt
)->count
;
571 int freq
= compute_call_stmt_bb_frequency
573 gimple_bb (e
->call_stmt
));
574 /* When call is speculative, we need to re-distribute probabilities
575 the same way as they was. This is not really correct because
576 in the other copy the speculation may differ; but probably it
577 is not really worth the effort. */
580 cgraph_edge
*direct
, *indirect
;
581 cgraph_edge
*direct2
= NULL
, *indirect2
= NULL
;
584 e
->speculative_call_info (direct
, indirect
, ref
);
585 gcc_assert (e
== indirect
);
586 if (e2
&& e2
->speculative
)
587 e2
->speculative_call_info (direct2
, indirect2
, ref
);
588 if (indirect
->count
|| direct
->count
)
590 /* We should mismatch earlier if there is no matching
596 "Mismatch in merging indirect edges\n");
598 else if (!e2
->speculative
)
599 indirect
->count
+= e2
->count
;
600 else if (e2
->speculative
)
602 if (DECL_ASSEMBLER_NAME (direct2
->callee
->decl
)
603 != DECL_ASSEMBLER_NAME (direct
->callee
->decl
))
605 if (direct2
->count
>= direct
->count
)
607 direct
->redirect_callee (direct2
->callee
);
608 indirect
->count
+= indirect2
->count
610 direct
->count
= direct2
->count
;
613 indirect
->count
+= indirect2
->count
+ direct2
->count
;
617 direct
->count
+= direct2
->count
;
618 indirect
->count
+= indirect2
->count
;
621 int prob
= RDIV (direct
->count
* REG_BR_PROB_BASE
,
622 direct
->count
+ indirect
->count
);
623 direct
->frequency
= RDIV (freq
* prob
, REG_BR_PROB_BASE
);
624 indirect
->frequency
= RDIV (freq
* (REG_BR_PROB_BASE
- prob
),
628 /* At the moment we should have only profile feedback based
629 speculations when merging. */
632 else if (e2
&& e2
->speculative
)
634 cgraph_edge
*direct
, *indirect
;
637 e2
->speculative_call_info (direct
, indirect
, ref
);
640 int prob
= RDIV (direct
->count
* REG_BR_PROB_BASE
, e
->count
);
641 e
->make_speculative (direct
->callee
, direct
->count
,
642 RDIV (freq
* prob
, REG_BR_PROB_BASE
));
651 src
->release_body ();
652 inline_update_overall_summary (dst
);
654 /* TODO: if there is no match, we can scale up. */
655 src
->decl
= oldsrcdecl
;
658 /* Return true if call to DEST is known to be self-recusive call withing FUNC. */
661 recursive_call_p (tree func
, tree dest
)
663 struct cgraph_node
*dest_node
= cgraph_node::get_create (dest
);
664 struct cgraph_node
*cnode
= cgraph_node::get_create (func
);
666 return dest_node
->semantically_equivalent_p (cnode
);