1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* This file gathers information about how variables whose scope is
24 confined to the compilation unit are used.
26 There are two categories of information produced by this pass:
28 1) The addressable (TREE_ADDRESSABLE) bit and readonly
29 (TREE_READONLY) bit associated with these variables is properly set
30 based on scanning all of the code withing the compilation unit.
32 2) The transitive call site specific clobber effects are computed
33 for the variables whose scope is contained within this compilation
36 First each function and static variable initialization is analyzed
37 to determine which local static variables are either read, written,
38 or have their address taken. Any local static that has its address
39 taken is removed from consideration. Once the local read and
40 writes are determined, a transitive closure of this information is
41 performed over the call graph to determine the worst case set of
42 side effects of each call. In later parts of the compiler, these
43 local and global sets are examined to make the call clobbering less
44 traumatic, promote some statics to registers, and improve aliasing
47 Currently must be run after inlining decisions have been made since
48 otherwise, the local sets will not contain information that is
49 consistent with post inlined state. The global sets are not prone
50 to this problem since they are by definition transitive.
55 #include "coretypes.h"
58 #include "tree-flow.h"
59 #include "tree-inline.h"
60 #include "tree-pass.h"
61 #include "langhooks.h"
62 #include "pointer-set.h"
64 #include "ipa-utils.h"
65 #include "ipa-reference.h"
67 #include "tree-gimple.h"
72 #include "diagnostic.h"
73 #include "langhooks.h"
75 /* This splay tree contains all of the static variables that are
76 being considered by the compilation level alias analysis. For
77 module_at_a_time compilation, this is the set of static but not
78 public variables. Any variables that either have their address
79 taken or participate in otherwise unsavory operations are deleted
81 static GTY((param1_is(int), param2_is(tree
)))
82 splay_tree reference_vars_to_consider
;
84 /* This bitmap is used to knock out the module static variables whose
85 addresses have been taken and passed around. */
86 static bitmap module_statics_escape
;
88 /* This bitmap is used to knock out the module static variables that
90 static bitmap module_statics_written
;
92 /* A bit is set for every module static we are considering. This is
93 ored into the local info when asm code is found that clobbers all
95 static bitmap all_module_statics
;
97 static struct pointer_set_t
*visited_nodes
;
99 static bitmap_obstack ipa_obstack
;
101 enum initialization_status_t
108 tree memory_identifier_string
;
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
)
114 return get_var_ann (node
->decl
)->reference_vars_info
;
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
)
122 ipa_reference_vars_info_t info
= get_var_ann (fn
)->reference_vars_info
;
127 /* This phase was not run. */
131 /* Get a bitmap that contains all of the globally referenced static
132 variables for function FN. */
134 static ipa_reference_global_vars_info_t
135 get_global_reference_vars_info (tree fn
)
137 ipa_reference_vars_info_t info
= get_var_ann (fn
)->reference_vars_info
;
142 /* This phase was not run. */
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. */
151 ipa_reference_get_read_local (tree fn
)
153 ipa_reference_local_vars_info_t l
= get_local_reference_vars_info (fn
);
155 return l
->statics_read
;
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. */
165 ipa_reference_get_written_local (tree fn
)
167 ipa_reference_local_vars_info_t l
= get_local_reference_vars_info (fn
);
169 return l
->statics_written
;
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. */
179 ipa_reference_get_read_global (tree fn
)
181 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
183 return g
->statics_read
;
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. */
194 ipa_reference_get_written_global (tree fn
)
196 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
198 return g
->statics_written
;
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. */
208 ipa_reference_get_not_read_global (tree fn
)
210 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
212 return g
->statics_not_read
;
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. */
223 ipa_reference_get_not_written_global (tree fn
)
225 ipa_reference_global_vars_info_t g
= get_global_reference_vars_info (fn
);
227 return g
->statics_not_written
;
234 /* Add VAR to all_module_statics and the two
235 reference_vars_to_consider* sets. */
238 add_static_var (tree var
)
240 int uid
= DECL_UID (var
);
241 if (!bitmap_bit_p (all_module_statics
, uid
))
243 splay_tree_insert (reference_vars_to_consider
,
244 uid
, (splay_tree_value
)var
);
245 bitmap_set_bit (all_module_statics
, uid
);
249 /* Return true if the variable T is the right kind of static variable to
250 perform compilation unit scope escape analysis. */
253 has_proper_scope_for_analysis (tree t
)
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
)))
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
))
265 /* Do not care about a local automatic that is not static. */
266 if (!TREE_STATIC (t
) && !DECL_EXTERNAL (t
))
269 if (DECL_EXTERNAL (t
) || TREE_PUBLIC (t
))
272 /* This is a variable we care about. Check if we have seen it
273 before, and if not add it the set of variables we care about. */
274 if (!bitmap_bit_p (all_module_statics
, DECL_UID (t
)))
280 /* If T is a VAR_DECL for a static that we are interested in, add the
281 uid to the bitmap. */
284 check_operand (ipa_reference_local_vars_info_t local
,
285 tree t
, bool checking_write
)
289 if ((TREE_CODE (t
) == VAR_DECL
)
290 && (has_proper_scope_for_analysis (t
)))
295 bitmap_set_bit (local
->statics_written
, DECL_UID (t
));
296 /* Mark the write so we can tell which statics are
298 bitmap_set_bit (module_statics_written
, DECL_UID (t
));
301 bitmap_set_bit (local
->statics_read
, DECL_UID (t
));
305 /* Examine tree T for references to static variables. All internal
306 references like array references or indirect references are added
307 to the READ_BM. Direct references are added to either READ_BM or
308 WRITE_BM depending on the value of CHECKING_WRITE. */
311 check_tree (ipa_reference_local_vars_info_t local
, tree t
, bool checking_write
)
313 if ((TREE_CODE (t
) == EXC_PTR_EXPR
) || (TREE_CODE (t
) == FILTER_EXPR
))
316 while (TREE_CODE (t
) == REALPART_EXPR
317 || TREE_CODE (t
) == IMAGPART_EXPR
318 || handled_component_p (t
))
320 if (TREE_CODE (t
) == ARRAY_REF
)
321 check_operand (local
, TREE_OPERAND (t
, 1), false);
322 t
= TREE_OPERAND (t
, 0);
325 /* The bottom of an indirect reference can only be read, not
326 written. So just recurse and whatever we find, check it against
329 /* if (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF) */
330 /* FIXME when we have array_ref's of pointers. */
331 if (INDIRECT_REF_P (t
))
332 check_tree (local
, TREE_OPERAND (t
, 0), false);
335 check_operand (local
, t
, checking_write
);
338 /* Scan tree T to see if there are any addresses taken in within T. */
341 look_for_address_of (tree t
)
343 if (TREE_CODE (t
) == ADDR_EXPR
)
345 tree x
= get_base_var (t
);
346 if (TREE_CODE (x
) == VAR_DECL
)
347 if (has_proper_scope_for_analysis (x
))
348 bitmap_set_bit (module_statics_escape
, DECL_UID (x
));
352 /* Check to see if T is a read or address of operation on a static var
353 we are interested in analyzing. LOCAL is passed in to get access
354 to its bit vectors. Local is NULL if this is called from a static
358 check_rhs_var (ipa_reference_local_vars_info_t local
, tree t
)
360 look_for_address_of (t
);
365 check_tree(local
, t
, false);
368 /* Check to see if T is an assignment to a static var we are
369 interested in analyzing. LOCAL is passed in to get access to its bit
373 check_lhs_var (ipa_reference_local_vars_info_t local
, tree t
)
378 check_tree(local
, t
, true);
381 /* This is a scaled down version of get_asm_expr_operands from
382 tree_ssa_operands.c. The version there runs much later and assumes
383 that aliasing information is already available. Here we are just
384 trying to find if the set of inputs and outputs contain references
385 or address of operations to local static variables. FN is the
386 function being analyzed and STMT is the actual asm statement. */
389 get_asm_expr_operands (ipa_reference_local_vars_info_t local
, tree stmt
)
391 int noutputs
= list_length (ASM_OUTPUTS (stmt
));
392 const char **oconstraints
393 = (const char **) alloca ((noutputs
) * sizeof (const char *));
396 const char *constraint
;
397 bool allows_mem
, allows_reg
, is_inout
;
399 for (i
=0, link
= ASM_OUTPUTS (stmt
); link
; ++i
, link
= TREE_CHAIN (link
))
401 oconstraints
[i
] = constraint
402 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
403 parse_output_constraint (&constraint
, i
, 0, 0,
404 &allows_mem
, &allows_reg
, &is_inout
);
406 check_lhs_var (local
, TREE_VALUE (link
));
409 for (link
= ASM_INPUTS (stmt
); link
; link
= TREE_CHAIN (link
))
412 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
413 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
414 oconstraints
, &allows_mem
, &allows_reg
);
416 check_rhs_var (local
, TREE_VALUE (link
));
419 for (link
= ASM_CLOBBERS (stmt
); link
; link
= TREE_CHAIN (link
))
420 if (simple_cst_equal(TREE_VALUE (link
), memory_identifier_string
) == 1)
422 /* Abandon all hope, ye who enter here. */
423 local
->calls_read_all
= true;
424 local
->calls_write_all
= true;
428 /* Check the parameters of a function call from CALLER to CALL_EXPR to
429 see if any of them are static vars. Also check to see if this is
430 either an indirect call, a call outside the compilation unit, or
431 has special attributes that effect the clobbers. The caller
432 parameter is the tree node for the caller and the second operand is
433 the tree node for the entire call expression. */
436 check_call (ipa_reference_local_vars_info_t local
, tree call_expr
)
438 int flags
= call_expr_flags (call_expr
);
439 tree operand_list
= TREE_OPERAND (call_expr
, 1);
441 tree callee_t
= get_callee_fndecl (call_expr
);
442 enum availability avail
= AVAIL_NOT_AVAILABLE
;
444 for (operand
= operand_list
;
445 operand
!= NULL_TREE
;
446 operand
= TREE_CHAIN (operand
))
448 tree argument
= TREE_VALUE (operand
);
449 check_rhs_var (local
, argument
);
454 struct cgraph_node
* callee
= cgraph_node(callee_t
);
455 avail
= cgraph_function_body_availability (callee
);
458 if (avail
== AVAIL_NOT_AVAILABLE
|| avail
== AVAIL_OVERWRITABLE
)
461 if (flags
& ECF_PURE
)
462 local
->calls_read_all
= true;
465 local
->calls_read_all
= true;
466 local
->calls_write_all
= true;
471 /* TP is the part of the tree currently under the microscope.
472 WALK_SUBTREES is part of the walk_tree api but is unused here.
473 DATA is cgraph_node of the function being walked. */
475 /* FIXME: When this is converted to run over SSA form, this code
476 should be converted to use the operand scanner. */
479 scan_for_static_refs (tree
*tp
,
483 struct cgraph_node
*fn
= data
;
485 ipa_reference_local_vars_info_t local
= NULL
;
487 local
= get_reference_vars_info_from_cgraph (fn
)->local
;
489 switch (TREE_CODE (t
))
492 if (DECL_INITIAL (t
))
493 walk_tree (&DECL_INITIAL (t
), scan_for_static_refs
, fn
, visited_nodes
);
499 /* First look on the lhs and see what variable is stored to */
500 tree lhs
= TREE_OPERAND (t
, 0);
501 tree rhs
= TREE_OPERAND (t
, 1);
502 check_lhs_var (local
, lhs
);
504 /* For the purposes of figuring out what the cast affects */
506 /* Next check the operands on the rhs to see if they are ok. */
507 switch (TREE_CODE_CLASS (TREE_CODE (rhs
)))
511 tree op0
= TREE_OPERAND (rhs
, 0);
512 tree op1
= TREE_OPERAND (rhs
, 1);
513 check_rhs_var (local
, op0
);
514 check_rhs_var (local
, op1
);
519 tree op0
= TREE_OPERAND (rhs
, 0);
520 check_rhs_var (local
, op0
);
525 check_rhs_var (local
, rhs
);
527 case tcc_declaration
:
528 check_rhs_var (local
, rhs
);
531 switch (TREE_CODE (rhs
))
534 check_rhs_var (local
, rhs
);
537 check_call (local
, rhs
);
551 /* This case is here to find addresses on rhs of constructors in
552 decl_initial of static variables. */
553 check_rhs_var (local
, t
);
558 if (DECL_NONLOCAL (TREE_OPERAND (t
, 0)))
560 /* Target of long jump. */
561 local
->calls_read_all
= true;
562 local
->calls_write_all
= true;
567 check_call (local
, t
);
572 get_asm_expr_operands (local
, t
);
583 /* Lookup the tree node for the static variable that has UID. */
585 get_static_decl (int index
)
587 splay_tree_node stn
=
588 splay_tree_lookup (reference_vars_to_consider
, index
);
590 return (tree
)stn
->value
;
594 /* Lookup the tree node for the static variable that has UID and
595 convert the name to a string for debugging. */
598 get_static_name (int index
)
600 splay_tree_node stn
=
601 splay_tree_lookup (reference_vars_to_consider
, index
);
603 return lang_hooks
.decl_printable_name ((tree
)(stn
->value
), 2);
607 /* Or in all of the bits from every callee into X, the caller's, bit
608 vector. There are several cases to check to avoid the sparse
612 propagate_bits (struct cgraph_node
*x
)
614 ipa_reference_vars_info_t x_info
= get_reference_vars_info_from_cgraph (x
);
615 ipa_reference_global_vars_info_t x_global
= x_info
->global
;
617 struct cgraph_edge
*e
;
618 for (e
= x
->callees
; e
; e
= e
->next_callee
)
620 struct cgraph_node
*y
= e
->callee
;
622 /* Only look at the master nodes and skip external nodes. */
623 y
= cgraph_master_clone (y
);
626 if (get_reference_vars_info_from_cgraph (y
))
628 ipa_reference_vars_info_t y_info
= get_reference_vars_info_from_cgraph (y
);
629 ipa_reference_global_vars_info_t y_global
= y_info
->global
;
631 if (x_global
->statics_read
632 != all_module_statics
)
634 if (y_global
->statics_read
635 == all_module_statics
)
637 BITMAP_FREE (x_global
->statics_read
);
638 x_global
->statics_read
639 = all_module_statics
;
641 /* Skip bitmaps that are pointer equal to node's bitmap
642 (no reason to spin within the cycle). */
643 else if (x_global
->statics_read
644 != y_global
->statics_read
)
645 bitmap_ior_into (x_global
->statics_read
,
646 y_global
->statics_read
);
649 if (x_global
->statics_written
650 != all_module_statics
)
652 if (y_global
->statics_written
653 == all_module_statics
)
655 BITMAP_FREE (x_global
->statics_written
);
656 x_global
->statics_written
657 = all_module_statics
;
659 /* Skip bitmaps that are pointer equal to node's bitmap
660 (no reason to spin within the cycle). */
661 else if (x_global
->statics_written
662 != y_global
->statics_written
)
663 bitmap_ior_into (x_global
->statics_written
,
664 y_global
->statics_written
);
675 /* Look at all of the callees of X to see which ones represent inlined
676 calls. For each of these callees, merge their local info into
677 TARGET and check their children recursively.
679 This function goes away when Jan changes the inliner and IPA
680 analysis so that this is not run between the time when inlining
681 decisions are made and when the inlining actually occurs. */
684 merge_callee_local_info (struct cgraph_node
*target
,
685 struct cgraph_node
*x
)
687 struct cgraph_edge
*e
;
688 ipa_reference_local_vars_info_t x_l
=
689 get_reference_vars_info_from_cgraph (target
)->local
;
691 /* Make the world safe for tail recursion. */
692 struct ipa_dfs_info
*node_info
= x
->aux
;
699 for (e
= x
->callees
; e
; e
= e
->next_callee
)
701 struct cgraph_node
*y
= e
->callee
;
702 if (y
->global
.inlined_to
)
704 ipa_reference_vars_info_t y_info
;
705 ipa_reference_local_vars_info_t y_l
;
706 struct cgraph_node
* orig_y
= y
;
708 y
= cgraph_master_clone (y
);
711 y_info
= get_reference_vars_info_from_cgraph (y
);
715 bitmap_ior_into (x_l
->statics_read
,
717 bitmap_ior_into (x_l
->statics_written
,
718 y_l
->statics_written
);
720 x_l
->calls_read_all
|= y_l
->calls_read_all
;
721 x_l
->calls_write_all
|= y_l
->calls_write_all
;
722 merge_callee_local_info (target
, y
);
726 fprintf(stderr
, "suspect inlining of ");
727 dump_cgraph_node (stderr
, orig_y
);
728 fprintf(stderr
, "\ninto ");
729 dump_cgraph_node (stderr
, target
);
730 dump_cgraph (stderr
);
736 node_info
->aux
= NULL
;
739 /* The init routine for analyzing global static variable usage. See
740 comments at top for description. */
744 memory_identifier_string
= build_string(7, "memory");
746 reference_vars_to_consider
=
747 splay_tree_new_ggc (splay_tree_compare_ints
);
749 bitmap_obstack_initialize (&ipa_obstack
);
750 module_statics_escape
= BITMAP_ALLOC (&ipa_obstack
);
751 module_statics_written
= BITMAP_ALLOC (&ipa_obstack
);
752 all_module_statics
= BITMAP_ALLOC (&ipa_obstack
);
754 /* There are some shared nodes, in particular the initializers on
755 static declarations. We do not need to scan them more than once
756 since all we would be interested in are the addressof
758 visited_nodes
= pointer_set_create ();
761 /* Check out the rhs of a static or global initialization VNODE to see
762 if any of them contain addressof operations. Note that some of
763 these variables may not even be referenced in the code in this
764 compilation unit but their right hand sides may contain references
765 to variables defined within this unit. */
768 analyze_variable (struct cgraph_varpool_node
*vnode
)
770 tree global
= vnode
->decl
;
771 if (TREE_CODE (global
) == VAR_DECL
)
773 if (DECL_INITIAL (global
))
774 walk_tree (&DECL_INITIAL (global
), scan_for_static_refs
,
775 NULL
, visited_nodes
);
777 else gcc_unreachable ();
780 /* This is the main routine for finding the reference patterns for
781 global variables within a function FN. */
784 analyze_function (struct cgraph_node
*fn
)
786 ipa_reference_vars_info_t info
787 = xcalloc (1, sizeof (struct ipa_reference_vars_info_d
));
788 ipa_reference_local_vars_info_t l
789 = xcalloc (1, sizeof (struct ipa_reference_local_vars_info_d
));
790 tree decl
= fn
->decl
;
792 /* Add the info to the tree's annotation. */
793 get_var_ann (fn
->decl
)->reference_vars_info
= info
;
796 l
->statics_read
= BITMAP_ALLOC (&ipa_obstack
);
797 l
->statics_written
= BITMAP_ALLOC (&ipa_obstack
);
800 fprintf (dump_file
, "\n local analysis of %s\n", cgraph_node_name (fn
));
803 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (decl
);
804 basic_block this_block
;
806 FOR_EACH_BB_FN (this_block
, this_cfun
)
808 block_stmt_iterator bsi
;
809 for (bsi
= bsi_start (this_block
); !bsi_end_p (bsi
); bsi_next (&bsi
))
810 walk_tree (bsi_stmt_ptr (bsi
), scan_for_static_refs
,
815 /* There may be const decls with interesting right hand sides. */
816 if (DECL_STRUCT_FUNCTION (decl
))
819 for (step
= DECL_STRUCT_FUNCTION (decl
)->unexpanded_var_list
;
821 step
= TREE_CHAIN (step
))
823 tree var
= TREE_VALUE (step
);
824 if (TREE_CODE (var
) == VAR_DECL
825 && DECL_INITIAL (var
)
826 && !TREE_STATIC (var
))
827 walk_tree (&DECL_INITIAL (var
), scan_for_static_refs
,
833 /* If FN is avail == AVAIL_OVERWRITABLE, replace the effects bit
834 vectors with worst case bit vectors. We had to analyze it above to
835 find out if it took the address of any statics. However, now that
836 we know that, we can get rid of all of the other side effects. */
839 clean_function (struct cgraph_node
*fn
)
841 ipa_reference_vars_info_t info
= get_reference_vars_info_from_cgraph (fn
);
842 ipa_reference_local_vars_info_t l
= info
->local
;
843 ipa_reference_global_vars_info_t g
= info
->global
;
848 && l
->statics_read
!= all_module_statics
)
849 BITMAP_FREE (l
->statics_read
);
850 if (l
->statics_written
851 &&l
->statics_written
!= all_module_statics
)
852 BITMAP_FREE (l
->statics_written
);
859 && g
->statics_read
!= all_module_statics
)
860 BITMAP_FREE (g
->statics_read
);
862 if (g
->statics_written
863 && g
->statics_written
!= all_module_statics
)
864 BITMAP_FREE (g
->statics_written
);
866 if (g
->statics_not_read
867 && g
->statics_not_read
!= all_module_statics
)
868 BITMAP_FREE (g
->statics_not_read
);
870 if (g
->statics_not_written
871 && g
->statics_not_written
!= all_module_statics
)
872 BITMAP_FREE (g
->statics_not_written
);
877 free (get_var_ann (fn
->decl
)->reference_vars_info
);
878 get_var_ann (fn
->decl
)->reference_vars_info
= NULL
;
882 /* Produce the global information by preforming a transitive closure
883 on the local information that was produced by ipa_analyze_function
884 and ipa_analyze_variable. */
887 static_execute (void)
889 struct cgraph_node
*node
;
890 struct cgraph_varpool_node
*vnode
;
891 struct cgraph_node
*w
;
892 struct cgraph_node
**order
=
893 xcalloc (cgraph_n_nodes
, sizeof (struct cgraph_node
*));
894 int order_pos
= order_pos
= ipa_utils_reduced_inorder (order
, false, true);
899 /* Process all of the variables first. */
900 for (vnode
= cgraph_varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
901 analyze_variable (vnode
);
903 /* Process all of the functions next.
905 We do not want to process any of the clones so we check that this
906 is a master clone. However, we do need to process any
907 AVAIL_OVERWRITABLE functions (these are never clones) because
908 they may cause a static variable to escape. The code that can
909 overwrite such a function cannot access the statics because it
910 would not be in the same compilation unit. When the analysis is
911 finished, the computed information of these AVAIL_OVERWRITABLE is
912 replaced with worst case info.
914 for (node
= cgraph_nodes
; node
; node
= node
->next
)
916 && (cgraph_is_master_clone (node
)
917 || (cgraph_function_body_availability (node
)
918 == AVAIL_OVERWRITABLE
)))
919 analyze_function (node
);
921 pointer_set_destroy (visited_nodes
);
922 visited_nodes
= NULL
;
924 dump_cgraph (dump_file
);
926 /* Prune out the variables that were found to behave badly
927 (i.e. have their address taken). */
931 bitmap module_statics_readonly
= BITMAP_ALLOC (&ipa_obstack
);
932 bitmap module_statics_const
= BITMAP_ALLOC (&ipa_obstack
);
933 bitmap bm_temp
= BITMAP_ALLOC (&ipa_obstack
);
935 EXECUTE_IF_SET_IN_BITMAP (module_statics_escape
, 0, index
, bi
)
937 splay_tree_remove (reference_vars_to_consider
, index
);
940 bitmap_and_compl_into (all_module_statics
,
941 module_statics_escape
);
943 bitmap_and_compl (module_statics_readonly
, all_module_statics
,
944 module_statics_written
);
946 /* If the address is not taken, we can unset the addressable bit
948 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
950 tree var
= get_static_decl (index
);
951 TREE_ADDRESSABLE (var
) = 0;
953 fprintf (dump_file
, "Not TREE_ADDRESSABLE var %s\n",
954 get_static_name (index
));
957 /* If the variable is never written, we can set the TREE_READONLY
958 flag. Additionally if it has a DECL_INITIAL that is made up of
959 constants we can treat the entire global as a constant. */
961 bitmap_and_compl (module_statics_readonly
, all_module_statics
,
962 module_statics_written
);
963 EXECUTE_IF_SET_IN_BITMAP (module_statics_readonly
, 0, index
, bi
)
965 tree var
= get_static_decl (index
);
966 TREE_READONLY (var
) = 1;
968 fprintf (dump_file
, "read-only var %s\n",
969 get_static_name (index
));
970 if (DECL_INITIAL (var
)
971 && is_gimple_min_invariant (DECL_INITIAL (var
)))
973 bitmap_set_bit (module_statics_const
, index
);
975 fprintf (dump_file
, "read-only constant %s\n",
976 get_static_name (index
));
980 BITMAP_FREE(module_statics_escape
);
981 BITMAP_FREE(module_statics_written
);
984 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
986 fprintf (dump_file
, "\nPromotable global:%s",
987 get_static_name (index
));
990 for (i
= 0; i
< order_pos
; i
++ )
992 ipa_reference_local_vars_info_t l
;
994 l
= get_reference_vars_info_from_cgraph (node
)->local
;
996 /* Any variables that are not in all_module_statics are
997 removed from the local maps. This will include all of the
998 variables that were found to escape in the function
1000 bitmap_and_into (l
->statics_read
,
1001 all_module_statics
);
1002 bitmap_and_into (l
->statics_written
,
1003 all_module_statics
);
1006 BITMAP_FREE(module_statics_readonly
);
1007 BITMAP_FREE(module_statics_const
);
1008 BITMAP_FREE(bm_temp
);
1013 for (i
= 0; i
< order_pos
; i
++ )
1016 ipa_reference_local_vars_info_t l
;
1020 l
= get_reference_vars_info_from_cgraph (node
)->local
;
1022 "\nFunction name:%s/%i:",
1023 cgraph_node_name (node
), node
->uid
);
1024 fprintf (dump_file
, "\n locals read: ");
1025 EXECUTE_IF_SET_IN_BITMAP (l
->statics_read
,
1028 fprintf (dump_file
, "%s ",
1029 get_static_name (index
));
1031 fprintf (dump_file
, "\n locals written: ");
1032 EXECUTE_IF_SET_IN_BITMAP (l
->statics_written
,
1035 fprintf(dump_file
, "%s ",
1036 get_static_name (index
));
1041 /* Propagate the local information thru the call graph to produce
1042 the global information. All the nodes within a cycle will have
1043 the same info so we collapse cycles first. Then we can do the
1044 propagation in one pass from the leaves to the roots. */
1045 order_pos
= ipa_utils_reduced_inorder (order
, true, true);
1047 ipa_utils_print_order(dump_file
, "reduced", order
, order_pos
);
1049 for (i
= 0; i
< order_pos
; i
++ )
1051 ipa_reference_vars_info_t node_info
;
1052 ipa_reference_global_vars_info_t node_g
=
1053 xcalloc (1, sizeof (struct ipa_reference_global_vars_info_d
));
1054 ipa_reference_local_vars_info_t node_l
;
1058 struct ipa_dfs_info
* w_info
;
1061 node_info
= get_reference_vars_info_from_cgraph (node
);
1064 dump_cgraph_node (stderr
, node
);
1065 dump_cgraph (stderr
);
1069 node_info
->global
= node_g
;
1070 node_l
= node_info
->local
;
1072 read_all
= node_l
->calls_read_all
;
1073 write_all
= node_l
->calls_write_all
;
1075 /* If any node in a cycle is calls_read_all or calls_write_all
1078 w
= w_info
->next_cycle
;
1081 ipa_reference_local_vars_info_t w_l
=
1082 get_reference_vars_info_from_cgraph (w
)->local
;
1083 read_all
|= w_l
->calls_read_all
;
1084 write_all
|= w_l
->calls_write_all
;
1087 w
= w_info
->next_cycle
;
1090 /* Initialized the bitmaps for the reduced nodes */
1092 node_g
->statics_read
= all_module_statics
;
1095 node_g
->statics_read
= BITMAP_ALLOC (&ipa_obstack
);
1096 bitmap_copy (node_g
->statics_read
,
1097 node_l
->statics_read
);
1101 node_g
->statics_written
= all_module_statics
;
1104 node_g
->statics_written
= BITMAP_ALLOC (&ipa_obstack
);
1105 bitmap_copy (node_g
->statics_written
,
1106 node_l
->statics_written
);
1110 w
= w_info
->next_cycle
;
1113 ipa_reference_vars_info_t w_ri
=
1114 get_reference_vars_info_from_cgraph (w
);
1115 ipa_reference_local_vars_info_t w_l
= w_ri
->local
;
1117 /* All nodes within a cycle share the same global info bitmaps. */
1118 w_ri
->global
= node_g
;
1120 /* These global bitmaps are initialized from the local info
1121 of all of the nodes in the region. However there is no
1122 need to do any work if the bitmaps were set to
1123 all_module_statics. */
1125 bitmap_ior_into (node_g
->statics_read
,
1128 bitmap_ior_into (node_g
->statics_written
,
1129 w_l
->statics_written
);
1131 w
= w_info
->next_cycle
;
1139 w
= w_info
->next_cycle
;
1143 /* Need to fix up the local information sets. The information that
1144 has been gathered so far is preinlining. However, the
1145 compilation will progress post inlining so the local sets for the
1146 inlined calls need to be merged into the callers. Note that the
1147 local sets are not shared between all of the nodes in a cycle so
1148 those nodes in the cycle must be processed explicitly. */
1149 for (i
= 0; i
< order_pos
; i
++ )
1151 struct ipa_dfs_info
* w_info
;
1153 merge_callee_local_info (node
, node
);
1156 w
= w_info
->next_cycle
;
1159 merge_callee_local_info (w
, w
);
1161 w
= w_info
->next_cycle
;
1167 for (i
= 0; i
< order_pos
; i
++ )
1169 ipa_reference_vars_info_t node_info
;
1170 ipa_reference_global_vars_info_t node_g
;
1171 ipa_reference_local_vars_info_t node_l
;
1174 struct ipa_dfs_info
* w_info
;
1177 node_info
= get_reference_vars_info_from_cgraph (node
);
1178 node_g
= node_info
->global
;
1179 node_l
= node_info
->local
;
1181 "\nFunction name:%s/%i:",
1182 cgraph_node_name (node
), node
->uid
);
1183 fprintf (dump_file
, "\n locals read: ");
1184 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_read
,
1187 fprintf (dump_file
, "%s ",
1188 get_static_name (index
));
1190 fprintf (dump_file
, "\n locals written: ");
1191 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_written
,
1194 fprintf(dump_file
, "%s ",
1195 get_static_name (index
));
1199 w
= w_info
->next_cycle
;
1202 ipa_reference_vars_info_t w_ri
=
1203 get_reference_vars_info_from_cgraph (w
);
1204 ipa_reference_local_vars_info_t w_l
= w_ri
->local
;
1205 fprintf (dump_file
, "\n next cycle: %s/%i ",
1206 cgraph_node_name (w
), w
->uid
);
1207 fprintf (dump_file
, "\n locals read: ");
1208 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_read
,
1211 fprintf (dump_file
, "%s ",
1212 get_static_name (index
));
1215 fprintf (dump_file
, "\n locals written: ");
1216 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_written
,
1219 fprintf(dump_file
, "%s ",
1220 get_static_name (index
));
1225 w
= w_info
->next_cycle
;
1227 fprintf (dump_file
, "\n globals read: ");
1228 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_read
,
1231 fprintf (dump_file
, "%s ",
1232 get_static_name (index
));
1234 fprintf (dump_file
, "\n globals written: ");
1235 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_written
,
1238 fprintf (dump_file
, "%s ",
1239 get_static_name (index
));
1245 for (i
= 0; i
< order_pos
; i
++ )
1247 ipa_reference_vars_info_t node_info
;
1248 ipa_reference_global_vars_info_t node_g
;
1250 node_info
= get_reference_vars_info_from_cgraph (node
);
1251 node_g
= node_info
->global
;
1253 /* Create the complimentary sets. These are more useful for
1255 node_g
->statics_not_read
= BITMAP_ALLOC (&ipa_obstack
);
1256 node_g
->statics_not_written
= BITMAP_ALLOC (&ipa_obstack
);
1258 if (node_g
->statics_read
!= all_module_statics
)
1260 bitmap_and_compl (node_g
->statics_not_read
,
1262 node_g
->statics_read
);
1265 if (node_g
->statics_written
1266 != all_module_statics
)
1267 bitmap_and_compl (node_g
->statics_not_written
,
1269 node_g
->statics_written
);
1274 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1276 /* Get rid of the aux information. */
1285 && (cgraph_function_body_availability (node
) == AVAIL_OVERWRITABLE
))
1286 clean_function (node
);
1292 gate_reference (void)
1294 return (flag_unit_at_a_time
!= 0 && flag_ipa_reference
1295 /* Don't bother doing anything if the program has errors. */
1296 && !(errorcount
|| sorrycount
));
1299 struct tree_opt_pass pass_ipa_reference
=
1301 "static-var", /* name */
1302 gate_reference
, /* gate */
1303 static_execute
, /* execute */
1306 0, /* static_pass_number */
1307 TV_IPA_REFERENCE
, /* tv_id */
1308 0, /* properties_required */
1309 0, /* properties_provided */
1310 0, /* properties_destroyed */
1311 0, /* todo_flags_start */
1312 0, /* todo_flags_finish */
1316 #include "gt-ipa-reference.h"