1 /* Utilities for ipa analysis.
2 Copyright (C) 2005-2013 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"
27 #include "tree-inline.h"
29 #include "langhooks.h"
30 #include "pointer-set.h"
31 #include "splay-tree.h"
33 #include "ipa-utils.h"
34 #include "ipa-reference.h"
38 #include "diagnostic.h"
39 #include "langhooks.h"
40 #include "lto-streamer.h"
41 #include "ipa-inline.h"
43 /* Debugging function for postorder and inorder code. NOTE is a string
44 that is printed before the nodes are printed. ORDER is an array of
45 cgraph_nodes that has COUNT useful nodes in it. */
48 ipa_print_order (FILE* out
,
50 struct cgraph_node
** order
,
54 fprintf (out
, "\n\n ordered call graph: %s\n", note
);
56 for (i
= count
- 1; i
>= 0; i
--)
57 dump_cgraph_node(dump_file
, order
[i
]);
64 struct cgraph_node
**stack
;
66 struct cgraph_node
**result
;
68 splay_tree nodes_marked_new
;
70 bool allow_overwritable
;
74 /* This is an implementation of Tarjan's strongly connected region
75 finder as reprinted in Aho Hopcraft and Ullman's The Design and
76 Analysis of Computer Programs (1975) pages 192-193. This version
77 has been customized for cgraph_nodes. The env parameter is because
78 it is recursive and there are no nested functions here. This
79 function should only be called from itself or
80 ipa_reduced_postorder. ENV is a stack env and would be
81 unnecessary if C had nested functions. V is the node to start
85 searchc (struct searchc_env
* env
, struct cgraph_node
*v
,
86 bool (*ignore_edge
) (struct cgraph_edge
*))
88 struct cgraph_edge
*edge
;
89 struct ipa_dfs_info
*v_info
= (struct ipa_dfs_info
*) v
->symbol
.aux
;
91 /* mark node as old */
92 v_info
->new_node
= false;
93 splay_tree_remove (env
->nodes_marked_new
, v
->uid
);
95 v_info
->dfn_number
= env
->count
;
96 v_info
->low_link
= env
->count
;
98 env
->stack
[(env
->stack_size
)++] = v
;
99 v_info
->on_stack
= true;
101 for (edge
= v
->callees
; edge
; edge
= edge
->next_callee
)
103 struct ipa_dfs_info
* w_info
;
104 enum availability avail
;
105 struct cgraph_node
*w
= cgraph_function_or_thunk_node (edge
->callee
, &avail
);
107 if (!w
|| (ignore_edge
&& ignore_edge (edge
)))
111 && (avail
> AVAIL_OVERWRITABLE
112 || (env
->allow_overwritable
&& avail
== AVAIL_OVERWRITABLE
)))
114 w_info
= (struct ipa_dfs_info
*) w
->symbol
.aux
;
115 if (w_info
->new_node
)
117 searchc (env
, w
, ignore_edge
);
119 (v_info
->low_link
< w_info
->low_link
) ?
120 v_info
->low_link
: w_info
->low_link
;
123 if ((w_info
->dfn_number
< v_info
->dfn_number
)
124 && (w_info
->on_stack
))
126 (w_info
->dfn_number
< v_info
->low_link
) ?
127 w_info
->dfn_number
: v_info
->low_link
;
132 if (v_info
->low_link
== v_info
->dfn_number
)
134 struct cgraph_node
*last
= NULL
;
135 struct cgraph_node
*x
;
136 struct ipa_dfs_info
*x_info
;
138 x
= env
->stack
[--(env
->stack_size
)];
139 x_info
= (struct ipa_dfs_info
*) x
->symbol
.aux
;
140 x_info
->on_stack
= false;
141 x_info
->scc_no
= v_info
->dfn_number
;
145 x_info
->next_cycle
= last
;
149 env
->result
[env
->order_pos
++] = x
;
153 env
->result
[env
->order_pos
++] = v
;
157 /* Topsort the call graph by caller relation. Put the result in ORDER.
159 The REDUCE flag is true if you want the cycles reduced to single nodes.
160 You can use ipa_get_nodes_in_cycle to obtain a vector containing all real
161 call graph nodes in a reduced node.
163 Set ALLOW_OVERWRITABLE if nodes with such availability should be included.
164 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
165 for the topological sort. */
168 ipa_reduced_postorder (struct cgraph_node
**order
,
169 bool reduce
, bool allow_overwritable
,
170 bool (*ignore_edge
) (struct cgraph_edge
*))
172 struct cgraph_node
*node
;
173 struct searchc_env env
;
174 splay_tree_node result
;
175 env
.stack
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
179 env
.nodes_marked_new
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
182 env
.allow_overwritable
= allow_overwritable
;
184 FOR_EACH_DEFINED_FUNCTION (node
)
186 enum availability avail
= cgraph_function_body_availability (node
);
188 if (avail
> AVAIL_OVERWRITABLE
189 || (allow_overwritable
190 && (avail
== AVAIL_OVERWRITABLE
)))
192 /* Reuse the info if it is already there. */
193 struct ipa_dfs_info
*info
= (struct ipa_dfs_info
*) node
->symbol
.aux
;
195 info
= XCNEW (struct ipa_dfs_info
);
196 info
->new_node
= true;
197 info
->on_stack
= false;
198 info
->next_cycle
= NULL
;
199 node
->symbol
.aux
= info
;
201 splay_tree_insert (env
.nodes_marked_new
,
202 (splay_tree_key
)node
->uid
,
203 (splay_tree_value
)node
);
206 node
->symbol
.aux
= NULL
;
208 result
= splay_tree_min (env
.nodes_marked_new
);
211 node
= (struct cgraph_node
*)result
->value
;
212 searchc (&env
, node
, ignore_edge
);
213 result
= splay_tree_min (env
.nodes_marked_new
);
215 splay_tree_delete (env
.nodes_marked_new
);
218 return env
.order_pos
;
221 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
225 ipa_free_postorder_info (void)
227 struct cgraph_node
*node
;
228 FOR_EACH_DEFINED_FUNCTION (node
)
230 /* Get rid of the aux information. */
231 if (node
->symbol
.aux
)
233 free (node
->symbol
.aux
);
234 node
->symbol
.aux
= NULL
;
239 /* Get the set of nodes for the cycle in the reduced call graph starting
243 ipa_get_nodes_in_cycle (struct cgraph_node
*node
)
245 vec
<cgraph_node_ptr
> v
= vNULL
;
246 struct ipa_dfs_info
*node_dfs_info
;
250 node_dfs_info
= (struct ipa_dfs_info
*) node
->symbol
.aux
;
251 node
= node_dfs_info
->next_cycle
;
256 struct postorder_stack
258 struct cgraph_node
*node
;
259 struct cgraph_edge
*edge
;
263 /* Fill array order with all nodes with output flag set in the reverse
264 topological order. Return the number of elements in the array.
265 FIXME: While walking, consider aliases, too. */
268 ipa_reverse_postorder (struct cgraph_node
**order
)
270 struct cgraph_node
*node
, *node2
;
273 struct cgraph_edge
*edge
;
277 struct postorder_stack
*stack
=
278 XCNEWVEC (struct postorder_stack
, cgraph_n_nodes
);
280 /* We have to deal with cycles nicely, so use a depth first traversal
281 output algorithm. Ignore the fact that some functions won't need
282 to be output and put them into order as well, so we get dependencies
283 right through inline functions. */
284 FOR_EACH_FUNCTION (node
)
285 node
->symbol
.aux
= NULL
;
286 for (pass
= 0; pass
< 2; pass
++)
287 FOR_EACH_FUNCTION (node
)
288 if (!node
->symbol
.aux
290 || (!node
->symbol
.address_taken
291 && !node
->global
.inlined_to
292 && !node
->symbol
.alias
&& !node
->thunk
.thunk_p
293 && !cgraph_only_called_directly_p (node
))))
296 stack
[stack_size
].node
= node
;
297 stack
[stack_size
].edge
= node
->callers
;
298 stack
[stack_size
].ref
= 0;
299 node
->symbol
.aux
= (void *)(size_t)1;
300 while (stack_size
>= 0)
305 while (stack
[stack_size
].edge
&& !node2
)
307 edge
= stack
[stack_size
].edge
;
308 node2
= edge
->caller
;
309 stack
[stack_size
].edge
= edge
->next_caller
;
310 /* Break possible cycles involving always-inline
311 functions by ignoring edges from always-inline
312 functions to non-always-inline functions. */
313 if (DECL_DISREGARD_INLINE_LIMITS (edge
->caller
->symbol
.decl
)
314 && !DECL_DISREGARD_INLINE_LIMITS
315 (cgraph_function_node (edge
->callee
, NULL
)->symbol
.decl
))
318 for (;ipa_ref_list_referring_iterate (&stack
[stack_size
].node
->symbol
.ref_list
,
319 stack
[stack_size
].ref
,
321 stack
[stack_size
].ref
++)
323 if (ref
->use
== IPA_REF_ALIAS
)
324 node2
= ipa_ref_referring_node (ref
);
328 if (!node2
->symbol
.aux
)
330 stack
[++stack_size
].node
= node2
;
331 stack
[stack_size
].edge
= node2
->callers
;
332 stack
[stack_size
].ref
= 0;
333 node2
->symbol
.aux
= (void *)(size_t)1;
336 order
[order_pos
++] = stack
[stack_size
--].node
;
340 FOR_EACH_FUNCTION (node
)
341 node
->symbol
.aux
= NULL
;
347 /* Given a memory reference T, will return the variable at the bottom
348 of the access. Unlike get_base_address, this will recurse through
352 get_base_var (tree t
)
354 while (!SSA_VAR_P (t
)
355 && (!CONSTANT_CLASS_P (t
))
356 && TREE_CODE (t
) != LABEL_DECL
357 && TREE_CODE (t
) != FUNCTION_DECL
358 && TREE_CODE (t
) != CONST_DECL
359 && TREE_CODE (t
) != CONSTRUCTOR
)
361 t
= TREE_OPERAND (t
, 0);
367 /* Create a new cgraph node set. */
370 cgraph_node_set_new (void)
372 cgraph_node_set new_node_set
;
374 new_node_set
= XCNEW (struct cgraph_node_set_def
);
375 new_node_set
->map
= pointer_map_create ();
376 new_node_set
->nodes
.create (0);
381 /* Add cgraph_node NODE to cgraph_node_set SET. */
384 cgraph_node_set_add (cgraph_node_set set
, struct cgraph_node
*node
)
388 slot
= pointer_map_insert (set
->map
, node
);
392 int index
= (size_t) *slot
- 1;
393 gcc_checking_assert ((set
->nodes
[index
]
398 *slot
= (void *)(size_t) (set
->nodes
.length () + 1);
400 /* Insert into node vector. */
401 set
->nodes
.safe_push (node
);
405 /* Remove cgraph_node NODE from cgraph_node_set SET. */
408 cgraph_node_set_remove (cgraph_node_set set
, struct cgraph_node
*node
)
410 void **slot
, **last_slot
;
412 struct cgraph_node
*last_node
;
414 slot
= pointer_map_contains (set
->map
, node
);
415 if (slot
== NULL
|| !*slot
)
418 index
= (size_t) *slot
- 1;
419 gcc_checking_assert (set
->nodes
[index
]
422 /* Remove from vector. We do this by swapping node with the last element
424 last_node
= set
->nodes
.pop ();
425 if (last_node
!= node
)
427 last_slot
= pointer_map_contains (set
->map
, last_node
);
428 gcc_checking_assert (last_slot
&& *last_slot
);
429 *last_slot
= (void *)(size_t) (index
+ 1);
431 /* Move the last element to the original spot of NODE. */
432 set
->nodes
[index
] = last_node
;
435 /* Remove element from hash table. */
440 /* Find NODE in SET and return an iterator to it if found. A null iterator
441 is returned if NODE is not in SET. */
443 cgraph_node_set_iterator
444 cgraph_node_set_find (cgraph_node_set set
, struct cgraph_node
*node
)
447 cgraph_node_set_iterator csi
;
449 slot
= pointer_map_contains (set
->map
, node
);
450 if (slot
== NULL
|| !*slot
)
451 csi
.index
= (unsigned) ~0;
453 csi
.index
= (size_t)*slot
- 1;
460 /* Dump content of SET to file F. */
463 dump_cgraph_node_set (FILE *f
, cgraph_node_set set
)
465 cgraph_node_set_iterator iter
;
467 for (iter
= csi_start (set
); !csi_end_p (iter
); csi_next (&iter
))
469 struct cgraph_node
*node
= csi_node (iter
);
470 fprintf (f
, " %s/%i", cgraph_node_name (node
), node
->symbol
.order
);
476 /* Dump content of SET to stderr. */
479 debug_cgraph_node_set (cgraph_node_set set
)
481 dump_cgraph_node_set (stderr
, set
);
485 /* Free varpool node set. */
488 free_cgraph_node_set (cgraph_node_set set
)
490 set
->nodes
.release ();
491 pointer_map_destroy (set
->map
);
496 /* Create a new varpool node set. */
499 varpool_node_set_new (void)
501 varpool_node_set new_node_set
;
503 new_node_set
= XCNEW (struct varpool_node_set_def
);
504 new_node_set
->map
= pointer_map_create ();
505 new_node_set
->nodes
.create (0);
510 /* Add varpool_node NODE to varpool_node_set SET. */
513 varpool_node_set_add (varpool_node_set set
, struct varpool_node
*node
)
517 slot
= pointer_map_insert (set
->map
, node
);
521 int index
= (size_t) *slot
- 1;
522 gcc_checking_assert ((set
->nodes
[index
]
527 *slot
= (void *)(size_t) (set
->nodes
.length () + 1);
529 /* Insert into node vector. */
530 set
->nodes
.safe_push (node
);
534 /* Remove varpool_node NODE from varpool_node_set SET. */
537 varpool_node_set_remove (varpool_node_set set
, struct varpool_node
*node
)
539 void **slot
, **last_slot
;
541 struct varpool_node
*last_node
;
543 slot
= pointer_map_contains (set
->map
, node
);
544 if (slot
== NULL
|| !*slot
)
547 index
= (size_t) *slot
- 1;
548 gcc_checking_assert (set
->nodes
[index
]
551 /* Remove from vector. We do this by swapping node with the last element
553 last_node
= set
->nodes
.pop ();
554 if (last_node
!= node
)
556 last_slot
= pointer_map_contains (set
->map
, last_node
);
557 gcc_checking_assert (last_slot
&& *last_slot
);
558 *last_slot
= (void *)(size_t) (index
+ 1);
560 /* Move the last element to the original spot of NODE. */
561 set
->nodes
[index
] = last_node
;
564 /* Remove element from hash table. */
569 /* Find NODE in SET and return an iterator to it if found. A null iterator
570 is returned if NODE is not in SET. */
572 varpool_node_set_iterator
573 varpool_node_set_find (varpool_node_set set
, struct varpool_node
*node
)
576 varpool_node_set_iterator vsi
;
578 slot
= pointer_map_contains (set
->map
, node
);
579 if (slot
== NULL
|| !*slot
)
580 vsi
.index
= (unsigned) ~0;
582 vsi
.index
= (size_t)*slot
- 1;
589 /* Dump content of SET to file F. */
592 dump_varpool_node_set (FILE *f
, varpool_node_set set
)
594 varpool_node_set_iterator iter
;
596 for (iter
= vsi_start (set
); !vsi_end_p (iter
); vsi_next (&iter
))
598 struct varpool_node
*node
= vsi_node (iter
);
599 fprintf (f
, " %s", varpool_node_name (node
));
605 /* Free varpool node set. */
608 free_varpool_node_set (varpool_node_set set
)
610 set
->nodes
.release ();
611 pointer_map_destroy (set
->map
);
616 /* Dump content of SET to stderr. */
619 debug_varpool_node_set (varpool_node_set set
)
621 dump_varpool_node_set (stderr
, set
);
625 /* SRC and DST are going to be merged. Take SRC's profile and merge it into
626 DST so it is not going to be lost. Destroy SRC's body on the way. */
629 ipa_merge_profiles (struct cgraph_node
*dst
,
630 struct cgraph_node
*src
)
632 tree oldsrcdecl
= src
->symbol
.decl
;
633 struct function
*srccfun
, *dstcfun
;
636 if (!src
->symbol
.definition
637 || !dst
->symbol
.definition
)
639 if (src
->frequency
< dst
->frequency
)
640 src
->frequency
= dst
->frequency
;
643 if (cgraph_dump_file
)
645 fprintf (cgraph_dump_file
, "Merging profiles of %s/%i to %s/%i\n",
646 xstrdup (cgraph_node_name (src
)), src
->symbol
.order
,
647 xstrdup (cgraph_node_name (dst
)), dst
->symbol
.order
);
649 dst
->count
+= src
->count
;
651 /* This is ugly. We need to get both function bodies into memory.
652 If declaration is merged, we need to duplicate it to be able
653 to load body that is being replaced. This makes symbol table
654 temporarily inconsistent. */
655 if (src
->symbol
.decl
== dst
->symbol
.decl
)
658 struct lto_in_decl_state temp
;
659 struct lto_in_decl_state
*state
;
661 /* We are going to move the decl, we want to remove its file decl data.
662 and link these with the new decl. */
663 temp
.fn_decl
= src
->symbol
.decl
;
664 slot
= htab_find_slot (src
->symbol
.lto_file_data
->function_decl_states
,
666 state
= (lto_in_decl_state
*)*slot
;
667 htab_clear_slot (src
->symbol
.lto_file_data
->function_decl_states
, slot
);
670 /* Duplicate the decl and be sure it does not link into body of DST. */
671 src
->symbol
.decl
= copy_node (src
->symbol
.decl
);
672 DECL_STRUCT_FUNCTION (src
->symbol
.decl
) = NULL
;
673 DECL_ARGUMENTS (src
->symbol
.decl
) = NULL
;
674 DECL_INITIAL (src
->symbol
.decl
) = NULL
;
675 DECL_RESULT (src
->symbol
.decl
) = NULL
;
677 /* Associate the decl state with new declaration, so LTO streamer
679 state
->fn_decl
= src
->symbol
.decl
;
680 slot
= htab_find_slot (src
->symbol
.lto_file_data
->function_decl_states
,
685 cgraph_get_body (src
);
686 cgraph_get_body (dst
);
687 srccfun
= DECL_STRUCT_FUNCTION (src
->symbol
.decl
);
688 dstcfun
= DECL_STRUCT_FUNCTION (dst
->symbol
.decl
);
689 if (n_basic_blocks_for_function (srccfun
)
690 != n_basic_blocks_for_function (dstcfun
))
692 if (cgraph_dump_file
)
693 fprintf (cgraph_dump_file
,
694 "Giving up; number of basic block mismatch.\n");
697 else if (last_basic_block_for_function (srccfun
)
698 != last_basic_block_for_function (dstcfun
))
700 if (cgraph_dump_file
)
701 fprintf (cgraph_dump_file
,
702 "Giving up; last block mismatch.\n");
707 basic_block srcbb
, dstbb
;
709 FOR_ALL_BB_FN (srcbb
, srccfun
)
713 dstbb
= BASIC_BLOCK_FOR_FUNCTION (dstcfun
, srcbb
->index
);
716 if (cgraph_dump_file
)
717 fprintf (cgraph_dump_file
,
718 "No matching block for bb %i.\n",
723 if (EDGE_COUNT (srcbb
->succs
) != EDGE_COUNT (dstbb
->succs
))
725 if (cgraph_dump_file
)
726 fprintf (cgraph_dump_file
,
727 "Edge count mistmatch for bb %i.\n",
732 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
734 edge srce
= EDGE_SUCC (srcbb
, i
);
735 edge dste
= EDGE_SUCC (dstbb
, i
);
736 if (srce
->dest
->index
!= dste
->dest
->index
)
738 if (cgraph_dump_file
)
739 fprintf (cgraph_dump_file
,
740 "Succ edge mistmatch for bb %i.\n",
750 struct cgraph_edge
*e
;
751 basic_block srcbb
, dstbb
;
753 /* TODO: merge also statement histograms. */
754 FOR_ALL_BB_FN (srcbb
, srccfun
)
758 dstbb
= BASIC_BLOCK_FOR_FUNCTION (dstcfun
, srcbb
->index
);
759 dstbb
->count
+= srcbb
->count
;
760 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
762 edge srce
= EDGE_SUCC (srcbb
, i
);
763 edge dste
= EDGE_SUCC (dstbb
, i
);
764 dste
->count
+= srce
->count
;
769 compute_function_frequency ();
771 for (e
= dst
->callees
; e
; e
= e
->next_callee
)
773 gcc_assert (!e
->speculative
);
774 e
->count
= gimple_bb (e
->call_stmt
)->count
;
775 e
->frequency
= compute_call_stmt_bb_frequency
777 gimple_bb (e
->call_stmt
));
779 for (e
= dst
->indirect_calls
; e
; e
= e
->next_callee
)
781 gcc_assert (!e
->speculative
);
782 e
->count
= gimple_bb (e
->call_stmt
)->count
;
783 e
->frequency
= compute_call_stmt_bb_frequency
785 gimple_bb (e
->call_stmt
));
787 cgraph_release_function_body (src
);
788 inline_update_overall_summary (dst
);
790 /* TODO: if there is no match, we can scale up. */
791 src
->symbol
.decl
= oldsrcdecl
;
794 /* Return true if call to DEST is known to be self-recusive call withing FUNC. */
797 recursive_call_p (tree func
, tree dest
)
799 struct cgraph_node
*dest_node
= cgraph_get_create_node (dest
);
800 struct cgraph_node
*cnode
= cgraph_get_create_node (func
);
802 return symtab_semantically_equivalent_p ((symtab_node
)dest_node
,