2008-05-07 Kai Tietz <kai,tietz@onevision.com>
[official-gcc.git] / gcc / tree-ssa-alias.c
blobc52d75e633ba87ea3f4d679953b8b4c39fadf5c2
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "expr.h"
32 #include "ggc.h"
33 #include "langhooks.h"
34 #include "flags.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "tree-dump.h"
38 #include "tree-gimple.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "tree-pass.h"
42 #include "tree-ssa-structalias.h"
43 #include "convert.h"
44 #include "params.h"
45 #include "ipa-type-escape.h"
46 #include "vec.h"
47 #include "bitmap.h"
48 #include "vecprim.h"
49 #include "pointer-set.h"
50 #include "alloc-pool.h"
52 /* Broad overview of how aliasing works:
54 First we compute points-to sets, which is done in
55 tree-ssa-structalias.c
57 During points-to set constraint finding, a bunch of little bits of
58 information is collected.
59 This is not done because it is necessary for points-to, but because
60 points-to has to walk every statement anyway. The function performing
61 this collecting is update_alias_info.
63 Bits update_alias_info collects include:
64 1. Directly escaping variables and variables whose value escapes
65 (using is_escape_site). This is the set of variables and values that
66 escape prior to transitive closure of the clobbers.
67 2. The set of variables dereferenced on the LHS (into
68 dereferenced_ptr_stores)
69 3. The set of variables dereferenced on the RHS (into
70 dereferenced_ptr_loads)
71 4. The set of all pointers we saw.
72 5. The number of loads and stores for each variable
73 6. The number of statements touching memory
74 7. The set of address taken variables.
77 #1 is computed by a combination of is_escape_site, and counting the
78 number of uses/deref operators. This function properly accounts for
79 situations like &ptr->field, which is *not* a dereference.
81 After points-to sets are computed, the sets themselves still
82 contain points-to specific variables, such as a variable that says
83 the pointer points to anything, a variable that says the pointer
84 points to readonly memory, etc.
86 These are eliminated in a later phase, as we will see.
88 The rest of the phases are located in tree-ssa-alias.c
90 The next phase after points-to set computation is called
91 "setup_pointers_and_addressables"
93 This pass does 3 main things:
95 1. All variables that can have TREE_ADDRESSABLE removed safely (IE
96 non-globals whose address is not taken), have TREE_ADDRESSABLE
97 removed.
98 2. All variables that may be aliased (which is the set of addressable
99 variables and globals) at all, are marked for renaming, and have
100 symbol memory tags created for them.
101 3. All variables which are stored into have their SMT's added to
102 written vars.
105 After this function is run, all variables that will ever have an
106 SMT, have one, though its aliases are not filled in.
108 The next phase is to compute flow-insensitive aliasing, which in
109 our case, is a misnomer. it is really computing aliasing that
110 requires no transitive closure to be correct. In particular, it
111 uses stack vs non-stack, TBAA, etc, to determine whether two
112 symbols could *ever* alias . This phase works by going through all
113 the pointers we collected during update_alias_info, and for every
114 addressable variable in the program, seeing if they alias. If so,
115 the addressable variable is added to the symbol memory tag for the
116 pointer.
118 As part of this, we handle symbol memory tags that conflict but
119 have no aliases in common, by forcing them to have a symbol in
120 common (through unioning alias sets or adding one as an alias of
121 the other), or by adding one as an alias of another. The case of
122 conflicts with no aliases in common occurs mainly due to aliasing
123 we cannot see. In particular, it generally means we have a load
124 through a pointer whose value came from outside the function.
125 Without an addressable symbol to point to, they would get the wrong
126 answer.
128 After flow insensitive aliasing is computed, we compute name tags
129 (called compute_flow_sensitive_info). We walk each pointer we
130 collected and see if it has a usable points-to set. If so, we
131 generate a name tag using that pointer, and make an alias bitmap for
132 it. Name tags are shared between all things with the same alias
133 bitmap. The alias bitmap will be translated from what points-to
134 computed. In particular, the "anything" variable in points-to will be
135 transformed into a pruned set of SMT's and their aliases that
136 compute_flow_insensitive_aliasing computed.
137 Note that since 4.3, every pointer that points-to computed a solution for
138 will get a name tag (whereas before 4.3, only those whose set did
139 *not* include the anything variable would). At the point where name
140 tags are all assigned, symbol memory tags are dead, and could be
141 deleted, *except* on global variables. Global variables still use
142 symbol memory tags as of right now.
144 After name tags are computed, the set of clobbered variables is
145 transitively closed. In particular, we compute the set of clobbered
146 variables based on the initial set of clobbers, plus the aliases of
147 pointers which either escape, or have their value escape.
149 After this, maybe_create_global_var is run, which handles a corner
150 case where we have no call clobbered variables, but have pure and
151 non-pure functions.
153 Staring at this function, I now remember it is a hack for the fact
154 that we do not mark all globals in the program as call clobbered for a
155 function unless they are actually used in that function. Instead, we
156 only mark the set that is actually clobbered. As a result, you can
157 end up with situations where you have no call clobbered vars set.
159 After maybe_create_global_var, we set pointers with the REF_ALL flag
160 to have alias sets that include all clobbered
161 memory tags and variables.
163 After this, memory partitioning is computed (by the function
164 compute_memory_partitions) and alias sets are reworked accordingly.
166 Lastly, we delete partitions with no symbols, and clean up after
167 ourselves. */
169 /* Structure to map a variable to its alias set. */
170 struct alias_map_d
172 /* Variable and its alias set. */
173 tree var;
174 alias_set_type set;
178 /* Counters used to display statistics on alias analysis. */
179 struct alias_stats_d
181 unsigned int alias_queries;
182 unsigned int alias_mayalias;
183 unsigned int alias_noalias;
184 unsigned int simple_queries;
185 unsigned int simple_resolved;
186 unsigned int tbaa_queries;
187 unsigned int tbaa_resolved;
188 unsigned int structnoaddress_queries;
189 unsigned int structnoaddress_resolved;
193 /* Local variables. */
194 static struct alias_stats_d alias_stats;
195 static bitmap_obstack alias_bitmap_obstack;
197 /* Local functions. */
198 static void compute_flow_insensitive_aliasing (struct alias_info *);
199 static void dump_alias_stats (FILE *);
200 static bool may_alias_p (tree, alias_set_type, tree, alias_set_type, bool);
201 static tree create_memory_tag (tree type, bool is_type_tag);
202 static tree get_smt_for (tree, struct alias_info *);
203 static tree get_nmt_for (tree);
204 static void add_may_alias (tree, tree);
205 static struct alias_info *init_alias_info (void);
206 static void delete_alias_info (struct alias_info *);
207 static void compute_flow_sensitive_aliasing (struct alias_info *);
208 static void setup_pointers_and_addressables (struct alias_info *);
209 static void create_global_var (void);
210 static void maybe_create_global_var (void);
211 static void set_pt_anything (tree);
213 void debug_mp_info (VEC(mem_sym_stats_t,heap) *);
215 static alloc_pool mem_sym_stats_pool;
217 /* Return memory reference stats for symbol VAR. Create a new slot in
218 cfun->gimple_df->mem_sym_stats if needed. */
220 static struct mem_sym_stats_d *
221 get_mem_sym_stats_for (tree var)
223 void **slot;
224 struct mem_sym_stats_d *stats;
225 struct pointer_map_t *map = gimple_mem_ref_stats (cfun)->mem_sym_stats;
227 gcc_assert (map);
229 slot = pointer_map_insert (map, var);
230 if (*slot == NULL)
232 stats = pool_alloc (mem_sym_stats_pool);
233 memset (stats, 0, sizeof (*stats));
234 stats->var = var;
235 *slot = (void *) stats;
237 else
238 stats = (struct mem_sym_stats_d *) *slot;
240 return stats;
244 /* Return memory reference statistics for variable VAR in function FN.
245 This is computed by alias analysis, but it is not kept
246 incrementally up-to-date. So, these stats are only accurate if
247 pass_may_alias has been run recently. If no alias information
248 exists, this function returns NULL. */
250 static mem_sym_stats_t
251 mem_sym_stats (struct function *fn, tree var)
253 void **slot;
254 struct pointer_map_t *stats_map = gimple_mem_ref_stats (fn)->mem_sym_stats;
256 if (stats_map == NULL)
257 return NULL;
259 slot = pointer_map_contains (stats_map, var);
260 if (slot == NULL)
261 return NULL;
263 return (mem_sym_stats_t) *slot;
267 /* Set MPT to be the memory partition associated with symbol SYM. */
269 static inline void
270 set_memory_partition (tree sym, tree mpt)
272 #if defined ENABLE_CHECKING
273 if (mpt)
274 gcc_assert (TREE_CODE (mpt) == MEMORY_PARTITION_TAG
275 && !is_gimple_reg (sym));
276 #endif
278 var_ann (sym)->mpt = mpt;
279 if (mpt)
281 if (MPT_SYMBOLS (mpt) == NULL)
282 MPT_SYMBOLS (mpt) = BITMAP_ALLOC (&alias_bitmap_obstack);
284 bitmap_set_bit (MPT_SYMBOLS (mpt), DECL_UID (sym));
286 /* MPT inherits the call-clobbering attributes from SYM. */
287 if (is_call_clobbered (sym))
289 MTAG_GLOBAL (mpt) = 1;
290 mark_call_clobbered (mpt, ESCAPE_IS_GLOBAL);
296 /* Mark variable VAR as being non-addressable. */
298 static void
299 mark_non_addressable (tree var)
301 tree mpt;
303 if (!TREE_ADDRESSABLE (var))
304 return;
306 mpt = memory_partition (var);
308 if (!MTAG_P (var))
309 var_ann (var)->call_clobbered = false;
311 bitmap_clear_bit (gimple_call_clobbered_vars (cfun), DECL_UID (var));
312 TREE_ADDRESSABLE (var) = 0;
314 if (mpt)
316 /* Note that it's possible for a symbol to have an associated
317 MPT and the MPT have a NULL empty set. During
318 init_alias_info, all MPTs get their sets cleared out, but the
319 symbols still point to the old MPTs that used to hold them.
320 This is done so that compute_memory_partitions can now which
321 symbols are losing or changing partitions and mark them for
322 renaming. */
323 if (MPT_SYMBOLS (mpt))
324 bitmap_clear_bit (MPT_SYMBOLS (mpt), DECL_UID (var));
325 set_memory_partition (var, NULL_TREE);
330 /* qsort comparison function to sort type/name tags by DECL_UID. */
332 static int
333 sort_tags_by_id (const void *pa, const void *pb)
335 const_tree const a = *(const_tree const *)pa;
336 const_tree const b = *(const_tree const *)pb;
338 return DECL_UID (a) - DECL_UID (b);
341 /* Initialize WORKLIST to contain those memory tags that are marked call
342 clobbered. Initialized WORKLIST2 to contain the reasons these
343 memory tags escaped. */
345 static void
346 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
347 VEC (int, heap) **worklist2,
348 bitmap on_worklist)
350 referenced_var_iterator rvi;
351 tree curr;
353 FOR_EACH_REFERENCED_VAR (curr, rvi)
355 if (MTAG_P (curr) && is_call_clobbered (curr))
357 VEC_safe_push (tree, heap, *worklist, curr);
358 VEC_safe_push (int, heap, *worklist2,
359 var_ann (curr)->escape_mask);
360 bitmap_set_bit (on_worklist, DECL_UID (curr));
365 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
366 ALIAS is not already marked call clobbered, and is a memory
367 tag. */
369 static void
370 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
371 VEC (int, heap) **worklist2, int reason,
372 bitmap on_worklist)
374 if (MTAG_P (alias) && !is_call_clobbered (alias)
375 && !bitmap_bit_p (on_worklist, DECL_UID (alias)))
377 VEC_safe_push (tree, heap, *worklist, alias);
378 VEC_safe_push (int, heap, *worklist2, reason);
379 bitmap_set_bit (on_worklist, DECL_UID (alias));
383 /* Mark aliases of TAG as call clobbered, and place any tags on the
384 alias list that were not already call clobbered on WORKLIST. */
386 static void
387 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
388 VEC (int, heap) **worklist2,
389 bitmap on_worklist, bitmap queued)
391 bitmap aliases;
392 bitmap_iterator bi;
393 unsigned int i;
394 tree entry;
395 var_ann_t ta = var_ann (tag);
397 if (!MTAG_P (tag))
398 return;
399 aliases = may_aliases (tag);
400 if (!aliases)
401 return;
403 EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
405 entry = referenced_var (i);
406 /* If you clobber one part of a structure, you
407 clobber the entire thing. While this does not make
408 the world a particularly nice place, it is necessary
409 in order to allow C/C++ tricks that involve
410 pointer arithmetic to work. */
411 if (TREE_CODE (entry) == STRUCT_FIELD_TAG)
412 bitmap_set_bit (queued, DECL_UID (SFT_PARENT_VAR (entry)));
413 else if (!unmodifiable_var_p (entry))
415 add_to_worklist (entry, worklist, worklist2, ta->escape_mask,
416 on_worklist);
417 mark_call_clobbered (entry, ta->escape_mask);
420 if (!bitmap_empty_p (queued))
422 EXECUTE_IF_SET_IN_BITMAP (queued, 0, i, bi)
424 subvar_t svars = get_subvars_for_var (referenced_var (i));
425 unsigned int i;
426 tree subvar;
428 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
429 if (!unmodifiable_var_p (subvar))
430 mark_call_clobbered (subvar, ta->escape_mask);
432 bitmap_clear (queued);
436 /* Tags containing global vars need to be marked as global.
437 Tags containing call clobbered vars need to be marked as call
438 clobbered. */
440 static void
441 compute_tag_properties (void)
443 referenced_var_iterator rvi;
444 tree tag;
445 bool changed = true;
446 VEC (tree, heap) *taglist = NULL;
448 FOR_EACH_REFERENCED_VAR (tag, rvi)
450 if (!MTAG_P (tag) || TREE_CODE (tag) == STRUCT_FIELD_TAG)
451 continue;
452 VEC_safe_push (tree, heap, taglist, tag);
455 /* We sort the taglist by DECL_UID, for two reasons.
456 1. To get a sequential ordering to make the bitmap accesses
457 faster.
458 2. Because of the way we compute aliases, it's more likely that
459 an earlier tag is included in a later tag, and this will reduce
460 the number of iterations.
462 If we had a real tag graph, we would just topo-order it and be
463 done with it. */
464 qsort (VEC_address (tree, taglist),
465 VEC_length (tree, taglist),
466 sizeof (tree),
467 sort_tags_by_id);
469 /* Go through each tag not marked as global, and if it aliases
470 global vars, mark it global.
472 If the tag contains call clobbered vars, mark it call
473 clobbered.
475 This loop iterates because tags may appear in the may-aliases
476 list of other tags when we group. */
478 while (changed)
480 unsigned int k;
482 changed = false;
483 for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
485 bitmap ma;
486 bitmap_iterator bi;
487 unsigned int i;
488 tree entry;
489 bool tagcc = is_call_clobbered (tag);
490 bool tagglobal = MTAG_GLOBAL (tag);
492 if (tagcc && tagglobal)
493 continue;
495 ma = may_aliases (tag);
496 if (!ma)
497 continue;
499 EXECUTE_IF_SET_IN_BITMAP (ma, 0, i, bi)
501 entry = referenced_var (i);
502 /* Call clobbered entries cause the tag to be marked
503 call clobbered. */
504 if (!tagcc && is_call_clobbered (entry))
506 mark_call_clobbered (tag, var_ann (entry)->escape_mask);
507 tagcc = true;
508 changed = true;
511 /* Global vars cause the tag to be marked global. */
512 if (!tagglobal && is_global_var (entry))
514 MTAG_GLOBAL (tag) = true;
515 changed = true;
516 tagglobal = true;
519 /* Early exit once both global and cc are set, since the
520 loop can't do any more than that. */
521 if (tagcc && tagglobal)
522 break;
526 VEC_free (tree, heap, taglist);
529 /* Set up the initial variable clobbers and globalness.
530 When this function completes, only tags whose aliases need to be
531 clobbered will be set clobbered. Tags clobbered because they
532 contain call clobbered vars are handled in compute_tag_properties. */
534 static void
535 set_initial_properties (struct alias_info *ai)
537 unsigned int i;
538 referenced_var_iterator rvi;
539 tree var;
540 tree ptr;
541 bitmap queued;
543 /* Temporary bitmap to avoid quadratic behavior in marking
544 call clobbers. */
545 queued = BITMAP_ALLOC (&alias_bitmap_obstack);
547 FOR_EACH_REFERENCED_VAR (var, rvi)
549 if (is_global_var (var)
550 && (!var_can_have_subvars (var)
551 || get_subvars_for_var (var) == NULL))
553 if (!unmodifiable_var_p (var))
554 mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
556 else if (TREE_CODE (var) == PARM_DECL
557 && gimple_default_def (cfun, var)
558 && POINTER_TYPE_P (TREE_TYPE (var)))
560 tree def = gimple_default_def (cfun, var);
561 get_ptr_info (def)->value_escapes_p = 1;
562 get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;
566 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
568 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
569 tree tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
571 if (pi->value_escapes_p)
573 /* If PTR escapes then its associated memory tags and
574 pointed-to variables are call-clobbered. */
575 if (pi->name_mem_tag)
576 mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
578 if (tag)
579 mark_call_clobbered (tag, pi->escape_mask);
581 if (pi->pt_vars)
583 bitmap_iterator bi;
584 unsigned int j;
585 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
587 tree alias = referenced_var (j);
589 /* If you clobber one part of a structure, you
590 clobber the entire thing. While this does not make
591 the world a particularly nice place, it is necessary
592 in order to allow C/C++ tricks that involve
593 pointer arithmetic to work. */
594 if (TREE_CODE (alias) == STRUCT_FIELD_TAG)
595 bitmap_set_bit (queued, DECL_UID (SFT_PARENT_VAR (alias)));
596 else if (!unmodifiable_var_p (alias))
597 mark_call_clobbered (alias, pi->escape_mask);
599 /* Process variables we need to clobber all parts of. */
600 if (!bitmap_empty_p (queued))
602 EXECUTE_IF_SET_IN_BITMAP (queued, 0, j, bi)
604 subvar_t svars = get_subvars_for_var (referenced_var (j));
605 unsigned int i;
606 tree subvar;
608 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
609 if (!unmodifiable_var_p (subvar))
610 mark_call_clobbered (subvar, pi->escape_mask);
612 bitmap_clear (queued);
617 /* If the name tag is call clobbered, so is the symbol tag
618 associated with the base VAR_DECL. */
619 if (pi->name_mem_tag
620 && tag
621 && is_call_clobbered (pi->name_mem_tag))
622 mark_call_clobbered (tag, pi->escape_mask);
624 /* Name tags and symbol tags that we don't know where they point
625 to, might point to global memory, and thus, are clobbered.
627 FIXME: This is not quite right. They should only be
628 clobbered if value_escapes_p is true, regardless of whether
629 they point to global memory or not.
630 So removing this code and fixing all the bugs would be nice.
631 It is the cause of a bunch of clobbering. */
632 if ((pi->pt_global_mem || pi->pt_anything)
633 && pi->is_dereferenced && pi->name_mem_tag)
635 mark_call_clobbered (pi->name_mem_tag, ESCAPE_IS_GLOBAL);
636 MTAG_GLOBAL (pi->name_mem_tag) = true;
639 if ((pi->pt_global_mem || pi->pt_anything)
640 && pi->is_dereferenced
641 && tag)
643 mark_call_clobbered (tag, ESCAPE_IS_GLOBAL);
644 MTAG_GLOBAL (tag) = true;
648 BITMAP_FREE (queued);
651 /* Compute which variables need to be marked call clobbered because
652 their tag is call clobbered, and which tags need to be marked
653 global because they contain global variables. */
655 static void
656 compute_call_clobbered (struct alias_info *ai)
658 VEC (tree, heap) *worklist = NULL;
659 VEC (int,heap) *worklist2 = NULL;
660 bitmap on_worklist, queued;
662 timevar_push (TV_CALL_CLOBBER);
663 on_worklist = BITMAP_ALLOC (NULL);
664 queued = BITMAP_ALLOC (NULL);
666 set_initial_properties (ai);
667 init_transitive_clobber_worklist (&worklist, &worklist2, on_worklist);
668 while (VEC_length (tree, worklist) != 0)
670 tree curr = VEC_pop (tree, worklist);
671 int reason = VEC_pop (int, worklist2);
673 bitmap_clear_bit (on_worklist, DECL_UID (curr));
674 mark_call_clobbered (curr, reason);
675 mark_aliases_call_clobbered (curr, &worklist, &worklist2,
676 on_worklist, queued);
678 VEC_free (tree, heap, worklist);
679 VEC_free (int, heap, worklist2);
680 BITMAP_FREE (on_worklist);
681 BITMAP_FREE (queued);
682 compute_tag_properties ();
683 timevar_pop (TV_CALL_CLOBBER);
687 /* Dump memory partition information to FILE. */
689 static void
690 dump_memory_partitions (FILE *file)
692 unsigned i, npart;
693 unsigned long nsyms;
694 tree mpt;
696 fprintf (file, "\nMemory partitions\n\n");
697 for (i = 0, npart = 0, nsyms = 0;
698 VEC_iterate (tree, gimple_ssa_operands (cfun)->mpt_table, i, mpt);
699 i++)
701 if (mpt)
703 bitmap syms = MPT_SYMBOLS (mpt);
704 unsigned long n = (syms) ? bitmap_count_bits (syms) : 0;
706 fprintf (file, "#%u: ", i);
707 print_generic_expr (file, mpt, 0);
708 fprintf (file, ": %lu elements: ", n);
709 dump_decl_set (file, syms);
710 npart++;
711 nsyms += n;
715 fprintf (file, "\n%u memory partitions holding %lu symbols\n", npart, nsyms);
719 /* Dump memory partition information to stderr. */
721 void
722 debug_memory_partitions (void)
724 dump_memory_partitions (stderr);
728 /* Return true if memory partitioning is required given the memory
729 reference estimates in STATS. */
731 static inline bool
732 need_to_partition_p (struct mem_ref_stats_d *stats)
734 long num_vops = stats->num_vuses + stats->num_vdefs;
735 long avg_vops = CEIL (num_vops, stats->num_mem_stmts);
736 return (num_vops > (long) MAX_ALIASED_VOPS
737 && avg_vops > (long) AVG_ALIASED_VOPS);
741 /* Count the actual number of virtual operators in CFUN. Note that
742 this is only meaningful after virtual operands have been populated,
743 so it should be invoked at the end of compute_may_aliases.
745 The number of virtual operators are stored in *NUM_VDEFS_P and
746 *NUM_VUSES_P, the number of partitioned symbols in
747 *NUM_PARTITIONED_P and the number of unpartitioned symbols in
748 *NUM_UNPARTITIONED_P.
750 If any of these pointers is NULL the corresponding count is not
751 computed. */
753 static void
754 count_mem_refs (long *num_vuses_p, long *num_vdefs_p,
755 long *num_partitioned_p, long *num_unpartitioned_p)
757 block_stmt_iterator bsi;
758 basic_block bb;
759 long num_vdefs, num_vuses, num_partitioned, num_unpartitioned;
760 referenced_var_iterator rvi;
761 tree sym;
763 num_vuses = num_vdefs = num_partitioned = num_unpartitioned = 0;
765 if (num_vuses_p || num_vdefs_p)
766 FOR_EACH_BB (bb)
767 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
769 tree stmt = bsi_stmt (bsi);
770 if (stmt_references_memory_p (stmt))
772 num_vuses += NUM_SSA_OPERANDS (stmt, SSA_OP_VUSE);
773 num_vdefs += NUM_SSA_OPERANDS (stmt, SSA_OP_VDEF);
777 if (num_partitioned_p || num_unpartitioned_p)
778 FOR_EACH_REFERENCED_VAR (sym, rvi)
780 if (is_gimple_reg (sym))
781 continue;
783 if (memory_partition (sym))
784 num_partitioned++;
785 else
786 num_unpartitioned++;
789 if (num_vdefs_p)
790 *num_vdefs_p = num_vdefs;
792 if (num_vuses_p)
793 *num_vuses_p = num_vuses;
795 if (num_partitioned_p)
796 *num_partitioned_p = num_partitioned;
798 if (num_unpartitioned_p)
799 *num_unpartitioned_p = num_unpartitioned;
803 /* The list is sorted by increasing partitioning score (PSCORE).
804 This score is computed such that symbols with high scores are
805 those that are least likely to be partitioned. Given a symbol
806 MP->VAR, PSCORE(S) is the result of the following weighted sum
808 PSCORE(S) = FW * 64 + FR * 32
809 + DW * 16 + DR * 8
810 + IW * 4 + IR * 2
811 + NO_ALIAS
813 where
815 FW Execution frequency of writes to S
816 FR Execution frequency of reads from S
817 DW Number of direct writes to S
818 DR Number of direct reads from S
819 IW Number of indirect writes to S
820 IR Number of indirect reads from S
821 NO_ALIAS State of the NO_ALIAS* flags
823 The basic idea here is that symbols that are frequently
824 written-to in hot paths of the code are the last to be considered
825 for partitioning. */
827 static inline long
828 mem_sym_score (mem_sym_stats_t mp)
830 /* Unpartitionable SFTs are automatically thrown to the bottom of
831 the list. They are not stored in partitions, but they are used
832 for computing overall statistics. */
833 if (TREE_CODE (mp->var) == STRUCT_FIELD_TAG
834 && SFT_UNPARTITIONABLE_P (mp->var))
835 return LONG_MAX;
837 return mp->frequency_writes * 64 + mp->frequency_reads * 32
838 + mp->num_direct_writes * 16 + mp->num_direct_reads * 8
839 + mp->num_indirect_writes * 4 + mp->num_indirect_reads * 2
840 + var_ann (mp->var)->noalias_state;
844 /* Dump memory reference stats for function CFUN to FILE. */
846 void
847 dump_mem_ref_stats (FILE *file)
849 long actual_num_vuses, actual_num_vdefs;
850 long num_partitioned, num_unpartitioned;
851 struct mem_ref_stats_d *stats;
853 stats = gimple_mem_ref_stats (cfun);
855 count_mem_refs (&actual_num_vuses, &actual_num_vdefs, &num_partitioned,
856 &num_unpartitioned);
858 fprintf (file, "\nMemory reference statistics for %s\n\n",
859 lang_hooks.decl_printable_name (current_function_decl, 2));
861 fprintf (file, "Number of memory statements: %ld\n",
862 stats->num_mem_stmts);
863 fprintf (file, "Number of call sites: %ld\n",
864 stats->num_call_sites);
865 fprintf (file, "Number of pure/const call sites: %ld\n",
866 stats->num_pure_const_call_sites);
867 fprintf (file, "Number of asm sites: %ld\n",
868 stats->num_asm_sites);
869 fprintf (file, "Estimated number of loads: %ld (%ld/stmt)\n",
870 stats->num_vuses,
871 (stats->num_mem_stmts)
872 ? CEIL (stats->num_vuses, stats->num_mem_stmts)
873 : 0);
874 fprintf (file, "Actual number of loads: %ld (%ld/stmt)\n",
875 actual_num_vuses,
876 (stats->num_mem_stmts)
877 ? CEIL (actual_num_vuses, stats->num_mem_stmts)
878 : 0);
880 if (actual_num_vuses > stats->num_vuses + (stats->num_vuses / 25))
881 fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
883 fprintf (file, "Estimated number of stores: %ld (%ld/stmt)\n",
884 stats->num_vdefs,
885 (stats->num_mem_stmts)
886 ? CEIL (stats->num_vdefs, stats->num_mem_stmts)
887 : 0);
888 fprintf (file, "Actual number of stores: %ld (%ld/stmt)\n",
889 actual_num_vdefs,
890 (stats->num_mem_stmts)
891 ? CEIL (actual_num_vdefs, stats->num_mem_stmts)
892 : 0);
894 if (actual_num_vdefs > stats->num_vdefs + (stats->num_vdefs / 25))
895 fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
897 fprintf (file, "Partitioning thresholds: MAX = %d AVG = %d "
898 "(%sNEED TO PARTITION)\n", MAX_ALIASED_VOPS, AVG_ALIASED_VOPS,
899 stats->num_mem_stmts && need_to_partition_p (stats) ? "" : "NO ");
900 fprintf (file, "Number of partitioned symbols: %ld\n", num_partitioned);
901 fprintf (file, "Number of unpartitioned symbols: %ld\n", num_unpartitioned);
905 /* Dump memory reference stats for function FN to stderr. */
907 void
908 debug_mem_ref_stats (void)
910 dump_mem_ref_stats (stderr);
914 /* Dump memory reference stats for variable VAR to FILE. */
916 static void
917 dump_mem_sym_stats (FILE *file, tree var)
919 mem_sym_stats_t stats = mem_sym_stats (cfun, var);
921 if (stats == NULL)
922 return;
924 fprintf (file, "read frequency: %6ld, write frequency: %6ld, "
925 "direct reads: %3ld, direct writes: %3ld, "
926 "indirect reads: %4ld, indirect writes: %4ld, symbol: ",
927 stats->frequency_reads, stats->frequency_writes,
928 stats->num_direct_reads, stats->num_direct_writes,
929 stats->num_indirect_reads, stats->num_indirect_writes);
930 print_generic_expr (file, stats->var, 0);
931 fprintf (file, ", tags: ");
932 dump_decl_set (file, stats->parent_tags);
936 /* Dump memory reference stats for variable VAR to stderr. */
938 void
939 debug_mem_sym_stats (tree var)
941 dump_mem_sym_stats (stderr, var);
944 /* Dump memory reference stats for variable VAR to FILE. For use
945 of tree-dfa.c:dump_variable. */
947 void
948 dump_mem_sym_stats_for_var (FILE *file, tree var)
950 mem_sym_stats_t stats = mem_sym_stats (cfun, var);
952 if (stats == NULL)
953 return;
955 fprintf (file, ", score: %ld", mem_sym_score (stats));
956 fprintf (file, ", direct reads: %ld", stats->num_direct_reads);
957 fprintf (file, ", direct writes: %ld", stats->num_direct_writes);
958 fprintf (file, ", indirect reads: %ld", stats->num_indirect_reads);
959 fprintf (file, ", indirect writes: %ld", stats->num_indirect_writes);
962 /* Dump memory reference stats for all memory symbols to FILE. */
964 static void
965 dump_all_mem_sym_stats (FILE *file)
967 referenced_var_iterator rvi;
968 tree sym;
970 FOR_EACH_REFERENCED_VAR (sym, rvi)
972 if (is_gimple_reg (sym))
973 continue;
975 dump_mem_sym_stats (file, sym);
980 /* Dump memory reference stats for all memory symbols to stderr. */
982 void
983 debug_all_mem_sym_stats (void)
985 dump_all_mem_sym_stats (stderr);
989 /* Dump the MP_INFO array to FILE. */
991 static void
992 dump_mp_info (FILE *file, VEC(mem_sym_stats_t,heap) *mp_info)
994 unsigned i;
995 mem_sym_stats_t mp_p;
997 for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
998 if (!mp_p->partitioned_p)
999 dump_mem_sym_stats (file, mp_p->var);
1003 /* Dump the MP_INFO array to stderr. */
1005 void
1006 debug_mp_info (VEC(mem_sym_stats_t,heap) *mp_info)
1008 dump_mp_info (stderr, mp_info);
1012 /* Update memory reference stats for symbol VAR in statement STMT.
1013 NUM_DIRECT_READS and NUM_DIRECT_WRITES specify the number of times
1014 that VAR is read/written in STMT (indirect reads/writes are not
1015 recorded by this function, see compute_memory_partitions). */
1017 void
1018 update_mem_sym_stats_from_stmt (tree var, tree stmt, long num_direct_reads,
1019 long num_direct_writes)
1021 mem_sym_stats_t stats;
1023 gcc_assert (num_direct_reads >= 0 && num_direct_writes >= 0);
1025 stats = get_mem_sym_stats_for (var);
1027 stats->num_direct_reads += num_direct_reads;
1028 stats->frequency_reads += ((long) bb_for_stmt (stmt)->frequency
1029 * num_direct_reads);
1031 stats->num_direct_writes += num_direct_writes;
1032 stats->frequency_writes += ((long) bb_for_stmt (stmt)->frequency
1033 * num_direct_writes);
1037 /* Given two MP_INFO entries MP1 and MP2, return -1 if MP1->VAR should
1038 be partitioned before MP2->VAR, 0 if they are the same or 1 if
1039 MP1->VAR should be partitioned after MP2->VAR. */
1041 static inline int
1042 compare_mp_info_entries (mem_sym_stats_t mp1, mem_sym_stats_t mp2)
1044 long pscore1 = mem_sym_score (mp1);
1045 long pscore2 = mem_sym_score (mp2);
1047 if (pscore1 < pscore2)
1048 return -1;
1049 else if (pscore1 > pscore2)
1050 return 1;
1051 else
1052 return DECL_UID (mp1->var) - DECL_UID (mp2->var);
1056 /* Comparison routine for qsort. The list is sorted by increasing
1057 partitioning score (PSCORE). This score is computed such that
1058 symbols with high scores are those that are least likely to be
1059 partitioned. */
1061 static int
1062 mp_info_cmp (const void *p, const void *q)
1064 mem_sym_stats_t e1 = *((const mem_sym_stats_t *) p);
1065 mem_sym_stats_t e2 = *((const mem_sym_stats_t *) q);
1066 return compare_mp_info_entries (e1, e2);
1070 /* Sort the array of reference counts used to compute memory partitions.
1071 Elements are sorted in ascending order of execution frequency and
1072 descending order of virtual operators needed. */
1074 static inline void
1075 sort_mp_info (VEC(mem_sym_stats_t,heap) *list)
1077 unsigned num = VEC_length (mem_sym_stats_t, list);
1079 if (num < 2)
1080 return;
1082 if (num == 2)
1084 if (compare_mp_info_entries (VEC_index (mem_sym_stats_t, list, 0),
1085 VEC_index (mem_sym_stats_t, list, 1)) > 0)
1087 /* Swap elements if they are in the wrong order. */
1088 mem_sym_stats_t tmp = VEC_index (mem_sym_stats_t, list, 0);
1089 VEC_replace (mem_sym_stats_t, list, 0,
1090 VEC_index (mem_sym_stats_t, list, 1));
1091 VEC_replace (mem_sym_stats_t, list, 1, tmp);
1094 return;
1097 /* There are 3 or more elements, call qsort. */
1098 qsort (VEC_address (mem_sym_stats_t, list),
1099 VEC_length (mem_sym_stats_t, list),
1100 sizeof (mem_sym_stats_t),
1101 mp_info_cmp);
1105 /* Return the memory partition tag (MPT) associated with memory
1106 symbol SYM. */
1108 static tree
1109 get_mpt_for (tree sym)
1111 tree mpt;
1113 /* Don't create a new tag unnecessarily. */
1114 mpt = memory_partition (sym);
1115 if (mpt == NULL_TREE)
1117 mpt = create_tag_raw (MEMORY_PARTITION_TAG, TREE_TYPE (sym), "MPT");
1118 TREE_ADDRESSABLE (mpt) = 0;
1119 add_referenced_var (mpt);
1120 VEC_safe_push (tree, heap, gimple_ssa_operands (cfun)->mpt_table, mpt);
1121 gcc_assert (MPT_SYMBOLS (mpt) == NULL);
1122 set_memory_partition (sym, mpt);
1125 return mpt;
1129 /* Add MP_P->VAR to a memory partition and return the partition. */
1131 static tree
1132 find_partition_for (mem_sym_stats_t mp_p)
1134 unsigned i;
1135 VEC(tree,heap) *mpt_table;
1136 tree mpt;
1138 mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1139 mpt = NULL_TREE;
1141 /* Find an existing partition for MP_P->VAR. */
1142 for (i = 0; VEC_iterate (tree, mpt_table, i, mpt); i++)
1144 mem_sym_stats_t mpt_stats;
1146 /* If MPT does not have any symbols yet, use it. */
1147 if (MPT_SYMBOLS (mpt) == NULL)
1148 break;
1150 /* Otherwise, see if MPT has common parent tags with MP_P->VAR,
1151 but avoid grouping clobbered variables with non-clobbered
1152 variables (otherwise, this tends to creates a single memory
1153 partition because other call-clobbered variables may have
1154 common parent tags with non-clobbered ones). */
1155 mpt_stats = get_mem_sym_stats_for (mpt);
1156 if (mp_p->parent_tags
1157 && mpt_stats->parent_tags
1158 && is_call_clobbered (mpt) == is_call_clobbered (mp_p->var)
1159 && bitmap_intersect_p (mpt_stats->parent_tags, mp_p->parent_tags))
1160 break;
1162 /* If no common parent tags are found, see if both MPT and
1163 MP_P->VAR are call-clobbered. */
1164 if (is_call_clobbered (mpt) && is_call_clobbered (mp_p->var))
1165 break;
1168 if (mpt == NULL_TREE)
1169 mpt = get_mpt_for (mp_p->var);
1170 else
1171 set_memory_partition (mp_p->var, mpt);
1173 mp_p->partitioned_p = true;
1175 mark_sym_for_renaming (mp_p->var);
1176 mark_sym_for_renaming (mpt);
1178 return mpt;
1182 /* Rewrite the alias set for TAG to use the newly created partitions.
1183 If TAG is NULL, rewrite the set of call-clobbered variables.
1184 NEW_ALIASES is a scratch bitmap to build the new set of aliases for
1185 TAG. */
1187 static void
1188 rewrite_alias_set_for (tree tag, bitmap new_aliases)
1190 bitmap_iterator bi;
1191 unsigned i;
1192 tree mpt, sym;
1194 EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, i, bi)
1196 sym = referenced_var (i);
1197 mpt = memory_partition (sym);
1198 if (mpt)
1199 bitmap_set_bit (new_aliases, DECL_UID (mpt));
1200 else
1201 bitmap_set_bit (new_aliases, DECL_UID (sym));
1204 /* Rebuild the may-alias array for TAG. */
1205 bitmap_copy (MTAG_ALIASES (tag), new_aliases);
1209 /* Determine how many virtual operands can be saved by partitioning
1210 MP_P->VAR into MPT. When a symbol S is thrown inside a partition
1211 P, every virtual operand that used to reference S will now
1212 reference P. Whether it reduces the number of virtual operands
1213 depends on:
1215 1- Direct references to S are never saved. Instead of the virtual
1216 operand to S, we will now have a virtual operand to P.
1218 2- Indirect references to S are reduced only for those memory tags
1219 holding S that already had other symbols partitioned into P.
1220 For instance, if a memory tag T has the alias set { a b S c },
1221 the first time we partition S into P, the alias set will become
1222 { a b P c }, so no virtual operands will be saved. However, if
1223 we now partition symbol 'c' into P, then the alias set for T
1224 will become { a b P }, so we will be saving one virtual operand
1225 for every indirect reference to 'c'.
1227 3- Is S is call-clobbered, we save as many virtual operands as
1228 call/asm sites exist in the code, but only if other
1229 call-clobbered symbols have been grouped into P. The first
1230 call-clobbered symbol that we group does not produce any
1231 savings.
1233 MEM_REF_STATS points to CFUN's memory reference information. */
1235 static void
1236 estimate_vop_reduction (struct mem_ref_stats_d *mem_ref_stats,
1237 mem_sym_stats_t mp_p, tree mpt)
1239 unsigned i;
1240 bitmap_iterator bi;
1241 mem_sym_stats_t mpt_stats;
1243 /* We should only get symbols with indirect references here. */
1244 gcc_assert (mp_p->num_indirect_reads > 0 || mp_p->num_indirect_writes > 0);
1246 /* Note that the only statistics we keep for MPT is the set of
1247 parent tags to know which memory tags have had alias members
1248 partitioned, and the indicator has_call_clobbered_vars.
1249 Reference counts are not important for MPT. */
1250 mpt_stats = get_mem_sym_stats_for (mpt);
1252 /* Traverse all the parent tags for MP_P->VAR. For every tag T, if
1253 partition P is already grouping aliases of T, then reduce the
1254 number of virtual operands by the number of direct references
1255 to T. */
1256 if (mp_p->parent_tags)
1258 if (mpt_stats->parent_tags == NULL)
1259 mpt_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1261 EXECUTE_IF_SET_IN_BITMAP (mp_p->parent_tags, 0, i, bi)
1263 if (bitmap_bit_p (mpt_stats->parent_tags, i))
1265 /* Partition MPT is already partitioning symbols in the
1266 alias set for TAG. This means that we are now saving
1267 1 virtual operand for every direct reference to TAG. */
1268 tree tag = referenced_var (i);
1269 mem_sym_stats_t tag_stats = mem_sym_stats (cfun, tag);
1270 mem_ref_stats->num_vuses -= tag_stats->num_direct_reads;
1271 mem_ref_stats->num_vdefs -= tag_stats->num_direct_writes;
1273 else
1275 /* This is the first symbol in tag I's alias set that is
1276 being grouped under MPT. We will not save any
1277 virtual operands this time, but record that MPT is
1278 grouping a symbol from TAG's alias set so that the
1279 next time we get the savings. */
1280 bitmap_set_bit (mpt_stats->parent_tags, i);
1285 /* If MP_P->VAR is call-clobbered, and MPT is already grouping
1286 call-clobbered symbols, then we will save as many virtual
1287 operands as asm/call sites there are. */
1288 if (is_call_clobbered (mp_p->var))
1290 if (mpt_stats->has_call_clobbered_vars)
1291 mem_ref_stats->num_vdefs -= mem_ref_stats->num_call_sites
1292 + mem_ref_stats->num_asm_sites;
1293 else
1294 mpt_stats->has_call_clobbered_vars = true;
1299 /* Helper for compute_memory_partitions. Transfer reference counts
1300 from pointers to their pointed-to sets. Counters for pointers were
1301 computed by update_alias_info. MEM_REF_STATS points to CFUN's
1302 memory reference information. */
1304 static void
1305 update_reference_counts (struct mem_ref_stats_d *mem_ref_stats)
1307 unsigned i;
1308 bitmap_iterator bi;
1309 mem_sym_stats_t sym_stats;
1311 for (i = 1; i < num_ssa_names; i++)
1313 tree ptr;
1314 struct ptr_info_def *pi;
1316 ptr = ssa_name (i);
1317 if (ptr
1318 && POINTER_TYPE_P (TREE_TYPE (ptr))
1319 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1320 && pi->is_dereferenced)
1322 unsigned j;
1323 bitmap_iterator bj;
1324 tree tag;
1325 mem_sym_stats_t ptr_stats, tag_stats;
1327 /* If PTR has flow-sensitive points-to information, use
1328 PTR's name tag, otherwise use the symbol tag associated
1329 with PTR's symbol. */
1330 if (pi->name_mem_tag)
1331 tag = pi->name_mem_tag;
1332 else
1333 tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
1335 ptr_stats = get_mem_sym_stats_for (ptr);
1336 tag_stats = get_mem_sym_stats_for (tag);
1338 /* TAG has as many direct references as dereferences we
1339 found for its parent pointer. */
1340 tag_stats->num_direct_reads += ptr_stats->num_direct_reads;
1341 tag_stats->num_direct_writes += ptr_stats->num_direct_writes;
1343 /* All the dereferences of pointer PTR are considered direct
1344 references to PTR's memory tag (TAG). In turn,
1345 references to TAG will become virtual operands for every
1346 symbol in TAG's alias set. So, for every symbol ALIAS in
1347 TAG's alias set, add as many indirect references to ALIAS
1348 as direct references there are for TAG. */
1349 if (MTAG_ALIASES (tag))
1350 EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, j, bj)
1352 tree alias = referenced_var (j);
1353 sym_stats = get_mem_sym_stats_for (alias);
1355 /* All the direct references to TAG are indirect references
1356 to ALIAS. */
1357 sym_stats->num_indirect_reads += ptr_stats->num_direct_reads;
1358 sym_stats->num_indirect_writes += ptr_stats->num_direct_writes;
1359 sym_stats->frequency_reads += ptr_stats->frequency_reads;
1360 sym_stats->frequency_writes += ptr_stats->frequency_writes;
1362 /* Indicate that TAG is one of ALIAS's parent tags. */
1363 if (sym_stats->parent_tags == NULL)
1364 sym_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1365 bitmap_set_bit (sym_stats->parent_tags, DECL_UID (tag));
1370 /* Call-clobbered symbols are indirectly written at every
1371 call/asm site. */
1372 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1374 tree sym = referenced_var (i);
1375 sym_stats = get_mem_sym_stats_for (sym);
1376 sym_stats->num_indirect_writes += mem_ref_stats->num_call_sites
1377 + mem_ref_stats->num_asm_sites;
1380 /* Addressable symbols are indirectly written at some ASM sites.
1381 Since only ASM sites that clobber memory actually affect
1382 addressable symbols, this is an over-estimation. */
1383 EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, i, bi)
1385 tree sym = referenced_var (i);
1386 sym_stats = get_mem_sym_stats_for (sym);
1387 sym_stats->num_indirect_writes += mem_ref_stats->num_asm_sites;
1392 /* Helper for compute_memory_partitions. Add all memory symbols to
1393 *MP_INFO_P and compute the initial estimate for the total number of
1394 virtual operands needed. MEM_REF_STATS points to CFUN's memory
1395 reference information. On exit, *TAGS_P will contain the list of
1396 memory tags whose alias set need to be rewritten after
1397 partitioning. */
1399 static void
1400 build_mp_info (struct mem_ref_stats_d *mem_ref_stats,
1401 VEC(mem_sym_stats_t,heap) **mp_info_p,
1402 VEC(tree,heap) **tags_p)
1404 tree var;
1405 referenced_var_iterator rvi;
1407 FOR_EACH_REFERENCED_VAR (var, rvi)
1409 mem_sym_stats_t sym_stats;
1410 tree old_mpt;
1412 /* We are only interested in memory symbols other than MPTs. */
1413 if (is_gimple_reg (var) || TREE_CODE (var) == MEMORY_PARTITION_TAG)
1414 continue;
1416 /* Collect memory tags into the TAGS array so that we can
1417 rewrite their alias sets after partitioning. */
1418 if (MTAG_P (var) && MTAG_ALIASES (var))
1419 VEC_safe_push (tree, heap, *tags_p, var);
1421 /* Since we are going to re-compute partitions, any symbols that
1422 used to belong to a partition must be detached from it and
1423 marked for renaming. */
1424 if ((old_mpt = memory_partition (var)) != NULL)
1426 mark_sym_for_renaming (old_mpt);
1427 set_memory_partition (var, NULL_TREE);
1428 mark_sym_for_renaming (var);
1431 sym_stats = get_mem_sym_stats_for (var);
1433 /* Add VAR's reference info to MP_INFO. Note that the only
1434 symbols that make sense to partition are those that have
1435 indirect references. If a symbol S is always directly
1436 referenced, partitioning it will not reduce the number of
1437 virtual operators. The only symbols that are profitable to
1438 partition are those that belong to alias sets and/or are
1439 call-clobbered. */
1440 if (sym_stats->num_indirect_reads > 0
1441 || sym_stats->num_indirect_writes > 0)
1442 VEC_safe_push (mem_sym_stats_t, heap, *mp_info_p, sym_stats);
1444 /* Update the number of estimated VOPS. Note that direct
1445 references to memory tags are always counted as indirect
1446 references to their alias set members, so if a memory tag has
1447 aliases, do not count its direct references to avoid double
1448 accounting. */
1449 if (!MTAG_P (var) || !MTAG_ALIASES (var))
1451 mem_ref_stats->num_vuses += sym_stats->num_direct_reads;
1452 mem_ref_stats->num_vdefs += sym_stats->num_direct_writes;
1455 mem_ref_stats->num_vuses += sym_stats->num_indirect_reads;
1456 mem_ref_stats->num_vdefs += sym_stats->num_indirect_writes;
1461 /* Compute memory partitions. A memory partition (MPT) is an
1462 arbitrary grouping of memory symbols, such that references to one
1463 member of the group is considered a reference to all the members of
1464 the group.
1466 As opposed to alias sets in memory tags, the grouping into
1467 partitions is completely arbitrary and only done to reduce the
1468 number of virtual operands. The only rule that needs to be
1469 observed when creating memory partitions is that given two memory
1470 partitions MPT.i and MPT.j, they must not contain symbols in
1471 common.
1473 Memory partitions are used when putting the program into Memory-SSA
1474 form. In particular, in Memory-SSA PHI nodes are not computed for
1475 individual memory symbols. They are computed for memory
1476 partitions. This reduces the amount of PHI nodes in the SSA graph
1477 at the expense of precision (i.e., it makes unrelated stores affect
1478 each other).
1480 However, it is possible to increase precision by changing this
1481 partitioning scheme. For instance, if the partitioning scheme is
1482 such that get_mpt_for is the identity function (that is,
1483 get_mpt_for (s) = s), this will result in ultimate precision at the
1484 expense of huge SSA webs.
1486 At the other extreme, a partitioning scheme that groups all the
1487 symbols in the same set results in minimal SSA webs and almost
1488 total loss of precision.
1490 There partitioning heuristic uses three parameters to decide the
1491 order in which symbols are processed. The list of symbols is
1492 sorted so that symbols that are more likely to be partitioned are
1493 near the top of the list:
1495 - Execution frequency. If a memory references is in a frequently
1496 executed code path, grouping it into a partition may block useful
1497 transformations and cause sub-optimal code generation. So, the
1498 partition heuristic tries to avoid grouping symbols with high
1499 execution frequency scores. Execution frequency is taken
1500 directly from the basic blocks where every reference is made (see
1501 update_mem_sym_stats_from_stmt), which in turn uses the
1502 profile guided machinery, so if the program is compiled with PGO
1503 enabled, more accurate partitioning decisions will be made.
1505 - Number of references. Symbols with few references in the code,
1506 are partitioned before symbols with many references.
1508 - NO_ALIAS attributes. Symbols with any of the NO_ALIAS*
1509 attributes are partitioned after symbols marked MAY_ALIAS.
1511 Once the list is sorted, the partitioning proceeds as follows:
1513 1- For every symbol S in MP_INFO, create a new memory partition MP,
1514 if necessary. To avoid memory partitions that contain symbols
1515 from non-conflicting alias sets, memory partitions are
1516 associated to the memory tag that holds S in its alias set. So,
1517 when looking for a memory partition for S, the memory partition
1518 associated with one of the memory tags holding S is chosen. If
1519 none exists, a new one is created.
1521 2- Add S to memory partition MP.
1523 3- Reduce by 1 the number of VOPS for every memory tag holding S.
1525 4- If the total number of VOPS is less than MAX_ALIASED_VOPS or the
1526 average number of VOPS per statement is less than
1527 AVG_ALIASED_VOPS, stop. Otherwise, go to the next symbol in the
1528 list. */
1530 static void
1531 compute_memory_partitions (void)
1533 tree tag;
1534 unsigned i;
1535 mem_sym_stats_t mp_p;
1536 VEC(mem_sym_stats_t,heap) *mp_info;
1537 bitmap new_aliases;
1538 VEC(tree,heap) *tags;
1539 struct mem_ref_stats_d *mem_ref_stats;
1540 int prev_max_aliased_vops;
1542 mem_ref_stats = gimple_mem_ref_stats (cfun);
1543 gcc_assert (mem_ref_stats->num_vuses == 0 && mem_ref_stats->num_vdefs == 0);
1545 if (mem_ref_stats->num_mem_stmts == 0)
1546 return;
1548 timevar_push (TV_MEMORY_PARTITIONING);
1550 mp_info = NULL;
1551 tags = NULL;
1552 prev_max_aliased_vops = MAX_ALIASED_VOPS;
1554 /* Since we clearly cannot lower the number of virtual operators
1555 below the total number of memory statements in the function, we
1556 may need to adjust MAX_ALIASED_VOPS beforehand. */
1557 if (MAX_ALIASED_VOPS < mem_ref_stats->num_mem_stmts)
1558 MAX_ALIASED_VOPS = mem_ref_stats->num_mem_stmts;
1560 /* Update reference stats for all the pointed-to variables and
1561 memory tags. */
1562 update_reference_counts (mem_ref_stats);
1564 /* Add all the memory symbols to MP_INFO. */
1565 build_mp_info (mem_ref_stats, &mp_info, &tags);
1567 /* No partitions required if we are below the threshold. */
1568 if (!need_to_partition_p (mem_ref_stats))
1570 if (dump_file)
1571 fprintf (dump_file, "\nMemory partitioning NOT NEEDED for %s\n",
1572 get_name (current_function_decl));
1573 goto done;
1576 /* Sort the MP_INFO array so that symbols that should be partitioned
1577 first are near the top of the list. */
1578 sort_mp_info (mp_info);
1580 if (dump_file)
1582 fprintf (dump_file, "\nMemory partitioning NEEDED for %s\n\n",
1583 get_name (current_function_decl));
1584 fprintf (dump_file, "Memory symbol references before partitioning:\n");
1585 dump_mp_info (dump_file, mp_info);
1588 /* Create partitions for variables in MP_INFO until we have enough
1589 to lower the total number of VOPS below MAX_ALIASED_VOPS or if
1590 the average number of VOPS per statement is below
1591 AVG_ALIASED_VOPS. */
1592 for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
1594 tree mpt;
1596 /* If we are below the threshold, stop. */
1597 if (!need_to_partition_p (mem_ref_stats))
1598 break;
1600 /* SFTs that are marked unpartitionable should not be added to
1601 partitions. These SFTs are special because they mark the
1602 first SFT into a structure where a pointer is pointing to.
1603 This is needed by the operand scanner to find adjacent
1604 fields. See add_vars_for_offset for details. */
1605 if (TREE_CODE (mp_p->var) == STRUCT_FIELD_TAG
1606 && SFT_UNPARTITIONABLE_P (mp_p->var))
1607 continue;
1609 mpt = find_partition_for (mp_p);
1610 estimate_vop_reduction (mem_ref_stats, mp_p, mpt);
1613 /* After partitions have been created, rewrite alias sets to use
1614 them instead of the original symbols. This way, if the alias set
1615 was computed as { a b c d e f }, and the subset { b e f } was
1616 grouped into partition MPT.3, then the new alias set for the tag
1617 will be { a c d MPT.3 }.
1619 Note that this is not strictly necessary. The operand scanner
1620 will always check if a symbol belongs to a partition when adding
1621 virtual operands. However, by reducing the size of the alias
1622 sets to be scanned, the work needed inside the operand scanner is
1623 significantly reduced. */
1624 new_aliases = BITMAP_ALLOC (&alias_bitmap_obstack);
1626 for (i = 0; VEC_iterate (tree, tags, i, tag); i++)
1628 rewrite_alias_set_for (tag, new_aliases);
1629 bitmap_clear (new_aliases);
1632 BITMAP_FREE (new_aliases);
1634 if (dump_file)
1636 fprintf (dump_file, "\nMemory symbol references after partitioning:\n");
1637 dump_mp_info (dump_file, mp_info);
1640 done:
1641 /* Free allocated memory. */
1642 VEC_free (mem_sym_stats_t, heap, mp_info);
1643 VEC_free (tree, heap, tags);
1645 MAX_ALIASED_VOPS = prev_max_aliased_vops;
1647 timevar_pop (TV_MEMORY_PARTITIONING);
1651 /* Compute may-alias information for every variable referenced in function
1652 FNDECL.
1654 Alias analysis proceeds in 3 main phases:
1656 1- Points-to and escape analysis.
1658 This phase walks the use-def chains in the SSA web looking for three
1659 things:
1661 * Assignments of the form P_i = &VAR
1662 * Assignments of the form P_i = malloc()
1663 * Pointers and ADDR_EXPR that escape the current function.
1665 The concept of 'escaping' is the same one used in the Java world. When
1666 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
1667 outside of the current function. So, assignment to global variables,
1668 function arguments and returning a pointer are all escape sites, as are
1669 conversions between pointers and integers.
1671 This is where we are currently limited. Since not everything is renamed
1672 into SSA, we lose track of escape properties when a pointer is stashed
1673 inside a field in a structure, for instance. In those cases, we are
1674 assuming that the pointer does escape.
1676 We use escape analysis to determine whether a variable is
1677 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
1678 is call-clobbered. If a pointer P_i escapes, then all the variables
1679 pointed-to by P_i (and its memory tag) also escape.
1681 2- Compute flow-sensitive aliases
1683 We have two classes of memory tags. Memory tags associated with the
1684 pointed-to data type of the pointers in the program. These tags are
1685 called "symbol memory tag" (SMT). The other class are those associated
1686 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
1687 when adding operands for an INDIRECT_REF *P_i, we will first check
1688 whether P_i has a name tag, if it does we use it, because that will have
1689 more precise aliasing information. Otherwise, we use the standard symbol
1690 tag.
1692 In this phase, we go through all the pointers we found in points-to
1693 analysis and create alias sets for the name memory tags associated with
1694 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
1695 it points to and its tag.
1698 3- Compute flow-insensitive aliases
1700 This pass will compare the alias set of every symbol memory tag and
1701 every addressable variable found in the program. Given a symbol
1702 memory tag SMT and an addressable variable V. If the alias sets of
1703 SMT and V conflict (as computed by may_alias_p), then V is marked
1704 as an alias tag and added to the alias set of SMT.
1706 For instance, consider the following function:
1708 foo (int i)
1710 int *p, a, b;
1712 if (i > 10)
1713 p = &a;
1714 else
1715 p = &b;
1717 *p = 3;
1718 a = b + 2;
1719 return *p;
1722 After aliasing analysis has finished, the symbol memory tag for pointer
1723 'p' will have two aliases, namely variables 'a' and 'b'. Every time
1724 pointer 'p' is dereferenced, we want to mark the operation as a
1725 potential reference to 'a' and 'b'.
1727 foo (int i)
1729 int *p, a, b;
1731 if (i_2 > 10)
1732 p_4 = &a;
1733 else
1734 p_6 = &b;
1735 # p_1 = PHI <p_4(1), p_6(2)>;
1737 # a_7 = VDEF <a_3>;
1738 # b_8 = VDEF <b_5>;
1739 *p_1 = 3;
1741 # a_9 = VDEF <a_7>
1742 # VUSE <b_8>
1743 a_9 = b_8 + 2;
1745 # VUSE <a_9>;
1746 # VUSE <b_8>;
1747 return *p_1;
1750 In certain cases, the list of may aliases for a pointer may grow too
1751 large. This may cause an explosion in the number of virtual operands
1752 inserted in the code. Resulting in increased memory consumption and
1753 compilation time.
1755 When the number of virtual operands needed to represent aliased
1756 loads and stores grows too large (configurable with option --param
1757 max-aliased-vops and --param avg-aliased-vops), alias sets are
1758 grouped to avoid severe compile-time slow downs and memory
1759 consumption. See compute_memory_partitions. */
1761 unsigned int
1762 compute_may_aliases (void)
1764 struct alias_info *ai;
1766 timevar_push (TV_TREE_MAY_ALIAS);
1768 memset (&alias_stats, 0, sizeof (alias_stats));
1770 /* Initialize aliasing information. */
1771 ai = init_alias_info ();
1773 /* For each pointer P_i, determine the sets of variables that P_i may
1774 point-to. For every addressable variable V, determine whether the
1775 address of V escapes the current function, making V call-clobbered
1776 (i.e., whether &V is stored in a global variable or if its passed as a
1777 function call argument). */
1778 compute_points_to_sets (ai);
1780 /* Collect all pointers and addressable variables, compute alias sets,
1781 create memory tags for pointers and promote variables whose address is
1782 not needed anymore. */
1783 setup_pointers_and_addressables (ai);
1785 /* Compute type-based flow-insensitive aliasing for all the type
1786 memory tags. */
1787 compute_flow_insensitive_aliasing (ai);
1789 /* Compute flow-sensitive, points-to based aliasing for all the name
1790 memory tags. */
1791 compute_flow_sensitive_aliasing (ai);
1793 /* Compute call clobbering information. */
1794 compute_call_clobbered (ai);
1796 /* If the program makes no reference to global variables, but it
1797 contains a mixture of pure and non-pure functions, then we need
1798 to create use-def and def-def links between these functions to
1799 avoid invalid transformations on them. */
1800 maybe_create_global_var ();
1802 /* Compute memory partitions for every memory variable. */
1803 compute_memory_partitions ();
1805 /* Remove partitions with no symbols. Partitions may end up with an
1806 empty MPT_SYMBOLS set if a previous round of alias analysis
1807 needed to partition more symbols. Since we don't need those
1808 partitions anymore, remove them to free up the space. */
1810 tree mpt;
1811 unsigned i;
1812 VEC(tree,heap) *mpt_table;
1814 mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1815 i = 0;
1816 while (i < VEC_length (tree, mpt_table))
1818 mpt = VEC_index (tree, mpt_table, i);
1819 if (MPT_SYMBOLS (mpt) == NULL)
1820 VEC_unordered_remove (tree, mpt_table, i);
1821 else
1822 i++;
1826 /* Populate all virtual operands and newly promoted register operands. */
1828 block_stmt_iterator bsi;
1829 basic_block bb;
1830 FOR_EACH_BB (bb)
1831 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1832 update_stmt_if_modified (bsi_stmt (bsi));
1835 /* Debugging dumps. */
1836 if (dump_file)
1838 dump_mem_ref_stats (dump_file);
1839 dump_alias_info (dump_file);
1840 dump_points_to_info (dump_file);
1842 if (dump_flags & TDF_STATS)
1843 dump_alias_stats (dump_file);
1845 if (dump_flags & TDF_DETAILS)
1846 dump_referenced_vars (dump_file);
1849 /* Report strict aliasing violations. */
1850 strict_aliasing_warning_backend ();
1852 /* Deallocate memory used by aliasing data structures. */
1853 delete_alias_info (ai);
1855 if (need_ssa_update_p ())
1856 update_ssa (TODO_update_ssa);
1858 timevar_pop (TV_TREE_MAY_ALIAS);
1860 return 0;
1863 /* Data structure used to count the number of dereferences to PTR
1864 inside an expression. */
1865 struct count_ptr_d
1867 tree ptr;
1868 unsigned count;
1872 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
1873 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
1875 static tree
1876 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
1878 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
1880 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
1881 pointer 'ptr' is *not* dereferenced, it is simply used to compute
1882 the address of 'fld' as 'ptr + offsetof(fld)'. */
1883 if (TREE_CODE (*tp) == ADDR_EXPR)
1885 *walk_subtrees = 0;
1886 return NULL_TREE;
1889 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
1890 count_p->count++;
1892 return NULL_TREE;
1896 /* Count the number of direct and indirect uses for pointer PTR in
1897 statement STMT. The number of direct uses is stored in
1898 *NUM_USES_P. Indirect references are counted separately depending
1899 on whether they are store or load operations. The counts are
1900 stored in *NUM_STORES_P and *NUM_LOADS_P. */
1902 void
1903 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
1904 unsigned *num_loads_p, unsigned *num_stores_p)
1906 ssa_op_iter i;
1907 tree use;
1909 *num_uses_p = 0;
1910 *num_loads_p = 0;
1911 *num_stores_p = 0;
1913 /* Find out the total number of uses of PTR in STMT. */
1914 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
1915 if (use == ptr)
1916 (*num_uses_p)++;
1918 /* Now count the number of indirect references to PTR. This is
1919 truly awful, but we don't have much choice. There are no parent
1920 pointers inside INDIRECT_REFs, so an expression like
1921 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
1922 find all the indirect and direct uses of x_1 inside. The only
1923 shortcut we can take is the fact that GIMPLE only allows
1924 INDIRECT_REFs inside the expressions below. */
1925 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1926 || (TREE_CODE (stmt) == RETURN_EXPR
1927 && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
1928 || TREE_CODE (stmt) == ASM_EXPR
1929 || TREE_CODE (stmt) == CALL_EXPR)
1931 tree lhs, rhs;
1933 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
1935 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1936 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1938 else if (TREE_CODE (stmt) == RETURN_EXPR)
1940 tree e = TREE_OPERAND (stmt, 0);
1941 lhs = GIMPLE_STMT_OPERAND (e, 0);
1942 rhs = GIMPLE_STMT_OPERAND (e, 1);
1944 else if (TREE_CODE (stmt) == ASM_EXPR)
1946 lhs = ASM_OUTPUTS (stmt);
1947 rhs = ASM_INPUTS (stmt);
1949 else
1951 lhs = NULL_TREE;
1952 rhs = stmt;
1955 if (lhs
1956 && (TREE_CODE (lhs) == TREE_LIST
1957 || EXPR_P (lhs)
1958 || GIMPLE_STMT_P (lhs)))
1960 struct count_ptr_d count;
1961 count.ptr = ptr;
1962 count.count = 0;
1963 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
1964 *num_stores_p = count.count;
1967 if (rhs
1968 && (TREE_CODE (rhs) == TREE_LIST
1969 || EXPR_P (rhs)
1970 || GIMPLE_STMT_P (rhs)))
1972 struct count_ptr_d count;
1973 count.ptr = ptr;
1974 count.count = 0;
1975 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
1976 *num_loads_p = count.count;
1980 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
1983 /* Remove memory references stats for function FN. */
1985 void
1986 delete_mem_ref_stats (struct function *fn)
1988 if (gimple_mem_ref_stats (fn)->mem_sym_stats)
1990 free_alloc_pool (mem_sym_stats_pool);
1991 pointer_map_destroy (gimple_mem_ref_stats (fn)->mem_sym_stats);
1993 gimple_mem_ref_stats (fn)->mem_sym_stats = NULL;
1997 /* Initialize memory reference stats. */
1999 static void
2000 init_mem_ref_stats (void)
2002 struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
2004 mem_sym_stats_pool = create_alloc_pool ("Mem sym stats",
2005 sizeof (struct mem_sym_stats_d),
2006 100);
2007 memset (mem_ref_stats, 0, sizeof (struct mem_ref_stats_d));
2008 mem_ref_stats->mem_sym_stats = pointer_map_create ();
2012 /* Helper for init_alias_info. Reset existing aliasing information. */
2014 static void
2015 reset_alias_info (void)
2017 referenced_var_iterator rvi;
2018 tree var;
2019 unsigned i;
2020 bitmap active_nmts, all_nmts;
2022 /* Clear the set of addressable variables. We do not need to clear
2023 the TREE_ADDRESSABLE bit on every symbol because we are going to
2024 re-compute addressability here. */
2025 bitmap_clear (gimple_addressable_vars (cfun));
2027 active_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
2028 all_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
2030 /* Clear flow-insensitive alias information from each symbol. */
2031 FOR_EACH_REFERENCED_VAR (var, rvi)
2033 if (is_gimple_reg (var))
2034 continue;
2036 if (MTAG_P (var))
2037 MTAG_ALIASES (var) = NULL;
2039 /* Memory partition information will be computed from scratch. */
2040 if (TREE_CODE (var) == MEMORY_PARTITION_TAG)
2041 MPT_SYMBOLS (var) = NULL;
2043 /* Collect all the name tags to determine if we have any
2044 orphaned that need to be removed from the IL. A name tag
2045 will be orphaned if it is not associated with any active SSA
2046 name. */
2047 if (TREE_CODE (var) == NAME_MEMORY_TAG)
2048 bitmap_set_bit (all_nmts, DECL_UID (var));
2050 /* Since we are about to re-discover call-clobbered
2051 variables, clear the call-clobbered flag. Variables that
2052 are intrinsically call-clobbered (globals, local statics,
2053 etc) will not be marked by the aliasing code, so we can't
2054 remove them from CALL_CLOBBERED_VARS.
2056 NB: STRUCT_FIELDS are still call clobbered if they are for a
2057 global variable, so we *don't* clear their call clobberedness
2058 just because they are tags, though we will clear it if they
2059 aren't for global variables. */
2060 if (TREE_CODE (var) == NAME_MEMORY_TAG
2061 || TREE_CODE (var) == SYMBOL_MEMORY_TAG
2062 || TREE_CODE (var) == MEMORY_PARTITION_TAG
2063 || !is_global_var (var))
2064 clear_call_clobbered (var);
2067 /* Clear flow-sensitive points-to information from each SSA name. */
2068 for (i = 1; i < num_ssa_names; i++)
2070 tree name = ssa_name (i);
2072 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
2073 continue;
2075 if (SSA_NAME_PTR_INFO (name))
2077 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
2079 /* Clear all the flags but keep the name tag to
2080 avoid creating new temporaries unnecessarily. If
2081 this pointer is found to point to a subset or
2082 superset of its former points-to set, then a new
2083 tag will need to be created in create_name_tags. */
2084 pi->pt_anything = 0;
2085 pi->pt_null = 0;
2086 pi->value_escapes_p = 0;
2087 pi->is_dereferenced = 0;
2088 if (pi->pt_vars)
2089 bitmap_clear (pi->pt_vars);
2091 /* Add NAME's name tag to the set of active tags. */
2092 if (pi->name_mem_tag)
2093 bitmap_set_bit (active_nmts, DECL_UID (pi->name_mem_tag));
2097 /* Name memory tags that are no longer associated with an SSA name
2098 are considered stale and should be removed from the IL. All the
2099 name tags that are in the set ALL_NMTS but not in ACTIVE_NMTS are
2100 considered stale and marked for renaming. */
2101 bitmap_and_compl_into (all_nmts, active_nmts);
2102 mark_set_for_renaming (all_nmts);
2104 BITMAP_FREE (all_nmts);
2105 BITMAP_FREE (active_nmts);
2109 /* Initialize the data structures used for alias analysis. */
2111 static struct alias_info *
2112 init_alias_info (void)
2114 struct alias_info *ai;
2115 referenced_var_iterator rvi;
2116 tree var;
2118 ai = XCNEW (struct alias_info);
2119 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
2120 sbitmap_zero (ai->ssa_names_visited);
2121 ai->processed_ptrs = VEC_alloc (tree, heap, 50);
2122 ai->written_vars = pointer_set_create ();
2123 ai->dereferenced_ptrs_store = pointer_set_create ();
2124 ai->dereferenced_ptrs_load = pointer_set_create ();
2126 /* Clear out all memory reference stats. */
2127 init_mem_ref_stats ();
2129 /* If aliases have been computed before, clear existing information. */
2130 if (gimple_aliases_computed_p (cfun))
2131 reset_alias_info ();
2132 else
2134 /* If this is the first time we compute aliasing information,
2135 every non-register symbol will need to be put into SSA form
2136 (the initial SSA form only operates on GIMPLE registers). */
2137 FOR_EACH_REFERENCED_VAR (var, rvi)
2138 if (!is_gimple_reg (var))
2139 mark_sym_for_renaming (var);
2142 /* Next time, we will need to reset alias information. */
2143 cfun->gimple_df->aliases_computed_p = true;
2144 if (alias_bitmap_obstack.elements != NULL)
2145 bitmap_obstack_release (&alias_bitmap_obstack);
2146 bitmap_obstack_initialize (&alias_bitmap_obstack);
2148 return ai;
2152 /* Deallocate memory used by alias analysis. */
2154 static void
2155 delete_alias_info (struct alias_info *ai)
2157 size_t i;
2159 sbitmap_free (ai->ssa_names_visited);
2161 VEC_free (tree, heap, ai->processed_ptrs);
2163 for (i = 0; i < ai->num_addressable_vars; i++)
2164 free (ai->addressable_vars[i]);
2165 free (ai->addressable_vars);
2167 for (i = 0; i < ai->num_pointers; i++)
2168 free (ai->pointers[i]);
2169 free (ai->pointers);
2171 pointer_set_destroy (ai->written_vars);
2172 pointer_set_destroy (ai->dereferenced_ptrs_store);
2173 pointer_set_destroy (ai->dereferenced_ptrs_load);
2174 free (ai);
2176 delete_mem_ref_stats (cfun);
2177 delete_points_to_sets ();
2181 /* Used for hashing to identify pointer infos with identical
2182 pt_vars bitmaps. */
2184 static int
2185 eq_ptr_info (const void *p1, const void *p2)
2187 const struct ptr_info_def *n1 = (const struct ptr_info_def *) p1;
2188 const struct ptr_info_def *n2 = (const struct ptr_info_def *) p2;
2189 return bitmap_equal_p (n1->pt_vars, n2->pt_vars);
2192 static hashval_t
2193 ptr_info_hash (const void *p)
2195 const struct ptr_info_def *n = (const struct ptr_info_def *) p;
2196 return bitmap_hash (n->pt_vars);
2200 /* Create name tags for all the pointers that have been dereferenced.
2201 We only create a name tag for a pointer P if P is found to point to
2202 a set of variables (so that we can alias them to *P) or if it is
2203 the result of a call to malloc (which means that P cannot point to
2204 anything else nor alias any other variable).
2206 If two pointers P and Q point to the same set of variables, they
2207 are assigned the same name tag. */
2209 static void
2210 create_name_tags (void)
2212 size_t i;
2213 VEC (tree, heap) *with_ptvars = NULL;
2214 tree ptr;
2215 htab_t ptr_hash;
2217 /* Collect the list of pointers with a non-empty points to set. */
2218 for (i = 1; i < num_ssa_names; i++)
2220 tree ptr = ssa_name (i);
2221 struct ptr_info_def *pi;
2223 if (!ptr
2224 || !POINTER_TYPE_P (TREE_TYPE (ptr))
2225 || !SSA_NAME_PTR_INFO (ptr))
2226 continue;
2228 pi = SSA_NAME_PTR_INFO (ptr);
2230 if (pi->pt_anything || !pi->is_dereferenced)
2232 /* No name tags for pointers that have not been
2233 dereferenced or point to an arbitrary location. */
2234 pi->name_mem_tag = NULL_TREE;
2235 continue;
2238 /* Set pt_anything on the pointers without pt_vars filled in so
2239 that they are assigned a symbol tag. */
2240 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
2241 VEC_safe_push (tree, heap, with_ptvars, ptr);
2242 else
2243 set_pt_anything (ptr);
2246 /* If we didn't find any pointers with pt_vars set, we're done. */
2247 if (!with_ptvars)
2248 return;
2250 ptr_hash = htab_create (10, ptr_info_hash, eq_ptr_info, NULL);
2252 /* Now go through the pointers with pt_vars, and find a name tag
2253 with the same pt_vars as this pointer, or create one if one
2254 doesn't exist. */
2255 for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
2257 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2258 tree old_name_tag = pi->name_mem_tag;
2259 struct ptr_info_def **slot;
2261 /* If PTR points to a set of variables, check if we don't
2262 have another pointer Q with the same points-to set before
2263 creating a tag. If so, use Q's tag instead of creating a
2264 new one.
2266 This is important for not creating unnecessary symbols
2267 and also for copy propagation. If we ever need to
2268 propagate PTR into Q or vice-versa, we would run into
2269 problems if they both had different name tags because
2270 they would have different SSA version numbers (which
2271 would force us to take the name tags in and out of SSA). */
2272 slot = (struct ptr_info_def **) htab_find_slot (ptr_hash, pi, INSERT);
2273 if (*slot)
2274 pi->name_mem_tag = (*slot)->name_mem_tag;
2275 else
2277 *slot = pi;
2279 /* If we didn't find a pointer with the same points-to set
2280 as PTR, create a new name tag if needed. */
2281 if (pi->name_mem_tag == NULL_TREE)
2282 pi->name_mem_tag = get_nmt_for (ptr);
2285 /* If the new name tag computed for PTR is different than
2286 the old name tag that it used to have, then the old tag
2287 needs to be removed from the IL, so we mark it for
2288 renaming. */
2289 if (old_name_tag && old_name_tag != pi->name_mem_tag)
2290 mark_sym_for_renaming (old_name_tag);
2292 /* Inherit volatility from the pointed-to type. */
2293 TREE_THIS_VOLATILE (pi->name_mem_tag)
2294 |= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
2296 /* Mark the new name tag for renaming. */
2297 mark_sym_for_renaming (pi->name_mem_tag);
2300 htab_delete (ptr_hash);
2302 VEC_free (tree, heap, with_ptvars);
2306 /* Union the alias set SET into the may-aliases for TAG. */
2308 static void
2309 union_alias_set_into (tree tag, bitmap set)
2311 bitmap ma = MTAG_ALIASES (tag);
2313 if (bitmap_empty_p (set))
2314 return;
2316 if (!ma)
2317 ma = MTAG_ALIASES (tag) = BITMAP_ALLOC (&alias_bitmap_obstack);
2318 bitmap_ior_into (ma, set);
2322 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
2323 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
2324 name tag and the variables it points-to are call-clobbered. Finally, if
2325 P_i escapes and we could not determine where it points to, then all the
2326 variables in the same alias set as *P_i are marked call-clobbered. This
2327 is necessary because we must assume that P_i may take the address of any
2328 variable in the same alias set. */
2330 static void
2331 compute_flow_sensitive_aliasing (struct alias_info *ai)
2333 size_t i;
2334 tree ptr;
2336 timevar_push (TV_FLOW_SENSITIVE);
2337 set_used_smts ();
2339 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2341 if (!find_what_p_points_to (ptr))
2342 set_pt_anything (ptr);
2345 create_name_tags ();
2347 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2349 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2351 /* Set up aliasing information for PTR's name memory tag (if it has
2352 one). Note that only pointers that have been dereferenced will
2353 have a name memory tag. */
2354 if (pi->name_mem_tag && pi->pt_vars)
2356 if (!bitmap_empty_p (pi->pt_vars))
2357 union_alias_set_into (pi->name_mem_tag, pi->pt_vars);
2360 timevar_pop (TV_FLOW_SENSITIVE);
2364 /* Return TRUE if at least one symbol in TAG2's alias set is also
2365 present in TAG1's alias set. */
2367 static bool
2368 have_common_aliases_p (bitmap tag1aliases, bitmap tag2aliases)
2371 /* This is the old behavior of have_common_aliases_p, which is to
2372 return false if both sets are empty, or one set is and the other
2373 isn't. */
2374 if (tag1aliases == NULL || tag2aliases == NULL)
2375 return false;
2377 return bitmap_intersect_p (tag1aliases, tag2aliases);
2380 /* Compute type-based alias sets. Traverse all the pointers and
2381 addressable variables found in setup_pointers_and_addressables.
2383 For every pointer P in AI->POINTERS and addressable variable V in
2384 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's symbol
2385 memory tag (SMT) if their alias sets conflict. V is then marked as
2386 an aliased symbol so that the operand scanner knows that statements
2387 containing V have aliased operands. */
2389 static void
2390 compute_flow_insensitive_aliasing (struct alias_info *ai)
2392 size_t i;
2394 timevar_push (TV_FLOW_INSENSITIVE);
2395 /* For every pointer P, determine which addressable variables may alias
2396 with P's symbol memory tag. */
2397 for (i = 0; i < ai->num_pointers; i++)
2399 size_t j;
2400 struct alias_map_d *p_map = ai->pointers[i];
2401 tree tag = symbol_mem_tag (p_map->var);
2402 tree var;
2404 for (j = 0; j < ai->num_addressable_vars; j++)
2406 struct alias_map_d *v_map;
2407 var_ann_t v_ann;
2408 bool tag_stored_p, var_stored_p;
2410 v_map = ai->addressable_vars[j];
2411 var = v_map->var;
2412 v_ann = var_ann (var);
2414 /* Skip memory tags and variables that have never been
2415 written to. We also need to check if the variables are
2416 call-clobbered because they may be overwritten by
2417 function calls. */
2418 tag_stored_p = pointer_set_contains (ai->written_vars, tag)
2419 || is_call_clobbered (tag);
2420 var_stored_p = pointer_set_contains (ai->written_vars, var)
2421 || is_call_clobbered (var);
2422 if (!tag_stored_p && !var_stored_p)
2423 continue;
2425 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
2427 /* We should never have a var with subvars here, because
2428 they shouldn't get into the set of addressable vars */
2429 gcc_assert (!var_can_have_subvars (var)
2430 || get_subvars_for_var (var) == NULL);
2432 /* Add VAR to TAG's may-aliases set. */
2433 add_may_alias (tag, var);
2438 /* Since this analysis is based exclusively on symbols, it fails to
2439 handle cases where two pointers P and Q have different memory
2440 tags with conflicting alias set numbers but no aliased symbols in
2441 common.
2443 For example, suppose that we have two memory tags SMT.1 and SMT.2
2444 such that
2446 may-aliases (SMT.1) = { a }
2447 may-aliases (SMT.2) = { b }
2449 and the alias set number of SMT.1 conflicts with that of SMT.2.
2450 Since they don't have symbols in common, loads and stores from
2451 SMT.1 and SMT.2 will seem independent of each other, which will
2452 lead to the optimizers making invalid transformations (see
2453 testsuite/gcc.c-torture/execute/pr15262-[12].c).
2455 To avoid this problem, we do a final traversal of AI->POINTERS
2456 looking for pairs of pointers that have no aliased symbols in
2457 common and yet have conflicting alias set numbers. */
2458 for (i = 0; i < ai->num_pointers; i++)
2460 size_t j;
2461 struct alias_map_d *p_map1 = ai->pointers[i];
2462 tree tag1 = symbol_mem_tag (p_map1->var);
2463 bitmap may_aliases1 = MTAG_ALIASES (tag1);
2465 for (j = 0; j < ai->num_pointers; j++)
2467 struct alias_map_d *p_map2 = ai->pointers[j];
2468 tree tag2 = symbol_mem_tag (p_map2->var);
2469 bitmap may_aliases2 = may_aliases (tag2);
2471 /* By convention tags don't alias themselves. */
2472 if (tag1 == tag2)
2473 continue;
2475 /* If the pointers may not point to each other, do nothing. */
2476 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
2477 continue;
2479 /* The two pointers may alias each other. If they already have
2480 symbols in common, do nothing. */
2481 if (have_common_aliases_p (may_aliases1, may_aliases2))
2482 continue;
2484 add_may_alias (tag1, tag2);
2487 timevar_pop (TV_FLOW_INSENSITIVE);
2491 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
2493 static void
2494 create_alias_map_for (tree var, struct alias_info *ai)
2496 struct alias_map_d *alias_map;
2497 alias_map = XCNEW (struct alias_map_d);
2498 alias_map->var = var;
2499 alias_map->set = get_alias_set (var);
2500 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
2504 /* Create memory tags for all the dereferenced pointers and build the
2505 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
2506 sets. Based on the address escape and points-to information collected
2507 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
2508 variables whose address is not needed anymore. */
2510 static void
2511 setup_pointers_and_addressables (struct alias_info *ai)
2513 size_t num_addressable_vars, num_pointers;
2514 referenced_var_iterator rvi;
2515 tree var;
2516 VEC (tree, heap) *varvec = NULL;
2517 safe_referenced_var_iterator srvi;
2519 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
2520 num_addressable_vars = num_pointers = 0;
2522 FOR_EACH_REFERENCED_VAR (var, rvi)
2524 if (may_be_aliased (var))
2525 num_addressable_vars++;
2527 if (POINTER_TYPE_P (TREE_TYPE (var)))
2529 /* Since we don't keep track of volatile variables, assume that
2530 these pointers are used in indirect store operations. */
2531 if (TREE_THIS_VOLATILE (var))
2532 pointer_set_insert (ai->dereferenced_ptrs_store, var);
2534 num_pointers++;
2538 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
2539 always going to be slightly bigger than we actually need them
2540 because some TREE_ADDRESSABLE variables will be marked
2541 non-addressable below and only pointers with unique symbol tags are
2542 going to be added to POINTERS. */
2543 ai->addressable_vars = XCNEWVEC (struct alias_map_d *, num_addressable_vars);
2544 ai->pointers = XCNEWVEC (struct alias_map_d *, num_pointers);
2545 ai->num_addressable_vars = 0;
2546 ai->num_pointers = 0;
2548 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
2550 subvar_t svars;
2552 /* Name memory tags already have flow-sensitive aliasing
2553 information, so they need not be processed by
2554 compute_flow_insensitive_aliasing. Similarly, symbol memory
2555 tags are already accounted for when we process their
2556 associated pointer.
2558 Structure fields, on the other hand, have to have some of this
2559 information processed for them, but it's pointless to mark them
2560 non-addressable (since they are fake variables anyway). */
2561 if (MTAG_P (var) && TREE_CODE (var) != STRUCT_FIELD_TAG)
2562 continue;
2564 /* Remove the ADDRESSABLE flag from every addressable variable whose
2565 address is not needed anymore. This is caused by the propagation
2566 of ADDR_EXPR constants into INDIRECT_REF expressions and the
2567 removal of dead pointer assignments done by the early scalar
2568 cleanup passes. */
2569 if (TREE_ADDRESSABLE (var))
2571 if (!bitmap_bit_p (gimple_addressable_vars (cfun), DECL_UID (var))
2572 && TREE_CODE (var) != RESULT_DECL
2573 && !is_global_var (var))
2575 bool okay_to_mark = true;
2577 /* Since VAR is now a regular GIMPLE register, we will need
2578 to rename VAR into SSA afterwards. */
2579 mark_sym_for_renaming (var);
2581 /* If VAR can have sub-variables, and any of its
2582 sub-variables has its address taken, then we cannot
2583 remove the addressable flag from VAR. */
2584 if (var_can_have_subvars (var)
2585 && (svars = get_subvars_for_var (var)))
2587 unsigned int i;
2588 tree subvar;
2590 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
2592 if (bitmap_bit_p (gimple_addressable_vars (cfun),
2593 DECL_UID (subvar)))
2594 okay_to_mark = false;
2595 mark_sym_for_renaming (subvar);
2599 /* The address of VAR is not needed, remove the
2600 addressable bit, so that it can be optimized as a
2601 regular variable. */
2602 if (okay_to_mark)
2604 /* The memory partition holding VAR will no longer
2605 contain VAR, and statements referencing it will need
2606 to be updated. */
2607 if (memory_partition (var))
2608 mark_sym_for_renaming (memory_partition (var));
2610 mark_non_addressable (var);
2615 /* Global variables and addressable locals may be aliased. Create an
2616 entry in ADDRESSABLE_VARS for VAR. */
2617 if (may_be_aliased (var))
2619 if (!var_can_have_subvars (var)
2620 || get_subvars_for_var (var) == NULL)
2621 create_alias_map_for (var, ai);
2623 mark_sym_for_renaming (var);
2626 /* Add pointer variables that have been dereferenced to the POINTERS
2627 array and create a symbol memory tag for them. */
2628 if (POINTER_TYPE_P (TREE_TYPE (var)))
2630 if ((pointer_set_contains (ai->dereferenced_ptrs_store, var)
2631 || pointer_set_contains (ai->dereferenced_ptrs_load, var)))
2633 tree tag, old_tag;
2634 var_ann_t t_ann;
2636 /* If pointer VAR still doesn't have a memory tag
2637 associated with it, create it now or re-use an
2638 existing one. */
2639 tag = get_smt_for (var, ai);
2640 t_ann = var_ann (tag);
2642 /* The symbol tag will need to be renamed into SSA
2643 afterwards. Note that we cannot do this inside
2644 get_smt_for because aliasing may run multiple times
2645 and we only create symbol tags the first time. */
2646 mark_sym_for_renaming (tag);
2648 /* Similarly, if pointer VAR used to have another type
2649 tag, we will need to process it in the renamer to
2650 remove the stale virtual operands. */
2651 old_tag = symbol_mem_tag (var);
2652 if (old_tag)
2653 mark_sym_for_renaming (old_tag);
2655 /* Associate the tag with pointer VAR. */
2656 set_symbol_mem_tag (var, tag);
2658 /* If pointer VAR has been used in a store operation,
2659 then its memory tag must be marked as written-to. */
2660 if (pointer_set_contains (ai->dereferenced_ptrs_store, var))
2661 pointer_set_insert (ai->written_vars, tag);
2663 else
2665 /* The pointer has not been dereferenced. If it had a
2666 symbol memory tag, remove it and mark the old tag for
2667 renaming to remove it out of the IL. */
2668 tree tag = symbol_mem_tag (var);
2669 if (tag)
2671 mark_sym_for_renaming (tag);
2672 set_symbol_mem_tag (var, NULL_TREE);
2678 VEC_free (tree, heap, varvec);
2682 /* Determine whether to use .GLOBAL_VAR to model call clobbering
2683 semantics. If the function makes no references to global
2684 variables and contains at least one call to a non-pure function,
2685 then we need to mark the side-effects of the call using .GLOBAL_VAR
2686 to represent all possible global memory referenced by the callee. */
2688 static void
2689 maybe_create_global_var (void)
2691 /* No need to create it, if we have one already. */
2692 if (gimple_global_var (cfun) == NULL_TREE)
2694 struct mem_ref_stats_d *stats = gimple_mem_ref_stats (cfun);
2696 /* Create .GLOBAL_VAR if there are no call-clobbered
2697 variables and the program contains a mixture of pure/const
2698 and regular function calls. This is to avoid the problem
2699 described in PR 20115:
2701 int X;
2702 int func_pure (void) { return X; }
2703 int func_non_pure (int a) { X += a; }
2704 int foo ()
2706 int a = func_pure ();
2707 func_non_pure (a);
2708 a = func_pure ();
2709 return a;
2712 Since foo() has no call-clobbered variables, there is
2713 no relationship between the calls to func_pure and
2714 func_non_pure. Since func_pure has no side-effects, value
2715 numbering optimizations elide the second call to func_pure.
2716 So, if we have some pure/const and some regular calls in the
2717 program we create .GLOBAL_VAR to avoid missing these
2718 relations. */
2719 if (bitmap_empty_p (gimple_call_clobbered_vars (cfun))
2720 && stats->num_call_sites > 0
2721 && stats->num_pure_const_call_sites > 0
2722 && stats->num_call_sites > stats->num_pure_const_call_sites)
2723 create_global_var ();
2728 /* Return TRUE if pointer PTR may point to variable VAR.
2730 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
2731 This is needed because when checking for type conflicts we are
2732 interested in the alias set of the memory location pointed-to by
2733 PTR. The alias set of PTR itself is irrelevant.
2735 VAR_ALIAS_SET is the alias set for VAR. */
2737 static bool
2738 may_alias_p (tree ptr, alias_set_type mem_alias_set,
2739 tree var, alias_set_type var_alias_set,
2740 bool alias_set_only)
2742 tree mem;
2744 alias_stats.alias_queries++;
2745 alias_stats.simple_queries++;
2747 /* By convention, a variable cannot alias itself. */
2748 mem = symbol_mem_tag (ptr);
2749 if (mem == var)
2751 alias_stats.alias_noalias++;
2752 alias_stats.simple_resolved++;
2753 return false;
2756 /* If -fargument-noalias-global is > 2, pointer arguments may
2757 not point to anything else. */
2758 if (flag_argument_noalias > 2 && TREE_CODE (ptr) == PARM_DECL)
2760 alias_stats.alias_noalias++;
2761 alias_stats.simple_resolved++;
2762 return false;
2765 /* If -fargument-noalias-global is > 1, pointer arguments may
2766 not point to global variables. */
2767 if (flag_argument_noalias > 1 && is_global_var (var)
2768 && TREE_CODE (ptr) == PARM_DECL)
2770 alias_stats.alias_noalias++;
2771 alias_stats.simple_resolved++;
2772 return false;
2775 /* If either MEM or VAR is a read-only global and the other one
2776 isn't, then PTR cannot point to VAR. */
2777 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
2778 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
2780 alias_stats.alias_noalias++;
2781 alias_stats.simple_resolved++;
2782 return false;
2785 /* If the pointed to memory has alias set zero, or the pointer
2786 is ref-all, or the pointer decl is marked that no TBAA is to
2787 be applied, the MEM can alias VAR. */
2788 if (mem_alias_set == 0
2789 || DECL_POINTER_ALIAS_SET (ptr) == 0
2790 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (ptr))
2791 || DECL_NO_TBAA_P (ptr))
2793 alias_stats.alias_mayalias++;
2794 alias_stats.simple_resolved++;
2795 return true;
2798 gcc_assert (TREE_CODE (mem) == SYMBOL_MEMORY_TAG);
2800 alias_stats.tbaa_queries++;
2802 /* If the alias sets don't conflict then MEM cannot alias VAR. */
2803 if (mem_alias_set != var_alias_set
2804 && !alias_set_subset_of (mem_alias_set, var_alias_set))
2806 alias_stats.alias_noalias++;
2807 alias_stats.tbaa_resolved++;
2808 return false;
2811 /* If VAR is a record or union type, PTR cannot point into VAR
2812 unless there is some explicit address operation in the
2813 program that can reference a field of the type pointed-to by
2814 PTR. This also assumes that the types of both VAR and PTR
2815 are contained within the compilation unit, and that there is
2816 no fancy addressing arithmetic associated with any of the
2817 types involved. */
2818 if (mem_alias_set != 0 && var_alias_set != 0)
2820 tree ptr_type = TREE_TYPE (ptr);
2821 tree var_type = TREE_TYPE (var);
2823 /* The star count is -1 if the type at the end of the
2824 pointer_to chain is not a record or union type. */
2825 if (!alias_set_only
2826 && ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
2828 int ptr_star_count = 0;
2830 /* ipa_type_escape_star_count_of_interesting_type is a
2831 little too restrictive for the pointer type, need to
2832 allow pointers to primitive types as long as those
2833 types cannot be pointers to everything. */
2834 while (POINTER_TYPE_P (ptr_type))
2836 /* Strip the *s off. */
2837 ptr_type = TREE_TYPE (ptr_type);
2838 ptr_star_count++;
2841 /* There does not appear to be a better test to see if
2842 the pointer type was one of the pointer to everything
2843 types. */
2844 if (ptr_star_count > 0)
2846 alias_stats.structnoaddress_queries++;
2847 if (ipa_type_escape_field_does_not_clobber_p (var_type,
2848 TREE_TYPE (ptr)))
2850 alias_stats.structnoaddress_resolved++;
2851 alias_stats.alias_noalias++;
2852 return false;
2855 else if (ptr_star_count == 0)
2857 /* If PTR_TYPE was not really a pointer to type, it cannot
2858 alias. */
2859 alias_stats.structnoaddress_queries++;
2860 alias_stats.structnoaddress_resolved++;
2861 alias_stats.alias_noalias++;
2862 return false;
2867 alias_stats.alias_mayalias++;
2868 return true;
2872 /* Add ALIAS to the set of variables that may alias VAR. */
2874 static void
2875 add_may_alias (tree var, tree alias)
2877 /* Don't allow self-referential aliases. */
2878 gcc_assert (var != alias);
2880 /* ALIAS must be addressable if it's being added to an alias set. */
2881 #if 1
2882 TREE_ADDRESSABLE (alias) = 1;
2883 #else
2884 gcc_assert (may_be_aliased (alias));
2885 #endif
2887 /* VAR must be a symbol or a name tag. */
2888 gcc_assert (TREE_CODE (var) == SYMBOL_MEMORY_TAG
2889 || TREE_CODE (var) == NAME_MEMORY_TAG);
2891 if (MTAG_ALIASES (var) == NULL)
2892 MTAG_ALIASES (var) = BITMAP_ALLOC (&alias_bitmap_obstack);
2894 bitmap_set_bit (MTAG_ALIASES (var), DECL_UID (alias));
2898 /* Mark pointer PTR as pointing to an arbitrary memory location. */
2900 static void
2901 set_pt_anything (tree ptr)
2903 struct ptr_info_def *pi = get_ptr_info (ptr);
2905 pi->pt_anything = 1;
2906 pi->pt_vars = NULL;
2908 /* The pointer used to have a name tag, but we now found it pointing
2909 to an arbitrary location. The name tag needs to be renamed and
2910 disassociated from PTR. */
2911 if (pi->name_mem_tag)
2913 mark_sym_for_renaming (pi->name_mem_tag);
2914 pi->name_mem_tag = NULL_TREE;
2919 /* Return true if STMT is an "escape" site from the current function. Escape
2920 sites those statements which might expose the address of a variable
2921 outside the current function. STMT is an escape site iff:
2923 1- STMT is a function call, or
2924 2- STMT is an __asm__ expression, or
2925 3- STMT is an assignment to a non-local variable, or
2926 4- STMT is a return statement.
2928 Return the type of escape site found, if we found one, or NO_ESCAPE
2929 if none. */
2931 enum escape_type
2932 is_escape_site (tree stmt)
2934 tree call = get_call_expr_in (stmt);
2935 if (call != NULL_TREE)
2937 if (!TREE_SIDE_EFFECTS (call))
2938 return ESCAPE_TO_PURE_CONST;
2940 return ESCAPE_TO_CALL;
2942 else if (TREE_CODE (stmt) == ASM_EXPR)
2943 return ESCAPE_TO_ASM;
2944 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2946 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
2948 /* Get to the base of _REF nodes. */
2949 if (TREE_CODE (lhs) != SSA_NAME)
2950 lhs = get_base_address (lhs);
2952 /* If we couldn't recognize the LHS of the assignment, assume that it
2953 is a non-local store. */
2954 if (lhs == NULL_TREE)
2955 return ESCAPE_UNKNOWN;
2957 if (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == NOP_EXPR
2958 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == CONVERT_EXPR
2959 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2961 tree from
2962 = TREE_TYPE (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0));
2963 tree to = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
2965 /* If the RHS is a conversion between a pointer and an integer, the
2966 pointer escapes since we can't track the integer. */
2967 if (POINTER_TYPE_P (from) && !POINTER_TYPE_P (to))
2968 return ESCAPE_BAD_CAST;
2971 /* If the LHS is an SSA name, it can't possibly represent a non-local
2972 memory store. */
2973 if (TREE_CODE (lhs) == SSA_NAME)
2974 return NO_ESCAPE;
2976 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2977 local variables we cannot be sure if it will escape, because we
2978 don't have information about objects not in SSA form. Need to
2979 implement something along the lines of
2981 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2982 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2983 Conference on Object-Oriented Programming Systems, Languages, and
2984 Applications (OOPSLA), pp. 1-19, 1999. */
2985 return ESCAPE_STORED_IN_GLOBAL;
2987 else if (TREE_CODE (stmt) == RETURN_EXPR)
2988 return ESCAPE_TO_RETURN;
2990 return NO_ESCAPE;
2993 /* Create a new memory tag of type TYPE.
2994 Does NOT push it into the current binding. */
2996 tree
2997 create_tag_raw (enum tree_code code, tree type, const char *prefix)
2999 tree tmp_var;
3001 tmp_var = build_decl (code, create_tmp_var_name (prefix), type);
3003 /* Make the variable writable. */
3004 TREE_READONLY (tmp_var) = 0;
3006 /* It doesn't start out global. */
3007 MTAG_GLOBAL (tmp_var) = 0;
3008 TREE_STATIC (tmp_var) = 0;
3009 TREE_USED (tmp_var) = 1;
3011 return tmp_var;
3014 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
3015 is considered to represent all the pointers whose pointed-to types are
3016 in the same alias set class. Otherwise, the tag represents a single
3017 SSA_NAME pointer variable. */
3019 static tree
3020 create_memory_tag (tree type, bool is_type_tag)
3022 tree tag = create_tag_raw (is_type_tag ? SYMBOL_MEMORY_TAG : NAME_MEMORY_TAG,
3023 type, (is_type_tag) ? "SMT" : "NMT");
3025 /* By default, memory tags are local variables. Alias analysis will
3026 determine whether they should be considered globals. */
3027 DECL_CONTEXT (tag) = current_function_decl;
3029 /* Memory tags are by definition addressable. */
3030 TREE_ADDRESSABLE (tag) = 1;
3032 set_symbol_mem_tag (tag, NULL_TREE);
3034 /* Add the tag to the symbol table. */
3035 add_referenced_var (tag);
3037 return tag;
3041 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
3042 This is used if P_i has been found to point to a specific set of
3043 variables or to a non-aliased memory location like the address returned
3044 by malloc functions. */
3046 static tree
3047 get_nmt_for (tree ptr)
3049 struct ptr_info_def *pi = get_ptr_info (ptr);
3050 tree tag = pi->name_mem_tag;
3052 if (tag == NULL_TREE)
3053 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
3054 return tag;
3058 /* Return the symbol memory tag associated to pointer PTR. A memory
3059 tag is an artificial variable that represents the memory location
3060 pointed-to by PTR. It is used to model the effects of pointer
3061 de-references on addressable variables.
3063 AI points to the data gathered during alias analysis. This
3064 function populates the array AI->POINTERS. */
3066 static tree
3067 get_smt_for (tree ptr, struct alias_info *ai)
3069 size_t i;
3070 tree tag;
3071 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3072 alias_set_type tag_set = get_alias_set (tag_type);
3074 /* To avoid creating unnecessary memory tags, only create one memory tag
3075 per alias set class. Note that it may be tempting to group
3076 memory tags based on conflicting alias sets instead of
3077 equivalence. That would be wrong because alias sets are not
3078 necessarily transitive (as demonstrated by the libstdc++ test
3079 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
3080 such that conflicts (A, B) == true and conflicts (A, C) == true,
3081 it does not necessarily follow that conflicts (B, C) == true. */
3082 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
3084 struct alias_map_d *curr = ai->pointers[i];
3085 tree curr_tag = symbol_mem_tag (curr->var);
3086 if (tag_set == curr->set)
3088 tag = curr_tag;
3089 break;
3093 /* If VAR cannot alias with any of the existing memory tags, create a new
3094 tag for PTR and add it to the POINTERS array. */
3095 if (tag == NULL_TREE)
3097 struct alias_map_d *alias_map;
3099 /* If PTR did not have a symbol tag already, create a new SMT.*
3100 artificial variable representing the memory location
3101 pointed-to by PTR. */
3102 tag = symbol_mem_tag (ptr);
3103 if (tag == NULL_TREE)
3104 tag = create_memory_tag (tag_type, true);
3106 /* Add PTR to the POINTERS array. Note that we are not interested in
3107 PTR's alias set. Instead, we cache the alias set for the memory that
3108 PTR points to. */
3109 alias_map = XCNEW (struct alias_map_d);
3110 alias_map->var = ptr;
3111 alias_map->set = tag_set;
3112 ai->pointers[ai->num_pointers++] = alias_map;
3115 /* If the pointed-to type is volatile, so is the tag. */
3116 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
3118 /* Make sure that the symbol tag has the same alias set as the
3119 pointed-to type. */
3120 gcc_assert (tag_set == get_alias_set (tag));
3122 return tag;
3126 /* Create GLOBAL_VAR, an artificial global variable to act as a
3127 representative of all the variables that may be clobbered by function
3128 calls. */
3130 static void
3131 create_global_var (void)
3133 tree global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
3134 void_type_node);
3135 DECL_ARTIFICIAL (global_var) = 1;
3136 TREE_READONLY (global_var) = 0;
3137 DECL_EXTERNAL (global_var) = 1;
3138 TREE_STATIC (global_var) = 1;
3139 TREE_USED (global_var) = 1;
3140 DECL_CONTEXT (global_var) = NULL_TREE;
3141 TREE_THIS_VOLATILE (global_var) = 0;
3142 TREE_ADDRESSABLE (global_var) = 0;
3144 create_var_ann (global_var);
3145 mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
3146 add_referenced_var (global_var);
3147 mark_sym_for_renaming (global_var);
3148 cfun->gimple_df->global_var = global_var;
3152 /* Dump alias statistics on FILE. */
3154 static void
3155 dump_alias_stats (FILE *file)
3157 const char *funcname
3158 = lang_hooks.decl_printable_name (current_function_decl, 2);
3159 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
3160 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
3161 fprintf (file, "Total alias mayalias results:\t%u\n",
3162 alias_stats.alias_mayalias);
3163 fprintf (file, "Total alias noalias results:\t%u\n",
3164 alias_stats.alias_noalias);
3165 fprintf (file, "Total simple queries:\t%u\n",
3166 alias_stats.simple_queries);
3167 fprintf (file, "Total simple resolved:\t%u\n",
3168 alias_stats.simple_resolved);
3169 fprintf (file, "Total TBAA queries:\t%u\n",
3170 alias_stats.tbaa_queries);
3171 fprintf (file, "Total TBAA resolved:\t%u\n",
3172 alias_stats.tbaa_resolved);
3173 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
3174 alias_stats.structnoaddress_queries);
3175 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
3176 alias_stats.structnoaddress_resolved);
3180 /* Dump alias information on FILE. */
3182 void
3183 dump_alias_info (FILE *file)
3185 size_t i;
3186 const char *funcname
3187 = lang_hooks.decl_printable_name (current_function_decl, 2);
3188 referenced_var_iterator rvi;
3189 tree var;
3191 fprintf (file, "\nAlias information for %s\n\n", funcname);
3193 dump_memory_partitions (file);
3195 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
3197 fprintf (file, "Aliased symbols\n\n");
3199 FOR_EACH_REFERENCED_VAR (var, rvi)
3201 if (may_be_aliased (var))
3202 dump_variable (file, var);
3205 fprintf (file, "\nDereferenced pointers\n\n");
3207 FOR_EACH_REFERENCED_VAR (var, rvi)
3208 if (symbol_mem_tag (var))
3209 dump_variable (file, var);
3211 fprintf (file, "\nSymbol memory tags\n\n");
3213 FOR_EACH_REFERENCED_VAR (var, rvi)
3215 if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
3216 dump_variable (file, var);
3219 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
3221 fprintf (file, "SSA_NAME pointers\n\n");
3222 for (i = 1; i < num_ssa_names; i++)
3224 tree ptr = ssa_name (i);
3225 struct ptr_info_def *pi;
3227 if (ptr == NULL_TREE)
3228 continue;
3230 pi = SSA_NAME_PTR_INFO (ptr);
3231 if (!SSA_NAME_IN_FREE_LIST (ptr)
3232 && pi
3233 && pi->name_mem_tag)
3234 dump_points_to_info_for (file, ptr);
3237 fprintf (file, "\nName memory tags\n\n");
3239 FOR_EACH_REFERENCED_VAR (var, rvi)
3241 if (TREE_CODE (var) == NAME_MEMORY_TAG)
3242 dump_variable (file, var);
3245 fprintf (file, "\n");
3249 /* Dump alias information on stderr. */
3251 void
3252 debug_alias_info (void)
3254 dump_alias_info (stderr);
3258 /* Return the alias information associated with pointer T. It creates a
3259 new instance if none existed. */
3261 struct ptr_info_def *
3262 get_ptr_info (tree t)
3264 struct ptr_info_def *pi;
3266 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
3268 pi = SSA_NAME_PTR_INFO (t);
3269 if (pi == NULL)
3271 pi = GGC_CNEW (struct ptr_info_def);
3272 SSA_NAME_PTR_INFO (t) = pi;
3275 return pi;
3279 /* Dump points-to information for SSA_NAME PTR into FILE. */
3281 void
3282 dump_points_to_info_for (FILE *file, tree ptr)
3284 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3286 print_generic_expr (file, ptr, dump_flags);
3288 if (pi)
3290 if (pi->name_mem_tag)
3292 fprintf (file, ", name memory tag: ");
3293 print_generic_expr (file, pi->name_mem_tag, dump_flags);
3296 if (pi->is_dereferenced)
3297 fprintf (file, ", is dereferenced");
3299 if (pi->value_escapes_p)
3300 fprintf (file, ", its value escapes");
3302 if (pi->pt_anything)
3303 fprintf (file, ", points-to anything");
3305 if (pi->pt_null)
3306 fprintf (file, ", points-to NULL");
3308 if (pi->pt_vars)
3310 fprintf (file, ", points-to vars: ");
3311 dump_decl_set (file, pi->pt_vars);
3315 fprintf (file, "\n");
3319 /* Dump points-to information for VAR into stderr. */
3321 void
3322 debug_points_to_info_for (tree var)
3324 dump_points_to_info_for (stderr, var);
3328 /* Dump points-to information into FILE. NOTE: This function is slow, as
3329 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
3331 void
3332 dump_points_to_info (FILE *file)
3334 basic_block bb;
3335 block_stmt_iterator si;
3336 ssa_op_iter iter;
3337 const char *fname =
3338 lang_hooks.decl_printable_name (current_function_decl, 2);
3339 referenced_var_iterator rvi;
3340 tree var;
3342 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
3344 /* First dump points-to information for the default definitions of
3345 pointer variables. This is necessary because default definitions are
3346 not part of the code. */
3347 FOR_EACH_REFERENCED_VAR (var, rvi)
3349 if (POINTER_TYPE_P (TREE_TYPE (var)))
3351 tree def = gimple_default_def (cfun, var);
3352 if (def)
3353 dump_points_to_info_for (file, def);
3357 /* Dump points-to information for every pointer defined in the program. */
3358 FOR_EACH_BB (bb)
3360 tree phi;
3362 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3364 tree ptr = PHI_RESULT (phi);
3365 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
3366 dump_points_to_info_for (file, ptr);
3369 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3371 tree stmt = bsi_stmt (si);
3372 tree def;
3373 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
3374 if (TREE_CODE (def) == SSA_NAME
3375 && POINTER_TYPE_P (TREE_TYPE (def)))
3376 dump_points_to_info_for (file, def);
3380 fprintf (file, "\n");
3384 /* Dump points-to info pointed to by PTO into STDERR. */
3386 void
3387 debug_points_to_info (void)
3389 dump_points_to_info (stderr);
3392 /* Dump to FILE the list of variables that may be aliasing VAR. */
3394 void
3395 dump_may_aliases_for (FILE *file, tree var)
3397 bitmap aliases;
3399 aliases = MTAG_ALIASES (var);
3400 if (aliases)
3402 bitmap_iterator bi;
3403 unsigned int i;
3404 tree al;
3406 fprintf (file, "{ ");
3407 EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
3409 al = referenced_var (i);
3410 print_generic_expr (file, al, dump_flags);
3411 fprintf (file, " ");
3413 fprintf (file, "}");
3418 /* Dump to stderr the list of variables that may be aliasing VAR. */
3420 void
3421 debug_may_aliases_for (tree var)
3423 dump_may_aliases_for (stderr, var);
3427 /* Return true if VAR may be aliased. */
3429 bool
3430 may_be_aliased (tree var)
3432 /* Obviously. */
3433 if (TREE_ADDRESSABLE (var))
3434 return true;
3436 /* Globally visible variables can have their addresses taken by other
3437 translation units. */
3438 if (MTAG_P (var)
3439 && (MTAG_GLOBAL (var) || TREE_PUBLIC (var)))
3440 return true;
3441 else if (!MTAG_P (var)
3442 && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
3443 return true;
3445 /* Automatic variables can't have their addresses escape any other
3446 way. This must be after the check for global variables, as
3447 extern declarations do not have TREE_STATIC set. */
3448 if (!TREE_STATIC (var))
3449 return false;
3451 /* If we're in unit-at-a-time mode, then we must have seen all
3452 occurrences of address-of operators, and so we can trust
3453 TREE_ADDRESSABLE. Otherwise we can only be sure the variable
3454 isn't addressable if it's local to the current function. */
3455 if (flag_unit_at_a_time)
3456 return false;
3458 if (decl_function_context (var) == current_function_decl)
3459 return false;
3461 return true;
3464 /* The following is based on code in add_stmt_operand to ensure that the
3465 same defs/uses/vdefs/vuses will be found after replacing a reference
3466 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
3467 is the address of var. Return a memtag for the ptr, after adding the
3468 proper may_aliases to it (which are the aliases of var, if it has any,
3469 or var itself). */
3471 static tree
3472 add_may_alias_for_new_tag (tree tag, tree var)
3474 bitmap aliases = NULL;
3476 if (MTAG_P (var))
3477 aliases = may_aliases (var);
3479 /* Case 1: |aliases| == 1 */
3480 if (aliases
3481 && bitmap_single_bit_set_p (aliases))
3483 tree ali = referenced_var (bitmap_first_set_bit (aliases));
3484 if (TREE_CODE (ali) == SYMBOL_MEMORY_TAG)
3485 return ali;
3488 /* Case 2: |aliases| == 0 */
3489 if (aliases == NULL)
3490 add_may_alias (tag, var);
3491 else
3493 /* Case 3: |aliases| > 1 */
3494 union_alias_set_into (tag, aliases);
3496 return tag;
3499 /* Create a new symbol tag for PTR. Construct the may-alias list of this type
3500 tag so that it has the aliasing of VAR, or of the relevant subvars of VAR
3501 according to the location accessed by EXPR.
3503 Note, the set of aliases represented by the new symbol tag are not marked
3504 for renaming. */
3506 void
3507 new_type_alias (tree ptr, tree var, tree expr)
3509 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3510 tree tag;
3511 subvar_t svars;
3512 tree ali = NULL_TREE;
3513 HOST_WIDE_INT offset, size, maxsize;
3514 tree ref;
3515 VEC (tree, heap) *overlaps = NULL;
3516 unsigned int len, i;
3517 tree subvar;
3520 gcc_assert (symbol_mem_tag (ptr) == NULL_TREE);
3521 gcc_assert (!MTAG_P (var));
3523 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
3524 gcc_assert (ref);
3526 tag = create_memory_tag (tag_type, true);
3527 set_symbol_mem_tag (ptr, tag);
3529 /* Add VAR to the may-alias set of PTR's new symbol tag. If VAR has
3530 subvars, add the subvars to the tag instead of the actual var. */
3531 if (var_can_have_subvars (ref)
3532 && (svars = get_subvars_for_var (ref)))
3534 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
3536 bool exact;
3538 if (overlap_subvar (offset, maxsize, subvar, &exact))
3539 VEC_safe_push (tree, heap, overlaps, subvar);
3541 gcc_assert (overlaps != NULL);
3543 else if (var_can_have_subvars (var)
3544 && (svars = get_subvars_for_var (var)))
3546 /* If the REF is not a direct access to VAR (e.g., it is a dereference
3547 of a pointer), we should scan the virtual operands of REF the same
3548 way as tree-ssa-operands do. At the moment, this is somewhat
3549 difficult, so we just give up and add all the subvars of VAR.
3550 On mem-ssa branch, the scanning for virtual operands have been
3551 split from the rest of tree-ssa-operands, so it should be much
3552 easier to fix this problem correctly once mem-ssa is merged. */
3553 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
3554 VEC_safe_push (tree, heap, overlaps, subvar);
3556 gcc_assert (overlaps != NULL);
3558 else
3559 ali = add_may_alias_for_new_tag (tag, var);
3561 len = VEC_length (tree, overlaps);
3562 if (len > 0)
3564 if (dump_file && (dump_flags & TDF_DETAILS))
3565 fprintf (dump_file, "\nnumber of overlapping subvars = %u\n", len);
3567 if (len == 1)
3568 ali = add_may_alias_for_new_tag (tag, VEC_index (tree, overlaps, 0));
3569 else if (len > 1)
3571 unsigned int k;
3572 tree sv_var;
3574 for (k = 0; VEC_iterate (tree, overlaps, k, sv_var); k++)
3576 ali = add_may_alias_for_new_tag (tag, sv_var);
3578 if (ali != tag)
3580 /* Can happen only if 'Case 1' of add_may_alias_for_new_tag
3581 took place. Since more than one svar was found, we add
3582 'ali' as one of the may_aliases of the new tag. */
3583 add_may_alias (tag, ali);
3584 ali = tag;
3588 VEC_free (tree, heap, overlaps);
3591 set_symbol_mem_tag (ptr, ali);
3592 TREE_READONLY (tag) = TREE_READONLY (var);
3593 MTAG_GLOBAL (tag) = is_global_var (var);
3596 /* This represents the used range of a variable. */
3598 typedef struct used_part
3600 HOST_WIDE_INT minused;
3601 HOST_WIDE_INT maxused;
3602 /* True if we have an explicit use/def of some portion of this variable,
3603 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
3604 bool explicit_uses;
3605 /* True if we have an implicit use/def of some portion of this
3606 variable. Implicit uses occur when we can't tell what part we
3607 are referencing, and have to make conservative assumptions. */
3608 bool implicit_uses;
3609 /* True if the structure is only written to or taken its address. */
3610 bool write_only;
3611 } *used_part_t;
3613 /* An array of used_part structures, indexed by variable uid. */
3615 static htab_t used_portions;
3617 struct used_part_map
3619 unsigned int uid;
3620 used_part_t to;
3623 /* Return true if the uid in the two used part maps are equal. */
3625 static int
3626 used_part_map_eq (const void *va, const void *vb)
3628 const struct used_part_map *a = (const struct used_part_map *) va;
3629 const struct used_part_map *b = (const struct used_part_map *) vb;
3630 return (a->uid == b->uid);
3633 /* Hash a from uid in a used_part_map. */
3635 static unsigned int
3636 used_part_map_hash (const void *item)
3638 return ((const struct used_part_map *)item)->uid;
3641 /* Free a used part map element. */
3643 static void
3644 free_used_part_map (void *item)
3646 free (((struct used_part_map *)item)->to);
3647 free (item);
3650 /* Lookup a used_part structure for a UID. */
3652 static used_part_t
3653 up_lookup (unsigned int uid)
3655 struct used_part_map *h, in;
3656 in.uid = uid;
3657 h = (struct used_part_map *) htab_find_with_hash (used_portions, &in, uid);
3658 if (!h)
3659 return NULL;
3660 return h->to;
3663 /* Insert the pair UID, TO into the used part hashtable. */
3665 static void
3666 up_insert (unsigned int uid, used_part_t to)
3668 struct used_part_map *h;
3669 void **loc;
3671 h = XNEW (struct used_part_map);
3672 h->uid = uid;
3673 h->to = to;
3674 loc = htab_find_slot_with_hash (used_portions, h,
3675 uid, INSERT);
3676 if (*loc != NULL)
3677 free (*loc);
3678 *(struct used_part_map **) loc = h;
3682 /* Given a variable uid, UID, get or create the entry in the used portions
3683 table for the variable. */
3685 static used_part_t
3686 get_or_create_used_part_for (size_t uid)
3688 used_part_t up;
3689 if ((up = up_lookup (uid)) == NULL)
3691 up = XCNEW (struct used_part);
3692 up->minused = INT_MAX;
3693 up->maxused = 0;
3694 up->explicit_uses = false;
3695 up->implicit_uses = false;
3696 up->write_only = true;
3699 return up;
3703 /* Create and return a structure sub-variable for field type FIELD at
3704 offset OFFSET, with size SIZE, of variable VAR. If ALIAS_SET not
3705 -1 this field is non-addressable and we should use this alias set
3706 with this field. */
3708 static tree
3709 create_sft (tree var, tree field, unsigned HOST_WIDE_INT offset,
3710 unsigned HOST_WIDE_INT size, alias_set_type alias_set,
3711 bool base_for_components)
3713 tree subvar = create_tag_raw (STRUCT_FIELD_TAG, field, "SFT");
3715 /* We need to copy the various flags from VAR to SUBVAR, so that
3716 they are is_global_var iff the original variable was. */
3717 DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
3718 MTAG_GLOBAL (subvar) = DECL_EXTERNAL (var);
3719 TREE_PUBLIC (subvar) = TREE_PUBLIC (var);
3720 TREE_STATIC (subvar) = TREE_STATIC (var);
3721 TREE_READONLY (subvar) = TREE_READONLY (var);
3722 TREE_ADDRESSABLE (subvar) = TREE_ADDRESSABLE (var);
3724 /* Add the new variable to REFERENCED_VARS. */
3725 set_symbol_mem_tag (subvar, NULL);
3726 add_referenced_var (subvar);
3727 SFT_PARENT_VAR (subvar) = var;
3728 SFT_OFFSET (subvar) = offset;
3729 SFT_SIZE (subvar) = size;
3730 SFT_ALIAS_SET (subvar) = alias_set;
3731 SFT_BASE_FOR_COMPONENTS_P (subvar) = base_for_components;
3732 SFT_UNPARTITIONABLE_P (subvar) = false;
3734 return subvar;
3738 /* Given an aggregate VAR, create the subvariables that represent its
3739 fields. */
3741 static void
3742 create_overlap_variables_for (tree var)
3744 VEC(fieldoff_s,heap) *fieldstack = NULL;
3745 used_part_t up;
3746 size_t uid = DECL_UID (var);
3748 up = up_lookup (uid);
3749 if (!up
3750 || up->write_only)
3751 return;
3753 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL,
3754 TREE_TYPE (var));
3755 /* Make sure to not create SFTs for structs we won't generate variable
3756 infos for. See tree-ssa-structalias.c:create_variable_info_for (). */
3757 if (VEC_length (fieldoff_s, fieldstack) > 1
3758 && VEC_length (fieldoff_s, fieldstack) <= MAX_FIELDS_FOR_FIELD_SENSITIVE)
3760 subvar_t *subvars;
3761 fieldoff_s *fo;
3762 bool notokay = false;
3763 int fieldcount = 0;
3764 int i;
3765 HOST_WIDE_INT lastfooffset = -1;
3766 HOST_WIDE_INT lastfosize = -1;
3767 tree lastfotype = NULL_TREE;
3769 /* Not all fields have DECL_SIZE set, and those that don't, we don't
3770 know their size, and thus, can't handle.
3771 The same is true of fields with DECL_SIZE that is not an integer
3772 constant (such as variable sized fields).
3773 Fields with offsets which are not constant will have an offset < 0
3774 We *could* handle fields that are constant sized arrays, but
3775 currently don't. Doing so would require some extra changes to
3776 tree-ssa-operands.c. */
3778 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3780 if (!fo->size
3781 || TREE_CODE (fo->size) != INTEGER_CST
3782 || fo->offset < 0)
3784 notokay = true;
3785 break;
3787 fieldcount++;
3790 /* The current heuristic we use is as follows:
3791 If the variable has no used portions in this function, no
3792 structure vars are created for it.
3793 Otherwise,
3794 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
3795 we always create structure vars for them.
3796 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3797 some explicit uses, we create structure vars for them.
3798 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3799 no explicit uses, we do not create structure vars for them.
3802 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
3803 && !up->explicit_uses)
3805 if (dump_file && (dump_flags & TDF_DETAILS))
3807 fprintf (dump_file, "Variable ");
3808 print_generic_expr (dump_file, var, 0);
3809 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
3811 notokay = true;
3814 /* Bail out, if we can't create overlap variables. */
3815 if (notokay)
3817 VEC_free (fieldoff_s, heap, fieldstack);
3818 return;
3821 /* Otherwise, create the variables. */
3822 subvars = lookup_subvars_for_var (var);
3823 *subvars = VEC_alloc (tree, gc, VEC_length (fieldoff_s, fieldstack));
3825 sort_fieldstack (fieldstack);
3827 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); ++i)
3829 HOST_WIDE_INT fosize;
3830 tree currfotype, subvar;
3832 fosize = TREE_INT_CST_LOW (fo->size);
3833 currfotype = fo->type;
3835 /* If this field isn't in the used portion,
3836 or it has the exact same offset and size as the last
3837 field, skip it. Note that we always need the field at
3838 offset 0 so we can properly handle pointers to the
3839 structure. */
3841 if ((fo->offset != 0
3842 && ((fo->offset <= up->minused
3843 && fo->offset + fosize <= up->minused)
3844 || fo->offset >= up->maxused))
3845 || (fo->offset == lastfooffset
3846 && fosize == lastfosize
3847 && currfotype == lastfotype))
3848 continue;
3849 subvar = create_sft (var, fo->type, fo->offset,
3850 fosize, fo->alias_set, fo->base_for_components);
3851 VEC_quick_push (tree, *subvars, subvar);
3853 if (dump_file)
3855 fprintf (dump_file, "structure field tag %s created for var %s",
3856 get_name (subvar), get_name (var));
3857 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
3858 SFT_OFFSET (subvar));
3859 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
3860 SFT_SIZE (subvar));
3861 fprintf (dump_file, "\n");
3864 lastfotype = currfotype;
3865 lastfooffset = fo->offset;
3866 lastfosize = fosize;
3869 /* Once we have created subvars, the original is no longer call
3870 clobbered on its own. Its call clobbered status depends
3871 completely on the call clobbered status of the subvars.
3873 add_referenced_var in the above loop will take care of
3874 marking subvars of global variables as call clobbered for us
3875 to start, since they are global as well. */
3876 clear_call_clobbered (var);
3879 VEC_free (fieldoff_s, heap, fieldstack);
3883 /* Find the conservative answer to the question of what portions of what
3884 structures are used by this statement. We assume that if we have a
3885 component ref with a known size + offset, that we only need that part
3886 of the structure. For unknown cases, or cases where we do something
3887 to the whole structure, we assume we need to create fields for the
3888 entire structure. */
3890 static tree
3891 find_used_portions (tree *tp, int *walk_subtrees, void *lhs_p)
3893 switch (TREE_CODE (*tp))
3895 case GIMPLE_MODIFY_STMT:
3896 /* Recurse manually here to track whether the use is in the
3897 LHS of an assignment. */
3898 find_used_portions (&GIMPLE_STMT_OPERAND (*tp, 0), walk_subtrees, tp);
3899 return find_used_portions (&GIMPLE_STMT_OPERAND (*tp, 1),
3900 walk_subtrees, NULL);
3901 case REALPART_EXPR:
3902 case IMAGPART_EXPR:
3903 case COMPONENT_REF:
3904 case ARRAY_REF:
3906 HOST_WIDE_INT bitsize;
3907 HOST_WIDE_INT bitmaxsize;
3908 HOST_WIDE_INT bitpos;
3909 tree ref;
3910 ref = get_ref_base_and_extent (*tp, &bitpos, &bitsize, &bitmaxsize);
3911 if (DECL_P (ref)
3912 && var_can_have_subvars (ref)
3913 && bitmaxsize != -1)
3915 size_t uid = DECL_UID (ref);
3916 used_part_t up;
3918 up = get_or_create_used_part_for (uid);
3920 if (bitpos <= up->minused)
3921 up->minused = bitpos;
3922 if ((bitpos + bitmaxsize >= up->maxused))
3923 up->maxused = bitpos + bitmaxsize;
3925 if (bitsize == bitmaxsize)
3926 up->explicit_uses = true;
3927 else
3928 up->implicit_uses = true;
3929 if (!lhs_p)
3930 up->write_only = false;
3931 up_insert (uid, up);
3933 *walk_subtrees = 0;
3934 return NULL_TREE;
3937 break;
3938 /* This is here to make sure we mark the entire base variable as used
3939 when you take its address. Because our used portion analysis is
3940 simple, we aren't looking at casts or pointer arithmetic to see what
3941 happens when you take the address. */
3942 case ADDR_EXPR:
3944 tree var = get_base_address (TREE_OPERAND (*tp, 0));
3946 if (var
3947 && DECL_P (var)
3948 && DECL_SIZE (var)
3949 && var_can_have_subvars (var)
3950 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3952 used_part_t up;
3953 size_t uid = DECL_UID (var);
3955 up = get_or_create_used_part_for (uid);
3957 up->minused = 0;
3958 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3959 up->implicit_uses = true;
3960 if (!lhs_p)
3961 up->write_only = false;
3963 up_insert (uid, up);
3964 *walk_subtrees = 0;
3965 return NULL_TREE;
3968 break;
3969 case CALL_EXPR:
3971 int i;
3972 int nargs = call_expr_nargs (*tp);
3973 for (i = 0; i < nargs; i++)
3975 tree *arg = &CALL_EXPR_ARG (*tp, i);
3976 if (TREE_CODE (*arg) == ADDR_EXPR)
3977 find_used_portions (arg, walk_subtrees, NULL);
3979 *walk_subtrees = 0;
3980 return NULL_TREE;
3982 case VAR_DECL:
3983 case PARM_DECL:
3984 case RESULT_DECL:
3986 tree var = *tp;
3987 if (DECL_SIZE (var)
3988 && var_can_have_subvars (var)
3989 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3991 used_part_t up;
3992 size_t uid = DECL_UID (var);
3994 up = get_or_create_used_part_for (uid);
3996 up->minused = 0;
3997 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3998 up->implicit_uses = true;
4000 up_insert (uid, up);
4001 *walk_subtrees = 0;
4002 return NULL_TREE;
4005 break;
4007 default:
4008 break;
4011 return NULL_TREE;
4014 /* Create structure field variables for structures used in this function. */
4016 static unsigned int
4017 create_structure_vars (void)
4019 basic_block bb;
4020 safe_referenced_var_iterator rvi;
4021 VEC (tree, heap) *varvec = NULL;
4022 tree var;
4024 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
4025 free_used_part_map);
4027 FOR_EACH_BB (bb)
4029 block_stmt_iterator bsi;
4030 tree phi;
4032 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4034 use_operand_p use;
4035 ssa_op_iter iter;
4037 FOR_EACH_PHI_ARG (use, phi, iter, SSA_OP_USE)
4039 tree op = USE_FROM_PTR (use);
4040 walk_tree_without_duplicates (&op, find_used_portions,
4041 NULL);
4045 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4047 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
4048 find_used_portions,
4049 NULL);
4052 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, rvi)
4054 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
4055 if (var
4056 && DECL_SIZE (var)
4057 && var_can_have_subvars (var)
4058 && !MTAG_P (var)
4059 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
4060 create_overlap_variables_for (var);
4062 htab_delete (used_portions);
4063 VEC_free (tree, heap, varvec);
4065 /* Update SSA operands of statements mentioning variables we split. */
4066 if (gimple_in_ssa_p (cfun))
4067 FOR_EACH_BB (bb)
4069 block_stmt_iterator bsi;
4070 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4072 tree stmt = bsi_stmt (bsi);
4073 bool update = false;
4074 unsigned int i;
4075 bitmap_iterator bi;
4077 if (STORED_SYMS (stmt))
4078 EXECUTE_IF_SET_IN_BITMAP (STORED_SYMS (stmt), 0, i, bi)
4080 tree sym = referenced_var_lookup (i);
4081 if (get_subvars_for_var (sym))
4083 update = true;
4084 break;
4088 if (LOADED_SYMS (stmt) && !update)
4089 EXECUTE_IF_SET_IN_BITMAP (LOADED_SYMS (stmt), 0, i, bi)
4091 tree sym = referenced_var_lookup (i);
4092 if (get_subvars_for_var (sym))
4094 update = true;
4095 break;
4099 if (stmt_ann (stmt)->addresses_taken && !update)
4100 EXECUTE_IF_SET_IN_BITMAP (stmt_ann (stmt)->addresses_taken,
4101 0, i, bi)
4103 tree sym = referenced_var_lookup (i);
4104 if (get_subvars_for_var (sym))
4106 update = true;
4107 break;
4111 if (update)
4112 update_stmt (stmt);
4116 return TODO_rebuild_alias;
4119 static bool
4120 gate_structure_vars (void)
4122 return flag_tree_salias != 0;
4125 struct gimple_opt_pass pass_create_structure_vars =
4128 GIMPLE_PASS,
4129 "salias", /* name */
4130 gate_structure_vars, /* gate */
4131 create_structure_vars, /* execute */
4132 NULL, /* sub */
4133 NULL, /* next */
4134 0, /* static_pass_number */
4135 0, /* tv_id */
4136 PROP_cfg, /* properties_required */
4137 0, /* properties_provided */
4138 0, /* properties_destroyed */
4139 0, /* todo_flags_start */
4140 TODO_dump_func /* todo_flags_finish */
4144 /* Reset the call_clobbered flags on our referenced vars. In
4145 theory, this only needs to be done for globals. */
4147 static unsigned int
4148 reset_cc_flags (void)
4150 tree var;
4151 referenced_var_iterator rvi;
4153 FOR_EACH_REFERENCED_VAR (var, rvi)
4154 var_ann (var)->call_clobbered = false;
4155 return 0;
4158 struct gimple_opt_pass pass_reset_cc_flags =
4161 GIMPLE_PASS,
4162 NULL, /* name */
4163 NULL, /* gate */
4164 reset_cc_flags, /* execute */
4165 NULL, /* sub */
4166 NULL, /* next */
4167 0, /* static_pass_number */
4168 0, /* tv_id */
4169 PROP_referenced_vars |PROP_cfg, /* properties_required */
4170 0, /* properties_provided */
4171 0, /* properties_destroyed */
4172 0, /* todo_flags_start */
4173 0 /* todo_flags_finish */
4177 static bool
4178 gate_build_alias (void)
4180 return !gate_structure_vars();
4184 struct gimple_opt_pass pass_build_alias =
4187 GIMPLE_PASS,
4188 "build_alias", /* name */
4189 gate_build_alias, /* gate */
4190 NULL, /* execute */
4191 NULL, /* sub */
4192 NULL, /* next */
4193 0, /* static_pass_number */
4194 0, /* tv_id */
4195 PROP_cfg | PROP_ssa, /* properties_required */
4196 PROP_alias, /* properties_provided */
4197 0, /* properties_destroyed */
4198 0, /* todo_flags_start */
4199 TODO_rebuild_alias /* todo_flags_finish */