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>
5 This file is part of GCC.
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
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
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/>. */
21 /* This file gathers information about how variables whose scope is
22 confined to the compilation unit are used.
24 There are two categories of information produced by this pass:
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.
30 2) The transitive call site specific clobber effects are computed
31 for the variables whose scope is contained within this compilation
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
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. */
52 #include "coretypes.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"
61 #include "ipa-utils.h"
62 #include "ipa-reference.h"
69 #include "diagnostic.h"
70 #include "langhooks.h"
72 /* The static variables defined within the compilation unit that are
73 loaded or stored directly by function that owns this structure. */
75 struct ipa_reference_local_vars_info_d
78 bitmap statics_written
;
80 /* Set when this function calls another function external to the
81 compilation unit or if the function has a asm clobber of memory.
82 In general, such calls are modeled as reading and writing all
83 variables (both bits on) but sometime there are attributes on the
84 called function so we can do better. */
89 /* Statics that are read and written by some set of functions. The
90 local ones are based on the loads and stores local to the function.
91 The global ones are based on the local info as well as the
92 transitive closure of the functions that are called. The
93 structures are separated to allow the global structures to be
94 shared between several functions since every function within a
95 strongly connected component will have the same information. This
96 sharing saves both time and space in the computation of the vectors
97 as well as their translation from decl_uid form to ann_uid
100 struct ipa_reference_global_vars_info_d
103 bitmap statics_written
;
104 bitmap statics_not_read
;
105 bitmap statics_not_written
;
108 typedef struct ipa_reference_local_vars_info_d
*ipa_reference_local_vars_info_t
;
109 typedef struct ipa_reference_global_vars_info_d
*ipa_reference_global_vars_info_t
;
110 struct ipa_reference_vars_info_d
112 ipa_reference_local_vars_info_t local
;
113 ipa_reference_global_vars_info_t global
;
116 typedef struct ipa_reference_vars_info_d
*ipa_reference_vars_info_t
;
118 /* This splay tree contains all of the static variables that are
119 being considered by the compilation level alias analysis. For
120 module_at_a_time compilation, this is the set of static but not
121 public variables. Any variables that either have their address
122 taken or participate in otherwise unsavory operations are deleted
124 static GTY((param1_is(int), param2_is(tree
)))
125 splay_tree reference_vars_to_consider
;
127 /* This bitmap is used to knock out the module static variables whose
128 addresses have been taken and passed around. */
129 static bitmap module_statics_escape
;
131 /* This bitmap is used to knock out the module static variables that
133 static bitmap module_statics_written
;
135 /* A bit is set for every module static we are considering. This is
136 ored into the local info when asm code is found that clobbers all
138 static bitmap all_module_statics
;
140 static struct pointer_set_t
*visited_nodes
;
142 /* Obstack holding bitmaps of local analysis (live from analysis to
144 static bitmap_obstack local_info_obstack
;
145 /* Obstack holding global analysis live forever. */
146 static bitmap_obstack global_info_obstack
;
148 /* Holders of ipa cgraph hooks: */
149 static struct cgraph_node_hook_list
*function_insertion_hook_holder
;
150 static struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
151 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
153 enum initialization_status_t
160 tree memory_identifier_string
;
162 /* Vector where the reference var infos are actually stored. */
163 DEF_VEC_P (ipa_reference_vars_info_t
);
164 DEF_VEC_ALLOC_P (ipa_reference_vars_info_t
, heap
);
165 static VEC (ipa_reference_vars_info_t
, heap
) *ipa_reference_vars_vector
;
167 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
168 static inline ipa_reference_vars_info_t
169 get_reference_vars_info (struct cgraph_node
*node
)
171 if (!ipa_reference_vars_vector
172 || VEC_length (ipa_reference_vars_info_t
, ipa_reference_vars_vector
) <= (unsigned int)node
->uid
)
174 return VEC_index (ipa_reference_vars_info_t
, ipa_reference_vars_vector
, node
->uid
);
177 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
179 set_reference_vars_info (struct cgraph_node
*node
, ipa_reference_vars_info_t info
)
181 if (!ipa_reference_vars_vector
182 || VEC_length (ipa_reference_vars_info_t
, ipa_reference_vars_vector
) <= (unsigned int)node
->uid
)
183 VEC_safe_grow_cleared (ipa_reference_vars_info_t
, heap
, ipa_reference_vars_vector
, node
->uid
+ 1);
184 VEC_replace (ipa_reference_vars_info_t
, ipa_reference_vars_vector
, node
->uid
, info
);
187 /* Get a bitmap that contains all of the locally referenced static
188 variables for function FN. */
189 static ipa_reference_local_vars_info_t
190 get_local_reference_vars_info (struct cgraph_node
*fn
)
192 ipa_reference_vars_info_t info
= get_reference_vars_info (fn
);
197 /* This phase was not run. */
201 /* Get a bitmap that contains all of the globally referenced static
202 variables for function FN. */
204 static ipa_reference_global_vars_info_t
205 get_global_reference_vars_info (struct cgraph_node
*fn
)
207 ipa_reference_vars_info_t info
= get_reference_vars_info (fn
);
212 /* This phase was not run. */
216 /* Return a bitmap indexed by VAR_DECL uid for the static variables
217 that are read during the execution of the function FN. Returns
218 NULL if no data is available. */
221 ipa_reference_get_read_global (struct cgraph_node
*fn
)
223 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
225 return g
->statics_read
;
230 /* Return a bitmap indexed by VAR_DECL uid for the static variables
231 that are written during the execution of the function FN. Note
232 that variables written may or may not be read during the function
233 call. Returns NULL if no data is available. */
236 ipa_reference_get_written_global (struct cgraph_node
*fn
)
238 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
240 return g
->statics_written
;
245 /* Return a bitmap indexed by_DECL_UID uid for the static variables
246 that are not read during the execution of the function FN. Returns
247 NULL if no data is available. */
250 ipa_reference_get_not_read_global (struct cgraph_node
*fn
)
252 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
254 return g
->statics_not_read
;
259 /* Return a bitmap indexed by DECL_UID uid for the static variables
260 that are not written during the execution of the function FN. Note
261 that variables written may or may not be read during the function
262 call. Returns NULL if no data is available. */
265 ipa_reference_get_not_written_global (struct cgraph_node
*fn
)
267 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
269 return g
->statics_not_written
;
276 /* Add VAR to all_module_statics and the two
277 reference_vars_to_consider* sets. */
280 add_static_var (tree var
)
282 int uid
= DECL_UID (var
);
283 gcc_assert (TREE_CODE (var
) == VAR_DECL
);
284 if (!bitmap_bit_p (all_module_statics
, uid
))
286 splay_tree_insert (reference_vars_to_consider
,
287 uid
, (splay_tree_value
)var
);
288 bitmap_set_bit (all_module_statics
, uid
);
292 /* Return true if the variable T is the right kind of static variable to
293 perform compilation unit scope escape analysis. */
296 has_proper_scope_for_analysis (tree t
)
298 /* If the variable has the "used" attribute, treat it as if it had a
299 been touched by the devil. */
300 if (lookup_attribute ("used", DECL_ATTRIBUTES (t
)))
303 /* Do not want to do anything with volatile except mark any
304 function that uses one to be not const or pure. */
305 if (TREE_THIS_VOLATILE (t
))
308 /* Do not care about a local automatic that is not static. */
309 if (!TREE_STATIC (t
) && !DECL_EXTERNAL (t
))
312 if (DECL_EXTERNAL (t
) || TREE_PUBLIC (t
))
315 /* We cannot touch decls where the type needs constructing. */
316 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t
)))
319 /* This is a variable we care about. Check if we have seen it
320 before, and if not add it the set of variables we care about. */
321 if (!bitmap_bit_p (all_module_statics
, DECL_UID (t
)))
327 /* Mark tree T as having address taken. */
330 mark_address_taken (tree x
)
332 if (TREE_CODE (x
) == VAR_DECL
333 && module_statics_escape
&& has_proper_scope_for_analysis (x
))
334 bitmap_set_bit (module_statics_escape
, DECL_UID (x
));
337 /* Mark load of T. */
340 mark_load (ipa_reference_local_vars_info_t local
,
343 if (TREE_CODE (t
) == VAR_DECL
344 && has_proper_scope_for_analysis (t
))
345 bitmap_set_bit (local
->statics_read
, DECL_UID (t
));
348 /* Mark store of T. */
351 mark_store (ipa_reference_local_vars_info_t local
,
354 if (TREE_CODE (t
) == VAR_DECL
355 && has_proper_scope_for_analysis (t
))
358 bitmap_set_bit (local
->statics_written
, DECL_UID (t
));
359 /* Mark the write so we can tell which statics are
361 if (module_statics_written
)
362 bitmap_set_bit (module_statics_written
, DECL_UID (t
));
366 /* Look for memory clobber and set read_all/write_all if present. */
369 check_asm_memory_clobber (ipa_reference_local_vars_info_t local
, gimple stmt
)
374 for (i
= 0; i
< gimple_asm_nclobbers (stmt
); i
++)
376 op
= gimple_asm_clobber_op (stmt
, i
);
377 if (simple_cst_equal(TREE_VALUE (op
), memory_identifier_string
) == 1)
379 /* Abandon all hope, ye who enter here. */
380 local
->calls_read_all
= true;
381 local
->calls_write_all
= true;
386 /* Look for external calls and set read_all/write_all correspondingly. */
389 check_call (ipa_reference_local_vars_info_t local
, gimple stmt
)
391 int flags
= gimple_call_flags (stmt
);
392 tree callee_t
= gimple_call_fndecl (stmt
);
393 enum availability avail
= AVAIL_NOT_AVAILABLE
;
397 struct cgraph_node
* callee
= cgraph_node(callee_t
);
398 avail
= cgraph_function_body_availability (callee
);
401 if (avail
<= AVAIL_OVERWRITABLE
)
404 if (flags
& ECF_CONST
)
406 else if (flags
& ECF_PURE
)
407 local
->calls_read_all
= true;
410 local
->calls_read_all
= true;
411 local
->calls_write_all
= true;
414 /* TODO: To be able to produce sane results, we should also handle
415 common builtins, in particular throw.
416 Indirect calls hsould be only counted and as inliner is replacing them
417 by direct calls, we can conclude if any indirect calls are left in body */
420 /* TP is the part of the tree currently under the microscope.
421 WALK_SUBTREES is part of the walk_tree api but is unused here.
422 DATA is cgraph_node of the function being walked. */
425 scan_stmt_for_static_refs (gimple_stmt_iterator
*gsip
,
426 struct cgraph_node
*fn
)
428 gimple stmt
= gsi_stmt (*gsip
);
429 ipa_reference_local_vars_info_t local
= NULL
;
434 local
= get_reference_vars_info (fn
)->local
;
436 if (gimple_loaded_syms (stmt
))
437 EXECUTE_IF_SET_IN_BITMAP (gimple_loaded_syms (stmt
), 0, i
, bi
)
438 mark_load (local
, referenced_var_lookup (i
));
439 if (gimple_stored_syms (stmt
))
440 EXECUTE_IF_SET_IN_BITMAP (gimple_stored_syms (stmt
), 0, i
, bi
)
441 mark_store (local
, referenced_var_lookup (i
));
442 if (gimple_addresses_taken (stmt
))
443 EXECUTE_IF_SET_IN_BITMAP (gimple_addresses_taken (stmt
), 0, i
, bi
)
444 mark_address_taken (referenced_var_lookup (i
));
446 switch (gimple_code (stmt
))
449 check_call (local
, stmt
);
453 check_asm_memory_clobber (local
, stmt
);
456 /* We used to check nonlocal labels here and set them as potentially modifying
457 everything. This is not needed, since we can get to nonlocal label only
458 from callee and thus we will get info propagated. */
467 /* Call-back to scan variable initializers for static references.
468 Called using walk_tree. */
471 scan_initializer_for_static_refs (tree
*tp
, int *walk_subtrees
,
472 void *data ATTRIBUTE_UNUSED
)
476 if (TREE_CODE (t
) == ADDR_EXPR
)
478 mark_address_taken (get_base_var (t
));
481 /* Save some cycles by not walking types and declaration as we
482 won't find anything useful there anyway. */
483 else if (IS_TYPE_OR_DECL_P (*tp
))
489 /* Lookup the tree node for the static variable that has UID. */
491 get_static_decl (int index
)
493 splay_tree_node stn
=
494 splay_tree_lookup (reference_vars_to_consider
, index
);
496 return (tree
)stn
->value
;
500 /* Lookup the tree node for the static variable that has UID and
501 convert the name to a string for debugging. */
504 get_static_name (int index
)
506 splay_tree_node stn
=
507 splay_tree_lookup (reference_vars_to_consider
, index
);
509 return lang_hooks
.decl_printable_name ((tree
)(stn
->value
), 2);
513 /* Or in all of the bits from every callee of X into X_GLOBAL, the caller's cycle,
514 bit vector. There are several cases to check to avoid the sparse
518 propagate_bits (ipa_reference_global_vars_info_t x_global
, struct cgraph_node
*x
)
520 struct cgraph_edge
*e
;
521 for (e
= x
->callees
; e
; e
= e
->next_callee
)
523 struct cgraph_node
*y
= e
->callee
;
525 /* Only look at the master nodes and skip external nodes. */
526 if (cgraph_function_body_availability (e
->callee
) > AVAIL_OVERWRITABLE
)
528 if (get_reference_vars_info (y
))
530 ipa_reference_vars_info_t y_info
531 = get_reference_vars_info (y
);
532 ipa_reference_global_vars_info_t y_global
= y_info
->global
;
534 /* Calls in current cycle do not have global computed yet. */
538 if (x_global
->statics_read
539 != all_module_statics
)
541 if (y_global
->statics_read
542 == all_module_statics
)
544 BITMAP_FREE (x_global
->statics_read
);
545 x_global
->statics_read
546 = all_module_statics
;
548 /* Skip bitmaps that are pointer equal to node's bitmap
549 (no reason to spin within the cycle). */
550 else if (x_global
->statics_read
551 != y_global
->statics_read
)
552 bitmap_ior_into (x_global
->statics_read
,
553 y_global
->statics_read
);
556 if (x_global
->statics_written
557 != all_module_statics
)
559 if (y_global
->statics_written
560 == all_module_statics
)
562 BITMAP_FREE (x_global
->statics_written
);
563 x_global
->statics_written
564 = all_module_statics
;
566 /* Skip bitmaps that are pointer equal to node's bitmap
567 (no reason to spin within the cycle). */
568 else if (x_global
->statics_written
569 != y_global
->statics_written
)
570 bitmap_ior_into (x_global
->statics_written
,
571 y_global
->statics_written
);
580 /* The init routine for analyzing global static variable usage. See
581 comments at top for description. */
585 memory_identifier_string
= build_string(7, "memory");
587 reference_vars_to_consider
=
588 splay_tree_new_ggc (splay_tree_compare_ints
);
590 bitmap_obstack_initialize (&local_info_obstack
);
591 bitmap_obstack_initialize (&global_info_obstack
);
592 module_statics_escape
= BITMAP_ALLOC (&local_info_obstack
);
593 module_statics_written
= BITMAP_ALLOC (&local_info_obstack
);
594 all_module_statics
= BITMAP_ALLOC (&global_info_obstack
);
596 /* There are some shared nodes, in particular the initializers on
597 static declarations. We do not need to scan them more than once
598 since all we would be interested in are the addressof
600 visited_nodes
= pointer_set_create ();
603 /* Check out the rhs of a static or global initialization VNODE to see
604 if any of them contain addressof operations. Note that some of
605 these variables may not even be referenced in the code in this
606 compilation unit but their right hand sides may contain references
607 to variables defined within this unit. */
610 analyze_variable (struct varpool_node
*vnode
)
612 struct walk_stmt_info wi
;
613 tree global
= vnode
->decl
;
615 memset (&wi
, 0, sizeof (wi
));
616 wi
.pset
= visited_nodes
;
617 walk_tree (&DECL_INITIAL (global
), scan_initializer_for_static_refs
,
621 /* Set up the persistent info for FN. */
623 static ipa_reference_local_vars_info_t
624 init_function_info (struct cgraph_node
*fn
)
626 ipa_reference_vars_info_t info
627 = XCNEW (struct ipa_reference_vars_info_d
);
628 ipa_reference_local_vars_info_t l
629 = XCNEW (struct ipa_reference_local_vars_info_d
);
631 /* Add the info to the tree's annotation. */
632 set_reference_vars_info (fn
, info
);
635 l
->statics_read
= BITMAP_ALLOC (&local_info_obstack
);
636 l
->statics_written
= BITMAP_ALLOC (&local_info_obstack
);
641 /* This is the main routine for finding the reference patterns for
642 global variables within a function FN. */
645 analyze_function (struct cgraph_node
*fn
)
647 tree decl
= fn
->decl
;
648 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (decl
);
649 basic_block this_block
;
650 #ifdef ENABLE_CHECKING
655 fprintf (dump_file
, "\n local analysis of %s\n", cgraph_node_name (fn
));
657 push_cfun (DECL_STRUCT_FUNCTION (decl
));
658 current_function_decl
= decl
;
660 init_function_info (fn
);
661 FOR_EACH_BB_FN (this_block
, this_cfun
)
663 gimple_stmt_iterator gsi
;
669 /* Find the addresses taken in phi node arguments. */
670 for (gsi
= gsi_start_phis (this_block
);
674 phi
= gsi_stmt (gsi
);
675 FOR_EACH_PHI_ARG (use
, phi
, iter
, SSA_OP_USE
)
677 op
= USE_FROM_PTR (use
);
678 if (TREE_CODE (op
) == ADDR_EXPR
)
679 mark_address_taken (get_base_var (op
));
683 for (gsi
= gsi_start_bb (this_block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
684 scan_stmt_for_static_refs (&gsi
, fn
);
687 #ifdef ENABLE_CHECKING
688 /* Verify that all local initializers was expanded by gimplifier. */
689 for (step
= DECL_STRUCT_FUNCTION (decl
)->local_decls
;
691 step
= TREE_CHAIN (step
))
693 tree var
= TREE_VALUE (step
);
694 if (TREE_CODE (var
) == VAR_DECL
695 && DECL_INITIAL (var
)
696 && !TREE_STATIC (var
))
701 current_function_decl
= NULL
;
704 /* Remove local data associated with function FN. */
706 clean_function_local_data (struct cgraph_node
*fn
)
708 ipa_reference_vars_info_t info
= get_reference_vars_info (fn
);
709 ipa_reference_local_vars_info_t l
= info
->local
;
713 && l
->statics_read
!= all_module_statics
)
714 BITMAP_FREE (l
->statics_read
);
715 if (l
->statics_written
716 &&l
->statics_written
!= all_module_statics
)
717 BITMAP_FREE (l
->statics_written
);
723 /* Remove all data associated with function FN. */
726 clean_function (struct cgraph_node
*fn
)
728 ipa_reference_vars_info_t info
= get_reference_vars_info (fn
);
729 ipa_reference_global_vars_info_t g
= info
->global
;
731 clean_function_local_data (fn
);
735 && g
->statics_read
!= all_module_statics
)
736 BITMAP_FREE (g
->statics_read
);
738 if (g
->statics_written
739 && g
->statics_written
!= all_module_statics
)
740 BITMAP_FREE (g
->statics_written
);
742 if (g
->statics_not_read
743 && g
->statics_not_read
!= all_module_statics
)
744 BITMAP_FREE (g
->statics_not_read
);
746 if (g
->statics_not_written
747 && g
->statics_not_written
!= all_module_statics
)
748 BITMAP_FREE (g
->statics_not_written
);
753 free (get_reference_vars_info (fn
));
754 set_reference_vars_info (fn
, NULL
);
757 /* Called when new function is inserted to callgraph late. */
759 add_new_function (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
761 /* There are some shared nodes, in particular the initializers on
762 static declarations. We do not need to scan them more than once
763 since all we would be interested in are the addressof
765 analyze_function (node
);
766 visited_nodes
= NULL
;
770 copy_local_bitmap (bitmap src
)
775 if (src
== all_module_statics
)
776 return all_module_statics
;
777 dst
= BITMAP_ALLOC (&local_info_obstack
);
778 bitmap_copy (dst
, src
);
783 copy_global_bitmap (bitmap src
)
788 if (src
== all_module_statics
)
789 return all_module_statics
;
790 dst
= BITMAP_ALLOC (&global_info_obstack
);
791 bitmap_copy (dst
, src
);
795 /* Called when new clone is inserted to callgraph late. */
798 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
799 void *data ATTRIBUTE_UNUSED
)
801 ipa_reference_global_vars_info_t ginfo
;
802 ipa_reference_local_vars_info_t linfo
;
803 ipa_reference_global_vars_info_t dst_ginfo
;
804 ipa_reference_local_vars_info_t dst_linfo
;
806 ginfo
= get_global_reference_vars_info (src
);
807 linfo
= get_local_reference_vars_info (src
);
808 if (!linfo
&& !ginfo
)
810 init_function_info (dst
);
813 dst_linfo
= get_local_reference_vars_info (dst
);
814 dst_linfo
->statics_read
= copy_local_bitmap (linfo
->statics_read
);
815 dst_linfo
->statics_written
= copy_local_bitmap (linfo
->statics_written
);
816 dst_linfo
->calls_read_all
= linfo
->calls_read_all
;
817 dst_linfo
->calls_write_all
= linfo
->calls_write_all
;
821 get_reference_vars_info (dst
)->global
= XCNEW (struct ipa_reference_global_vars_info_d
);
822 dst_ginfo
= get_global_reference_vars_info (dst
);
823 dst_ginfo
->statics_read
= copy_global_bitmap (ginfo
->statics_read
);
824 dst_ginfo
->statics_written
= copy_global_bitmap (ginfo
->statics_written
);
825 dst_ginfo
->statics_not_read
= copy_global_bitmap (ginfo
->statics_not_read
);
826 dst_ginfo
->statics_not_written
= copy_global_bitmap (ginfo
->statics_not_written
);
830 /* Called when node is removed. */
833 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
835 if (get_reference_vars_info (node
))
836 clean_function (node
);
839 /* Analyze each function in the cgraph to see which global or statics
840 are read or written. */
843 generate_summary (void)
845 struct cgraph_node
*node
;
846 struct varpool_node
*vnode
;
849 bitmap module_statics_readonly
;
852 function_insertion_hook_holder
=
853 cgraph_add_function_insertion_hook (&add_new_function
, NULL
);
854 node_removal_hook_holder
=
855 cgraph_add_node_removal_hook (&remove_node_data
, NULL
);
856 node_duplication_hook_holder
=
857 cgraph_add_node_duplication_hook (&duplicate_node_data
, NULL
);
859 module_statics_readonly
= BITMAP_ALLOC (&local_info_obstack
);
860 bm_temp
= BITMAP_ALLOC (&local_info_obstack
);
862 /* Process all of the variables first. */
863 FOR_EACH_STATIC_INITIALIZER (vnode
)
864 analyze_variable (vnode
);
866 /* Process all of the functions next.
868 We do not want to process any of the clones so we check that this
869 is a master clone. However, we do need to process any
870 AVAIL_OVERWRITABLE functions (these are never clones) because
871 they may cause a static variable to escape. The code that can
872 overwrite such a function cannot access the statics because it
873 would not be in the same compilation unit. When the analysis is
874 finished, the computed information of these AVAIL_OVERWRITABLE is
875 replaced with worst case info.
877 for (node
= cgraph_nodes
; node
; node
= node
->next
)
878 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
879 analyze_function (node
);
881 pointer_set_destroy (visited_nodes
);
882 visited_nodes
= NULL
;
884 /* Prune out the variables that were found to behave badly
885 (i.e. have their address taken). */
886 EXECUTE_IF_SET_IN_BITMAP (module_statics_escape
, 0, index
, bi
)
888 splay_tree_remove (reference_vars_to_consider
, index
);
891 bitmap_and_compl_into (all_module_statics
,
892 module_statics_escape
);
894 bitmap_and_compl (module_statics_readonly
, all_module_statics
,
895 module_statics_written
);
897 /* If the address is not taken, we can unset the addressable bit
899 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
901 tree var
= get_static_decl (index
);
902 TREE_ADDRESSABLE (var
) = 0;
904 fprintf (dump_file
, "Not TREE_ADDRESSABLE var %s\n",
905 get_static_name (index
));
908 /* If the variable is never written, we can set the TREE_READONLY
909 flag. Additionally if it has a DECL_INITIAL that is made up of
910 constants we can treat the entire global as a constant. */
912 bitmap_and_compl (module_statics_readonly
, all_module_statics
,
913 module_statics_written
);
914 EXECUTE_IF_SET_IN_BITMAP (module_statics_readonly
, 0, index
, bi
)
916 tree var
= get_static_decl (index
);
918 /* Ignore variables in named sections - changing TREE_READONLY
919 changes the section flags, potentially causing conflicts with
920 other variables in the same named section. */
921 if (DECL_SECTION_NAME (var
) == NULL_TREE
)
923 TREE_READONLY (var
) = 1;
925 fprintf (dump_file
, "read-only var %s\n",
926 get_static_name (index
));
930 BITMAP_FREE(module_statics_escape
);
931 BITMAP_FREE(module_statics_written
);
932 module_statics_escape
= NULL
;
933 module_statics_written
= NULL
;
936 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
938 fprintf (dump_file
, "\nPromotable global:%s",
939 get_static_name (index
));
942 for (node
= cgraph_nodes
; node
; node
= node
->next
)
943 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
945 ipa_reference_local_vars_info_t l
;
946 l
= get_reference_vars_info (node
)->local
;
948 /* Any variables that are not in all_module_statics are
949 removed from the local maps. This will include all of the
950 variables that were found to escape in the function
952 bitmap_and_into (l
->statics_read
,
954 bitmap_and_into (l
->statics_written
,
958 BITMAP_FREE(module_statics_readonly
);
959 BITMAP_FREE(bm_temp
);
962 for (node
= cgraph_nodes
; node
; node
= node
->next
)
963 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
965 ipa_reference_local_vars_info_t l
;
969 l
= get_reference_vars_info (node
)->local
;
971 "\nFunction name:%s/%i:",
972 cgraph_node_name (node
), node
->uid
);
973 fprintf (dump_file
, "\n locals read: ");
974 EXECUTE_IF_SET_IN_BITMAP (l
->statics_read
,
977 fprintf (dump_file
, "%s ",
978 get_static_name (index
));
980 fprintf (dump_file
, "\n locals written: ");
981 EXECUTE_IF_SET_IN_BITMAP (l
->statics_written
,
984 fprintf(dump_file
, "%s ",
985 get_static_name (index
));
987 if (l
->calls_read_all
)
988 fprintf (dump_file
, "\n calls read all: ");
989 if (l
->calls_write_all
)
990 fprintf (dump_file
, "\n calls read all: ");
994 /* Produce the global information by preforming a transitive closure
995 on the local information that was produced by ipa_analyze_function
996 and ipa_analyze_variable. */
1001 struct cgraph_node
*node
;
1002 struct cgraph_node
*w
;
1003 struct cgraph_node
**order
=
1004 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1005 int order_pos
= ipa_utils_reduced_inorder (order
, false, true);
1008 cgraph_remove_function_insertion_hook (function_insertion_hook_holder
);
1010 dump_cgraph (dump_file
);
1012 /* Propagate the local information thru the call graph to produce
1013 the global information. All the nodes within a cycle will have
1014 the same info so we collapse cycles first. Then we can do the
1015 propagation in one pass from the leaves to the roots. */
1016 order_pos
= ipa_utils_reduced_inorder (order
, true, true);
1018 ipa_utils_print_order(dump_file
, "reduced", order
, order_pos
);
1020 for (i
= 0; i
< order_pos
; i
++ )
1022 ipa_reference_vars_info_t node_info
;
1023 ipa_reference_global_vars_info_t node_g
=
1024 XCNEW (struct ipa_reference_global_vars_info_d
);
1025 ipa_reference_local_vars_info_t node_l
;
1029 struct ipa_dfs_info
* w_info
;
1032 node_info
= get_reference_vars_info (node
);
1035 dump_cgraph_node (stderr
, node
);
1036 dump_cgraph (stderr
);
1040 gcc_assert (!node_info
->global
);
1041 node_l
= node_info
->local
;
1043 read_all
= node_l
->calls_read_all
;
1044 write_all
= node_l
->calls_write_all
;
1046 /* If any node in a cycle is calls_read_all or calls_write_all
1048 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1049 w
= w_info
->next_cycle
;
1052 ipa_reference_local_vars_info_t w_l
=
1053 get_reference_vars_info (w
)->local
;
1054 read_all
|= w_l
->calls_read_all
;
1055 write_all
|= w_l
->calls_write_all
;
1057 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1058 w
= w_info
->next_cycle
;
1061 /* Initialized the bitmaps for the reduced nodes */
1063 node_g
->statics_read
= all_module_statics
;
1066 node_g
->statics_read
= BITMAP_ALLOC (&global_info_obstack
);
1067 bitmap_copy (node_g
->statics_read
,
1068 node_l
->statics_read
);
1072 node_g
->statics_written
= all_module_statics
;
1075 node_g
->statics_written
= BITMAP_ALLOC (&global_info_obstack
);
1076 bitmap_copy (node_g
->statics_written
,
1077 node_l
->statics_written
);
1080 propagate_bits (node_g
, node
);
1081 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1082 w
= w_info
->next_cycle
;
1085 ipa_reference_vars_info_t w_ri
=
1086 get_reference_vars_info (w
);
1087 ipa_reference_local_vars_info_t w_l
= w_ri
->local
;
1089 /* These global bitmaps are initialized from the local info
1090 of all of the nodes in the region. However there is no
1091 need to do any work if the bitmaps were set to
1092 all_module_statics. */
1094 bitmap_ior_into (node_g
->statics_read
,
1097 bitmap_ior_into (node_g
->statics_written
,
1098 w_l
->statics_written
);
1099 propagate_bits (node_g
, w
);
1100 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1101 w
= w_info
->next_cycle
;
1104 /* All nodes within a cycle have the same global info bitmaps. */
1105 node_info
->global
= node_g
;
1106 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1107 w
= w_info
->next_cycle
;
1110 ipa_reference_vars_info_t w_ri
=
1111 get_reference_vars_info (w
);
1113 gcc_assert (!w_ri
->global
);
1114 w_ri
->global
= XCNEW (struct ipa_reference_global_vars_info_d
);
1115 w_ri
->global
->statics_read
= copy_global_bitmap (node_g
->statics_read
);
1116 w_ri
->global
->statics_written
= copy_global_bitmap (node_g
->statics_written
);
1118 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1119 w
= w_info
->next_cycle
;
1125 for (i
= 0; i
< order_pos
; i
++ )
1127 ipa_reference_vars_info_t node_info
;
1128 ipa_reference_global_vars_info_t node_g
;
1129 ipa_reference_local_vars_info_t node_l
;
1132 struct ipa_dfs_info
* w_info
;
1135 node_info
= get_reference_vars_info (node
);
1136 node_g
= node_info
->global
;
1137 node_l
= node_info
->local
;
1139 "\nFunction name:%s/%i:",
1140 cgraph_node_name (node
), node
->uid
);
1141 fprintf (dump_file
, "\n locals read: ");
1142 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_read
,
1145 fprintf (dump_file
, "%s ",
1146 get_static_name (index
));
1148 fprintf (dump_file
, "\n locals written: ");
1149 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_written
,
1152 fprintf(dump_file
, "%s ",
1153 get_static_name (index
));
1156 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1157 w
= w_info
->next_cycle
;
1160 ipa_reference_vars_info_t w_ri
=
1161 get_reference_vars_info (w
);
1162 ipa_reference_local_vars_info_t w_l
= w_ri
->local
;
1163 fprintf (dump_file
, "\n next cycle: %s/%i ",
1164 cgraph_node_name (w
), w
->uid
);
1165 fprintf (dump_file
, "\n locals read: ");
1166 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_read
,
1169 fprintf (dump_file
, "%s ",
1170 get_static_name (index
));
1173 fprintf (dump_file
, "\n locals written: ");
1174 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_written
,
1177 fprintf(dump_file
, "%s ",
1178 get_static_name (index
));
1182 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1183 w
= w_info
->next_cycle
;
1185 fprintf (dump_file
, "\n globals read: ");
1186 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_read
,
1189 fprintf (dump_file
, "%s ",
1190 get_static_name (index
));
1192 fprintf (dump_file
, "\n globals written: ");
1193 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_written
,
1196 fprintf (dump_file
, "%s ",
1197 get_static_name (index
));
1203 for (i
= 0; i
< order_pos
; i
++ )
1205 ipa_reference_vars_info_t node_info
;
1206 ipa_reference_global_vars_info_t node_g
;
1208 node_info
= get_reference_vars_info (node
);
1209 node_g
= node_info
->global
;
1211 /* Create the complimentary sets. These are more useful for
1213 node_g
->statics_not_read
= BITMAP_ALLOC (&global_info_obstack
);
1214 node_g
->statics_not_written
= BITMAP_ALLOC (&global_info_obstack
);
1216 if (node_g
->statics_read
!= all_module_statics
)
1217 bitmap_and_compl (node_g
->statics_not_read
,
1219 node_g
->statics_read
);
1221 if (node_g
->statics_written
1222 != all_module_statics
)
1223 bitmap_and_compl (node_g
->statics_not_written
,
1225 node_g
->statics_written
);
1230 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1232 ipa_reference_vars_info_t node_info
;
1233 node_info
= get_reference_vars_info (node
);
1234 /* Get rid of the aux information. */
1242 if (cgraph_function_body_availability (node
) == AVAIL_OVERWRITABLE
)
1243 clean_function (node
);
1245 clean_function_local_data (node
);
1247 bitmap_obstack_release (&local_info_obstack
);
1253 gate_reference (void)
1255 return (flag_ipa_reference
1256 /* Don't bother doing anything if the program has errors. */
1257 && !(errorcount
|| sorrycount
));
1260 struct ipa_opt_pass pass_ipa_reference
=
1264 "static-var", /* name */
1265 gate_reference
, /* gate */
1266 propagate
, /* execute */
1269 0, /* static_pass_number */
1270 TV_IPA_REFERENCE
, /* tv_id */
1271 0, /* properties_required */
1272 0, /* properties_provided */
1273 0, /* properties_destroyed */
1274 0, /* todo_flags_start */
1275 0 /* todo_flags_finish */
1277 generate_summary
, /* generate_summary */
1278 NULL
, /* write_summary */
1279 NULL
, /* read_summary */
1280 NULL
, /* function_read_summary */
1282 NULL
, /* function_transform */
1283 NULL
/* variable_transform */
1286 #include "gt-ipa-reference.h"