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