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