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