2005-10-18 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / gcc / tree-ssa-alias.c
blob1f6eae717d215147a851d58ab80ede410cb8d968
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;
138 DEF_VEC_I(int);
139 DEF_VEC_ALLOC_I(int,heap);
141 /* Return true if TAG can touch global memory. */
142 static bool
143 tag_marked_global (tree tag)
145 gcc_assert (MTAG_P (tag));
146 return MTAG_GLOBAL (tag);
149 /* Mark TAG, an alias tag, as possibly touching global memory. */
150 static void
151 mark_tag_global (tree tag)
153 gcc_assert (MTAG_P (tag));
154 MTAG_GLOBAL (tag) = 1;
157 /* qsort comparison function to sort type/name tags by DECL_UID. */
159 static int
160 sort_tags_by_id (const void *pa, const void *pb)
162 tree a = *(tree *)pa;
163 tree b = *(tree *)pb;
165 return DECL_UID (a) - DECL_UID (b);
168 /* Initialize WORKLIST to contain those memory tags that are marked call
169 clobbered. Initialized WORKLIST2 to contain the reasons these
170 memory tags escaped. */
172 static void
173 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
174 VEC (int, heap) **worklist2)
176 referenced_var_iterator rvi;
177 tree curr;
179 FOR_EACH_REFERENCED_VAR (curr, rvi)
181 if (MTAG_P (curr) && is_call_clobbered (curr))
183 VEC_safe_push (tree, heap, *worklist, curr);
184 VEC_safe_push (int, heap, *worklist2, var_ann (curr)->escape_mask);
189 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
190 ALIAS is not already marked call clobbered, and is a memory
191 tag. */
193 static void
194 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
195 VEC (int, heap) **worklist2,
196 int reason)
198 if (MTAG_P (alias) && !is_call_clobbered (alias))
200 VEC_safe_push (tree, heap, *worklist, alias);
201 VEC_safe_push (int, heap, *worklist2, reason);
205 /* Mark aliases of TAG as call clobbered, and place any tags on the
206 alias list that were not already call clobbered on WORKLIST. */
208 static void
209 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
210 VEC (int, heap) **worklist2)
212 unsigned int i;
213 varray_type ma;
214 var_ann_t ta = var_ann (tag);
216 if (!MTAG_P (tag))
217 return;
218 ma = may_aliases (tag);
219 if (!ma)
220 return;
222 for (i = 0; i < VARRAY_ACTIVE_SIZE (ma); i++)
224 tree entry = VARRAY_TREE (ma, i);
225 if (!unmodifiable_var_p (entry))
227 add_to_worklist (entry, worklist, worklist2, ta->escape_mask);
228 mark_call_clobbered (entry, ta->escape_mask);
233 /* Tags containing global vars need to be marked as global.
234 Tags containing call clobbered vars need to be marked as call
235 clobbered. */
237 static void
238 compute_tag_properties (void)
240 referenced_var_iterator rvi;
241 tree tag;
242 bool changed = true;
243 VEC (tree, heap) *taglist = NULL;
245 FOR_EACH_REFERENCED_VAR (tag, rvi)
247 if (!MTAG_P (tag) || TREE_CODE (tag) == STRUCT_FIELD_TAG)
248 continue;
249 VEC_safe_push (tree, heap, taglist, tag);
252 /* We sort the taglist by DECL_UID, for two reasons.
253 1. To get a sequential ordering to make the bitmap accesses
254 faster.
255 2. Because of the way we compute aliases, it's more likely that
256 an earlier tag is included in a later tag, and this will reduce
257 the number of iterations.
259 If we had a real tag graph, we would just topo-order it and be
260 done with it. */
261 qsort (VEC_address (tree, taglist),
262 VEC_length (tree, taglist),
263 sizeof (tree),
264 sort_tags_by_id);
266 /* Go through each tag not marked as global, and if it aliases
267 global vars, mark it global.
269 If the tag contains call clobbered vars, mark it call
270 clobbered. */
272 while (changed)
274 unsigned int k;
276 changed = false;
277 for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
279 varray_type ma;
280 unsigned int i;
282 if (is_call_clobbered (tag) && tag_marked_global (tag))
283 continue;
285 ma = may_aliases (tag);
286 if (!ma)
287 continue;
289 for (i = 0; i < VARRAY_ACTIVE_SIZE (ma); i++)
291 tree entry = VARRAY_TREE (ma, i);
293 /* Call clobbered entries cause the tag to be marked
294 call clobbered. */
295 if (is_call_clobbered (entry) && !is_call_clobbered (tag))
297 mark_call_clobbered (tag, var_ann (entry)->escape_mask);
298 changed = true;
301 /* Global vars cause the tag to be marked global. */
302 if (is_global_var (entry) && !tag_marked_global (tag))
304 mark_tag_global (tag);
305 changed = true;
310 VEC_free (tree, heap, taglist);
313 /* Set up the initial variable clobbers and globalness.
314 When this function completes, only tags whose aliases need to be
315 clobbered will be set clobbered. Tags clobbered because they
316 contain call clobbered vars are handled in compute_tag_properties. */
318 static void
319 set_initial_properties (struct alias_info *ai)
321 unsigned int i;
322 referenced_var_iterator rvi;
323 tree var;
325 FOR_EACH_REFERENCED_VAR (var, rvi)
327 if (is_global_var (var)
328 && (!var_can_have_subvars (var)
329 || get_subvars_for_var (var) == NULL))
331 if (!unmodifiable_var_p (var))
332 mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
334 else if (TREE_CODE (var) == PARM_DECL
335 && default_def (var)
336 && POINTER_TYPE_P (TREE_TYPE (var)))
338 tree def = default_def (var);
339 get_ptr_info (def)->value_escapes_p = 1;
340 get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;
344 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
346 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
347 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
348 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
350 if (pi->value_escapes_p)
352 /* If PTR escapes then its associated memory tags and
353 pointed-to variables are call-clobbered. */
354 if (pi->name_mem_tag)
355 mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
357 if (v_ann->type_mem_tag)
358 mark_call_clobbered (v_ann->type_mem_tag, pi->escape_mask);
360 if (pi->pt_vars)
362 bitmap_iterator bi;
363 unsigned int j;
364 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
365 if (!unmodifiable_var_p (referenced_var (j)))
366 mark_call_clobbered (referenced_var (j), pi->escape_mask);
369 /* If the name tag is call clobbered, so is the type tag
370 associated with the base VAR_DECL. */
371 if (pi->name_mem_tag
372 && v_ann->type_mem_tag
373 && is_call_clobbered (pi->name_mem_tag))
374 mark_call_clobbered (v_ann->type_mem_tag, pi->escape_mask);
376 if ((pi->pt_global_mem || pi->pt_anything) && pi->name_mem_tag)
377 mark_tag_global (pi->name_mem_tag);
378 if ((pi->pt_global_mem || pi->pt_anything) && v_ann->type_mem_tag)
379 mark_tag_global (v_ann->type_mem_tag);
382 /* Compute which variables need to be marked call clobbered because
383 their tag is call clobbered, and which tags need to be marked
384 global because they contain global variables. */
386 static void
387 compute_call_clobbered (struct alias_info *ai)
389 VEC (tree, heap) *worklist = NULL;
390 VEC(int,heap) *worklist2 = NULL;
392 set_initial_properties (ai);
393 init_transitive_clobber_worklist (&worklist, &worklist2);
394 while (VEC_length (tree, worklist) != 0)
396 tree curr = VEC_pop (tree, worklist);
397 int reason = VEC_pop (int, worklist2);
399 mark_call_clobbered (curr, reason);
400 mark_aliases_call_clobbered (curr, &worklist, &worklist2);
402 VEC_free (tree, heap, worklist);
403 VEC_free (int, heap, worklist2);
404 compute_tag_properties ();
407 /* Compute may-alias information for every variable referenced in function
408 FNDECL.
410 Alias analysis proceeds in 3 main phases:
412 1- Points-to and escape analysis.
414 This phase walks the use-def chains in the SSA web looking for three
415 things:
417 * Assignments of the form P_i = &VAR
418 * Assignments of the form P_i = malloc()
419 * Pointers and ADDR_EXPR that escape the current function.
421 The concept of 'escaping' is the same one used in the Java world. When
422 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
423 outside of the current function. So, assignment to global variables,
424 function arguments and returning a pointer are all escape sites, as are
425 conversions between pointers and integers.
427 This is where we are currently limited. Since not everything is renamed
428 into SSA, we lose track of escape properties when a pointer is stashed
429 inside a field in a structure, for instance. In those cases, we are
430 assuming that the pointer does escape.
432 We use escape analysis to determine whether a variable is
433 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
434 is call-clobbered. If a pointer P_i escapes, then all the variables
435 pointed-to by P_i (and its memory tag) also escape.
437 2- Compute flow-sensitive aliases
439 We have two classes of memory tags. Memory tags associated with the
440 pointed-to data type of the pointers in the program. These tags are
441 called "type memory tag" (TMT). The other class are those associated
442 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
443 when adding operands for an INDIRECT_REF *P_i, we will first check
444 whether P_i has a name tag, if it does we use it, because that will have
445 more precise aliasing information. Otherwise, we use the standard type
446 tag.
448 In this phase, we go through all the pointers we found in points-to
449 analysis and create alias sets for the name memory tags associated with
450 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
451 it points to and its tag.
454 3- Compute flow-insensitive aliases
456 This pass will compare the alias set of every type memory tag and every
457 addressable variable found in the program. Given a type memory tag TMT
458 and an addressable variable V. If the alias sets of TMT and V conflict
459 (as computed by may_alias_p), then V is marked as an alias tag and added
460 to the alias set of TMT.
462 For instance, consider the following function:
464 foo (int i)
466 int *p, a, b;
468 if (i > 10)
469 p = &a;
470 else
471 p = &b;
473 *p = 3;
474 a = b + 2;
475 return *p;
478 After aliasing analysis has finished, the type memory tag for pointer
479 'p' will have two aliases, namely variables 'a' and 'b'. Every time
480 pointer 'p' is dereferenced, we want to mark the operation as a
481 potential reference to 'a' and 'b'.
483 foo (int i)
485 int *p, a, b;
487 if (i_2 > 10)
488 p_4 = &a;
489 else
490 p_6 = &b;
491 # p_1 = PHI <p_4(1), p_6(2)>;
493 # a_7 = V_MAY_DEF <a_3>;
494 # b_8 = V_MAY_DEF <b_5>;
495 *p_1 = 3;
497 # a_9 = V_MAY_DEF <a_7>
498 # VUSE <b_8>
499 a_9 = b_8 + 2;
501 # VUSE <a_9>;
502 # VUSE <b_8>;
503 return *p_1;
506 In certain cases, the list of may aliases for a pointer may grow too
507 large. This may cause an explosion in the number of virtual operands
508 inserted in the code. Resulting in increased memory consumption and
509 compilation time.
511 When the number of virtual operands needed to represent aliased
512 loads and stores grows too large (configurable with @option{--param
513 max-aliased-vops}), alias sets are grouped to avoid severe
514 compile-time slow downs and memory consumption. See group_aliases. */
516 static void
517 compute_may_aliases (void)
519 struct alias_info *ai;
521 memset (&alias_stats, 0, sizeof (alias_stats));
523 /* Initialize aliasing information. */
524 ai = init_alias_info ();
526 /* For each pointer P_i, determine the sets of variables that P_i may
527 point-to. For every addressable variable V, determine whether the
528 address of V escapes the current function, making V call-clobbered
529 (i.e., whether &V is stored in a global variable or if its passed as a
530 function call argument). */
531 compute_points_to_sets (ai);
533 /* Collect all pointers and addressable variables, compute alias sets,
534 create memory tags for pointers and promote variables whose address is
535 not needed anymore. */
536 setup_pointers_and_addressables (ai);
538 /* Compute flow-sensitive, points-to based aliasing for all the name
539 memory tags. Note that this pass needs to be done before flow
540 insensitive analysis because it uses the points-to information
541 gathered before to mark call-clobbered type tags. */
542 compute_flow_sensitive_aliasing (ai);
544 /* Compute type-based flow-insensitive aliasing for all the type
545 memory tags. */
546 compute_flow_insensitive_aliasing (ai);
548 /* Determine if we need to enable alias grouping. */
549 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
550 group_aliases (ai);
552 /* Compute call clobbering information. */
553 compute_call_clobbered (ai);
555 /* If the program has too many call-clobbered variables and/or function
556 calls, create .GLOBAL_VAR and use it to model call-clobbering
557 semantics at call sites. This reduces the number of virtual operands
558 considerably, improving compile times at the expense of lost
559 aliasing precision. */
560 maybe_create_global_var (ai);
562 /* Debugging dumps. */
563 if (dump_file)
565 dump_referenced_vars (dump_file);
566 if (dump_flags & TDF_STATS)
567 dump_alias_stats (dump_file);
568 dump_points_to_info (dump_file);
569 dump_alias_info (dump_file);
572 /* Deallocate memory used by aliasing data structures. */
573 delete_alias_info (ai);
576 block_stmt_iterator bsi;
577 basic_block bb;
578 FOR_EACH_BB (bb)
580 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
582 update_stmt_if_modified (bsi_stmt (bsi));
589 struct tree_opt_pass pass_may_alias =
591 "alias", /* name */
592 NULL, /* gate */
593 compute_may_aliases, /* execute */
594 NULL, /* sub */
595 NULL, /* next */
596 0, /* static_pass_number */
597 TV_TREE_MAY_ALIAS, /* tv_id */
598 PROP_cfg | PROP_ssa, /* properties_required */
599 PROP_alias, /* properties_provided */
600 0, /* properties_destroyed */
601 0, /* todo_flags_start */
602 TODO_dump_func | TODO_update_ssa
603 | TODO_ggc_collect | TODO_verify_ssa
604 | TODO_verify_stmts, /* todo_flags_finish */
605 0 /* letter */
609 /* Data structure used to count the number of dereferences to PTR
610 inside an expression. */
611 struct count_ptr_d
613 tree ptr;
614 unsigned count;
618 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
619 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
621 static tree
622 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
624 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
626 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
627 pointer 'ptr' is *not* dereferenced, it is simply used to compute
628 the address of 'fld' as 'ptr + offsetof(fld)'. */
629 if (TREE_CODE (*tp) == ADDR_EXPR)
631 *walk_subtrees = 0;
632 return NULL_TREE;
635 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
636 count_p->count++;
638 return NULL_TREE;
642 /* Count the number of direct and indirect uses for pointer PTR in
643 statement STMT. The two counts are stored in *NUM_USES_P and
644 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
645 least one of those dereferences is a store operation. */
647 void
648 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
649 unsigned *num_derefs_p, bool *is_store)
651 ssa_op_iter i;
652 tree use;
654 *num_uses_p = 0;
655 *num_derefs_p = 0;
656 *is_store = false;
658 /* Find out the total number of uses of PTR in STMT. */
659 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
660 if (use == ptr)
661 (*num_uses_p)++;
663 /* Now count the number of indirect references to PTR. This is
664 truly awful, but we don't have much choice. There are no parent
665 pointers inside INDIRECT_REFs, so an expression like
666 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
667 find all the indirect and direct uses of x_1 inside. The only
668 shortcut we can take is the fact that GIMPLE only allows
669 INDIRECT_REFs inside the expressions below. */
670 if (TREE_CODE (stmt) == MODIFY_EXPR
671 || (TREE_CODE (stmt) == RETURN_EXPR
672 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
673 || TREE_CODE (stmt) == ASM_EXPR
674 || TREE_CODE (stmt) == CALL_EXPR)
676 tree lhs, rhs;
678 if (TREE_CODE (stmt) == MODIFY_EXPR)
680 lhs = TREE_OPERAND (stmt, 0);
681 rhs = TREE_OPERAND (stmt, 1);
683 else if (TREE_CODE (stmt) == RETURN_EXPR)
685 tree e = TREE_OPERAND (stmt, 0);
686 lhs = TREE_OPERAND (e, 0);
687 rhs = TREE_OPERAND (e, 1);
689 else if (TREE_CODE (stmt) == ASM_EXPR)
691 lhs = ASM_OUTPUTS (stmt);
692 rhs = ASM_INPUTS (stmt);
694 else
696 lhs = NULL_TREE;
697 rhs = stmt;
700 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
702 struct count_ptr_d count;
703 count.ptr = ptr;
704 count.count = 0;
705 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
706 *is_store = true;
707 *num_derefs_p = count.count;
710 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
712 struct count_ptr_d count;
713 count.ptr = ptr;
714 count.count = 0;
715 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
716 *num_derefs_p += count.count;
720 gcc_assert (*num_uses_p >= *num_derefs_p);
723 /* Initialize the data structures used for alias analysis. */
725 static struct alias_info *
726 init_alias_info (void)
728 struct alias_info *ai;
729 referenced_var_iterator rvi;
730 tree var;
732 bitmap_obstack_initialize (&alias_obstack);
733 ai = xcalloc (1, sizeof (struct alias_info));
734 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
735 sbitmap_zero (ai->ssa_names_visited);
736 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
737 ai->written_vars = BITMAP_ALLOC (&alias_obstack);
738 ai->dereferenced_ptrs_store = BITMAP_ALLOC (&alias_obstack);
739 ai->dereferenced_ptrs_load = BITMAP_ALLOC (&alias_obstack);
741 /* If aliases have been computed before, clear existing information. */
742 if (aliases_computed_p)
744 unsigned i;
746 /* Similarly, clear the set of addressable variables. In this
747 case, we can just clear the set because addressability is
748 only computed here. */
749 bitmap_clear (addressable_vars);
751 /* Clear flow-insensitive alias information from each symbol. */
752 FOR_EACH_REFERENCED_VAR (var, rvi)
754 var_ann_t ann = var_ann (var);
756 ann->is_alias_tag = 0;
757 ann->may_aliases = NULL;
758 NUM_REFERENCES_CLEAR (ann);
760 /* Since we are about to re-discover call-clobbered
761 variables, clear the call-clobbered flag. Variables that
762 are intrinsically call-clobbered (globals, local statics,
763 etc) will not be marked by the aliasing code, so we can't
764 remove them from CALL_CLOBBERED_VARS.
766 NB: STRUCT_FIELDS are still call clobbered if they are for
767 a global variable, so we *don't* clear their call clobberedness
768 just because they are tags, though we will clear it if they
769 aren't for global variables. */
770 if (TREE_CODE (var) == NAME_MEMORY_TAG
771 || TREE_CODE (var) == TYPE_MEMORY_TAG
772 || !is_global_var (var))
773 clear_call_clobbered (var);
776 /* Clear flow-sensitive points-to information from each SSA name. */
777 for (i = 1; i < num_ssa_names; i++)
779 tree name = ssa_name (i);
781 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
782 continue;
784 if (SSA_NAME_PTR_INFO (name))
786 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
788 /* Clear all the flags but keep the name tag to
789 avoid creating new temporaries unnecessarily. If
790 this pointer is found to point to a subset or
791 superset of its former points-to set, then a new
792 tag will need to be created in create_name_tags. */
793 pi->pt_anything = 0;
794 pi->pt_null = 0;
795 pi->value_escapes_p = 0;
796 pi->is_dereferenced = 0;
797 if (pi->pt_vars)
798 bitmap_clear (pi->pt_vars);
803 /* Next time, we will need to reset alias information. */
804 aliases_computed_p = true;
806 return ai;
810 /* Deallocate memory used by alias analysis. */
812 static void
813 delete_alias_info (struct alias_info *ai)
815 size_t i;
816 referenced_var_iterator rvi;
817 tree var;
819 sbitmap_free (ai->ssa_names_visited);
820 ai->processed_ptrs = NULL;
822 for (i = 0; i < ai->num_addressable_vars; i++)
823 free (ai->addressable_vars[i]);
825 FOR_EACH_REFERENCED_VAR(var, rvi)
827 var_ann_t ann = var_ann (var);
828 NUM_REFERENCES_CLEAR (ann);
831 free (ai->addressable_vars);
833 for (i = 0; i < ai->num_pointers; i++)
834 free (ai->pointers[i]);
835 free (ai->pointers);
837 BITMAP_FREE (ai->written_vars);
838 BITMAP_FREE (ai->dereferenced_ptrs_store);
839 BITMAP_FREE (ai->dereferenced_ptrs_load);
840 bitmap_obstack_release (&alias_obstack);
841 free (ai);
843 delete_points_to_sets ();
846 /* Create name tags for all the pointers that have been dereferenced.
847 We only create a name tag for a pointer P if P is found to point to
848 a set of variables (so that we can alias them to *P) or if it is
849 the result of a call to malloc (which means that P cannot point to
850 anything else nor alias any other variable).
852 If two pointers P and Q point to the same set of variables, they
853 are assigned the same name tag. */
855 static void
856 create_name_tags (void)
858 size_t i;
859 VEC (tree, heap) *with_ptvars = NULL;
860 tree ptr;
862 /* Collect the list of pointers with a non-empty points to set. */
863 for (i = 1; i < num_ssa_names; i++)
865 tree ptr = ssa_name (i);
866 struct ptr_info_def *pi;
868 if (!ptr
869 || !POINTER_TYPE_P (TREE_TYPE (ptr))
870 || !SSA_NAME_PTR_INFO (ptr))
871 continue;
873 pi = SSA_NAME_PTR_INFO (ptr);
875 if (pi->pt_anything || !pi->is_dereferenced)
877 /* No name tags for pointers that have not been
878 dereferenced or point to an arbitrary location. */
879 pi->name_mem_tag = NULL_TREE;
880 continue;
883 /* Set pt_anything on the pointers without pt_vars filled in so
884 that they are assigned a type tag. */
886 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
887 VEC_safe_push (tree, heap, with_ptvars, ptr);
888 else
889 set_pt_anything (ptr);
892 /* If we didn't find any pointers with pt_vars set, we're done. */
893 if (!with_ptvars)
894 return;
896 /* Now go through the pointers with pt_vars, and find a name tag
897 with the same pt_vars as this pointer, or create one if one
898 doesn't exist. */
899 for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
901 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
902 size_t j;
903 tree ptr2;
904 tree old_name_tag = pi->name_mem_tag;
906 /* If PTR points to a set of variables, check if we don't
907 have another pointer Q with the same points-to set before
908 creating a tag. If so, use Q's tag instead of creating a
909 new one.
911 This is important for not creating unnecessary symbols
912 and also for copy propagation. If we ever need to
913 propagate PTR into Q or vice-versa, we would run into
914 problems if they both had different name tags because
915 they would have different SSA version numbers (which
916 would force us to take the name tags in and out of SSA). */
917 for (j = 0; j < i && VEC_iterate (tree, with_ptvars, j, ptr2); j++)
919 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (ptr2);
921 if (bitmap_equal_p (pi->pt_vars, qi->pt_vars))
923 pi->name_mem_tag = qi->name_mem_tag;
924 break;
928 /* If we didn't find a pointer with the same points-to set
929 as PTR, create a new name tag if needed. */
930 if (pi->name_mem_tag == NULL_TREE)
931 pi->name_mem_tag = get_nmt_for (ptr);
933 /* If the new name tag computed for PTR is different than
934 the old name tag that it used to have, then the old tag
935 needs to be removed from the IL, so we mark it for
936 renaming. */
937 if (old_name_tag && old_name_tag != pi->name_mem_tag)
938 mark_sym_for_renaming (old_name_tag);
940 TREE_THIS_VOLATILE (pi->name_mem_tag)
941 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
943 /* Mark the new name tag for renaming. */
944 mark_sym_for_renaming (pi->name_mem_tag);
947 VEC_free (tree, heap, with_ptvars);
951 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
952 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
953 name tag and the variables it points-to are call-clobbered. Finally, if
954 P_i escapes and we could not determine where it points to, then all the
955 variables in the same alias set as *P_i are marked call-clobbered. This
956 is necessary because we must assume that P_i may take the address of any
957 variable in the same alias set. */
959 static void
960 compute_flow_sensitive_aliasing (struct alias_info *ai)
962 size_t i;
964 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
966 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
967 if (!find_what_p_points_to (ptr))
968 set_pt_anything (ptr);
971 create_name_tags ();
973 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
975 unsigned j;
976 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
977 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
978 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
979 bitmap_iterator bi;
982 /* Set up aliasing information for PTR's name memory tag (if it has
983 one). Note that only pointers that have been dereferenced will
984 have a name memory tag. */
985 if (pi->name_mem_tag && pi->pt_vars)
986 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
988 add_may_alias (pi->name_mem_tag, referenced_var (j));
989 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
995 /* Compute type-based alias sets. Traverse all the pointers and
996 addressable variables found in setup_pointers_and_addressables.
998 For every pointer P in AI->POINTERS and addressable variable V in
999 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
1000 memory tag (TMT) if their alias sets conflict. V is then marked as
1001 an alias tag so that the operand scanner knows that statements
1002 containing V have aliased operands. */
1004 static void
1005 compute_flow_insensitive_aliasing (struct alias_info *ai)
1007 size_t i;
1009 /* Initialize counter for the total number of virtual operands that
1010 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
1011 threshold set by --params max-alias-vops, we enable alias
1012 grouping. */
1013 ai->total_alias_vops = 0;
1015 /* For every pointer P, determine which addressable variables may alias
1016 with P's type memory tag. */
1017 for (i = 0; i < ai->num_pointers; i++)
1019 size_t j;
1020 struct alias_map_d *p_map = ai->pointers[i];
1021 tree tag = var_ann (p_map->var)->type_mem_tag;
1022 var_ann_t tag_ann = var_ann (tag);
1024 p_map->total_alias_vops = 0;
1025 p_map->may_aliases = BITMAP_ALLOC (&alias_obstack);
1027 for (j = 0; j < ai->num_addressable_vars; j++)
1029 struct alias_map_d *v_map;
1030 var_ann_t v_ann;
1031 tree var;
1032 bool tag_stored_p, var_stored_p;
1034 v_map = ai->addressable_vars[j];
1035 var = v_map->var;
1036 v_ann = var_ann (var);
1038 /* Skip memory tags and variables that have never been
1039 written to. We also need to check if the variables are
1040 call-clobbered because they may be overwritten by
1041 function calls.
1043 Note this is effectively random accessing elements in
1044 the sparse bitset, which can be highly inefficient.
1045 So we first check the call_clobbered status of the
1046 tag and variable before querying the bitmap. */
1047 tag_stored_p = is_call_clobbered (tag)
1048 || bitmap_bit_p (ai->written_vars, DECL_UID (tag));
1049 var_stored_p = is_call_clobbered (var)
1050 || bitmap_bit_p (ai->written_vars, DECL_UID (var));
1051 if (!tag_stored_p && !var_stored_p)
1052 continue;
1054 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
1056 size_t num_tag_refs, num_var_refs;
1058 num_tag_refs = NUM_REFERENCES (tag_ann);
1059 num_var_refs = NUM_REFERENCES (v_ann);
1061 /* Add VAR to TAG's may-aliases set. */
1063 /* We should never have a var with subvars here, because
1064 they shouldn't get into the set of addressable vars */
1065 gcc_assert (!var_can_have_subvars (var)
1066 || get_subvars_for_var (var) == NULL);
1068 add_may_alias (tag, var);
1069 /* Update the bitmap used to represent TAG's alias set
1070 in case we need to group aliases. */
1071 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
1073 /* Update the total number of virtual operands due to
1074 aliasing. Since we are adding one more alias to TAG's
1075 may-aliases set, the total number of virtual operands due
1076 to aliasing will be increased by the number of references
1077 made to VAR and TAG (every reference to TAG will also
1078 count as a reference to VAR). */
1079 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1080 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1087 /* Since this analysis is based exclusively on symbols, it fails to
1088 handle cases where two pointers P and Q have different memory
1089 tags with conflicting alias set numbers but no aliased symbols in
1090 common.
1092 For example, suppose that we have two memory tags TMT.1 and TMT.2
1093 such that
1095 may-aliases (TMT.1) = { a }
1096 may-aliases (TMT.2) = { b }
1098 and the alias set number of TMT.1 conflicts with that of TMT.2.
1099 Since they don't have symbols in common, loads and stores from
1100 TMT.1 and TMT.2 will seem independent of each other, which will
1101 lead to the optimizers making invalid transformations (see
1102 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1104 To avoid this problem, we do a final traversal of AI->POINTERS
1105 looking for pairs of pointers that have no aliased symbols in
1106 common and yet have conflicting alias set numbers. */
1107 for (i = 0; i < ai->num_pointers; i++)
1109 size_t j;
1110 struct alias_map_d *p_map1 = ai->pointers[i];
1111 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1112 bitmap may_aliases1 = p_map1->may_aliases;
1114 for (j = i + 1; j < ai->num_pointers; j++)
1116 struct alias_map_d *p_map2 = ai->pointers[j];
1117 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1118 bitmap may_aliases2 = p_map2->may_aliases;
1120 /* If the pointers may not point to each other, do nothing. */
1121 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
1122 continue;
1124 /* The two pointers may alias each other. If they already have
1125 symbols in common, do nothing. */
1126 if (bitmap_intersect_p (may_aliases1, may_aliases2))
1127 continue;
1129 if (!bitmap_empty_p (may_aliases2))
1131 unsigned int k;
1132 bitmap_iterator bi;
1134 /* Add all the aliases for TAG2 into TAG1's alias set.
1135 FIXME, update grouping heuristic counters. */
1136 EXECUTE_IF_SET_IN_BITMAP (may_aliases2, 0, k, bi)
1137 add_may_alias (tag1, referenced_var (k));
1138 bitmap_ior_into (may_aliases1, may_aliases2);
1140 else
1142 /* Since TAG2 does not have any aliases of its own, add
1143 TAG2 itself to the alias set of TAG1. */
1144 add_may_alias (tag1, tag2);
1145 bitmap_set_bit (may_aliases1, DECL_UID (tag2));
1150 if (dump_file)
1151 fprintf (dump_file, "\n%s: Total number of aliased vops: %ld\n",
1152 get_name (current_function_decl),
1153 ai->total_alias_vops);
1157 /* Comparison function for qsort used in group_aliases. */
1159 static int
1160 total_alias_vops_cmp (const void *p, const void *q)
1162 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1163 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1164 long n1 = (*p1)->total_alias_vops;
1165 long n2 = (*p2)->total_alias_vops;
1167 /* We want to sort in descending order. */
1168 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1171 /* Group all the aliases for TAG to make TAG represent all the
1172 variables in its alias set. Update the total number
1173 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1174 function will make TAG be the unique alias tag for all the
1175 variables in its may-aliases. So, given:
1177 may-aliases(TAG) = { V1, V2, V3 }
1179 This function will group the variables into:
1181 may-aliases(V1) = { TAG }
1182 may-aliases(V2) = { TAG }
1183 may-aliases(V2) = { TAG } */
1185 static void
1186 group_aliases_into (tree tag, bitmap tag_aliases, struct alias_info *ai)
1188 unsigned int i;
1189 var_ann_t tag_ann = var_ann (tag);
1190 size_t num_tag_refs = NUM_REFERENCES (tag_ann);
1191 bitmap_iterator bi;
1193 EXECUTE_IF_SET_IN_BITMAP (tag_aliases, 0, i, bi)
1195 tree var = referenced_var (i);
1196 var_ann_t ann = var_ann (var);
1198 /* Make TAG the unique alias of VAR. */
1199 ann->is_alias_tag = 0;
1200 ann->may_aliases = NULL;
1202 /* Note that VAR and TAG may be the same if the function has no
1203 addressable variables (see the discussion at the end of
1204 setup_pointers_and_addressables). */
1205 if (var != tag)
1206 add_may_alias (var, tag);
1208 /* Reduce total number of virtual operands contributed
1209 by TAG on behalf of VAR. Notice that the references to VAR
1210 itself won't be removed. We will merely replace them with
1211 references to TAG. */
1212 ai->total_alias_vops -= num_tag_refs;
1215 /* We have reduced the number of virtual operands that TAG makes on
1216 behalf of all the variables formerly aliased with it. However,
1217 we have also "removed" all the virtual operands for TAG itself,
1218 so we add them back. */
1219 ai->total_alias_vops += num_tag_refs;
1221 /* TAG no longer has any aliases. */
1222 tag_ann->may_aliases = NULL;
1226 /* Group may-aliases sets to reduce the number of virtual operands due
1227 to aliasing.
1229 1- Sort the list of pointers in decreasing number of contributed
1230 virtual operands.
1232 2- Take the first entry in AI->POINTERS and revert the role of
1233 the memory tag and its aliases. Usually, whenever an aliased
1234 variable Vi is found to alias with a memory tag T, we add Vi
1235 to the may-aliases set for T. Meaning that after alias
1236 analysis, we will have:
1238 may-aliases(T) = { V1, V2, V3, ..., Vn }
1240 This means that every statement that references T, will get 'n'
1241 virtual operands for each of the Vi tags. But, when alias
1242 grouping is enabled, we make T an alias tag and add it to the
1243 alias set of all the Vi variables:
1245 may-aliases(V1) = { T }
1246 may-aliases(V2) = { T }
1248 may-aliases(Vn) = { T }
1250 This has two effects: (a) statements referencing T will only get
1251 a single virtual operand, and, (b) all the variables Vi will now
1252 appear to alias each other. So, we lose alias precision to
1253 improve compile time. But, in theory, a program with such a high
1254 level of aliasing should not be very optimizable in the first
1255 place.
1257 3- Since variables may be in the alias set of more than one
1258 memory tag, the grouping done in step (2) needs to be extended
1259 to all the memory tags that have a non-empty intersection with
1260 the may-aliases set of tag T. For instance, if we originally
1261 had these may-aliases sets:
1263 may-aliases(T) = { V1, V2, V3 }
1264 may-aliases(R) = { V2, V4 }
1266 In step (2) we would have reverted the aliases for T as:
1268 may-aliases(V1) = { T }
1269 may-aliases(V2) = { T }
1270 may-aliases(V3) = { T }
1272 But note that now V2 is no longer aliased with R. We could
1273 add R to may-aliases(V2), but we are in the process of
1274 grouping aliases to reduce virtual operands so what we do is
1275 add V4 to the grouping to obtain:
1277 may-aliases(V1) = { T }
1278 may-aliases(V2) = { T }
1279 may-aliases(V3) = { T }
1280 may-aliases(V4) = { T }
1282 4- If the total number of virtual operands due to aliasing is
1283 still above the threshold set by max-alias-vops, go back to (2). */
1285 static void
1286 group_aliases (struct alias_info *ai)
1288 size_t i;
1290 /* Sort the POINTERS array in descending order of contributed
1291 virtual operands. */
1292 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1293 total_alias_vops_cmp);
1295 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1296 and the tag's may-aliases set. */
1297 for (i = 0; i < ai->num_pointers; i++)
1299 size_t j;
1300 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1301 bitmap tag1_aliases = ai->pointers[i]->may_aliases;
1303 /* Skip tags that have been grouped already. */
1304 if (ai->pointers[i]->grouped_p)
1305 continue;
1307 /* See if TAG1 had any aliases in common with other type tags.
1308 If we find a TAG2 with common aliases with TAG1, add TAG2's
1309 aliases into TAG1. */
1310 for (j = i + 1; j < ai->num_pointers; j++)
1312 bitmap tag2_aliases = ai->pointers[j]->may_aliases;
1314 if (bitmap_intersect_p (tag1_aliases, tag2_aliases))
1316 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1318 bitmap_ior_into (tag1_aliases, tag2_aliases);
1320 /* TAG2 does not need its aliases anymore. */
1321 bitmap_clear (tag2_aliases);
1322 var_ann (tag2)->may_aliases = NULL;
1324 /* TAG1 is the unique alias of TAG2. */
1325 add_may_alias (tag2, tag1);
1327 ai->pointers[j]->grouped_p = true;
1331 /* Now group all the aliases we collected into TAG1. */
1332 group_aliases_into (tag1, tag1_aliases, ai);
1334 /* If we've reduced total number of virtual operands below the
1335 threshold, stop. */
1336 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1337 break;
1340 /* Finally, all the variables that have been grouped cannot be in
1341 the may-alias set of name memory tags. Suppose that we have
1342 grouped the aliases in this code so that may-aliases(a) = TMT.20
1344 p_5 = &a;
1346 # a_9 = V_MAY_DEF <a_8>
1347 p_5->field = 0
1348 ... Several modifications to TMT.20 ...
1349 # VUSE <a_9>
1350 x_30 = p_5->field
1352 Since p_5 points to 'a', the optimizers will try to propagate 0
1353 into p_5->field, but that is wrong because there have been
1354 modifications to 'TMT.20' in between. To prevent this we have to
1355 replace 'a' with 'TMT.20' in the name tag of p_5. */
1356 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1358 size_t j;
1359 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1360 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1361 varray_type aliases;
1363 if (name_tag == NULL_TREE)
1364 continue;
1366 aliases = var_ann (name_tag)->may_aliases;
1367 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1369 tree alias = VARRAY_TREE (aliases, j);
1370 var_ann_t ann = var_ann (alias);
1372 if ((!MTAG_P (alias) || TREE_CODE (alias) == STRUCT_FIELD_TAG)
1373 && ann->may_aliases)
1375 tree new_alias;
1377 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1379 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1380 replace_may_alias (name_tag, j, new_alias);
1385 if (dump_file)
1386 fprintf (dump_file,
1387 "%s: Total number of aliased vops after grouping: %ld%s\n",
1388 get_name (current_function_decl),
1389 ai->total_alias_vops,
1390 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1394 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1396 static void
1397 create_alias_map_for (tree var, struct alias_info *ai)
1399 struct alias_map_d *alias_map;
1400 alias_map = xcalloc (1, sizeof (*alias_map));
1401 alias_map->var = var;
1402 alias_map->set = get_alias_set (var);
1403 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1407 /* Create memory tags for all the dereferenced pointers and build the
1408 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1409 sets. Based on the address escape and points-to information collected
1410 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1411 variables whose address is not needed anymore. */
1413 static void
1414 setup_pointers_and_addressables (struct alias_info *ai)
1416 size_t n_vars, num_addressable_vars, num_pointers;
1417 referenced_var_iterator rvi;
1418 tree var;
1419 VEC (tree, heap) *varvec = NULL;
1420 safe_referenced_var_iterator srvi;
1422 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1423 num_addressable_vars = num_pointers = 0;
1425 FOR_EACH_REFERENCED_VAR (var, rvi)
1427 if (may_be_aliased (var))
1428 num_addressable_vars++;
1430 if (POINTER_TYPE_P (TREE_TYPE (var)))
1432 /* Since we don't keep track of volatile variables, assume that
1433 these pointers are used in indirect store operations. */
1434 if (TREE_THIS_VOLATILE (var))
1435 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
1437 num_pointers++;
1441 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1442 always going to be slightly bigger than we actually need them
1443 because some TREE_ADDRESSABLE variables will be marked
1444 non-addressable below and only pointers with unique type tags are
1445 going to be added to POINTERS. */
1446 ai->addressable_vars = xcalloc (num_addressable_vars,
1447 sizeof (struct alias_map_d *));
1448 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1449 ai->num_addressable_vars = 0;
1450 ai->num_pointers = 0;
1452 /* Since we will be creating type memory tags within this loop, cache the
1453 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1454 unnecessarily. */
1455 n_vars = num_referenced_vars;
1457 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
1459 var_ann_t v_ann = var_ann (var);
1460 subvar_t svars;
1462 /* Name memory tags already have flow-sensitive aliasing
1463 information, so they need not be processed by
1464 compute_flow_insensitive_aliasing. Similarly, type memory
1465 tags are already accounted for when we process their
1466 associated pointer.
1468 Structure fields, on the other hand, have to have some of this
1469 information processed for them, but it's pointless to mark them
1470 non-addressable (since they are fake variables anyway). */
1471 if (MTAG_P (var) && TREE_CODE (var) != STRUCT_FIELD_TAG)
1472 continue;
1474 /* Remove the ADDRESSABLE flag from every addressable variable whose
1475 address is not needed anymore. This is caused by the propagation
1476 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1477 removal of dead pointer assignments done by the early scalar
1478 cleanup passes. */
1479 if (TREE_ADDRESSABLE (var))
1481 if (!bitmap_bit_p (addressable_vars, DECL_UID (var))
1482 && TREE_CODE (var) != RESULT_DECL
1483 && !is_global_var (var))
1485 bool okay_to_mark = true;
1487 /* Since VAR is now a regular GIMPLE register, we will need
1488 to rename VAR into SSA afterwards. */
1489 mark_sym_for_renaming (var);
1491 /* If VAR can have sub-variables, and any of its
1492 sub-variables has its address taken, then we cannot
1493 remove the addressable flag from VAR. */
1494 if (var_can_have_subvars (var)
1495 && (svars = get_subvars_for_var (var)))
1497 subvar_t sv;
1499 for (sv = svars; sv; sv = sv->next)
1501 if (bitmap_bit_p (addressable_vars, DECL_UID (sv->var)))
1502 okay_to_mark = false;
1503 mark_sym_for_renaming (sv->var);
1507 /* The address of VAR is not needed, remove the
1508 addressable bit, so that it can be optimized as a
1509 regular variable. */
1510 if (okay_to_mark)
1511 mark_non_addressable (var);
1515 /* Global variables and addressable locals may be aliased. Create an
1516 entry in ADDRESSABLE_VARS for VAR. */
1517 if (may_be_aliased (var)
1518 && (!var_can_have_subvars (var)
1519 || get_subvars_for_var (var) == NULL))
1521 create_alias_map_for (var, ai);
1522 mark_sym_for_renaming (var);
1525 /* Add pointer variables that have been dereferenced to the POINTERS
1526 array and create a type memory tag for them. */
1527 if (POINTER_TYPE_P (TREE_TYPE (var)))
1529 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var))
1530 || bitmap_bit_p (ai->dereferenced_ptrs_load, DECL_UID (var))))
1532 tree tag;
1533 var_ann_t t_ann;
1535 /* If pointer VAR still doesn't have a memory tag
1536 associated with it, create it now or re-use an
1537 existing one. */
1538 tag = get_tmt_for (var, ai);
1539 t_ann = var_ann (tag);
1541 /* The type tag will need to be renamed into SSA
1542 afterwards. Note that we cannot do this inside
1543 get_tmt_for because aliasing may run multiple times
1544 and we only create type tags the first time. */
1545 mark_sym_for_renaming (tag);
1547 /* Similarly, if pointer VAR used to have another type
1548 tag, we will need to process it in the renamer to
1549 remove the stale virtual operands. */
1550 if (v_ann->type_mem_tag)
1551 mark_sym_for_renaming (v_ann->type_mem_tag);
1553 /* Associate the tag with pointer VAR. */
1554 v_ann->type_mem_tag = tag;
1556 /* If pointer VAR has been used in a store operation,
1557 then its memory tag must be marked as written-to. */
1558 if (bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var)))
1559 bitmap_set_bit (ai->written_vars, DECL_UID (tag));
1561 /* All the dereferences of pointer VAR count as
1562 references of TAG. Since TAG can be associated with
1563 several pointers, add the dereferences of VAR to the
1564 TAG. */
1565 NUM_REFERENCES_SET (t_ann,
1566 NUM_REFERENCES (t_ann)
1567 + NUM_REFERENCES (v_ann));
1569 else
1571 /* The pointer has not been dereferenced. If it had a
1572 type memory tag, remove it and mark the old tag for
1573 renaming to remove it out of the IL. */
1574 var_ann_t ann = var_ann (var);
1575 tree tag = ann->type_mem_tag;
1576 if (tag)
1578 mark_sym_for_renaming (tag);
1579 ann->type_mem_tag = NULL_TREE;
1584 VEC_free (tree, heap, varvec);
1588 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1589 every call site, we need to emit V_MAY_DEF expressions to represent the
1590 clobbering effects of the call for variables whose address escapes the
1591 current function.
1593 One approach is to group all call-clobbered variables into a single
1594 representative that is used as an alias of every call-clobbered variable
1595 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1596 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1598 The second approach is to emit a clobbering V_MAY_DEF for every
1599 call-clobbered variable at call sites. This is the preferred way in terms
1600 of optimization opportunities but it may create too many V_MAY_DEF operands
1601 if there are many call clobbered variables and function calls in the
1602 function.
1604 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1605 function calls found by the number of call-clobbered variables. If that
1606 product is beyond a certain threshold, as determined by the parameterized
1607 values shown below, we use .GLOBAL_VAR.
1609 FIXME. This heuristic should be improved. One idea is to use several
1610 .GLOBAL_VARs of different types instead of a single one. The thresholds
1611 have been derived from a typical bootstrap cycle, including all target
1612 libraries. Compile times were found increase by ~1% compared to using
1613 .GLOBAL_VAR. */
1615 static void
1616 maybe_create_global_var (struct alias_info *ai)
1618 unsigned i, n_clobbered;
1619 bitmap_iterator bi;
1621 /* No need to create it, if we have one already. */
1622 if (global_var == NULL_TREE)
1624 /* Count all the call-clobbered variables. */
1625 n_clobbered = 0;
1626 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1628 n_clobbered++;
1631 /* If the number of virtual operands that would be needed to
1632 model all the call-clobbered variables is larger than
1633 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1635 Also create .GLOBAL_VAR if there are no call-clobbered
1636 variables and the program contains a mixture of pure/const
1637 and regular function calls. This is to avoid the problem
1638 described in PR 20115:
1640 int X;
1641 int func_pure (void) { return X; }
1642 int func_non_pure (int a) { X += a; }
1643 int foo ()
1645 int a = func_pure ();
1646 func_non_pure (a);
1647 a = func_pure ();
1648 return a;
1651 Since foo() has no call-clobbered variables, there is
1652 no relationship between the calls to func_pure and
1653 func_non_pure. Since func_pure has no side-effects, value
1654 numbering optimizations elide the second call to func_pure.
1655 So, if we have some pure/const and some regular calls in the
1656 program we create .GLOBAL_VAR to avoid missing these
1657 relations. */
1658 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1659 || (n_clobbered == 0
1660 && ai->num_calls_found > 0
1661 && ai->num_pure_const_calls_found > 0
1662 && ai->num_calls_found > ai->num_pure_const_calls_found))
1663 create_global_var ();
1666 /* Mark all call-clobbered symbols for renaming. Since the initial
1667 rewrite into SSA ignored all call sites, we may need to rename
1668 .GLOBAL_VAR and the call-clobbered variables. */
1669 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1671 tree var = referenced_var (i);
1673 /* If the function has calls to clobbering functions and
1674 .GLOBAL_VAR has been created, make it an alias for all
1675 call-clobbered variables. */
1676 if (global_var && var != global_var)
1678 subvar_t svars;
1679 add_may_alias (var, global_var);
1680 if (var_can_have_subvars (var)
1681 && (svars = get_subvars_for_var (var)))
1683 subvar_t sv;
1684 for (sv = svars; sv; sv = sv->next)
1685 mark_sym_for_renaming (sv->var);
1689 mark_sym_for_renaming (var);
1694 /* Return TRUE if pointer PTR may point to variable VAR.
1696 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1697 This is needed because when checking for type conflicts we are
1698 interested in the alias set of the memory location pointed-to by
1699 PTR. The alias set of PTR itself is irrelevant.
1701 VAR_ALIAS_SET is the alias set for VAR. */
1703 static bool
1704 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1705 tree var, HOST_WIDE_INT var_alias_set,
1706 bool alias_set_only)
1708 tree mem;
1709 var_ann_t m_ann;
1711 alias_stats.alias_queries++;
1712 alias_stats.simple_queries++;
1714 /* By convention, a variable cannot alias itself. */
1715 mem = var_ann (ptr)->type_mem_tag;
1716 if (mem == var)
1718 alias_stats.alias_noalias++;
1719 alias_stats.simple_resolved++;
1720 return false;
1723 /* If -fargument-noalias-global is >1, pointer arguments may
1724 not point to global variables. */
1725 if (flag_argument_noalias > 1 && is_global_var (var)
1726 && TREE_CODE (ptr) == PARM_DECL)
1728 alias_stats.alias_noalias++;
1729 alias_stats.simple_resolved++;
1730 return false;
1733 /* If either MEM or VAR is a read-only global and the other one
1734 isn't, then PTR cannot point to VAR. */
1735 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1736 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1738 alias_stats.alias_noalias++;
1739 alias_stats.simple_resolved++;
1740 return false;
1743 m_ann = var_ann (mem);
1745 gcc_assert (TREE_CODE (mem) == TYPE_MEMORY_TAG);
1747 alias_stats.tbaa_queries++;
1749 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1750 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1752 alias_stats.alias_noalias++;
1753 alias_stats.tbaa_resolved++;
1754 return false;
1757 /* If var is a record or union type, ptr cannot point into var
1758 unless there is some operation explicit address operation in the
1759 program that can reference a field of the ptr's dereferenced
1760 type. This also assumes that the types of both var and ptr are
1761 contained within the compilation unit, and that there is no fancy
1762 addressing arithmetic associated with any of the types
1763 involved. */
1765 if ((mem_alias_set != 0) && (var_alias_set != 0))
1767 tree ptr_type = TREE_TYPE (ptr);
1768 tree var_type = TREE_TYPE (var);
1770 /* The star count is -1 if the type at the end of the pointer_to
1771 chain is not a record or union type. */
1772 if ((!alias_set_only) &&
1773 ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
1775 int ptr_star_count = 0;
1777 /* Ipa_type_escape_star_count_of_interesting_type is a little to
1778 restrictive for the pointer type, need to allow pointers to
1779 primitive types as long as those types cannot be pointers
1780 to everything. */
1781 while (POINTER_TYPE_P (ptr_type))
1782 /* Strip the *'s off. */
1784 ptr_type = TREE_TYPE (ptr_type);
1785 ptr_star_count++;
1788 /* There does not appear to be a better test to see if the
1789 pointer type was one of the pointer to everything
1790 types. */
1792 if (ptr_star_count > 0)
1794 alias_stats.structnoaddress_queries++;
1795 if (ipa_type_escape_field_does_not_clobber_p (var_type,
1796 TREE_TYPE (ptr)))
1798 alias_stats.structnoaddress_resolved++;
1799 alias_stats.alias_noalias++;
1800 return false;
1803 else if (ptr_star_count == 0)
1805 /* If ptr_type was not really a pointer to type, it cannot
1806 alias. */
1807 alias_stats.structnoaddress_queries++;
1808 alias_stats.structnoaddress_resolved++;
1809 alias_stats.alias_noalias++;
1810 return false;
1815 alias_stats.alias_mayalias++;
1816 return true;
1820 /* Add ALIAS to the set of variables that may alias VAR. */
1822 static void
1823 add_may_alias (tree var, tree alias)
1825 size_t i;
1826 var_ann_t v_ann = get_var_ann (var);
1827 var_ann_t a_ann = get_var_ann (alias);
1829 /* Don't allow self-referential aliases. */
1830 gcc_assert (var != alias);
1832 /* ALIAS must be addressable if it's being added to an alias set. */
1833 #if 1
1834 TREE_ADDRESSABLE (alias) = 1;
1835 #else
1836 gcc_assert (may_be_aliased (alias));
1837 #endif
1839 if (v_ann->may_aliases == NULL)
1840 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1842 /* Avoid adding duplicates. */
1843 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1844 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1845 return;
1847 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1848 a_ann->is_alias_tag = 1;
1852 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1854 static void
1855 replace_may_alias (tree var, size_t i, tree new_alias)
1857 var_ann_t v_ann = var_ann (var);
1858 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1862 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1864 static void
1865 set_pt_anything (tree ptr)
1867 struct ptr_info_def *pi = get_ptr_info (ptr);
1869 pi->pt_anything = 1;
1870 pi->pt_vars = NULL;
1872 /* The pointer used to have a name tag, but we now found it pointing
1873 to an arbitrary location. The name tag needs to be renamed and
1874 disassociated from PTR. */
1875 if (pi->name_mem_tag)
1877 mark_sym_for_renaming (pi->name_mem_tag);
1878 pi->name_mem_tag = NULL_TREE;
1883 /* Return true if STMT is an "escape" site from the current function. Escape
1884 sites those statements which might expose the address of a variable
1885 outside the current function. STMT is an escape site iff:
1887 1- STMT is a function call, or
1888 2- STMT is an __asm__ expression, or
1889 3- STMT is an assignment to a non-local variable, or
1890 4- STMT is a return statement.
1892 AI points to the alias information collected so far. */
1894 enum escape_type
1895 is_escape_site (tree stmt, struct alias_info *ai)
1897 tree call = get_call_expr_in (stmt);
1898 if (call != NULL_TREE)
1900 ai->num_calls_found++;
1902 if (!TREE_SIDE_EFFECTS (call))
1904 ai->num_pure_const_calls_found++;
1905 return ESCAPE_TO_PURE_CONST;
1908 return ESCAPE_TO_CALL;
1910 else if (TREE_CODE (stmt) == ASM_EXPR)
1911 return ESCAPE_TO_ASM;
1912 else if (TREE_CODE (stmt) == MODIFY_EXPR)
1914 tree lhs = TREE_OPERAND (stmt, 0);
1916 /* Get to the base of _REF nodes. */
1917 if (TREE_CODE (lhs) != SSA_NAME)
1918 lhs = get_base_address (lhs);
1920 /* If we couldn't recognize the LHS of the assignment, assume that it
1921 is a non-local store. */
1922 if (lhs == NULL_TREE)
1923 return ESCAPE_UNKNOWN;
1925 /* If the RHS is a conversion between a pointer and an integer, the
1926 pointer escapes since we can't track the integer. */
1927 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
1928 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
1929 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
1930 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
1931 (TREE_OPERAND (stmt, 1), 0)))
1932 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1933 return ESCAPE_BAD_CAST;
1935 /* If the LHS is an SSA name, it can't possibly represent a non-local
1936 memory store. */
1937 if (TREE_CODE (lhs) == SSA_NAME)
1938 return NO_ESCAPE;
1940 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
1941 local variables we cannot be sure if it will escape, because we
1942 don't have information about objects not in SSA form. Need to
1943 implement something along the lines of
1945 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
1946 Midkiff, ``Escape analysis for java,'' in Proceedings of the
1947 Conference on Object-Oriented Programming Systems, Languages, and
1948 Applications (OOPSLA), pp. 1-19, 1999. */
1949 return ESCAPE_STORED_IN_GLOBAL;
1951 else if (TREE_CODE (stmt) == RETURN_EXPR)
1952 return ESCAPE_TO_RETURN;
1954 return NO_ESCAPE;
1958 /* Create a new memory tag of type TYPE.
1959 Does NOT push it into the current binding. */
1961 static tree
1962 create_tag_raw (enum tree_code code, tree type, const char *prefix)
1964 tree tmp_var;
1965 tree new_type;
1967 /* Make the type of the variable writable. */
1968 new_type = build_type_variant (type, 0, 0);
1969 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
1971 tmp_var = build_decl (code, create_tmp_var_name (prefix),
1972 type);
1973 /* Make the variable writable. */
1974 TREE_READONLY (tmp_var) = 0;
1976 /* It doesn't start out global. */
1977 MTAG_GLOBAL (tmp_var) = 0;
1978 TREE_STATIC (tmp_var) = 0;
1979 TREE_USED (tmp_var) = 1;
1981 return tmp_var;
1984 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
1985 is considered to represent all the pointers whose pointed-to types are
1986 in the same alias set class. Otherwise, the tag represents a single
1987 SSA_NAME pointer variable. */
1989 static tree
1990 create_memory_tag (tree type, bool is_type_tag)
1992 var_ann_t ann;
1993 tree tag = create_tag_raw (is_type_tag ? TYPE_MEMORY_TAG : NAME_MEMORY_TAG,
1994 type, (is_type_tag) ? "TMT" : "NMT");
1996 /* By default, memory tags are local variables. Alias analysis will
1997 determine whether they should be considered globals. */
1998 DECL_CONTEXT (tag) = current_function_decl;
2000 /* Memory tags are by definition addressable. */
2001 TREE_ADDRESSABLE (tag) = 1;
2003 ann = get_var_ann (tag);
2004 ann->type_mem_tag = NULL_TREE;
2006 /* Add the tag to the symbol table. */
2007 add_referenced_tmp_var (tag);
2009 return tag;
2013 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2014 This is used if P_i has been found to point to a specific set of
2015 variables or to a non-aliased memory location like the address returned
2016 by malloc functions. */
2018 static tree
2019 get_nmt_for (tree ptr)
2021 struct ptr_info_def *pi = get_ptr_info (ptr);
2022 tree tag = pi->name_mem_tag;
2024 if (tag == NULL_TREE)
2025 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2026 return tag;
2030 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2031 artificial variable that represents the memory location pointed-to by
2032 PTR. It is used to model the effects of pointer de-references on
2033 addressable variables.
2035 AI points to the data gathered during alias analysis. This function
2036 populates the array AI->POINTERS. */
2038 static tree
2039 get_tmt_for (tree ptr, struct alias_info *ai)
2041 size_t i;
2042 tree tag;
2043 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2044 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2046 /* To avoid creating unnecessary memory tags, only create one memory tag
2047 per alias set class. Note that it may be tempting to group
2048 memory tags based on conflicting alias sets instead of
2049 equivalence. That would be wrong because alias sets are not
2050 necessarily transitive (as demonstrated by the libstdc++ test
2051 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2052 such that conflicts (A, B) == true and conflicts (A, C) == true,
2053 it does not necessarily follow that conflicts (B, C) == true. */
2054 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2056 struct alias_map_d *curr = ai->pointers[i];
2057 tree curr_tag = var_ann (curr->var)->type_mem_tag;
2058 if (tag_set == curr->set
2059 && TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (curr_tag)))
2061 tag = curr_tag;
2062 break;
2066 /* If VAR cannot alias with any of the existing memory tags, create a new
2067 tag for PTR and add it to the POINTERS array. */
2068 if (tag == NULL_TREE)
2070 struct alias_map_d *alias_map;
2072 /* If PTR did not have a type tag already, create a new TMT.*
2073 artificial variable representing the memory location
2074 pointed-to by PTR. */
2075 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2076 tag = create_memory_tag (tag_type, true);
2077 else
2078 tag = var_ann (ptr)->type_mem_tag;
2080 /* Add PTR to the POINTERS array. Note that we are not interested in
2081 PTR's alias set. Instead, we cache the alias set for the memory that
2082 PTR points to. */
2083 alias_map = xcalloc (1, sizeof (*alias_map));
2084 alias_map->var = ptr;
2085 alias_map->set = tag_set;
2086 ai->pointers[ai->num_pointers++] = alias_map;
2089 /* If the pointed-to type is volatile, so is the tag. */
2090 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2092 /* Make sure that the type tag has the same alias set as the
2093 pointed-to type. */
2094 gcc_assert (tag_set == get_alias_set (tag));
2096 /* If PTR's pointed-to type is read-only, then TAG's type must also
2097 be read-only. */
2098 gcc_assert (TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (tag)));
2100 return tag;
2104 /* Create GLOBAL_VAR, an artificial global variable to act as a
2105 representative of all the variables that may be clobbered by function
2106 calls. */
2108 static void
2109 create_global_var (void)
2111 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2112 void_type_node);
2113 DECL_ARTIFICIAL (global_var) = 1;
2114 TREE_READONLY (global_var) = 0;
2115 DECL_EXTERNAL (global_var) = 1;
2116 TREE_STATIC (global_var) = 1;
2117 TREE_USED (global_var) = 1;
2118 DECL_CONTEXT (global_var) = NULL_TREE;
2119 TREE_THIS_VOLATILE (global_var) = 0;
2120 TREE_ADDRESSABLE (global_var) = 0;
2121 create_var_ann (global_var);
2122 mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
2123 add_referenced_tmp_var (global_var);
2124 mark_sym_for_renaming (global_var);
2128 /* Dump alias statistics on FILE. */
2130 static void
2131 dump_alias_stats (FILE *file)
2133 const char *funcname
2134 = lang_hooks.decl_printable_name (current_function_decl, 2);
2135 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2136 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2137 fprintf (file, "Total alias mayalias results:\t%u\n",
2138 alias_stats.alias_mayalias);
2139 fprintf (file, "Total alias noalias results:\t%u\n",
2140 alias_stats.alias_noalias);
2141 fprintf (file, "Total simple queries:\t%u\n",
2142 alias_stats.simple_queries);
2143 fprintf (file, "Total simple resolved:\t%u\n",
2144 alias_stats.simple_resolved);
2145 fprintf (file, "Total TBAA queries:\t%u\n",
2146 alias_stats.tbaa_queries);
2147 fprintf (file, "Total TBAA resolved:\t%u\n",
2148 alias_stats.tbaa_resolved);
2149 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
2150 alias_stats.structnoaddress_queries);
2151 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
2152 alias_stats.structnoaddress_resolved);
2156 /* Dump alias information on FILE. */
2158 void
2159 dump_alias_info (FILE *file)
2161 size_t i;
2162 const char *funcname
2163 = lang_hooks.decl_printable_name (current_function_decl, 2);
2164 referenced_var_iterator rvi;
2165 tree var;
2167 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2169 fprintf (file, "Aliased symbols\n\n");
2171 FOR_EACH_REFERENCED_VAR (var, rvi)
2173 if (may_be_aliased (var))
2174 dump_variable (file, var);
2177 fprintf (file, "\nDereferenced pointers\n\n");
2179 FOR_EACH_REFERENCED_VAR (var, rvi)
2181 var_ann_t ann = var_ann (var);
2182 if (ann->type_mem_tag)
2183 dump_variable (file, var);
2186 fprintf (file, "\nType memory tags\n\n");
2188 FOR_EACH_REFERENCED_VAR (var, rvi)
2190 if (TREE_CODE (var) == TYPE_MEMORY_TAG)
2191 dump_variable (file, var);
2194 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2196 fprintf (file, "SSA_NAME pointers\n\n");
2197 for (i = 1; i < num_ssa_names; i++)
2199 tree ptr = ssa_name (i);
2200 struct ptr_info_def *pi;
2202 if (ptr == NULL_TREE)
2203 continue;
2205 pi = SSA_NAME_PTR_INFO (ptr);
2206 if (!SSA_NAME_IN_FREE_LIST (ptr)
2207 && pi
2208 && pi->name_mem_tag)
2209 dump_points_to_info_for (file, ptr);
2212 fprintf (file, "\nName memory tags\n\n");
2214 FOR_EACH_REFERENCED_VAR (var, rvi)
2216 if (TREE_CODE (var) == NAME_MEMORY_TAG)
2217 dump_variable (file, var);
2220 fprintf (file, "\n");
2224 /* Dump alias information on stderr. */
2226 void
2227 debug_alias_info (void)
2229 dump_alias_info (stderr);
2233 /* Return the alias information associated with pointer T. It creates a
2234 new instance if none existed. */
2236 struct ptr_info_def *
2237 get_ptr_info (tree t)
2239 struct ptr_info_def *pi;
2241 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2243 pi = SSA_NAME_PTR_INFO (t);
2244 if (pi == NULL)
2246 pi = ggc_alloc (sizeof (*pi));
2247 memset ((void *)pi, 0, sizeof (*pi));
2248 SSA_NAME_PTR_INFO (t) = pi;
2251 return pi;
2255 /* Dump points-to information for SSA_NAME PTR into FILE. */
2257 void
2258 dump_points_to_info_for (FILE *file, tree ptr)
2260 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2262 print_generic_expr (file, ptr, dump_flags);
2264 if (pi)
2266 if (pi->name_mem_tag)
2268 fprintf (file, ", name memory tag: ");
2269 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2272 if (pi->is_dereferenced)
2273 fprintf (file, ", is dereferenced");
2275 if (pi->value_escapes_p)
2276 fprintf (file, ", its value escapes");
2278 if (pi->pt_anything)
2279 fprintf (file, ", points-to anything");
2281 if (pi->pt_null)
2282 fprintf (file, ", points-to NULL");
2284 if (pi->pt_vars)
2286 unsigned ix;
2287 bitmap_iterator bi;
2289 fprintf (file, ", points-to vars: { ");
2290 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2292 print_generic_expr (file, referenced_var (ix), dump_flags);
2293 fprintf (file, " ");
2295 fprintf (file, "}");
2299 fprintf (file, "\n");
2303 /* Dump points-to information for VAR into stderr. */
2305 void
2306 debug_points_to_info_for (tree var)
2308 dump_points_to_info_for (stderr, var);
2312 /* Dump points-to information into FILE. NOTE: This function is slow, as
2313 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2315 void
2316 dump_points_to_info (FILE *file)
2318 basic_block bb;
2319 block_stmt_iterator si;
2320 ssa_op_iter iter;
2321 const char *fname =
2322 lang_hooks.decl_printable_name (current_function_decl, 2);
2323 referenced_var_iterator rvi;
2324 tree var;
2326 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2328 /* First dump points-to information for the default definitions of
2329 pointer variables. This is necessary because default definitions are
2330 not part of the code. */
2331 FOR_EACH_REFERENCED_VAR (var, rvi)
2333 if (POINTER_TYPE_P (TREE_TYPE (var)))
2335 tree def = default_def (var);
2336 if (def)
2337 dump_points_to_info_for (file, def);
2341 /* Dump points-to information for every pointer defined in the program. */
2342 FOR_EACH_BB (bb)
2344 tree phi;
2346 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2348 tree ptr = PHI_RESULT (phi);
2349 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2350 dump_points_to_info_for (file, ptr);
2353 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2355 tree stmt = bsi_stmt (si);
2356 tree def;
2357 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2358 if (POINTER_TYPE_P (TREE_TYPE (def)))
2359 dump_points_to_info_for (file, def);
2363 fprintf (file, "\n");
2367 /* Dump points-to info pointed to by PTO into STDERR. */
2369 void
2370 debug_points_to_info (void)
2372 dump_points_to_info (stderr);
2375 /* Dump to FILE the list of variables that may be aliasing VAR. */
2377 void
2378 dump_may_aliases_for (FILE *file, tree var)
2380 varray_type aliases;
2382 if (TREE_CODE (var) == SSA_NAME)
2383 var = SSA_NAME_VAR (var);
2385 aliases = var_ann (var)->may_aliases;
2386 if (aliases)
2388 size_t i;
2389 fprintf (file, "{ ");
2390 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2392 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2393 fprintf (file, " ");
2395 fprintf (file, "}");
2400 /* Dump to stderr the list of variables that may be aliasing VAR. */
2402 void
2403 debug_may_aliases_for (tree var)
2405 dump_may_aliases_for (stderr, var);
2408 /* Return true if VAR may be aliased. */
2410 bool
2411 may_be_aliased (tree var)
2413 /* Obviously. */
2414 if (TREE_ADDRESSABLE (var))
2415 return true;
2417 /* Globally visible variables can have their addresses taken by other
2418 translation units. */
2420 if (MTAG_P (var)
2421 && (MTAG_GLOBAL (var) || TREE_PUBLIC (var)))
2422 return true;
2423 else if (!MTAG_P (var)
2424 && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
2425 return true;
2427 /* Automatic variables can't have their addresses escape any other way.
2428 This must be after the check for global variables, as extern declarations
2429 do not have TREE_STATIC set. */
2430 if (!TREE_STATIC (var))
2431 return false;
2433 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2434 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2435 we can only be sure the variable isn't addressable if it's local to the
2436 current function. */
2437 if (flag_unit_at_a_time)
2438 return false;
2439 if (decl_function_context (var) == current_function_decl)
2440 return false;
2442 return true;
2446 /* Given two symbols return TRUE if one is in the alias set of the other. */
2447 bool
2448 is_aliased_with (tree tag, tree sym)
2450 size_t i;
2451 varray_type aliases;
2453 if (var_ann (sym)->is_alias_tag)
2455 aliases = var_ann (tag)->may_aliases;
2457 if (aliases == NULL)
2458 return false;
2460 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2461 if (VARRAY_TREE (aliases, i) == sym)
2462 return true;
2464 else
2466 aliases = var_ann (sym)->may_aliases;
2468 if (aliases == NULL)
2469 return false;
2471 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2472 if (VARRAY_TREE (aliases, i) == tag)
2473 return true;
2476 return false;
2480 /* Add VAR to the list of may-aliases of PTR's type tag. If PTR
2481 doesn't already have a type tag, create one. */
2483 void
2484 add_type_alias (tree ptr, tree var)
2486 varray_type aliases;
2487 tree tag;
2488 var_ann_t ann = var_ann (ptr);
2489 subvar_t svars;
2490 VEC (tree, heap) *varvec = NULL;
2492 if (ann->type_mem_tag == NULL_TREE)
2494 tree q = NULL_TREE;
2495 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2496 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2497 safe_referenced_var_iterator rvi;
2499 /* PTR doesn't have a type tag, create a new one and add VAR to
2500 the new tag's alias set.
2502 FIXME, This is slower than necessary. We need to determine
2503 whether there is another pointer Q with the same alias set as
2504 PTR. This could be sped up by having type tags associated
2505 with types. */
2506 FOR_EACH_REFERENCED_VAR_SAFE (q, varvec, rvi)
2508 if (POINTER_TYPE_P (TREE_TYPE (q))
2509 && tag_set == get_alias_set (TREE_TYPE (TREE_TYPE (q))))
2511 /* Found another pointer Q with the same alias set as
2512 the PTR's pointed-to type. If Q has a type tag, use
2513 it. Otherwise, create a new memory tag for PTR. */
2514 var_ann_t ann1 = var_ann (q);
2515 if (ann1->type_mem_tag)
2516 ann->type_mem_tag = ann1->type_mem_tag;
2517 else
2518 ann->type_mem_tag = create_memory_tag (tag_type, true);
2519 goto found_tag;
2523 /* Couldn't find any other pointer with a type tag we could use.
2524 Create a new memory tag for PTR. */
2525 ann->type_mem_tag = create_memory_tag (tag_type, true);
2528 found_tag:
2529 /* If VAR is not already PTR's type tag, add it to the may-alias set
2530 for PTR's type tag. */
2531 gcc_assert (var_ann (var)->type_mem_tag == NULL);
2532 tag = ann->type_mem_tag;
2534 /* If VAR has subvars, add the subvars to the tag instead of the
2535 actual var. */
2536 if (var_can_have_subvars (var)
2537 && (svars = get_subvars_for_var (var)))
2539 subvar_t sv;
2540 for (sv = svars; sv; sv = sv->next)
2541 add_may_alias (tag, sv->var);
2543 else
2544 add_may_alias (tag, var);
2546 /* TAG and its set of aliases need to be marked for renaming. */
2547 mark_sym_for_renaming (tag);
2548 if ((aliases = var_ann (tag)->may_aliases) != NULL)
2550 size_t i;
2551 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2552 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2555 /* If we had grouped aliases, VAR may have aliases of its own. Mark
2556 them for renaming as well. Other statements referencing the
2557 aliases of VAR will need to be updated. */
2558 if ((aliases = var_ann (var)->may_aliases) != NULL)
2560 size_t i;
2561 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2562 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2564 VEC_free (tree, heap, varvec);
2568 /* Create a new type tag for PTR. Construct the may-alias list of this type
2569 tag so that it has the aliasing of VAR.
2571 Note, the set of aliases represented by the new type tag are not marked
2572 for renaming. */
2574 void
2575 new_type_alias (tree ptr, tree var)
2577 var_ann_t p_ann = var_ann (ptr);
2578 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2579 var_ann_t v_ann = var_ann (var);
2580 tree tag;
2581 subvar_t svars;
2583 gcc_assert (p_ann->type_mem_tag == NULL_TREE);
2584 gcc_assert (!MTAG_P (var));
2586 /* Add VAR to the may-alias set of PTR's new type tag. If VAR has
2587 subvars, add the subvars to the tag instead of the actual var. */
2588 if (var_can_have_subvars (var)
2589 && (svars = get_subvars_for_var (var)))
2591 subvar_t sv;
2593 tag = create_memory_tag (tag_type, true);
2594 p_ann->type_mem_tag = tag;
2596 for (sv = svars; sv; sv = sv->next)
2597 add_may_alias (tag, sv->var);
2599 else
2601 /* The following is based on code in add_stmt_operand to ensure that the
2602 same defs/uses/vdefs/vuses will be found after replacing a reference
2603 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
2604 is the address of var. */
2605 varray_type aliases = v_ann->may_aliases;
2607 if ((aliases != NULL)
2608 && (VARRAY_ACTIVE_SIZE (aliases) == 1))
2610 tree ali = VARRAY_TREE (aliases, 0);
2612 if (TREE_CODE (ali) == TYPE_MEMORY_TAG)
2614 p_ann->type_mem_tag = ali;
2615 return;
2619 tag = create_memory_tag (tag_type, true);
2620 p_ann->type_mem_tag = tag;
2622 if (aliases == NULL)
2623 add_may_alias (tag, var);
2624 else
2626 size_t i;
2628 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2629 add_may_alias (tag, VARRAY_TREE (aliases, i));
2636 /* This represents the used range of a variable. */
2638 typedef struct used_part
2640 HOST_WIDE_INT minused;
2641 HOST_WIDE_INT maxused;
2642 /* True if we have an explicit use/def of some portion of this variable,
2643 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2644 bool explicit_uses;
2645 /* True if we have an implicit use/def of some portion of this
2646 variable. Implicit uses occur when we can't tell what part we
2647 are referencing, and have to make conservative assumptions. */
2648 bool implicit_uses;
2649 } *used_part_t;
2651 /* An array of used_part structures, indexed by variable uid. */
2653 static htab_t used_portions;
2655 struct used_part_map
2657 unsigned int uid;
2658 used_part_t to;
2661 /* Return true if the uid in the two used part maps are equal. */
2663 static int
2664 used_part_map_eq (const void *va, const void *vb)
2666 const struct used_part_map *a = va, *b = vb;
2667 return (a->uid == b->uid);
2670 /* Hash a from uid in a used_part_map. */
2672 static unsigned int
2673 used_part_map_hash (const void *item)
2675 return ((const struct used_part_map *)item)->uid;
2678 /* Free a used part map element. */
2680 static void
2681 free_used_part_map (void *item)
2683 free (((struct used_part_map *)item)->to);
2684 free (item);
2687 /* Lookup a used_part structure for a UID. */
2689 static used_part_t
2690 up_lookup (unsigned int uid)
2692 struct used_part_map *h, in;
2693 in.uid = uid;
2694 h = htab_find_with_hash (used_portions, &in, uid);
2695 if (!h)
2696 return NULL;
2697 return h->to;
2700 /* Insert the pair UID, TO into the used part hashtable. */
2702 static void
2703 up_insert (unsigned int uid, used_part_t to)
2705 struct used_part_map *h;
2706 void **loc;
2708 h = xmalloc (sizeof (struct used_part_map));
2709 h->uid = uid;
2710 h->to = to;
2711 loc = htab_find_slot_with_hash (used_portions, h,
2712 uid, INSERT);
2713 if (*loc != NULL)
2714 free (*loc);
2715 *(struct used_part_map **) loc = h;
2719 /* Given a variable uid, UID, get or create the entry in the used portions
2720 table for the variable. */
2722 static used_part_t
2723 get_or_create_used_part_for (size_t uid)
2725 used_part_t up;
2726 if ((up = up_lookup (uid)) == NULL)
2728 up = xcalloc (1, sizeof (struct used_part));
2729 up->minused = INT_MAX;
2730 up->maxused = 0;
2731 up->explicit_uses = false;
2732 up->implicit_uses = false;
2735 return up;
2739 /* Create and return a structure sub-variable for field FIELD of
2740 variable VAR. */
2742 static tree
2743 create_sft (tree var, tree field)
2745 var_ann_t ann;
2746 tree subvar = create_tag_raw (STRUCT_FIELD_TAG, TREE_TYPE (field), "SFT");
2748 /* We need to copy the various flags from VAR to SUBVAR, so that
2749 they are is_global_var iff the original variable was. */
2750 DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
2751 MTAG_GLOBAL (subvar) = DECL_EXTERNAL (var);
2752 TREE_PUBLIC (subvar) = TREE_PUBLIC (var);
2753 TREE_STATIC (subvar) = TREE_STATIC (var);
2754 TREE_READONLY (subvar) = TREE_READONLY (var);
2756 /* Add the new variable to REFERENCED_VARS. */
2757 ann = get_var_ann (subvar);
2758 ann->type_mem_tag = NULL;
2759 add_referenced_tmp_var (subvar);
2761 return subvar;
2765 /* Given an aggregate VAR, create the subvariables that represent its
2766 fields. */
2768 static void
2769 create_overlap_variables_for (tree var)
2771 VEC(fieldoff_s,heap) *fieldstack = NULL;
2772 used_part_t up;
2773 size_t uid = DECL_UID (var);
2775 if (!up_lookup (uid))
2776 return;
2778 up = up_lookup (uid);
2779 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL);
2780 if (VEC_length (fieldoff_s, fieldstack) != 0)
2782 subvar_t *subvars;
2783 fieldoff_s *fo;
2784 bool notokay = false;
2785 int fieldcount = 0;
2786 int i;
2787 HOST_WIDE_INT lastfooffset = -1;
2788 HOST_WIDE_INT lastfosize = -1;
2789 tree lastfotype = NULL_TREE;
2791 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2792 know their size, and thus, can't handle.
2793 The same is true of fields with DECL_SIZE that is not an integer
2794 constant (such as variable sized fields).
2795 Fields with offsets which are not constant will have an offset < 0
2796 We *could* handle fields that are constant sized arrays, but
2797 currently don't. Doing so would require some extra changes to
2798 tree-ssa-operands.c. */
2800 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
2802 if (!DECL_SIZE (fo->field)
2803 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
2804 || fo->offset < 0)
2806 notokay = true;
2807 break;
2809 fieldcount++;
2812 /* The current heuristic we use is as follows:
2813 If the variable has no used portions in this function, no
2814 structure vars are created for it.
2815 Otherwise,
2816 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
2817 we always create structure vars for them.
2818 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2819 some explicit uses, we create structure vars for them.
2820 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2821 no explicit uses, we do not create structure vars for them.
2824 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
2825 && !up->explicit_uses)
2827 if (dump_file && (dump_flags & TDF_DETAILS))
2829 fprintf (dump_file, "Variable ");
2830 print_generic_expr (dump_file, var, 0);
2831 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
2833 notokay = true;
2836 /* Bail out, if we can't create overlap variables. */
2837 if (notokay)
2839 VEC_free (fieldoff_s, heap, fieldstack);
2840 return;
2843 /* Otherwise, create the variables. */
2844 subvars = lookup_subvars_for_var (var);
2846 sort_fieldstack (fieldstack);
2848 for (i = VEC_length (fieldoff_s, fieldstack);
2849 VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
2851 subvar_t sv;
2852 HOST_WIDE_INT fosize;
2853 tree currfotype;
2855 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
2856 currfotype = TREE_TYPE (fo->field);
2858 /* If this field isn't in the used portion,
2859 or it has the exact same offset and size as the last
2860 field, skip it. */
2862 if (((fo->offset <= up->minused
2863 && fo->offset + fosize <= up->minused)
2864 || fo->offset >= up->maxused)
2865 || (fo->offset == lastfooffset
2866 && fosize == lastfosize
2867 && currfotype == lastfotype))
2868 continue;
2869 sv = ggc_alloc (sizeof (struct subvar));
2870 sv->offset = fo->offset;
2871 sv->size = fosize;
2872 sv->next = *subvars;
2873 sv->var = create_sft (var, fo->field);
2875 if (dump_file)
2877 fprintf (dump_file, "structure field tag %s created for var %s",
2878 get_name (sv->var), get_name (var));
2879 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
2880 sv->offset);
2881 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
2882 sv->size);
2883 fprintf (dump_file, "\n");
2886 lastfotype = currfotype;
2887 lastfooffset = fo->offset;
2888 lastfosize = fosize;
2889 *subvars = sv;
2892 /* Once we have created subvars, the original is no longer call
2893 clobbered on its own. Its call clobbered status depends
2894 completely on the call clobbered status of the subvars.
2896 add_referenced_var in the above loop will take care of
2897 marking subvars of global variables as call clobbered for us
2898 to start, since they are global as well. */
2899 clear_call_clobbered (var);
2902 VEC_free (fieldoff_s, heap, fieldstack);
2906 /* Find the conservative answer to the question of what portions of what
2907 structures are used by this statement. We assume that if we have a
2908 component ref with a known size + offset, that we only need that part
2909 of the structure. For unknown cases, or cases where we do something
2910 to the whole structure, we assume we need to create fields for the
2911 entire structure. */
2913 static tree
2914 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2916 switch (TREE_CODE (*tp))
2918 case COMPONENT_REF:
2920 HOST_WIDE_INT bitsize;
2921 HOST_WIDE_INT bitpos;
2922 tree offset;
2923 enum machine_mode mode;
2924 int unsignedp;
2925 int volatilep;
2926 tree ref;
2927 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
2928 &unsignedp, &volatilep, false);
2929 if (DECL_P (ref) && offset == NULL && bitsize != -1)
2931 size_t uid = DECL_UID (ref);
2932 used_part_t up;
2934 up = get_or_create_used_part_for (uid);
2936 if (bitpos <= up->minused)
2937 up->minused = bitpos;
2938 if ((bitpos + bitsize >= up->maxused))
2939 up->maxused = bitpos + bitsize;
2941 up->explicit_uses = true;
2942 up_insert (uid, up);
2944 *walk_subtrees = 0;
2945 return NULL_TREE;
2947 else if (DECL_P (ref))
2949 if (DECL_SIZE (ref)
2950 && var_can_have_subvars (ref)
2951 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
2953 used_part_t up;
2954 size_t uid = DECL_UID (ref);
2956 up = get_or_create_used_part_for (uid);
2958 up->minused = 0;
2959 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
2961 up->implicit_uses = true;
2963 up_insert (uid, up);
2965 *walk_subtrees = 0;
2966 return NULL_TREE;
2970 break;
2971 /* This is here to make sure we mark the entire base variable as used
2972 when you take its address. Because our used portion analysis is
2973 simple, we aren't looking at casts or pointer arithmetic to see what
2974 happens when you take the address. */
2975 case ADDR_EXPR:
2977 tree var = get_base_address (TREE_OPERAND (*tp, 0));
2979 if (var
2980 && DECL_P (var)
2981 && DECL_SIZE (var)
2982 && var_can_have_subvars (var)
2983 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2985 used_part_t up;
2986 size_t uid = DECL_UID (var);
2988 up = get_or_create_used_part_for (uid);
2990 up->minused = 0;
2991 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
2992 up->implicit_uses = true;
2994 up_insert (uid, up);
2995 *walk_subtrees = 0;
2996 return NULL_TREE;
2999 break;
3000 case VAR_DECL:
3001 case PARM_DECL:
3002 case RESULT_DECL:
3004 tree var = *tp;
3005 if (DECL_SIZE (var)
3006 && var_can_have_subvars (var)
3007 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3009 used_part_t up;
3010 size_t uid = DECL_UID (var);
3012 up = get_or_create_used_part_for (uid);
3014 up->minused = 0;
3015 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3016 up->implicit_uses = true;
3018 up_insert (uid, up);
3019 *walk_subtrees = 0;
3020 return NULL_TREE;
3023 break;
3025 default:
3026 break;
3029 return NULL_TREE;
3032 /* Create structure field variables for structures used in this function. */
3034 static void
3035 create_structure_vars (void)
3037 basic_block bb;
3038 safe_referenced_var_iterator rvi;
3039 VEC (tree, heap) *varvec = NULL;
3040 tree var;
3042 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
3043 free_used_part_map);
3045 FOR_EACH_BB (bb)
3047 block_stmt_iterator bsi;
3048 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3050 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3051 find_used_portions,
3052 NULL);
3055 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, rvi)
3057 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3058 if (var
3059 && DECL_SIZE (var)
3060 && var_can_have_subvars (var)
3061 && !MTAG_P (var)
3062 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3063 create_overlap_variables_for (var);
3065 htab_delete (used_portions);
3066 VEC_free (tree, heap, varvec);
3070 static bool
3071 gate_structure_vars (void)
3073 return flag_tree_salias != 0;
3076 struct tree_opt_pass pass_create_structure_vars =
3078 "salias", /* name */
3079 gate_structure_vars, /* gate */
3080 create_structure_vars, /* execute */
3081 NULL, /* sub */
3082 NULL, /* next */
3083 0, /* static_pass_number */
3084 0, /* tv_id */
3085 PROP_cfg, /* properties_required */
3086 0, /* properties_provided */
3087 0, /* properties_destroyed */
3088 0, /* todo_flags_start */
3089 TODO_dump_func, /* todo_flags_finish */
3090 0 /* letter */