1 /* Alias analysis for trees.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
34 #include "langhooks.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "tree-gimple.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
47 /* Structure to map a variable to its alias set and keep track of the
48 virtual operands that will be needed to represent it. */
51 /* Variable and its alias set. */
55 /* Total number of virtual operands that will be needed to represent
56 all the aliases of VAR. */
57 long total_alias_vops;
59 /* Nonzero if the aliases for this memory tag have been grouped
60 already. Used in group_aliases. */
61 unsigned int grouped_p : 1;
63 /* Set of variables aliased with VAR. This is the exact same
64 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
65 bitmap form to speed up alias grouping. */
70 /* Alias information used by compute_may_aliases and its helpers. */
73 /* SSA names visited while collecting points-to information. If bit I
74 is set, it means that SSA variable with version I has already been
76 sbitmap ssa_names_visited;
78 /* Array of SSA_NAME pointers processed by the points-to collector. */
79 varray_type processed_ptrs;
81 /* Variables whose address is still needed. */
82 bitmap addresses_needed;
84 /* ADDRESSABLE_VARS contains all the global variables and locals that
85 have had their address taken. */
86 struct alias_map_d **addressable_vars;
87 size_t num_addressable_vars;
89 /* POINTERS contains all the _DECL pointers with unique memory tags
90 that have been referenced in the program. */
91 struct alias_map_d **pointers;
94 /* Number of function calls found in the program. */
95 size_t num_calls_found;
97 /* Array of counters to keep track of how many times each pointer has
98 been dereferenced in the program. This is used by the alias grouping
99 heuristic in compute_flow_insensitive_aliasing. */
100 varray_type num_references;
102 /* Total number of virtual operands that will be needed to represent
103 all the aliases of all the pointers found in the program. */
104 long total_alias_vops;
106 /* Variables that have been written to. */
109 /* Pointers that have been used in an indirect store operation. */
110 bitmap dereferenced_ptrs_store;
112 /* Pointers that have been used in an indirect load operation. */
113 bitmap dereferenced_ptrs_load;
117 /* Counters used to display statistics on alias analysis. */
120 unsigned int alias_queries;
121 unsigned int alias_mayalias;
122 unsigned int alias_noalias;
123 unsigned int simple_queries;
124 unsigned int simple_resolved;
125 unsigned int tbaa_queries;
126 unsigned int tbaa_resolved;
130 /* Local variables. */
131 static struct alias_stats_d alias_stats;
133 /* Local functions. */
134 static void compute_flow_insensitive_aliasing (struct alias_info *);
135 static void dump_alias_stats (FILE *);
136 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT);
137 static tree create_memory_tag (tree type, bool is_type_tag);
138 static tree get_tmt_for (tree, struct alias_info *);
139 static tree get_nmt_for (tree);
140 static void add_may_alias (tree, tree);
141 static void replace_may_alias (tree, size_t, tree);
142 static struct alias_info *init_alias_info (void);
143 static void delete_alias_info (struct alias_info *);
144 static void compute_points_to_and_addr_escape (struct alias_info *);
145 static void compute_flow_sensitive_aliasing (struct alias_info *);
146 static void setup_pointers_and_addressables (struct alias_info *);
147 static bool collect_points_to_info_r (tree, tree, void *);
148 static bool is_escape_site (tree, size_t *);
149 static void add_pointed_to_var (struct alias_info *, tree, tree);
150 static void create_global_var (void);
151 static void collect_points_to_info_for (struct alias_info *, tree);
152 static bool ptr_is_dereferenced_by (tree, tree, bool *);
153 static void maybe_create_global_var (struct alias_info *ai);
154 static void group_aliases (struct alias_info *);
155 static void set_pt_anything (tree ptr);
156 static void set_pt_malloc (tree ptr);
158 /* Global declarations. */
160 /* Call clobbered variables in the function. If bit I is set, then
161 REFERENCED_VARS (I) is call-clobbered. */
162 bitmap call_clobbered_vars;
164 /* Addressable variables in the function. If bit I is set, then
165 REFERENCED_VARS (I) has had its address taken. Note that
166 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
167 addressable variable is not necessarily call-clobbered (e.g., a
168 local addressable whose address does not escape) and not all
169 call-clobbered variables are addressable (e.g., a local static
171 bitmap addressable_vars;
173 /* When the program has too many call-clobbered variables and call-sites,
174 this variable is used to represent the clobbering effects of function
175 calls. In these cases, all the call clobbered variables in the program
176 are forced to alias this variable. This reduces compile times by not
177 having to keep track of too many V_MAY_DEF expressions at call sites. */
181 /* Compute may-alias information for every variable referenced in function
184 Alias analysis proceeds in 3 main phases:
186 1- Points-to and escape analysis.
188 This phase walks the use-def chains in the SSA web looking for three
191 * Assignments of the form P_i = &VAR
192 * Assignments of the form P_i = malloc()
193 * Pointers and ADDR_EXPR that escape the current function.
195 The concept of 'escaping' is the same one used in the Java world. When
196 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
197 outside of the current function. So, assignment to global variables,
198 function arguments and returning a pointer are all escape sites, as are
199 conversions between pointers and integers.
201 This is where we are currently limited. Since not everything is renamed
202 into SSA, we lose track of escape properties when a pointer is stashed
203 inside a field in a structure, for instance. In those cases, we are
204 assuming that the pointer does escape.
206 We use escape analysis to determine whether a variable is
207 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
208 is call-clobbered. If a pointer P_i escapes, then all the variables
209 pointed-to by P_i (and its memory tag) also escape.
211 2- Compute flow-sensitive aliases
213 We have two classes of memory tags. Memory tags associated with the
214 pointed-to data type of the pointers in the program. These tags are
215 called "type memory tag" (TMT). The other class are those associated
216 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
217 when adding operands for an INDIRECT_REF *P_i, we will first check
218 whether P_i has a name tag, if it does we use it, because that will have
219 more precise aliasing information. Otherwise, we use the standard type
222 In this phase, we go through all the pointers we found in points-to
223 analysis and create alias sets for the name memory tags associated with
224 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
225 it points to and its tag.
228 3- Compute flow-insensitive aliases
230 This pass will compare the alias set of every type memory tag and every
231 addressable variable found in the program. Given a type memory tag TMT
232 and an addressable variable V. If the alias sets of TMT and V conflict
233 (as computed by may_alias_p), then V is marked as an alias tag and added
234 to the alias set of TMT.
236 For instance, consider the following function:
252 After aliasing analysis has finished, the type memory tag for pointer
253 'p' will have two aliases, namely variables 'a' and 'b'. Every time
254 pointer 'p' is dereferenced, we want to mark the operation as a
255 potential reference to 'a' and 'b'.
265 # p_1 = PHI <p_4(1), p_6(2)>;
267 # a_7 = V_MAY_DEF <a_3>;
268 # b_8 = V_MAY_DEF <b_5>;
271 # a_9 = V_MAY_DEF <a_7>
280 In certain cases, the list of may aliases for a pointer may grow too
281 large. This may cause an explosion in the number of virtual operands
282 inserted in the code. Resulting in increased memory consumption and
285 When the number of virtual operands needed to represent aliased
286 loads and stores grows too large (configurable with @option{--param
287 max-aliased-vops}), alias sets are grouped to avoid severe
288 compile-time slow downs and memory consumption. See group_aliases. */
291 compute_may_aliases (void)
293 struct alias_info *ai;
295 memset (&alias_stats, 0, sizeof (alias_stats));
297 /* Initialize aliasing information. */
298 ai = init_alias_info ();
300 /* For each pointer P_i, determine the sets of variables that P_i may
301 point-to. For every addressable variable V, determine whether the
302 address of V escapes the current function, making V call-clobbered
303 (i.e., whether &V is stored in a global variable or if its passed as a
304 function call argument). */
305 compute_points_to_and_addr_escape (ai);
307 /* Collect all pointers and addressable variables, compute alias sets,
308 create memory tags for pointers and promote variables whose address is
309 not needed anymore. */
310 setup_pointers_and_addressables (ai);
312 /* Compute flow-sensitive, points-to based aliasing for all the name
313 memory tags. Note that this pass needs to be done before flow
314 insensitive analysis because it uses the points-to information
315 gathered before to mark call-clobbered type tags. */
316 compute_flow_sensitive_aliasing (ai);
318 /* Compute type-based flow-insensitive aliasing for all the type
320 compute_flow_insensitive_aliasing (ai);
322 /* If the program has too many call-clobbered variables and/or function
323 calls, create .GLOBAL_VAR and use it to model call-clobbering
324 semantics at call sites. This reduces the number of virtual operands
325 considerably, improving compile times at the expense of lost
326 aliasing precision. */
327 maybe_create_global_var (ai);
329 /* Debugging dumps. */
332 dump_referenced_vars (dump_file);
333 if (dump_flags & TDF_STATS)
334 dump_alias_stats (dump_file);
335 dump_points_to_info (dump_file);
336 dump_alias_info (dump_file);
339 /* Deallocate memory used by aliasing data structures. */
340 delete_alias_info (ai);
343 struct tree_opt_pass pass_may_alias =
347 compute_may_aliases, /* execute */
350 0, /* static_pass_number */
351 TV_TREE_MAY_ALIAS, /* tv_id */
352 PROP_cfg | PROP_ssa, /* properties_required */
353 PROP_alias, /* properties_provided */
354 0, /* properties_destroyed */
355 0, /* todo_flags_start */
356 TODO_dump_func | TODO_rename_vars
357 | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
361 /* Count the number of calls in the function and conditionally
362 create GLOBAL_VAR. This is performed before translation
363 into SSA (and thus before alias analysis) to avoid compile time
364 and memory utilization explosions in functions with many
365 of calls and call clobbered variables. */
368 count_calls_and_maybe_create_global_var (void)
370 struct alias_info ai;
374 memset (&ai, 0, sizeof (struct alias_info));
376 /* First count the number of calls in the IL. */
379 block_stmt_iterator si;
381 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
383 tree stmt = bsi_stmt (si);
385 if (get_call_expr_in (stmt) != NULL_TREE)
386 ai.num_calls_found++;
390 /* If there are no call clobbered variables, then maybe_create_global_var
391 will always create a GLOBAL_VAR. At this point we do not want that
392 behavior. So we turn on one bit in CALL_CLOBBERED_VARs, call
393 maybe_create_global_var, then reset the bit to its original state. */
394 temp = bitmap_bit_p (call_clobbered_vars, 0);
395 bitmap_set_bit (call_clobbered_vars, 0);
396 maybe_create_global_var (&ai);
398 bitmap_clear_bit (call_clobbered_vars, 0);
401 struct tree_opt_pass pass_maybe_create_global_var =
403 "maybe_create_global_var", /* name */
405 count_calls_and_maybe_create_global_var, /* execute */
408 0, /* static_pass_number */
409 TV_TREE_MAY_ALIAS, /* tv_id */
410 PROP_cfg, /* properties_required */
411 0, /* properties_provided */
412 0, /* properties_destroyed */
413 0, /* todo_flags_start */
414 0, /* todo_flags_finish */
418 /* Initialize the data structures used for alias analysis. */
420 static struct alias_info *
421 init_alias_info (void)
423 struct alias_info *ai;
424 static bool aliases_computed_p = false;
426 ai = xcalloc (1, sizeof (struct alias_info));
427 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
428 sbitmap_zero (ai->ssa_names_visited);
429 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
430 ai->addresses_needed = BITMAP_XMALLOC ();
431 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
432 ai->written_vars = BITMAP_XMALLOC ();
433 ai->dereferenced_ptrs_store = BITMAP_XMALLOC ();
434 ai->dereferenced_ptrs_load = BITMAP_XMALLOC ();
436 /* If aliases have been computed before, clear existing information. */
437 if (aliases_computed_p)
442 /* Clear the call-clobbered set. We are going to re-discover
443 call-clobbered variables. */
444 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
446 tree var = referenced_var (i);
448 /* Variables that are intrinsically call-clobbered (globals,
449 local statics, etc) will not be marked by the aliasing
450 code, so we can't remove them from CALL_CLOBBERED_VARS. */
451 if (!is_call_clobbered (var))
452 bitmap_clear_bit (call_clobbered_vars, var_ann (var)->uid);
455 /* Similarly, clear the set of addressable variables. In this
456 case, we can just clear the set because addressability is
457 only computed here. */
458 bitmap_clear (addressable_vars);
460 /* Clear flow-insensitive alias information from each symbol. */
461 for (i = 0; i < num_referenced_vars; i++)
463 var_ann_t ann = var_ann (referenced_var (i));
464 ann->is_alias_tag = 0;
465 ann->may_aliases = NULL;
468 /* Clear flow-sensitive points-to information from each SSA name. */
469 for (i = 1; i < num_ssa_names; i++)
471 tree name = ssa_name (i);
473 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
476 if (SSA_NAME_PTR_INFO (name))
478 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
480 /* Clear all the flags but keep the name tag to
481 avoid creating new temporaries unnecessarily. If
482 this pointer is found to point to a subset or
483 superset of its former points-to set, then a new
484 tag will need to be created in create_name_tags. */
487 pi->value_escapes_p = 0;
488 pi->is_dereferenced = 0;
490 bitmap_clear (pi->pt_vars);
495 /* Next time, we will need to reset alias information. */
496 aliases_computed_p = true;
502 /* Deallocate memory used by alias analysis. */
505 delete_alias_info (struct alias_info *ai)
509 sbitmap_free (ai->ssa_names_visited);
510 ai->processed_ptrs = NULL;
511 BITMAP_XFREE (ai->addresses_needed);
513 for (i = 0; i < ai->num_addressable_vars; i++)
515 sbitmap_free (ai->addressable_vars[i]->may_aliases);
516 free (ai->addressable_vars[i]);
518 free (ai->addressable_vars);
520 for (i = 0; i < ai->num_pointers; i++)
522 sbitmap_free (ai->pointers[i]->may_aliases);
523 free (ai->pointers[i]);
527 ai->num_references = NULL;
528 BITMAP_XFREE (ai->written_vars);
529 BITMAP_XFREE (ai->dereferenced_ptrs_store);
530 BITMAP_XFREE (ai->dereferenced_ptrs_load);
536 /* Walk use-def chains for pointer PTR to determine what variables is PTR
540 collect_points_to_info_for (struct alias_info *ai, tree ptr)
542 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
544 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
546 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
547 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
548 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
553 /* Helper for ptr_is_dereferenced_by. Called by walk_tree to look for
554 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
557 find_ptr_dereference (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
559 tree ptr = (tree) data;
561 if (INDIRECT_REF_P (*tp)
562 && TREE_OPERAND (*tp, 0) == ptr)
569 /* Return true if STMT contains (ALIGN/MISALIGNED_)INDIRECT_REF <PTR>.
570 *IS_STORE is set to 'true' if the dereference is on the LHS of an
574 ptr_is_dereferenced_by (tree ptr, tree stmt, bool *is_store)
578 if (TREE_CODE (stmt) == MODIFY_EXPR
579 || (TREE_CODE (stmt) == RETURN_EXPR
580 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR))
584 e = (TREE_CODE (stmt) == RETURN_EXPR) ? TREE_OPERAND (stmt, 0) : stmt;
585 lhs = TREE_OPERAND (e, 0);
586 rhs = TREE_OPERAND (e, 1);
589 && walk_tree (&lhs, find_ptr_dereference, ptr, NULL))
594 else if (EXPR_P (rhs)
595 && walk_tree (&rhs, find_ptr_dereference, ptr, NULL))
600 else if (TREE_CODE (stmt) == ASM_EXPR)
602 if (walk_tree (&ASM_OUTPUTS (stmt), find_ptr_dereference, ptr, NULL)
603 || walk_tree (&ASM_CLOBBERS (stmt), find_ptr_dereference, ptr, NULL))
608 else if (walk_tree (&ASM_INPUTS (stmt), find_ptr_dereference, ptr, NULL))
618 /* Traverse use-def links for all the pointers in the program to collect
619 address escape and points-to information.
621 This is loosely based on the same idea described in R. Hasti and S.
622 Horwitz, ``Using static single assignment form to improve
623 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
624 Programming Language Design and Implementation, pp. 97-105, 1998. */
627 compute_points_to_and_addr_escape (struct alias_info *ai)
634 timevar_push (TV_TREE_PTA);
638 bb_ann_t block_ann = bb_ann (bb);
639 block_stmt_iterator si;
641 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
644 tree stmt = bsi_stmt (si);
645 bool stmt_escapes_p = is_escape_site (stmt, &ai->num_calls_found);
648 /* Mark all the variables whose address are taken by the
649 statement. Note that this will miss all the addresses taken
650 in PHI nodes (those are discovered while following the use-def
652 get_stmt_operands (stmt);
653 addr_taken = addresses_taken (stmt);
655 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
657 tree var = referenced_var (i);
658 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
660 mark_call_clobbered (var);
664 block_ann->has_escape_site = 1;
666 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
668 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
669 struct ptr_info_def *pi;
672 /* If the operand's variable may be aliased, keep track
673 of how many times we've referenced it. This is used
674 for alias grouping in compute_flow_sensitive_aliasing.
675 Note that we don't need to grow AI->NUM_REFERENCES
676 because we are processing regular variables, not
677 memory tags (the array's initial size is set to
678 NUM_REFERENCED_VARS). */
679 if (may_be_aliased (SSA_NAME_VAR (op)))
680 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
682 if (!POINTER_TYPE_P (TREE_TYPE (op)))
685 collect_points_to_info_for (ai, op);
687 pi = SSA_NAME_PTR_INFO (op);
688 if (ptr_is_dereferenced_by (op, stmt, &is_store))
690 /* Mark OP as dereferenced. In a subsequent pass,
691 dereferenced pointers that point to a set of
692 variables will be assigned a name tag to alias
693 all the variables OP points to. */
694 pi->is_dereferenced = 1;
696 /* Keep track of how many time we've dereferenced each
697 pointer. Again, we don't need to grow
698 AI->NUM_REFERENCES because we're processing
699 existing program variables. */
700 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
702 /* If this is a store operation, mark OP as being
703 dereferenced to store, otherwise mark it as being
704 dereferenced to load. */
706 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
708 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
710 else if (stmt_escapes_p)
712 /* Note that even if STMT is an escape point, pointer OP
713 will not escape if it is being dereferenced. That's
714 why we only check for escape points if OP is not
715 dereferenced by STMT. */
716 pi->value_escapes_p = 1;
718 /* If the statement makes a function call, assume
719 that pointer OP will be dereferenced in a store
720 operation inside the called function. */
721 if (get_call_expr_in (stmt))
723 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
724 pi->is_dereferenced = 1;
729 /* Update reference counter for definitions to any
730 potentially aliased variable. This is used in the alias
731 grouping heuristics. */
732 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
734 tree var = SSA_NAME_VAR (op);
735 var_ann_t ann = var_ann (var);
736 bitmap_set_bit (ai->written_vars, ann->uid);
737 if (may_be_aliased (var))
738 (VARRAY_UINT (ai->num_references, ann->uid))++;
740 if (POINTER_TYPE_P (TREE_TYPE (op)))
741 collect_points_to_info_for (ai, op);
744 /* Mark variables in V_MAY_DEF operands as being written to. */
745 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
747 tree var = SSA_NAME_VAR (op);
748 var_ann_t ann = var_ann (var);
749 bitmap_set_bit (ai->written_vars, ann->uid);
752 /* After promoting variables and computing aliasing we will
753 need to re-scan most statements. FIXME: Try to minimize the
754 number of statements re-scanned. It's not really necessary to
755 re-scan *all* statements. */
760 timevar_pop (TV_TREE_PTA);
764 /* Create name tags for all the pointers that have been dereferenced.
765 We only create a name tag for a pointer P if P is found to point to
766 a set of variables (so that we can alias them to *P) or if it is
767 the result of a call to malloc (which means that P cannot point to
768 anything else nor alias any other variable).
770 If two pointers P and Q point to the same set of variables, they
771 are assigned the same name tag. */
774 create_name_tags (struct alias_info *ai)
778 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
780 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
781 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
783 if (pi->pt_anything || !pi->is_dereferenced)
785 /* No name tags for pointers that have not been
786 dereferenced or point to an arbitrary location. */
787 pi->name_mem_tag = NULL_TREE;
791 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
794 tree old_name_tag = pi->name_mem_tag;
796 /* If PTR points to a set of variables, check if we don't
797 have another pointer Q with the same points-to set before
798 creating a tag. If so, use Q's tag instead of creating a
801 This is important for not creating unnecessary symbols
802 and also for copy propagation. If we ever need to
803 propagate PTR into Q or vice-versa, we would run into
804 problems if they both had different name tags because
805 they would have different SSA version numbers (which
806 would force us to take the name tags in and out of SSA). */
807 for (j = 0; j < i; j++)
809 tree q = VARRAY_TREE (ai->processed_ptrs, j);
810 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
815 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
817 pi->name_mem_tag = qi->name_mem_tag;
822 /* If we didn't find a pointer with the same points-to set
823 as PTR, create a new name tag if needed. */
824 if (pi->name_mem_tag == NULL_TREE)
825 pi->name_mem_tag = get_nmt_for (ptr);
827 /* If the new name tag computed for PTR is different than
828 the old name tag that it used to have, then the old tag
829 needs to be removed from the IL, so we mark it for
831 if (old_name_tag && old_name_tag != pi->name_mem_tag)
832 bitmap_set_bit (vars_to_rename, var_ann (old_name_tag)->uid);
834 else if (pi->pt_malloc)
836 /* Otherwise, create a unique name tag for this pointer. */
837 pi->name_mem_tag = get_nmt_for (ptr);
841 /* Only pointers that may point to malloc or other variables
842 may receive a name tag. If the pointer does not point to
843 a known spot, we should use type tags. */
844 set_pt_anything (ptr);
848 TREE_THIS_VOLATILE (pi->name_mem_tag)
849 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
851 /* Mark the new name tag for renaming. */
852 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
858 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
859 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
860 name tag and the variables it points-to are call-clobbered. Finally, if
861 P_i escapes and we could not determine where it points to, then all the
862 variables in the same alias set as *P_i are marked call-clobbered. This
863 is necessary because we must assume that P_i may take the address of any
864 variable in the same alias set. */
867 compute_flow_sensitive_aliasing (struct alias_info *ai)
871 create_name_tags (ai);
873 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
876 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
877 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
878 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
881 if (pi->value_escapes_p || pi->pt_anything)
883 /* If PTR escapes or may point to anything, then its associated
884 memory tags and pointed-to variables are call-clobbered. */
885 if (pi->name_mem_tag)
886 mark_call_clobbered (pi->name_mem_tag);
888 if (v_ann->type_mem_tag)
889 mark_call_clobbered (v_ann->type_mem_tag);
892 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
894 mark_call_clobbered (referenced_var (j));
898 /* Set up aliasing information for PTR's name memory tag (if it has
899 one). Note that only pointers that have been dereferenced will
900 have a name memory tag. */
901 if (pi->name_mem_tag && pi->pt_vars)
902 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
904 add_may_alias (pi->name_mem_tag, referenced_var (j));
907 /* If the name tag is call clobbered, so is the type tag
908 associated with the base VAR_DECL. */
910 && v_ann->type_mem_tag
911 && is_call_clobbered (pi->name_mem_tag))
912 mark_call_clobbered (v_ann->type_mem_tag);
917 /* Compute type-based alias sets. Traverse all the pointers and
918 addressable variables found in setup_pointers_and_addressables.
920 For every pointer P in AI->POINTERS and addressable variable V in
921 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
922 memory tag (TMT) if their alias sets conflict. V is then marked as
923 an alias tag so that the operand scanner knows that statements
924 containing V have aliased operands. */
927 compute_flow_insensitive_aliasing (struct alias_info *ai)
931 /* Initialize counter for the total number of virtual operands that
932 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
933 threshold set by --params max-alias-vops, we enable alias
935 ai->total_alias_vops = 0;
937 /* For every pointer P, determine which addressable variables may alias
938 with P's type memory tag. */
939 for (i = 0; i < ai->num_pointers; i++)
942 struct alias_map_d *p_map = ai->pointers[i];
943 tree tag = var_ann (p_map->var)->type_mem_tag;
944 var_ann_t tag_ann = var_ann (tag);
946 p_map->total_alias_vops = 0;
947 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
948 sbitmap_zero (p_map->may_aliases);
950 for (j = 0; j < ai->num_addressable_vars; j++)
952 struct alias_map_d *v_map;
955 bool tag_stored_p, var_stored_p;
957 v_map = ai->addressable_vars[j];
959 v_ann = var_ann (var);
961 /* Skip memory tags and variables that have never been
962 written to. We also need to check if the variables are
963 call-clobbered because they may be overwritten by
966 Note this is effectively random accessing elements in
967 the sparse bitset, which can be highly inefficient.
968 So we first check the call_clobbered status of the
969 tag and variable before querying the bitmap. */
970 tag_stored_p = is_call_clobbered (tag)
971 || bitmap_bit_p (ai->written_vars, tag_ann->uid);
972 var_stored_p = is_call_clobbered (var)
973 || bitmap_bit_p (ai->written_vars, v_ann->uid);
974 if (!tag_stored_p && !var_stored_p)
977 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
979 size_t num_tag_refs, num_var_refs;
981 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
982 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
984 /* Add VAR to TAG's may-aliases set. */
985 add_may_alias (tag, var);
987 /* Update the total number of virtual operands due to
988 aliasing. Since we are adding one more alias to TAG's
989 may-aliases set, the total number of virtual operands due
990 to aliasing will be increased by the number of references
991 made to VAR and TAG (every reference to TAG will also
992 count as a reference to VAR). */
993 ai->total_alias_vops += (num_var_refs + num_tag_refs);
994 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
996 /* Update the bitmap used to represent TAG's alias set
997 in case we need to group aliases. */
998 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
1003 /* Since this analysis is based exclusively on symbols, it fails to
1004 handle cases where two pointers P and Q have different memory
1005 tags with conflicting alias set numbers but no aliased symbols in
1008 For example, suppose that we have two memory tags TMT.1 and TMT.2
1011 may-aliases (TMT.1) = { a }
1012 may-aliases (TMT.2) = { b }
1014 and the alias set number of TMT.1 conflicts with that of TMT.2.
1015 Since they don't have symbols in common, loads and stores from
1016 TMT.1 and TMT.2 will seem independent of each other, which will
1017 lead to the optimizers making invalid transformations (see
1018 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1020 To avoid this problem, we do a final traversal of AI->POINTERS
1021 looking for pairs of pointers that have no aliased symbols in
1022 common and yet have conflicting alias set numbers. */
1023 for (i = 0; i < ai->num_pointers; i++)
1026 struct alias_map_d *p_map1 = ai->pointers[i];
1027 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1028 sbitmap may_aliases1 = p_map1->may_aliases;
1030 for (j = i + 1; j < ai->num_pointers; j++)
1032 struct alias_map_d *p_map2 = ai->pointers[j];
1033 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1034 sbitmap may_aliases2 = p_map2->may_aliases;
1036 /* If the pointers may not point to each other, do nothing. */
1037 if (!may_alias_p (p_map1->var, p_map1->set, p_map2->var, p_map2->set))
1040 /* The two pointers may alias each other. If they already have
1041 symbols in common, do nothing. */
1042 if (sbitmap_any_common_bits (may_aliases1, may_aliases2))
1045 if (sbitmap_first_set_bit (may_aliases2) >= 0)
1049 /* Add all the aliases for TAG2 into TAG1's alias set.
1050 FIXME, update grouping heuristic counters. */
1051 EXECUTE_IF_SET_IN_SBITMAP (may_aliases2, 0, k,
1052 add_may_alias (tag1, referenced_var (k)));
1053 sbitmap_a_or_b (may_aliases1, may_aliases1, may_aliases2);
1057 /* Since TAG2 does not have any aliases of its own, add
1058 TAG2 itself to the alias set of TAG1. */
1059 add_may_alias (tag1, tag2);
1065 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
1066 get_name (current_function_decl),
1067 ai->total_alias_vops);
1069 /* Determine if we need to enable alias grouping. */
1070 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1075 /* Comparison function for qsort used in group_aliases. */
1078 total_alias_vops_cmp (const void *p, const void *q)
1080 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1081 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1082 long n1 = (*p1)->total_alias_vops;
1083 long n2 = (*p2)->total_alias_vops;
1085 /* We want to sort in descending order. */
1086 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1089 /* Group all the aliases for TAG to make TAG represent all the
1090 variables in its alias set. Update the total number
1091 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1092 function will make TAG be the unique alias tag for all the
1093 variables in its may-aliases. So, given:
1095 may-aliases(TAG) = { V1, V2, V3 }
1097 This function will group the variables into:
1099 may-aliases(V1) = { TAG }
1100 may-aliases(V2) = { TAG }
1101 may-aliases(V2) = { TAG } */
1104 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
1107 var_ann_t tag_ann = var_ann (tag);
1108 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1110 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i,
1112 tree var = referenced_var (i);
1113 var_ann_t ann = var_ann (var);
1115 /* Make TAG the unique alias of VAR. */
1116 ann->is_alias_tag = 0;
1117 ann->may_aliases = NULL;
1119 /* Note that VAR and TAG may be the same if the function has no
1120 addressable variables (see the discussion at the end of
1121 setup_pointers_and_addressables). */
1123 add_may_alias (var, tag);
1125 /* Reduce total number of virtual operands contributed
1126 by TAG on behalf of VAR. Notice that the references to VAR
1127 itself won't be removed. We will merely replace them with
1128 references to TAG. */
1129 ai->total_alias_vops -= num_tag_refs;
1132 /* We have reduced the number of virtual operands that TAG makes on
1133 behalf of all the variables formerly aliased with it. However,
1134 we have also "removed" all the virtual operands for TAG itself,
1135 so we add them back. */
1136 ai->total_alias_vops += num_tag_refs;
1138 /* TAG no longer has any aliases. */
1139 tag_ann->may_aliases = NULL;
1143 /* Group may-aliases sets to reduce the number of virtual operands due
1146 1- Sort the list of pointers in decreasing number of contributed
1149 2- Take the first entry in AI->POINTERS and revert the role of
1150 the memory tag and its aliases. Usually, whenever an aliased
1151 variable Vi is found to alias with a memory tag T, we add Vi
1152 to the may-aliases set for T. Meaning that after alias
1153 analysis, we will have:
1155 may-aliases(T) = { V1, V2, V3, ..., Vn }
1157 This means that every statement that references T, will get 'n'
1158 virtual operands for each of the Vi tags. But, when alias
1159 grouping is enabled, we make T an alias tag and add it to the
1160 alias set of all the Vi variables:
1162 may-aliases(V1) = { T }
1163 may-aliases(V2) = { T }
1165 may-aliases(Vn) = { T }
1167 This has two effects: (a) statements referencing T will only get
1168 a single virtual operand, and, (b) all the variables Vi will now
1169 appear to alias each other. So, we lose alias precision to
1170 improve compile time. But, in theory, a program with such a high
1171 level of aliasing should not be very optimizable in the first
1174 3- Since variables may be in the alias set of more than one
1175 memory tag, the grouping done in step (2) needs to be extended
1176 to all the memory tags that have a non-empty intersection with
1177 the may-aliases set of tag T. For instance, if we originally
1178 had these may-aliases sets:
1180 may-aliases(T) = { V1, V2, V3 }
1181 may-aliases(R) = { V2, V4 }
1183 In step (2) we would have reverted the aliases for T as:
1185 may-aliases(V1) = { T }
1186 may-aliases(V2) = { T }
1187 may-aliases(V3) = { T }
1189 But note that now V2 is no longer aliased with R. We could
1190 add R to may-aliases(V2), but we are in the process of
1191 grouping aliases to reduce virtual operands so what we do is
1192 add V4 to the grouping to obtain:
1194 may-aliases(V1) = { T }
1195 may-aliases(V2) = { T }
1196 may-aliases(V3) = { T }
1197 may-aliases(V4) = { T }
1199 4- If the total number of virtual operands due to aliasing is
1200 still above the threshold set by max-alias-vops, go back to (2). */
1203 group_aliases (struct alias_info *ai)
1207 /* Sort the POINTERS array in descending order of contributed
1208 virtual operands. */
1209 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1210 total_alias_vops_cmp);
1212 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1213 and the tag's may-aliases set. */
1214 for (i = 0; i < ai->num_pointers; i++)
1217 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1218 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1220 /* Skip tags that have been grouped already. */
1221 if (ai->pointers[i]->grouped_p)
1224 /* See if TAG1 had any aliases in common with other type tags.
1225 If we find a TAG2 with common aliases with TAG1, add TAG2's
1226 aliases into TAG1. */
1227 for (j = i + 1; j < ai->num_pointers; j++)
1229 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1231 if (sbitmap_any_common_bits (tag1_aliases, tag2_aliases))
1233 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1235 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1237 /* TAG2 does not need its aliases anymore. */
1238 sbitmap_zero (tag2_aliases);
1239 var_ann (tag2)->may_aliases = NULL;
1241 /* TAG1 is the unique alias of TAG2. */
1242 add_may_alias (tag2, tag1);
1244 ai->pointers[j]->grouped_p = true;
1248 /* Now group all the aliases we collected into TAG1. */
1249 group_aliases_into (tag1, tag1_aliases, ai);
1251 /* If we've reduced total number of virtual operands below the
1253 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1257 /* Finally, all the variables that have been grouped cannot be in
1258 the may-alias set of name memory tags. Suppose that we have
1259 grouped the aliases in this code so that may-aliases(a) = TMT.20
1263 # a_9 = V_MAY_DEF <a_8>
1265 ... Several modifications to TMT.20 ...
1269 Since p_5 points to 'a', the optimizers will try to propagate 0
1270 into p_5->field, but that is wrong because there have been
1271 modifications to 'TMT.20' in between. To prevent this we have to
1272 replace 'a' with 'TMT.20' in the name tag of p_5. */
1273 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1276 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1277 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1278 varray_type aliases;
1280 if (name_tag == NULL_TREE)
1283 aliases = var_ann (name_tag)->may_aliases;
1284 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1286 tree alias = VARRAY_TREE (aliases, j);
1287 var_ann_t ann = var_ann (alias);
1289 if (ann->mem_tag_kind == NOT_A_TAG && ann->may_aliases)
1293 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1295 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1296 replace_may_alias (name_tag, j, new_alias);
1303 "%s: Total number of aliased vops after grouping: %ld%s\n",
1304 get_name (current_function_decl),
1305 ai->total_alias_vops,
1306 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1310 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1313 create_alias_map_for (tree var, struct alias_info *ai)
1315 struct alias_map_d *alias_map;
1316 alias_map = xcalloc (1, sizeof (*alias_map));
1317 alias_map->var = var;
1318 alias_map->set = get_alias_set (var);
1319 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1323 /* Create memory tags for all the dereferenced pointers and build the
1324 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1325 sets. Based on the address escape and points-to information collected
1326 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1327 variables whose address is not needed anymore. */
1330 setup_pointers_and_addressables (struct alias_info *ai)
1332 size_t i, n_vars, num_addressable_vars, num_pointers;
1334 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1335 num_addressable_vars = num_pointers = 0;
1336 for (i = 0; i < num_referenced_vars; i++)
1338 tree var = referenced_var (i);
1340 if (may_be_aliased (var))
1341 num_addressable_vars++;
1343 if (POINTER_TYPE_P (TREE_TYPE (var)))
1345 /* Since we don't keep track of volatile variables, assume that
1346 these pointers are used in indirect store operations. */
1347 if (TREE_THIS_VOLATILE (var))
1348 bitmap_set_bit (ai->dereferenced_ptrs_store, var_ann (var)->uid);
1354 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1355 always going to be slightly bigger than we actually need them
1356 because some TREE_ADDRESSABLE variables will be marked
1357 non-addressable below and only pointers with unique type tags are
1358 going to be added to POINTERS. */
1359 ai->addressable_vars = xcalloc (num_addressable_vars,
1360 sizeof (struct alias_map_d *));
1361 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1362 ai->num_addressable_vars = 0;
1363 ai->num_pointers = 0;
1365 /* Since we will be creating type memory tags within this loop, cache the
1366 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1368 n_vars = num_referenced_vars;
1370 for (i = 0; i < n_vars; i++)
1372 tree var = referenced_var (i);
1373 var_ann_t v_ann = var_ann (var);
1375 /* Name memory tags already have flow-sensitive aliasing
1376 information, so they need not be processed by
1377 compute_flow_insensitive_aliasing. Similarly, type memory
1378 tags are already accounted for when we process their
1379 associated pointer. */
1380 if (v_ann->mem_tag_kind != NOT_A_TAG)
1383 /* Remove the ADDRESSABLE flag from every addressable variable whose
1384 address is not needed anymore. This is caused by the propagation
1385 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1386 removal of dead pointer assignments done by the early scalar
1388 if (TREE_ADDRESSABLE (var))
1390 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1391 && v_ann->mem_tag_kind == NOT_A_TAG
1392 && TREE_CODE (var) != RESULT_DECL
1393 && !is_global_var (var))
1395 /* The address of VAR is not needed, remove the
1396 addressable bit, so that it can be optimized as a
1397 regular variable. */
1398 mark_non_addressable (var);
1400 /* Since VAR is now a regular GIMPLE register, we will need
1401 to rename VAR into SSA afterwards. */
1402 bitmap_set_bit (vars_to_rename, v_ann->uid);
1406 /* Add the variable to the set of addressables. Mostly
1407 used when scanning operands for ASM_EXPRs that
1408 clobber memory. In those cases, we need to clobber
1409 all call-clobbered variables and all addressables. */
1410 bitmap_set_bit (addressable_vars, v_ann->uid);
1414 /* Global variables and addressable locals may be aliased. Create an
1415 entry in ADDRESSABLE_VARS for VAR. */
1416 if (may_be_aliased (var))
1418 create_alias_map_for (var, ai);
1419 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1422 /* Add pointer variables that have been dereferenced to the POINTERS
1423 array and create a type memory tag for them. */
1424 if (POINTER_TYPE_P (TREE_TYPE (var)))
1426 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1427 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1432 /* If pointer VAR still doesn't have a memory tag
1433 associated with it, create it now or re-use an
1435 tag = get_tmt_for (var, ai);
1436 t_ann = var_ann (tag);
1438 /* The type tag will need to be renamed into SSA
1439 afterwards. Note that we cannot do this inside
1440 get_tmt_for because aliasing may run multiple times
1441 and we only create type tags the first time. */
1442 bitmap_set_bit (vars_to_rename, t_ann->uid);
1444 /* Associate the tag with pointer VAR. */
1445 v_ann->type_mem_tag = tag;
1447 /* If pointer VAR has been used in a store operation,
1448 then its memory tag must be marked as written-to. */
1449 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1450 bitmap_set_bit (ai->written_vars, t_ann->uid);
1452 /* If pointer VAR is a global variable or a PARM_DECL,
1453 then its memory tag should be considered a global
1455 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1456 mark_call_clobbered (tag);
1458 /* All the dereferences of pointer VAR count as
1459 references of TAG. Since TAG can be associated with
1460 several pointers, add the dereferences of VAR to the
1461 TAG. We may need to grow AI->NUM_REFERENCES because
1462 we have been adding name and type tags. */
1463 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1464 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1466 VARRAY_UINT (ai->num_references, t_ann->uid)
1467 += VARRAY_UINT (ai->num_references, v_ann->uid);
1471 /* The pointer has not been dereferenced. If it had a
1472 type memory tag, remove it and mark the old tag for
1473 renaming to remove it out of the IL. */
1474 var_ann_t ann = var_ann (var);
1475 tree tag = ann->type_mem_tag;
1478 bitmap_set_bit (vars_to_rename, var_ann (tag)->uid);
1479 ann->type_mem_tag = NULL_TREE;
1487 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1488 every call site, we need to emit V_MAY_DEF expressions to represent the
1489 clobbering effects of the call for variables whose address escapes the
1492 One approach is to group all call-clobbered variables into a single
1493 representative that is used as an alias of every call-clobbered variable
1494 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1495 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1497 The second approach is to emit a clobbering V_MAY_DEF for every
1498 call-clobbered variable at call sites. This is the preferred way in terms
1499 of optimization opportunities but it may create too many V_MAY_DEF operands
1500 if there are many call clobbered variables and function calls in the
1503 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1504 function calls found by the number of call-clobbered variables. If that
1505 product is beyond a certain threshold, as determined by the parameterized
1506 values shown below, we use .GLOBAL_VAR.
1508 FIXME. This heuristic should be improved. One idea is to use several
1509 .GLOBAL_VARs of different types instead of a single one. The thresholds
1510 have been derived from a typical bootstrap cycle, including all target
1511 libraries. Compile times were found increase by ~1% compared to using
1515 maybe_create_global_var (struct alias_info *ai)
1517 unsigned i, n_clobbered;
1520 /* No need to create it, if we have one already. */
1521 if (global_var == NULL_TREE)
1523 /* Count all the call-clobbered variables. */
1525 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1530 /* Create .GLOBAL_VAR if we have too many call-clobbered
1531 variables. We also create .GLOBAL_VAR when there no
1532 call-clobbered variables to prevent code motion
1533 transformations from re-arranging function calls that may
1534 have side effects. For instance,
1543 There are no call-clobbered variables in foo(), so it would
1544 be entirely possible for a pass to want to move the call to
1545 f() after the call to g(). If f() has side effects, that
1546 would be wrong. Creating .GLOBAL_VAR in this case will
1547 insert VDEFs for it and prevent such transformations. */
1548 if (n_clobbered == 0
1549 || ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD)
1550 create_global_var ();
1553 /* If the function has calls to clobbering functions and .GLOBAL_VAR has
1554 been created, make it an alias for all call-clobbered variables. */
1556 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1558 tree var = referenced_var (i);
1559 if (var != global_var)
1561 add_may_alias (var, global_var);
1562 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1568 /* Return TRUE if pointer PTR may point to variable VAR.
1570 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1571 This is needed because when checking for type conflicts we are
1572 interested in the alias set of the memory location pointed-to by
1573 PTR. The alias set of PTR itself is irrelevant.
1575 VAR_ALIAS_SET is the alias set for VAR. */
1578 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1579 tree var, HOST_WIDE_INT var_alias_set)
1582 var_ann_t v_ann, m_ann;
1584 alias_stats.alias_queries++;
1585 alias_stats.simple_queries++;
1587 /* By convention, a variable cannot alias itself. */
1588 mem = var_ann (ptr)->type_mem_tag;
1591 alias_stats.alias_noalias++;
1592 alias_stats.simple_resolved++;
1596 v_ann = var_ann (var);
1597 m_ann = var_ann (mem);
1599 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1601 alias_stats.tbaa_queries++;
1603 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1604 PTR can't possibly affect VAR. Note, that we are specifically testing
1605 for PTR's alias set here, not its pointed-to type. We also can't
1606 do this check with relaxed aliasing enabled. */
1607 if (POINTER_TYPE_P (TREE_TYPE (var))
1608 && var_alias_set != 0
1609 && mem_alias_set != 0)
1611 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1612 if (ptr_alias_set == var_alias_set)
1614 alias_stats.alias_noalias++;
1615 alias_stats.tbaa_resolved++;
1620 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1621 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1623 alias_stats.alias_noalias++;
1624 alias_stats.tbaa_resolved++;
1628 alias_stats.alias_mayalias++;
1633 /* Add ALIAS to the set of variables that may alias VAR. */
1636 add_may_alias (tree var, tree alias)
1639 var_ann_t v_ann = get_var_ann (var);
1640 var_ann_t a_ann = get_var_ann (alias);
1642 gcc_assert (var != alias);
1644 if (v_ann->may_aliases == NULL)
1645 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1647 /* Avoid adding duplicates. */
1648 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1649 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1652 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1653 FIXME, call-clobbering should only depend on whether an address
1654 escapes. It should be independent of aliasing. */
1655 if (is_call_clobbered (var))
1656 mark_call_clobbered (alias);
1658 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1659 else if (is_call_clobbered (alias))
1660 mark_call_clobbered (var);
1662 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1663 a_ann->is_alias_tag = 1;
1667 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1670 replace_may_alias (tree var, size_t i, tree new_alias)
1672 var_ann_t v_ann = var_ann (var);
1673 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1675 /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1676 FIXME, call-clobbering should only depend on whether an address
1677 escapes. It should be independent of aliasing. */
1678 if (is_call_clobbered (var))
1679 mark_call_clobbered (new_alias);
1681 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1682 else if (is_call_clobbered (new_alias))
1683 mark_call_clobbered (var);
1687 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1690 set_pt_anything (tree ptr)
1692 struct ptr_info_def *pi = get_ptr_info (ptr);
1694 pi->pt_anything = 1;
1697 /* The pointer used to have a name tag, but we now found it pointing
1698 to an arbitrary location. The name tag needs to be renamed and
1699 disassociated from PTR. */
1700 if (pi->name_mem_tag)
1702 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
1703 pi->name_mem_tag = NULL_TREE;
1708 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1711 set_pt_malloc (tree ptr)
1713 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1715 /* If the pointer has already been found to point to arbitrary
1716 memory locations, it is unsafe to mark it as pointing to malloc. */
1717 if (pi->pt_anything)
1724 /* Given two different pointers DEST and ORIG. Merge the points-to
1725 information in ORIG into DEST. AI is as in
1726 collect_points_to_info. */
1729 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1731 struct ptr_info_def *dest_pi, *orig_pi;
1733 gcc_assert (dest != orig);
1735 /* Make sure we have points-to information for ORIG. */
1736 collect_points_to_info_for (ai, orig);
1738 dest_pi = get_ptr_info (dest);
1739 orig_pi = SSA_NAME_PTR_INFO (orig);
1743 /* Notice that we never merge PT_MALLOC. This attribute is only
1744 true if the pointer is the result of a malloc() call.
1745 Otherwise, we can end up in this situation:
1751 P_j would be marked as PT_MALLOC, however we currently do not
1752 handle cases of more than one pointer pointing to the same
1755 FIXME: If the merging comes from an expression that preserves
1756 the PT_MALLOC attribute (copy assignment, address
1757 arithmetic), we ought to merge PT_MALLOC, but then both
1758 pointers would end up getting different name tags because
1759 create_name_tags is not smart enough to determine that the
1760 two come from the same malloc call. Copy propagation before
1761 aliasing should cure this. */
1762 gcc_assert (orig_pi != dest_pi);
1764 dest_pi->pt_malloc = 0;
1766 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1767 set_pt_anything (dest);
1769 if (!dest_pi->pt_anything
1771 && !bitmap_empty_p (orig_pi->pt_vars))
1773 if (dest_pi->pt_vars == NULL)
1775 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1776 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1779 bitmap_ior_into (dest_pi->pt_vars, orig_pi->pt_vars);
1783 set_pt_anything (dest);
1787 /* Add EXPR to the list of expressions pointed-to by PTR. */
1790 add_pointed_to_expr (struct alias_info *ai, tree ptr, tree expr)
1792 if (TREE_CODE (expr) == WITH_SIZE_EXPR)
1793 expr = TREE_OPERAND (expr, 0);
1797 if (TREE_CODE (expr) == CALL_EXPR
1798 && (call_expr_flags (expr) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1800 /* If EXPR is a malloc-like call, then the area pointed to PTR
1801 is guaranteed to not alias with anything else. */
1802 set_pt_malloc (ptr);
1804 else if (TREE_CODE (expr) == ADDR_EXPR)
1806 /* Found P_i = ADDR_EXPR */
1807 add_pointed_to_var (ai, ptr, expr);
1809 else if (TREE_CODE (expr) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (expr)))
1811 /* Found P_i = Q_j. */
1812 merge_pointed_to_info (ai, ptr, expr);
1814 else if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
1816 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1817 tree op0 = TREE_OPERAND (expr, 0);
1818 tree op1 = TREE_OPERAND (expr, 1);
1820 /* Both operands may be of pointer type. FIXME: Shouldn't
1821 we just expect PTR + OFFSET always? */
1822 if (POINTER_TYPE_P (TREE_TYPE (op0))
1823 && TREE_CODE (op0) != INTEGER_CST)
1825 if (TREE_CODE (op0) == SSA_NAME)
1826 merge_pointed_to_info (ai, ptr, op0);
1827 else if (TREE_CODE (op0) == ADDR_EXPR)
1828 add_pointed_to_var (ai, ptr, op0);
1830 set_pt_anything (ptr);
1833 if (POINTER_TYPE_P (TREE_TYPE (op1))
1834 && TREE_CODE (op1) != INTEGER_CST)
1836 if (TREE_CODE (op1) == SSA_NAME)
1837 merge_pointed_to_info (ai, ptr, op1);
1838 else if (TREE_CODE (op1) == ADDR_EXPR)
1839 add_pointed_to_var (ai, ptr, op1);
1841 set_pt_anything (ptr);
1844 /* Neither operand is a pointer? VAR can be pointing anywhere.
1845 FIXME: Shouldn't we abort here? If we get here, we found
1846 PTR = INT_CST + INT_CST, which should not be a valid pointer
1848 if (!(POINTER_TYPE_P (TREE_TYPE (op0))
1849 && TREE_CODE (op0) != INTEGER_CST)
1850 && !(POINTER_TYPE_P (TREE_TYPE (op1))
1851 && TREE_CODE (op1) != INTEGER_CST))
1852 set_pt_anything (ptr);
1856 /* If we can't recognize the expression, assume that PTR may
1858 set_pt_anything (ptr);
1863 /* If VALUE is of the form &DECL, add DECL to the set of variables
1864 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1865 PTR. AI is as in collect_points_to_info. */
1868 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1870 struct ptr_info_def *pi = get_ptr_info (ptr);
1874 gcc_assert (TREE_CODE (value) == ADDR_EXPR);
1876 pt_var = TREE_OPERAND (value, 0);
1877 if (REFERENCE_CLASS_P (pt_var))
1878 pt_var = get_base_address (pt_var);
1880 if (pt_var && SSA_VAR_P (pt_var))
1882 uid = var_ann (pt_var)->uid;
1883 bitmap_set_bit (ai->addresses_needed, uid);
1885 if (pi->pt_vars == NULL)
1886 pi->pt_vars = BITMAP_GGC_ALLOC ();
1887 bitmap_set_bit (pi->pt_vars, uid);
1889 /* If the variable is a global, mark the pointer as pointing to
1890 global memory (which will make its tag a global variable). */
1891 if (is_global_var (pt_var))
1892 pi->pt_global_mem = 1;
1897 /* Callback for walk_use_def_chains to gather points-to information from the
1900 VAR is an SSA variable or a GIMPLE expression.
1902 STMT is the statement that generates the SSA variable or, if STMT is a
1903 PHI_NODE, VAR is one of the PHI arguments.
1905 DATA is a pointer to a structure of type ALIAS_INFO. */
1908 collect_points_to_info_r (tree var, tree stmt, void *data)
1910 struct alias_info *ai = (struct alias_info *) data;
1912 if (dump_file && (dump_flags & TDF_DETAILS))
1914 fprintf (dump_file, "Visiting use-def links for ");
1915 print_generic_expr (dump_file, var, dump_flags);
1916 fprintf (dump_file, "\n");
1919 switch (TREE_CODE (stmt))
1922 if (TREE_CODE (TREE_OPERAND (stmt, 0)) != MODIFY_EXPR)
1924 stmt = TREE_OPERAND (stmt, 0);
1929 tree rhs = TREE_OPERAND (stmt, 1);
1931 add_pointed_to_expr (ai, var, rhs);
1936 /* Pointers defined by __asm__ statements can point anywhere. */
1937 set_pt_anything (var);
1941 if (IS_EMPTY_STMT (stmt))
1943 tree decl = SSA_NAME_VAR (var);
1945 if (TREE_CODE (decl) == PARM_DECL)
1946 add_pointed_to_expr (ai, var, decl);
1947 else if (DECL_INITIAL (decl))
1948 add_pointed_to_expr (ai, var, DECL_INITIAL (decl));
1950 add_pointed_to_expr (ai, var, decl);
1956 /* It STMT is a PHI node, then VAR is one of its arguments. The
1957 variable that we are analyzing is the LHS of the PHI node. */
1958 tree lhs = PHI_RESULT (stmt);
1960 switch (TREE_CODE (var))
1963 add_pointed_to_var (ai, lhs, var);
1967 /* Avoid unnecessary merges. */
1969 merge_pointed_to_info (ai, lhs, var);
1973 gcc_assert (is_gimple_min_invariant (var));
1974 add_pointed_to_expr (ai, lhs, var);
1988 /* Return true if STMT is an "escape" site from the current function. Escape
1989 sites those statements which might expose the address of a variable
1990 outside the current function. STMT is an escape site iff:
1992 1- STMT is a function call, or
1993 2- STMT is an __asm__ expression, or
1994 3- STMT is an assignment to a non-local variable, or
1995 4- STMT is a return statement.
1997 If NUM_CALLS_P is not NULL, the counter is incremented if STMT contains
2001 is_escape_site (tree stmt, size_t *num_calls_p)
2003 if (get_call_expr_in (stmt) != NULL_TREE)
2010 else if (TREE_CODE (stmt) == ASM_EXPR)
2012 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2014 tree lhs = TREE_OPERAND (stmt, 0);
2016 /* Get to the base of _REF nodes. */
2017 if (TREE_CODE (lhs) != SSA_NAME)
2018 lhs = get_base_address (lhs);
2020 /* If we couldn't recognize the LHS of the assignment, assume that it
2021 is a non-local store. */
2022 if (lhs == NULL_TREE)
2025 /* If the RHS is a conversion between a pointer and an integer, the
2026 pointer escapes since we can't track the integer. */
2027 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2028 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2029 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2030 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
2031 (TREE_OPERAND (stmt, 1), 0)))
2032 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
2035 /* If the LHS is an SSA name, it can't possibly represent a non-local
2037 if (TREE_CODE (lhs) == SSA_NAME)
2040 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2041 local variables we cannot be sure if it will escape, because we
2042 don't have information about objects not in SSA form. Need to
2043 implement something along the lines of
2045 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2046 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2047 Conference on Object-Oriented Programming Systems, Languages, and
2048 Applications (OOPSLA), pp. 1-19, 1999. */
2051 else if (TREE_CODE (stmt) == RETURN_EXPR)
2058 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2059 is considered to represent all the pointers whose pointed-to types are
2060 in the same alias set class. Otherwise, the tag represents a single
2061 SSA_NAME pointer variable. */
2064 create_memory_tag (tree type, bool is_type_tag)
2067 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2069 /* By default, memory tags are local variables. Alias analysis will
2070 determine whether they should be considered globals. */
2071 DECL_CONTEXT (tag) = current_function_decl;
2073 /* Memory tags are by definition addressable. This also prevents
2074 is_gimple_ref frome confusing memory tags with optimizable
2076 TREE_ADDRESSABLE (tag) = 1;
2078 ann = get_var_ann (tag);
2079 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2080 ann->type_mem_tag = NULL_TREE;
2082 /* Add the tag to the symbol table. */
2083 add_referenced_tmp_var (tag);
2089 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2090 This is used if P_i has been found to point to a specific set of
2091 variables or to a non-aliased memory location like the address returned
2092 by malloc functions. */
2095 get_nmt_for (tree ptr)
2097 struct ptr_info_def *pi = get_ptr_info (ptr);
2098 tree tag = pi->name_mem_tag;
2100 if (tag == NULL_TREE)
2101 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2103 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
2104 then its name tag should be considered a global variable. */
2105 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
2107 || pi->pt_global_mem)
2108 mark_call_clobbered (tag);
2114 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2115 artificial variable that represents the memory location pointed-to by
2116 PTR. It is used to model the effects of pointer de-references on
2117 addressable variables.
2119 AI points to the data gathered during alias analysis. This function
2120 populates the array AI->POINTERS. */
2123 get_tmt_for (tree ptr, struct alias_info *ai)
2127 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2128 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2130 /* To avoid creating unnecessary memory tags, only create one memory tag
2131 per alias set class. Note that it may be tempting to group
2132 memory tags based on conflicting alias sets instead of
2133 equivalence. That would be wrong because alias sets are not
2134 necessarily transitive (as demonstrated by the libstdc++ test
2135 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2136 such that conflicts (A, B) == true and conflicts (A, C) == true,
2137 it does not necessarily follow that conflicts (B, C) == true. */
2138 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2140 struct alias_map_d *curr = ai->pointers[i];
2141 if (tag_set == curr->set)
2143 tag = var_ann (curr->var)->type_mem_tag;
2148 /* If VAR cannot alias with any of the existing memory tags, create a new
2149 tag for PTR and add it to the POINTERS array. */
2150 if (tag == NULL_TREE)
2152 struct alias_map_d *alias_map;
2154 /* If PTR did not have a type tag already, create a new TMT.*
2155 artificial variable representing the memory location
2156 pointed-to by PTR. */
2157 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2158 tag = create_memory_tag (tag_type, true);
2160 tag = var_ann (ptr)->type_mem_tag;
2162 /* Add PTR to the POINTERS array. Note that we are not interested in
2163 PTR's alias set. Instead, we cache the alias set for the memory that
2165 alias_map = xcalloc (1, sizeof (*alias_map));
2166 alias_map->var = ptr;
2167 alias_map->set = tag_set;
2168 ai->pointers[ai->num_pointers++] = alias_map;
2171 /* If the pointed-to type is volatile, so is the tag. */
2172 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2174 /* Make sure that the type tag has the same alias set as the
2176 gcc_assert (tag_set == get_alias_set (tag));
2182 /* Create GLOBAL_VAR, an artificial global variable to act as a
2183 representative of all the variables that may be clobbered by function
2187 create_global_var (void)
2189 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2191 DECL_ARTIFICIAL (global_var) = 1;
2192 TREE_READONLY (global_var) = 0;
2193 DECL_EXTERNAL (global_var) = 1;
2194 TREE_STATIC (global_var) = 1;
2195 TREE_USED (global_var) = 1;
2196 DECL_CONTEXT (global_var) = NULL_TREE;
2197 TREE_THIS_VOLATILE (global_var) = 0;
2198 TREE_ADDRESSABLE (global_var) = 0;
2200 add_referenced_tmp_var (global_var);
2201 bitmap_set_bit (vars_to_rename, var_ann (global_var)->uid);
2205 /* Dump alias statistics on FILE. */
2208 dump_alias_stats (FILE *file)
2210 const char *funcname
2211 = lang_hooks.decl_printable_name (current_function_decl, 2);
2212 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2213 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2214 fprintf (file, "Total alias mayalias results:\t%u\n",
2215 alias_stats.alias_mayalias);
2216 fprintf (file, "Total alias noalias results:\t%u\n",
2217 alias_stats.alias_noalias);
2218 fprintf (file, "Total simple queries:\t%u\n",
2219 alias_stats.simple_queries);
2220 fprintf (file, "Total simple resolved:\t%u\n",
2221 alias_stats.simple_resolved);
2222 fprintf (file, "Total TBAA queries:\t%u\n",
2223 alias_stats.tbaa_queries);
2224 fprintf (file, "Total TBAA resolved:\t%u\n",
2225 alias_stats.tbaa_resolved);
2229 /* Dump alias information on FILE. */
2232 dump_alias_info (FILE *file)
2235 const char *funcname
2236 = lang_hooks.decl_printable_name (current_function_decl, 2);
2238 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2240 fprintf (file, "Aliased symbols\n\n");
2241 for (i = 0; i < num_referenced_vars; i++)
2243 tree var = referenced_var (i);
2244 if (may_be_aliased (var))
2245 dump_variable (file, var);
2248 fprintf (file, "\nDereferenced pointers\n\n");
2249 for (i = 0; i < num_referenced_vars; i++)
2251 tree var = referenced_var (i);
2252 var_ann_t ann = var_ann (var);
2253 if (ann->type_mem_tag)
2254 dump_variable (file, var);
2257 fprintf (file, "\nType memory tags\n\n");
2258 for (i = 0; i < num_referenced_vars; i++)
2260 tree var = referenced_var (i);
2261 var_ann_t ann = var_ann (var);
2262 if (ann->mem_tag_kind == TYPE_TAG)
2263 dump_variable (file, var);
2266 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2268 fprintf (file, "SSA_NAME pointers\n\n");
2269 for (i = 1; i < num_ssa_names; i++)
2271 tree ptr = ssa_name (i);
2272 struct ptr_info_def *pi;
2274 if (ptr == NULL_TREE)
2277 pi = SSA_NAME_PTR_INFO (ptr);
2278 if (!SSA_NAME_IN_FREE_LIST (ptr)
2280 && pi->name_mem_tag)
2281 dump_points_to_info_for (file, ptr);
2284 fprintf (file, "\nName memory tags\n\n");
2285 for (i = 0; i < num_referenced_vars; i++)
2287 tree var = referenced_var (i);
2288 var_ann_t ann = var_ann (var);
2289 if (ann->mem_tag_kind == NAME_TAG)
2290 dump_variable (file, var);
2293 fprintf (file, "\n");
2297 /* Dump alias information on stderr. */
2300 debug_alias_info (void)
2302 dump_alias_info (stderr);
2306 /* Return the alias information associated with pointer T. It creates a
2307 new instance if none existed. */
2309 struct ptr_info_def *
2310 get_ptr_info (tree t)
2312 struct ptr_info_def *pi;
2314 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2316 pi = SSA_NAME_PTR_INFO (t);
2319 pi = ggc_alloc (sizeof (*pi));
2320 memset ((void *)pi, 0, sizeof (*pi));
2321 SSA_NAME_PTR_INFO (t) = pi;
2328 /* Dump points-to information for SSA_NAME PTR into FILE. */
2331 dump_points_to_info_for (FILE *file, tree ptr)
2333 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2335 print_generic_expr (file, ptr, dump_flags);
2339 if (pi->name_mem_tag)
2341 fprintf (file, ", name memory tag: ");
2342 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2345 if (pi->is_dereferenced)
2346 fprintf (file, ", is dereferenced");
2348 if (pi->value_escapes_p)
2349 fprintf (file, ", its value escapes");
2351 if (pi->pt_anything)
2352 fprintf (file, ", points-to anything");
2355 fprintf (file, ", points-to malloc");
2362 fprintf (file, ", points-to vars: { ");
2363 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2365 print_generic_expr (file, referenced_var (ix), dump_flags);
2366 fprintf (file, " ");
2368 fprintf (file, "}");
2372 fprintf (file, "\n");
2376 /* Dump points-to information for VAR into stderr. */
2379 debug_points_to_info_for (tree var)
2381 dump_points_to_info_for (stderr, var);
2385 /* Dump points-to information into FILE. NOTE: This function is slow, as
2386 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2389 dump_points_to_info (FILE *file)
2392 block_stmt_iterator si;
2396 lang_hooks.decl_printable_name (current_function_decl, 2);
2398 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2400 /* First dump points-to information for the default definitions of
2401 pointer variables. This is necessary because default definitions are
2402 not part of the code. */
2403 for (i = 0; i < num_referenced_vars; i++)
2405 tree var = referenced_var (i);
2406 if (POINTER_TYPE_P (TREE_TYPE (var)))
2408 var_ann_t ann = var_ann (var);
2409 if (ann->default_def)
2410 dump_points_to_info_for (file, ann->default_def);
2414 /* Dump points-to information for every pointer defined in the program. */
2419 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2421 tree ptr = PHI_RESULT (phi);
2422 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2423 dump_points_to_info_for (file, ptr);
2426 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2428 tree stmt = bsi_stmt (si);
2430 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2431 if (POINTER_TYPE_P (TREE_TYPE (def)))
2432 dump_points_to_info_for (file, def);
2436 fprintf (file, "\n");
2440 /* Dump points-to info pointed by PTO into STDERR. */
2443 debug_points_to_info (void)
2445 dump_points_to_info (stderr);
2448 /* Dump to FILE the list of variables that may be aliasing VAR. */
2451 dump_may_aliases_for (FILE *file, tree var)
2453 varray_type aliases;
2455 if (TREE_CODE (var) == SSA_NAME)
2456 var = SSA_NAME_VAR (var);
2458 aliases = var_ann (var)->may_aliases;
2462 fprintf (file, "{ ");
2463 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2465 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2466 fprintf (file, " ");
2468 fprintf (file, "}");
2473 /* Dump to stderr the list of variables that may be aliasing VAR. */
2476 debug_may_aliases_for (tree var)
2478 dump_may_aliases_for (stderr, var);
2481 /* Return true if VAR may be aliased. */
2484 may_be_aliased (tree var)
2487 if (TREE_ADDRESSABLE (var))
2490 /* Globally visible variables can have their addresses taken by other
2491 translation units. */
2492 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2495 /* Automatic variables can't have their addresses escape any other way.
2496 This must be after the check for global variables, as extern declarations
2497 do not have TREE_STATIC set. */
2498 if (!TREE_STATIC (var))
2501 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2502 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2503 we can only be sure the variable isn't addressable if it's local to the
2504 current function. */
2505 if (flag_unit_at_a_time)
2507 if (decl_function_context (var) == current_function_decl)