PR c/529
[official-gcc.git] / gcc / tree-ssa-alias.c
blob3b08b23de963d3b3423fb583bd2743022a9c9395
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)
10 any later version.
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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "expr.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "function.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"
43 #include "tree-ssa-structalias.h"
44 #include "convert.h"
45 #include "params.h"
46 #include "ipa-type-escape.h"
47 #include "vec.h"
48 #include "bitmap.h"
50 /* Obstack used to hold grouping bitmaps and other temporary bitmaps used by
51 aliasing */
52 static bitmap_obstack alias_obstack;
54 /* 'true' after aliases have been computed (see compute_may_aliases). */
55 bool aliases_computed_p;
57 /* Structure to map a variable to its alias set and keep track of the
58 virtual operands that will be needed to represent it. */
59 struct alias_map_d
61 /* Variable and its alias set. */
62 tree var;
63 HOST_WIDE_INT set;
65 /* Total number of virtual operands that will be needed to represent
66 all the aliases of VAR. */
67 long total_alias_vops;
69 /* Nonzero if the aliases for this memory tag have been grouped
70 already. Used in group_aliases. */
71 unsigned int grouped_p : 1;
73 /* Set of variables aliased with VAR. This is the exact same
74 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
75 bitmap form to speed up alias grouping. */
76 bitmap may_aliases;
80 /* Counters used to display statistics on alias analysis. */
81 struct alias_stats_d
83 unsigned int alias_queries;
84 unsigned int alias_mayalias;
85 unsigned int alias_noalias;
86 unsigned int simple_queries;
87 unsigned int simple_resolved;
88 unsigned int tbaa_queries;
89 unsigned int tbaa_resolved;
90 unsigned int structnoaddress_queries;
91 unsigned int structnoaddress_resolved;
95 /* Local variables. */
96 static struct alias_stats_d alias_stats;
98 /* Local functions. */
99 static void compute_flow_insensitive_aliasing (struct alias_info *);
100 static void dump_alias_stats (FILE *);
101 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT, bool);
102 static tree create_memory_tag (tree type, bool is_type_tag);
103 static tree get_tmt_for (tree, struct alias_info *);
104 static tree get_nmt_for (tree);
105 static void add_may_alias (tree, tree);
106 static void replace_may_alias (tree, size_t, tree);
107 static struct alias_info *init_alias_info (void);
108 static void delete_alias_info (struct alias_info *);
109 static void compute_flow_sensitive_aliasing (struct alias_info *);
110 static void setup_pointers_and_addressables (struct alias_info *);
111 static void create_global_var (void);
112 static void maybe_create_global_var (struct alias_info *ai);
113 static void group_aliases (struct alias_info *);
114 static void set_pt_anything (tree ptr);
116 /* Global declarations. */
118 /* Call clobbered variables in the function. If bit I is set, then
119 REFERENCED_VARS (I) is call-clobbered. */
120 bitmap call_clobbered_vars;
122 /* Addressable variables in the function. If bit I is set, then
123 REFERENCED_VARS (I) has had its address taken. Note that
124 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
125 addressable variable is not necessarily call-clobbered (e.g., a
126 local addressable whose address does not escape) and not all
127 call-clobbered variables are addressable (e.g., a local static
128 variable). */
129 bitmap addressable_vars;
131 /* When the program has too many call-clobbered variables and call-sites,
132 this variable is used to represent the clobbering effects of function
133 calls. In these cases, all the call clobbered variables in the program
134 are forced to alias this variable. This reduces compile times by not
135 having to keep track of too many V_MAY_DEF expressions at call sites. */
136 tree global_var;
139 /* Compute may-alias information for every variable referenced in function
140 FNDECL.
142 Alias analysis proceeds in 3 main phases:
144 1- Points-to and escape analysis.
146 This phase walks the use-def chains in the SSA web looking for three
147 things:
149 * Assignments of the form P_i = &VAR
150 * Assignments of the form P_i = malloc()
151 * Pointers and ADDR_EXPR that escape the current function.
153 The concept of 'escaping' is the same one used in the Java world. When
154 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
155 outside of the current function. So, assignment to global variables,
156 function arguments and returning a pointer are all escape sites, as are
157 conversions between pointers and integers.
159 This is where we are currently limited. Since not everything is renamed
160 into SSA, we lose track of escape properties when a pointer is stashed
161 inside a field in a structure, for instance. In those cases, we are
162 assuming that the pointer does escape.
164 We use escape analysis to determine whether a variable is
165 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
166 is call-clobbered. If a pointer P_i escapes, then all the variables
167 pointed-to by P_i (and its memory tag) also escape.
169 2- Compute flow-sensitive aliases
171 We have two classes of memory tags. Memory tags associated with the
172 pointed-to data type of the pointers in the program. These tags are
173 called "type memory tag" (TMT). The other class are those associated
174 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
175 when adding operands for an INDIRECT_REF *P_i, we will first check
176 whether P_i has a name tag, if it does we use it, because that will have
177 more precise aliasing information. Otherwise, we use the standard type
178 tag.
180 In this phase, we go through all the pointers we found in points-to
181 analysis and create alias sets for the name memory tags associated with
182 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
183 it points to and its tag.
186 3- Compute flow-insensitive aliases
188 This pass will compare the alias set of every type memory tag and every
189 addressable variable found in the program. Given a type memory tag TMT
190 and an addressable variable V. If the alias sets of TMT and V conflict
191 (as computed by may_alias_p), then V is marked as an alias tag and added
192 to the alias set of TMT.
194 For instance, consider the following function:
196 foo (int i)
198 int *p, a, b;
200 if (i > 10)
201 p = &a;
202 else
203 p = &b;
205 *p = 3;
206 a = b + 2;
207 return *p;
210 After aliasing analysis has finished, the type memory tag for pointer
211 'p' will have two aliases, namely variables 'a' and 'b'. Every time
212 pointer 'p' is dereferenced, we want to mark the operation as a
213 potential reference to 'a' and 'b'.
215 foo (int i)
217 int *p, a, b;
219 if (i_2 > 10)
220 p_4 = &a;
221 else
222 p_6 = &b;
223 # p_1 = PHI <p_4(1), p_6(2)>;
225 # a_7 = V_MAY_DEF <a_3>;
226 # b_8 = V_MAY_DEF <b_5>;
227 *p_1 = 3;
229 # a_9 = V_MAY_DEF <a_7>
230 # VUSE <b_8>
231 a_9 = b_8 + 2;
233 # VUSE <a_9>;
234 # VUSE <b_8>;
235 return *p_1;
238 In certain cases, the list of may aliases for a pointer may grow too
239 large. This may cause an explosion in the number of virtual operands
240 inserted in the code. Resulting in increased memory consumption and
241 compilation time.
243 When the number of virtual operands needed to represent aliased
244 loads and stores grows too large (configurable with @option{--param
245 max-aliased-vops}), alias sets are grouped to avoid severe
246 compile-time slow downs and memory consumption. See group_aliases. */
248 static void
249 compute_may_aliases (void)
251 struct alias_info *ai;
253 memset (&alias_stats, 0, sizeof (alias_stats));
255 /* Initialize aliasing information. */
256 ai = init_alias_info ();
258 /* For each pointer P_i, determine the sets of variables that P_i may
259 point-to. For every addressable variable V, determine whether the
260 address of V escapes the current function, making V call-clobbered
261 (i.e., whether &V is stored in a global variable or if its passed as a
262 function call argument). */
263 compute_points_to_sets (ai);
265 /* Collect all pointers and addressable variables, compute alias sets,
266 create memory tags for pointers and promote variables whose address is
267 not needed anymore. */
268 setup_pointers_and_addressables (ai);
270 /* Compute flow-sensitive, points-to based aliasing for all the name
271 memory tags. Note that this pass needs to be done before flow
272 insensitive analysis because it uses the points-to information
273 gathered before to mark call-clobbered type tags. */
274 compute_flow_sensitive_aliasing (ai);
276 /* Compute type-based flow-insensitive aliasing for all the type
277 memory tags. */
278 compute_flow_insensitive_aliasing (ai);
280 /* If the program has too many call-clobbered variables and/or function
281 calls, create .GLOBAL_VAR and use it to model call-clobbering
282 semantics at call sites. This reduces the number of virtual operands
283 considerably, improving compile times at the expense of lost
284 aliasing precision. */
285 maybe_create_global_var (ai);
287 /* Debugging dumps. */
288 if (dump_file)
290 dump_referenced_vars (dump_file);
291 if (dump_flags & TDF_STATS)
292 dump_alias_stats (dump_file);
293 dump_points_to_info (dump_file);
294 dump_alias_info (dump_file);
297 /* Deallocate memory used by aliasing data structures. */
298 delete_alias_info (ai);
301 block_stmt_iterator bsi;
302 basic_block bb;
303 FOR_EACH_BB (bb)
305 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
307 update_stmt_if_modified (bsi_stmt (bsi));
314 struct tree_opt_pass pass_may_alias =
316 "alias", /* name */
317 NULL, /* gate */
318 compute_may_aliases, /* execute */
319 NULL, /* sub */
320 NULL, /* next */
321 0, /* static_pass_number */
322 TV_TREE_MAY_ALIAS, /* tv_id */
323 PROP_cfg | PROP_ssa, /* properties_required */
324 PROP_alias, /* properties_provided */
325 0, /* properties_destroyed */
326 0, /* todo_flags_start */
327 TODO_dump_func | TODO_update_ssa
328 | TODO_ggc_collect | TODO_verify_ssa
329 | TODO_verify_stmts, /* todo_flags_finish */
330 0 /* letter */
334 /* Data structure used to count the number of dereferences to PTR
335 inside an expression. */
336 struct count_ptr_d
338 tree ptr;
339 unsigned count;
343 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
344 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
346 static tree
347 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
349 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
351 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
352 pointer 'ptr' is *not* dereferenced, it is simply used to compute
353 the address of 'fld' as 'ptr + offsetof(fld)'. */
354 if (TREE_CODE (*tp) == ADDR_EXPR)
356 *walk_subtrees = 0;
357 return NULL_TREE;
360 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
361 count_p->count++;
363 return NULL_TREE;
367 /* Count the number of direct and indirect uses for pointer PTR in
368 statement STMT. The two counts are stored in *NUM_USES_P and
369 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
370 least one of those dereferences is a store operation. */
372 void
373 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
374 unsigned *num_derefs_p, bool *is_store)
376 ssa_op_iter i;
377 tree use;
379 *num_uses_p = 0;
380 *num_derefs_p = 0;
381 *is_store = false;
383 /* Find out the total number of uses of PTR in STMT. */
384 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
385 if (use == ptr)
386 (*num_uses_p)++;
388 /* Now count the number of indirect references to PTR. This is
389 truly awful, but we don't have much choice. There are no parent
390 pointers inside INDIRECT_REFs, so an expression like
391 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
392 find all the indirect and direct uses of x_1 inside. The only
393 shortcut we can take is the fact that GIMPLE only allows
394 INDIRECT_REFs inside the expressions below. */
395 if (TREE_CODE (stmt) == MODIFY_EXPR
396 || (TREE_CODE (stmt) == RETURN_EXPR
397 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
398 || TREE_CODE (stmt) == ASM_EXPR
399 || TREE_CODE (stmt) == CALL_EXPR)
401 tree lhs, rhs;
403 if (TREE_CODE (stmt) == MODIFY_EXPR)
405 lhs = TREE_OPERAND (stmt, 0);
406 rhs = TREE_OPERAND (stmt, 1);
408 else if (TREE_CODE (stmt) == RETURN_EXPR)
410 tree e = TREE_OPERAND (stmt, 0);
411 lhs = TREE_OPERAND (e, 0);
412 rhs = TREE_OPERAND (e, 1);
414 else if (TREE_CODE (stmt) == ASM_EXPR)
416 lhs = ASM_OUTPUTS (stmt);
417 rhs = ASM_INPUTS (stmt);
419 else
421 lhs = NULL_TREE;
422 rhs = stmt;
425 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
427 struct count_ptr_d count;
428 count.ptr = ptr;
429 count.count = 0;
430 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
431 *is_store = true;
432 *num_derefs_p = count.count;
435 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
437 struct count_ptr_d count;
438 count.ptr = ptr;
439 count.count = 0;
440 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
441 *num_derefs_p += count.count;
445 gcc_assert (*num_uses_p >= *num_derefs_p);
448 /* Initialize the data structures used for alias analysis. */
450 static struct alias_info *
451 init_alias_info (void)
453 struct alias_info *ai;
454 referenced_var_iterator rvi;
455 tree var;
457 bitmap_obstack_initialize (&alias_obstack);
458 ai = xcalloc (1, sizeof (struct alias_info));
459 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
460 sbitmap_zero (ai->ssa_names_visited);
461 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
462 ai->written_vars = BITMAP_ALLOC (&alias_obstack);
463 ai->dereferenced_ptrs_store = BITMAP_ALLOC (&alias_obstack);
464 ai->dereferenced_ptrs_load = BITMAP_ALLOC (&alias_obstack);
466 /* If aliases have been computed before, clear existing information. */
467 if (aliases_computed_p)
469 unsigned i;
471 /* Similarly, clear the set of addressable variables. In this
472 case, we can just clear the set because addressability is
473 only computed here. */
474 bitmap_clear (addressable_vars);
476 /* Clear flow-insensitive alias information from each symbol. */
477 FOR_EACH_REFERENCED_VAR (var, rvi)
479 var_ann_t ann = var_ann (var);
481 ann->is_alias_tag = 0;
482 ann->may_aliases = NULL;
483 NUM_REFERENCES_CLEAR (ann);
485 /* Since we are about to re-discover call-clobbered
486 variables, clear the call-clobbered flag. Variables that
487 are intrinsically call-clobbered (globals, local statics,
488 etc) will not be marked by the aliasing code, so we can't
489 remove them from CALL_CLOBBERED_VARS.
491 NB: STRUCT_FIELDS are still call clobbered if they are for
492 a global variable, so we *don't* clear their call clobberedness
493 just because they are tags, though we will clear it if they
494 aren't for global variables. */
495 if (ann->mem_tag_kind == NAME_TAG
496 || ann->mem_tag_kind == TYPE_TAG
497 || !is_global_var (var))
498 clear_call_clobbered (var);
501 /* Clear flow-sensitive points-to information from each SSA name. */
502 for (i = 1; i < num_ssa_names; i++)
504 tree name = ssa_name (i);
506 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
507 continue;
509 if (SSA_NAME_PTR_INFO (name))
511 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
513 /* Clear all the flags but keep the name tag to
514 avoid creating new temporaries unnecessarily. If
515 this pointer is found to point to a subset or
516 superset of its former points-to set, then a new
517 tag will need to be created in create_name_tags. */
518 pi->pt_anything = 0;
519 pi->pt_null = 0;
520 pi->value_escapes_p = 0;
521 pi->is_dereferenced = 0;
522 if (pi->pt_vars)
523 bitmap_clear (pi->pt_vars);
528 /* Next time, we will need to reset alias information. */
529 aliases_computed_p = true;
531 return ai;
535 /* Deallocate memory used by alias analysis. */
537 static void
538 delete_alias_info (struct alias_info *ai)
540 size_t i;
541 referenced_var_iterator rvi;
542 tree var;
544 sbitmap_free (ai->ssa_names_visited);
545 ai->processed_ptrs = NULL;
547 for (i = 0; i < ai->num_addressable_vars; i++)
548 free (ai->addressable_vars[i]);
550 FOR_EACH_REFERENCED_VAR(var, rvi)
552 var_ann_t ann = var_ann (var);
553 NUM_REFERENCES_CLEAR (ann);
556 free (ai->addressable_vars);
558 for (i = 0; i < ai->num_pointers; i++)
559 free (ai->pointers[i]);
560 free (ai->pointers);
562 BITMAP_FREE (ai->written_vars);
563 BITMAP_FREE (ai->dereferenced_ptrs_store);
564 BITMAP_FREE (ai->dereferenced_ptrs_load);
565 bitmap_obstack_release (&alias_obstack);
566 free (ai);
568 delete_points_to_sets ();
572 /* Create name tags for all the pointers that have been dereferenced.
573 We only create a name tag for a pointer P if P is found to point to
574 a set of variables (so that we can alias them to *P) or if it is
575 the result of a call to malloc (which means that P cannot point to
576 anything else nor alias any other variable).
578 If two pointers P and Q point to the same set of variables, they
579 are assigned the same name tag. */
581 static void
582 create_name_tags (void)
584 size_t i;
586 for (i = 1; i < num_ssa_names; i++)
588 tree ptr = ssa_name (i);
589 struct ptr_info_def *pi;
591 if (!ptr
592 || !POINTER_TYPE_P (TREE_TYPE (ptr))
593 || !SSA_NAME_PTR_INFO (ptr))
594 continue;
596 pi = SSA_NAME_PTR_INFO (ptr);
598 if (pi->pt_anything || !pi->is_dereferenced)
600 /* No name tags for pointers that have not been
601 dereferenced or point to an arbitrary location. */
602 pi->name_mem_tag = NULL_TREE;
603 continue;
606 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
608 size_t j;
609 tree old_name_tag = pi->name_mem_tag;
611 /* If PTR points to a set of variables, check if we don't
612 have another pointer Q with the same points-to set before
613 creating a tag. If so, use Q's tag instead of creating a
614 new one.
616 This is important for not creating unnecessary symbols
617 and also for copy propagation. If we ever need to
618 propagate PTR into Q or vice-versa, we would run into
619 problems if they both had different name tags because
620 they would have different SSA version numbers (which
621 would force us to take the name tags in and out of SSA). */
622 for (j = 1; j < i; j++)
624 tree q = ssa_name (j);
625 struct ptr_info_def *qi;
627 if (!q || !POINTER_TYPE_P (TREE_TYPE (q)))
628 continue;
630 qi = SSA_NAME_PTR_INFO (q);
632 if (qi
633 && qi->pt_vars
634 && qi->name_mem_tag
635 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
637 pi->name_mem_tag = qi->name_mem_tag;
638 break;
642 /* If we didn't find a pointer with the same points-to set
643 as PTR, create a new name tag if needed. */
644 if (pi->name_mem_tag == NULL_TREE)
645 pi->name_mem_tag = get_nmt_for (ptr);
647 /* If the new name tag computed for PTR is different than
648 the old name tag that it used to have, then the old tag
649 needs to be removed from the IL, so we mark it for
650 renaming. */
651 if (old_name_tag && old_name_tag != pi->name_mem_tag)
652 mark_sym_for_renaming (old_name_tag);
654 else
656 /* If the pointer does not point to a known spot, we should
657 use type tags. */
658 set_pt_anything (ptr);
659 continue;
662 TREE_THIS_VOLATILE (pi->name_mem_tag)
663 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
665 /* Mark the new name tag for renaming. */
666 mark_sym_for_renaming (pi->name_mem_tag);
671 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
672 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
673 name tag and the variables it points-to are call-clobbered. Finally, if
674 P_i escapes and we could not determine where it points to, then all the
675 variables in the same alias set as *P_i are marked call-clobbered. This
676 is necessary because we must assume that P_i may take the address of any
677 variable in the same alias set. */
679 static void
680 compute_flow_sensitive_aliasing (struct alias_info *ai)
682 size_t i;
684 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
686 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
687 if (!find_what_p_points_to (ptr))
688 set_pt_anything (ptr);
691 create_name_tags ();
693 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
695 unsigned j;
696 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
697 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
698 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
699 bitmap_iterator bi;
701 if (pi->value_escapes_p || pi->pt_anything)
703 /* If PTR escapes or may point to anything, then its associated
704 memory tags and pointed-to variables are call-clobbered. */
705 if (pi->name_mem_tag)
706 mark_call_clobbered (pi->name_mem_tag);
708 if (v_ann->type_mem_tag)
709 mark_call_clobbered (v_ann->type_mem_tag);
711 if (pi->pt_vars)
712 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
713 mark_call_clobbered (referenced_var (j));
716 /* Set up aliasing information for PTR's name memory tag (if it has
717 one). Note that only pointers that have been dereferenced will
718 have a name memory tag. */
719 if (pi->name_mem_tag && pi->pt_vars)
720 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
722 add_may_alias (pi->name_mem_tag, referenced_var (j));
723 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
726 /* If the name tag is call clobbered, so is the type tag
727 associated with the base VAR_DECL. */
728 if (pi->name_mem_tag
729 && v_ann->type_mem_tag
730 && is_call_clobbered (pi->name_mem_tag))
731 mark_call_clobbered (v_ann->type_mem_tag);
736 /* Compute type-based alias sets. Traverse all the pointers and
737 addressable variables found in setup_pointers_and_addressables.
739 For every pointer P in AI->POINTERS and addressable variable V in
740 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
741 memory tag (TMT) if their alias sets conflict. V is then marked as
742 an alias tag so that the operand scanner knows that statements
743 containing V have aliased operands. */
745 static void
746 compute_flow_insensitive_aliasing (struct alias_info *ai)
748 size_t i;
750 /* Initialize counter for the total number of virtual operands that
751 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
752 threshold set by --params max-alias-vops, we enable alias
753 grouping. */
754 ai->total_alias_vops = 0;
756 /* For every pointer P, determine which addressable variables may alias
757 with P's type memory tag. */
758 for (i = 0; i < ai->num_pointers; i++)
760 size_t j;
761 struct alias_map_d *p_map = ai->pointers[i];
762 tree tag = var_ann (p_map->var)->type_mem_tag;
763 var_ann_t tag_ann = var_ann (tag);
765 p_map->total_alias_vops = 0;
766 p_map->may_aliases = BITMAP_ALLOC (&alias_obstack);
768 for (j = 0; j < ai->num_addressable_vars; j++)
770 struct alias_map_d *v_map;
771 var_ann_t v_ann;
772 tree var;
773 bool tag_stored_p, var_stored_p;
775 v_map = ai->addressable_vars[j];
776 var = v_map->var;
777 v_ann = var_ann (var);
779 /* Skip memory tags and variables that have never been
780 written to. We also need to check if the variables are
781 call-clobbered because they may be overwritten by
782 function calls.
784 Note this is effectively random accessing elements in
785 the sparse bitset, which can be highly inefficient.
786 So we first check the call_clobbered status of the
787 tag and variable before querying the bitmap. */
788 tag_stored_p = is_call_clobbered (tag)
789 || bitmap_bit_p (ai->written_vars, DECL_UID (tag));
790 var_stored_p = is_call_clobbered (var)
791 || bitmap_bit_p (ai->written_vars, DECL_UID (var));
792 if (!tag_stored_p && !var_stored_p)
793 continue;
795 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
797 subvar_t svars;
798 size_t num_tag_refs, num_var_refs;
800 num_tag_refs = NUM_REFERENCES (tag_ann);
801 num_var_refs = NUM_REFERENCES (v_ann);
803 /* Add VAR to TAG's may-aliases set. */
805 /* If this is an aggregate, we may have subvariables for it
806 that need to be pointed to. */
807 if (var_can_have_subvars (var)
808 && (svars = get_subvars_for_var (var)))
810 subvar_t sv;
812 for (sv = svars; sv; sv = sv->next)
814 add_may_alias (tag, sv->var);
815 /* Update the bitmap used to represent TAG's alias set
816 in case we need to group aliases. */
817 bitmap_set_bit (p_map->may_aliases, DECL_UID (sv->var));
820 else
822 add_may_alias (tag, var);
823 /* Update the bitmap used to represent TAG's alias set
824 in case we need to group aliases. */
825 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
828 /* Update the total number of virtual operands due to
829 aliasing. Since we are adding one more alias to TAG's
830 may-aliases set, the total number of virtual operands due
831 to aliasing will be increased by the number of references
832 made to VAR and TAG (every reference to TAG will also
833 count as a reference to VAR). */
834 ai->total_alias_vops += (num_var_refs + num_tag_refs);
835 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
842 /* Since this analysis is based exclusively on symbols, it fails to
843 handle cases where two pointers P and Q have different memory
844 tags with conflicting alias set numbers but no aliased symbols in
845 common.
847 For example, suppose that we have two memory tags TMT.1 and TMT.2
848 such that
850 may-aliases (TMT.1) = { a }
851 may-aliases (TMT.2) = { b }
853 and the alias set number of TMT.1 conflicts with that of TMT.2.
854 Since they don't have symbols in common, loads and stores from
855 TMT.1 and TMT.2 will seem independent of each other, which will
856 lead to the optimizers making invalid transformations (see
857 testsuite/gcc.c-torture/execute/pr15262-[12].c).
859 To avoid this problem, we do a final traversal of AI->POINTERS
860 looking for pairs of pointers that have no aliased symbols in
861 common and yet have conflicting alias set numbers. */
862 for (i = 0; i < ai->num_pointers; i++)
864 size_t j;
865 struct alias_map_d *p_map1 = ai->pointers[i];
866 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
867 bitmap may_aliases1 = p_map1->may_aliases;
869 for (j = i + 1; j < ai->num_pointers; j++)
871 struct alias_map_d *p_map2 = ai->pointers[j];
872 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
873 bitmap may_aliases2 = p_map2->may_aliases;
875 /* If the pointers may not point to each other, do nothing. */
876 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
877 continue;
879 /* The two pointers may alias each other. If they already have
880 symbols in common, do nothing. */
881 if (bitmap_intersect_p (may_aliases1, may_aliases2))
882 continue;
884 if (!bitmap_empty_p (may_aliases2))
886 unsigned int k;
887 bitmap_iterator bi;
889 /* Add all the aliases for TAG2 into TAG1's alias set.
890 FIXME, update grouping heuristic counters. */
891 EXECUTE_IF_SET_IN_BITMAP (may_aliases2, 0, k, bi)
892 add_may_alias (tag1, referenced_var (k));
893 bitmap_ior_into (may_aliases1, may_aliases2);
895 else
897 /* Since TAG2 does not have any aliases of its own, add
898 TAG2 itself to the alias set of TAG1. */
899 add_may_alias (tag1, tag2);
900 bitmap_set_bit (may_aliases1, DECL_UID (tag2));
905 if (dump_file)
906 fprintf (dump_file, "\n%s: Total number of aliased vops: %ld\n",
907 get_name (current_function_decl),
908 ai->total_alias_vops);
910 /* Determine if we need to enable alias grouping. */
911 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
912 group_aliases (ai);
916 /* Comparison function for qsort used in group_aliases. */
918 static int
919 total_alias_vops_cmp (const void *p, const void *q)
921 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
922 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
923 long n1 = (*p1)->total_alias_vops;
924 long n2 = (*p2)->total_alias_vops;
926 /* We want to sort in descending order. */
927 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
930 /* Group all the aliases for TAG to make TAG represent all the
931 variables in its alias set. Update the total number
932 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
933 function will make TAG be the unique alias tag for all the
934 variables in its may-aliases. So, given:
936 may-aliases(TAG) = { V1, V2, V3 }
938 This function will group the variables into:
940 may-aliases(V1) = { TAG }
941 may-aliases(V2) = { TAG }
942 may-aliases(V2) = { TAG } */
944 static void
945 group_aliases_into (tree tag, bitmap tag_aliases, struct alias_info *ai)
947 unsigned int i;
948 var_ann_t tag_ann = var_ann (tag);
949 size_t num_tag_refs = NUM_REFERENCES (tag_ann);
950 bitmap_iterator bi;
952 EXECUTE_IF_SET_IN_BITMAP (tag_aliases, 0, i, bi)
954 tree var = referenced_var (i);
955 var_ann_t ann = var_ann (var);
957 /* Make TAG the unique alias of VAR. */
958 ann->is_alias_tag = 0;
959 ann->may_aliases = NULL;
961 /* Note that VAR and TAG may be the same if the function has no
962 addressable variables (see the discussion at the end of
963 setup_pointers_and_addressables). */
964 if (var != tag)
965 add_may_alias (var, tag);
967 /* Reduce total number of virtual operands contributed
968 by TAG on behalf of VAR. Notice that the references to VAR
969 itself won't be removed. We will merely replace them with
970 references to TAG. */
971 ai->total_alias_vops -= num_tag_refs;
974 /* We have reduced the number of virtual operands that TAG makes on
975 behalf of all the variables formerly aliased with it. However,
976 we have also "removed" all the virtual operands for TAG itself,
977 so we add them back. */
978 ai->total_alias_vops += num_tag_refs;
980 /* TAG no longer has any aliases. */
981 tag_ann->may_aliases = NULL;
985 /* Group may-aliases sets to reduce the number of virtual operands due
986 to aliasing.
988 1- Sort the list of pointers in decreasing number of contributed
989 virtual operands.
991 2- Take the first entry in AI->POINTERS and revert the role of
992 the memory tag and its aliases. Usually, whenever an aliased
993 variable Vi is found to alias with a memory tag T, we add Vi
994 to the may-aliases set for T. Meaning that after alias
995 analysis, we will have:
997 may-aliases(T) = { V1, V2, V3, ..., Vn }
999 This means that every statement that references T, will get 'n'
1000 virtual operands for each of the Vi tags. But, when alias
1001 grouping is enabled, we make T an alias tag and add it to the
1002 alias set of all the Vi variables:
1004 may-aliases(V1) = { T }
1005 may-aliases(V2) = { T }
1007 may-aliases(Vn) = { T }
1009 This has two effects: (a) statements referencing T will only get
1010 a single virtual operand, and, (b) all the variables Vi will now
1011 appear to alias each other. So, we lose alias precision to
1012 improve compile time. But, in theory, a program with such a high
1013 level of aliasing should not be very optimizable in the first
1014 place.
1016 3- Since variables may be in the alias set of more than one
1017 memory tag, the grouping done in step (2) needs to be extended
1018 to all the memory tags that have a non-empty intersection with
1019 the may-aliases set of tag T. For instance, if we originally
1020 had these may-aliases sets:
1022 may-aliases(T) = { V1, V2, V3 }
1023 may-aliases(R) = { V2, V4 }
1025 In step (2) we would have reverted the aliases for T as:
1027 may-aliases(V1) = { T }
1028 may-aliases(V2) = { T }
1029 may-aliases(V3) = { T }
1031 But note that now V2 is no longer aliased with R. We could
1032 add R to may-aliases(V2), but we are in the process of
1033 grouping aliases to reduce virtual operands so what we do is
1034 add V4 to the grouping to obtain:
1036 may-aliases(V1) = { T }
1037 may-aliases(V2) = { T }
1038 may-aliases(V3) = { T }
1039 may-aliases(V4) = { T }
1041 4- If the total number of virtual operands due to aliasing is
1042 still above the threshold set by max-alias-vops, go back to (2). */
1044 static void
1045 group_aliases (struct alias_info *ai)
1047 size_t i;
1049 /* Sort the POINTERS array in descending order of contributed
1050 virtual operands. */
1051 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1052 total_alias_vops_cmp);
1054 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1055 and the tag's may-aliases set. */
1056 for (i = 0; i < ai->num_pointers; i++)
1058 size_t j;
1059 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1060 bitmap tag1_aliases = ai->pointers[i]->may_aliases;
1062 /* Skip tags that have been grouped already. */
1063 if (ai->pointers[i]->grouped_p)
1064 continue;
1066 /* See if TAG1 had any aliases in common with other type tags.
1067 If we find a TAG2 with common aliases with TAG1, add TAG2's
1068 aliases into TAG1. */
1069 for (j = i + 1; j < ai->num_pointers; j++)
1071 bitmap tag2_aliases = ai->pointers[j]->may_aliases;
1073 if (bitmap_intersect_p (tag1_aliases, tag2_aliases))
1075 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1077 bitmap_ior_into (tag1_aliases, tag2_aliases);
1079 /* TAG2 does not need its aliases anymore. */
1080 bitmap_clear (tag2_aliases);
1081 var_ann (tag2)->may_aliases = NULL;
1083 /* TAG1 is the unique alias of TAG2. */
1084 add_may_alias (tag2, tag1);
1086 ai->pointers[j]->grouped_p = true;
1090 /* Now group all the aliases we collected into TAG1. */
1091 group_aliases_into (tag1, tag1_aliases, ai);
1093 /* If we've reduced total number of virtual operands below the
1094 threshold, stop. */
1095 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1096 break;
1099 /* Finally, all the variables that have been grouped cannot be in
1100 the may-alias set of name memory tags. Suppose that we have
1101 grouped the aliases in this code so that may-aliases(a) = TMT.20
1103 p_5 = &a;
1105 # a_9 = V_MAY_DEF <a_8>
1106 p_5->field = 0
1107 ... Several modifications to TMT.20 ...
1108 # VUSE <a_9>
1109 x_30 = p_5->field
1111 Since p_5 points to 'a', the optimizers will try to propagate 0
1112 into p_5->field, but that is wrong because there have been
1113 modifications to 'TMT.20' in between. To prevent this we have to
1114 replace 'a' with 'TMT.20' in the name tag of p_5. */
1115 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1117 size_t j;
1118 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1119 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1120 varray_type aliases;
1122 if (name_tag == NULL_TREE)
1123 continue;
1125 aliases = var_ann (name_tag)->may_aliases;
1126 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1128 tree alias = VARRAY_TREE (aliases, j);
1129 var_ann_t ann = var_ann (alias);
1131 if ((ann->mem_tag_kind == NOT_A_TAG
1132 || ann->mem_tag_kind == STRUCT_FIELD)
1133 && ann->may_aliases)
1135 tree new_alias;
1137 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1139 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1140 replace_may_alias (name_tag, j, new_alias);
1145 if (dump_file)
1146 fprintf (dump_file,
1147 "%s: Total number of aliased vops after grouping: %ld%s\n",
1148 get_name (current_function_decl),
1149 ai->total_alias_vops,
1150 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1154 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1156 static void
1157 create_alias_map_for (tree var, struct alias_info *ai)
1159 struct alias_map_d *alias_map;
1160 alias_map = xcalloc (1, sizeof (*alias_map));
1161 alias_map->var = var;
1162 alias_map->set = get_alias_set (var);
1163 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1167 /* Create memory tags for all the dereferenced pointers and build the
1168 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1169 sets. Based on the address escape and points-to information collected
1170 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1171 variables whose address is not needed anymore. */
1173 static void
1174 setup_pointers_and_addressables (struct alias_info *ai)
1176 size_t n_vars, num_addressable_vars, num_pointers;
1177 referenced_var_iterator rvi;
1178 tree var;
1179 VEC (tree, heap) *varvec = NULL;
1180 safe_referenced_var_iterator srvi;
1182 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1183 num_addressable_vars = num_pointers = 0;
1185 FOR_EACH_REFERENCED_VAR (var, rvi)
1187 if (may_be_aliased (var))
1188 num_addressable_vars++;
1190 if (POINTER_TYPE_P (TREE_TYPE (var)))
1192 /* Since we don't keep track of volatile variables, assume that
1193 these pointers are used in indirect store operations. */
1194 if (TREE_THIS_VOLATILE (var))
1195 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
1197 num_pointers++;
1201 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1202 always going to be slightly bigger than we actually need them
1203 because some TREE_ADDRESSABLE variables will be marked
1204 non-addressable below and only pointers with unique type tags are
1205 going to be added to POINTERS. */
1206 ai->addressable_vars = xcalloc (num_addressable_vars,
1207 sizeof (struct alias_map_d *));
1208 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1209 ai->num_addressable_vars = 0;
1210 ai->num_pointers = 0;
1212 /* Since we will be creating type memory tags within this loop, cache the
1213 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1214 unnecessarily. */
1215 n_vars = num_referenced_vars;
1217 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
1219 var_ann_t v_ann = var_ann (var);
1220 subvar_t svars;
1222 /* Name memory tags already have flow-sensitive aliasing
1223 information, so they need not be processed by
1224 compute_flow_insensitive_aliasing. Similarly, type memory
1225 tags are already accounted for when we process their
1226 associated pointer.
1228 Structure fields, on the other hand, have to have some of this
1229 information processed for them, but it's pointless to mark them
1230 non-addressable (since they are fake variables anyway). */
1231 if (v_ann->mem_tag_kind != NOT_A_TAG
1232 && v_ann->mem_tag_kind != STRUCT_FIELD)
1233 continue;
1235 /* Remove the ADDRESSABLE flag from every addressable variable whose
1236 address is not needed anymore. This is caused by the propagation
1237 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1238 removal of dead pointer assignments done by the early scalar
1239 cleanup passes. */
1240 if (TREE_ADDRESSABLE (var))
1242 if (!bitmap_bit_p (addressable_vars, DECL_UID (var))
1243 && TREE_CODE (var) != RESULT_DECL
1244 && !is_global_var (var))
1246 bool okay_to_mark = true;
1248 /* Since VAR is now a regular GIMPLE register, we will need
1249 to rename VAR into SSA afterwards. */
1250 mark_sym_for_renaming (var);
1252 /* If VAR can have sub-variables, and any of its
1253 sub-variables has its address taken, then we cannot
1254 remove the addressable flag from VAR. */
1255 if (var_can_have_subvars (var)
1256 && (svars = get_subvars_for_var (var)))
1258 subvar_t sv;
1260 for (sv = svars; sv; sv = sv->next)
1262 if (bitmap_bit_p (addressable_vars, DECL_UID (sv->var)))
1263 okay_to_mark = false;
1264 mark_sym_for_renaming (sv->var);
1268 /* The address of VAR is not needed, remove the
1269 addressable bit, so that it can be optimized as a
1270 regular variable. */
1271 if (okay_to_mark)
1272 mark_non_addressable (var);
1276 /* Global variables and addressable locals may be aliased. Create an
1277 entry in ADDRESSABLE_VARS for VAR. */
1278 if (may_be_aliased (var))
1280 create_alias_map_for (var, ai);
1281 mark_sym_for_renaming (var);
1284 /* Add pointer variables that have been dereferenced to the POINTERS
1285 array and create a type memory tag for them. */
1286 if (POINTER_TYPE_P (TREE_TYPE (var)))
1288 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var))
1289 || bitmap_bit_p (ai->dereferenced_ptrs_load, DECL_UID (var))))
1291 tree tag;
1292 var_ann_t t_ann;
1294 /* If pointer VAR still doesn't have a memory tag
1295 associated with it, create it now or re-use an
1296 existing one. */
1297 tag = get_tmt_for (var, ai);
1298 t_ann = var_ann (tag);
1300 /* The type tag will need to be renamed into SSA
1301 afterwards. Note that we cannot do this inside
1302 get_tmt_for because aliasing may run multiple times
1303 and we only create type tags the first time. */
1304 mark_sym_for_renaming (tag);
1306 /* Similarly, if pointer VAR used to have another type
1307 tag, we will need to process it in the renamer to
1308 remove the stale virtual operands. */
1309 if (v_ann->type_mem_tag)
1310 mark_sym_for_renaming (v_ann->type_mem_tag);
1312 /* Associate the tag with pointer VAR. */
1313 v_ann->type_mem_tag = tag;
1315 /* If pointer VAR has been used in a store operation,
1316 then its memory tag must be marked as written-to. */
1317 if (bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var)))
1318 bitmap_set_bit (ai->written_vars, DECL_UID (tag));
1320 /* If pointer VAR is a global variable or a PARM_DECL,
1321 then its memory tag should be considered a global
1322 variable. */
1323 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1324 mark_call_clobbered (tag);
1326 /* All the dereferences of pointer VAR count as
1327 references of TAG. Since TAG can be associated with
1328 several pointers, add the dereferences of VAR to the
1329 TAG. */
1330 NUM_REFERENCES_SET (t_ann,
1331 NUM_REFERENCES (t_ann)
1332 + NUM_REFERENCES (v_ann));
1334 else
1336 /* The pointer has not been dereferenced. If it had a
1337 type memory tag, remove it and mark the old tag for
1338 renaming to remove it out of the IL. */
1339 var_ann_t ann = var_ann (var);
1340 tree tag = ann->type_mem_tag;
1341 if (tag)
1343 mark_sym_for_renaming (tag);
1344 ann->type_mem_tag = NULL_TREE;
1349 VEC_free (tree, heap, varvec);
1353 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1354 every call site, we need to emit V_MAY_DEF expressions to represent the
1355 clobbering effects of the call for variables whose address escapes the
1356 current function.
1358 One approach is to group all call-clobbered variables into a single
1359 representative that is used as an alias of every call-clobbered variable
1360 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1361 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1363 The second approach is to emit a clobbering V_MAY_DEF for every
1364 call-clobbered variable at call sites. This is the preferred way in terms
1365 of optimization opportunities but it may create too many V_MAY_DEF operands
1366 if there are many call clobbered variables and function calls in the
1367 function.
1369 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1370 function calls found by the number of call-clobbered variables. If that
1371 product is beyond a certain threshold, as determined by the parameterized
1372 values shown below, we use .GLOBAL_VAR.
1374 FIXME. This heuristic should be improved. One idea is to use several
1375 .GLOBAL_VARs of different types instead of a single one. The thresholds
1376 have been derived from a typical bootstrap cycle, including all target
1377 libraries. Compile times were found increase by ~1% compared to using
1378 .GLOBAL_VAR. */
1380 static void
1381 maybe_create_global_var (struct alias_info *ai)
1383 unsigned i, n_clobbered;
1384 bitmap_iterator bi;
1386 /* No need to create it, if we have one already. */
1387 if (global_var == NULL_TREE)
1389 /* Count all the call-clobbered variables. */
1390 n_clobbered = 0;
1391 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1393 n_clobbered++;
1396 /* If the number of virtual operands that would be needed to
1397 model all the call-clobbered variables is larger than
1398 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1400 Also create .GLOBAL_VAR if there are no call-clobbered
1401 variables and the program contains a mixture of pure/const
1402 and regular function calls. This is to avoid the problem
1403 described in PR 20115:
1405 int X;
1406 int func_pure (void) { return X; }
1407 int func_non_pure (int a) { X += a; }
1408 int foo ()
1410 int a = func_pure ();
1411 func_non_pure (a);
1412 a = func_pure ();
1413 return a;
1416 Since foo() has no call-clobbered variables, there is
1417 no relationship between the calls to func_pure and
1418 func_non_pure. Since func_pure has no side-effects, value
1419 numbering optimizations elide the second call to func_pure.
1420 So, if we have some pure/const and some regular calls in the
1421 program we create .GLOBAL_VAR to avoid missing these
1422 relations. */
1423 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1424 || (n_clobbered == 0
1425 && ai->num_calls_found > 0
1426 && ai->num_pure_const_calls_found > 0
1427 && ai->num_calls_found > ai->num_pure_const_calls_found))
1428 create_global_var ();
1431 /* Mark all call-clobbered symbols for renaming. Since the initial
1432 rewrite into SSA ignored all call sites, we may need to rename
1433 .GLOBAL_VAR and the call-clobbered variables. */
1434 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1436 tree var = referenced_var (i);
1438 /* If the function has calls to clobbering functions and
1439 .GLOBAL_VAR has been created, make it an alias for all
1440 call-clobbered variables. */
1441 if (global_var && var != global_var)
1443 subvar_t svars;
1444 add_may_alias (var, global_var);
1445 if (var_can_have_subvars (var)
1446 && (svars = get_subvars_for_var (var)))
1448 subvar_t sv;
1449 for (sv = svars; sv; sv = sv->next)
1450 mark_sym_for_renaming (sv->var);
1454 mark_sym_for_renaming (var);
1459 /* Return TRUE if pointer PTR may point to variable VAR.
1461 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1462 This is needed because when checking for type conflicts we are
1463 interested in the alias set of the memory location pointed-to by
1464 PTR. The alias set of PTR itself is irrelevant.
1466 VAR_ALIAS_SET is the alias set for VAR. */
1468 static bool
1469 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1470 tree var, HOST_WIDE_INT var_alias_set,
1471 bool alias_set_only)
1473 tree mem;
1474 var_ann_t m_ann;
1476 alias_stats.alias_queries++;
1477 alias_stats.simple_queries++;
1479 /* By convention, a variable cannot alias itself. */
1480 mem = var_ann (ptr)->type_mem_tag;
1481 if (mem == var)
1483 alias_stats.alias_noalias++;
1484 alias_stats.simple_resolved++;
1485 return false;
1488 /* If -fargument-noalias-global is >1, pointer arguments may
1489 not point to global variables. */
1490 if (flag_argument_noalias > 1 && is_global_var (var)
1491 && TREE_CODE (ptr) == PARM_DECL)
1493 alias_stats.alias_noalias++;
1494 alias_stats.simple_resolved++;
1495 return false;
1498 /* If either MEM or VAR is a read-only global and the other one
1499 isn't, then PTR cannot point to VAR. */
1500 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1501 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1503 alias_stats.alias_noalias++;
1504 alias_stats.simple_resolved++;
1505 return false;
1508 m_ann = var_ann (mem);
1510 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1512 alias_stats.tbaa_queries++;
1514 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1515 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1517 alias_stats.alias_noalias++;
1518 alias_stats.tbaa_resolved++;
1519 return false;
1522 /* If var is a record or union type, ptr cannot point into var
1523 unless there is some operation explicit address operation in the
1524 program that can reference a field of the ptr's dereferenced
1525 type. This also assumes that the types of both var and ptr are
1526 contained within the compilation unit, and that there is no fancy
1527 addressing arithmetic associated with any of the types
1528 involved. */
1530 if ((mem_alias_set != 0) && (var_alias_set != 0))
1532 tree ptr_type = TREE_TYPE (ptr);
1533 tree var_type = TREE_TYPE (var);
1535 /* The star count is -1 if the type at the end of the pointer_to
1536 chain is not a record or union type. */
1537 if ((!alias_set_only) &&
1538 ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
1540 int ptr_star_count = 0;
1542 /* Ipa_type_escape_star_count_of_interesting_type is a little to
1543 restrictive for the pointer type, need to allow pointers to
1544 primitive types as long as those types cannot be pointers
1545 to everything. */
1546 while (POINTER_TYPE_P (ptr_type))
1547 /* Strip the *'s off. */
1549 ptr_type = TREE_TYPE (ptr_type);
1550 ptr_star_count++;
1553 /* There does not appear to be a better test to see if the
1554 pointer type was one of the pointer to everything
1555 types. */
1557 if (ptr_star_count > 0)
1559 alias_stats.structnoaddress_queries++;
1560 if (ipa_type_escape_field_does_not_clobber_p (var_type,
1561 TREE_TYPE (ptr)))
1563 alias_stats.structnoaddress_resolved++;
1564 alias_stats.alias_noalias++;
1565 return false;
1568 else if (ptr_star_count == 0)
1570 /* If ptr_type was not really a pointer to type, it cannot
1571 alias. */
1572 alias_stats.structnoaddress_queries++;
1573 alias_stats.structnoaddress_resolved++;
1574 alias_stats.alias_noalias++;
1575 return false;
1580 alias_stats.alias_mayalias++;
1581 return true;
1585 /* Add ALIAS to the set of variables that may alias VAR. */
1587 static void
1588 add_may_alias (tree var, tree alias)
1590 size_t i;
1591 var_ann_t v_ann = get_var_ann (var);
1592 var_ann_t a_ann = get_var_ann (alias);
1594 /* Don't allow self-referential aliases. */
1595 gcc_assert (var != alias);
1597 /* ALIAS must be addressable if it's being added to an alias set. */
1598 #if 1
1599 TREE_ADDRESSABLE (alias) = 1;
1600 #else
1601 gcc_assert (may_be_aliased (alias));
1602 #endif
1604 if (v_ann->may_aliases == NULL)
1605 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1607 /* Avoid adding duplicates. */
1608 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1609 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1610 return;
1612 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1613 FIXME, call-clobbering should only depend on whether an address
1614 escapes. It should be independent of aliasing. */
1615 if (is_call_clobbered (var))
1616 mark_call_clobbered (alias);
1618 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1619 else if (is_call_clobbered (alias))
1620 mark_call_clobbered (var);
1622 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1623 a_ann->is_alias_tag = 1;
1627 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1629 static void
1630 replace_may_alias (tree var, size_t i, tree new_alias)
1632 var_ann_t v_ann = var_ann (var);
1633 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1635 /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1636 FIXME, call-clobbering should only depend on whether an address
1637 escapes. It should be independent of aliasing. */
1638 if (is_call_clobbered (var))
1639 mark_call_clobbered (new_alias);
1641 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1642 else if (is_call_clobbered (new_alias))
1643 mark_call_clobbered (var);
1647 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1649 static void
1650 set_pt_anything (tree ptr)
1652 struct ptr_info_def *pi = get_ptr_info (ptr);
1654 pi->pt_anything = 1;
1655 pi->pt_vars = NULL;
1657 /* The pointer used to have a name tag, but we now found it pointing
1658 to an arbitrary location. The name tag needs to be renamed and
1659 disassociated from PTR. */
1660 if (pi->name_mem_tag)
1662 mark_sym_for_renaming (pi->name_mem_tag);
1663 pi->name_mem_tag = NULL_TREE;
1668 /* Return true if STMT is an "escape" site from the current function. Escape
1669 sites those statements which might expose the address of a variable
1670 outside the current function. STMT is an escape site iff:
1672 1- STMT is a function call, or
1673 2- STMT is an __asm__ expression, or
1674 3- STMT is an assignment to a non-local variable, or
1675 4- STMT is a return statement.
1677 AI points to the alias information collected so far. */
1679 bool
1680 is_escape_site (tree stmt, struct alias_info *ai)
1682 tree call = get_call_expr_in (stmt);
1683 if (call != NULL_TREE)
1685 ai->num_calls_found++;
1687 if (!TREE_SIDE_EFFECTS (call))
1688 ai->num_pure_const_calls_found++;
1690 return true;
1692 else if (TREE_CODE (stmt) == ASM_EXPR)
1693 return true;
1694 else if (TREE_CODE (stmt) == MODIFY_EXPR)
1696 tree lhs = TREE_OPERAND (stmt, 0);
1698 /* Get to the base of _REF nodes. */
1699 if (TREE_CODE (lhs) != SSA_NAME)
1700 lhs = get_base_address (lhs);
1702 /* If we couldn't recognize the LHS of the assignment, assume that it
1703 is a non-local store. */
1704 if (lhs == NULL_TREE)
1705 return true;
1707 /* If the RHS is a conversion between a pointer and an integer, the
1708 pointer escapes since we can't track the integer. */
1709 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
1710 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
1711 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
1712 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
1713 (TREE_OPERAND (stmt, 1), 0)))
1714 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1715 return true;
1717 /* If the LHS is an SSA name, it can't possibly represent a non-local
1718 memory store. */
1719 if (TREE_CODE (lhs) == SSA_NAME)
1720 return false;
1722 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
1723 local variables we cannot be sure if it will escape, because we
1724 don't have information about objects not in SSA form. Need to
1725 implement something along the lines of
1727 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
1728 Midkiff, ``Escape analysis for java,'' in Proceedings of the
1729 Conference on Object-Oriented Programming Systems, Languages, and
1730 Applications (OOPSLA), pp. 1-19, 1999. */
1731 return true;
1733 else if (TREE_CODE (stmt) == RETURN_EXPR)
1734 return true;
1736 return false;
1740 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
1741 is considered to represent all the pointers whose pointed-to types are
1742 in the same alias set class. Otherwise, the tag represents a single
1743 SSA_NAME pointer variable. */
1745 static tree
1746 create_memory_tag (tree type, bool is_type_tag)
1748 var_ann_t ann;
1749 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
1751 /* By default, memory tags are local variables. Alias analysis will
1752 determine whether they should be considered globals. */
1753 DECL_CONTEXT (tag) = current_function_decl;
1755 /* Memory tags are by definition addressable. */
1756 TREE_ADDRESSABLE (tag) = 1;
1758 ann = get_var_ann (tag);
1759 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
1760 ann->type_mem_tag = NULL_TREE;
1762 /* Add the tag to the symbol table. */
1763 add_referenced_tmp_var (tag);
1765 return tag;
1769 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
1770 This is used if P_i has been found to point to a specific set of
1771 variables or to a non-aliased memory location like the address returned
1772 by malloc functions. */
1774 static tree
1775 get_nmt_for (tree ptr)
1777 struct ptr_info_def *pi = get_ptr_info (ptr);
1778 tree tag = pi->name_mem_tag;
1780 if (tag == NULL_TREE)
1781 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
1783 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
1784 then its name tag should be considered a global variable. */
1785 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
1786 || pi->pt_global_mem)
1787 mark_call_clobbered (tag);
1789 return tag;
1793 /* Return the type memory tag associated to pointer PTR. A memory tag is an
1794 artificial variable that represents the memory location pointed-to by
1795 PTR. It is used to model the effects of pointer de-references on
1796 addressable variables.
1798 AI points to the data gathered during alias analysis. This function
1799 populates the array AI->POINTERS. */
1801 static tree
1802 get_tmt_for (tree ptr, struct alias_info *ai)
1804 size_t i;
1805 tree tag;
1806 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
1807 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
1809 /* To avoid creating unnecessary memory tags, only create one memory tag
1810 per alias set class. Note that it may be tempting to group
1811 memory tags based on conflicting alias sets instead of
1812 equivalence. That would be wrong because alias sets are not
1813 necessarily transitive (as demonstrated by the libstdc++ test
1814 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
1815 such that conflicts (A, B) == true and conflicts (A, C) == true,
1816 it does not necessarily follow that conflicts (B, C) == true. */
1817 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
1819 struct alias_map_d *curr = ai->pointers[i];
1820 tree curr_tag = var_ann (curr->var)->type_mem_tag;
1821 if (tag_set == curr->set
1822 && TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (curr_tag)))
1824 tag = curr_tag;
1825 break;
1829 /* If VAR cannot alias with any of the existing memory tags, create a new
1830 tag for PTR and add it to the POINTERS array. */
1831 if (tag == NULL_TREE)
1833 struct alias_map_d *alias_map;
1835 /* If PTR did not have a type tag already, create a new TMT.*
1836 artificial variable representing the memory location
1837 pointed-to by PTR. */
1838 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
1839 tag = create_memory_tag (tag_type, true);
1840 else
1841 tag = var_ann (ptr)->type_mem_tag;
1843 /* Add PTR to the POINTERS array. Note that we are not interested in
1844 PTR's alias set. Instead, we cache the alias set for the memory that
1845 PTR points to. */
1846 alias_map = xcalloc (1, sizeof (*alias_map));
1847 alias_map->var = ptr;
1848 alias_map->set = tag_set;
1849 ai->pointers[ai->num_pointers++] = alias_map;
1852 /* If the pointed-to type is volatile, so is the tag. */
1853 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
1855 /* Make sure that the type tag has the same alias set as the
1856 pointed-to type. */
1857 gcc_assert (tag_set == get_alias_set (tag));
1859 /* If PTR's pointed-to type is read-only, then TAG's type must also
1860 be read-only. */
1861 gcc_assert (TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (tag)));
1863 return tag;
1867 /* Create GLOBAL_VAR, an artificial global variable to act as a
1868 representative of all the variables that may be clobbered by function
1869 calls. */
1871 static void
1872 create_global_var (void)
1874 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
1875 void_type_node);
1876 DECL_ARTIFICIAL (global_var) = 1;
1877 TREE_READONLY (global_var) = 0;
1878 DECL_EXTERNAL (global_var) = 1;
1879 TREE_STATIC (global_var) = 1;
1880 TREE_USED (global_var) = 1;
1881 DECL_CONTEXT (global_var) = NULL_TREE;
1882 TREE_THIS_VOLATILE (global_var) = 0;
1883 TREE_ADDRESSABLE (global_var) = 0;
1885 add_referenced_tmp_var (global_var);
1886 mark_sym_for_renaming (global_var);
1890 /* Dump alias statistics on FILE. */
1892 static void
1893 dump_alias_stats (FILE *file)
1895 const char *funcname
1896 = lang_hooks.decl_printable_name (current_function_decl, 2);
1897 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
1898 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
1899 fprintf (file, "Total alias mayalias results:\t%u\n",
1900 alias_stats.alias_mayalias);
1901 fprintf (file, "Total alias noalias results:\t%u\n",
1902 alias_stats.alias_noalias);
1903 fprintf (file, "Total simple queries:\t%u\n",
1904 alias_stats.simple_queries);
1905 fprintf (file, "Total simple resolved:\t%u\n",
1906 alias_stats.simple_resolved);
1907 fprintf (file, "Total TBAA queries:\t%u\n",
1908 alias_stats.tbaa_queries);
1909 fprintf (file, "Total TBAA resolved:\t%u\n",
1910 alias_stats.tbaa_resolved);
1911 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
1912 alias_stats.structnoaddress_queries);
1913 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
1914 alias_stats.structnoaddress_resolved);
1918 /* Dump alias information on FILE. */
1920 void
1921 dump_alias_info (FILE *file)
1923 size_t i;
1924 const char *funcname
1925 = lang_hooks.decl_printable_name (current_function_decl, 2);
1926 referenced_var_iterator rvi;
1927 tree var;
1929 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
1931 fprintf (file, "Aliased symbols\n\n");
1933 FOR_EACH_REFERENCED_VAR (var, rvi)
1935 if (may_be_aliased (var))
1936 dump_variable (file, var);
1939 fprintf (file, "\nDereferenced pointers\n\n");
1941 FOR_EACH_REFERENCED_VAR (var, rvi)
1943 var_ann_t ann = var_ann (var);
1944 if (ann->type_mem_tag)
1945 dump_variable (file, var);
1948 fprintf (file, "\nType memory tags\n\n");
1950 FOR_EACH_REFERENCED_VAR (var, rvi)
1952 var_ann_t ann = var_ann (var);
1953 if (ann->mem_tag_kind == TYPE_TAG)
1954 dump_variable (file, var);
1957 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
1959 fprintf (file, "SSA_NAME pointers\n\n");
1960 for (i = 1; i < num_ssa_names; i++)
1962 tree ptr = ssa_name (i);
1963 struct ptr_info_def *pi;
1965 if (ptr == NULL_TREE)
1966 continue;
1968 pi = SSA_NAME_PTR_INFO (ptr);
1969 if (!SSA_NAME_IN_FREE_LIST (ptr)
1970 && pi
1971 && pi->name_mem_tag)
1972 dump_points_to_info_for (file, ptr);
1975 fprintf (file, "\nName memory tags\n\n");
1977 FOR_EACH_REFERENCED_VAR (var, rvi)
1979 var_ann_t ann = var_ann (var);
1980 if (ann->mem_tag_kind == NAME_TAG)
1981 dump_variable (file, var);
1984 fprintf (file, "\n");
1988 /* Dump alias information on stderr. */
1990 void
1991 debug_alias_info (void)
1993 dump_alias_info (stderr);
1997 /* Return the alias information associated with pointer T. It creates a
1998 new instance if none existed. */
2000 struct ptr_info_def *
2001 get_ptr_info (tree t)
2003 struct ptr_info_def *pi;
2005 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2007 pi = SSA_NAME_PTR_INFO (t);
2008 if (pi == NULL)
2010 pi = ggc_alloc (sizeof (*pi));
2011 memset ((void *)pi, 0, sizeof (*pi));
2012 SSA_NAME_PTR_INFO (t) = pi;
2015 return pi;
2019 /* Dump points-to information for SSA_NAME PTR into FILE. */
2021 void
2022 dump_points_to_info_for (FILE *file, tree ptr)
2024 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2026 print_generic_expr (file, ptr, dump_flags);
2028 if (pi)
2030 if (pi->name_mem_tag)
2032 fprintf (file, ", name memory tag: ");
2033 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2036 if (pi->is_dereferenced)
2037 fprintf (file, ", is dereferenced");
2039 if (pi->value_escapes_p)
2040 fprintf (file, ", its value escapes");
2042 if (pi->pt_anything)
2043 fprintf (file, ", points-to anything");
2045 if (pi->pt_null)
2046 fprintf (file, ", points-to NULL");
2048 if (pi->pt_vars)
2050 unsigned ix;
2051 bitmap_iterator bi;
2053 fprintf (file, ", points-to vars: { ");
2054 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2056 print_generic_expr (file, referenced_var (ix), dump_flags);
2057 fprintf (file, " ");
2059 fprintf (file, "}");
2063 fprintf (file, "\n");
2067 /* Dump points-to information for VAR into stderr. */
2069 void
2070 debug_points_to_info_for (tree var)
2072 dump_points_to_info_for (stderr, var);
2076 /* Dump points-to information into FILE. NOTE: This function is slow, as
2077 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2079 void
2080 dump_points_to_info (FILE *file)
2082 basic_block bb;
2083 block_stmt_iterator si;
2084 ssa_op_iter iter;
2085 const char *fname =
2086 lang_hooks.decl_printable_name (current_function_decl, 2);
2087 referenced_var_iterator rvi;
2088 tree var;
2090 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2092 /* First dump points-to information for the default definitions of
2093 pointer variables. This is necessary because default definitions are
2094 not part of the code. */
2095 FOR_EACH_REFERENCED_VAR (var, rvi)
2097 if (POINTER_TYPE_P (TREE_TYPE (var)))
2099 tree def = default_def (var);
2100 if (def)
2101 dump_points_to_info_for (file, def);
2105 /* Dump points-to information for every pointer defined in the program. */
2106 FOR_EACH_BB (bb)
2108 tree phi;
2110 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2112 tree ptr = PHI_RESULT (phi);
2113 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2114 dump_points_to_info_for (file, ptr);
2117 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2119 tree stmt = bsi_stmt (si);
2120 tree def;
2121 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2122 if (POINTER_TYPE_P (TREE_TYPE (def)))
2123 dump_points_to_info_for (file, def);
2127 fprintf (file, "\n");
2131 /* Dump points-to info pointed to by PTO into STDERR. */
2133 void
2134 debug_points_to_info (void)
2136 dump_points_to_info (stderr);
2139 /* Dump to FILE the list of variables that may be aliasing VAR. */
2141 void
2142 dump_may_aliases_for (FILE *file, tree var)
2144 varray_type aliases;
2146 if (TREE_CODE (var) == SSA_NAME)
2147 var = SSA_NAME_VAR (var);
2149 aliases = var_ann (var)->may_aliases;
2150 if (aliases)
2152 size_t i;
2153 fprintf (file, "{ ");
2154 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2156 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2157 fprintf (file, " ");
2159 fprintf (file, "}");
2164 /* Dump to stderr the list of variables that may be aliasing VAR. */
2166 void
2167 debug_may_aliases_for (tree var)
2169 dump_may_aliases_for (stderr, var);
2172 /* Return true if VAR may be aliased. */
2174 bool
2175 may_be_aliased (tree var)
2177 /* Obviously. */
2178 if (TREE_ADDRESSABLE (var))
2179 return true;
2181 /* Globally visible variables can have their addresses taken by other
2182 translation units. */
2183 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2184 return true;
2186 /* Automatic variables can't have their addresses escape any other way.
2187 This must be after the check for global variables, as extern declarations
2188 do not have TREE_STATIC set. */
2189 if (!TREE_STATIC (var))
2190 return false;
2192 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2193 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2194 we can only be sure the variable isn't addressable if it's local to the
2195 current function. */
2196 if (flag_unit_at_a_time)
2197 return false;
2198 if (decl_function_context (var) == current_function_decl)
2199 return false;
2201 return true;
2205 /* Given two symbols return TRUE if one is in the alias set of the other. */
2206 bool
2207 is_aliased_with (tree tag, tree sym)
2209 size_t i;
2210 varray_type aliases;
2212 if (var_ann (sym)->is_alias_tag)
2214 aliases = var_ann (tag)->may_aliases;
2216 if (aliases == NULL)
2217 return false;
2219 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2220 if (VARRAY_TREE (aliases, i) == sym)
2221 return true;
2223 else
2225 aliases = var_ann (sym)->may_aliases;
2227 if (aliases == NULL)
2228 return false;
2230 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2231 if (VARRAY_TREE (aliases, i) == tag)
2232 return true;
2235 return false;
2239 /* Add VAR to the list of may-aliases of PTR's type tag. If PTR
2240 doesn't already have a type tag, create one. */
2242 void
2243 add_type_alias (tree ptr, tree var)
2245 varray_type aliases;
2246 tree tag;
2247 var_ann_t ann = var_ann (ptr);
2248 subvar_t svars;
2249 VEC (tree, heap) *varvec = NULL;
2251 if (ann->type_mem_tag == NULL_TREE)
2253 tree q = NULL_TREE;
2254 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2255 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2256 safe_referenced_var_iterator rvi;
2258 /* PTR doesn't have a type tag, create a new one and add VAR to
2259 the new tag's alias set.
2261 FIXME, This is slower than necessary. We need to determine
2262 whether there is another pointer Q with the same alias set as
2263 PTR. This could be sped up by having type tags associated
2264 with types. */
2265 FOR_EACH_REFERENCED_VAR_SAFE (q, varvec, rvi)
2267 if (POINTER_TYPE_P (TREE_TYPE (q))
2268 && tag_set == get_alias_set (TREE_TYPE (TREE_TYPE (q))))
2270 /* Found another pointer Q with the same alias set as
2271 the PTR's pointed-to type. If Q has a type tag, use
2272 it. Otherwise, create a new memory tag for PTR. */
2273 var_ann_t ann1 = var_ann (q);
2274 if (ann1->type_mem_tag)
2275 ann->type_mem_tag = ann1->type_mem_tag;
2276 else
2277 ann->type_mem_tag = create_memory_tag (tag_type, true);
2278 goto found_tag;
2282 /* Couldn't find any other pointer with a type tag we could use.
2283 Create a new memory tag for PTR. */
2284 ann->type_mem_tag = create_memory_tag (tag_type, true);
2287 found_tag:
2288 /* If VAR is not already PTR's type tag, add it to the may-alias set
2289 for PTR's type tag. */
2290 gcc_assert (var_ann (var)->type_mem_tag == NOT_A_TAG);
2291 tag = ann->type_mem_tag;
2293 /* If VAR has subvars, add the subvars to the tag instead of the
2294 actual var. */
2295 if (var_can_have_subvars (var)
2296 && (svars = get_subvars_for_var (var)))
2298 subvar_t sv;
2299 for (sv = svars; sv; sv = sv->next)
2300 add_may_alias (tag, sv->var);
2302 else
2303 add_may_alias (tag, var);
2305 /* TAG and its set of aliases need to be marked for renaming. */
2306 mark_sym_for_renaming (tag);
2307 if ((aliases = var_ann (tag)->may_aliases) != NULL)
2309 size_t i;
2310 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2311 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2314 /* If we had grouped aliases, VAR may have aliases of its own. Mark
2315 them for renaming as well. Other statements referencing the
2316 aliases of VAR will need to be updated. */
2317 if ((aliases = var_ann (var)->may_aliases) != NULL)
2319 size_t i;
2320 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2321 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2323 VEC_free (tree, heap, varvec);
2327 /* Create a new type tag for PTR. Construct the may-alias list of this type
2328 tag so that it has the aliasing of VAR.
2330 Note, the set of aliases represented by the new type tag are not marked
2331 for renaming. */
2333 void
2334 new_type_alias (tree ptr, tree var)
2336 var_ann_t p_ann = var_ann (ptr);
2337 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2338 var_ann_t v_ann = var_ann (var);
2339 tree tag;
2340 subvar_t svars;
2342 gcc_assert (p_ann->type_mem_tag == NULL_TREE);
2343 gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG);
2345 /* Add VAR to the may-alias set of PTR's new type tag. If VAR has
2346 subvars, add the subvars to the tag instead of the actual var. */
2347 if (var_can_have_subvars (var)
2348 && (svars = get_subvars_for_var (var)))
2350 subvar_t sv;
2352 tag = create_memory_tag (tag_type, true);
2353 p_ann->type_mem_tag = tag;
2355 for (sv = svars; sv; sv = sv->next)
2356 add_may_alias (tag, sv->var);
2358 else
2360 /* The following is based on code in add_stmt_operand to ensure that the
2361 same defs/uses/vdefs/vuses will be found after replacing a reference
2362 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
2363 is the address of var. */
2364 varray_type aliases = v_ann->may_aliases;
2366 if ((aliases != NULL)
2367 && (VARRAY_ACTIVE_SIZE (aliases) == 1))
2369 tree ali = VARRAY_TREE (aliases, 0);
2371 if (get_var_ann (ali)->mem_tag_kind == TYPE_TAG)
2373 p_ann->type_mem_tag = ali;
2374 return;
2378 tag = create_memory_tag (tag_type, true);
2379 p_ann->type_mem_tag = tag;
2381 if (aliases == NULL)
2382 add_may_alias (tag, var);
2383 else
2385 size_t i;
2387 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2388 add_may_alias (tag, VARRAY_TREE (aliases, i));
2395 /* This represents the used range of a variable. */
2397 typedef struct used_part
2399 HOST_WIDE_INT minused;
2400 HOST_WIDE_INT maxused;
2401 /* True if we have an explicit use/def of some portion of this variable,
2402 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2403 bool explicit_uses;
2404 /* True if we have an implicit use/def of some portion of this
2405 variable. Implicit uses occur when we can't tell what part we
2406 are referencing, and have to make conservative assumptions. */
2407 bool implicit_uses;
2408 } *used_part_t;
2410 /* An array of used_part structures, indexed by variable uid. */
2412 static htab_t used_portions;
2414 struct used_part_map
2416 unsigned int uid;
2417 used_part_t to;
2420 /* Return true if the uid in the two used part maps are equal. */
2422 static int
2423 used_part_map_eq (const void *va, const void *vb)
2425 const struct used_part_map *a = va, *b = vb;
2426 return (a->uid == b->uid);
2429 /* Hash a from uid in a used_part_map. */
2431 static unsigned int
2432 used_part_map_hash (const void *item)
2434 return ((const struct used_part_map *)item)->uid;
2437 /* Free a used part map element. */
2439 static void
2440 free_used_part_map (void *item)
2442 free (((struct used_part_map *)item)->to);
2443 free (item);
2446 /* Lookup a used_part structure for a UID. */
2448 static used_part_t
2449 up_lookup (unsigned int uid)
2451 struct used_part_map *h, in;
2452 in.uid = uid;
2453 h = htab_find_with_hash (used_portions, &in, uid);
2454 if (!h)
2455 return NULL;
2456 return h->to;
2459 /* Insert the pair UID, TO into the used part hashtable. */
2461 static void
2462 up_insert (unsigned int uid, used_part_t to)
2464 struct used_part_map *h;
2465 void **loc;
2467 h = xmalloc (sizeof (struct used_part_map));
2468 h->uid = uid;
2469 h->to = to;
2470 loc = htab_find_slot_with_hash (used_portions, h,
2471 uid, INSERT);
2472 if (*loc != NULL)
2473 free (*loc);
2474 *(struct used_part_map **) loc = h;
2478 /* Given a variable uid, UID, get or create the entry in the used portions
2479 table for the variable. */
2481 static used_part_t
2482 get_or_create_used_part_for (size_t uid)
2484 used_part_t up;
2485 if ((up = up_lookup (uid)) == NULL)
2487 up = xcalloc (1, sizeof (struct used_part));
2488 up->minused = INT_MAX;
2489 up->maxused = 0;
2490 up->explicit_uses = false;
2491 up->implicit_uses = false;
2494 return up;
2498 /* Create and return a structure sub-variable for field FIELD of
2499 variable VAR. */
2501 static tree
2502 create_sft (tree var, tree field)
2504 var_ann_t ann;
2505 tree subvar = create_tmp_var_raw (TREE_TYPE (field), "SFT");
2507 /* We need to copy the various flags from VAR to SUBVAR, so that
2508 they are is_global_var iff the original variable was. */
2509 DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
2510 DECL_EXTERNAL (subvar) = DECL_EXTERNAL (var);
2511 TREE_PUBLIC (subvar) = TREE_PUBLIC (var);
2512 TREE_STATIC (subvar) = TREE_STATIC (var);
2513 TREE_READONLY (subvar) = TREE_READONLY (var);
2515 /* Add the new variable to REFERENCED_VARS. */
2516 ann = get_var_ann (subvar);
2517 ann->mem_tag_kind = STRUCT_FIELD;
2518 ann->type_mem_tag = NULL;
2519 add_referenced_tmp_var (subvar);
2521 return subvar;
2525 /* Given an aggregate VAR, create the subvariables that represent its
2526 fields. */
2528 static void
2529 create_overlap_variables_for (tree var)
2531 VEC(fieldoff_s,heap) *fieldstack = NULL;
2532 used_part_t up;
2533 size_t uid = DECL_UID (var);
2535 if (!up_lookup (uid))
2536 return;
2538 up = up_lookup (uid);
2539 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL);
2540 if (VEC_length (fieldoff_s, fieldstack) != 0)
2542 subvar_t *subvars;
2543 fieldoff_s *fo;
2544 bool notokay = false;
2545 int fieldcount = 0;
2546 int i;
2547 HOST_WIDE_INT lastfooffset = -1;
2548 HOST_WIDE_INT lastfosize = -1;
2549 tree lastfotype = NULL_TREE;
2551 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2552 know their size, and thus, can't handle.
2553 The same is true of fields with DECL_SIZE that is not an integer
2554 constant (such as variable sized fields).
2555 Fields with offsets which are not constant will have an offset < 0
2556 We *could* handle fields that are constant sized arrays, but
2557 currently don't. Doing so would require some extra changes to
2558 tree-ssa-operands.c. */
2560 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
2562 if (!DECL_SIZE (fo->field)
2563 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
2564 || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
2565 || fo->offset < 0)
2567 notokay = true;
2568 break;
2570 fieldcount++;
2573 /* The current heuristic we use is as follows:
2574 If the variable has no used portions in this function, no
2575 structure vars are created for it.
2576 Otherwise,
2577 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
2578 we always create structure vars for them.
2579 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2580 some explicit uses, we create structure vars for them.
2581 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2582 no explicit uses, we do not create structure vars for them.
2585 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
2586 && !up->explicit_uses)
2588 if (dump_file && (dump_flags & TDF_DETAILS))
2590 fprintf (dump_file, "Variable ");
2591 print_generic_expr (dump_file, var, 0);
2592 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
2594 notokay = true;
2597 /* Bail out, if we can't create overlap variables. */
2598 if (notokay)
2600 VEC_free (fieldoff_s, heap, fieldstack);
2601 return;
2604 /* Otherwise, create the variables. */
2605 subvars = lookup_subvars_for_var (var);
2607 sort_fieldstack (fieldstack);
2609 for (i = VEC_length (fieldoff_s, fieldstack);
2610 VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
2612 subvar_t sv;
2613 HOST_WIDE_INT fosize;
2614 tree currfotype;
2616 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
2617 currfotype = TREE_TYPE (fo->field);
2619 /* If this field isn't in the used portion,
2620 or it has the exact same offset and size as the last
2621 field, skip it. */
2623 if (((fo->offset <= up->minused
2624 && fo->offset + fosize <= up->minused)
2625 || fo->offset >= up->maxused)
2626 || (fo->offset == lastfooffset
2627 && fosize == lastfosize
2628 && currfotype == lastfotype))
2629 continue;
2630 sv = ggc_alloc (sizeof (struct subvar));
2631 sv->offset = fo->offset;
2632 sv->size = fosize;
2633 sv->next = *subvars;
2634 sv->var = create_sft (var, fo->field);
2636 if (dump_file)
2638 fprintf (dump_file, "structure field tag %s created for var %s",
2639 get_name (sv->var), get_name (var));
2640 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
2641 sv->offset);
2642 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
2643 sv->size);
2644 fprintf (dump_file, "\n");
2647 lastfotype = currfotype;
2648 lastfooffset = fo->offset;
2649 lastfosize = fosize;
2650 *subvars = sv;
2653 /* Once we have created subvars, the original is no longer call
2654 clobbered on its own. Its call clobbered status depends
2655 completely on the call clobbered status of the subvars.
2657 add_referenced_var in the above loop will take care of
2658 marking subvars of global variables as call clobbered for us
2659 to start, since they are global as well. */
2660 clear_call_clobbered (var);
2663 VEC_free (fieldoff_s, heap, fieldstack);
2667 /* Find the conservative answer to the question of what portions of what
2668 structures are used by this statement. We assume that if we have a
2669 component ref with a known size + offset, that we only need that part
2670 of the structure. For unknown cases, or cases where we do something
2671 to the whole structure, we assume we need to create fields for the
2672 entire structure. */
2674 static tree
2675 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2677 switch (TREE_CODE (*tp))
2679 case COMPONENT_REF:
2681 HOST_WIDE_INT bitsize;
2682 HOST_WIDE_INT bitpos;
2683 tree offset;
2684 enum machine_mode mode;
2685 int unsignedp;
2686 int volatilep;
2687 tree ref;
2688 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
2689 &unsignedp, &volatilep, false);
2690 if (DECL_P (ref) && offset == NULL && bitsize != -1)
2692 size_t uid = DECL_UID (ref);
2693 used_part_t up;
2695 up = get_or_create_used_part_for (uid);
2697 if (bitpos <= up->minused)
2698 up->minused = bitpos;
2699 if ((bitpos + bitsize >= up->maxused))
2700 up->maxused = bitpos + bitsize;
2702 up->explicit_uses = true;
2703 up_insert (uid, up);
2705 *walk_subtrees = 0;
2706 return NULL_TREE;
2708 else if (DECL_P (ref))
2710 if (DECL_SIZE (ref)
2711 && var_can_have_subvars (ref)
2712 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
2714 used_part_t up;
2715 size_t uid = DECL_UID (ref);
2717 up = get_or_create_used_part_for (uid);
2719 up->minused = 0;
2720 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
2722 up->implicit_uses = true;
2724 up_insert (uid, up);
2726 *walk_subtrees = 0;
2727 return NULL_TREE;
2731 break;
2732 /* This is here to make sure we mark the entire base variable as used
2733 when you take its address. Because our used portion analysis is
2734 simple, we aren't looking at casts or pointer arithmetic to see what
2735 happens when you take the address. */
2736 case ADDR_EXPR:
2738 tree var = get_base_address (TREE_OPERAND (*tp, 0));
2740 if (var
2741 && DECL_P (var)
2742 && DECL_SIZE (var)
2743 && var_can_have_subvars (var)
2744 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2746 used_part_t up;
2747 size_t uid = DECL_UID (var);
2749 up = get_or_create_used_part_for (uid);
2751 up->minused = 0;
2752 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
2753 up->implicit_uses = true;
2755 up_insert (uid, up);
2756 *walk_subtrees = 0;
2757 return NULL_TREE;
2760 break;
2761 case VAR_DECL:
2762 case PARM_DECL:
2764 tree var = *tp;
2765 if (DECL_SIZE (var)
2766 && var_can_have_subvars (var)
2767 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2769 used_part_t up;
2770 size_t uid = DECL_UID (var);
2772 up = get_or_create_used_part_for (uid);
2774 up->minused = 0;
2775 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
2776 up->implicit_uses = true;
2778 up_insert (uid, up);
2779 *walk_subtrees = 0;
2780 return NULL_TREE;
2783 break;
2785 default:
2786 break;
2789 return NULL_TREE;
2792 /* Create structure field variables for structures used in this function. */
2794 static void
2795 create_structure_vars (void)
2797 basic_block bb;
2798 safe_referenced_var_iterator rvi;
2799 VEC (tree, heap) *varvec = NULL;
2800 tree var;
2802 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
2803 free_used_part_map);
2805 FOR_EACH_BB (bb)
2807 block_stmt_iterator bsi;
2808 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2810 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
2811 find_used_portions,
2812 NULL);
2815 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, rvi)
2817 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
2818 if (var
2819 && DECL_SIZE (var)
2820 && var_can_have_subvars (var)
2821 && var_ann (var)->mem_tag_kind == NOT_A_TAG
2822 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2823 create_overlap_variables_for (var);
2825 htab_delete (used_portions);
2826 VEC_free (tree, heap, varvec);
2830 static bool
2831 gate_structure_vars (void)
2833 return flag_tree_salias != 0;
2836 struct tree_opt_pass pass_create_structure_vars =
2838 "salias", /* name */
2839 gate_structure_vars, /* gate */
2840 create_structure_vars, /* execute */
2841 NULL, /* sub */
2842 NULL, /* next */
2843 0, /* static_pass_number */
2844 0, /* tv_id */
2845 PROP_cfg, /* properties_required */
2846 0, /* properties_provided */
2847 0, /* properties_destroyed */
2848 0, /* todo_flags_start */
2849 TODO_dump_func, /* todo_flags_finish */
2850 0 /* letter */