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