* varasm.c (elf_record_gcc_switches): Cast second argument of
[official-gcc.git] / gcc / tree-ssa-alias.c
blob3d4fe56e654a6a0936547a96ea7e4e2d500ccd10
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"
49 #include "vecprim.h"
51 /* Obstack used to hold grouping bitmaps and other temporary bitmaps used by
52 aliasing */
53 static bitmap_obstack alias_obstack;
55 /* Structure to map a variable to its alias set and keep track of the
56 virtual operands that will be needed to represent it. */
57 struct alias_map_d
59 /* Variable and its alias set. */
60 tree var;
61 HOST_WIDE_INT set;
63 /* Total number of virtual operands that will be needed to represent
64 all the aliases of VAR. */
65 long total_alias_vops;
67 /* Nonzero if the aliases for this memory tag have been grouped
68 already. Used in group_aliases. */
69 unsigned int grouped_p : 1;
71 /* Set of variables aliased with VAR. This is the exact same
72 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
73 bitmap form to speed up alias grouping. */
74 bitmap may_aliases;
78 /* Counters used to display statistics on alias analysis. */
79 struct alias_stats_d
81 unsigned int alias_queries;
82 unsigned int alias_mayalias;
83 unsigned int alias_noalias;
84 unsigned int simple_queries;
85 unsigned int simple_resolved;
86 unsigned int tbaa_queries;
87 unsigned int tbaa_resolved;
88 unsigned int structnoaddress_queries;
89 unsigned int structnoaddress_resolved;
93 /* Local variables. */
94 static struct alias_stats_d alias_stats;
96 /* Local functions. */
97 static void compute_flow_insensitive_aliasing (struct alias_info *);
98 static void finalize_ref_all_pointers (struct alias_info *);
99 static void dump_alias_stats (FILE *);
100 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT, bool);
101 static tree create_memory_tag (tree type, bool is_type_tag);
102 static tree get_tmt_for (tree, struct alias_info *);
103 static tree get_nmt_for (tree);
104 static void add_may_alias (tree, tree);
105 static void replace_may_alias (tree, size_t, tree);
106 static struct alias_info *init_alias_info (void);
107 static void delete_alias_info (struct alias_info *);
108 static void compute_flow_sensitive_aliasing (struct alias_info *);
109 static void setup_pointers_and_addressables (struct alias_info *);
110 static void create_global_var (void);
111 static void maybe_create_global_var (struct alias_info *ai);
112 static void group_aliases (struct alias_info *);
113 static void set_pt_anything (tree ptr);
115 /* Global declarations. */
117 /* qsort comparison function to sort type/name tags by DECL_UID. */
119 static int
120 sort_tags_by_id (const void *pa, const void *pb)
122 tree a = *(tree *)pa;
123 tree b = *(tree *)pb;
125 return DECL_UID (a) - DECL_UID (b);
128 /* Initialize WORKLIST to contain those memory tags that are marked call
129 clobbered. Initialized WORKLIST2 to contain the reasons these
130 memory tags escaped. */
132 static void
133 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
134 VEC (int, heap) **worklist2)
136 referenced_var_iterator rvi;
137 tree curr;
139 FOR_EACH_REFERENCED_VAR (curr, rvi)
141 if (MTAG_P (curr) && is_call_clobbered (curr))
143 VEC_safe_push (tree, heap, *worklist, curr);
144 VEC_safe_push (int, heap, *worklist2, var_ann (curr)->escape_mask);
149 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
150 ALIAS is not already marked call clobbered, and is a memory
151 tag. */
153 static void
154 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
155 VEC (int, heap) **worklist2,
156 int reason)
158 if (MTAG_P (alias) && !is_call_clobbered (alias))
160 VEC_safe_push (tree, heap, *worklist, alias);
161 VEC_safe_push (int, heap, *worklist2, reason);
165 /* Mark aliases of TAG as call clobbered, and place any tags on the
166 alias list that were not already call clobbered on WORKLIST. */
168 static void
169 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
170 VEC (int, heap) **worklist2)
172 unsigned int i;
173 VEC (tree, gc) *ma;
174 tree entry;
175 var_ann_t ta = var_ann (tag);
177 if (!MTAG_P (tag))
178 return;
179 ma = may_aliases (tag);
180 if (!ma)
181 return;
183 for (i = 0; VEC_iterate (tree, ma, i, entry); i++)
185 if (!unmodifiable_var_p (entry))
187 add_to_worklist (entry, worklist, worklist2, ta->escape_mask);
188 mark_call_clobbered (entry, ta->escape_mask);
193 /* Tags containing global vars need to be marked as global.
194 Tags containing call clobbered vars need to be marked as call
195 clobbered. */
197 static void
198 compute_tag_properties (void)
200 referenced_var_iterator rvi;
201 tree tag;
202 bool changed = true;
203 VEC (tree, heap) *taglist = NULL;
205 FOR_EACH_REFERENCED_VAR (tag, rvi)
207 if (!MTAG_P (tag) || TREE_CODE (tag) == STRUCT_FIELD_TAG)
208 continue;
209 VEC_safe_push (tree, heap, taglist, tag);
212 /* We sort the taglist by DECL_UID, for two reasons.
213 1. To get a sequential ordering to make the bitmap accesses
214 faster.
215 2. Because of the way we compute aliases, it's more likely that
216 an earlier tag is included in a later tag, and this will reduce
217 the number of iterations.
219 If we had a real tag graph, we would just topo-order it and be
220 done with it. */
221 qsort (VEC_address (tree, taglist),
222 VEC_length (tree, taglist),
223 sizeof (tree),
224 sort_tags_by_id);
226 /* Go through each tag not marked as global, and if it aliases
227 global vars, mark it global.
229 If the tag contains call clobbered vars, mark it call
230 clobbered.
232 This loop iterates because tags may appear in the may-aliases
233 list of other tags when we group. */
235 while (changed)
237 unsigned int k;
239 changed = false;
240 for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
242 VEC (tree, gc) *ma;
243 unsigned int i;
244 tree entry;
245 bool tagcc = is_call_clobbered (tag);
246 bool tagglobal = MTAG_GLOBAL (tag);
248 if (tagcc && tagglobal)
249 continue;
251 ma = may_aliases (tag);
252 if (!ma)
253 continue;
255 for (i = 0; VEC_iterate (tree, ma, i, entry); i++)
257 /* Call clobbered entries cause the tag to be marked
258 call clobbered. */
259 if (!tagcc && is_call_clobbered (entry))
261 mark_call_clobbered (tag, var_ann (entry)->escape_mask);
262 tagcc = true;
263 changed = true;
266 /* Global vars cause the tag to be marked global. */
267 if (!tagglobal && is_global_var (entry))
269 MTAG_GLOBAL (tag) = true;
270 changed = true;
271 tagglobal = true;
274 /* Early exit once both global and cc are set, since the
275 loop can't do any more than that. */
276 if (tagcc && tagglobal)
277 break;
281 VEC_free (tree, heap, taglist);
284 /* Set up the initial variable clobbers and globalness.
285 When this function completes, only tags whose aliases need to be
286 clobbered will be set clobbered. Tags clobbered because they
287 contain call clobbered vars are handled in compute_tag_properties. */
289 static void
290 set_initial_properties (struct alias_info *ai)
292 unsigned int i;
293 referenced_var_iterator rvi;
294 tree var;
295 tree ptr;
297 FOR_EACH_REFERENCED_VAR (var, rvi)
299 if (is_global_var (var)
300 && (!var_can_have_subvars (var)
301 || get_subvars_for_var (var) == NULL))
303 if (!unmodifiable_var_p (var))
304 mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
306 else if (TREE_CODE (var) == PARM_DECL
307 && gimple_default_def (cfun, var)
308 && POINTER_TYPE_P (TREE_TYPE (var)))
310 tree def = gimple_default_def (cfun, var);
311 get_ptr_info (def)->value_escapes_p = 1;
312 get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;
316 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
318 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
319 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
321 if (pi->value_escapes_p)
323 /* If PTR escapes then its associated memory tags and
324 pointed-to variables are call-clobbered. */
325 if (pi->name_mem_tag)
326 mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
328 if (v_ann->symbol_mem_tag)
329 mark_call_clobbered (v_ann->symbol_mem_tag, pi->escape_mask);
331 if (pi->pt_vars)
333 bitmap_iterator bi;
334 unsigned int j;
335 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
336 if (!unmodifiable_var_p (referenced_var (j)))
337 mark_call_clobbered (referenced_var (j), pi->escape_mask);
341 /* If the name tag is call clobbered, so is the symbol tag
342 associated with the base VAR_DECL. */
343 if (pi->name_mem_tag
344 && v_ann->symbol_mem_tag
345 && is_call_clobbered (pi->name_mem_tag))
346 mark_call_clobbered (v_ann->symbol_mem_tag, pi->escape_mask);
348 /* Name tags and symbol tags that we don't know where they point
349 to, might point to global memory, and thus, are clobbered.
351 FIXME: This is not quite right. They should only be
352 clobbered if value_escapes_p is true, regardless of whether
353 they point to global memory or not.
354 So removing this code and fixing all the bugs would be nice.
355 It is the cause of a bunch of clobbering. */
356 if ((pi->pt_global_mem || pi->pt_anything)
357 && pi->is_dereferenced && pi->name_mem_tag)
359 mark_call_clobbered (pi->name_mem_tag, ESCAPE_IS_GLOBAL);
360 MTAG_GLOBAL (pi->name_mem_tag) = true;
363 if ((pi->pt_global_mem || pi->pt_anything)
364 && pi->is_dereferenced
365 && v_ann->symbol_mem_tag)
367 mark_call_clobbered (v_ann->symbol_mem_tag, ESCAPE_IS_GLOBAL);
368 MTAG_GLOBAL (v_ann->symbol_mem_tag) = true;
373 /* Compute which variables need to be marked call clobbered because
374 their tag is call clobbered, and which tags need to be marked
375 global because they contain global variables. */
377 static void
378 compute_call_clobbered (struct alias_info *ai)
380 VEC (tree, heap) *worklist = NULL;
381 VEC(int,heap) *worklist2 = NULL;
383 set_initial_properties (ai);
384 init_transitive_clobber_worklist (&worklist, &worklist2);
385 while (VEC_length (tree, worklist) != 0)
387 tree curr = VEC_pop (tree, worklist);
388 int reason = VEC_pop (int, worklist2);
390 mark_call_clobbered (curr, reason);
391 mark_aliases_call_clobbered (curr, &worklist, &worklist2);
393 VEC_free (tree, heap, worklist);
394 VEC_free (int, heap, worklist2);
395 compute_tag_properties ();
398 /* Compute may-alias information for every variable referenced in function
399 FNDECL.
401 Alias analysis proceeds in 3 main phases:
403 1- Points-to and escape analysis.
405 This phase walks the use-def chains in the SSA web looking for three
406 things:
408 * Assignments of the form P_i = &VAR
409 * Assignments of the form P_i = malloc()
410 * Pointers and ADDR_EXPR that escape the current function.
412 The concept of 'escaping' is the same one used in the Java world. When
413 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
414 outside of the current function. So, assignment to global variables,
415 function arguments and returning a pointer are all escape sites, as are
416 conversions between pointers and integers.
418 This is where we are currently limited. Since not everything is renamed
419 into SSA, we lose track of escape properties when a pointer is stashed
420 inside a field in a structure, for instance. In those cases, we are
421 assuming that the pointer does escape.
423 We use escape analysis to determine whether a variable is
424 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
425 is call-clobbered. If a pointer P_i escapes, then all the variables
426 pointed-to by P_i (and its memory tag) also escape.
428 2- Compute flow-sensitive aliases
430 We have two classes of memory tags. Memory tags associated with the
431 pointed-to data type of the pointers in the program. These tags are
432 called "symbol memory tag" (SMT). The other class are those associated
433 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
434 when adding operands for an INDIRECT_REF *P_i, we will first check
435 whether P_i has a name tag, if it does we use it, because that will have
436 more precise aliasing information. Otherwise, we use the standard symbol
437 tag.
439 In this phase, we go through all the pointers we found in points-to
440 analysis and create alias sets for the name memory tags associated with
441 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
442 it points to and its tag.
445 3- Compute flow-insensitive aliases
447 This pass will compare the alias set of every symbol memory tag and
448 every addressable variable found in the program. Given a symbol
449 memory tag SMT and an addressable variable V. If the alias sets of
450 SMT and V conflict (as computed by may_alias_p), then V is marked
451 as an alias tag and added to the alias set of SMT.
453 For instance, consider the following function:
455 foo (int i)
457 int *p, a, b;
459 if (i > 10)
460 p = &a;
461 else
462 p = &b;
464 *p = 3;
465 a = b + 2;
466 return *p;
469 After aliasing analysis has finished, the symbol memory tag for pointer
470 'p' will have two aliases, namely variables 'a' and 'b'. Every time
471 pointer 'p' is dereferenced, we want to mark the operation as a
472 potential reference to 'a' and 'b'.
474 foo (int i)
476 int *p, a, b;
478 if (i_2 > 10)
479 p_4 = &a;
480 else
481 p_6 = &b;
482 # p_1 = PHI <p_4(1), p_6(2)>;
484 # a_7 = V_MAY_DEF <a_3>;
485 # b_8 = V_MAY_DEF <b_5>;
486 *p_1 = 3;
488 # a_9 = V_MAY_DEF <a_7>
489 # VUSE <b_8>
490 a_9 = b_8 + 2;
492 # VUSE <a_9>;
493 # VUSE <b_8>;
494 return *p_1;
497 In certain cases, the list of may aliases for a pointer may grow too
498 large. This may cause an explosion in the number of virtual operands
499 inserted in the code. Resulting in increased memory consumption and
500 compilation time.
502 When the number of virtual operands needed to represent aliased
503 loads and stores grows too large (configurable with @option{--param
504 max-aliased-vops}), alias sets are grouped to avoid severe
505 compile-time slow downs and memory consumption. See group_aliases. */
507 static unsigned int
508 compute_may_aliases (void)
510 struct alias_info *ai;
512 memset (&alias_stats, 0, sizeof (alias_stats));
514 /* Initialize aliasing information. */
515 ai = init_alias_info ();
517 /* For each pointer P_i, determine the sets of variables that P_i may
518 point-to. For every addressable variable V, determine whether the
519 address of V escapes the current function, making V call-clobbered
520 (i.e., whether &V is stored in a global variable or if its passed as a
521 function call argument). */
522 compute_points_to_sets (ai);
524 /* Collect all pointers and addressable variables, compute alias sets,
525 create memory tags for pointers and promote variables whose address is
526 not needed anymore. */
527 setup_pointers_and_addressables (ai);
529 /* Compute type-based flow-insensitive aliasing for all the type
530 memory tags. */
531 compute_flow_insensitive_aliasing (ai);
533 /* Compute flow-sensitive, points-to based aliasing for all the name
534 memory tags. */
535 compute_flow_sensitive_aliasing (ai);
537 /* Compute call clobbering information. */
538 compute_call_clobbered (ai);
540 /* Determine if we need to enable alias grouping. */
541 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
542 group_aliases (ai);
544 /* If the program has too many call-clobbered variables and/or function
545 calls, create .GLOBAL_VAR and use it to model call-clobbering
546 semantics at call sites. This reduces the number of virtual operands
547 considerably, improving compile times at the expense of lost
548 aliasing precision. */
549 maybe_create_global_var (ai);
551 /* If the program contains ref-all pointers, finalize may-alias information
552 for them. This pass needs to be run after call-clobbering information
553 has been computed. */
554 if (ai->ref_all_symbol_mem_tag)
555 finalize_ref_all_pointers (ai);
557 /* Debugging dumps. */
558 if (dump_file)
560 dump_referenced_vars (dump_file);
561 if (dump_flags & TDF_STATS)
562 dump_alias_stats (dump_file);
563 dump_points_to_info (dump_file);
564 dump_alias_info (dump_file);
567 /* Deallocate memory used by aliasing data structures. */
568 delete_alias_info (ai);
571 block_stmt_iterator bsi;
572 basic_block bb;
573 FOR_EACH_BB (bb)
575 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
577 update_stmt_if_modified (bsi_stmt (bsi));
581 return 0;
585 struct tree_opt_pass pass_may_alias =
587 "alias", /* name */
588 NULL, /* gate */
589 compute_may_aliases, /* execute */
590 NULL, /* sub */
591 NULL, /* next */
592 0, /* static_pass_number */
593 TV_TREE_MAY_ALIAS, /* tv_id */
594 PROP_cfg | PROP_ssa, /* properties_required */
595 PROP_alias, /* properties_provided */
596 0, /* properties_destroyed */
597 0, /* todo_flags_start */
598 TODO_dump_func | TODO_update_ssa
599 | TODO_ggc_collect | TODO_verify_ssa
600 | TODO_verify_stmts, /* todo_flags_finish */
601 0 /* letter */
605 /* Data structure used to count the number of dereferences to PTR
606 inside an expression. */
607 struct count_ptr_d
609 tree ptr;
610 unsigned count;
614 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
615 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
617 static tree
618 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
620 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
622 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
623 pointer 'ptr' is *not* dereferenced, it is simply used to compute
624 the address of 'fld' as 'ptr + offsetof(fld)'. */
625 if (TREE_CODE (*tp) == ADDR_EXPR)
627 *walk_subtrees = 0;
628 return NULL_TREE;
631 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
632 count_p->count++;
634 return NULL_TREE;
638 /* Count the number of direct and indirect uses for pointer PTR in
639 statement STMT. The two counts are stored in *NUM_USES_P and
640 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
641 least one of those dereferences is a store operation. */
643 void
644 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
645 unsigned *num_derefs_p, bool *is_store)
647 ssa_op_iter i;
648 tree use;
650 *num_uses_p = 0;
651 *num_derefs_p = 0;
652 *is_store = false;
654 /* Find out the total number of uses of PTR in STMT. */
655 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
656 if (use == ptr)
657 (*num_uses_p)++;
659 /* Now count the number of indirect references to PTR. This is
660 truly awful, but we don't have much choice. There are no parent
661 pointers inside INDIRECT_REFs, so an expression like
662 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
663 find all the indirect and direct uses of x_1 inside. The only
664 shortcut we can take is the fact that GIMPLE only allows
665 INDIRECT_REFs inside the expressions below. */
666 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
667 || (TREE_CODE (stmt) == RETURN_EXPR
668 && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
669 || TREE_CODE (stmt) == ASM_EXPR
670 || TREE_CODE (stmt) == CALL_EXPR)
672 tree lhs, rhs;
674 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
676 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
677 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
679 else if (TREE_CODE (stmt) == RETURN_EXPR)
681 tree e = TREE_OPERAND (stmt, 0);
682 lhs = GIMPLE_STMT_OPERAND (e, 0);
683 rhs = GIMPLE_STMT_OPERAND (e, 1);
685 else if (TREE_CODE (stmt) == ASM_EXPR)
687 lhs = ASM_OUTPUTS (stmt);
688 rhs = ASM_INPUTS (stmt);
690 else
692 lhs = NULL_TREE;
693 rhs = stmt;
696 if (lhs && (TREE_CODE (lhs) == TREE_LIST
697 || EXPR_P (lhs) || GIMPLE_STMT_P (lhs)))
699 struct count_ptr_d count;
700 count.ptr = ptr;
701 count.count = 0;
702 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
703 *is_store = true;
704 *num_derefs_p = count.count;
707 if (rhs && (TREE_CODE (rhs) == TREE_LIST
708 || EXPR_P (rhs) || GIMPLE_STMT_P (rhs)))
710 struct count_ptr_d count;
711 count.ptr = ptr;
712 count.count = 0;
713 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
714 *num_derefs_p += count.count;
718 gcc_assert (*num_uses_p >= *num_derefs_p);
721 /* Initialize the data structures used for alias analysis. */
723 static struct alias_info *
724 init_alias_info (void)
726 struct alias_info *ai;
727 referenced_var_iterator rvi;
728 tree var;
730 bitmap_obstack_initialize (&alias_obstack);
731 ai = XCNEW (struct alias_info);
732 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
733 sbitmap_zero (ai->ssa_names_visited);
734 ai->processed_ptrs = VEC_alloc (tree, heap, 50);
735 ai->written_vars = BITMAP_ALLOC (&alias_obstack);
736 ai->dereferenced_ptrs_store = BITMAP_ALLOC (&alias_obstack);
737 ai->dereferenced_ptrs_load = BITMAP_ALLOC (&alias_obstack);
739 /* If aliases have been computed before, clear existing information. */
740 if (gimple_aliases_computed_p (cfun))
742 unsigned i;
744 /* Similarly, clear the set of addressable variables. In this
745 case, we can just clear the set because addressability is
746 only computed here. */
747 bitmap_clear (gimple_addressable_vars (cfun));
749 /* Clear flow-insensitive alias information from each symbol. */
750 FOR_EACH_REFERENCED_VAR (var, rvi)
752 var_ann_t ann = var_ann (var);
754 ann->is_aliased = 0;
755 ann->may_aliases = NULL;
756 NUM_REFERENCES_CLEAR (ann);
758 /* Since we are about to re-discover call-clobbered
759 variables, clear the call-clobbered flag. Variables that
760 are intrinsically call-clobbered (globals, local statics,
761 etc) will not be marked by the aliasing code, so we can't
762 remove them from CALL_CLOBBERED_VARS.
764 NB: STRUCT_FIELDS are still call clobbered if they are for
765 a global variable, so we *don't* clear their call clobberedness
766 just because they are tags, though we will clear it if they
767 aren't for global variables. */
768 if (TREE_CODE (var) == NAME_MEMORY_TAG
769 || TREE_CODE (var) == SYMBOL_MEMORY_TAG
770 || !is_global_var (var))
771 clear_call_clobbered (var);
774 /* Clear flow-sensitive points-to information from each SSA name. */
775 for (i = 1; i < num_ssa_names; i++)
777 tree name = ssa_name (i);
779 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
780 continue;
782 if (SSA_NAME_PTR_INFO (name))
784 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
786 /* Clear all the flags but keep the name tag to
787 avoid creating new temporaries unnecessarily. If
788 this pointer is found to point to a subset or
789 superset of its former points-to set, then a new
790 tag will need to be created in create_name_tags. */
791 pi->pt_anything = 0;
792 pi->pt_null = 0;
793 pi->value_escapes_p = 0;
794 pi->is_dereferenced = 0;
795 if (pi->pt_vars)
796 bitmap_clear (pi->pt_vars);
801 /* Next time, we will need to reset alias information. */
802 cfun->gimple_df->aliases_computed_p = true;
804 return ai;
808 /* Deallocate memory used by alias analysis. */
810 static void
811 delete_alias_info (struct alias_info *ai)
813 size_t i;
814 referenced_var_iterator rvi;
815 tree var;
817 sbitmap_free (ai->ssa_names_visited);
818 VEC_free (tree, heap, ai->processed_ptrs);
820 for (i = 0; i < ai->num_addressable_vars; i++)
821 free (ai->addressable_vars[i]);
823 FOR_EACH_REFERENCED_VAR(var, rvi)
825 var_ann_t ann = var_ann (var);
826 NUM_REFERENCES_CLEAR (ann);
829 free (ai->addressable_vars);
831 for (i = 0; i < ai->num_pointers; i++)
832 free (ai->pointers[i]);
833 free (ai->pointers);
835 BITMAP_FREE (ai->written_vars);
836 BITMAP_FREE (ai->dereferenced_ptrs_store);
837 BITMAP_FREE (ai->dereferenced_ptrs_load);
838 bitmap_obstack_release (&alias_obstack);
839 free (ai);
841 delete_points_to_sets ();
844 /* Used for hashing to identify pointer infos with identical
845 pt_vars bitmaps. */
846 static int
847 eq_ptr_info (const void *p1, const void *p2)
849 const struct ptr_info_def *n1 = (const struct ptr_info_def *) p1;
850 const struct ptr_info_def *n2 = (const struct ptr_info_def *) p2;
851 return bitmap_equal_p (n1->pt_vars, n2->pt_vars);
854 static hashval_t
855 ptr_info_hash (const void *p)
857 const struct ptr_info_def *n = (const struct ptr_info_def *) p;
858 return bitmap_hash (n->pt_vars);
861 /* Create name tags for all the pointers that have been dereferenced.
862 We only create a name tag for a pointer P if P is found to point to
863 a set of variables (so that we can alias them to *P) or if it is
864 the result of a call to malloc (which means that P cannot point to
865 anything else nor alias any other variable).
867 If two pointers P and Q point to the same set of variables, they
868 are assigned the same name tag. */
870 static void
871 create_name_tags (void)
873 size_t i;
874 VEC (tree, heap) *with_ptvars = NULL;
875 tree ptr;
876 htab_t ptr_hash;
878 /* Collect the list of pointers with a non-empty points to set. */
879 for (i = 1; i < num_ssa_names; i++)
881 tree ptr = ssa_name (i);
882 struct ptr_info_def *pi;
884 if (!ptr
885 || !POINTER_TYPE_P (TREE_TYPE (ptr))
886 || !SSA_NAME_PTR_INFO (ptr))
887 continue;
889 pi = SSA_NAME_PTR_INFO (ptr);
891 if (pi->pt_anything || !pi->is_dereferenced)
893 /* No name tags for pointers that have not been
894 dereferenced or point to an arbitrary location. */
895 pi->name_mem_tag = NULL_TREE;
896 continue;
899 /* Set pt_anything on the pointers without pt_vars filled in so
900 that they are assigned a symbol tag. */
901 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
902 VEC_safe_push (tree, heap, with_ptvars, ptr);
903 else
904 set_pt_anything (ptr);
907 /* If we didn't find any pointers with pt_vars set, we're done. */
908 if (!with_ptvars)
909 return;
911 ptr_hash = htab_create (10, ptr_info_hash, eq_ptr_info, NULL);
912 /* Now go through the pointers with pt_vars, and find a name tag
913 with the same pt_vars as this pointer, or create one if one
914 doesn't exist. */
915 for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
917 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
918 tree old_name_tag = pi->name_mem_tag;
919 struct ptr_info_def **slot;
921 /* If PTR points to a set of variables, check if we don't
922 have another pointer Q with the same points-to set before
923 creating a tag. If so, use Q's tag instead of creating a
924 new one.
926 This is important for not creating unnecessary symbols
927 and also for copy propagation. If we ever need to
928 propagate PTR into Q or vice-versa, we would run into
929 problems if they both had different name tags because
930 they would have different SSA version numbers (which
931 would force us to take the name tags in and out of SSA). */
933 slot = (struct ptr_info_def **) htab_find_slot (ptr_hash, pi, INSERT);
934 if (*slot)
935 pi->name_mem_tag = (*slot)->name_mem_tag;
936 else
938 *slot = pi;
939 /* If we didn't find a pointer with the same points-to set
940 as PTR, create a new name tag if needed. */
941 if (pi->name_mem_tag == NULL_TREE)
942 pi->name_mem_tag = get_nmt_for (ptr);
945 /* If the new name tag computed for PTR is different than
946 the old name tag that it used to have, then the old tag
947 needs to be removed from the IL, so we mark it for
948 renaming. */
949 if (old_name_tag && old_name_tag != pi->name_mem_tag)
950 mark_sym_for_renaming (old_name_tag);
952 TREE_THIS_VOLATILE (pi->name_mem_tag)
953 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
955 /* Mark the new name tag for renaming. */
956 mark_sym_for_renaming (pi->name_mem_tag);
958 htab_delete (ptr_hash);
960 VEC_free (tree, heap, with_ptvars);
964 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
965 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
966 name tag and the variables it points-to are call-clobbered. Finally, if
967 P_i escapes and we could not determine where it points to, then all the
968 variables in the same alias set as *P_i are marked call-clobbered. This
969 is necessary because we must assume that P_i may take the address of any
970 variable in the same alias set. */
972 static void
973 compute_flow_sensitive_aliasing (struct alias_info *ai)
975 size_t i;
976 tree ptr;
978 set_used_smts ();
980 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
982 if (!find_what_p_points_to (ptr))
983 set_pt_anything (ptr);
986 create_name_tags ();
988 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
990 unsigned j;
991 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
992 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
993 bitmap_iterator bi;
996 /* Set up aliasing information for PTR's name memory tag (if it has
997 one). Note that only pointers that have been dereferenced will
998 have a name memory tag. */
999 if (pi->name_mem_tag && pi->pt_vars)
1000 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
1002 add_may_alias (pi->name_mem_tag, referenced_var (j));
1003 if (j != DECL_UID (v_ann->symbol_mem_tag))
1004 add_may_alias (v_ann->symbol_mem_tag, referenced_var (j));
1010 /* Compute type-based alias sets. Traverse all the pointers and
1011 addressable variables found in setup_pointers_and_addressables.
1013 For every pointer P in AI->POINTERS and addressable variable V in
1014 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's symbol
1015 memory tag (SMT) if their alias sets conflict. V is then marked as
1016 an alias tag so that the operand scanner knows that statements
1017 containing V have aliased operands. */
1019 static void
1020 compute_flow_insensitive_aliasing (struct alias_info *ai)
1022 size_t i;
1024 /* Initialize counter for the total number of virtual operands that
1025 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
1026 threshold set by --params max-alias-vops, we enable alias
1027 grouping. */
1028 ai->total_alias_vops = 0;
1030 /* For every pointer P, determine which addressable variables may alias
1031 with P's symbol memory tag. */
1032 for (i = 0; i < ai->num_pointers; i++)
1034 size_t j;
1035 struct alias_map_d *p_map = ai->pointers[i];
1036 tree tag = var_ann (p_map->var)->symbol_mem_tag;
1037 var_ann_t tag_ann = var_ann (tag);
1038 tree var;
1040 /* Call-clobbering information is not finalized yet at this point. */
1041 if (PTR_IS_REF_ALL (p_map->var))
1042 continue;
1044 p_map->total_alias_vops = 0;
1045 p_map->may_aliases = BITMAP_ALLOC (&alias_obstack);
1047 /* Add any pre-existing may_aliases to the bitmap used to represent
1048 TAG's alias set in case we need to group aliases. */
1049 for (j = 0; VEC_iterate (tree, tag_ann->may_aliases, j, var); ++j)
1050 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
1052 for (j = 0; j < ai->num_addressable_vars; j++)
1054 struct alias_map_d *v_map;
1055 var_ann_t v_ann;
1056 bool tag_stored_p, var_stored_p;
1058 v_map = ai->addressable_vars[j];
1059 var = v_map->var;
1060 v_ann = var_ann (var);
1062 /* Skip memory tags and variables that have never been
1063 written to. We also need to check if the variables are
1064 call-clobbered because they may be overwritten by
1065 function calls.
1067 Note this is effectively random accessing elements in
1068 the sparse bitset, which can be highly inefficient.
1069 So we first check the call_clobbered status of the
1070 tag and variable before querying the bitmap. */
1071 tag_stored_p = is_call_clobbered (tag)
1072 || bitmap_bit_p (ai->written_vars, DECL_UID (tag));
1073 var_stored_p = is_call_clobbered (var)
1074 || bitmap_bit_p (ai->written_vars, DECL_UID (var));
1075 if (!tag_stored_p && !var_stored_p)
1076 continue;
1078 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
1080 size_t num_tag_refs, num_var_refs;
1082 num_tag_refs = NUM_REFERENCES (tag_ann);
1083 num_var_refs = NUM_REFERENCES (v_ann);
1085 /* Add VAR to TAG's may-aliases set. */
1087 /* We should never have a var with subvars here, because
1088 they shouldn't get into the set of addressable vars */
1089 gcc_assert (!var_can_have_subvars (var)
1090 || get_subvars_for_var (var) == NULL);
1092 add_may_alias (tag, var);
1093 /* Update the bitmap used to represent TAG's alias set
1094 in case we need to group aliases. */
1095 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
1097 /* Update the total number of virtual operands due to
1098 aliasing. Since we are adding one more alias to TAG's
1099 may-aliases set, the total number of virtual operands due
1100 to aliasing will be increased by the number of references
1101 made to VAR and TAG (every reference to TAG will also
1102 count as a reference to VAR). */
1103 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1104 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1111 /* Since this analysis is based exclusively on symbols, it fails to
1112 handle cases where two pointers P and Q have different memory
1113 tags with conflicting alias set numbers but no aliased symbols in
1114 common.
1116 For example, suppose that we have two memory tags SMT.1 and SMT.2
1117 such that
1119 may-aliases (SMT.1) = { a }
1120 may-aliases (SMT.2) = { b }
1122 and the alias set number of SMT.1 conflicts with that of SMT.2.
1123 Since they don't have symbols in common, loads and stores from
1124 SMT.1 and SMT.2 will seem independent of each other, which will
1125 lead to the optimizers making invalid transformations (see
1126 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1128 To avoid this problem, we do a final traversal of AI->POINTERS
1129 looking for pairs of pointers that have no aliased symbols in
1130 common and yet have conflicting alias set numbers. */
1131 for (i = 0; i < ai->num_pointers; i++)
1133 size_t j;
1134 struct alias_map_d *p_map1 = ai->pointers[i];
1135 tree tag1 = var_ann (p_map1->var)->symbol_mem_tag;
1136 bitmap may_aliases1 = p_map1->may_aliases;
1138 if (PTR_IS_REF_ALL (p_map1->var))
1139 continue;
1141 for (j = i + 1; j < ai->num_pointers; j++)
1143 struct alias_map_d *p_map2 = ai->pointers[j];
1144 tree tag2 = var_ann (p_map2->var)->symbol_mem_tag;
1145 bitmap may_aliases2 = p_map2->may_aliases;
1147 if (PTR_IS_REF_ALL (p_map2->var))
1148 continue;
1150 /* If the pointers may not point to each other, do nothing. */
1151 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
1152 continue;
1154 /* The two pointers may alias each other. If they already have
1155 symbols in common, do nothing. */
1156 if (bitmap_intersect_p (may_aliases1, may_aliases2))
1157 continue;
1159 if (!bitmap_empty_p (may_aliases2))
1161 unsigned int k;
1162 bitmap_iterator bi;
1164 /* Add all the aliases for TAG2 into TAG1's alias set.
1165 FIXME, update grouping heuristic counters. */
1166 EXECUTE_IF_SET_IN_BITMAP (may_aliases2, 0, k, bi)
1167 add_may_alias (tag1, referenced_var (k));
1168 bitmap_ior_into (may_aliases1, may_aliases2);
1170 else
1172 /* Since TAG2 does not have any aliases of its own, add
1173 TAG2 itself to the alias set of TAG1. */
1174 add_may_alias (tag1, tag2);
1175 bitmap_set_bit (may_aliases1, DECL_UID (tag2));
1180 if (dump_file)
1181 fprintf (dump_file, "\n%s: Total number of aliased vops: %ld\n",
1182 get_name (current_function_decl),
1183 ai->total_alias_vops);
1187 /* Finalize may-alias information for ref-all pointers. Traverse all
1188 the addressable variables found in setup_pointers_and_addressables.
1190 If flow-sensitive alias analysis has attached a name memory tag to
1191 a ref-all pointer, we will use it for the dereferences because that
1192 will have more precise aliasing information. But if there is no
1193 name tag, we will use a special symbol tag that aliases all the
1194 call-clobbered addressable variables. */
1196 static void
1197 finalize_ref_all_pointers (struct alias_info *ai)
1199 size_t i;
1201 if (gimple_global_var (cfun))
1202 add_may_alias (ai->ref_all_symbol_mem_tag, gimple_global_var (cfun));
1203 else
1205 /* First add the real call-clobbered variables. */
1206 for (i = 0; i < ai->num_addressable_vars; i++)
1208 tree var = ai->addressable_vars[i]->var;
1209 if (is_call_clobbered (var))
1210 add_may_alias (ai->ref_all_symbol_mem_tag, var);
1213 /* Then add the call-clobbered pointer memory tags. See
1214 compute_flow_insensitive_aliasing for the rationale. */
1215 for (i = 0; i < ai->num_pointers; i++)
1217 tree ptr = ai->pointers[i]->var, tag;
1218 if (PTR_IS_REF_ALL (ptr))
1219 continue;
1220 tag = var_ann (ptr)->symbol_mem_tag;
1221 if (is_call_clobbered (tag))
1222 add_may_alias (ai->ref_all_symbol_mem_tag, tag);
1228 /* Comparison function for qsort used in group_aliases. */
1230 static int
1231 total_alias_vops_cmp (const void *p, const void *q)
1233 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1234 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1235 long n1 = (*p1)->total_alias_vops;
1236 long n2 = (*p2)->total_alias_vops;
1238 /* We want to sort in descending order. */
1239 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1242 /* Group all the aliases for TAG to make TAG represent all the
1243 variables in its alias set. Update the total number
1244 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1245 function will make TAG be the unique alias tag for all the
1246 variables in its may-aliases. So, given:
1248 may-aliases(TAG) = { V1, V2, V3 }
1250 This function will group the variables into:
1252 may-aliases(V1) = { TAG }
1253 may-aliases(V2) = { TAG }
1254 may-aliases(V2) = { TAG } */
1256 static void
1257 group_aliases_into (tree tag, bitmap tag_aliases, struct alias_info *ai)
1259 unsigned int i;
1260 var_ann_t tag_ann = var_ann (tag);
1261 size_t num_tag_refs = NUM_REFERENCES (tag_ann);
1262 bitmap_iterator bi;
1264 EXECUTE_IF_SET_IN_BITMAP (tag_aliases, 0, i, bi)
1266 tree var = referenced_var (i);
1267 var_ann_t ann = var_ann (var);
1269 /* Make TAG the unique alias of VAR. */
1270 ann->is_aliased = 0;
1271 ann->may_aliases = NULL;
1273 /* Note that VAR and TAG may be the same if the function has no
1274 addressable variables (see the discussion at the end of
1275 setup_pointers_and_addressables). */
1276 if (var != tag)
1277 add_may_alias (var, tag);
1279 /* Reduce total number of virtual operands contributed
1280 by TAG on behalf of VAR. Notice that the references to VAR
1281 itself won't be removed. We will merely replace them with
1282 references to TAG. */
1283 ai->total_alias_vops -= num_tag_refs;
1286 /* We have reduced the number of virtual operands that TAG makes on
1287 behalf of all the variables formerly aliased with it. However,
1288 we have also "removed" all the virtual operands for TAG itself,
1289 so we add them back. */
1290 ai->total_alias_vops += num_tag_refs;
1292 /* TAG no longer has any aliases. */
1293 tag_ann->may_aliases = NULL;
1296 /* Replacing may aliases in name tags during grouping can up with the
1297 same SMT multiple times in the may_alias list. It's quicker to
1298 just remove them post-hoc than it is to avoid them during
1299 replacement. Thus, this routine sorts the may-alias list and
1300 removes duplicates. */
1302 static void
1303 compact_name_tags (void)
1305 referenced_var_iterator rvi;
1306 tree var;
1308 FOR_EACH_REFERENCED_VAR (var, rvi)
1310 if (TREE_CODE (var) == NAME_MEMORY_TAG)
1312 VEC(tree, gc) *aliases, *new_aliases;
1313 tree alias, last_alias;
1314 int i;
1316 last_alias = NULL;
1317 aliases = var_ann (var)->may_aliases;
1318 new_aliases = NULL;
1320 if (VEC_length (tree, aliases) > 1)
1322 bool changed = false;
1323 qsort (VEC_address (tree, aliases),
1324 VEC_length (tree, aliases),
1325 sizeof (tree), sort_tags_by_id);
1327 for (i = 0; VEC_iterate (tree, aliases, i, alias); i++)
1329 if (alias == last_alias)
1331 changed = true;
1332 continue;
1335 VEC_safe_push (tree, gc, new_aliases, alias);
1336 last_alias = alias;
1339 /* Only replace the array if something has changed. */
1340 if (changed)
1342 VEC_free (tree, gc, aliases);
1343 var_ann (var)->may_aliases = new_aliases;
1345 else
1346 VEC_free (tree, gc, new_aliases);
1352 /* Group may-aliases sets to reduce the number of virtual operands due
1353 to aliasing.
1355 1- Sort the list of pointers in decreasing number of contributed
1356 virtual operands.
1358 2- Take the first entry in AI->POINTERS and revert the role of
1359 the memory tag and its aliases. Usually, whenever an aliased
1360 variable Vi is found to alias with a memory tag T, we add Vi
1361 to the may-aliases set for T. Meaning that after alias
1362 analysis, we will have:
1364 may-aliases(T) = { V1, V2, V3, ..., Vn }
1366 This means that every statement that references T, will get 'n'
1367 virtual operands for each of the Vi tags. But, when alias
1368 grouping is enabled, we make T an alias tag and add it to the
1369 alias set of all the Vi variables:
1371 may-aliases(V1) = { T }
1372 may-aliases(V2) = { T }
1374 may-aliases(Vn) = { T }
1376 This has two effects: (a) statements referencing T will only get
1377 a single virtual operand, and, (b) all the variables Vi will now
1378 appear to alias each other. So, we lose alias precision to
1379 improve compile time. But, in theory, a program with such a high
1380 level of aliasing should not be very optimizable in the first
1381 place.
1383 3- Since variables may be in the alias set of more than one
1384 memory tag, the grouping done in step (2) needs to be extended
1385 to all the memory tags that have a non-empty intersection with
1386 the may-aliases set of tag T. For instance, if we originally
1387 had these may-aliases sets:
1389 may-aliases(T) = { V1, V2, V3 }
1390 may-aliases(R) = { V2, V4 }
1392 In step (2) we would have reverted the aliases for T as:
1394 may-aliases(V1) = { T }
1395 may-aliases(V2) = { T }
1396 may-aliases(V3) = { T }
1398 But note that now V2 is no longer aliased with R. We could
1399 add R to may-aliases(V2), but we are in the process of
1400 grouping aliases to reduce virtual operands so what we do is
1401 add V4 to the grouping to obtain:
1403 may-aliases(V1) = { T }
1404 may-aliases(V2) = { T }
1405 may-aliases(V3) = { T }
1406 may-aliases(V4) = { T }
1408 4- If the total number of virtual operands due to aliasing is
1409 still above the threshold set by max-alias-vops, go back to (2). */
1411 static void
1412 group_aliases (struct alias_info *ai)
1414 size_t i;
1415 tree ptr;
1417 /* Sort the POINTERS array in descending order of contributed
1418 virtual operands. */
1419 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1420 total_alias_vops_cmp);
1422 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1423 and the tag's may-aliases set. */
1424 for (i = 0; i < ai->num_pointers; i++)
1426 size_t j;
1427 tree tag1 = var_ann (ai->pointers[i]->var)->symbol_mem_tag;
1428 bitmap tag1_aliases = ai->pointers[i]->may_aliases;
1430 /* Skip tags that have been grouped already. */
1431 if (ai->pointers[i]->grouped_p)
1432 continue;
1434 /* See if TAG1 had any aliases in common with other symbol tags.
1435 If we find a TAG2 with common aliases with TAG1, add TAG2's
1436 aliases into TAG1. */
1437 for (j = i + 1; j < ai->num_pointers; j++)
1439 bitmap tag2_aliases = ai->pointers[j]->may_aliases;
1441 if (bitmap_intersect_p (tag1_aliases, tag2_aliases))
1443 tree tag2 = var_ann (ai->pointers[j]->var)->symbol_mem_tag;
1445 bitmap_ior_into (tag1_aliases, tag2_aliases);
1447 /* TAG2 does not need its aliases anymore. */
1448 bitmap_clear (tag2_aliases);
1449 var_ann (tag2)->may_aliases = NULL;
1451 /* TAG1 is the unique alias of TAG2. */
1452 add_may_alias (tag2, tag1);
1454 ai->pointers[j]->grouped_p = true;
1458 /* Now group all the aliases we collected into TAG1. */
1459 group_aliases_into (tag1, tag1_aliases, ai);
1461 /* If we've reduced total number of virtual operands below the
1462 threshold, stop. */
1463 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1464 break;
1467 /* Finally, all the variables that have been grouped cannot be in
1468 the may-alias set of name memory tags. Suppose that we have
1469 grouped the aliases in this code so that may-aliases(a) = SMT.20
1471 p_5 = &a;
1473 # a_9 = V_MAY_DEF <a_8>
1474 p_5->field = 0
1475 ... Several modifications to SMT.20 ...
1476 # VUSE <a_9>
1477 x_30 = p_5->field
1479 Since p_5 points to 'a', the optimizers will try to propagate 0
1480 into p_5->field, but that is wrong because there have been
1481 modifications to 'SMT.20' in between. To prevent this we have to
1482 replace 'a' with 'SMT.20' in the name tag of p_5. */
1483 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
1485 size_t j;
1486 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1487 VEC(tree,gc) *aliases;
1488 tree alias;
1490 if (name_tag == NULL_TREE)
1491 continue;
1493 aliases = var_ann (name_tag)->may_aliases;
1494 for (j = 0; VEC_iterate (tree, aliases, j, alias); j++)
1496 var_ann_t ann = var_ann (alias);
1498 if ((!MTAG_P (alias)
1499 || TREE_CODE (alias) == STRUCT_FIELD_TAG)
1500 && ann->may_aliases)
1502 tree new_alias;
1504 gcc_assert (VEC_length (tree, ann->may_aliases) == 1);
1506 new_alias = VEC_index (tree, ann->may_aliases, 0);
1507 replace_may_alias (name_tag, j, new_alias);
1512 compact_name_tags ();
1514 if (dump_file)
1515 fprintf (dump_file,
1516 "%s: Total number of aliased vops after grouping: %ld%s\n",
1517 get_name (current_function_decl),
1518 ai->total_alias_vops,
1519 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1523 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1525 static void
1526 create_alias_map_for (tree var, struct alias_info *ai)
1528 struct alias_map_d *alias_map;
1529 alias_map = XCNEW (struct alias_map_d);
1530 alias_map->var = var;
1531 alias_map->set = get_alias_set (var);
1532 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1536 /* Create memory tags for all the dereferenced pointers and build the
1537 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1538 sets. Based on the address escape and points-to information collected
1539 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1540 variables whose address is not needed anymore. */
1542 static void
1543 setup_pointers_and_addressables (struct alias_info *ai)
1545 size_t n_vars, num_addressable_vars, num_pointers;
1546 referenced_var_iterator rvi;
1547 tree var;
1548 VEC (tree, heap) *varvec = NULL;
1549 safe_referenced_var_iterator srvi;
1551 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1552 num_addressable_vars = num_pointers = 0;
1554 FOR_EACH_REFERENCED_VAR (var, rvi)
1556 if (may_be_aliased (var))
1557 num_addressable_vars++;
1559 if (POINTER_TYPE_P (TREE_TYPE (var)))
1561 /* Since we don't keep track of volatile variables, assume that
1562 these pointers are used in indirect store operations. */
1563 if (TREE_THIS_VOLATILE (var))
1564 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
1566 num_pointers++;
1570 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1571 always going to be slightly bigger than we actually need them
1572 because some TREE_ADDRESSABLE variables will be marked
1573 non-addressable below and only pointers with unique symbol tags are
1574 going to be added to POINTERS. */
1575 ai->addressable_vars = XCNEWVEC (struct alias_map_d *, num_addressable_vars);
1576 ai->pointers = XCNEWVEC (struct alias_map_d *, num_pointers);
1577 ai->num_addressable_vars = 0;
1578 ai->num_pointers = 0;
1580 /* Since we will be creating symbol memory tags within this loop,
1581 cache the value of NUM_REFERENCED_VARS to avoid processing the
1582 additional tags unnecessarily. */
1583 n_vars = num_referenced_vars;
1585 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
1587 var_ann_t v_ann = var_ann (var);
1588 subvar_t svars;
1590 /* Name memory tags already have flow-sensitive aliasing
1591 information, so they need not be processed by
1592 compute_flow_insensitive_aliasing. Similarly, symbol memory
1593 tags are already accounted for when we process their
1594 associated pointer.
1596 Structure fields, on the other hand, have to have some of this
1597 information processed for them, but it's pointless to mark them
1598 non-addressable (since they are fake variables anyway). */
1599 if (MTAG_P (var) && TREE_CODE (var) != STRUCT_FIELD_TAG)
1600 continue;
1602 /* Remove the ADDRESSABLE flag from every addressable variable whose
1603 address is not needed anymore. This is caused by the propagation
1604 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1605 removal of dead pointer assignments done by the early scalar
1606 cleanup passes. */
1607 if (TREE_ADDRESSABLE (var))
1609 if (!bitmap_bit_p (gimple_addressable_vars (cfun), DECL_UID (var))
1610 && TREE_CODE (var) != RESULT_DECL
1611 && !is_global_var (var))
1613 bool okay_to_mark = true;
1615 /* Since VAR is now a regular GIMPLE register, we will need
1616 to rename VAR into SSA afterwards. */
1617 mark_sym_for_renaming (var);
1619 /* If VAR can have sub-variables, and any of its
1620 sub-variables has its address taken, then we cannot
1621 remove the addressable flag from VAR. */
1622 if (var_can_have_subvars (var)
1623 && (svars = get_subvars_for_var (var)))
1625 subvar_t sv;
1627 for (sv = svars; sv; sv = sv->next)
1629 if (bitmap_bit_p (gimple_addressable_vars (cfun),
1630 DECL_UID (sv->var)))
1631 okay_to_mark = false;
1632 mark_sym_for_renaming (sv->var);
1636 /* The address of VAR is not needed, remove the
1637 addressable bit, so that it can be optimized as a
1638 regular variable. */
1639 if (okay_to_mark)
1640 mark_non_addressable (var);
1644 /* Global variables and addressable locals may be aliased. Create an
1645 entry in ADDRESSABLE_VARS for VAR. */
1646 if (may_be_aliased (var)
1647 && (!var_can_have_subvars (var)
1648 || get_subvars_for_var (var) == NULL))
1650 create_alias_map_for (var, ai);
1651 mark_sym_for_renaming (var);
1654 /* Add pointer variables that have been dereferenced to the POINTERS
1655 array and create a symbol memory tag for them. */
1656 if (POINTER_TYPE_P (TREE_TYPE (var)))
1658 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var))
1659 || bitmap_bit_p (ai->dereferenced_ptrs_load, DECL_UID (var))))
1661 tree tag;
1662 var_ann_t t_ann;
1664 /* If pointer VAR still doesn't have a memory tag
1665 associated with it, create it now or re-use an
1666 existing one. */
1667 tag = get_tmt_for (var, ai);
1668 t_ann = var_ann (tag);
1670 /* The symbol tag will need to be renamed into SSA
1671 afterwards. Note that we cannot do this inside
1672 get_tmt_for because aliasing may run multiple times
1673 and we only create symbol tags the first time. */
1674 mark_sym_for_renaming (tag);
1676 /* Similarly, if pointer VAR used to have another type
1677 tag, we will need to process it in the renamer to
1678 remove the stale virtual operands. */
1679 if (v_ann->symbol_mem_tag)
1680 mark_sym_for_renaming (v_ann->symbol_mem_tag);
1682 /* Associate the tag with pointer VAR. */
1683 v_ann->symbol_mem_tag = tag;
1685 /* If pointer VAR has been used in a store operation,
1686 then its memory tag must be marked as written-to. */
1687 if (bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var)))
1688 bitmap_set_bit (ai->written_vars, DECL_UID (tag));
1690 /* All the dereferences of pointer VAR count as
1691 references of TAG. Since TAG can be associated with
1692 several pointers, add the dereferences of VAR to the
1693 TAG. */
1694 NUM_REFERENCES_SET (t_ann,
1695 NUM_REFERENCES (t_ann)
1696 + NUM_REFERENCES (v_ann));
1698 else
1700 /* The pointer has not been dereferenced. If it had a
1701 symbol memory tag, remove it and mark the old tag for
1702 renaming to remove it out of the IL. */
1703 var_ann_t ann = var_ann (var);
1704 tree tag = ann->symbol_mem_tag;
1705 if (tag)
1707 mark_sym_for_renaming (tag);
1708 ann->symbol_mem_tag = NULL_TREE;
1713 VEC_free (tree, heap, varvec);
1717 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1718 every call site, we need to emit V_MAY_DEF expressions to represent the
1719 clobbering effects of the call for variables whose address escapes the
1720 current function.
1722 One approach is to group all call-clobbered variables into a single
1723 representative that is used as an alias of every call-clobbered variable
1724 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1725 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1727 The second approach is to emit a clobbering V_MAY_DEF for every
1728 call-clobbered variable at call sites. This is the preferred way in terms
1729 of optimization opportunities but it may create too many V_MAY_DEF operands
1730 if there are many call clobbered variables and function calls in the
1731 function.
1733 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1734 function calls found by the number of call-clobbered variables. If that
1735 product is beyond a certain threshold, as determined by the parameterized
1736 values shown below, we use .GLOBAL_VAR.
1738 FIXME. This heuristic should be improved. One idea is to use several
1739 .GLOBAL_VARs of different types instead of a single one. The thresholds
1740 have been derived from a typical bootstrap cycle, including all target
1741 libraries. Compile times were found increase by ~1% compared to using
1742 .GLOBAL_VAR. */
1744 static void
1745 maybe_create_global_var (struct alias_info *ai)
1747 unsigned i, n_clobbered;
1748 bitmap_iterator bi;
1750 /* No need to create it, if we have one already. */
1751 if (gimple_global_var (cfun) == NULL_TREE)
1753 /* Count all the call-clobbered variables. */
1754 n_clobbered = 0;
1755 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1757 n_clobbered++;
1760 /* If the number of virtual operands that would be needed to
1761 model all the call-clobbered variables is larger than
1762 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1764 Also create .GLOBAL_VAR if there are no call-clobbered
1765 variables and the program contains a mixture of pure/const
1766 and regular function calls. This is to avoid the problem
1767 described in PR 20115:
1769 int X;
1770 int func_pure (void) { return X; }
1771 int func_non_pure (int a) { X += a; }
1772 int foo ()
1774 int a = func_pure ();
1775 func_non_pure (a);
1776 a = func_pure ();
1777 return a;
1780 Since foo() has no call-clobbered variables, there is
1781 no relationship between the calls to func_pure and
1782 func_non_pure. Since func_pure has no side-effects, value
1783 numbering optimizations elide the second call to func_pure.
1784 So, if we have some pure/const and some regular calls in the
1785 program we create .GLOBAL_VAR to avoid missing these
1786 relations. */
1787 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1788 || (n_clobbered == 0
1789 && ai->num_calls_found > 0
1790 && ai->num_pure_const_calls_found > 0
1791 && ai->num_calls_found > ai->num_pure_const_calls_found))
1792 create_global_var ();
1795 /* Mark all call-clobbered symbols for renaming. Since the initial
1796 rewrite into SSA ignored all call sites, we may need to rename
1797 .GLOBAL_VAR and the call-clobbered variables. */
1798 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1800 tree var = referenced_var (i);
1802 /* If the function has calls to clobbering functions and
1803 .GLOBAL_VAR has been created, make it an alias for all
1804 call-clobbered variables. */
1805 if (gimple_global_var (cfun) && var != gimple_global_var (cfun))
1807 add_may_alias (var, gimple_global_var (cfun));
1808 gcc_assert (!get_subvars_for_var (var));
1811 mark_sym_for_renaming (var);
1816 /* Return TRUE if pointer PTR may point to variable VAR.
1818 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1819 This is needed because when checking for type conflicts we are
1820 interested in the alias set of the memory location pointed-to by
1821 PTR. The alias set of PTR itself is irrelevant.
1823 VAR_ALIAS_SET is the alias set for VAR. */
1825 static bool
1826 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1827 tree var, HOST_WIDE_INT var_alias_set,
1828 bool alias_set_only)
1830 tree mem;
1832 alias_stats.alias_queries++;
1833 alias_stats.simple_queries++;
1835 /* By convention, a variable cannot alias itself. */
1836 mem = var_ann (ptr)->symbol_mem_tag;
1837 if (mem == var)
1839 alias_stats.alias_noalias++;
1840 alias_stats.simple_resolved++;
1841 return false;
1844 /* If -fargument-noalias-global is > 2, pointer arguments may
1845 not point to anything else. */
1846 if (flag_argument_noalias > 2 && TREE_CODE (ptr) == PARM_DECL)
1848 alias_stats.alias_noalias++;
1849 alias_stats.simple_resolved++;
1850 return false;
1853 /* If -fargument-noalias-global is > 1, pointer arguments may
1854 not point to global variables. */
1855 if (flag_argument_noalias > 1 && is_global_var (var)
1856 && TREE_CODE (ptr) == PARM_DECL)
1858 alias_stats.alias_noalias++;
1859 alias_stats.simple_resolved++;
1860 return false;
1863 /* If either MEM or VAR is a read-only global and the other one
1864 isn't, then PTR cannot point to VAR. */
1865 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1866 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1868 alias_stats.alias_noalias++;
1869 alias_stats.simple_resolved++;
1870 return false;
1873 gcc_assert (TREE_CODE (mem) == SYMBOL_MEMORY_TAG);
1875 alias_stats.tbaa_queries++;
1877 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1878 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1880 alias_stats.alias_noalias++;
1881 alias_stats.tbaa_resolved++;
1882 return false;
1885 /* If var is a record or union type, ptr cannot point into var
1886 unless there is some operation explicit address operation in the
1887 program that can reference a field of the ptr's dereferenced
1888 type. This also assumes that the types of both var and ptr are
1889 contained within the compilation unit, and that there is no fancy
1890 addressing arithmetic associated with any of the types
1891 involved. */
1893 if ((mem_alias_set != 0) && (var_alias_set != 0))
1895 tree ptr_type = TREE_TYPE (ptr);
1896 tree var_type = TREE_TYPE (var);
1898 /* The star count is -1 if the type at the end of the pointer_to
1899 chain is not a record or union type. */
1900 if ((!alias_set_only) &&
1901 ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
1903 int ptr_star_count = 0;
1905 /* Ipa_type_escape_star_count_of_interesting_type is a little to
1906 restrictive for the pointer type, need to allow pointers to
1907 primitive types as long as those types cannot be pointers
1908 to everything. */
1909 while (POINTER_TYPE_P (ptr_type))
1910 /* Strip the *'s off. */
1912 ptr_type = TREE_TYPE (ptr_type);
1913 ptr_star_count++;
1916 /* There does not appear to be a better test to see if the
1917 pointer type was one of the pointer to everything
1918 types. */
1920 if (ptr_star_count > 0)
1922 alias_stats.structnoaddress_queries++;
1923 if (ipa_type_escape_field_does_not_clobber_p (var_type,
1924 TREE_TYPE (ptr)))
1926 alias_stats.structnoaddress_resolved++;
1927 alias_stats.alias_noalias++;
1928 return false;
1931 else if (ptr_star_count == 0)
1933 /* If ptr_type was not really a pointer to type, it cannot
1934 alias. */
1935 alias_stats.structnoaddress_queries++;
1936 alias_stats.structnoaddress_resolved++;
1937 alias_stats.alias_noalias++;
1938 return false;
1943 alias_stats.alias_mayalias++;
1944 return true;
1948 /* Add ALIAS to the set of variables that may alias VAR. */
1950 static void
1951 add_may_alias (tree var, tree alias)
1953 size_t i;
1954 var_ann_t v_ann = get_var_ann (var);
1955 var_ann_t a_ann = get_var_ann (alias);
1956 tree al;
1958 /* Don't allow self-referential aliases. */
1959 gcc_assert (var != alias);
1961 /* ALIAS must be addressable if it's being added to an alias set. */
1962 #if 1
1963 TREE_ADDRESSABLE (alias) = 1;
1964 #else
1965 gcc_assert (may_be_aliased (alias));
1966 #endif
1968 if (v_ann->may_aliases == NULL)
1969 v_ann->may_aliases = VEC_alloc (tree, gc, 2);
1971 /* Avoid adding duplicates. */
1972 for (i = 0; VEC_iterate (tree, v_ann->may_aliases, i, al); i++)
1973 if (alias == al)
1974 return;
1976 VEC_safe_push (tree, gc, v_ann->may_aliases, alias);
1977 a_ann->is_aliased = 1;
1981 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1983 static void
1984 replace_may_alias (tree var, size_t i, tree new_alias)
1986 var_ann_t v_ann = var_ann (var);
1987 VEC_replace (tree, v_ann->may_aliases, i, new_alias);
1991 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1993 static void
1994 set_pt_anything (tree ptr)
1996 struct ptr_info_def *pi = get_ptr_info (ptr);
1998 pi->pt_anything = 1;
1999 pi->pt_vars = NULL;
2001 /* The pointer used to have a name tag, but we now found it pointing
2002 to an arbitrary location. The name tag needs to be renamed and
2003 disassociated from PTR. */
2004 if (pi->name_mem_tag)
2006 mark_sym_for_renaming (pi->name_mem_tag);
2007 pi->name_mem_tag = NULL_TREE;
2012 /* Return true if STMT is an "escape" site from the current function. Escape
2013 sites those statements which might expose the address of a variable
2014 outside the current function. STMT is an escape site iff:
2016 1- STMT is a function call, or
2017 2- STMT is an __asm__ expression, or
2018 3- STMT is an assignment to a non-local variable, or
2019 4- STMT is a return statement.
2021 Return the type of escape site found, if we found one, or NO_ESCAPE
2022 if none. */
2024 enum escape_type
2025 is_escape_site (tree stmt)
2027 tree call = get_call_expr_in (stmt);
2028 if (call != NULL_TREE)
2030 if (!TREE_SIDE_EFFECTS (call))
2031 return ESCAPE_TO_PURE_CONST;
2033 return ESCAPE_TO_CALL;
2035 else if (TREE_CODE (stmt) == ASM_EXPR)
2036 return ESCAPE_TO_ASM;
2037 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2039 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
2041 /* Get to the base of _REF nodes. */
2042 if (TREE_CODE (lhs) != SSA_NAME)
2043 lhs = get_base_address (lhs);
2045 /* If we couldn't recognize the LHS of the assignment, assume that it
2046 is a non-local store. */
2047 if (lhs == NULL_TREE)
2048 return ESCAPE_UNKNOWN;
2050 if (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == NOP_EXPR
2051 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == CONVERT_EXPR
2052 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2054 tree from
2055 = TREE_TYPE (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0));
2056 tree to = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
2058 /* If the RHS is a conversion between a pointer and an integer, the
2059 pointer escapes since we can't track the integer. */
2060 if (POINTER_TYPE_P (from) && !POINTER_TYPE_P (to))
2061 return ESCAPE_BAD_CAST;
2063 /* Same if the RHS is a conversion between a regular pointer and a
2064 ref-all pointer since we can't track the SMT of the former. */
2065 if (POINTER_TYPE_P (from) && !TYPE_REF_CAN_ALIAS_ALL (from)
2066 && POINTER_TYPE_P (to) && TYPE_REF_CAN_ALIAS_ALL (to))
2067 return ESCAPE_BAD_CAST;
2070 /* If the LHS is an SSA name, it can't possibly represent a non-local
2071 memory store. */
2072 if (TREE_CODE (lhs) == SSA_NAME)
2073 return NO_ESCAPE;
2075 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2076 local variables we cannot be sure if it will escape, because we
2077 don't have information about objects not in SSA form. Need to
2078 implement something along the lines of
2080 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2081 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2082 Conference on Object-Oriented Programming Systems, Languages, and
2083 Applications (OOPSLA), pp. 1-19, 1999. */
2084 return ESCAPE_STORED_IN_GLOBAL;
2086 else if (TREE_CODE (stmt) == RETURN_EXPR)
2087 return ESCAPE_TO_RETURN;
2089 return NO_ESCAPE;
2092 /* Create a new memory tag of type TYPE.
2093 Does NOT push it into the current binding. */
2095 static tree
2096 create_tag_raw (enum tree_code code, tree type, const char *prefix)
2098 tree tmp_var;
2099 tree new_type;
2101 /* Make the type of the variable writable. */
2102 new_type = build_type_variant (type, 0, 0);
2103 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
2105 tmp_var = build_decl (code, create_tmp_var_name (prefix),
2106 type);
2107 /* Make the variable writable. */
2108 TREE_READONLY (tmp_var) = 0;
2110 /* It doesn't start out global. */
2111 MTAG_GLOBAL (tmp_var) = 0;
2112 TREE_STATIC (tmp_var) = 0;
2113 TREE_USED (tmp_var) = 1;
2115 return tmp_var;
2118 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2119 is considered to represent all the pointers whose pointed-to types are
2120 in the same alias set class. Otherwise, the tag represents a single
2121 SSA_NAME pointer variable. */
2123 static tree
2124 create_memory_tag (tree type, bool is_type_tag)
2126 var_ann_t ann;
2127 tree tag = create_tag_raw (is_type_tag ? SYMBOL_MEMORY_TAG : NAME_MEMORY_TAG,
2128 type, (is_type_tag) ? "SMT" : "NMT");
2130 /* By default, memory tags are local variables. Alias analysis will
2131 determine whether they should be considered globals. */
2132 DECL_CONTEXT (tag) = current_function_decl;
2134 /* Memory tags are by definition addressable. */
2135 TREE_ADDRESSABLE (tag) = 1;
2137 ann = get_var_ann (tag);
2138 ann->symbol_mem_tag = NULL_TREE;
2140 /* Add the tag to the symbol table. */
2141 add_referenced_var (tag);
2143 return tag;
2147 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2148 This is used if P_i has been found to point to a specific set of
2149 variables or to a non-aliased memory location like the address returned
2150 by malloc functions. */
2152 static tree
2153 get_nmt_for (tree ptr)
2155 struct ptr_info_def *pi = get_ptr_info (ptr);
2156 tree tag = pi->name_mem_tag;
2158 if (tag == NULL_TREE)
2159 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2160 return tag;
2164 /* Return the symbol memory tag associated to pointer PTR. A memory
2165 tag is an artificial variable that represents the memory location
2166 pointed-to by PTR. It is used to model the effects of pointer
2167 de-references on addressable variables.
2169 AI points to the data gathered during alias analysis. This
2170 function populates the array AI->POINTERS. */
2172 static tree
2173 get_tmt_for (tree ptr, struct alias_info *ai)
2175 size_t i;
2176 tree tag;
2177 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2178 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2180 /* We use a unique memory tag for all the ref-all pointers. */
2181 if (PTR_IS_REF_ALL (ptr))
2183 if (!ai->ref_all_symbol_mem_tag)
2184 ai->ref_all_symbol_mem_tag = create_memory_tag (void_type_node, true);
2185 return ai->ref_all_symbol_mem_tag;
2188 /* To avoid creating unnecessary memory tags, only create one memory tag
2189 per alias set class. Note that it may be tempting to group
2190 memory tags based on conflicting alias sets instead of
2191 equivalence. That would be wrong because alias sets are not
2192 necessarily transitive (as demonstrated by the libstdc++ test
2193 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2194 such that conflicts (A, B) == true and conflicts (A, C) == true,
2195 it does not necessarily follow that conflicts (B, C) == true. */
2196 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2198 struct alias_map_d *curr = ai->pointers[i];
2199 tree curr_tag = var_ann (curr->var)->symbol_mem_tag;
2200 if (tag_set == curr->set)
2202 tag = curr_tag;
2203 break;
2207 /* If VAR cannot alias with any of the existing memory tags, create a new
2208 tag for PTR and add it to the POINTERS array. */
2209 if (tag == NULL_TREE)
2211 struct alias_map_d *alias_map;
2213 /* If PTR did not have a symbol tag already, create a new SMT.*
2214 artificial variable representing the memory location
2215 pointed-to by PTR. */
2216 if (var_ann (ptr)->symbol_mem_tag == NULL_TREE)
2217 tag = create_memory_tag (tag_type, true);
2218 else
2219 tag = var_ann (ptr)->symbol_mem_tag;
2221 /* Add PTR to the POINTERS array. Note that we are not interested in
2222 PTR's alias set. Instead, we cache the alias set for the memory that
2223 PTR points to. */
2224 alias_map = XCNEW (struct alias_map_d);
2225 alias_map->var = ptr;
2226 alias_map->set = tag_set;
2227 ai->pointers[ai->num_pointers++] = alias_map;
2230 /* If the pointed-to type is volatile, so is the tag. */
2231 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2233 /* Make sure that the symbol tag has the same alias set as the
2234 pointed-to type. */
2235 gcc_assert (tag_set == get_alias_set (tag));
2237 return tag;
2241 /* Create GLOBAL_VAR, an artificial global variable to act as a
2242 representative of all the variables that may be clobbered by function
2243 calls. */
2245 static void
2246 create_global_var (void)
2248 tree global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2249 void_type_node);
2250 DECL_ARTIFICIAL (global_var) = 1;
2251 TREE_READONLY (global_var) = 0;
2252 DECL_EXTERNAL (global_var) = 1;
2253 TREE_STATIC (global_var) = 1;
2254 TREE_USED (global_var) = 1;
2255 DECL_CONTEXT (global_var) = NULL_TREE;
2256 TREE_THIS_VOLATILE (global_var) = 0;
2257 TREE_ADDRESSABLE (global_var) = 0;
2259 create_var_ann (global_var);
2260 mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
2261 add_referenced_var (global_var);
2262 mark_sym_for_renaming (global_var);
2263 cfun->gimple_df->global_var = global_var;
2267 /* Dump alias statistics on FILE. */
2269 static void
2270 dump_alias_stats (FILE *file)
2272 const char *funcname
2273 = lang_hooks.decl_printable_name (current_function_decl, 2);
2274 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2275 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2276 fprintf (file, "Total alias mayalias results:\t%u\n",
2277 alias_stats.alias_mayalias);
2278 fprintf (file, "Total alias noalias results:\t%u\n",
2279 alias_stats.alias_noalias);
2280 fprintf (file, "Total simple queries:\t%u\n",
2281 alias_stats.simple_queries);
2282 fprintf (file, "Total simple resolved:\t%u\n",
2283 alias_stats.simple_resolved);
2284 fprintf (file, "Total TBAA queries:\t%u\n",
2285 alias_stats.tbaa_queries);
2286 fprintf (file, "Total TBAA resolved:\t%u\n",
2287 alias_stats.tbaa_resolved);
2288 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
2289 alias_stats.structnoaddress_queries);
2290 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
2291 alias_stats.structnoaddress_resolved);
2295 /* Dump alias information on FILE. */
2297 void
2298 dump_alias_info (FILE *file)
2300 size_t i;
2301 const char *funcname
2302 = lang_hooks.decl_printable_name (current_function_decl, 2);
2303 referenced_var_iterator rvi;
2304 tree var;
2306 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2308 fprintf (file, "Aliased symbols\n\n");
2310 FOR_EACH_REFERENCED_VAR (var, rvi)
2312 if (may_be_aliased (var))
2313 dump_variable (file, var);
2316 fprintf (file, "\nDereferenced pointers\n\n");
2318 FOR_EACH_REFERENCED_VAR (var, rvi)
2320 var_ann_t ann = var_ann (var);
2321 if (ann->symbol_mem_tag)
2322 dump_variable (file, var);
2325 fprintf (file, "\nSymbol memory tags\n\n");
2327 FOR_EACH_REFERENCED_VAR (var, rvi)
2329 if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
2330 dump_variable (file, var);
2333 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2335 fprintf (file, "SSA_NAME pointers\n\n");
2336 for (i = 1; i < num_ssa_names; i++)
2338 tree ptr = ssa_name (i);
2339 struct ptr_info_def *pi;
2341 if (ptr == NULL_TREE)
2342 continue;
2344 pi = SSA_NAME_PTR_INFO (ptr);
2345 if (!SSA_NAME_IN_FREE_LIST (ptr)
2346 && pi
2347 && pi->name_mem_tag)
2348 dump_points_to_info_for (file, ptr);
2351 fprintf (file, "\nName memory tags\n\n");
2353 FOR_EACH_REFERENCED_VAR (var, rvi)
2355 if (TREE_CODE (var) == NAME_MEMORY_TAG)
2356 dump_variable (file, var);
2359 fprintf (file, "\n");
2363 /* Dump alias information on stderr. */
2365 void
2366 debug_alias_info (void)
2368 dump_alias_info (stderr);
2372 /* Return the alias information associated with pointer T. It creates a
2373 new instance if none existed. */
2375 struct ptr_info_def *
2376 get_ptr_info (tree t)
2378 struct ptr_info_def *pi;
2380 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2382 pi = SSA_NAME_PTR_INFO (t);
2383 if (pi == NULL)
2385 pi = GGC_CNEW (struct ptr_info_def);
2386 SSA_NAME_PTR_INFO (t) = pi;
2389 return pi;
2393 /* Dump points-to information for SSA_NAME PTR into FILE. */
2395 void
2396 dump_points_to_info_for (FILE *file, tree ptr)
2398 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2400 print_generic_expr (file, ptr, dump_flags);
2402 if (pi)
2404 if (pi->name_mem_tag)
2406 fprintf (file, ", name memory tag: ");
2407 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2410 if (pi->is_dereferenced)
2411 fprintf (file, ", is dereferenced");
2413 if (pi->value_escapes_p)
2414 fprintf (file, ", its value escapes");
2416 if (pi->pt_anything)
2417 fprintf (file, ", points-to anything");
2419 if (pi->pt_null)
2420 fprintf (file, ", points-to NULL");
2422 if (pi->pt_vars)
2424 unsigned ix;
2425 bitmap_iterator bi;
2427 fprintf (file, ", points-to vars: { ");
2428 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2430 print_generic_expr (file, referenced_var (ix), dump_flags);
2431 fprintf (file, " ");
2433 fprintf (file, "}");
2437 fprintf (file, "\n");
2441 /* Dump points-to information for VAR into stderr. */
2443 void
2444 debug_points_to_info_for (tree var)
2446 dump_points_to_info_for (stderr, var);
2450 /* Dump points-to information into FILE. NOTE: This function is slow, as
2451 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2453 void
2454 dump_points_to_info (FILE *file)
2456 basic_block bb;
2457 block_stmt_iterator si;
2458 ssa_op_iter iter;
2459 const char *fname =
2460 lang_hooks.decl_printable_name (current_function_decl, 2);
2461 referenced_var_iterator rvi;
2462 tree var;
2464 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2466 /* First dump points-to information for the default definitions of
2467 pointer variables. This is necessary because default definitions are
2468 not part of the code. */
2469 FOR_EACH_REFERENCED_VAR (var, rvi)
2471 if (POINTER_TYPE_P (TREE_TYPE (var)))
2473 tree def = gimple_default_def (cfun, var);
2474 if (def)
2475 dump_points_to_info_for (file, def);
2479 /* Dump points-to information for every pointer defined in the program. */
2480 FOR_EACH_BB (bb)
2482 tree phi;
2484 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2486 tree ptr = PHI_RESULT (phi);
2487 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2488 dump_points_to_info_for (file, ptr);
2491 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2493 tree stmt = bsi_stmt (si);
2494 tree def;
2495 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2496 if (POINTER_TYPE_P (TREE_TYPE (def)))
2497 dump_points_to_info_for (file, def);
2501 fprintf (file, "\n");
2505 /* Dump points-to info pointed to by PTO into STDERR. */
2507 void
2508 debug_points_to_info (void)
2510 dump_points_to_info (stderr);
2513 /* Dump to FILE the list of variables that may be aliasing VAR. */
2515 void
2516 dump_may_aliases_for (FILE *file, tree var)
2518 VEC(tree, gc) *aliases;
2520 if (TREE_CODE (var) == SSA_NAME)
2521 var = SSA_NAME_VAR (var);
2523 aliases = var_ann (var)->may_aliases;
2524 if (aliases)
2526 size_t i;
2527 tree al;
2528 fprintf (file, "{ ");
2529 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2531 print_generic_expr (file, al, dump_flags);
2532 fprintf (file, " ");
2534 fprintf (file, "}");
2539 /* Dump to stderr the list of variables that may be aliasing VAR. */
2541 void
2542 debug_may_aliases_for (tree var)
2544 dump_may_aliases_for (stderr, var);
2547 /* Return true if VAR may be aliased. */
2549 bool
2550 may_be_aliased (tree var)
2552 /* Obviously. */
2553 if (TREE_ADDRESSABLE (var))
2554 return true;
2556 /* Globally visible variables can have their addresses taken by other
2557 translation units. */
2559 if (MTAG_P (var)
2560 && (MTAG_GLOBAL (var) || TREE_PUBLIC (var)))
2561 return true;
2562 else if (!MTAG_P (var)
2563 && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
2564 return true;
2566 /* Automatic variables can't have their addresses escape any other way.
2567 This must be after the check for global variables, as extern declarations
2568 do not have TREE_STATIC set. */
2569 if (!TREE_STATIC (var))
2570 return false;
2572 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2573 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2574 we can only be sure the variable isn't addressable if it's local to the
2575 current function. */
2576 if (flag_unit_at_a_time)
2577 return false;
2578 if (decl_function_context (var) == current_function_decl)
2579 return false;
2581 return true;
2585 /* Given two symbols return TRUE if one is in the alias set of the other. */
2586 bool
2587 is_aliased_with (tree tag, tree sym)
2589 size_t i;
2590 VEC(tree,gc) *aliases;
2591 tree al;
2593 if (var_ann (sym)->is_aliased)
2595 aliases = var_ann (tag)->may_aliases;
2597 if (aliases == NULL)
2598 return false;
2600 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2601 if (al == sym)
2602 return true;
2604 else
2606 aliases = var_ann (sym)->may_aliases;
2608 if (aliases == NULL)
2609 return false;
2611 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2612 if (al == tag)
2613 return true;
2616 return false;
2619 /* The following is based on code in add_stmt_operand to ensure that the
2620 same defs/uses/vdefs/vuses will be found after replacing a reference
2621 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
2622 is the address of var. Return a memtag for the ptr, after adding the
2623 proper may_aliases to it (which are the aliases of var, if it has any,
2624 or var itself). */
2626 static tree
2627 add_may_alias_for_new_tag (tree tag, tree var)
2629 var_ann_t v_ann = var_ann (var);
2630 VEC(tree, gc) *aliases = v_ann->may_aliases;
2632 /* Case 1: |aliases| == 1 */
2633 if ((aliases != NULL)
2634 && (VEC_length (tree, aliases) == 1))
2636 tree ali = VEC_index (tree, aliases, 0);
2638 if (TREE_CODE (ali) == SYMBOL_MEMORY_TAG)
2639 return ali;
2642 /* Case 2: |aliases| == 0 */
2643 if (aliases == NULL)
2644 add_may_alias (tag, var);
2645 else
2647 /* Case 3: |aliases| > 1 */
2648 unsigned i;
2649 tree al;
2651 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2652 add_may_alias (tag, al);
2655 return tag;
2658 /* Create a new symbol tag for PTR. Construct the may-alias list of this type
2659 tag so that it has the aliasing of VAR, or of the relevant subvars of VAR
2660 according to the location accessed by EXPR.
2662 Note, the set of aliases represented by the new symbol tag are not marked
2663 for renaming. */
2665 void
2666 new_type_alias (tree ptr, tree var, tree expr)
2668 var_ann_t p_ann = var_ann (ptr);
2669 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2670 tree tag;
2671 subvar_t svars;
2672 tree ali = NULL_TREE;
2673 HOST_WIDE_INT offset, size, maxsize;
2674 tree ref;
2675 VEC (tree, heap) *overlaps = NULL;
2676 subvar_t sv;
2677 unsigned int len;
2679 gcc_assert (p_ann->symbol_mem_tag == NULL_TREE);
2680 gcc_assert (!MTAG_P (var));
2682 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
2683 gcc_assert (ref);
2685 tag = create_memory_tag (tag_type, true);
2686 p_ann->symbol_mem_tag = tag;
2688 /* Add VAR to the may-alias set of PTR's new symbol tag. If VAR has
2689 subvars, add the subvars to the tag instead of the actual var. */
2690 if (var_can_have_subvars (ref)
2691 && (svars = get_subvars_for_var (ref)))
2693 for (sv = svars; sv; sv = sv->next)
2695 bool exact;
2697 if (overlap_subvar (offset, maxsize, sv->var, &exact))
2698 VEC_safe_push (tree, heap, overlaps, sv->var);
2700 gcc_assert (overlaps != NULL);
2702 else if (var_can_have_subvars (var)
2703 && (svars = get_subvars_for_var (var)))
2705 /* If the REF is not a direct access to VAR (e.g., it is a dereference
2706 of a pointer), we should scan the virtual operands of REF the same
2707 way as tree-ssa-operands do. At the moment, this is somewhat
2708 difficult, so we just give up and add all the subvars of VAR.
2709 On mem-ssa branch, the scanning for virtual operands have been
2710 split from the rest of tree-ssa-operands, so it should be much
2711 easier to fix this problem correctly once mem-ssa is merged. */
2712 for (sv = svars; sv; sv = sv->next)
2713 VEC_safe_push (tree, heap, overlaps, sv->var);
2715 gcc_assert (overlaps != NULL);
2717 else
2718 ali = add_may_alias_for_new_tag (tag, var);
2720 len = VEC_length (tree, overlaps);
2721 if (len > 0)
2723 if (dump_file && (dump_flags & TDF_DETAILS))
2724 fprintf (dump_file, "\nnumber of overlapping subvars = %u\n", len);
2726 if (len == 1)
2727 ali = add_may_alias_for_new_tag (tag, VEC_index (tree, overlaps, 0));
2728 else if (len > 1)
2730 unsigned int k;
2731 tree sv_var;
2733 for (k = 0; VEC_iterate (tree, overlaps, k, sv_var); k++)
2735 ali = add_may_alias_for_new_tag (tag, sv_var);
2737 if (ali != tag)
2739 /* Can happen only if 'Case 1' of add_may_alias_for_new_tag
2740 took place. Since more than one svar was found, we add
2741 'ali' as one of the may_aliases of the new tag. */
2742 add_may_alias (tag, ali);
2743 ali = tag;
2747 VEC_free (tree, heap, overlaps);
2750 p_ann->symbol_mem_tag = ali;
2751 TREE_READONLY (tag) = TREE_READONLY (var);
2752 MTAG_GLOBAL (tag) = is_global_var (var);
2755 /* This represents the used range of a variable. */
2757 typedef struct used_part
2759 HOST_WIDE_INT minused;
2760 HOST_WIDE_INT maxused;
2761 /* True if we have an explicit use/def of some portion of this variable,
2762 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2763 bool explicit_uses;
2764 /* True if we have an implicit use/def of some portion of this
2765 variable. Implicit uses occur when we can't tell what part we
2766 are referencing, and have to make conservative assumptions. */
2767 bool implicit_uses;
2768 /* True if the structure is only written to or taken its address. */
2769 bool write_only;
2770 } *used_part_t;
2772 /* An array of used_part structures, indexed by variable uid. */
2774 static htab_t used_portions;
2776 struct used_part_map
2778 unsigned int uid;
2779 used_part_t to;
2782 /* Return true if the uid in the two used part maps are equal. */
2784 static int
2785 used_part_map_eq (const void *va, const void *vb)
2787 const struct used_part_map *a = (const struct used_part_map *) va;
2788 const struct used_part_map *b = (const struct used_part_map *) vb;
2789 return (a->uid == b->uid);
2792 /* Hash a from uid in a used_part_map. */
2794 static unsigned int
2795 used_part_map_hash (const void *item)
2797 return ((const struct used_part_map *)item)->uid;
2800 /* Free a used part map element. */
2802 static void
2803 free_used_part_map (void *item)
2805 free (((struct used_part_map *)item)->to);
2806 free (item);
2809 /* Lookup a used_part structure for a UID. */
2811 static used_part_t
2812 up_lookup (unsigned int uid)
2814 struct used_part_map *h, in;
2815 in.uid = uid;
2816 h = (struct used_part_map *) htab_find_with_hash (used_portions, &in, uid);
2817 if (!h)
2818 return NULL;
2819 return h->to;
2822 /* Insert the pair UID, TO into the used part hashtable. */
2824 static void
2825 up_insert (unsigned int uid, used_part_t to)
2827 struct used_part_map *h;
2828 void **loc;
2830 h = XNEW (struct used_part_map);
2831 h->uid = uid;
2832 h->to = to;
2833 loc = htab_find_slot_with_hash (used_portions, h,
2834 uid, INSERT);
2835 if (*loc != NULL)
2836 free (*loc);
2837 *(struct used_part_map **) loc = h;
2841 /* Given a variable uid, UID, get or create the entry in the used portions
2842 table for the variable. */
2844 static used_part_t
2845 get_or_create_used_part_for (size_t uid)
2847 used_part_t up;
2848 if ((up = up_lookup (uid)) == NULL)
2850 up = XCNEW (struct used_part);
2851 up->minused = INT_MAX;
2852 up->maxused = 0;
2853 up->explicit_uses = false;
2854 up->implicit_uses = false;
2855 up->write_only = true;
2858 return up;
2862 /* Create and return a structure sub-variable for field type FIELD at
2863 offset OFFSET, with size SIZE, of variable VAR. */
2865 static tree
2866 create_sft (tree var, tree field, unsigned HOST_WIDE_INT offset,
2867 unsigned HOST_WIDE_INT size)
2869 var_ann_t ann;
2870 tree subvar = create_tag_raw (STRUCT_FIELD_TAG, field, "SFT");
2872 /* We need to copy the various flags from VAR to SUBVAR, so that
2873 they are is_global_var iff the original variable was. */
2874 DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
2875 MTAG_GLOBAL (subvar) = DECL_EXTERNAL (var);
2876 TREE_PUBLIC (subvar) = TREE_PUBLIC (var);
2877 TREE_STATIC (subvar) = TREE_STATIC (var);
2878 TREE_READONLY (subvar) = TREE_READONLY (var);
2879 TREE_ADDRESSABLE (subvar) = TREE_ADDRESSABLE (var);
2881 /* Add the new variable to REFERENCED_VARS. */
2882 ann = get_var_ann (subvar);
2883 ann->symbol_mem_tag = NULL;
2884 add_referenced_var (subvar);
2885 SFT_PARENT_VAR (subvar) = var;
2886 SFT_OFFSET (subvar) = offset;
2887 SFT_SIZE (subvar) = size;
2888 return subvar;
2892 /* Given an aggregate VAR, create the subvariables that represent its
2893 fields. */
2895 static void
2896 create_overlap_variables_for (tree var)
2898 VEC(fieldoff_s,heap) *fieldstack = NULL;
2899 used_part_t up;
2900 size_t uid = DECL_UID (var);
2902 up = up_lookup (uid);
2903 if (!up
2904 || up->write_only)
2905 return;
2907 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL);
2908 if (VEC_length (fieldoff_s, fieldstack) != 0)
2910 subvar_t *subvars;
2911 fieldoff_s *fo;
2912 bool notokay = false;
2913 int fieldcount = 0;
2914 int i;
2915 HOST_WIDE_INT lastfooffset = -1;
2916 HOST_WIDE_INT lastfosize = -1;
2917 tree lastfotype = NULL_TREE;
2919 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2920 know their size, and thus, can't handle.
2921 The same is true of fields with DECL_SIZE that is not an integer
2922 constant (such as variable sized fields).
2923 Fields with offsets which are not constant will have an offset < 0
2924 We *could* handle fields that are constant sized arrays, but
2925 currently don't. Doing so would require some extra changes to
2926 tree-ssa-operands.c. */
2928 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
2930 if (!fo->size
2931 || TREE_CODE (fo->size) != INTEGER_CST
2932 || fo->offset < 0)
2934 notokay = true;
2935 break;
2937 fieldcount++;
2940 /* The current heuristic we use is as follows:
2941 If the variable has no used portions in this function, no
2942 structure vars are created for it.
2943 Otherwise,
2944 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
2945 we always create structure vars for them.
2946 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2947 some explicit uses, we create structure vars for them.
2948 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2949 no explicit uses, we do not create structure vars for them.
2952 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
2953 && !up->explicit_uses)
2955 if (dump_file && (dump_flags & TDF_DETAILS))
2957 fprintf (dump_file, "Variable ");
2958 print_generic_expr (dump_file, var, 0);
2959 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
2961 notokay = true;
2964 /* Bail out, if we can't create overlap variables. */
2965 if (notokay)
2967 VEC_free (fieldoff_s, heap, fieldstack);
2968 return;
2971 /* Otherwise, create the variables. */
2972 subvars = lookup_subvars_for_var (var);
2974 sort_fieldstack (fieldstack);
2976 for (i = VEC_length (fieldoff_s, fieldstack);
2977 VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
2979 subvar_t sv;
2980 HOST_WIDE_INT fosize;
2981 tree currfotype;
2983 fosize = TREE_INT_CST_LOW (fo->size);
2984 currfotype = fo->type;
2986 /* If this field isn't in the used portion,
2987 or it has the exact same offset and size as the last
2988 field, skip it. */
2990 if (((fo->offset <= up->minused
2991 && fo->offset + fosize <= up->minused)
2992 || fo->offset >= up->maxused)
2993 || (fo->offset == lastfooffset
2994 && fosize == lastfosize
2995 && currfotype == lastfotype))
2996 continue;
2997 sv = GGC_NEW (struct subvar);
2998 sv->next = *subvars;
2999 sv->var = create_sft (var, fo->type, fo->offset, fosize);
3001 if (dump_file)
3003 fprintf (dump_file, "structure field tag %s created for var %s",
3004 get_name (sv->var), get_name (var));
3005 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
3006 SFT_OFFSET (sv->var));
3007 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
3008 SFT_SIZE (sv->var));
3009 fprintf (dump_file, "\n");
3012 lastfotype = currfotype;
3013 lastfooffset = fo->offset;
3014 lastfosize = fosize;
3015 *subvars = sv;
3018 /* Once we have created subvars, the original is no longer call
3019 clobbered on its own. Its call clobbered status depends
3020 completely on the call clobbered status of the subvars.
3022 add_referenced_var in the above loop will take care of
3023 marking subvars of global variables as call clobbered for us
3024 to start, since they are global as well. */
3025 clear_call_clobbered (var);
3028 VEC_free (fieldoff_s, heap, fieldstack);
3032 /* Find the conservative answer to the question of what portions of what
3033 structures are used by this statement. We assume that if we have a
3034 component ref with a known size + offset, that we only need that part
3035 of the structure. For unknown cases, or cases where we do something
3036 to the whole structure, we assume we need to create fields for the
3037 entire structure. */
3039 static tree
3040 find_used_portions (tree *tp, int *walk_subtrees, void *lhs_p)
3042 switch (TREE_CODE (*tp))
3044 case GIMPLE_MODIFY_STMT:
3045 /* Recurse manually here to track whether the use is in the
3046 LHS of an assignment. */
3047 find_used_portions (&GIMPLE_STMT_OPERAND (*tp, 0), walk_subtrees, tp);
3048 return find_used_portions (&GIMPLE_STMT_OPERAND (*tp, 1),
3049 walk_subtrees, NULL);
3050 case REALPART_EXPR:
3051 case IMAGPART_EXPR:
3052 case COMPONENT_REF:
3053 case ARRAY_REF:
3055 HOST_WIDE_INT bitsize;
3056 HOST_WIDE_INT bitmaxsize;
3057 HOST_WIDE_INT bitpos;
3058 tree ref;
3059 ref = get_ref_base_and_extent (*tp, &bitpos, &bitsize, &bitmaxsize);
3060 if (DECL_P (ref)
3061 && var_can_have_subvars (ref)
3062 && bitmaxsize != -1)
3064 size_t uid = DECL_UID (ref);
3065 used_part_t up;
3067 up = get_or_create_used_part_for (uid);
3069 if (bitpos <= up->minused)
3070 up->minused = bitpos;
3071 if ((bitpos + bitmaxsize >= up->maxused))
3072 up->maxused = bitpos + bitmaxsize;
3074 if (bitsize == bitmaxsize)
3075 up->explicit_uses = true;
3076 else
3077 up->implicit_uses = true;
3078 if (!lhs_p)
3079 up->write_only = false;
3080 up_insert (uid, up);
3082 *walk_subtrees = 0;
3083 return NULL_TREE;
3086 break;
3087 /* This is here to make sure we mark the entire base variable as used
3088 when you take its address. Because our used portion analysis is
3089 simple, we aren't looking at casts or pointer arithmetic to see what
3090 happens when you take the address. */
3091 case ADDR_EXPR:
3093 tree var = get_base_address (TREE_OPERAND (*tp, 0));
3095 if (var
3096 && DECL_P (var)
3097 && DECL_SIZE (var)
3098 && var_can_have_subvars (var)
3099 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3101 used_part_t up;
3102 size_t uid = DECL_UID (var);
3104 up = get_or_create_used_part_for (uid);
3106 up->minused = 0;
3107 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3108 up->implicit_uses = true;
3109 if (!lhs_p)
3110 up->write_only = false;
3112 up_insert (uid, up);
3113 *walk_subtrees = 0;
3114 return NULL_TREE;
3117 break;
3118 case CALL_EXPR:
3120 tree *arg;
3121 for (arg = &TREE_OPERAND (*tp, 1); *arg; arg = &TREE_CHAIN (*arg))
3123 if (TREE_CODE (TREE_VALUE (*arg)) != ADDR_EXPR)
3124 find_used_portions (&TREE_VALUE (*arg), walk_subtrees, NULL);
3126 *walk_subtrees = 0;
3127 return NULL_TREE;
3129 case VAR_DECL:
3130 case PARM_DECL:
3131 case RESULT_DECL:
3133 tree var = *tp;
3134 if (DECL_SIZE (var)
3135 && var_can_have_subvars (var)
3136 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3138 used_part_t up;
3139 size_t uid = DECL_UID (var);
3141 up = get_or_create_used_part_for (uid);
3143 up->minused = 0;
3144 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3145 up->implicit_uses = true;
3147 up_insert (uid, up);
3148 *walk_subtrees = 0;
3149 return NULL_TREE;
3152 break;
3154 default:
3155 break;
3158 return NULL_TREE;
3161 /* Create structure field variables for structures used in this function. */
3163 static unsigned int
3164 create_structure_vars (void)
3166 basic_block bb;
3167 safe_referenced_var_iterator rvi;
3168 VEC (tree, heap) *varvec = NULL;
3169 tree var;
3171 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
3172 free_used_part_map);
3174 FOR_EACH_BB (bb)
3176 block_stmt_iterator bsi;
3177 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3179 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3180 find_used_portions,
3181 NULL);
3184 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, rvi)
3186 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3187 if (var
3188 && DECL_SIZE (var)
3189 && var_can_have_subvars (var)
3190 && !MTAG_P (var)
3191 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3192 create_overlap_variables_for (var);
3194 htab_delete (used_portions);
3195 VEC_free (tree, heap, varvec);
3196 return 0;
3199 static bool
3200 gate_structure_vars (void)
3202 return flag_tree_salias != 0;
3205 struct tree_opt_pass pass_create_structure_vars =
3207 "salias", /* name */
3208 gate_structure_vars, /* gate */
3209 create_structure_vars, /* execute */
3210 NULL, /* sub */
3211 NULL, /* next */
3212 0, /* static_pass_number */
3213 0, /* tv_id */
3214 PROP_cfg, /* properties_required */
3215 0, /* properties_provided */
3216 0, /* properties_destroyed */
3217 0, /* todo_flags_start */
3218 TODO_dump_func, /* todo_flags_finish */
3219 0 /* letter */
3222 /* Reset the DECL_CALL_CLOBBERED flags on our referenced vars. In
3223 theory, this only needs to be done for globals. */
3225 static unsigned int
3226 reset_cc_flags (void)
3228 tree var;
3229 referenced_var_iterator rvi;
3231 FOR_EACH_REFERENCED_VAR (var, rvi)
3232 DECL_CALL_CLOBBERED (var) = false;
3233 return 0;
3236 struct tree_opt_pass pass_reset_cc_flags =
3238 NULL, /* name */
3239 NULL, /* gate */
3240 reset_cc_flags, /* execute */
3241 NULL, /* sub */
3242 NULL, /* next */
3243 0, /* static_pass_number */
3244 0, /* tv_id */
3245 PROP_referenced_vars |PROP_cfg, /* properties_required */
3246 0, /* properties_provided */
3247 0, /* properties_destroyed */
3248 0, /* todo_flags_start */
3249 0, /* todo_flags_finish */
3250 0 /* letter */