merge with trunk @ 139506
[official-gcc.git] / gcc / ipa-reference.c
blobc8b23b6faef03af63eafc449010c4a8be6624915
1 /* Callgraph based analysis of static variables.
2 Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
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
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file gathers information about how variables whose scope is
22 confined to the compilation unit are used.
24 There are two categories of information produced by this pass:
26 1) The addressable (TREE_ADDRESSABLE) bit and readonly
27 (TREE_READONLY) bit associated with these variables is properly set
28 based on scanning all of the code withing the compilation unit.
30 2) The transitive call site specific clobber effects are computed
31 for the variables whose scope is contained within this compilation
32 unit.
34 First each function and static variable initialization is analyzed
35 to determine which local static variables are either read, written,
36 or have their address taken. Any local static that has its address
37 taken is removed from consideration. Once the local read and
38 writes are determined, a transitive closure of this information is
39 performed over the call graph to determine the worst case set of
40 side effects of each call. In later parts of the compiler, these
41 local and global sets are examined to make the call clobbering less
42 traumatic, promote some statics to registers, and improve aliasing
43 information.
45 Currently must be run after inlining decisions have been made since
46 otherwise, the local sets will not contain information that is
47 consistent with post inlined state. The global sets are not prone
48 to this problem since they are by definition transitive. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "tree.h"
55 #include "tree-flow.h"
56 #include "tree-inline.h"
57 #include "tree-pass.h"
58 #include "langhooks.h"
59 #include "pointer-set.h"
60 #include "ggc.h"
61 #include "ipa-utils.h"
62 #include "ipa-reference.h"
63 #include "c-common.h"
64 #include "gimple.h"
65 #include "cgraph.h"
66 #include "output.h"
67 #include "flags.h"
68 #include "timevar.h"
69 #include "diagnostic.h"
70 #include "langhooks.h"
72 /* This splay tree contains all of the static variables that are
73 being considered by the compilation level alias analysis. For
74 module_at_a_time compilation, this is the set of static but not
75 public variables. Any variables that either have their address
76 taken or participate in otherwise unsavory operations are deleted
77 from this list. */
78 static GTY((param1_is(int), param2_is(tree)))
79 splay_tree reference_vars_to_consider;
81 /* This bitmap is used to knock out the module static variables whose
82 addresses have been taken and passed around. */
83 static bitmap module_statics_escape;
85 /* This bitmap is used to knock out the module static variables that
86 are not readonly. */
87 static bitmap module_statics_written;
89 /* A bit is set for every module static we are considering. This is
90 ored into the local info when asm code is found that clobbers all
91 memory. */
92 static bitmap all_module_statics;
94 static struct pointer_set_t *visited_nodes;
96 static bitmap_obstack ipa_obstack;
98 /* Holders of ipa cgraph hooks: */
99 static struct cgraph_node_hook_list *function_insertion_hook_holder;
101 enum initialization_status_t
103 UNINITIALIZED,
104 RUNNING,
105 FINISHED
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_function_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_function_ann (fn)->reference_vars_info;
124 if (info)
125 return info->local;
126 else
127 /* This phase was not run. */
128 return NULL;
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_function_ann (fn)->reference_vars_info;
139 if (info)
140 return info->global;
141 else
142 /* This phase was not run. */
143 return NULL;
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. */
150 bitmap
151 ipa_reference_get_read_local (tree fn)
153 ipa_reference_local_vars_info_t l = get_local_reference_vars_info (fn);
154 if (l)
155 return l->statics_read;
156 else
157 return NULL;
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. */
164 bitmap
165 ipa_reference_get_written_local (tree fn)
167 ipa_reference_local_vars_info_t l = get_local_reference_vars_info (fn);
168 if (l)
169 return l->statics_written;
170 else
171 return NULL;
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. */
178 bitmap
179 ipa_reference_get_read_global (tree fn)
181 ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
182 if (g)
183 return g->statics_read;
184 else
185 return NULL;
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. */
193 bitmap
194 ipa_reference_get_written_global (tree fn)
196 ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
197 if (g)
198 return g->statics_written;
199 else
200 return NULL;
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. */
207 bitmap
208 ipa_reference_get_not_read_global (tree fn)
210 ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
211 if (g)
212 return g->statics_not_read;
213 else
214 return NULL;
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. */
222 bitmap
223 ipa_reference_get_not_written_global (tree fn)
225 ipa_reference_global_vars_info_t g = get_global_reference_vars_info (fn);
226 if (g)
227 return g->statics_not_written;
228 else
229 return NULL;
234 /* Add VAR to all_module_statics and the two
235 reference_vars_to_consider* sets. */
237 static inline void
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. */
252 static inline bool
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)))
258 return false;
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))
263 return false;
265 /* Do not care about a local automatic that is not static. */
266 if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
267 return false;
269 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
270 return false;
272 /* We cannot touch decls where the type needs constructing. */
273 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
274 return false;
276 /* This is a variable we care about. Check if we have seen it
277 before, and if not add it the set of variables we care about. */
278 if (!bitmap_bit_p (all_module_statics, DECL_UID (t)))
279 add_static_var (t);
281 return true;
284 /* If T is a VAR_DECL for a static that we are interested in, add the
285 uid to the bitmap. */
287 static void
288 check_operand (ipa_reference_local_vars_info_t local,
289 tree t, bool checking_write)
291 if (!t) return;
293 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
294 && (has_proper_scope_for_analysis (t)))
296 if (checking_write)
298 if (local)
299 bitmap_set_bit (local->statics_written, DECL_UID (t));
300 /* Mark the write so we can tell which statics are
301 readonly. */
302 if (module_statics_written)
303 bitmap_set_bit (module_statics_written, DECL_UID (t));
305 else if (local)
306 bitmap_set_bit (local->statics_read, DECL_UID (t));
310 /* Examine tree T for references to static variables. All internal
311 references like array references or indirect references are added
312 to the READ_BM. Direct references are added to either READ_BM or
313 WRITE_BM depending on the value of CHECKING_WRITE. */
315 static void
316 check_tree (ipa_reference_local_vars_info_t local, tree t, bool checking_write)
318 if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
319 return;
321 while (TREE_CODE (t) == REALPART_EXPR
322 || TREE_CODE (t) == IMAGPART_EXPR
323 || handled_component_p (t))
325 if (TREE_CODE (t) == ARRAY_REF)
326 check_operand (local, TREE_OPERAND (t, 1), false);
327 t = TREE_OPERAND (t, 0);
330 /* The bottom of an indirect reference can only be read, not
331 written. So just recurse and whatever we find, check it against
332 the read bitmaps. */
334 /* if (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF) */
335 /* FIXME when we have array_ref's of pointers. */
336 if (INDIRECT_REF_P (t))
337 check_tree (local, TREE_OPERAND (t, 0), false);
339 if (SSA_VAR_P (t))
340 check_operand (local, t, checking_write);
343 /* Scan tree T to see if there are any addresses taken in within T. */
345 static void
346 look_for_address_of (tree t)
348 if (TREE_CODE (t) == ADDR_EXPR)
350 tree x = get_base_var (t);
351 if (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == FUNCTION_DECL)
352 if (has_proper_scope_for_analysis (x) && module_statics_escape)
353 bitmap_set_bit (module_statics_escape, DECL_UID (x));
357 /* Check to see if T is a read or address of operation on a static var
358 we are interested in analyzing. LOCAL is passed in to get access
359 to its bit vectors. Local is NULL if this is called from a static
360 initializer. */
362 static void
363 check_rhs_var (ipa_reference_local_vars_info_t local, tree t)
365 look_for_address_of (t);
367 if (local == NULL)
368 return;
370 check_tree(local, t, false);
373 /* Check to see if T is an assignment to a static var we are
374 interested in analyzing. LOCAL is passed in to get access to its bit
375 vectors. */
377 static void
378 check_lhs_var (ipa_reference_local_vars_info_t local, tree t)
380 if (local == NULL)
381 return;
383 check_tree(local, t, true);
386 /* This is a scaled down version of get_asm_expr_operands from
387 tree_ssa_operands.c. The version there runs much later and assumes
388 that aliasing information is already available. Here we are just
389 trying to find if the set of inputs and outputs contain references
390 or address of operations to local static variables. FN is the
391 function being analyzed and STMT is the actual asm statement. */
393 static void
394 get_asm_stmt_operands (ipa_reference_local_vars_info_t local, gimple stmt)
396 size_t noutputs = gimple_asm_noutputs (stmt);
397 const char **oconstraints
398 = (const char **) alloca ((noutputs) * sizeof (const char *));
399 size_t i;
400 tree op;
401 const char *constraint;
402 bool allows_mem, allows_reg, is_inout;
404 for (i = 0; i < noutputs; i++)
406 op = gimple_asm_output_op (stmt, i);
407 oconstraints[i] = constraint
408 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
409 parse_output_constraint (&constraint, i, 0, 0,
410 &allows_mem, &allows_reg, &is_inout);
412 check_lhs_var (local, TREE_VALUE (op));
415 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
417 op = gimple_asm_input_op (stmt, i);
418 constraint
419 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
420 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
421 oconstraints, &allows_mem, &allows_reg);
423 check_rhs_var (local, TREE_VALUE (op));
426 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
428 op = gimple_asm_clobber_op (stmt, i);
429 if (simple_cst_equal(TREE_VALUE (op), memory_identifier_string) == 1)
431 /* Abandon all hope, ye who enter here. */
432 local->calls_read_all = true;
433 local->calls_write_all = true;
438 /* Check the parameters of a function call from CALLER to CALL_EXPR to
439 see if any of them are static vars. Also check to see if this is
440 either an indirect call, a call outside the compilation unit, or
441 has special attributes that effect the clobbers. The caller
442 parameter is the tree node for the caller and the second operand is
443 the tree node for the entire call expression. */
445 static void
446 check_call (ipa_reference_local_vars_info_t local, gimple stmt)
448 int flags = gimple_call_flags (stmt);
449 tree operand;
450 tree callee_t = gimple_call_fndecl (stmt);
451 enum availability avail = AVAIL_NOT_AVAILABLE;
452 size_t i;
454 if ((operand = gimple_call_lhs (stmt)) != NULL)
455 check_lhs_var (local, operand);
457 for (i = 0; i < gimple_call_num_args (stmt); i++)
458 check_rhs_var (local, gimple_call_arg (stmt, i));
460 if (callee_t)
462 struct cgraph_node* callee = cgraph_node(callee_t);
463 avail = cgraph_function_body_availability (callee);
466 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
467 if (local)
469 if (flags & ECF_PURE)
470 local->calls_read_all = true;
471 else
473 local->calls_read_all = true;
474 local->calls_write_all = true;
479 /* TP is the part of the tree currently under the microscope.
480 WALK_SUBTREES is part of the walk_tree api but is unused here.
481 DATA is cgraph_node of the function being walked. */
483 /* FIXME: When this is converted to run over SSA form, this code
484 should be converted to use the operand scanner. */
486 static tree
487 scan_stmt_for_static_refs (gimple_stmt_iterator *gsip, bool *handled_ops_p,
488 struct walk_stmt_info *data)
490 struct cgraph_node *fn = (struct cgraph_node *) data->info;
491 gimple stmt = gsi_stmt (*gsip);
492 ipa_reference_local_vars_info_t local = NULL;
493 if (fn)
494 local = get_reference_vars_info_from_cgraph (fn)->local;
496 switch (gimple_code (stmt))
498 case GIMPLE_ASSIGN:
500 /* First look on the lhs and see what variable is stored to */
501 tree lhs = gimple_assign_lhs (stmt);
502 tree rhs1 = gimple_assign_rhs1 (stmt);
503 tree rhs2 = gimple_assign_rhs2 (stmt);
504 enum tree_code code = gimple_assign_rhs_code (stmt);
506 check_lhs_var (local, lhs);
508 /* For the purposes of figuring out what the cast affects */
510 /* Next check the operands on the rhs to see if they are ok. */
511 switch (TREE_CODE_CLASS (code))
513 case tcc_binary:
514 case tcc_comparison:
515 check_rhs_var (local, rhs1);
516 check_rhs_var (local, rhs2);
517 break;
519 case tcc_unary:
520 case tcc_reference:
521 case tcc_declaration:
522 check_rhs_var (local, rhs1);
523 break;
525 case tcc_expression:
526 switch (code)
528 case ADDR_EXPR:
529 check_rhs_var (local, rhs1);
530 break;
531 default:
532 break;
534 break;
535 default:
536 break;
538 *handled_ops_p = true;
540 break;
542 case GIMPLE_LABEL:
543 if (DECL_NONLOCAL (gimple_label_label (stmt)))
545 /* Target of long jump. */
546 local->calls_read_all = true;
547 local->calls_write_all = true;
549 break;
551 case GIMPLE_CALL:
552 check_call (local, stmt);
553 *handled_ops_p = true;
554 break;
556 case GIMPLE_ASM:
557 get_asm_stmt_operands (local, stmt);
558 *handled_ops_p = true;
559 break;
561 default:
562 break;
564 return NULL;
567 /* Call-back to scan GIMPLE operands for static references. This is supposed
568 to work with scan_stmt_for_static_refs so the real call-back data is stored
569 inside a walk_stmt_info struct. Callers using the walk_tree interface must
570 also wrap the call-back data in a walk_stmt_info struct. */
572 static tree
573 scan_op_for_static_refs (tree *tp, int *walk_subtrees, void *data)
575 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
576 struct cgraph_node *fn = (struct cgraph_node *) wi->info;
577 tree t = *tp;
578 ipa_reference_local_vars_info_t local = NULL;
579 if (fn)
580 local = get_reference_vars_info_from_cgraph (fn)->local;
582 switch (TREE_CODE (t))
584 case VAR_DECL:
585 if (DECL_INITIAL (t))
586 walk_tree (&DECL_INITIAL (t), scan_op_for_static_refs, data,
587 wi->pset);
588 *walk_subtrees = 0;
589 break;
591 case ADDR_EXPR:
592 /* This case is here to find addresses on rhs of constructors in
593 decl_initial of static variables. */
594 check_rhs_var (local, t);
595 *walk_subtrees = 0;
596 break;
598 default:
599 break;
601 return NULL;
604 /* Lookup the tree node for the static variable that has UID. */
605 static tree
606 get_static_decl (int index)
608 splay_tree_node stn =
609 splay_tree_lookup (reference_vars_to_consider, index);
610 if (stn)
611 return (tree)stn->value;
612 return NULL;
615 /* Lookup the tree node for the static variable that has UID and
616 convert the name to a string for debugging. */
618 static const char *
619 get_static_name (int index)
621 splay_tree_node stn =
622 splay_tree_lookup (reference_vars_to_consider, index);
623 if (stn)
624 return lang_hooks.decl_printable_name ((tree)(stn->value), 2);
625 return NULL;
628 /* Or in all of the bits from every callee into X, the caller's, bit
629 vector. There are several cases to check to avoid the sparse
630 bitmap oring. */
632 static void
633 propagate_bits (struct cgraph_node *x)
635 ipa_reference_vars_info_t x_info = get_reference_vars_info_from_cgraph (x);
636 ipa_reference_global_vars_info_t x_global = x_info->global;
638 struct cgraph_edge *e;
639 for (e = x->callees; e; e = e->next_callee)
641 struct cgraph_node *y = e->callee;
643 /* Only look at the master nodes and skip external nodes. */
644 y = cgraph_master_clone (y);
645 if (y)
647 if (get_reference_vars_info_from_cgraph (y))
649 ipa_reference_vars_info_t y_info
650 = get_reference_vars_info_from_cgraph (y);
651 ipa_reference_global_vars_info_t y_global = y_info->global;
653 if (x_global->statics_read
654 != all_module_statics)
656 if (y_global->statics_read
657 == all_module_statics)
659 BITMAP_FREE (x_global->statics_read);
660 x_global->statics_read
661 = all_module_statics;
663 /* Skip bitmaps that are pointer equal to node's bitmap
664 (no reason to spin within the cycle). */
665 else if (x_global->statics_read
666 != y_global->statics_read)
667 bitmap_ior_into (x_global->statics_read,
668 y_global->statics_read);
671 if (x_global->statics_written
672 != all_module_statics)
674 if (y_global->statics_written
675 == all_module_statics)
677 BITMAP_FREE (x_global->statics_written);
678 x_global->statics_written
679 = all_module_statics;
681 /* Skip bitmaps that are pointer equal to node's bitmap
682 (no reason to spin within the cycle). */
683 else if (x_global->statics_written
684 != y_global->statics_written)
685 bitmap_ior_into (x_global->statics_written,
686 y_global->statics_written);
689 else
691 gcc_unreachable ();
697 /* Look at all of the callees of X to see which ones represent inlined
698 calls. For each of these callees, merge their local info into
699 TARGET and check their children recursively.
701 This function goes away when Jan changes the inliner and IPA
702 analysis so that this is not run between the time when inlining
703 decisions are made and when the inlining actually occurs. */
705 static void
706 merge_callee_local_info (struct cgraph_node *target,
707 struct cgraph_node *x)
709 struct cgraph_edge *e;
710 ipa_reference_local_vars_info_t x_l =
711 get_reference_vars_info_from_cgraph (target)->local;
713 /* Make the world safe for tail recursion. */
714 struct ipa_dfs_info *node_info = (struct ipa_dfs_info *) x->aux;
716 if (node_info->aux)
717 return;
719 node_info->aux = x;
721 for (e = x->callees; e; e = e->next_callee)
723 struct cgraph_node *y = e->callee;
724 if (y->global.inlined_to)
726 ipa_reference_vars_info_t y_info;
727 ipa_reference_local_vars_info_t y_l;
728 struct cgraph_node* orig_y = y;
730 y = cgraph_master_clone (y);
731 if (y)
733 y_info = get_reference_vars_info_from_cgraph (y);
734 y_l = y_info->local;
735 if (x_l != y_l)
737 bitmap_ior_into (x_l->statics_read,
738 y_l->statics_read);
739 bitmap_ior_into (x_l->statics_written,
740 y_l->statics_written);
742 x_l->calls_read_all |= y_l->calls_read_all;
743 x_l->calls_write_all |= y_l->calls_write_all;
744 merge_callee_local_info (target, y);
746 else
748 fprintf(stderr, "suspect inlining of ");
749 dump_cgraph_node (stderr, orig_y);
750 fprintf(stderr, "\ninto ");
751 dump_cgraph_node (stderr, target);
752 dump_cgraph (stderr);
753 gcc_assert(false);
758 node_info->aux = NULL;
761 /* The init routine for analyzing global static variable usage. See
762 comments at top for description. */
763 static void
764 ipa_init (void)
766 struct cgraph_node *node;
767 memory_identifier_string = build_string(7, "memory");
769 reference_vars_to_consider =
770 splay_tree_new_ggc (splay_tree_compare_ints);
772 bitmap_obstack_initialize (&ipa_obstack);
773 module_statics_escape = BITMAP_ALLOC (&ipa_obstack);
774 module_statics_written = BITMAP_ALLOC (&ipa_obstack);
775 all_module_statics = BITMAP_ALLOC (&ipa_obstack);
777 /* This will add NODE->DECL to the splay trees. */
778 for (node = cgraph_nodes; node; node = node->next)
779 has_proper_scope_for_analysis (node->decl);
781 /* There are some shared nodes, in particular the initializers on
782 static declarations. We do not need to scan them more than once
783 since all we would be interested in are the addressof
784 operations. */
785 visited_nodes = pointer_set_create ();
788 /* Check out the rhs of a static or global initialization VNODE to see
789 if any of them contain addressof operations. Note that some of
790 these variables may not even be referenced in the code in this
791 compilation unit but their right hand sides may contain references
792 to variables defined within this unit. */
794 static void
795 analyze_variable (struct varpool_node *vnode)
797 struct walk_stmt_info wi;
798 tree global = vnode->decl;
800 memset (&wi, 0, sizeof (wi));
801 wi.pset = visited_nodes;
802 walk_tree (&DECL_INITIAL (global), scan_op_for_static_refs,
803 &wi, wi.pset);
806 /* Set up the persistent info for FN. */
808 static ipa_reference_local_vars_info_t
809 init_function_info (struct cgraph_node *fn)
811 ipa_reference_vars_info_t info
812 = XCNEW (struct ipa_reference_vars_info_d);
813 ipa_reference_local_vars_info_t l
814 = XCNEW (struct ipa_reference_local_vars_info_d);
815 tree decl = fn->decl;
817 /* Add the info to the tree's annotation. */
818 get_function_ann (decl)->reference_vars_info = info;
820 info->local = l;
821 l->statics_read = BITMAP_ALLOC (&ipa_obstack);
822 l->statics_written = BITMAP_ALLOC (&ipa_obstack);
824 return l;
827 /* This is the main routine for finding the reference patterns for
828 global variables within a function FN. */
830 static void
831 analyze_function (struct cgraph_node *fn)
833 ipa_reference_local_vars_info_t l = init_function_info (fn);
834 tree decl = fn->decl;
835 struct walk_stmt_info wi;
836 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
837 basic_block this_block;
839 if (dump_file)
840 fprintf (dump_file, "\n local analysis of %s\n", cgraph_node_name (fn));
842 FOR_EACH_BB_FN (this_block, this_cfun)
844 gimple_stmt_iterator gsi;
845 gimple phi;
846 tree op;
847 use_operand_p use;
848 ssa_op_iter iter;
850 /* Find the addresses taken in phi node arguments. */
851 for (gsi = gsi_start_phis (this_block);
852 !gsi_end_p (gsi);
853 gsi_next (&gsi))
855 phi = gsi_stmt (gsi);
856 FOR_EACH_PHI_ARG (use, phi, iter, SSA_OP_USE)
858 op = USE_FROM_PTR (use);
859 if (TREE_CODE (op) == ADDR_EXPR)
860 check_rhs_var (l, op);
864 memset (&wi, 0, sizeof (wi));
865 wi.info = fn;
866 wi.pset = visited_nodes;
867 for (gsi = gsi_start_bb (this_block); !gsi_end_p (gsi); gsi_next (&gsi))
868 walk_gimple_stmt (&gsi, scan_stmt_for_static_refs,
869 scan_op_for_static_refs, &wi);
872 /* There may be const decls with interesting right hand sides. */
873 if (DECL_STRUCT_FUNCTION (decl))
875 tree step;
876 for (step = DECL_STRUCT_FUNCTION (decl)->local_decls;
877 step;
878 step = TREE_CHAIN (step))
880 tree var = TREE_VALUE (step);
881 if (TREE_CODE (var) == VAR_DECL
882 && DECL_INITIAL (var)
883 && !TREE_STATIC (var))
885 memset (&wi, 0, sizeof (wi));
886 wi.info = fn;
887 wi.pset = visited_nodes;
888 walk_tree (&DECL_INITIAL (var), scan_op_for_static_refs,
889 &wi, wi.pset);
895 /* If FN is avail == AVAIL_OVERWRITABLE, replace the effects bit
896 vectors with worst case bit vectors. We had to analyze it above to
897 find out if it took the address of any statics. However, now that
898 we know that, we can get rid of all of the other side effects. */
900 static void
901 clean_function (struct cgraph_node *fn)
903 ipa_reference_vars_info_t info = get_reference_vars_info_from_cgraph (fn);
904 ipa_reference_local_vars_info_t l = info->local;
905 ipa_reference_global_vars_info_t g = info->global;
907 if (l)
909 if (l->statics_read
910 && l->statics_read != all_module_statics)
911 BITMAP_FREE (l->statics_read);
912 if (l->statics_written
913 &&l->statics_written != all_module_statics)
914 BITMAP_FREE (l->statics_written);
915 free (l);
918 if (g)
920 if (g->statics_read
921 && g->statics_read != all_module_statics)
922 BITMAP_FREE (g->statics_read);
924 if (g->statics_written
925 && g->statics_written != all_module_statics)
926 BITMAP_FREE (g->statics_written);
928 if (g->statics_not_read
929 && g->statics_not_read != all_module_statics)
930 BITMAP_FREE (g->statics_not_read);
932 if (g->statics_not_written
933 && g->statics_not_written != all_module_statics)
934 BITMAP_FREE (g->statics_not_written);
935 free (g);
938 free (get_function_ann (fn->decl)->reference_vars_info);
939 get_function_ann (fn->decl)->reference_vars_info = NULL;
942 /* Called when new function is inserted to callgraph late. */
943 static void
944 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
946 /* There are some shared nodes, in particular the initializers on
947 static declarations. We do not need to scan them more than once
948 since all we would be interested in are the addressof
949 operations. */
950 visited_nodes = pointer_set_create ();
951 analyze_function (node);
952 pointer_set_destroy (visited_nodes);
953 visited_nodes = NULL;
956 /* Analyze each function in the cgraph to see which global or statics
957 are read or written. */
959 static void
960 generate_summary (void)
962 struct cgraph_node *node;
963 struct varpool_node *vnode;
964 unsigned int index;
965 bitmap_iterator bi;
966 bitmap module_statics_readonly;
967 bitmap bm_temp;
969 function_insertion_hook_holder =
970 cgraph_add_function_insertion_hook (&add_new_function, NULL);
971 ipa_init ();
972 module_statics_readonly = BITMAP_ALLOC (&ipa_obstack);
973 bm_temp = BITMAP_ALLOC (&ipa_obstack);
975 /* Process all of the variables first. */
976 FOR_EACH_STATIC_INITIALIZER (vnode)
977 analyze_variable (vnode);
979 /* Process all of the functions next.
981 We do not want to process any of the clones so we check that this
982 is a master clone. However, we do need to process any
983 AVAIL_OVERWRITABLE functions (these are never clones) because
984 they may cause a static variable to escape. The code that can
985 overwrite such a function cannot access the statics because it
986 would not be in the same compilation unit. When the analysis is
987 finished, the computed information of these AVAIL_OVERWRITABLE is
988 replaced with worst case info.
990 for (node = cgraph_nodes; node; node = node->next)
991 if (node->analyzed
992 && (cgraph_is_master_clone (node)
993 || (cgraph_function_body_availability (node)
994 == AVAIL_OVERWRITABLE)))
995 analyze_function (node);
997 pointer_set_destroy (visited_nodes);
998 visited_nodes = NULL;
1000 /* Prune out the variables that were found to behave badly
1001 (i.e. have their address taken). */
1002 EXECUTE_IF_SET_IN_BITMAP (module_statics_escape, 0, index, bi)
1004 splay_tree_remove (reference_vars_to_consider, index);
1007 bitmap_and_compl_into (all_module_statics,
1008 module_statics_escape);
1010 bitmap_and_compl (module_statics_readonly, all_module_statics,
1011 module_statics_written);
1013 /* If the address is not taken, we can unset the addressable bit
1014 on this variable. */
1015 EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi)
1017 tree var = get_static_decl (index);
1018 TREE_ADDRESSABLE (var) = 0;
1019 if (dump_file)
1020 fprintf (dump_file, "Not TREE_ADDRESSABLE var %s\n",
1021 get_static_name (index));
1024 /* If the variable is never written, we can set the TREE_READONLY
1025 flag. Additionally if it has a DECL_INITIAL that is made up of
1026 constants we can treat the entire global as a constant. */
1028 bitmap_and_compl (module_statics_readonly, all_module_statics,
1029 module_statics_written);
1030 EXECUTE_IF_SET_IN_BITMAP (module_statics_readonly, 0, index, bi)
1032 tree var = get_static_decl (index);
1034 /* Readonly on a function decl is very different from the
1035 variable. */
1036 if (TREE_CODE (var) == FUNCTION_DECL)
1037 continue;
1039 /* Ignore variables in named sections - changing TREE_READONLY
1040 changes the section flags, potentially causing conflicts with
1041 other variables in the same named section. */
1042 if (DECL_SECTION_NAME (var) == NULL_TREE)
1044 TREE_READONLY (var) = 1;
1045 if (dump_file)
1046 fprintf (dump_file, "read-only var %s\n",
1047 get_static_name (index));
1051 BITMAP_FREE(module_statics_escape);
1052 BITMAP_FREE(module_statics_written);
1053 module_statics_escape = NULL;
1054 module_statics_written = NULL;
1056 if (dump_file)
1057 EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi)
1059 fprintf (dump_file, "\nPromotable global:%s",
1060 get_static_name (index));
1063 for (node = cgraph_nodes; node; node = node->next)
1064 if (node->analyzed
1065 && (cgraph_is_master_clone (node)
1066 || (cgraph_function_body_availability (node)
1067 == AVAIL_OVERWRITABLE)))
1069 ipa_reference_local_vars_info_t l;
1070 l = get_reference_vars_info_from_cgraph (node)->local;
1072 /* Any variables that are not in all_module_statics are
1073 removed from the local maps. This will include all of the
1074 variables that were found to escape in the function
1075 scanning. */
1076 bitmap_and_into (l->statics_read,
1077 all_module_statics);
1078 bitmap_and_into (l->statics_written,
1079 all_module_statics);
1082 BITMAP_FREE(module_statics_readonly);
1083 BITMAP_FREE(bm_temp);
1085 if (dump_file)
1086 for (node = cgraph_nodes; node; node = node->next)
1087 if (node->analyzed
1088 && (cgraph_is_master_clone (node)
1089 || (cgraph_function_body_availability (node)
1090 == AVAIL_OVERWRITABLE)))
1092 ipa_reference_local_vars_info_t l;
1093 unsigned int index;
1094 bitmap_iterator bi;
1096 l = get_reference_vars_info_from_cgraph (node)->local;
1097 fprintf (dump_file,
1098 "\nFunction name:%s/%i:",
1099 cgraph_node_name (node), node->uid);
1100 fprintf (dump_file, "\n locals read: ");
1101 EXECUTE_IF_SET_IN_BITMAP (l->statics_read,
1102 0, index, bi)
1104 fprintf (dump_file, "%s ",
1105 get_static_name (index));
1107 fprintf (dump_file, "\n locals written: ");
1108 EXECUTE_IF_SET_IN_BITMAP (l->statics_written,
1109 0, index, bi)
1111 fprintf(dump_file, "%s ",
1112 get_static_name (index));
1117 /* Produce the global information by preforming a transitive closure
1118 on the local information that was produced by ipa_analyze_function
1119 and ipa_analyze_variable. */
1121 static unsigned int
1122 propagate (void)
1124 struct cgraph_node *node;
1125 struct cgraph_node *w;
1126 struct cgraph_node **order =
1127 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1128 int order_pos = ipa_utils_reduced_inorder (order, false, true);
1129 int i;
1131 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1132 if (dump_file)
1133 dump_cgraph (dump_file);
1135 /* Propagate the local information thru the call graph to produce
1136 the global information. All the nodes within a cycle will have
1137 the same info so we collapse cycles first. Then we can do the
1138 propagation in one pass from the leaves to the roots. */
1139 order_pos = ipa_utils_reduced_inorder (order, true, true);
1140 if (dump_file)
1141 ipa_utils_print_order(dump_file, "reduced", order, order_pos);
1143 for (i = 0; i < order_pos; i++ )
1145 ipa_reference_vars_info_t node_info;
1146 ipa_reference_global_vars_info_t node_g =
1147 XCNEW (struct ipa_reference_global_vars_info_d);
1148 ipa_reference_local_vars_info_t node_l;
1150 bool read_all;
1151 bool write_all;
1152 struct ipa_dfs_info * w_info;
1154 node = order[i];
1155 node_info = get_reference_vars_info_from_cgraph (node);
1156 if (!node_info)
1158 dump_cgraph_node (stderr, node);
1159 dump_cgraph (stderr);
1160 gcc_unreachable ();
1163 node_info->global = node_g;
1164 node_l = node_info->local;
1166 read_all = node_l->calls_read_all;
1167 write_all = node_l->calls_write_all;
1169 /* If any node in a cycle is calls_read_all or calls_write_all
1170 they all are. */
1171 w_info = (struct ipa_dfs_info *) node->aux;
1172 w = w_info->next_cycle;
1173 while (w)
1175 ipa_reference_local_vars_info_t w_l =
1176 get_reference_vars_info_from_cgraph (w)->local;
1177 read_all |= w_l->calls_read_all;
1178 write_all |= w_l->calls_write_all;
1180 w_info = (struct ipa_dfs_info *) w->aux;
1181 w = w_info->next_cycle;
1184 /* Initialized the bitmaps for the reduced nodes */
1185 if (read_all)
1186 node_g->statics_read = all_module_statics;
1187 else
1189 node_g->statics_read = BITMAP_ALLOC (&ipa_obstack);
1190 bitmap_copy (node_g->statics_read,
1191 node_l->statics_read);
1194 if (write_all)
1195 node_g->statics_written = all_module_statics;
1196 else
1198 node_g->statics_written = BITMAP_ALLOC (&ipa_obstack);
1199 bitmap_copy (node_g->statics_written,
1200 node_l->statics_written);
1203 w_info = (struct ipa_dfs_info *) node->aux;
1204 w = w_info->next_cycle;
1205 while (w)
1207 ipa_reference_vars_info_t w_ri =
1208 get_reference_vars_info_from_cgraph (w);
1209 ipa_reference_local_vars_info_t w_l = w_ri->local;
1211 /* All nodes within a cycle share the same global info bitmaps. */
1212 w_ri->global = node_g;
1214 /* These global bitmaps are initialized from the local info
1215 of all of the nodes in the region. However there is no
1216 need to do any work if the bitmaps were set to
1217 all_module_statics. */
1218 if (!read_all)
1219 bitmap_ior_into (node_g->statics_read,
1220 w_l->statics_read);
1221 if (!write_all)
1222 bitmap_ior_into (node_g->statics_written,
1223 w_l->statics_written);
1224 w_info = (struct ipa_dfs_info *) w->aux;
1225 w = w_info->next_cycle;
1228 w = node;
1229 while (w)
1231 propagate_bits (w);
1232 w_info = (struct ipa_dfs_info *) w->aux;
1233 w = w_info->next_cycle;
1237 /* Need to fix up the local information sets. The information that
1238 has been gathered so far is preinlining. However, the
1239 compilation will progress post inlining so the local sets for the
1240 inlined calls need to be merged into the callers. Note that the
1241 local sets are not shared between all of the nodes in a cycle so
1242 those nodes in the cycle must be processed explicitly. */
1243 for (i = 0; i < order_pos; i++ )
1245 struct ipa_dfs_info * w_info;
1246 node = order[i];
1247 merge_callee_local_info (node, node);
1249 w_info = (struct ipa_dfs_info *) node->aux;
1250 w = w_info->next_cycle;
1251 while (w)
1253 merge_callee_local_info (w, w);
1254 w_info = (struct ipa_dfs_info *) w->aux;
1255 w = w_info->next_cycle;
1259 if (dump_file)
1261 for (i = 0; i < order_pos; i++ )
1263 ipa_reference_vars_info_t node_info;
1264 ipa_reference_global_vars_info_t node_g;
1265 ipa_reference_local_vars_info_t node_l;
1266 unsigned int index;
1267 bitmap_iterator bi;
1268 struct ipa_dfs_info * w_info;
1270 node = order[i];
1271 node_info = get_reference_vars_info_from_cgraph (node);
1272 node_g = node_info->global;
1273 node_l = node_info->local;
1274 fprintf (dump_file,
1275 "\nFunction name:%s/%i:",
1276 cgraph_node_name (node), node->uid);
1277 fprintf (dump_file, "\n locals read: ");
1278 EXECUTE_IF_SET_IN_BITMAP (node_l->statics_read,
1279 0, index, bi)
1281 fprintf (dump_file, "%s ",
1282 get_static_name (index));
1284 fprintf (dump_file, "\n locals written: ");
1285 EXECUTE_IF_SET_IN_BITMAP (node_l->statics_written,
1286 0, index, bi)
1288 fprintf(dump_file, "%s ",
1289 get_static_name (index));
1292 w_info = (struct ipa_dfs_info *) node->aux;
1293 w = w_info->next_cycle;
1294 while (w)
1296 ipa_reference_vars_info_t w_ri =
1297 get_reference_vars_info_from_cgraph (w);
1298 ipa_reference_local_vars_info_t w_l = w_ri->local;
1299 fprintf (dump_file, "\n next cycle: %s/%i ",
1300 cgraph_node_name (w), w->uid);
1301 fprintf (dump_file, "\n locals read: ");
1302 EXECUTE_IF_SET_IN_BITMAP (w_l->statics_read,
1303 0, index, bi)
1305 fprintf (dump_file, "%s ",
1306 get_static_name (index));
1309 fprintf (dump_file, "\n locals written: ");
1310 EXECUTE_IF_SET_IN_BITMAP (w_l->statics_written,
1311 0, index, bi)
1313 fprintf(dump_file, "%s ",
1314 get_static_name (index));
1318 w_info = (struct ipa_dfs_info *) w->aux;
1319 w = w_info->next_cycle;
1321 fprintf (dump_file, "\n globals read: ");
1322 EXECUTE_IF_SET_IN_BITMAP (node_g->statics_read,
1323 0, index, bi)
1325 fprintf (dump_file, "%s ",
1326 get_static_name (index));
1328 fprintf (dump_file, "\n globals written: ");
1329 EXECUTE_IF_SET_IN_BITMAP (node_g->statics_written,
1330 0, index, bi)
1332 fprintf (dump_file, "%s ",
1333 get_static_name (index));
1338 /* Cleanup. */
1339 for (i = 0; i < order_pos; i++ )
1341 ipa_reference_vars_info_t node_info;
1342 ipa_reference_global_vars_info_t node_g;
1343 node = order[i];
1344 node_info = get_reference_vars_info_from_cgraph (node);
1345 node_g = node_info->global;
1347 /* Create the complimentary sets. These are more useful for
1348 certain apis. */
1349 node_g->statics_not_read = BITMAP_ALLOC (&ipa_obstack);
1350 node_g->statics_not_written = BITMAP_ALLOC (&ipa_obstack);
1352 if (node_g->statics_read != all_module_statics)
1354 bitmap_and_compl (node_g->statics_not_read,
1355 all_module_statics,
1356 node_g->statics_read);
1359 if (node_g->statics_written
1360 != all_module_statics)
1361 bitmap_and_compl (node_g->statics_not_written,
1362 all_module_statics,
1363 node_g->statics_written);
1366 free (order);
1368 for (node = cgraph_nodes; node; node = node->next)
1370 /* Get rid of the aux information. */
1372 if (node->aux)
1374 free (node->aux);
1375 node->aux = NULL;
1378 if (node->analyzed
1379 && (cgraph_function_body_availability (node) == AVAIL_OVERWRITABLE))
1380 clean_function (node);
1382 return 0;
1386 static bool
1387 gate_reference (void)
1389 return (flag_ipa_reference
1390 /* Don't bother doing anything if the program has errors. */
1391 && !(errorcount || sorrycount));
1394 struct ipa_opt_pass pass_ipa_reference =
1397 IPA_PASS,
1398 "static-var", /* name */
1399 gate_reference, /* gate */
1400 propagate, /* execute */
1401 NULL, /* sub */
1402 NULL, /* next */
1403 0, /* static_pass_number */
1404 TV_IPA_REFERENCE, /* tv_id */
1405 0, /* properties_required */
1406 0, /* properties_provided */
1407 0, /* properties_destroyed */
1408 0, /* todo_flags_start */
1409 0 /* todo_flags_finish */
1411 generate_summary, /* generate_summary */
1412 NULL, /* write_summary */
1413 NULL, /* read_summary */
1414 NULL, /* function_read_summary */
1415 0, /* TODOs */
1416 NULL, /* function_transform */
1417 NULL /* variable_transform */
1420 #include "gt-ipa-reference.h"