1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file gathers information about how variables whose scope is
23 confined to the compilation unit are used.
25 The transitive call site specific clobber effects are computed
26 for the variables whose scope is contained within this compilation
29 First each function and static variable initialization is analyzed
30 to determine which local static variables are either read, written,
31 or have their address taken. Any local static that has its address
32 taken is removed from consideration. Once the local read and
33 writes are determined, a transitive closure of this information is
34 performed over the call graph to determine the worst case set of
35 side effects of each call. In later parts of the compiler, these
36 local and global sets are examined to make the call clobbering less
37 traumatic, promote some statics to registers, and improve aliasing
42 #include "coretypes.h"
45 #include "tree-flow.h"
46 #include "tree-inline.h"
47 #include "tree-pass.h"
48 #include "langhooks.h"
49 #include "pointer-set.h"
50 #include "splay-tree.h"
52 #include "ipa-utils.h"
53 #include "ipa-reference.h"
59 #include "diagnostic.h"
60 #include "langhooks.h"
61 #include "lto-streamer.h"
63 static void remove_node_data (struct cgraph_node
*node
,
64 void *data ATTRIBUTE_UNUSED
);
65 static void duplicate_node_data (struct cgraph_node
*src
,
66 struct cgraph_node
*dst
,
67 void *data ATTRIBUTE_UNUSED
);
69 /* The static variables defined within the compilation unit that are
70 loaded or stored directly by function that owns this structure. */
72 struct ipa_reference_local_vars_info_d
75 bitmap statics_written
;
78 /* Statics that are read and written by some set of functions. The
79 local ones are based on the loads and stores local to the function.
80 The global ones are based on the local info as well as the
81 transitive closure of the functions that are called. */
83 struct ipa_reference_global_vars_info_d
86 bitmap statics_written
;
89 /* Information we save about every function after ipa-reference is completed. */
91 struct ipa_reference_optimization_summary_d
93 bitmap statics_not_read
;
94 bitmap statics_not_written
;
97 typedef struct ipa_reference_local_vars_info_d
*ipa_reference_local_vars_info_t
;
98 typedef struct ipa_reference_global_vars_info_d
*ipa_reference_global_vars_info_t
;
99 typedef struct ipa_reference_optimization_summary_d
*ipa_reference_optimization_summary_t
;
101 struct ipa_reference_vars_info_d
103 struct ipa_reference_local_vars_info_d local
;
104 struct ipa_reference_global_vars_info_d global
;
107 typedef struct ipa_reference_vars_info_d
*ipa_reference_vars_info_t
;
109 /* This splay tree contains all of the static variables that are
110 being considered by the compilation level alias analysis. */
111 static splay_tree reference_vars_to_consider
;
113 /* A bit is set for every module static we are considering. This is
114 ored into the local info when asm code is found that clobbers all
116 static bitmap all_module_statics
;
118 /* Obstack holding bitmaps of local analysis (live from analysis to
120 static bitmap_obstack local_info_obstack
;
121 /* Obstack holding global analysis live forever. */
122 static bitmap_obstack optimization_summary_obstack
;
124 /* Holders of ipa cgraph hooks: */
125 static struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
126 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
128 /* Vector where the reference var infos are actually stored. */
129 DEF_VEC_P (ipa_reference_vars_info_t
);
130 DEF_VEC_ALLOC_P (ipa_reference_vars_info_t
, heap
);
131 static VEC (ipa_reference_vars_info_t
, heap
) *ipa_reference_vars_vector
;
132 DEF_VEC_P (ipa_reference_optimization_summary_t
);
133 DEF_VEC_ALLOC_P (ipa_reference_optimization_summary_t
, heap
);
134 static VEC (ipa_reference_optimization_summary_t
, heap
) *ipa_reference_opt_sum_vector
;
136 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
137 static inline ipa_reference_vars_info_t
138 get_reference_vars_info (struct cgraph_node
*node
)
140 if (!ipa_reference_vars_vector
141 || VEC_length (ipa_reference_vars_info_t
,
142 ipa_reference_vars_vector
) <= (unsigned int) node
->uid
)
144 return VEC_index (ipa_reference_vars_info_t
, ipa_reference_vars_vector
,
148 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
149 static inline ipa_reference_optimization_summary_t
150 get_reference_optimization_summary (struct cgraph_node
*node
)
152 if (!ipa_reference_opt_sum_vector
153 || (VEC_length (ipa_reference_optimization_summary_t
,
154 ipa_reference_opt_sum_vector
)
155 <= (unsigned int) node
->uid
))
157 return VEC_index (ipa_reference_optimization_summary_t
, ipa_reference_opt_sum_vector
,
161 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
163 set_reference_vars_info (struct cgraph_node
*node
,
164 ipa_reference_vars_info_t info
)
166 if (!ipa_reference_vars_vector
167 || VEC_length (ipa_reference_vars_info_t
,
168 ipa_reference_vars_vector
) <= (unsigned int) node
->uid
)
169 VEC_safe_grow_cleared (ipa_reference_vars_info_t
, heap
,
170 ipa_reference_vars_vector
, node
->uid
+ 1);
171 VEC_replace (ipa_reference_vars_info_t
, ipa_reference_vars_vector
,
175 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
177 set_reference_optimization_summary (struct cgraph_node
*node
,
178 ipa_reference_optimization_summary_t info
)
180 if (!ipa_reference_opt_sum_vector
181 || (VEC_length (ipa_reference_optimization_summary_t
,
182 ipa_reference_opt_sum_vector
)
183 <= (unsigned int) node
->uid
))
184 VEC_safe_grow_cleared (ipa_reference_optimization_summary_t
,
185 heap
, ipa_reference_opt_sum_vector
, node
->uid
+ 1);
186 VEC_replace (ipa_reference_optimization_summary_t
,
187 ipa_reference_opt_sum_vector
, node
->uid
, info
);
190 /* Return a bitmap indexed by_DECL_UID uid for the static variables
191 that are not read during the execution of the function FN. Returns
192 NULL if no data is available. */
195 ipa_reference_get_not_read_global (struct cgraph_node
*fn
)
197 ipa_reference_optimization_summary_t info
;
199 info
= get_reference_optimization_summary (fn
);
201 return info
->statics_not_read
;
202 else if (flags_from_decl_or_type (fn
->decl
) & ECF_LEAF
)
203 return all_module_statics
;
208 /* Return a bitmap indexed by DECL_UID uid for the static variables
209 that are not written during the execution of the function FN. Note
210 that variables written may or may not be read during the function
211 call. Returns NULL if no data is available. */
214 ipa_reference_get_not_written_global (struct cgraph_node
*fn
)
216 ipa_reference_optimization_summary_t info
;
218 info
= get_reference_optimization_summary (fn
);
220 return info
->statics_not_written
;
221 else if (flags_from_decl_or_type (fn
->decl
) & ECF_LEAF
)
222 return all_module_statics
;
229 /* Add VAR to all_module_statics and the two
230 reference_vars_to_consider* sets. */
233 add_static_var (tree var
)
235 int uid
= DECL_UID (var
);
236 gcc_assert (TREE_CODE (var
) == VAR_DECL
);
238 splay_tree_insert (reference_vars_to_consider
,
239 uid
, (splay_tree_value
)var
);
240 bitmap_set_bit (all_module_statics
, uid
);
243 /* Return true if the variable T is the right kind of static variable to
244 perform compilation unit scope escape analysis. */
247 is_proper_for_analysis (tree t
)
249 /* We handle only variables whose address is never taken. */
250 if (TREE_ADDRESSABLE (t
))
253 /* If the variable has the "used" attribute, treat it as if it had a
254 been touched by the devil. */
255 if (DECL_PRESERVE_P (t
))
258 /* Do not want to do anything with volatile except mark any
259 function that uses one to be not const or pure. */
260 if (TREE_THIS_VOLATILE (t
))
263 /* We do not need to analyze readonly vars, we already know they do not
265 if (TREE_READONLY (t
))
268 /* We cannot touch decls where the type needs constructing. */
269 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (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 (all_module_statics
275 && !bitmap_bit_p (all_module_statics
, DECL_UID (t
)))
281 /* Lookup the tree node for the static variable that has UID and
282 convert the name to a string for debugging. */
285 get_static_name (int index
)
287 splay_tree_node stn
=
288 splay_tree_lookup (reference_vars_to_consider
, index
);
290 return lang_hooks
.decl_printable_name ((tree
)(stn
->value
), 2);
294 /* Or in all of the bits from every callee of X into X_GLOBAL, the caller's cycle,
295 bit vector. There are several cases to check to avoid the sparse
299 propagate_bits (ipa_reference_global_vars_info_t x_global
, struct cgraph_node
*x
)
301 struct cgraph_edge
*e
;
302 for (e
= x
->callees
; e
; e
= e
->next_callee
)
304 struct cgraph_node
*y
= e
->callee
;
305 enum availability avail
;
307 avail
= cgraph_function_body_availability (e
->callee
);
308 /* Only look into nodes we can propagate something. */
309 if (avail
> AVAIL_OVERWRITABLE
310 || (avail
== AVAIL_OVERWRITABLE
311 && (flags_from_decl_or_type (e
->callee
->decl
) & ECF_LEAF
)))
313 int flags
= flags_from_decl_or_type (e
->callee
->decl
);
314 if (get_reference_vars_info (y
))
316 ipa_reference_vars_info_t y_info
317 = get_reference_vars_info (y
);
318 ipa_reference_global_vars_info_t y_global
= &y_info
->global
;
320 /* Calls in current cycle do not have global computed yet. */
321 if (!y_global
->statics_read
)
324 /* If function is declared const, it reads no memory even if it
325 seems so to local analysis. */
326 if (flags
& ECF_CONST
)
329 if (x_global
->statics_read
330 != all_module_statics
)
332 if (y_global
->statics_read
333 == all_module_statics
)
335 BITMAP_FREE (x_global
->statics_read
);
336 x_global
->statics_read
337 = all_module_statics
;
339 /* Skip bitmaps that are pointer equal to node's bitmap
340 (no reason to spin within the cycle). */
341 else if (x_global
->statics_read
342 != y_global
->statics_read
)
343 bitmap_ior_into (x_global
->statics_read
,
344 y_global
->statics_read
);
347 /* If function is declared pure, it has no stores even if it
348 seems so to local analysis; If we can not return from here,
349 we can safely ignore the call. */
350 if ((flags
& ECF_PURE
)
351 || cgraph_edge_cannot_lead_to_return (e
))
354 if (x_global
->statics_written
355 != all_module_statics
)
357 if (y_global
->statics_written
358 == all_module_statics
)
360 BITMAP_FREE (x_global
->statics_written
);
361 x_global
->statics_written
362 = all_module_statics
;
364 /* Skip bitmaps that are pointer equal to node's bitmap
365 (no reason to spin within the cycle). */
366 else if (x_global
->statics_written
367 != y_global
->statics_written
)
368 bitmap_ior_into (x_global
->statics_written
,
369 y_global
->statics_written
);
378 /* The init routine for analyzing global static variable usage. See
379 comments at top for description. */
383 static bool init_p
= false;
391 reference_vars_to_consider
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
393 bitmap_obstack_initialize (&local_info_obstack
);
394 bitmap_obstack_initialize (&optimization_summary_obstack
);
395 all_module_statics
= BITMAP_ALLOC (&optimization_summary_obstack
);
397 node_removal_hook_holder
=
398 cgraph_add_node_removal_hook (&remove_node_data
, NULL
);
399 node_duplication_hook_holder
=
400 cgraph_add_node_duplication_hook (&duplicate_node_data
, NULL
);
404 /* Set up the persistent info for FN. */
406 static ipa_reference_local_vars_info_t
407 init_function_info (struct cgraph_node
*fn
)
409 ipa_reference_vars_info_t info
410 = XCNEW (struct ipa_reference_vars_info_d
);
412 /* Add the info to the tree's annotation. */
413 set_reference_vars_info (fn
, info
);
415 info
->local
.statics_read
= BITMAP_ALLOC (&local_info_obstack
);
416 info
->local
.statics_written
= BITMAP_ALLOC (&local_info_obstack
);
422 /* This is the main routine for finding the reference patterns for
423 global variables within a function FN. */
426 analyze_function (struct cgraph_node
*fn
)
428 ipa_reference_local_vars_info_t local
;
433 local
= init_function_info (fn
);
434 for (i
= 0; ipa_ref_list_reference_iterate (&fn
->ref_list
, i
, ref
); i
++)
436 if (ref
->refered_type
!= IPA_REF_VARPOOL
)
438 var
= ipa_ref_varpool_node (ref
)->decl
;
439 if (ipa_ref_varpool_node (ref
)->externally_visible
440 || !ipa_ref_varpool_node (ref
)->analyzed
441 || !is_proper_for_analysis (var
))
446 bitmap_set_bit (local
->statics_read
, DECL_UID (var
));
449 if (ipa_ref_cannot_lead_to_return (ref
))
451 bitmap_set_bit (local
->statics_written
, DECL_UID (var
));
459 if (cgraph_node_cannot_return (fn
))
460 bitmap_clear (local
->statics_written
);
464 copy_global_bitmap (bitmap src
)
469 if (src
== all_module_statics
)
470 return all_module_statics
;
471 dst
= BITMAP_ALLOC (&optimization_summary_obstack
);
472 bitmap_copy (dst
, src
);
477 /* Called when new clone is inserted to callgraph late. */
480 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
481 void *data ATTRIBUTE_UNUSED
)
483 ipa_reference_optimization_summary_t ginfo
;
484 ipa_reference_optimization_summary_t dst_ginfo
;
486 ginfo
= get_reference_optimization_summary (src
);
489 dst_ginfo
= XCNEW (struct ipa_reference_optimization_summary_d
);
490 set_reference_optimization_summary (dst
, dst_ginfo
);
491 dst_ginfo
->statics_not_read
= copy_global_bitmap (ginfo
->statics_not_read
);
492 dst_ginfo
->statics_not_written
= copy_global_bitmap (ginfo
->statics_not_written
);
495 /* Called when node is removed. */
498 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
500 ipa_reference_optimization_summary_t ginfo
;
501 ginfo
= get_reference_optimization_summary (node
);
504 if (ginfo
->statics_not_read
505 && ginfo
->statics_not_read
!= all_module_statics
)
506 BITMAP_FREE (ginfo
->statics_not_read
);
508 if (ginfo
->statics_not_written
509 && ginfo
->statics_not_written
!= all_module_statics
)
510 BITMAP_FREE (ginfo
->statics_not_written
);
512 set_reference_optimization_summary (node
, NULL
);
516 /* Analyze each function in the cgraph to see which global or statics
517 are read or written. */
520 generate_summary (void)
522 struct cgraph_node
*node
;
528 bm_temp
= BITMAP_ALLOC (&local_info_obstack
);
530 /* Process all of the functions next. */
531 for (node
= cgraph_nodes
; node
; node
= node
->next
)
533 analyze_function (node
);
536 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
538 fprintf (dump_file
, "\nPromotable global:%s",
539 get_static_name (index
));
542 BITMAP_FREE(bm_temp
);
545 for (node
= cgraph_nodes
; node
; node
= node
->next
)
546 if (cgraph_function_body_availability (node
) >= AVAIL_OVERWRITABLE
)
548 ipa_reference_local_vars_info_t l
;
552 l
= &get_reference_vars_info (node
)->local
;
554 "\nFunction name:%s/%i:",
555 cgraph_node_name (node
), node
->uid
);
556 fprintf (dump_file
, "\n locals read: ");
558 EXECUTE_IF_SET_IN_BITMAP (l
->statics_read
,
561 fprintf (dump_file
, "%s ",
562 get_static_name (index
));
564 fprintf (dump_file
, "\n locals written: ");
565 if (l
->statics_written
)
566 EXECUTE_IF_SET_IN_BITMAP (l
->statics_written
,
569 fprintf(dump_file
, "%s ",
570 get_static_name (index
));
575 /* Set READ_ALL/WRITE_ALL based on decl flags of NODE. */
578 read_write_all_from_decl (struct cgraph_node
*node
, bool * read_all
,
581 tree decl
= node
->decl
;
582 int flags
= flags_from_decl_or_type (decl
);
583 if ((flags
& ECF_LEAF
)
584 && cgraph_function_body_availability (node
) <= AVAIL_OVERWRITABLE
)
586 else if (flags
& ECF_CONST
)
588 else if ((flags
& ECF_PURE
)
589 || cgraph_node_cannot_return (node
))
592 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
593 fprintf (dump_file
, " %s/%i -> read all\n",
594 cgraph_node_name (node
), node
->uid
);
598 /* TODO: To be able to produce sane results, we should also handle
599 common builtins, in particular throw. */
602 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
603 fprintf (dump_file
, " %s/%i -> read all, write all\n",
604 cgraph_node_name (node
), node
->uid
);
608 /* Produce the global information by preforming a transitive closure
609 on the local information that was produced by ipa_analyze_function */
614 struct cgraph_node
*node
;
615 struct cgraph_node
*w
;
616 struct cgraph_node
**order
=
617 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
618 int order_pos
= ipa_utils_reduced_inorder (order
, false, true, NULL
);
622 dump_cgraph (dump_file
);
624 ipa_discover_readonly_nonaddressable_vars ();
627 /* Propagate the local information thru the call graph to produce
628 the global information. All the nodes within a cycle will have
629 the same info so we collapse cycles first. Then we can do the
630 propagation in one pass from the leaves to the roots. */
631 order_pos
= ipa_utils_reduced_inorder (order
, true, true, NULL
);
633 ipa_utils_print_order(dump_file
, "reduced", order
, order_pos
);
635 for (i
= 0; i
< order_pos
; i
++ )
637 ipa_reference_vars_info_t node_info
;
638 ipa_reference_global_vars_info_t node_g
;
639 ipa_reference_local_vars_info_t node_l
;
640 struct cgraph_edge
*e
, *ie
;
644 struct ipa_dfs_info
* w_info
;
647 node_info
= get_reference_vars_info (node
);
648 gcc_assert (node_info
);
651 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
652 fprintf (dump_file
, "Starting cycle with %s/%i\n",
653 cgraph_node_name (node
), node
->uid
);
655 node_l
= &node_info
->local
;
656 node_g
= &node_info
->global
;
661 /* When function is overwritable, we can not assume anything. */
662 if (cgraph_function_body_availability (node
) <= AVAIL_OVERWRITABLE
)
663 read_write_all_from_decl (node
, &read_all
, &write_all
);
665 for (e
= node
->callees
; e
; e
= e
->next_callee
)
666 if (cgraph_function_body_availability (e
->callee
) <= AVAIL_OVERWRITABLE
)
667 read_write_all_from_decl (e
->callee
, &read_all
, &write_all
);
669 for (ie
= node
->indirect_calls
; ie
; ie
= ie
->next_callee
)
670 if (!(ie
->indirect_info
->ecf_flags
& ECF_CONST
))
673 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
674 fprintf (dump_file
, " indirect call -> read all\n");
675 if (!cgraph_edge_cannot_lead_to_return (ie
)
676 && !(ie
->indirect_info
->ecf_flags
& ECF_PURE
))
678 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
679 fprintf (dump_file
, " indirect call -> write all\n");
685 /* If any node in a cycle is read_all or write_all
687 w_info
= (struct ipa_dfs_info
*) node
->aux
;
688 w
= w_info
->next_cycle
;
689 while (w
&& (!read_all
|| !write_all
))
691 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
692 fprintf (dump_file
, " Visiting %s/%i\n",
693 cgraph_node_name (w
), w
->uid
);
694 /* When function is overwritable, we can not assume anything. */
695 if (cgraph_function_body_availability (w
) <= AVAIL_OVERWRITABLE
)
696 read_write_all_from_decl (w
, &read_all
, &write_all
);
698 for (e
= w
->callees
; e
; e
= e
->next_callee
)
699 if (cgraph_function_body_availability (e
->callee
) <= AVAIL_OVERWRITABLE
)
700 read_write_all_from_decl (e
->callee
, &read_all
, &write_all
);
702 for (ie
= w
->indirect_calls
; ie
; ie
= ie
->next_callee
)
703 if (!(ie
->indirect_info
->ecf_flags
& ECF_CONST
))
706 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
707 fprintf (dump_file
, " indirect call -> read all\n");
708 if (!cgraph_edge_cannot_lead_to_return (ie
)
709 && !(ie
->indirect_info
->ecf_flags
& ECF_PURE
))
712 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
713 fprintf (dump_file
, " indirect call -> write all\n");
717 w_info
= (struct ipa_dfs_info
*) w
->aux
;
718 w
= w_info
->next_cycle
;
722 /* Initialized the bitmaps for the reduced nodes */
724 node_g
->statics_read
= all_module_statics
;
727 node_g
->statics_read
= BITMAP_ALLOC (&local_info_obstack
);
728 bitmap_copy (node_g
->statics_read
,
729 node_l
->statics_read
);
732 node_g
->statics_written
= all_module_statics
;
735 node_g
->statics_written
= BITMAP_ALLOC (&local_info_obstack
);
736 bitmap_copy (node_g
->statics_written
,
737 node_l
->statics_written
);
740 propagate_bits (node_g
, node
);
741 w_info
= (struct ipa_dfs_info
*) node
->aux
;
742 w
= w_info
->next_cycle
;
743 while (w
&& (!read_all
|| !write_all
))
745 ipa_reference_vars_info_t w_ri
=
746 get_reference_vars_info (w
);
747 ipa_reference_local_vars_info_t w_l
= &w_ri
->local
;
748 int flags
= flags_from_decl_or_type (w
->decl
);
750 /* These global bitmaps are initialized from the local info
751 of all of the nodes in the region. However there is no
752 need to do any work if the bitmaps were set to
753 all_module_statics. */
754 if (!read_all
&& !(flags
& ECF_CONST
))
755 bitmap_ior_into (node_g
->statics_read
,
758 && !(flags
& ECF_PURE
)
759 && !cgraph_node_cannot_return (w
))
760 bitmap_ior_into (node_g
->statics_written
,
761 w_l
->statics_written
);
762 propagate_bits (node_g
, w
);
763 w_info
= (struct ipa_dfs_info
*) w
->aux
;
764 w
= w_info
->next_cycle
;
767 /* All nodes within a cycle have the same global info bitmaps. */
768 node_info
->global
= *node_g
;
769 w_info
= (struct ipa_dfs_info
*) node
->aux
;
770 w
= w_info
->next_cycle
;
773 ipa_reference_vars_info_t w_ri
=
774 get_reference_vars_info (w
);
776 w_ri
->global
= *node_g
;
778 w_info
= (struct ipa_dfs_info
*) w
->aux
;
779 w
= w_info
->next_cycle
;
785 for (i
= 0; i
< order_pos
; i
++ )
787 ipa_reference_vars_info_t node_info
;
788 ipa_reference_global_vars_info_t node_g
;
789 ipa_reference_local_vars_info_t node_l
;
792 struct ipa_dfs_info
* w_info
;
795 node_info
= get_reference_vars_info (node
);
796 node_g
= &node_info
->global
;
797 node_l
= &node_info
->local
;
799 "\nFunction name:%s/%i:",
800 cgraph_node_name (node
), node
->uid
);
801 fprintf (dump_file
, "\n locals read: ");
802 if (node_l
->statics_read
)
803 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_read
,
806 fprintf (dump_file
, "%s ",
807 get_static_name (index
));
809 fprintf (dump_file
, "\n locals written: ");
810 if (node_l
->statics_written
)
811 EXECUTE_IF_SET_IN_BITMAP (node_l
->statics_written
,
814 fprintf(dump_file
, "%s ",
815 get_static_name (index
));
818 w_info
= (struct ipa_dfs_info
*) node
->aux
;
819 w
= w_info
->next_cycle
;
822 ipa_reference_vars_info_t w_ri
=
823 get_reference_vars_info (w
);
824 ipa_reference_local_vars_info_t w_l
= &w_ri
->local
;
825 fprintf (dump_file
, "\n next cycle: %s/%i ",
826 cgraph_node_name (w
), w
->uid
);
827 fprintf (dump_file
, "\n locals read: ");
828 if (w_l
->statics_read
)
829 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_read
,
832 fprintf (dump_file
, "%s ",
833 get_static_name (index
));
836 fprintf (dump_file
, "\n locals written: ");
837 if (w_l
->statics_written
)
838 EXECUTE_IF_SET_IN_BITMAP (w_l
->statics_written
,
841 fprintf (dump_file
, "%s ",
842 get_static_name (index
));
845 w_info
= (struct ipa_dfs_info
*) w
->aux
;
846 w
= w_info
->next_cycle
;
848 fprintf (dump_file
, "\n globals read: ");
849 if (node_g
->statics_read
== all_module_statics
)
850 fprintf (dump_file
, "ALL");
852 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_read
,
855 fprintf (dump_file
, "%s ",
856 get_static_name (index
));
858 fprintf (dump_file
, "\n globals written: ");
859 if (node_g
->statics_written
== all_module_statics
)
860 fprintf (dump_file
, "ALL");
862 EXECUTE_IF_SET_IN_BITMAP (node_g
->statics_written
,
865 fprintf (dump_file
, "%s ",
866 get_static_name (index
));
872 for (node
= cgraph_nodes
; node
; node
= node
->next
)
874 ipa_reference_vars_info_t node_info
;
875 ipa_reference_global_vars_info_t node_g
;
876 ipa_reference_optimization_summary_t opt
;
881 node_info
= get_reference_vars_info (node
);
882 if (cgraph_function_body_availability (node
) > AVAIL_OVERWRITABLE
883 || (flags_from_decl_or_type (node
->decl
) & ECF_LEAF
))
885 node_g
= &node_info
->global
;
887 opt
= XCNEW (struct ipa_reference_optimization_summary_d
);
888 set_reference_optimization_summary (node
, opt
);
890 /* Create the complimentary sets. */
892 if (bitmap_empty_p (node_g
->statics_read
))
893 opt
->statics_not_read
= all_module_statics
;
896 opt
->statics_not_read
897 = BITMAP_ALLOC (&optimization_summary_obstack
);
898 if (node_g
->statics_read
!= all_module_statics
)
899 bitmap_and_compl (opt
->statics_not_read
,
901 node_g
->statics_read
);
904 if (bitmap_empty_p (node_g
->statics_written
))
905 opt
->statics_not_written
= all_module_statics
;
908 opt
->statics_not_written
909 = BITMAP_ALLOC (&optimization_summary_obstack
);
910 if (node_g
->statics_written
!= all_module_statics
)
911 bitmap_and_compl (opt
->statics_not_written
,
913 node_g
->statics_written
);
926 bitmap_obstack_release (&local_info_obstack
);
927 VEC_free (ipa_reference_vars_info_t
, heap
, ipa_reference_vars_vector
);
928 ipa_reference_vars_vector
= NULL
;
930 splay_tree_delete (reference_vars_to_consider
);
931 reference_vars_to_consider
= NULL
;
935 /* Return true if we need to write summary of NODE. */
938 write_node_summary_p (struct cgraph_node
*node
,
940 varpool_node_set vset
,
941 bitmap ltrans_statics
)
943 ipa_reference_optimization_summary_t info
;
945 /* See if we have (non-empty) info. */
946 if (!node
->analyzed
|| node
->global
.inlined_to
)
948 info
= get_reference_optimization_summary (node
);
949 if (!info
|| (bitmap_empty_p (info
->statics_not_read
)
950 && bitmap_empty_p (info
->statics_not_written
)))
953 /* See if we want to encode it.
954 Encode also referenced functions since constant folding might turn it into
957 In future we might also want to include summaries of functions references
958 by initializers of constant variables references in current unit. */
959 if (!reachable_from_this_partition_p (node
, set
)
960 && !referenced_from_this_partition_p (&node
->ref_list
, set
, vset
))
963 /* See if the info has non-empty intersections with vars we want to encode. */
964 if (!bitmap_intersect_p (info
->statics_not_read
, ltrans_statics
)
965 && !bitmap_intersect_p (info
->statics_not_written
, ltrans_statics
))
970 /* Stream out BITS<RANS_STATICS as list of decls to OB.
971 LTRANS_STATICS_BITCOUNT specify number of bits in LTRANS_STATICS
972 or -1. When it is positive, just output -1 when
973 BITS<RANS_STATICS == BITS<RANS_STATICS. */
976 stream_out_bitmap (struct lto_simple_output_block
*ob
,
977 bitmap bits
, bitmap ltrans_statics
,
978 int ltrans_statics_bitcount
)
983 if (bits
== all_module_statics
)
985 lto_output_sleb128_stream (ob
->main_stream
, -1);
988 EXECUTE_IF_AND_IN_BITMAP (bits
, ltrans_statics
, 0, index
, bi
)
990 if (count
== ltrans_statics_bitcount
)
992 lto_output_sleb128_stream (ob
->main_stream
, -1);
995 lto_output_sleb128_stream (ob
->main_stream
, count
);
998 EXECUTE_IF_AND_IN_BITMAP (bits
, ltrans_statics
, 0, index
, bi
)
1000 tree decl
= (tree
)splay_tree_lookup (reference_vars_to_consider
, index
)->value
;
1001 lto_output_var_decl_index(ob
->decl_state
, ob
->main_stream
, decl
);
1005 /* Serialize the ipa info for lto. */
1008 ipa_reference_write_optimization_summary (cgraph_node_set set
,
1009 varpool_node_set vset
)
1011 struct cgraph_node
*node
;
1012 struct lto_simple_output_block
*ob
1013 = lto_create_simple_output_block (LTO_section_ipa_reference
);
1014 unsigned int count
= 0;
1015 int ltrans_statics_bitcount
= 0;
1016 lto_cgraph_encoder_t encoder
= ob
->decl_state
->cgraph_node_encoder
;
1017 lto_varpool_encoder_t varpool_encoder
= ob
->decl_state
->varpool_node_encoder
;
1018 bitmap ltrans_statics
= BITMAP_ALLOC (NULL
);
1021 reference_vars_to_consider
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
1023 /* See what variables we are interested in. */
1024 for (i
= 0; i
< lto_varpool_encoder_size (varpool_encoder
); i
++)
1026 struct varpool_node
*vnode
= lto_varpool_encoder_deref (varpool_encoder
, i
);
1027 if (!vnode
->externally_visible
1029 && bitmap_bit_p (all_module_statics
, DECL_UID (vnode
->decl
))
1030 && referenced_from_this_partition_p (&vnode
->ref_list
, set
, vset
))
1032 tree decl
= vnode
->decl
;
1033 bitmap_set_bit (ltrans_statics
, DECL_UID (decl
));
1034 splay_tree_insert (reference_vars_to_consider
,
1035 DECL_UID (decl
), (splay_tree_value
)decl
);
1036 ltrans_statics_bitcount
++;
1041 if (ltrans_statics_bitcount
)
1042 for (i
= 0; i
< lto_cgraph_encoder_size (encoder
); i
++)
1043 if (write_node_summary_p (lto_cgraph_encoder_deref (encoder
, i
),
1044 set
, vset
, ltrans_statics
))
1047 lto_output_uleb128_stream (ob
->main_stream
, count
);
1049 stream_out_bitmap (ob
, ltrans_statics
, ltrans_statics
,
1052 /* Process all of the functions. */
1053 if (ltrans_statics_bitcount
)
1054 for (i
= 0; i
< lto_cgraph_encoder_size (encoder
); i
++)
1056 node
= lto_cgraph_encoder_deref (encoder
, i
);
1057 if (write_node_summary_p (node
, set
, vset
, ltrans_statics
))
1059 ipa_reference_optimization_summary_t info
;
1062 info
= get_reference_optimization_summary (node
);
1063 node_ref
= lto_cgraph_encoder_encode (encoder
, node
);
1064 lto_output_uleb128_stream (ob
->main_stream
, node_ref
);
1066 stream_out_bitmap (ob
, info
->statics_not_read
, ltrans_statics
,
1067 ltrans_statics_bitcount
);
1068 stream_out_bitmap (ob
, info
->statics_not_written
, ltrans_statics
,
1069 ltrans_statics_bitcount
);
1072 BITMAP_FREE (ltrans_statics
);
1073 lto_destroy_simple_output_block (ob
);
1074 splay_tree_delete (reference_vars_to_consider
);
1077 /* Deserialize the ipa info for lto. */
1080 ipa_reference_read_optimization_summary (void)
1082 struct lto_file_decl_data
** file_data_vec
1083 = lto_get_file_decl_data ();
1084 struct lto_file_decl_data
* file_data
;
1086 bitmap_obstack_initialize (&optimization_summary_obstack
);
1088 node_removal_hook_holder
=
1089 cgraph_add_node_removal_hook (&remove_node_data
, NULL
);
1090 node_duplication_hook_holder
=
1091 cgraph_add_node_duplication_hook (&duplicate_node_data
, NULL
);
1092 all_module_statics
= BITMAP_ALLOC (&optimization_summary_obstack
);
1094 while ((file_data
= file_data_vec
[j
++]))
1098 struct lto_input_block
*ib
1099 = lto_create_simple_input_block (file_data
,
1100 LTO_section_ipa_reference
,
1105 unsigned int f_count
= lto_input_uleb128 (ib
);
1109 b_count
= lto_input_sleb128 (ib
);
1111 fprintf (dump_file
, "all module statics:");
1112 for (i
= 0; i
< (unsigned int)b_count
; i
++)
1114 unsigned int var_index
= lto_input_uleb128 (ib
);
1115 tree v_decl
= lto_file_decl_data_get_var_decl (file_data
,
1117 bitmap_set_bit (all_module_statics
, DECL_UID (v_decl
));
1119 fprintf (dump_file
, " %s",
1120 lang_hooks
.decl_printable_name (v_decl
, 2));
1123 for (i
= 0; i
< f_count
; i
++)
1125 unsigned int j
, index
;
1126 struct cgraph_node
*node
;
1127 ipa_reference_optimization_summary_t info
;
1129 lto_cgraph_encoder_t encoder
;
1131 index
= lto_input_uleb128 (ib
);
1132 encoder
= file_data
->cgraph_node_encoder
;
1133 node
= lto_cgraph_encoder_deref (encoder
, index
);
1134 info
= XCNEW (struct ipa_reference_optimization_summary_d
);
1135 set_reference_optimization_summary (node
, info
);
1136 info
->statics_not_read
= BITMAP_ALLOC (&optimization_summary_obstack
);
1137 info
->statics_not_written
= BITMAP_ALLOC (&optimization_summary_obstack
);
1140 "\nFunction name:%s/%i:\n static not read:",
1141 cgraph_node_name (node
), node
->uid
);
1143 /* Set the statics not read. */
1144 v_count
= lto_input_sleb128 (ib
);
1147 info
->statics_not_read
= all_module_statics
;
1149 fprintf (dump_file
, " all module statics");
1152 for (j
= 0; j
< (unsigned int)v_count
; j
++)
1154 unsigned int var_index
= lto_input_uleb128 (ib
);
1155 tree v_decl
= lto_file_decl_data_get_var_decl (file_data
,
1157 bitmap_set_bit (info
->statics_not_read
, DECL_UID (v_decl
));
1159 fprintf (dump_file
, " %s",
1160 lang_hooks
.decl_printable_name (v_decl
, 2));
1165 "\n static not written:");
1166 /* Set the statics not written. */
1167 v_count
= lto_input_sleb128 (ib
);
1170 info
->statics_not_written
= all_module_statics
;
1172 fprintf (dump_file
, " all module statics");
1175 for (j
= 0; j
< (unsigned int)v_count
; j
++)
1177 unsigned int var_index
= lto_input_uleb128 (ib
);
1178 tree v_decl
= lto_file_decl_data_get_var_decl (file_data
,
1180 bitmap_set_bit (info
->statics_not_written
, DECL_UID (v_decl
));
1182 fprintf (dump_file
, " %s",
1183 lang_hooks
.decl_printable_name (v_decl
, 2));
1186 fprintf (dump_file
, "\n");
1189 lto_destroy_simple_input_block (file_data
,
1190 LTO_section_ipa_reference
,
1194 /* Fatal error here. We do not want to support compiling ltrans units with
1195 different version of compiler or different flags than the WPA unit, so
1196 this should never happen. */
1197 fatal_error ("ipa reference summary is missing in ltrans unit");
1202 gate_reference (void)
1204 return (flag_ipa_reference
1205 /* Don't bother doing anything if the program has errors. */
1209 struct ipa_opt_pass_d pass_ipa_reference
=
1213 "static-var", /* name */
1214 gate_reference
, /* gate */
1215 propagate
, /* execute */
1218 0, /* static_pass_number */
1219 TV_IPA_REFERENCE
, /* tv_id */
1220 0, /* properties_required */
1221 0, /* properties_provided */
1222 0, /* properties_destroyed */
1223 0, /* todo_flags_start */
1224 0 /* todo_flags_finish */
1226 NULL
, /* generate_summary */
1227 NULL
, /* write_summary */
1228 NULL
, /* read_summary */
1229 ipa_reference_write_optimization_summary
,/* write_optimization_summary */
1230 ipa_reference_read_optimization_summary
,/* read_optimization_summary */
1231 NULL
, /* stmt_fixup */
1233 NULL
, /* function_transform */
1234 NULL
/* variable_transform */