OSDN Git Service

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