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"
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. Destroy SRC's body on the way. */
406 ipa_merge_profiles (struct cgraph_node
*dst
,
407 struct cgraph_node
*src
)
409 tree oldsrcdecl
= src
->decl
;
410 struct function
*srccfun
, *dstcfun
;
416 if (src
->frequency
< dst
->frequency
)
417 src
->frequency
= dst
->frequency
;
419 /* Time profiles are merged. */
420 if (dst
->tp_first_run
> src
->tp_first_run
&& src
->tp_first_run
)
421 dst
->tp_first_run
= src
->tp_first_run
;
423 if (src
->profile_id
&& !dst
->profile_id
)
424 dst
->profile_id
= src
->profile_id
;
428 if (symtab
->dump_file
)
430 fprintf (symtab
->dump_file
, "Merging profiles of %s/%i to %s/%i\n",
431 xstrdup_for_dump (src
->name ()), src
->order
,
432 xstrdup_for_dump (dst
->name ()), dst
->order
);
434 dst
->count
+= src
->count
;
436 /* This is ugly. We need to get both function bodies into memory.
437 If declaration is merged, we need to duplicate it to be able
438 to load body that is being replaced. This makes symbol table
439 temporarily inconsistent. */
440 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 lto_in_decl_state
**slot
449 = src
->lto_file_data
->function_decl_states
->find_slot (&temp
,
452 src
->lto_file_data
->function_decl_states
->clear_slot (slot
);
455 /* Duplicate the decl and be sure it does not link into body of DST. */
456 src
->decl
= copy_node (src
->decl
);
457 DECL_STRUCT_FUNCTION (src
->decl
) = NULL
;
458 DECL_ARGUMENTS (src
->decl
) = NULL
;
459 DECL_INITIAL (src
->decl
) = NULL
;
460 DECL_RESULT (src
->decl
) = NULL
;
462 /* Associate the decl state with new declaration, so LTO streamer
464 state
->fn_decl
= src
->decl
;
466 = src
->lto_file_data
->function_decl_states
->find_slot (state
, INSERT
);
472 srccfun
= DECL_STRUCT_FUNCTION (src
->decl
);
473 dstcfun
= DECL_STRUCT_FUNCTION (dst
->decl
);
474 if (n_basic_blocks_for_fn (srccfun
)
475 != n_basic_blocks_for_fn (dstcfun
))
477 if (symtab
->dump_file
)
478 fprintf (symtab
->dump_file
,
479 "Giving up; number of basic block mismatch.\n");
482 else if (last_basic_block_for_fn (srccfun
)
483 != last_basic_block_for_fn (dstcfun
))
485 if (symtab
->dump_file
)
486 fprintf (symtab
->dump_file
,
487 "Giving up; last block mismatch.\n");
492 basic_block srcbb
, dstbb
;
494 FOR_ALL_BB_FN (srcbb
, srccfun
)
498 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
501 if (symtab
->dump_file
)
502 fprintf (symtab
->dump_file
,
503 "No matching block for bb %i.\n",
508 if (EDGE_COUNT (srcbb
->succs
) != EDGE_COUNT (dstbb
->succs
))
510 if (symtab
->dump_file
)
511 fprintf (symtab
->dump_file
,
512 "Edge count mistmatch for bb %i.\n",
517 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
519 edge srce
= EDGE_SUCC (srcbb
, i
);
520 edge dste
= EDGE_SUCC (dstbb
, i
);
521 if (srce
->dest
->index
!= dste
->dest
->index
)
523 if (symtab
->dump_file
)
524 fprintf (symtab
->dump_file
,
525 "Succ edge mistmatch for bb %i.\n",
535 struct cgraph_edge
*e
;
536 basic_block srcbb
, dstbb
;
538 /* TODO: merge also statement histograms. */
539 FOR_ALL_BB_FN (srcbb
, srccfun
)
543 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
544 dstbb
->count
+= srcbb
->count
;
545 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
547 edge srce
= EDGE_SUCC (srcbb
, i
);
548 edge dste
= EDGE_SUCC (dstbb
, i
);
549 dste
->count
+= srce
->count
;
554 compute_function_frequency ();
556 for (e
= dst
->callees
; e
; e
= e
->next_callee
)
558 gcc_assert (!e
->speculative
);
559 e
->count
= gimple_bb (e
->call_stmt
)->count
;
560 e
->frequency
= compute_call_stmt_bb_frequency
562 gimple_bb (e
->call_stmt
));
564 for (e
= dst
->indirect_calls
; e
; e
= e
->next_callee
)
566 gcc_assert (!e
->speculative
);
567 e
->count
= gimple_bb (e
->call_stmt
)->count
;
568 e
->frequency
= compute_call_stmt_bb_frequency
570 gimple_bb (e
->call_stmt
));
572 src
->release_body ();
573 inline_update_overall_summary (dst
);
575 /* TODO: if there is no match, we can scale up. */
576 src
->decl
= oldsrcdecl
;
579 /* Return true if call to DEST is known to be self-recusive call withing FUNC. */
582 recursive_call_p (tree func
, tree dest
)
584 struct cgraph_node
*dest_node
= cgraph_node::get_create (dest
);
585 struct cgraph_node
*cnode
= cgraph_node::get_create (func
);
587 return dest_node
->semantically_equivalent_p (cnode
);