OSDN Git Service

98a4ce7d7141ff7e840f5f77179f31a947375196
[pf3gnuchains/gcc-fork.git] / gcc / ipa-reference.c
1 /* Callgraph based analysis of static variables.
2    Copyright (C) 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
3    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
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 /* This file gathers information about how variables whose scope is
22    confined to the compilation unit are used.
23
24    There are two categories of information produced by this pass:
25
26    1) The addressable (TREE_ADDRESSABLE) bit and readonly
27    (TREE_READONLY) bit associated with these variables is properly set
28    based on scanning all of the code withing the compilation unit.
29
30    2) The transitive call site specific clobber effects are computed
31    for the variables whose scope is contained within this compilation
32    unit.
33
34    First each function and static variable initialization is analyzed
35    to determine which local static variables are either read, written,
36    or have their address taken.  Any local static that has its address
37    taken is removed from consideration.  Once the local read and
38    writes are determined, a transitive closure of this information is
39    performed over the call graph to determine the worst case set of
40    side effects of each call.  In later parts of the compiler, these
41    local and global sets are examined to make the call clobbering less
42    traumatic, promote some statics to registers, and improve aliasing
43    information.
44
45    Currently must be run after inlining decisions have been made since
46    otherwise, the local sets will not contain information that is
47    consistent with post inlined state.  The global sets are not prone
48    to this problem since they are by definition transitive.  */
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "tree.h"
55 #include "tree-flow.h"
56 #include "tree-inline.h"
57 #include "tree-pass.h"
58 #include "langhooks.h"
59 #include "pointer-set.h"
60 #include "splay-tree.h"
61 #include "ggc.h"
62 #include "ipa-utils.h"
63 #include "ipa-reference.h"
64 #include "gimple.h"
65 #include "cgraph.h"
66 #include "output.h"
67 #include "flags.h"
68 #include "timevar.h"
69 #include "diagnostic.h"
70 #include "langhooks.h"
71 #include "lto-streamer.h"
72
73 static void add_new_function (struct cgraph_node *node,
74                               void *data ATTRIBUTE_UNUSED);
75 static void remove_node_data (struct cgraph_node *node,
76                               void *data ATTRIBUTE_UNUSED);
77 static void duplicate_node_data (struct cgraph_node *src,
78                                  struct cgraph_node *dst,
79                                  void *data ATTRIBUTE_UNUSED);
80
81 /* The static variables defined within the compilation unit that are
82    loaded or stored directly by function that owns this structure.  */
83
84 struct ipa_reference_local_vars_info_d
85 {
86   bitmap statics_read;
87   bitmap statics_written;
88
89   /* Set when this function calls another function external to the
90      compilation unit or if the function has a asm clobber of memory.
91      In general, such calls are modeled as reading and writing all
92      variables (both bits on) but sometime there are attributes on the
93      called function so we can do better.  */
94   bool calls_read_all;
95   bool calls_write_all;
96 };
97
98 /* Statics that are read and written by some set of functions. The
99    local ones are based on the loads and stores local to the function.
100    The global ones are based on the local info as well as the
101    transitive closure of the functions that are called.  The
102    structures are separated to allow the global structures to be
103    shared between several functions since every function within a
104    strongly connected component will have the same information.  This
105    sharing saves both time and space in the computation of the vectors
106    as well as their translation from decl_uid form to ann_uid
107    form.  */
108
109 struct ipa_reference_global_vars_info_d
110 {
111   bitmap statics_read;
112   bitmap statics_written;
113   bitmap statics_not_read;
114   bitmap statics_not_written;
115 };
116
117 typedef struct ipa_reference_local_vars_info_d *ipa_reference_local_vars_info_t;
118 typedef struct ipa_reference_global_vars_info_d *ipa_reference_global_vars_info_t;
119 struct ipa_reference_vars_info_d
120 {
121   ipa_reference_local_vars_info_t local;
122   ipa_reference_global_vars_info_t global;
123 };
124
125 typedef struct ipa_reference_vars_info_d *ipa_reference_vars_info_t;
126
127 /* This splay tree contains all of the static variables that are
128    being considered by the compilation level alias analysis.  For
129    module_at_a_time compilation, this is the set of static but not
130    public variables.  Any variables that either have their address
131    taken or participate in otherwise unsavory operations are deleted
132    from this list.  */
133 static GTY((param1_is(int), param2_is(tree)))
134      splay_tree reference_vars_to_consider;
135
136 /* This bitmap is used to knock out the module static variables whose
137    addresses have been taken and passed around.  */
138 static bitmap module_statics_escape;
139
140 /* This bitmap is used to knock out the module static variables that
141    are not readonly.  */
142 static bitmap module_statics_written;
143
144 /* A bit is set for every module static we are considering.  This is
145    ored into the local info when asm code is found that clobbers all
146    memory. */
147 static bitmap all_module_statics;
148
149 static struct pointer_set_t *visited_nodes;
150
151 /* Obstack holding bitmaps of local analysis (live from analysis to
152    propagation)  */
153 static bitmap_obstack local_info_obstack;
154 /* Obstack holding global analysis live forever.  */
155 static bitmap_obstack global_info_obstack;
156
157 /* Holders of ipa cgraph hooks: */
158 static struct cgraph_node_hook_list *function_insertion_hook_holder;
159 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
160 static struct cgraph_node_hook_list *node_removal_hook_holder;
161
162 enum initialization_status_t
163 {
164   UNINITIALIZED,
165   RUNNING,
166   FINISHED
167 };
168
169 tree memory_identifier_string;
170
171 /* Vector where the reference var infos are actually stored. */
172 DEF_VEC_P (ipa_reference_vars_info_t);
173 DEF_VEC_ALLOC_P (ipa_reference_vars_info_t, heap);
174 static VEC (ipa_reference_vars_info_t, heap) *ipa_reference_vars_vector;
175
176 /* Return the ipa_reference_vars structure starting from the cgraph NODE.  */
177 static inline ipa_reference_vars_info_t
178 get_reference_vars_info (struct cgraph_node *node)
179 {
180   if (!ipa_reference_vars_vector
181       || VEC_length (ipa_reference_vars_info_t, ipa_reference_vars_vector) <= (unsigned int)node->uid)
182     return NULL;
183   return VEC_index (ipa_reference_vars_info_t, ipa_reference_vars_vector, node->uid);
184 }
185
186 /* Return the ipa_reference_vars structure starting from the cgraph NODE.  */
187 static inline void
188 set_reference_vars_info (struct cgraph_node *node, ipa_reference_vars_info_t info)
189 {
190   if (!ipa_reference_vars_vector
191       || VEC_length (ipa_reference_vars_info_t, ipa_reference_vars_vector) <= (unsigned int)node->uid)
192      VEC_safe_grow_cleared (ipa_reference_vars_info_t, heap, ipa_reference_vars_vector, node->uid + 1);
193   VEC_replace (ipa_reference_vars_info_t, ipa_reference_vars_vector, node->uid, info);
194 }
195
196 /* Get a bitmap that contains all of the locally referenced static
197    variables for function FN.  */
198 static ipa_reference_local_vars_info_t
199 get_local_reference_vars_info (struct cgraph_node *fn)
200 {
201   ipa_reference_vars_info_t info = get_reference_vars_info (fn);
202
203   if (info)
204     return info->local;
205   else
206     /* This phase was not run.  */
207     return NULL;
208 }
209
210 /* Get a bitmap that contains all of the globally referenced static
211    variables for function FN.  */
212
213 static ipa_reference_global_vars_info_t
214 get_global_reference_vars_info (struct cgraph_node *fn)
215 {
216   ipa_reference_vars_info_t info = get_reference_vars_info (fn);
217
218   if (info)
219     return info->global;
220   else
221     /* This phase was not run.  */
222     return NULL;
223 }
224
225 /* Return a bitmap indexed by VAR_DECL uid for the static variables
226    that are read during the execution of the function FN.  Returns
227    NULL if no data is available.  */
228
229 bitmap
230 ipa_reference_get_read_global (struct cgraph_node *fn)
231 {
232   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
233   if (g)
234     return g->statics_read;
235   else
236     return NULL;
237 }
238
239 /* Return a bitmap indexed by VAR_DECL uid for the static variables
240    that are written during the execution of the function FN.  Note
241    that variables written may or may not be read during the function
242    call.  Returns NULL if no data is available.  */
243
244 bitmap
245 ipa_reference_get_written_global (struct cgraph_node *fn)
246 {
247   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
248   if (g)
249     return g->statics_written;
250   else
251     return NULL;
252 }
253
254 /* Return a bitmap indexed by_DECL_UID uid for the static variables
255    that are not read during the execution of the function FN.  Returns
256    NULL if no data is available.  */
257
258 bitmap
259 ipa_reference_get_not_read_global (struct cgraph_node *fn)
260 {
261   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
262   if (g)
263     return g->statics_not_read;
264   else
265     return NULL;
266 }
267
268 /* Return a bitmap indexed by DECL_UID uid for the static variables
269    that are not written during the execution of the function FN.  Note
270    that variables written may or may not be read during the function
271    call.  Returns NULL if no data is available.  */
272
273 bitmap
274 ipa_reference_get_not_written_global (struct cgraph_node *fn)
275 {
276   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
277   if (g)
278     return g->statics_not_written;
279   else
280     return NULL;
281 }
282
283 \f
284
285 /* Add VAR to all_module_statics and the two
286    reference_vars_to_consider* sets.  */
287
288 static inline void
289 add_static_var (tree var)
290 {
291   int uid = DECL_UID (var);
292   gcc_assert (TREE_CODE (var) == VAR_DECL);
293   if (!bitmap_bit_p (all_module_statics, uid))
294     {
295       splay_tree_insert (reference_vars_to_consider,
296                          uid, (splay_tree_value)var);
297       bitmap_set_bit (all_module_statics, uid);
298     }
299 }
300
301 /* Return true if the variable T is the right kind of static variable to
302    perform compilation unit scope escape analysis.  */
303
304 static inline bool
305 has_proper_scope_for_analysis (tree t)
306 {
307   /* If the variable has the "used" attribute, treat it as if it had a
308      been touched by the devil.  */
309   if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
310     return false;
311
312   /* Do not want to do anything with volatile except mark any
313      function that uses one to be not const or pure.  */
314   if (TREE_THIS_VOLATILE (t))
315     return false;
316
317   /* Do not care about a local automatic that is not static.  */
318   if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
319     return false;
320
321   /* FIXME: for LTO we should include PUBLIC vars too.  This is bit difficult
322      as summarie would need unsharing.  */
323   if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
324     return false;
325
326   /* We cannot touch decls where the type needs constructing.  */
327   if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
328     return false;
329
330   /* This is a variable we care about.  Check if we have seen it
331      before, and if not add it the set of variables we care about.  */
332   if (!bitmap_bit_p (all_module_statics, DECL_UID (t)))
333     add_static_var (t);
334
335   return true;
336 }
337
338 /* Mark tree T as having address taken.  */
339
340 static void
341 mark_address_taken (tree x)
342 {
343   if (TREE_CODE (x) == VAR_DECL
344       && module_statics_escape && has_proper_scope_for_analysis (x))
345     bitmap_set_bit (module_statics_escape, DECL_UID (x));
346 }
347
348 /* Wrapper around mark_address_taken for the stmt walker.  */
349
350 static bool
351 mark_address (gimple stmt ATTRIBUTE_UNUSED, tree addr,
352               void *data ATTRIBUTE_UNUSED)
353 {
354   while (handled_component_p (addr))
355     addr = TREE_OPERAND (addr, 0);
356   mark_address_taken (addr);
357   return false;
358 }
359
360 /* Mark load of T.  */
361
362 static bool
363 mark_load (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
364 {
365   ipa_reference_local_vars_info_t local = (ipa_reference_local_vars_info_t)data;
366   if (TREE_CODE (t) == VAR_DECL
367       && has_proper_scope_for_analysis (t))
368     bitmap_set_bit (local->statics_read, DECL_UID (t));
369   return false;
370 }
371
372 /* Mark store of T.  */
373
374 static bool
375 mark_store (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
376 {
377   ipa_reference_local_vars_info_t local = (ipa_reference_local_vars_info_t)data;
378   if (TREE_CODE (t) == VAR_DECL
379       && has_proper_scope_for_analysis (t))
380     {
381       if (local)
382         bitmap_set_bit (local->statics_written, DECL_UID (t));
383       /* Mark the write so we can tell which statics are
384          readonly.  */
385       if (module_statics_written)
386         bitmap_set_bit (module_statics_written, DECL_UID (t));
387     }
388   return false;
389 }
390
391 /* Look for memory clobber and set read_all/write_all if present.  */
392
393 static void
394 check_asm_memory_clobber (ipa_reference_local_vars_info_t local, gimple stmt)
395 {
396   size_t i;
397   tree op;
398
399   for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
400     {
401       op = gimple_asm_clobber_op (stmt, i);
402       if (simple_cst_equal(TREE_VALUE (op), memory_identifier_string) == 1)
403         {
404           /* Abandon all hope, ye who enter here. */
405           local->calls_read_all = true;
406           local->calls_write_all = true;
407         }
408     }
409 }
410
411 /* Look for external calls and set read_all/write_all correspondingly.  */
412
413 static void
414 check_call (ipa_reference_local_vars_info_t local, gimple stmt)
415 {
416   int flags = gimple_call_flags (stmt);
417   tree callee_t = gimple_call_fndecl (stmt);
418
419   /* Process indirect calls.  All direct calles are handled at propagation
420      time.  */
421   if (!callee_t)
422     {
423       if (flags & ECF_CONST)
424         ;
425       else if (flags & ECF_PURE)
426         local->calls_read_all = true;
427       else
428         {
429           local->calls_read_all = true;
430           /* When function does not reutrn, it is safe to ignore anythign it writes
431              to, because the effect will never happen.  */
432           if ((flags & (ECF_NOTHROW | ECF_NORETURN))
433               != (ECF_NOTHROW | ECF_NORETURN))
434             local->calls_write_all = true;
435         }
436     }
437 }
438
439 /* TP is the part of the tree currently under the microscope.
440    WALK_SUBTREES is part of the walk_tree api but is unused here.
441    DATA is cgraph_node of the function being walked.  */
442
443 static tree
444 scan_stmt_for_static_refs (gimple_stmt_iterator *gsip,
445                            struct cgraph_node *fn)
446 {
447   gimple stmt = gsi_stmt (*gsip);
448   ipa_reference_local_vars_info_t local = NULL;
449
450   if (is_gimple_debug (stmt))
451     return NULL;
452
453   if (fn)
454     local = get_reference_vars_info (fn)->local;
455
456   /* Look for direct loads and stores.  */
457   walk_stmt_load_store_addr_ops (stmt, local, mark_load, mark_store,
458                                  mark_address);
459
460   if (is_gimple_call (stmt))
461     check_call (local, stmt);
462   else if (gimple_code (stmt) == GIMPLE_ASM)
463     check_asm_memory_clobber (local, stmt);
464
465   return NULL;
466 }
467
468 /* Call-back to scan variable initializers for static references.
469    Called using walk_tree.  */
470
471 static tree
472 scan_initializer_for_static_refs (tree *tp, int *walk_subtrees,
473                                   void *data ATTRIBUTE_UNUSED)
474 {
475   tree t = *tp;
476
477   if (TREE_CODE (t) == ADDR_EXPR)
478     {
479       mark_address_taken (get_base_var (t));
480       *walk_subtrees = 0;
481     }
482   /* Save some cycles by not walking types and declaration as we
483      won't find anything useful there anyway.  */
484   else if (IS_TYPE_OR_DECL_P (*tp))
485     *walk_subtrees = 0;
486
487   return NULL;
488 }
489
490 /* Lookup the tree node for the static variable that has UID.  */
491 static tree
492 get_static_decl (int index)
493 {
494   splay_tree_node stn =
495     splay_tree_lookup (reference_vars_to_consider, index);
496   if (stn)
497     return (tree)stn->value;
498   return NULL;
499 }
500
501 /* Lookup the tree node for the static variable that has UID and
502    convert the name to a string for debugging.  */
503
504 static const char *
505 get_static_name (int index)
506 {
507   splay_tree_node stn =
508     splay_tree_lookup (reference_vars_to_consider, index);
509   if (stn)
510     return lang_hooks.decl_printable_name ((tree)(stn->value), 2);
511   return NULL;
512 }
513
514 /* Or in all of the bits from every callee of X into X_GLOBAL, the caller's cycle,
515    bit vector.  There are several cases to check to avoid the sparse
516    bitmap oring.  */
517
518 static void
519 propagate_bits (ipa_reference_global_vars_info_t x_global, struct cgraph_node *x)
520 {
521   struct cgraph_edge *e;
522   for (e = x->callees; e; e = e->next_callee)
523     {
524       struct cgraph_node *y = e->callee;
525
526       /* Only look into nodes we can propagate something.  */
527       if (cgraph_function_body_availability (e->callee) > AVAIL_OVERWRITABLE)
528         {
529           if (get_reference_vars_info (y))
530             {
531               ipa_reference_vars_info_t y_info
532                 = get_reference_vars_info (y);
533               ipa_reference_global_vars_info_t y_global = y_info->global;
534
535               /* Calls in current cycle do not have global computed yet.  */
536               if (!y_info->global)
537                 continue;
538
539               if (x_global->statics_read
540                   != all_module_statics)
541                 {
542                   if (y_global->statics_read
543                       == all_module_statics)
544                     {
545                       BITMAP_FREE (x_global->statics_read);
546                       x_global->statics_read
547                         = all_module_statics;
548                     }
549                   /* Skip bitmaps that are pointer equal to node's bitmap
550                      (no reason to spin within the cycle).  */
551                   else if (x_global->statics_read
552                            != y_global->statics_read)
553                     bitmap_ior_into (x_global->statics_read,
554                                      y_global->statics_read);
555                 }
556
557               if (x_global->statics_written
558                   != all_module_statics)
559                 {
560                   if (y_global->statics_written
561                       == all_module_statics)
562                     {
563                       BITMAP_FREE (x_global->statics_written);
564                       x_global->statics_written
565                         = all_module_statics;
566                     }
567                   /* Skip bitmaps that are pointer equal to node's bitmap
568                      (no reason to spin within the cycle).  */
569                   else if (x_global->statics_written
570                            != y_global->statics_written)
571                     bitmap_ior_into (x_global->statics_written,
572                                      y_global->statics_written);
573                 }
574             }
575           else
576             gcc_unreachable ();
577         }
578     }
579 }
580
581 /* The init routine for analyzing global static variable usage.  See
582    comments at top for description.  */
583 static void
584 ipa_init (void)
585 {
586   static bool init_p = false;
587
588   if (init_p)
589     return;
590
591   init_p = true;
592
593   memory_identifier_string = build_string(7, "memory");
594
595   reference_vars_to_consider =
596     splay_tree_new_ggc (splay_tree_compare_ints);
597
598   bitmap_obstack_initialize (&local_info_obstack);
599   bitmap_obstack_initialize (&global_info_obstack);
600   module_statics_escape = BITMAP_ALLOC (&local_info_obstack);
601   module_statics_written = BITMAP_ALLOC (&local_info_obstack);
602   all_module_statics = BITMAP_ALLOC (&global_info_obstack);
603
604   /* There are some shared nodes, in particular the initializers on
605      static declarations.  We do not need to scan them more than once
606      since all we would be interested in are the addressof
607      operations.  */
608   visited_nodes = pointer_set_create ();
609
610   function_insertion_hook_holder =
611       cgraph_add_function_insertion_hook (&add_new_function, NULL);
612   node_removal_hook_holder =
613       cgraph_add_node_removal_hook (&remove_node_data, NULL);
614   node_duplication_hook_holder =
615       cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
616 }
617
618 /* Check out the rhs of a static or global initialization VNODE to see
619    if any of them contain addressof operations.  Note that some of
620    these variables may  not even be referenced in the code in this
621    compilation unit but their right hand sides may contain references
622    to variables defined within this unit.  */
623
624 static void
625 analyze_variable (struct varpool_node *vnode)
626 {
627   struct walk_stmt_info wi;
628   tree global = vnode->decl;
629
630   memset (&wi, 0, sizeof (wi));
631   wi.pset = visited_nodes;
632   walk_tree (&DECL_INITIAL (global), scan_initializer_for_static_refs,
633              &wi, wi.pset);
634 }
635
636
637 /* Set up the persistent info for FN.  */
638
639 static ipa_reference_local_vars_info_t
640 init_function_info (struct cgraph_node *fn)
641 {
642   ipa_reference_vars_info_t info
643     = XCNEW (struct ipa_reference_vars_info_d);
644   ipa_reference_local_vars_info_t l
645     = XCNEW (struct ipa_reference_local_vars_info_d);
646
647   /* Add the info to the tree's annotation.  */
648   set_reference_vars_info (fn, info);
649
650   info->local = l;
651   l->statics_read = BITMAP_ALLOC (&local_info_obstack);
652   l->statics_written = BITMAP_ALLOC (&local_info_obstack);
653
654   return l;
655 }
656
657
658 /* This is the main routine for finding the reference patterns for
659    global variables within a function FN.  */
660
661 static void
662 analyze_function (struct cgraph_node *fn)
663 {
664   tree decl = fn->decl;
665   struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
666   basic_block this_block;
667 #ifdef ENABLE_CHECKING
668   tree step;
669 #endif
670   ipa_reference_local_vars_info_t local;
671
672   if (dump_file)
673     fprintf (dump_file, "\n local analysis of %s\n", cgraph_node_name (fn));
674
675   push_cfun (DECL_STRUCT_FUNCTION (decl));
676   current_function_decl = decl;
677
678   init_function_info (fn);
679   FOR_EACH_BB_FN (this_block, this_cfun)
680     {
681       gimple_stmt_iterator gsi;
682       gimple phi;
683       tree op;
684       use_operand_p use;
685       ssa_op_iter iter;
686
687       /* Find the addresses taken in phi node arguments.  */
688       for (gsi = gsi_start_phis (this_block);
689            !gsi_end_p (gsi);
690            gsi_next (&gsi))
691         {
692           phi = gsi_stmt (gsi);
693           FOR_EACH_PHI_ARG (use, phi, iter, SSA_OP_USE)
694             {
695               op = USE_FROM_PTR (use);
696               if (TREE_CODE (op) == ADDR_EXPR)
697                 mark_address_taken (get_base_var (op));
698             }
699         }
700
701       for (gsi = gsi_start_bb (this_block); !gsi_end_p (gsi); gsi_next (&gsi))
702         scan_stmt_for_static_refs (&gsi, fn);
703     }
704
705   local = get_reference_vars_info (fn)->local;
706   if ((flags_from_decl_or_type (decl) & (ECF_NOTHROW | ECF_NORETURN))
707       == (ECF_NOTHROW | ECF_NORETURN))
708     {
709       local->calls_write_all = false;
710       bitmap_clear (local->statics_written);
711     }
712
713   /* Free bitmaps of direct references if we can not use them anyway.  */
714   if (local->calls_write_all)
715     BITMAP_FREE (local->statics_written);
716   if (local->calls_read_all)
717     BITMAP_FREE (local->statics_read);
718
719
720 #ifdef ENABLE_CHECKING
721   /* Verify that all local initializers was expanded by gimplifier.  */
722   for (step = DECL_STRUCT_FUNCTION (decl)->local_decls;
723        step;
724        step = TREE_CHAIN (step))
725     {
726       tree var = TREE_VALUE (step);
727       if (TREE_CODE (var) == VAR_DECL
728           && DECL_INITIAL (var)
729           && !TREE_STATIC (var))
730         gcc_unreachable ();
731     }
732 #endif
733   pop_cfun ();
734   current_function_decl = NULL;
735 }
736
737 /* Remove local data associated with function FN.  */
738 static void
739 clean_function_local_data (struct cgraph_node *fn)
740 {
741   ipa_reference_vars_info_t info = get_reference_vars_info (fn);
742   ipa_reference_local_vars_info_t l = info->local;
743   if (l)
744     {
745       if (l->statics_read
746           && l->statics_read != all_module_statics)
747         BITMAP_FREE (l->statics_read);
748       if (l->statics_written
749           &&l->statics_written != all_module_statics)
750         BITMAP_FREE (l->statics_written);
751       free (l);
752       info->local = NULL;
753     }
754 }
755
756 /* Remove all data associated with function FN.  */
757
758 static void
759 clean_function (struct cgraph_node *fn)
760 {
761   ipa_reference_vars_info_t info = get_reference_vars_info (fn);
762   ipa_reference_global_vars_info_t g = info->global;
763
764   clean_function_local_data (fn);
765   if (g)
766     {
767       if (g->statics_read
768           && g->statics_read != all_module_statics)
769         BITMAP_FREE (g->statics_read);
770
771       if (g->statics_written
772           && g->statics_written != all_module_statics)
773         BITMAP_FREE (g->statics_written);
774
775       if (g->statics_not_read
776           && g->statics_not_read != all_module_statics)
777         BITMAP_FREE (g->statics_not_read);
778
779       if (g->statics_not_written
780           && g->statics_not_written != all_module_statics)
781         BITMAP_FREE (g->statics_not_written);
782       free (g);
783       info->global = NULL;
784     }
785
786   free (get_reference_vars_info (fn));
787   set_reference_vars_info (fn, NULL);
788 }
789
790 /* Called when new function is inserted to callgraph late.  */
791 static void
792 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
793 {
794   /* There are some shared nodes, in particular the initializers on
795      static declarations.  We do not need to scan them more than once
796      since all we would be interested in are the addressof
797      operations.  */
798   analyze_function (node);
799   visited_nodes = NULL;
800 }
801
802 static bitmap
803 copy_local_bitmap (bitmap src)
804 {
805   bitmap dst;
806   if (!src)
807     return NULL;
808   if (src == all_module_statics)
809     return all_module_statics;
810   dst = BITMAP_ALLOC (&local_info_obstack);
811   bitmap_copy (dst, src);
812   return dst;
813 }
814
815 static bitmap
816 copy_global_bitmap (bitmap src)
817 {
818   bitmap dst;
819   if (!src)
820     return NULL;
821   if (src == all_module_statics)
822     return all_module_statics;
823   dst = BITMAP_ALLOC (&global_info_obstack);
824   bitmap_copy (dst, src);
825   return dst;
826 }
827
828 /* Called when new clone is inserted to callgraph late.  */
829
830 static void
831 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
832                      void *data ATTRIBUTE_UNUSED)
833 {
834   ipa_reference_global_vars_info_t ginfo;
835   ipa_reference_local_vars_info_t linfo;
836   ipa_reference_global_vars_info_t dst_ginfo;
837   ipa_reference_local_vars_info_t dst_linfo;
838
839   ginfo = get_global_reference_vars_info (src);
840   linfo = get_local_reference_vars_info (src);
841   if (!linfo && !ginfo)
842     return;
843   init_function_info (dst);
844   if (linfo)
845     {
846       dst_linfo = get_local_reference_vars_info (dst);
847       dst_linfo->statics_read = copy_local_bitmap (linfo->statics_read);
848       dst_linfo->statics_written = copy_local_bitmap (linfo->statics_written);
849       dst_linfo->calls_read_all = linfo->calls_read_all;
850       dst_linfo->calls_write_all = linfo->calls_write_all;
851     }
852   if (ginfo)
853     {
854       get_reference_vars_info (dst)->global = XCNEW (struct ipa_reference_global_vars_info_d);
855       dst_ginfo = get_global_reference_vars_info (dst);
856       dst_ginfo->statics_read = copy_global_bitmap (ginfo->statics_read);
857       dst_ginfo->statics_written = copy_global_bitmap (ginfo->statics_written);
858       dst_ginfo->statics_not_read = copy_global_bitmap (ginfo->statics_not_read);
859       dst_ginfo->statics_not_written = copy_global_bitmap (ginfo->statics_not_written);
860     }
861 }
862
863 /* Called when node is removed.  */
864
865 static void
866 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
867 {
868   if (get_reference_vars_info (node))
869     clean_function (node);
870 }
871
872 /* Analyze each function in the cgraph to see which global or statics
873    are read or written.  */
874
875 static void
876 generate_summary (void)
877 {
878   struct cgraph_node *node;
879   struct varpool_node *vnode;
880   unsigned int index;
881   bitmap_iterator bi;
882   bitmap module_statics_readonly;
883   bitmap bm_temp;
884
885   ipa_init ();
886   module_statics_readonly = BITMAP_ALLOC (&local_info_obstack);
887   bm_temp = BITMAP_ALLOC (&local_info_obstack);
888
889   /* Process all of the variables first.  */
890   FOR_EACH_STATIC_INITIALIZER (vnode)
891     analyze_variable (vnode);
892
893   /* Process all of the functions next.
894
895      We do not want to process any of the clones so we check that this
896      is a master clone.  However, we do need to process any
897      AVAIL_OVERWRITABLE functions (these are never clones) because
898      they may cause a static variable to escape.  The code that can
899      overwrite such a function cannot access the statics because it
900      would not be in the same compilation unit.  When the analysis is
901      finished, the computed information of these AVAIL_OVERWRITABLE is
902      replaced with worst case info.
903   */
904   for (node = cgraph_nodes; node; node = node->next)
905     if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
906       analyze_function (node);
907
908   pointer_set_destroy (visited_nodes);
909   visited_nodes = NULL;
910
911   /* Prune out the variables that were found to behave badly
912      (i.e. have their address taken).  */
913   EXECUTE_IF_SET_IN_BITMAP (module_statics_escape, 0, index, bi)
914     {
915       splay_tree_remove (reference_vars_to_consider, index);
916     }
917
918   bitmap_and_compl_into (all_module_statics,
919                          module_statics_escape);
920
921   bitmap_and_compl (module_statics_readonly, all_module_statics,
922                     module_statics_written);
923
924   /* If the address is not taken, we can unset the addressable bit
925      on this variable.  */
926   EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi)
927     {
928       tree var = get_static_decl (index);
929       TREE_ADDRESSABLE (var) = 0;
930       if (dump_file)
931         fprintf (dump_file, "Not TREE_ADDRESSABLE var %s\n",
932                  get_static_name (index));
933     }
934
935   /* If the variable is never written, we can set the TREE_READONLY
936      flag.  Additionally if it has a DECL_INITIAL that is made up of
937      constants we can treat the entire global as a constant.  */
938
939   bitmap_and_compl (module_statics_readonly, all_module_statics,
940                     module_statics_written);
941   EXECUTE_IF_SET_IN_BITMAP (module_statics_readonly, 0, index, bi)
942     {
943       tree var = get_static_decl (index);
944
945       /* Ignore variables in named sections - changing TREE_READONLY
946          changes the section flags, potentially causing conflicts with
947          other variables in the same named section.  */
948       if (DECL_SECTION_NAME (var) == NULL_TREE)
949         {
950           TREE_READONLY (var) = 1;
951           if (dump_file)
952             fprintf (dump_file, "read-only var %s\n",
953                      get_static_name (index));
954         }
955     }
956
957   BITMAP_FREE(module_statics_escape);
958   BITMAP_FREE(module_statics_written);
959   module_statics_escape = NULL;
960   module_statics_written = NULL;
961
962   if (dump_file)
963     EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi)
964       {
965         fprintf (dump_file, "\nPromotable global:%s",
966                  get_static_name (index));
967       }
968
969   for (node = cgraph_nodes; node; node = node->next)
970     if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
971       {
972         ipa_reference_local_vars_info_t l;
973         l = get_reference_vars_info (node)->local;
974
975         /* Any variables that are not in all_module_statics are
976            removed from the local maps.  This will include all of the
977            variables that were found to escape in the function
978            scanning.  */
979         if (l->statics_read)
980           bitmap_and_into (l->statics_read,
981                            all_module_statics);
982         if (l->statics_written)
983           bitmap_and_into (l->statics_written,
984                            all_module_statics);
985       }
986
987   BITMAP_FREE(module_statics_readonly);
988   BITMAP_FREE(bm_temp);
989
990   if (dump_file)
991     for (node = cgraph_nodes; node; node = node->next)
992       if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
993         {
994           ipa_reference_local_vars_info_t l;
995           unsigned int index;
996           bitmap_iterator bi;
997
998           l = get_reference_vars_info (node)->local;
999           fprintf (dump_file,
1000                    "\nFunction name:%s/%i:",
1001                    cgraph_node_name (node), node->uid);
1002           fprintf (dump_file, "\n  locals read: ");
1003           if (l->statics_read)
1004             EXECUTE_IF_SET_IN_BITMAP (l->statics_read,
1005                                       0, index, bi)
1006               {
1007                 fprintf (dump_file, "%s ",
1008                          get_static_name (index));
1009               }
1010           fprintf (dump_file, "\n  locals written: ");
1011           if (l->statics_written)
1012             EXECUTE_IF_SET_IN_BITMAP (l->statics_written,
1013                                       0, index, bi)
1014               {
1015                 fprintf(dump_file, "%s ",
1016                         get_static_name (index));
1017               }
1018           if (l->calls_read_all)
1019              fprintf (dump_file, "\n  calls read all: ");
1020           if (l->calls_write_all)
1021              fprintf (dump_file, "\n  calls read all: ");
1022         }
1023 }
1024
1025
1026 /* Return true if we need to write summary of NODE. */
1027
1028 static bool
1029 write_node_summary_p (struct cgraph_node *node)
1030 {
1031   gcc_assert (node->global.inlined_to == NULL);
1032   return (node->analyzed
1033           && cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE
1034           && get_reference_vars_info (node) != NULL);
1035 }
1036
1037 /* Serialize the ipa info for lto.  */
1038
1039 static void
1040 ipa_reference_write_summary (cgraph_node_set set)
1041 {
1042   struct cgraph_node *node;
1043   struct lto_simple_output_block *ob
1044     = lto_create_simple_output_block (LTO_section_ipa_reference);
1045   unsigned int count = 0;
1046   cgraph_node_set_iterator csi;
1047
1048   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
1049     if (write_node_summary_p (csi_node (csi)))
1050         count++;
1051
1052   lto_output_uleb128_stream (ob->main_stream, count);
1053
1054   /* Process all of the functions.  */
1055   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
1056     {
1057       node = csi_node (csi);
1058       if (write_node_summary_p (node))
1059         {
1060           ipa_reference_local_vars_info_t l
1061             = get_reference_vars_info (node)->local;
1062           unsigned int index;
1063           bitmap_iterator bi;
1064           lto_cgraph_encoder_t encoder;
1065           int node_ref;
1066
1067           encoder = ob->decl_state->cgraph_node_encoder;
1068           node_ref = lto_cgraph_encoder_encode (encoder, node);
1069           lto_output_uleb128_stream (ob->main_stream, node_ref);
1070
1071           /* Stream out the statics read.  */
1072           if (l->calls_read_all)
1073             lto_output_sleb128_stream (ob->main_stream, -1);
1074           else
1075             {
1076               lto_output_sleb128_stream (ob->main_stream,
1077                                          bitmap_count_bits (l->statics_read));
1078               EXECUTE_IF_SET_IN_BITMAP (l->statics_read, 0, index, bi)
1079                 lto_output_var_decl_index(ob->decl_state, ob->main_stream,
1080                                           get_static_decl (index));
1081             }
1082
1083           /* Stream out the statics written.  */
1084           if (l->calls_write_all)
1085             lto_output_sleb128_stream (ob->main_stream, -1);
1086           else
1087             {
1088               lto_output_sleb128_stream (ob->main_stream,
1089                                          bitmap_count_bits (l->statics_written));
1090               EXECUTE_IF_SET_IN_BITMAP (l->statics_written, 0, index, bi)
1091                 lto_output_var_decl_index(ob->decl_state, ob->main_stream,
1092                                           get_static_decl (index));
1093             }
1094         }
1095     }
1096   lto_destroy_simple_output_block (ob);
1097 }
1098
1099
1100 /* Deserialize the ipa info for lto.  */
1101
1102 static void
1103 ipa_reference_read_summary (void)
1104 {
1105   struct lto_file_decl_data ** file_data_vec
1106     = lto_get_file_decl_data ();
1107   struct lto_file_decl_data * file_data;
1108   unsigned int j = 0;
1109
1110   ipa_init ();
1111
1112   while ((file_data = file_data_vec[j++]))
1113     {
1114       const char *data;
1115       size_t len;
1116       struct lto_input_block *ib
1117         = lto_create_simple_input_block (file_data,
1118                                          LTO_section_ipa_reference,
1119                                          &data, &len);
1120       if (ib)
1121         {
1122           unsigned int i;
1123           unsigned int f_count = lto_input_uleb128 (ib);
1124
1125           for (i = 0; i < f_count; i++)
1126             {
1127               unsigned int j, index;
1128               struct cgraph_node *node;
1129               ipa_reference_local_vars_info_t l;
1130               int v_count;
1131               lto_cgraph_encoder_t encoder;
1132
1133               index = lto_input_uleb128 (ib);
1134               encoder = file_data->cgraph_node_encoder;
1135               node = lto_cgraph_encoder_deref (encoder, index);
1136               l = init_function_info (node);
1137
1138               /* Set the statics read.  */
1139               v_count = lto_input_sleb128 (ib);
1140               if (v_count == -1)
1141                 l->calls_read_all = true;
1142               else
1143                 for (j = 0; j < (unsigned int)v_count; j++)
1144                   {
1145                     unsigned int var_index = lto_input_uleb128 (ib);
1146                     tree v_decl = lto_file_decl_data_get_var_decl (file_data,
1147                                                                    var_index);
1148                     add_static_var (v_decl);
1149                     bitmap_set_bit (l->statics_read, DECL_UID (v_decl));
1150                   }
1151
1152               /* Set the statics written.  */
1153               v_count = lto_input_sleb128 (ib);
1154               if (v_count == -1)
1155                 l->calls_write_all = true;
1156               else
1157                 for (j = 0; j < (unsigned int)v_count; j++)
1158                   {
1159                     unsigned int var_index = lto_input_uleb128 (ib);
1160                     tree v_decl = lto_file_decl_data_get_var_decl (file_data,
1161                                                                    var_index);
1162                     add_static_var (v_decl);
1163                     bitmap_set_bit (l->statics_written, DECL_UID (v_decl));
1164                   }
1165             }
1166
1167           lto_destroy_simple_input_block (file_data,
1168                                           LTO_section_ipa_reference,
1169                                           ib, data, len);
1170         }
1171     }
1172 }
1173
1174
1175 \f
1176 /* Set READ_ALL/WRITE_ALL based on DECL flags.  */
1177 static void
1178 read_write_all_from_decl (tree decl, bool * read_all, bool * write_all)
1179 {
1180   int flags = flags_from_decl_or_type (decl);
1181   if (flags & ECF_CONST)
1182     ;
1183   else if (flags & ECF_PURE)
1184     *read_all = true;
1185   else
1186     {
1187        /* TODO: To be able to produce sane results, we should also handle
1188           common builtins, in particular throw.
1189           Indirect calls hsould be only counted and as inliner is replacing them
1190           by direct calls, we can conclude if any indirect calls are left in body */
1191       *read_all = true;
1192       /* When function does not reutrn, it is safe to ignore anythign it writes
1193          to, because the effect will never happen.  */
1194       if ((flags & (ECF_NOTHROW | ECF_NORETURN))
1195           != (ECF_NOTHROW | ECF_NORETURN))
1196         *write_all = true;
1197     }
1198 }
1199
1200 /* Produce the global information by preforming a transitive closure
1201    on the local information that was produced by ipa_analyze_function
1202    and ipa_analyze_variable.  */
1203
1204 static unsigned int
1205 propagate (void)
1206 {
1207   struct cgraph_node *node;
1208   struct cgraph_node *w;
1209   struct cgraph_node **order =
1210     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1211   int order_pos = ipa_utils_reduced_inorder (order, false, true, NULL);
1212   int i;
1213
1214   cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1215   if (dump_file)
1216     dump_cgraph (dump_file);
1217
1218   /* Propagate the local information thru the call graph to produce
1219      the global information.  All the nodes within a cycle will have
1220      the same info so we collapse cycles first.  Then we can do the
1221      propagation in one pass from the leaves to the roots.  */
1222   order_pos = ipa_utils_reduced_inorder (order, true, true, NULL);
1223   if (dump_file)
1224     ipa_utils_print_order(dump_file, "reduced", order, order_pos);
1225
1226   for (i = 0; i < order_pos; i++ )
1227     {
1228       ipa_reference_vars_info_t node_info;
1229       ipa_reference_global_vars_info_t node_g =
1230         XCNEW (struct ipa_reference_global_vars_info_d);
1231       ipa_reference_local_vars_info_t node_l;
1232       struct cgraph_edge *e;
1233
1234       bool read_all;
1235       bool write_all;
1236       struct ipa_dfs_info * w_info;
1237
1238       node = order[i];
1239       node_info = get_reference_vars_info (node);
1240       if (!node_info)
1241         {
1242           dump_cgraph_node (stderr, node);
1243           dump_cgraph (stderr);
1244           gcc_unreachable ();
1245         }
1246
1247       gcc_assert (!node_info->global);
1248       node_l = node_info->local;
1249
1250       read_all = node_l->calls_read_all;
1251       write_all = node_l->calls_write_all;
1252
1253       /* When function is overwrittable, we can not assume anything.  */
1254       if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
1255         read_write_all_from_decl (node->decl, &read_all, &write_all);
1256
1257       for (e = node->callees; e; e = e->next_callee)
1258         if (cgraph_function_body_availability (e->callee) <= AVAIL_OVERWRITABLE)
1259           read_write_all_from_decl (e->callee->decl, &read_all, &write_all);
1260
1261
1262       /* If any node in a cycle is calls_read_all or calls_write_all
1263          they all are. */
1264       w_info = (struct ipa_dfs_info *) node->aux;
1265       w = w_info->next_cycle;
1266       while (w)
1267         {
1268           ipa_reference_local_vars_info_t w_l =
1269             get_reference_vars_info (w)->local;
1270
1271           /* When function is overwrittable, we can not assume anything.  */
1272           if (cgraph_function_body_availability (w) <= AVAIL_OVERWRITABLE)
1273             read_write_all_from_decl (w->decl, &read_all, &write_all);
1274
1275           for (e = w->callees; e; e = e->next_callee)
1276             if (cgraph_function_body_availability (e->callee) <= AVAIL_OVERWRITABLE)
1277               read_write_all_from_decl (e->callee->decl, &read_all, &write_all);
1278
1279           read_all |= w_l->calls_read_all;
1280           write_all |= w_l->calls_write_all;
1281
1282           w_info = (struct ipa_dfs_info *) w->aux;
1283           w = w_info->next_cycle;
1284         }
1285
1286
1287       /* Initialized the bitmaps for the reduced nodes */
1288       if (read_all)
1289         node_g->statics_read = all_module_statics;
1290       else
1291         {
1292           node_g->statics_read = BITMAP_ALLOC (&global_info_obstack);
1293           bitmap_copy (node_g->statics_read,
1294                        node_l->statics_read);
1295         }
1296       if (write_all)
1297         node_g->statics_written = all_module_statics;
1298       else
1299         {
1300           node_g->statics_written = BITMAP_ALLOC (&global_info_obstack);
1301           bitmap_copy (node_g->statics_written,
1302                        node_l->statics_written);
1303         }
1304
1305       propagate_bits (node_g, node);
1306       w_info = (struct ipa_dfs_info *) node->aux;
1307       w = w_info->next_cycle;
1308       while (w)
1309         {
1310           ipa_reference_vars_info_t w_ri =
1311             get_reference_vars_info (w);
1312           ipa_reference_local_vars_info_t w_l = w_ri->local;
1313
1314           /* These global bitmaps are initialized from the local info
1315              of all of the nodes in the region.  However there is no
1316              need to do any work if the bitmaps were set to
1317              all_module_statics.  */
1318           if (!read_all)
1319             bitmap_ior_into (node_g->statics_read,
1320                              w_l->statics_read);
1321           if (!write_all)
1322             bitmap_ior_into (node_g->statics_written,
1323                              w_l->statics_written);
1324           propagate_bits (node_g, w);
1325           w_info = (struct ipa_dfs_info *) w->aux;
1326           w = w_info->next_cycle;
1327         }
1328
1329       /* All nodes within a cycle have the same global info bitmaps.  */
1330       node_info->global = node_g;
1331       w_info = (struct ipa_dfs_info *) node->aux;
1332       w = w_info->next_cycle;
1333       while (w)
1334         {
1335           ipa_reference_vars_info_t w_ri =
1336             get_reference_vars_info (w);
1337
1338           gcc_assert (!w_ri->global);
1339           w_ri->global = XCNEW (struct ipa_reference_global_vars_info_d);
1340           w_ri->global->statics_read = copy_global_bitmap (node_g->statics_read);
1341           w_ri->global->statics_written = copy_global_bitmap (node_g->statics_written);
1342
1343           w_info = (struct ipa_dfs_info *) w->aux;
1344           w = w_info->next_cycle;
1345         }
1346     }
1347
1348   if (dump_file)
1349     {
1350       for (i = 0; i < order_pos; i++ )
1351         {
1352           ipa_reference_vars_info_t node_info;
1353           ipa_reference_global_vars_info_t node_g;
1354           ipa_reference_local_vars_info_t node_l;
1355           unsigned int index;
1356           bitmap_iterator bi;
1357           struct ipa_dfs_info * w_info;
1358
1359           node = order[i];
1360           node_info = get_reference_vars_info (node);
1361           node_g = node_info->global;
1362           node_l = node_info->local;
1363           fprintf (dump_file,
1364                    "\nFunction name:%s/%i:",
1365                    cgraph_node_name (node), node->uid);
1366           fprintf (dump_file, "\n  locals read: ");
1367           if (node_l->statics_read)
1368             EXECUTE_IF_SET_IN_BITMAP (node_l->statics_read,
1369                                       0, index, bi)
1370               {
1371                 fprintf (dump_file, "%s ",
1372                          get_static_name (index));
1373               }
1374           fprintf (dump_file, "\n  locals written: ");
1375           if (node_l->statics_written)
1376             EXECUTE_IF_SET_IN_BITMAP (node_l->statics_written,
1377                                       0, index, bi)
1378               {
1379                 fprintf(dump_file, "%s ",
1380                         get_static_name (index));
1381               }
1382
1383           w_info = (struct ipa_dfs_info *) node->aux;
1384           w = w_info->next_cycle;
1385           while (w)
1386             {
1387               ipa_reference_vars_info_t w_ri =
1388                 get_reference_vars_info (w);
1389               ipa_reference_local_vars_info_t w_l = w_ri->local;
1390               fprintf (dump_file, "\n  next cycle: %s/%i ",
1391                        cgraph_node_name (w), w->uid);
1392               fprintf (dump_file, "\n    locals read: ");
1393               if (w_l->statics_read)
1394                 EXECUTE_IF_SET_IN_BITMAP (w_l->statics_read,
1395                                           0, index, bi)
1396                   {
1397                     fprintf (dump_file, "%s ",
1398                              get_static_name (index));
1399                   }
1400
1401               fprintf (dump_file, "\n    locals written: ");
1402               if (w_l->statics_written)
1403                 EXECUTE_IF_SET_IN_BITMAP (w_l->statics_written,
1404                                           0, index, bi)
1405                   {
1406                     fprintf (dump_file, "%s ",
1407                              get_static_name (index));
1408                   }
1409
1410               w_info = (struct ipa_dfs_info *) w->aux;
1411               w = w_info->next_cycle;
1412             }
1413           fprintf (dump_file, "\n  globals read: ");
1414           if (node_g->statics_read == all_module_statics)
1415             fprintf (dump_file, "ALL");
1416           else
1417             EXECUTE_IF_SET_IN_BITMAP (node_g->statics_read,
1418                                       0, index, bi)
1419               {
1420                 fprintf (dump_file, "%s ",
1421                          get_static_name (index));
1422               }
1423           fprintf (dump_file, "\n  globals written: ");
1424           if (node_g->statics_written == all_module_statics)
1425             fprintf (dump_file, "ALL");
1426           else
1427             EXECUTE_IF_SET_IN_BITMAP (node_g->statics_written,
1428                                       0, index, bi)
1429               {
1430                 fprintf (dump_file, "%s ",
1431                          get_static_name (index));
1432               }
1433         }
1434     }
1435
1436   /* Cleanup. */
1437   for (i = 0; i < order_pos; i++ )
1438     {
1439       ipa_reference_vars_info_t node_info;
1440       ipa_reference_global_vars_info_t node_g;
1441       node = order[i];
1442       node_info = get_reference_vars_info (node);
1443       node_g = node_info->global;
1444
1445       /* Create the complimentary sets.  These are more useful for
1446          certain apis.  */
1447       node_g->statics_not_read = BITMAP_ALLOC (&global_info_obstack);
1448       node_g->statics_not_written = BITMAP_ALLOC (&global_info_obstack);
1449
1450       if (node_g->statics_read != all_module_statics)
1451         bitmap_and_compl (node_g->statics_not_read,
1452                           all_module_statics,
1453                           node_g->statics_read);
1454
1455       if (node_g->statics_written
1456           != all_module_statics)
1457         bitmap_and_compl (node_g->statics_not_written,
1458                           all_module_statics,
1459                           node_g->statics_written);
1460    }
1461
1462   free (order);
1463
1464   for (node = cgraph_nodes; node; node = node->next)
1465     {
1466       ipa_reference_vars_info_t node_info;
1467       node_info = get_reference_vars_info (node);
1468       /* Get rid of the aux information.  */
1469
1470       if (node->aux)
1471         {
1472           free (node->aux);
1473           node->aux = NULL;
1474         }
1475
1476       if (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE)
1477         clean_function (node);
1478       else if (node_info)
1479         clean_function_local_data (node);
1480     }
1481   bitmap_obstack_release (&local_info_obstack);
1482   return 0;
1483 }
1484
1485
1486 static bool
1487 gate_reference (void)
1488 {
1489   return (flag_ipa_reference
1490           /* Don't bother doing anything if the program has errors.  */
1491           && !(errorcount || sorrycount));
1492 }
1493
1494 struct ipa_opt_pass_d pass_ipa_reference =
1495 {
1496  {
1497   IPA_PASS,
1498   "static-var",                         /* name */
1499   gate_reference,                       /* gate */
1500   propagate,                            /* execute */
1501   NULL,                                 /* sub */
1502   NULL,                                 /* next */
1503   0,                                    /* static_pass_number */
1504   TV_IPA_REFERENCE,                     /* tv_id */
1505   0,                                    /* properties_required */
1506   0,                                    /* properties_provided */
1507   0,                                    /* properties_destroyed */
1508   0,                                    /* todo_flags_start */
1509   0                                     /* todo_flags_finish */
1510  },
1511  generate_summary,                      /* generate_summary */
1512  ipa_reference_write_summary,           /* write_summary */
1513  ipa_reference_read_summary,            /* read_summary */
1514  NULL,                                  /* function_read_summary */
1515  NULL,                                  /* stmt_fixup */
1516  0,                                     /* TODOs */
1517  NULL,                                  /* function_transform */
1518  NULL                                   /* variable_transform */
1519 };
1520
1521 #include "gt-ipa-reference.h"