]>
Commit | Line | Data |
---|---|---|
ca31b95f | 1 | /* Basic IPA optimizations and utilities. |
d1e082c2 | 2 | Copyright (C) 2003-2013 Free Software Foundation, Inc. |
ca31b95f JH |
3 | |
4 | This file is part of GCC. | |
5 | ||
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 | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
ca31b95f JH |
9 | version. |
10 | ||
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 | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
ca31b95f JH |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "tm.h" | |
24 | #include "cgraph.h" | |
f4b3ca72 | 25 | #include "tree-pass.h" |
9187e02d | 26 | #include "gimple.h" |
fed5ae11 | 27 | #include "ggc.h" |
4a444e58 | 28 | #include "flags.h" |
5dde3b01 | 29 | #include "pointer-set.h" |
9e97ff61 JH |
30 | #include "target.h" |
31 | #include "tree-iterator.h" | |
af8bca3c | 32 | #include "ipa-utils.h" |
04142cc3 | 33 | #include "ipa-inline.h" |
0208f7da JH |
34 | #include "tree-inline.h" |
35 | #include "profile.h" | |
36 | #include "params.h" | |
ca31b95f | 37 | |
e70670cf JH |
38 | /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */ |
39 | ||
40 | static bool | |
41 | cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
42 | { | |
43 | /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */ | |
44 | return !(cgraph_only_called_directly_or_aliased_p (node) | |
45 | && !ipa_ref_has_aliases_p (&node->symbol.ref_list) | |
46 | && node->symbol.definition | |
47 | && !DECL_EXTERNAL (node->symbol.decl) | |
48 | && !node->symbol.externally_visible | |
49 | && !node->symbol.used_from_other_partition | |
50 | && !node->symbol.in_other_partition); | |
51 | } | |
52 | ||
53 | /* Return true when function can be marked local. */ | |
54 | ||
55 | static bool | |
56 | cgraph_local_node_p (struct cgraph_node *node) | |
57 | { | |
58 | struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL); | |
59 | ||
60 | /* FIXME: thunks can be considered local, but we need prevent i386 | |
61 | from attempting to change calling convention of them. */ | |
62 | if (n->thunk.thunk_p) | |
63 | return false; | |
64 | return !cgraph_for_node_and_aliases (n, | |
65 | cgraph_non_local_node_p_1, NULL, true); | |
66 | ||
67 | } | |
68 | ||
69 | /* Return true when NODE has ADDR reference. */ | |
70 | ||
71 | static bool | |
72 | has_addr_references_p (struct cgraph_node *node, | |
73 | void *data ATTRIBUTE_UNUSED) | |
74 | { | |
75 | int i; | |
76 | struct ipa_ref *ref; | |
77 | ||
78 | for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, | |
79 | i, ref); i++) | |
80 | if (ref->use == IPA_REF_ADDR) | |
81 | return true; | |
82 | return false; | |
83 | } | |
84 | ||
d563610d JH |
85 | /* Look for all functions inlined to NODE and update their inlined_to pointers |
86 | to INLINED_TO. */ | |
87 | ||
88 | static void | |
89 | update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to) | |
90 | { | |
91 | struct cgraph_edge *e; | |
92 | for (e = node->callees; e; e = e->next_callee) | |
93 | if (e->callee->global.inlined_to) | |
94 | { | |
95 | e->callee->global.inlined_to = inlined_to; | |
96 | update_inlined_to_pointer (e->callee, inlined_to); | |
97 | } | |
98 | } | |
99 | ||
04142cc3 | 100 | /* Add symtab NODE to queue starting at FIRST. |
19fb0b86 JH |
101 | |
102 | The queue is linked via AUX pointers and terminated by pointer to 1. | |
103 | We enqueue nodes at two occasions: when we find them reachable or when we find | |
104 | their bodies needed for further clonning. In the second case we mark them | |
105 | by pointer to 2 after processing so they are re-queue when they become | |
106 | reachable. */ | |
b34fd25c JH |
107 | |
108 | static void | |
04142cc3 JH |
109 | enqueue_node (symtab_node node, symtab_node *first, |
110 | struct pointer_set_t *reachable) | |
b34fd25c | 111 | { |
19fb0b86 | 112 | /* Node is still in queue; do nothing. */ |
960bfb69 | 113 | if (node->symbol.aux && node->symbol.aux != (void *) 2) |
19fb0b86 JH |
114 | return; |
115 | /* Node was already processed as unreachable, re-enqueue | |
116 | only if it became reachable now. */ | |
93a18a70 | 117 | if (node->symbol.aux == (void *)2 && !pointer_set_contains (reachable, node)) |
19fb0b86 | 118 | return; |
960bfb69 | 119 | node->symbol.aux = *first; |
b34fd25c JH |
120 | *first = node; |
121 | } | |
122 | ||
b34fd25c JH |
123 | /* Process references. */ |
124 | ||
125 | static void | |
126 | process_references (struct ipa_ref_list *list, | |
04142cc3 | 127 | symtab_node *first, |
93a18a70 JH |
128 | bool before_inlining_p, |
129 | struct pointer_set_t *reachable) | |
b34fd25c JH |
130 | { |
131 | int i; | |
132 | struct ipa_ref *ref; | |
133 | for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++) | |
134 | { | |
e70670cf JH |
135 | symtab_node node = ref->referred; |
136 | ||
4f63dfc6 | 137 | if (node->symbol.definition && !node->symbol.in_other_partition |
40a7fe1e | 138 | && ((!DECL_EXTERNAL (node->symbol.decl) || node->symbol.alias) |
e70670cf JH |
139 | || (before_inlining_p |
140 | /* We use variable constructors during late complation for | |
141 | constant folding. Keep references alive so partitioning | |
142 | knows about potential references. */ | |
143 | || (TREE_CODE (node->symbol.decl) == VAR_DECL | |
6a6dac52 JH |
144 | && flag_wpa |
145 | && ctor_for_folding (node->symbol.decl) | |
146 | != error_mark_node)))) | |
e70670cf JH |
147 | pointer_set_insert (reachable, node); |
148 | enqueue_node ((symtab_node) node, first, reachable); | |
b34fd25c JH |
149 | } |
150 | } | |
151 | ||
41817394 | 152 | |
ca31b95f | 153 | /* Perform reachability analysis and reclaim all unreachable nodes. |
04142cc3 JH |
154 | |
155 | The algorithm is basically mark&sweep but with some extra refinements: | |
156 | ||
157 | - reachable extern inline functions needs special handling; the bodies needs | |
158 | to stay in memory until inlining in hope that they will be inlined. | |
159 | After inlining we release their bodies and turn them into unanalyzed | |
160 | nodes even when they are reachable. | |
161 | ||
162 | BEFORE_INLINING_P specify whether we are before or after inlining. | |
163 | ||
164 | - virtual functions are kept in callgraph even if they seem unreachable in | |
165 | hope calls to them will be devirtualized. | |
166 | ||
167 | Again we remove them after inlining. In late optimization some | |
168 | devirtualization may happen, but it is not importnat since we won't inline | |
169 | the call. In theory early opts and IPA should work out all important cases. | |
170 | ||
171 | - virtual clones needs bodies of their origins for later materialization; | |
172 | this means that we want to keep the body even if the origin is unreachable | |
173 | otherwise. To avoid origin from sitting in the callgraph and being | |
174 | walked by IPA passes, we turn them into unanalyzed nodes with body | |
175 | defined. | |
176 | ||
177 | We maintain set of function declaration where body needs to stay in | |
178 | body_needed_for_clonning | |
179 | ||
180 | Inline clones represent special case: their declaration match the | |
181 | declaration of origin and cgraph_remove_node already knows how to | |
182 | reshape callgraph and preserve body when offline copy of function or | |
183 | inline clone is being removed. | |
184 | ||
6649df51 JH |
185 | - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL |
186 | variables with DECL_INITIAL set. We finalize these and keep reachable | |
187 | ones around for constant folding purposes. After inlining we however | |
188 | stop walking their references to let everything static referneced by them | |
189 | to be removed when it is otherwise unreachable. | |
190 | ||
04142cc3 JH |
191 | We maintain queue of both reachable symbols (i.e. defined symbols that needs |
192 | to stay) and symbols that are in boundary (i.e. external symbols referenced | |
193 | by reachable symbols or origins of clones). The queue is represented | |
194 | as linked list by AUX pointer terminated by 1. | |
195 | ||
196 | A the end we keep all reachable symbols. For symbols in boundary we always | |
197 | turn definition into a declaration, but we may keep function body around | |
198 | based on body_needed_for_clonning | |
199 | ||
200 | All symbols that enter the queue have AUX pointer non-zero and are in the | |
201 | boundary. Pointer set REACHABLE is used to track reachable symbols. | |
202 | ||
203 | Every symbol can be visited twice - once as part of boundary and once | |
204 | as real reachable symbol. enqueue_node needs to decide whether the | |
205 | node needs to be re-queued for second processing. For this purpose | |
206 | we set AUX pointer of processed symbols in the boundary to constant 2. */ | |
ca31b95f JH |
207 | |
208 | bool | |
04142cc3 | 209 | symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file) |
ca31b95f | 210 | { |
04142cc3 | 211 | symtab_node first = (symtab_node) (void *) 1; |
96fc428c | 212 | struct cgraph_node *node, *next; |
b34fd25c | 213 | struct varpool_node *vnode, *vnext; |
ca31b95f | 214 | bool changed = false; |
93a18a70 | 215 | struct pointer_set_t *reachable = pointer_set_create (); |
04142cc3 | 216 | struct pointer_set_t *body_needed_for_clonning = pointer_set_create (); |
ca31b95f JH |
217 | |
218 | #ifdef ENABLE_CHECKING | |
474ffc72 | 219 | verify_symtab (); |
ca31b95f | 220 | #endif |
61a74079 JH |
221 | if (optimize && flag_devirtualize) |
222 | build_type_inheritance_graph (); | |
10d22567 ZD |
223 | if (file) |
224 | fprintf (file, "\nReclaiming functions:"); | |
ca31b95f | 225 | #ifdef ENABLE_CHECKING |
65c70e6b | 226 | FOR_EACH_FUNCTION (node) |
960bfb69 | 227 | gcc_assert (!node->symbol.aux); |
65c70e6b | 228 | FOR_EACH_VARIABLE (vnode) |
960bfb69 | 229 | gcc_assert (!vnode->symbol.aux); |
ca31b95f | 230 | #endif |
530f3a1b JH |
231 | /* Mark functions whose bodies are obviously needed. |
232 | This is mostly when they can be referenced externally. Inline clones | |
233 | are special since their declarations are shared with master clone and thus | |
234 | cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */ | |
c0c123ef JH |
235 | FOR_EACH_FUNCTION (node) |
236 | { | |
237 | node->used_as_abstract_origin = false; | |
238 | if (node->symbol.definition | |
239 | && !node->global.inlined_to | |
0dcc26c3 | 240 | && !node->symbol.in_other_partition |
c0c123ef JH |
241 | && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node) |
242 | /* Keep around virtual functions for possible devirtualization. */ | |
243 | || (before_inlining_p | |
244 | && DECL_VIRTUAL_P (node->symbol.decl)))) | |
245 | { | |
246 | gcc_assert (!node->global.inlined_to); | |
247 | pointer_set_insert (reachable, node); | |
248 | enqueue_node ((symtab_node)node, &first, reachable); | |
249 | } | |
250 | else | |
251 | gcc_assert (!node->symbol.aux); | |
252 | } | |
530f3a1b JH |
253 | |
254 | /* Mark variables that are obviously needed. */ | |
04142cc3 | 255 | FOR_EACH_DEFINED_VARIABLE (vnode) |
4f63dfc6 JH |
256 | if (!varpool_can_remove_if_no_refs (vnode) |
257 | && !vnode->symbol.in_other_partition) | |
04142cc3 JH |
258 | { |
259 | pointer_set_insert (reachable, vnode); | |
260 | enqueue_node ((symtab_node)vnode, &first, reachable); | |
261 | } | |
262 | ||
263 | /* Perform reachability analysis. */ | |
264 | while (first != (symtab_node) (void *) 1) | |
b34fd25c | 265 | { |
04142cc3 JH |
266 | bool in_boundary_p = !pointer_set_contains (reachable, first); |
267 | symtab_node node = first; | |
ca31b95f | 268 | |
04142cc3 | 269 | first = (symtab_node)first->symbol.aux; |
19fb0b86 | 270 | |
04142cc3 JH |
271 | /* If we are processing symbol in boundary, mark its AUX pointer for |
272 | possible later re-processing in enqueue_node. */ | |
273 | if (in_boundary_p) | |
274 | node->symbol.aux = (void *)2; | |
275 | else | |
276 | { | |
c0c123ef JH |
277 | if (DECL_ABSTRACT_ORIGIN (node->symbol.decl)) |
278 | { | |
279 | struct cgraph_node *origin_node | |
280 | = cgraph_get_create_real_symbol_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl)); | |
281 | origin_node->used_as_abstract_origin = true; | |
282 | enqueue_node ((symtab_node) origin_node, &first, reachable); | |
283 | } | |
04142cc3 JH |
284 | /* If any symbol in a comdat group is reachable, force |
285 | all other in the same comdat group to be also reachable. */ | |
286 | if (node->symbol.same_comdat_group) | |
287 | { | |
288 | symtab_node next; | |
289 | for (next = node->symbol.same_comdat_group; | |
290 | next != node; | |
291 | next = next->symbol.same_comdat_group) | |
292 | if (!pointer_set_insert (reachable, next)) | |
293 | enqueue_node ((symtab_node) next, &first, reachable); | |
294 | } | |
295 | /* Mark references as reachable. */ | |
296 | process_references (&node->symbol.ref_list, &first, | |
297 | before_inlining_p, reachable); | |
298 | } | |
19fb0b86 | 299 | |
5d59b5e1 | 300 | if (cgraph_node *cnode = dyn_cast <cgraph_node> (node)) |
b34fd25c | 301 | { |
04142cc3 JH |
302 | /* Mark the callees reachable unless they are direct calls to extern |
303 | inline functions we decided to not inline. */ | |
304 | if (!in_boundary_p) | |
8a6295ba | 305 | { |
04142cc3 JH |
306 | struct cgraph_edge *e; |
307 | for (e = cnode->callees; e; e = e->next_callee) | |
ed62e0d9 | 308 | { |
e70670cf | 309 | if (e->callee->symbol.definition |
4f63dfc6 | 310 | && !e->callee->symbol.in_other_partition |
ed62e0d9 | 311 | && (!e->inline_failed |
960bfb69 | 312 | || !DECL_EXTERNAL (e->callee->symbol.decl) |
40a7fe1e | 313 | || e->callee->symbol.alias |
ed62e0d9 | 314 | || before_inlining_p)) |
93a18a70 | 315 | pointer_set_insert (reachable, e->callee); |
04142cc3 | 316 | enqueue_node ((symtab_node) e->callee, &first, reachable); |
93a18a70 | 317 | } |
04142cc3 JH |
318 | |
319 | /* When inline clone exists, mark body to be preserved so when removing | |
320 | offline copy of the function we don't kill it. */ | |
4f63dfc6 | 321 | if (cnode->global.inlined_to) |
04142cc3 | 322 | pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl); |
b66887e4 | 323 | |
4f63dfc6 JH |
324 | /* For non-inline clones, force their origins to the boundary and ensure |
325 | that body is not removed. */ | |
326 | while (cnode->clone_of) | |
327 | { | |
328 | bool noninline = cnode->clone_of->symbol.decl != cnode->symbol.decl; | |
329 | cnode = cnode->clone_of; | |
330 | if (noninline) | |
331 | { | |
332 | pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl); | |
333 | enqueue_node ((symtab_node)cnode, &first, reachable); | |
334 | } | |
b34fd25c | 335 | } |
47cb0d7d | 336 | } |
b34fd25c | 337 | } |
6649df51 | 338 | /* When we see constructor of external variable, keep referred nodes in the |
5d59b5e1 LC |
339 | boundary. This will also hold initializers of the external vars NODE |
340 | refers to. */ | |
341 | varpool_node *vnode = dyn_cast <varpool_node> (node); | |
342 | if (vnode | |
6649df51 | 343 | && DECL_EXTERNAL (node->symbol.decl) |
e70670cf | 344 | && !vnode->symbol.alias |
6649df51 | 345 | && in_boundary_p) |
5d59b5e1 | 346 | { |
6649df51 | 347 | struct ipa_ref *ref; |
5d59b5e1 | 348 | for (int i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++) |
6649df51 | 349 | enqueue_node (ref->referred, &first, reachable); |
5d59b5e1 | 350 | } |
ca31b95f JH |
351 | } |
352 | ||
04142cc3 | 353 | /* Remove unreachable functions. */ |
2aae7680 | 354 | for (node = cgraph_first_function (); node; node = next) |
ca31b95f | 355 | { |
2aae7680 | 356 | next = cgraph_next_function (node); |
e70670cf JH |
357 | |
358 | /* If node is not needed at all, remove it. */ | |
960bfb69 | 359 | if (!node->symbol.aux) |
ca31b95f | 360 | { |
10d22567 ZD |
361 | if (file) |
362 | fprintf (file, " %s", cgraph_node_name (node)); | |
04142cc3 JH |
363 | cgraph_remove_node (node); |
364 | changed = true; | |
365 | } | |
e70670cf | 366 | /* If node is unreachable, remove its body. */ |
04142cc3 JH |
367 | else if (!pointer_set_contains (reachable, node)) |
368 | { | |
e70670cf JH |
369 | if (!pointer_set_contains (body_needed_for_clonning, node->symbol.decl)) |
370 | cgraph_release_function_body (node); | |
4f63dfc6 | 371 | else if (!node->clone_of) |
815effe1 | 372 | gcc_assert (in_lto_p || DECL_RESULT (node->symbol.decl)); |
e70670cf | 373 | if (node->symbol.definition) |
bb853349 | 374 | { |
04142cc3 JH |
375 | if (file) |
376 | fprintf (file, " %s", cgraph_node_name (node)); | |
51a5c0c2 JH |
377 | node->symbol.analyzed = false; |
378 | node->symbol.definition = false; | |
379 | node->symbol.cpp_implicit_alias = false; | |
380 | node->symbol.alias = false; | |
381 | node->symbol.weakref = false; | |
382 | if (!node->symbol.in_other_partition) | |
383 | node->local.local = false; | |
384 | cgraph_node_remove_callees (node); | |
385 | ipa_remove_all_references (&node->symbol.ref_list); | |
bb853349 JH |
386 | changed = true; |
387 | } | |
ca31b95f | 388 | } |
4f63dfc6 JH |
389 | else |
390 | gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node) | |
815effe1 | 391 | || in_lto_p || DECL_RESULT (node->symbol.decl)); |
ca31b95f | 392 | } |
04142cc3 JH |
393 | |
394 | /* Inline clones might be kept around so their materializing allows further | |
395 | cloning. If the function the clone is inlined into is removed, we need | |
396 | to turn it into normal cone. */ | |
65c70e6b | 397 | FOR_EACH_FUNCTION (node) |
9187e02d | 398 | { |
9187e02d JH |
399 | if (node->global.inlined_to |
400 | && !node->callers) | |
401 | { | |
402 | gcc_assert (node->clones); | |
d563610d JH |
403 | node->global.inlined_to = NULL; |
404 | update_inlined_to_pointer (node, node); | |
9187e02d | 405 | } |
960bfb69 | 406 | node->symbol.aux = NULL; |
9187e02d | 407 | } |
4a444e58 | 408 | |
04142cc3 | 409 | /* Remove unreachable variables. */ |
4a444e58 | 410 | if (file) |
04142cc3 | 411 | fprintf (file, "\nReclaiming variables:"); |
2aae7680 | 412 | for (vnode = varpool_first_variable (); vnode; vnode = vnext) |
b34fd25c | 413 | { |
2aae7680 | 414 | vnext = varpool_next_variable (vnode); |
b9bd2075 JH |
415 | if (!vnode->symbol.aux |
416 | /* For can_refer_decl_in_current_unit_p we want to track for | |
417 | all external variables if they are defined in other partition | |
418 | or not. */ | |
419 | && (!flag_ltrans || !DECL_EXTERNAL (vnode->symbol.decl))) | |
04142cc3 | 420 | { |
4a444e58 JH |
421 | if (file) |
422 | fprintf (file, " %s", varpool_node_name (vnode)); | |
423 | varpool_remove_node (vnode); | |
424 | changed = true; | |
b34fd25c | 425 | } |
04142cc3 JH |
426 | else if (!pointer_set_contains (reachable, vnode)) |
427 | { | |
6a6dac52 | 428 | tree init; |
e70670cf | 429 | if (vnode->symbol.definition) |
04142cc3 JH |
430 | { |
431 | if (file) | |
432 | fprintf (file, " %s", varpool_node_name (vnode)); | |
433 | changed = true; | |
434 | } | |
e70670cf JH |
435 | vnode->symbol.definition = false; |
436 | vnode->symbol.analyzed = false; | |
04142cc3 | 437 | vnode->symbol.aux = NULL; |
e70670cf JH |
438 | |
439 | /* Keep body if it may be useful for constant folding. */ | |
6a6dac52 | 440 | if ((init = ctor_for_folding (vnode->symbol.decl)) == error_mark_node) |
e70670cf | 441 | varpool_remove_initializer (vnode); |
6a6dac52 JH |
442 | else |
443 | DECL_INITIAL (vnode->symbol.decl) = init; | |
e70670cf | 444 | ipa_remove_all_references (&vnode->symbol.ref_list); |
04142cc3 JH |
445 | } |
446 | else | |
447 | vnode->symbol.aux = NULL; | |
b34fd25c | 448 | } |
4a444e58 | 449 | |
04142cc3 JH |
450 | pointer_set_destroy (reachable); |
451 | pointer_set_destroy (body_needed_for_clonning); | |
4a444e58 | 452 | |
04142cc3 | 453 | /* Now update address_taken flags and try to promote functions to be local. */ |
bd3cdcc0 JH |
454 | if (file) |
455 | fprintf (file, "\nClearing address taken flags:"); | |
65c70e6b | 456 | FOR_EACH_DEFINED_FUNCTION (node) |
960bfb69 JH |
457 | if (node->symbol.address_taken |
458 | && !node->symbol.used_from_other_partition) | |
bd3cdcc0 | 459 | { |
41817394 | 460 | if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true)) |
bd3cdcc0 JH |
461 | { |
462 | if (file) | |
463 | fprintf (file, " %s", cgraph_node_name (node)); | |
960bfb69 | 464 | node->symbol.address_taken = false; |
4a444e58 JH |
465 | changed = true; |
466 | if (cgraph_local_node_p (node)) | |
467 | { | |
468 | node->local.local = true; | |
469 | if (file) | |
470 | fprintf (file, " (local)"); | |
471 | } | |
bd3cdcc0 JH |
472 | } |
473 | } | |
10a5dd5d JH |
474 | if (file) |
475 | fprintf (file, "\n"); | |
b34fd25c | 476 | |
873aa8f5 | 477 | #ifdef ENABLE_CHECKING |
474ffc72 | 478 | verify_symtab (); |
873aa8f5 | 479 | #endif |
4537ec0c | 480 | |
a8da72b8 | 481 | /* If we removed something, perhaps profile could be improved. */ |
9771b263 | 482 | if (changed && optimize && inline_edge_summary_vec.exists ()) |
a8da72b8 | 483 | FOR_EACH_DEFINED_FUNCTION (node) |
08f835dc | 484 | ipa_propagate_frequency (node); |
a8da72b8 | 485 | |
ca31b95f JH |
486 | return changed; |
487 | } | |
f4b3ca72 | 488 | |
4a444e58 JH |
489 | /* Discover variables that have no longer address taken or that are read only |
490 | and update their flags. | |
491 | ||
492 | FIXME: This can not be done in between gimplify and omp_expand since | |
493 | readonly flag plays role on what is shared and what is not. Currently we do | |
f10ea640 JH |
494 | this transformation as part of whole program visibility and re-do at |
495 | ipa-reference pass (to take into account clonning), but it would | |
496 | make sense to do it before early optimizations. */ | |
4a444e58 JH |
497 | |
498 | void | |
499 | ipa_discover_readonly_nonaddressable_vars (void) | |
500 | { | |
501 | struct varpool_node *vnode; | |
502 | if (dump_file) | |
503 | fprintf (dump_file, "Clearing variable flags:"); | |
65c70e6b | 504 | FOR_EACH_VARIABLE (vnode) |
e70670cf | 505 | if (vnode->symbol.definition && varpool_all_refs_explicit_p (vnode) |
960bfb69 JH |
506 | && (TREE_ADDRESSABLE (vnode->symbol.decl) |
507 | || !TREE_READONLY (vnode->symbol.decl))) | |
4a444e58 JH |
508 | { |
509 | bool written = false; | |
510 | bool address_taken = false; | |
511 | int i; | |
512 | struct ipa_ref *ref; | |
5932a4d4 | 513 | for (i = 0; ipa_ref_list_referring_iterate (&vnode->symbol.ref_list, |
960bfb69 | 514 | i, ref) |
4a444e58 JH |
515 | && (!written || !address_taken); i++) |
516 | switch (ref->use) | |
517 | { | |
518 | case IPA_REF_ADDR: | |
519 | address_taken = true; | |
520 | break; | |
521 | case IPA_REF_LOAD: | |
522 | break; | |
523 | case IPA_REF_STORE: | |
524 | written = true; | |
525 | break; | |
526 | } | |
960bfb69 | 527 | if (TREE_ADDRESSABLE (vnode->symbol.decl) && !address_taken) |
4a444e58 JH |
528 | { |
529 | if (dump_file) | |
530 | fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode)); | |
960bfb69 | 531 | TREE_ADDRESSABLE (vnode->symbol.decl) = 0; |
4a444e58 | 532 | } |
960bfb69 | 533 | if (!TREE_READONLY (vnode->symbol.decl) && !address_taken && !written |
4a444e58 JH |
534 | /* Making variable in explicit section readonly can cause section |
535 | type conflict. | |
536 | See e.g. gcc.c-torture/compile/pr23237.c */ | |
960bfb69 | 537 | && DECL_SECTION_NAME (vnode->symbol.decl) == NULL) |
4a444e58 JH |
538 | { |
539 | if (dump_file) | |
540 | fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode)); | |
960bfb69 | 541 | TREE_READONLY (vnode->symbol.decl) = 1; |
4a444e58 JH |
542 | } |
543 | } | |
544 | if (dump_file) | |
545 | fprintf (dump_file, "\n"); | |
546 | } | |
547 | ||
430c6ceb JH |
548 | /* Return true when there is a reference to node and it is not vtable. */ |
549 | static bool | |
edb983b2 | 550 | address_taken_from_non_vtable_p (symtab_node node) |
430c6ceb JH |
551 | { |
552 | int i; | |
553 | struct ipa_ref *ref; | |
5932a4d4 | 554 | for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, |
960bfb69 | 555 | i, ref); i++) |
d2640c43 JH |
556 | if (ref->use == IPA_REF_ADDR) |
557 | { | |
558 | struct varpool_node *node; | |
5d59b5e1 | 559 | if (is_a <cgraph_node> (ref->referring)) |
d2640c43 | 560 | return true; |
5932a4d4 | 561 | node = ipa_ref_referring_varpool_node (ref); |
960bfb69 | 562 | if (!DECL_VIRTUAL_P (node->symbol.decl)) |
d2640c43 JH |
563 | return true; |
564 | } | |
430c6ceb JH |
565 | return false; |
566 | } | |
567 | ||
edb983b2 JH |
568 | /* A helper for comdat_can_be_unshared_p. */ |
569 | ||
570 | static bool | |
571 | comdat_can_be_unshared_p_1 (symtab_node node) | |
572 | { | |
573 | /* When address is taken, we don't know if equality comparison won't | |
0987ffe7 JH |
574 | break eventually. Exception are virutal functions, C++ |
575 | constructors/destructors and vtables, where this is not possible by | |
576 | language standard. */ | |
edb983b2 | 577 | if (!DECL_VIRTUAL_P (node->symbol.decl) |
0987ffe7 JH |
578 | && (TREE_CODE (node->symbol.decl) != FUNCTION_DECL |
579 | || (!DECL_CXX_CONSTRUCTOR_P (node->symbol.decl) | |
580 | && !DECL_CXX_DESTRUCTOR_P (node->symbol.decl))) | |
edb983b2 JH |
581 | && address_taken_from_non_vtable_p (node)) |
582 | return false; | |
583 | ||
584 | /* If the symbol is used in some weird way, better to not touch it. */ | |
585 | if (node->symbol.force_output) | |
586 | return false; | |
587 | ||
588 | /* Explicit instantiations needs to be output when possibly | |
589 | used externally. */ | |
590 | if (node->symbol.forced_by_abi | |
591 | && TREE_PUBLIC (node->symbol.decl) | |
592 | && (node->symbol.resolution != LDPR_PREVAILING_DEF_IRONLY | |
593 | && !flag_whole_program)) | |
594 | return false; | |
595 | ||
596 | /* Non-readonly and volatile variables can not be duplicated. */ | |
597 | if (is_a <varpool_node> (node) | |
598 | && (!TREE_READONLY (node->symbol.decl) | |
599 | || TREE_THIS_VOLATILE (node->symbol.decl))) | |
600 | return false; | |
601 | return true; | |
602 | } | |
603 | ||
430c6ceb JH |
604 | /* COMDAT functions must be shared only if they have address taken, |
605 | otherwise we can produce our own private implementation with | |
606 | -fwhole-program. | |
607 | Return true when turning COMDAT functoin static can not lead to wrong | |
608 | code when the resulting object links with a library defining same COMDAT. | |
609 | ||
610 | Virtual functions do have their addresses taken from the vtables, | |
611 | but in C++ there is no way to compare their addresses for equality. */ | |
612 | ||
e70670cf | 613 | static bool |
edb983b2 | 614 | comdat_can_be_unshared_p (symtab_node node) |
430c6ceb | 615 | { |
edb983b2 | 616 | if (!comdat_can_be_unshared_p_1 (node)) |
430c6ceb | 617 | return false; |
960bfb69 | 618 | if (node->symbol.same_comdat_group) |
430c6ceb | 619 | { |
edb983b2 | 620 | symtab_node next; |
430c6ceb JH |
621 | |
622 | /* If more than one function is in the same COMDAT group, it must | |
623 | be shared even if just one function in the comdat group has | |
624 | address taken. */ | |
edb983b2 JH |
625 | for (next = node->symbol.same_comdat_group; |
626 | next != node; next = next->symbol.same_comdat_group) | |
627 | if (!comdat_can_be_unshared_p_1 (next)) | |
628 | return false; | |
430c6ceb JH |
629 | } |
630 | return true; | |
631 | } | |
632 | ||
4a444e58 JH |
633 | /* Return true when function NODE should be considered externally visible. */ |
634 | ||
b20996ff | 635 | static bool |
39e2db00 | 636 | cgraph_externally_visible_p (struct cgraph_node *node, |
5ac42672 | 637 | bool whole_program) |
b20996ff | 638 | { |
e70670cf | 639 | if (!node->symbol.definition) |
b820a2f9 | 640 | return false; |
e5b962d0 JH |
641 | if (!TREE_PUBLIC (node->symbol.decl) |
642 | || DECL_EXTERNAL (node->symbol.decl)) | |
b20996ff | 643 | return false; |
5dde3b01 | 644 | |
bf243ea7 JH |
645 | /* Do not try to localize built-in functions yet. One of problems is that we |
646 | end up mangling their asm for WHOPR that makes it impossible to call them | |
647 | using the implicit built-in declarations anymore. Similarly this enables | |
648 | us to remove them as unreachable before actual calls may appear during | |
649 | expansion or folding. */ | |
960bfb69 | 650 | if (DECL_BUILT_IN (node->symbol.decl)) |
bf243ea7 JH |
651 | return true; |
652 | ||
0e9ea52b | 653 | /* If linker counts on us, we must preserve the function. */ |
65d630d4 | 654 | if (symtab_used_from_object_file_p ((symtab_node) node)) |
0e9ea52b | 655 | return true; |
960bfb69 | 656 | if (DECL_PRESERVE_P (node->symbol.decl)) |
93a3eea4 | 657 | return true; |
960bfb69 JH |
658 | if (lookup_attribute ("externally_visible", |
659 | DECL_ATTRIBUTES (node->symbol.decl))) | |
93a3eea4 | 660 | return true; |
9d602c59 | 661 | if (TARGET_DLLIMPORT_DECL_ATTRIBUTES |
960bfb69 JH |
662 | && lookup_attribute ("dllexport", |
663 | DECL_ATTRIBUTES (node->symbol.decl))) | |
9d602c59 | 664 | return true; |
960bfb69 | 665 | if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY) |
c3e3f090 | 666 | return false; |
430c6ceb JH |
667 | /* When doing LTO or whole program, we can bring COMDAT functoins static. |
668 | This improves code quality and we know we will duplicate them at most twice | |
669 | (in the case that we are not using plugin and link with object file | |
670 | implementing same COMDAT) */ | |
671 | if ((in_lto_p || whole_program) | |
960bfb69 | 672 | && DECL_COMDAT (node->symbol.decl) |
edb983b2 | 673 | && comdat_can_be_unshared_p ((symtab_node) node)) |
430c6ceb JH |
674 | return false; |
675 | ||
5dde3b01 | 676 | /* When doing link time optimizations, hidden symbols become local. */ |
0e9ea52b | 677 | if (in_lto_p |
960bfb69 JH |
678 | && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN |
679 | || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL) | |
99fecd47 JH |
680 | /* Be sure that node is defined in IR file, not in other object |
681 | file. In that case we don't set used_from_other_object_file. */ | |
e70670cf | 682 | && node->symbol.definition) |
5dde3b01 JH |
683 | ; |
684 | else if (!whole_program) | |
b932b8b1 | 685 | return true; |
93a3eea4 | 686 | |
960bfb69 | 687 | if (MAIN_NAME_P (DECL_NAME (node->symbol.decl))) |
bb853349 | 688 | return true; |
93a3eea4 JH |
689 | |
690 | return false; | |
691 | } | |
692 | ||
693 | /* Return true when variable VNODE should be considered externally visible. */ | |
694 | ||
38877e98 | 695 | bool |
5ac42672 | 696 | varpool_externally_visible_p (struct varpool_node *vnode) |
93a3eea4 | 697 | { |
6649df51 JH |
698 | if (DECL_EXTERNAL (vnode->symbol.decl)) |
699 | return true; | |
700 | ||
e5b962d0 | 701 | if (!TREE_PUBLIC (vnode->symbol.decl)) |
93a3eea4 JH |
702 | return false; |
703 | ||
93a3eea4 | 704 | /* If linker counts on us, we must preserve the function. */ |
65d630d4 | 705 | if (symtab_used_from_object_file_p ((symtab_node) vnode)) |
93a3eea4 JH |
706 | return true; |
707 | ||
960bfb69 | 708 | if (DECL_HARD_REGISTER (vnode->symbol.decl)) |
a296a010 | 709 | return true; |
960bfb69 | 710 | if (DECL_PRESERVE_P (vnode->symbol.decl)) |
93a3eea4 JH |
711 | return true; |
712 | if (lookup_attribute ("externally_visible", | |
960bfb69 | 713 | DECL_ATTRIBUTES (vnode->symbol.decl))) |
93a3eea4 | 714 | return true; |
9d602c59 KT |
715 | if (TARGET_DLLIMPORT_DECL_ATTRIBUTES |
716 | && lookup_attribute ("dllexport", | |
960bfb69 | 717 | DECL_ATTRIBUTES (vnode->symbol.decl))) |
9d602c59 | 718 | return true; |
93a3eea4 JH |
719 | |
720 | /* See if we have linker information about symbol not being used or | |
721 | if we need to make guess based on the declaration. | |
722 | ||
723 | Even if the linker clams the symbol is unused, never bring internal | |
724 | symbols that are declared by user as used or externally visible. | |
725 | This is needed for i.e. references from asm statements. */ | |
65d630d4 | 726 | if (symtab_used_from_object_file_p ((symtab_node) vnode)) |
93a3eea4 | 727 | return true; |
960bfb69 | 728 | if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY) |
ed0d2da0 | 729 | return false; |
93a3eea4 | 730 | |
073a8998 | 731 | /* As a special case, the COMDAT virtual tables can be unshared. |
430c6ceb JH |
732 | In LTO mode turn vtables into static variables. The variable is readonly, |
733 | so this does not enable more optimization, but referring static var | |
734 | is faster for dynamic linking. Also this match logic hidding vtables | |
735 | from LTO symbol tables. */ | |
736 | if ((in_lto_p || flag_whole_program) | |
edb983b2 JH |
737 | && DECL_COMDAT (vnode->symbol.decl) |
738 | && comdat_can_be_unshared_p ((symtab_node) vnode)) | |
430c6ceb JH |
739 | return false; |
740 | ||
93a3eea4 JH |
741 | /* When doing link time optimizations, hidden symbols become local. */ |
742 | if (in_lto_p | |
960bfb69 JH |
743 | && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN |
744 | || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL) | |
93a3eea4 JH |
745 | /* Be sure that node is defined in IR file, not in other object |
746 | file. In that case we don't set used_from_other_object_file. */ | |
e70670cf | 747 | && vnode->symbol.definition) |
93a3eea4 JH |
748 | ; |
749 | else if (!flag_whole_program) | |
750 | return true; | |
751 | ||
752 | /* Do not attempt to privatize COMDATS by default. | |
753 | This would break linking with C++ libraries sharing | |
754 | inline definitions. | |
755 | ||
756 | FIXME: We can do so for readonly vars with no address taken and | |
757 | possibly also for vtables since no direct pointer comparsion is done. | |
758 | It might be interesting to do so to reduce linking overhead. */ | |
960bfb69 | 759 | if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl)) |
b20996ff JH |
760 | return true; |
761 | return false; | |
762 | } | |
763 | ||
af15184a JH |
764 | /* Return true if reference to NODE can be replaced by a local alias. |
765 | Local aliases save dynamic linking overhead and enable more optimizations. | |
766 | */ | |
767 | ||
768 | bool | |
769 | can_replace_by_local_alias (symtab_node node) | |
770 | { | |
771 | return (symtab_node_availability (node) > AVAIL_OVERWRITABLE | |
09ce3660 | 772 | && !symtab_can_be_discarded (node)); |
af15184a JH |
773 | } |
774 | ||
f4b3ca72 JH |
775 | /* Mark visibility of all functions. |
776 | ||
777 | A local function is one whose calls can occur only in the current | |
778 | compilation unit and all its calls are explicit, so we can change | |
779 | its calling convention. We simply mark all static functions whose | |
780 | address is not taken as local. | |
781 | ||
782 | We also change the TREE_PUBLIC flag of all declarations that are public | |
783 | in language point of view but we want to overwrite this default | |
784 | via visibilities for the backend point of view. */ | |
785 | ||
4e260309 | 786 | static unsigned int |
b20996ff | 787 | function_and_variable_visibility (bool whole_program) |
f4b3ca72 JH |
788 | { |
789 | struct cgraph_node *node; | |
790 | struct varpool_node *vnode; | |
5dde3b01 | 791 | |
5ac42672 JH |
792 | /* All aliases should be procssed at this point. */ |
793 | gcc_checking_assert (!alias_pairs || !alias_pairs->length()); | |
f4b3ca72 | 794 | |
65c70e6b | 795 | FOR_EACH_FUNCTION (node) |
f4b3ca72 | 796 | { |
960bfb69 | 797 | int flags = flags_from_decl_or_type (node->symbol.decl); |
8cb114b9 JH |
798 | |
799 | /* Optimize away PURE and CONST constructors and destructors. */ | |
530f3a1b JH |
800 | if (optimize |
801 | && (flags & (ECF_CONST | ECF_PURE)) | |
802 | && !(flags & ECF_LOOPING_CONST_OR_PURE)) | |
803 | { | |
960bfb69 JH |
804 | DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0; |
805 | DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0; | |
530f3a1b JH |
806 | } |
807 | ||
8cb114b9 JH |
808 | /* Frontends and alias code marks nodes as needed before parsing is finished. |
809 | We may end up marking as node external nodes where this flag is meaningless | |
810 | strip it. */ | |
edb983b2 JH |
811 | if (DECL_EXTERNAL (node->symbol.decl) || !node->symbol.definition) |
812 | { | |
813 | node->symbol.force_output = 0; | |
814 | node->symbol.forced_by_abi = 0; | |
815 | } | |
8cb114b9 | 816 | |
589520b6 JH |
817 | /* C++ FE on lack of COMDAT support create local COMDAT functions |
818 | (that ought to be shared but can not due to object format | |
073a8998 | 819 | limitations). It is necessary to keep the flag to make rest of C++ FE |
589520b6 | 820 | happy. Clear the flag here to avoid confusion in middle-end. */ |
960bfb69 JH |
821 | if (DECL_COMDAT (node->symbol.decl) && !TREE_PUBLIC (node->symbol.decl)) |
822 | DECL_COMDAT (node->symbol.decl) = 0; | |
08346abd JH |
823 | |
824 | /* For external decls stop tracking same_comdat_group. It doesn't matter | |
825 | what comdat group they are in when they won't be emitted in this TU. */ | |
960bfb69 | 826 | if (node->symbol.same_comdat_group && DECL_EXTERNAL (node->symbol.decl)) |
3fd54fb0 | 827 | { |
78eaf7bf | 828 | #ifdef ENABLE_CHECKING |
960bfb69 | 829 | symtab_node n; |
78eaf7bf | 830 | |
960bfb69 JH |
831 | for (n = node->symbol.same_comdat_group; |
832 | n != (symtab_node)node; | |
833 | n = n->symbol.same_comdat_group) | |
3fd54fb0 JJ |
834 | /* If at least one of same comdat group functions is external, |
835 | all of them have to be, otherwise it is a front-end bug. */ | |
960bfb69 | 836 | gcc_assert (DECL_EXTERNAL (n->symbol.decl)); |
78eaf7bf | 837 | #endif |
65d630d4 | 838 | symtab_dissolve_same_comdat_group_list ((symtab_node) node); |
3fd54fb0 | 839 | } |
960bfb69 JH |
840 | gcc_assert ((!DECL_WEAK (node->symbol.decl) |
841 | && !DECL_COMDAT (node->symbol.decl)) | |
842 | || TREE_PUBLIC (node->symbol.decl) | |
08346abd | 843 | || node->symbol.weakref |
960bfb69 | 844 | || DECL_EXTERNAL (node->symbol.decl)); |
5ac42672 | 845 | if (cgraph_externally_visible_p (node, whole_program)) |
b20996ff JH |
846 | { |
847 | gcc_assert (!node->global.inlined_to); | |
960bfb69 | 848 | node->symbol.externally_visible = true; |
b20996ff JH |
849 | } |
850 | else | |
edb983b2 JH |
851 | { |
852 | node->symbol.externally_visible = false; | |
853 | node->symbol.forced_by_abi = false; | |
854 | } | |
08346abd JH |
855 | if (!node->symbol.externally_visible |
856 | && node->symbol.definition && !node->symbol.weakref | |
960bfb69 | 857 | && !DECL_EXTERNAL (node->symbol.decl)) |
f4b3ca72 | 858 | { |
960bfb69 JH |
859 | gcc_assert (whole_program || in_lto_p |
860 | || !TREE_PUBLIC (node->symbol.decl)); | |
702d8703 JH |
861 | node->symbol.unique_name = ((node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY |
862 | || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP) | |
863 | && TREE_PUBLIC (node->symbol.decl)); | |
65d630d4 | 864 | symtab_make_decl_local (node->symbol.decl); |
960bfb69 JH |
865 | node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY; |
866 | if (node->symbol.same_comdat_group) | |
78eaf7bf MJ |
867 | /* cgraph_externally_visible_p has already checked all other nodes |
868 | in the group and they will all be made local. We need to | |
869 | dissolve the group at once so that the predicate does not | |
870 | segfault though. */ | |
65d630d4 | 871 | symtab_dissolve_same_comdat_group_list ((symtab_node) node); |
f4b3ca72 | 872 | } |
c47d0034 JH |
873 | |
874 | if (node->thunk.thunk_p | |
960bfb69 | 875 | && TREE_PUBLIC (node->symbol.decl)) |
c47d0034 JH |
876 | { |
877 | struct cgraph_node *decl_node = node; | |
878 | ||
39e2db00 | 879 | decl_node = cgraph_function_node (decl_node->callees->callee, NULL); |
c47d0034 JH |
880 | |
881 | /* Thunks have the same visibility as function they are attached to. | |
2fda8e14 | 882 | Make sure the C++ front end set this up properly. */ |
960bfb69 | 883 | if (DECL_ONE_ONLY (decl_node->symbol.decl)) |
c47d0034 | 884 | { |
960bfb69 JH |
885 | gcc_checking_assert (DECL_COMDAT (node->symbol.decl) |
886 | == DECL_COMDAT (decl_node->symbol.decl)); | |
887 | gcc_checking_assert (DECL_COMDAT_GROUP (node->symbol.decl) | |
888 | == DECL_COMDAT_GROUP (decl_node->symbol.decl)); | |
889 | gcc_checking_assert (node->symbol.same_comdat_group); | |
c47d0034 | 890 | } |
960bfb69 JH |
891 | if (DECL_EXTERNAL (decl_node->symbol.decl)) |
892 | DECL_EXTERNAL (node->symbol.decl) = 1; | |
c47d0034 | 893 | } |
f4b3ca72 | 894 | } |
65c70e6b | 895 | FOR_EACH_DEFINED_FUNCTION (node) |
af15184a | 896 | { |
51a5c0c2 | 897 | node->local.local |= cgraph_local_node_p (node); |
af15184a JH |
898 | |
899 | /* If we know that function can not be overwritten by a different semantics | |
900 | and moreover its section can not be discarded, replace all direct calls | |
901 | by calls to an nonoverwritable alias. This make dynamic linking | |
902 | cheaper and enable more optimization. | |
903 | ||
904 | TODO: We can also update virtual tables. */ | |
905 | if (node->callers && can_replace_by_local_alias ((symtab_node)node)) | |
906 | { | |
907 | struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias ((symtab_node) node)); | |
908 | ||
909 | if (alias != node) | |
910 | { | |
911 | while (node->callers) | |
912 | { | |
913 | struct cgraph_edge *e = node->callers; | |
914 | ||
915 | cgraph_redirect_edge_callee (e, alias); | |
a2e2a668 | 916 | if (gimple_has_body_p (e->caller->symbol.decl)) |
af15184a JH |
917 | { |
918 | push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl)); | |
919 | cgraph_redirect_edge_call_stmt_to_callee (e); | |
920 | pop_cfun (); | |
921 | } | |
922 | } | |
923 | } | |
924 | } | |
925 | } | |
65c70e6b | 926 | FOR_EACH_VARIABLE (vnode) |
e9fecf0e JH |
927 | { |
928 | /* weak flag makes no sense on local variables. */ | |
960bfb69 | 929 | gcc_assert (!DECL_WEAK (vnode->symbol.decl) |
08346abd | 930 | || vnode->symbol.weakref |
960bfb69 JH |
931 | || TREE_PUBLIC (vnode->symbol.decl) |
932 | || DECL_EXTERNAL (vnode->symbol.decl)); | |
e9fecf0e JH |
933 | /* In several cases declarations can not be common: |
934 | ||
935 | - when declaration has initializer | |
936 | - when it is in weak | |
937 | - when it has specific section | |
938 | - when it resides in non-generic address space. | |
939 | - if declaration is local, it will get into .local common section | |
940 | so common flag is not needed. Frontends still produce these in | |
941 | certain cases, such as for: | |
942 | ||
943 | static int a __attribute__ ((common)) | |
944 | ||
945 | Canonicalize things here and clear the redundant flag. */ | |
960bfb69 JH |
946 | if (DECL_COMMON (vnode->symbol.decl) |
947 | && (!(TREE_PUBLIC (vnode->symbol.decl) | |
948 | || DECL_EXTERNAL (vnode->symbol.decl)) | |
949 | || (DECL_INITIAL (vnode->symbol.decl) | |
950 | && DECL_INITIAL (vnode->symbol.decl) != error_mark_node) | |
951 | || DECL_WEAK (vnode->symbol.decl) | |
952 | || DECL_SECTION_NAME (vnode->symbol.decl) != NULL | |
e9fecf0e | 953 | || ! (ADDR_SPACE_GENERIC_P |
960bfb69 JH |
954 | (TYPE_ADDR_SPACE (TREE_TYPE (vnode->symbol.decl)))))) |
955 | DECL_COMMON (vnode->symbol.decl) = 0; | |
e9fecf0e | 956 | } |
65c70e6b | 957 | FOR_EACH_DEFINED_VARIABLE (vnode) |
f4b3ca72 | 958 | { |
e70670cf | 959 | if (!vnode->symbol.definition) |
b820a2f9 | 960 | continue; |
5ac42672 | 961 | if (varpool_externally_visible_p (vnode)) |
960bfb69 | 962 | vnode->symbol.externally_visible = true; |
b20996ff | 963 | else |
edb983b2 JH |
964 | { |
965 | vnode->symbol.externally_visible = false; | |
966 | vnode->symbol.forced_by_abi = false; | |
967 | } | |
08346abd JH |
968 | if (!vnode->symbol.externally_visible |
969 | && !vnode->symbol.weakref) | |
f4b3ca72 | 970 | { |
960bfb69 | 971 | gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->symbol.decl)); |
702d8703 JH |
972 | vnode->symbol.unique_name = ((vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY |
973 | || vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP) | |
974 | && TREE_PUBLIC (vnode->symbol.decl)); | |
7bf4274e | 975 | symtab_make_decl_local (vnode->symbol.decl); |
474ffc72 | 976 | if (vnode->symbol.same_comdat_group) |
65d630d4 | 977 | symtab_dissolve_same_comdat_group_list ((symtab_node) vnode); |
960bfb69 | 978 | vnode->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY; |
f4b3ca72 | 979 | } |
f4b3ca72 JH |
980 | } |
981 | ||
982 | if (dump_file) | |
983 | { | |
984 | fprintf (dump_file, "\nMarking local functions:"); | |
65c70e6b | 985 | FOR_EACH_DEFINED_FUNCTION (node) |
f4b3ca72 JH |
986 | if (node->local.local) |
987 | fprintf (dump_file, " %s", cgraph_node_name (node)); | |
988 | fprintf (dump_file, "\n\n"); | |
989 | fprintf (dump_file, "\nMarking externally visible functions:"); | |
65c70e6b | 990 | FOR_EACH_DEFINED_FUNCTION (node) |
960bfb69 | 991 | if (node->symbol.externally_visible) |
f4b3ca72 JH |
992 | fprintf (dump_file, " %s", cgraph_node_name (node)); |
993 | fprintf (dump_file, "\n\n"); | |
a8289259 | 994 | fprintf (dump_file, "\nMarking externally visible variables:"); |
65c70e6b | 995 | FOR_EACH_DEFINED_VARIABLE (vnode) |
960bfb69 | 996 | if (vnode->symbol.externally_visible) |
a8289259 JH |
997 | fprintf (dump_file, " %s", varpool_node_name (vnode)); |
998 | fprintf (dump_file, "\n\n"); | |
f4b3ca72 JH |
999 | } |
1000 | cgraph_function_flags_ready = true; | |
4e260309 | 1001 | return 0; |
f4b3ca72 JH |
1002 | } |
1003 | ||
b20996ff JH |
1004 | /* Local function pass handling visibilities. This happens before LTO streaming |
1005 | so in particular -fwhole-program should be ignored at this level. */ | |
1006 | ||
1007 | static unsigned int | |
1008 | local_function_and_variable_visibility (void) | |
1009 | { | |
014d92e1 | 1010 | return function_and_variable_visibility (flag_whole_program && !flag_lto); |
b20996ff JH |
1011 | } |
1012 | ||
27a4cd48 DM |
1013 | namespace { |
1014 | ||
1015 | const pass_data pass_data_ipa_function_and_variable_visibility = | |
f4b3ca72 | 1016 | { |
27a4cd48 DM |
1017 | SIMPLE_IPA_PASS, /* type */ |
1018 | "visibility", /* name */ | |
1019 | OPTGROUP_NONE, /* optinfo_flags */ | |
1020 | false, /* has_gate */ | |
1021 | true, /* has_execute */ | |
1022 | TV_CGRAPHOPT, /* tv_id */ | |
1023 | 0, /* properties_required */ | |
1024 | 0, /* properties_provided */ | |
1025 | 0, /* properties_destroyed */ | |
1026 | 0, /* todo_flags_start */ | |
1027 | ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */ | |
f4b3ca72 | 1028 | }; |
fed5ae11 | 1029 | |
27a4cd48 DM |
1030 | class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass |
1031 | { | |
1032 | public: | |
1033 | pass_ipa_function_and_variable_visibility(gcc::context *ctxt) | |
1034 | : simple_ipa_opt_pass(pass_data_ipa_function_and_variable_visibility, ctxt) | |
1035 | {} | |
1036 | ||
1037 | /* opt_pass methods: */ | |
1038 | unsigned int execute () { | |
1039 | return local_function_and_variable_visibility (); | |
1040 | } | |
1041 | ||
1042 | }; // class pass_ipa_function_and_variable_visibility | |
1043 | ||
1044 | } // anon namespace | |
1045 | ||
1046 | simple_ipa_opt_pass * | |
1047 | make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt) | |
1048 | { | |
1049 | return new pass_ipa_function_and_variable_visibility (ctxt); | |
1050 | } | |
1051 | ||
a8da72b8 L |
1052 | /* Free inline summary. */ |
1053 | ||
1054 | static unsigned | |
1055 | free_inline_summary (void) | |
1056 | { | |
1057 | inline_free_summary (); | |
1058 | return 0; | |
1059 | } | |
1060 | ||
27a4cd48 DM |
1061 | namespace { |
1062 | ||
1063 | const pass_data pass_data_ipa_free_inline_summary = | |
a8da72b8 | 1064 | { |
27a4cd48 DM |
1065 | SIMPLE_IPA_PASS, /* type */ |
1066 | "*free_inline_summary", /* name */ | |
1067 | OPTGROUP_NONE, /* optinfo_flags */ | |
1068 | false, /* has_gate */ | |
1069 | true, /* has_execute */ | |
1070 | TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */ | |
1071 | 0, /* properties_required */ | |
1072 | 0, /* properties_provided */ | |
1073 | 0, /* properties_destroyed */ | |
1074 | 0, /* todo_flags_start */ | |
1075 | 0, /* todo_flags_finish */ | |
a8da72b8 L |
1076 | }; |
1077 | ||
27a4cd48 DM |
1078 | class pass_ipa_free_inline_summary : public simple_ipa_opt_pass |
1079 | { | |
1080 | public: | |
1081 | pass_ipa_free_inline_summary(gcc::context *ctxt) | |
1082 | : simple_ipa_opt_pass(pass_data_ipa_free_inline_summary, ctxt) | |
1083 | {} | |
1084 | ||
1085 | /* opt_pass methods: */ | |
1086 | unsigned int execute () { return free_inline_summary (); } | |
1087 | ||
1088 | }; // class pass_ipa_free_inline_summary | |
1089 | ||
1090 | } // anon namespace | |
1091 | ||
1092 | simple_ipa_opt_pass * | |
1093 | make_pass_ipa_free_inline_summary (gcc::context *ctxt) | |
1094 | { | |
1095 | return new pass_ipa_free_inline_summary (ctxt); | |
1096 | } | |
1097 | ||
b20996ff JH |
1098 | /* Do not re-run on ltrans stage. */ |
1099 | ||
1100 | static bool | |
1101 | gate_whole_program_function_and_variable_visibility (void) | |
1102 | { | |
1103 | return !flag_ltrans; | |
1104 | } | |
1105 | ||
073a8998 | 1106 | /* Bring functionss local at LTO time with -fwhole-program. */ |
b20996ff JH |
1107 | |
1108 | static unsigned int | |
1109 | whole_program_function_and_variable_visibility (void) | |
1110 | { | |
b20996ff | 1111 | function_and_variable_visibility (flag_whole_program); |
f10ea640 JH |
1112 | if (optimize) |
1113 | ipa_discover_readonly_nonaddressable_vars (); | |
b20996ff JH |
1114 | return 0; |
1115 | } | |
1116 | ||
27a4cd48 DM |
1117 | namespace { |
1118 | ||
1119 | const pass_data pass_data_ipa_whole_program_visibility = | |
b20996ff | 1120 | { |
27a4cd48 DM |
1121 | IPA_PASS, /* type */ |
1122 | "whole-program", /* name */ | |
1123 | OPTGROUP_NONE, /* optinfo_flags */ | |
1124 | true, /* has_gate */ | |
1125 | true, /* has_execute */ | |
1126 | TV_CGRAPHOPT, /* tv_id */ | |
1127 | 0, /* properties_required */ | |
1128 | 0, /* properties_provided */ | |
1129 | 0, /* properties_destroyed */ | |
1130 | 0, /* todo_flags_start */ | |
1131 | ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */ | |
b20996ff | 1132 | }; |
fed5ae11 | 1133 | |
27a4cd48 DM |
1134 | class pass_ipa_whole_program_visibility : public ipa_opt_pass_d |
1135 | { | |
1136 | public: | |
1137 | pass_ipa_whole_program_visibility(gcc::context *ctxt) | |
1138 | : ipa_opt_pass_d(pass_data_ipa_whole_program_visibility, ctxt, | |
1139 | NULL, /* generate_summary */ | |
1140 | NULL, /* write_summary */ | |
1141 | NULL, /* read_summary */ | |
1142 | NULL, /* write_optimization_summary */ | |
1143 | NULL, /* read_optimization_summary */ | |
1144 | NULL, /* stmt_fixup */ | |
1145 | 0, /* function_transform_todo_flags_start */ | |
1146 | NULL, /* function_transform */ | |
1147 | NULL) /* variable_transform */ | |
1148 | {} | |
1149 | ||
1150 | /* opt_pass methods: */ | |
1151 | bool gate () { | |
1152 | return gate_whole_program_function_and_variable_visibility (); | |
1153 | } | |
1154 | unsigned int execute () { | |
1155 | return whole_program_function_and_variable_visibility (); | |
1156 | } | |
1157 | ||
1158 | }; // class pass_ipa_whole_program_visibility | |
1159 | ||
1160 | } // anon namespace | |
1161 | ||
1162 | ipa_opt_pass_d * | |
1163 | make_pass_ipa_whole_program_visibility (gcc::context *ctxt) | |
1164 | { | |
1165 | return new pass_ipa_whole_program_visibility (ctxt); | |
1166 | } | |
1167 | ||
9e97ff61 JH |
1168 | /* Generate and emit a static constructor or destructor. WHICH must |
1169 | be one of 'I' (for a constructor) or 'D' (for a destructor). BODY | |
1170 | is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the | |
3a9ed12a | 1171 | initialization priority for this constructor or destructor. |
9e97ff61 | 1172 | |
3a9ed12a JH |
1173 | FINAL specify whether the externally visible name for collect2 should |
1174 | be produced. */ | |
1175 | ||
1176 | static void | |
1177 | cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final) | |
9e97ff61 JH |
1178 | { |
1179 | static int counter = 0; | |
1180 | char which_buf[16]; | |
1181 | tree decl, name, resdecl; | |
1182 | ||
1183 | /* The priority is encoded in the constructor or destructor name. | |
1184 | collect2 will sort the names and arrange that they are called at | |
1185 | program startup. */ | |
3a9ed12a JH |
1186 | if (final) |
1187 | sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++); | |
1188 | else | |
1189 | /* Proudce sane name but one not recognizable by collect2, just for the | |
1190 | case we fail to inline the function. */ | |
1191 | sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++); | |
9e97ff61 JH |
1192 | name = get_file_function_name (which_buf); |
1193 | ||
1194 | decl = build_decl (input_location, FUNCTION_DECL, name, | |
1195 | build_function_type_list (void_type_node, NULL_TREE)); | |
1196 | current_function_decl = decl; | |
1197 | ||
1198 | resdecl = build_decl (input_location, | |
1199 | RESULT_DECL, NULL_TREE, void_type_node); | |
1200 | DECL_ARTIFICIAL (resdecl) = 1; | |
1201 | DECL_RESULT (decl) = resdecl; | |
1202 | DECL_CONTEXT (resdecl) = decl; | |
1203 | ||
1204 | allocate_struct_function (decl, false); | |
1205 | ||
1206 | TREE_STATIC (decl) = 1; | |
1207 | TREE_USED (decl) = 1; | |
1208 | DECL_ARTIFICIAL (decl) = 1; | |
1209 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
1210 | DECL_SAVED_TREE (decl) = body; | |
3a9ed12a | 1211 | if (!targetm.have_ctors_dtors && final) |
9e97ff61 JH |
1212 | { |
1213 | TREE_PUBLIC (decl) = 1; | |
1214 | DECL_PRESERVE_P (decl) = 1; | |
1215 | } | |
1216 | DECL_UNINLINABLE (decl) = 1; | |
1217 | ||
1218 | DECL_INITIAL (decl) = make_node (BLOCK); | |
1219 | TREE_USED (DECL_INITIAL (decl)) = 1; | |
1220 | ||
1221 | DECL_SOURCE_LOCATION (decl) = input_location; | |
1222 | cfun->function_end_locus = input_location; | |
1223 | ||
1224 | switch (which) | |
1225 | { | |
1226 | case 'I': | |
1227 | DECL_STATIC_CONSTRUCTOR (decl) = 1; | |
1228 | decl_init_priority_insert (decl, priority); | |
1229 | break; | |
1230 | case 'D': | |
1231 | DECL_STATIC_DESTRUCTOR (decl) = 1; | |
1232 | decl_fini_priority_insert (decl, priority); | |
1233 | break; | |
1234 | default: | |
1235 | gcc_unreachable (); | |
1236 | } | |
1237 | ||
1238 | gimplify_function_tree (decl); | |
1239 | ||
1240 | cgraph_add_new_function (decl, false); | |
1241 | ||
1242 | set_cfun (NULL); | |
1243 | current_function_decl = NULL; | |
1244 | } | |
1245 | ||
3a9ed12a JH |
1246 | /* Generate and emit a static constructor or destructor. WHICH must |
1247 | be one of 'I' (for a constructor) or 'D' (for a destructor). BODY | |
1248 | is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the | |
1249 | initialization priority for this constructor or destructor. */ | |
1250 | ||
1251 | void | |
1252 | cgraph_build_static_cdtor (char which, tree body, int priority) | |
1253 | { | |
1254 | cgraph_build_static_cdtor_1 (which, body, priority, false); | |
1255 | } | |
9e97ff61 JH |
1256 | |
1257 | /* A vector of FUNCTION_DECLs declared as static constructors. */ | |
9771b263 | 1258 | static vec<tree> static_ctors; |
9e97ff61 | 1259 | /* A vector of FUNCTION_DECLs declared as static destructors. */ |
9771b263 | 1260 | static vec<tree> static_dtors; |
9e97ff61 JH |
1261 | |
1262 | /* When target does not have ctors and dtors, we call all constructor | |
1263 | and destructor by special initialization/destruction function | |
1264 | recognized by collect2. | |
1265 | ||
1266 | When we are going to build this function, collect all constructors and | |
1267 | destructors and turn them into normal functions. */ | |
1268 | ||
1269 | static void | |
1270 | record_cdtor_fn (struct cgraph_node *node) | |
1271 | { | |
960bfb69 | 1272 | if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)) |
9771b263 | 1273 | static_ctors.safe_push (node->symbol.decl); |
960bfb69 | 1274 | if (DECL_STATIC_DESTRUCTOR (node->symbol.decl)) |
9771b263 | 1275 | static_dtors.safe_push (node->symbol.decl); |
960bfb69 JH |
1276 | node = cgraph_get_node (node->symbol.decl); |
1277 | DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1; | |
9e97ff61 JH |
1278 | } |
1279 | ||
1280 | /* Define global constructors/destructor functions for the CDTORS, of | |
1281 | which they are LEN. The CDTORS are sorted by initialization | |
1282 | priority. If CTOR_P is true, these are constructors; otherwise, | |
1283 | they are destructors. */ | |
1284 | ||
1285 | static void | |
9771b263 | 1286 | build_cdtor (bool ctor_p, vec<tree> cdtors) |
9e97ff61 JH |
1287 | { |
1288 | size_t i,j; | |
9771b263 | 1289 | size_t len = cdtors.length (); |
9e97ff61 JH |
1290 | |
1291 | i = 0; | |
1292 | while (i < len) | |
1293 | { | |
1294 | tree body; | |
1295 | tree fn; | |
1296 | priority_type priority; | |
1297 | ||
1298 | priority = 0; | |
1299 | body = NULL_TREE; | |
1300 | j = i; | |
1301 | do | |
1302 | { | |
1303 | priority_type p; | |
9771b263 | 1304 | fn = cdtors[j]; |
9e97ff61 JH |
1305 | p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn); |
1306 | if (j == i) | |
1307 | priority = p; | |
1308 | else if (p != priority) | |
1309 | break; | |
1310 | j++; | |
1311 | } | |
1312 | while (j < len); | |
1313 | ||
48c24aca | 1314 | /* When there is only one cdtor and target supports them, do nothing. */ |
9e97ff61 JH |
1315 | if (j == i + 1 |
1316 | && targetm.have_ctors_dtors) | |
1317 | { | |
1318 | i++; | |
1319 | continue; | |
1320 | } | |
1321 | /* Find the next batch of constructors/destructors with the same | |
1322 | initialization priority. */ | |
48c24aca | 1323 | for (;i < j; i++) |
9e97ff61 | 1324 | { |
9e97ff61 | 1325 | tree call; |
9771b263 | 1326 | fn = cdtors[i]; |
9e97ff61 JH |
1327 | call = build_call_expr (fn, 0); |
1328 | if (ctor_p) | |
1329 | DECL_STATIC_CONSTRUCTOR (fn) = 0; | |
1330 | else | |
1331 | DECL_STATIC_DESTRUCTOR (fn) = 0; | |
1332 | /* We do not want to optimize away pure/const calls here. | |
1333 | When optimizing, these should be already removed, when not | |
1334 | optimizing, we want user to be able to breakpoint in them. */ | |
1335 | TREE_SIDE_EFFECTS (call) = 1; | |
1336 | append_to_statement_list (call, &body); | |
9e97ff61 | 1337 | } |
9e97ff61 JH |
1338 | gcc_assert (body != NULL_TREE); |
1339 | /* Generate a function to call all the function of like | |
1340 | priority. */ | |
3a9ed12a | 1341 | cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true); |
9e97ff61 JH |
1342 | } |
1343 | } | |
1344 | ||
1345 | /* Comparison function for qsort. P1 and P2 are actually of type | |
1346 | "tree *" and point to static constructors. DECL_INIT_PRIORITY is | |
1347 | used to determine the sort order. */ | |
1348 | ||
1349 | static int | |
1350 | compare_ctor (const void *p1, const void *p2) | |
1351 | { | |
1352 | tree f1; | |
1353 | tree f2; | |
1354 | int priority1; | |
1355 | int priority2; | |
1356 | ||
1357 | f1 = *(const tree *)p1; | |
1358 | f2 = *(const tree *)p2; | |
1359 | priority1 = DECL_INIT_PRIORITY (f1); | |
1360 | priority2 = DECL_INIT_PRIORITY (f2); | |
1361 | ||
1362 | if (priority1 < priority2) | |
1363 | return -1; | |
1364 | else if (priority1 > priority2) | |
1365 | return 1; | |
1366 | else | |
1367 | /* Ensure a stable sort. Constructors are executed in backwarding | |
1368 | order to make LTO initialize braries first. */ | |
1369 | return DECL_UID (f2) - DECL_UID (f1); | |
1370 | } | |
1371 | ||
1372 | /* Comparison function for qsort. P1 and P2 are actually of type | |
1373 | "tree *" and point to static destructors. DECL_FINI_PRIORITY is | |
1374 | used to determine the sort order. */ | |
1375 | ||
1376 | static int | |
1377 | compare_dtor (const void *p1, const void *p2) | |
1378 | { | |
1379 | tree f1; | |
1380 | tree f2; | |
1381 | int priority1; | |
1382 | int priority2; | |
1383 | ||
1384 | f1 = *(const tree *)p1; | |
1385 | f2 = *(const tree *)p2; | |
1386 | priority1 = DECL_FINI_PRIORITY (f1); | |
1387 | priority2 = DECL_FINI_PRIORITY (f2); | |
1388 | ||
1389 | if (priority1 < priority2) | |
1390 | return -1; | |
1391 | else if (priority1 > priority2) | |
1392 | return 1; | |
1393 | else | |
1394 | /* Ensure a stable sort. */ | |
1395 | return DECL_UID (f1) - DECL_UID (f2); | |
1396 | } | |
1397 | ||
1398 | /* Generate functions to call static constructors and destructors | |
1399 | for targets that do not support .ctors/.dtors sections. These | |
1400 | functions have magic names which are detected by collect2. */ | |
1401 | ||
1402 | static void | |
1403 | build_cdtor_fns (void) | |
1404 | { | |
9771b263 | 1405 | if (!static_ctors.is_empty ()) |
9e97ff61 JH |
1406 | { |
1407 | gcc_assert (!targetm.have_ctors_dtors || in_lto_p); | |
9771b263 | 1408 | static_ctors.qsort (compare_ctor); |
48c24aca | 1409 | build_cdtor (/*ctor_p=*/true, static_ctors); |
9e97ff61 JH |
1410 | } |
1411 | ||
9771b263 | 1412 | if (!static_dtors.is_empty ()) |
9e97ff61 JH |
1413 | { |
1414 | gcc_assert (!targetm.have_ctors_dtors || in_lto_p); | |
9771b263 | 1415 | static_dtors.qsort (compare_dtor); |
48c24aca | 1416 | build_cdtor (/*ctor_p=*/false, static_dtors); |
9e97ff61 JH |
1417 | } |
1418 | } | |
1419 | ||
1420 | /* Look for constructors and destructors and produce function calling them. | |
1421 | This is needed for targets not supporting ctors or dtors, but we perform the | |
073a8998 | 1422 | transformation also at linktime to merge possibly numerous |
9e97ff61 JH |
1423 | constructors/destructors into single function to improve code locality and |
1424 | reduce size. */ | |
1425 | ||
1426 | static unsigned int | |
1427 | ipa_cdtor_merge (void) | |
1428 | { | |
1429 | struct cgraph_node *node; | |
65c70e6b JH |
1430 | FOR_EACH_DEFINED_FUNCTION (node) |
1431 | if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl) | |
1432 | || DECL_STATIC_DESTRUCTOR (node->symbol.decl)) | |
9e97ff61 JH |
1433 | record_cdtor_fn (node); |
1434 | build_cdtor_fns (); | |
9771b263 DN |
1435 | static_ctors.release (); |
1436 | static_dtors.release (); | |
9e97ff61 JH |
1437 | return 0; |
1438 | } | |
1439 | ||
1440 | /* Perform the pass when we have no ctors/dtors support | |
1441 | or at LTO time to merge multiple constructors into single | |
1442 | function. */ | |
1443 | ||
1444 | static bool | |
1445 | gate_ipa_cdtor_merge (void) | |
1446 | { | |
1447 | return !targetm.have_ctors_dtors || (optimize && in_lto_p); | |
1448 | } | |
1449 | ||
27a4cd48 DM |
1450 | namespace { |
1451 | ||
1452 | const pass_data pass_data_ipa_cdtor_merge = | |
9e97ff61 | 1453 | { |
27a4cd48 DM |
1454 | IPA_PASS, /* type */ |
1455 | "cdtor", /* name */ | |
1456 | OPTGROUP_NONE, /* optinfo_flags */ | |
1457 | true, /* has_gate */ | |
1458 | true, /* has_execute */ | |
1459 | TV_CGRAPHOPT, /* tv_id */ | |
1460 | 0, /* properties_required */ | |
1461 | 0, /* properties_provided */ | |
1462 | 0, /* properties_destroyed */ | |
1463 | 0, /* todo_flags_start */ | |
1464 | 0, /* todo_flags_finish */ | |
9e97ff61 | 1465 | }; |
27a4cd48 DM |
1466 | |
1467 | class pass_ipa_cdtor_merge : public ipa_opt_pass_d | |
1468 | { | |
1469 | public: | |
1470 | pass_ipa_cdtor_merge(gcc::context *ctxt) | |
1471 | : ipa_opt_pass_d(pass_data_ipa_cdtor_merge, ctxt, | |
1472 | NULL, /* generate_summary */ | |
1473 | NULL, /* write_summary */ | |
1474 | NULL, /* read_summary */ | |
1475 | NULL, /* write_optimization_summary */ | |
1476 | NULL, /* read_optimization_summary */ | |
1477 | NULL, /* stmt_fixup */ | |
1478 | 0, /* function_transform_todo_flags_start */ | |
1479 | NULL, /* function_transform */ | |
1480 | NULL) /* variable_transform */ | |
1481 | {} | |
1482 | ||
1483 | /* opt_pass methods: */ | |
1484 | bool gate () { return gate_ipa_cdtor_merge (); } | |
1485 | unsigned int execute () { return ipa_cdtor_merge (); } | |
1486 | ||
1487 | }; // class pass_ipa_cdtor_merge | |
1488 | ||
1489 | } // anon namespace | |
1490 | ||
1491 | ipa_opt_pass_d * | |
1492 | make_pass_ipa_cdtor_merge (gcc::context *ctxt) | |
1493 | { | |
1494 | return new pass_ipa_cdtor_merge (ctxt); | |
1495 | } |