]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa.c
re PR c++/41090 (Using static label reference in c++ class constructor produces wrong...
[thirdparty/gcc.git] / gcc / ipa.c
CommitLineData
ca31b95f 1/* Basic IPA optimizations and utilities.
d1e082c2 2 Copyright (C) 2003-2013 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);
856a1588 226 if (!inline_summary_vec && edge->call_stmt)
3462aa02 227 cgraph_redirect_edge_call_stmt_to_callee (edge);
856a1588 228 else
7b395ddd 229 inline_update_overall_summary (node);
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 {
67348ccc 357 if (DECL_ABSTRACT_ORIGIN (node->decl))
c0c123ef
JH
358 {
359 struct cgraph_node *origin_node
6f99e449 360 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
c0c123ef 361 origin_node->used_as_abstract_origin = true;
67348ccc 362 enqueue_node (origin_node, &first, reachable);
c0c123ef 363 }
04142cc3 364 /* If any symbol in a comdat group is reachable, force
1f26ac87
JM
365 all externally visible symbols in the same comdat
366 group to be reachable as well. Comdat-local symbols
367 can be discarded if all uses were inlined. */
67348ccc 368 if (node->same_comdat_group)
04142cc3 369 {
5e20cdc9 370 symtab_node *next;
67348ccc 371 for (next = node->same_comdat_group;
04142cc3 372 next != node;
67348ccc 373 next = next->same_comdat_group)
1f26ac87
JM
374 if (!symtab_comdat_local_p (next)
375 && !pointer_set_insert (reachable, next))
67348ccc 376 enqueue_node (next, &first, reachable);
04142cc3
JH
377 }
378 /* Mark references as reachable. */
67348ccc 379 process_references (&node->ref_list, &first,
04142cc3
JH
380 before_inlining_p, reachable);
381 }
19fb0b86 382
5d59b5e1 383 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
b34fd25c 384 {
04142cc3
JH
385 /* Mark the callees reachable unless they are direct calls to extern
386 inline functions we decided to not inline. */
387 if (!in_boundary_p)
8a6295ba 388 {
04142cc3 389 struct cgraph_edge *e;
3462aa02
JH
390 /* Keep alive possible targets for devirtualization. */
391 if (optimize && flag_devirtualize)
392 {
393 struct cgraph_edge *next;
394 for (e = cnode->indirect_calls; e; e = next)
395 {
396 next = e->next_callee;
397 if (e->indirect_info->polymorphic)
398 walk_polymorphic_call_targets (reachable_call_targets,
399 e, &first, reachable,
400 before_inlining_p);
401 }
402 }
04142cc3 403 for (e = cnode->callees; e; e = e->next_callee)
ed62e0d9 404 {
67348ccc
DM
405 if (e->callee->definition
406 && !e->callee->in_other_partition
ed62e0d9 407 && (!e->inline_failed
67348ccc
DM
408 || !DECL_EXTERNAL (e->callee->decl)
409 || e->callee->alias
ed62e0d9 410 || before_inlining_p))
93a18a70 411 pointer_set_insert (reachable, e->callee);
67348ccc 412 enqueue_node (e->callee, &first, reachable);
93a18a70 413 }
04142cc3
JH
414
415 /* When inline clone exists, mark body to be preserved so when removing
416 offline copy of the function we don't kill it. */
4f63dfc6 417 if (cnode->global.inlined_to)
67348ccc 418 pointer_set_insert (body_needed_for_clonning, cnode->decl);
b66887e4 419
4f63dfc6
JH
420 /* For non-inline clones, force their origins to the boundary and ensure
421 that body is not removed. */
422 while (cnode->clone_of)
423 {
67348ccc 424 bool noninline = cnode->clone_of->decl != cnode->decl;
4f63dfc6
JH
425 cnode = cnode->clone_of;
426 if (noninline)
427 {
67348ccc
DM
428 pointer_set_insert (body_needed_for_clonning, cnode->decl);
429 enqueue_node (cnode, &first, reachable);
4f63dfc6 430 }
b34fd25c 431 }
0136f8f0
AH
432
433 }
434 /* If any reachable function has simd clones, mark them as
435 reachable as well. */
436 if (cnode->simd_clones)
437 {
438 cgraph_node *next;
439 for (next = cnode->simd_clones;
440 next;
441 next = next->simdclone->next_clone)
442 if (in_boundary_p
443 || !pointer_set_insert (reachable, next))
444 enqueue_node (next, &first, reachable);
47cb0d7d 445 }
b34fd25c 446 }
6649df51 447 /* When we see constructor of external variable, keep referred nodes in the
5d59b5e1
LC
448 boundary. This will also hold initializers of the external vars NODE
449 refers to. */
450 varpool_node *vnode = dyn_cast <varpool_node> (node);
451 if (vnode
67348ccc
DM
452 && DECL_EXTERNAL (node->decl)
453 && !vnode->alias
6649df51 454 && in_boundary_p)
5d59b5e1 455 {
6649df51 456 struct ipa_ref *ref;
67348ccc 457 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
6649df51 458 enqueue_node (ref->referred, &first, reachable);
5d59b5e1 459 }
ca31b95f
JH
460 }
461
04142cc3 462 /* Remove unreachable functions. */
2aae7680 463 for (node = cgraph_first_function (); node; node = next)
ca31b95f 464 {
2aae7680 465 next = cgraph_next_function (node);
e70670cf
JH
466
467 /* If node is not needed at all, remove it. */
67348ccc 468 if (!node->aux)
ca31b95f 469 {
10d22567 470 if (file)
fec39fa6 471 fprintf (file, " %s", node->name ());
04142cc3
JH
472 cgraph_remove_node (node);
473 changed = true;
474 }
e70670cf 475 /* If node is unreachable, remove its body. */
04142cc3
JH
476 else if (!pointer_set_contains (reachable, node))
477 {
67348ccc 478 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
e70670cf 479 cgraph_release_function_body (node);
4f63dfc6 480 else if (!node->clone_of)
67348ccc
DM
481 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
482 if (node->definition)
bb853349 483 {
04142cc3 484 if (file)
fec39fa6 485 fprintf (file, " %s", node->name ());
67348ccc
DM
486 node->analyzed = false;
487 node->definition = false;
488 node->cpp_implicit_alias = false;
489 node->alias = false;
490 node->weakref = false;
491 if (!node->in_other_partition)
51a5c0c2
JH
492 node->local.local = false;
493 cgraph_node_remove_callees (node);
67348ccc 494 ipa_remove_all_references (&node->ref_list);
bb853349
JH
495 changed = true;
496 }
ca31b95f 497 }
4f63dfc6
JH
498 else
499 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
67348ccc 500 || in_lto_p || DECL_RESULT (node->decl));
ca31b95f 501 }
04142cc3
JH
502
503 /* Inline clones might be kept around so their materializing allows further
504 cloning. If the function the clone is inlined into is removed, we need
505 to turn it into normal cone. */
65c70e6b 506 FOR_EACH_FUNCTION (node)
9187e02d 507 {
9187e02d
JH
508 if (node->global.inlined_to
509 && !node->callers)
510 {
511 gcc_assert (node->clones);
d563610d
JH
512 node->global.inlined_to = NULL;
513 update_inlined_to_pointer (node, node);
9187e02d 514 }
67348ccc 515 node->aux = NULL;
9187e02d 516 }
4a444e58 517
04142cc3 518 /* Remove unreachable variables. */
4a444e58 519 if (file)
04142cc3 520 fprintf (file, "\nReclaiming variables:");
2aae7680 521 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
b34fd25c 522 {
2aae7680 523 vnext = varpool_next_variable (vnode);
67348ccc 524 if (!vnode->aux
b9bd2075
JH
525 /* For can_refer_decl_in_current_unit_p we want to track for
526 all external variables if they are defined in other partition
527 or not. */
67348ccc 528 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
04142cc3 529 {
4a444e58 530 if (file)
fec39fa6 531 fprintf (file, " %s", vnode->name ());
4a444e58
JH
532 varpool_remove_node (vnode);
533 changed = true;
b34fd25c 534 }
04142cc3
JH
535 else if (!pointer_set_contains (reachable, vnode))
536 {
6a6dac52 537 tree init;
67348ccc 538 if (vnode->definition)
04142cc3
JH
539 {
540 if (file)
fec39fa6 541 fprintf (file, " %s", vnode->name ());
04142cc3
JH
542 changed = true;
543 }
67348ccc
DM
544 vnode->definition = false;
545 vnode->analyzed = false;
546 vnode->aux = NULL;
e70670cf
JH
547
548 /* Keep body if it may be useful for constant folding. */
67348ccc 549 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
e70670cf 550 varpool_remove_initializer (vnode);
6a6dac52 551 else
67348ccc
DM
552 DECL_INITIAL (vnode->decl) = init;
553 ipa_remove_all_references (&vnode->ref_list);
04142cc3
JH
554 }
555 else
67348ccc 556 vnode->aux = NULL;
b34fd25c 557 }
4a444e58 558
04142cc3
JH
559 pointer_set_destroy (reachable);
560 pointer_set_destroy (body_needed_for_clonning);
3462aa02 561 pointer_set_destroy (reachable_call_targets);
4a444e58 562
04142cc3 563 /* Now update address_taken flags and try to promote functions to be local. */
bd3cdcc0
JH
564 if (file)
565 fprintf (file, "\nClearing address taken flags:");
65c70e6b 566 FOR_EACH_DEFINED_FUNCTION (node)
67348ccc
DM
567 if (node->address_taken
568 && !node->used_from_other_partition)
bd3cdcc0 569 {
41817394 570 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
bd3cdcc0
JH
571 {
572 if (file)
fec39fa6 573 fprintf (file, " %s", node->name ());
67348ccc 574 node->address_taken = false;
4a444e58
JH
575 changed = true;
576 if (cgraph_local_node_p (node))
577 {
578 node->local.local = true;
579 if (file)
580 fprintf (file, " (local)");
581 }
bd3cdcc0
JH
582 }
583 }
10a5dd5d
JH
584 if (file)
585 fprintf (file, "\n");
b34fd25c 586
873aa8f5 587#ifdef ENABLE_CHECKING
474ffc72 588 verify_symtab ();
873aa8f5 589#endif
4537ec0c 590
a8da72b8 591 /* If we removed something, perhaps profile could be improved. */
9771b263 592 if (changed && optimize && inline_edge_summary_vec.exists ())
a8da72b8 593 FOR_EACH_DEFINED_FUNCTION (node)
08f835dc 594 ipa_propagate_frequency (node);
a8da72b8 595
3462aa02 596 timevar_pop (TV_IPA_UNREACHABLE);
ca31b95f
JH
597 return changed;
598}
f4b3ca72 599
4a444e58
JH
600/* Discover variables that have no longer address taken or that are read only
601 and update their flags.
602
603 FIXME: This can not be done in between gimplify and omp_expand since
604 readonly flag plays role on what is shared and what is not. Currently we do
f10ea640
JH
605 this transformation as part of whole program visibility and re-do at
606 ipa-reference pass (to take into account clonning), but it would
607 make sense to do it before early optimizations. */
4a444e58
JH
608
609void
610ipa_discover_readonly_nonaddressable_vars (void)
611{
2c8326a5 612 varpool_node *vnode;
4a444e58
JH
613 if (dump_file)
614 fprintf (dump_file, "Clearing variable flags:");
65c70e6b 615 FOR_EACH_VARIABLE (vnode)
67348ccc
DM
616 if (vnode->definition && varpool_all_refs_explicit_p (vnode)
617 && (TREE_ADDRESSABLE (vnode->decl)
618 || !TREE_READONLY (vnode->decl)))
4a444e58
JH
619 {
620 bool written = false;
621 bool address_taken = false;
622 int i;
623 struct ipa_ref *ref;
67348ccc 624 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
960bfb69 625 i, ref)
4a444e58
JH
626 && (!written || !address_taken); i++)
627 switch (ref->use)
628 {
629 case IPA_REF_ADDR:
630 address_taken = true;
631 break;
632 case IPA_REF_LOAD:
633 break;
634 case IPA_REF_STORE:
635 written = true;
636 break;
637 }
67348ccc 638 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
4a444e58
JH
639 {
640 if (dump_file)
fec39fa6 641 fprintf (dump_file, " %s (addressable)", vnode->name ());
67348ccc 642 TREE_ADDRESSABLE (vnode->decl) = 0;
4a444e58 643 }
67348ccc 644 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
4a444e58
JH
645 /* Making variable in explicit section readonly can cause section
646 type conflict.
647 See e.g. gcc.c-torture/compile/pr23237.c */
67348ccc 648 && DECL_SECTION_NAME (vnode->decl) == NULL)
4a444e58
JH
649 {
650 if (dump_file)
fec39fa6 651 fprintf (dump_file, " %s (read-only)", vnode->name ());
67348ccc 652 TREE_READONLY (vnode->decl) = 1;
4a444e58
JH
653 }
654 }
655 if (dump_file)
656 fprintf (dump_file, "\n");
657}
658
430c6ceb
JH
659/* Return true when there is a reference to node and it is not vtable. */
660static bool
5e20cdc9 661address_taken_from_non_vtable_p (symtab_node *node)
430c6ceb
JH
662{
663 int i;
664 struct ipa_ref *ref;
67348ccc 665 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
960bfb69 666 i, ref); i++)
d2640c43
JH
667 if (ref->use == IPA_REF_ADDR)
668 {
2c8326a5 669 varpool_node *node;
5d59b5e1 670 if (is_a <cgraph_node> (ref->referring))
d2640c43 671 return true;
5932a4d4 672 node = ipa_ref_referring_varpool_node (ref);
67348ccc 673 if (!DECL_VIRTUAL_P (node->decl))
d2640c43
JH
674 return true;
675 }
430c6ceb
JH
676 return false;
677}
678
edb983b2
JH
679/* A helper for comdat_can_be_unshared_p. */
680
681static bool
5e20cdc9 682comdat_can_be_unshared_p_1 (symtab_node *node)
edb983b2
JH
683{
684 /* When address is taken, we don't know if equality comparison won't
0987ffe7
JH
685 break eventually. Exception are virutal functions, C++
686 constructors/destructors and vtables, where this is not possible by
687 language standard. */
67348ccc
DM
688 if (!DECL_VIRTUAL_P (node->decl)
689 && (TREE_CODE (node->decl) != FUNCTION_DECL
690 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
691 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
edb983b2
JH
692 && address_taken_from_non_vtable_p (node))
693 return false;
694
695 /* If the symbol is used in some weird way, better to not touch it. */
67348ccc 696 if (node->force_output)
edb983b2
JH
697 return false;
698
699 /* Explicit instantiations needs to be output when possibly
700 used externally. */
67348ccc
DM
701 if (node->forced_by_abi
702 && TREE_PUBLIC (node->decl)
703 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
edb983b2
JH
704 && !flag_whole_program))
705 return false;
706
707 /* Non-readonly and volatile variables can not be duplicated. */
708 if (is_a <varpool_node> (node)
67348ccc
DM
709 && (!TREE_READONLY (node->decl)
710 || TREE_THIS_VOLATILE (node->decl)))
edb983b2
JH
711 return false;
712 return true;
713}
714
430c6ceb
JH
715/* COMDAT functions must be shared only if they have address taken,
716 otherwise we can produce our own private implementation with
717 -fwhole-program.
718 Return true when turning COMDAT functoin static can not lead to wrong
719 code when the resulting object links with a library defining same COMDAT.
720
721 Virtual functions do have their addresses taken from the vtables,
722 but in C++ there is no way to compare their addresses for equality. */
723
e70670cf 724static bool
5e20cdc9 725comdat_can_be_unshared_p (symtab_node *node)
430c6ceb 726{
edb983b2 727 if (!comdat_can_be_unshared_p_1 (node))
430c6ceb 728 return false;
67348ccc 729 if (node->same_comdat_group)
430c6ceb 730 {
5e20cdc9 731 symtab_node *next;
430c6ceb
JH
732
733 /* If more than one function is in the same COMDAT group, it must
734 be shared even if just one function in the comdat group has
735 address taken. */
67348ccc
DM
736 for (next = node->same_comdat_group;
737 next != node; next = next->same_comdat_group)
edb983b2
JH
738 if (!comdat_can_be_unshared_p_1 (next))
739 return false;
430c6ceb
JH
740 }
741 return true;
742}
743
4a444e58
JH
744/* Return true when function NODE should be considered externally visible. */
745
b20996ff 746static bool
39e2db00 747cgraph_externally_visible_p (struct cgraph_node *node,
5ac42672 748 bool whole_program)
b20996ff 749{
67348ccc 750 if (!node->definition)
b820a2f9 751 return false;
67348ccc
DM
752 if (!TREE_PUBLIC (node->decl)
753 || DECL_EXTERNAL (node->decl))
b20996ff 754 return false;
5dde3b01 755
bf243ea7
JH
756 /* Do not try to localize built-in functions yet. One of problems is that we
757 end up mangling their asm for WHOPR that makes it impossible to call them
758 using the implicit built-in declarations anymore. Similarly this enables
759 us to remove them as unreachable before actual calls may appear during
760 expansion or folding. */
67348ccc 761 if (DECL_BUILT_IN (node->decl))
bf243ea7
JH
762 return true;
763
0e9ea52b 764 /* If linker counts on us, we must preserve the function. */
67348ccc 765 if (symtab_used_from_object_file_p (node))
0e9ea52b 766 return true;
67348ccc 767 if (DECL_PRESERVE_P (node->decl))
93a3eea4 768 return true;
960bfb69 769 if (lookup_attribute ("externally_visible",
67348ccc 770 DECL_ATTRIBUTES (node->decl)))
93a3eea4 771 return true;
9d602c59 772 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
960bfb69 773 && lookup_attribute ("dllexport",
67348ccc 774 DECL_ATTRIBUTES (node->decl)))
9d602c59 775 return true;
67348ccc 776 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
c3e3f090 777 return false;
430c6ceb
JH
778 /* When doing LTO or whole program, we can bring COMDAT functoins static.
779 This improves code quality and we know we will duplicate them at most twice
780 (in the case that we are not using plugin and link with object file
781 implementing same COMDAT) */
782 if ((in_lto_p || whole_program)
67348ccc
DM
783 && DECL_COMDAT (node->decl)
784 && comdat_can_be_unshared_p (node))
430c6ceb
JH
785 return false;
786
5dde3b01 787 /* When doing link time optimizations, hidden symbols become local. */
0e9ea52b 788 if (in_lto_p
67348ccc
DM
789 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
790 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
99fecd47
JH
791 /* Be sure that node is defined in IR file, not in other object
792 file. In that case we don't set used_from_other_object_file. */
67348ccc 793 && node->definition)
5dde3b01
JH
794 ;
795 else if (!whole_program)
b932b8b1 796 return true;
93a3eea4 797
67348ccc 798 if (MAIN_NAME_P (DECL_NAME (node->decl)))
bb853349 799 return true;
93a3eea4
JH
800
801 return false;
802}
803
804/* Return true when variable VNODE should be considered externally visible. */
805
38877e98 806bool
2c8326a5 807varpool_externally_visible_p (varpool_node *vnode)
93a3eea4 808{
67348ccc 809 if (DECL_EXTERNAL (vnode->decl))
6649df51
JH
810 return true;
811
67348ccc 812 if (!TREE_PUBLIC (vnode->decl))
93a3eea4
JH
813 return false;
814
93a3eea4 815 /* If linker counts on us, we must preserve the function. */
67348ccc 816 if (symtab_used_from_object_file_p (vnode))
93a3eea4
JH
817 return true;
818
67348ccc 819 if (DECL_HARD_REGISTER (vnode->decl))
a296a010 820 return true;
67348ccc 821 if (DECL_PRESERVE_P (vnode->decl))
93a3eea4
JH
822 return true;
823 if (lookup_attribute ("externally_visible",
67348ccc 824 DECL_ATTRIBUTES (vnode->decl)))
93a3eea4 825 return true;
9d602c59
KT
826 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
827 && lookup_attribute ("dllexport",
67348ccc 828 DECL_ATTRIBUTES (vnode->decl)))
9d602c59 829 return true;
93a3eea4
JH
830
831 /* See if we have linker information about symbol not being used or
832 if we need to make guess based on the declaration.
833
834 Even if the linker clams the symbol is unused, never bring internal
835 symbols that are declared by user as used or externally visible.
836 This is needed for i.e. references from asm statements. */
67348ccc 837 if (symtab_used_from_object_file_p (vnode))
93a3eea4 838 return true;
67348ccc 839 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
ed0d2da0 840 return false;
93a3eea4 841
073a8998 842 /* As a special case, the COMDAT virtual tables can be unshared.
430c6ceb
JH
843 In LTO mode turn vtables into static variables. The variable is readonly,
844 so this does not enable more optimization, but referring static var
845 is faster for dynamic linking. Also this match logic hidding vtables
846 from LTO symbol tables. */
847 if ((in_lto_p || flag_whole_program)
67348ccc
DM
848 && DECL_COMDAT (vnode->decl)
849 && comdat_can_be_unshared_p (vnode))
430c6ceb
JH
850 return false;
851
93a3eea4
JH
852 /* When doing link time optimizations, hidden symbols become local. */
853 if (in_lto_p
67348ccc
DM
854 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
855 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
93a3eea4
JH
856 /* Be sure that node is defined in IR file, not in other object
857 file. In that case we don't set used_from_other_object_file. */
67348ccc 858 && vnode->definition)
93a3eea4
JH
859 ;
860 else if (!flag_whole_program)
861 return true;
862
863 /* Do not attempt to privatize COMDATS by default.
864 This would break linking with C++ libraries sharing
865 inline definitions.
866
867 FIXME: We can do so for readonly vars with no address taken and
868 possibly also for vtables since no direct pointer comparsion is done.
869 It might be interesting to do so to reduce linking overhead. */
67348ccc 870 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
b20996ff
JH
871 return true;
872 return false;
873}
874
af15184a
JH
875/* Return true if reference to NODE can be replaced by a local alias.
876 Local aliases save dynamic linking overhead and enable more optimizations.
877 */
878
879bool
5e20cdc9 880can_replace_by_local_alias (symtab_node *node)
af15184a
JH
881{
882 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
09ce3660 883 && !symtab_can_be_discarded (node));
af15184a
JH
884}
885
f4b3ca72
JH
886/* Mark visibility of all functions.
887
888 A local function is one whose calls can occur only in the current
889 compilation unit and all its calls are explicit, so we can change
890 its calling convention. We simply mark all static functions whose
891 address is not taken as local.
892
893 We also change the TREE_PUBLIC flag of all declarations that are public
894 in language point of view but we want to overwrite this default
895 via visibilities for the backend point of view. */
896
4e260309 897static unsigned int
b20996ff 898function_and_variable_visibility (bool whole_program)
f4b3ca72
JH
899{
900 struct cgraph_node *node;
2c8326a5 901 varpool_node *vnode;
5dde3b01 902
5ac42672 903 /* All aliases should be procssed at this point. */
c3284718 904 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
f4b3ca72 905
65c70e6b 906 FOR_EACH_FUNCTION (node)
f4b3ca72 907 {
67348ccc 908 int flags = flags_from_decl_or_type (node->decl);
8cb114b9
JH
909
910 /* Optimize away PURE and CONST constructors and destructors. */
530f3a1b
JH
911 if (optimize
912 && (flags & (ECF_CONST | ECF_PURE))
913 && !(flags & ECF_LOOPING_CONST_OR_PURE))
914 {
67348ccc
DM
915 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
916 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
530f3a1b
JH
917 }
918
8cb114b9
JH
919 /* Frontends and alias code marks nodes as needed before parsing is finished.
920 We may end up marking as node external nodes where this flag is meaningless
921 strip it. */
67348ccc 922 if (DECL_EXTERNAL (node->decl) || !node->definition)
edb983b2 923 {
67348ccc
DM
924 node->force_output = 0;
925 node->forced_by_abi = 0;
edb983b2 926 }
8cb114b9 927
589520b6
JH
928 /* C++ FE on lack of COMDAT support create local COMDAT functions
929 (that ought to be shared but can not due to object format
073a8998 930 limitations). It is necessary to keep the flag to make rest of C++ FE
589520b6 931 happy. Clear the flag here to avoid confusion in middle-end. */
67348ccc
DM
932 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
933 DECL_COMDAT (node->decl) = 0;
08346abd
JH
934
935 /* For external decls stop tracking same_comdat_group. It doesn't matter
936 what comdat group they are in when they won't be emitted in this TU. */
67348ccc 937 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
3fd54fb0 938 {
78eaf7bf 939#ifdef ENABLE_CHECKING
5e20cdc9 940 symtab_node *n;
78eaf7bf 941
67348ccc
DM
942 for (n = node->same_comdat_group;
943 n != node;
944 n = n->same_comdat_group)
3fd54fb0
JJ
945 /* If at least one of same comdat group functions is external,
946 all of them have to be, otherwise it is a front-end bug. */
67348ccc 947 gcc_assert (DECL_EXTERNAL (n->decl));
78eaf7bf 948#endif
67348ccc 949 symtab_dissolve_same_comdat_group_list (node);
3fd54fb0 950 }
67348ccc
DM
951 gcc_assert ((!DECL_WEAK (node->decl)
952 && !DECL_COMDAT (node->decl))
953 || TREE_PUBLIC (node->decl)
954 || node->weakref
955 || DECL_EXTERNAL (node->decl));
5ac42672 956 if (cgraph_externally_visible_p (node, whole_program))
b20996ff
JH
957 {
958 gcc_assert (!node->global.inlined_to);
67348ccc 959 node->externally_visible = true;
b20996ff
JH
960 }
961 else
edb983b2 962 {
67348ccc
DM
963 node->externally_visible = false;
964 node->forced_by_abi = false;
edb983b2 965 }
67348ccc
DM
966 if (!node->externally_visible
967 && node->definition && !node->weakref
968 && !DECL_EXTERNAL (node->decl))
f4b3ca72 969 {
960bfb69 970 gcc_assert (whole_program || in_lto_p
67348ccc
DM
971 || !TREE_PUBLIC (node->decl));
972 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
973 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
974 && TREE_PUBLIC (node->decl));
67348ccc 975 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
1f26ac87 976 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
78eaf7bf
MJ
977 /* cgraph_externally_visible_p has already checked all other nodes
978 in the group and they will all be made local. We need to
979 dissolve the group at once so that the predicate does not
980 segfault though. */
67348ccc 981 symtab_dissolve_same_comdat_group_list (node);
1f26ac87 982 symtab_make_decl_local (node->decl);
f4b3ca72 983 }
c47d0034
JH
984
985 if (node->thunk.thunk_p
67348ccc 986 && TREE_PUBLIC (node->decl))
c47d0034
JH
987 {
988 struct cgraph_node *decl_node = node;
989
39e2db00 990 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
c47d0034
JH
991
992 /* Thunks have the same visibility as function they are attached to.
2fda8e14 993 Make sure the C++ front end set this up properly. */
67348ccc 994 if (DECL_ONE_ONLY (decl_node->decl))
c47d0034 995 {
67348ccc
DM
996 gcc_checking_assert (DECL_COMDAT (node->decl)
997 == DECL_COMDAT (decl_node->decl));
998 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
999 == DECL_COMDAT_GROUP (decl_node->decl));
1000 gcc_checking_assert (node->same_comdat_group);
c47d0034 1001 }
67348ccc
DM
1002 if (DECL_EXTERNAL (decl_node->decl))
1003 DECL_EXTERNAL (node->decl) = 1;
c47d0034 1004 }
f4b3ca72 1005 }
65c70e6b 1006 FOR_EACH_DEFINED_FUNCTION (node)
af15184a 1007 {
51a5c0c2 1008 node->local.local |= cgraph_local_node_p (node);
af15184a
JH
1009
1010 /* If we know that function can not be overwritten by a different semantics
1011 and moreover its section can not be discarded, replace all direct calls
1012 by calls to an nonoverwritable alias. This make dynamic linking
1013 cheaper and enable more optimization.
1014
1015 TODO: We can also update virtual tables. */
67348ccc 1016 if (node->callers && can_replace_by_local_alias (node))
af15184a 1017 {
67348ccc 1018 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
af15184a 1019
5b79657a 1020 if (alias && alias != node)
af15184a
JH
1021 {
1022 while (node->callers)
1023 {
1024 struct cgraph_edge *e = node->callers;
1025
1026 cgraph_redirect_edge_callee (e, alias);
67348ccc 1027 if (gimple_has_body_p (e->caller->decl))
af15184a 1028 {
67348ccc 1029 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
af15184a
JH
1030 cgraph_redirect_edge_call_stmt_to_callee (e);
1031 pop_cfun ();
1032 }
1033 }
1034 }
1035 }
1036 }
65c70e6b 1037 FOR_EACH_VARIABLE (vnode)
e9fecf0e
JH
1038 {
1039 /* weak flag makes no sense on local variables. */
67348ccc
DM
1040 gcc_assert (!DECL_WEAK (vnode->decl)
1041 || vnode->weakref
1042 || TREE_PUBLIC (vnode->decl)
1043 || DECL_EXTERNAL (vnode->decl));
e9fecf0e
JH
1044 /* In several cases declarations can not be common:
1045
1046 - when declaration has initializer
1047 - when it is in weak
1048 - when it has specific section
1049 - when it resides in non-generic address space.
1050 - if declaration is local, it will get into .local common section
1051 so common flag is not needed. Frontends still produce these in
1052 certain cases, such as for:
1053
1054 static int a __attribute__ ((common))
1055
1056 Canonicalize things here and clear the redundant flag. */
67348ccc
DM
1057 if (DECL_COMMON (vnode->decl)
1058 && (!(TREE_PUBLIC (vnode->decl)
1059 || DECL_EXTERNAL (vnode->decl))
1060 || (DECL_INITIAL (vnode->decl)
1061 && DECL_INITIAL (vnode->decl) != error_mark_node)
1062 || DECL_WEAK (vnode->decl)
1063 || DECL_SECTION_NAME (vnode->decl) != NULL
e9fecf0e 1064 || ! (ADDR_SPACE_GENERIC_P
67348ccc
DM
1065 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1066 DECL_COMMON (vnode->decl) = 0;
e9fecf0e 1067 }
65c70e6b 1068 FOR_EACH_DEFINED_VARIABLE (vnode)
f4b3ca72 1069 {
67348ccc 1070 if (!vnode->definition)
b820a2f9 1071 continue;
5ac42672 1072 if (varpool_externally_visible_p (vnode))
67348ccc 1073 vnode->externally_visible = true;
b20996ff 1074 else
edb983b2 1075 {
67348ccc
DM
1076 vnode->externally_visible = false;
1077 vnode->forced_by_abi = false;
edb983b2 1078 }
67348ccc
DM
1079 if (!vnode->externally_visible
1080 && !vnode->weakref)
f4b3ca72 1081 {
67348ccc
DM
1082 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1083 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1084 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1085 && TREE_PUBLIC (vnode->decl));
1086 symtab_make_decl_local (vnode->decl);
1087 if (vnode->same_comdat_group)
1088 symtab_dissolve_same_comdat_group_list (vnode);
1089 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
f4b3ca72 1090 }
f4b3ca72
JH
1091 }
1092
1093 if (dump_file)
1094 {
1095 fprintf (dump_file, "\nMarking local functions:");
65c70e6b 1096 FOR_EACH_DEFINED_FUNCTION (node)
f4b3ca72 1097 if (node->local.local)
fec39fa6 1098 fprintf (dump_file, " %s", node->name ());
f4b3ca72
JH
1099 fprintf (dump_file, "\n\n");
1100 fprintf (dump_file, "\nMarking externally visible functions:");
65c70e6b 1101 FOR_EACH_DEFINED_FUNCTION (node)
67348ccc 1102 if (node->externally_visible)
fec39fa6 1103 fprintf (dump_file, " %s", node->name ());
f4b3ca72 1104 fprintf (dump_file, "\n\n");
a8289259 1105 fprintf (dump_file, "\nMarking externally visible variables:");
65c70e6b 1106 FOR_EACH_DEFINED_VARIABLE (vnode)
67348ccc 1107 if (vnode->externally_visible)
fec39fa6 1108 fprintf (dump_file, " %s", vnode->name ());
a8289259 1109 fprintf (dump_file, "\n\n");
f4b3ca72
JH
1110 }
1111 cgraph_function_flags_ready = true;
4e260309 1112 return 0;
f4b3ca72
JH
1113}
1114
b20996ff
JH
1115/* Local function pass handling visibilities. This happens before LTO streaming
1116 so in particular -fwhole-program should be ignored at this level. */
1117
1118static unsigned int
1119local_function_and_variable_visibility (void)
1120{
014d92e1 1121 return function_and_variable_visibility (flag_whole_program && !flag_lto);
b20996ff
JH
1122}
1123
27a4cd48
DM
1124namespace {
1125
1126const pass_data pass_data_ipa_function_and_variable_visibility =
f4b3ca72 1127{
27a4cd48
DM
1128 SIMPLE_IPA_PASS, /* type */
1129 "visibility", /* name */
1130 OPTGROUP_NONE, /* optinfo_flags */
1131 false, /* has_gate */
1132 true, /* has_execute */
1133 TV_CGRAPHOPT, /* tv_id */
1134 0, /* properties_required */
1135 0, /* properties_provided */
1136 0, /* properties_destroyed */
1137 0, /* todo_flags_start */
1138 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
f4b3ca72 1139};
fed5ae11 1140
27a4cd48
DM
1141class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1142{
1143public:
c3284718
RS
1144 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1145 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1146 ctxt)
27a4cd48
DM
1147 {}
1148
1149 /* opt_pass methods: */
1150 unsigned int execute () {
1151 return local_function_and_variable_visibility ();
1152 }
1153
1154}; // class pass_ipa_function_and_variable_visibility
1155
1156} // anon namespace
1157
1158simple_ipa_opt_pass *
1159make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1160{
1161 return new pass_ipa_function_and_variable_visibility (ctxt);
1162}
1163
a8da72b8
L
1164/* Free inline summary. */
1165
1166static unsigned
1167free_inline_summary (void)
1168{
1169 inline_free_summary ();
1170 return 0;
1171}
1172
27a4cd48
DM
1173namespace {
1174
1175const pass_data pass_data_ipa_free_inline_summary =
a8da72b8 1176{
27a4cd48
DM
1177 SIMPLE_IPA_PASS, /* type */
1178 "*free_inline_summary", /* name */
1179 OPTGROUP_NONE, /* optinfo_flags */
1180 false, /* has_gate */
1181 true, /* has_execute */
1182 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1183 0, /* properties_required */
1184 0, /* properties_provided */
1185 0, /* properties_destroyed */
1186 0, /* todo_flags_start */
1187 0, /* todo_flags_finish */
a8da72b8
L
1188};
1189
27a4cd48
DM
1190class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1191{
1192public:
c3284718
RS
1193 pass_ipa_free_inline_summary (gcc::context *ctxt)
1194 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
27a4cd48
DM
1195 {}
1196
1197 /* opt_pass methods: */
1198 unsigned int execute () { return free_inline_summary (); }
1199
1200}; // class pass_ipa_free_inline_summary
1201
1202} // anon namespace
1203
1204simple_ipa_opt_pass *
1205make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1206{
1207 return new pass_ipa_free_inline_summary (ctxt);
1208}
1209
b20996ff
JH
1210/* Do not re-run on ltrans stage. */
1211
1212static bool
1213gate_whole_program_function_and_variable_visibility (void)
1214{
1215 return !flag_ltrans;
1216}
1217
073a8998 1218/* Bring functionss local at LTO time with -fwhole-program. */
b20996ff
JH
1219
1220static unsigned int
1221whole_program_function_and_variable_visibility (void)
1222{
b20996ff 1223 function_and_variable_visibility (flag_whole_program);
f10ea640
JH
1224 if (optimize)
1225 ipa_discover_readonly_nonaddressable_vars ();
b20996ff
JH
1226 return 0;
1227}
1228
27a4cd48
DM
1229namespace {
1230
1231const pass_data pass_data_ipa_whole_program_visibility =
b20996ff 1232{
27a4cd48
DM
1233 IPA_PASS, /* type */
1234 "whole-program", /* name */
1235 OPTGROUP_NONE, /* optinfo_flags */
1236 true, /* has_gate */
1237 true, /* has_execute */
1238 TV_CGRAPHOPT, /* tv_id */
1239 0, /* properties_required */
1240 0, /* properties_provided */
1241 0, /* properties_destroyed */
1242 0, /* todo_flags_start */
1243 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
b20996ff 1244};
fed5ae11 1245
27a4cd48
DM
1246class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1247{
1248public:
c3284718
RS
1249 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1250 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1251 NULL, /* generate_summary */
1252 NULL, /* write_summary */
1253 NULL, /* read_summary */
1254 NULL, /* write_optimization_summary */
1255 NULL, /* read_optimization_summary */
1256 NULL, /* stmt_fixup */
1257 0, /* function_transform_todo_flags_start */
1258 NULL, /* function_transform */
1259 NULL) /* variable_transform */
27a4cd48
DM
1260 {}
1261
1262 /* opt_pass methods: */
1263 bool gate () {
1264 return gate_whole_program_function_and_variable_visibility ();
1265 }
1266 unsigned int execute () {
1267 return whole_program_function_and_variable_visibility ();
1268 }
1269
1270}; // class pass_ipa_whole_program_visibility
1271
1272} // anon namespace
1273
1274ipa_opt_pass_d *
1275make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1276{
1277 return new pass_ipa_whole_program_visibility (ctxt);
1278}
1279
9e97ff61 1280/* Generate and emit a static constructor or destructor. WHICH must
089d1227
IE
1281 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1282 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1283 initialization priority for this constructor or destructor.
9e97ff61 1284
3a9ed12a
JH
1285 FINAL specify whether the externally visible name for collect2 should
1286 be produced. */
1287
1288static void
1289cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
9e97ff61
JH
1290{
1291 static int counter = 0;
1292 char which_buf[16];
1293 tree decl, name, resdecl;
1294
1295 /* The priority is encoded in the constructor or destructor name.
1296 collect2 will sort the names and arrange that they are called at
1297 program startup. */
3a9ed12a
JH
1298 if (final)
1299 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1300 else
1301 /* Proudce sane name but one not recognizable by collect2, just for the
1302 case we fail to inline the function. */
1303 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
9e97ff61
JH
1304 name = get_file_function_name (which_buf);
1305
1306 decl = build_decl (input_location, FUNCTION_DECL, name,
1307 build_function_type_list (void_type_node, NULL_TREE));
1308 current_function_decl = decl;
1309
1310 resdecl = build_decl (input_location,
1311 RESULT_DECL, NULL_TREE, void_type_node);
1312 DECL_ARTIFICIAL (resdecl) = 1;
1313 DECL_RESULT (decl) = resdecl;
1314 DECL_CONTEXT (resdecl) = decl;
1315
1316 allocate_struct_function (decl, false);
1317
1318 TREE_STATIC (decl) = 1;
1319 TREE_USED (decl) = 1;
1320 DECL_ARTIFICIAL (decl) = 1;
1321 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1322 DECL_SAVED_TREE (decl) = body;
3a9ed12a 1323 if (!targetm.have_ctors_dtors && final)
9e97ff61
JH
1324 {
1325 TREE_PUBLIC (decl) = 1;
1326 DECL_PRESERVE_P (decl) = 1;
1327 }
1328 DECL_UNINLINABLE (decl) = 1;
1329
1330 DECL_INITIAL (decl) = make_node (BLOCK);
1331 TREE_USED (DECL_INITIAL (decl)) = 1;
1332
1333 DECL_SOURCE_LOCATION (decl) = input_location;
1334 cfun->function_end_locus = input_location;
1335
1336 switch (which)
1337 {
1338 case 'I':
1339 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1340 decl_init_priority_insert (decl, priority);
1341 break;
1342 case 'D':
1343 DECL_STATIC_DESTRUCTOR (decl) = 1;
1344 decl_fini_priority_insert (decl, priority);
1345 break;
1346 default:
1347 gcc_unreachable ();
1348 }
1349
1350 gimplify_function_tree (decl);
1351
1352 cgraph_add_new_function (decl, false);
1353
1354 set_cfun (NULL);
1355 current_function_decl = NULL;
1356}
1357
3a9ed12a 1358/* Generate and emit a static constructor or destructor. WHICH must
089d1227
IE
1359 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1360 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1361 initialization priority for this constructor or destructor. */
3a9ed12a
JH
1362
1363void
1364cgraph_build_static_cdtor (char which, tree body, int priority)
1365{
1366 cgraph_build_static_cdtor_1 (which, body, priority, false);
1367}
9e97ff61
JH
1368
1369/* A vector of FUNCTION_DECLs declared as static constructors. */
9771b263 1370static vec<tree> static_ctors;
9e97ff61 1371/* A vector of FUNCTION_DECLs declared as static destructors. */
9771b263 1372static vec<tree> static_dtors;
9e97ff61
JH
1373
1374/* When target does not have ctors and dtors, we call all constructor
1375 and destructor by special initialization/destruction function
1376 recognized by collect2.
1377
1378 When we are going to build this function, collect all constructors and
1379 destructors and turn them into normal functions. */
1380
1381static void
1382record_cdtor_fn (struct cgraph_node *node)
1383{
67348ccc
DM
1384 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1385 static_ctors.safe_push (node->decl);
1386 if (DECL_STATIC_DESTRUCTOR (node->decl))
1387 static_dtors.safe_push (node->decl);
1388 node = cgraph_get_node (node->decl);
1389 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
9e97ff61
JH
1390}
1391
1392/* Define global constructors/destructor functions for the CDTORS, of
1393 which they are LEN. The CDTORS are sorted by initialization
1394 priority. If CTOR_P is true, these are constructors; otherwise,
1395 they are destructors. */
1396
1397static void
9771b263 1398build_cdtor (bool ctor_p, vec<tree> cdtors)
9e97ff61
JH
1399{
1400 size_t i,j;
9771b263 1401 size_t len = cdtors.length ();
9e97ff61
JH
1402
1403 i = 0;
1404 while (i < len)
1405 {
1406 tree body;
1407 tree fn;
1408 priority_type priority;
1409
1410 priority = 0;
1411 body = NULL_TREE;
1412 j = i;
1413 do
1414 {
1415 priority_type p;
9771b263 1416 fn = cdtors[j];
9e97ff61
JH
1417 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1418 if (j == i)
1419 priority = p;
1420 else if (p != priority)
1421 break;
1422 j++;
1423 }
1424 while (j < len);
1425
48c24aca 1426 /* When there is only one cdtor and target supports them, do nothing. */
9e97ff61
JH
1427 if (j == i + 1
1428 && targetm.have_ctors_dtors)
1429 {
1430 i++;
1431 continue;
1432 }
1433 /* Find the next batch of constructors/destructors with the same
1434 initialization priority. */
48c24aca 1435 for (;i < j; i++)
9e97ff61 1436 {
9e97ff61 1437 tree call;
9771b263 1438 fn = cdtors[i];
9e97ff61
JH
1439 call = build_call_expr (fn, 0);
1440 if (ctor_p)
1441 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1442 else
1443 DECL_STATIC_DESTRUCTOR (fn) = 0;
1444 /* We do not want to optimize away pure/const calls here.
1445 When optimizing, these should be already removed, when not
1446 optimizing, we want user to be able to breakpoint in them. */
1447 TREE_SIDE_EFFECTS (call) = 1;
1448 append_to_statement_list (call, &body);
9e97ff61 1449 }
9e97ff61
JH
1450 gcc_assert (body != NULL_TREE);
1451 /* Generate a function to call all the function of like
1452 priority. */
3a9ed12a 1453 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
9e97ff61
JH
1454 }
1455}
1456
1457/* Comparison function for qsort. P1 and P2 are actually of type
1458 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1459 used to determine the sort order. */
1460
1461static int
1462compare_ctor (const void *p1, const void *p2)
1463{
1464 tree f1;
1465 tree f2;
1466 int priority1;
1467 int priority2;
1468
1469 f1 = *(const tree *)p1;
1470 f2 = *(const tree *)p2;
1471 priority1 = DECL_INIT_PRIORITY (f1);
1472 priority2 = DECL_INIT_PRIORITY (f2);
1473
1474 if (priority1 < priority2)
1475 return -1;
1476 else if (priority1 > priority2)
1477 return 1;
1478 else
1479 /* Ensure a stable sort. Constructors are executed in backwarding
1480 order to make LTO initialize braries first. */
1481 return DECL_UID (f2) - DECL_UID (f1);
1482}
1483
1484/* Comparison function for qsort. P1 and P2 are actually of type
1485 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1486 used to determine the sort order. */
1487
1488static int
1489compare_dtor (const void *p1, const void *p2)
1490{
1491 tree f1;
1492 tree f2;
1493 int priority1;
1494 int priority2;
1495
1496 f1 = *(const tree *)p1;
1497 f2 = *(const tree *)p2;
1498 priority1 = DECL_FINI_PRIORITY (f1);
1499 priority2 = DECL_FINI_PRIORITY (f2);
1500
1501 if (priority1 < priority2)
1502 return -1;
1503 else if (priority1 > priority2)
1504 return 1;
1505 else
1506 /* Ensure a stable sort. */
1507 return DECL_UID (f1) - DECL_UID (f2);
1508}
1509
1510/* Generate functions to call static constructors and destructors
1511 for targets that do not support .ctors/.dtors sections. These
1512 functions have magic names which are detected by collect2. */
1513
1514static void
1515build_cdtor_fns (void)
1516{
9771b263 1517 if (!static_ctors.is_empty ())
9e97ff61
JH
1518 {
1519 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
9771b263 1520 static_ctors.qsort (compare_ctor);
48c24aca 1521 build_cdtor (/*ctor_p=*/true, static_ctors);
9e97ff61
JH
1522 }
1523
9771b263 1524 if (!static_dtors.is_empty ())
9e97ff61
JH
1525 {
1526 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
9771b263 1527 static_dtors.qsort (compare_dtor);
48c24aca 1528 build_cdtor (/*ctor_p=*/false, static_dtors);
9e97ff61
JH
1529 }
1530}
1531
1532/* Look for constructors and destructors and produce function calling them.
1533 This is needed for targets not supporting ctors or dtors, but we perform the
073a8998 1534 transformation also at linktime to merge possibly numerous
9e97ff61
JH
1535 constructors/destructors into single function to improve code locality and
1536 reduce size. */
1537
1538static unsigned int
1539ipa_cdtor_merge (void)
1540{
1541 struct cgraph_node *node;
65c70e6b 1542 FOR_EACH_DEFINED_FUNCTION (node)
67348ccc
DM
1543 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1544 || DECL_STATIC_DESTRUCTOR (node->decl))
9e97ff61
JH
1545 record_cdtor_fn (node);
1546 build_cdtor_fns ();
9771b263
DN
1547 static_ctors.release ();
1548 static_dtors.release ();
9e97ff61
JH
1549 return 0;
1550}
1551
1552/* Perform the pass when we have no ctors/dtors support
1553 or at LTO time to merge multiple constructors into single
1554 function. */
1555
1556static bool
1557gate_ipa_cdtor_merge (void)
1558{
1559 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1560}
1561
27a4cd48
DM
1562namespace {
1563
1564const pass_data pass_data_ipa_cdtor_merge =
9e97ff61 1565{
27a4cd48
DM
1566 IPA_PASS, /* type */
1567 "cdtor", /* name */
1568 OPTGROUP_NONE, /* optinfo_flags */
1569 true, /* has_gate */
1570 true, /* has_execute */
1571 TV_CGRAPHOPT, /* tv_id */
1572 0, /* properties_required */
1573 0, /* properties_provided */
1574 0, /* properties_destroyed */
1575 0, /* todo_flags_start */
1576 0, /* todo_flags_finish */
9e97ff61 1577};
27a4cd48
DM
1578
1579class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1580{
1581public:
c3284718
RS
1582 pass_ipa_cdtor_merge (gcc::context *ctxt)
1583 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1584 NULL, /* generate_summary */
1585 NULL, /* write_summary */
1586 NULL, /* read_summary */
1587 NULL, /* write_optimization_summary */
1588 NULL, /* read_optimization_summary */
1589 NULL, /* stmt_fixup */
1590 0, /* function_transform_todo_flags_start */
1591 NULL, /* function_transform */
1592 NULL) /* variable_transform */
27a4cd48
DM
1593 {}
1594
1595 /* opt_pass methods: */
1596 bool gate () { return gate_ipa_cdtor_merge (); }
1597 unsigned int execute () { return ipa_cdtor_merge (); }
1598
1599}; // class pass_ipa_cdtor_merge
1600
1601} // anon namespace
1602
1603ipa_opt_pass_d *
1604make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1605{
1606 return new pass_ipa_cdtor_merge (ctxt);
1607}