OSDN Git Service

5e1a6ebbaeb0a92d294823f961e40b12df96846f
[pf3gnuchains/gcc-fork.git] / gcc / ipa-reference.c
1 /* Callgraph based analysis of static variables.
2    Copyright (C) 2004, 2005, 2007 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
51 #include "config.h"
52 #include "system.h"
53 #include "coretypes.h"
54 #include "tm.h"
55 #include "tree.h"
56 #include "tree-flow.h"
57 #include "tree-inline.h"
58 #include "tree-pass.h"
59 #include "langhooks.h"
60 #include "pointer-set.h"
61 #include "ggc.h"
62 #include "ipa-utils.h"
63 #include "ipa-reference.h"
64 #include "c-common.h"
65 #include "tree-gimple.h"
66 #include "cgraph.h"
67 #include "output.h"
68 #include "flags.h"
69 #include "timevar.h"
70 #include "diagnostic.h"
71 #include "langhooks.h"
72
73 /* This splay tree contains all of the static variables that are
74    being considered by the compilation level alias analysis.  For
75    module_at_a_time compilation, this is the set of static but not
76    public variables.  Any variables that either have their address
77    taken or participate in otherwise unsavory operations are deleted
78    from this list.  */
79 static GTY((param1_is(int), param2_is(tree)))
80      splay_tree reference_vars_to_consider;
81
82 /* This bitmap is used to knock out the module static variables whose
83    addresses have been taken and passed around.  */
84 static bitmap module_statics_escape;
85
86 /* This bitmap is used to knock out the module static variables that
87    are not readonly.  */
88 static bitmap module_statics_written;
89
90 /* A bit is set for every module static we are considering.  This is
91    ored into the local info when asm code is found that clobbers all
92    memory. */
93 static bitmap all_module_statics;
94
95 static struct pointer_set_t *visited_nodes;
96
97 static bitmap_obstack ipa_obstack;
98
99 enum initialization_status_t
100 {
101   UNINITIALIZED,
102   RUNNING,
103   FINISHED
104 };
105
106 tree memory_identifier_string;
107
108 /* Return the ipa_reference_vars structure starting from the cgraph NODE.  */
109 static inline ipa_reference_vars_info_t
110 get_reference_vars_info_from_cgraph (struct cgraph_node * node)
111 {
112   return get_function_ann (node->decl)->reference_vars_info;
113 }
114
115 /* Get a bitmap that contains all of the locally referenced static
116    variables for function FN.  */
117 static ipa_reference_local_vars_info_t
118 get_local_reference_vars_info (tree fn) 
119 {
120   ipa_reference_vars_info_t info = get_function_ann (fn)->reference_vars_info;
121
122   if (info)
123     return info->local;
124   else
125     /* This phase was not run.  */ 
126     return NULL;
127 }
128
129 /* Get a bitmap that contains all of the globally referenced static
130    variables for function FN.  */
131  
132 static ipa_reference_global_vars_info_t
133 get_global_reference_vars_info (tree fn) 
134 {
135   ipa_reference_vars_info_t info = get_function_ann (fn)->reference_vars_info;
136
137   if (info)
138     return info->global;
139   else
140     /* This phase was not run.  */ 
141     return NULL;
142 }
143
144 /* Return a bitmap indexed by VAR_DECL uid for the static variables
145    that may be read locally by the execution of the function fn.
146    Returns NULL if no data is available.  */
147
148 bitmap 
149 ipa_reference_get_read_local (tree fn)
150 {
151   ipa_reference_local_vars_info_t l = get_local_reference_vars_info (fn);
152   if (l) 
153     return l->statics_read;
154   else
155     return NULL;
156 }
157
158 /* Return a bitmap indexed by VAR_DECL uid for the static variables
159    that may be written locally by the execution of the function fn.
160    Returns NULL if no data is available.  */
161
162 bitmap 
163 ipa_reference_get_written_local (tree fn)
164 {
165   ipa_reference_local_vars_info_t l = get_local_reference_vars_info (fn);
166   if (l) 
167     return l->statics_written;
168   else
169     return NULL;
170 }
171
172 /* Return a bitmap indexed by VAR_DECL uid for the static variables
173    that are read during the execution of the function FN.  Returns
174    NULL if no data is available.  */
175
176 bitmap 
177 ipa_reference_get_read_global (tree fn) 
178 {
179   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
180   if (g) 
181     return g->statics_read;
182   else
183     return NULL;
184 }
185
186 /* Return a bitmap indexed by VAR_DECL uid for the static variables
187    that are written during the execution of the function FN.  Note
188    that variables written may or may not be read during the function
189    call.  Returns NULL if no data is available.  */
190
191 bitmap 
192 ipa_reference_get_written_global (tree fn) 
193 {
194   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
195   if (g) 
196     return g->statics_written;
197   else
198     return NULL;
199 }
200
201 /* Return a bitmap indexed by_DECL_UID uid for the static variables
202    that are not read during the execution of the function FN.  Returns
203    NULL if no data is available.  */
204
205 bitmap 
206 ipa_reference_get_not_read_global (tree fn) 
207 {
208   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
209   if (g) 
210     return g->statics_not_read;
211   else
212     return NULL;
213 }
214
215 /* Return a bitmap indexed by DECL_UID uid for the static variables
216    that are not written during the execution of the function FN.  Note
217    that variables written may or may not be read during the function
218    call.  Returns NULL if no data is available.  */
219
220 bitmap 
221 ipa_reference_get_not_written_global (tree fn) 
222 {
223   ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
224   if (g) 
225     return g->statics_not_written;
226   else
227     return NULL;
228 }
229
230 \f
231
232 /* Add VAR to all_module_statics and the two
233    reference_vars_to_consider* sets.  */
234
235 static inline void 
236 add_static_var (tree var) 
237 {
238   int uid = DECL_UID (var);
239   if (!bitmap_bit_p (all_module_statics, uid))
240     {
241       splay_tree_insert (reference_vars_to_consider,
242                          uid, (splay_tree_value)var);
243       bitmap_set_bit (all_module_statics, uid);
244     }
245 }
246
247 /* Return true if the variable T is the right kind of static variable to
248    perform compilation unit scope escape analysis.  */
249
250 static inline bool 
251 has_proper_scope_for_analysis (tree t)
252 {
253   /* If the variable has the "used" attribute, treat it as if it had a
254      been touched by the devil.  */
255   if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
256     return false;
257
258   /* Do not want to do anything with volatile except mark any
259      function that uses one to be not const or pure.  */
260   if (TREE_THIS_VOLATILE (t)) 
261     return false;
262
263   /* Do not care about a local automatic that is not static.  */
264   if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
265     return false;
266
267   if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
268     return false;
269
270   /* This is a variable we care about.  Check if we have seen it
271      before, and if not add it the set of variables we care about.  */
272   if (!bitmap_bit_p (all_module_statics, DECL_UID (t)))
273     add_static_var (t);
274
275   return true;
276 }
277
278 /* If T is a VAR_DECL for a static that we are interested in, add the
279    uid to the bitmap.  */
280
281 static void
282 check_operand (ipa_reference_local_vars_info_t local, 
283                tree t, bool checking_write)
284 {
285   if (!t) return;
286
287   if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
288       && (has_proper_scope_for_analysis (t))) 
289     {
290       if (checking_write)
291         {
292           if (local)
293             bitmap_set_bit (local->statics_written, DECL_UID (t));
294           /* Mark the write so we can tell which statics are
295              readonly.  */
296           bitmap_set_bit (module_statics_written, DECL_UID (t));
297         }
298       else if (local)
299         bitmap_set_bit (local->statics_read, DECL_UID (t));
300     }
301 }
302
303 /* Examine tree T for references to static variables. All internal
304    references like array references or indirect references are added
305    to the READ_BM. Direct references are added to either READ_BM or
306    WRITE_BM depending on the value of CHECKING_WRITE.   */
307
308 static void
309 check_tree (ipa_reference_local_vars_info_t local, tree t, bool checking_write)
310 {
311   if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
312     return;
313
314   while (TREE_CODE (t) == REALPART_EXPR 
315          || TREE_CODE (t) == IMAGPART_EXPR
316          || handled_component_p (t))
317     {
318       if (TREE_CODE (t) == ARRAY_REF)
319         check_operand (local, TREE_OPERAND (t, 1), false);
320       t = TREE_OPERAND (t, 0);
321     }
322
323   /* The bottom of an indirect reference can only be read, not
324      written.  So just recurse and whatever we find, check it against
325      the read bitmaps.  */
326
327   /*  if (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF) */
328   /* FIXME when we have array_ref's of pointers.  */
329   if (INDIRECT_REF_P (t))
330     check_tree (local, TREE_OPERAND (t, 0), false);
331
332   if (SSA_VAR_P (t))
333     check_operand (local, t, checking_write);
334 }
335
336 /* Scan tree T to see if there are any addresses taken in within T.  */
337
338 static void 
339 look_for_address_of (tree t)
340 {
341   if (TREE_CODE (t) == ADDR_EXPR)
342     {
343       tree x = get_base_var (t);
344       if (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == FUNCTION_DECL) 
345         if (has_proper_scope_for_analysis (x))
346           bitmap_set_bit (module_statics_escape, DECL_UID (x));
347     }
348 }
349
350 /* Check to see if T is a read or address of operation on a static var
351    we are interested in analyzing.  LOCAL is passed in to get access
352    to its bit vectors.  Local is NULL if this is called from a static
353    initializer.  */
354
355 static void
356 check_rhs_var (ipa_reference_local_vars_info_t local, tree t)
357 {
358   look_for_address_of (t);
359
360   if (local == NULL) 
361     return;
362
363   check_tree(local, t, false);
364 }
365
366 /* Check to see if T is an assignment to a static var we are
367    interested in analyzing.  LOCAL is passed in to get access to its bit
368    vectors.  */
369
370 static void
371 check_lhs_var (ipa_reference_local_vars_info_t local, tree t)
372 {
373   if (local == NULL) 
374     return;
375    
376   check_tree(local, t, true);
377 }
378
379 /* This is a scaled down version of get_asm_expr_operands from
380    tree_ssa_operands.c.  The version there runs much later and assumes
381    that aliasing information is already available. Here we are just
382    trying to find if the set of inputs and outputs contain references
383    or address of operations to local static variables.  FN is the
384    function being analyzed and STMT is the actual asm statement.  */
385
386 static void
387 get_asm_expr_operands (ipa_reference_local_vars_info_t local, tree stmt)
388 {
389   int noutputs = list_length (ASM_OUTPUTS (stmt));
390   const char **oconstraints
391     = (const char **) alloca ((noutputs) * sizeof (const char *));
392   int i;
393   tree link;
394   const char *constraint;
395   bool allows_mem, allows_reg, is_inout;
396   
397   for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
398     {
399       oconstraints[i] = constraint
400         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
401       parse_output_constraint (&constraint, i, 0, 0,
402                                &allows_mem, &allows_reg, &is_inout);
403       
404       check_lhs_var (local, TREE_VALUE (link));
405     }
406
407   for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
408     {
409       constraint
410         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
411       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
412                               oconstraints, &allows_mem, &allows_reg);
413       
414       check_rhs_var (local, TREE_VALUE (link));
415     }
416   
417   for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
418     if (simple_cst_equal(TREE_VALUE (link), memory_identifier_string) == 1) 
419       {
420         /* Abandon all hope, ye who enter here. */
421         local->calls_read_all = true;
422         local->calls_write_all = true;
423       }      
424 }
425
426 /* Check the parameters of a function call from CALLER to CALL_EXPR to
427    see if any of them are static vars.  Also check to see if this is
428    either an indirect call, a call outside the compilation unit, or
429    has special attributes that effect the clobbers.  The caller
430    parameter is the tree node for the caller and the second operand is
431    the tree node for the entire call expression.  */
432
433 static void
434 check_call (ipa_reference_local_vars_info_t local, tree call_expr) 
435 {
436   int flags = call_expr_flags (call_expr);
437   tree operand;
438   tree callee_t = get_callee_fndecl (call_expr);
439   enum availability avail = AVAIL_NOT_AVAILABLE;
440   call_expr_arg_iterator iter;
441
442   FOR_EACH_CALL_EXPR_ARG (operand, iter, call_expr)
443     check_rhs_var (local, operand);
444
445   if (callee_t)
446     {
447       struct cgraph_node* callee = cgraph_node(callee_t);
448       avail = cgraph_function_body_availability (callee);
449     }
450
451   if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
452     if (local) 
453       {
454         if (flags & ECF_PURE) 
455           local->calls_read_all = true;
456         else 
457           {
458             local->calls_read_all = true;
459             local->calls_write_all = true;
460           }
461       }
462 }
463
464 /* TP is the part of the tree currently under the microscope.
465    WALK_SUBTREES is part of the walk_tree api but is unused here.
466    DATA is cgraph_node of the function being walked.  */
467
468 /* FIXME: When this is converted to run over SSA form, this code
469    should be converted to use the operand scanner.  */
470
471 static tree
472 scan_for_static_refs (tree *tp, 
473                       int *walk_subtrees, 
474                       void *data)
475 {
476   struct cgraph_node *fn = (struct cgraph_node *) data;
477   tree t = *tp;
478   ipa_reference_local_vars_info_t local = NULL;
479   if (fn)
480     local = get_reference_vars_info_from_cgraph (fn)->local;
481
482   switch (TREE_CODE (t))  
483     {
484     case VAR_DECL:
485       if (DECL_INITIAL (t))
486         walk_tree (&DECL_INITIAL (t), scan_for_static_refs, fn, visited_nodes);
487       *walk_subtrees = 0;
488       break;
489
490     case GIMPLE_MODIFY_STMT:
491       {
492         /* First look on the lhs and see what variable is stored to */
493         tree lhs = GIMPLE_STMT_OPERAND (t, 0);
494         tree rhs = GIMPLE_STMT_OPERAND (t, 1);
495         check_lhs_var (local, lhs);
496
497         /* For the purposes of figuring out what the cast affects */
498
499         /* Next check the operands on the rhs to see if they are ok. */
500         switch (TREE_CODE_CLASS (TREE_CODE (rhs))) 
501           {
502           case tcc_binary:          
503           case tcc_comparison:      
504             {
505               tree op0 = TREE_OPERAND (rhs, 0);
506               tree op1 = TREE_OPERAND (rhs, 1);
507               check_rhs_var (local, op0);
508               check_rhs_var (local, op1);
509             }
510             break;
511           case tcc_unary:
512             {
513               tree op0 = TREE_OPERAND (rhs, 0);
514               check_rhs_var (local, op0);
515             }
516
517             break;
518           case tcc_reference:
519             check_rhs_var (local, rhs);
520             break;
521           case tcc_declaration:
522             check_rhs_var (local, rhs);
523             break;
524           case tcc_expression:
525             switch (TREE_CODE (rhs)) 
526               {
527               case ADDR_EXPR:
528                 check_rhs_var (local, rhs);
529                 break;
530               default:
531                 break;
532               }
533             break;
534           case tcc_vl_exp:
535             switch (TREE_CODE (rhs))
536               {
537               case CALL_EXPR:
538                 check_call (local, rhs);
539                 break;
540               default:
541                 break;
542               }
543             break;
544           default:
545             break;
546           }
547         *walk_subtrees = 0;
548       }
549       break;
550
551     case ADDR_EXPR:
552       /* This case is here to find addresses on rhs of constructors in
553          decl_initial of static variables. */
554       check_rhs_var (local, t);
555       *walk_subtrees = 0;
556       break;
557
558     case LABEL_EXPR:
559       if (DECL_NONLOCAL (TREE_OPERAND (t, 0)))
560         {
561           /* Target of long jump. */
562           local->calls_read_all = true;
563           local->calls_write_all = true;
564         }
565       break;
566
567     case CALL_EXPR: 
568       check_call (local, t);
569       *walk_subtrees = 0;
570       break;
571       
572     case ASM_EXPR:
573       get_asm_expr_operands (local, t);
574       *walk_subtrees = 0;
575       break;
576       
577     default:
578       break;
579     }
580   return NULL;
581 }
582
583
584 /* Lookup the tree node for the static variable that has UID.  */
585 static tree
586 get_static_decl (int index)
587 {
588   splay_tree_node stn = 
589     splay_tree_lookup (reference_vars_to_consider, index);
590   if (stn)
591     return (tree)stn->value;
592   return NULL;
593 }
594
595 /* Lookup the tree node for the static variable that has UID and
596    convert the name to a string for debugging.  */
597
598 static const char *
599 get_static_name (int index)
600 {
601   splay_tree_node stn = 
602     splay_tree_lookup (reference_vars_to_consider, index);
603   if (stn)
604     return lang_hooks.decl_printable_name ((tree)(stn->value), 2);
605   return NULL;
606 }
607
608 /* Or in all of the bits from every callee into X, the caller's, bit
609    vector.  There are several cases to check to avoid the sparse
610    bitmap oring.  */
611
612 static void
613 propagate_bits (struct cgraph_node *x)
614 {
615   ipa_reference_vars_info_t x_info = get_reference_vars_info_from_cgraph (x);
616   ipa_reference_global_vars_info_t x_global = x_info->global;
617
618   struct cgraph_edge *e;
619   for (e = x->callees; e; e = e->next_callee) 
620     {
621       struct cgraph_node *y = e->callee;
622
623       /* Only look at the master nodes and skip external nodes.  */
624       y = cgraph_master_clone (y);
625       if (y)
626         {
627           if (get_reference_vars_info_from_cgraph (y))
628             {
629               ipa_reference_vars_info_t y_info = get_reference_vars_info_from_cgraph (y);
630               ipa_reference_global_vars_info_t y_global = y_info->global;
631               
632               if (x_global->statics_read
633                   != all_module_statics)
634                 {
635                   if (y_global->statics_read 
636                       == all_module_statics)
637                     {
638                       BITMAP_FREE (x_global->statics_read);
639                       x_global->statics_read 
640                         = all_module_statics;
641                     }
642                   /* Skip bitmaps that are pointer equal to node's bitmap
643                      (no reason to spin within the cycle).  */
644                   else if (x_global->statics_read 
645                            != y_global->statics_read)
646                     bitmap_ior_into (x_global->statics_read,
647                                      y_global->statics_read);
648                 }
649               
650               if (x_global->statics_written 
651                   != all_module_statics)
652                 {
653                   if (y_global->statics_written 
654                       == all_module_statics)
655                     {
656                       BITMAP_FREE (x_global->statics_written);
657                       x_global->statics_written 
658                         = all_module_statics;
659                     }
660                   /* Skip bitmaps that are pointer equal to node's bitmap
661                      (no reason to spin within the cycle).  */
662                   else if (x_global->statics_written 
663                            != y_global->statics_written)
664                     bitmap_ior_into (x_global->statics_written,
665                                      y_global->statics_written);
666                 }
667             }
668           else 
669             {
670               gcc_unreachable ();
671             }
672         }
673     }
674 }
675
676 /* Look at all of the callees of X to see which ones represent inlined
677    calls.  For each of these callees, merge their local info into
678    TARGET and check their children recursively.  
679
680    This function goes away when Jan changes the inliner and IPA
681    analysis so that this is not run between the time when inlining
682    decisions are made and when the inlining actually occurs.  */
683
684 static void 
685 merge_callee_local_info (struct cgraph_node *target, 
686                          struct cgraph_node *x)
687 {
688   struct cgraph_edge *e;
689   ipa_reference_local_vars_info_t x_l = 
690     get_reference_vars_info_from_cgraph (target)->local;
691
692   /* Make the world safe for tail recursion.  */
693   struct ipa_dfs_info *node_info = (struct ipa_dfs_info *) x->aux;
694   
695   if (node_info->aux) 
696     return;
697
698   node_info->aux = x;
699
700   for (e = x->callees; e; e = e->next_callee) 
701     {
702       struct cgraph_node *y = e->callee;
703       if (y->global.inlined_to) 
704         {
705           ipa_reference_vars_info_t y_info;
706           ipa_reference_local_vars_info_t y_l;
707           struct cgraph_node* orig_y = y;
708          
709           y = cgraph_master_clone (y);
710           if (y)
711             {
712               y_info = get_reference_vars_info_from_cgraph (y);
713               y_l = y_info->local;
714               if (x_l != y_l)
715                 {
716                   bitmap_ior_into (x_l->statics_read,
717                                    y_l->statics_read);
718                   bitmap_ior_into (x_l->statics_written,
719                                    y_l->statics_written);
720                 }
721               x_l->calls_read_all |= y_l->calls_read_all;
722               x_l->calls_write_all |= y_l->calls_write_all;
723               merge_callee_local_info (target, y);
724             }
725           else 
726             {
727               fprintf(stderr, "suspect inlining of ");
728               dump_cgraph_node (stderr, orig_y);
729               fprintf(stderr, "\ninto ");
730               dump_cgraph_node (stderr, target);
731               dump_cgraph (stderr);
732               gcc_assert(false);
733             }
734         }
735     }
736
737   node_info->aux = NULL;
738 }
739
740 /* The init routine for analyzing global static variable usage.  See
741    comments at top for description.  */
742 static void 
743 ipa_init (void) 
744 {
745   struct cgraph_node *node;
746   memory_identifier_string = build_string(7, "memory");
747
748   reference_vars_to_consider =
749     splay_tree_new_ggc (splay_tree_compare_ints);
750
751   bitmap_obstack_initialize (&ipa_obstack);
752   module_statics_escape = BITMAP_ALLOC (&ipa_obstack);
753   module_statics_written = BITMAP_ALLOC (&ipa_obstack);
754   all_module_statics = BITMAP_ALLOC (&ipa_obstack);
755
756   /* This will add NODE->DECL to the splay trees.  */
757   for (node = cgraph_nodes; node; node = node->next)
758     has_proper_scope_for_analysis (node->decl);
759
760   /* There are some shared nodes, in particular the initializers on
761      static declarations.  We do not need to scan them more than once
762      since all we would be interested in are the addressof
763      operations.  */
764   visited_nodes = pointer_set_create ();
765 }
766
767 /* Check out the rhs of a static or global initialization VNODE to see
768    if any of them contain addressof operations.  Note that some of
769    these variables may  not even be referenced in the code in this
770    compilation unit but their right hand sides may contain references
771    to variables defined within this unit.  */
772
773 static void 
774 analyze_variable (struct varpool_node *vnode)
775 {
776   tree global = vnode->decl;
777   walk_tree (&DECL_INITIAL (global), scan_for_static_refs, 
778              NULL, visited_nodes);
779 }
780
781 /* This is the main routine for finding the reference patterns for
782    global variables within a function FN.  */
783
784 static void
785 analyze_function (struct cgraph_node *fn)
786 {
787   ipa_reference_vars_info_t info 
788     = XCNEW (struct ipa_reference_vars_info_d);
789   ipa_reference_local_vars_info_t l
790     = XCNEW (struct ipa_reference_local_vars_info_d);
791   tree decl = fn->decl;
792
793   /* Add the info to the tree's annotation.  */
794   get_function_ann (fn->decl)->reference_vars_info = info;
795
796   info->local = l;
797   l->statics_read = BITMAP_ALLOC (&ipa_obstack);
798   l->statics_written = BITMAP_ALLOC (&ipa_obstack);
799
800   if (dump_file)
801     fprintf (dump_file, "\n local analysis of %s\n", cgraph_node_name (fn));
802   
803   {
804     struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
805     basic_block this_block;
806
807     FOR_EACH_BB_FN (this_block, this_cfun)
808       {
809         block_stmt_iterator bsi;
810         tree phi, op;
811         use_operand_p use;
812         ssa_op_iter iter;
813
814         /* Find the addresses taken in phi node arguments.  */
815         for (phi = phi_nodes (this_block); phi; phi = PHI_CHAIN (phi))
816           {
817             FOR_EACH_PHI_ARG (use, phi, iter, SSA_OP_USE)
818               {
819                 op = USE_FROM_PTR (use);
820                 if (TREE_CODE (op) == ADDR_EXPR)
821                   check_rhs_var (l, op);
822               }
823           }
824
825         for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
826           walk_tree (bsi_stmt_ptr (bsi), scan_for_static_refs, 
827                      fn, visited_nodes);
828       }
829   }
830
831   /* There may be const decls with interesting right hand sides.  */
832   if (DECL_STRUCT_FUNCTION (decl))
833     {
834       tree step;
835       for (step = DECL_STRUCT_FUNCTION (decl)->unexpanded_var_list;
836            step;
837            step = TREE_CHAIN (step))
838         {
839           tree var = TREE_VALUE (step);
840           if (TREE_CODE (var) == VAR_DECL 
841               && DECL_INITIAL (var)
842               && !TREE_STATIC (var))
843             walk_tree (&DECL_INITIAL (var), scan_for_static_refs, 
844                        fn, visited_nodes);
845         }
846     }
847 }
848
849 /* If FN is avail == AVAIL_OVERWRITABLE, replace the effects bit
850    vectors with worst case bit vectors.  We had to analyze it above to
851    find out if it took the address of any statics. However, now that
852    we know that, we can get rid of all of the other side effects.  */
853
854 static void
855 clean_function (struct cgraph_node *fn)
856 {
857   ipa_reference_vars_info_t info = get_reference_vars_info_from_cgraph (fn);
858   ipa_reference_local_vars_info_t l = info->local;
859   ipa_reference_global_vars_info_t g = info->global;
860   
861   if (l)
862     {
863       if (l->statics_read
864           && l->statics_read != all_module_statics)
865         BITMAP_FREE (l->statics_read);
866       if (l->statics_written
867           &&l->statics_written != all_module_statics)
868         BITMAP_FREE (l->statics_written);
869       free (l);
870     }
871   
872   if (g)
873     {
874       if (g->statics_read
875           && g->statics_read != all_module_statics)
876         BITMAP_FREE (g->statics_read);
877       
878       if (g->statics_written
879           && g->statics_written != all_module_statics)
880         BITMAP_FREE (g->statics_written);
881       
882       if (g->statics_not_read
883           && g->statics_not_read != all_module_statics)
884         BITMAP_FREE (g->statics_not_read);
885       
886       if (g->statics_not_written
887           && g->statics_not_written != all_module_statics)
888         BITMAP_FREE (g->statics_not_written);
889       free (g);
890     }
891
892   
893   free (get_function_ann (fn->decl)->reference_vars_info);
894   get_function_ann (fn->decl)->reference_vars_info = NULL;
895 }
896
897 \f
898 /* Produce the global information by preforming a transitive closure
899    on the local information that was produced by ipa_analyze_function
900    and ipa_analyze_variable.  */
901
902 static unsigned int
903 static_execute (void)
904 {
905   struct cgraph_node *node;
906   struct varpool_node *vnode;
907   struct cgraph_node *w;
908   struct cgraph_node **order =
909     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
910   int order_pos = ipa_utils_reduced_inorder (order, false, true);
911   int i;
912
913   ipa_init ();
914
915   /* Process all of the variables first.  */
916   FOR_EACH_STATIC_INITIALIZER (vnode)
917     analyze_variable (vnode);
918
919   /* Process all of the functions next. 
920
921      We do not want to process any of the clones so we check that this
922      is a master clone.  However, we do need to process any
923      AVAIL_OVERWRITABLE functions (these are never clones) because
924      they may cause a static variable to escape.  The code that can
925      overwrite such a function cannot access the statics because it
926      would not be in the same compilation unit.  When the analysis is
927      finished, the computed information of these AVAIL_OVERWRITABLE is
928      replaced with worst case info.  
929   */
930   for (node = cgraph_nodes; node; node = node->next)
931     if (node->analyzed 
932         && (cgraph_is_master_clone (node)
933             || (cgraph_function_body_availability (node) 
934                 == AVAIL_OVERWRITABLE)))
935       analyze_function (node);
936
937   pointer_set_destroy (visited_nodes);
938   visited_nodes = NULL;
939   if (dump_file) 
940     dump_cgraph (dump_file);
941
942   /* Prune out the variables that were found to behave badly
943      (i.e. have their address taken).  */
944   {
945     unsigned int index;
946     bitmap_iterator bi;
947     bitmap module_statics_readonly = BITMAP_ALLOC (&ipa_obstack);
948     bitmap module_statics_const = BITMAP_ALLOC (&ipa_obstack);
949     bitmap bm_temp = BITMAP_ALLOC (&ipa_obstack);
950
951     EXECUTE_IF_SET_IN_BITMAP (module_statics_escape, 0, index, bi)
952       {
953         splay_tree_remove (reference_vars_to_consider, index);
954       }
955
956     bitmap_and_compl_into (all_module_statics, 
957                            module_statics_escape);
958
959     bitmap_and_compl (module_statics_readonly, all_module_statics,
960                       module_statics_written);
961
962     /* If the address is not taken, we can unset the addressable bit
963        on this variable.  */
964     EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi)
965       {
966         tree var = get_static_decl (index);
967         TREE_ADDRESSABLE (var) = 0;
968         if (dump_file) 
969           fprintf (dump_file, "Not TREE_ADDRESSABLE var %s\n",
970                    get_static_name (index));
971       }
972
973     /* If the variable is never written, we can set the TREE_READONLY
974        flag.  Additionally if it has a DECL_INITIAL that is made up of
975        constants we can treat the entire global as a constant.  */
976
977     bitmap_and_compl (module_statics_readonly, all_module_statics,
978                       module_statics_written);
979     EXECUTE_IF_SET_IN_BITMAP (module_statics_readonly, 0, index, bi)
980       {
981         tree var = get_static_decl (index);
982
983         /* Readonly on a function decl is very different from the
984            variable.  */
985         if (TREE_CODE (var) == FUNCTION_DECL)
986           continue;
987
988         /* Ignore variables in named sections - changing TREE_READONLY
989            changes the section flags, potentially causing conflicts with
990            other variables in the same named section.  */
991         if (DECL_SECTION_NAME (var) == NULL_TREE)
992           {
993             TREE_READONLY (var) = 1;
994             if (dump_file)
995               fprintf (dump_file, "read-only var %s\n", 
996                        get_static_name (index));
997           }
998         if (DECL_INITIAL (var)
999             && is_gimple_min_invariant (DECL_INITIAL (var)))
1000           {
1001             bitmap_set_bit (module_statics_const, index);
1002             if (dump_file)
1003               fprintf (dump_file, "read-only constant %s\n",
1004                        get_static_name (index));
1005           }
1006       }
1007
1008     BITMAP_FREE(module_statics_escape);
1009     BITMAP_FREE(module_statics_written);
1010
1011     if (dump_file)
1012       EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi)
1013         {
1014           fprintf (dump_file, "\nPromotable global:%s",
1015                    get_static_name (index));
1016         }
1017
1018     for (i = 0; i < order_pos; i++ )
1019       {
1020         ipa_reference_local_vars_info_t l;
1021         node = order[i];
1022         l = get_reference_vars_info_from_cgraph (node)->local;
1023
1024         /* Any variables that are not in all_module_statics are
1025            removed from the local maps.  This will include all of the
1026            variables that were found to escape in the function
1027            scanning.  */
1028         bitmap_and_into (l->statics_read, 
1029                          all_module_statics);
1030         bitmap_and_into (l->statics_written, 
1031                          all_module_statics);
1032       }
1033
1034     BITMAP_FREE(module_statics_readonly);
1035     BITMAP_FREE(module_statics_const);
1036     BITMAP_FREE(bm_temp);
1037   }
1038
1039   if (dump_file)
1040     {
1041       for (i = 0; i < order_pos; i++ )
1042         {
1043           unsigned int index;
1044           ipa_reference_local_vars_info_t l;
1045           bitmap_iterator bi;
1046
1047           node = order[i];
1048           l = get_reference_vars_info_from_cgraph (node)->local;
1049           fprintf (dump_file, 
1050                    "\nFunction name:%s/%i:", 
1051                    cgraph_node_name (node), node->uid);
1052           fprintf (dump_file, "\n  locals read: ");
1053           EXECUTE_IF_SET_IN_BITMAP (l->statics_read,
1054                                     0, index, bi)
1055             {
1056               fprintf (dump_file, "%s ",
1057                        get_static_name (index));
1058             }
1059           fprintf (dump_file, "\n  locals written: ");
1060           EXECUTE_IF_SET_IN_BITMAP (l->statics_written,
1061                                     0, index, bi)
1062             {
1063               fprintf(dump_file, "%s ",
1064                       get_static_name (index));
1065             }
1066         }
1067     }
1068
1069   /* Propagate the local information thru the call graph to produce
1070      the global information.  All the nodes within a cycle will have
1071      the same info so we collapse cycles first.  Then we can do the
1072      propagation in one pass from the leaves to the roots.  */
1073   order_pos = ipa_utils_reduced_inorder (order, true, true);
1074   if (dump_file)
1075     ipa_utils_print_order(dump_file, "reduced", order, order_pos);
1076
1077   for (i = 0; i < order_pos; i++ )
1078     {
1079       ipa_reference_vars_info_t node_info;
1080       ipa_reference_global_vars_info_t node_g = 
1081         XCNEW (struct ipa_reference_global_vars_info_d);
1082       ipa_reference_local_vars_info_t node_l;
1083       
1084       bool read_all;
1085       bool write_all;
1086       struct ipa_dfs_info * w_info;
1087
1088       node = order[i];
1089       node_info = get_reference_vars_info_from_cgraph (node);
1090       if (!node_info) 
1091         {
1092           dump_cgraph_node (stderr, node);
1093           dump_cgraph (stderr);
1094           gcc_unreachable ();
1095         }
1096
1097       node_info->global = node_g;
1098       node_l = node_info->local;
1099
1100       read_all = node_l->calls_read_all;
1101       write_all = node_l->calls_write_all;
1102
1103       /* If any node in a cycle is calls_read_all or calls_write_all
1104          they all are. */
1105       w_info = (struct ipa_dfs_info *) node->aux;
1106       w = w_info->next_cycle;
1107       while (w)
1108         {
1109           ipa_reference_local_vars_info_t w_l = 
1110             get_reference_vars_info_from_cgraph (w)->local;
1111           read_all |= w_l->calls_read_all;
1112           write_all |= w_l->calls_write_all;
1113
1114           w_info = (struct ipa_dfs_info *) w->aux;
1115           w = w_info->next_cycle;
1116         }
1117
1118       /* Initialized the bitmaps for the reduced nodes */
1119       if (read_all) 
1120         node_g->statics_read = all_module_statics;
1121       else 
1122         {
1123           node_g->statics_read = BITMAP_ALLOC (&ipa_obstack);
1124           bitmap_copy (node_g->statics_read, 
1125                        node_l->statics_read);
1126         }
1127
1128       if (write_all) 
1129         node_g->statics_written = all_module_statics;
1130       else
1131         {
1132           node_g->statics_written = BITMAP_ALLOC (&ipa_obstack);
1133           bitmap_copy (node_g->statics_written, 
1134                        node_l->statics_written);
1135         }
1136
1137       w_info = (struct ipa_dfs_info *) node->aux;
1138       w = w_info->next_cycle;
1139       while (w)
1140         {
1141           ipa_reference_vars_info_t w_ri = 
1142             get_reference_vars_info_from_cgraph (w);
1143           ipa_reference_local_vars_info_t w_l = w_ri->local;
1144
1145           /* All nodes within a cycle share the same global info bitmaps.  */
1146           w_ri->global = node_g;
1147           
1148           /* These global bitmaps are initialized from the local info
1149              of all of the nodes in the region.  However there is no
1150              need to do any work if the bitmaps were set to
1151              all_module_statics.  */
1152           if (!read_all)
1153             bitmap_ior_into (node_g->statics_read,
1154                              w_l->statics_read);
1155           if (!write_all)
1156             bitmap_ior_into (node_g->statics_written,
1157                              w_l->statics_written);
1158           w_info = (struct ipa_dfs_info *) w->aux;
1159           w = w_info->next_cycle;
1160         }
1161
1162       w = node;
1163       while (w)
1164         {
1165           propagate_bits (w);
1166           w_info = (struct ipa_dfs_info *) w->aux;
1167           w = w_info->next_cycle;
1168         }
1169     }
1170
1171   /* Need to fix up the local information sets.  The information that
1172      has been gathered so far is preinlining.  However, the
1173      compilation will progress post inlining so the local sets for the
1174      inlined calls need to be merged into the callers.  Note that the
1175      local sets are not shared between all of the nodes in a cycle so
1176      those nodes in the cycle must be processed explicitly.  */
1177   for (i = 0; i < order_pos; i++ )
1178     {
1179       struct ipa_dfs_info * w_info;
1180       node = order[i];
1181       merge_callee_local_info (node, node);
1182       
1183       w_info = (struct ipa_dfs_info *) node->aux;
1184       w = w_info->next_cycle;
1185       while (w)
1186         {
1187           merge_callee_local_info (w, w);
1188           w_info = (struct ipa_dfs_info *) w->aux;
1189           w = w_info->next_cycle;
1190         }
1191     }
1192
1193   if (dump_file)
1194     {
1195       for (i = 0; i < order_pos; i++ )
1196         {
1197           ipa_reference_vars_info_t node_info;
1198           ipa_reference_global_vars_info_t node_g;
1199           ipa_reference_local_vars_info_t node_l;
1200           unsigned int index;
1201           bitmap_iterator bi;
1202           struct ipa_dfs_info * w_info;
1203
1204           node = order[i];
1205           node_info = get_reference_vars_info_from_cgraph (node);
1206           node_g = node_info->global;
1207           node_l = node_info->local;
1208           fprintf (dump_file, 
1209                    "\nFunction name:%s/%i:", 
1210                    cgraph_node_name (node), node->uid);
1211           fprintf (dump_file, "\n  locals read: ");
1212           EXECUTE_IF_SET_IN_BITMAP (node_l->statics_read,
1213                                     0, index, bi)
1214             {
1215               fprintf (dump_file, "%s ",
1216                        get_static_name (index));
1217             }
1218           fprintf (dump_file, "\n  locals written: ");
1219           EXECUTE_IF_SET_IN_BITMAP (node_l->statics_written,
1220                                     0, index, bi)
1221             {
1222               fprintf(dump_file, "%s ",
1223                       get_static_name (index));
1224             }
1225
1226           w_info = (struct ipa_dfs_info *) node->aux;
1227           w = w_info->next_cycle;
1228           while (w) 
1229             {
1230               ipa_reference_vars_info_t w_ri = 
1231                 get_reference_vars_info_from_cgraph (w);
1232               ipa_reference_local_vars_info_t w_l = w_ri->local;
1233               fprintf (dump_file, "\n  next cycle: %s/%i ",
1234                        cgraph_node_name (w), w->uid);
1235               fprintf (dump_file, "\n    locals read: ");
1236               EXECUTE_IF_SET_IN_BITMAP (w_l->statics_read,
1237                                         0, index, bi)
1238                 {
1239                   fprintf (dump_file, "%s ",
1240                            get_static_name (index));
1241                 }
1242
1243               fprintf (dump_file, "\n    locals written: ");
1244               EXECUTE_IF_SET_IN_BITMAP (w_l->statics_written,
1245                                         0, index, bi)
1246                 {
1247                   fprintf(dump_file, "%s ",
1248                           get_static_name (index));
1249                 }
1250               
1251
1252               w_info = (struct ipa_dfs_info *) w->aux;
1253               w = w_info->next_cycle;
1254             }
1255           fprintf (dump_file, "\n  globals read: ");
1256           EXECUTE_IF_SET_IN_BITMAP (node_g->statics_read,
1257                                     0, index, bi)
1258             {
1259               fprintf (dump_file, "%s ",
1260                        get_static_name (index));
1261             }
1262           fprintf (dump_file, "\n  globals written: ");
1263           EXECUTE_IF_SET_IN_BITMAP (node_g->statics_written,
1264                                     0, index, bi)
1265             {
1266               fprintf (dump_file, "%s ",
1267                        get_static_name (index));
1268             }
1269         }
1270     }
1271
1272   /* Cleanup. */
1273   for (i = 0; i < order_pos; i++ )
1274     {
1275       ipa_reference_vars_info_t node_info;
1276       ipa_reference_global_vars_info_t node_g;
1277       node = order[i];
1278       node_info = get_reference_vars_info_from_cgraph (node);
1279       node_g = node_info->global;
1280       
1281       /* Create the complimentary sets.  These are more useful for
1282          certain apis.  */
1283       node_g->statics_not_read = BITMAP_ALLOC (&ipa_obstack);
1284       node_g->statics_not_written = BITMAP_ALLOC (&ipa_obstack);
1285
1286       if (node_g->statics_read != all_module_statics) 
1287         {
1288           bitmap_and_compl (node_g->statics_not_read, 
1289                             all_module_statics,
1290                             node_g->statics_read);
1291         }
1292
1293       if (node_g->statics_written 
1294           != all_module_statics) 
1295         bitmap_and_compl (node_g->statics_not_written, 
1296                           all_module_statics,
1297                           node_g->statics_written);
1298    }
1299
1300   free (order);
1301
1302   for (node = cgraph_nodes; node; node = node->next)
1303     {
1304       /* Get rid of the aux information.  */
1305       
1306       if (node->aux)
1307         {
1308           free (node->aux);
1309           node->aux = NULL;
1310         }
1311       
1312       if (node->analyzed 
1313           && (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE))
1314         clean_function (node);
1315     }
1316   return 0;
1317 }
1318
1319
1320 static bool
1321 gate_reference (void)
1322 {
1323   return (flag_unit_at_a_time != 0  && flag_ipa_reference
1324           /* Don't bother doing anything if the program has errors.  */
1325           && !(errorcount || sorrycount));
1326 }
1327
1328 struct tree_opt_pass pass_ipa_reference =
1329 {
1330   "static-var",                         /* name */
1331   gate_reference,                       /* gate */
1332   static_execute,                       /* execute */
1333   NULL,                                 /* sub */
1334   NULL,                                 /* next */
1335   0,                                    /* static_pass_number */
1336   TV_IPA_REFERENCE,                     /* tv_id */
1337   0,                                    /* properties_required */
1338   0,                                    /* properties_provided */
1339   0,                                    /* properties_destroyed */
1340   0,                                    /* todo_flags_start */
1341   0,                                    /* todo_flags_finish */
1342   0                                     /* letter */
1343 };
1344
1345 #include "gt-ipa-reference.h"
1346