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)
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/>. */
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
33 #include "langhooks.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"
45 #include "ipa-type-escape.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
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
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
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
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
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
169 /* Structure to map a variable to its alias set. */
172 /* Variable and its alias set. */
178 /* Counters used to display statistics on alias analysis. */
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 finalize_ref_all_pointers (struct alias_info *);
200 static void dump_alias_stats (FILE *);
201 static bool may_alias_p (tree, alias_set_type, tree, alias_set_type, bool);
202 static tree create_memory_tag (tree type, bool is_type_tag);
203 static tree get_smt_for (tree, struct alias_info *);
204 static tree get_nmt_for (tree);
205 static void add_may_alias (tree, tree);
206 static struct alias_info *init_alias_info (void);
207 static void delete_alias_info (struct alias_info *);
208 static void compute_flow_sensitive_aliasing (struct alias_info *);
209 static void setup_pointers_and_addressables (struct alias_info *);
210 static void create_global_var (void);
211 static void maybe_create_global_var (void);
212 static void set_pt_anything (tree);
214 void debug_mp_info (VEC(mem_sym_stats_t,heap) *);
216 static alloc_pool mem_sym_stats_pool;
218 /* Return memory reference stats for symbol VAR. Create a new slot in
219 cfun->gimple_df->mem_sym_stats if needed. */
221 static struct mem_sym_stats_d *
222 get_mem_sym_stats_for (tree var)
225 struct mem_sym_stats_d *stats;
226 struct pointer_map_t *map = gimple_mem_ref_stats (cfun)->mem_sym_stats;
230 slot = pointer_map_insert (map, var);
233 stats = pool_alloc (mem_sym_stats_pool);
234 memset (stats, 0, sizeof (*stats));
236 *slot = (void *) stats;
239 stats = (struct mem_sym_stats_d *) *slot;
245 /* Return memory reference statistics for variable VAR in function FN.
246 This is computed by alias analysis, but it is not kept
247 incrementally up-to-date. So, these stats are only accurate if
248 pass_may_alias has been run recently. If no alias information
249 exists, this function returns NULL. */
251 static mem_sym_stats_t
252 mem_sym_stats (struct function *fn, tree var)
255 struct pointer_map_t *stats_map = gimple_mem_ref_stats (fn)->mem_sym_stats;
257 if (stats_map == NULL)
260 slot = pointer_map_contains (stats_map, var);
264 return (mem_sym_stats_t) *slot;
268 /* Set MPT to be the memory partition associated with symbol SYM. */
271 set_memory_partition (tree sym, tree mpt)
273 #if defined ENABLE_CHECKING
275 gcc_assert (TREE_CODE (mpt) == MEMORY_PARTITION_TAG
276 && !is_gimple_reg (sym));
279 var_ann (sym)->mpt = mpt;
282 if (MPT_SYMBOLS (mpt) == NULL)
283 MPT_SYMBOLS (mpt) = BITMAP_ALLOC (&alias_bitmap_obstack);
285 bitmap_set_bit (MPT_SYMBOLS (mpt), DECL_UID (sym));
287 /* MPT inherits the call-clobbering attributes from SYM. */
288 if (is_call_clobbered (sym))
290 MTAG_GLOBAL (mpt) = 1;
291 mark_call_clobbered (mpt, ESCAPE_IS_GLOBAL);
297 /* Mark variable VAR as being non-addressable. */
300 mark_non_addressable (tree var)
304 if (!TREE_ADDRESSABLE (var))
307 mpt = memory_partition (var);
310 var_ann (var)->call_clobbered = false;
312 bitmap_clear_bit (gimple_call_clobbered_vars (cfun), DECL_UID (var));
313 TREE_ADDRESSABLE (var) = 0;
317 /* Note that it's possible for a symbol to have an associated
318 MPT and the MPT have a NULL empty set. During
319 init_alias_info, all MPTs get their sets cleared out, but the
320 symbols still point to the old MPTs that used to hold them.
321 This is done so that compute_memory_partitions can now which
322 symbols are losing or changing partitions and mark them for
324 if (MPT_SYMBOLS (mpt))
325 bitmap_clear_bit (MPT_SYMBOLS (mpt), DECL_UID (var));
326 set_memory_partition (var, NULL_TREE);
331 /* qsort comparison function to sort type/name tags by DECL_UID. */
334 sort_tags_by_id (const void *pa, const void *pb)
336 const_tree const a = *(const_tree const *)pa;
337 const_tree const b = *(const_tree const *)pb;
339 return DECL_UID (a) - DECL_UID (b);
342 /* Initialize WORKLIST to contain those memory tags that are marked call
343 clobbered. Initialized WORKLIST2 to contain the reasons these
344 memory tags escaped. */
347 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
348 VEC (int, heap) **worklist2,
351 referenced_var_iterator rvi;
354 FOR_EACH_REFERENCED_VAR (curr, rvi)
356 if (MTAG_P (curr) && is_call_clobbered (curr))
358 VEC_safe_push (tree, heap, *worklist, curr);
359 VEC_safe_push (int, heap, *worklist2,
360 var_ann (curr)->escape_mask);
361 bitmap_set_bit (on_worklist, DECL_UID (curr));
366 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
367 ALIAS is not already marked call clobbered, and is a memory
371 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
372 VEC (int, heap) **worklist2, int reason,
375 if (MTAG_P (alias) && !is_call_clobbered (alias)
376 && !bitmap_bit_p (on_worklist, DECL_UID (alias)))
378 VEC_safe_push (tree, heap, *worklist, alias);
379 VEC_safe_push (int, heap, *worklist2, reason);
380 bitmap_set_bit (on_worklist, DECL_UID (alias));
384 /* Mark aliases of TAG as call clobbered, and place any tags on the
385 alias list that were not already call clobbered on WORKLIST. */
388 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
389 VEC (int, heap) **worklist2,
390 bitmap on_worklist, bitmap queued)
396 var_ann_t ta = var_ann (tag);
400 aliases = may_aliases (tag);
404 EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
406 entry = referenced_var (i);
407 /* If you clobber one part of a structure, you
408 clobber the entire thing. While this does not make
409 the world a particularly nice place, it is necessary
410 in order to allow C/C++ tricks that involve
411 pointer arithmetic to work. */
412 if (TREE_CODE (entry) == STRUCT_FIELD_TAG)
413 bitmap_set_bit (queued, DECL_UID (SFT_PARENT_VAR (entry)));
414 else if (!unmodifiable_var_p (entry))
416 add_to_worklist (entry, worklist, worklist2, ta->escape_mask,
418 mark_call_clobbered (entry, ta->escape_mask);
421 if (!bitmap_empty_p (queued))
423 EXECUTE_IF_SET_IN_BITMAP (queued, 0, i, bi)
425 subvar_t svars = get_subvars_for_var (referenced_var (i));
429 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
430 if (!unmodifiable_var_p (subvar))
431 mark_call_clobbered (subvar, ta->escape_mask);
433 bitmap_clear (queued);
437 /* Tags containing global vars need to be marked as global.
438 Tags containing call clobbered vars need to be marked as call
442 compute_tag_properties (void)
444 referenced_var_iterator rvi;
447 VEC (tree, heap) *taglist = NULL;
449 FOR_EACH_REFERENCED_VAR (tag, rvi)
451 if (!MTAG_P (tag) || TREE_CODE (tag) == STRUCT_FIELD_TAG)
453 VEC_safe_push (tree, heap, taglist, tag);
456 /* We sort the taglist by DECL_UID, for two reasons.
457 1. To get a sequential ordering to make the bitmap accesses
459 2. Because of the way we compute aliases, it's more likely that
460 an earlier tag is included in a later tag, and this will reduce
461 the number of iterations.
463 If we had a real tag graph, we would just topo-order it and be
465 qsort (VEC_address (tree, taglist),
466 VEC_length (tree, taglist),
470 /* Go through each tag not marked as global, and if it aliases
471 global vars, mark it global.
473 If the tag contains call clobbered vars, mark it call
476 This loop iterates because tags may appear in the may-aliases
477 list of other tags when we group. */
484 for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
490 bool tagcc = is_call_clobbered (tag);
491 bool tagglobal = MTAG_GLOBAL (tag);
493 if (tagcc && tagglobal)
496 ma = may_aliases (tag);
500 EXECUTE_IF_SET_IN_BITMAP (ma, 0, i, bi)
502 entry = referenced_var (i);
503 /* Call clobbered entries cause the tag to be marked
505 if (!tagcc && is_call_clobbered (entry))
507 mark_call_clobbered (tag, var_ann (entry)->escape_mask);
512 /* Global vars cause the tag to be marked global. */
513 if (!tagglobal && is_global_var (entry))
515 MTAG_GLOBAL (tag) = true;
520 /* Early exit once both global and cc are set, since the
521 loop can't do any more than that. */
522 if (tagcc && tagglobal)
527 VEC_free (tree, heap, taglist);
530 /* Set up the initial variable clobbers and globalness.
531 When this function completes, only tags whose aliases need to be
532 clobbered will be set clobbered. Tags clobbered because they
533 contain call clobbered vars are handled in compute_tag_properties. */
536 set_initial_properties (struct alias_info *ai)
539 referenced_var_iterator rvi;
544 /* Temporary bitmap to avoid quadratic behavior in marking
546 queued = BITMAP_ALLOC (&alias_bitmap_obstack);
548 FOR_EACH_REFERENCED_VAR (var, rvi)
550 if (is_global_var (var)
551 && (!var_can_have_subvars (var)
552 || get_subvars_for_var (var) == NULL))
554 if (!unmodifiable_var_p (var))
555 mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
557 else if (TREE_CODE (var) == PARM_DECL
558 && gimple_default_def (cfun, var)
559 && POINTER_TYPE_P (TREE_TYPE (var)))
561 tree def = gimple_default_def (cfun, var);
562 get_ptr_info (def)->value_escapes_p = 1;
563 get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;
567 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
569 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
570 tree tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
572 if (pi->value_escapes_p)
574 /* If PTR escapes then its associated memory tags and
575 pointed-to variables are call-clobbered. */
576 if (pi->name_mem_tag)
577 mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
580 mark_call_clobbered (tag, pi->escape_mask);
586 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
588 tree alias = referenced_var (j);
590 /* If you clobber one part of a structure, you
591 clobber the entire thing. While this does not make
592 the world a particularly nice place, it is necessary
593 in order to allow C/C++ tricks that involve
594 pointer arithmetic to work. */
595 if (TREE_CODE (alias) == STRUCT_FIELD_TAG)
596 bitmap_set_bit (queued, DECL_UID (SFT_PARENT_VAR (alias)));
597 else if (!unmodifiable_var_p (alias))
598 mark_call_clobbered (alias, pi->escape_mask);
600 /* Process variables we need to clobber all parts of. */
601 if (!bitmap_empty_p (queued))
603 EXECUTE_IF_SET_IN_BITMAP (queued, 0, j, bi)
605 subvar_t svars = get_subvars_for_var (referenced_var (j));
609 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
610 if (!unmodifiable_var_p (subvar))
611 mark_call_clobbered (subvar, pi->escape_mask);
613 bitmap_clear (queued);
618 /* If the name tag is call clobbered, so is the symbol tag
619 associated with the base VAR_DECL. */
622 && is_call_clobbered (pi->name_mem_tag))
623 mark_call_clobbered (tag, pi->escape_mask);
625 /* Name tags and symbol tags that we don't know where they point
626 to, might point to global memory, and thus, are clobbered.
628 FIXME: This is not quite right. They should only be
629 clobbered if value_escapes_p is true, regardless of whether
630 they point to global memory or not.
631 So removing this code and fixing all the bugs would be nice.
632 It is the cause of a bunch of clobbering. */
633 if ((pi->pt_global_mem || pi->pt_anything)
634 && pi->is_dereferenced && pi->name_mem_tag)
636 mark_call_clobbered (pi->name_mem_tag, ESCAPE_IS_GLOBAL);
637 MTAG_GLOBAL (pi->name_mem_tag) = true;
640 if ((pi->pt_global_mem || pi->pt_anything)
641 && pi->is_dereferenced
644 mark_call_clobbered (tag, ESCAPE_IS_GLOBAL);
645 MTAG_GLOBAL (tag) = true;
649 BITMAP_FREE (queued);
652 /* Compute which variables need to be marked call clobbered because
653 their tag is call clobbered, and which tags need to be marked
654 global because they contain global variables. */
657 compute_call_clobbered (struct alias_info *ai)
659 VEC (tree, heap) *worklist = NULL;
660 VEC (int,heap) *worklist2 = NULL;
661 bitmap on_worklist, queued;
663 timevar_push (TV_CALL_CLOBBER);
664 on_worklist = BITMAP_ALLOC (NULL);
665 queued = BITMAP_ALLOC (NULL);
667 set_initial_properties (ai);
668 init_transitive_clobber_worklist (&worklist, &worklist2, on_worklist);
669 while (VEC_length (tree, worklist) != 0)
671 tree curr = VEC_pop (tree, worklist);
672 int reason = VEC_pop (int, worklist2);
674 bitmap_clear_bit (on_worklist, DECL_UID (curr));
675 mark_call_clobbered (curr, reason);
676 mark_aliases_call_clobbered (curr, &worklist, &worklist2,
677 on_worklist, queued);
679 VEC_free (tree, heap, worklist);
680 VEC_free (int, heap, worklist2);
681 BITMAP_FREE (on_worklist);
682 BITMAP_FREE (queued);
683 compute_tag_properties ();
684 timevar_pop (TV_CALL_CLOBBER);
688 /* Dump memory partition information to FILE. */
691 dump_memory_partitions (FILE *file)
697 fprintf (file, "\nMemory partitions\n\n");
698 for (i = 0, npart = 0, nsyms = 0;
699 VEC_iterate (tree, gimple_ssa_operands (cfun)->mpt_table, i, mpt);
704 bitmap syms = MPT_SYMBOLS (mpt);
705 unsigned long n = (syms) ? bitmap_count_bits (syms) : 0;
707 fprintf (file, "#%u: ", i);
708 print_generic_expr (file, mpt, 0);
709 fprintf (file, ": %lu elements: ", n);
710 dump_decl_set (file, syms);
716 fprintf (file, "\n%u memory partitions holding %lu symbols\n", npart, nsyms);
720 /* Dump memory partition information to stderr. */
723 debug_memory_partitions (void)
725 dump_memory_partitions (stderr);
729 /* Return true if memory partitioning is required given the memory
730 reference estimates in STATS. */
733 need_to_partition_p (struct mem_ref_stats_d *stats)
735 long num_vops = stats->num_vuses + stats->num_vdefs;
736 long avg_vops = CEIL (num_vops, stats->num_mem_stmts);
737 return (num_vops > (long) MAX_ALIASED_VOPS
738 && avg_vops > (long) AVG_ALIASED_VOPS);
742 /* Count the actual number of virtual operators in CFUN. Note that
743 this is only meaningful after virtual operands have been populated,
744 so it should be invoked at the end of compute_may_aliases.
746 The number of virtual operators are stored in *NUM_VDEFS_P and
747 *NUM_VUSES_P, the number of partitioned symbols in
748 *NUM_PARTITIONED_P and the number of unpartitioned symbols in
749 *NUM_UNPARTITIONED_P.
751 If any of these pointers is NULL the corresponding count is not
755 count_mem_refs (long *num_vuses_p, long *num_vdefs_p,
756 long *num_partitioned_p, long *num_unpartitioned_p)
758 block_stmt_iterator bsi;
760 long num_vdefs, num_vuses, num_partitioned, num_unpartitioned;
761 referenced_var_iterator rvi;
764 num_vuses = num_vdefs = num_partitioned = num_unpartitioned = 0;
766 if (num_vuses_p || num_vdefs_p)
768 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
770 tree stmt = bsi_stmt (bsi);
771 if (stmt_references_memory_p (stmt))
773 num_vuses += NUM_SSA_OPERANDS (stmt, SSA_OP_VUSE);
774 num_vdefs += NUM_SSA_OPERANDS (stmt, SSA_OP_VDEF);
778 if (num_partitioned_p || num_unpartitioned_p)
779 FOR_EACH_REFERENCED_VAR (sym, rvi)
781 if (is_gimple_reg (sym))
784 if (memory_partition (sym))
791 *num_vdefs_p = num_vdefs;
794 *num_vuses_p = num_vuses;
796 if (num_partitioned_p)
797 *num_partitioned_p = num_partitioned;
799 if (num_unpartitioned_p)
800 *num_unpartitioned_p = num_unpartitioned;
804 /* The list is sorted by increasing partitioning score (PSCORE).
805 This score is computed such that symbols with high scores are
806 those that are least likely to be partitioned. Given a symbol
807 MP->VAR, PSCORE(S) is the result of the following weighted sum
809 PSCORE(S) = FW * 64 + FR * 32
816 FW Execution frequency of writes to S
817 FR Execution frequency of reads from S
818 DW Number of direct writes to S
819 DR Number of direct reads from S
820 IW Number of indirect writes to S
821 IR Number of indirect reads from S
822 NO_ALIAS State of the NO_ALIAS* flags
824 The basic idea here is that symbols that are frequently
825 written-to in hot paths of the code are the last to be considered
829 mem_sym_score (mem_sym_stats_t mp)
831 return mp->frequency_writes * 64 + mp->frequency_reads * 32
832 + mp->num_direct_writes * 16 + mp->num_direct_reads * 8
833 + mp->num_indirect_writes * 4 + mp->num_indirect_reads * 2
834 + var_ann (mp->var)->noalias_state;
838 /* Dump memory reference stats for function CFUN to FILE. */
841 dump_mem_ref_stats (FILE *file)
843 long actual_num_vuses, actual_num_vdefs;
844 long num_partitioned, num_unpartitioned;
845 struct mem_ref_stats_d *stats;
847 stats = gimple_mem_ref_stats (cfun);
849 count_mem_refs (&actual_num_vuses, &actual_num_vdefs, &num_partitioned,
852 fprintf (file, "\nMemory reference statistics for %s\n\n",
853 lang_hooks.decl_printable_name (current_function_decl, 2));
855 fprintf (file, "Number of memory statements: %ld\n",
856 stats->num_mem_stmts);
857 fprintf (file, "Number of call sites: %ld\n",
858 stats->num_call_sites);
859 fprintf (file, "Number of pure/const call sites: %ld\n",
860 stats->num_pure_const_call_sites);
861 fprintf (file, "Number of asm sites: %ld\n",
862 stats->num_asm_sites);
863 fprintf (file, "Estimated number of loads: %ld (%ld/stmt)\n",
865 (stats->num_mem_stmts)
866 ? CEIL (stats->num_vuses, stats->num_mem_stmts)
868 fprintf (file, "Actual number of loads: %ld (%ld/stmt)\n",
870 (stats->num_mem_stmts)
871 ? CEIL (actual_num_vuses, stats->num_mem_stmts)
874 if (actual_num_vuses > stats->num_vuses + (stats->num_vuses / 25))
875 fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
877 fprintf (file, "Estimated number of stores: %ld (%ld/stmt)\n",
879 (stats->num_mem_stmts)
880 ? CEIL (stats->num_vdefs, stats->num_mem_stmts)
882 fprintf (file, "Actual number of stores: %ld (%ld/stmt)\n",
884 (stats->num_mem_stmts)
885 ? CEIL (actual_num_vdefs, stats->num_mem_stmts)
888 if (actual_num_vdefs > stats->num_vdefs + (stats->num_vdefs / 25))
889 fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
891 fprintf (file, "Partitioning thresholds: MAX = %d AVG = %d "
892 "(%sNEED TO PARTITION)\n", MAX_ALIASED_VOPS, AVG_ALIASED_VOPS,
893 stats->num_mem_stmts && need_to_partition_p (stats) ? "" : "NO ");
894 fprintf (file, "Number of partitioned symbols: %ld\n", num_partitioned);
895 fprintf (file, "Number of unpartitioned symbols: %ld\n", num_unpartitioned);
899 /* Dump memory reference stats for function FN to stderr. */
902 debug_mem_ref_stats (void)
904 dump_mem_ref_stats (stderr);
908 /* Dump memory reference stats for variable VAR to FILE. */
911 dump_mem_sym_stats (FILE *file, tree var)
913 mem_sym_stats_t stats = mem_sym_stats (cfun, var);
918 fprintf (file, "read frequency: %6ld, write frequency: %6ld, "
919 "direct reads: %3ld, direct writes: %3ld, "
920 "indirect reads: %4ld, indirect writes: %4ld, symbol: ",
921 stats->frequency_reads, stats->frequency_writes,
922 stats->num_direct_reads, stats->num_direct_writes,
923 stats->num_indirect_reads, stats->num_indirect_writes);
924 print_generic_expr (file, stats->var, 0);
925 fprintf (file, ", tags: ");
926 dump_decl_set (file, stats->parent_tags);
930 /* Dump memory reference stats for variable VAR to stderr. */
933 debug_mem_sym_stats (tree var)
935 dump_mem_sym_stats (stderr, var);
938 /* Dump memory reference stats for variable VAR to FILE. For use
939 of tree-dfa.c:dump_variable. */
942 dump_mem_sym_stats_for_var (FILE *file, tree var)
944 mem_sym_stats_t stats = mem_sym_stats (cfun, var);
949 fprintf (file, ", score: %ld", mem_sym_score (stats));
950 fprintf (file, ", direct reads: %ld", stats->num_direct_reads);
951 fprintf (file, ", direct writes: %ld", stats->num_direct_writes);
952 fprintf (file, ", indirect reads: %ld", stats->num_indirect_reads);
953 fprintf (file, ", indirect writes: %ld", stats->num_indirect_writes);
956 /* Dump memory reference stats for all memory symbols to FILE. */
959 dump_all_mem_sym_stats (FILE *file)
961 referenced_var_iterator rvi;
964 FOR_EACH_REFERENCED_VAR (sym, rvi)
966 if (is_gimple_reg (sym))
969 dump_mem_sym_stats (file, sym);
974 /* Dump memory reference stats for all memory symbols to stderr. */
977 debug_all_mem_sym_stats (void)
979 dump_all_mem_sym_stats (stderr);
983 /* Dump the MP_INFO array to FILE. */
986 dump_mp_info (FILE *file, VEC(mem_sym_stats_t,heap) *mp_info)
989 mem_sym_stats_t mp_p;
991 for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
992 if (!mp_p->partitioned_p)
993 dump_mem_sym_stats (file, mp_p->var);
997 /* Dump the MP_INFO array to stderr. */
1000 debug_mp_info (VEC(mem_sym_stats_t,heap) *mp_info)
1002 dump_mp_info (stderr, mp_info);
1006 /* Update memory reference stats for symbol VAR in statement STMT.
1007 NUM_DIRECT_READS and NUM_DIRECT_WRITES specify the number of times
1008 that VAR is read/written in STMT (indirect reads/writes are not
1009 recorded by this function, see compute_memory_partitions). */
1012 update_mem_sym_stats_from_stmt (tree var, tree stmt, long num_direct_reads,
1013 long num_direct_writes)
1015 mem_sym_stats_t stats;
1017 gcc_assert (num_direct_reads >= 0 && num_direct_writes >= 0);
1019 stats = get_mem_sym_stats_for (var);
1021 stats->num_direct_reads += num_direct_reads;
1022 stats->frequency_reads += ((long) bb_for_stmt (stmt)->frequency
1023 * num_direct_reads);
1025 stats->num_direct_writes += num_direct_writes;
1026 stats->frequency_writes += ((long) bb_for_stmt (stmt)->frequency
1027 * num_direct_writes);
1031 /* Given two MP_INFO entries MP1 and MP2, return -1 if MP1->VAR should
1032 be partitioned before MP2->VAR, 0 if they are the same or 1 if
1033 MP1->VAR should be partitioned after MP2->VAR. */
1036 compare_mp_info_entries (mem_sym_stats_t mp1, mem_sym_stats_t mp2)
1038 long pscore1 = mem_sym_score (mp1);
1039 long pscore2 = mem_sym_score (mp2);
1041 if (pscore1 < pscore2)
1043 else if (pscore1 > pscore2)
1046 return DECL_UID (mp1->var) - DECL_UID (mp2->var);
1050 /* Comparison routine for qsort. The list is sorted by increasing
1051 partitioning score (PSCORE). This score is computed such that
1052 symbols with high scores are those that are least likely to be
1056 mp_info_cmp (const void *p, const void *q)
1058 mem_sym_stats_t e1 = *((const mem_sym_stats_t *) p);
1059 mem_sym_stats_t e2 = *((const mem_sym_stats_t *) q);
1060 return compare_mp_info_entries (e1, e2);
1064 /* Sort the array of reference counts used to compute memory partitions.
1065 Elements are sorted in ascending order of execution frequency and
1066 descending order of virtual operators needed. */
1069 sort_mp_info (VEC(mem_sym_stats_t,heap) *list)
1071 unsigned num = VEC_length (mem_sym_stats_t, list);
1078 if (compare_mp_info_entries (VEC_index (mem_sym_stats_t, list, 0),
1079 VEC_index (mem_sym_stats_t, list, 1)) > 0)
1081 /* Swap elements if they are in the wrong order. */
1082 mem_sym_stats_t tmp = VEC_index (mem_sym_stats_t, list, 0);
1083 VEC_replace (mem_sym_stats_t, list, 0,
1084 VEC_index (mem_sym_stats_t, list, 1));
1085 VEC_replace (mem_sym_stats_t, list, 1, tmp);
1091 /* There are 3 or more elements, call qsort. */
1092 qsort (VEC_address (mem_sym_stats_t, list),
1093 VEC_length (mem_sym_stats_t, list),
1094 sizeof (mem_sym_stats_t),
1099 /* Return the memory partition tag (MPT) associated with memory
1103 get_mpt_for (tree sym)
1107 /* Don't create a new tag unnecessarily. */
1108 mpt = memory_partition (sym);
1109 if (mpt == NULL_TREE)
1111 mpt = create_tag_raw (MEMORY_PARTITION_TAG, TREE_TYPE (sym), "MPT");
1112 TREE_ADDRESSABLE (mpt) = 0;
1113 add_referenced_var (mpt);
1114 VEC_safe_push (tree, heap, gimple_ssa_operands (cfun)->mpt_table, mpt);
1115 gcc_assert (MPT_SYMBOLS (mpt) == NULL);
1116 set_memory_partition (sym, mpt);
1123 /* Add MP_P->VAR to a memory partition and return the partition. */
1126 find_partition_for (mem_sym_stats_t mp_p)
1129 VEC(tree,heap) *mpt_table;
1132 mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1135 /* Find an existing partition for MP_P->VAR. */
1136 for (i = 0; VEC_iterate (tree, mpt_table, i, mpt); i++)
1138 mem_sym_stats_t mpt_stats;
1140 /* If MPT does not have any symbols yet, use it. */
1141 if (MPT_SYMBOLS (mpt) == NULL)
1144 /* Otherwise, see if MPT has common parent tags with MP_P->VAR,
1145 but avoid grouping clobbered variables with non-clobbered
1146 variables (otherwise, this tends to creates a single memory
1147 partition because other call-clobbered variables may have
1148 common parent tags with non-clobbered ones). */
1149 mpt_stats = get_mem_sym_stats_for (mpt);
1150 if (mp_p->parent_tags
1151 && mpt_stats->parent_tags
1152 && is_call_clobbered (mpt) == is_call_clobbered (mp_p->var)
1153 && bitmap_intersect_p (mpt_stats->parent_tags, mp_p->parent_tags))
1156 /* If no common parent tags are found, see if both MPT and
1157 MP_P->VAR are call-clobbered. */
1158 if (is_call_clobbered (mpt) && is_call_clobbered (mp_p->var))
1162 if (mpt == NULL_TREE)
1163 mpt = get_mpt_for (mp_p->var);
1165 set_memory_partition (mp_p->var, mpt);
1167 mp_p->partitioned_p = true;
1169 mark_sym_for_renaming (mp_p->var);
1170 mark_sym_for_renaming (mpt);
1176 /* Rewrite the alias set for TAG to use the newly created partitions.
1177 If TAG is NULL, rewrite the set of call-clobbered variables.
1178 NEW_ALIASES is a scratch bitmap to build the new set of aliases for
1182 rewrite_alias_set_for (tree tag, bitmap new_aliases)
1188 EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, i, bi)
1190 sym = referenced_var (i);
1191 mpt = memory_partition (sym);
1193 bitmap_set_bit (new_aliases, DECL_UID (mpt));
1195 bitmap_set_bit (new_aliases, DECL_UID (sym));
1198 /* Rebuild the may-alias array for TAG. */
1199 bitmap_copy (MTAG_ALIASES (tag), new_aliases);
1203 /* Determine how many virtual operands can be saved by partitioning
1204 MP_P->VAR into MPT. When a symbol S is thrown inside a partition
1205 P, every virtual operand that used to reference S will now
1206 reference P. Whether it reduces the number of virtual operands
1209 1- Direct references to S are never saved. Instead of the virtual
1210 operand to S, we will now have a virtual operand to P.
1212 2- Indirect references to S are reduced only for those memory tags
1213 holding S that already had other symbols partitioned into P.
1214 For instance, if a memory tag T has the alias set { a b S c },
1215 the first time we partition S into P, the alias set will become
1216 { a b P c }, so no virtual operands will be saved. However, if
1217 we now partition symbol 'c' into P, then the alias set for T
1218 will become { a b P }, so we will be saving one virtual operand
1219 for every indirect reference to 'c'.
1221 3- Is S is call-clobbered, we save as many virtual operands as
1222 call/asm sites exist in the code, but only if other
1223 call-clobbered symbols have been grouped into P. The first
1224 call-clobbered symbol that we group does not produce any
1227 MEM_REF_STATS points to CFUN's memory reference information. */
1230 estimate_vop_reduction (struct mem_ref_stats_d *mem_ref_stats,
1231 mem_sym_stats_t mp_p, tree mpt)
1235 mem_sym_stats_t mpt_stats;
1237 /* We should only get symbols with indirect references here. */
1238 gcc_assert (mp_p->num_indirect_reads > 0 || mp_p->num_indirect_writes > 0);
1240 /* Note that the only statistics we keep for MPT is the set of
1241 parent tags to know which memory tags have had alias members
1242 partitioned, and the indicator has_call_clobbered_vars.
1243 Reference counts are not important for MPT. */
1244 mpt_stats = get_mem_sym_stats_for (mpt);
1246 /* Traverse all the parent tags for MP_P->VAR. For every tag T, if
1247 partition P is already grouping aliases of T, then reduce the
1248 number of virtual operands by the number of direct references
1250 if (mp_p->parent_tags)
1252 if (mpt_stats->parent_tags == NULL)
1253 mpt_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1255 EXECUTE_IF_SET_IN_BITMAP (mp_p->parent_tags, 0, i, bi)
1257 if (bitmap_bit_p (mpt_stats->parent_tags, i))
1259 /* Partition MPT is already partitioning symbols in the
1260 alias set for TAG. This means that we are now saving
1261 1 virtual operand for every direct reference to TAG. */
1262 tree tag = referenced_var (i);
1263 mem_sym_stats_t tag_stats = mem_sym_stats (cfun, tag);
1264 mem_ref_stats->num_vuses -= tag_stats->num_direct_reads;
1265 mem_ref_stats->num_vdefs -= tag_stats->num_direct_writes;
1269 /* This is the first symbol in tag I's alias set that is
1270 being grouped under MPT. We will not save any
1271 virtual operands this time, but record that MPT is
1272 grouping a symbol from TAG's alias set so that the
1273 next time we get the savings. */
1274 bitmap_set_bit (mpt_stats->parent_tags, i);
1279 /* If MP_P->VAR is call-clobbered, and MPT is already grouping
1280 call-clobbered symbols, then we will save as many virtual
1281 operands as asm/call sites there are. */
1282 if (is_call_clobbered (mp_p->var))
1284 if (mpt_stats->has_call_clobbered_vars)
1285 mem_ref_stats->num_vdefs -= mem_ref_stats->num_call_sites
1286 + mem_ref_stats->num_asm_sites;
1288 mpt_stats->has_call_clobbered_vars = true;
1293 /* Helper for compute_memory_partitions. Transfer reference counts
1294 from pointers to their pointed-to sets. Counters for pointers were
1295 computed by update_alias_info. MEM_REF_STATS points to CFUN's
1296 memory reference information. */
1299 update_reference_counts (struct mem_ref_stats_d *mem_ref_stats)
1303 mem_sym_stats_t sym_stats;
1305 for (i = 1; i < num_ssa_names; i++)
1308 struct ptr_info_def *pi;
1312 && POINTER_TYPE_P (TREE_TYPE (ptr))
1313 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1314 && pi->is_dereferenced)
1319 mem_sym_stats_t ptr_stats, tag_stats;
1321 /* If PTR has flow-sensitive points-to information, use
1322 PTR's name tag, otherwise use the symbol tag associated
1323 with PTR's symbol. */
1324 if (pi->name_mem_tag)
1325 tag = pi->name_mem_tag;
1327 tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
1329 ptr_stats = get_mem_sym_stats_for (ptr);
1330 tag_stats = get_mem_sym_stats_for (tag);
1332 /* TAG has as many direct references as dereferences we
1333 found for its parent pointer. */
1334 tag_stats->num_direct_reads += ptr_stats->num_direct_reads;
1335 tag_stats->num_direct_writes += ptr_stats->num_direct_writes;
1337 /* All the dereferences of pointer PTR are considered direct
1338 references to PTR's memory tag (TAG). In turn,
1339 references to TAG will become virtual operands for every
1340 symbol in TAG's alias set. So, for every symbol ALIAS in
1341 TAG's alias set, add as many indirect references to ALIAS
1342 as direct references there are for TAG. */
1343 if (MTAG_ALIASES (tag))
1344 EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, j, bj)
1346 tree alias = referenced_var (j);
1347 sym_stats = get_mem_sym_stats_for (alias);
1349 /* All the direct references to TAG are indirect references
1351 sym_stats->num_indirect_reads += ptr_stats->num_direct_reads;
1352 sym_stats->num_indirect_writes += ptr_stats->num_direct_writes;
1353 sym_stats->frequency_reads += ptr_stats->frequency_reads;
1354 sym_stats->frequency_writes += ptr_stats->frequency_writes;
1356 /* Indicate that TAG is one of ALIAS's parent tags. */
1357 if (sym_stats->parent_tags == NULL)
1358 sym_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1359 bitmap_set_bit (sym_stats->parent_tags, DECL_UID (tag));
1364 /* Call-clobbered symbols are indirectly written at every
1366 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1368 tree sym = referenced_var (i);
1369 sym_stats = get_mem_sym_stats_for (sym);
1370 sym_stats->num_indirect_writes += mem_ref_stats->num_call_sites
1371 + mem_ref_stats->num_asm_sites;
1374 /* Addressable symbols are indirectly written at some ASM sites.
1375 Since only ASM sites that clobber memory actually affect
1376 addressable symbols, this is an over-estimation. */
1377 EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, i, bi)
1379 tree sym = referenced_var (i);
1380 sym_stats = get_mem_sym_stats_for (sym);
1381 sym_stats->num_indirect_writes += mem_ref_stats->num_asm_sites;
1386 /* Helper for compute_memory_partitions. Add all memory symbols to
1387 *MP_INFO_P and compute the initial estimate for the total number of
1388 virtual operands needed. MEM_REF_STATS points to CFUN's memory
1389 reference information. On exit, *TAGS_P will contain the list of
1390 memory tags whose alias set need to be rewritten after
1394 build_mp_info (struct mem_ref_stats_d *mem_ref_stats,
1395 VEC(mem_sym_stats_t,heap) **mp_info_p,
1396 VEC(tree,heap) **tags_p)
1399 referenced_var_iterator rvi;
1401 FOR_EACH_REFERENCED_VAR (var, rvi)
1403 mem_sym_stats_t sym_stats;
1406 /* We are only interested in memory symbols other than MPTs. */
1407 if (is_gimple_reg (var) || TREE_CODE (var) == MEMORY_PARTITION_TAG)
1410 /* Collect memory tags into the TAGS array so that we can
1411 rewrite their alias sets after partitioning. */
1412 if (MTAG_P (var) && MTAG_ALIASES (var))
1413 VEC_safe_push (tree, heap, *tags_p, var);
1415 /* Since we are going to re-compute partitions, any symbols that
1416 used to belong to a partition must be detached from it and
1417 marked for renaming. */
1418 if ((old_mpt = memory_partition (var)) != NULL)
1420 mark_sym_for_renaming (old_mpt);
1421 set_memory_partition (var, NULL_TREE);
1422 mark_sym_for_renaming (var);
1425 sym_stats = get_mem_sym_stats_for (var);
1427 /* Add VAR's reference info to MP_INFO. Note that the only
1428 symbols that make sense to partition are those that have
1429 indirect references. If a symbol S is always directly
1430 referenced, partitioning it will not reduce the number of
1431 virtual operators. The only symbols that are profitable to
1432 partition are those that belong to alias sets and/or are
1434 if (sym_stats->num_indirect_reads > 0
1435 || sym_stats->num_indirect_writes > 0)
1436 VEC_safe_push (mem_sym_stats_t, heap, *mp_info_p, sym_stats);
1438 /* Update the number of estimated VOPS. Note that direct
1439 references to memory tags are always counted as indirect
1440 references to their alias set members, so if a memory tag has
1441 aliases, do not count its direct references to avoid double
1443 if (!MTAG_P (var) || !MTAG_ALIASES (var))
1445 mem_ref_stats->num_vuses += sym_stats->num_direct_reads;
1446 mem_ref_stats->num_vdefs += sym_stats->num_direct_writes;
1449 mem_ref_stats->num_vuses += sym_stats->num_indirect_reads;
1450 mem_ref_stats->num_vdefs += sym_stats->num_indirect_writes;
1455 /* Compute memory partitions. A memory partition (MPT) is an
1456 arbitrary grouping of memory symbols, such that references to one
1457 member of the group is considered a reference to all the members of
1460 As opposed to alias sets in memory tags, the grouping into
1461 partitions is completely arbitrary and only done to reduce the
1462 number of virtual operands. The only rule that needs to be
1463 observed when creating memory partitions is that given two memory
1464 partitions MPT.i and MPT.j, they must not contain symbols in
1467 Memory partitions are used when putting the program into Memory-SSA
1468 form. In particular, in Memory-SSA PHI nodes are not computed for
1469 individual memory symbols. They are computed for memory
1470 partitions. This reduces the amount of PHI nodes in the SSA graph
1471 at the expense of precision (i.e., it makes unrelated stores affect
1474 However, it is possible to increase precision by changing this
1475 partitioning scheme. For instance, if the partitioning scheme is
1476 such that get_mpt_for is the identity function (that is,
1477 get_mpt_for (s) = s), this will result in ultimate precision at the
1478 expense of huge SSA webs.
1480 At the other extreme, a partitioning scheme that groups all the
1481 symbols in the same set results in minimal SSA webs and almost
1482 total loss of precision.
1484 There partitioning heuristic uses three parameters to decide the
1485 order in which symbols are processed. The list of symbols is
1486 sorted so that symbols that are more likely to be partitioned are
1487 near the top of the list:
1489 - Execution frequency. If a memory references is in a frequently
1490 executed code path, grouping it into a partition may block useful
1491 transformations and cause sub-optimal code generation. So, the
1492 partition heuristic tries to avoid grouping symbols with high
1493 execution frequency scores. Execution frequency is taken
1494 directly from the basic blocks where every reference is made (see
1495 update_mem_sym_stats_from_stmt), which in turn uses the
1496 profile guided machinery, so if the program is compiled with PGO
1497 enabled, more accurate partitioning decisions will be made.
1499 - Number of references. Symbols with few references in the code,
1500 are partitioned before symbols with many references.
1502 - NO_ALIAS attributes. Symbols with any of the NO_ALIAS*
1503 attributes are partitioned after symbols marked MAY_ALIAS.
1505 Once the list is sorted, the partitioning proceeds as follows:
1507 1- For every symbol S in MP_INFO, create a new memory partition MP,
1508 if necessary. To avoid memory partitions that contain symbols
1509 from non-conflicting alias sets, memory partitions are
1510 associated to the memory tag that holds S in its alias set. So,
1511 when looking for a memory partition for S, the memory partition
1512 associated with one of the memory tags holding S is chosen. If
1513 none exists, a new one is created.
1515 2- Add S to memory partition MP.
1517 3- Reduce by 1 the number of VOPS for every memory tag holding S.
1519 4- If the total number of VOPS is less than MAX_ALIASED_VOPS or the
1520 average number of VOPS per statement is less than
1521 AVG_ALIASED_VOPS, stop. Otherwise, go to the next symbol in the
1525 compute_memory_partitions (void)
1529 mem_sym_stats_t mp_p;
1530 VEC(mem_sym_stats_t,heap) *mp_info;
1532 VEC(tree,heap) *tags;
1533 struct mem_ref_stats_d *mem_ref_stats;
1534 int prev_max_aliased_vops;
1536 mem_ref_stats = gimple_mem_ref_stats (cfun);
1537 gcc_assert (mem_ref_stats->num_vuses == 0 && mem_ref_stats->num_vdefs == 0);
1539 if (mem_ref_stats->num_mem_stmts == 0)
1542 timevar_push (TV_MEMORY_PARTITIONING);
1546 prev_max_aliased_vops = MAX_ALIASED_VOPS;
1548 /* Since we clearly cannot lower the number of virtual operators
1549 below the total number of memory statements in the function, we
1550 may need to adjust MAX_ALIASED_VOPS beforehand. */
1551 if (MAX_ALIASED_VOPS < mem_ref_stats->num_mem_stmts)
1552 MAX_ALIASED_VOPS = mem_ref_stats->num_mem_stmts;
1554 /* Update reference stats for all the pointed-to variables and
1556 update_reference_counts (mem_ref_stats);
1558 /* Add all the memory symbols to MP_INFO. */
1559 build_mp_info (mem_ref_stats, &mp_info, &tags);
1561 /* No partitions required if we are below the threshold. */
1562 if (!need_to_partition_p (mem_ref_stats))
1565 fprintf (dump_file, "\nMemory partitioning NOT NEEDED for %s\n",
1566 get_name (current_function_decl));
1570 /* Sort the MP_INFO array so that symbols that should be partitioned
1571 first are near the top of the list. */
1572 sort_mp_info (mp_info);
1576 fprintf (dump_file, "\nMemory partitioning NEEDED for %s\n\n",
1577 get_name (current_function_decl));
1578 fprintf (dump_file, "Memory symbol references before partitioning:\n");
1579 dump_mp_info (dump_file, mp_info);
1582 /* Create partitions for variables in MP_INFO until we have enough
1583 to lower the total number of VOPS below MAX_ALIASED_VOPS or if
1584 the average number of VOPS per statement is below
1585 AVG_ALIASED_VOPS. */
1586 for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
1590 /* If we are below the threshold, stop. */
1591 if (!need_to_partition_p (mem_ref_stats))
1594 mpt = find_partition_for (mp_p);
1595 estimate_vop_reduction (mem_ref_stats, mp_p, mpt);
1598 /* After partitions have been created, rewrite alias sets to use
1599 them instead of the original symbols. This way, if the alias set
1600 was computed as { a b c d e f }, and the subset { b e f } was
1601 grouped into partition MPT.3, then the new alias set for the tag
1602 will be { a c d MPT.3 }.
1604 Note that this is not strictly necessary. The operand scanner
1605 will always check if a symbol belongs to a partition when adding
1606 virtual operands. However, by reducing the size of the alias
1607 sets to be scanned, the work needed inside the operand scanner is
1608 significantly reduced. */
1609 new_aliases = BITMAP_ALLOC (&alias_bitmap_obstack);
1611 for (i = 0; VEC_iterate (tree, tags, i, tag); i++)
1613 rewrite_alias_set_for (tag, new_aliases);
1614 bitmap_clear (new_aliases);
1617 BITMAP_FREE (new_aliases);
1621 fprintf (dump_file, "\nMemory symbol references after partitioning:\n");
1622 dump_mp_info (dump_file, mp_info);
1626 /* Free allocated memory. */
1627 VEC_free (mem_sym_stats_t, heap, mp_info);
1628 VEC_free (tree, heap, tags);
1630 MAX_ALIASED_VOPS = prev_max_aliased_vops;
1632 timevar_pop (TV_MEMORY_PARTITIONING);
1636 /* Compute may-alias information for every variable referenced in function
1639 Alias analysis proceeds in 3 main phases:
1641 1- Points-to and escape analysis.
1643 This phase walks the use-def chains in the SSA web looking for three
1646 * Assignments of the form P_i = &VAR
1647 * Assignments of the form P_i = malloc()
1648 * Pointers and ADDR_EXPR that escape the current function.
1650 The concept of 'escaping' is the same one used in the Java world. When
1651 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
1652 outside of the current function. So, assignment to global variables,
1653 function arguments and returning a pointer are all escape sites, as are
1654 conversions between pointers and integers.
1656 This is where we are currently limited. Since not everything is renamed
1657 into SSA, we lose track of escape properties when a pointer is stashed
1658 inside a field in a structure, for instance. In those cases, we are
1659 assuming that the pointer does escape.
1661 We use escape analysis to determine whether a variable is
1662 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
1663 is call-clobbered. If a pointer P_i escapes, then all the variables
1664 pointed-to by P_i (and its memory tag) also escape.
1666 2- Compute flow-sensitive aliases
1668 We have two classes of memory tags. Memory tags associated with the
1669 pointed-to data type of the pointers in the program. These tags are
1670 called "symbol memory tag" (SMT). The other class are those associated
1671 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
1672 when adding operands for an INDIRECT_REF *P_i, we will first check
1673 whether P_i has a name tag, if it does we use it, because that will have
1674 more precise aliasing information. Otherwise, we use the standard symbol
1677 In this phase, we go through all the pointers we found in points-to
1678 analysis and create alias sets for the name memory tags associated with
1679 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
1680 it points to and its tag.
1683 3- Compute flow-insensitive aliases
1685 This pass will compare the alias set of every symbol memory tag and
1686 every addressable variable found in the program. Given a symbol
1687 memory tag SMT and an addressable variable V. If the alias sets of
1688 SMT and V conflict (as computed by may_alias_p), then V is marked
1689 as an alias tag and added to the alias set of SMT.
1691 For instance, consider the following function:
1707 After aliasing analysis has finished, the symbol memory tag for pointer
1708 'p' will have two aliases, namely variables 'a' and 'b'. Every time
1709 pointer 'p' is dereferenced, we want to mark the operation as a
1710 potential reference to 'a' and 'b'.
1720 # p_1 = PHI <p_4(1), p_6(2)>;
1735 In certain cases, the list of may aliases for a pointer may grow too
1736 large. This may cause an explosion in the number of virtual operands
1737 inserted in the code. Resulting in increased memory consumption and
1740 When the number of virtual operands needed to represent aliased
1741 loads and stores grows too large (configurable with option --param
1742 max-aliased-vops and --param avg-aliased-vops), alias sets are
1743 grouped to avoid severe compile-time slow downs and memory
1744 consumption. See compute_memory_partitions. */
1747 compute_may_aliases (void)
1749 struct alias_info *ai;
1751 timevar_push (TV_TREE_MAY_ALIAS);
1753 memset (&alias_stats, 0, sizeof (alias_stats));
1755 /* Initialize aliasing information. */
1756 ai = init_alias_info ();
1758 /* For each pointer P_i, determine the sets of variables that P_i may
1759 point-to. For every addressable variable V, determine whether the
1760 address of V escapes the current function, making V call-clobbered
1761 (i.e., whether &V is stored in a global variable or if its passed as a
1762 function call argument). */
1763 compute_points_to_sets (ai);
1765 /* Collect all pointers and addressable variables, compute alias sets,
1766 create memory tags for pointers and promote variables whose address is
1767 not needed anymore. */
1768 setup_pointers_and_addressables (ai);
1770 /* Compute type-based flow-insensitive aliasing for all the type
1772 compute_flow_insensitive_aliasing (ai);
1774 /* Compute flow-sensitive, points-to based aliasing for all the name
1776 compute_flow_sensitive_aliasing (ai);
1778 /* Compute call clobbering information. */
1779 compute_call_clobbered (ai);
1781 /* If the program makes no reference to global variables, but it
1782 contains a mixture of pure and non-pure functions, then we need
1783 to create use-def and def-def links between these functions to
1784 avoid invalid transformations on them. */
1785 maybe_create_global_var ();
1787 /* If the program contains ref-all pointers, finalize may-alias information
1788 for them. This pass needs to be run after call-clobbering information
1789 has been computed. */
1790 if (ai->ref_all_symbol_mem_tag)
1791 finalize_ref_all_pointers (ai);
1793 /* Compute memory partitions for every memory variable. */
1794 compute_memory_partitions ();
1796 /* Remove partitions with no symbols. Partitions may end up with an
1797 empty MPT_SYMBOLS set if a previous round of alias analysis
1798 needed to partition more symbols. Since we don't need those
1799 partitions anymore, remove them to free up the space. */
1803 VEC(tree,heap) *mpt_table;
1805 mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1807 while (i < VEC_length (tree, mpt_table))
1809 mpt = VEC_index (tree, mpt_table, i);
1810 if (MPT_SYMBOLS (mpt) == NULL)
1811 VEC_unordered_remove (tree, mpt_table, i);
1817 /* Populate all virtual operands and newly promoted register operands. */
1819 block_stmt_iterator bsi;
1822 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1823 update_stmt_if_modified (bsi_stmt (bsi));
1826 /* Debugging dumps. */
1829 dump_mem_ref_stats (dump_file);
1830 dump_alias_info (dump_file);
1831 dump_points_to_info (dump_file);
1833 if (dump_flags & TDF_STATS)
1834 dump_alias_stats (dump_file);
1836 if (dump_flags & TDF_DETAILS)
1837 dump_referenced_vars (dump_file);
1840 /* Report strict aliasing violations. */
1841 strict_aliasing_warning_backend ();
1843 /* Deallocate memory used by aliasing data structures. */
1844 delete_alias_info (ai);
1846 if (need_ssa_update_p ())
1847 update_ssa (TODO_update_ssa);
1849 timevar_pop (TV_TREE_MAY_ALIAS);
1854 /* Data structure used to count the number of dereferences to PTR
1855 inside an expression. */
1863 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
1864 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
1867 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
1869 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
1871 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
1872 pointer 'ptr' is *not* dereferenced, it is simply used to compute
1873 the address of 'fld' as 'ptr + offsetof(fld)'. */
1874 if (TREE_CODE (*tp) == ADDR_EXPR)
1880 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
1887 /* Count the number of direct and indirect uses for pointer PTR in
1888 statement STMT. The number of direct uses is stored in
1889 *NUM_USES_P. Indirect references are counted separately depending
1890 on whether they are store or load operations. The counts are
1891 stored in *NUM_STORES_P and *NUM_LOADS_P. */
1894 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
1895 unsigned *num_loads_p, unsigned *num_stores_p)
1904 /* Find out the total number of uses of PTR in STMT. */
1905 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
1909 /* Now count the number of indirect references to PTR. This is
1910 truly awful, but we don't have much choice. There are no parent
1911 pointers inside INDIRECT_REFs, so an expression like
1912 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
1913 find all the indirect and direct uses of x_1 inside. The only
1914 shortcut we can take is the fact that GIMPLE only allows
1915 INDIRECT_REFs inside the expressions below. */
1916 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1917 || (TREE_CODE (stmt) == RETURN_EXPR
1918 && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
1919 || TREE_CODE (stmt) == ASM_EXPR
1920 || TREE_CODE (stmt) == CALL_EXPR)
1924 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
1926 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1927 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1929 else if (TREE_CODE (stmt) == RETURN_EXPR)
1931 tree e = TREE_OPERAND (stmt, 0);
1932 lhs = GIMPLE_STMT_OPERAND (e, 0);
1933 rhs = GIMPLE_STMT_OPERAND (e, 1);
1935 else if (TREE_CODE (stmt) == ASM_EXPR)
1937 lhs = ASM_OUTPUTS (stmt);
1938 rhs = ASM_INPUTS (stmt);
1947 && (TREE_CODE (lhs) == TREE_LIST
1949 || GIMPLE_STMT_P (lhs)))
1951 struct count_ptr_d count;
1954 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
1955 *num_stores_p = count.count;
1959 && (TREE_CODE (rhs) == TREE_LIST
1961 || GIMPLE_STMT_P (rhs)))
1963 struct count_ptr_d count;
1966 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
1967 *num_loads_p = count.count;
1971 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
1974 /* Remove memory references stats for function FN. */
1977 delete_mem_ref_stats (struct function *fn)
1979 if (gimple_mem_ref_stats (fn)->mem_sym_stats)
1981 free_alloc_pool (mem_sym_stats_pool);
1982 pointer_map_destroy (gimple_mem_ref_stats (fn)->mem_sym_stats);
1984 gimple_mem_ref_stats (fn)->mem_sym_stats = NULL;
1988 /* Initialize memory reference stats. */
1991 init_mem_ref_stats (void)
1993 struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
1995 mem_sym_stats_pool = create_alloc_pool ("Mem sym stats",
1996 sizeof (struct mem_sym_stats_d),
1998 memset (mem_ref_stats, 0, sizeof (struct mem_ref_stats_d));
1999 mem_ref_stats->mem_sym_stats = pointer_map_create ();
2003 /* Helper for init_alias_info. Reset existing aliasing information. */
2006 reset_alias_info (void)
2008 referenced_var_iterator rvi;
2011 bitmap active_nmts, all_nmts;
2013 /* Clear the set of addressable variables. We do not need to clear
2014 the TREE_ADDRESSABLE bit on every symbol because we are going to
2015 re-compute addressability here. */
2016 bitmap_clear (gimple_addressable_vars (cfun));
2018 active_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
2019 all_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
2021 /* Clear flow-insensitive alias information from each symbol. */
2022 FOR_EACH_REFERENCED_VAR (var, rvi)
2024 if (is_gimple_reg (var))
2028 MTAG_ALIASES (var) = NULL;
2030 /* Memory partition information will be computed from scratch. */
2031 if (TREE_CODE (var) == MEMORY_PARTITION_TAG)
2032 MPT_SYMBOLS (var) = NULL;
2034 /* Collect all the name tags to determine if we have any
2035 orphaned that need to be removed from the IL. A name tag
2036 will be orphaned if it is not associated with any active SSA
2038 if (TREE_CODE (var) == NAME_MEMORY_TAG)
2039 bitmap_set_bit (all_nmts, DECL_UID (var));
2041 /* Since we are about to re-discover call-clobbered
2042 variables, clear the call-clobbered flag. Variables that
2043 are intrinsically call-clobbered (globals, local statics,
2044 etc) will not be marked by the aliasing code, so we can't
2045 remove them from CALL_CLOBBERED_VARS.
2047 NB: STRUCT_FIELDS are still call clobbered if they are for a
2048 global variable, so we *don't* clear their call clobberedness
2049 just because they are tags, though we will clear it if they
2050 aren't for global variables. */
2051 if (TREE_CODE (var) == NAME_MEMORY_TAG
2052 || TREE_CODE (var) == SYMBOL_MEMORY_TAG
2053 || TREE_CODE (var) == MEMORY_PARTITION_TAG
2054 || !is_global_var (var))
2055 clear_call_clobbered (var);
2058 /* Clear flow-sensitive points-to information from each SSA name. */
2059 for (i = 1; i < num_ssa_names; i++)
2061 tree name = ssa_name (i);
2063 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
2066 if (SSA_NAME_PTR_INFO (name))
2068 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
2070 /* Clear all the flags but keep the name tag to
2071 avoid creating new temporaries unnecessarily. If
2072 this pointer is found to point to a subset or
2073 superset of its former points-to set, then a new
2074 tag will need to be created in create_name_tags. */
2075 pi->pt_anything = 0;
2077 pi->value_escapes_p = 0;
2078 pi->is_dereferenced = 0;
2080 bitmap_clear (pi->pt_vars);
2082 /* Add NAME's name tag to the set of active tags. */
2083 if (pi->name_mem_tag)
2084 bitmap_set_bit (active_nmts, DECL_UID (pi->name_mem_tag));
2088 /* Name memory tags that are no longer associated with an SSA name
2089 are considered stale and should be removed from the IL. All the
2090 name tags that are in the set ALL_NMTS but not in ACTIVE_NMTS are
2091 considered stale and marked for renaming. */
2092 bitmap_and_compl_into (all_nmts, active_nmts);
2093 mark_set_for_renaming (all_nmts);
2095 BITMAP_FREE (all_nmts);
2096 BITMAP_FREE (active_nmts);
2100 /* Initialize the data structures used for alias analysis. */
2102 static struct alias_info *
2103 init_alias_info (void)
2105 struct alias_info *ai;
2106 referenced_var_iterator rvi;
2109 ai = XCNEW (struct alias_info);
2110 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
2111 sbitmap_zero (ai->ssa_names_visited);
2112 ai->processed_ptrs = VEC_alloc (tree, heap, 50);
2113 ai->written_vars = pointer_set_create ();
2114 ai->dereferenced_ptrs_store = pointer_set_create ();
2115 ai->dereferenced_ptrs_load = pointer_set_create ();
2117 /* Clear out all memory reference stats. */
2118 init_mem_ref_stats ();
2120 /* If aliases have been computed before, clear existing information. */
2121 if (gimple_aliases_computed_p (cfun))
2122 reset_alias_info ();
2125 /* If this is the first time we compute aliasing information,
2126 every non-register symbol will need to be put into SSA form
2127 (the initial SSA form only operates on GIMPLE registers). */
2128 FOR_EACH_REFERENCED_VAR (var, rvi)
2129 if (!is_gimple_reg (var))
2130 mark_sym_for_renaming (var);
2133 /* Next time, we will need to reset alias information. */
2134 cfun->gimple_df->aliases_computed_p = true;
2135 if (alias_bitmap_obstack.elements != NULL)
2136 bitmap_obstack_release (&alias_bitmap_obstack);
2137 bitmap_obstack_initialize (&alias_bitmap_obstack);
2143 /* Deallocate memory used by alias analysis. */
2146 delete_alias_info (struct alias_info *ai)
2150 sbitmap_free (ai->ssa_names_visited);
2152 VEC_free (tree, heap, ai->processed_ptrs);
2154 for (i = 0; i < ai->num_addressable_vars; i++)
2155 free (ai->addressable_vars[i]);
2156 free (ai->addressable_vars);
2158 for (i = 0; i < ai->num_pointers; i++)
2159 free (ai->pointers[i]);
2160 free (ai->pointers);
2162 pointer_set_destroy (ai->written_vars);
2163 pointer_set_destroy (ai->dereferenced_ptrs_store);
2164 pointer_set_destroy (ai->dereferenced_ptrs_load);
2167 delete_mem_ref_stats (cfun);
2168 delete_points_to_sets ();
2172 /* Used for hashing to identify pointer infos with identical
2176 eq_ptr_info (const void *p1, const void *p2)
2178 const struct ptr_info_def *n1 = (const struct ptr_info_def *) p1;
2179 const struct ptr_info_def *n2 = (const struct ptr_info_def *) p2;
2180 return bitmap_equal_p (n1->pt_vars, n2->pt_vars);
2184 ptr_info_hash (const void *p)
2186 const struct ptr_info_def *n = (const struct ptr_info_def *) p;
2187 return bitmap_hash (n->pt_vars);
2191 /* Create name tags for all the pointers that have been dereferenced.
2192 We only create a name tag for a pointer P if P is found to point to
2193 a set of variables (so that we can alias them to *P) or if it is
2194 the result of a call to malloc (which means that P cannot point to
2195 anything else nor alias any other variable).
2197 If two pointers P and Q point to the same set of variables, they
2198 are assigned the same name tag. */
2201 create_name_tags (void)
2204 VEC (tree, heap) *with_ptvars = NULL;
2208 /* Collect the list of pointers with a non-empty points to set. */
2209 for (i = 1; i < num_ssa_names; i++)
2211 tree ptr = ssa_name (i);
2212 struct ptr_info_def *pi;
2215 || !POINTER_TYPE_P (TREE_TYPE (ptr))
2216 || !SSA_NAME_PTR_INFO (ptr))
2219 pi = SSA_NAME_PTR_INFO (ptr);
2221 if (pi->pt_anything || !pi->is_dereferenced)
2223 /* No name tags for pointers that have not been
2224 dereferenced or point to an arbitrary location. */
2225 pi->name_mem_tag = NULL_TREE;
2229 /* Set pt_anything on the pointers without pt_vars filled in so
2230 that they are assigned a symbol tag. */
2231 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
2232 VEC_safe_push (tree, heap, with_ptvars, ptr);
2234 set_pt_anything (ptr);
2237 /* If we didn't find any pointers with pt_vars set, we're done. */
2241 ptr_hash = htab_create (10, ptr_info_hash, eq_ptr_info, NULL);
2243 /* Now go through the pointers with pt_vars, and find a name tag
2244 with the same pt_vars as this pointer, or create one if one
2246 for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
2248 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2249 tree old_name_tag = pi->name_mem_tag;
2250 struct ptr_info_def **slot;
2252 /* If PTR points to a set of variables, check if we don't
2253 have another pointer Q with the same points-to set before
2254 creating a tag. If so, use Q's tag instead of creating a
2257 This is important for not creating unnecessary symbols
2258 and also for copy propagation. If we ever need to
2259 propagate PTR into Q or vice-versa, we would run into
2260 problems if they both had different name tags because
2261 they would have different SSA version numbers (which
2262 would force us to take the name tags in and out of SSA). */
2263 slot = (struct ptr_info_def **) htab_find_slot (ptr_hash, pi, INSERT);
2265 pi->name_mem_tag = (*slot)->name_mem_tag;
2270 /* If we didn't find a pointer with the same points-to set
2271 as PTR, create a new name tag if needed. */
2272 if (pi->name_mem_tag == NULL_TREE)
2273 pi->name_mem_tag = get_nmt_for (ptr);
2276 /* If the new name tag computed for PTR is different than
2277 the old name tag that it used to have, then the old tag
2278 needs to be removed from the IL, so we mark it for
2280 if (old_name_tag && old_name_tag != pi->name_mem_tag)
2281 mark_sym_for_renaming (old_name_tag);
2283 /* Inherit volatility from the pointed-to type. */
2284 TREE_THIS_VOLATILE (pi->name_mem_tag)
2285 |= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
2287 /* Mark the new name tag for renaming. */
2288 mark_sym_for_renaming (pi->name_mem_tag);
2291 htab_delete (ptr_hash);
2293 VEC_free (tree, heap, with_ptvars);
2297 /* Union the alias set SET into the may-aliases for TAG. */
2300 union_alias_set_into (tree tag, bitmap set)
2302 bitmap ma = MTAG_ALIASES (tag);
2304 if (bitmap_empty_p (set))
2308 ma = MTAG_ALIASES (tag) = BITMAP_ALLOC (&alias_bitmap_obstack);
2309 bitmap_ior_into (ma, set);
2313 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
2314 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
2315 name tag and the variables it points-to are call-clobbered. Finally, if
2316 P_i escapes and we could not determine where it points to, then all the
2317 variables in the same alias set as *P_i are marked call-clobbered. This
2318 is necessary because we must assume that P_i may take the address of any
2319 variable in the same alias set. */
2322 compute_flow_sensitive_aliasing (struct alias_info *ai)
2327 timevar_push (TV_FLOW_SENSITIVE);
2330 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2332 if (!find_what_p_points_to (ptr))
2333 set_pt_anything (ptr);
2336 create_name_tags ();
2338 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2340 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2341 tree tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
2343 /* Set up aliasing information for PTR's name memory tag (if it has
2344 one). Note that only pointers that have been dereferenced will
2345 have a name memory tag. */
2346 if (pi->name_mem_tag && pi->pt_vars)
2348 if (!bitmap_empty_p (pi->pt_vars))
2350 union_alias_set_into (pi->name_mem_tag, pi->pt_vars);
2351 union_alias_set_into (tag, pi->pt_vars);
2352 bitmap_clear_bit (MTAG_ALIASES (tag), DECL_UID (tag));
2354 /* It may be the case that this the tag uid was the only
2355 bit we had set in the aliases list, and in this case,
2356 we don't want to keep an empty bitmap, as this
2357 asserts in tree-ssa-operands.c . */
2358 if (bitmap_empty_p (MTAG_ALIASES (tag)))
2359 BITMAP_FREE (MTAG_ALIASES (tag));
2363 timevar_pop (TV_FLOW_SENSITIVE);
2367 /* Return TRUE if at least one symbol in TAG2's alias set is also
2368 present in TAG1's alias set. */
2371 have_common_aliases_p (bitmap tag1aliases, bitmap tag2aliases)
2374 /* This is the old behavior of have_common_aliases_p, which is to
2375 return false if both sets are empty, or one set is and the other
2377 if ((tag1aliases == NULL && tag2aliases != NULL)
2378 || (tag2aliases == NULL && tag1aliases != NULL)
2379 || (tag1aliases == NULL && tag2aliases == NULL))
2382 return bitmap_intersect_p (tag1aliases, tag2aliases);
2385 /* Compute type-based alias sets. Traverse all the pointers and
2386 addressable variables found in setup_pointers_and_addressables.
2388 For every pointer P in AI->POINTERS and addressable variable V in
2389 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's symbol
2390 memory tag (SMT) if their alias sets conflict. V is then marked as
2391 an aliased symbol so that the operand scanner knows that statements
2392 containing V have aliased operands. */
2395 compute_flow_insensitive_aliasing (struct alias_info *ai)
2399 timevar_push (TV_FLOW_INSENSITIVE);
2400 /* For every pointer P, determine which addressable variables may alias
2401 with P's symbol memory tag. */
2402 for (i = 0; i < ai->num_pointers; i++)
2405 struct alias_map_d *p_map = ai->pointers[i];
2406 tree tag = symbol_mem_tag (p_map->var);
2409 /* Call-clobbering information is not finalized yet at this point. */
2410 if (PTR_IS_REF_ALL (p_map->var))
2413 for (j = 0; j < ai->num_addressable_vars; j++)
2415 struct alias_map_d *v_map;
2417 bool tag_stored_p, var_stored_p;
2419 v_map = ai->addressable_vars[j];
2421 v_ann = var_ann (var);
2423 /* Skip memory tags and variables that have never been
2424 written to. We also need to check if the variables are
2425 call-clobbered because they may be overwritten by
2427 tag_stored_p = pointer_set_contains (ai->written_vars, tag)
2428 || is_call_clobbered (tag);
2429 var_stored_p = pointer_set_contains (ai->written_vars, var)
2430 || is_call_clobbered (var);
2431 if (!tag_stored_p && !var_stored_p)
2434 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
2436 /* We should never have a var with subvars here, because
2437 they shouldn't get into the set of addressable vars */
2438 gcc_assert (!var_can_have_subvars (var)
2439 || get_subvars_for_var (var) == NULL);
2441 /* Add VAR to TAG's may-aliases set. */
2442 add_may_alias (tag, var);
2447 /* Since this analysis is based exclusively on symbols, it fails to
2448 handle cases where two pointers P and Q have different memory
2449 tags with conflicting alias set numbers but no aliased symbols in
2452 For example, suppose that we have two memory tags SMT.1 and SMT.2
2455 may-aliases (SMT.1) = { a }
2456 may-aliases (SMT.2) = { b }
2458 and the alias set number of SMT.1 conflicts with that of SMT.2.
2459 Since they don't have symbols in common, loads and stores from
2460 SMT.1 and SMT.2 will seem independent of each other, which will
2461 lead to the optimizers making invalid transformations (see
2462 testsuite/gcc.c-torture/execute/pr15262-[12].c).
2464 To avoid this problem, we do a final traversal of AI->POINTERS
2465 looking for pairs of pointers that have no aliased symbols in
2466 common and yet have conflicting alias set numbers. */
2467 for (i = 0; i < ai->num_pointers; i++)
2470 struct alias_map_d *p_map1 = ai->pointers[i];
2471 tree tag1 = symbol_mem_tag (p_map1->var);
2472 bitmap may_aliases1 = MTAG_ALIASES (tag1);
2474 if (PTR_IS_REF_ALL (p_map1->var))
2477 for (j = i + 1; j < ai->num_pointers; j++)
2479 struct alias_map_d *p_map2 = ai->pointers[j];
2480 tree tag2 = symbol_mem_tag (p_map2->var);
2481 bitmap may_aliases2 = may_aliases (tag2);
2483 if (PTR_IS_REF_ALL (p_map2->var))
2486 /* If the pointers may not point to each other, do nothing. */
2487 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
2490 /* The two pointers may alias each other. If they already have
2491 symbols in common, do nothing. */
2492 if (have_common_aliases_p (may_aliases1, may_aliases2))
2495 if (may_aliases2 && !bitmap_empty_p (may_aliases2))
2497 union_alias_set_into (tag1, may_aliases2);
2501 /* Since TAG2 does not have any aliases of its own, add
2502 TAG2 itself to the alias set of TAG1. */
2503 add_may_alias (tag1, tag2);
2508 timevar_pop (TV_FLOW_INSENSITIVE);
2512 /* Finalize may-alias information for ref-all pointers. Traverse all
2513 the addressable variables found in setup_pointers_and_addressables.
2515 If flow-sensitive alias analysis has attached a name memory tag to
2516 a ref-all pointer, we will use it for the dereferences because that
2517 will have more precise aliasing information. But if there is no
2518 name tag, we will use a special symbol tag that aliases all the
2519 call-clobbered addressable variables. */
2522 finalize_ref_all_pointers (struct alias_info *ai)
2526 /* First add the real call-clobbered variables. */
2527 for (i = 0; i < ai->num_addressable_vars; i++)
2529 tree var = ai->addressable_vars[i]->var;
2530 if (is_call_clobbered (var))
2531 add_may_alias (ai->ref_all_symbol_mem_tag, var);
2534 /* Then add the call-clobbered pointer memory tags. See
2535 compute_flow_insensitive_aliasing for the rationale. */
2536 for (i = 0; i < ai->num_pointers; i++)
2538 tree ptr = ai->pointers[i]->var, tag;
2539 /* Avoid adding to self and clean up. */
2540 if (PTR_IS_REF_ALL (ptr))
2542 struct ptr_info_def *pi = get_ptr_info (ptr);
2543 if (pi->is_dereferenced)
2544 pi->pt_anything = 0;
2547 tag = symbol_mem_tag (ptr);
2548 if (is_call_clobbered (tag))
2549 add_may_alias (ai->ref_all_symbol_mem_tag, tag);
2555 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
2558 create_alias_map_for (tree var, struct alias_info *ai)
2560 struct alias_map_d *alias_map;
2561 alias_map = XCNEW (struct alias_map_d);
2562 alias_map->var = var;
2563 alias_map->set = get_alias_set (var);
2564 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
2568 /* Create memory tags for all the dereferenced pointers and build the
2569 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
2570 sets. Based on the address escape and points-to information collected
2571 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
2572 variables whose address is not needed anymore. */
2575 setup_pointers_and_addressables (struct alias_info *ai)
2577 size_t num_addressable_vars, num_pointers;
2578 referenced_var_iterator rvi;
2580 VEC (tree, heap) *varvec = NULL;
2581 safe_referenced_var_iterator srvi;
2583 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
2584 num_addressable_vars = num_pointers = 0;
2586 FOR_EACH_REFERENCED_VAR (var, rvi)
2588 if (may_be_aliased (var))
2589 num_addressable_vars++;
2591 if (POINTER_TYPE_P (TREE_TYPE (var)))
2593 /* Since we don't keep track of volatile variables, assume that
2594 these pointers are used in indirect store operations. */
2595 if (TREE_THIS_VOLATILE (var))
2596 pointer_set_insert (ai->dereferenced_ptrs_store, var);
2602 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
2603 always going to be slightly bigger than we actually need them
2604 because some TREE_ADDRESSABLE variables will be marked
2605 non-addressable below and only pointers with unique symbol tags are
2606 going to be added to POINTERS. */
2607 ai->addressable_vars = XCNEWVEC (struct alias_map_d *, num_addressable_vars);
2608 ai->pointers = XCNEWVEC (struct alias_map_d *, num_pointers);
2609 ai->num_addressable_vars = 0;
2610 ai->num_pointers = 0;
2612 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
2616 /* Name memory tags already have flow-sensitive aliasing
2617 information, so they need not be processed by
2618 compute_flow_insensitive_aliasing. Similarly, symbol memory
2619 tags are already accounted for when we process their
2622 Structure fields, on the other hand, have to have some of this
2623 information processed for them, but it's pointless to mark them
2624 non-addressable (since they are fake variables anyway). */
2625 if (MTAG_P (var) && TREE_CODE (var) != STRUCT_FIELD_TAG)
2628 /* Remove the ADDRESSABLE flag from every addressable variable whose
2629 address is not needed anymore. This is caused by the propagation
2630 of ADDR_EXPR constants into INDIRECT_REF expressions and the
2631 removal of dead pointer assignments done by the early scalar
2633 if (TREE_ADDRESSABLE (var))
2635 if (!bitmap_bit_p (gimple_addressable_vars (cfun), DECL_UID (var))
2636 && TREE_CODE (var) != RESULT_DECL
2637 && !is_global_var (var))
2639 bool okay_to_mark = true;
2641 /* Since VAR is now a regular GIMPLE register, we will need
2642 to rename VAR into SSA afterwards. */
2643 mark_sym_for_renaming (var);
2645 /* If VAR can have sub-variables, and any of its
2646 sub-variables has its address taken, then we cannot
2647 remove the addressable flag from VAR. */
2648 if (var_can_have_subvars (var)
2649 && (svars = get_subvars_for_var (var)))
2654 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
2656 if (bitmap_bit_p (gimple_addressable_vars (cfun),
2658 okay_to_mark = false;
2659 mark_sym_for_renaming (subvar);
2663 /* The address of VAR is not needed, remove the
2664 addressable bit, so that it can be optimized as a
2665 regular variable. */
2668 /* The memory partition holding VAR will no longer
2669 contain VAR, and statements referencing it will need
2671 if (memory_partition (var))
2672 mark_sym_for_renaming (memory_partition (var));
2674 mark_non_addressable (var);
2679 /* Global variables and addressable locals may be aliased. Create an
2680 entry in ADDRESSABLE_VARS for VAR. */
2681 if (may_be_aliased (var))
2683 if (!var_can_have_subvars (var)
2684 || get_subvars_for_var (var) == NULL)
2685 create_alias_map_for (var, ai);
2687 mark_sym_for_renaming (var);
2690 /* Add pointer variables that have been dereferenced to the POINTERS
2691 array and create a symbol memory tag for them. */
2692 if (POINTER_TYPE_P (TREE_TYPE (var)))
2694 if ((pointer_set_contains (ai->dereferenced_ptrs_store, var)
2695 || pointer_set_contains (ai->dereferenced_ptrs_load, var)))
2700 /* If pointer VAR still doesn't have a memory tag
2701 associated with it, create it now or re-use an
2703 tag = get_smt_for (var, ai);
2704 t_ann = var_ann (tag);
2706 /* The symbol tag will need to be renamed into SSA
2707 afterwards. Note that we cannot do this inside
2708 get_smt_for because aliasing may run multiple times
2709 and we only create symbol tags the first time. */
2710 mark_sym_for_renaming (tag);
2712 /* Similarly, if pointer VAR used to have another type
2713 tag, we will need to process it in the renamer to
2714 remove the stale virtual operands. */
2715 old_tag = symbol_mem_tag (var);
2717 mark_sym_for_renaming (old_tag);
2719 /* Associate the tag with pointer VAR. */
2720 set_symbol_mem_tag (var, tag);
2722 /* If pointer VAR has been used in a store operation,
2723 then its memory tag must be marked as written-to. */
2724 if (pointer_set_contains (ai->dereferenced_ptrs_store, var))
2725 pointer_set_insert (ai->written_vars, tag);
2729 /* The pointer has not been dereferenced. If it had a
2730 symbol memory tag, remove it and mark the old tag for
2731 renaming to remove it out of the IL. */
2732 tree tag = symbol_mem_tag (var);
2735 mark_sym_for_renaming (tag);
2736 set_symbol_mem_tag (var, NULL_TREE);
2742 VEC_free (tree, heap, varvec);
2746 /* Determine whether to use .GLOBAL_VAR to model call clobbering
2747 semantics. If the function makes no references to global
2748 variables and contains at least one call to a non-pure function,
2749 then we need to mark the side-effects of the call using .GLOBAL_VAR
2750 to represent all possible global memory referenced by the callee. */
2753 maybe_create_global_var (void)
2755 /* No need to create it, if we have one already. */
2756 if (gimple_global_var (cfun) == NULL_TREE)
2758 struct mem_ref_stats_d *stats = gimple_mem_ref_stats (cfun);
2760 /* Create .GLOBAL_VAR if there are no call-clobbered
2761 variables and the program contains a mixture of pure/const
2762 and regular function calls. This is to avoid the problem
2763 described in PR 20115:
2766 int func_pure (void) { return X; }
2767 int func_non_pure (int a) { X += a; }
2770 int a = func_pure ();
2776 Since foo() has no call-clobbered variables, there is
2777 no relationship between the calls to func_pure and
2778 func_non_pure. Since func_pure has no side-effects, value
2779 numbering optimizations elide the second call to func_pure.
2780 So, if we have some pure/const and some regular calls in the
2781 program we create .GLOBAL_VAR to avoid missing these
2783 if (bitmap_empty_p (gimple_call_clobbered_vars (cfun))
2784 && stats->num_call_sites > 0
2785 && stats->num_pure_const_call_sites > 0
2786 && stats->num_call_sites > stats->num_pure_const_call_sites)
2787 create_global_var ();
2792 /* Return TRUE if pointer PTR may point to variable VAR.
2794 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
2795 This is needed because when checking for type conflicts we are
2796 interested in the alias set of the memory location pointed-to by
2797 PTR. The alias set of PTR itself is irrelevant.
2799 VAR_ALIAS_SET is the alias set for VAR. */
2802 may_alias_p (tree ptr, alias_set_type mem_alias_set,
2803 tree var, alias_set_type var_alias_set,
2804 bool alias_set_only)
2808 alias_stats.alias_queries++;
2809 alias_stats.simple_queries++;
2811 /* By convention, a variable cannot alias itself. */
2812 mem = symbol_mem_tag (ptr);
2815 alias_stats.alias_noalias++;
2816 alias_stats.simple_resolved++;
2820 /* If -fargument-noalias-global is > 2, pointer arguments may
2821 not point to anything else. */
2822 if (flag_argument_noalias > 2 && TREE_CODE (ptr) == PARM_DECL)
2824 alias_stats.alias_noalias++;
2825 alias_stats.simple_resolved++;
2829 /* If -fargument-noalias-global is > 1, pointer arguments may
2830 not point to global variables. */
2831 if (flag_argument_noalias > 1 && is_global_var (var)
2832 && TREE_CODE (ptr) == PARM_DECL)
2834 alias_stats.alias_noalias++;
2835 alias_stats.simple_resolved++;
2839 /* If either MEM or VAR is a read-only global and the other one
2840 isn't, then PTR cannot point to VAR. */
2841 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
2842 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
2844 alias_stats.alias_noalias++;
2845 alias_stats.simple_resolved++;
2849 gcc_assert (TREE_CODE (mem) == SYMBOL_MEMORY_TAG);
2851 if (!DECL_NO_TBAA_P (ptr))
2853 alias_stats.tbaa_queries++;
2855 /* If the alias sets don't conflict then MEM cannot alias VAR. */
2856 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
2858 alias_stats.alias_noalias++;
2859 alias_stats.tbaa_resolved++;
2863 /* If VAR is a record or union type, PTR cannot point into VAR
2864 unless there is some explicit address operation in the
2865 program that can reference a field of the type pointed-to by
2866 PTR. This also assumes that the types of both VAR and PTR
2867 are contained within the compilation unit, and that there is
2868 no fancy addressing arithmetic associated with any of the
2870 if (mem_alias_set != 0 && var_alias_set != 0)
2872 tree ptr_type = TREE_TYPE (ptr);
2873 tree var_type = TREE_TYPE (var);
2875 /* The star count is -1 if the type at the end of the
2876 pointer_to chain is not a record or union type. */
2877 if ((!alias_set_only) &&
2878 ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
2880 int ptr_star_count = 0;
2882 /* ipa_type_escape_star_count_of_interesting_type is a
2883 little too restrictive for the pointer type, need to
2884 allow pointers to primitive types as long as those
2885 types cannot be pointers to everything. */
2886 while (POINTER_TYPE_P (ptr_type))
2888 /* Strip the *s off. */
2889 ptr_type = TREE_TYPE (ptr_type);
2893 /* There does not appear to be a better test to see if
2894 the pointer type was one of the pointer to everything
2896 if (ptr_star_count > 0)
2898 alias_stats.structnoaddress_queries++;
2899 if (ipa_type_escape_field_does_not_clobber_p (var_type,
2902 alias_stats.structnoaddress_resolved++;
2903 alias_stats.alias_noalias++;
2907 else if (ptr_star_count == 0)
2909 /* If PTR_TYPE was not really a pointer to type, it cannot
2911 alias_stats.structnoaddress_queries++;
2912 alias_stats.structnoaddress_resolved++;
2913 alias_stats.alias_noalias++;
2920 alias_stats.alias_mayalias++;
2925 /* Add ALIAS to the set of variables that may alias VAR. */
2928 add_may_alias (tree var, tree alias)
2930 /* Don't allow self-referential aliases. */
2931 gcc_assert (var != alias);
2933 /* ALIAS must be addressable if it's being added to an alias set. */
2935 TREE_ADDRESSABLE (alias) = 1;
2937 gcc_assert (may_be_aliased (alias));
2940 /* VAR must be a symbol or a name tag. */
2941 gcc_assert (TREE_CODE (var) == SYMBOL_MEMORY_TAG
2942 || TREE_CODE (var) == NAME_MEMORY_TAG);
2944 if (MTAG_ALIASES (var) == NULL)
2945 MTAG_ALIASES (var) = BITMAP_ALLOC (&alias_bitmap_obstack);
2947 bitmap_set_bit (MTAG_ALIASES (var), DECL_UID (alias));
2951 /* Mark pointer PTR as pointing to an arbitrary memory location. */
2954 set_pt_anything (tree ptr)
2956 struct ptr_info_def *pi = get_ptr_info (ptr);
2958 pi->pt_anything = 1;
2961 /* The pointer used to have a name tag, but we now found it pointing
2962 to an arbitrary location. The name tag needs to be renamed and
2963 disassociated from PTR. */
2964 if (pi->name_mem_tag)
2966 mark_sym_for_renaming (pi->name_mem_tag);
2967 pi->name_mem_tag = NULL_TREE;
2972 /* Return true if STMT is an "escape" site from the current function. Escape
2973 sites those statements which might expose the address of a variable
2974 outside the current function. STMT is an escape site iff:
2976 1- STMT is a function call, or
2977 2- STMT is an __asm__ expression, or
2978 3- STMT is an assignment to a non-local variable, or
2979 4- STMT is a return statement.
2981 Return the type of escape site found, if we found one, or NO_ESCAPE
2985 is_escape_site (tree stmt)
2987 tree call = get_call_expr_in (stmt);
2988 if (call != NULL_TREE)
2990 if (!TREE_SIDE_EFFECTS (call))
2991 return ESCAPE_TO_PURE_CONST;
2993 return ESCAPE_TO_CALL;
2995 else if (TREE_CODE (stmt) == ASM_EXPR)
2996 return ESCAPE_TO_ASM;
2997 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2999 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3001 /* Get to the base of _REF nodes. */
3002 if (TREE_CODE (lhs) != SSA_NAME)
3003 lhs = get_base_address (lhs);
3005 /* If we couldn't recognize the LHS of the assignment, assume that it
3006 is a non-local store. */
3007 if (lhs == NULL_TREE)
3008 return ESCAPE_UNKNOWN;
3010 if (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == NOP_EXPR
3011 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == CONVERT_EXPR
3012 || TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
3015 = TREE_TYPE (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0));
3016 tree to = TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 1));
3018 /* If the RHS is a conversion between a pointer and an integer, the
3019 pointer escapes since we can't track the integer. */
3020 if (POINTER_TYPE_P (from) && !POINTER_TYPE_P (to))
3021 return ESCAPE_BAD_CAST;
3023 /* Same if the RHS is a conversion between a regular pointer and a
3024 ref-all pointer since we can't track the SMT of the former. */
3025 if (POINTER_TYPE_P (from) && !TYPE_REF_CAN_ALIAS_ALL (from)
3026 && POINTER_TYPE_P (to) && TYPE_REF_CAN_ALIAS_ALL (to))
3027 return ESCAPE_BAD_CAST;
3030 /* If the LHS is an SSA name, it can't possibly represent a non-local
3032 if (TREE_CODE (lhs) == SSA_NAME)
3035 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
3036 local variables we cannot be sure if it will escape, because we
3037 don't have information about objects not in SSA form. Need to
3038 implement something along the lines of
3040 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
3041 Midkiff, ``Escape analysis for java,'' in Proceedings of the
3042 Conference on Object-Oriented Programming Systems, Languages, and
3043 Applications (OOPSLA), pp. 1-19, 1999. */
3044 return ESCAPE_STORED_IN_GLOBAL;
3046 else if (TREE_CODE (stmt) == RETURN_EXPR)
3047 return ESCAPE_TO_RETURN;
3052 /* Create a new memory tag of type TYPE.
3053 Does NOT push it into the current binding. */
3056 create_tag_raw (enum tree_code code, tree type, const char *prefix)
3060 tmp_var = build_decl (code, create_tmp_var_name (prefix), type);
3062 /* Make the variable writable. */
3063 TREE_READONLY (tmp_var) = 0;
3065 /* It doesn't start out global. */
3066 MTAG_GLOBAL (tmp_var) = 0;
3067 TREE_STATIC (tmp_var) = 0;
3068 TREE_USED (tmp_var) = 1;
3073 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
3074 is considered to represent all the pointers whose pointed-to types are
3075 in the same alias set class. Otherwise, the tag represents a single
3076 SSA_NAME pointer variable. */
3079 create_memory_tag (tree type, bool is_type_tag)
3081 tree tag = create_tag_raw (is_type_tag ? SYMBOL_MEMORY_TAG : NAME_MEMORY_TAG,
3082 type, (is_type_tag) ? "SMT" : "NMT");
3084 /* By default, memory tags are local variables. Alias analysis will
3085 determine whether they should be considered globals. */
3086 DECL_CONTEXT (tag) = current_function_decl;
3088 /* Memory tags are by definition addressable. */
3089 TREE_ADDRESSABLE (tag) = 1;
3091 set_symbol_mem_tag (tag, NULL_TREE);
3093 /* Add the tag to the symbol table. */
3094 add_referenced_var (tag);
3100 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
3101 This is used if P_i has been found to point to a specific set of
3102 variables or to a non-aliased memory location like the address returned
3103 by malloc functions. */
3106 get_nmt_for (tree ptr)
3108 struct ptr_info_def *pi = get_ptr_info (ptr);
3109 tree tag = pi->name_mem_tag;
3111 if (tag == NULL_TREE)
3112 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
3117 /* Return the symbol memory tag associated to pointer PTR. A memory
3118 tag is an artificial variable that represents the memory location
3119 pointed-to by PTR. It is used to model the effects of pointer
3120 de-references on addressable variables.
3122 AI points to the data gathered during alias analysis. This
3123 function populates the array AI->POINTERS. */
3126 get_smt_for (tree ptr, struct alias_info *ai)
3130 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3131 alias_set_type tag_set = get_alias_set (tag_type);
3133 /* We use a unique memory tag for all the ref-all pointers. */
3134 if (PTR_IS_REF_ALL (ptr))
3136 if (!ai->ref_all_symbol_mem_tag)
3137 ai->ref_all_symbol_mem_tag = create_memory_tag (void_type_node, true);
3138 return ai->ref_all_symbol_mem_tag;
3141 /* To avoid creating unnecessary memory tags, only create one memory tag
3142 per alias set class. Note that it may be tempting to group
3143 memory tags based on conflicting alias sets instead of
3144 equivalence. That would be wrong because alias sets are not
3145 necessarily transitive (as demonstrated by the libstdc++ test
3146 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
3147 such that conflicts (A, B) == true and conflicts (A, C) == true,
3148 it does not necessarily follow that conflicts (B, C) == true. */
3149 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
3151 struct alias_map_d *curr = ai->pointers[i];
3152 tree curr_tag = symbol_mem_tag (curr->var);
3153 if (tag_set == curr->set)
3160 /* If VAR cannot alias with any of the existing memory tags, create a new
3161 tag for PTR and add it to the POINTERS array. */
3162 if (tag == NULL_TREE)
3164 struct alias_map_d *alias_map;
3166 /* If PTR did not have a symbol tag already, create a new SMT.*
3167 artificial variable representing the memory location
3168 pointed-to by PTR. */
3169 tag = symbol_mem_tag (ptr);
3170 if (tag == NULL_TREE)
3171 tag = create_memory_tag (tag_type, true);
3173 /* Add PTR to the POINTERS array. Note that we are not interested in
3174 PTR's alias set. Instead, we cache the alias set for the memory that
3176 alias_map = XCNEW (struct alias_map_d);
3177 alias_map->var = ptr;
3178 alias_map->set = tag_set;
3179 ai->pointers[ai->num_pointers++] = alias_map;
3182 /* If the pointed-to type is volatile, so is the tag. */
3183 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
3185 /* Make sure that the symbol tag has the same alias set as the
3187 gcc_assert (tag_set == get_alias_set (tag));
3193 /* Create GLOBAL_VAR, an artificial global variable to act as a
3194 representative of all the variables that may be clobbered by function
3198 create_global_var (void)
3200 tree global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
3202 DECL_ARTIFICIAL (global_var) = 1;
3203 TREE_READONLY (global_var) = 0;
3204 DECL_EXTERNAL (global_var) = 1;
3205 TREE_STATIC (global_var) = 1;
3206 TREE_USED (global_var) = 1;
3207 DECL_CONTEXT (global_var) = NULL_TREE;
3208 TREE_THIS_VOLATILE (global_var) = 0;
3209 TREE_ADDRESSABLE (global_var) = 0;
3211 create_var_ann (global_var);
3212 mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
3213 add_referenced_var (global_var);
3214 mark_sym_for_renaming (global_var);
3215 cfun->gimple_df->global_var = global_var;
3219 /* Dump alias statistics on FILE. */
3222 dump_alias_stats (FILE *file)
3224 const char *funcname
3225 = lang_hooks.decl_printable_name (current_function_decl, 2);
3226 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
3227 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
3228 fprintf (file, "Total alias mayalias results:\t%u\n",
3229 alias_stats.alias_mayalias);
3230 fprintf (file, "Total alias noalias results:\t%u\n",
3231 alias_stats.alias_noalias);
3232 fprintf (file, "Total simple queries:\t%u\n",
3233 alias_stats.simple_queries);
3234 fprintf (file, "Total simple resolved:\t%u\n",
3235 alias_stats.simple_resolved);
3236 fprintf (file, "Total TBAA queries:\t%u\n",
3237 alias_stats.tbaa_queries);
3238 fprintf (file, "Total TBAA resolved:\t%u\n",
3239 alias_stats.tbaa_resolved);
3240 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
3241 alias_stats.structnoaddress_queries);
3242 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
3243 alias_stats.structnoaddress_resolved);
3247 /* Dump alias information on FILE. */
3250 dump_alias_info (FILE *file)
3253 const char *funcname
3254 = lang_hooks.decl_printable_name (current_function_decl, 2);
3255 referenced_var_iterator rvi;
3258 fprintf (file, "\nAlias information for %s\n\n", funcname);
3260 dump_memory_partitions (file);
3262 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
3264 fprintf (file, "Aliased symbols\n\n");
3266 FOR_EACH_REFERENCED_VAR (var, rvi)
3268 if (may_be_aliased (var))
3269 dump_variable (file, var);
3272 fprintf (file, "\nDereferenced pointers\n\n");
3274 FOR_EACH_REFERENCED_VAR (var, rvi)
3275 if (symbol_mem_tag (var))
3276 dump_variable (file, var);
3278 fprintf (file, "\nSymbol memory tags\n\n");
3280 FOR_EACH_REFERENCED_VAR (var, rvi)
3282 if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
3283 dump_variable (file, var);
3286 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
3288 fprintf (file, "SSA_NAME pointers\n\n");
3289 for (i = 1; i < num_ssa_names; i++)
3291 tree ptr = ssa_name (i);
3292 struct ptr_info_def *pi;
3294 if (ptr == NULL_TREE)
3297 pi = SSA_NAME_PTR_INFO (ptr);
3298 if (!SSA_NAME_IN_FREE_LIST (ptr)
3300 && pi->name_mem_tag)
3301 dump_points_to_info_for (file, ptr);
3304 fprintf (file, "\nName memory tags\n\n");
3306 FOR_EACH_REFERENCED_VAR (var, rvi)
3308 if (TREE_CODE (var) == NAME_MEMORY_TAG)
3309 dump_variable (file, var);
3312 fprintf (file, "\n");
3316 /* Dump alias information on stderr. */
3319 debug_alias_info (void)
3321 dump_alias_info (stderr);
3325 /* Return the alias information associated with pointer T. It creates a
3326 new instance if none existed. */
3328 struct ptr_info_def *
3329 get_ptr_info (tree t)
3331 struct ptr_info_def *pi;
3333 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
3335 pi = SSA_NAME_PTR_INFO (t);
3338 pi = GGC_CNEW (struct ptr_info_def);
3339 SSA_NAME_PTR_INFO (t) = pi;
3346 /* Dump points-to information for SSA_NAME PTR into FILE. */
3349 dump_points_to_info_for (FILE *file, tree ptr)
3351 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3353 print_generic_expr (file, ptr, dump_flags);
3357 if (pi->name_mem_tag)
3359 fprintf (file, ", name memory tag: ");
3360 print_generic_expr (file, pi->name_mem_tag, dump_flags);
3363 if (pi->is_dereferenced)
3364 fprintf (file, ", is dereferenced");
3366 if (pi->value_escapes_p)
3367 fprintf (file, ", its value escapes");
3369 if (pi->pt_anything)
3370 fprintf (file, ", points-to anything");
3373 fprintf (file, ", points-to NULL");
3377 fprintf (file, ", points-to vars: ");
3378 dump_decl_set (file, pi->pt_vars);
3382 fprintf (file, "\n");
3386 /* Dump points-to information for VAR into stderr. */
3389 debug_points_to_info_for (tree var)
3391 dump_points_to_info_for (stderr, var);
3395 /* Dump points-to information into FILE. NOTE: This function is slow, as
3396 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
3399 dump_points_to_info (FILE *file)
3402 block_stmt_iterator si;
3405 lang_hooks.decl_printable_name (current_function_decl, 2);
3406 referenced_var_iterator rvi;
3409 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
3411 /* First dump points-to information for the default definitions of
3412 pointer variables. This is necessary because default definitions are
3413 not part of the code. */
3414 FOR_EACH_REFERENCED_VAR (var, rvi)
3416 if (POINTER_TYPE_P (TREE_TYPE (var)))
3418 tree def = gimple_default_def (cfun, var);
3420 dump_points_to_info_for (file, def);
3424 /* Dump points-to information for every pointer defined in the program. */
3429 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3431 tree ptr = PHI_RESULT (phi);
3432 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
3433 dump_points_to_info_for (file, ptr);
3436 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3438 tree stmt = bsi_stmt (si);
3440 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
3441 if (TREE_CODE (def) == SSA_NAME
3442 && POINTER_TYPE_P (TREE_TYPE (def)))
3443 dump_points_to_info_for (file, def);
3447 fprintf (file, "\n");
3451 /* Dump points-to info pointed to by PTO into STDERR. */
3454 debug_points_to_info (void)
3456 dump_points_to_info (stderr);
3459 /* Dump to FILE the list of variables that may be aliasing VAR. */
3462 dump_may_aliases_for (FILE *file, tree var)
3466 aliases = MTAG_ALIASES (var);
3473 fprintf (file, "{ ");
3474 EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
3476 al = referenced_var (i);
3477 print_generic_expr (file, al, dump_flags);
3478 fprintf (file, " ");
3480 fprintf (file, "}");
3485 /* Dump to stderr the list of variables that may be aliasing VAR. */
3488 debug_may_aliases_for (tree var)
3490 dump_may_aliases_for (stderr, var);
3494 /* Return true if VAR may be aliased. */
3497 may_be_aliased (tree var)
3500 if (TREE_ADDRESSABLE (var))
3503 /* Globally visible variables can have their addresses taken by other
3504 translation units. */
3506 && (MTAG_GLOBAL (var) || TREE_PUBLIC (var)))
3508 else if (!MTAG_P (var)
3509 && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
3512 /* Automatic variables can't have their addresses escape any other
3513 way. This must be after the check for global variables, as
3514 extern declarations do not have TREE_STATIC set. */
3515 if (!TREE_STATIC (var))
3518 /* If we're in unit-at-a-time mode, then we must have seen all
3519 occurrences of address-of operators, and so we can trust
3520 TREE_ADDRESSABLE. Otherwise we can only be sure the variable
3521 isn't addressable if it's local to the current function. */
3522 if (flag_unit_at_a_time)
3525 if (decl_function_context (var) == current_function_decl)
3531 /* The following is based on code in add_stmt_operand to ensure that the
3532 same defs/uses/vdefs/vuses will be found after replacing a reference
3533 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
3534 is the address of var. Return a memtag for the ptr, after adding the
3535 proper may_aliases to it (which are the aliases of var, if it has any,
3539 add_may_alias_for_new_tag (tree tag, tree var)
3541 bitmap aliases = NULL;
3544 aliases = may_aliases (var);
3546 /* Case 1: |aliases| == 1 */
3548 && bitmap_single_bit_set_p (aliases))
3550 tree ali = referenced_var (bitmap_first_set_bit (aliases));
3551 if (TREE_CODE (ali) == SYMBOL_MEMORY_TAG)
3555 /* Case 2: |aliases| == 0 */
3556 if (aliases == NULL)
3557 add_may_alias (tag, var);
3560 /* Case 3: |aliases| > 1 */
3561 union_alias_set_into (tag, aliases);
3566 /* Create a new symbol tag for PTR. Construct the may-alias list of this type
3567 tag so that it has the aliasing of VAR, or of the relevant subvars of VAR
3568 according to the location accessed by EXPR.
3570 Note, the set of aliases represented by the new symbol tag are not marked
3574 new_type_alias (tree ptr, tree var, tree expr)
3576 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3579 tree ali = NULL_TREE;
3580 HOST_WIDE_INT offset, size, maxsize;
3582 VEC (tree, heap) *overlaps = NULL;
3583 unsigned int len, i;
3587 gcc_assert (symbol_mem_tag (ptr) == NULL_TREE);
3588 gcc_assert (!MTAG_P (var));
3590 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
3593 tag = create_memory_tag (tag_type, true);
3594 set_symbol_mem_tag (ptr, tag);
3596 /* Add VAR to the may-alias set of PTR's new symbol tag. If VAR has
3597 subvars, add the subvars to the tag instead of the actual var. */
3598 if (var_can_have_subvars (ref)
3599 && (svars = get_subvars_for_var (ref)))
3601 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
3605 if (overlap_subvar (offset, maxsize, subvar, &exact))
3606 VEC_safe_push (tree, heap, overlaps, subvar);
3608 gcc_assert (overlaps != NULL);
3610 else if (var_can_have_subvars (var)
3611 && (svars = get_subvars_for_var (var)))
3613 /* If the REF is not a direct access to VAR (e.g., it is a dereference
3614 of a pointer), we should scan the virtual operands of REF the same
3615 way as tree-ssa-operands do. At the moment, this is somewhat
3616 difficult, so we just give up and add all the subvars of VAR.
3617 On mem-ssa branch, the scanning for virtual operands have been
3618 split from the rest of tree-ssa-operands, so it should be much
3619 easier to fix this problem correctly once mem-ssa is merged. */
3620 for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
3621 VEC_safe_push (tree, heap, overlaps, subvar);
3623 gcc_assert (overlaps != NULL);
3626 ali = add_may_alias_for_new_tag (tag, var);
3628 len = VEC_length (tree, overlaps);
3631 if (dump_file && (dump_flags & TDF_DETAILS))
3632 fprintf (dump_file, "\nnumber of overlapping subvars = %u\n", len);
3635 ali = add_may_alias_for_new_tag (tag, VEC_index (tree, overlaps, 0));
3641 for (k = 0; VEC_iterate (tree, overlaps, k, sv_var); k++)
3643 ali = add_may_alias_for_new_tag (tag, sv_var);
3647 /* Can happen only if 'Case 1' of add_may_alias_for_new_tag
3648 took place. Since more than one svar was found, we add
3649 'ali' as one of the may_aliases of the new tag. */
3650 add_may_alias (tag, ali);
3655 VEC_free (tree, heap, overlaps);
3658 set_symbol_mem_tag (ptr, ali);
3659 TREE_READONLY (tag) = TREE_READONLY (var);
3660 MTAG_GLOBAL (tag) = is_global_var (var);
3663 /* This represents the used range of a variable. */
3665 typedef struct used_part
3667 HOST_WIDE_INT minused;
3668 HOST_WIDE_INT maxused;
3669 /* True if we have an explicit use/def of some portion of this variable,
3670 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
3672 /* True if we have an implicit use/def of some portion of this
3673 variable. Implicit uses occur when we can't tell what part we
3674 are referencing, and have to make conservative assumptions. */
3676 /* True if the structure is only written to or taken its address. */
3680 /* An array of used_part structures, indexed by variable uid. */
3682 static htab_t used_portions;
3684 struct used_part_map
3690 /* Return true if the uid in the two used part maps are equal. */
3693 used_part_map_eq (const void *va, const void *vb)
3695 const struct used_part_map *a = (const struct used_part_map *) va;
3696 const struct used_part_map *b = (const struct used_part_map *) vb;
3697 return (a->uid == b->uid);
3700 /* Hash a from uid in a used_part_map. */
3703 used_part_map_hash (const void *item)
3705 return ((const struct used_part_map *)item)->uid;
3708 /* Free a used part map element. */
3711 free_used_part_map (void *item)
3713 free (((struct used_part_map *)item)->to);
3717 /* Lookup a used_part structure for a UID. */
3720 up_lookup (unsigned int uid)
3722 struct used_part_map *h, in;
3724 h = (struct used_part_map *) htab_find_with_hash (used_portions, &in, uid);
3730 /* Insert the pair UID, TO into the used part hashtable. */
3733 up_insert (unsigned int uid, used_part_t to)
3735 struct used_part_map *h;
3738 h = XNEW (struct used_part_map);
3741 loc = htab_find_slot_with_hash (used_portions, h,
3745 *(struct used_part_map **) loc = h;
3749 /* Given a variable uid, UID, get or create the entry in the used portions
3750 table for the variable. */
3753 get_or_create_used_part_for (size_t uid)
3756 if ((up = up_lookup (uid)) == NULL)
3758 up = XCNEW (struct used_part);
3759 up->minused = INT_MAX;
3761 up->explicit_uses = false;
3762 up->implicit_uses = false;
3763 up->write_only = true;
3770 /* Create and return a structure sub-variable for field type FIELD at
3771 offset OFFSET, with size SIZE, of variable VAR. If ALIAS_SET not
3772 -1 this field is non-addressable and we should use this alias set
3776 create_sft (tree var, tree field, unsigned HOST_WIDE_INT offset,
3777 unsigned HOST_WIDE_INT size, alias_set_type alias_set)
3779 tree subvar = create_tag_raw (STRUCT_FIELD_TAG, field, "SFT");
3781 /* We need to copy the various flags from VAR to SUBVAR, so that
3782 they are is_global_var iff the original variable was. */
3783 DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
3784 MTAG_GLOBAL (subvar) = DECL_EXTERNAL (var);
3785 TREE_PUBLIC (subvar) = TREE_PUBLIC (var);
3786 TREE_STATIC (subvar) = TREE_STATIC (var);
3787 TREE_READONLY (subvar) = TREE_READONLY (var);
3788 TREE_ADDRESSABLE (subvar) = TREE_ADDRESSABLE (var);
3790 /* Add the new variable to REFERENCED_VARS. */
3791 set_symbol_mem_tag (subvar, NULL);
3792 add_referenced_var (subvar);
3793 SFT_PARENT_VAR (subvar) = var;
3794 SFT_OFFSET (subvar) = offset;
3795 SFT_SIZE (subvar) = size;
3796 SFT_ALIAS_SET (subvar) = alias_set;
3801 /* Given an aggregate VAR, create the subvariables that represent its
3805 create_overlap_variables_for (tree var)
3807 VEC(fieldoff_s,heap) *fieldstack = NULL;
3809 size_t uid = DECL_UID (var);
3811 up = up_lookup (uid);
3816 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL,
3818 if (VEC_length (fieldoff_s, fieldstack) != 0)
3822 bool notokay = false;
3825 HOST_WIDE_INT lastfooffset = -1;
3826 HOST_WIDE_INT lastfosize = -1;
3827 tree lastfotype = NULL_TREE;
3829 /* Not all fields have DECL_SIZE set, and those that don't, we don't
3830 know their size, and thus, can't handle.
3831 The same is true of fields with DECL_SIZE that is not an integer
3832 constant (such as variable sized fields).
3833 Fields with offsets which are not constant will have an offset < 0
3834 We *could* handle fields that are constant sized arrays, but
3835 currently don't. Doing so would require some extra changes to
3836 tree-ssa-operands.c. */
3838 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3841 || TREE_CODE (fo->size) != INTEGER_CST
3850 /* The current heuristic we use is as follows:
3851 If the variable has no used portions in this function, no
3852 structure vars are created for it.
3854 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
3855 we always create structure vars for them.
3856 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3857 some explicit uses, we create structure vars for them.
3858 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3859 no explicit uses, we do not create structure vars for them.
3862 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
3863 && !up->explicit_uses)
3865 if (dump_file && (dump_flags & TDF_DETAILS))
3867 fprintf (dump_file, "Variable ");
3868 print_generic_expr (dump_file, var, 0);
3869 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
3874 /* Bail out, if we can't create overlap variables. */
3877 VEC_free (fieldoff_s, heap, fieldstack);
3881 /* Otherwise, create the variables. */
3882 subvars = lookup_subvars_for_var (var);
3883 *subvars = VEC_alloc (tree, gc, VEC_length (fieldoff_s, fieldstack));
3885 sort_fieldstack (fieldstack);
3887 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); ++i)
3889 HOST_WIDE_INT fosize;
3890 tree currfotype, subvar;
3892 fosize = TREE_INT_CST_LOW (fo->size);
3893 currfotype = fo->type;
3895 /* If this field isn't in the used portion,
3896 or it has the exact same offset and size as the last
3897 field, skip it. Note that we always need the field at
3898 offset 0 so we can properly handle pointers to the
3901 if ((fo->offset != 0
3902 && ((fo->offset <= up->minused
3903 && fo->offset + fosize <= up->minused)
3904 || fo->offset >= up->maxused))
3905 || (fo->offset == lastfooffset
3906 && fosize == lastfosize
3907 && currfotype == lastfotype))
3909 subvar = create_sft (var, fo->type, fo->offset,
3910 fosize, fo->alias_set);
3911 VEC_quick_push (tree, *subvars, subvar);
3915 fprintf (dump_file, "structure field tag %s created for var %s",
3916 get_name (subvar), get_name (var));
3917 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
3918 SFT_OFFSET (subvar));
3919 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
3921 fprintf (dump_file, "\n");
3924 lastfotype = currfotype;
3925 lastfooffset = fo->offset;
3926 lastfosize = fosize;
3929 /* Once we have created subvars, the original is no longer call
3930 clobbered on its own. Its call clobbered status depends
3931 completely on the call clobbered status of the subvars.
3933 add_referenced_var in the above loop will take care of
3934 marking subvars of global variables as call clobbered for us
3935 to start, since they are global as well. */
3936 clear_call_clobbered (var);
3939 VEC_free (fieldoff_s, heap, fieldstack);
3943 /* Find the conservative answer to the question of what portions of what
3944 structures are used by this statement. We assume that if we have a
3945 component ref with a known size + offset, that we only need that part
3946 of the structure. For unknown cases, or cases where we do something
3947 to the whole structure, we assume we need to create fields for the
3948 entire structure. */
3951 find_used_portions (tree *tp, int *walk_subtrees, void *lhs_p)
3953 switch (TREE_CODE (*tp))
3955 case GIMPLE_MODIFY_STMT:
3956 /* Recurse manually here to track whether the use is in the
3957 LHS of an assignment. */
3958 find_used_portions (&GIMPLE_STMT_OPERAND (*tp, 0), walk_subtrees, tp);
3959 return find_used_portions (&GIMPLE_STMT_OPERAND (*tp, 1),
3960 walk_subtrees, NULL);
3966 HOST_WIDE_INT bitsize;
3967 HOST_WIDE_INT bitmaxsize;
3968 HOST_WIDE_INT bitpos;
3970 ref = get_ref_base_and_extent (*tp, &bitpos, &bitsize, &bitmaxsize);
3972 && var_can_have_subvars (ref)
3973 && bitmaxsize != -1)
3975 size_t uid = DECL_UID (ref);
3978 up = get_or_create_used_part_for (uid);
3980 if (bitpos <= up->minused)
3981 up->minused = bitpos;
3982 if ((bitpos + bitmaxsize >= up->maxused))
3983 up->maxused = bitpos + bitmaxsize;
3985 if (bitsize == bitmaxsize)
3986 up->explicit_uses = true;
3988 up->implicit_uses = true;
3990 up->write_only = false;
3991 up_insert (uid, up);
3998 /* This is here to make sure we mark the entire base variable as used
3999 when you take its address. Because our used portion analysis is
4000 simple, we aren't looking at casts or pointer arithmetic to see what
4001 happens when you take the address. */
4004 tree var = get_base_address (TREE_OPERAND (*tp, 0));
4009 && var_can_have_subvars (var)
4010 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
4013 size_t uid = DECL_UID (var);
4015 up = get_or_create_used_part_for (uid);
4018 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
4019 up->implicit_uses = true;
4021 up->write_only = false;
4023 up_insert (uid, up);
4032 int nargs = call_expr_nargs (*tp);
4033 for (i = 0; i < nargs; i++)
4035 tree *arg = &CALL_EXPR_ARG (*tp, i);
4036 if (TREE_CODE (*arg) == ADDR_EXPR)
4037 find_used_portions (arg, walk_subtrees, NULL);
4048 && var_can_have_subvars (var)
4049 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
4052 size_t uid = DECL_UID (var);
4054 up = get_or_create_used_part_for (uid);
4057 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
4058 up->implicit_uses = true;
4060 up_insert (uid, up);
4074 /* Create structure field variables for structures used in this function. */
4077 create_structure_vars (void)
4080 safe_referenced_var_iterator rvi;
4081 VEC (tree, heap) *varvec = NULL;
4084 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
4085 free_used_part_map);
4089 block_stmt_iterator bsi;
4092 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4097 FOR_EACH_PHI_ARG (use, phi, iter, SSA_OP_USE)
4099 tree op = USE_FROM_PTR (use);
4100 walk_tree_without_duplicates (&op, find_used_portions,
4105 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4107 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),