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