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"
26 #include "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-expr.h"
32 #include "tree-inline.h"
34 #include "langhooks.h"
35 #include "splay-tree.h"
36 #include "ipa-utils.h"
37 #include "ipa-reference.h"
39 #include "diagnostic.h"
40 #include "langhooks.h"
41 #include "lto-streamer.h"
42 #include "ipa-inline.h"
44 /* Debugging function for postorder and inorder code. NOTE is a string
45 that is printed before the nodes are printed. ORDER is an array of
46 cgraph_nodes that has COUNT useful nodes in it. */
49 ipa_print_order (FILE* out
,
51 struct cgraph_node
** order
,
55 fprintf (out
, "\n\n ordered call graph: %s\n", note
);
57 for (i
= count
- 1; i
>= 0; i
--)
65 struct cgraph_node
**stack
;
67 struct cgraph_node
**result
;
69 splay_tree nodes_marked_new
;
71 bool allow_overwritable
;
75 /* This is an implementation of Tarjan's strongly connected region
76 finder as reprinted in Aho Hopcraft and Ullman's The Design and
77 Analysis of Computer Programs (1975) pages 192-193. This version
78 has been customized for cgraph_nodes. The env parameter is because
79 it is recursive and there are no nested functions here. This
80 function should only be called from itself or
81 ipa_reduced_postorder. ENV is a stack env and would be
82 unnecessary if C had nested functions. V is the node to start
86 searchc (struct searchc_env
* env
, struct cgraph_node
*v
,
87 bool (*ignore_edge
) (struct cgraph_edge
*))
89 struct cgraph_edge
*edge
;
90 struct ipa_dfs_info
*v_info
= (struct ipa_dfs_info
*) v
->aux
;
92 /* mark node as old */
93 v_info
->new_node
= false;
94 splay_tree_remove (env
->nodes_marked_new
, v
->uid
);
96 v_info
->dfn_number
= env
->count
;
97 v_info
->low_link
= env
->count
;
99 env
->stack
[(env
->stack_size
)++] = v
;
100 v_info
->on_stack
= true;
102 for (edge
= v
->callees
; edge
; edge
= edge
->next_callee
)
104 struct ipa_dfs_info
* w_info
;
105 enum availability avail
;
106 struct cgraph_node
*w
= edge
->callee
->ultimate_alias_target (&avail
);
108 if (!w
|| (ignore_edge
&& ignore_edge (edge
)))
112 && (avail
> AVAIL_INTERPOSABLE
113 || (env
->allow_overwritable
&& avail
== AVAIL_INTERPOSABLE
)))
115 w_info
= (struct ipa_dfs_info
*) w
->aux
;
116 if (w_info
->new_node
)
118 searchc (env
, w
, ignore_edge
);
120 (v_info
->low_link
< w_info
->low_link
) ?
121 v_info
->low_link
: w_info
->low_link
;
124 if ((w_info
->dfn_number
< v_info
->dfn_number
)
125 && (w_info
->on_stack
))
127 (w_info
->dfn_number
< v_info
->low_link
) ?
128 w_info
->dfn_number
: v_info
->low_link
;
133 if (v_info
->low_link
== v_info
->dfn_number
)
135 struct cgraph_node
*last
= NULL
;
136 struct cgraph_node
*x
;
137 struct ipa_dfs_info
*x_info
;
139 x
= env
->stack
[--(env
->stack_size
)];
140 x_info
= (struct ipa_dfs_info
*) x
->aux
;
141 x_info
->on_stack
= false;
142 x_info
->scc_no
= v_info
->dfn_number
;
146 x_info
->next_cycle
= last
;
150 env
->result
[env
->order_pos
++] = x
;
154 env
->result
[env
->order_pos
++] = v
;
158 /* Topsort the call graph by caller relation. Put the result in ORDER.
160 The REDUCE flag is true if you want the cycles reduced to single nodes.
161 You can use ipa_get_nodes_in_cycle to obtain a vector containing all real
162 call graph nodes in a reduced node.
164 Set ALLOW_OVERWRITABLE if nodes with such availability should be included.
165 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
166 for the topological sort. */
169 ipa_reduced_postorder (struct cgraph_node
**order
,
170 bool reduce
, bool allow_overwritable
,
171 bool (*ignore_edge
) (struct cgraph_edge
*))
173 struct cgraph_node
*node
;
174 struct searchc_env env
;
175 splay_tree_node result
;
176 env
.stack
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
180 env
.nodes_marked_new
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
183 env
.allow_overwritable
= allow_overwritable
;
185 FOR_EACH_DEFINED_FUNCTION (node
)
187 enum availability avail
= node
->get_availability ();
189 if (avail
> AVAIL_INTERPOSABLE
190 || (allow_overwritable
191 && (avail
== AVAIL_INTERPOSABLE
)))
193 /* Reuse the info if it is already there. */
194 struct ipa_dfs_info
*info
= (struct ipa_dfs_info
*) node
->aux
;
196 info
= XCNEW (struct ipa_dfs_info
);
197 info
->new_node
= true;
198 info
->on_stack
= false;
199 info
->next_cycle
= NULL
;
202 splay_tree_insert (env
.nodes_marked_new
,
203 (splay_tree_key
)node
->uid
,
204 (splay_tree_value
)node
);
209 result
= splay_tree_min (env
.nodes_marked_new
);
212 node
= (struct cgraph_node
*)result
->value
;
213 searchc (&env
, node
, ignore_edge
);
214 result
= splay_tree_min (env
.nodes_marked_new
);
216 splay_tree_delete (env
.nodes_marked_new
);
219 return env
.order_pos
;
222 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
226 ipa_free_postorder_info (void)
228 struct cgraph_node
*node
;
229 FOR_EACH_DEFINED_FUNCTION (node
)
231 /* Get rid of the aux information. */
240 /* Get the set of nodes for the cycle in the reduced call graph starting
244 ipa_get_nodes_in_cycle (struct cgraph_node
*node
)
246 vec
<cgraph_node
*> v
= vNULL
;
247 struct ipa_dfs_info
*node_dfs_info
;
251 node_dfs_info
= (struct ipa_dfs_info
*) node
->aux
;
252 node
= node_dfs_info
->next_cycle
;
257 /* Return true iff the CS is an edge within a strongly connected component as
258 computed by ipa_reduced_postorder. */
261 ipa_edge_within_scc (struct cgraph_edge
*cs
)
263 struct ipa_dfs_info
*caller_dfs
= (struct ipa_dfs_info
*) cs
->caller
->aux
;
264 struct ipa_dfs_info
*callee_dfs
;
265 struct cgraph_node
*callee
= cs
->callee
->function_symbol ();
267 callee_dfs
= (struct ipa_dfs_info
*) callee
->aux
;
270 && caller_dfs
->scc_no
== callee_dfs
->scc_no
);
273 struct postorder_stack
275 struct cgraph_node
*node
;
276 struct cgraph_edge
*edge
;
280 /* Fill array order with all nodes with output flag set in the reverse
281 topological order. Return the number of elements in the array.
282 FIXME: While walking, consider aliases, too. */
285 ipa_reverse_postorder (struct cgraph_node
**order
)
287 struct cgraph_node
*node
, *node2
;
290 struct cgraph_edge
*edge
;
292 struct ipa_ref
*ref
= NULL
;
294 struct postorder_stack
*stack
=
295 XCNEWVEC (struct postorder_stack
, cgraph_n_nodes
);
297 /* We have to deal with cycles nicely, so use a depth first traversal
298 output algorithm. Ignore the fact that some functions won't need
299 to be output and put them into order as well, so we get dependencies
300 right through inline functions. */
301 FOR_EACH_FUNCTION (node
)
303 for (pass
= 0; pass
< 2; pass
++)
304 FOR_EACH_FUNCTION (node
)
307 || (!node
->address_taken
308 && !node
->global
.inlined_to
309 && !node
->alias
&& !node
->thunk
.thunk_p
310 && !node
->only_called_directly_p ())))
313 stack
[stack_size
].node
= node
;
314 stack
[stack_size
].edge
= node
->callers
;
315 stack
[stack_size
].ref
= 0;
316 node
->aux
= (void *)(size_t)1;
317 while (stack_size
>= 0)
322 while (stack
[stack_size
].edge
&& !node2
)
324 edge
= stack
[stack_size
].edge
;
325 node2
= edge
->caller
;
326 stack
[stack_size
].edge
= edge
->next_caller
;
327 /* Break possible cycles involving always-inline
328 functions by ignoring edges from always-inline
329 functions to non-always-inline functions. */
330 if (DECL_DISREGARD_INLINE_LIMITS (edge
->caller
->decl
)
331 && !DECL_DISREGARD_INLINE_LIMITS
332 (edge
->callee
->function_symbol ()->decl
))
335 for (; stack
[stack_size
].node
->iterate_referring (
336 stack
[stack_size
].ref
,
338 stack
[stack_size
].ref
++)
340 if (ref
->use
== IPA_REF_ALIAS
)
341 node2
= dyn_cast
<cgraph_node
*> (ref
->referring
);
347 stack
[++stack_size
].node
= node2
;
348 stack
[stack_size
].edge
= node2
->callers
;
349 stack
[stack_size
].ref
= 0;
350 node2
->aux
= (void *)(size_t)1;
353 order
[order_pos
++] = stack
[stack_size
--].node
;
357 FOR_EACH_FUNCTION (node
)
364 /* Given a memory reference T, will return the variable at the bottom
365 of the access. Unlike get_base_address, this will recurse through
369 get_base_var (tree t
)
371 while (!SSA_VAR_P (t
)
372 && (!CONSTANT_CLASS_P (t
))
373 && TREE_CODE (t
) != LABEL_DECL
374 && TREE_CODE (t
) != FUNCTION_DECL
375 && TREE_CODE (t
) != CONST_DECL
376 && TREE_CODE (t
) != CONSTRUCTOR
)
378 t
= TREE_OPERAND (t
, 0);
384 /* SRC and DST are going to be merged. Take SRC's profile and merge it into
385 DST so it is not going to be lost. Destroy SRC's body on the way. */
388 ipa_merge_profiles (struct cgraph_node
*dst
,
389 struct cgraph_node
*src
)
391 tree oldsrcdecl
= src
->decl
;
392 struct function
*srccfun
, *dstcfun
;
398 if (src
->frequency
< dst
->frequency
)
399 src
->frequency
= dst
->frequency
;
401 /* Time profiles are merged. */
402 if (dst
->tp_first_run
> src
->tp_first_run
&& src
->tp_first_run
)
403 dst
->tp_first_run
= src
->tp_first_run
;
405 if (src
->profile_id
&& !dst
->profile_id
)
406 dst
->profile_id
= src
->profile_id
;
410 if (cgraph_dump_file
)
412 fprintf (cgraph_dump_file
, "Merging profiles of %s/%i to %s/%i\n",
413 xstrdup (src
->name ()), src
->order
,
414 xstrdup (dst
->name ()), dst
->order
);
416 dst
->count
+= src
->count
;
418 /* This is ugly. We need to get both function bodies into memory.
419 If declaration is merged, we need to duplicate it to be able
420 to load body that is being replaced. This makes symbol table
421 temporarily inconsistent. */
422 if (src
->decl
== dst
->decl
)
425 struct lto_in_decl_state temp
;
426 struct lto_in_decl_state
*state
;
428 /* We are going to move the decl, we want to remove its file decl data.
429 and link these with the new decl. */
430 temp
.fn_decl
= src
->decl
;
431 slot
= htab_find_slot (src
->lto_file_data
->function_decl_states
,
433 state
= (lto_in_decl_state
*)*slot
;
434 htab_clear_slot (src
->lto_file_data
->function_decl_states
, slot
);
437 /* Duplicate the decl and be sure it does not link into body of DST. */
438 src
->decl
= copy_node (src
->decl
);
439 DECL_STRUCT_FUNCTION (src
->decl
) = NULL
;
440 DECL_ARGUMENTS (src
->decl
) = NULL
;
441 DECL_INITIAL (src
->decl
) = NULL
;
442 DECL_RESULT (src
->decl
) = NULL
;
444 /* Associate the decl state with new declaration, so LTO streamer
446 state
->fn_decl
= src
->decl
;
447 slot
= htab_find_slot (src
->lto_file_data
->function_decl_states
,
454 srccfun
= DECL_STRUCT_FUNCTION (src
->decl
);
455 dstcfun
= DECL_STRUCT_FUNCTION (dst
->decl
);
456 if (n_basic_blocks_for_fn (srccfun
)
457 != n_basic_blocks_for_fn (dstcfun
))
459 if (cgraph_dump_file
)
460 fprintf (cgraph_dump_file
,
461 "Giving up; number of basic block mismatch.\n");
464 else if (last_basic_block_for_fn (srccfun
)
465 != last_basic_block_for_fn (dstcfun
))
467 if (cgraph_dump_file
)
468 fprintf (cgraph_dump_file
,
469 "Giving up; last block mismatch.\n");
474 basic_block srcbb
, dstbb
;
476 FOR_ALL_BB_FN (srcbb
, srccfun
)
480 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
483 if (cgraph_dump_file
)
484 fprintf (cgraph_dump_file
,
485 "No matching block for bb %i.\n",
490 if (EDGE_COUNT (srcbb
->succs
) != EDGE_COUNT (dstbb
->succs
))
492 if (cgraph_dump_file
)
493 fprintf (cgraph_dump_file
,
494 "Edge count mistmatch for bb %i.\n",
499 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
501 edge srce
= EDGE_SUCC (srcbb
, i
);
502 edge dste
= EDGE_SUCC (dstbb
, i
);
503 if (srce
->dest
->index
!= dste
->dest
->index
)
505 if (cgraph_dump_file
)
506 fprintf (cgraph_dump_file
,
507 "Succ edge mistmatch for bb %i.\n",
517 struct cgraph_edge
*e
;
518 basic_block srcbb
, dstbb
;
520 /* TODO: merge also statement histograms. */
521 FOR_ALL_BB_FN (srcbb
, srccfun
)
525 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
526 dstbb
->count
+= srcbb
->count
;
527 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
529 edge srce
= EDGE_SUCC (srcbb
, i
);
530 edge dste
= EDGE_SUCC (dstbb
, i
);
531 dste
->count
+= srce
->count
;
536 compute_function_frequency ();
538 for (e
= dst
->callees
; e
; e
= e
->next_callee
)
540 gcc_assert (!e
->speculative
);
541 e
->count
= gimple_bb (e
->call_stmt
)->count
;
542 e
->frequency
= compute_call_stmt_bb_frequency
544 gimple_bb (e
->call_stmt
));
546 for (e
= dst
->indirect_calls
; e
; e
= e
->next_callee
)
548 gcc_assert (!e
->speculative
);
549 e
->count
= gimple_bb (e
->call_stmt
)->count
;
550 e
->frequency
= compute_call_stmt_bb_frequency
552 gimple_bb (e
->call_stmt
));
554 src
->release_body ();
555 inline_update_overall_summary (dst
);
557 /* TODO: if there is no match, we can scale up. */
558 src
->decl
= oldsrcdecl
;
561 /* Return true if call to DEST is known to be self-recusive call withing FUNC. */
564 recursive_call_p (tree func
, tree dest
)
566 struct cgraph_node
*dest_node
= cgraph_node::get_create (dest
);
567 struct cgraph_node
*cnode
= cgraph_node::get_create (func
);
569 return dest_node
->semantically_equivalent_p (cnode
);