1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004-2015 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 The transitive call site specific clobber effects are computed
25 for the variables whose scope is contained within this compilation
28 First each function and static variable initialization is analyzed
29 to determine which local static variables are either read, written,
30 or have their address taken. Any local static that has its address
31 taken is removed from consideration. Once the local read and
32 writes are determined, a transitive closure of this information is
33 performed over the call graph to determine the worst case set of
34 side effects of each call. In later parts of the compiler, these
35 local and global sets are examined to make the call clobbering less
36 traumatic, promote some statics to registers, and improve aliasing
41 #include "coretypes.h"
46 #include "double-int.h"
54 #include "fold-const.h"
57 #include "hard-reg-set.h"
60 #include "basic-block.h"
61 #include "tree-ssa-alias.h"
62 #include "internal-fn.h"
63 #include "gimple-expr.h"
66 #include "tree-inline.h"
67 #include "tree-pass.h"
68 #include "splay-tree.h"
70 #include "plugin-api.h"
73 #include "ipa-utils.h"
75 #include "ipa-reference.h"
77 #include "diagnostic.h"
78 #include "data-streamer.h"
79 #include "lto-streamer.h"
81 static void remove_node_data (struct cgraph_node
*node
,
82 void *data ATTRIBUTE_UNUSED
);
83 static void duplicate_node_data (struct cgraph_node
*src
,
84 struct cgraph_node
*dst
,
85 void *data ATTRIBUTE_UNUSED
);
87 /* The static variables defined within the compilation unit that are
88 loaded or stored directly by function that owns this structure. */
90 struct ipa_reference_local_vars_info_d
93 bitmap statics_written
;
96 /* Statics that are read and written by some set of functions. The
97 local ones are based on the loads and stores local to the function.
98 The global ones are based on the local info as well as the
99 transitive closure of the functions that are called. */
101 struct ipa_reference_global_vars_info_d
104 bitmap statics_written
;
107 /* Information we save about every function after ipa-reference is completed. */
109 struct ipa_reference_optimization_summary_d
111 bitmap statics_not_read
;
112 bitmap statics_not_written
;
115 typedef struct ipa_reference_local_vars_info_d
*ipa_reference_local_vars_info_t
;
116 typedef struct ipa_reference_global_vars_info_d
*ipa_reference_global_vars_info_t
;
117 typedef struct ipa_reference_optimization_summary_d
*ipa_reference_optimization_summary_t
;
119 struct ipa_reference_vars_info_d
121 struct ipa_reference_local_vars_info_d local
;
122 struct ipa_reference_global_vars_info_d global
;
125 typedef struct ipa_reference_vars_info_d
*ipa_reference_vars_info_t
;
127 /* This splay tree contains all of the static variables that are
128 being considered by the compilation level alias analysis. */
129 static splay_tree reference_vars_to_consider
;
131 /* Set of all interesting module statics. A bit is set for every module
132 static we are considering. This is added to the local info when asm
133 code is found that clobbers all memory. */
134 static bitmap all_module_statics
;
135 /* Set of all statics that should be ignored becuase they are touched by
136 -fno-ipa-reference code. */
137 static bitmap ignore_module_statics
;
139 /* Obstack holding bitmaps of local analysis (live from analysis to
141 static bitmap_obstack local_info_obstack
;
142 /* Obstack holding global analysis live forever. */
143 static bitmap_obstack optimization_summary_obstack
;
145 /* Holders of ipa cgraph hooks: */
146 static struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
147 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
149 /* Vector where the reference var infos are actually stored.
150 Indexed by UID of call graph nodes. */
151 static vec
<ipa_reference_vars_info_t
> ipa_reference_vars_vector
;
153 static vec
<ipa_reference_optimization_summary_t
> ipa_reference_opt_sum_vector
;
155 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
156 static inline ipa_reference_vars_info_t
157 get_reference_vars_info (struct cgraph_node
*node
)
159 if (!ipa_reference_vars_vector
.exists ()
160 || ipa_reference_vars_vector
.length () <= (unsigned int) node
->uid
)
162 return ipa_reference_vars_vector
[node
->uid
];
165 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
166 static inline ipa_reference_optimization_summary_t
167 get_reference_optimization_summary (struct cgraph_node
*node
)
169 if (!ipa_reference_opt_sum_vector
.exists ()
170 || (ipa_reference_opt_sum_vector
.length () <= (unsigned int) node
->uid
))
172 return ipa_reference_opt_sum_vector
[node
->uid
];
175 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
177 set_reference_vars_info (struct cgraph_node
*node
,
178 ipa_reference_vars_info_t info
)
180 if (!ipa_reference_vars_vector
.exists ()
181 || ipa_reference_vars_vector
.length () <= (unsigned int) node
->uid
)
182 ipa_reference_vars_vector
.safe_grow_cleared (node
->uid
+ 1);
183 ipa_reference_vars_vector
[node
->uid
] = info
;
186 /* Return the ipa_reference_vars structure starting from the cgraph NODE. */
188 set_reference_optimization_summary (struct cgraph_node
*node
,
189 ipa_reference_optimization_summary_t info
)
191 if (!ipa_reference_opt_sum_vector
.exists ()
192 || (ipa_reference_opt_sum_vector
.length () <= (unsigned int) node
->uid
))
193 ipa_reference_opt_sum_vector
.safe_grow_cleared (node
->uid
+ 1);
194 ipa_reference_opt_sum_vector
[node
->uid
] = info
;
197 /* Return a bitmap indexed by DECL_UID for the static variables that
198 are *not* read during the execution of the function FN. Returns
199 NULL if no data is available. */
202 ipa_reference_get_not_read_global (struct cgraph_node
*fn
)
204 if (!opt_for_fn (fn
->decl
, flag_ipa_reference
)
205 || !opt_for_fn (current_function_decl
, flag_ipa_reference
))
207 ipa_reference_optimization_summary_t info
=
208 get_reference_optimization_summary (fn
->function_symbol (NULL
));
210 return info
->statics_not_read
;
211 else if (flags_from_decl_or_type (fn
->decl
) & ECF_LEAF
)
212 return all_module_statics
;
217 /* Return a bitmap indexed by DECL_UID for the static variables that
218 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 (struct cgraph_node
*fn
)
225 if (!opt_for_fn (fn
->decl
, flag_ipa_reference
)
226 || !opt_for_fn (current_function_decl
, flag_ipa_reference
))
228 ipa_reference_optimization_summary_t info
=
229 get_reference_optimization_summary (fn
);
231 return info
->statics_not_written
;
232 else if (flags_from_decl_or_type (fn
->decl
) & ECF_LEAF
)
233 return all_module_statics
;
240 /* Add VAR to all_module_statics and the two
241 reference_vars_to_consider* sets. */
244 add_static_var (tree var
)
246 int uid
= DECL_UID (var
);
247 gcc_assert (TREE_CODE (var
) == VAR_DECL
);
249 splay_tree_insert (reference_vars_to_consider
,
250 uid
, (splay_tree_value
)var
);
251 bitmap_set_bit (all_module_statics
, uid
);
254 /* Return true if the variable T is the right kind of static variable to
255 perform compilation unit scope escape analysis. */
258 is_proper_for_analysis (tree t
)
260 /* If the variable has the "used" attribute, treat it as if it had a
261 been touched by the devil. */
262 if (DECL_PRESERVE_P (t
))
265 /* Do not want to do anything with volatile except mark any
266 function that uses one to be not const or pure. */
267 if (TREE_THIS_VOLATILE (t
))
270 /* We do not need to analyze readonly vars, we already know they do not
272 if (TREE_READONLY (t
))
275 /* We can not track variables with address taken. */
276 if (TREE_ADDRESSABLE (t
))
279 /* TODO: We could track public variables that are not addressable, but currently
280 frontends don't give us those. */
284 /* TODO: Check aliases. */
285 if (bitmap_bit_p (ignore_module_statics
, DECL_UID (t
)))
288 /* This is a variable we care about. Check if we have seen it
289 before, and if not add it the set of variables we care about. */
290 if (all_module_statics
291 && !bitmap_bit_p (all_module_statics
, DECL_UID (t
)))
297 /* Lookup the tree node for the static variable that has UID and
298 convert the name to a string for debugging. */
301 get_static_name (int index
)
303 splay_tree_node stn
=
304 splay_tree_lookup (reference_vars_to_consider
, index
);
305 return fndecl_name ((tree
)(stn
->value
));
308 /* Dump a set of static vars to FILE. */
310 dump_static_vars_set_to_file (FILE *f
, bitmap set
)
316 else if (set
== all_module_statics
)
319 EXECUTE_IF_SET_IN_BITMAP (set
, 0, index
, bi
)
321 fprintf (f
, "%s ", get_static_name (index
));
325 /* Compute X |= Y, taking into account the possibility that
326 either X or Y is already the maximum set.
327 Return true if X is the maximum set after taking the union with Y. */
330 union_static_var_sets (bitmap
&x
, bitmap y
)
332 if (x
!= all_module_statics
)
334 if (y
== all_module_statics
)
337 x
= all_module_statics
;
339 else if (bitmap_ior_into (x
, y
))
341 /* The union may have reduced X to the maximum set.
342 In that case, we want to make that visible explicitly.
343 Even though bitmap_equal_p can be very expensive, it
344 turns out to be an overall win to check this here for
345 an LTO bootstrap of GCC itself. Liberally extrapoliate
346 that result to be applicable to all cases. */
347 if (bitmap_equal_p (x
, all_module_statics
))
350 x
= all_module_statics
;
354 return x
== all_module_statics
;
357 /* Return a copy of SET on the bitmap obstack containing SET.
358 But if SET is NULL or the maximum set, return that instead. */
361 copy_static_var_set (bitmap set
)
363 if (set
== NULL
|| set
== all_module_statics
)
365 bitmap_obstack
*o
= set
->obstack
;
366 gcc_checking_assert (o
);
367 bitmap copy
= BITMAP_ALLOC (o
);
368 bitmap_copy (copy
, set
);
372 /* Compute the union all of the statics read and written by every callee of X
373 into X_GLOBAL->statics_read and X_GLOBAL->statics_written. X_GLOBAL is
374 actually the set representing the cycle containing X. If the read and
375 written sets of X_GLOBAL has been reduced to the maximum set, we don't
376 have to look at the remaining callees. */
379 propagate_bits (ipa_reference_global_vars_info_t x_global
, struct cgraph_node
*x
)
381 struct cgraph_edge
*e
;
382 bool read_all
= x_global
->statics_read
== all_module_statics
;
383 bool write_all
= x_global
->statics_written
== all_module_statics
;
385 e
&& !(read_all
&& write_all
);
388 enum availability avail
;
389 struct cgraph_node
*y
= e
->callee
->function_symbol (&avail
);
393 /* Only look into nodes we can propagate something. */
394 int flags
= flags_from_decl_or_type (y
->decl
);
395 if (opt_for_fn (y
->decl
, flag_ipa_reference
)
396 && (avail
> AVAIL_INTERPOSABLE
397 || (avail
== AVAIL_INTERPOSABLE
&& (flags
& ECF_LEAF
))))
399 if (get_reference_vars_info (y
))
401 ipa_reference_vars_info_t y_info
= get_reference_vars_info (y
);
402 ipa_reference_global_vars_info_t y_global
= &y_info
->global
;
404 /* Calls in the current cycle do not have their global set
405 computed yet (but everything else does because we're
406 visiting nodes in topological order). */
407 if (!y_global
->statics_read
)
410 /* If the function is const, it reads no memory even if it
411 seems so to local analysis. */
412 if (flags
& ECF_CONST
)
415 union_static_var_sets (x_global
->statics_read
,
416 y_global
->statics_read
);
418 /* If the function is pure, it has no stores even if it
419 seems so to local analysis. If we cannot return from
420 the function, we can safely ignore the call. */
421 if ((flags
& ECF_PURE
)
422 || e
->cannot_lead_to_return_p ())
425 union_static_var_sets (x_global
->statics_written
,
426 y_global
->statics_written
);
434 static bool ipa_init_p
= false;
436 /* The init routine for analyzing global static variable usage. See
437 comments at top for description. */
447 reference_vars_to_consider
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
449 bitmap_obstack_initialize (&local_info_obstack
);
450 bitmap_obstack_initialize (&optimization_summary_obstack
);
451 all_module_statics
= BITMAP_ALLOC (&optimization_summary_obstack
);
452 ignore_module_statics
= BITMAP_ALLOC (&optimization_summary_obstack
);
454 node_removal_hook_holder
=
455 symtab
->add_cgraph_removal_hook (&remove_node_data
, NULL
);
456 node_duplication_hook_holder
=
457 symtab
->add_cgraph_duplication_hook (&duplicate_node_data
, NULL
);
461 /* Set up the persistent info for FN. */
463 static ipa_reference_local_vars_info_t
464 init_function_info (struct cgraph_node
*fn
)
466 ipa_reference_vars_info_t info
467 = XCNEW (struct ipa_reference_vars_info_d
);
469 /* Add the info to the tree's annotation. */
470 set_reference_vars_info (fn
, info
);
472 info
->local
.statics_read
= BITMAP_ALLOC (&local_info_obstack
);
473 info
->local
.statics_written
= BITMAP_ALLOC (&local_info_obstack
);
479 /* This is the main routine for finding the reference patterns for
480 global variables within a function FN. */
483 analyze_function (struct cgraph_node
*fn
)
485 ipa_reference_local_vars_info_t local
;
486 struct ipa_ref
*ref
= NULL
;
490 if (!opt_for_fn (fn
->decl
, flag_ipa_reference
))
492 local
= init_function_info (fn
);
493 for (i
= 0; fn
->iterate_reference (i
, ref
); i
++)
495 if (!is_a
<varpool_node
*> (ref
->referred
))
497 var
= ref
->referred
->decl
;
498 if (!is_proper_for_analysis (var
))
503 bitmap_set_bit (local
->statics_read
, DECL_UID (var
));
506 if (ref
->cannot_lead_to_return ())
508 bitmap_set_bit (local
->statics_written
, DECL_UID (var
));
517 if (fn
->cannot_return_p ())
518 bitmap_clear (local
->statics_written
);
522 /* Called when new clone is inserted to callgraph late. */
525 duplicate_node_data (struct cgraph_node
*src
, struct cgraph_node
*dst
,
526 void *data ATTRIBUTE_UNUSED
)
528 ipa_reference_optimization_summary_t ginfo
;
529 ipa_reference_optimization_summary_t dst_ginfo
;
531 ginfo
= get_reference_optimization_summary (src
);
534 dst_ginfo
= XCNEW (struct ipa_reference_optimization_summary_d
);
535 set_reference_optimization_summary (dst
, dst_ginfo
);
536 dst_ginfo
->statics_not_read
=
537 copy_static_var_set (ginfo
->statics_not_read
);
538 dst_ginfo
->statics_not_written
=
539 copy_static_var_set (ginfo
->statics_not_written
);
542 /* Called when node is removed. */
545 remove_node_data (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
547 ipa_reference_optimization_summary_t ginfo
;
548 ginfo
= get_reference_optimization_summary (node
);
551 if (ginfo
->statics_not_read
552 && ginfo
->statics_not_read
!= all_module_statics
)
553 BITMAP_FREE (ginfo
->statics_not_read
);
555 if (ginfo
->statics_not_written
556 && ginfo
->statics_not_written
!= all_module_statics
)
557 BITMAP_FREE (ginfo
->statics_not_written
);
559 set_reference_optimization_summary (node
, NULL
);
563 /* Analyze each function in the cgraph to see which global or statics
564 are read or written. */
567 generate_summary (void)
569 struct cgraph_node
*node
;
575 /* Process all of the functions next. */
576 FOR_EACH_DEFINED_FUNCTION (node
)
577 if (!node
->alias
&& !opt_for_fn (node
->decl
, flag_ipa_reference
))
579 struct ipa_ref
*ref
= NULL
;
582 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
584 if (!is_a
<varpool_node
*> (ref
->referred
))
586 var
= ref
->referred
->decl
;
587 if (!is_proper_for_analysis (var
))
589 bitmap_set_bit (ignore_module_statics
, DECL_UID (var
));
592 FOR_EACH_DEFINED_FUNCTION (node
)
593 analyze_function (node
);
596 EXECUTE_IF_SET_IN_BITMAP (all_module_statics
, 0, index
, bi
)
598 fprintf (dump_file
, "\nPromotable global:%s (uid=%u)\n",
599 get_static_name (index
), index
);
603 FOR_EACH_DEFINED_FUNCTION (node
)
604 if (node
->get_availability () >= AVAIL_INTERPOSABLE
605 && opt_for_fn (node
->decl
, flag_ipa_reference
))
607 ipa_reference_local_vars_info_t l
;
611 l
= &get_reference_vars_info (node
)->local
;
613 "\nFunction name:%s/%i:",
614 node
->asm_name (), node
->order
);
615 fprintf (dump_file
, "\n locals read: ");
617 EXECUTE_IF_SET_IN_BITMAP (l
->statics_read
,
620 fprintf (dump_file
, "%s ",
621 get_static_name (index
));
623 fprintf (dump_file
, "\n locals written: ");
624 if (l
->statics_written
)
625 EXECUTE_IF_SET_IN_BITMAP (l
->statics_written
,
628 fprintf (dump_file
, "%s ", get_static_name (index
));
633 /* Set READ_ALL/WRITE_ALL based on decl flags of NODE. */
636 read_write_all_from_decl (struct cgraph_node
*node
,
637 bool &read_all
, bool &write_all
)
639 tree decl
= node
->decl
;
640 int flags
= flags_from_decl_or_type (decl
);
641 if ((flags
& ECF_LEAF
)
642 && node
->get_availability () < AVAIL_INTERPOSABLE
)
644 else if (flags
& ECF_CONST
)
646 else if ((flags
& ECF_PURE
) || node
->cannot_return_p ())
649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
650 fprintf (dump_file
, " %s/%i -> read all\n",
651 node
->asm_name (), node
->order
);
655 /* TODO: To be able to produce sane results, we should also handle
656 common builtins, in particular throw. */
659 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
660 fprintf (dump_file
, " %s/%i -> read all, write all\n",
661 node
->asm_name (), node
->order
);
665 /* Set READ_ALL/WRITE_ALL based on decl flags of NODE or any member
666 in the cycle of NODE. */
669 get_read_write_all_from_node (struct cgraph_node
*node
,
670 bool &read_all
, bool &write_all
)
672 struct cgraph_edge
*e
, *ie
;
674 /* When function is overwritable, we can not assume anything. */
675 if (node
->get_availability () <= AVAIL_INTERPOSABLE
676 || (node
->analyzed
&& !opt_for_fn (node
->decl
, flag_ipa_reference
)))
677 read_write_all_from_decl (node
, read_all
, write_all
);
679 for (e
= node
->callees
;
680 e
&& !(read_all
&& write_all
);
683 enum availability avail
;
684 struct cgraph_node
*callee
= e
->callee
->function_symbol (&avail
);
685 gcc_checking_assert (callee
);
686 if (avail
<= AVAIL_INTERPOSABLE
687 || (callee
->analyzed
&& !opt_for_fn (callee
->decl
, flag_ipa_reference
)))
688 read_write_all_from_decl (callee
, read_all
, write_all
);
691 for (ie
= node
->indirect_calls
;
692 ie
&& !(read_all
&& write_all
);
693 ie
= ie
->next_callee
)
694 if (!(ie
->indirect_info
->ecf_flags
& ECF_CONST
))
697 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
698 fprintf (dump_file
, " indirect call -> read all\n");
699 if (!ie
->cannot_lead_to_return_p ()
700 && !(ie
->indirect_info
->ecf_flags
& ECF_PURE
))
702 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
703 fprintf (dump_file
, " indirect call -> write all\n");
709 /* Skip edges from and to nodes without ipa_reference enables. This leave
710 them out of strongy connected coponents and makes them easyto skip in the
711 propagation loop bellow. */
714 ignore_edge_p (cgraph_edge
*e
)
716 return (!opt_for_fn (e
->caller
->decl
, flag_ipa_reference
)
717 || !opt_for_fn (e
->callee
->function_symbol ()->decl
,
718 flag_ipa_reference
));
721 /* Produce the global information by preforming a transitive closure
722 on the local information that was produced by ipa_analyze_function. */
727 struct cgraph_node
*node
;
728 struct cgraph_node
**order
=
729 XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
735 cgraph_node::dump_cgraph (dump_file
);
737 remove_p
= ipa_discover_readonly_nonaddressable_vars ();
740 /* Propagate the local information through the call graph to produce
741 the global information. All the nodes within a cycle will have
742 the same info so we collapse cycles first. Then we can do the
743 propagation in one pass from the leaves to the roots. */
744 order_pos
= ipa_reduced_postorder (order
, true, true, ignore_edge_p
);
746 ipa_print_order (dump_file
, "reduced", order
, order_pos
);
748 for (i
= 0; i
< order_pos
; i
++ )
751 struct cgraph_node
*w
;
752 ipa_reference_vars_info_t node_info
;
753 ipa_reference_global_vars_info_t node_g
;
754 ipa_reference_local_vars_info_t node_l
;
755 bool read_all
= false;
756 bool write_all
= false;
759 if (node
->alias
|| !opt_for_fn (node
->decl
, flag_ipa_reference
))
762 node_info
= get_reference_vars_info (node
);
763 gcc_assert (node_info
);
764 node_l
= &node_info
->local
;
765 node_g
= &node_info
->global
;
767 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
768 fprintf (dump_file
, "Starting cycle with %s/%i\n",
769 node
->asm_name (), node
->order
);
771 vec
<cgraph_node
*> cycle_nodes
= ipa_get_nodes_in_cycle (node
);
773 /* If any node in a cycle is read_all or write_all, they all are. */
774 FOR_EACH_VEC_ELT (cycle_nodes
, x
, w
)
776 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
777 fprintf (dump_file
, " Visiting %s/%i\n",
778 w
->asm_name (), w
->order
);
779 get_read_write_all_from_node (w
, read_all
, write_all
);
780 if (read_all
&& write_all
)
784 /* Initialized the bitmaps global sets for the reduced node. */
786 node_g
->statics_read
= all_module_statics
;
788 node_g
->statics_read
= copy_static_var_set (node_l
->statics_read
);
790 node_g
->statics_written
= all_module_statics
;
792 node_g
->statics_written
= copy_static_var_set (node_l
->statics_written
);
794 /* Merge the sets of this cycle with all sets of callees reached
796 FOR_EACH_VEC_ELT (cycle_nodes
, x
, w
)
798 if (read_all
&& write_all
)
803 ipa_reference_vars_info_t w_ri
= get_reference_vars_info (w
);
804 ipa_reference_local_vars_info_t w_l
= &w_ri
->local
;
805 int flags
= flags_from_decl_or_type (w
->decl
);
807 if (!(flags
& ECF_CONST
))
808 read_all
= union_static_var_sets (node_g
->statics_read
,
810 if (!(flags
& ECF_PURE
)
811 && !w
->cannot_return_p ())
812 write_all
= union_static_var_sets (node_g
->statics_written
,
813 w_l
->statics_written
);
816 propagate_bits (node_g
, w
);
819 /* All nodes within a cycle have the same global info bitmaps. */
820 FOR_EACH_VEC_ELT (cycle_nodes
, x
, w
)
822 ipa_reference_vars_info_t w_ri
= get_reference_vars_info (w
);
823 w_ri
->global
= *node_g
;
826 cycle_nodes
.release ();
831 for (i
= 0; i
< order_pos
; i
++)
834 struct cgraph_node
*w
;
837 if (node
->alias
|| !opt_for_fn (node
->decl
, flag_ipa_reference
))
841 "\nFunction name:%s/%i:",
842 node
->asm_name (), node
->order
);
844 ipa_reference_vars_info_t node_info
= get_reference_vars_info (node
);
845 ipa_reference_global_vars_info_t node_g
= &node_info
->global
;
847 vec
<cgraph_node
*> cycle_nodes
= ipa_get_nodes_in_cycle (node
);
848 FOR_EACH_VEC_ELT (cycle_nodes
, x
, w
)
850 ipa_reference_vars_info_t w_ri
= get_reference_vars_info (w
);
851 ipa_reference_local_vars_info_t w_l
= &w_ri
->local
;
853 fprintf (dump_file
, "\n next cycle: %s/%i ",
854 w
->asm_name (), w
->order
);
855 fprintf (dump_file
, "\n locals read: ");
856 dump_static_vars_set_to_file (dump_file
, w_l
->statics_read
);
857 fprintf (dump_file
, "\n locals written: ");
858 dump_static_vars_set_to_file (dump_file
, w_l
->statics_written
);
860 cycle_nodes
.release ();
862 fprintf (dump_file
, "\n globals read: ");
863 dump_static_vars_set_to_file (dump_file
, node_g
->statics_read
);
864 fprintf (dump_file
, "\n globals written: ");
865 dump_static_vars_set_to_file (dump_file
, node_g
->statics_written
);
866 fprintf (dump_file
, "\n");
871 FOR_EACH_DEFINED_FUNCTION (node
)
873 ipa_reference_vars_info_t node_info
;
874 ipa_reference_global_vars_info_t node_g
;
875 ipa_reference_optimization_summary_t opt
;
877 node_info
= get_reference_vars_info (node
);
878 if (!node
->alias
&& opt_for_fn (node
->decl
, flag_ipa_reference
)
879 && (node
->get_availability () > AVAIL_INTERPOSABLE
880 || (flags_from_decl_or_type (node
->decl
) & ECF_LEAF
)))
882 node_g
= &node_info
->global
;
884 opt
= XCNEW (struct ipa_reference_optimization_summary_d
);
885 set_reference_optimization_summary (node
, opt
);
887 /* Create the complimentary sets. */
889 if (bitmap_empty_p (node_g
->statics_read
))
890 opt
->statics_not_read
= all_module_statics
;
893 opt
->statics_not_read
894 = BITMAP_ALLOC (&optimization_summary_obstack
);
895 if (node_g
->statics_read
!= all_module_statics
)
896 bitmap_and_compl (opt
->statics_not_read
,
898 node_g
->statics_read
);
901 if (bitmap_empty_p (node_g
->statics_written
))
902 opt
->statics_not_written
= all_module_statics
;
905 opt
->statics_not_written
906 = BITMAP_ALLOC (&optimization_summary_obstack
);
907 if (node_g
->statics_written
!= all_module_statics
)
908 bitmap_and_compl (opt
->statics_not_written
,
910 node_g
->statics_written
);
916 ipa_free_postorder_info ();
919 bitmap_obstack_release (&local_info_obstack
);
920 ipa_reference_vars_vector
.release ();
922 splay_tree_delete (reference_vars_to_consider
);
923 reference_vars_to_consider
= NULL
;
924 return remove_p
? TODO_remove_functions
: 0;
927 /* Return true if we need to write summary of NODE. */
930 write_node_summary_p (struct cgraph_node
*node
,
931 lto_symtab_encoder_t encoder
,
932 bitmap ltrans_statics
)
934 ipa_reference_optimization_summary_t info
;
936 /* See if we have (non-empty) info. */
937 if (!node
->definition
|| node
->global
.inlined_to
)
939 info
= get_reference_optimization_summary (node
);
940 if (!info
|| (bitmap_empty_p (info
->statics_not_read
)
941 && bitmap_empty_p (info
->statics_not_written
)))
944 /* See if we want to encode it.
945 Encode also referenced functions since constant folding might turn it into
948 In future we might also want to include summaries of functions references
949 by initializers of constant variables references in current unit. */
950 if (!reachable_from_this_partition_p (node
, encoder
)
951 && !referenced_from_this_partition_p (node
, encoder
))
954 /* See if the info has non-empty intersections with vars we want to encode. */
955 if (!bitmap_intersect_p (info
->statics_not_read
, ltrans_statics
)
956 && !bitmap_intersect_p (info
->statics_not_written
, ltrans_statics
))
961 /* Stream out BITS<RANS_STATICS as list of decls to OB.
962 LTRANS_STATICS_BITCOUNT specify number of bits in LTRANS_STATICS
963 or -1. When it is positive, just output -1 when
964 BITS<RANS_STATICS == BITS<RANS_STATICS. */
967 stream_out_bitmap (struct lto_simple_output_block
*ob
,
968 bitmap bits
, bitmap ltrans_statics
,
969 int ltrans_statics_bitcount
)
974 if (bits
== all_module_statics
)
976 streamer_write_hwi_stream (ob
->main_stream
, -1);
979 EXECUTE_IF_AND_IN_BITMAP (bits
, ltrans_statics
, 0, index
, bi
)
981 if (count
== ltrans_statics_bitcount
)
983 streamer_write_hwi_stream (ob
->main_stream
, -1);
986 streamer_write_hwi_stream (ob
->main_stream
, count
);
989 EXECUTE_IF_AND_IN_BITMAP (bits
, ltrans_statics
, 0, index
, bi
)
991 tree decl
= (tree
)splay_tree_lookup (reference_vars_to_consider
, index
)->value
;
992 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, decl
);
996 /* Serialize the ipa info for lto. */
999 ipa_reference_write_optimization_summary (void)
1001 struct lto_simple_output_block
*ob
1002 = lto_create_simple_output_block (LTO_section_ipa_reference
);
1003 unsigned int count
= 0;
1004 int ltrans_statics_bitcount
= 0;
1005 lto_symtab_encoder_t encoder
= ob
->decl_state
->symtab_node_encoder
;
1006 bitmap ltrans_statics
= BITMAP_ALLOC (NULL
);
1009 reference_vars_to_consider
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
1011 /* See what variables we are interested in. */
1012 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
1014 symtab_node
*snode
= lto_symtab_encoder_deref (encoder
, i
);
1015 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (snode
);
1017 && bitmap_bit_p (all_module_statics
, DECL_UID (vnode
->decl
))
1018 && referenced_from_this_partition_p (vnode
, encoder
))
1020 tree decl
= vnode
->decl
;
1021 bitmap_set_bit (ltrans_statics
, DECL_UID (decl
));
1022 splay_tree_insert (reference_vars_to_consider
,
1023 DECL_UID (decl
), (splay_tree_value
)decl
);
1024 ltrans_statics_bitcount
++;
1029 if (ltrans_statics_bitcount
)
1030 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
1032 symtab_node
*snode
= lto_symtab_encoder_deref (encoder
, i
);
1033 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (snode
);
1034 if (cnode
&& write_node_summary_p (cnode
, encoder
, ltrans_statics
))
1038 streamer_write_uhwi_stream (ob
->main_stream
, count
);
1040 stream_out_bitmap (ob
, ltrans_statics
, ltrans_statics
,
1043 /* Process all of the functions. */
1044 if (ltrans_statics_bitcount
)
1045 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
1047 symtab_node
*snode
= lto_symtab_encoder_deref (encoder
, i
);
1048 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (snode
);
1049 if (cnode
&& write_node_summary_p (cnode
, encoder
, ltrans_statics
))
1051 ipa_reference_optimization_summary_t info
;
1054 info
= get_reference_optimization_summary (cnode
);
1055 node_ref
= lto_symtab_encoder_encode (encoder
, snode
);
1056 streamer_write_uhwi_stream (ob
->main_stream
, node_ref
);
1058 stream_out_bitmap (ob
, info
->statics_not_read
, ltrans_statics
,
1059 ltrans_statics_bitcount
);
1060 stream_out_bitmap (ob
, info
->statics_not_written
, ltrans_statics
,
1061 ltrans_statics_bitcount
);
1064 BITMAP_FREE (ltrans_statics
);
1065 lto_destroy_simple_output_block (ob
);
1066 splay_tree_delete (reference_vars_to_consider
);
1069 /* Deserialize the ipa info for lto. */
1072 ipa_reference_read_optimization_summary (void)
1074 struct lto_file_decl_data
** file_data_vec
1075 = lto_get_file_decl_data ();
1076 struct lto_file_decl_data
* file_data
;
1078 bitmap_obstack_initialize (&optimization_summary_obstack
);
1080 node_removal_hook_holder
=
1081 symtab
->add_cgraph_removal_hook (&remove_node_data
, NULL
);
1082 node_duplication_hook_holder
=
1083 symtab
->add_cgraph_duplication_hook (&duplicate_node_data
, NULL
);
1084 all_module_statics
= BITMAP_ALLOC (&optimization_summary_obstack
);
1086 while ((file_data
= file_data_vec
[j
++]))
1090 struct lto_input_block
*ib
1091 = lto_create_simple_input_block (file_data
,
1092 LTO_section_ipa_reference
,
1097 unsigned int f_count
= streamer_read_uhwi (ib
);
1101 b_count
= streamer_read_hwi (ib
);
1103 fprintf (dump_file
, "all module statics:");
1104 for (i
= 0; i
< (unsigned int)b_count
; i
++)
1106 unsigned int var_index
= streamer_read_uhwi (ib
);
1107 tree v_decl
= lto_file_decl_data_get_var_decl (file_data
,
1109 bitmap_set_bit (all_module_statics
, DECL_UID (v_decl
));
1111 fprintf (dump_file
, " %s", fndecl_name (v_decl
));
1114 for (i
= 0; i
< f_count
; i
++)
1116 unsigned int j
, index
;
1117 struct cgraph_node
*node
;
1118 ipa_reference_optimization_summary_t info
;
1120 lto_symtab_encoder_t encoder
;
1122 index
= streamer_read_uhwi (ib
);
1123 encoder
= file_data
->symtab_node_encoder
;
1124 node
= dyn_cast
<cgraph_node
*> (lto_symtab_encoder_deref
1126 info
= XCNEW (struct ipa_reference_optimization_summary_d
);
1127 set_reference_optimization_summary (node
, info
);
1128 info
->statics_not_read
= BITMAP_ALLOC (&optimization_summary_obstack
);
1129 info
->statics_not_written
= BITMAP_ALLOC (&optimization_summary_obstack
);
1132 "\nFunction name:%s/%i:\n static not read:",
1133 node
->asm_name (), node
->order
);
1135 /* Set the statics not read. */
1136 v_count
= streamer_read_hwi (ib
);
1139 info
->statics_not_read
= all_module_statics
;
1141 fprintf (dump_file
, " all module statics");
1144 for (j
= 0; j
< (unsigned int)v_count
; j
++)
1146 unsigned int var_index
= streamer_read_uhwi (ib
);
1147 tree v_decl
= lto_file_decl_data_get_var_decl (file_data
,
1149 bitmap_set_bit (info
->statics_not_read
, DECL_UID (v_decl
));
1151 fprintf (dump_file
, " %s", fndecl_name (v_decl
));
1156 "\n static not written:");
1157 /* Set the statics not written. */
1158 v_count
= streamer_read_hwi (ib
);
1161 info
->statics_not_written
= all_module_statics
;
1163 fprintf (dump_file
, " all module statics");
1166 for (j
= 0; j
< (unsigned int)v_count
; j
++)
1168 unsigned int var_index
= streamer_read_uhwi (ib
);
1169 tree v_decl
= lto_file_decl_data_get_var_decl (file_data
,
1171 bitmap_set_bit (info
->statics_not_written
, DECL_UID (v_decl
));
1173 fprintf (dump_file
, " %s", fndecl_name (v_decl
));
1176 fprintf (dump_file
, "\n");
1179 lto_destroy_simple_input_block (file_data
,
1180 LTO_section_ipa_reference
,
1184 /* Fatal error here. We do not want to support compiling ltrans units with
1185 different version of compiler or different flags than the WPA unit, so
1186 this should never happen. */
1187 fatal_error ("ipa reference summary is missing in ltrans unit");
1193 const pass_data pass_data_ipa_reference
=
1195 IPA_PASS
, /* type */
1196 "static-var", /* name */
1197 OPTGROUP_NONE
, /* optinfo_flags */
1198 TV_IPA_REFERENCE
, /* tv_id */
1199 0, /* properties_required */
1200 0, /* properties_provided */
1201 0, /* properties_destroyed */
1202 0, /* todo_flags_start */
1203 0, /* todo_flags_finish */
1206 class pass_ipa_reference
: public ipa_opt_pass_d
1209 pass_ipa_reference (gcc::context
*ctxt
)
1210 : ipa_opt_pass_d (pass_data_ipa_reference
, ctxt
,
1211 NULL
, /* generate_summary */
1212 NULL
, /* write_summary */
1213 NULL
, /* read_summary */
1214 ipa_reference_write_optimization_summary
, /*
1215 write_optimization_summary */
1216 ipa_reference_read_optimization_summary
, /*
1217 read_optimization_summary */
1218 NULL
, /* stmt_fixup */
1219 0, /* function_transform_todo_flags_start */
1220 NULL
, /* function_transform */
1221 NULL
) /* variable_transform */
1224 /* opt_pass methods: */
1225 virtual bool gate (function
*)
1227 return ((in_lto_p
|| flag_ipa_reference
)
1228 /* Don't bother doing anything if the program has errors. */
1232 virtual unsigned int execute (function
*) { return propagate (); }
1234 }; // class pass_ipa_reference
1239 make_pass_ipa_reference (gcc::context
*ctxt
)
1241 return new pass_ipa_reference (ctxt
);
1244 /* Reset all state within ipa-reference.c so that we can rerun the compiler
1245 within the same process. For use by toplev::finalize. */
1248 ipa_reference_c_finalize (void)
1252 bitmap_obstack_release (&optimization_summary_obstack
);
1256 if (node_removal_hook_holder
)
1258 symtab
->remove_cgraph_removal_hook (node_removal_hook_holder
);
1259 node_removal_hook_holder
= NULL
;
1261 if (node_duplication_hook_holder
)
1263 symtab
->remove_cgraph_duplication_hook (node_duplication_hook_holder
);
1264 node_duplication_hook_holder
= NULL
;