1 /* Utilities for ipa analysis.
2 Copyright (C) 2005-2014 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"
31 #include "hard-reg-set.h"
34 #include "dominance.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
42 #include "tree-inline.h"
44 #include "langhooks.h"
45 #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"
59 #include "ipa-inline.h"
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. */
66 ipa_print_order (FILE* out
,
68 struct cgraph_node
** order
,
72 fprintf (out
, "\n\n ordered call graph: %s\n", note
);
74 for (i
= count
- 1; i
>= 0; i
--)
82 struct cgraph_node
**stack
;
84 struct cgraph_node
**result
;
86 splay_tree nodes_marked_new
;
88 bool allow_overwritable
;
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
98 ipa_reduced_postorder. ENV is a stack env and would be
99 unnecessary if C had nested functions. V is the node to start
103 searchc (struct searchc_env
* env
, struct cgraph_node
*v
,
104 bool (*ignore_edge
) (struct cgraph_edge
*))
106 struct cgraph_edge
*edge
;
107 struct ipa_dfs_info
*v_info
= (struct ipa_dfs_info
*) v
->aux
;
109 /* mark node as old */
110 v_info
->new_node
= false;
111 splay_tree_remove (env
->nodes_marked_new
, v
->uid
);
113 v_info
->dfn_number
= env
->count
;
114 v_info
->low_link
= env
->count
;
116 env
->stack
[(env
->stack_size
)++] = v
;
117 v_info
->on_stack
= true;
119 for (edge
= v
->callees
; edge
; edge
= edge
->next_callee
)
121 struct ipa_dfs_info
* w_info
;
122 enum availability avail
;
123 struct cgraph_node
*w
= edge
->callee
->ultimate_alias_target (&avail
);
125 if (!w
|| (ignore_edge
&& ignore_edge (edge
)))
129 && (avail
> AVAIL_INTERPOSABLE
130 || (env
->allow_overwritable
&& avail
== AVAIL_INTERPOSABLE
)))
132 w_info
= (struct ipa_dfs_info
*) w
->aux
;
133 if (w_info
->new_node
)
135 searchc (env
, w
, ignore_edge
);
137 (v_info
->low_link
< w_info
->low_link
) ?
138 v_info
->low_link
: w_info
->low_link
;
141 if ((w_info
->dfn_number
< v_info
->dfn_number
)
142 && (w_info
->on_stack
))
144 (w_info
->dfn_number
< v_info
->low_link
) ?
145 w_info
->dfn_number
: v_info
->low_link
;
150 if (v_info
->low_link
== v_info
->dfn_number
)
152 struct cgraph_node
*last
= NULL
;
153 struct cgraph_node
*x
;
154 struct ipa_dfs_info
*x_info
;
156 x
= env
->stack
[--(env
->stack_size
)];
157 x_info
= (struct ipa_dfs_info
*) x
->aux
;
158 x_info
->on_stack
= false;
159 x_info
->scc_no
= v_info
->dfn_number
;
163 x_info
->next_cycle
= last
;
167 env
->result
[env
->order_pos
++] = x
;
171 env
->result
[env
->order_pos
++] = v
;
175 /* Topsort the call graph by caller relation. Put the result in ORDER.
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.
181 Set ALLOW_OVERWRITABLE if nodes with such availability should be included.
182 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
183 for the topological sort. */
186 ipa_reduced_postorder (struct cgraph_node
**order
,
187 bool reduce
, bool allow_overwritable
,
188 bool (*ignore_edge
) (struct cgraph_edge
*))
190 struct cgraph_node
*node
;
191 struct searchc_env env
;
192 splay_tree_node result
;
193 env
.stack
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
197 env
.nodes_marked_new
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
200 env
.allow_overwritable
= allow_overwritable
;
202 FOR_EACH_DEFINED_FUNCTION (node
)
204 enum availability avail
= node
->get_availability ();
206 if (avail
> AVAIL_INTERPOSABLE
207 || (allow_overwritable
208 && (avail
== AVAIL_INTERPOSABLE
)))
210 /* Reuse the info if it is already there. */
211 struct ipa_dfs_info
*info
= (struct ipa_dfs_info
*) node
->aux
;
213 info
= XCNEW (struct ipa_dfs_info
);
214 info
->new_node
= true;
215 info
->on_stack
= false;
216 info
->next_cycle
= NULL
;
219 splay_tree_insert (env
.nodes_marked_new
,
220 (splay_tree_key
)node
->uid
,
221 (splay_tree_value
)node
);
226 result
= splay_tree_min (env
.nodes_marked_new
);
229 node
= (struct cgraph_node
*)result
->value
;
230 searchc (&env
, node
, ignore_edge
);
231 result
= splay_tree_min (env
.nodes_marked_new
);
233 splay_tree_delete (env
.nodes_marked_new
);
236 return env
.order_pos
;
239 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
243 ipa_free_postorder_info (void)
245 struct cgraph_node
*node
;
246 FOR_EACH_DEFINED_FUNCTION (node
)
248 /* Get rid of the aux information. */
257 /* Get the set of nodes for the cycle in the reduced call graph starting
261 ipa_get_nodes_in_cycle (struct cgraph_node
*node
)
263 vec
<cgraph_node
*> v
= vNULL
;
264 struct ipa_dfs_info
*node_dfs_info
;
268 node_dfs_info
= (struct ipa_dfs_info
*) node
->aux
;
269 node
= node_dfs_info
->next_cycle
;
274 /* Return true iff the CS is an edge within a strongly connected component as
275 computed by ipa_reduced_postorder. */
278 ipa_edge_within_scc (struct cgraph_edge
*cs
)
280 struct ipa_dfs_info
*caller_dfs
= (struct ipa_dfs_info
*) cs
->caller
->aux
;
281 struct ipa_dfs_info
*callee_dfs
;
282 struct cgraph_node
*callee
= cs
->callee
->function_symbol ();
284 callee_dfs
= (struct ipa_dfs_info
*) callee
->aux
;
287 && caller_dfs
->scc_no
== callee_dfs
->scc_no
);
290 struct postorder_stack
292 struct cgraph_node
*node
;
293 struct cgraph_edge
*edge
;
297 /* Fill array order with all nodes with output flag set in the reverse
298 topological order. Return the number of elements in the array.
299 FIXME: While walking, consider aliases, too. */
302 ipa_reverse_postorder (struct cgraph_node
**order
)
304 struct cgraph_node
*node
, *node2
;
307 struct cgraph_edge
*edge
;
309 struct ipa_ref
*ref
= NULL
;
311 struct postorder_stack
*stack
=
312 XCNEWVEC (struct postorder_stack
, symtab
->cgraph_count
);
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. */
318 FOR_EACH_FUNCTION (node
)
320 for (pass
= 0; pass
< 2; pass
++)
321 FOR_EACH_FUNCTION (node
)
324 || (!node
->address_taken
325 && !node
->global
.inlined_to
326 && !node
->alias
&& !node
->thunk
.thunk_p
327 && !node
->only_called_directly_p ())))
330 stack
[stack_size
].node
= node
;
331 stack
[stack_size
].edge
= node
->callers
;
332 stack
[stack_size
].ref
= 0;
333 node
->aux
= (void *)(size_t)1;
334 while (stack_size
>= 0)
339 while (stack
[stack_size
].edge
&& !node2
)
341 edge
= stack
[stack_size
].edge
;
342 node2
= edge
->caller
;
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. */
347 if (DECL_DISREGARD_INLINE_LIMITS (edge
->caller
->decl
)
348 && !DECL_DISREGARD_INLINE_LIMITS
349 (edge
->callee
->function_symbol ()->decl
))
352 for (; stack
[stack_size
].node
->iterate_referring (
353 stack
[stack_size
].ref
,
355 stack
[stack_size
].ref
++)
357 if (ref
->use
== IPA_REF_ALIAS
)
358 node2
= dyn_cast
<cgraph_node
*> (ref
->referring
);
364 stack
[++stack_size
].node
= node2
;
365 stack
[stack_size
].edge
= node2
->callers
;
366 stack
[stack_size
].ref
= 0;
367 node2
->aux
= (void *)(size_t)1;
370 order
[order_pos
++] = stack
[stack_size
--].node
;
374 FOR_EACH_FUNCTION (node
)
381 /* Given a memory reference T, will return the variable at the bottom
382 of the access. Unlike get_base_address, this will recurse through
386 get_base_var (tree t
)
388 while (!SSA_VAR_P (t
)
389 && (!CONSTANT_CLASS_P (t
))
390 && TREE_CODE (t
) != LABEL_DECL
391 && TREE_CODE (t
) != FUNCTION_DECL
392 && TREE_CODE (t
) != CONST_DECL
393 && TREE_CODE (t
) != CONSTRUCTOR
)
395 t
= TREE_OPERAND (t
, 0);
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. */
405 ipa_merge_profiles (struct cgraph_node
*dst
,
406 struct cgraph_node
*src
)
408 tree oldsrcdecl
= src
->decl
;
409 struct function
*srccfun
, *dstcfun
;
415 if (src
->frequency
< dst
->frequency
)
416 src
->frequency
= dst
->frequency
;
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
;
422 if (src
->profile_id
&& !dst
->profile_id
)
423 dst
->profile_id
= src
->profile_id
;
427 if (symtab
->dump_file
)
429 fprintf (symtab
->dump_file
, "Merging profiles of %s/%i to %s/%i\n",
430 xstrdup (src
->name ()), src
->order
,
431 xstrdup (dst
->name ()), dst
->order
);
433 dst
->count
+= src
->count
;
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. */
439 if (src
->decl
== dst
->decl
)
442 struct lto_in_decl_state temp
;
443 struct lto_in_decl_state
*state
;
445 /* We are going to move the decl, we want to remove its file decl data.
446 and link these with the new decl. */
447 temp
.fn_decl
= src
->decl
;
448 slot
= htab_find_slot (src
->lto_file_data
->function_decl_states
,
450 state
= (lto_in_decl_state
*)*slot
;
451 htab_clear_slot (src
->lto_file_data
->function_decl_states
, slot
);
454 /* Duplicate the decl and be sure it does not link into body of DST. */
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
;
461 /* Associate the decl state with new declaration, so LTO streamer
463 state
->fn_decl
= src
->decl
;
464 slot
= htab_find_slot (src
->lto_file_data
->function_decl_states
,
471 srccfun
= DECL_STRUCT_FUNCTION (src
->decl
);
472 dstcfun
= DECL_STRUCT_FUNCTION (dst
->decl
);
473 if (n_basic_blocks_for_fn (srccfun
)
474 != n_basic_blocks_for_fn (dstcfun
))
476 if (symtab
->dump_file
)
477 fprintf (symtab
->dump_file
,
478 "Giving up; number of basic block mismatch.\n");
481 else if (last_basic_block_for_fn (srccfun
)
482 != last_basic_block_for_fn (dstcfun
))
484 if (symtab
->dump_file
)
485 fprintf (symtab
->dump_file
,
486 "Giving up; last block mismatch.\n");
491 basic_block srcbb
, dstbb
;
493 FOR_ALL_BB_FN (srcbb
, srccfun
)
497 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
500 if (symtab
->dump_file
)
501 fprintf (symtab
->dump_file
,
502 "No matching block for bb %i.\n",
507 if (EDGE_COUNT (srcbb
->succs
) != EDGE_COUNT (dstbb
->succs
))
509 if (symtab
->dump_file
)
510 fprintf (symtab
->dump_file
,
511 "Edge count mistmatch for bb %i.\n",
516 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
518 edge srce
= EDGE_SUCC (srcbb
, i
);
519 edge dste
= EDGE_SUCC (dstbb
, i
);
520 if (srce
->dest
->index
!= dste
->dest
->index
)
522 if (symtab
->dump_file
)
523 fprintf (symtab
->dump_file
,
524 "Succ edge mistmatch for bb %i.\n",
534 struct cgraph_edge
*e
;
535 basic_block srcbb
, dstbb
;
537 /* TODO: merge also statement histograms. */
538 FOR_ALL_BB_FN (srcbb
, srccfun
)
542 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
543 dstbb
->count
+= srcbb
->count
;
544 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
546 edge srce
= EDGE_SUCC (srcbb
, i
);
547 edge dste
= EDGE_SUCC (dstbb
, i
);
548 dste
->count
+= srce
->count
;
553 compute_function_frequency ();
555 for (e
= dst
->callees
; e
; e
= e
->next_callee
)
557 gcc_assert (!e
->speculative
);
558 e
->count
= gimple_bb (e
->call_stmt
)->count
;
559 e
->frequency
= compute_call_stmt_bb_frequency
561 gimple_bb (e
->call_stmt
));
563 for (e
= dst
->indirect_calls
; e
; e
= e
->next_callee
)
565 gcc_assert (!e
->speculative
);
566 e
->count
= gimple_bb (e
->call_stmt
)->count
;
567 e
->frequency
= compute_call_stmt_bb_frequency
569 gimple_bb (e
->call_stmt
));
571 src
->release_body ();
572 inline_update_overall_summary (dst
);
574 /* TODO: if there is no match, we can scale up. */
575 src
->decl
= oldsrcdecl
;
578 /* Return true if call to DEST is known to be self-recusive call withing FUNC. */
581 recursive_call_p (tree func
, tree dest
)
583 struct cgraph_node
*dest_node
= cgraph_node::get_create (dest
);
584 struct cgraph_node
*cnode
= cgraph_node::get_create (func
);
586 return dest_node
->semantically_equivalent_p (cnode
);