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