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 (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
342 ipa_reference_local_vars_info_t local
= (ipa_reference_local_vars_info_t
)data
;
343 if (TREE_CODE (t
) == VAR_DECL
344 && has_proper_scope_for_analysis (t
))
345 bitmap_set_bit (local
->statics_read
, DECL_UID (t
));
349 /* Mark store of T. */
352 mark_store (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
354 ipa_reference_local_vars_info_t local
= (ipa_reference_local_vars_info_t
)data
;
355 if (TREE_CODE (t
) == VAR_DECL
356 && has_proper_scope_for_analysis (t
))
359 bitmap_set_bit (local
->statics_written
, DECL_UID (t
));
360 /* Mark the write so we can tell which statics are
362 if (module_statics_written
)
363 bitmap_set_bit (module_statics_written
, DECL_UID (t
));
368 /* Look for memory clobber and set read_all/write_all if present. */
371 check_asm_memory_clobber (ipa_reference_local_vars_info_t local
, gimple stmt
)
376 for (i
= 0; i
< gimple_asm_nclobbers (stmt
); i
++)
378 op
= gimple_asm_clobber_op (stmt
, i
);
379 if (simple_cst_equal(TREE_VALUE (op
), memory_identifier_string
) == 1)
381 /* Abandon all hope, ye who enter here. */
382 local
->calls_read_all
= true;
383 local
->calls_write_all
= true;
388 /* Look for external calls and set read_all/write_all correspondingly. */
391 check_call (ipa_reference_local_vars_info_t local
, gimple stmt
)
393 int flags
= gimple_call_flags (stmt
);
394 tree callee_t
= gimple_call_fndecl (stmt
);
395 enum availability avail
= AVAIL_NOT_AVAILABLE
;
399 struct cgraph_node
* callee
= cgraph_node(callee_t
);
400 avail
= cgraph_function_body_availability (callee
);
403 if (avail
<= AVAIL_OVERWRITABLE
)
406 if (flags
& ECF_CONST
)
408 else if (flags
& ECF_PURE
)
409 local
->calls_read_all
= true;
412 local
->calls_read_all
= true;
413 local
->calls_write_all
= true;
416 /* TODO: To be able to produce sane results, we should also handle
417 common builtins, in particular throw.
418 Indirect calls hsould be only counted and as inliner is replacing them
419 by direct calls, we can conclude if any indirect calls are left in body */
422 /* TP is the part of the tree currently under the microscope.
423 WALK_SUBTREES is part of the walk_tree api but is unused here.
424 DATA is cgraph_node of the function being walked. */
427 scan_stmt_for_static_refs (gimple_stmt_iterator
*gsip
,
428 struct cgraph_node
*fn
)
430 gimple stmt
= gsi_stmt (*gsip
);
431 ipa_reference_local_vars_info_t local
= NULL
;
436 local
= get_reference_vars_info (fn
)->local
;
438 /* Look for direct loads and stores. */
439 walk_stmt_load_store_addr_ops (stmt
, local
, mark_load
, mark_store
, NULL
);
441 if (is_gimple_call (stmt
))
442 check_call (local
, stmt
);
443 else if (gimple_code (stmt
) == GIMPLE_ASM
)
444 check_asm_memory_clobber (local
, stmt
);
446 if (gimple_addresses_taken (stmt
))
447 EXECUTE_IF_SET_IN_BITMAP (gimple_addresses_taken (stmt
), 0, i
, bi
)
448 mark_address_taken (referenced_var_lookup (i
));
453 /* Call-back to scan variable initializers for static references.
454 Called using walk_tree. */
457 scan_initializer_for_static_refs (tree
*tp
, int *walk_subtrees
,
458 void *data ATTRIBUTE_UNUSED
)
462 if (TREE_CODE (t
) == ADDR_EXPR
)
464 mark_address_taken (get_base_var (t
));
467 /* Save some cycles by not walking types and declaration as we
468 won't find anything useful there anyway. */
469 else if (IS_TYPE_OR_DECL_P (*tp
))
475 /* Lookup the tree node for the static variable that has UID. */
477 get_static_decl (int index
)
479 splay_tree_node stn
=
480 splay_tree_lookup (reference_vars_to_consider
, index
);
482 return (tree
)stn
->value
;
486 /* Lookup the tree node for the static variable that has UID and
487 convert the name to a string for debugging. */
490 get_static_name (int index
)
492 splay_tree_node stn
=
493 splay_tree_lookup (reference_vars_to_consider
, index
);
495 return lang_hooks
.decl_printable_name ((tree
)(stn
->value
), 2);
499 /* Or in all of the bits from every callee of X into X_GLOBAL, the caller's cycle,
500 bit vector. There are several cases to check to avoid the sparse
504 propagate_bits (ipa_reference_global_vars_info_t x_global
, struct cgraph_node
*x
)
506 struct cgraph_edge
*e
;
507 for (e
= x
->callees
; e
; e
= e
->next_callee
)
509 struct cgraph_node
*y
= e
->callee
;
511 /* Only look at the master nodes and skip external nodes. */
512 if (cgraph_function_body_availability (e
->callee
) > AVAIL_OVERWRITABLE
)
514 if (get_reference_vars_info (y
))
516 ipa_reference_vars_info_t y_info
517 = get_reference_vars_info (y
);
518 ipa_reference_global_vars_info_t y_global
= y_info
->global
;
520 /* Calls in current cycle do not have global computed yet. */
524 if (x_global
->statics_read
525 != all_module_statics
)
527 if (y_global
->statics_read
528 == all_module_statics
)
530 BITMAP_FREE (x_global
->statics_read
);
531 x_global
->statics_read
532 = all_module_statics
;
534 /* Skip bitmaps that are pointer equal to node's bitmap
535 (no reason to spin within the cycle). */
536 else if (x_global
->statics_read
537 != y_global
->statics_read
)
538 bitmap_ior_into (x_global
->statics_read
,
539 y_global
->statics_read
);
542 if (x_global
->statics_written
543 != all_module_statics
)
545 if (y_global
->statics_written
546 == all_module_statics
)
548 BITMAP_FREE (x_global
->statics_written
);
549 x_global
->statics_written
550 = all_module_statics
;
552 /* Skip bitmaps that are pointer equal to node's bitmap
553 (no reason to spin within the cycle). */
554 else if (x_global
->statics_written
555 != y_global
->statics_written
)
556 bitmap_ior_into (x_global
->statics_written
,
557 y_global
->statics_written
);
566 /* The init routine for analyzing global static variable usage. See
567 comments at top for description. */
571 memory_identifier_string
= build_string(7, "memory");
573 reference_vars_to_consider
=
574 splay_tree_new_ggc (splay_tree_compare_ints
);
576 bitmap_obstack_initialize (&local_info_obstack
);
577 bitmap_obstack_initialize (&global_info_obstack
);
578 module_statics_escape
= BITMAP_ALLOC (&local_info_obstack
);
579 module_statics_written
= BITMAP_ALLOC (&local_info_obstack
);
580 all_module_statics
= BITMAP_ALLOC (&global_info_obstack
);
582 /* There are some shared nodes, in particular the initializers on
583 static declarations. We do not need to scan them more than once
584 since all we would be interested in are the addressof
586 visited_nodes
= pointer_set_create ();
589 /* Check out the rhs of a static or global initialization VNODE to see
590 if any of them contain addressof operations. Note that some of
591 these variables may not even be referenced in the code in this
592 compilation unit but their right hand sides may contain references
593 to variables defined within this unit. */
596 analyze_variable (struct varpool_node
*vnode
)
598 struct walk_stmt_info wi
;
599 tree global
= vnode
->decl
;
601 memset (&wi
, 0, sizeof (wi
));
602 wi
.pset
= visited_nodes
;
603 walk_tree (&DECL_INITIAL (global
), scan_initializer_for_static_refs
,
607 /* Set up the persistent info for FN. */
609 static ipa_reference_local_vars_info_t
610 init_function_info (struct cgraph_node
*fn
)
612 ipa_reference_vars_info_t info
613 = XCNEW (struct ipa_reference_vars_info_d
);
614 ipa_reference_local_vars_info_t l
615 = XCNEW (struct ipa_reference_local_vars_info_d
);
617 /* Add the info to the tree's annotation. */
618 set_reference_vars_info (fn
, info
);
621 l
->statics_read
= BITMAP_ALLOC (&local_info_obstack
);
622 l
->statics_written
= BITMAP_ALLOC (&local_info_obstack
);
627 /* This is the main routine for finding the reference patterns for
628 global variables within a function FN. */
631 analyze_function (struct cgraph_node
*fn
)
633 tree decl
= fn
->decl
;
634 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (decl
);
635 basic_block this_block
;
636 #ifdef ENABLE_CHECKING
641 fprintf (dump_file
, "\n local analysis of %s\n", cgraph_node_name (fn
));
643 push_cfun (DECL_STRUCT_FUNCTION (decl
));
644 current_function_decl
= decl
;
646 init_function_info (fn
);
647 FOR_EACH_BB_FN (this_block
, this_cfun
)
649 gimple_stmt_iterator gsi
;
655 /* Find the addresses taken in phi node arguments. */
656 for (gsi
= gsi_start_phis (this_block
);
660 phi
= gsi_stmt (gsi
);
661 FOR_EACH_PHI_ARG (use
, phi
, iter
, SSA_OP_USE
)
663 op
= USE_FROM_PTR (use
);
664 if (TREE_CODE (op
) == ADDR_EXPR
)
665 mark_address_taken (get_base_var (op
));
669 for (gsi
= gsi_start_bb (this_block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
670 scan_stmt_for_static_refs (&gsi
, fn
);
673 #ifdef ENABLE_CHECKING
674 /* Verify that all local initializers was expanded by gimplifier. */
675 for (step
= DECL_STRUCT_FUNCTION (decl
)->local_decls
;
677 step
= TREE_CHAIN (step
))
679 tree var
= TREE_VALUE (step
);
680 if (TREE_CODE (var
) == VAR_DECL
681 && DECL_INITIAL (var
)
682 && !TREE_STATIC (var
))
687 current_function_decl
= NULL
;
690 /* Remove local data associated with function FN. */
692 clean_function_local_data (struct cgraph_node
*fn
)
694 ipa_reference_vars_info_t info
= get_reference_vars_info (fn
);
695 ipa_reference_local_vars_info_t l
= info
->local
;
699 && l
->statics_read
!= all_module_statics
)
700 BITMAP_FREE (l
->statics_read
);
701 if (l
->statics_written
702 &&l
->statics_written
!= all_module_statics
)
703 BITMAP_FREE (l
->statics_written
);
709 /* Remove all data associated with function FN. */
712 clean_function (struct cgraph_node
*fn
)
714 ipa_reference_vars_info_t info
= get_reference_vars_info (fn
);
715 ipa_reference_global_vars_info_t g
= info
->global
;
717 clean_function_local_data (fn
);
721 && g
->statics_read
!= all_module_statics
)
722 BITMAP_FREE (g
->statics_read
);
724 if (g
->statics_written
725 && g
->statics_written
!= all_module_statics
)
726 BITMAP_FREE (g
->statics_written
);
728 if (g
->statics_not_read
729 && g
->statics_not_read
!= all_module_statics
)
730 BITMAP_FREE (g
->statics_not_read
);
732 if (g
->statics_not_written
733 && g
->statics_not_written
!= all_module_statics
)
734 BITMAP_FREE (g
->statics_not_written
);
739 free (get_reference_vars_info (fn
));
740 set_reference_vars_info (fn
, NULL
);
743 /* Called when new function is inserted to callgraph late. */
745 add_new_function (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
747 /* There are some shared nodes, in particular the initializers on
748 static declarations. We do not need to scan them more than once
749 since all we would be interested in are the addressof
751 analyze_function (node
);
752 visited_nodes
= NULL
;
756 copy_local_bitmap (bitmap src
)
761 if (src
== all_module_statics
)
762 return all_module_statics
;
763 dst
= BITMAP_ALLOC (&local_info_obstack
);
764 bitmap_copy (dst
, src
);
769 copy_global_bitmap (bitmap src
)
774 if (src
== all_module_statics
)
775 return all_module_statics
;
776 dst
= BITMAP_ALLOC (&global_info_obstack
);
777 bitmap_copy (dst
, src
);
781 /* Called when new clone is inserted to callgraph late. */
784 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
785 void *data ATTRIBUTE_UNUSED
)
787 ipa_reference_global_vars_info_t ginfo
;
788 ipa_reference_local_vars_info_t linfo
;
789 ipa_reference_global_vars_info_t dst_ginfo
;
790 ipa_reference_local_vars_info_t dst_linfo
;
792 ginfo
= get_global_reference_vars_info (src
);
793 linfo
= get_local_reference_vars_info (src
);
794 if (!linfo
&& !ginfo
)
796 init_function_info (dst
);
799 dst_linfo
= get_local_reference_vars_info (dst
);
800 dst_linfo
->statics_read
= copy_local_bitmap (linfo
->statics_read
);
801 dst_linfo
->statics_written
= copy_local_bitmap (linfo
->statics_written
);
802 dst_linfo
->calls_read_all
= linfo
->calls_read_all
;
803 dst_linfo
->calls_write_all
= linfo
->calls_write_all
;
807 get_reference_vars_info (dst
)->global
= XCNEW (struct ipa_reference_global_vars_info_d
);
808 dst_ginfo
= get_global_reference_vars_info (dst
);
809 dst_ginfo
->statics_read
= copy_global_bitmap (ginfo
->statics_read
);
810 dst_ginfo
->statics_written
= copy_global_bitmap (ginfo
->statics_written
);
811 dst_ginfo
->statics_not_read
= copy_global_bitmap (ginfo
->statics_not_read
);
812 dst_ginfo
->statics_not_written
= copy_global_bitmap (ginfo
->statics_not_written
);
816 /* Called when node is removed. */
819 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
821 if (get_reference_vars_info (node
))
822 clean_function (node
);
825 /* Analyze each function in the cgraph to see which global or statics
826 are read or written. */
829 generate_summary (void)
831 struct cgraph_node
*node
;
832 struct varpool_node
*vnode
;
835 bitmap module_statics_readonly
;
838 function_insertion_hook_holder
=
839 cgraph_add_function_insertion_hook (&add_new_function
, NULL
);
840 node_removal_hook_holder
=
841 cgraph_add_node_removal_hook (&remove_node_data
, NULL
);
842 node_duplication_hook_holder
=
843 cgraph_add_node_duplication_hook (&duplicate_node_data
, NULL
);
845 module_statics_readonly
= BITMAP_ALLOC (&local_info_obstack
);
846 bm_temp
= BITMAP_ALLOC (&local_info_obstack
);
848 /* Process all of the variables first. */
849 FOR_EACH_STATIC_INITIALIZER (vnode
)
850 analyze_variable (vnode
);
852 /* Process all of the functions next.
854 We do not want to process any of the clones so we check that this
855 is a master clone. However, we do need to process any
856 AVAIL_OVERWRITABLE functions (these are never clones) because
857 they may cause a static variable to escape. The code that can
858 overwrite such a function cannot access the statics because it
859 would not be in the same compilation unit. When the analysis is
860 finished, the computed information of these AVAIL_OVERWRITABLE is
861 replaced with worst case info.
863 for (node
= cgraph_nodes
; node
; node
= node
->next
)
864 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
865 analyze_function (node
);
867 pointer_set_destroy (visited_nodes
);
868 visited_nodes
= NULL
;
870 /* Prune out the variables that were found to behave badly
871 (i.e. have their address taken). */
872 EXECUTE_IF_SET_IN_BITMAP (module_statics_escape
, 0, index
, bi
)
874 splay_tree_remove (reference_vars_to_consider
, index
);
877 bitmap_and_compl_into (all_module_statics
,
878 module_statics_escape
);
880 bitmap_and_compl (module_statics_readonly
, all_module_statics
,
881 module_statics_written
);
883 /* If the address is not taken, we can unset the addressable bit
885 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
887 tree var
= get_static_decl (index
);
888 TREE_ADDRESSABLE (var
) = 0;
890 fprintf (dump_file
, "Not TREE_ADDRESSABLE var %s\n",
891 get_static_name (index
));
894 /* If the variable is never written, we can set the TREE_READONLY
895 flag. Additionally if it has a DECL_INITIAL that is made up of
896 constants we can treat the entire global as a constant. */
898 bitmap_and_compl (module_statics_readonly
, all_module_statics
,
899 module_statics_written
);
900 EXECUTE_IF_SET_IN_BITMAP (module_statics_readonly
, 0, index
, bi
)
902 tree var
= get_static_decl (index
);
904 /* Ignore variables in named sections - changing TREE_READONLY
905 changes the section flags, potentially causing conflicts with
906 other variables in the same named section. */
907 if (DECL_SECTION_NAME (var
) == NULL_TREE
)
909 TREE_READONLY (var
) = 1;
911 fprintf (dump_file
, "read-only var %s\n",
912 get_static_name (index
));
916 BITMAP_FREE(module_statics_escape
);
917 BITMAP_FREE(module_statics_written
);
918 module_statics_escape
= NULL
;
919 module_statics_written
= NULL
;
922 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
924 fprintf (dump_file
, "\nPromotable global:%s",
925 get_static_name (index
));
928 for (node
= cgraph_nodes
; node
; node
= node
->next
)
929 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
931 ipa_reference_local_vars_info_t l
;
932 l
= get_reference_vars_info (node
)->local
;
934 /* Any variables that are not in all_module_statics are
935 removed from the local maps. This will include all of the
936 variables that were found to escape in the function
938 bitmap_and_into (l
->statics_read
,
940 bitmap_and_into (l
->statics_written
,
944 BITMAP_FREE(module_statics_readonly
);
945 BITMAP_FREE(bm_temp
);
948 for (node
= cgraph_nodes
; node
; node
= node
->next
)
949 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
951 ipa_reference_local_vars_info_t l
;
955 l
= get_reference_vars_info (node
)->local
;
957 "\nFunction name:%s/%i:",
958 cgraph_node_name (node
), node
->uid
);
959 fprintf (dump_file
, "\n locals read: ");
960 EXECUTE_IF_SET_IN_BITMAP (l
->statics_read
,
963 fprintf (dump_file
, "%s ",
964 get_static_name (index
));
966 fprintf (dump_file
, "\n locals written: ");
967 EXECUTE_IF_SET_IN_BITMAP (l
->statics_written
,
970 fprintf(dump_file
, "%s ",
971 get_static_name (index
));
973 if (l
->calls_read_all
)
974 fprintf (dump_file
, "\n calls read all: ");
975 if (l
->calls_write_all
)
976 fprintf (dump_file
, "\n calls read all: ");
980 /* Produce the global information by preforming a transitive closure
981 on the local information that was produced by ipa_analyze_function
982 and ipa_analyze_variable. */
987 struct cgraph_node
*node
;
988 struct cgraph_node
*w
;
989 struct cgraph_node
**order
=
990 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
991 int order_pos
= ipa_utils_reduced_inorder (order
, false, true);
994 cgraph_remove_function_insertion_hook (function_insertion_hook_holder
);
996 dump_cgraph (dump_file
);
998 /* Propagate the local information thru the call graph to produce
999 the global information. All the nodes within a cycle will have
1000 the same info so we collapse cycles first. Then we can do the
1001 propagation in one pass from the leaves to the roots. */
1002 order_pos
= ipa_utils_reduced_inorder (order
, true, true);
1004 ipa_utils_print_order(dump_file
, "reduced", order
, order_pos
);
1006 for (i
= 0; i
< order_pos
; i
++ )
1008 ipa_reference_vars_info_t node_info
;
1009 ipa_reference_global_vars_info_t node_g
=
1010 XCNEW (struct ipa_reference_global_vars_info_d
);
1011 ipa_reference_local_vars_info_t node_l
;
1015 struct ipa_dfs_info
* w_info
;
1018 node_info
= get_reference_vars_info (node
);
1021 dump_cgraph_node (stderr
, node
);
1022 dump_cgraph (stderr
);
1026 gcc_assert (!node_info
->global
);
1027 node_l
= node_info
->local
;
1029 read_all
= node_l
->calls_read_all
;
1030 write_all
= node_l
->calls_write_all
;
1032 /* If any node in a cycle is calls_read_all or calls_write_all
1034 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1035 w
= w_info
->next_cycle
;
1038 ipa_reference_local_vars_info_t w_l
=
1039 get_reference_vars_info (w
)->local
;
1040 read_all
|= w_l
->calls_read_all
;
1041 write_all
|= w_l
->calls_write_all
;
1043 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1044 w
= w_info
->next_cycle
;
1047 /* Initialized the bitmaps for the reduced nodes */
1049 node_g
->statics_read
= all_module_statics
;
1052 node_g
->statics_read
= BITMAP_ALLOC (&global_info_obstack
);
1053 bitmap_copy (node_g
->statics_read
,
1054 node_l
->statics_read
);
1058 node_g
->statics_written
= all_module_statics
;
1061 node_g
->statics_written
= BITMAP_ALLOC (&global_info_obstack
);
1062 bitmap_copy (node_g
->statics_written
,
1063 node_l
->statics_written
);
1066 propagate_bits (node_g
, node
);
1067 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1068 w
= w_info
->next_cycle
;
1071 ipa_reference_vars_info_t w_ri
=
1072 get_reference_vars_info (w
);
1073 ipa_reference_local_vars_info_t w_l
= w_ri
->local
;
1075 /* These global bitmaps are initialized from the local info
1076 of all of the nodes in the region. However there is no
1077 need to do any work if the bitmaps were set to
1078 all_module_statics. */
1080 bitmap_ior_into (node_g
->statics_read
,
1083 bitmap_ior_into (node_g
->statics_written
,
1084 w_l
->statics_written
);
1085 propagate_bits (node_g
, w
);
1086 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1087 w
= w_info
->next_cycle
;
1090 /* All nodes within a cycle have the same global info bitmaps. */
1091 node_info
->global
= node_g
;
1092 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1093 w
= w_info
->next_cycle
;
1096 ipa_reference_vars_info_t w_ri
=
1097 get_reference_vars_info (w
);
1099 gcc_assert (!w_ri
->global
);
1100 w_ri
->global
= XCNEW (struct ipa_reference_global_vars_info_d
);
1101 w_ri
->global
->statics_read
= copy_global_bitmap (node_g
->statics_read
);
1102 w_ri
->global
->statics_written
= copy_global_bitmap (node_g
->statics_written
);
1104 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1105 w
= w_info
->next_cycle
;
1111 for (i
= 0; i
< order_pos
; i
++ )
1113 ipa_reference_vars_info_t node_info
;
1114 ipa_reference_global_vars_info_t node_g
;
1115 ipa_reference_local_vars_info_t node_l
;
1118 struct ipa_dfs_info
* w_info
;
1121 node_info
= get_reference_vars_info (node
);
1122 node_g
= node_info
->global
;
1123 node_l
= node_info
->local
;
1125 "\nFunction name:%s/%i:",
1126 cgraph_node_name (node
), node
->uid
);
1127 fprintf (dump_file
, "\n locals read: ");
1128 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_read
,
1131 fprintf (dump_file
, "%s ",
1132 get_static_name (index
));
1134 fprintf (dump_file
, "\n locals written: ");
1135 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_written
,
1138 fprintf(dump_file
, "%s ",
1139 get_static_name (index
));
1142 w_info
= (struct ipa_dfs_info
*) node
->aux
;
1143 w
= w_info
->next_cycle
;
1146 ipa_reference_vars_info_t w_ri
=
1147 get_reference_vars_info (w
);
1148 ipa_reference_local_vars_info_t w_l
= w_ri
->local
;
1149 fprintf (dump_file
, "\n next cycle: %s/%i ",
1150 cgraph_node_name (w
), w
->uid
);
1151 fprintf (dump_file
, "\n locals read: ");
1152 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_read
,
1155 fprintf (dump_file
, "%s ",
1156 get_static_name (index
));
1159 fprintf (dump_file
, "\n locals written: ");
1160 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_written
,
1163 fprintf(dump_file
, "%s ",
1164 get_static_name (index
));
1168 w_info
= (struct ipa_dfs_info
*) w
->aux
;
1169 w
= w_info
->next_cycle
;
1171 fprintf (dump_file
, "\n globals read: ");
1172 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_read
,
1175 fprintf (dump_file
, "%s ",
1176 get_static_name (index
));
1178 fprintf (dump_file
, "\n globals written: ");
1179 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_written
,
1182 fprintf (dump_file
, "%s ",
1183 get_static_name (index
));
1189 for (i
= 0; i
< order_pos
; i
++ )
1191 ipa_reference_vars_info_t node_info
;
1192 ipa_reference_global_vars_info_t node_g
;
1194 node_info
= get_reference_vars_info (node
);
1195 node_g
= node_info
->global
;
1197 /* Create the complimentary sets. These are more useful for
1199 node_g
->statics_not_read
= BITMAP_ALLOC (&global_info_obstack
);
1200 node_g
->statics_not_written
= BITMAP_ALLOC (&global_info_obstack
);
1202 if (node_g
->statics_read
!= all_module_statics
)
1203 bitmap_and_compl (node_g
->statics_not_read
,
1205 node_g
->statics_read
);
1207 if (node_g
->statics_written
1208 != all_module_statics
)
1209 bitmap_and_compl (node_g
->statics_not_written
,
1211 node_g
->statics_written
);
1216 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1218 ipa_reference_vars_info_t node_info
;
1219 node_info
= get_reference_vars_info (node
);
1220 /* Get rid of the aux information. */
1228 if (cgraph_function_body_availability (node
) == AVAIL_OVERWRITABLE
)
1229 clean_function (node
);
1231 clean_function_local_data (node
);
1233 bitmap_obstack_release (&local_info_obstack
);
1239 gate_reference (void)
1241 return (flag_ipa_reference
1242 /* Don't bother doing anything if the program has errors. */
1243 && !(errorcount
|| sorrycount
));
1246 struct ipa_opt_pass pass_ipa_reference
=
1250 "static-var", /* name */
1251 gate_reference
, /* gate */
1252 propagate
, /* execute */
1255 0, /* static_pass_number */
1256 TV_IPA_REFERENCE
, /* tv_id */
1257 0, /* properties_required */
1258 0, /* properties_provided */
1259 0, /* properties_destroyed */
1260 0, /* todo_flags_start */
1261 0 /* todo_flags_finish */
1263 generate_summary
, /* generate_summary */
1264 NULL
, /* write_summary */
1265 NULL
, /* read_summary */
1266 NULL
, /* function_read_summary */
1268 NULL
, /* function_transform */
1269 NULL
/* variable_transform */
1272 #include "gt-ipa-reference.h"