OSDN Git Service

* ipa.c (function_and_variable_visibility): Fix my accidentail commit and
[pf3gnuchains/gcc-fork.git] / gcc / ipa.c
1 /* Basic IPA optimizations and utilities.
2    Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation,
3    Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cgraph.h"
26 #include "tree-pass.h"
27 #include "timevar.h"
28 #include "gimple.h"
29 #include "ggc.h"
30
31 /* Fill array order with all nodes with output flag set in the reverse
32    topological order.  */
33
34 int
35 cgraph_postorder (struct cgraph_node **order)
36 {
37   struct cgraph_node *node, *node2;
38   int stack_size = 0;
39   int order_pos = 0;
40   struct cgraph_edge *edge, last;
41   int pass;
42
43   struct cgraph_node **stack =
44     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
45
46   /* We have to deal with cycles nicely, so use a depth first traversal
47      output algorithm.  Ignore the fact that some functions won't need
48      to be output and put them into order as well, so we get dependencies
49      right through inline functions.  */
50   for (node = cgraph_nodes; node; node = node->next)
51     node->aux = NULL;
52   for (pass = 0; pass < 2; pass++)
53     for (node = cgraph_nodes; node; node = node->next)
54       if (!node->aux
55           && (pass
56               || (!cgraph_only_called_directly_p (node)
57                   && !node->address_taken)))
58         {
59           node2 = node;
60           if (!node->callers)
61             node->aux = &last;
62           else
63             node->aux = node->callers;
64           while (node2)
65             {
66               while (node2->aux != &last)
67                 {
68                   edge = (struct cgraph_edge *) node2->aux;
69                   if (edge->next_caller)
70                     node2->aux = edge->next_caller;
71                   else
72                     node2->aux = &last;
73                   if (!edge->caller->aux)
74                     {
75                       if (!edge->caller->callers)
76                         edge->caller->aux = &last;
77                       else
78                         edge->caller->aux = edge->caller->callers;
79                       stack[stack_size++] = node2;
80                       node2 = edge->caller;
81                       break;
82                     }
83                 }
84               if (node2->aux == &last)
85                 {
86                   order[order_pos++] = node2;
87                   if (stack_size)
88                     node2 = stack[--stack_size];
89                   else
90                     node2 = NULL;
91                 }
92             }
93         }
94   free (stack);
95   for (node = cgraph_nodes; node; node = node->next)
96     node->aux = NULL;
97   return order_pos;
98 }
99
100 /* Look for all functions inlined to NODE and update their inlined_to pointers
101    to INLINED_TO.  */
102
103 static void
104 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
105 {
106   struct cgraph_edge *e;
107   for (e = node->callees; e; e = e->next_callee)
108     if (e->callee->global.inlined_to)
109       {
110         e->callee->global.inlined_to = inlined_to;
111         update_inlined_to_pointer (e->callee, inlined_to);
112       }
113 }
114
115 /* Perform reachability analysis and reclaim all unreachable nodes.
116    If BEFORE_INLINING_P is true this function is called before inlining
117    decisions has been made.  If BEFORE_INLINING_P is false this function also 
118    removes unneeded bodies of extern inline functions.  */
119
120 bool
121 cgraph_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
122 {
123   struct cgraph_node *first = (struct cgraph_node *) (void *) 1;
124   struct cgraph_node *node, *next;
125   bool changed = false;
126
127 #ifdef ENABLE_CHECKING
128   verify_cgraph ();
129 #endif
130   if (file)
131     fprintf (file, "\nReclaiming functions:");
132 #ifdef ENABLE_CHECKING
133   for (node = cgraph_nodes; node; node = node->next)
134     gcc_assert (!node->aux);
135 #endif
136   for (node = cgraph_nodes; node; node = node->next)
137     if (!cgraph_can_remove_if_no_direct_calls_p (node)
138         && ((!DECL_EXTERNAL (node->decl)) 
139             || !node->analyzed
140             || before_inlining_p))
141       {
142         gcc_assert (!node->global.inlined_to);
143         node->aux = first;
144         first = node;
145       }
146     else
147       gcc_assert (!node->aux);
148
149   /* Perform reachability analysis.  As a special case do not consider
150      extern inline functions not inlined as live because we won't output
151      them at all.  */
152   while (first != (void *) 1)
153     {
154       struct cgraph_edge *e;
155       node = first;
156       first = (struct cgraph_node *) first->aux;
157
158       for (e = node->callees; e; e = e->next_callee)
159         if (!e->callee->aux
160             && node->analyzed
161             && (!e->inline_failed || !e->callee->analyzed
162                 || (!DECL_EXTERNAL (e->callee->decl))
163                 || before_inlining_p))
164           {
165             e->callee->aux = first;
166             first = e->callee;
167           }
168       while (node->clone_of && !node->clone_of->aux && !gimple_has_body_p (node->decl))
169         {
170           node = node->clone_of;
171           node->aux = first;
172           first = node;
173         }
174     }
175
176   /* Remove unreachable nodes.  Extern inline functions need special care;
177      Unreachable extern inline functions shall be removed.
178      Reachable extern inline functions we never inlined shall get their bodies
179      eliminated.
180      Reachable extern inline functions we sometimes inlined will be turned into
181      unanalyzed nodes so they look like for true extern functions to the rest
182      of code.  Body of such functions is released via remove_node once the
183      inline clones are eliminated.  */
184   for (node = cgraph_nodes; node; node = next)
185     {
186       next = node->next;
187       if (!node->aux)
188         {
189           node->global.inlined_to = NULL;
190           if (file)
191             fprintf (file, " %s", cgraph_node_name (node));
192           if (!node->analyzed || !DECL_EXTERNAL (node->decl)
193               || before_inlining_p)
194             cgraph_remove_node (node);
195           else
196             {
197               struct cgraph_edge *e;
198
199               /* See if there is reachable caller.  */
200               for (e = node->callers; e; e = e->next_caller)
201                 if (e->caller->aux)
202                   break;
203
204               /* If so, we need to keep node in the callgraph.  */
205               if (e || node->needed)
206                 {
207                   struct cgraph_node *clone;
208
209                   /* If there are still clones, we must keep body around.
210                      Otherwise we can just remove the body but keep the clone.  */
211                   for (clone = node->clones; clone;
212                        clone = clone->next_sibling_clone)
213                     if (clone->aux)
214                       break;
215                   if (!clone)
216                     {
217                       cgraph_release_function_body (node);
218                       cgraph_node_remove_callees (node);
219                       node->analyzed = false;
220                       node->local.inlinable = false;
221                     }
222                 }
223               else
224                 cgraph_remove_node (node);
225             }
226           changed = true;
227         }
228     }
229   for (node = cgraph_nodes; node; node = node->next)
230     {
231       /* Inline clones might be kept around so their materializing allows further
232          cloning.  If the function the clone is inlined into is removed, we need
233          to turn it into normal cone.  */
234       if (node->global.inlined_to
235           && !node->callers)
236         {
237           gcc_assert (node->clones);
238           node->global.inlined_to = NULL;
239           update_inlined_to_pointer (node, node);
240         }
241       node->aux = NULL;
242     }
243 #ifdef ENABLE_CHECKING
244   verify_cgraph ();
245 #endif
246
247   /* Reclaim alias pairs for functions that have disappeared from the
248      call graph.  */
249   remove_unreachable_alias_pairs ();
250
251   return changed;
252 }
253
254 static bool
255 cgraph_externally_visible_p (struct cgraph_node *node, bool whole_program)
256 {
257   if (!node->local.finalized)
258     return false;
259   if (!DECL_COMDAT (node->decl)
260       && (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl)))
261     return false;
262   if (!whole_program)
263     return true;
264   /* COMDAT functions must be shared only if they have address taken,
265      otherwise we can produce our own private implementation with
266      -fwhole-program.  */
267   if (DECL_COMDAT (node->decl) && (node->address_taken || !node->analyzed))
268     return true;
269   if (MAIN_NAME_P (DECL_NAME (node->decl)))
270     return true;
271   if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (node->decl)))
272     return true;
273   return false;
274 }
275
276 /* Mark visibility of all functions.
277
278    A local function is one whose calls can occur only in the current
279    compilation unit and all its calls are explicit, so we can change
280    its calling convention.  We simply mark all static functions whose
281    address is not taken as local.
282
283    We also change the TREE_PUBLIC flag of all declarations that are public
284    in language point of view but we want to overwrite this default
285    via visibilities for the backend point of view.  */
286
287 static unsigned int
288 function_and_variable_visibility (bool whole_program)
289 {
290   struct cgraph_node *node;
291   struct varpool_node *vnode;
292
293   for (node = cgraph_nodes; node; node = node->next)
294     {
295       gcc_assert ((!DECL_WEAK (node->decl) && !DECL_COMDAT (node->decl))
296                   || TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl));
297       if (cgraph_externally_visible_p (node, whole_program))
298         {
299           gcc_assert (!node->global.inlined_to);
300           node->local.externally_visible = true;
301         }
302       else
303         node->local.externally_visible = false;
304       if (!node->local.externally_visible && node->analyzed
305           && !DECL_EXTERNAL (node->decl))
306         {
307           gcc_assert (whole_program || !TREE_PUBLIC (node->decl));
308           TREE_PUBLIC (node->decl) = 0;
309           DECL_COMDAT (node->decl) = 0;
310           DECL_WEAK (node->decl) = 0;
311         }
312       node->local.local = (cgraph_only_called_directly_p (node)
313                            && node->analyzed
314                            && !DECL_EXTERNAL (node->decl)
315                            && !node->local.externally_visible);
316     }
317   for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
318     {
319       if (!vnode->finalized)
320         continue;
321       gcc_assert ((!DECL_WEAK (vnode->decl) && !DECL_COMMON (vnode->decl))
322                   || TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl));
323       if (vnode->needed
324           && (DECL_COMDAT (vnode->decl) || TREE_PUBLIC (vnode->decl))
325           && (!whole_program
326               /* We can privatize comdat readonly variables whose address is not taken,
327                  but doing so is not going to bring us optimization oppurtunities until
328                  we start reordering datastructures.  */
329               || DECL_COMDAT (vnode->decl)
330               || DECL_WEAK (vnode->decl)
331               || lookup_attribute ("externally_visible",
332                                    DECL_ATTRIBUTES (vnode->decl))))
333         vnode->externally_visible = true;
334       else
335         vnode->externally_visible = false;
336       if (!vnode->externally_visible)
337         {
338           gcc_assert (whole_program || !TREE_PUBLIC (vnode->decl));
339           TREE_PUBLIC (vnode->decl) = 0;
340           DECL_COMMON (vnode->decl) = 0;
341         }
342      gcc_assert (TREE_STATIC (vnode->decl));
343     }
344
345   if (dump_file)
346     {
347       fprintf (dump_file, "\nMarking local functions:");
348       for (node = cgraph_nodes; node; node = node->next)
349         if (node->local.local)
350           fprintf (dump_file, " %s", cgraph_node_name (node));
351       fprintf (dump_file, "\n\n");
352       fprintf (dump_file, "\nMarking externally visible functions:");
353       for (node = cgraph_nodes; node; node = node->next)
354         if (node->local.externally_visible)
355           fprintf (dump_file, " %s", cgraph_node_name (node));
356       fprintf (dump_file, "\n\n");
357       fprintf (dump_file, "\nMarking externally visible variables:");
358       for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
359         if (vnode->externally_visible)
360           fprintf (dump_file, " %s", varpool_node_name (vnode));
361       fprintf (dump_file, "\n\n");
362     }
363   cgraph_function_flags_ready = true;
364   return 0;
365 }
366
367 /* Local function pass handling visibilities.  This happens before LTO streaming
368    so in particular -fwhole-program should be ignored at this level.  */
369
370 static unsigned int
371 local_function_and_variable_visibility (void)
372 {
373   return function_and_variable_visibility (flag_whole_program && !flag_lto && !flag_whopr);
374 }
375
376 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility = 
377 {
378  {
379   SIMPLE_IPA_PASS,
380   "visibility",                         /* name */
381   NULL,                                 /* gate */
382   local_function_and_variable_visibility,/* execute */
383   NULL,                                 /* sub */
384   NULL,                                 /* next */
385   0,                                    /* static_pass_number */
386   TV_CGRAPHOPT,                         /* tv_id */
387   0,                                    /* properties_required */
388   0,                                    /* properties_provided */
389   0,                                    /* properties_destroyed */
390   0,                                    /* todo_flags_start */
391   TODO_remove_functions | TODO_dump_cgraph/* todo_flags_finish */
392  }
393 };
394
395 /* Do not re-run on ltrans stage.  */
396
397 static bool
398 gate_whole_program_function_and_variable_visibility (void)
399 {
400   return !flag_ltrans;
401 }
402
403 /* Bring functionss local at LTO time whith -fwhole-program.  */
404
405 static unsigned int
406 whole_program_function_and_variable_visibility (void)
407 {
408   struct cgraph_node *node;
409   struct varpool_node *vnode;
410
411   function_and_variable_visibility (flag_whole_program);
412
413   for (node = cgraph_nodes; node; node = node->next)
414     if ((node->local.externally_visible && !DECL_COMDAT (node->decl))
415         && node->local.finalized)
416       cgraph_mark_needed_node (node);
417   for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
418     if (vnode->externally_visible && !DECL_COMDAT (vnode->decl))
419       varpool_mark_needed_node (vnode);
420   if (dump_file)
421     {
422       fprintf (dump_file, "\nNeeded variables:");
423       for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
424         if (vnode->needed)
425           fprintf (dump_file, " %s", varpool_node_name (vnode));
426       fprintf (dump_file, "\n\n");
427     }
428   return 0;
429 }
430
431 struct ipa_opt_pass_d pass_ipa_whole_program_visibility =
432 {
433  {
434   IPA_PASS,
435   "whole-program",                      /* name */
436   gate_whole_program_function_and_variable_visibility,/* gate */
437   whole_program_function_and_variable_visibility,/* execute */
438   NULL,                                 /* sub */
439   NULL,                                 /* next */
440   0,                                    /* static_pass_number */
441   TV_CGRAPHOPT,                         /* tv_id */
442   0,                                    /* properties_required */
443   0,                                    /* properties_provided */
444   0,                                    /* properties_destroyed */
445   0,                                    /* todo_flags_start */
446   TODO_dump_cgraph | TODO_remove_functions/* todo_flags_finish */
447  },
448  NULL,                                  /* generate_summary */
449  NULL,                                  /* write_summary */
450  NULL,                                  /* read_summary */
451  NULL,                                  /* function_read_summary */
452  NULL,                                  /* stmt_fixup */
453  0,                                     /* TODOs */
454  NULL,                                  /* function_transform */
455  NULL,                                  /* variable_transform */
456 };
457
458 /* Hash a cgraph node set element.  */
459
460 static hashval_t
461 hash_cgraph_node_set_element (const void *p)
462 {
463   const_cgraph_node_set_element element = (const_cgraph_node_set_element) p;
464   return htab_hash_pointer (element->node);
465 }
466
467 /* Compare two cgraph node set elements.  */
468
469 static int
470 eq_cgraph_node_set_element (const void *p1, const void *p2)
471 {
472   const_cgraph_node_set_element e1 = (const_cgraph_node_set_element) p1;
473   const_cgraph_node_set_element e2 = (const_cgraph_node_set_element) p2;
474
475   return e1->node == e2->node;
476 }
477
478 /* Create a new cgraph node set.  */
479
480 cgraph_node_set
481 cgraph_node_set_new (void)
482 {
483   cgraph_node_set new_node_set;
484
485   new_node_set = GGC_NEW (struct cgraph_node_set_def);
486   new_node_set->hashtab = htab_create_ggc (10,
487                                            hash_cgraph_node_set_element,
488                                            eq_cgraph_node_set_element,
489                                            NULL);
490   new_node_set->nodes = NULL;
491   return new_node_set;
492 }
493
494 /* Add cgraph_node NODE to cgraph_node_set SET.  */
495
496 void
497 cgraph_node_set_add (cgraph_node_set set, struct cgraph_node *node)
498 {
499   void **slot;
500   cgraph_node_set_element element;
501   struct cgraph_node_set_element_def dummy;
502
503   dummy.node = node;
504   slot = htab_find_slot (set->hashtab, &dummy, INSERT);
505
506   if (*slot != HTAB_EMPTY_ENTRY)
507     {
508       element = (cgraph_node_set_element) *slot;
509       gcc_assert (node == element->node
510                   && (VEC_index (cgraph_node_ptr, set->nodes, element->index)
511                       == node));
512       return;
513     }
514
515   /* Insert node into hash table.  */
516   element =
517     (cgraph_node_set_element) GGC_NEW (struct cgraph_node_set_element_def);
518   element->node = node;
519   element->index = VEC_length (cgraph_node_ptr, set->nodes);
520   *slot = element;
521
522   /* Insert into node vector.  */
523   VEC_safe_push (cgraph_node_ptr, gc, set->nodes, node);
524 }
525
526 /* Remove cgraph_node NODE from cgraph_node_set SET.  */
527
528 void
529 cgraph_node_set_remove (cgraph_node_set set, struct cgraph_node *node)
530 {
531   void **slot, **last_slot;
532   cgraph_node_set_element element, last_element;
533   struct cgraph_node *last_node;
534   struct cgraph_node_set_element_def dummy;
535
536   dummy.node = node;
537   slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
538   if (slot == NULL)
539     return;
540
541   element = (cgraph_node_set_element) *slot;
542   gcc_assert (VEC_index (cgraph_node_ptr, set->nodes, element->index)
543               == node);
544
545   /* Remove from vector. We do this by swapping node with the last element
546      of the vector.  */
547   last_node = VEC_pop (cgraph_node_ptr, set->nodes);
548   if (last_node != node)
549     {
550       dummy.node = last_node;
551       last_slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
552       last_element = (cgraph_node_set_element) *last_slot;
553       gcc_assert (last_element);
554
555       /* Move the last element to the original spot of NODE.  */
556       last_element->index = element->index;
557       VEC_replace (cgraph_node_ptr, set->nodes, last_element->index,
558                    last_node);
559     }
560   
561   /* Remove element from hash table.  */
562   htab_clear_slot (set->hashtab, slot);
563   ggc_free (element);
564 }
565
566 /* Find NODE in SET and return an iterator to it if found.  A null iterator
567    is returned if NODE is not in SET.  */
568
569 cgraph_node_set_iterator
570 cgraph_node_set_find (cgraph_node_set set, struct cgraph_node *node)
571 {
572   void **slot;
573   struct cgraph_node_set_element_def dummy;
574   cgraph_node_set_element element;
575   cgraph_node_set_iterator csi;
576
577   dummy.node = node;
578   slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
579   if (slot == NULL)
580     csi.index = (unsigned) ~0;
581   else
582     {
583       element = (cgraph_node_set_element) *slot;
584       gcc_assert (VEC_index (cgraph_node_ptr, set->nodes, element->index)
585                   == node);
586       csi.index = element->index;
587     }
588   csi.set = set;
589
590   return csi;
591 }
592
593 /* Dump content of SET to file F.  */
594
595 void
596 dump_cgraph_node_set (FILE *f, cgraph_node_set set)
597 {
598   cgraph_node_set_iterator iter;
599
600   for (iter = csi_start (set); !csi_end_p (iter); csi_next (&iter))
601     {
602       struct cgraph_node *node = csi_node (iter);
603       dump_cgraph_node (f, node);
604     }
605 }
606
607 /* Dump content of SET to stderr.  */
608
609 void
610 debug_cgraph_node_set (cgraph_node_set set)
611 {
612   dump_cgraph_node_set (stderr, set);
613 }
614