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