Update concepts branch to revision 131834
[official-gcc.git] / gcc / tree-ssa-alias.c
blob0e5071994de691cdcc12315e67ae75bf82350a8e
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 tree create_memory_tag (tree type, bool is_type_tag);
201 static tree get_smt_for (tree, struct alias_info *);
202 static tree get_nmt_for (tree);
203 static void add_may_alias (tree, tree);
204 static struct alias_info *init_alias_info (void);
205 static void delete_alias_info (struct alias_info *);
206 static void compute_flow_sensitive_aliasing (struct alias_info *);
207 static void setup_pointers_and_addressables (struct alias_info *);
208 static void create_global_var (void);
209 static void maybe_create_global_var (void);
210 static void set_pt_anything (tree);
212 void debug_mp_info (VEC(mem_sym_stats_t,heap) *);
214 static alloc_pool mem_sym_stats_pool;
216 /* Return memory reference stats for symbol VAR. Create a new slot in
217 cfun->gimple_df->mem_sym_stats if needed. */
219 static struct mem_sym_stats_d *
220 get_mem_sym_stats_for (tree var)
222 void **slot;
223 struct mem_sym_stats_d *stats;
224 struct pointer_map_t *map = gimple_mem_ref_stats (cfun)->mem_sym_stats;
226 gcc_assert (map);
228 slot = pointer_map_insert (map, var);
229 if (*slot == NULL)
231 stats = pool_alloc (mem_sym_stats_pool);
232 memset (stats, 0, sizeof (*stats));
233 stats->var = var;
234 *slot = (void *) stats;
236 else
237 stats = (struct mem_sym_stats_d *) *slot;
239 return stats;
243 /* Return memory reference statistics for variable VAR in function FN.
244 This is computed by alias analysis, but it is not kept
245 incrementally up-to-date. So, these stats are only accurate if
246 pass_may_alias has been run recently. If no alias information
247 exists, this function returns NULL. */
249 static mem_sym_stats_t
250 mem_sym_stats (struct function *fn, tree var)
252 void **slot;
253 struct pointer_map_t *stats_map = gimple_mem_ref_stats (fn)->mem_sym_stats;
255 if (stats_map == NULL)
256 return NULL;
258 slot = pointer_map_contains (stats_map, var);
259 if (slot == NULL)
260 return NULL;
262 return (mem_sym_stats_t) *slot;
266 /* Set MPT to be the memory partition associated with symbol SYM. */
268 static inline void
269 set_memory_partition (tree sym, tree mpt)
271 #if defined ENABLE_CHECKING
272 if (mpt)
273 gcc_assert (TREE_CODE (mpt) == MEMORY_PARTITION_TAG
274 && !is_gimple_reg (sym));
275 #endif
277 var_ann (sym)->mpt = mpt;
278 if (mpt)
280 if (MPT_SYMBOLS (mpt) == NULL)
281 MPT_SYMBOLS (mpt) = BITMAP_ALLOC (&alias_bitmap_obstack);
283 bitmap_set_bit (MPT_SYMBOLS (mpt), DECL_UID (sym));
285 /* MPT inherits the call-clobbering attributes from SYM. */
286 if (is_call_clobbered (sym))
288 MTAG_GLOBAL (mpt) = 1;
289 mark_call_clobbered (mpt, ESCAPE_IS_GLOBAL);
295 /* Mark variable VAR as being non-addressable. */
297 static void
298 mark_non_addressable (tree var)
300 tree mpt;
302 if (!TREE_ADDRESSABLE (var))
303 return;
305 mpt = memory_partition (var);
307 clear_call_clobbered (var);
308 TREE_ADDRESSABLE (var) = 0;
310 if (mpt)
312 /* Note that it's possible for a symbol to have an associated
313 MPT and the MPT have a NULL empty set. During
314 init_alias_info, all MPTs get their sets cleared out, but the
315 symbols still point to the old MPTs that used to hold them.
316 This is done so that compute_memory_partitions can now which
317 symbols are losing or changing partitions and mark them for
318 renaming. */
319 if (MPT_SYMBOLS (mpt))
320 bitmap_clear_bit (MPT_SYMBOLS (mpt), DECL_UID (var));
321 set_memory_partition (var, NULL_TREE);
326 /* qsort comparison function to sort type/name tags by DECL_UID. */
328 static int
329 sort_tags_by_id (const void *pa, const void *pb)
331 const_tree const a = *(const_tree const *)pa;
332 const_tree const b = *(const_tree const *)pb;
334 return DECL_UID (a) - DECL_UID (b);
337 /* Initialize WORKLIST to contain those memory tags that are marked call
338 clobbered. Initialized WORKLIST2 to contain the reasons these
339 memory tags escaped. */
341 static void
342 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
343 VEC (int, heap) **worklist2,
344 bitmap on_worklist)
346 referenced_var_iterator rvi;
347 tree curr;
349 FOR_EACH_REFERENCED_VAR (curr, rvi)
351 if (MTAG_P (curr) && is_call_clobbered (curr))
353 VEC_safe_push (tree, heap, *worklist, curr);
354 VEC_safe_push (int, heap, *worklist2,
355 var_ann (curr)->escape_mask);
356 bitmap_set_bit (on_worklist, DECL_UID (curr));
361 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
362 ALIAS is not already marked call clobbered, and is a memory
363 tag. */
365 static void
366 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
367 VEC (int, heap) **worklist2, int reason,
368 bitmap on_worklist)
370 if (MTAG_P (alias) && !is_call_clobbered (alias)
371 && !bitmap_bit_p (on_worklist, DECL_UID (alias)))
373 VEC_safe_push (tree, heap, *worklist, alias);
374 VEC_safe_push (int, heap, *worklist2, reason);
375 bitmap_set_bit (on_worklist, DECL_UID (alias));
379 /* Mark aliases of TAG as call clobbered, and place any tags on the
380 alias list that were not already call clobbered on WORKLIST. */
382 static void
383 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
384 VEC (int, heap) **worklist2, bitmap on_worklist)
386 bitmap aliases;
387 bitmap_iterator bi;
388 unsigned int i;
389 tree entry;
390 var_ann_t ta = var_ann (tag);
392 if (!MTAG_P (tag))
393 return;
394 aliases = may_aliases (tag);
395 if (!aliases)
396 return;
398 EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
400 entry = referenced_var (i);
401 /* If you clobber one part of a structure, you
402 clobber the entire thing. While this does not make
403 the world a particularly nice place, it is necessary
404 in order to allow C/C++ tricks that involve
405 pointer arithmetic to work. */
406 if (!unmodifiable_var_p (entry))
408 add_to_worklist (entry, worklist, worklist2, ta->escape_mask,
409 on_worklist);
410 mark_call_clobbered (entry, ta->escape_mask);
415 /* Tags containing global vars need to be marked as global.
416 Tags containing call clobbered vars need to be marked as call
417 clobbered. */
419 static void
420 compute_tag_properties (void)
422 referenced_var_iterator rvi;
423 tree tag;
424 bool changed = true;
425 VEC (tree, heap) *taglist = NULL;
427 FOR_EACH_REFERENCED_VAR (tag, rvi)
429 if (!MTAG_P (tag))
430 continue;
431 VEC_safe_push (tree, heap, taglist, tag);
434 /* We sort the taglist by DECL_UID, for two reasons.
435 1. To get a sequential ordering to make the bitmap accesses
436 faster.
437 2. Because of the way we compute aliases, it's more likely that
438 an earlier tag is included in a later tag, and this will reduce
439 the number of iterations.
441 If we had a real tag graph, we would just topo-order it and be
442 done with it. */
443 qsort (VEC_address (tree, taglist),
444 VEC_length (tree, taglist),
445 sizeof (tree),
446 sort_tags_by_id);
448 /* Go through each tag not marked as global, and if it aliases
449 global vars, mark it global.
451 If the tag contains call clobbered vars, mark it call
452 clobbered.
454 This loop iterates because tags may appear in the may-aliases
455 list of other tags when we group. */
457 while (changed)
459 unsigned int k;
461 changed = false;
462 for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
464 bitmap ma;
465 bitmap_iterator bi;
466 unsigned int i;
467 tree entry;
468 bool tagcc = is_call_clobbered (tag);
469 bool tagglobal = MTAG_GLOBAL (tag);
471 if (tagcc && tagglobal)
472 continue;
474 ma = may_aliases (tag);
475 if (!ma)
476 continue;
478 EXECUTE_IF_SET_IN_BITMAP (ma, 0, i, bi)
480 entry = referenced_var (i);
481 /* Call clobbered entries cause the tag to be marked
482 call clobbered. */
483 if (!tagcc && is_call_clobbered (entry))
485 mark_call_clobbered (tag, var_ann (entry)->escape_mask);
486 tagcc = true;
487 changed = true;
490 /* Global vars cause the tag to be marked global. */
491 if (!tagglobal && is_global_var (entry))
493 MTAG_GLOBAL (tag) = true;
494 changed = true;
495 tagglobal = true;
498 /* Early exit once both global and cc are set, since the
499 loop can't do any more than that. */
500 if (tagcc && tagglobal)
501 break;
505 VEC_free (tree, heap, taglist);
508 /* Set up the initial variable clobbers and globalness.
509 When this function completes, only tags whose aliases need to be
510 clobbered will be set clobbered. Tags clobbered because they
511 contain call clobbered vars are handled in compute_tag_properties. */
513 static void
514 set_initial_properties (struct alias_info *ai)
516 unsigned int i;
517 referenced_var_iterator rvi;
518 tree var;
519 tree ptr;
520 bool any_pt_anything = false;
521 enum escape_type pt_anything_mask = 0;
523 FOR_EACH_REFERENCED_VAR (var, rvi)
525 if (is_global_var (var))
527 if (!unmodifiable_var_p (var))
528 mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
530 else if (TREE_CODE (var) == PARM_DECL
531 && gimple_default_def (cfun, var)
532 && POINTER_TYPE_P (TREE_TYPE (var)))
534 tree def = gimple_default_def (cfun, var);
535 get_ptr_info (def)->value_escapes_p = 1;
536 get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;
540 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
542 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
543 tree tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
545 /* A pointer that only escapes via a function return does not
546 add to the call clobber or call used solution.
547 To exclude ESCAPE_TO_PURE_CONST we would need to track
548 call used variables separately or compute those properly
549 in the operand scanner. */
550 if (pi->value_escapes_p
551 && pi->escape_mask & ~ESCAPE_TO_RETURN)
553 /* If PTR escapes then its associated memory tags and
554 pointed-to variables are call-clobbered. */
555 if (pi->name_mem_tag)
556 mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
558 if (tag)
559 mark_call_clobbered (tag, pi->escape_mask);
561 /* Defer to points-to analysis if possible, otherwise
562 clobber all addressable variables. Parameters cannot
563 point to local memory though.
564 ??? Properly tracking which pointers point to non-local
565 memory only would make a big difference here. */
566 if (!clobber_what_p_points_to (ptr)
567 && !(pi->escape_mask & ESCAPE_IS_PARM))
569 any_pt_anything = true;
570 pt_anything_mask |= pi->escape_mask;
574 /* If the name tag is call clobbered, so is the symbol tag
575 associated with the base VAR_DECL. */
576 if (pi->name_mem_tag
577 && tag
578 && is_call_clobbered (pi->name_mem_tag))
579 mark_call_clobbered (tag, pi->escape_mask);
581 /* Name tags and symbol tags that we don't know where they point
582 to, might point to global memory, and thus, are clobbered.
584 FIXME: This is not quite right. They should only be
585 clobbered if value_escapes_p is true, regardless of whether
586 they point to global memory or not.
587 So removing this code and fixing all the bugs would be nice.
588 It is the cause of a bunch of clobbering. */
589 if ((pi->pt_global_mem || pi->pt_anything)
590 && pi->memory_tag_needed && pi->name_mem_tag)
592 mark_call_clobbered (pi->name_mem_tag, ESCAPE_IS_GLOBAL);
593 MTAG_GLOBAL (pi->name_mem_tag) = true;
596 if ((pi->pt_global_mem || pi->pt_anything)
597 && pi->memory_tag_needed
598 && tag)
600 mark_call_clobbered (tag, ESCAPE_IS_GLOBAL);
601 MTAG_GLOBAL (tag) = true;
605 /* If a pt_anything pointer escaped we need to mark all addressable
606 variables call clobbered. */
607 if (any_pt_anything)
609 bitmap_iterator bi;
610 unsigned int j;
612 EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, j, bi)
614 tree var = referenced_var (j);
615 if (!unmodifiable_var_p (var))
616 mark_call_clobbered (var, pt_anything_mask);
621 /* Compute which variables need to be marked call clobbered because
622 their tag is call clobbered, and which tags need to be marked
623 global because they contain global variables. */
625 static void
626 compute_call_clobbered (struct alias_info *ai)
628 VEC (tree, heap) *worklist = NULL;
629 VEC (int,heap) *worklist2 = NULL;
630 bitmap on_worklist;
632 timevar_push (TV_CALL_CLOBBER);
633 on_worklist = BITMAP_ALLOC (NULL);
635 set_initial_properties (ai);
636 init_transitive_clobber_worklist (&worklist, &worklist2, on_worklist);
637 while (VEC_length (tree, worklist) != 0)
639 tree curr = VEC_pop (tree, worklist);
640 int reason = VEC_pop (int, worklist2);
642 bitmap_clear_bit (on_worklist, DECL_UID (curr));
643 mark_call_clobbered (curr, reason);
644 mark_aliases_call_clobbered (curr, &worklist, &worklist2, on_worklist);
646 VEC_free (tree, heap, worklist);
647 VEC_free (int, heap, worklist2);
648 BITMAP_FREE (on_worklist);
649 compute_tag_properties ();
650 timevar_pop (TV_CALL_CLOBBER);
654 /* Dump memory partition information to FILE. */
656 static void
657 dump_memory_partitions (FILE *file)
659 unsigned i, npart;
660 unsigned long nsyms;
661 tree mpt;
663 fprintf (file, "\nMemory partitions\n\n");
664 for (i = 0, npart = 0, nsyms = 0;
665 VEC_iterate (tree, gimple_ssa_operands (cfun)->mpt_table, i, mpt);
666 i++)
668 if (mpt)
670 bitmap syms = MPT_SYMBOLS (mpt);
671 unsigned long n = (syms) ? bitmap_count_bits (syms) : 0;
673 fprintf (file, "#%u: ", i);
674 print_generic_expr (file, mpt, 0);
675 fprintf (file, ": %lu elements: ", n);
676 dump_decl_set (file, syms);
677 npart++;
678 nsyms += n;
682 fprintf (file, "\n%u memory partitions holding %lu symbols\n", npart, nsyms);
686 /* Dump memory partition information to stderr. */
688 void
689 debug_memory_partitions (void)
691 dump_memory_partitions (stderr);
695 /* Return true if memory partitioning is required given the memory
696 reference estimates in STATS. */
698 static inline bool
699 need_to_partition_p (struct mem_ref_stats_d *stats)
701 long num_vops = stats->num_vuses + stats->num_vdefs;
702 long avg_vops = CEIL (num_vops, stats->num_mem_stmts);
703 return (num_vops > (long) MAX_ALIASED_VOPS
704 && avg_vops > (long) AVG_ALIASED_VOPS);
708 /* Count the actual number of virtual operators in CFUN. Note that
709 this is only meaningful after virtual operands have been populated,
710 so it should be invoked at the end of compute_may_aliases.
712 The number of virtual operators are stored in *NUM_VDEFS_P and
713 *NUM_VUSES_P, the number of partitioned symbols in
714 *NUM_PARTITIONED_P and the number of unpartitioned symbols in
715 *NUM_UNPARTITIONED_P.
717 If any of these pointers is NULL the corresponding count is not
718 computed. */
720 static void
721 count_mem_refs (long *num_vuses_p, long *num_vdefs_p,
722 long *num_partitioned_p, long *num_unpartitioned_p)
724 block_stmt_iterator bsi;
725 basic_block bb;
726 long num_vdefs, num_vuses, num_partitioned, num_unpartitioned;
727 referenced_var_iterator rvi;
728 tree sym;
730 num_vuses = num_vdefs = num_partitioned = num_unpartitioned = 0;
732 if (num_vuses_p || num_vdefs_p)
733 FOR_EACH_BB (bb)
734 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
736 tree stmt = bsi_stmt (bsi);
737 if (stmt_references_memory_p (stmt))
739 num_vuses += NUM_SSA_OPERANDS (stmt, SSA_OP_VUSE);
740 num_vdefs += NUM_SSA_OPERANDS (stmt, SSA_OP_VDEF);
744 if (num_partitioned_p || num_unpartitioned_p)
745 FOR_EACH_REFERENCED_VAR (sym, rvi)
747 if (is_gimple_reg (sym))
748 continue;
750 if (memory_partition (sym))
751 num_partitioned++;
752 else
753 num_unpartitioned++;
756 if (num_vdefs_p)
757 *num_vdefs_p = num_vdefs;
759 if (num_vuses_p)
760 *num_vuses_p = num_vuses;
762 if (num_partitioned_p)
763 *num_partitioned_p = num_partitioned;
765 if (num_unpartitioned_p)
766 *num_unpartitioned_p = num_unpartitioned;
770 /* The list is sorted by increasing partitioning score (PSCORE).
771 This score is computed such that symbols with high scores are
772 those that are least likely to be partitioned. Given a symbol
773 MP->VAR, PSCORE(S) is the result of the following weighted sum
775 PSCORE(S) = FW * 64 + FR * 32
776 + DW * 16 + DR * 8
777 + IW * 4 + IR * 2
778 + NO_ALIAS
780 where
782 FW Execution frequency of writes to S
783 FR Execution frequency of reads from S
784 DW Number of direct writes to S
785 DR Number of direct reads from S
786 IW Number of indirect writes to S
787 IR Number of indirect reads from S
788 NO_ALIAS State of the NO_ALIAS* flags
790 The basic idea here is that symbols that are frequently
791 written-to in hot paths of the code are the last to be considered
792 for partitioning. */
794 static inline long
795 mem_sym_score (mem_sym_stats_t mp)
797 return mp->frequency_writes * 64 + mp->frequency_reads * 32
798 + mp->num_direct_writes * 16 + mp->num_direct_reads * 8
799 + mp->num_indirect_writes * 4 + mp->num_indirect_reads * 2
800 + var_ann (mp->var)->noalias_state;
804 /* Dump memory reference stats for function CFUN to FILE. */
806 void
807 dump_mem_ref_stats (FILE *file)
809 long actual_num_vuses, actual_num_vdefs;
810 long num_partitioned, num_unpartitioned;
811 struct mem_ref_stats_d *stats;
813 stats = gimple_mem_ref_stats (cfun);
815 count_mem_refs (&actual_num_vuses, &actual_num_vdefs, &num_partitioned,
816 &num_unpartitioned);
818 fprintf (file, "\nMemory reference statistics for %s\n\n",
819 lang_hooks.decl_printable_name (current_function_decl, 2));
821 fprintf (file, "Number of memory statements: %ld\n",
822 stats->num_mem_stmts);
823 fprintf (file, "Number of call sites: %ld\n",
824 stats->num_call_sites);
825 fprintf (file, "Number of pure/const call sites: %ld\n",
826 stats->num_pure_const_call_sites);
827 fprintf (file, "Number of asm sites: %ld\n",
828 stats->num_asm_sites);
829 fprintf (file, "Estimated number of loads: %ld (%ld/stmt)\n",
830 stats->num_vuses,
831 (stats->num_mem_stmts)
832 ? CEIL (stats->num_vuses, stats->num_mem_stmts)
833 : 0);
834 fprintf (file, "Actual number of loads: %ld (%ld/stmt)\n",
835 actual_num_vuses,
836 (stats->num_mem_stmts)
837 ? CEIL (actual_num_vuses, stats->num_mem_stmts)
838 : 0);
840 if (actual_num_vuses > stats->num_vuses + (stats->num_vuses / 25))
841 fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
843 fprintf (file, "Estimated number of stores: %ld (%ld/stmt)\n",
844 stats->num_vdefs,
845 (stats->num_mem_stmts)
846 ? CEIL (stats->num_vdefs, stats->num_mem_stmts)
847 : 0);
848 fprintf (file, "Actual number of stores: %ld (%ld/stmt)\n",
849 actual_num_vdefs,
850 (stats->num_mem_stmts)
851 ? CEIL (actual_num_vdefs, stats->num_mem_stmts)
852 : 0);
854 if (actual_num_vdefs > stats->num_vdefs + (stats->num_vdefs / 25))
855 fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
857 fprintf (file, "Partitioning thresholds: MAX = %d AVG = %d "
858 "(%sNEED TO PARTITION)\n", MAX_ALIASED_VOPS, AVG_ALIASED_VOPS,
859 stats->num_mem_stmts && need_to_partition_p (stats) ? "" : "NO ");
860 fprintf (file, "Number of partitioned symbols: %ld\n", num_partitioned);
861 fprintf (file, "Number of unpartitioned symbols: %ld\n", num_unpartitioned);
865 /* Dump memory reference stats for function FN to stderr. */
867 void
868 debug_mem_ref_stats (void)
870 dump_mem_ref_stats (stderr);
874 /* Dump memory reference stats for variable VAR to FILE. */
876 static void
877 dump_mem_sym_stats (FILE *file, tree var)
879 mem_sym_stats_t stats = mem_sym_stats (cfun, var);
881 if (stats == NULL)
882 return;
884 fprintf (file, "read frequency: %6ld, write frequency: %6ld, "
885 "direct reads: %3ld, direct writes: %3ld, "
886 "indirect reads: %4ld, indirect writes: %4ld, symbol: ",
887 stats->frequency_reads, stats->frequency_writes,
888 stats->num_direct_reads, stats->num_direct_writes,
889 stats->num_indirect_reads, stats->num_indirect_writes);
890 print_generic_expr (file, stats->var, 0);
891 fprintf (file, ", tags: ");
892 dump_decl_set (file, stats->parent_tags);
896 /* Dump memory reference stats for variable VAR to stderr. */
898 void
899 debug_mem_sym_stats (tree var)
901 dump_mem_sym_stats (stderr, var);
904 /* Dump memory reference stats for variable VAR to FILE. For use
905 of tree-dfa.c:dump_variable. */
907 void
908 dump_mem_sym_stats_for_var (FILE *file, tree var)
910 mem_sym_stats_t stats = mem_sym_stats (cfun, var);
912 if (stats == NULL)
913 return;
915 fprintf (file, ", score: %ld", mem_sym_score (stats));
916 fprintf (file, ", direct reads: %ld", stats->num_direct_reads);
917 fprintf (file, ", direct writes: %ld", stats->num_direct_writes);
918 fprintf (file, ", indirect reads: %ld", stats->num_indirect_reads);
919 fprintf (file, ", indirect writes: %ld", stats->num_indirect_writes);
922 /* Dump memory reference stats for all memory symbols to FILE. */
924 static void
925 dump_all_mem_sym_stats (FILE *file)
927 referenced_var_iterator rvi;
928 tree sym;
930 FOR_EACH_REFERENCED_VAR (sym, rvi)
932 if (is_gimple_reg (sym))
933 continue;
935 dump_mem_sym_stats (file, sym);
940 /* Dump memory reference stats for all memory symbols to stderr. */
942 void
943 debug_all_mem_sym_stats (void)
945 dump_all_mem_sym_stats (stderr);
949 /* Dump the MP_INFO array to FILE. */
951 static void
952 dump_mp_info (FILE *file, VEC(mem_sym_stats_t,heap) *mp_info)
954 unsigned i;
955 mem_sym_stats_t mp_p;
957 for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
958 if (!mp_p->partitioned_p)
959 dump_mem_sym_stats (file, mp_p->var);
963 /* Dump the MP_INFO array to stderr. */
965 void
966 debug_mp_info (VEC(mem_sym_stats_t,heap) *mp_info)
968 dump_mp_info (stderr, mp_info);
972 /* Update memory reference stats for symbol VAR in statement STMT.
973 NUM_DIRECT_READS and NUM_DIRECT_WRITES specify the number of times
974 that VAR is read/written in STMT (indirect reads/writes are not
975 recorded by this function, see compute_memory_partitions). */
977 void
978 update_mem_sym_stats_from_stmt (tree var, tree stmt, long num_direct_reads,
979 long num_direct_writes)
981 mem_sym_stats_t stats;
983 gcc_assert (num_direct_reads >= 0 && num_direct_writes >= 0);
985 stats = get_mem_sym_stats_for (var);
987 stats->num_direct_reads += num_direct_reads;
988 stats->frequency_reads += ((long) bb_for_stmt (stmt)->frequency
989 * num_direct_reads);
991 stats->num_direct_writes += num_direct_writes;
992 stats->frequency_writes += ((long) bb_for_stmt (stmt)->frequency
993 * num_direct_writes);
997 /* Given two MP_INFO entries MP1 and MP2, return -1 if MP1->VAR should
998 be partitioned before MP2->VAR, 0 if they are the same or 1 if
999 MP1->VAR should be partitioned after MP2->VAR. */
1001 static inline int
1002 compare_mp_info_entries (mem_sym_stats_t mp1, mem_sym_stats_t mp2)
1004 long pscore1 = mem_sym_score (mp1);
1005 long pscore2 = mem_sym_score (mp2);
1007 if (pscore1 < pscore2)
1008 return -1;
1009 else if (pscore1 > pscore2)
1010 return 1;
1011 else
1012 return DECL_UID (mp1->var) - DECL_UID (mp2->var);
1016 /* Comparison routine for qsort. The list is sorted by increasing
1017 partitioning score (PSCORE). This score is computed such that
1018 symbols with high scores are those that are least likely to be
1019 partitioned. */
1021 static int
1022 mp_info_cmp (const void *p, const void *q)
1024 mem_sym_stats_t e1 = *((const mem_sym_stats_t *) p);
1025 mem_sym_stats_t e2 = *((const mem_sym_stats_t *) q);
1026 return compare_mp_info_entries (e1, e2);
1030 /* Sort the array of reference counts used to compute memory partitions.
1031 Elements are sorted in ascending order of execution frequency and
1032 descending order of virtual operators needed. */
1034 static inline void
1035 sort_mp_info (VEC(mem_sym_stats_t,heap) *list)
1037 unsigned num = VEC_length (mem_sym_stats_t, list);
1039 if (num < 2)
1040 return;
1042 if (num == 2)
1044 if (compare_mp_info_entries (VEC_index (mem_sym_stats_t, list, 0),
1045 VEC_index (mem_sym_stats_t, list, 1)) > 0)
1047 /* Swap elements if they are in the wrong order. */
1048 mem_sym_stats_t tmp = VEC_index (mem_sym_stats_t, list, 0);
1049 VEC_replace (mem_sym_stats_t, list, 0,
1050 VEC_index (mem_sym_stats_t, list, 1));
1051 VEC_replace (mem_sym_stats_t, list, 1, tmp);
1054 return;
1057 /* There are 3 or more elements, call qsort. */
1058 qsort (VEC_address (mem_sym_stats_t, list),
1059 VEC_length (mem_sym_stats_t, list),
1060 sizeof (mem_sym_stats_t),
1061 mp_info_cmp);
1065 /* Return the memory partition tag (MPT) associated with memory
1066 symbol SYM. */
1068 static tree
1069 get_mpt_for (tree sym)
1071 tree mpt;
1073 /* Don't create a new tag unnecessarily. */
1074 mpt = memory_partition (sym);
1075 if (mpt == NULL_TREE)
1077 mpt = create_tag_raw (MEMORY_PARTITION_TAG, TREE_TYPE (sym), "MPT");
1078 TREE_ADDRESSABLE (mpt) = 0;
1079 add_referenced_var (mpt);
1080 VEC_safe_push (tree, heap, gimple_ssa_operands (cfun)->mpt_table, mpt);
1081 gcc_assert (MPT_SYMBOLS (mpt) == NULL);
1082 set_memory_partition (sym, mpt);
1085 return mpt;
1089 /* Add MP_P->VAR to a memory partition and return the partition. */
1091 static tree
1092 find_partition_for (mem_sym_stats_t mp_p)
1094 unsigned i;
1095 VEC(tree,heap) *mpt_table;
1096 tree mpt;
1098 mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1099 mpt = NULL_TREE;
1101 /* Find an existing partition for MP_P->VAR. */
1102 for (i = 0; VEC_iterate (tree, mpt_table, i, mpt); i++)
1104 mem_sym_stats_t mpt_stats;
1106 /* If MPT does not have any symbols yet, use it. */
1107 if (MPT_SYMBOLS (mpt) == NULL)
1108 break;
1110 /* Otherwise, see if MPT has common parent tags with MP_P->VAR,
1111 but avoid grouping clobbered variables with non-clobbered
1112 variables (otherwise, this tends to creates a single memory
1113 partition because other call-clobbered variables may have
1114 common parent tags with non-clobbered ones). */
1115 mpt_stats = get_mem_sym_stats_for (mpt);
1116 if (mp_p->parent_tags
1117 && mpt_stats->parent_tags
1118 && is_call_clobbered (mpt) == is_call_clobbered (mp_p->var)
1119 && bitmap_intersect_p (mpt_stats->parent_tags, mp_p->parent_tags))
1120 break;
1122 /* If no common parent tags are found, see if both MPT and
1123 MP_P->VAR are call-clobbered. */
1124 if (is_call_clobbered (mpt) && is_call_clobbered (mp_p->var))
1125 break;
1128 if (mpt == NULL_TREE)
1129 mpt = get_mpt_for (mp_p->var);
1130 else
1131 set_memory_partition (mp_p->var, mpt);
1133 mp_p->partitioned_p = true;
1135 mark_sym_for_renaming (mp_p->var);
1136 mark_sym_for_renaming (mpt);
1138 return mpt;
1142 /* Rewrite the alias set for TAG to use the newly created partitions.
1143 If TAG is NULL, rewrite the set of call-clobbered variables.
1144 NEW_ALIASES is a scratch bitmap to build the new set of aliases for
1145 TAG. */
1147 static void
1148 rewrite_alias_set_for (tree tag, bitmap new_aliases)
1150 bitmap_iterator bi;
1151 unsigned i;
1152 tree mpt, sym;
1154 EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, i, bi)
1156 sym = referenced_var (i);
1157 mpt = memory_partition (sym);
1158 if (mpt)
1159 bitmap_set_bit (new_aliases, DECL_UID (mpt));
1160 else
1161 bitmap_set_bit (new_aliases, DECL_UID (sym));
1164 /* Rebuild the may-alias array for TAG. */
1165 bitmap_copy (MTAG_ALIASES (tag), new_aliases);
1169 /* Determine how many virtual operands can be saved by partitioning
1170 MP_P->VAR into MPT. When a symbol S is thrown inside a partition
1171 P, every virtual operand that used to reference S will now
1172 reference P. Whether it reduces the number of virtual operands
1173 depends on:
1175 1- Direct references to S are never saved. Instead of the virtual
1176 operand to S, we will now have a virtual operand to P.
1178 2- Indirect references to S are reduced only for those memory tags
1179 holding S that already had other symbols partitioned into P.
1180 For instance, if a memory tag T has the alias set { a b S c },
1181 the first time we partition S into P, the alias set will become
1182 { a b P c }, so no virtual operands will be saved. However, if
1183 we now partition symbol 'c' into P, then the alias set for T
1184 will become { a b P }, so we will be saving one virtual operand
1185 for every indirect reference to 'c'.
1187 3- Is S is call-clobbered, we save as many virtual operands as
1188 call/asm sites exist in the code, but only if other
1189 call-clobbered symbols have been grouped into P. The first
1190 call-clobbered symbol that we group does not produce any
1191 savings.
1193 MEM_REF_STATS points to CFUN's memory reference information. */
1195 static void
1196 estimate_vop_reduction (struct mem_ref_stats_d *mem_ref_stats,
1197 mem_sym_stats_t mp_p, tree mpt)
1199 unsigned i;
1200 bitmap_iterator bi;
1201 mem_sym_stats_t mpt_stats;
1203 /* We should only get symbols with indirect references here. */
1204 gcc_assert (mp_p->num_indirect_reads > 0 || mp_p->num_indirect_writes > 0);
1206 /* Note that the only statistics we keep for MPT is the set of
1207 parent tags to know which memory tags have had alias members
1208 partitioned, and the indicator has_call_clobbered_vars.
1209 Reference counts are not important for MPT. */
1210 mpt_stats = get_mem_sym_stats_for (mpt);
1212 /* Traverse all the parent tags for MP_P->VAR. For every tag T, if
1213 partition P is already grouping aliases of T, then reduce the
1214 number of virtual operands by the number of direct references
1215 to T. */
1216 if (mp_p->parent_tags)
1218 if (mpt_stats->parent_tags == NULL)
1219 mpt_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1221 EXECUTE_IF_SET_IN_BITMAP (mp_p->parent_tags, 0, i, bi)
1223 if (bitmap_bit_p (mpt_stats->parent_tags, i))
1225 /* Partition MPT is already partitioning symbols in the
1226 alias set for TAG. This means that we are now saving
1227 1 virtual operand for every direct reference to TAG. */
1228 tree tag = referenced_var (i);
1229 mem_sym_stats_t tag_stats = mem_sym_stats (cfun, tag);
1230 mem_ref_stats->num_vuses -= tag_stats->num_direct_reads;
1231 mem_ref_stats->num_vdefs -= tag_stats->num_direct_writes;
1233 else
1235 /* This is the first symbol in tag I's alias set that is
1236 being grouped under MPT. We will not save any
1237 virtual operands this time, but record that MPT is
1238 grouping a symbol from TAG's alias set so that the
1239 next time we get the savings. */
1240 bitmap_set_bit (mpt_stats->parent_tags, i);
1245 /* If MP_P->VAR is call-clobbered, and MPT is already grouping
1246 call-clobbered symbols, then we will save as many virtual
1247 operands as asm/call sites there are. */
1248 if (is_call_clobbered (mp_p->var))
1250 if (mpt_stats->has_call_clobbered_vars)
1251 mem_ref_stats->num_vdefs -= mem_ref_stats->num_call_sites
1252 + mem_ref_stats->num_asm_sites;
1253 else
1254 mpt_stats->has_call_clobbered_vars = true;
1259 /* Helper for compute_memory_partitions. Transfer reference counts
1260 from pointers to their pointed-to sets. Counters for pointers were
1261 computed by update_alias_info. MEM_REF_STATS points to CFUN's
1262 memory reference information. */
1264 static void
1265 update_reference_counts (struct mem_ref_stats_d *mem_ref_stats)
1267 unsigned i;
1268 bitmap_iterator bi;
1269 mem_sym_stats_t sym_stats;
1271 for (i = 1; i < num_ssa_names; i++)
1273 tree ptr;
1274 struct ptr_info_def *pi;
1276 ptr = ssa_name (i);
1277 if (ptr
1278 && POINTER_TYPE_P (TREE_TYPE (ptr))
1279 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1280 && pi->memory_tag_needed)
1282 unsigned j;
1283 bitmap_iterator bj;
1284 tree tag;
1285 mem_sym_stats_t ptr_stats, tag_stats;
1287 /* If PTR has flow-sensitive points-to information, use
1288 PTR's name tag, otherwise use the symbol tag associated
1289 with PTR's symbol. */
1290 if (pi->name_mem_tag)
1291 tag = pi->name_mem_tag;
1292 else
1293 tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
1295 ptr_stats = get_mem_sym_stats_for (ptr);
1296 tag_stats = get_mem_sym_stats_for (tag);
1298 /* TAG has as many direct references as dereferences we
1299 found for its parent pointer. */
1300 tag_stats->num_direct_reads += ptr_stats->num_direct_reads;
1301 tag_stats->num_direct_writes += ptr_stats->num_direct_writes;
1303 /* All the dereferences of pointer PTR are considered direct
1304 references to PTR's memory tag (TAG). In turn,
1305 references to TAG will become virtual operands for every
1306 symbol in TAG's alias set. So, for every symbol ALIAS in
1307 TAG's alias set, add as many indirect references to ALIAS
1308 as direct references there are for TAG. */
1309 if (MTAG_ALIASES (tag))
1310 EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, j, bj)
1312 tree alias = referenced_var (j);
1313 sym_stats = get_mem_sym_stats_for (alias);
1315 /* All the direct references to TAG are indirect references
1316 to ALIAS. */
1317 sym_stats->num_indirect_reads += ptr_stats->num_direct_reads;
1318 sym_stats->num_indirect_writes += ptr_stats->num_direct_writes;
1319 sym_stats->frequency_reads += ptr_stats->frequency_reads;
1320 sym_stats->frequency_writes += ptr_stats->frequency_writes;
1322 /* Indicate that TAG is one of ALIAS's parent tags. */
1323 if (sym_stats->parent_tags == NULL)
1324 sym_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1325 bitmap_set_bit (sym_stats->parent_tags, DECL_UID (tag));
1330 /* Call-clobbered symbols are indirectly written at every
1331 call/asm site. */
1332 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1334 tree sym = referenced_var (i);
1335 sym_stats = get_mem_sym_stats_for (sym);
1336 sym_stats->num_indirect_writes += mem_ref_stats->num_call_sites
1337 + mem_ref_stats->num_asm_sites;
1340 /* Addressable symbols are indirectly written at some ASM sites.
1341 Since only ASM sites that clobber memory actually affect
1342 addressable symbols, this is an over-estimation. */
1343 EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, i, bi)
1345 tree sym = referenced_var (i);
1346 sym_stats = get_mem_sym_stats_for (sym);
1347 sym_stats->num_indirect_writes += mem_ref_stats->num_asm_sites;
1352 /* Helper for compute_memory_partitions. Add all memory symbols to
1353 *MP_INFO_P and compute the initial estimate for the total number of
1354 virtual operands needed. MEM_REF_STATS points to CFUN's memory
1355 reference information. On exit, *TAGS_P will contain the list of
1356 memory tags whose alias set need to be rewritten after
1357 partitioning. */
1359 static void
1360 build_mp_info (struct mem_ref_stats_d *mem_ref_stats,
1361 VEC(mem_sym_stats_t,heap) **mp_info_p,
1362 VEC(tree,heap) **tags_p)
1364 tree var;
1365 referenced_var_iterator rvi;
1367 FOR_EACH_REFERENCED_VAR (var, rvi)
1369 mem_sym_stats_t sym_stats;
1370 tree old_mpt;
1372 /* We are only interested in memory symbols other than MPTs. */
1373 if (is_gimple_reg (var) || TREE_CODE (var) == MEMORY_PARTITION_TAG)
1374 continue;
1376 /* Collect memory tags into the TAGS array so that we can
1377 rewrite their alias sets after partitioning. */
1378 if (MTAG_P (var) && MTAG_ALIASES (var))
1379 VEC_safe_push (tree, heap, *tags_p, var);
1381 /* Since we are going to re-compute partitions, any symbols that
1382 used to belong to a partition must be detached from it and
1383 marked for renaming. */
1384 if ((old_mpt = memory_partition (var)) != NULL)
1386 mark_sym_for_renaming (old_mpt);
1387 set_memory_partition (var, NULL_TREE);
1388 mark_sym_for_renaming (var);
1391 sym_stats = get_mem_sym_stats_for (var);
1393 /* Add VAR's reference info to MP_INFO. Note that the only
1394 symbols that make sense to partition are those that have
1395 indirect references. If a symbol S is always directly
1396 referenced, partitioning it will not reduce the number of
1397 virtual operators. The only symbols that are profitable to
1398 partition are those that belong to alias sets and/or are
1399 call-clobbered. */
1400 if (sym_stats->num_indirect_reads > 0
1401 || sym_stats->num_indirect_writes > 0)
1402 VEC_safe_push (mem_sym_stats_t, heap, *mp_info_p, sym_stats);
1404 /* Update the number of estimated VOPS. Note that direct
1405 references to memory tags are always counted as indirect
1406 references to their alias set members, so if a memory tag has
1407 aliases, do not count its direct references to avoid double
1408 accounting. */
1409 if (!MTAG_P (var) || !MTAG_ALIASES (var))
1411 mem_ref_stats->num_vuses += sym_stats->num_direct_reads;
1412 mem_ref_stats->num_vdefs += sym_stats->num_direct_writes;
1415 mem_ref_stats->num_vuses += sym_stats->num_indirect_reads;
1416 mem_ref_stats->num_vdefs += sym_stats->num_indirect_writes;
1421 /* Compute memory partitions. A memory partition (MPT) is an
1422 arbitrary grouping of memory symbols, such that references to one
1423 member of the group is considered a reference to all the members of
1424 the group.
1426 As opposed to alias sets in memory tags, the grouping into
1427 partitions is completely arbitrary and only done to reduce the
1428 number of virtual operands. The only rule that needs to be
1429 observed when creating memory partitions is that given two memory
1430 partitions MPT.i and MPT.j, they must not contain symbols in
1431 common.
1433 Memory partitions are used when putting the program into Memory-SSA
1434 form. In particular, in Memory-SSA PHI nodes are not computed for
1435 individual memory symbols. They are computed for memory
1436 partitions. This reduces the amount of PHI nodes in the SSA graph
1437 at the expense of precision (i.e., it makes unrelated stores affect
1438 each other).
1440 However, it is possible to increase precision by changing this
1441 partitioning scheme. For instance, if the partitioning scheme is
1442 such that get_mpt_for is the identity function (that is,
1443 get_mpt_for (s) = s), this will result in ultimate precision at the
1444 expense of huge SSA webs.
1446 At the other extreme, a partitioning scheme that groups all the
1447 symbols in the same set results in minimal SSA webs and almost
1448 total loss of precision.
1450 There partitioning heuristic uses three parameters to decide the
1451 order in which symbols are processed. The list of symbols is
1452 sorted so that symbols that are more likely to be partitioned are
1453 near the top of the list:
1455 - Execution frequency. If a memory references is in a frequently
1456 executed code path, grouping it into a partition may block useful
1457 transformations and cause sub-optimal code generation. So, the
1458 partition heuristic tries to avoid grouping symbols with high
1459 execution frequency scores. Execution frequency is taken
1460 directly from the basic blocks where every reference is made (see
1461 update_mem_sym_stats_from_stmt), which in turn uses the
1462 profile guided machinery, so if the program is compiled with PGO
1463 enabled, more accurate partitioning decisions will be made.
1465 - Number of references. Symbols with few references in the code,
1466 are partitioned before symbols with many references.
1468 - NO_ALIAS attributes. Symbols with any of the NO_ALIAS*
1469 attributes are partitioned after symbols marked MAY_ALIAS.
1471 Once the list is sorted, the partitioning proceeds as follows:
1473 1- For every symbol S in MP_INFO, create a new memory partition MP,
1474 if necessary. To avoid memory partitions that contain symbols
1475 from non-conflicting alias sets, memory partitions are
1476 associated to the memory tag that holds S in its alias set. So,
1477 when looking for a memory partition for S, the memory partition
1478 associated with one of the memory tags holding S is chosen. If
1479 none exists, a new one is created.
1481 2- Add S to memory partition MP.
1483 3- Reduce by 1 the number of VOPS for every memory tag holding S.
1485 4- If the total number of VOPS is less than MAX_ALIASED_VOPS or the
1486 average number of VOPS per statement is less than
1487 AVG_ALIASED_VOPS, stop. Otherwise, go to the next symbol in the
1488 list. */
1490 static void
1491 compute_memory_partitions (void)
1493 tree tag;
1494 unsigned i;
1495 mem_sym_stats_t mp_p;
1496 VEC(mem_sym_stats_t,heap) *mp_info;
1497 bitmap new_aliases;
1498 VEC(tree,heap) *tags;
1499 struct mem_ref_stats_d *mem_ref_stats;
1500 int prev_max_aliased_vops;
1502 mem_ref_stats = gimple_mem_ref_stats (cfun);
1503 gcc_assert (mem_ref_stats->num_vuses == 0 && mem_ref_stats->num_vdefs == 0);
1505 if (mem_ref_stats->num_mem_stmts == 0)
1506 return;
1508 timevar_push (TV_MEMORY_PARTITIONING);
1510 mp_info = NULL;
1511 tags = NULL;
1512 prev_max_aliased_vops = MAX_ALIASED_VOPS;
1514 /* Since we clearly cannot lower the number of virtual operators
1515 below the total number of memory statements in the function, we
1516 may need to adjust MAX_ALIASED_VOPS beforehand. */
1517 if (MAX_ALIASED_VOPS < mem_ref_stats->num_mem_stmts)
1518 MAX_ALIASED_VOPS = mem_ref_stats->num_mem_stmts;
1520 /* Update reference stats for all the pointed-to variables and
1521 memory tags. */
1522 update_reference_counts (mem_ref_stats);
1524 /* Add all the memory symbols to MP_INFO. */
1525 build_mp_info (mem_ref_stats, &mp_info, &tags);
1527 /* No partitions required if we are below the threshold. */
1528 if (!need_to_partition_p (mem_ref_stats))
1530 if (dump_file)
1531 fprintf (dump_file, "\nMemory partitioning NOT NEEDED for %s\n",
1532 get_name (current_function_decl));
1533 goto done;
1536 /* Sort the MP_INFO array so that symbols that should be partitioned
1537 first are near the top of the list. */
1538 sort_mp_info (mp_info);
1540 if (dump_file)
1542 fprintf (dump_file, "\nMemory partitioning NEEDED for %s\n\n",
1543 get_name (current_function_decl));
1544 fprintf (dump_file, "Memory symbol references before partitioning:\n");
1545 dump_mp_info (dump_file, mp_info);
1548 /* Create partitions for variables in MP_INFO until we have enough
1549 to lower the total number of VOPS below MAX_ALIASED_VOPS or if
1550 the average number of VOPS per statement is below
1551 AVG_ALIASED_VOPS. */
1552 for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
1554 tree mpt;
1556 /* If we are below the threshold, stop. */
1557 if (!need_to_partition_p (mem_ref_stats))
1558 break;
1560 mpt = find_partition_for (mp_p);
1561 estimate_vop_reduction (mem_ref_stats, mp_p, mpt);
1564 /* After partitions have been created, rewrite alias sets to use
1565 them instead of the original symbols. This way, if the alias set
1566 was computed as { a b c d e f }, and the subset { b e f } was
1567 grouped into partition MPT.3, then the new alias set for the tag
1568 will be { a c d MPT.3 }.
1570 Note that this is not strictly necessary. The operand scanner
1571 will always check if a symbol belongs to a partition when adding
1572 virtual operands. However, by reducing the size of the alias
1573 sets to be scanned, the work needed inside the operand scanner is
1574 significantly reduced. */
1575 new_aliases = BITMAP_ALLOC (&alias_bitmap_obstack);
1577 for (i = 0; VEC_iterate (tree, tags, i, tag); i++)
1579 rewrite_alias_set_for (tag, new_aliases);
1580 bitmap_clear (new_aliases);
1583 BITMAP_FREE (new_aliases);
1585 if (dump_file)
1587 fprintf (dump_file, "\nMemory symbol references after partitioning:\n");
1588 dump_mp_info (dump_file, mp_info);
1591 done:
1592 /* Free allocated memory. */
1593 VEC_free (mem_sym_stats_t, heap, mp_info);
1594 VEC_free (tree, heap, tags);
1596 MAX_ALIASED_VOPS = prev_max_aliased_vops;
1598 timevar_pop (TV_MEMORY_PARTITIONING);
1602 /* Compute may-alias information for every variable referenced in function
1603 FNDECL.
1605 Alias analysis proceeds in 3 main phases:
1607 1- Points-to and escape analysis.
1609 This phase walks the use-def chains in the SSA web looking for three
1610 things:
1612 * Assignments of the form P_i = &VAR
1613 * Assignments of the form P_i = malloc()
1614 * Pointers and ADDR_EXPR that escape the current function.
1616 The concept of 'escaping' is the same one used in the Java world. When
1617 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
1618 outside of the current function. So, assignment to global variables,
1619 function arguments and returning a pointer are all escape sites, as are
1620 conversions between pointers and integers.
1622 This is where we are currently limited. Since not everything is renamed
1623 into SSA, we lose track of escape properties when a pointer is stashed
1624 inside a field in a structure, for instance. In those cases, we are
1625 assuming that the pointer does escape.
1627 We use escape analysis to determine whether a variable is
1628 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
1629 is call-clobbered. If a pointer P_i escapes, then all the variables
1630 pointed-to by P_i (and its memory tag) also escape.
1632 2- Compute flow-sensitive aliases
1634 We have two classes of memory tags. Memory tags associated with the
1635 pointed-to data type of the pointers in the program. These tags are
1636 called "symbol memory tag" (SMT). The other class are those associated
1637 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
1638 when adding operands for an INDIRECT_REF *P_i, we will first check
1639 whether P_i has a name tag, if it does we use it, because that will have
1640 more precise aliasing information. Otherwise, we use the standard symbol
1641 tag.
1643 In this phase, we go through all the pointers we found in points-to
1644 analysis and create alias sets for the name memory tags associated with
1645 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
1646 it points to and its tag.
1649 3- Compute flow-insensitive aliases
1651 This pass will compare the alias set of every symbol memory tag and
1652 every addressable variable found in the program. Given a symbol
1653 memory tag SMT and an addressable variable V. If the alias sets of
1654 SMT and V conflict (as computed by may_alias_p), then V is marked
1655 as an alias tag and added to the alias set of SMT.
1657 For instance, consider the following function:
1659 foo (int i)
1661 int *p, a, b;
1663 if (i > 10)
1664 p = &a;
1665 else
1666 p = &b;
1668 *p = 3;
1669 a = b + 2;
1670 return *p;
1673 After aliasing analysis has finished, the symbol memory tag for pointer
1674 'p' will have two aliases, namely variables 'a' and 'b'. Every time
1675 pointer 'p' is dereferenced, we want to mark the operation as a
1676 potential reference to 'a' and 'b'.
1678 foo (int i)
1680 int *p, a, b;
1682 if (i_2 > 10)
1683 p_4 = &a;
1684 else
1685 p_6 = &b;
1686 # p_1 = PHI <p_4(1), p_6(2)>;
1688 # a_7 = VDEF <a_3>;
1689 # b_8 = VDEF <b_5>;
1690 *p_1 = 3;
1692 # a_9 = VDEF <a_7>
1693 # VUSE <b_8>
1694 a_9 = b_8 + 2;
1696 # VUSE <a_9>;
1697 # VUSE <b_8>;
1698 return *p_1;
1701 In certain cases, the list of may aliases for a pointer may grow too
1702 large. This may cause an explosion in the number of virtual operands
1703 inserted in the code. Resulting in increased memory consumption and
1704 compilation time.
1706 When the number of virtual operands needed to represent aliased
1707 loads and stores grows too large (configurable with option --param
1708 max-aliased-vops and --param avg-aliased-vops), alias sets are
1709 grouped to avoid severe compile-time slow downs and memory
1710 consumption. See compute_memory_partitions. */
1712 unsigned int
1713 compute_may_aliases (void)
1715 struct alias_info *ai;
1717 timevar_push (TV_TREE_MAY_ALIAS);
1719 memset (&alias_stats, 0, sizeof (alias_stats));
1721 /* Initialize aliasing information. */
1722 ai = init_alias_info ();
1724 /* For each pointer P_i, determine the sets of variables that P_i may
1725 point-to. For every addressable variable V, determine whether the
1726 address of V escapes the current function, making V call-clobbered
1727 (i.e., whether &V is stored in a global variable or if its passed as a
1728 function call argument). */
1729 compute_points_to_sets (ai);
1731 /* Collect all pointers and addressable variables, compute alias sets,
1732 create memory tags for pointers and promote variables whose address is
1733 not needed anymore. */
1734 setup_pointers_and_addressables (ai);
1736 /* Compute type-based flow-insensitive aliasing for all the type
1737 memory tags. */
1738 compute_flow_insensitive_aliasing (ai);
1740 /* Compute flow-sensitive, points-to based aliasing for all the name
1741 memory tags. */
1742 compute_flow_sensitive_aliasing (ai);
1744 /* Compute call clobbering information. */
1745 compute_call_clobbered (ai);
1747 /* If the program makes no reference to global variables, but it
1748 contains a mixture of pure and non-pure functions, then we need
1749 to create use-def and def-def links between these functions to
1750 avoid invalid transformations on them. */
1751 maybe_create_global_var ();
1753 /* Compute memory partitions for every memory variable. */
1754 compute_memory_partitions ();
1756 /* Remove partitions with no symbols. Partitions may end up with an
1757 empty MPT_SYMBOLS set if a previous round of alias analysis
1758 needed to partition more symbols. Since we don't need those
1759 partitions anymore, remove them to free up the space. */
1761 tree mpt;
1762 unsigned i;
1763 VEC(tree,heap) *mpt_table;
1765 mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1766 i = 0;
1767 while (i < VEC_length (tree, mpt_table))
1769 mpt = VEC_index (tree, mpt_table, i);
1770 if (MPT_SYMBOLS (mpt) == NULL)
1771 VEC_unordered_remove (tree, mpt_table, i);
1772 else
1773 i++;
1777 /* Populate all virtual operands and newly promoted register operands. */
1779 block_stmt_iterator bsi;
1780 basic_block bb;
1781 FOR_EACH_BB (bb)
1782 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1783 update_stmt_if_modified (bsi_stmt (bsi));
1786 /* Debugging dumps. */
1787 if (dump_file)
1789 dump_mem_ref_stats (dump_file);
1790 dump_alias_info (dump_file);
1791 dump_points_to_info (dump_file);
1793 if (dump_flags & TDF_STATS)
1794 dump_alias_stats (dump_file);
1796 if (dump_flags & TDF_DETAILS)
1797 dump_referenced_vars (dump_file);
1800 /* Report strict aliasing violations. */
1801 strict_aliasing_warning_backend ();
1803 /* Deallocate memory used by aliasing data structures. */
1804 delete_alias_info (ai);
1806 if (need_ssa_update_p ())
1807 update_ssa (TODO_update_ssa);
1809 timevar_pop (TV_TREE_MAY_ALIAS);
1811 return 0;
1814 /* Data structure used to count the number of dereferences to PTR
1815 inside an expression. */
1816 struct count_ptr_d
1818 tree ptr;
1819 unsigned count;
1823 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
1824 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
1826 static tree
1827 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
1829 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
1831 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
1832 pointer 'ptr' is *not* dereferenced, it is simply used to compute
1833 the address of 'fld' as 'ptr + offsetof(fld)'. */
1834 if (TREE_CODE (*tp) == ADDR_EXPR)
1836 *walk_subtrees = 0;
1837 return NULL_TREE;
1840 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
1841 count_p->count++;
1843 return NULL_TREE;
1847 /* Count the number of direct and indirect uses for pointer PTR in
1848 statement STMT. The number of direct uses is stored in
1849 *NUM_USES_P. Indirect references are counted separately depending
1850 on whether they are store or load operations. The counts are
1851 stored in *NUM_STORES_P and *NUM_LOADS_P. */
1853 void
1854 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
1855 unsigned *num_loads_p, unsigned *num_stores_p)
1857 ssa_op_iter i;
1858 tree use;
1860 *num_uses_p = 0;
1861 *num_loads_p = 0;
1862 *num_stores_p = 0;
1864 /* Find out the total number of uses of PTR in STMT. */
1865 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
1866 if (use == ptr)
1867 (*num_uses_p)++;
1869 /* Now count the number of indirect references to PTR. This is
1870 truly awful, but we don't have much choice. There are no parent
1871 pointers inside INDIRECT_REFs, so an expression like
1872 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
1873 find all the indirect and direct uses of x_1 inside. The only
1874 shortcut we can take is the fact that GIMPLE only allows
1875 INDIRECT_REFs inside the expressions below. */
1876 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1877 || (TREE_CODE (stmt) == RETURN_EXPR
1878 && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
1879 || TREE_CODE (stmt) == ASM_EXPR
1880 || TREE_CODE (stmt) == CALL_EXPR)
1882 tree lhs, rhs;
1884 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
1886 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1887 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1889 else if (TREE_CODE (stmt) == RETURN_EXPR)
1891 tree e = TREE_OPERAND (stmt, 0);
1892 lhs = GIMPLE_STMT_OPERAND (e, 0);
1893 rhs = GIMPLE_STMT_OPERAND (e, 1);
1895 else if (TREE_CODE (stmt) == ASM_EXPR)
1897 lhs = ASM_OUTPUTS (stmt);
1898 rhs = ASM_INPUTS (stmt);
1900 else
1902 lhs = NULL_TREE;
1903 rhs = stmt;
1906 if (lhs
1907 && (TREE_CODE (lhs) == TREE_LIST
1908 || EXPR_P (lhs)
1909 || GIMPLE_STMT_P (lhs)))
1911 struct count_ptr_d count;
1912 count.ptr = ptr;
1913 count.count = 0;
1914 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
1915 *num_stores_p = count.count;
1918 if (rhs
1919 && (TREE_CODE (rhs) == TREE_LIST
1920 || EXPR_P (rhs)
1921 || GIMPLE_STMT_P (rhs)))
1923 struct count_ptr_d count;
1924 count.ptr = ptr;
1925 count.count = 0;
1926 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
1927 *num_loads_p = count.count;
1931 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
1934 /* Remove memory references stats for function FN. */
1936 void
1937 delete_mem_ref_stats (struct function *fn)
1939 if (gimple_mem_ref_stats (fn)->mem_sym_stats)
1941 free_alloc_pool (mem_sym_stats_pool);
1942 pointer_map_destroy (gimple_mem_ref_stats (fn)->mem_sym_stats);
1944 gimple_mem_ref_stats (fn)->mem_sym_stats = NULL;
1948 /* Initialize memory reference stats. */
1950 static void
1951 init_mem_ref_stats (void)
1953 struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
1955 mem_sym_stats_pool = create_alloc_pool ("Mem sym stats",
1956 sizeof (struct mem_sym_stats_d),
1957 100);
1958 memset (mem_ref_stats, 0, sizeof (struct mem_ref_stats_d));
1959 mem_ref_stats->mem_sym_stats = pointer_map_create ();
1963 /* Helper for init_alias_info. Reset existing aliasing information. */
1965 static void
1966 reset_alias_info (void)
1968 referenced_var_iterator rvi;
1969 tree var;
1970 unsigned i;
1971 bitmap active_nmts, all_nmts;
1973 /* Clear the set of addressable variables. We do not need to clear
1974 the TREE_ADDRESSABLE bit on every symbol because we are going to
1975 re-compute addressability here. */
1976 bitmap_clear (gimple_addressable_vars (cfun));
1978 active_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
1979 all_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
1981 /* Clear flow-insensitive alias information from each symbol. */
1982 FOR_EACH_REFERENCED_VAR (var, rvi)
1984 if (is_gimple_reg (var))
1985 continue;
1987 if (MTAG_P (var))
1988 MTAG_ALIASES (var) = NULL;
1990 /* Memory partition information will be computed from scratch. */
1991 if (TREE_CODE (var) == MEMORY_PARTITION_TAG)
1992 MPT_SYMBOLS (var) = NULL;
1994 /* Collect all the name tags to determine if we have any
1995 orphaned that need to be removed from the IL. A name tag
1996 will be orphaned if it is not associated with any active SSA
1997 name. */
1998 if (TREE_CODE (var) == NAME_MEMORY_TAG)
1999 bitmap_set_bit (all_nmts, DECL_UID (var));
2001 /* Since we are about to re-discover call-clobbered
2002 variables, clear the call-clobbered flag. */
2003 clear_call_clobbered (var);
2006 /* There should be no call-clobbered variable left. */
2007 gcc_assert (bitmap_empty_p (gimple_call_clobbered_vars (cfun)));
2009 /* Clear flow-sensitive points-to information from each SSA name. */
2010 for (i = 1; i < num_ssa_names; i++)
2012 tree name = ssa_name (i);
2014 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
2015 continue;
2017 if (SSA_NAME_PTR_INFO (name))
2019 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
2021 /* Clear all the flags but keep the name tag to
2022 avoid creating new temporaries unnecessarily. If
2023 this pointer is found to point to a subset or
2024 superset of its former points-to set, then a new
2025 tag will need to be created in create_name_tags. */
2026 pi->pt_anything = 0;
2027 pi->pt_null = 0;
2028 pi->value_escapes_p = 0;
2029 pi->memory_tag_needed = 0;
2030 pi->is_dereferenced = 0;
2031 if (pi->pt_vars)
2032 bitmap_clear (pi->pt_vars);
2034 /* Add NAME's name tag to the set of active tags. */
2035 if (pi->name_mem_tag)
2036 bitmap_set_bit (active_nmts, DECL_UID (pi->name_mem_tag));
2040 /* Name memory tags that are no longer associated with an SSA name
2041 are considered stale and should be removed from the IL. All the
2042 name tags that are in the set ALL_NMTS but not in ACTIVE_NMTS are
2043 considered stale and marked for renaming. */
2044 bitmap_and_compl_into (all_nmts, active_nmts);
2045 mark_set_for_renaming (all_nmts);
2047 BITMAP_FREE (all_nmts);
2048 BITMAP_FREE (active_nmts);
2052 /* Initialize the data structures used for alias analysis. */
2054 static struct alias_info *
2055 init_alias_info (void)
2057 struct alias_info *ai;
2058 referenced_var_iterator rvi;
2059 tree var;
2061 ai = XCNEW (struct alias_info);
2062 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
2063 sbitmap_zero (ai->ssa_names_visited);
2064 ai->processed_ptrs = VEC_alloc (tree, heap, 50);
2065 ai->written_vars = pointer_set_create ();
2066 ai->dereferenced_ptrs_store = pointer_set_create ();
2067 ai->dereferenced_ptrs_load = pointer_set_create ();
2069 /* Clear out all memory reference stats. */
2070 init_mem_ref_stats ();
2072 /* If aliases have been computed before, clear existing information. */
2073 if (gimple_aliases_computed_p (cfun))
2074 reset_alias_info ();
2075 else
2077 /* If this is the first time we compute aliasing information,
2078 every non-register symbol will need to be put into SSA form
2079 (the initial SSA form only operates on GIMPLE registers). */
2080 FOR_EACH_REFERENCED_VAR (var, rvi)
2081 if (!is_gimple_reg (var))
2082 mark_sym_for_renaming (var);
2085 /* Next time, we will need to reset alias information. */
2086 cfun->gimple_df->aliases_computed_p = true;
2087 if (alias_bitmap_obstack.elements != NULL)
2088 bitmap_obstack_release (&alias_bitmap_obstack);
2089 bitmap_obstack_initialize (&alias_bitmap_obstack);
2091 return ai;
2095 /* Deallocate memory used by alias analysis. */
2097 static void
2098 delete_alias_info (struct alias_info *ai)
2100 size_t i;
2102 sbitmap_free (ai->ssa_names_visited);
2104 VEC_free (tree, heap, ai->processed_ptrs);
2106 for (i = 0; i < ai->num_addressable_vars; i++)
2107 free (ai->addressable_vars[i]);
2108 free (ai->addressable_vars);
2110 for (i = 0; i < ai->num_pointers; i++)
2111 free (ai->pointers[i]);
2112 free (ai->pointers);
2114 pointer_set_destroy (ai->written_vars);
2115 pointer_set_destroy (ai->dereferenced_ptrs_store);
2116 pointer_set_destroy (ai->dereferenced_ptrs_load);
2117 free (ai);
2119 delete_mem_ref_stats (cfun);
2120 delete_points_to_sets ();
2124 /* Used for hashing to identify pointer infos with identical
2125 pt_vars bitmaps. */
2127 static int
2128 eq_ptr_info (const void *p1, const void *p2)
2130 const struct ptr_info_def *n1 = (const struct ptr_info_def *) p1;
2131 const struct ptr_info_def *n2 = (const struct ptr_info_def *) p2;
2132 return bitmap_equal_p (n1->pt_vars, n2->pt_vars);
2135 static hashval_t
2136 ptr_info_hash (const void *p)
2138 const struct ptr_info_def *n = (const struct ptr_info_def *) p;
2139 return bitmap_hash (n->pt_vars);
2143 /* Create name tags for all the pointers that have been dereferenced.
2144 We only create a name tag for a pointer P if P is found to point to
2145 a set of variables (so that we can alias them to *P) or if it is
2146 the result of a call to malloc (which means that P cannot point to
2147 anything else nor alias any other variable).
2149 If two pointers P and Q point to the same set of variables, they
2150 are assigned the same name tag. */
2152 static void
2153 create_name_tags (void)
2155 size_t i;
2156 VEC (tree, heap) *with_ptvars = NULL;
2157 tree ptr;
2158 htab_t ptr_hash;
2160 /* Collect the list of pointers with a non-empty points to set. */
2161 for (i = 1; i < num_ssa_names; i++)
2163 tree ptr = ssa_name (i);
2164 struct ptr_info_def *pi;
2166 if (!ptr
2167 || !POINTER_TYPE_P (TREE_TYPE (ptr))
2168 || !SSA_NAME_PTR_INFO (ptr))
2169 continue;
2171 pi = SSA_NAME_PTR_INFO (ptr);
2173 if (pi->pt_anything || !pi->memory_tag_needed)
2175 /* No name tags for pointers that have not been
2176 dereferenced or point to an arbitrary location. */
2177 pi->name_mem_tag = NULL_TREE;
2178 continue;
2181 /* Set pt_anything on the pointers without pt_vars filled in so
2182 that they are assigned a symbol tag. */
2183 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
2184 VEC_safe_push (tree, heap, with_ptvars, ptr);
2185 else
2186 set_pt_anything (ptr);
2189 /* If we didn't find any pointers with pt_vars set, we're done. */
2190 if (!with_ptvars)
2191 return;
2193 ptr_hash = htab_create (10, ptr_info_hash, eq_ptr_info, NULL);
2195 /* Now go through the pointers with pt_vars, and find a name tag
2196 with the same pt_vars as this pointer, or create one if one
2197 doesn't exist. */
2198 for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
2200 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2201 tree old_name_tag = pi->name_mem_tag;
2202 struct ptr_info_def **slot;
2204 /* If PTR points to a set of variables, check if we don't
2205 have another pointer Q with the same points-to set before
2206 creating a tag. If so, use Q's tag instead of creating a
2207 new one.
2209 This is important for not creating unnecessary symbols
2210 and also for copy propagation. If we ever need to
2211 propagate PTR into Q or vice-versa, we would run into
2212 problems if they both had different name tags because
2213 they would have different SSA version numbers (which
2214 would force us to take the name tags in and out of SSA). */
2215 slot = (struct ptr_info_def **) htab_find_slot (ptr_hash, pi, INSERT);
2216 if (*slot)
2217 pi->name_mem_tag = (*slot)->name_mem_tag;
2218 else
2220 *slot = pi;
2222 /* If we didn't find a pointer with the same points-to set
2223 as PTR, create a new name tag if needed. */
2224 if (pi->name_mem_tag == NULL_TREE)
2225 pi->name_mem_tag = get_nmt_for (ptr);
2228 /* If the new name tag computed for PTR is different than
2229 the old name tag that it used to have, then the old tag
2230 needs to be removed from the IL, so we mark it for
2231 renaming. */
2232 if (old_name_tag && old_name_tag != pi->name_mem_tag)
2233 mark_sym_for_renaming (old_name_tag);
2235 /* Inherit volatility from the pointed-to type. */
2236 TREE_THIS_VOLATILE (pi->name_mem_tag)
2237 |= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
2239 /* Mark the new name tag for renaming. */
2240 mark_sym_for_renaming (pi->name_mem_tag);
2243 htab_delete (ptr_hash);
2245 VEC_free (tree, heap, with_ptvars);
2249 /* Union the alias set SET into the may-aliases for TAG. */
2251 static void
2252 union_alias_set_into (tree tag, bitmap set)
2254 bitmap ma = MTAG_ALIASES (tag);
2256 if (bitmap_empty_p (set))
2257 return;
2259 if (!ma)
2260 ma = MTAG_ALIASES (tag) = BITMAP_ALLOC (&alias_bitmap_obstack);
2261 bitmap_ior_into (ma, set);
2265 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
2266 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
2267 name tag and the variables it points-to are call-clobbered. Finally, if
2268 P_i escapes and we could not determine where it points to, then all the
2269 variables in the same alias set as *P_i are marked call-clobbered. This
2270 is necessary because we must assume that P_i may take the address of any
2271 variable in the same alias set. */
2273 static void
2274 compute_flow_sensitive_aliasing (struct alias_info *ai)
2276 size_t i;
2277 tree ptr;
2279 timevar_push (TV_FLOW_SENSITIVE);
2280 set_used_smts ();
2282 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2284 if (!find_what_p_points_to (ptr))
2285 set_pt_anything (ptr);
2288 create_name_tags ();
2290 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2292 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2294 /* Set up aliasing information for PTR's name memory tag (if it has
2295 one). Note that only pointers that have been dereferenced will
2296 have a name memory tag. */
2297 if (pi->name_mem_tag && pi->pt_vars)
2299 if (!bitmap_empty_p (pi->pt_vars))
2300 union_alias_set_into (pi->name_mem_tag, pi->pt_vars);
2303 timevar_pop (TV_FLOW_SENSITIVE);
2307 /* Return TRUE if at least one symbol in TAG2's alias set is also
2308 present in TAG1's alias set. */
2310 static bool
2311 have_common_aliases_p (bitmap tag1aliases, bitmap tag2aliases)
2314 /* This is the old behavior of have_common_aliases_p, which is to
2315 return false if both sets are empty, or one set is and the other
2316 isn't. */
2317 if (tag1aliases == NULL || tag2aliases == NULL)
2318 return false;
2320 return bitmap_intersect_p (tag1aliases, tag2aliases);
2323 /* Compute type-based alias sets. Traverse all the pointers and
2324 addressable variables found in setup_pointers_and_addressables.
2326 For every pointer P in AI->POINTERS and addressable variable V in
2327 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's symbol
2328 memory tag (SMT) if their alias sets conflict. V is then marked as
2329 an aliased symbol so that the operand scanner knows that statements
2330 containing V have aliased operands. */
2332 static void
2333 compute_flow_insensitive_aliasing (struct alias_info *ai)
2335 size_t i;
2337 timevar_push (TV_FLOW_INSENSITIVE);
2338 /* For every pointer P, determine which addressable variables may alias
2339 with P's symbol memory tag. */
2340 for (i = 0; i < ai->num_pointers; i++)
2342 size_t j;
2343 struct alias_map_d *p_map = ai->pointers[i];
2344 tree tag = symbol_mem_tag (p_map->var);
2345 tree var;
2347 for (j = 0; j < ai->num_addressable_vars; j++)
2349 struct alias_map_d *v_map;
2350 var_ann_t v_ann;
2351 bool tag_stored_p, var_stored_p;
2353 v_map = ai->addressable_vars[j];
2354 var = v_map->var;
2355 v_ann = var_ann (var);
2357 /* Skip memory tags and variables that have never been
2358 written to. We also need to check if the variables are
2359 call-clobbered because they may be overwritten by
2360 function calls. */
2361 tag_stored_p = pointer_set_contains (ai->written_vars, tag)
2362 || is_call_clobbered (tag);
2363 var_stored_p = pointer_set_contains (ai->written_vars, var)
2364 || is_call_clobbered (var);
2365 if (!tag_stored_p && !var_stored_p)
2366 continue;
2368 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
2370 /* Add VAR to TAG's may-aliases set. */
2371 add_may_alias (tag, var);
2376 /* Since this analysis is based exclusively on symbols, it fails to
2377 handle cases where two pointers P and Q have different memory
2378 tags with conflicting alias set numbers but no aliased symbols in
2379 common.
2381 For example, suppose that we have two memory tags SMT.1 and SMT.2
2382 such that
2384 may-aliases (SMT.1) = { a }
2385 may-aliases (SMT.2) = { b }
2387 and the alias set number of SMT.1 conflicts with that of SMT.2.
2388 Since they don't have symbols in common, loads and stores from
2389 SMT.1 and SMT.2 will seem independent of each other, which will
2390 lead to the optimizers making invalid transformations (see
2391 testsuite/gcc.c-torture/execute/pr15262-[12].c).
2393 To avoid this problem, we do a final traversal of AI->POINTERS
2394 looking for pairs of pointers that have no aliased symbols in
2395 common and yet have conflicting alias set numbers. */
2396 for (i = 0; i < ai->num_pointers; i++)
2398 size_t j;
2399 struct alias_map_d *p_map1 = ai->pointers[i];
2400 tree tag1 = symbol_mem_tag (p_map1->var);
2401 bitmap may_aliases1 = MTAG_ALIASES (tag1);
2403 for (j = 0; j < ai->num_pointers; j++)
2405 struct alias_map_d *p_map2 = ai->pointers[j];
2406 tree tag2 = symbol_mem_tag (p_map2->var);
2407 bitmap may_aliases2 = may_aliases (tag2);
2409 /* By convention tags don't alias themselves. */
2410 if (tag1 == tag2)
2411 continue;
2413 /* If the pointers may not point to each other, do nothing. */
2414 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
2415 continue;
2417 /* The two pointers may alias each other. If they already have
2418 symbols in common, do nothing. */
2419 if (have_common_aliases_p (may_aliases1, may_aliases2))
2420 continue;
2422 add_may_alias (tag1, tag2);
2425 timevar_pop (TV_FLOW_INSENSITIVE);
2429 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
2431 static void
2432 create_alias_map_for (tree var, struct alias_info *ai)
2434 struct alias_map_d *alias_map;
2435 alias_map = XCNEW (struct alias_map_d);
2436 alias_map->var = var;
2437 alias_map->set = get_alias_set (var);
2438 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
2442 /* Create memory tags for all the dereferenced pointers and build the
2443 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
2444 sets. Based on the address escape and points-to information collected
2445 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
2446 variables whose address is not needed anymore. */
2448 static void
2449 setup_pointers_and_addressables (struct alias_info *ai)
2451 size_t num_addressable_vars, num_pointers;
2452 referenced_var_iterator rvi;
2453 tree var;
2454 VEC (tree, heap) *varvec = NULL;
2455 safe_referenced_var_iterator srvi;
2457 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
2458 num_addressable_vars = num_pointers = 0;
2460 FOR_EACH_REFERENCED_VAR (var, rvi)
2462 if (may_be_aliased (var))
2463 num_addressable_vars++;
2465 if (POINTER_TYPE_P (TREE_TYPE (var)))
2467 /* Since we don't keep track of volatile variables, assume that
2468 these pointers are used in indirect store operations. */
2469 if (TREE_THIS_VOLATILE (var))
2470 pointer_set_insert (ai->dereferenced_ptrs_store, var);
2472 num_pointers++;
2476 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
2477 always going to be slightly bigger than we actually need them
2478 because some TREE_ADDRESSABLE variables will be marked
2479 non-addressable below and only pointers with unique symbol tags are
2480 going to be added to POINTERS. */
2481 ai->addressable_vars = XCNEWVEC (struct alias_map_d *, num_addressable_vars);
2482 ai->pointers = XCNEWVEC (struct alias_map_d *, num_pointers);
2483 ai->num_addressable_vars = 0;
2484 ai->num_pointers = 0;
2486 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
2488 /* Name memory tags already have flow-sensitive aliasing
2489 information, so they need not be processed by
2490 compute_flow_insensitive_aliasing. Similarly, symbol memory
2491 tags are already accounted for when we process their
2492 associated pointer.
2494 Structure fields, on the other hand, have to have some of this
2495 information processed for them, but it's pointless to mark them
2496 non-addressable (since they are fake variables anyway). */
2497 if (MTAG_P (var))
2498 continue;
2500 /* Remove the ADDRESSABLE flag from every addressable variable whose
2501 address is not needed anymore. This is caused by the propagation
2502 of ADDR_EXPR constants into INDIRECT_REF expressions and the
2503 removal of dead pointer assignments done by the early scalar
2504 cleanup passes. */
2505 if (TREE_ADDRESSABLE (var))
2507 if (!bitmap_bit_p (gimple_addressable_vars (cfun), DECL_UID (var))
2508 && TREE_CODE (var) != RESULT_DECL
2509 && !is_global_var (var))
2511 bool okay_to_mark = true;
2513 /* Since VAR is now a regular GIMPLE register, we will need
2514 to rename VAR into SSA afterwards. */
2515 mark_sym_for_renaming (var);
2517 /* The address of VAR is not needed, remove the
2518 addressable bit, so that it can be optimized as a
2519 regular variable. */
2520 if (okay_to_mark)
2522 /* The memory partition holding VAR will no longer
2523 contain VAR, and statements referencing it will need
2524 to be updated. */
2525 if (memory_partition (var))
2526 mark_sym_for_renaming (memory_partition (var));
2528 mark_non_addressable (var);
2533 /* Global variables and addressable locals may be aliased. Create an
2534 entry in ADDRESSABLE_VARS for VAR. */
2535 if (may_be_aliased (var))
2537 create_alias_map_for (var, ai);
2538 mark_sym_for_renaming (var);
2541 /* Add pointer variables that have been dereferenced to the POINTERS
2542 array and create a symbol memory tag for them. */
2543 if (POINTER_TYPE_P (TREE_TYPE (var)))
2545 if ((pointer_set_contains (ai->dereferenced_ptrs_store, var)
2546 || pointer_set_contains (ai->dereferenced_ptrs_load, var)))
2548 tree tag, old_tag;
2549 var_ann_t t_ann;
2551 /* If pointer VAR still doesn't have a memory tag
2552 associated with it, create it now or re-use an
2553 existing one. */
2554 tag = get_smt_for (var, ai);
2555 t_ann = var_ann (tag);
2557 /* The symbol tag will need to be renamed into SSA
2558 afterwards. Note that we cannot do this inside
2559 get_smt_for because aliasing may run multiple times
2560 and we only create symbol tags the first time. */
2561 mark_sym_for_renaming (tag);
2563 /* Similarly, if pointer VAR used to have another type
2564 tag, we will need to process it in the renamer to
2565 remove the stale virtual operands. */
2566 old_tag = symbol_mem_tag (var);
2567 if (old_tag)
2568 mark_sym_for_renaming (old_tag);
2570 /* Associate the tag with pointer VAR. */
2571 set_symbol_mem_tag (var, tag);
2573 /* If pointer VAR has been used in a store operation,
2574 then its memory tag must be marked as written-to. */
2575 if (pointer_set_contains (ai->dereferenced_ptrs_store, var))
2576 pointer_set_insert (ai->written_vars, tag);
2578 else
2580 /* The pointer has not been dereferenced. If it had a
2581 symbol memory tag, remove it and mark the old tag for
2582 renaming to remove it out of the IL. */
2583 tree tag = symbol_mem_tag (var);
2584 if (tag)
2586 mark_sym_for_renaming (tag);
2587 set_symbol_mem_tag (var, NULL_TREE);
2593 VEC_free (tree, heap, varvec);
2597 /* Determine whether to use .GLOBAL_VAR to model call clobbering
2598 semantics. If the function makes no references to global
2599 variables and contains at least one call to a non-pure function,
2600 then we need to mark the side-effects of the call using .GLOBAL_VAR
2601 to represent all possible global memory referenced by the callee. */
2603 static void
2604 maybe_create_global_var (void)
2606 /* No need to create it, if we have one already. */
2607 if (gimple_global_var (cfun) == NULL_TREE)
2609 struct mem_ref_stats_d *stats = gimple_mem_ref_stats (cfun);
2611 /* Create .GLOBAL_VAR if there are no call-clobbered
2612 variables and the program contains a mixture of pure/const
2613 and regular function calls. This is to avoid the problem
2614 described in PR 20115:
2616 int X;
2617 int func_pure (void) { return X; }
2618 int func_non_pure (int a) { X += a; }
2619 int foo ()
2621 int a = func_pure ();
2622 func_non_pure (a);
2623 a = func_pure ();
2624 return a;
2627 Since foo() has no call-clobbered variables, there is
2628 no relationship between the calls to func_pure and
2629 func_non_pure. Since func_pure has no side-effects, value
2630 numbering optimizations elide the second call to func_pure.
2631 So, if we have some pure/const and some regular calls in the
2632 program we create .GLOBAL_VAR to avoid missing these
2633 relations. */
2634 if (bitmap_empty_p (gimple_call_clobbered_vars (cfun))
2635 && stats->num_call_sites > 0
2636 && stats->num_pure_const_call_sites > 0
2637 && stats->num_call_sites > stats->num_pure_const_call_sites)
2638 create_global_var ();
2643 /* Return TRUE if pointer PTR may point to variable VAR.
2645 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
2646 This is needed because when checking for type conflicts we are
2647 interested in the alias set of the memory location pointed-to by
2648 PTR. The alias set of PTR itself is irrelevant.
2650 VAR_ALIAS_SET is the alias set for VAR. */
2652 bool
2653 may_alias_p (tree ptr, alias_set_type mem_alias_set,
2654 tree var, alias_set_type var_alias_set,
2655 bool alias_set_only)
2657 tree mem;
2659 alias_stats.alias_queries++;
2660 alias_stats.simple_queries++;
2662 /* By convention, a variable cannot alias itself. */
2663 mem = symbol_mem_tag (ptr);
2664 if (mem == var)
2666 alias_stats.alias_noalias++;
2667 alias_stats.simple_resolved++;
2668 return false;
2671 /* If -fargument-noalias-global is > 2, pointer arguments may
2672 not point to anything else. */
2673 if (flag_argument_noalias > 2 && TREE_CODE (ptr) == PARM_DECL)
2675 alias_stats.alias_noalias++;
2676 alias_stats.simple_resolved++;
2677 return false;
2680 /* If -fargument-noalias-global is > 1, pointer arguments may
2681 not point to global variables. */
2682 if (flag_argument_noalias > 1 && is_global_var (var)
2683 && TREE_CODE (ptr) == PARM_DECL)
2685 alias_stats.alias_noalias++;
2686 alias_stats.simple_resolved++;
2687 return false;
2690 /* If either MEM or VAR is a read-only global and the other one
2691 isn't, then PTR cannot point to VAR. */
2692 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
2693 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
2695 alias_stats.alias_noalias++;
2696 alias_stats.simple_resolved++;
2697 return false;
2700 /* If the pointed to memory has alias set zero, or the pointer
2701 is ref-all, or the pointer decl is marked that no TBAA is to
2702 be applied, the MEM can alias VAR. */
2703 if (mem_alias_set == 0
2704 || DECL_POINTER_ALIAS_SET (ptr) == 0
2705 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (ptr))
2706 || DECL_NO_TBAA_P (ptr))
2708 alias_stats.alias_mayalias++;
2709 alias_stats.simple_resolved++;
2710 return true;
2713 gcc_assert (TREE_CODE (mem) == SYMBOL_MEMORY_TAG);
2715 alias_stats.tbaa_queries++;
2717 /* If the alias sets don't conflict then MEM cannot alias VAR. */
2718 if (mem_alias_set != var_alias_set
2719 && !alias_set_subset_of (mem_alias_set, var_alias_set))
2721 alias_stats.alias_noalias++;
2722 alias_stats.tbaa_resolved++;
2723 return false;
2726 /* If VAR is a record or union type, PTR cannot point into VAR
2727 unless there is some explicit address operation in the
2728 program that can reference a field of the type pointed-to by
2729 PTR. This also assumes that the types of both VAR and PTR
2730 are contained within the compilation unit, and that there is
2731 no fancy addressing arithmetic associated with any of the
2732 types involved. */
2733 if (mem_alias_set != 0 && var_alias_set != 0)
2735 tree ptr_type = TREE_TYPE (ptr);
2736 tree var_type = TREE_TYPE (var);
2738 /* The star count is -1 if the type at the end of the
2739 pointer_to chain is not a record or union type. */
2740 if (!alias_set_only
2741 && ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
2743 int ptr_star_count = 0;
2745 /* ipa_type_escape_star_count_of_interesting_type is a
2746 little too restrictive for the pointer type, need to
2747 allow pointers to primitive types as long as those
2748 types cannot be pointers to everything. */
2749 while (POINTER_TYPE_P (ptr_type))
2751 /* Strip the *s off. */
2752 ptr_type = TREE_TYPE (ptr_type);
2753 ptr_star_count++;
2756 /* There does not appear to be a better test to see if
2757 the pointer type was one of the pointer to everything
2758 types. */
2759 if (ptr_star_count > 0)
2761 alias_stats.structnoaddress_queries++;
2762 if (ipa_type_escape_field_does_not_clobber_p (var_type,
2763 TREE_TYPE (ptr)))
2765 alias_stats.structnoaddress_resolved++;
2766 alias_stats.alias_noalias++;
2767 return false;
2770 else if (ptr_star_count == 0)
2772 /* If PTR_TYPE was not really a pointer to type, it cannot
2773 alias. */
2774 alias_stats.structnoaddress_queries++;
2775 alias_stats.structnoaddress_resolved++;
2776 alias_stats.alias_noalias++;
2777 return false;
2782 alias_stats.alias_mayalias++;
2783 return true;
2786 /* Return true, if PTR may point to a global variable. */
2788 bool
2789 may_point_to_global_var (tree ptr)
2791 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2793 /* If we do not have points-to information for this variable,
2794 we have to punt. */
2795 if (!pi
2796 || !pi->name_mem_tag)
2797 return true;
2799 /* The name memory tag is marked as global variable if the points-to
2800 set contains a global variable. */
2801 return is_global_var (pi->name_mem_tag);
2804 /* Add ALIAS to the set of variables that may alias VAR. */
2806 static void
2807 add_may_alias (tree var, tree alias)
2809 /* Don't allow self-referential aliases. */
2810 gcc_assert (var != alias);
2812 /* ALIAS must be addressable if it's being added to an alias set. */
2813 #if 1
2814 TREE_ADDRESSABLE (alias) = 1;
2815 #else
2816 gcc_assert (may_be_aliased (alias));
2817 #endif
2819 /* VAR must be a symbol or a name tag. */
2820 gcc_assert (TREE_CODE (var) == SYMBOL_MEMORY_TAG
2821 || TREE_CODE (var) == NAME_MEMORY_TAG);
2823 if (MTAG_ALIASES (var) == NULL)
2824 MTAG_ALIASES (var) = BITMAP_ALLOC (&alias_bitmap_obstack);
2826 bitmap_set_bit (MTAG_ALIASES (var), DECL_UID (alias));
2830 /* Mark pointer PTR as pointing to an arbitrary memory location. */
2832 static void
2833 set_pt_anything (tree ptr)
2835 struct ptr_info_def *pi = get_ptr_info (ptr);
2837 pi->pt_anything = 1;
2838 /* Anything includes global memory. */
2839 pi->pt_global_mem = 1;
2840 pi->pt_vars = NULL;
2842 /* The pointer used to have a name tag, but we now found it pointing
2843 to an arbitrary location. The name tag needs to be renamed and
2844 disassociated from PTR. */
2845 if (pi->name_mem_tag)
2847 mark_sym_for_renaming (pi->name_mem_tag);
2848 pi->name_mem_tag = NULL_TREE;
2853 /* Return true if STMT is an "escape" site from the current function. Escape
2854 sites those statements which might expose the address of a variable
2855 outside the current function. STMT is an escape site iff:
2857 1- STMT is a function call, or
2858 2- STMT is an __asm__ expression, or
2859 3- STMT is an assignment to a non-local variable, or
2860 4- STMT is a return statement.
2862 Return the type of escape site found, if we found one, or NO_ESCAPE
2863 if none. */
2865 enum escape_type
2866 is_escape_site (tree stmt)
2868 tree call = get_call_expr_in (stmt);
2869 if (call != NULL_TREE)
2871 if (!TREE_SIDE_EFFECTS (call))
2872 return ESCAPE_TO_PURE_CONST;
2874 return ESCAPE_TO_CALL;
2876 else if (TREE_CODE (stmt) == ASM_EXPR)
2877 return ESCAPE_TO_ASM;
2878 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2880 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
2882 /* Get to the base of _REF nodes. */
2883 if (TREE_CODE (lhs) != SSA_NAME)
2884 lhs = get_base_address (lhs);
2886 /* If we couldn't recognize the LHS of the assignment, assume that it
2887 is a non-local store. */
2888 if (lhs == NULL_TREE)
2889 return ESCAPE_UNKNOWN;
2891 if (CONVERT_EXPR_P (GIMPLE_STMT_OPERAND (stmt, 1))
2892 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2894 tree from
2895 = TREE_TYPE (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0));
2896 tree to = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
2898 /* If the RHS is a conversion between a pointer and an integer, the
2899 pointer escapes since we can't track the integer. */
2900 if (POINTER_TYPE_P (from) && !POINTER_TYPE_P (to))
2901 return ESCAPE_BAD_CAST;
2904 /* If the LHS is an SSA name, it can't possibly represent a non-local
2905 memory store. */
2906 if (TREE_CODE (lhs) == SSA_NAME)
2907 return NO_ESCAPE;
2909 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2910 local variables we cannot be sure if it will escape, because we
2911 don't have information about objects not in SSA form. Need to
2912 implement something along the lines of
2914 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2915 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2916 Conference on Object-Oriented Programming Systems, Languages, and
2917 Applications (OOPSLA), pp. 1-19, 1999. */
2918 return ESCAPE_STORED_IN_GLOBAL;
2920 else if (TREE_CODE (stmt) == RETURN_EXPR)
2921 return ESCAPE_TO_RETURN;
2923 return NO_ESCAPE;
2926 /* Create a new memory tag of type TYPE.
2927 Does NOT push it into the current binding. */
2929 tree
2930 create_tag_raw (enum tree_code code, tree type, const char *prefix)
2932 tree tmp_var;
2934 tmp_var = build_decl (code, create_tmp_var_name (prefix), type);
2936 /* Memory tags are always writable and non-static. */
2937 TREE_READONLY (tmp_var) = 0;
2938 TREE_STATIC (tmp_var) = 0;
2940 /* It doesn't start out global. */
2941 MTAG_GLOBAL (tmp_var) = 0;
2942 TREE_USED (tmp_var) = 1;
2944 return tmp_var;
2947 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2948 is considered to represent all the pointers whose pointed-to types are
2949 in the same alias set class. Otherwise, the tag represents a single
2950 SSA_NAME pointer variable. */
2952 static tree
2953 create_memory_tag (tree type, bool is_type_tag)
2955 tree tag = create_tag_raw (is_type_tag ? SYMBOL_MEMORY_TAG : NAME_MEMORY_TAG,
2956 type, (is_type_tag) ? "SMT" : "NMT");
2958 /* By default, memory tags are local variables. Alias analysis will
2959 determine whether they should be considered globals. */
2960 DECL_CONTEXT (tag) = current_function_decl;
2962 /* Memory tags are by definition addressable. */
2963 TREE_ADDRESSABLE (tag) = 1;
2965 set_symbol_mem_tag (tag, NULL_TREE);
2967 /* Add the tag to the symbol table. */
2968 add_referenced_var (tag);
2970 return tag;
2974 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2975 This is used if P_i has been found to point to a specific set of
2976 variables or to a non-aliased memory location like the address returned
2977 by malloc functions. */
2979 static tree
2980 get_nmt_for (tree ptr)
2982 struct ptr_info_def *pi = get_ptr_info (ptr);
2983 tree tag = pi->name_mem_tag;
2985 if (tag == NULL_TREE)
2986 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2987 return tag;
2991 /* Return the symbol memory tag associated to pointer PTR. A memory
2992 tag is an artificial variable that represents the memory location
2993 pointed-to by PTR. It is used to model the effects of pointer
2994 de-references on addressable variables.
2996 AI points to the data gathered during alias analysis. This
2997 function populates the array AI->POINTERS. */
2999 static tree
3000 get_smt_for (tree ptr, struct alias_info *ai)
3002 size_t i;
3003 tree tag;
3004 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3005 alias_set_type tag_set = get_alias_set (tag_type);
3007 /* To avoid creating unnecessary memory tags, only create one memory tag
3008 per alias set class. Note that it may be tempting to group
3009 memory tags based on conflicting alias sets instead of
3010 equivalence. That would be wrong because alias sets are not
3011 necessarily transitive (as demonstrated by the libstdc++ test
3012 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
3013 such that conflicts (A, B) == true and conflicts (A, C) == true,
3014 it does not necessarily follow that conflicts (B, C) == true. */
3015 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
3017 struct alias_map_d *curr = ai->pointers[i];
3018 tree curr_tag = symbol_mem_tag (curr->var);
3019 if (tag_set == curr->set)
3021 tag = curr_tag;
3022 break;
3026 /* If VAR cannot alias with any of the existing memory tags, create a new
3027 tag for PTR and add it to the POINTERS array. */
3028 if (tag == NULL_TREE)
3030 struct alias_map_d *alias_map;
3032 /* If PTR did not have a symbol tag already, create a new SMT.*
3033 artificial variable representing the memory location
3034 pointed-to by PTR. */
3035 tag = symbol_mem_tag (ptr);
3036 if (tag == NULL_TREE)
3037 tag = create_memory_tag (tag_type, true);
3039 /* Add PTR to the POINTERS array. Note that we are not interested in
3040 PTR's alias set. Instead, we cache the alias set for the memory that
3041 PTR points to. */
3042 alias_map = XCNEW (struct alias_map_d);
3043 alias_map->var = ptr;
3044 alias_map->set = tag_set;
3045 ai->pointers[ai->num_pointers++] = alias_map;
3048 /* If the pointed-to type is volatile, so is the tag. */
3049 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
3051 /* Make sure that the symbol tag has the same alias set as the
3052 pointed-to type or at least accesses through the pointer will
3053 alias that set. The latter can happen after the vectorizer
3054 created pointers of vector type. */
3055 gcc_assert (tag_set == get_alias_set (tag)
3056 || alias_set_subset_of (tag_set, get_alias_set (tag)));
3058 return tag;
3062 /* Create GLOBAL_VAR, an artificial global variable to act as a
3063 representative of all the variables that may be clobbered by function
3064 calls. */
3066 static void
3067 create_global_var (void)
3069 tree global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
3070 void_type_node);
3071 DECL_ARTIFICIAL (global_var) = 1;
3072 TREE_READONLY (global_var) = 0;
3073 DECL_EXTERNAL (global_var) = 1;
3074 TREE_STATIC (global_var) = 1;
3075 TREE_USED (global_var) = 1;
3076 DECL_CONTEXT (global_var) = NULL_TREE;
3077 TREE_THIS_VOLATILE (global_var) = 0;
3078 TREE_ADDRESSABLE (global_var) = 0;
3080 create_var_ann (global_var);
3081 mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
3082 add_referenced_var (global_var);
3083 mark_sym_for_renaming (global_var);
3084 cfun->gimple_df->global_var = global_var;
3088 /* Dump alias statistics on FILE. */
3090 static void
3091 dump_alias_stats (FILE *file)
3093 const char *funcname
3094 = lang_hooks.decl_printable_name (current_function_decl, 2);
3095 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
3096 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
3097 fprintf (file, "Total alias mayalias results:\t%u\n",
3098 alias_stats.alias_mayalias);
3099 fprintf (file, "Total alias noalias results:\t%u\n",
3100 alias_stats.alias_noalias);
3101 fprintf (file, "Total simple queries:\t%u\n",
3102 alias_stats.simple_queries);
3103 fprintf (file, "Total simple resolved:\t%u\n",
3104 alias_stats.simple_resolved);
3105 fprintf (file, "Total TBAA queries:\t%u\n",
3106 alias_stats.tbaa_queries);
3107 fprintf (file, "Total TBAA resolved:\t%u\n",
3108 alias_stats.tbaa_resolved);
3109 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
3110 alias_stats.structnoaddress_queries);
3111 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
3112 alias_stats.structnoaddress_resolved);
3116 /* Dump alias information on FILE. */
3118 void
3119 dump_alias_info (FILE *file)
3121 size_t i;
3122 const char *funcname
3123 = lang_hooks.decl_printable_name (current_function_decl, 2);
3124 referenced_var_iterator rvi;
3125 tree var;
3127 fprintf (file, "\nAlias information for %s\n\n", funcname);
3129 dump_memory_partitions (file);
3131 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
3133 fprintf (file, "Aliased symbols\n\n");
3135 FOR_EACH_REFERENCED_VAR (var, rvi)
3137 if (may_be_aliased (var))
3138 dump_variable (file, var);
3141 fprintf (file, "\nDereferenced pointers\n\n");
3143 FOR_EACH_REFERENCED_VAR (var, rvi)
3144 if (symbol_mem_tag (var))
3145 dump_variable (file, var);
3147 fprintf (file, "\nSymbol memory tags\n\n");
3149 FOR_EACH_REFERENCED_VAR (var, rvi)
3151 if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
3152 dump_variable (file, var);
3155 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
3157 fprintf (file, "SSA_NAME pointers\n\n");
3158 for (i = 1; i < num_ssa_names; i++)
3160 tree ptr = ssa_name (i);
3161 struct ptr_info_def *pi;
3163 if (ptr == NULL_TREE)
3164 continue;
3166 pi = SSA_NAME_PTR_INFO (ptr);
3167 if (!SSA_NAME_IN_FREE_LIST (ptr)
3168 && pi
3169 && pi->name_mem_tag)
3170 dump_points_to_info_for (file, ptr);
3173 fprintf (file, "\nName memory tags\n\n");
3175 FOR_EACH_REFERENCED_VAR (var, rvi)
3177 if (TREE_CODE (var) == NAME_MEMORY_TAG)
3178 dump_variable (file, var);
3181 fprintf (file, "\n");
3185 /* Dump alias information on stderr. */
3187 void
3188 debug_alias_info (void)
3190 dump_alias_info (stderr);
3194 /* Return the alias information associated with pointer T. It creates a
3195 new instance if none existed. */
3197 struct ptr_info_def *
3198 get_ptr_info (tree t)
3200 struct ptr_info_def *pi;
3202 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
3204 pi = SSA_NAME_PTR_INFO (t);
3205 if (pi == NULL)
3207 pi = GGC_CNEW (struct ptr_info_def);
3208 SSA_NAME_PTR_INFO (t) = pi;
3211 return pi;
3215 /* Dump points-to information for SSA_NAME PTR into FILE. */
3217 void
3218 dump_points_to_info_for (FILE *file, tree ptr)
3220 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3222 print_generic_expr (file, ptr, dump_flags);
3224 if (pi)
3226 if (pi->name_mem_tag)
3228 fprintf (file, ", name memory tag: ");
3229 print_generic_expr (file, pi->name_mem_tag, dump_flags);
3232 if (pi->is_dereferenced)
3233 fprintf (file, ", is dereferenced");
3234 else if (pi->memory_tag_needed)
3235 fprintf (file, ", is dereferenced in call");
3237 if (pi->value_escapes_p)
3238 fprintf (file, ", its value escapes");
3240 if (pi->pt_anything)
3241 fprintf (file, ", points-to anything");
3243 if (pi->pt_null)
3244 fprintf (file, ", points-to NULL");
3246 if (pi->pt_vars)
3248 fprintf (file, ", points-to vars: ");
3249 dump_decl_set (file, pi->pt_vars);
3253 fprintf (file, "\n");
3257 /* Dump points-to information for VAR into stderr. */
3259 void
3260 debug_points_to_info_for (tree var)
3262 dump_points_to_info_for (stderr, var);
3266 /* Dump points-to information into FILE. NOTE: This function is slow, as
3267 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
3269 void
3270 dump_points_to_info (FILE *file)
3272 basic_block bb;
3273 block_stmt_iterator si;
3274 ssa_op_iter iter;
3275 const char *fname =
3276 lang_hooks.decl_printable_name (current_function_decl, 2);
3277 referenced_var_iterator rvi;
3278 tree var;
3280 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
3282 /* First dump points-to information for the default definitions of
3283 pointer variables. This is necessary because default definitions are
3284 not part of the code. */
3285 FOR_EACH_REFERENCED_VAR (var, rvi)
3287 if (POINTER_TYPE_P (TREE_TYPE (var)))
3289 tree def = gimple_default_def (cfun, var);
3290 if (def)
3291 dump_points_to_info_for (file, def);
3295 /* Dump points-to information for every pointer defined in the program. */
3296 FOR_EACH_BB (bb)
3298 tree phi;
3300 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3302 tree ptr = PHI_RESULT (phi);
3303 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
3304 dump_points_to_info_for (file, ptr);
3307 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3309 tree stmt = bsi_stmt (si);
3310 tree def;
3311 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
3312 if (TREE_CODE (def) == SSA_NAME
3313 && POINTER_TYPE_P (TREE_TYPE (def)))
3314 dump_points_to_info_for (file, def);
3318 fprintf (file, "\n");
3322 /* Dump points-to info pointed to by PTO into STDERR. */
3324 void
3325 debug_points_to_info (void)
3327 dump_points_to_info (stderr);
3330 /* Dump to FILE the list of variables that may be aliasing VAR. */
3332 void
3333 dump_may_aliases_for (FILE *file, tree var)
3335 bitmap aliases;
3337 aliases = MTAG_ALIASES (var);
3338 if (aliases)
3340 bitmap_iterator bi;
3341 unsigned int i;
3342 tree al;
3344 fprintf (file, "{ ");
3345 EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
3347 al = referenced_var (i);
3348 print_generic_expr (file, al, dump_flags);
3349 fprintf (file, " ");
3351 fprintf (file, "}");
3356 /* Dump to stderr the list of variables that may be aliasing VAR. */
3358 void
3359 debug_may_aliases_for (tree var)
3361 dump_may_aliases_for (stderr, var);
3365 /* Return true if VAR may be aliased. */
3367 bool
3368 may_be_aliased (tree var)
3370 /* Obviously. */
3371 if (TREE_ADDRESSABLE (var))
3372 return true;
3374 /* Globally visible variables can have their addresses taken by other
3375 translation units. */
3376 if (MTAG_P (var)
3377 && MTAG_GLOBAL (var))
3378 return true;
3379 else if (!MTAG_P (var)
3380 && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
3381 return true;
3383 /* Automatic variables can't have their addresses escape any other
3384 way. This must be after the check for global variables, as
3385 extern declarations do not have TREE_STATIC set. */
3386 if (!TREE_STATIC (var))
3387 return false;
3389 /* If we're in unit-at-a-time mode, then we must have seen all
3390 occurrences of address-of operators, and so we can trust
3391 TREE_ADDRESSABLE. Otherwise we can only be sure the variable
3392 isn't addressable if it's local to the current function. */
3393 if (flag_unit_at_a_time)
3394 return false;
3396 if (decl_function_context (var) == current_function_decl)
3397 return false;
3399 return true;
3402 /* The following is based on code in add_stmt_operand to ensure that the
3403 same defs/uses/vdefs/vuses will be found after replacing a reference
3404 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
3405 is the address of var. Return a memtag for the ptr, after adding the
3406 proper may_aliases to it (which are the aliases of var, if it has any,
3407 or var itself). */
3409 static tree
3410 add_may_alias_for_new_tag (tree tag, tree var)
3412 bitmap aliases = NULL;
3414 if (MTAG_P (var))
3415 aliases = may_aliases (var);
3417 /* Case 1: |aliases| == 1 */
3418 if (aliases
3419 && bitmap_single_bit_set_p (aliases))
3421 tree ali = referenced_var (bitmap_first_set_bit (aliases));
3422 if (TREE_CODE (ali) == SYMBOL_MEMORY_TAG)
3423 return ali;
3426 /* Case 2: |aliases| == 0 */
3427 if (aliases == NULL)
3428 add_may_alias (tag, var);
3429 else
3431 /* Case 3: |aliases| > 1 */
3432 union_alias_set_into (tag, aliases);
3434 return tag;
3437 /* Create a new symbol tag for PTR. Construct the may-alias list of
3438 this type tag so that it has the aliasing of VAR according to the
3439 location accessed by EXPR.
3441 Note, the set of aliases represented by the new symbol tag are not
3442 marked for renaming. */
3444 void
3445 new_type_alias (tree ptr, tree var, tree expr)
3447 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3448 tree tag;
3449 tree ali = NULL_TREE;
3450 HOST_WIDE_INT offset, size, maxsize;
3451 tree ref;
3453 gcc_assert (symbol_mem_tag (ptr) == NULL_TREE);
3454 gcc_assert (!MTAG_P (var));
3456 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
3457 gcc_assert (ref);
3459 tag = create_memory_tag (tag_type, true);
3460 set_symbol_mem_tag (ptr, tag);
3462 ali = add_may_alias_for_new_tag (tag, var);
3464 set_symbol_mem_tag (ptr, ali);
3465 MTAG_GLOBAL (tag) = is_global_var (var);
3469 /* Reset the call_clobbered flags on our referenced vars. In
3470 theory, this only needs to be done for globals. */
3472 static unsigned int
3473 reset_cc_flags (void)
3475 tree var;
3476 referenced_var_iterator rvi;
3478 FOR_EACH_REFERENCED_VAR (var, rvi)
3479 var_ann (var)->call_clobbered = false;
3480 return 0;
3483 struct gimple_opt_pass pass_reset_cc_flags =
3486 GIMPLE_PASS,
3487 NULL, /* name */
3488 NULL, /* gate */
3489 reset_cc_flags, /* execute */
3490 NULL, /* sub */
3491 NULL, /* next */
3492 0, /* static_pass_number */
3493 0, /* tv_id */
3494 PROP_referenced_vars |PROP_cfg, /* properties_required */
3495 0, /* properties_provided */
3496 0, /* properties_destroyed */
3497 0, /* todo_flags_start */
3498 0 /* todo_flags_finish */
3503 /* A dummy pass to cause aliases to be computed via TODO_rebuild_alias. */
3505 struct gimple_opt_pass pass_build_alias =
3508 GIMPLE_PASS,
3509 "alias", /* name */
3510 NULL, /* gate */
3511 NULL, /* execute */
3512 NULL, /* sub */
3513 NULL, /* next */
3514 0, /* static_pass_number */
3515 0, /* tv_id */
3516 PROP_cfg | PROP_ssa, /* properties_required */
3517 PROP_alias, /* properties_provided */
3518 0, /* properties_destroyed */
3519 0, /* todo_flags_start */
3520 TODO_rebuild_alias | TODO_dump_func /* todo_flags_finish */