]>
Commit | Line | Data |
---|---|---|
ca31b95f | 1 | /* Basic IPA optimizations and utilities. |
85ec4feb | 2 | Copyright (C) 2003-2018 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" | |
c7131fb2 | 23 | #include "backend.h" |
957060b5 | 24 | #include "target.h" |
c7131fb2 AM |
25 | #include "tree.h" |
26 | #include "gimple.h" | |
957060b5 AM |
27 | #include "alloc-pool.h" |
28 | #include "tree-pass.h" | |
29 | #include "stringpool.h" | |
30 | #include "cgraph.h" | |
45b0be94 | 31 | #include "gimplify.h" |
9e97ff61 | 32 | #include "tree-iterator.h" |
af8bca3c | 33 | #include "ipa-utils.h" |
dd912cb8 | 34 | #include "symbol-summary.h" |
8bc5448f | 35 | #include "tree-vrp.h" |
c582198b | 36 | #include "ipa-prop.h" |
27d020cf | 37 | #include "ipa-fnsummary.h" |
2b5f0895 | 38 | #include "dbgcnt.h" |
775669c1 | 39 | #include "debug.h" |
314e6352 ML |
40 | #include "stringpool.h" |
41 | #include "attribs.h" | |
e70670cf JH |
42 | |
43 | /* Return true when NODE has ADDR reference. */ | |
44 | ||
45 | static bool | |
46 | has_addr_references_p (struct cgraph_node *node, | |
4f4ada6a | 47 | void *) |
e70670cf JH |
48 | { |
49 | int i; | |
d122681a | 50 | struct ipa_ref *ref = NULL; |
e70670cf | 51 | |
d122681a | 52 | for (i = 0; node->iterate_referring (i, ref); i++) |
e70670cf JH |
53 | if (ref->use == IPA_REF_ADDR) |
54 | return true; | |
55 | return false; | |
56 | } | |
57 | ||
4f4ada6a JH |
58 | /* Return true when NODE can be target of an indirect call. */ |
59 | ||
60 | static bool | |
61 | is_indirect_call_target_p (struct cgraph_node *node, void *) | |
62 | { | |
63 | return node->indirect_call_target; | |
64 | } | |
65 | ||
d563610d JH |
66 | /* Look for all functions inlined to NODE and update their inlined_to pointers |
67 | to INLINED_TO. */ | |
68 | ||
69 | static void | |
70 | update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to) | |
71 | { | |
72 | struct cgraph_edge *e; | |
73 | for (e = node->callees; e; e = e->next_callee) | |
74 | if (e->callee->global.inlined_to) | |
75 | { | |
76 | e->callee->global.inlined_to = inlined_to; | |
77 | update_inlined_to_pointer (e->callee, inlined_to); | |
78 | } | |
79 | } | |
80 | ||
04142cc3 | 81 | /* Add symtab NODE to queue starting at FIRST. |
19fb0b86 JH |
82 | |
83 | The queue is linked via AUX pointers and terminated by pointer to 1. | |
84 | We enqueue nodes at two occasions: when we find them reachable or when we find | |
85 | their bodies needed for further clonning. In the second case we mark them | |
86 | by pointer to 2 after processing so they are re-queue when they become | |
87 | reachable. */ | |
b34fd25c JH |
88 | |
89 | static void | |
5e20cdc9 | 90 | enqueue_node (symtab_node *node, symtab_node **first, |
6e2830c3 | 91 | hash_set<symtab_node *> *reachable) |
b34fd25c | 92 | { |
19fb0b86 | 93 | /* Node is still in queue; do nothing. */ |
67348ccc | 94 | if (node->aux && node->aux != (void *) 2) |
19fb0b86 JH |
95 | return; |
96 | /* Node was already processed as unreachable, re-enqueue | |
97 | only if it became reachable now. */ | |
6e2830c3 | 98 | if (node->aux == (void *)2 && !reachable->contains (node)) |
19fb0b86 | 99 | return; |
67348ccc | 100 | node->aux = *first; |
b34fd25c JH |
101 | *first = node; |
102 | } | |
103 | ||
12485662 JH |
104 | /* Return true if NODE may get inlined later. |
105 | This is used to keep DECL_EXTERNAL function bodies around long enough | |
106 | so inliner can proces them. */ | |
107 | ||
108 | static bool | |
109 | possible_inline_candidate_p (symtab_node *node) | |
110 | { | |
111 | if (symtab->state >= IPA_SSA_AFTER_INLINING) | |
112 | return false; | |
113 | cgraph_node *cnode = dyn_cast <cgraph_node *> (node); | |
114 | if (!cnode) | |
115 | return false; | |
116 | if (DECL_UNINLINABLE (cnode->decl)) | |
117 | return false; | |
118 | if (opt_for_fn (cnode->decl, optimize)) | |
119 | return true; | |
120 | if (symtab->state >= IPA_SSA) | |
121 | return false; | |
122 | return lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl)); | |
123 | } | |
124 | ||
b34fd25c JH |
125 | /* Process references. */ |
126 | ||
127 | static void | |
d122681a | 128 | process_references (symtab_node *snode, |
5e20cdc9 | 129 | symtab_node **first, |
6e2830c3 | 130 | hash_set<symtab_node *> *reachable) |
b34fd25c JH |
131 | { |
132 | int i; | |
d122681a ML |
133 | struct ipa_ref *ref = NULL; |
134 | for (i = 0; snode->iterate_reference (i, ref); i++) | |
b34fd25c | 135 | { |
5e20cdc9 | 136 | symtab_node *node = ref->referred; |
17e0fc92 | 137 | symtab_node *body = node->ultimate_alias_target (); |
e70670cf | 138 | |
67348ccc DM |
139 | if (node->definition && !node->in_other_partition |
140 | && ((!DECL_EXTERNAL (node->decl) || node->alias) | |
12485662 | 141 | || (possible_inline_candidate_p (node) |
17e0fc92 | 142 | /* We use variable constructors during late compilation for |
e70670cf JH |
143 | constant folding. Keep references alive so partitioning |
144 | knows about potential references. */ | |
8813a647 | 145 | || (VAR_P (node->decl) |
5b42d196 JH |
146 | && (flag_wpa |
147 | || flag_incremental_link | |
148 | == INCREMENTAL_LINK_LTO) | |
149 | && dyn_cast <varpool_node *> (node) | |
150 | ->ctor_useable_for_folding_p ())))) | |
17e0fc92 JH |
151 | { |
152 | /* Be sure that we will not optimize out alias target | |
153 | body. */ | |
154 | if (DECL_EXTERNAL (node->decl) | |
155 | && node->alias | |
12485662 | 156 | && symtab->state < IPA_SSA_AFTER_INLINING) |
17e0fc92 JH |
157 | reachable->add (body); |
158 | reachable->add (node); | |
159 | } | |
67348ccc | 160 | enqueue_node (node, first, reachable); |
b34fd25c JH |
161 | } |
162 | } | |
163 | ||
3462aa02 JH |
164 | /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark |
165 | all its potential targets as reachable to permit later inlining if | |
166 | devirtualization happens. After inlining still keep their declarations | |
167 | around, so we can devirtualize to a direct call. | |
168 | ||
169 | Also try to make trivial devirutalization when no or only one target is | |
170 | possible. */ | |
171 | ||
172 | static void | |
6e2830c3 | 173 | walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets, |
3462aa02 | 174 | struct cgraph_edge *edge, |
5e20cdc9 | 175 | symtab_node **first, |
12485662 | 176 | hash_set<symtab_node *> *reachable) |
3462aa02 JH |
177 | { |
178 | unsigned int i; | |
179 | void *cache_token; | |
180 | bool final; | |
181 | vec <cgraph_node *>targets | |
182 | = possible_polymorphic_call_targets | |
183 | (edge, &final, &cache_token); | |
184 | ||
6e2830c3 | 185 | if (!reachable_call_targets->add (cache_token)) |
3462aa02 | 186 | { |
c3284718 | 187 | for (i = 0; i < targets.length (); i++) |
3462aa02 JH |
188 | { |
189 | struct cgraph_node *n = targets[i]; | |
190 | ||
191 | /* Do not bother to mark virtual methods in anonymous namespace; | |
192 | either we will find use of virtual table defining it, or it is | |
193 | unused. */ | |
67348ccc | 194 | if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE |
3462aa02 | 195 | && type_in_anonymous_namespace_p |
70e7f2a2 | 196 | (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)))) |
3462aa02 JH |
197 | continue; |
198 | ||
4f4ada6a JH |
199 | n->indirect_call_target = true; |
200 | symtab_node *body = n->function_symbol (); | |
17e0fc92 | 201 | |
3462aa02 JH |
202 | /* Prior inlining, keep alive bodies of possible targets for |
203 | devirtualization. */ | |
4f4ada6a | 204 | if (n->definition |
12485662 | 205 | && (possible_inline_candidate_p (body) |
4f4ada6a JH |
206 | && opt_for_fn (body->decl, flag_devirtualize))) |
207 | { | |
208 | /* Be sure that we will not optimize out alias target | |
209 | body. */ | |
210 | if (DECL_EXTERNAL (n->decl) | |
211 | && n->alias | |
12485662 | 212 | && symtab->state < IPA_SSA_AFTER_INLINING) |
4f4ada6a JH |
213 | reachable->add (body); |
214 | reachable->add (n); | |
215 | } | |
3462aa02 JH |
216 | /* Even after inlining we want to keep the possible targets in the |
217 | boundary, so late passes can still produce direct call even if | |
218 | the chance for inlining is lost. */ | |
67348ccc | 219 | enqueue_node (n, first, reachable); |
3462aa02 JH |
220 | } |
221 | } | |
222 | ||
223 | /* Very trivial devirtualization; when the type is | |
224 | final or anonymous (so we know all its derivation) | |
225 | and there is only one possible virtual call target, | |
226 | make the edge direct. */ | |
227 | if (final) | |
228 | { | |
2b5f0895 | 229 | if (targets.length () <= 1 && dbg_cnt (devirt)) |
3462aa02 | 230 | { |
7b395ddd | 231 | cgraph_node *target, *node = edge->caller; |
3462aa02 JH |
232 | if (targets.length () == 1) |
233 | target = targets[0]; | |
234 | else | |
d52f5295 | 235 | target = cgraph_node::get_create |
3462aa02 JH |
236 | (builtin_decl_implicit (BUILT_IN_UNREACHABLE)); |
237 | ||
2b5f0895 | 238 | if (dump_enabled_p ()) |
4f5b9c80 DM |
239 | { |
240 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt, | |
464d0118 ML |
241 | "devirtualizing call in %s to %s\n", |
242 | edge->caller->dump_name (), | |
243 | target->dump_name ()); | |
2b5f0895 | 244 | } |
3dafb85c | 245 | edge = edge->make_direct (target); |
0bceb671 JH |
246 | if (ipa_fn_summaries) |
247 | ipa_update_overall_fn_summary (node); | |
477145c8 | 248 | else if (edge->call_stmt) |
31db0fe0 | 249 | edge->redirect_call_stmt_to_callee (); |
3462aa02 JH |
250 | } |
251 | } | |
252 | } | |
41817394 | 253 | |
ca31b95f | 254 | /* Perform reachability analysis and reclaim all unreachable nodes. |
04142cc3 JH |
255 | |
256 | The algorithm is basically mark&sweep but with some extra refinements: | |
257 | ||
258 | - reachable extern inline functions needs special handling; the bodies needs | |
259 | to stay in memory until inlining in hope that they will be inlined. | |
260 | After inlining we release their bodies and turn them into unanalyzed | |
261 | nodes even when they are reachable. | |
262 | ||
04142cc3 JH |
263 | - virtual functions are kept in callgraph even if they seem unreachable in |
264 | hope calls to them will be devirtualized. | |
265 | ||
266 | Again we remove them after inlining. In late optimization some | |
31519c38 | 267 | devirtualization may happen, but it is not important since we won't inline |
04142cc3 JH |
268 | the call. In theory early opts and IPA should work out all important cases. |
269 | ||
270 | - virtual clones needs bodies of their origins for later materialization; | |
271 | this means that we want to keep the body even if the origin is unreachable | |
272 | otherwise. To avoid origin from sitting in the callgraph and being | |
273 | walked by IPA passes, we turn them into unanalyzed nodes with body | |
274 | defined. | |
275 | ||
276 | We maintain set of function declaration where body needs to stay in | |
277 | body_needed_for_clonning | |
278 | ||
279 | Inline clones represent special case: their declaration match the | |
280 | declaration of origin and cgraph_remove_node already knows how to | |
281 | reshape callgraph and preserve body when offline copy of function or | |
282 | inline clone is being removed. | |
283 | ||
6649df51 JH |
284 | - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL |
285 | variables with DECL_INITIAL set. We finalize these and keep reachable | |
286 | ones around for constant folding purposes. After inlining we however | |
287 | stop walking their references to let everything static referneced by them | |
288 | to be removed when it is otherwise unreachable. | |
289 | ||
04142cc3 JH |
290 | We maintain queue of both reachable symbols (i.e. defined symbols that needs |
291 | to stay) and symbols that are in boundary (i.e. external symbols referenced | |
292 | by reachable symbols or origins of clones). The queue is represented | |
293 | as linked list by AUX pointer terminated by 1. | |
294 | ||
31519c38 | 295 | At the end we keep all reachable symbols. For symbols in boundary we always |
04142cc3 JH |
296 | turn definition into a declaration, but we may keep function body around |
297 | based on body_needed_for_clonning | |
298 | ||
299 | All symbols that enter the queue have AUX pointer non-zero and are in the | |
300 | boundary. Pointer set REACHABLE is used to track reachable symbols. | |
301 | ||
302 | Every symbol can be visited twice - once as part of boundary and once | |
303 | as real reachable symbol. enqueue_node needs to decide whether the | |
304 | node needs to be re-queued for second processing. For this purpose | |
305 | we set AUX pointer of processed symbols in the boundary to constant 2. */ | |
ca31b95f JH |
306 | |
307 | bool | |
17e0fc92 | 308 | symbol_table::remove_unreachable_nodes (FILE *file) |
ca31b95f | 309 | { |
5e20cdc9 | 310 | symtab_node *first = (symtab_node *) (void *) 1; |
96fc428c | 311 | struct cgraph_node *node, *next; |
2c8326a5 | 312 | varpool_node *vnode, *vnext; |
ca31b95f | 313 | bool changed = false; |
6e2830c3 TS |
314 | hash_set<symtab_node *> reachable; |
315 | hash_set<tree> body_needed_for_clonning; | |
316 | hash_set<void *> reachable_call_targets; | |
ca31b95f | 317 | |
3462aa02 | 318 | timevar_push (TV_IPA_UNREACHABLE); |
2bf86c84 | 319 | build_type_inheritance_graph (); |
10d22567 ZD |
320 | if (file) |
321 | fprintf (file, "\nReclaiming functions:"); | |
b2b29377 MM |
322 | if (flag_checking) |
323 | { | |
324 | FOR_EACH_FUNCTION (node) | |
325 | gcc_assert (!node->aux); | |
326 | FOR_EACH_VARIABLE (vnode) | |
327 | gcc_assert (!vnode->aux); | |
328 | } | |
530f3a1b JH |
329 | /* Mark functions whose bodies are obviously needed. |
330 | This is mostly when they can be referenced externally. Inline clones | |
331 | are special since their declarations are shared with master clone and thus | |
332 | cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */ | |
c0c123ef JH |
333 | FOR_EACH_FUNCTION (node) |
334 | { | |
335 | node->used_as_abstract_origin = false; | |
4f4ada6a | 336 | node->indirect_call_target = false; |
67348ccc | 337 | if (node->definition |
c0c123ef | 338 | && !node->global.inlined_to |
67348ccc | 339 | && !node->in_other_partition |
d52f5295 | 340 | && !node->can_remove_if_no_direct_calls_and_refs_p ()) |
c0c123ef JH |
341 | { |
342 | gcc_assert (!node->global.inlined_to); | |
6e2830c3 TS |
343 | reachable.add (node); |
344 | enqueue_node (node, &first, &reachable); | |
c0c123ef JH |
345 | } |
346 | else | |
67348ccc | 347 | gcc_assert (!node->aux); |
c0c123ef | 348 | } |
530f3a1b JH |
349 | |
350 | /* Mark variables that are obviously needed. */ | |
04142cc3 | 351 | FOR_EACH_DEFINED_VARIABLE (vnode) |
9041d2e6 | 352 | if (!vnode->can_remove_if_no_refs_p() |
67348ccc | 353 | && !vnode->in_other_partition) |
04142cc3 | 354 | { |
6e2830c3 TS |
355 | reachable.add (vnode); |
356 | enqueue_node (vnode, &first, &reachable); | |
04142cc3 JH |
357 | } |
358 | ||
359 | /* Perform reachability analysis. */ | |
5e20cdc9 | 360 | while (first != (symtab_node *) (void *) 1) |
b34fd25c | 361 | { |
6e2830c3 | 362 | bool in_boundary_p = !reachable.contains (first); |
5e20cdc9 | 363 | symtab_node *node = first; |
ca31b95f | 364 | |
5e20cdc9 | 365 | first = (symtab_node *)first->aux; |
19fb0b86 | 366 | |
04142cc3 JH |
367 | /* If we are processing symbol in boundary, mark its AUX pointer for |
368 | possible later re-processing in enqueue_node. */ | |
369 | if (in_boundary_p) | |
4bd019b8 JH |
370 | { |
371 | node->aux = (void *)2; | |
372 | if (node->alias && node->analyzed) | |
373 | enqueue_node (node->get_alias_target (), &first, &reachable); | |
374 | } | |
04142cc3 JH |
375 | else |
376 | { | |
31dad809 JJ |
377 | if (TREE_CODE (node->decl) == FUNCTION_DECL |
378 | && DECL_ABSTRACT_ORIGIN (node->decl)) | |
c0c123ef JH |
379 | { |
380 | struct cgraph_node *origin_node | |
4ad08ee8 JH |
381 | = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl)); |
382 | if (origin_node && !origin_node->used_as_abstract_origin) | |
383 | { | |
384 | origin_node->used_as_abstract_origin = true; | |
385 | gcc_assert (!origin_node->prev_sibling_clone); | |
386 | gcc_assert (!origin_node->next_sibling_clone); | |
387 | for (cgraph_node *n = origin_node->clones; n; | |
388 | n = n->next_sibling_clone) | |
389 | if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl)) | |
390 | n->used_as_abstract_origin = true; | |
4ad08ee8 | 391 | } |
c0c123ef | 392 | } |
04142cc3 | 393 | /* If any symbol in a comdat group is reachable, force |
1f26ac87 JM |
394 | all externally visible symbols in the same comdat |
395 | group to be reachable as well. Comdat-local symbols | |
396 | can be discarded if all uses were inlined. */ | |
67348ccc | 397 | if (node->same_comdat_group) |
04142cc3 | 398 | { |
5e20cdc9 | 399 | symtab_node *next; |
67348ccc | 400 | for (next = node->same_comdat_group; |
04142cc3 | 401 | next != node; |
67348ccc | 402 | next = next->same_comdat_group) |
d52f5295 | 403 | if (!next->comdat_local_p () |
6e2830c3 TS |
404 | && !reachable.add (next)) |
405 | enqueue_node (next, &first, &reachable); | |
04142cc3 JH |
406 | } |
407 | /* Mark references as reachable. */ | |
12485662 | 408 | process_references (node, &first, &reachable); |
04142cc3 | 409 | } |
19fb0b86 | 410 | |
7de90a6c | 411 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node)) |
b34fd25c | 412 | { |
04142cc3 JH |
413 | /* Mark the callees reachable unless they are direct calls to extern |
414 | inline functions we decided to not inline. */ | |
415 | if (!in_boundary_p) | |
8a6295ba | 416 | { |
04142cc3 | 417 | struct cgraph_edge *e; |
3462aa02 | 418 | /* Keep alive possible targets for devirtualization. */ |
2bf86c84 JH |
419 | if (opt_for_fn (cnode->decl, optimize) |
420 | && opt_for_fn (cnode->decl, flag_devirtualize)) | |
3462aa02 JH |
421 | { |
422 | struct cgraph_edge *next; | |
423 | for (e = cnode->indirect_calls; e; e = next) | |
424 | { | |
425 | next = e->next_callee; | |
426 | if (e->indirect_info->polymorphic) | |
6e2830c3 | 427 | walk_polymorphic_call_targets (&reachable_call_targets, |
12485662 | 428 | e, &first, &reachable); |
3462aa02 JH |
429 | } |
430 | } | |
04142cc3 | 431 | for (e = cnode->callees; e; e = e->next_callee) |
ed62e0d9 | 432 | { |
17e0fc92 | 433 | symtab_node *body = e->callee->function_symbol (); |
67348ccc DM |
434 | if (e->callee->definition |
435 | && !e->callee->in_other_partition | |
ed62e0d9 | 436 | && (!e->inline_failed |
67348ccc DM |
437 | || !DECL_EXTERNAL (e->callee->decl) |
438 | || e->callee->alias | |
12485662 | 439 | || possible_inline_candidate_p (e->callee))) |
789c2741 JH |
440 | { |
441 | /* Be sure that we will not optimize out alias target | |
442 | body. */ | |
443 | if (DECL_EXTERNAL (e->callee->decl) | |
444 | && e->callee->alias | |
12485662 | 445 | && symtab->state < IPA_SSA_AFTER_INLINING) |
17e0fc92 | 446 | reachable.add (body); |
6e2830c3 | 447 | reachable.add (e->callee); |
789c2741 | 448 | } |
6e2830c3 | 449 | enqueue_node (e->callee, &first, &reachable); |
93a18a70 | 450 | } |
04142cc3 JH |
451 | |
452 | /* When inline clone exists, mark body to be preserved so when removing | |
453 | offline copy of the function we don't kill it. */ | |
4f63dfc6 | 454 | if (cnode->global.inlined_to) |
6e2830c3 | 455 | body_needed_for_clonning.add (cnode->decl); |
b66887e4 | 456 | |
4f63dfc6 JH |
457 | /* For non-inline clones, force their origins to the boundary and ensure |
458 | that body is not removed. */ | |
459 | while (cnode->clone_of) | |
460 | { | |
67348ccc | 461 | bool noninline = cnode->clone_of->decl != cnode->decl; |
4f63dfc6 JH |
462 | cnode = cnode->clone_of; |
463 | if (noninline) | |
464 | { | |
6e2830c3 TS |
465 | body_needed_for_clonning.add (cnode->decl); |
466 | enqueue_node (cnode, &first, &reachable); | |
4f63dfc6 | 467 | } |
b34fd25c | 468 | } |
0136f8f0 AH |
469 | |
470 | } | |
4bd019b8 JH |
471 | else if (cnode->thunk.thunk_p) |
472 | enqueue_node (cnode->callees->callee, &first, &reachable); | |
48de5d37 | 473 | |
0136f8f0 AH |
474 | /* If any reachable function has simd clones, mark them as |
475 | reachable as well. */ | |
476 | if (cnode->simd_clones) | |
477 | { | |
478 | cgraph_node *next; | |
479 | for (next = cnode->simd_clones; | |
480 | next; | |
481 | next = next->simdclone->next_clone) | |
482 | if (in_boundary_p | |
6e2830c3 TS |
483 | || !reachable.add (next)) |
484 | enqueue_node (next, &first, &reachable); | |
47cb0d7d | 485 | } |
b34fd25c | 486 | } |
6649df51 | 487 | /* When we see constructor of external variable, keep referred nodes in the |
5d59b5e1 LC |
488 | boundary. This will also hold initializers of the external vars NODE |
489 | refers to. */ | |
7de90a6c | 490 | varpool_node *vnode = dyn_cast <varpool_node *> (node); |
5d59b5e1 | 491 | if (vnode |
67348ccc DM |
492 | && DECL_EXTERNAL (node->decl) |
493 | && !vnode->alias | |
6649df51 | 494 | && in_boundary_p) |
5d59b5e1 | 495 | { |
d122681a ML |
496 | struct ipa_ref *ref = NULL; |
497 | for (int i = 0; node->iterate_reference (i, ref); i++) | |
6e2830c3 | 498 | enqueue_node (ref->referred, &first, &reachable); |
5d59b5e1 | 499 | } |
ca31b95f JH |
500 | } |
501 | ||
04142cc3 | 502 | /* Remove unreachable functions. */ |
3dafb85c | 503 | for (node = first_function (); node; node = next) |
ca31b95f | 504 | { |
3dafb85c | 505 | next = next_function (node); |
e70670cf JH |
506 | |
507 | /* If node is not needed at all, remove it. */ | |
67348ccc | 508 | if (!node->aux) |
ca31b95f | 509 | { |
10d22567 | 510 | if (file) |
464d0118 | 511 | fprintf (file, " %s", node->dump_name ()); |
d52f5295 | 512 | node->remove (); |
04142cc3 JH |
513 | changed = true; |
514 | } | |
e70670cf | 515 | /* If node is unreachable, remove its body. */ |
6e2830c3 | 516 | else if (!reachable.contains (node)) |
04142cc3 | 517 | { |
d3f2e41e JH |
518 | /* We keep definitions of thunks and aliases in the boundary so |
519 | we can walk to the ultimate alias targets and function symbols | |
520 | reliably. */ | |
521 | if (node->alias || node->thunk.thunk_p) | |
522 | ; | |
523 | else if (!body_needed_for_clonning.contains (node->decl) | |
524 | && !node->alias && !node->thunk.thunk_p) | |
d52f5295 | 525 | node->release_body (); |
4f63dfc6 | 526 | else if (!node->clone_of) |
67348ccc | 527 | gcc_assert (in_lto_p || DECL_RESULT (node->decl)); |
4bd019b8 | 528 | if (node->definition && !node->alias && !node->thunk.thunk_p) |
bb853349 | 529 | { |
04142cc3 | 530 | if (file) |
464d0118 | 531 | fprintf (file, " %s", node->dump_name ()); |
3d8d0043 | 532 | node->body_removed = true; |
67348ccc DM |
533 | node->analyzed = false; |
534 | node->definition = false; | |
535 | node->cpp_implicit_alias = false; | |
536 | node->alias = false; | |
71e54687 | 537 | node->transparent_alias = false; |
d833415c | 538 | node->thunk.thunk_p = false; |
67348ccc | 539 | node->weakref = false; |
8fe91ca8 JH |
540 | /* After early inlining we drop always_inline attributes on |
541 | bodies of functions that are still referenced (have their | |
542 | address taken). */ | |
543 | DECL_ATTRIBUTES (node->decl) | |
544 | = remove_attribute ("always_inline", | |
545 | DECL_ATTRIBUTES (node->decl)); | |
67348ccc | 546 | if (!node->in_other_partition) |
51a5c0c2 | 547 | node->local.local = false; |
d52f5295 | 548 | node->remove_callees (); |
d122681a | 549 | node->remove_all_references (); |
bb853349 | 550 | changed = true; |
d5e254e1 IE |
551 | if (node->thunk.thunk_p |
552 | && node->thunk.add_pointer_bounds_args) | |
553 | { | |
554 | node->thunk.thunk_p = false; | |
555 | node->thunk.add_pointer_bounds_args = false; | |
556 | } | |
bb853349 | 557 | } |
ca31b95f | 558 | } |
4f63dfc6 | 559 | else |
d52f5295 | 560 | gcc_assert (node->clone_of || !node->has_gimple_body_p () |
67348ccc | 561 | || in_lto_p || DECL_RESULT (node->decl)); |
ca31b95f | 562 | } |
04142cc3 JH |
563 | |
564 | /* Inline clones might be kept around so their materializing allows further | |
565 | cloning. If the function the clone is inlined into is removed, we need | |
566 | to turn it into normal cone. */ | |
65c70e6b | 567 | FOR_EACH_FUNCTION (node) |
9187e02d | 568 | { |
9187e02d JH |
569 | if (node->global.inlined_to |
570 | && !node->callers) | |
571 | { | |
572 | gcc_assert (node->clones); | |
d563610d JH |
573 | node->global.inlined_to = NULL; |
574 | update_inlined_to_pointer (node, node); | |
9187e02d | 575 | } |
67348ccc | 576 | node->aux = NULL; |
9187e02d | 577 | } |
4a444e58 | 578 | |
04142cc3 | 579 | /* Remove unreachable variables. */ |
4a444e58 | 580 | if (file) |
04142cc3 | 581 | fprintf (file, "\nReclaiming variables:"); |
3dafb85c | 582 | for (vnode = first_variable (); vnode; vnode = vnext) |
b34fd25c | 583 | { |
3dafb85c | 584 | vnext = next_variable (vnode); |
67348ccc | 585 | if (!vnode->aux |
b9bd2075 JH |
586 | /* For can_refer_decl_in_current_unit_p we want to track for |
587 | all external variables if they are defined in other partition | |
588 | or not. */ | |
67348ccc | 589 | && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl))) |
04142cc3 | 590 | { |
d2b35c04 JH |
591 | struct ipa_ref *ref = NULL; |
592 | ||
593 | /* First remove the aliases, so varpool::remove can possibly lookup | |
594 | the constructor and save it for future use. */ | |
595 | while (vnode->iterate_direct_aliases (0, ref)) | |
596 | { | |
597 | if (file) | |
464d0118 | 598 | fprintf (file, " %s", ref->referred->dump_name ()); |
d2b35c04 JH |
599 | ref->referring->remove (); |
600 | } | |
4a444e58 | 601 | if (file) |
464d0118 | 602 | fprintf (file, " %s", vnode->dump_name ()); |
d2b35c04 | 603 | vnext = next_variable (vnode); |
775669c1 | 604 | /* Signal removal to the debug machinery. */ |
5b42d196 | 605 | if (! flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO) |
775669c1 RB |
606 | { |
607 | vnode->definition = false; | |
608 | (*debug_hooks->late_global_decl) (vnode->decl); | |
609 | } | |
d52f5295 | 610 | vnode->remove (); |
4a444e58 | 611 | changed = true; |
b34fd25c | 612 | } |
4bd019b8 | 613 | else if (!reachable.contains (vnode) && !vnode->alias) |
04142cc3 | 614 | { |
6a6dac52 | 615 | tree init; |
67348ccc | 616 | if (vnode->definition) |
04142cc3 JH |
617 | { |
618 | if (file) | |
fec39fa6 | 619 | fprintf (file, " %s", vnode->name ()); |
04142cc3 JH |
620 | changed = true; |
621 | } | |
1acc5591 | 622 | /* Keep body if it may be useful for constant folding. */ |
5b42d196 | 623 | if ((flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO) |
31db0fe0 | 624 | || ((init = ctor_for_folding (vnode->decl)) == error_mark_node)) |
1acc5591 JH |
625 | vnode->remove_initializer (); |
626 | else | |
627 | DECL_INITIAL (vnode->decl) = init; | |
3d8d0043 | 628 | vnode->body_removed = true; |
67348ccc DM |
629 | vnode->definition = false; |
630 | vnode->analyzed = false; | |
631 | vnode->aux = NULL; | |
e70670cf | 632 | |
d52f5295 | 633 | vnode->remove_from_same_comdat_group (); |
7b3376a0 | 634 | |
d122681a | 635 | vnode->remove_all_references (); |
04142cc3 JH |
636 | } |
637 | else | |
67348ccc | 638 | vnode->aux = NULL; |
b34fd25c | 639 | } |
4a444e58 | 640 | |
04142cc3 | 641 | /* Now update address_taken flags and try to promote functions to be local. */ |
bd3cdcc0 JH |
642 | if (file) |
643 | fprintf (file, "\nClearing address taken flags:"); | |
65c70e6b | 644 | FOR_EACH_DEFINED_FUNCTION (node) |
67348ccc DM |
645 | if (node->address_taken |
646 | && !node->used_from_other_partition) | |
bd3cdcc0 | 647 | { |
1ede94c5 | 648 | if (!node->call_for_symbol_and_aliases |
31db0fe0 | 649 | (has_addr_references_p, NULL, true)) |
bd3cdcc0 JH |
650 | { |
651 | if (file) | |
fec39fa6 | 652 | fprintf (file, " %s", node->name ()); |
67348ccc | 653 | node->address_taken = false; |
4a444e58 | 654 | changed = true; |
4f4ada6a JH |
655 | if (node->local_p () |
656 | /* Virtual functions may be kept in cgraph just because | |
657 | of possible later devirtualization. Do not mark them as | |
658 | local too early so we won't optimize them out before | |
659 | we are done with polymorphic call analysis. */ | |
12485662 | 660 | && (symtab->state >= IPA_SSA_AFTER_INLINING |
4f4ada6a JH |
661 | || !node->call_for_symbol_and_aliases |
662 | (is_indirect_call_target_p, NULL, true))) | |
4a444e58 JH |
663 | { |
664 | node->local.local = true; | |
665 | if (file) | |
666 | fprintf (file, " (local)"); | |
667 | } | |
bd3cdcc0 JH |
668 | } |
669 | } | |
10a5dd5d JH |
670 | if (file) |
671 | fprintf (file, "\n"); | |
b34fd25c | 672 | |
b2b29377 | 673 | symtab_node::checking_verify_symtab_nodes (); |
4537ec0c | 674 | |
a8da72b8 | 675 | /* If we removed something, perhaps profile could be improved. */ |
29f1e2b1 | 676 | if (changed && (optimize || in_lto_p) && ipa_call_summaries) |
a8da72b8 | 677 | FOR_EACH_DEFINED_FUNCTION (node) |
08f835dc | 678 | ipa_propagate_frequency (node); |
a8da72b8 | 679 | |
3462aa02 | 680 | timevar_pop (TV_IPA_UNREACHABLE); |
ca31b95f JH |
681 | return changed; |
682 | } | |
f4b3ca72 | 683 | |
6de88c6a JH |
684 | /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ |
685 | as needed, also clear EXPLICIT_REFS if the references to given variable | |
686 | do not need to be explicit. */ | |
687 | ||
688 | void | |
689 | process_references (varpool_node *vnode, | |
690 | bool *written, bool *address_taken, | |
691 | bool *read, bool *explicit_refs) | |
692 | { | |
693 | int i; | |
694 | struct ipa_ref *ref; | |
695 | ||
9041d2e6 | 696 | if (!vnode->all_refs_explicit_p () |
6de88c6a JH |
697 | || TREE_THIS_VOLATILE (vnode->decl)) |
698 | *explicit_refs = false; | |
699 | ||
d122681a | 700 | for (i = 0; vnode->iterate_referring (i, ref) |
6de88c6a JH |
701 | && *explicit_refs && (!*written || !*address_taken || !*read); i++) |
702 | switch (ref->use) | |
703 | { | |
704 | case IPA_REF_ADDR: | |
705 | *address_taken = true; | |
706 | break; | |
707 | case IPA_REF_LOAD: | |
708 | *read = true; | |
709 | break; | |
710 | case IPA_REF_STORE: | |
711 | *written = true; | |
712 | break; | |
713 | case IPA_REF_ALIAS: | |
d52f5295 ML |
714 | process_references (dyn_cast<varpool_node *> (ref->referring), written, |
715 | address_taken, read, explicit_refs); | |
6de88c6a JH |
716 | break; |
717 | } | |
718 | } | |
719 | ||
720 | /* Set TREE_READONLY bit. */ | |
721 | ||
722 | bool | |
723 | set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED) | |
724 | { | |
725 | TREE_READONLY (vnode->decl) = true; | |
726 | return false; | |
727 | } | |
728 | ||
729 | /* Set writeonly bit and clear the initalizer, since it will not be needed. */ | |
730 | ||
731 | bool | |
dea91a66 | 732 | set_writeonly_bit (varpool_node *vnode, void *data) |
6de88c6a JH |
733 | { |
734 | vnode->writeonly = true; | |
29f1e2b1 | 735 | if (optimize || in_lto_p) |
6de88c6a JH |
736 | { |
737 | DECL_INITIAL (vnode->decl) = NULL; | |
738 | if (!vnode->alias) | |
dea91a66 JH |
739 | { |
740 | if (vnode->num_references ()) | |
741 | *(bool *)data = true; | |
742 | vnode->remove_all_references (); | |
743 | } | |
6de88c6a JH |
744 | } |
745 | return false; | |
746 | } | |
747 | ||
748 | /* Clear addressale bit of VNODE. */ | |
749 | ||
750 | bool | |
751 | clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED) | |
752 | { | |
753 | vnode->address_taken = false; | |
754 | TREE_ADDRESSABLE (vnode->decl) = 0; | |
755 | return false; | |
756 | } | |
757 | ||
2e14744f ML |
758 | /* Discover variables that have no longer address taken, are read-only or |
759 | write-only and update their flags. | |
4a444e58 | 760 | |
2e14744f | 761 | Return true when unreachable symbol removal should be done. |
dea91a66 | 762 | |
4a444e58 JH |
763 | FIXME: This can not be done in between gimplify and omp_expand since |
764 | readonly flag plays role on what is shared and what is not. Currently we do | |
f10ea640 JH |
765 | this transformation as part of whole program visibility and re-do at |
766 | ipa-reference pass (to take into account clonning), but it would | |
767 | make sense to do it before early optimizations. */ | |
4a444e58 | 768 | |
dea91a66 | 769 | bool |
2e14744f | 770 | ipa_discover_variable_flags (void) |
4a444e58 | 771 | { |
2e14744f ML |
772 | if (!flag_ipa_reference_addressable) |
773 | return false; | |
774 | ||
dea91a66 | 775 | bool remove_p = false; |
2c8326a5 | 776 | varpool_node *vnode; |
4a444e58 JH |
777 | if (dump_file) |
778 | fprintf (dump_file, "Clearing variable flags:"); | |
65c70e6b | 779 | FOR_EACH_VARIABLE (vnode) |
6de88c6a | 780 | if (!vnode->alias |
67348ccc | 781 | && (TREE_ADDRESSABLE (vnode->decl) |
6de88c6a | 782 | || !vnode->writeonly |
67348ccc | 783 | || !TREE_READONLY (vnode->decl))) |
4a444e58 JH |
784 | { |
785 | bool written = false; | |
786 | bool address_taken = false; | |
6de88c6a JH |
787 | bool read = false; |
788 | bool explicit_refs = true; | |
789 | ||
dea91a66 JH |
790 | process_references (vnode, &written, &address_taken, &read, |
791 | &explicit_refs); | |
6de88c6a JH |
792 | if (!explicit_refs) |
793 | continue; | |
794 | if (!address_taken) | |
4a444e58 | 795 | { |
6de88c6a | 796 | if (TREE_ADDRESSABLE (vnode->decl) && dump_file) |
d5ce4663 | 797 | fprintf (dump_file, " %s (non-addressable)", vnode->name ()); |
31de7606 JH |
798 | vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL, |
799 | true); | |
4a444e58 | 800 | } |
6de88c6a | 801 | if (!address_taken && !written |
4a444e58 JH |
802 | /* Making variable in explicit section readonly can cause section |
803 | type conflict. | |
804 | See e.g. gcc.c-torture/compile/pr23237.c */ | |
24d047a3 | 805 | && vnode->get_section () == NULL) |
4a444e58 | 806 | { |
6de88c6a | 807 | if (!TREE_READONLY (vnode->decl) && dump_file) |
fec39fa6 | 808 | fprintf (dump_file, " %s (read-only)", vnode->name ()); |
31de7606 | 809 | vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true); |
6de88c6a | 810 | } |
d5ce4663 | 811 | if (!vnode->writeonly && !read && !address_taken && written) |
6de88c6a JH |
812 | { |
813 | if (dump_file) | |
814 | fprintf (dump_file, " %s (write-only)", vnode->name ()); | |
31de7606 JH |
815 | vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p, |
816 | true); | |
4a444e58 JH |
817 | } |
818 | } | |
819 | if (dump_file) | |
820 | fprintf (dump_file, "\n"); | |
dea91a66 | 821 | return remove_p; |
4a444e58 JH |
822 | } |
823 | ||
9e97ff61 | 824 | /* Generate and emit a static constructor or destructor. WHICH must |
31db0fe0 ML |
825 | be one of 'I' (for a constructor), 'D' (for a destructor). |
826 | BODY is a STATEMENT_LIST containing GENERIC | |
d5e254e1 IE |
827 | statements. PRIORITY is the initialization priority for this |
828 | constructor or destructor. | |
9e97ff61 | 829 | |
3a9ed12a JH |
830 | FINAL specify whether the externally visible name for collect2 should |
831 | be produced. */ | |
832 | ||
833 | static void | |
834 | cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final) | |
9e97ff61 JH |
835 | { |
836 | static int counter = 0; | |
837 | char which_buf[16]; | |
838 | tree decl, name, resdecl; | |
839 | ||
840 | /* The priority is encoded in the constructor or destructor name. | |
841 | collect2 will sort the names and arrange that they are called at | |
842 | program startup. */ | |
3a9ed12a JH |
843 | if (final) |
844 | sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++); | |
845 | else | |
846 | /* Proudce sane name but one not recognizable by collect2, just for the | |
847 | case we fail to inline the function. */ | |
848 | sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++); | |
9e97ff61 JH |
849 | name = get_file_function_name (which_buf); |
850 | ||
851 | decl = build_decl (input_location, FUNCTION_DECL, name, | |
852 | build_function_type_list (void_type_node, NULL_TREE)); | |
853 | current_function_decl = decl; | |
854 | ||
855 | resdecl = build_decl (input_location, | |
856 | RESULT_DECL, NULL_TREE, void_type_node); | |
857 | DECL_ARTIFICIAL (resdecl) = 1; | |
858 | DECL_RESULT (decl) = resdecl; | |
859 | DECL_CONTEXT (resdecl) = decl; | |
860 | ||
861 | allocate_struct_function (decl, false); | |
862 | ||
863 | TREE_STATIC (decl) = 1; | |
864 | TREE_USED (decl) = 1; | |
865 | DECL_ARTIFICIAL (decl) = 1; | |
3f2dd8cd | 866 | DECL_IGNORED_P (decl) = 1; |
9e97ff61 JH |
867 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; |
868 | DECL_SAVED_TREE (decl) = body; | |
3a9ed12a | 869 | if (!targetm.have_ctors_dtors && final) |
9e97ff61 JH |
870 | { |
871 | TREE_PUBLIC (decl) = 1; | |
872 | DECL_PRESERVE_P (decl) = 1; | |
873 | } | |
874 | DECL_UNINLINABLE (decl) = 1; | |
875 | ||
876 | DECL_INITIAL (decl) = make_node (BLOCK); | |
01771d43 | 877 | BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl; |
9e97ff61 JH |
878 | TREE_USED (DECL_INITIAL (decl)) = 1; |
879 | ||
880 | DECL_SOURCE_LOCATION (decl) = input_location; | |
881 | cfun->function_end_locus = input_location; | |
882 | ||
883 | switch (which) | |
884 | { | |
885 | case 'I': | |
886 | DECL_STATIC_CONSTRUCTOR (decl) = 1; | |
887 | decl_init_priority_insert (decl, priority); | |
888 | break; | |
889 | case 'D': | |
890 | DECL_STATIC_DESTRUCTOR (decl) = 1; | |
891 | decl_fini_priority_insert (decl, priority); | |
892 | break; | |
893 | default: | |
894 | gcc_unreachable (); | |
895 | } | |
896 | ||
897 | gimplify_function_tree (decl); | |
898 | ||
d52f5295 | 899 | cgraph_node::add_new_function (decl, false); |
9e97ff61 JH |
900 | |
901 | set_cfun (NULL); | |
902 | current_function_decl = NULL; | |
903 | } | |
904 | ||
3a9ed12a | 905 | /* Generate and emit a static constructor or destructor. WHICH must |
31db0fe0 ML |
906 | be one of 'I' (for a constructor) or 'D' (for a destructor). |
907 | BODY is a STATEMENT_LIST containing GENERIC | |
d5e254e1 IE |
908 | statements. PRIORITY is the initialization priority for this |
909 | constructor or destructor. */ | |
3a9ed12a JH |
910 | |
911 | void | |
912 | cgraph_build_static_cdtor (char which, tree body, int priority) | |
913 | { | |
914 | cgraph_build_static_cdtor_1 (which, body, priority, false); | |
915 | } | |
9e97ff61 | 916 | |
9e97ff61 JH |
917 | /* When target does not have ctors and dtors, we call all constructor |
918 | and destructor by special initialization/destruction function | |
919 | recognized by collect2. | |
920 | ||
921 | When we are going to build this function, collect all constructors and | |
922 | destructors and turn them into normal functions. */ | |
923 | ||
924 | static void | |
37a51997 | 925 | record_cdtor_fn (struct cgraph_node *node, vec<tree> *ctors, vec<tree> *dtors) |
9e97ff61 | 926 | { |
67348ccc | 927 | if (DECL_STATIC_CONSTRUCTOR (node->decl)) |
37a51997 | 928 | ctors->safe_push (node->decl); |
67348ccc | 929 | if (DECL_STATIC_DESTRUCTOR (node->decl)) |
37a51997 | 930 | dtors->safe_push (node->decl); |
d52f5295 | 931 | node = cgraph_node::get (node->decl); |
67348ccc | 932 | DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1; |
9e97ff61 JH |
933 | } |
934 | ||
935 | /* Define global constructors/destructor functions for the CDTORS, of | |
936 | which they are LEN. The CDTORS are sorted by initialization | |
937 | priority. If CTOR_P is true, these are constructors; otherwise, | |
938 | they are destructors. */ | |
939 | ||
940 | static void | |
37a51997 | 941 | build_cdtor (bool ctor_p, const vec<tree> &cdtors) |
9e97ff61 JH |
942 | { |
943 | size_t i,j; | |
9771b263 | 944 | size_t len = cdtors.length (); |
9e97ff61 JH |
945 | |
946 | i = 0; | |
947 | while (i < len) | |
948 | { | |
949 | tree body; | |
950 | tree fn; | |
951 | priority_type priority; | |
952 | ||
953 | priority = 0; | |
954 | body = NULL_TREE; | |
955 | j = i; | |
956 | do | |
957 | { | |
958 | priority_type p; | |
9771b263 | 959 | fn = cdtors[j]; |
9e97ff61 JH |
960 | p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn); |
961 | if (j == i) | |
962 | priority = p; | |
963 | else if (p != priority) | |
964 | break; | |
965 | j++; | |
966 | } | |
967 | while (j < len); | |
968 | ||
48c24aca | 969 | /* When there is only one cdtor and target supports them, do nothing. */ |
9e97ff61 JH |
970 | if (j == i + 1 |
971 | && targetm.have_ctors_dtors) | |
972 | { | |
973 | i++; | |
974 | continue; | |
975 | } | |
976 | /* Find the next batch of constructors/destructors with the same | |
977 | initialization priority. */ | |
48c24aca | 978 | for (;i < j; i++) |
9e97ff61 | 979 | { |
9e97ff61 | 980 | tree call; |
9771b263 | 981 | fn = cdtors[i]; |
9e97ff61 JH |
982 | call = build_call_expr (fn, 0); |
983 | if (ctor_p) | |
984 | DECL_STATIC_CONSTRUCTOR (fn) = 0; | |
985 | else | |
986 | DECL_STATIC_DESTRUCTOR (fn) = 0; | |
987 | /* We do not want to optimize away pure/const calls here. | |
988 | When optimizing, these should be already removed, when not | |
989 | optimizing, we want user to be able to breakpoint in them. */ | |
990 | TREE_SIDE_EFFECTS (call) = 1; | |
991 | append_to_statement_list (call, &body); | |
9e97ff61 | 992 | } |
9e97ff61 JH |
993 | gcc_assert (body != NULL_TREE); |
994 | /* Generate a function to call all the function of like | |
995 | priority. */ | |
3a9ed12a | 996 | cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true); |
9e97ff61 JH |
997 | } |
998 | } | |
999 | ||
1000 | /* Comparison function for qsort. P1 and P2 are actually of type | |
1001 | "tree *" and point to static constructors. DECL_INIT_PRIORITY is | |
1002 | used to determine the sort order. */ | |
1003 | ||
1004 | static int | |
1005 | compare_ctor (const void *p1, const void *p2) | |
1006 | { | |
1007 | tree f1; | |
1008 | tree f2; | |
1009 | int priority1; | |
1010 | int priority2; | |
1011 | ||
1012 | f1 = *(const tree *)p1; | |
1013 | f2 = *(const tree *)p2; | |
1014 | priority1 = DECL_INIT_PRIORITY (f1); | |
1015 | priority2 = DECL_INIT_PRIORITY (f2); | |
1016 | ||
1017 | if (priority1 < priority2) | |
1018 | return -1; | |
1019 | else if (priority1 > priority2) | |
1020 | return 1; | |
1021 | else | |
1022 | /* Ensure a stable sort. Constructors are executed in backwarding | |
1023 | order to make LTO initialize braries first. */ | |
1024 | return DECL_UID (f2) - DECL_UID (f1); | |
1025 | } | |
1026 | ||
1027 | /* Comparison function for qsort. P1 and P2 are actually of type | |
1028 | "tree *" and point to static destructors. DECL_FINI_PRIORITY is | |
1029 | used to determine the sort order. */ | |
1030 | ||
1031 | static int | |
1032 | compare_dtor (const void *p1, const void *p2) | |
1033 | { | |
1034 | tree f1; | |
1035 | tree f2; | |
1036 | int priority1; | |
1037 | int priority2; | |
1038 | ||
1039 | f1 = *(const tree *)p1; | |
1040 | f2 = *(const tree *)p2; | |
1041 | priority1 = DECL_FINI_PRIORITY (f1); | |
1042 | priority2 = DECL_FINI_PRIORITY (f2); | |
1043 | ||
1044 | if (priority1 < priority2) | |
1045 | return -1; | |
1046 | else if (priority1 > priority2) | |
1047 | return 1; | |
1048 | else | |
1049 | /* Ensure a stable sort. */ | |
1050 | return DECL_UID (f1) - DECL_UID (f2); | |
1051 | } | |
1052 | ||
1053 | /* Generate functions to call static constructors and destructors | |
1054 | for targets that do not support .ctors/.dtors sections. These | |
1055 | functions have magic names which are detected by collect2. */ | |
1056 | ||
1057 | static void | |
37a51997 | 1058 | build_cdtor_fns (vec<tree> *ctors, vec<tree> *dtors) |
9e97ff61 | 1059 | { |
37a51997 | 1060 | if (!ctors->is_empty ()) |
9e97ff61 JH |
1061 | { |
1062 | gcc_assert (!targetm.have_ctors_dtors || in_lto_p); | |
37a51997 TS |
1063 | ctors->qsort (compare_ctor); |
1064 | build_cdtor (/*ctor_p=*/true, *ctors); | |
9e97ff61 JH |
1065 | } |
1066 | ||
37a51997 | 1067 | if (!dtors->is_empty ()) |
9e97ff61 JH |
1068 | { |
1069 | gcc_assert (!targetm.have_ctors_dtors || in_lto_p); | |
37a51997 TS |
1070 | dtors->qsort (compare_dtor); |
1071 | build_cdtor (/*ctor_p=*/false, *dtors); | |
9e97ff61 JH |
1072 | } |
1073 | } | |
1074 | ||
1075 | /* Look for constructors and destructors and produce function calling them. | |
1076 | This is needed for targets not supporting ctors or dtors, but we perform the | |
073a8998 | 1077 | transformation also at linktime to merge possibly numerous |
9e97ff61 JH |
1078 | constructors/destructors into single function to improve code locality and |
1079 | reduce size. */ | |
1080 | ||
1081 | static unsigned int | |
1082 | ipa_cdtor_merge (void) | |
1083 | { | |
37a51997 TS |
1084 | /* A vector of FUNCTION_DECLs declared as static constructors. */ |
1085 | auto_vec<tree, 20> ctors; | |
1086 | /* A vector of FUNCTION_DECLs declared as static destructors. */ | |
1087 | auto_vec<tree, 20> dtors; | |
9e97ff61 | 1088 | struct cgraph_node *node; |
65c70e6b | 1089 | FOR_EACH_DEFINED_FUNCTION (node) |
67348ccc DM |
1090 | if (DECL_STATIC_CONSTRUCTOR (node->decl) |
1091 | || DECL_STATIC_DESTRUCTOR (node->decl)) | |
37a51997 TS |
1092 | record_cdtor_fn (node, &ctors, &dtors); |
1093 | build_cdtor_fns (&ctors, &dtors); | |
9e97ff61 JH |
1094 | return 0; |
1095 | } | |
1096 | ||
17795822 TS |
1097 | namespace { |
1098 | ||
1099 | const pass_data pass_data_ipa_cdtor_merge = | |
9e97ff61 | 1100 | { |
27a4cd48 DM |
1101 | IPA_PASS, /* type */ |
1102 | "cdtor", /* name */ | |
1103 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
1104 | TV_CGRAPHOPT, /* tv_id */ |
1105 | 0, /* properties_required */ | |
1106 | 0, /* properties_provided */ | |
1107 | 0, /* properties_destroyed */ | |
1108 | 0, /* todo_flags_start */ | |
1109 | 0, /* todo_flags_finish */ | |
9e97ff61 | 1110 | }; |
27a4cd48 | 1111 | |
17795822 | 1112 | class pass_ipa_cdtor_merge : public ipa_opt_pass_d |
27a4cd48 DM |
1113 | { |
1114 | public: | |
c3284718 RS |
1115 | pass_ipa_cdtor_merge (gcc::context *ctxt) |
1116 | : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt, | |
1117 | NULL, /* generate_summary */ | |
1118 | NULL, /* write_summary */ | |
1119 | NULL, /* read_summary */ | |
1120 | NULL, /* write_optimization_summary */ | |
1121 | NULL, /* read_optimization_summary */ | |
1122 | NULL, /* stmt_fixup */ | |
1123 | 0, /* function_transform_todo_flags_start */ | |
1124 | NULL, /* function_transform */ | |
1125 | NULL) /* variable_transform */ | |
27a4cd48 DM |
1126 | {} |
1127 | ||
1128 | /* opt_pass methods: */ | |
1a3d085c | 1129 | virtual bool gate (function *); |
be55bfe6 | 1130 | virtual unsigned int execute (function *) { return ipa_cdtor_merge (); } |
27a4cd48 DM |
1131 | |
1132 | }; // class pass_ipa_cdtor_merge | |
1133 | ||
1a3d085c TS |
1134 | bool |
1135 | pass_ipa_cdtor_merge::gate (function *) | |
1136 | { | |
1137 | /* Perform the pass when we have no ctors/dtors support | |
1138 | or at LTO time to merge multiple constructors into single | |
1139 | function. */ | |
29f1e2b1 | 1140 | return !targetm.have_ctors_dtors || in_lto_p; |
1a3d085c TS |
1141 | } |
1142 | ||
17795822 TS |
1143 | } // anon namespace |
1144 | ||
27a4cd48 DM |
1145 | ipa_opt_pass_d * |
1146 | make_pass_ipa_cdtor_merge (gcc::context *ctxt) | |
1147 | { | |
1148 | return new pass_ipa_cdtor_merge (ctxt); | |
1149 | } | |
eb6a09a7 JH |
1150 | |
1151 | /* Invalid pointer representing BOTTOM for single user dataflow. */ | |
1152 | #define BOTTOM ((cgraph_node *)(size_t) 2) | |
1153 | ||
1154 | /* Meet operation for single user dataflow. | |
1155 | Here we want to associate variables with sigle function that may access it. | |
1156 | ||
1157 | FUNCTION is current single user of a variable, VAR is variable that uses it. | |
1158 | Latttice is stored in SINGLE_USER_MAP. | |
1159 | ||
1160 | We represent: | |
1161 | - TOP by no entry in SIGNLE_USER_MAP | |
1162 | - BOTTOM by BOTTOM in AUX pointer (to save lookups) | |
1163 | - known single user by cgraph pointer in SINGLE_USER_MAP. */ | |
1164 | ||
1165 | cgraph_node * | |
1166 | meet (cgraph_node *function, varpool_node *var, | |
1eb68d2d | 1167 | hash_map<varpool_node *, cgraph_node *> &single_user_map) |
eb6a09a7 JH |
1168 | { |
1169 | struct cgraph_node *user, **f; | |
1170 | ||
1171 | if (var->aux == BOTTOM) | |
1172 | return BOTTOM; | |
1173 | ||
1eb68d2d | 1174 | f = single_user_map.get (var); |
eb6a09a7 JH |
1175 | if (!f) |
1176 | return function; | |
1177 | user = *f; | |
1178 | if (!function) | |
1179 | return user; | |
1180 | else if (function != user) | |
1181 | return BOTTOM; | |
1182 | else | |
1183 | return function; | |
1184 | } | |
1185 | ||
1186 | /* Propagation step of single-use dataflow. | |
1187 | ||
1188 | Check all uses of VNODE and see if they are used by single function FUNCTION. | |
1189 | SINGLE_USER_MAP represents the dataflow lattice. */ | |
1190 | ||
1191 | cgraph_node * | |
1192 | propagate_single_user (varpool_node *vnode, cgraph_node *function, | |
1eb68d2d | 1193 | hash_map<varpool_node *, cgraph_node *> &single_user_map) |
eb6a09a7 JH |
1194 | { |
1195 | int i; | |
1196 | struct ipa_ref *ref; | |
1197 | ||
1198 | gcc_assert (!vnode->externally_visible); | |
1199 | ||
1200 | /* If node is an alias, first meet with its target. */ | |
1201 | if (vnode->alias) | |
9041d2e6 | 1202 | function = meet (function, vnode->get_alias_target (), single_user_map); |
eb6a09a7 JH |
1203 | |
1204 | /* Check all users and see if they correspond to a single function. */ | |
d52f5295 | 1205 | for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++) |
eb6a09a7 JH |
1206 | { |
1207 | struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring); | |
1208 | if (cnode) | |
1209 | { | |
1210 | if (cnode->global.inlined_to) | |
1211 | cnode = cnode->global.inlined_to; | |
1212 | if (!function) | |
1213 | function = cnode; | |
1214 | else if (function != cnode) | |
1215 | function = BOTTOM; | |
1216 | } | |
1217 | else | |
17e0fc92 JH |
1218 | function = meet (function, dyn_cast <varpool_node *> (ref->referring), |
1219 | single_user_map); | |
eb6a09a7 JH |
1220 | } |
1221 | return function; | |
1222 | } | |
1223 | ||
1224 | /* Pass setting used_by_single_function flag. | |
17e0fc92 JH |
1225 | This flag is set on variable when there is only one function that may |
1226 | possibly referr to it. */ | |
eb6a09a7 JH |
1227 | |
1228 | static unsigned int | |
1229 | ipa_single_use (void) | |
1230 | { | |
1231 | varpool_node *first = (varpool_node *) (void *) 1; | |
1232 | varpool_node *var; | |
1eb68d2d | 1233 | hash_map<varpool_node *, cgraph_node *> single_user_map; |
eb6a09a7 JH |
1234 | |
1235 | FOR_EACH_DEFINED_VARIABLE (var) | |
9041d2e6 | 1236 | if (!var->all_refs_explicit_p ()) |
eb6a09a7 JH |
1237 | var->aux = BOTTOM; |
1238 | else | |
1239 | { | |
1240 | /* Enqueue symbol for dataflow. */ | |
1241 | var->aux = first; | |
1242 | first = var; | |
1243 | } | |
1244 | ||
1245 | /* The actual dataflow. */ | |
1246 | ||
1247 | while (first != (void *) 1) | |
1248 | { | |
1249 | cgraph_node *user, *orig_user, **f; | |
1250 | ||
1251 | var = first; | |
1252 | first = (varpool_node *)first->aux; | |
1253 | ||
1eb68d2d | 1254 | f = single_user_map.get (var); |
eb6a09a7 JH |
1255 | if (f) |
1256 | orig_user = *f; | |
1257 | else | |
1258 | orig_user = NULL; | |
1259 | user = propagate_single_user (var, orig_user, single_user_map); | |
1260 | ||
1261 | gcc_checking_assert (var->aux != BOTTOM); | |
1262 | ||
1263 | /* If user differs, enqueue all references. */ | |
1264 | if (user != orig_user) | |
1265 | { | |
1266 | unsigned int i; | |
1267 | ipa_ref *ref; | |
1268 | ||
1eb68d2d | 1269 | single_user_map.put (var, user); |
eb6a09a7 JH |
1270 | |
1271 | /* Enqueue all aliases for re-processing. */ | |
31de7606 JH |
1272 | for (i = 0; var->iterate_direct_aliases (i, ref); i++) |
1273 | if (!ref->referring->aux) | |
eb6a09a7 JH |
1274 | { |
1275 | ref->referring->aux = first; | |
1276 | first = dyn_cast <varpool_node *> (ref->referring); | |
1277 | } | |
1278 | /* Enqueue all users for re-processing. */ | |
d52f5295 | 1279 | for (i = 0; var->iterate_reference (i, ref); i++) |
eb6a09a7 JH |
1280 | if (!ref->referred->aux |
1281 | && ref->referred->definition | |
1282 | && is_a <varpool_node *> (ref->referred)) | |
1283 | { | |
1284 | ref->referred->aux = first; | |
1285 | first = dyn_cast <varpool_node *> (ref->referred); | |
1286 | } | |
1287 | ||
1288 | /* If user is BOTTOM, just punt on this var. */ | |
1289 | if (user == BOTTOM) | |
1290 | var->aux = BOTTOM; | |
1291 | else | |
1292 | var->aux = NULL; | |
1293 | } | |
1294 | else | |
1295 | var->aux = NULL; | |
1296 | } | |
1297 | ||
1298 | FOR_EACH_DEFINED_VARIABLE (var) | |
1299 | { | |
1300 | if (var->aux != BOTTOM) | |
1301 | { | |
17e0fc92 JH |
1302 | /* Not having the single user known means that the VAR is |
1303 | unreachable. Either someone forgot to remove unreachable | |
1304 | variables or the reachability here is wrong. */ | |
1305 | ||
b2b29377 MM |
1306 | gcc_checking_assert (single_user_map.get (var)); |
1307 | ||
eb6a09a7 JH |
1308 | if (dump_file) |
1309 | { | |
464d0118 ML |
1310 | fprintf (dump_file, "Variable %s is used by single function\n", |
1311 | var->dump_name ()); | |
eb6a09a7 JH |
1312 | } |
1313 | var->used_by_single_function = true; | |
1314 | } | |
1315 | var->aux = NULL; | |
1316 | } | |
1317 | return 0; | |
1318 | } | |
1319 | ||
17795822 TS |
1320 | namespace { |
1321 | ||
1322 | const pass_data pass_data_ipa_single_use = | |
eb6a09a7 JH |
1323 | { |
1324 | IPA_PASS, /* type */ | |
1325 | "single-use", /* name */ | |
1326 | OPTGROUP_NONE, /* optinfo_flags */ | |
eb6a09a7 JH |
1327 | TV_CGRAPHOPT, /* tv_id */ |
1328 | 0, /* properties_required */ | |
1329 | 0, /* properties_provided */ | |
1330 | 0, /* properties_destroyed */ | |
1331 | 0, /* todo_flags_start */ | |
1332 | 0, /* todo_flags_finish */ | |
1333 | }; | |
1334 | ||
17795822 | 1335 | class pass_ipa_single_use : public ipa_opt_pass_d |
eb6a09a7 JH |
1336 | { |
1337 | public: | |
1338 | pass_ipa_single_use (gcc::context *ctxt) | |
1339 | : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt, | |
1340 | NULL, /* generate_summary */ | |
1341 | NULL, /* write_summary */ | |
1342 | NULL, /* read_summary */ | |
1343 | NULL, /* write_optimization_summary */ | |
1344 | NULL, /* read_optimization_summary */ | |
1345 | NULL, /* stmt_fixup */ | |
1346 | 0, /* function_transform_todo_flags_start */ | |
1347 | NULL, /* function_transform */ | |
1348 | NULL) /* variable_transform */ | |
1349 | {} | |
1350 | ||
1351 | /* opt_pass methods: */ | |
eb6a09a7 JH |
1352 | virtual unsigned int execute (function *) { return ipa_single_use (); } |
1353 | ||
1354 | }; // class pass_ipa_single_use | |
1355 | ||
17795822 TS |
1356 | } // anon namespace |
1357 | ||
eb6a09a7 JH |
1358 | ipa_opt_pass_d * |
1359 | make_pass_ipa_single_use (gcc::context *ctxt) | |
1360 | { | |
1361 | return new pass_ipa_single_use (ctxt); | |
1362 | } | |
f0251020 RB |
1363 | |
1364 | /* Materialize all clones. */ | |
1365 | ||
1366 | namespace { | |
1367 | ||
1368 | const pass_data pass_data_materialize_all_clones = | |
1369 | { | |
1370 | SIMPLE_IPA_PASS, /* type */ | |
1371 | "materialize-all-clones", /* name */ | |
1372 | OPTGROUP_NONE, /* optinfo_flags */ | |
1373 | TV_IPA_OPT, /* tv_id */ | |
1374 | 0, /* properties_required */ | |
1375 | 0, /* properties_provided */ | |
1376 | 0, /* properties_destroyed */ | |
1377 | 0, /* todo_flags_start */ | |
1378 | 0, /* todo_flags_finish */ | |
1379 | }; | |
1380 | ||
1381 | class pass_materialize_all_clones : public simple_ipa_opt_pass | |
1382 | { | |
1383 | public: | |
1384 | pass_materialize_all_clones (gcc::context *ctxt) | |
1385 | : simple_ipa_opt_pass (pass_data_materialize_all_clones, ctxt) | |
1386 | {} | |
1387 | ||
1388 | /* opt_pass methods: */ | |
1389 | virtual unsigned int execute (function *) | |
1390 | { | |
1391 | symtab->materialize_all_clones (); | |
1392 | return 0; | |
1393 | } | |
1394 | ||
1395 | }; // class pass_materialize_all_clones | |
1396 | ||
1397 | } // anon namespace | |
1398 | ||
1399 | simple_ipa_opt_pass * | |
1400 | make_pass_materialize_all_clones (gcc::context *ctxt) | |
1401 | { | |
1402 | return new pass_materialize_all_clones (ctxt); | |
1403 | } |