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