1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005 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 /* 'true' after aliases have been computed (see compute_may_aliases). */
48 bool aliases_computed_p;
50 /* Structure to map a variable to its alias set and keep track of the
51 virtual operands that will be needed to represent it. */
54 /* Variable and its alias set. */
58 /* Total number of virtual operands that will be needed to represent
59 all the aliases of VAR. */
60 long total_alias_vops;
62 /* Nonzero if the aliases for this memory tag have been grouped
63 already. Used in group_aliases. */
64 unsigned int grouped_p : 1;
66 /* Set of variables aliased with VAR. This is the exact same
67 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
68 bitmap form to speed up alias grouping. */
73 /* Alias information used by compute_may_aliases and its helpers. */
76 /* SSA names visited while collecting points-to information. If bit I
77 is set, it means that SSA variable with version I has already been
79 sbitmap ssa_names_visited;
81 /* Array of SSA_NAME pointers processed by the points-to collector. */
82 varray_type processed_ptrs;
84 /* Variables whose address is still needed. */
85 bitmap addresses_needed;
87 /* ADDRESSABLE_VARS contains all the global variables and locals that
88 have had their address taken. */
89 struct alias_map_d **addressable_vars;
90 size_t num_addressable_vars;
92 /* POINTERS contains all the _DECL pointers with unique memory tags
93 that have been referenced in the program. */
94 struct alias_map_d **pointers;
97 /* Number of function calls found in the program. */
98 size_t num_calls_found;
100 /* Number of const/pure function calls found in the program. */
101 size_t num_pure_const_calls_found;
103 /* Array of counters to keep track of how many times each pointer has
104 been dereferenced in the program. This is used by the alias grouping
105 heuristic in compute_flow_insensitive_aliasing. */
106 varray_type num_references;
108 /* Total number of virtual operands that will be needed to represent
109 all the aliases of all the pointers found in the program. */
110 long total_alias_vops;
112 /* Variables that have been written to. */
115 /* Pointers that have been used in an indirect store operation. */
116 bitmap dereferenced_ptrs_store;
118 /* Pointers that have been used in an indirect load operation. */
119 bitmap dereferenced_ptrs_load;
123 /* Counters used to display statistics on alias analysis. */
126 unsigned int alias_queries;
127 unsigned int alias_mayalias;
128 unsigned int alias_noalias;
129 unsigned int simple_queries;
130 unsigned int simple_resolved;
131 unsigned int tbaa_queries;
132 unsigned int tbaa_resolved;
136 /* Local variables. */
137 static struct alias_stats_d alias_stats;
139 /* Local functions. */
140 static void compute_flow_insensitive_aliasing (struct alias_info *);
141 static void dump_alias_stats (FILE *);
142 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT);
143 static tree create_memory_tag (tree type, bool is_type_tag);
144 static tree get_tmt_for (tree, struct alias_info *);
145 static tree get_nmt_for (tree);
146 static void add_may_alias (tree, tree);
147 static void replace_may_alias (tree, size_t, tree);
148 static struct alias_info *init_alias_info (void);
149 static void delete_alias_info (struct alias_info *);
150 static void compute_points_to_and_addr_escape (struct alias_info *);
151 static void compute_flow_sensitive_aliasing (struct alias_info *);
152 static void setup_pointers_and_addressables (struct alias_info *);
153 static bool collect_points_to_info_r (tree, tree, void *);
154 static bool is_escape_site (tree, struct alias_info *);
155 static void add_pointed_to_var (struct alias_info *, tree, tree);
156 static void create_global_var (void);
157 static void collect_points_to_info_for (struct alias_info *, tree);
158 static void maybe_create_global_var (struct alias_info *ai);
159 static void group_aliases (struct alias_info *);
160 static void set_pt_anything (tree ptr);
161 static void set_pt_malloc (tree ptr);
163 /* Global declarations. */
165 /* Call clobbered variables in the function. If bit I is set, then
166 REFERENCED_VARS (I) is call-clobbered. */
167 bitmap call_clobbered_vars;
169 /* Addressable variables in the function. If bit I is set, then
170 REFERENCED_VARS (I) has had its address taken. Note that
171 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
172 addressable variable is not necessarily call-clobbered (e.g., a
173 local addressable whose address does not escape) and not all
174 call-clobbered variables are addressable (e.g., a local static
176 bitmap addressable_vars;
178 /* When the program has too many call-clobbered variables and call-sites,
179 this variable is used to represent the clobbering effects of function
180 calls. In these cases, all the call clobbered variables in the program
181 are forced to alias this variable. This reduces compile times by not
182 having to keep track of too many V_MAY_DEF expressions at call sites. */
186 /* Compute may-alias information for every variable referenced in function
189 Alias analysis proceeds in 3 main phases:
191 1- Points-to and escape analysis.
193 This phase walks the use-def chains in the SSA web looking for three
196 * Assignments of the form P_i = &VAR
197 * Assignments of the form P_i = malloc()
198 * Pointers and ADDR_EXPR that escape the current function.
200 The concept of 'escaping' is the same one used in the Java world. When
201 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
202 outside of the current function. So, assignment to global variables,
203 function arguments and returning a pointer are all escape sites, as are
204 conversions between pointers and integers.
206 This is where we are currently limited. Since not everything is renamed
207 into SSA, we lose track of escape properties when a pointer is stashed
208 inside a field in a structure, for instance. In those cases, we are
209 assuming that the pointer does escape.
211 We use escape analysis to determine whether a variable is
212 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
213 is call-clobbered. If a pointer P_i escapes, then all the variables
214 pointed-to by P_i (and its memory tag) also escape.
216 2- Compute flow-sensitive aliases
218 We have two classes of memory tags. Memory tags associated with the
219 pointed-to data type of the pointers in the program. These tags are
220 called "type memory tag" (TMT). The other class are those associated
221 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
222 when adding operands for an INDIRECT_REF *P_i, we will first check
223 whether P_i has a name tag, if it does we use it, because that will have
224 more precise aliasing information. Otherwise, we use the standard type
227 In this phase, we go through all the pointers we found in points-to
228 analysis and create alias sets for the name memory tags associated with
229 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
230 it points to and its tag.
233 3- Compute flow-insensitive aliases
235 This pass will compare the alias set of every type memory tag and every
236 addressable variable found in the program. Given a type memory tag TMT
237 and an addressable variable V. If the alias sets of TMT and V conflict
238 (as computed by may_alias_p), then V is marked as an alias tag and added
239 to the alias set of TMT.
241 For instance, consider the following function:
257 After aliasing analysis has finished, the type memory tag for pointer
258 'p' will have two aliases, namely variables 'a' and 'b'. Every time
259 pointer 'p' is dereferenced, we want to mark the operation as a
260 potential reference to 'a' and 'b'.
270 # p_1 = PHI <p_4(1), p_6(2)>;
272 # a_7 = V_MAY_DEF <a_3>;
273 # b_8 = V_MAY_DEF <b_5>;
276 # a_9 = V_MAY_DEF <a_7>
285 In certain cases, the list of may aliases for a pointer may grow too
286 large. This may cause an explosion in the number of virtual operands
287 inserted in the code. Resulting in increased memory consumption and
290 When the number of virtual operands needed to represent aliased
291 loads and stores grows too large (configurable with @option{--param
292 max-aliased-vops}), alias sets are grouped to avoid severe
293 compile-time slow downs and memory consumption. See group_aliases. */
296 compute_may_aliases (void)
298 struct alias_info *ai;
300 memset (&alias_stats, 0, sizeof (alias_stats));
302 /* Initialize aliasing information. */
303 ai = init_alias_info ();
305 /* For each pointer P_i, determine the sets of variables that P_i may
306 point-to. For every addressable variable V, determine whether the
307 address of V escapes the current function, making V call-clobbered
308 (i.e., whether &V is stored in a global variable or if its passed as a
309 function call argument). */
310 compute_points_to_and_addr_escape (ai);
312 /* Collect all pointers and addressable variables, compute alias sets,
313 create memory tags for pointers and promote variables whose address is
314 not needed anymore. */
315 setup_pointers_and_addressables (ai);
317 /* Compute flow-sensitive, points-to based aliasing for all the name
318 memory tags. Note that this pass needs to be done before flow
319 insensitive analysis because it uses the points-to information
320 gathered before to mark call-clobbered type tags. */
321 compute_flow_sensitive_aliasing (ai);
323 /* Compute type-based flow-insensitive aliasing for all the type
325 compute_flow_insensitive_aliasing (ai);
327 /* If the program has too many call-clobbered variables and/or function
328 calls, create .GLOBAL_VAR and use it to model call-clobbering
329 semantics at call sites. This reduces the number of virtual operands
330 considerably, improving compile times at the expense of lost
331 aliasing precision. */
332 maybe_create_global_var (ai);
334 /* Debugging dumps. */
337 dump_referenced_vars (dump_file);
338 if (dump_flags & TDF_STATS)
339 dump_alias_stats (dump_file);
340 dump_points_to_info (dump_file);
341 dump_alias_info (dump_file);
344 /* Deallocate memory used by aliasing data structures. */
345 delete_alias_info (ai);
348 block_stmt_iterator bsi;
352 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
354 update_stmt_if_modified (bsi_stmt (bsi));
361 struct tree_opt_pass pass_may_alias =
365 compute_may_aliases, /* execute */
368 0, /* static_pass_number */
369 TV_TREE_MAY_ALIAS, /* tv_id */
370 PROP_cfg | PROP_ssa, /* properties_required */
371 PROP_alias, /* properties_provided */
372 0, /* properties_destroyed */
373 0, /* todo_flags_start */
374 TODO_dump_func | TODO_update_ssa
375 | TODO_ggc_collect | TODO_verify_ssa
376 | TODO_verify_stmts, /* todo_flags_finish */
381 /* Data structure used to count the number of dereferences to PTR
382 inside an expression. */
390 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
391 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
394 count_ptr_derefs (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
396 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
398 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
405 /* Count the number of direct and indirect uses for pointer PTR in
406 statement STMT. The two counts are stored in *NUM_USES_P and
407 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
408 least one of those dereferences is a store operation. */
411 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
412 unsigned *num_derefs_p, bool *is_store)
421 /* Find out the total number of uses of PTR in STMT. */
422 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
426 /* Now count the number of indirect references to PTR. This is
427 truly awful, but we don't have much choice. There are no parent
428 pointers inside INDIRECT_REFs, so an expression like
429 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
430 find all the indirect and direct uses of x_1 inside. The only
431 shortcut we can take is the fact that GIMPLE only allows
432 INDIRECT_REFs inside the expressions below. */
433 if (TREE_CODE (stmt) == MODIFY_EXPR
434 || (TREE_CODE (stmt) == RETURN_EXPR
435 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
436 || TREE_CODE (stmt) == ASM_EXPR
437 || TREE_CODE (stmt) == CALL_EXPR)
441 if (TREE_CODE (stmt) == MODIFY_EXPR)
443 lhs = TREE_OPERAND (stmt, 0);
444 rhs = TREE_OPERAND (stmt, 1);
446 else if (TREE_CODE (stmt) == RETURN_EXPR)
448 tree e = TREE_OPERAND (stmt, 0);
449 lhs = TREE_OPERAND (e, 0);
450 rhs = TREE_OPERAND (e, 1);
452 else if (TREE_CODE (stmt) == ASM_EXPR)
454 lhs = ASM_OUTPUTS (stmt);
455 rhs = ASM_INPUTS (stmt);
463 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
465 struct count_ptr_d count;
468 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
470 *num_derefs_p = count.count;
473 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
475 struct count_ptr_d count;
478 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
479 *num_derefs_p += count.count;
483 gcc_assert (*num_uses_p >= *num_derefs_p);
487 /* Initialize the data structures used for alias analysis. */
489 static struct alias_info *
490 init_alias_info (void)
492 struct alias_info *ai;
494 ai = xcalloc (1, sizeof (struct alias_info));
495 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
496 sbitmap_zero (ai->ssa_names_visited);
497 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
498 ai->addresses_needed = BITMAP_ALLOC (NULL);
499 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
500 ai->written_vars = BITMAP_ALLOC (NULL);
501 ai->dereferenced_ptrs_store = BITMAP_ALLOC (NULL);
502 ai->dereferenced_ptrs_load = BITMAP_ALLOC (NULL);
504 /* If aliases have been computed before, clear existing information. */
505 if (aliases_computed_p)
509 /* Similarly, clear the set of addressable variables. In this
510 case, we can just clear the set because addressability is
511 only computed here. */
512 bitmap_clear (addressable_vars);
514 /* Clear flow-insensitive alias information from each symbol. */
515 for (i = 0; i < num_referenced_vars; i++)
517 tree var = referenced_var (i);
518 var_ann_t ann = var_ann (var);
520 ann->is_alias_tag = 0;
521 ann->may_aliases = NULL;
523 /* Since we are about to re-discover call-clobbered
524 variables, clear the call-clobbered flag. Variables that
525 are intrinsically call-clobbered (globals, local statics,
526 etc) will not be marked by the aliasing code, so we can't
527 remove them from CALL_CLOBBERED_VARS.
529 NB: STRUCT_FIELDS are still call clobbered if they are for
530 a global variable, so we *don't* clear their call clobberedness
531 just because they are tags, though we will clear it if they
532 aren't for global variables. */
533 if (ann->mem_tag_kind == NAME_TAG
534 || ann->mem_tag_kind == TYPE_TAG
535 || !is_global_var (var))
536 clear_call_clobbered (var);
539 /* Clear flow-sensitive points-to information from each SSA name. */
540 for (i = 1; i < num_ssa_names; i++)
542 tree name = ssa_name (i);
544 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
547 if (SSA_NAME_PTR_INFO (name))
549 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
551 /* Clear all the flags but keep the name tag to
552 avoid creating new temporaries unnecessarily. If
553 this pointer is found to point to a subset or
554 superset of its former points-to set, then a new
555 tag will need to be created in create_name_tags. */
559 pi->value_escapes_p = 0;
560 pi->is_dereferenced = 0;
562 bitmap_clear (pi->pt_vars);
567 /* Next time, we will need to reset alias information. */
568 aliases_computed_p = true;
574 /* Deallocate memory used by alias analysis. */
577 delete_alias_info (struct alias_info *ai)
581 sbitmap_free (ai->ssa_names_visited);
582 ai->processed_ptrs = NULL;
583 BITMAP_FREE (ai->addresses_needed);
585 for (i = 0; i < ai->num_addressable_vars; i++)
587 sbitmap_free (ai->addressable_vars[i]->may_aliases);
588 free (ai->addressable_vars[i]);
590 free (ai->addressable_vars);
592 for (i = 0; i < ai->num_pointers; i++)
594 sbitmap_free (ai->pointers[i]->may_aliases);
595 free (ai->pointers[i]);
599 ai->num_references = NULL;
600 BITMAP_FREE (ai->written_vars);
601 BITMAP_FREE (ai->dereferenced_ptrs_store);
602 BITMAP_FREE (ai->dereferenced_ptrs_load);
608 /* Walk use-def chains for pointer PTR to determine what variables is PTR
612 collect_points_to_info_for (struct alias_info *ai, tree ptr)
614 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
616 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
618 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
619 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
620 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
625 /* Traverse use-def links for all the pointers in the program to collect
626 address escape and points-to information.
628 This is loosely based on the same idea described in R. Hasti and S.
629 Horwitz, ``Using static single assignment form to improve
630 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
631 Programming Language Design and Implementation, pp. 97-105, 1998. */
634 compute_points_to_and_addr_escape (struct alias_info *ai)
641 timevar_push (TV_TREE_PTA);
645 bb_ann_t block_ann = bb_ann (bb);
646 block_stmt_iterator si;
648 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
651 tree stmt = bsi_stmt (si);
652 bool stmt_escapes_p = is_escape_site (stmt, ai);
655 /* Mark all the variables whose address are taken by the
656 statement. Note that this will miss all the addresses taken
657 in PHI nodes (those are discovered while following the use-def
659 get_stmt_operands (stmt);
660 addr_taken = addresses_taken (stmt);
662 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
664 tree var = referenced_var (i);
665 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
667 mark_call_clobbered (var);
671 block_ann->has_escape_site = 1;
673 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
675 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
676 struct ptr_info_def *pi;
678 unsigned num_uses, num_derefs;
680 /* If the operand's variable may be aliased, keep track
681 of how many times we've referenced it. This is used
682 for alias grouping in compute_flow_sensitive_aliasing.
683 Note that we don't need to grow AI->NUM_REFERENCES
684 because we are processing regular variables, not
685 memory tags (the array's initial size is set to
686 NUM_REFERENCED_VARS). */
687 if (may_be_aliased (SSA_NAME_VAR (op)))
688 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
690 if (!POINTER_TYPE_P (TREE_TYPE (op)))
693 collect_points_to_info_for (ai, op);
695 pi = SSA_NAME_PTR_INFO (op);
696 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs,
701 /* Mark OP as dereferenced. In a subsequent pass,
702 dereferenced pointers that point to a set of
703 variables will be assigned a name tag to alias
704 all the variables OP points to. */
705 pi->is_dereferenced = 1;
707 /* Keep track of how many time we've dereferenced each
708 pointer. Again, we don't need to grow
709 AI->NUM_REFERENCES because we're processing
710 existing program variables. */
711 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
713 /* If this is a store operation, mark OP as being
714 dereferenced to store, otherwise mark it as being
715 dereferenced to load. */
717 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
719 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
722 if (stmt_escapes_p && num_derefs < num_uses)
724 /* If STMT is an escape point and STMT contains at
725 least one direct use of OP, then the value of OP
726 escapes and so the pointed-to variables need to
727 be marked call-clobbered. */
728 pi->value_escapes_p = 1;
730 /* If the statement makes a function call, assume
731 that pointer OP will be dereferenced in a store
732 operation inside the called function. */
733 if (get_call_expr_in (stmt))
735 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
736 pi->is_dereferenced = 1;
741 /* Update reference counter for definitions to any
742 potentially aliased variable. This is used in the alias
743 grouping heuristics. */
744 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
746 tree var = SSA_NAME_VAR (op);
747 var_ann_t ann = var_ann (var);
748 bitmap_set_bit (ai->written_vars, ann->uid);
749 if (may_be_aliased (var))
750 (VARRAY_UINT (ai->num_references, ann->uid))++;
752 if (POINTER_TYPE_P (TREE_TYPE (op)))
753 collect_points_to_info_for (ai, op);
756 /* Mark variables in V_MAY_DEF operands as being written to. */
757 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
759 tree var = DECL_P (op) ? op : SSA_NAME_VAR (op);
760 var_ann_t ann = var_ann (var);
761 bitmap_set_bit (ai->written_vars, ann->uid);
764 /* After promoting variables and computing aliasing we will
765 need to re-scan most statements. FIXME: Try to minimize the
766 number of statements re-scanned. It's not really necessary to
767 re-scan *all* statements. */
768 mark_stmt_modified (stmt);
772 timevar_pop (TV_TREE_PTA);
776 /* Create name tags for all the pointers that have been dereferenced.
777 We only create a name tag for a pointer P if P is found to point to
778 a set of variables (so that we can alias them to *P) or if it is
779 the result of a call to malloc (which means that P cannot point to
780 anything else nor alias any other variable).
782 If two pointers P and Q point to the same set of variables, they
783 are assigned the same name tag. */
786 create_name_tags (struct alias_info *ai)
790 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
792 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
793 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
795 if (pi->pt_anything || !pi->is_dereferenced)
797 /* No name tags for pointers that have not been
798 dereferenced or point to an arbitrary location. */
799 pi->name_mem_tag = NULL_TREE;
803 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
806 tree old_name_tag = pi->name_mem_tag;
808 /* If PTR points to a set of variables, check if we don't
809 have another pointer Q with the same points-to set before
810 creating a tag. If so, use Q's tag instead of creating a
813 This is important for not creating unnecessary symbols
814 and also for copy propagation. If we ever need to
815 propagate PTR into Q or vice-versa, we would run into
816 problems if they both had different name tags because
817 they would have different SSA version numbers (which
818 would force us to take the name tags in and out of SSA). */
819 for (j = 0; j < i; j++)
821 tree q = VARRAY_TREE (ai->processed_ptrs, j);
822 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
827 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
829 pi->name_mem_tag = qi->name_mem_tag;
834 /* If we didn't find a pointer with the same points-to set
835 as PTR, create a new name tag if needed. */
836 if (pi->name_mem_tag == NULL_TREE)
837 pi->name_mem_tag = get_nmt_for (ptr);
839 /* If the new name tag computed for PTR is different than
840 the old name tag that it used to have, then the old tag
841 needs to be removed from the IL, so we mark it for
843 if (old_name_tag && old_name_tag != pi->name_mem_tag)
844 mark_sym_for_renaming (old_name_tag);
846 else if (pi->pt_malloc)
848 /* Otherwise, create a unique name tag for this pointer. */
849 pi->name_mem_tag = get_nmt_for (ptr);
853 /* Only pointers that may point to malloc or other variables
854 may receive a name tag. If the pointer does not point to
855 a known spot, we should use type tags. */
856 set_pt_anything (ptr);
860 TREE_THIS_VOLATILE (pi->name_mem_tag)
861 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
863 /* Mark the new name tag for renaming. */
864 mark_sym_for_renaming (pi->name_mem_tag);
870 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
871 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
872 name tag and the variables it points-to are call-clobbered. Finally, if
873 P_i escapes and we could not determine where it points to, then all the
874 variables in the same alias set as *P_i are marked call-clobbered. This
875 is necessary because we must assume that P_i may take the address of any
876 variable in the same alias set. */
879 compute_flow_sensitive_aliasing (struct alias_info *ai)
883 create_name_tags (ai);
885 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
888 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
889 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
890 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
893 if (pi->value_escapes_p || pi->pt_anything)
895 /* If PTR escapes or may point to anything, then its associated
896 memory tags and pointed-to variables are call-clobbered. */
897 if (pi->name_mem_tag)
898 mark_call_clobbered (pi->name_mem_tag);
900 if (v_ann->type_mem_tag)
901 mark_call_clobbered (v_ann->type_mem_tag);
904 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
906 mark_call_clobbered (referenced_var (j));
910 /* Set up aliasing information for PTR's name memory tag (if it has
911 one). Note that only pointers that have been dereferenced will
912 have a name memory tag. */
913 if (pi->name_mem_tag && pi->pt_vars)
914 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
916 add_may_alias (pi->name_mem_tag, referenced_var (j));
917 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
920 /* If the name tag is call clobbered, so is the type tag
921 associated with the base VAR_DECL. */
923 && v_ann->type_mem_tag
924 && is_call_clobbered (pi->name_mem_tag))
925 mark_call_clobbered (v_ann->type_mem_tag);
930 /* Compute type-based alias sets. Traverse all the pointers and
931 addressable variables found in setup_pointers_and_addressables.
933 For every pointer P in AI->POINTERS and addressable variable V in
934 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
935 memory tag (TMT) if their alias sets conflict. V is then marked as
936 an alias tag so that the operand scanner knows that statements
937 containing V have aliased operands. */
940 compute_flow_insensitive_aliasing (struct alias_info *ai)
944 /* Initialize counter for the total number of virtual operands that
945 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
946 threshold set by --params max-alias-vops, we enable alias
948 ai->total_alias_vops = 0;
950 /* For every pointer P, determine which addressable variables may alias
951 with P's type memory tag. */
952 for (i = 0; i < ai->num_pointers; i++)
955 struct alias_map_d *p_map = ai->pointers[i];
956 tree tag = var_ann (p_map->var)->type_mem_tag;
957 var_ann_t tag_ann = var_ann (tag);
959 p_map->total_alias_vops = 0;
960 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
961 sbitmap_zero (p_map->may_aliases);
963 for (j = 0; j < ai->num_addressable_vars; j++)
965 struct alias_map_d *v_map;
968 bool tag_stored_p, var_stored_p;
970 v_map = ai->addressable_vars[j];
972 v_ann = var_ann (var);
974 /* Skip memory tags and variables that have never been
975 written to. We also need to check if the variables are
976 call-clobbered because they may be overwritten by
979 Note this is effectively random accessing elements in
980 the sparse bitset, which can be highly inefficient.
981 So we first check the call_clobbered status of the
982 tag and variable before querying the bitmap. */
983 tag_stored_p = is_call_clobbered (tag)
984 || bitmap_bit_p (ai->written_vars, tag_ann->uid);
985 var_stored_p = is_call_clobbered (var)
986 || bitmap_bit_p (ai->written_vars, v_ann->uid);
987 if (!tag_stored_p && !var_stored_p)
990 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
993 size_t num_tag_refs, num_var_refs;
995 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
996 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
998 /* Add VAR to TAG's may-aliases set. */
1000 /* If this is an aggregate, we may have subvariables for it
1001 that need to be pointed to. */
1002 if (var_can_have_subvars (var)
1003 && (svars = get_subvars_for_var (var)))
1007 for (sv = svars; sv; sv = sv->next)
1009 add_may_alias (tag, sv->var);
1010 /* Update the bitmap used to represent TAG's alias set
1011 in case we need to group aliases. */
1012 SET_BIT (p_map->may_aliases, var_ann (sv->var)->uid);
1017 add_may_alias (tag, var);
1018 /* Update the bitmap used to represent TAG's alias set
1019 in case we need to group aliases. */
1020 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
1023 /* Update the total number of virtual operands due to
1024 aliasing. Since we are adding one more alias to TAG's
1025 may-aliases set, the total number of virtual operands due
1026 to aliasing will be increased by the number of references
1027 made to VAR and TAG (every reference to TAG will also
1028 count as a reference to VAR). */
1029 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1030 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1037 /* Since this analysis is based exclusively on symbols, it fails to
1038 handle cases where two pointers P and Q have different memory
1039 tags with conflicting alias set numbers but no aliased symbols in
1042 For example, suppose that we have two memory tags TMT.1 and TMT.2
1045 may-aliases (TMT.1) = { a }
1046 may-aliases (TMT.2) = { b }
1048 and the alias set number of TMT.1 conflicts with that of TMT.2.
1049 Since they don't have symbols in common, loads and stores from
1050 TMT.1 and TMT.2 will seem independent of each other, which will
1051 lead to the optimizers making invalid transformations (see
1052 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1054 To avoid this problem, we do a final traversal of AI->POINTERS
1055 looking for pairs of pointers that have no aliased symbols in
1056 common and yet have conflicting alias set numbers. */
1057 for (i = 0; i < ai->num_pointers; i++)
1060 struct alias_map_d *p_map1 = ai->pointers[i];
1061 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1062 sbitmap may_aliases1 = p_map1->may_aliases;
1064 for (j = i + 1; j < ai->num_pointers; j++)
1066 struct alias_map_d *p_map2 = ai->pointers[j];
1067 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1068 sbitmap may_aliases2 = p_map2->may_aliases;
1070 /* If the pointers may not point to each other, do nothing. */
1071 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set))
1074 /* The two pointers may alias each other. If they already have
1075 symbols in common, do nothing. */
1076 if (sbitmap_any_common_bits (may_aliases1, may_aliases2))
1079 if (sbitmap_first_set_bit (may_aliases2) >= 0)
1083 /* Add all the aliases for TAG2 into TAG1's alias set.
1084 FIXME, update grouping heuristic counters. */
1085 EXECUTE_IF_SET_IN_SBITMAP (may_aliases2, 0, k,
1086 add_may_alias (tag1, referenced_var (k)));
1087 sbitmap_a_or_b (may_aliases1, may_aliases1, may_aliases2);
1091 /* Since TAG2 does not have any aliases of its own, add
1092 TAG2 itself to the alias set of TAG1. */
1093 add_may_alias (tag1, tag2);
1094 SET_BIT (may_aliases1, var_ann (tag2)->uid);
1100 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
1101 get_name (current_function_decl),
1102 ai->total_alias_vops);
1104 /* Determine if we need to enable alias grouping. */
1105 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1110 /* Comparison function for qsort used in group_aliases. */
1113 total_alias_vops_cmp (const void *p, const void *q)
1115 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1116 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1117 long n1 = (*p1)->total_alias_vops;
1118 long n2 = (*p2)->total_alias_vops;
1120 /* We want to sort in descending order. */
1121 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1124 /* Group all the aliases for TAG to make TAG represent all the
1125 variables in its alias set. Update the total number
1126 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1127 function will make TAG be the unique alias tag for all the
1128 variables in its may-aliases. So, given:
1130 may-aliases(TAG) = { V1, V2, V3 }
1132 This function will group the variables into:
1134 may-aliases(V1) = { TAG }
1135 may-aliases(V2) = { TAG }
1136 may-aliases(V2) = { TAG } */
1139 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
1142 var_ann_t tag_ann = var_ann (tag);
1143 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1145 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i,
1147 tree var = referenced_var (i);
1148 var_ann_t ann = var_ann (var);
1150 /* Make TAG the unique alias of VAR. */
1151 ann->is_alias_tag = 0;
1152 ann->may_aliases = NULL;
1154 /* Note that VAR and TAG may be the same if the function has no
1155 addressable variables (see the discussion at the end of
1156 setup_pointers_and_addressables). */
1158 add_may_alias (var, tag);
1160 /* Reduce total number of virtual operands contributed
1161 by TAG on behalf of VAR. Notice that the references to VAR
1162 itself won't be removed. We will merely replace them with
1163 references to TAG. */
1164 ai->total_alias_vops -= num_tag_refs;
1167 /* We have reduced the number of virtual operands that TAG makes on
1168 behalf of all the variables formerly aliased with it. However,
1169 we have also "removed" all the virtual operands for TAG itself,
1170 so we add them back. */
1171 ai->total_alias_vops += num_tag_refs;
1173 /* TAG no longer has any aliases. */
1174 tag_ann->may_aliases = NULL;
1178 /* Group may-aliases sets to reduce the number of virtual operands due
1181 1- Sort the list of pointers in decreasing number of contributed
1184 2- Take the first entry in AI->POINTERS and revert the role of
1185 the memory tag and its aliases. Usually, whenever an aliased
1186 variable Vi is found to alias with a memory tag T, we add Vi
1187 to the may-aliases set for T. Meaning that after alias
1188 analysis, we will have:
1190 may-aliases(T) = { V1, V2, V3, ..., Vn }
1192 This means that every statement that references T, will get 'n'
1193 virtual operands for each of the Vi tags. But, when alias
1194 grouping is enabled, we make T an alias tag and add it to the
1195 alias set of all the Vi variables:
1197 may-aliases(V1) = { T }
1198 may-aliases(V2) = { T }
1200 may-aliases(Vn) = { T }
1202 This has two effects: (a) statements referencing T will only get
1203 a single virtual operand, and, (b) all the variables Vi will now
1204 appear to alias each other. So, we lose alias precision to
1205 improve compile time. But, in theory, a program with such a high
1206 level of aliasing should not be very optimizable in the first
1209 3- Since variables may be in the alias set of more than one
1210 memory tag, the grouping done in step (2) needs to be extended
1211 to all the memory tags that have a non-empty intersection with
1212 the may-aliases set of tag T. For instance, if we originally
1213 had these may-aliases sets:
1215 may-aliases(T) = { V1, V2, V3 }
1216 may-aliases(R) = { V2, V4 }
1218 In step (2) we would have reverted the aliases for T as:
1220 may-aliases(V1) = { T }
1221 may-aliases(V2) = { T }
1222 may-aliases(V3) = { T }
1224 But note that now V2 is no longer aliased with R. We could
1225 add R to may-aliases(V2), but we are in the process of
1226 grouping aliases to reduce virtual operands so what we do is
1227 add V4 to the grouping to obtain:
1229 may-aliases(V1) = { T }
1230 may-aliases(V2) = { T }
1231 may-aliases(V3) = { T }
1232 may-aliases(V4) = { T }
1234 4- If the total number of virtual operands due to aliasing is
1235 still above the threshold set by max-alias-vops, go back to (2). */
1238 group_aliases (struct alias_info *ai)
1242 /* Sort the POINTERS array in descending order of contributed
1243 virtual operands. */
1244 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1245 total_alias_vops_cmp);
1247 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1248 and the tag's may-aliases set. */
1249 for (i = 0; i < ai->num_pointers; i++)
1252 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1253 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1255 /* Skip tags that have been grouped already. */
1256 if (ai->pointers[i]->grouped_p)
1259 /* See if TAG1 had any aliases in common with other type tags.
1260 If we find a TAG2 with common aliases with TAG1, add TAG2's
1261 aliases into TAG1. */
1262 for (j = i + 1; j < ai->num_pointers; j++)
1264 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1266 if (sbitmap_any_common_bits (tag1_aliases, tag2_aliases))
1268 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1270 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1272 /* TAG2 does not need its aliases anymore. */
1273 sbitmap_zero (tag2_aliases);
1274 var_ann (tag2)->may_aliases = NULL;
1276 /* TAG1 is the unique alias of TAG2. */
1277 add_may_alias (tag2, tag1);
1279 ai->pointers[j]->grouped_p = true;
1283 /* Now group all the aliases we collected into TAG1. */
1284 group_aliases_into (tag1, tag1_aliases, ai);
1286 /* If we've reduced total number of virtual operands below the
1288 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1292 /* Finally, all the variables that have been grouped cannot be in
1293 the may-alias set of name memory tags. Suppose that we have
1294 grouped the aliases in this code so that may-aliases(a) = TMT.20
1298 # a_9 = V_MAY_DEF <a_8>
1300 ... Several modifications to TMT.20 ...
1304 Since p_5 points to 'a', the optimizers will try to propagate 0
1305 into p_5->field, but that is wrong because there have been
1306 modifications to 'TMT.20' in between. To prevent this we have to
1307 replace 'a' with 'TMT.20' in the name tag of p_5. */
1308 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1311 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1312 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1313 varray_type aliases;
1315 if (name_tag == NULL_TREE)
1318 aliases = var_ann (name_tag)->may_aliases;
1319 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1321 tree alias = VARRAY_TREE (aliases, j);
1322 var_ann_t ann = var_ann (alias);
1324 if ((ann->mem_tag_kind == NOT_A_TAG
1325 || ann->mem_tag_kind == STRUCT_FIELD)
1326 && ann->may_aliases)
1330 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1332 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1333 replace_may_alias (name_tag, j, new_alias);
1340 "%s: Total number of aliased vops after grouping: %ld%s\n",
1341 get_name (current_function_decl),
1342 ai->total_alias_vops,
1343 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1347 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1350 create_alias_map_for (tree var, struct alias_info *ai)
1352 struct alias_map_d *alias_map;
1353 alias_map = xcalloc (1, sizeof (*alias_map));
1354 alias_map->var = var;
1355 alias_map->set = get_alias_set (var);
1356 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1360 /* Create memory tags for all the dereferenced pointers and build the
1361 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1362 sets. Based on the address escape and points-to information collected
1363 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1364 variables whose address is not needed anymore. */
1367 setup_pointers_and_addressables (struct alias_info *ai)
1369 size_t i, n_vars, num_addressable_vars, num_pointers;
1371 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1372 num_addressable_vars = num_pointers = 0;
1373 for (i = 0; i < num_referenced_vars; i++)
1375 tree var = referenced_var (i);
1377 if (may_be_aliased (var))
1378 num_addressable_vars++;
1380 if (POINTER_TYPE_P (TREE_TYPE (var)))
1382 /* Since we don't keep track of volatile variables, assume that
1383 these pointers are used in indirect store operations. */
1384 if (TREE_THIS_VOLATILE (var))
1385 bitmap_set_bit (ai->dereferenced_ptrs_store, var_ann (var)->uid);
1391 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1392 always going to be slightly bigger than we actually need them
1393 because some TREE_ADDRESSABLE variables will be marked
1394 non-addressable below and only pointers with unique type tags are
1395 going to be added to POINTERS. */
1396 ai->addressable_vars = xcalloc (num_addressable_vars,
1397 sizeof (struct alias_map_d *));
1398 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1399 ai->num_addressable_vars = 0;
1400 ai->num_pointers = 0;
1402 /* Since we will be creating type memory tags within this loop, cache the
1403 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1405 n_vars = num_referenced_vars;
1407 for (i = 0; i < n_vars; i++)
1409 tree var = referenced_var (i);
1410 var_ann_t v_ann = var_ann (var);
1413 /* Name memory tags already have flow-sensitive aliasing
1414 information, so they need not be processed by
1415 compute_flow_insensitive_aliasing. Similarly, type memory
1416 tags are already accounted for when we process their
1419 Structure fields, on the other hand, have to have some of this
1420 information processed for them, but it's pointless to mark them
1421 non-addressable (since they are fake variables anyway). */
1422 if (v_ann->mem_tag_kind != NOT_A_TAG
1423 && v_ann->mem_tag_kind != STRUCT_FIELD)
1426 /* Remove the ADDRESSABLE flag from every addressable variable whose
1427 address is not needed anymore. This is caused by the propagation
1428 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1429 removal of dead pointer assignments done by the early scalar
1431 if (TREE_ADDRESSABLE (var) && v_ann->mem_tag_kind != STRUCT_FIELD)
1433 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1434 && TREE_CODE (var) != RESULT_DECL
1435 && !is_global_var (var))
1437 bool okay_to_mark = true;
1439 /* Since VAR is now a regular GIMPLE register, we will need
1440 to rename VAR into SSA afterwards. */
1441 mark_sym_for_renaming (var);
1443 if (var_can_have_subvars (var)
1444 && (svars = get_subvars_for_var (var)))
1448 for (sv = svars; sv; sv = sv->next)
1450 var_ann_t svann = var_ann (sv->var);
1451 if (bitmap_bit_p (ai->addresses_needed, svann->uid))
1452 okay_to_mark = false;
1453 mark_sym_for_renaming (sv->var);
1457 /* The address of VAR is not needed, remove the
1458 addressable bit, so that it can be optimized as a
1459 regular variable. */
1461 mark_non_addressable (var);
1465 /* Add the variable to the set of addressables. Mostly
1466 used when scanning operands for ASM_EXPRs that
1467 clobber memory. In those cases, we need to clobber
1468 all call-clobbered variables and all addressables. */
1469 bitmap_set_bit (addressable_vars, v_ann->uid);
1470 if (var_can_have_subvars (var)
1471 && (svars = get_subvars_for_var (var)))
1474 for (sv = svars; sv; sv = sv->next)
1475 bitmap_set_bit (addressable_vars, var_ann (sv->var)->uid);
1481 /* Global variables and addressable locals may be aliased. Create an
1482 entry in ADDRESSABLE_VARS for VAR. */
1483 if (may_be_aliased (var))
1485 create_alias_map_for (var, ai);
1486 mark_sym_for_renaming (var);
1489 /* Add pointer variables that have been dereferenced to the POINTERS
1490 array and create a type memory tag for them. */
1491 if (POINTER_TYPE_P (TREE_TYPE (var)))
1493 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1494 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1499 /* If pointer VAR still doesn't have a memory tag
1500 associated with it, create it now or re-use an
1502 tag = get_tmt_for (var, ai);
1503 t_ann = var_ann (tag);
1505 /* The type tag will need to be renamed into SSA
1506 afterwards. Note that we cannot do this inside
1507 get_tmt_for because aliasing may run multiple times
1508 and we only create type tags the first time. */
1509 mark_sym_for_renaming (tag);
1511 /* Similarly, if pointer VAR used to have another type
1512 tag, we will need to process it in the renamer to
1513 remove the stale virtual operands. */
1514 if (v_ann->type_mem_tag)
1515 mark_sym_for_renaming (v_ann->type_mem_tag);
1517 /* Associate the tag with pointer VAR. */
1518 v_ann->type_mem_tag = tag;
1520 /* If pointer VAR has been used in a store operation,
1521 then its memory tag must be marked as written-to. */
1522 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1523 bitmap_set_bit (ai->written_vars, t_ann->uid);
1525 /* If pointer VAR is a global variable or a PARM_DECL,
1526 then its memory tag should be considered a global
1528 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1529 mark_call_clobbered (tag);
1531 /* All the dereferences of pointer VAR count as
1532 references of TAG. Since TAG can be associated with
1533 several pointers, add the dereferences of VAR to the
1534 TAG. We may need to grow AI->NUM_REFERENCES because
1535 we have been adding name and type tags. */
1536 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1537 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1539 VARRAY_UINT (ai->num_references, t_ann->uid)
1540 += VARRAY_UINT (ai->num_references, v_ann->uid);
1544 /* The pointer has not been dereferenced. If it had a
1545 type memory tag, remove it and mark the old tag for
1546 renaming to remove it out of the IL. */
1547 var_ann_t ann = var_ann (var);
1548 tree tag = ann->type_mem_tag;
1551 mark_sym_for_renaming (tag);
1552 ann->type_mem_tag = NULL_TREE;
1560 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1561 every call site, we need to emit V_MAY_DEF expressions to represent the
1562 clobbering effects of the call for variables whose address escapes the
1565 One approach is to group all call-clobbered variables into a single
1566 representative that is used as an alias of every call-clobbered variable
1567 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1568 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1570 The second approach is to emit a clobbering V_MAY_DEF for every
1571 call-clobbered variable at call sites. This is the preferred way in terms
1572 of optimization opportunities but it may create too many V_MAY_DEF operands
1573 if there are many call clobbered variables and function calls in the
1576 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1577 function calls found by the number of call-clobbered variables. If that
1578 product is beyond a certain threshold, as determined by the parameterized
1579 values shown below, we use .GLOBAL_VAR.
1581 FIXME. This heuristic should be improved. One idea is to use several
1582 .GLOBAL_VARs of different types instead of a single one. The thresholds
1583 have been derived from a typical bootstrap cycle, including all target
1584 libraries. Compile times were found increase by ~1% compared to using
1588 maybe_create_global_var (struct alias_info *ai)
1590 unsigned i, n_clobbered;
1593 /* No need to create it, if we have one already. */
1594 if (global_var == NULL_TREE)
1596 /* Count all the call-clobbered variables. */
1598 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1603 /* If the number of virtual operands that would be needed to
1604 model all the call-clobbered variables is larger than
1605 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1607 Also create .GLOBAL_VAR if there are no call-clobbered
1608 variables and the program contains a mixture of pure/const
1609 and regular function calls. This is to avoid the problem
1610 described in PR 20115:
1613 int func_pure (void) { return X; }
1614 int func_non_pure (int a) { X += a; }
1617 int a = func_pure ();
1623 Since foo() has no call-clobbered variables, there is
1624 no relationship between the calls to func_pure and
1625 func_non_pure. Since func_pure has no side-effects, value
1626 numbering optimizations elide the second call to func_pure.
1627 So, if we have some pure/const and some regular calls in the
1628 program we create .GLOBAL_VAR to avoid missing these
1630 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1631 || (n_clobbered == 0
1632 && ai->num_calls_found > 0
1633 && ai->num_pure_const_calls_found > 0
1634 && ai->num_calls_found > ai->num_pure_const_calls_found))
1635 create_global_var ();
1638 /* Mark all call-clobbered symbols for renaming. Since the initial
1639 rewrite into SSA ignored all call sites, we may need to rename
1640 .GLOBAL_VAR and the call-clobbered variables. */
1641 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1643 tree var = referenced_var (i);
1645 /* If the function has calls to clobbering functions and
1646 .GLOBAL_VAR has been created, make it an alias for all
1647 call-clobbered variables. */
1648 if (global_var && var != global_var)
1651 add_may_alias (var, global_var);
1652 if (var_can_have_subvars (var)
1653 && (svars = get_subvars_for_var (var)))
1656 for (sv = svars; sv; sv = sv->next)
1657 mark_sym_for_renaming (sv->var);
1661 mark_sym_for_renaming (var);
1666 /* Return TRUE if pointer PTR may point to variable VAR.
1668 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1669 This is needed because when checking for type conflicts we are
1670 interested in the alias set of the memory location pointed-to by
1671 PTR. The alias set of PTR itself is irrelevant.
1673 VAR_ALIAS_SET is the alias set for VAR. */
1676 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1677 tree var, HOST_WIDE_INT var_alias_set)
1682 alias_stats.alias_queries++;
1683 alias_stats.simple_queries++;
1685 /* By convention, a variable cannot alias itself. */
1686 mem = var_ann (ptr)->type_mem_tag;
1689 alias_stats.alias_noalias++;
1690 alias_stats.simple_resolved++;
1694 /* If -fargument-noalias-global is >1, pointer arguments may
1695 not point to global variables. */
1696 if (flag_argument_noalias > 1 && is_global_var (var)
1697 && TREE_CODE (ptr) == PARM_DECL)
1699 alias_stats.alias_noalias++;
1700 alias_stats.simple_resolved++;
1704 /* If either MEM or VAR is a read-only global and the other one
1705 isn't, then PTR cannot point to VAR. */
1706 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1707 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1709 alias_stats.alias_noalias++;
1710 alias_stats.simple_resolved++;
1714 m_ann = var_ann (mem);
1716 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1718 alias_stats.tbaa_queries++;
1720 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1721 PTR can't possibly affect VAR. Note, that we are specifically testing
1722 for PTR's alias set here, not its pointed-to type. We also can't
1723 do this check with relaxed aliasing enabled. */
1724 if (POINTER_TYPE_P (TREE_TYPE (var))
1725 && var_alias_set != 0
1726 && mem_alias_set != 0)
1728 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1729 if (ptr_alias_set == var_alias_set)
1731 alias_stats.alias_noalias++;
1732 alias_stats.tbaa_resolved++;
1737 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1738 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1740 alias_stats.alias_noalias++;
1741 alias_stats.tbaa_resolved++;
1744 alias_stats.alias_mayalias++;
1749 /* Add ALIAS to the set of variables that may alias VAR. */
1752 add_may_alias (tree var, tree alias)
1755 var_ann_t v_ann = get_var_ann (var);
1756 var_ann_t a_ann = get_var_ann (alias);
1758 gcc_assert (var != alias);
1760 if (v_ann->may_aliases == NULL)
1761 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1763 /* Avoid adding duplicates. */
1764 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1765 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1768 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1769 FIXME, call-clobbering should only depend on whether an address
1770 escapes. It should be independent of aliasing. */
1771 if (is_call_clobbered (var))
1772 mark_call_clobbered (alias);
1774 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1775 else if (is_call_clobbered (alias))
1776 mark_call_clobbered (var);
1778 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1779 a_ann->is_alias_tag = 1;
1783 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1786 replace_may_alias (tree var, size_t i, tree new_alias)
1788 var_ann_t v_ann = var_ann (var);
1789 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1791 /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1792 FIXME, call-clobbering should only depend on whether an address
1793 escapes. It should be independent of aliasing. */
1794 if (is_call_clobbered (var))
1795 mark_call_clobbered (new_alias);
1797 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1798 else if (is_call_clobbered (new_alias))
1799 mark_call_clobbered (var);
1803 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1806 set_pt_anything (tree ptr)
1808 struct ptr_info_def *pi = get_ptr_info (ptr);
1810 pi->pt_anything = 1;
1813 /* The pointer used to have a name tag, but we now found it pointing
1814 to an arbitrary location. The name tag needs to be renamed and
1815 disassociated from PTR. */
1816 if (pi->name_mem_tag)
1818 mark_sym_for_renaming (pi->name_mem_tag);
1819 pi->name_mem_tag = NULL_TREE;
1824 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1827 set_pt_malloc (tree ptr)
1829 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1831 /* If the pointer has already been found to point to arbitrary
1832 memory locations, it is unsafe to mark it as pointing to malloc. */
1833 if (pi->pt_anything)
1840 /* Given two different pointers DEST and ORIG. Merge the points-to
1841 information in ORIG into DEST. AI contains all the alias
1842 information collected up to this point. */
1845 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1847 struct ptr_info_def *dest_pi, *orig_pi;
1849 gcc_assert (dest != orig);
1851 /* Make sure we have points-to information for ORIG. */
1852 collect_points_to_info_for (ai, orig);
1854 dest_pi = get_ptr_info (dest);
1855 orig_pi = SSA_NAME_PTR_INFO (orig);
1859 gcc_assert (orig_pi != dest_pi);
1861 /* Notice that we never merge PT_MALLOC. This attribute is only
1862 true if the pointer is the result of a malloc() call.
1863 Otherwise, we can end up in this situation:
1869 P_j would be marked as PT_MALLOC, however we currently do not
1870 handle cases of more than one pointer pointing to the same
1873 FIXME: If the merging comes from an expression that preserves
1874 the PT_MALLOC attribute (copy assignment, address
1875 arithmetic), we ought to merge PT_MALLOC, but then both
1876 pointers would end up getting different name tags because
1877 create_name_tags is not smart enough to determine that the
1878 two come from the same malloc call. Copy propagation before
1879 aliasing should cure this. */
1880 dest_pi->pt_malloc = 0;
1881 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1882 set_pt_anything (dest);
1884 dest_pi->pt_null |= orig_pi->pt_null;
1886 if (!dest_pi->pt_anything
1888 && !bitmap_empty_p (orig_pi->pt_vars))
1890 if (dest_pi->pt_vars == NULL)
1892 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1893 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1896 bitmap_ior_into (dest_pi->pt_vars, orig_pi->pt_vars);
1900 set_pt_anything (dest);
1904 /* Add EXPR to the list of expressions pointed-to by PTR. */
1907 add_pointed_to_expr (struct alias_info *ai, tree ptr, tree expr)
1909 if (TREE_CODE (expr) == WITH_SIZE_EXPR)
1910 expr = TREE_OPERAND (expr, 0);
1914 if (TREE_CODE (expr) == CALL_EXPR
1915 && (call_expr_flags (expr) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1917 /* If EXPR is a malloc-like call, then the area pointed to PTR
1918 is guaranteed to not alias with anything else. */
1919 set_pt_malloc (ptr);
1921 else if (TREE_CODE (expr) == ADDR_EXPR)
1923 /* Found P_i = ADDR_EXPR */
1924 add_pointed_to_var (ai, ptr, expr);
1926 else if (TREE_CODE (expr) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (expr)))
1928 /* Found P_i = Q_j. */
1929 merge_pointed_to_info (ai, ptr, expr);
1931 else if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
1933 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1934 tree op0 = TREE_OPERAND (expr, 0);
1935 tree op1 = TREE_OPERAND (expr, 1);
1937 /* Both operands may be of pointer type. FIXME: Shouldn't
1938 we just expect PTR + OFFSET always? */
1939 if (POINTER_TYPE_P (TREE_TYPE (op0))
1940 && TREE_CODE (op0) != INTEGER_CST)
1942 if (TREE_CODE (op0) == SSA_NAME)
1943 merge_pointed_to_info (ai, ptr, op0);
1944 else if (TREE_CODE (op0) == ADDR_EXPR)
1945 add_pointed_to_var (ai, ptr, op0);
1947 set_pt_anything (ptr);
1950 if (POINTER_TYPE_P (TREE_TYPE (op1))
1951 && TREE_CODE (op1) != INTEGER_CST)
1953 if (TREE_CODE (op1) == SSA_NAME)
1954 merge_pointed_to_info (ai, ptr, op1);
1955 else if (TREE_CODE (op1) == ADDR_EXPR)
1956 add_pointed_to_var (ai, ptr, op1);
1958 set_pt_anything (ptr);
1961 /* Neither operand is a pointer? VAR can be pointing anywhere.
1962 FIXME: Shouldn't we abort here? If we get here, we found
1963 PTR = INT_CST + INT_CST, which should not be a valid pointer
1965 if (!(POINTER_TYPE_P (TREE_TYPE (op0))
1966 && TREE_CODE (op0) != INTEGER_CST)
1967 && !(POINTER_TYPE_P (TREE_TYPE (op1))
1968 && TREE_CODE (op1) != INTEGER_CST))
1969 set_pt_anything (ptr);
1971 else if (integer_zerop (expr))
1973 /* EXPR is the NULL pointer. Mark PTR as pointing to NULL. */
1974 SSA_NAME_PTR_INFO (ptr)->pt_null = 1;
1978 /* If we can't recognize the expression, assume that PTR may
1980 set_pt_anything (ptr);
1985 /* If VALUE is of the form &DECL, add DECL to the set of variables
1986 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1987 PTR. AI points to the collected alias information. */
1990 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1992 struct ptr_info_def *pi = get_ptr_info (ptr);
1993 tree pt_var = NULL_TREE;
1994 HOST_WIDE_INT offset, size;
2000 gcc_assert (TREE_CODE (value) == ADDR_EXPR);
2002 addrop = TREE_OPERAND (value, 0);
2003 if (REFERENCE_CLASS_P (addrop))
2004 pt_var = get_base_address (addrop);
2008 /* If this is a component_ref, see if we can get a smaller number of
2009 variables to take the address of. */
2010 if (TREE_CODE (addrop) == COMPONENT_REF
2011 && (ref = okay_component_ref_for_subvars (addrop, &offset ,&size)))
2014 svars = get_subvars_for_var (ref);
2016 uid = var_ann (pt_var)->uid;
2018 if (pi->pt_vars == NULL)
2019 pi->pt_vars = BITMAP_GGC_ALLOC ();
2020 /* If the variable is a global, mark the pointer as pointing to
2021 global memory (which will make its tag a global variable). */
2022 if (is_global_var (pt_var))
2023 pi->pt_global_mem = 1;
2025 for (sv = svars; sv; sv = sv->next)
2027 if (overlap_subvar (offset, size, sv, NULL))
2029 bitmap_set_bit (pi->pt_vars, var_ann (sv->var)->uid);
2030 bitmap_set_bit (ai->addresses_needed, var_ann (sv->var)->uid);
2034 else if (pt_var && SSA_VAR_P (pt_var))
2037 uid = var_ann (pt_var)->uid;
2039 if (pi->pt_vars == NULL)
2040 pi->pt_vars = BITMAP_GGC_ALLOC ();
2042 /* If this is an aggregate, we may have subvariables for it that need
2043 to be pointed to. */
2044 if (var_can_have_subvars (pt_var)
2045 && (svars = get_subvars_for_var (pt_var)))
2048 for (sv = svars; sv; sv = sv->next)
2050 uid = var_ann (sv->var)->uid;
2051 bitmap_set_bit (ai->addresses_needed, uid);
2052 bitmap_set_bit (pi->pt_vars, uid);
2057 bitmap_set_bit (ai->addresses_needed, uid);
2058 bitmap_set_bit (pi->pt_vars, uid);
2061 /* If the variable is a global, mark the pointer as pointing to
2062 global memory (which will make its tag a global variable). */
2063 if (is_global_var (pt_var))
2064 pi->pt_global_mem = 1;
2069 /* Callback for walk_use_def_chains to gather points-to information from the
2072 VAR is an SSA variable or a GIMPLE expression.
2074 STMT is the statement that generates the SSA variable or, if STMT is a
2075 PHI_NODE, VAR is one of the PHI arguments.
2077 DATA is a pointer to a structure of type ALIAS_INFO. */
2080 collect_points_to_info_r (tree var, tree stmt, void *data)
2082 struct alias_info *ai = (struct alias_info *) data;
2084 if (dump_file && (dump_flags & TDF_DETAILS))
2086 fprintf (dump_file, "Visiting use-def links for ");
2087 print_generic_expr (dump_file, var, dump_flags);
2088 fprintf (dump_file, "\n");
2091 switch (TREE_CODE (stmt))
2094 gcc_assert (TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR);
2095 stmt = TREE_OPERAND (stmt, 0);
2100 tree rhs = TREE_OPERAND (stmt, 1);
2102 add_pointed_to_expr (ai, var, rhs);
2107 /* Pointers defined by __asm__ statements can point anywhere. */
2108 set_pt_anything (var);
2112 if (IS_EMPTY_STMT (stmt))
2114 tree decl = SSA_NAME_VAR (var);
2116 if (TREE_CODE (decl) == PARM_DECL)
2117 add_pointed_to_expr (ai, var, decl);
2118 else if (DECL_INITIAL (decl))
2119 add_pointed_to_expr (ai, var, DECL_INITIAL (decl));
2121 add_pointed_to_expr (ai, var, decl);
2127 /* It STMT is a PHI node, then VAR is one of its arguments. The
2128 variable that we are analyzing is the LHS of the PHI node. */
2129 tree lhs = PHI_RESULT (stmt);
2131 switch (TREE_CODE (var))
2134 add_pointed_to_var (ai, lhs, var);
2138 /* Avoid unnecessary merges. */
2140 merge_pointed_to_info (ai, lhs, var);
2144 gcc_assert (is_gimple_min_invariant (var));
2145 add_pointed_to_expr (ai, lhs, var);
2159 /* Return true if STMT is an "escape" site from the current function. Escape
2160 sites those statements which might expose the address of a variable
2161 outside the current function. STMT is an escape site iff:
2163 1- STMT is a function call, or
2164 2- STMT is an __asm__ expression, or
2165 3- STMT is an assignment to a non-local variable, or
2166 4- STMT is a return statement.
2168 AI points to the alias information collected so far. */
2171 is_escape_site (tree stmt, struct alias_info *ai)
2173 tree call = get_call_expr_in (stmt);
2174 if (call != NULL_TREE)
2176 ai->num_calls_found++;
2178 if (!TREE_SIDE_EFFECTS (call))
2179 ai->num_pure_const_calls_found++;
2183 else if (TREE_CODE (stmt) == ASM_EXPR)
2185 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2187 tree lhs = TREE_OPERAND (stmt, 0);
2189 /* Get to the base of _REF nodes. */
2190 if (TREE_CODE (lhs) != SSA_NAME)
2191 lhs = get_base_address (lhs);
2193 /* If we couldn't recognize the LHS of the assignment, assume that it
2194 is a non-local store. */
2195 if (lhs == NULL_TREE)
2198 /* If the RHS is a conversion between a pointer and an integer, the
2199 pointer escapes since we can't track the integer. */
2200 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2201 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2202 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2203 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
2204 (TREE_OPERAND (stmt, 1), 0)))
2205 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
2208 /* If the LHS is an SSA name, it can't possibly represent a non-local
2210 if (TREE_CODE (lhs) == SSA_NAME)
2213 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2214 local variables we cannot be sure if it will escape, because we
2215 don't have information about objects not in SSA form. Need to
2216 implement something along the lines of
2218 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2219 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2220 Conference on Object-Oriented Programming Systems, Languages, and
2221 Applications (OOPSLA), pp. 1-19, 1999. */
2224 else if (TREE_CODE (stmt) == RETURN_EXPR)
2231 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2232 is considered to represent all the pointers whose pointed-to types are
2233 in the same alias set class. Otherwise, the tag represents a single
2234 SSA_NAME pointer variable. */
2237 create_memory_tag (tree type, bool is_type_tag)
2240 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2242 /* By default, memory tags are local variables. Alias analysis will
2243 determine whether they should be considered globals. */
2244 DECL_CONTEXT (tag) = current_function_decl;
2246 /* Memory tags are by definition addressable. This also prevents
2247 is_gimple_ref frome confusing memory tags with optimizable
2249 TREE_ADDRESSABLE (tag) = 1;
2251 ann = get_var_ann (tag);
2252 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2253 ann->type_mem_tag = NULL_TREE;
2255 /* Add the tag to the symbol table. */
2256 add_referenced_tmp_var (tag);
2262 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2263 This is used if P_i has been found to point to a specific set of
2264 variables or to a non-aliased memory location like the address returned
2265 by malloc functions. */
2268 get_nmt_for (tree ptr)
2270 struct ptr_info_def *pi = get_ptr_info (ptr);
2271 tree tag = pi->name_mem_tag;
2273 if (tag == NULL_TREE)
2274 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2276 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
2277 then its name tag should be considered a global variable. */
2278 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
2280 || pi->pt_global_mem)
2281 mark_call_clobbered (tag);
2287 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2288 artificial variable that represents the memory location pointed-to by
2289 PTR. It is used to model the effects of pointer de-references on
2290 addressable variables.
2292 AI points to the data gathered during alias analysis. This function
2293 populates the array AI->POINTERS. */
2296 get_tmt_for (tree ptr, struct alias_info *ai)
2300 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2301 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2303 /* To avoid creating unnecessary memory tags, only create one memory tag
2304 per alias set class. Note that it may be tempting to group
2305 memory tags based on conflicting alias sets instead of
2306 equivalence. That would be wrong because alias sets are not
2307 necessarily transitive (as demonstrated by the libstdc++ test
2308 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2309 such that conflicts (A, B) == true and conflicts (A, C) == true,
2310 it does not necessarily follow that conflicts (B, C) == true. */
2311 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2313 struct alias_map_d *curr = ai->pointers[i];
2314 tree curr_tag = var_ann (curr->var)->type_mem_tag;
2315 if (tag_set == curr->set
2316 && TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (curr_tag)))
2323 /* If VAR cannot alias with any of the existing memory tags, create a new
2324 tag for PTR and add it to the POINTERS array. */
2325 if (tag == NULL_TREE)
2327 struct alias_map_d *alias_map;
2329 /* If PTR did not have a type tag already, create a new TMT.*
2330 artificial variable representing the memory location
2331 pointed-to by PTR. */
2332 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2333 tag = create_memory_tag (tag_type, true);
2335 tag = var_ann (ptr)->type_mem_tag;
2337 /* Add PTR to the POINTERS array. Note that we are not interested in
2338 PTR's alias set. Instead, we cache the alias set for the memory that
2340 alias_map = xcalloc (1, sizeof (*alias_map));
2341 alias_map->var = ptr;
2342 alias_map->set = tag_set;
2343 ai->pointers[ai->num_pointers++] = alias_map;
2346 /* If the pointed-to type is volatile, so is the tag. */
2347 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2349 /* Make sure that the type tag has the same alias set as the
2351 gcc_assert (tag_set == get_alias_set (tag));
2353 /* If PTR's pointed-to type is read-only, then TAG's type must also
2355 gcc_assert (TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (tag)));
2361 /* Create GLOBAL_VAR, an artificial global variable to act as a
2362 representative of all the variables that may be clobbered by function
2366 create_global_var (void)
2368 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2370 DECL_ARTIFICIAL (global_var) = 1;
2371 TREE_READONLY (global_var) = 0;
2372 DECL_EXTERNAL (global_var) = 1;
2373 TREE_STATIC (global_var) = 1;
2374 TREE_USED (global_var) = 1;
2375 DECL_CONTEXT (global_var) = NULL_TREE;
2376 TREE_THIS_VOLATILE (global_var) = 0;
2377 TREE_ADDRESSABLE (global_var) = 0;
2379 add_referenced_tmp_var (global_var);
2380 mark_sym_for_renaming (global_var);
2384 /* Dump alias statistics on FILE. */
2387 dump_alias_stats (FILE *file)
2389 const char *funcname
2390 = lang_hooks.decl_printable_name (current_function_decl, 2);
2391 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2392 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2393 fprintf (file, "Total alias mayalias results:\t%u\n",
2394 alias_stats.alias_mayalias);
2395 fprintf (file, "Total alias noalias results:\t%u\n",
2396 alias_stats.alias_noalias);
2397 fprintf (file, "Total simple queries:\t%u\n",
2398 alias_stats.simple_queries);
2399 fprintf (file, "Total simple resolved:\t%u\n",
2400 alias_stats.simple_resolved);
2401 fprintf (file, "Total TBAA queries:\t%u\n",
2402 alias_stats.tbaa_queries);
2403 fprintf (file, "Total TBAA resolved:\t%u\n",
2404 alias_stats.tbaa_resolved);
2408 /* Dump alias information on FILE. */
2411 dump_alias_info (FILE *file)
2414 const char *funcname
2415 = lang_hooks.decl_printable_name (current_function_decl, 2);
2417 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2419 fprintf (file, "Aliased symbols\n\n");
2420 for (i = 0; i < num_referenced_vars; i++)
2422 tree var = referenced_var (i);
2423 if (may_be_aliased (var))
2424 dump_variable (file, var);
2427 fprintf (file, "\nDereferenced pointers\n\n");
2428 for (i = 0; i < num_referenced_vars; i++)
2430 tree var = referenced_var (i);
2431 var_ann_t ann = var_ann (var);
2432 if (ann->type_mem_tag)
2433 dump_variable (file, var);
2436 fprintf (file, "\nType memory tags\n\n");
2437 for (i = 0; i < num_referenced_vars; i++)
2439 tree var = referenced_var (i);
2440 var_ann_t ann = var_ann (var);
2441 if (ann->mem_tag_kind == TYPE_TAG)
2442 dump_variable (file, var);
2445 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2447 fprintf (file, "SSA_NAME pointers\n\n");
2448 for (i = 1; i < num_ssa_names; i++)
2450 tree ptr = ssa_name (i);
2451 struct ptr_info_def *pi;
2453 if (ptr == NULL_TREE)
2456 pi = SSA_NAME_PTR_INFO (ptr);
2457 if (!SSA_NAME_IN_FREE_LIST (ptr)
2459 && pi->name_mem_tag)
2460 dump_points_to_info_for (file, ptr);
2463 fprintf (file, "\nName memory tags\n\n");
2464 for (i = 0; i < num_referenced_vars; i++)
2466 tree var = referenced_var (i);
2467 var_ann_t ann = var_ann (var);
2468 if (ann->mem_tag_kind == NAME_TAG)
2469 dump_variable (file, var);
2472 fprintf (file, "\n");
2476 /* Dump alias information on stderr. */
2479 debug_alias_info (void)
2481 dump_alias_info (stderr);
2485 /* Return the alias information associated with pointer T. It creates a
2486 new instance if none existed. */
2488 struct ptr_info_def *
2489 get_ptr_info (tree t)
2491 struct ptr_info_def *pi;
2493 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2495 pi = SSA_NAME_PTR_INFO (t);
2498 pi = ggc_alloc (sizeof (*pi));
2499 memset ((void *)pi, 0, sizeof (*pi));
2500 SSA_NAME_PTR_INFO (t) = pi;
2507 /* Dump points-to information for SSA_NAME PTR into FILE. */
2510 dump_points_to_info_for (FILE *file, tree ptr)
2512 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2514 print_generic_expr (file, ptr, dump_flags);
2518 if (pi->name_mem_tag)
2520 fprintf (file, ", name memory tag: ");
2521 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2524 if (pi->is_dereferenced)
2525 fprintf (file, ", is dereferenced");
2527 if (pi->value_escapes_p)
2528 fprintf (file, ", its value escapes");
2530 if (pi->pt_anything)
2531 fprintf (file, ", points-to anything");
2534 fprintf (file, ", points-to malloc");
2537 fprintf (file, ", points-to NULL");
2544 fprintf (file, ", points-to vars: { ");
2545 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2547 print_generic_expr (file, referenced_var (ix), dump_flags);
2548 fprintf (file, " ");
2550 fprintf (file, "}");
2554 fprintf (file, "\n");
2558 /* Dump points-to information for VAR into stderr. */
2561 debug_points_to_info_for (tree var)
2563 dump_points_to_info_for (stderr, var);
2567 /* Dump points-to information into FILE. NOTE: This function is slow, as
2568 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2571 dump_points_to_info (FILE *file)
2574 block_stmt_iterator si;
2578 lang_hooks.decl_printable_name (current_function_decl, 2);
2580 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2582 /* First dump points-to information for the default definitions of
2583 pointer variables. This is necessary because default definitions are
2584 not part of the code. */
2585 for (i = 0; i < num_referenced_vars; i++)
2587 tree var = referenced_var (i);
2588 if (POINTER_TYPE_P (TREE_TYPE (var)))
2590 var_ann_t ann = var_ann (var);
2591 if (ann->default_def)
2592 dump_points_to_info_for (file, ann->default_def);
2596 /* Dump points-to information for every pointer defined in the program. */
2601 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2603 tree ptr = PHI_RESULT (phi);
2604 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2605 dump_points_to_info_for (file, ptr);
2608 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2610 tree stmt = bsi_stmt (si);
2612 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2613 if (POINTER_TYPE_P (TREE_TYPE (def)))
2614 dump_points_to_info_for (file, def);
2618 fprintf (file, "\n");
2622 /* Dump points-to info pointed by PTO into STDERR. */
2625 debug_points_to_info (void)
2627 dump_points_to_info (stderr);
2630 /* Dump to FILE the list of variables that may be aliasing VAR. */
2633 dump_may_aliases_for (FILE *file, tree var)
2635 varray_type aliases;
2637 if (TREE_CODE (var) == SSA_NAME)
2638 var = SSA_NAME_VAR (var);
2640 aliases = var_ann (var)->may_aliases;
2644 fprintf (file, "{ ");
2645 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2647 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2648 fprintf (file, " ");
2650 fprintf (file, "}");
2655 /* Dump to stderr the list of variables that may be aliasing VAR. */
2658 debug_may_aliases_for (tree var)
2660 dump_may_aliases_for (stderr, var);
2663 /* Return true if VAR may be aliased. */
2666 may_be_aliased (tree var)
2669 if (TREE_ADDRESSABLE (var))
2672 /* Globally visible variables can have their addresses taken by other
2673 translation units. */
2674 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2677 /* Automatic variables can't have their addresses escape any other way.
2678 This must be after the check for global variables, as extern declarations
2679 do not have TREE_STATIC set. */
2680 if (!TREE_STATIC (var))
2683 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2684 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2685 we can only be sure the variable isn't addressable if it's local to the
2686 current function. */
2687 if (flag_unit_at_a_time)
2689 if (decl_function_context (var) == current_function_decl)
2696 /* Add VAR to the list of may-aliases of PTR's type tag. If PTR
2697 doesn't already have a type tag, create one. */
2700 add_type_alias (tree ptr, tree var)
2702 varray_type aliases;
2704 var_ann_t ann = var_ann (ptr);
2707 if (ann->type_mem_tag == NULL_TREE)
2711 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2712 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2714 /* PTR doesn't have a type tag, create a new one and add VAR to
2715 the new tag's alias set.
2717 FIXME, This is slower than necessary. We need to determine
2718 whether there is another pointer Q with the same alias set as
2719 PTR. This could be sped up by having type tags associated
2721 for (i = 0; i < num_referenced_vars; i++)
2723 q = referenced_var (i);
2725 if (POINTER_TYPE_P (TREE_TYPE (q))
2726 && tag_set == get_alias_set (TREE_TYPE (TREE_TYPE (q))))
2728 /* Found another pointer Q with the same alias set as
2729 the PTR's pointed-to type. If Q has a type tag, use
2730 it. Otherwise, create a new memory tag for PTR. */
2731 var_ann_t ann1 = var_ann (q);
2732 if (ann1->type_mem_tag)
2733 ann->type_mem_tag = ann1->type_mem_tag;
2735 ann->type_mem_tag = create_memory_tag (tag_type, true);
2740 /* Couldn't find any other pointer with a type tag we could use.
2741 Create a new memory tag for PTR. */
2742 ann->type_mem_tag = create_memory_tag (tag_type, true);
2746 /* If VAR is not already PTR's type tag, add it to the may-alias set
2747 for PTR's type tag. */
2748 gcc_assert (var_ann (var)->type_mem_tag == NOT_A_TAG);
2749 tag = ann->type_mem_tag;
2751 /* If VAR has subvars, add the subvars to the tag instead of the
2753 if (var_can_have_subvars (var)
2754 && (svars = get_subvars_for_var (var)))
2757 for (sv = svars; sv; sv = sv->next)
2758 add_may_alias (tag, sv->var);
2761 add_may_alias (tag, var);
2763 /* TAG and its set of aliases need to be marked for renaming. */
2764 mark_sym_for_renaming (tag);
2765 if ((aliases = var_ann (tag)->may_aliases) != NULL)
2768 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2769 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2772 /* If we had grouped aliases, VAR may have aliases of its own. Mark
2773 them for renaming as well. Other statements referencing the
2774 aliases of VAR will need to be updated. */
2775 if ((aliases = var_ann (var)->may_aliases) != NULL)
2778 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2779 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2784 /* This structure is simply used during pushing fields onto the fieldstack
2785 to track the offset of the field, since bitpos_of_field gives it relative
2786 to its immediate containing type, and we want it relative to the ultimate
2787 containing object. */
2789 typedef struct fieldoff
2792 HOST_WIDE_INT offset;
2795 DEF_VEC_MALLOC_P(fieldoff_t);
2797 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2799 Return -1 if the position is conditional or otherwise non-constant
2802 static HOST_WIDE_INT
2803 bitpos_of_field (const tree fdecl)
2806 if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2807 || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2810 return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2811 + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2814 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all the fields
2815 of TYPE onto fieldstack, recording their offsets along the way.
2816 OFFSET is used to keep track of the offset in this entire structure, rather
2817 than just the immediately containing structure. */
2820 push_fields_onto_fieldstack (tree type, VEC(fieldoff_t) **fieldstack,
2821 HOST_WIDE_INT offset)
2824 tree field = TYPE_FIELDS (type);
2827 if (var_can_have_subvars (field)
2828 && TREE_CODE (field) == FIELD_DECL)
2830 size_t before = VEC_length (fieldoff_t, *fieldstack);
2831 /* Empty structures may have actual size, like in C++. So see if we
2832 actually end up pushing a field, and if not, if the size is nonzero,
2833 push the field onto the stack */
2834 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack, offset);
2835 if (before == VEC_length (fieldoff_t, *fieldstack)
2836 && DECL_SIZE (field)
2837 && !integer_zerop (DECL_SIZE (field)))
2839 pair = xmalloc (sizeof (struct fieldoff));
2840 pair->field = field;
2841 pair->offset = offset;
2842 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2845 else if (TREE_CODE (field) == FIELD_DECL)
2847 pair = xmalloc (sizeof (struct fieldoff));
2848 pair->field = field;
2849 pair->offset = offset + bitpos_of_field (field);
2850 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2852 for (field = TREE_CHAIN (field); field; field = TREE_CHAIN (field))
2854 if (TREE_CODE (field) != FIELD_DECL)
2856 if (var_can_have_subvars (field))
2858 size_t before = VEC_length (fieldoff_t, *fieldstack);
2859 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack,
2860 offset + bitpos_of_field (field));
2861 /* Empty structures may have actual size, like in C++. So see if we
2862 actually end up pushing a field, and if not, if the size is nonzero,
2863 push the field onto the stack */
2864 if (before == VEC_length (fieldoff_t, *fieldstack)
2865 && DECL_SIZE (field)
2866 && !integer_zerop (DECL_SIZE (field)))
2868 pair = xmalloc (sizeof (struct fieldoff));
2869 pair->field = field;
2870 pair->offset = offset + bitpos_of_field (field);
2871 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2876 pair = xmalloc (sizeof (struct fieldoff));
2877 pair->field = field;
2878 pair->offset = offset + bitpos_of_field (field);
2879 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2885 /* This represents the used range of a variable. */
2887 typedef struct used_part
2889 HOST_WIDE_INT minused;
2890 HOST_WIDE_INT maxused;
2891 /* True if we have an explicit use/def of some portion of this variable,
2892 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2894 /* True if we have an implicit use/def of some portion of this
2895 variable. Implicit uses occur when we can't tell what part we
2896 are referencing, and have to make conservative assumptions. */
2900 /* An array of used_part structures, indexed by variable uid. */
2902 static used_part_t *used_portions;
2904 /* Given a variable uid, UID, get or create the entry in the used portions
2905 table for the variable. */
2908 get_or_create_used_part_for (size_t uid)
2911 if (used_portions[uid] == NULL)
2913 up = xcalloc (1, sizeof (struct used_part));
2914 up->minused = INT_MAX;
2916 up->explicit_uses = false;
2917 up->implicit_uses = false;
2920 up = used_portions[uid];
2924 /* qsort comparison function for two fieldoff_t's PA and PB */
2927 fieldoff_compare (const void *pa, const void *pb)
2929 const fieldoff_t foa = *(fieldoff_t *)pa;
2930 const fieldoff_t fob = *(fieldoff_t *)pb;
2931 HOST_WIDE_INT foasize, fobsize;
2932 if (foa->offset != fob->offset)
2933 return foa->offset - fob->offset;
2935 foasize = TREE_INT_CST_LOW (DECL_SIZE (foa->field));
2936 fobsize = TREE_INT_CST_LOW (DECL_SIZE (fob->field));
2937 if (foasize != fobsize)
2938 return foasize - fobsize;
2942 /* Given an aggregate VAR, create the subvariables that represent its
2946 create_overlap_variables_for (tree var)
2948 VEC(fieldoff_t) *fieldstack = NULL;
2950 size_t uid = var_ann (var)->uid;
2952 if (used_portions[uid] == NULL)
2955 up = used_portions[uid];
2956 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0);
2957 if (VEC_length (fieldoff_t, fieldstack) != 0)
2961 bool notokay = false;
2964 HOST_WIDE_INT lastfooffset = -1;
2965 HOST_WIDE_INT lastfosize = -1;
2966 tree lastfotype = NULL_TREE;
2968 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2969 know their size, and thus, can't handle.
2970 The same is true of fields with DECL_SIZE that is not an integer
2971 constant (such as variable sized fields).
2972 Fields with offsets which are not constant will have an offset < 0
2973 We *could* handle fields that are constant sized arrays, but
2974 currently don't. Doing so would require some extra changes to
2975 tree-ssa-operands.c. */
2977 for (i = 0; VEC_iterate (fieldoff_t, fieldstack, i, fo); i++)
2979 if (!DECL_SIZE (fo->field)
2980 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
2981 || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
2990 /* The current heuristic we use is as follows:
2991 If the variable has no used portions in this function, no
2992 structure vars are created for it.
2994 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
2995 we always create structure vars for them.
2996 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2997 some explicit uses, we create structure vars for them.
2998 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2999 no explicit uses, we do not create structure vars for them.
3002 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
3003 && !up->explicit_uses)
3005 if (dump_file && (dump_flags & TDF_DETAILS))
3007 fprintf (dump_file, "Variable ");
3008 print_generic_expr (dump_file, var, 0);
3009 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
3015 /* Cleanup after ourselves if we can't create overlap variables. */
3018 while (VEC_length (fieldoff_t, fieldstack) != 0)
3020 fo = VEC_pop (fieldoff_t, fieldstack);
3023 VEC_free (fieldoff_t, fieldstack);
3026 /* Otherwise, create the variables. */
3027 subvars = lookup_subvars_for_var (var);
3029 qsort (VEC_address (fieldoff_t, fieldstack),
3030 VEC_length (fieldoff_t, fieldstack),
3031 sizeof (fieldoff_t),
3034 while (VEC_length (fieldoff_t, fieldstack) != 0)
3037 HOST_WIDE_INT fosize;
3041 fo = VEC_pop (fieldoff_t, fieldstack);
3042 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
3043 currfotype = TREE_TYPE (fo->field);
3045 /* If this field isn't in the used portion,
3046 or it has the exact same offset and size as the last
3049 if (((fo->offset <= up->minused
3050 && fo->offset + fosize <= up->minused)
3051 || fo->offset >= up->maxused)
3052 || (fo->offset == lastfooffset
3053 && fosize == lastfosize
3054 && currfotype == lastfotype))
3059 sv = ggc_alloc (sizeof (struct subvar));
3060 sv->offset = fo->offset;
3062 sv->next = *subvars;
3063 sv->var = create_tmp_var_raw (TREE_TYPE (fo->field), "SFT");
3066 fprintf (dump_file, "structure field tag %s created for var %s",
3067 get_name (sv->var), get_name (var));
3068 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
3070 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
3072 fprintf (dump_file, "\n");
3076 /* We need to copy the various flags from var to sv->var, so that
3077 they are is_global_var iff the original variable was. */
3079 DECL_EXTERNAL (sv->var) = DECL_EXTERNAL (var);
3080 TREE_PUBLIC (sv->var) = TREE_PUBLIC (var);
3081 TREE_STATIC (sv->var) = TREE_STATIC (var);
3082 TREE_READONLY (sv->var) = TREE_READONLY (var);
3084 /* Like other memory tags, these need to be marked addressable to
3085 keep is_gimple_reg from thinking they are real. */
3086 TREE_ADDRESSABLE (sv->var) = 1;
3088 DECL_CONTEXT (sv->var) = DECL_CONTEXT (var);
3090 ann = get_var_ann (sv->var);
3091 ann->mem_tag_kind = STRUCT_FIELD;
3092 ann->type_mem_tag = NULL;
3093 add_referenced_tmp_var (sv->var);
3095 lastfotype = currfotype;
3096 lastfooffset = fo->offset;
3097 lastfosize = fosize;
3102 /* Once we have created subvars, the original is no longer call
3103 clobbered on its own. Its call clobbered status depends
3104 completely on the call clobbered status of the subvars.
3106 add_referenced_var in the above loop will take care of
3107 marking subvars of global variables as call clobbered for us
3108 to start, since they are global as well. */
3109 clear_call_clobbered (var);
3113 VEC_free (fieldoff_t, fieldstack);
3117 /* Find the conservative answer to the question of what portions of what
3118 structures are used by this statement. We assume that if we have a
3119 component ref with a known size + offset, that we only need that part
3120 of the structure. For unknown cases, or cases where we do something
3121 to the whole structure, we assume we need to create fields for the
3122 entire structure. */
3125 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3127 switch (TREE_CODE (*tp))
3131 HOST_WIDE_INT bitsize;
3132 HOST_WIDE_INT bitpos;
3134 enum machine_mode mode;
3138 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
3139 &unsignedp, &volatilep, false);
3140 if (DECL_P (ref) && offset == NULL && bitsize != -1)
3142 size_t uid = var_ann (ref)->uid;
3145 up = get_or_create_used_part_for (uid);
3147 if (bitpos <= up->minused)
3148 up->minused = bitpos;
3149 if ((bitpos + bitsize >= up->maxused))
3150 up->maxused = bitpos + bitsize;
3152 up->explicit_uses = true;
3153 used_portions[uid] = up;
3158 else if (DECL_P (ref))
3161 && var_can_have_subvars (ref)
3162 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
3165 size_t uid = var_ann (ref)->uid;
3167 up = get_or_create_used_part_for (uid);
3170 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
3172 up->implicit_uses = true;
3174 used_portions[uid] = up;
3187 && var_can_have_subvars (var)
3188 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3191 size_t uid = var_ann (var)->uid;
3193 up = get_or_create_used_part_for (uid);
3196 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3197 up->implicit_uses = true;
3199 used_portions[uid] = up;
3213 /* We are about to create some new referenced variables, and we need the
3216 static size_t old_referenced_vars;
3219 /* Create structure field variables for structures used in this function. */
3222 create_structure_vars (void)
3227 old_referenced_vars = num_referenced_vars;
3228 used_portions = xcalloc (num_referenced_vars, sizeof (used_part_t));
3232 block_stmt_iterator bsi;
3233 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3235 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3240 for (i = 0; i < old_referenced_vars; i++)
3242 tree var = referenced_var (i);
3243 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3246 && var_can_have_subvars (var)
3247 && var_ann (var)->mem_tag_kind == NOT_A_TAG
3248 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3249 create_overlap_variables_for (var);
3251 for (i = 0; i < old_referenced_vars; i++)
3252 free (used_portions[i]);
3254 free (used_portions);
3258 gate_structure_vars (void)
3260 return flag_tree_salias != 0;
3263 struct tree_opt_pass pass_create_structure_vars =
3265 "salias", /* name */
3266 gate_structure_vars, /* gate */
3267 create_structure_vars, /* execute */
3270 0, /* static_pass_number */
3272 PROP_cfg, /* properties_required */
3273 0, /* properties_provided */
3274 0, /* properties_destroyed */
3275 0, /* todo_flags_start */
3276 TODO_dump_func, /* todo_flags_finish */