1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 /* Fill array order with all nodes with output flag set in the reverse
30 cgraph_postorder (struct cgraph_node
**order
)
32 struct cgraph_node
*node
, *node2
;
35 struct cgraph_edge
*edge
, last
;
37 struct cgraph_node
**stack
=
38 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
40 /* We have to deal with cycles nicely, so use a depth first traversal
41 output algorithm. Ignore the fact that some functions won't need
42 to be output and put them into order as well, so we get dependencies
43 right through intline functions. */
44 for (node
= cgraph_nodes
; node
; node
= node
->next
)
46 for (node
= cgraph_nodes
; node
; node
= node
->next
)
53 node
->aux
= node
->callers
;
56 while (node2
->aux
!= &last
)
59 if (edge
->next_caller
)
60 node2
->aux
= edge
->next_caller
;
63 if (!edge
->caller
->aux
)
65 if (!edge
->caller
->callers
)
66 edge
->caller
->aux
= &last
;
68 edge
->caller
->aux
= edge
->caller
->callers
;
69 stack
[stack_size
++] = node2
;
74 if (node2
->aux
== &last
)
76 order
[order_pos
++] = node2
;
78 node2
= stack
[--stack_size
];
85 for (node
= cgraph_nodes
; node
; node
= node
->next
)
90 /* Perform reachability analysis and reclaim all unreachable nodes.
91 If BEFORE_INLINING_P is true this function is called before inlining
92 decisions has been made. If BEFORE_INLINING_P is false this function also
93 removes unneeded bodies of extern inline functions. */
96 cgraph_remove_unreachable_nodes (bool before_inlining_p
, FILE *file
)
98 struct cgraph_node
*first
= (void *) 1;
99 struct cgraph_node
*node
, *next
;
100 bool changed
= false;
103 #ifdef ENABLE_CHECKING
107 fprintf (file
, "\nReclaiming functions:");
108 #ifdef ENABLE_CHECKING
109 for (node
= cgraph_nodes
; node
; node
= node
->next
)
110 gcc_assert (!node
->aux
);
112 for (node
= cgraph_nodes
; node
; node
= node
->next
)
113 if (node
->needed
&& !node
->global
.inlined_to
114 && ((!DECL_EXTERNAL (node
->decl
))
116 || before_inlining_p
))
122 gcc_assert (!node
->aux
);
124 /* Perform reachability analysis. As a special case do not consider
125 extern inline functions not inlined as live because we won't output
127 while (first
!= (void *) 1)
129 struct cgraph_edge
*e
;
133 for (e
= node
->callees
; e
; e
= e
->next_callee
)
136 && (!e
->inline_failed
|| !e
->callee
->analyzed
137 || (!DECL_EXTERNAL (e
->callee
->decl
))
138 || before_inlining_p
))
140 e
->callee
->aux
= first
;
145 /* Remove unreachable nodes. Extern inline functions need special care;
146 Unreachable extern inline functions shall be removed.
147 Reachable extern inline functions we never inlined shall get their bodies
149 Reachable extern inline functions we sometimes inlined will be turned into
150 unanalyzed nodes so they look like for true extern functions to the rest
151 of code. Body of such functions is released via remove_node once the
152 inline clones are eliminated. */
153 for (node
= cgraph_nodes
; node
; node
= next
)
159 tree decl
= node
->decl
;
161 node
->global
.inlined_to
= NULL
;
162 if (DECL_STRUCT_FUNCTION (decl
))
163 local_insns
= node
->local
.self_insns
;
167 fprintf (file
, " %s", cgraph_node_name (node
));
168 if (!node
->analyzed
|| !DECL_EXTERNAL (node
->decl
)
169 || before_inlining_p
)
170 cgraph_remove_node (node
);
173 struct cgraph_edge
*e
;
175 for (e
= node
->callers
; e
; e
= e
->next_caller
)
178 if (e
|| node
->needed
)
180 struct cgraph_node
*clone
;
182 for (clone
= node
->next_clone
; clone
;
183 clone
= clone
->next_clone
)
188 DECL_SAVED_TREE (node
->decl
) = NULL
;
189 DECL_STRUCT_FUNCTION (node
->decl
) = NULL
;
190 DECL_INITIAL (node
->decl
) = error_mark_node
;
191 node
->analyzed
= false;
193 cgraph_node_remove_callees (node
);
194 node
->analyzed
= false;
197 cgraph_remove_node (node
);
199 if (!DECL_SAVED_TREE (decl
))
200 insns
+= local_insns
;
204 for (node
= cgraph_nodes
; node
; node
= node
->next
)
207 fprintf (file
, "\nReclaimed %i insns", insns
);