* gcc.dg/20061124-1.c: Add exit() function prototype.
[official-gcc.git] / gcc / tree-ssa-alias.c
bloba12dca28025bbcb903b6ca08efe31070725eb230
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "expr.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "tree-gimple.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "tree-ssa-structalias.h"
44 #include "convert.h"
45 #include "params.h"
46 #include "ipa-type-escape.h"
47 #include "vec.h"
48 #include "bitmap.h"
49 #include "vecprim.h"
51 /* Obstack used to hold grouping bitmaps and other temporary bitmaps used by
52 aliasing */
53 static bitmap_obstack alias_obstack;
55 /* 'true' after aliases have been computed (see compute_may_aliases). */
56 bool aliases_computed_p;
58 /* Structure to map a variable to its alias set and keep track of the
59 virtual operands that will be needed to represent it. */
60 struct alias_map_d
62 /* Variable and its alias set. */
63 tree var;
64 HOST_WIDE_INT set;
66 /* Total number of virtual operands that will be needed to represent
67 all the aliases of VAR. */
68 long total_alias_vops;
70 /* Nonzero if the aliases for this memory tag have been grouped
71 already. Used in group_aliases. */
72 unsigned int grouped_p : 1;
74 /* Set of variables aliased with VAR. This is the exact same
75 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
76 bitmap form to speed up alias grouping. */
77 bitmap may_aliases;
81 /* Counters used to display statistics on alias analysis. */
82 struct alias_stats_d
84 unsigned int alias_queries;
85 unsigned int alias_mayalias;
86 unsigned int alias_noalias;
87 unsigned int simple_queries;
88 unsigned int simple_resolved;
89 unsigned int tbaa_queries;
90 unsigned int tbaa_resolved;
91 unsigned int structnoaddress_queries;
92 unsigned int structnoaddress_resolved;
96 /* Local variables. */
97 static struct alias_stats_d alias_stats;
99 /* Local functions. */
100 static void compute_flow_insensitive_aliasing (struct alias_info *);
101 static void finalize_ref_all_pointers (struct alias_info *);
102 static void dump_alias_stats (FILE *);
103 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT, bool);
104 static tree create_memory_tag (tree type, bool is_type_tag);
105 static tree get_tmt_for (tree, struct alias_info *);
106 static tree get_nmt_for (tree);
107 static void add_may_alias (tree, tree);
108 static void replace_may_alias (tree, size_t, tree);
109 static struct alias_info *init_alias_info (void);
110 static void delete_alias_info (struct alias_info *);
111 static void compute_flow_sensitive_aliasing (struct alias_info *);
112 static void setup_pointers_and_addressables (struct alias_info *);
113 static void create_global_var (void);
114 static void maybe_create_global_var (struct alias_info *ai);
115 static void group_aliases (struct alias_info *);
116 static void set_pt_anything (tree ptr);
118 /* Global declarations. */
120 /* Call clobbered variables in the function. If bit I is set, then
121 REFERENCED_VARS (I) is call-clobbered. */
122 bitmap call_clobbered_vars;
124 /* Addressable variables in the function. If bit I is set, then
125 REFERENCED_VARS (I) has had its address taken. Note that
126 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
127 addressable variable is not necessarily call-clobbered (e.g., a
128 local addressable whose address does not escape) and not all
129 call-clobbered variables are addressable (e.g., a local static
130 variable). */
131 bitmap addressable_vars;
133 /* When the program has too many call-clobbered variables and call-sites,
134 this variable is used to represent the clobbering effects of function
135 calls. In these cases, all the call clobbered variables in the program
136 are forced to alias this variable. This reduces compile times by not
137 having to keep track of too many V_MAY_DEF expressions at call sites. */
138 tree global_var;
140 /* qsort comparison function to sort type/name tags by DECL_UID. */
142 static int
143 sort_tags_by_id (const void *pa, const void *pb)
145 tree a = *(tree *)pa;
146 tree b = *(tree *)pb;
148 return DECL_UID (a) - DECL_UID (b);
151 /* Initialize WORKLIST to contain those memory tags that are marked call
152 clobbered. Initialized WORKLIST2 to contain the reasons these
153 memory tags escaped. */
155 static void
156 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
157 VEC (int, heap) **worklist2)
159 referenced_var_iterator rvi;
160 tree curr;
162 FOR_EACH_REFERENCED_VAR (curr, rvi)
164 if (MTAG_P (curr) && is_call_clobbered (curr))
166 VEC_safe_push (tree, heap, *worklist, curr);
167 VEC_safe_push (int, heap, *worklist2, var_ann (curr)->escape_mask);
172 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
173 ALIAS is not already marked call clobbered, and is a memory
174 tag. */
176 static void
177 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
178 VEC (int, heap) **worklist2,
179 int reason)
181 if (MTAG_P (alias) && !is_call_clobbered (alias))
183 VEC_safe_push (tree, heap, *worklist, alias);
184 VEC_safe_push (int, heap, *worklist2, reason);
188 /* Mark aliases of TAG as call clobbered, and place any tags on the
189 alias list that were not already call clobbered on WORKLIST. */
191 static void
192 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
193 VEC (int, heap) **worklist2)
195 unsigned int i;
196 VEC (tree, gc) *ma;
197 tree entry;
198 var_ann_t ta = var_ann (tag);
200 if (!MTAG_P (tag))
201 return;
202 ma = may_aliases (tag);
203 if (!ma)
204 return;
206 for (i = 0; VEC_iterate (tree, ma, i, entry); i++)
208 if (!unmodifiable_var_p (entry))
210 add_to_worklist (entry, worklist, worklist2, ta->escape_mask);
211 mark_call_clobbered (entry, ta->escape_mask);
216 /* Tags containing global vars need to be marked as global.
217 Tags containing call clobbered vars need to be marked as call
218 clobbered. */
220 static void
221 compute_tag_properties (void)
223 referenced_var_iterator rvi;
224 tree tag;
225 bool changed = true;
226 VEC (tree, heap) *taglist = NULL;
228 FOR_EACH_REFERENCED_VAR (tag, rvi)
230 if (!MTAG_P (tag) || TREE_CODE (tag) == STRUCT_FIELD_TAG)
231 continue;
232 VEC_safe_push (tree, heap, taglist, tag);
235 /* We sort the taglist by DECL_UID, for two reasons.
236 1. To get a sequential ordering to make the bitmap accesses
237 faster.
238 2. Because of the way we compute aliases, it's more likely that
239 an earlier tag is included in a later tag, and this will reduce
240 the number of iterations.
242 If we had a real tag graph, we would just topo-order it and be
243 done with it. */
244 qsort (VEC_address (tree, taglist),
245 VEC_length (tree, taglist),
246 sizeof (tree),
247 sort_tags_by_id);
249 /* Go through each tag not marked as global, and if it aliases
250 global vars, mark it global.
252 If the tag contains call clobbered vars, mark it call
253 clobbered.
255 This loop iterates because tags may appear in the may-aliases
256 list of other tags when we group. */
258 while (changed)
260 unsigned int k;
262 changed = false;
263 for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
265 VEC (tree, gc) *ma;
266 unsigned int i;
267 tree entry;
268 bool tagcc = is_call_clobbered (tag);
269 bool tagglobal = MTAG_GLOBAL (tag);
271 if (tagcc && tagglobal)
272 continue;
274 ma = may_aliases (tag);
275 if (!ma)
276 continue;
278 for (i = 0; VEC_iterate (tree, ma, i, entry); i++)
280 /* Call clobbered entries cause the tag to be marked
281 call clobbered. */
282 if (!tagcc && is_call_clobbered (entry))
284 mark_call_clobbered (tag, var_ann (entry)->escape_mask);
285 tagcc = true;
286 changed = true;
289 /* Global vars cause the tag to be marked global. */
290 if (!tagglobal && is_global_var (entry))
292 MTAG_GLOBAL (tag) = true;
293 changed = true;
294 tagglobal = true;
297 /* Early exit once both global and cc are set, since the
298 loop can't do any more than that. */
299 if (tagcc && tagglobal)
300 break;
304 VEC_free (tree, heap, taglist);
307 /* Set up the initial variable clobbers and globalness.
308 When this function completes, only tags whose aliases need to be
309 clobbered will be set clobbered. Tags clobbered because they
310 contain call clobbered vars are handled in compute_tag_properties. */
312 static void
313 set_initial_properties (struct alias_info *ai)
315 unsigned int i;
316 referenced_var_iterator rvi;
317 tree var;
318 tree ptr;
320 FOR_EACH_REFERENCED_VAR (var, rvi)
322 if (is_global_var (var)
323 && (!var_can_have_subvars (var)
324 || get_subvars_for_var (var) == NULL))
326 if (!unmodifiable_var_p (var))
327 mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
329 else if (TREE_CODE (var) == PARM_DECL
330 && default_def (var)
331 && POINTER_TYPE_P (TREE_TYPE (var)))
333 tree def = default_def (var);
334 get_ptr_info (def)->value_escapes_p = 1;
335 get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;
339 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
341 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
342 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
344 if (pi->value_escapes_p)
346 /* If PTR escapes then its associated memory tags and
347 pointed-to variables are call-clobbered. */
348 if (pi->name_mem_tag)
349 mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
351 if (v_ann->symbol_mem_tag)
352 mark_call_clobbered (v_ann->symbol_mem_tag, pi->escape_mask);
354 if (pi->pt_vars)
356 bitmap_iterator bi;
357 unsigned int j;
358 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
359 if (!unmodifiable_var_p (referenced_var (j)))
360 mark_call_clobbered (referenced_var (j), pi->escape_mask);
364 /* If the name tag is call clobbered, so is the symbol tag
365 associated with the base VAR_DECL. */
366 if (pi->name_mem_tag
367 && v_ann->symbol_mem_tag
368 && is_call_clobbered (pi->name_mem_tag))
369 mark_call_clobbered (v_ann->symbol_mem_tag, pi->escape_mask);
371 /* Name tags and symbol tags that we don't know where they point
372 to, might point to global memory, and thus, are clobbered.
374 FIXME: This is not quite right. They should only be
375 clobbered if value_escapes_p is true, regardless of whether
376 they point to global memory or not.
377 So removing this code and fixing all the bugs would be nice.
378 It is the cause of a bunch of clobbering. */
379 if ((pi->pt_global_mem || pi->pt_anything)
380 && pi->is_dereferenced && pi->name_mem_tag)
382 mark_call_clobbered (pi->name_mem_tag, ESCAPE_IS_GLOBAL);
383 MTAG_GLOBAL (pi->name_mem_tag) = true;
386 if ((pi->pt_global_mem || pi->pt_anything)
387 && pi->is_dereferenced
388 && v_ann->symbol_mem_tag)
390 mark_call_clobbered (v_ann->symbol_mem_tag, ESCAPE_IS_GLOBAL);
391 MTAG_GLOBAL (v_ann->symbol_mem_tag) = true;
397 /* This variable is set to true if we are updating the used alone
398 information for SMTs, or are in a pass that is going to break it
399 temporarily. */
400 bool updating_used_alone;
402 /* Compute which variables need to be marked call clobbered because
403 their tag is call clobbered, and which tags need to be marked
404 global because they contain global variables. */
406 static void
407 compute_call_clobbered (struct alias_info *ai)
409 VEC (tree, heap) *worklist = NULL;
410 VEC(int,heap) *worklist2 = NULL;
412 set_initial_properties (ai);
413 init_transitive_clobber_worklist (&worklist, &worklist2);
414 while (VEC_length (tree, worklist) != 0)
416 tree curr = VEC_pop (tree, worklist);
417 int reason = VEC_pop (int, worklist2);
419 mark_call_clobbered (curr, reason);
420 mark_aliases_call_clobbered (curr, &worklist, &worklist2);
422 VEC_free (tree, heap, worklist);
423 VEC_free (int, heap, worklist2);
424 compute_tag_properties ();
428 /* Helper for recalculate_used_alone. Return a conservatively correct
429 answer as to whether STMT may make a store on the LHS to SYM. */
431 static bool
432 lhs_may_store_to (tree stmt, tree sym ATTRIBUTE_UNUSED)
434 tree lhs = TREE_OPERAND (stmt, 0);
436 lhs = get_base_address (lhs);
438 if (!lhs)
439 return false;
441 if (TREE_CODE (lhs) == SSA_NAME)
442 return false;
443 /* We could do better here by looking at the type tag of LHS, but it
444 is unclear whether this is worth it. */
445 return true;
448 /* Recalculate the used_alone information for SMTs . */
450 void
451 recalculate_used_alone (void)
453 VEC (tree, heap) *calls = NULL;
454 block_stmt_iterator bsi;
455 basic_block bb;
456 tree stmt;
457 size_t i;
458 referenced_var_iterator rvi;
459 tree var;
461 /* First, reset all the SMT used alone bits to zero. */
462 updating_used_alone = true;
463 FOR_EACH_REFERENCED_VAR (var, rvi)
464 if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
466 SMT_OLD_USED_ALONE (var) = SMT_USED_ALONE (var);
467 SMT_USED_ALONE (var) = 0;
470 /* Walk all the statements.
471 Calls get put into a list of statements to update, since we will
472 need to update operands on them if we make any changes.
473 If we see a bare use of a SMT anywhere in a real virtual use or virtual
474 def, mark the SMT as used alone, and for renaming. */
475 FOR_EACH_BB (bb)
477 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
479 bool iscall = false;
480 ssa_op_iter iter;
482 stmt = bsi_stmt (bsi);
484 if (TREE_CODE (stmt) == CALL_EXPR
485 || (TREE_CODE (stmt) == MODIFY_EXPR
486 && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR))
488 iscall = true;
489 VEC_safe_push (tree, heap, calls, stmt);
492 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter,
493 SSA_OP_VUSE | SSA_OP_VIRTUAL_DEFS)
495 tree svar = var;
497 if (TREE_CODE (var) == SSA_NAME)
498 svar = SSA_NAME_VAR (var);
500 if (TREE_CODE (svar) == SYMBOL_MEMORY_TAG)
502 /* We only care about the LHS on calls. */
503 if (iscall && !lhs_may_store_to (stmt, svar))
504 continue;
506 if (!SMT_USED_ALONE (svar))
508 SMT_USED_ALONE (svar) = true;
510 /* Only need to mark for renaming if it wasn't
511 used alone before. */
512 if (!SMT_OLD_USED_ALONE (svar))
513 mark_sym_for_renaming (svar);
520 /* Update the operands on all the calls we saw. */
521 if (calls)
523 for (i = 0; VEC_iterate (tree, calls, i, stmt); i++)
524 update_stmt (stmt);
527 /* We need to mark SMT's that are no longer used for renaming so the
528 symbols go away, or else verification will be angry with us, even
529 though they are dead. */
530 FOR_EACH_REFERENCED_VAR (var, rvi)
531 if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
533 if (SMT_OLD_USED_ALONE (var) && !SMT_USED_ALONE (var))
534 mark_sym_for_renaming (var);
537 VEC_free (tree, heap, calls);
538 updating_used_alone = false;
541 /* Compute may-alias information for every variable referenced in function
542 FNDECL.
544 Alias analysis proceeds in 3 main phases:
546 1- Points-to and escape analysis.
548 This phase walks the use-def chains in the SSA web looking for three
549 things:
551 * Assignments of the form P_i = &VAR
552 * Assignments of the form P_i = malloc()
553 * Pointers and ADDR_EXPR that escape the current function.
555 The concept of 'escaping' is the same one used in the Java world. When
556 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
557 outside of the current function. So, assignment to global variables,
558 function arguments and returning a pointer are all escape sites, as are
559 conversions between pointers and integers.
561 This is where we are currently limited. Since not everything is renamed
562 into SSA, we lose track of escape properties when a pointer is stashed
563 inside a field in a structure, for instance. In those cases, we are
564 assuming that the pointer does escape.
566 We use escape analysis to determine whether a variable is
567 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
568 is call-clobbered. If a pointer P_i escapes, then all the variables
569 pointed-to by P_i (and its memory tag) also escape.
571 2- Compute flow-sensitive aliases
573 We have two classes of memory tags. Memory tags associated with the
574 pointed-to data type of the pointers in the program. These tags are
575 called "symbol memory tag" (SMT). The other class are those associated
576 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
577 when adding operands for an INDIRECT_REF *P_i, we will first check
578 whether P_i has a name tag, if it does we use it, because that will have
579 more precise aliasing information. Otherwise, we use the standard symbol
580 tag.
582 In this phase, we go through all the pointers we found in points-to
583 analysis and create alias sets for the name memory tags associated with
584 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
585 it points to and its tag.
588 3- Compute flow-insensitive aliases
590 This pass will compare the alias set of every symbol memory tag and
591 every addressable variable found in the program. Given a symbol
592 memory tag SMT and an addressable variable V. If the alias sets of
593 SMT and V conflict (as computed by may_alias_p), then V is marked
594 as an alias tag and added to the alias set of SMT.
596 For instance, consider the following function:
598 foo (int i)
600 int *p, a, b;
602 if (i > 10)
603 p = &a;
604 else
605 p = &b;
607 *p = 3;
608 a = b + 2;
609 return *p;
612 After aliasing analysis has finished, the symbol memory tag for pointer
613 'p' will have two aliases, namely variables 'a' and 'b'. Every time
614 pointer 'p' is dereferenced, we want to mark the operation as a
615 potential reference to 'a' and 'b'.
617 foo (int i)
619 int *p, a, b;
621 if (i_2 > 10)
622 p_4 = &a;
623 else
624 p_6 = &b;
625 # p_1 = PHI <p_4(1), p_6(2)>;
627 # a_7 = V_MAY_DEF <a_3>;
628 # b_8 = V_MAY_DEF <b_5>;
629 *p_1 = 3;
631 # a_9 = V_MAY_DEF <a_7>
632 # VUSE <b_8>
633 a_9 = b_8 + 2;
635 # VUSE <a_9>;
636 # VUSE <b_8>;
637 return *p_1;
640 In certain cases, the list of may aliases for a pointer may grow too
641 large. This may cause an explosion in the number of virtual operands
642 inserted in the code. Resulting in increased memory consumption and
643 compilation time.
645 When the number of virtual operands needed to represent aliased
646 loads and stores grows too large (configurable with @option{--param
647 max-aliased-vops}), alias sets are grouped to avoid severe
648 compile-time slow downs and memory consumption. See group_aliases. */
650 static unsigned int
651 compute_may_aliases (void)
653 struct alias_info *ai;
655 memset (&alias_stats, 0, sizeof (alias_stats));
657 /* Initialize aliasing information. */
658 ai = init_alias_info ();
660 /* For each pointer P_i, determine the sets of variables that P_i may
661 point-to. For every addressable variable V, determine whether the
662 address of V escapes the current function, making V call-clobbered
663 (i.e., whether &V is stored in a global variable or if its passed as a
664 function call argument). */
665 compute_points_to_sets (ai);
667 /* Collect all pointers and addressable variables, compute alias sets,
668 create memory tags for pointers and promote variables whose address is
669 not needed anymore. */
670 setup_pointers_and_addressables (ai);
672 /* Compute flow-sensitive, points-to based aliasing for all the name
673 memory tags. Note that this pass needs to be done before flow
674 insensitive analysis because it uses the points-to information
675 gathered before to mark call-clobbered symbol tags. */
676 compute_flow_sensitive_aliasing (ai);
678 /* Compute type-based flow-insensitive aliasing for all the type
679 memory tags. */
680 compute_flow_insensitive_aliasing (ai);
682 /* Compute call clobbering information. */
683 compute_call_clobbered (ai);
685 /* Determine if we need to enable alias grouping. */
686 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
687 group_aliases (ai);
689 /* If the program has too many call-clobbered variables and/or function
690 calls, create .GLOBAL_VAR and use it to model call-clobbering
691 semantics at call sites. This reduces the number of virtual operands
692 considerably, improving compile times at the expense of lost
693 aliasing precision. */
694 maybe_create_global_var (ai);
696 /* If the program contains ref-all pointers, finalize may-alias information
697 for them. This pass needs to be run after call-clobbering information
698 has been computed. */
699 if (ai->ref_all_symbol_mem_tag)
700 finalize_ref_all_pointers (ai);
702 /* Debugging dumps. */
703 if (dump_file)
705 dump_referenced_vars (dump_file);
706 if (dump_flags & TDF_STATS)
707 dump_alias_stats (dump_file);
708 dump_points_to_info (dump_file);
709 dump_alias_info (dump_file);
712 /* Deallocate memory used by aliasing data structures. */
713 delete_alias_info (ai);
715 updating_used_alone = true;
717 block_stmt_iterator bsi;
718 basic_block bb;
719 FOR_EACH_BB (bb)
721 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
723 update_stmt_if_modified (bsi_stmt (bsi));
727 recalculate_used_alone ();
728 updating_used_alone = false;
729 return 0;
733 struct tree_opt_pass pass_may_alias =
735 "alias", /* name */
736 NULL, /* gate */
737 compute_may_aliases, /* execute */
738 NULL, /* sub */
739 NULL, /* next */
740 0, /* static_pass_number */
741 TV_TREE_MAY_ALIAS, /* tv_id */
742 PROP_cfg | PROP_ssa, /* properties_required */
743 PROP_alias, /* properties_provided */
744 0, /* properties_destroyed */
745 0, /* todo_flags_start */
746 TODO_dump_func | TODO_update_ssa
747 | TODO_ggc_collect | TODO_verify_ssa
748 | TODO_verify_stmts, /* todo_flags_finish */
749 0 /* letter */
753 /* Data structure used to count the number of dereferences to PTR
754 inside an expression. */
755 struct count_ptr_d
757 tree ptr;
758 unsigned count;
762 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
763 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
765 static tree
766 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
768 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
770 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
771 pointer 'ptr' is *not* dereferenced, it is simply used to compute
772 the address of 'fld' as 'ptr + offsetof(fld)'. */
773 if (TREE_CODE (*tp) == ADDR_EXPR)
775 *walk_subtrees = 0;
776 return NULL_TREE;
779 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
780 count_p->count++;
782 return NULL_TREE;
786 /* Count the number of direct and indirect uses for pointer PTR in
787 statement STMT. The two counts are stored in *NUM_USES_P and
788 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
789 least one of those dereferences is a store operation. */
791 void
792 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
793 unsigned *num_derefs_p, bool *is_store)
795 ssa_op_iter i;
796 tree use;
798 *num_uses_p = 0;
799 *num_derefs_p = 0;
800 *is_store = false;
802 /* Find out the total number of uses of PTR in STMT. */
803 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
804 if (use == ptr)
805 (*num_uses_p)++;
807 /* Now count the number of indirect references to PTR. This is
808 truly awful, but we don't have much choice. There are no parent
809 pointers inside INDIRECT_REFs, so an expression like
810 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
811 find all the indirect and direct uses of x_1 inside. The only
812 shortcut we can take is the fact that GIMPLE only allows
813 INDIRECT_REFs inside the expressions below. */
814 if (TREE_CODE (stmt) == MODIFY_EXPR
815 || (TREE_CODE (stmt) == RETURN_EXPR
816 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
817 || TREE_CODE (stmt) == ASM_EXPR
818 || TREE_CODE (stmt) == CALL_EXPR)
820 tree lhs, rhs;
822 if (TREE_CODE (stmt) == MODIFY_EXPR)
824 lhs = TREE_OPERAND (stmt, 0);
825 rhs = TREE_OPERAND (stmt, 1);
827 else if (TREE_CODE (stmt) == RETURN_EXPR)
829 tree e = TREE_OPERAND (stmt, 0);
830 lhs = TREE_OPERAND (e, 0);
831 rhs = TREE_OPERAND (e, 1);
833 else if (TREE_CODE (stmt) == ASM_EXPR)
835 lhs = ASM_OUTPUTS (stmt);
836 rhs = ASM_INPUTS (stmt);
838 else
840 lhs = NULL_TREE;
841 rhs = stmt;
844 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
846 struct count_ptr_d count;
847 count.ptr = ptr;
848 count.count = 0;
849 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
850 *is_store = true;
851 *num_derefs_p = count.count;
854 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
856 struct count_ptr_d count;
857 count.ptr = ptr;
858 count.count = 0;
859 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
860 *num_derefs_p += count.count;
864 gcc_assert (*num_uses_p >= *num_derefs_p);
867 /* Initialize the data structures used for alias analysis. */
869 static struct alias_info *
870 init_alias_info (void)
872 struct alias_info *ai;
873 referenced_var_iterator rvi;
874 tree var;
876 bitmap_obstack_initialize (&alias_obstack);
877 ai = XCNEW (struct alias_info);
878 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
879 sbitmap_zero (ai->ssa_names_visited);
880 ai->processed_ptrs = VEC_alloc (tree, heap, 50);
881 ai->written_vars = BITMAP_ALLOC (&alias_obstack);
882 ai->dereferenced_ptrs_store = BITMAP_ALLOC (&alias_obstack);
883 ai->dereferenced_ptrs_load = BITMAP_ALLOC (&alias_obstack);
885 /* If aliases have been computed before, clear existing information. */
886 if (aliases_computed_p)
888 unsigned i;
890 /* Similarly, clear the set of addressable variables. In this
891 case, we can just clear the set because addressability is
892 only computed here. */
893 bitmap_clear (addressable_vars);
895 /* Clear flow-insensitive alias information from each symbol. */
896 FOR_EACH_REFERENCED_VAR (var, rvi)
898 var_ann_t ann = var_ann (var);
900 ann->is_aliased = 0;
901 ann->may_aliases = NULL;
902 NUM_REFERENCES_CLEAR (ann);
904 /* Since we are about to re-discover call-clobbered
905 variables, clear the call-clobbered flag. Variables that
906 are intrinsically call-clobbered (globals, local statics,
907 etc) will not be marked by the aliasing code, so we can't
908 remove them from CALL_CLOBBERED_VARS.
910 NB: STRUCT_FIELDS are still call clobbered if they are for
911 a global variable, so we *don't* clear their call clobberedness
912 just because they are tags, though we will clear it if they
913 aren't for global variables. */
914 if (TREE_CODE (var) == NAME_MEMORY_TAG
915 || TREE_CODE (var) == SYMBOL_MEMORY_TAG
916 || !is_global_var (var))
917 clear_call_clobbered (var);
920 /* Clear flow-sensitive points-to information from each SSA name. */
921 for (i = 1; i < num_ssa_names; i++)
923 tree name = ssa_name (i);
925 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
926 continue;
928 if (SSA_NAME_PTR_INFO (name))
930 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
932 /* Clear all the flags but keep the name tag to
933 avoid creating new temporaries unnecessarily. If
934 this pointer is found to point to a subset or
935 superset of its former points-to set, then a new
936 tag will need to be created in create_name_tags. */
937 pi->pt_anything = 0;
938 pi->pt_null = 0;
939 pi->value_escapes_p = 0;
940 pi->is_dereferenced = 0;
941 if (pi->pt_vars)
942 bitmap_clear (pi->pt_vars);
947 /* Next time, we will need to reset alias information. */
948 aliases_computed_p = true;
950 return ai;
954 /* Deallocate memory used by alias analysis. */
956 static void
957 delete_alias_info (struct alias_info *ai)
959 size_t i;
960 referenced_var_iterator rvi;
961 tree var;
963 sbitmap_free (ai->ssa_names_visited);
964 VEC_free (tree, heap, ai->processed_ptrs);
966 for (i = 0; i < ai->num_addressable_vars; i++)
967 free (ai->addressable_vars[i]);
969 FOR_EACH_REFERENCED_VAR(var, rvi)
971 var_ann_t ann = var_ann (var);
972 NUM_REFERENCES_CLEAR (ann);
975 free (ai->addressable_vars);
977 for (i = 0; i < ai->num_pointers; i++)
978 free (ai->pointers[i]);
979 free (ai->pointers);
981 BITMAP_FREE (ai->written_vars);
982 BITMAP_FREE (ai->dereferenced_ptrs_store);
983 BITMAP_FREE (ai->dereferenced_ptrs_load);
984 bitmap_obstack_release (&alias_obstack);
985 free (ai);
987 delete_points_to_sets ();
990 /* Used for hashing to identify pointer infos with identical
991 pt_vars bitmaps. */
992 static int
993 eq_ptr_info (const void *p1, const void *p2)
995 const struct ptr_info_def *n1 = (const struct ptr_info_def *) p1;
996 const struct ptr_info_def *n2 = (const struct ptr_info_def *) p2;
997 return bitmap_equal_p (n1->pt_vars, n2->pt_vars);
1000 static hashval_t
1001 ptr_info_hash (const void *p)
1003 const struct ptr_info_def *n = (const struct ptr_info_def *) p;
1004 return bitmap_hash (n->pt_vars);
1007 /* Create name tags for all the pointers that have been dereferenced.
1008 We only create a name tag for a pointer P if P is found to point to
1009 a set of variables (so that we can alias them to *P) or if it is
1010 the result of a call to malloc (which means that P cannot point to
1011 anything else nor alias any other variable).
1013 If two pointers P and Q point to the same set of variables, they
1014 are assigned the same name tag. */
1016 static void
1017 create_name_tags (void)
1019 size_t i;
1020 VEC (tree, heap) *with_ptvars = NULL;
1021 tree ptr;
1022 htab_t ptr_hash;
1024 /* Collect the list of pointers with a non-empty points to set. */
1025 for (i = 1; i < num_ssa_names; i++)
1027 tree ptr = ssa_name (i);
1028 struct ptr_info_def *pi;
1030 if (!ptr
1031 || !POINTER_TYPE_P (TREE_TYPE (ptr))
1032 || !SSA_NAME_PTR_INFO (ptr))
1033 continue;
1035 pi = SSA_NAME_PTR_INFO (ptr);
1037 if (pi->pt_anything || !pi->is_dereferenced)
1039 /* No name tags for pointers that have not been
1040 dereferenced or point to an arbitrary location. */
1041 pi->name_mem_tag = NULL_TREE;
1042 continue;
1045 /* Set pt_anything on the pointers without pt_vars filled in so
1046 that they are assigned a symbol tag. */
1047 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
1048 VEC_safe_push (tree, heap, with_ptvars, ptr);
1049 else
1050 set_pt_anything (ptr);
1053 /* If we didn't find any pointers with pt_vars set, we're done. */
1054 if (!with_ptvars)
1055 return;
1057 ptr_hash = htab_create (10, ptr_info_hash, eq_ptr_info, NULL);
1058 /* Now go through the pointers with pt_vars, and find a name tag
1059 with the same pt_vars as this pointer, or create one if one
1060 doesn't exist. */
1061 for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
1063 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1064 tree old_name_tag = pi->name_mem_tag;
1065 struct ptr_info_def **slot;
1067 /* If PTR points to a set of variables, check if we don't
1068 have another pointer Q with the same points-to set before
1069 creating a tag. If so, use Q's tag instead of creating a
1070 new one.
1072 This is important for not creating unnecessary symbols
1073 and also for copy propagation. If we ever need to
1074 propagate PTR into Q or vice-versa, we would run into
1075 problems if they both had different name tags because
1076 they would have different SSA version numbers (which
1077 would force us to take the name tags in and out of SSA). */
1079 slot = (struct ptr_info_def **) htab_find_slot (ptr_hash, pi, INSERT);
1080 if (*slot)
1081 pi->name_mem_tag = (*slot)->name_mem_tag;
1082 else
1084 *slot = pi;
1085 /* If we didn't find a pointer with the same points-to set
1086 as PTR, create a new name tag if needed. */
1087 if (pi->name_mem_tag == NULL_TREE)
1088 pi->name_mem_tag = get_nmt_for (ptr);
1091 /* If the new name tag computed for PTR is different than
1092 the old name tag that it used to have, then the old tag
1093 needs to be removed from the IL, so we mark it for
1094 renaming. */
1095 if (old_name_tag && old_name_tag != pi->name_mem_tag)
1096 mark_sym_for_renaming (old_name_tag);
1098 TREE_THIS_VOLATILE (pi->name_mem_tag)
1099 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
1101 /* Mark the new name tag for renaming. */
1102 mark_sym_for_renaming (pi->name_mem_tag);
1104 htab_delete (ptr_hash);
1106 VEC_free (tree, heap, with_ptvars);
1110 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
1111 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
1112 name tag and the variables it points-to are call-clobbered. Finally, if
1113 P_i escapes and we could not determine where it points to, then all the
1114 variables in the same alias set as *P_i are marked call-clobbered. This
1115 is necessary because we must assume that P_i may take the address of any
1116 variable in the same alias set. */
1118 static void
1119 compute_flow_sensitive_aliasing (struct alias_info *ai)
1121 size_t i;
1122 tree ptr;
1124 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
1126 if (!find_what_p_points_to (ptr))
1127 set_pt_anything (ptr);
1130 create_name_tags ();
1132 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
1134 unsigned j;
1135 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1136 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
1137 bitmap_iterator bi;
1140 /* Set up aliasing information for PTR's name memory tag (if it has
1141 one). Note that only pointers that have been dereferenced will
1142 have a name memory tag. */
1143 if (pi->name_mem_tag && pi->pt_vars)
1144 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
1146 add_may_alias (pi->name_mem_tag, referenced_var (j));
1147 add_may_alias (v_ann->symbol_mem_tag, referenced_var (j));
1153 /* Compute type-based alias sets. Traverse all the pointers and
1154 addressable variables found in setup_pointers_and_addressables.
1156 For every pointer P in AI->POINTERS and addressable variable V in
1157 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's symbol
1158 memory tag (SMT) if their alias sets conflict. V is then marked as
1159 an alias tag so that the operand scanner knows that statements
1160 containing V have aliased operands. */
1162 static void
1163 compute_flow_insensitive_aliasing (struct alias_info *ai)
1165 size_t i;
1167 /* Initialize counter for the total number of virtual operands that
1168 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
1169 threshold set by --params max-alias-vops, we enable alias
1170 grouping. */
1171 ai->total_alias_vops = 0;
1173 /* For every pointer P, determine which addressable variables may alias
1174 with P's symbol memory tag. */
1175 for (i = 0; i < ai->num_pointers; i++)
1177 size_t j;
1178 struct alias_map_d *p_map = ai->pointers[i];
1179 tree tag = var_ann (p_map->var)->symbol_mem_tag;
1180 var_ann_t tag_ann = var_ann (tag);
1181 tree var;
1183 /* Call-clobbering information is not finalized yet at this point. */
1184 if (PTR_IS_REF_ALL (p_map->var))
1185 continue;
1187 p_map->total_alias_vops = 0;
1188 p_map->may_aliases = BITMAP_ALLOC (&alias_obstack);
1190 /* Add any pre-existing may_aliases to the bitmap used to represent
1191 TAG's alias set in case we need to group aliases. */
1192 for (j = 0; VEC_iterate (tree, tag_ann->may_aliases, j, var); ++j)
1193 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
1195 for (j = 0; j < ai->num_addressable_vars; j++)
1197 struct alias_map_d *v_map;
1198 var_ann_t v_ann;
1199 bool tag_stored_p, var_stored_p;
1201 v_map = ai->addressable_vars[j];
1202 var = v_map->var;
1203 v_ann = var_ann (var);
1205 /* Skip memory tags and variables that have never been
1206 written to. We also need to check if the variables are
1207 call-clobbered because they may be overwritten by
1208 function calls.
1210 Note this is effectively random accessing elements in
1211 the sparse bitset, which can be highly inefficient.
1212 So we first check the call_clobbered status of the
1213 tag and variable before querying the bitmap. */
1214 tag_stored_p = is_call_clobbered (tag)
1215 || bitmap_bit_p (ai->written_vars, DECL_UID (tag));
1216 var_stored_p = is_call_clobbered (var)
1217 || bitmap_bit_p (ai->written_vars, DECL_UID (var));
1218 if (!tag_stored_p && !var_stored_p)
1219 continue;
1221 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
1223 size_t num_tag_refs, num_var_refs;
1225 num_tag_refs = NUM_REFERENCES (tag_ann);
1226 num_var_refs = NUM_REFERENCES (v_ann);
1228 /* Add VAR to TAG's may-aliases set. */
1230 /* We should never have a var with subvars here, because
1231 they shouldn't get into the set of addressable vars */
1232 gcc_assert (!var_can_have_subvars (var)
1233 || get_subvars_for_var (var) == NULL);
1235 add_may_alias (tag, var);
1236 /* Update the bitmap used to represent TAG's alias set
1237 in case we need to group aliases. */
1238 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
1240 /* Update the total number of virtual operands due to
1241 aliasing. Since we are adding one more alias to TAG's
1242 may-aliases set, the total number of virtual operands due
1243 to aliasing will be increased by the number of references
1244 made to VAR and TAG (every reference to TAG will also
1245 count as a reference to VAR). */
1246 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1247 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1254 /* Since this analysis is based exclusively on symbols, it fails to
1255 handle cases where two pointers P and Q have different memory
1256 tags with conflicting alias set numbers but no aliased symbols in
1257 common.
1259 For example, suppose that we have two memory tags SMT.1 and SMT.2
1260 such that
1262 may-aliases (SMT.1) = { a }
1263 may-aliases (SMT.2) = { b }
1265 and the alias set number of SMT.1 conflicts with that of SMT.2.
1266 Since they don't have symbols in common, loads and stores from
1267 SMT.1 and SMT.2 will seem independent of each other, which will
1268 lead to the optimizers making invalid transformations (see
1269 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1271 To avoid this problem, we do a final traversal of AI->POINTERS
1272 looking for pairs of pointers that have no aliased symbols in
1273 common and yet have conflicting alias set numbers. */
1274 for (i = 0; i < ai->num_pointers; i++)
1276 size_t j;
1277 struct alias_map_d *p_map1 = ai->pointers[i];
1278 tree tag1 = var_ann (p_map1->var)->symbol_mem_tag;
1279 bitmap may_aliases1 = p_map1->may_aliases;
1281 if (PTR_IS_REF_ALL (p_map1->var))
1282 continue;
1284 for (j = i + 1; j < ai->num_pointers; j++)
1286 struct alias_map_d *p_map2 = ai->pointers[j];
1287 tree tag2 = var_ann (p_map2->var)->symbol_mem_tag;
1288 bitmap may_aliases2 = p_map2->may_aliases;
1290 if (PTR_IS_REF_ALL (p_map2->var))
1291 continue;
1293 /* If the pointers may not point to each other, do nothing. */
1294 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
1295 continue;
1297 /* The two pointers may alias each other. If they already have
1298 symbols in common, do nothing. */
1299 if (bitmap_intersect_p (may_aliases1, may_aliases2))
1300 continue;
1302 if (!bitmap_empty_p (may_aliases2))
1304 unsigned int k;
1305 bitmap_iterator bi;
1307 /* Add all the aliases for TAG2 into TAG1's alias set.
1308 FIXME, update grouping heuristic counters. */
1309 EXECUTE_IF_SET_IN_BITMAP (may_aliases2, 0, k, bi)
1310 add_may_alias (tag1, referenced_var (k));
1311 bitmap_ior_into (may_aliases1, may_aliases2);
1313 else
1315 /* Since TAG2 does not have any aliases of its own, add
1316 TAG2 itself to the alias set of TAG1. */
1317 add_may_alias (tag1, tag2);
1318 bitmap_set_bit (may_aliases1, DECL_UID (tag2));
1323 if (dump_file)
1324 fprintf (dump_file, "\n%s: Total number of aliased vops: %ld\n",
1325 get_name (current_function_decl),
1326 ai->total_alias_vops);
1330 /* Finalize may-alias information for ref-all pointers. Traverse all
1331 the addressable variables found in setup_pointers_and_addressables.
1333 If flow-sensitive alias analysis has attached a name memory tag to
1334 a ref-all pointer, we will use it for the dereferences because that
1335 will have more precise aliasing information. But if there is no
1336 name tag, we will use a special symbol tag that aliases all the
1337 call-clobbered addressable variables. */
1339 static void
1340 finalize_ref_all_pointers (struct alias_info *ai)
1342 size_t i;
1344 if (global_var)
1345 add_may_alias (ai->ref_all_symbol_mem_tag, global_var);
1346 else
1348 /* First add the real call-clobbered variables. */
1349 for (i = 0; i < ai->num_addressable_vars; i++)
1351 tree var = ai->addressable_vars[i]->var;
1352 if (is_call_clobbered (var))
1353 add_may_alias (ai->ref_all_symbol_mem_tag, var);
1356 /* Then add the call-clobbered pointer memory tags. See
1357 compute_flow_insensitive_aliasing for the rationale. */
1358 for (i = 0; i < ai->num_pointers; i++)
1360 tree ptr = ai->pointers[i]->var, tag;
1361 if (PTR_IS_REF_ALL (ptr))
1362 continue;
1363 tag = var_ann (ptr)->symbol_mem_tag;
1364 if (is_call_clobbered (tag))
1365 add_may_alias (ai->ref_all_symbol_mem_tag, tag);
1371 /* Comparison function for qsort used in group_aliases. */
1373 static int
1374 total_alias_vops_cmp (const void *p, const void *q)
1376 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1377 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1378 long n1 = (*p1)->total_alias_vops;
1379 long n2 = (*p2)->total_alias_vops;
1381 /* We want to sort in descending order. */
1382 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1385 /* Group all the aliases for TAG to make TAG represent all the
1386 variables in its alias set. Update the total number
1387 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1388 function will make TAG be the unique alias tag for all the
1389 variables in its may-aliases. So, given:
1391 may-aliases(TAG) = { V1, V2, V3 }
1393 This function will group the variables into:
1395 may-aliases(V1) = { TAG }
1396 may-aliases(V2) = { TAG }
1397 may-aliases(V2) = { TAG } */
1399 static void
1400 group_aliases_into (tree tag, bitmap tag_aliases, struct alias_info *ai)
1402 unsigned int i;
1403 var_ann_t tag_ann = var_ann (tag);
1404 size_t num_tag_refs = NUM_REFERENCES (tag_ann);
1405 bitmap_iterator bi;
1407 EXECUTE_IF_SET_IN_BITMAP (tag_aliases, 0, i, bi)
1409 tree var = referenced_var (i);
1410 var_ann_t ann = var_ann (var);
1412 /* Make TAG the unique alias of VAR. */
1413 ann->is_aliased = 0;
1414 ann->may_aliases = NULL;
1416 /* Note that VAR and TAG may be the same if the function has no
1417 addressable variables (see the discussion at the end of
1418 setup_pointers_and_addressables). */
1419 if (var != tag)
1420 add_may_alias (var, tag);
1422 /* Reduce total number of virtual operands contributed
1423 by TAG on behalf of VAR. Notice that the references to VAR
1424 itself won't be removed. We will merely replace them with
1425 references to TAG. */
1426 ai->total_alias_vops -= num_tag_refs;
1429 /* We have reduced the number of virtual operands that TAG makes on
1430 behalf of all the variables formerly aliased with it. However,
1431 we have also "removed" all the virtual operands for TAG itself,
1432 so we add them back. */
1433 ai->total_alias_vops += num_tag_refs;
1435 /* TAG no longer has any aliases. */
1436 tag_ann->may_aliases = NULL;
1439 /* Simple comparison function for qsort that sorts based on pointer
1440 address. */
1442 static int
1443 tree_pointer_compare (const void *pa, const void *pb)
1445 const tree a = *((const tree *)pa);
1446 const tree b = *((const tree *)pb);
1448 return b - a;
1452 /* Replacing may aliases in name tags during grouping can up with the
1453 same SMT multiple times in the may_alias list. It's quicker to
1454 just remove them post-hoc than it is to avoid them during
1455 replacement. Thus, this routine sorts the may-alias list and
1456 removes duplicates. */
1458 static void
1459 compact_name_tags (void)
1461 referenced_var_iterator rvi;
1462 tree var;
1464 FOR_EACH_REFERENCED_VAR (var, rvi)
1466 if (TREE_CODE (var) == NAME_MEMORY_TAG)
1468 VEC(tree, gc) *aliases, *new_aliases;
1469 tree alias, last_alias;
1470 int i;
1472 last_alias = NULL;
1473 aliases = var_ann (var)->may_aliases;
1474 new_aliases = NULL;
1476 if (VEC_length (tree, aliases) > 1)
1478 bool changed = false;
1479 qsort (VEC_address (tree, aliases), VEC_length (tree, aliases),
1480 sizeof (tree), tree_pointer_compare);
1482 for (i = 0; VEC_iterate (tree, aliases, i, alias); i++)
1484 if (alias == last_alias)
1486 changed = true;
1487 continue;
1490 VEC_safe_push (tree, gc, new_aliases, alias);
1491 last_alias = alias;
1494 /* Only replace the array if something has changed. */
1495 if (changed)
1497 VEC_free (tree, gc, aliases);
1498 var_ann (var)->may_aliases = new_aliases;
1500 else
1501 VEC_free (tree, gc, new_aliases);
1507 /* Group may-aliases sets to reduce the number of virtual operands due
1508 to aliasing.
1510 1- Sort the list of pointers in decreasing number of contributed
1511 virtual operands.
1513 2- Take the first entry in AI->POINTERS and revert the role of
1514 the memory tag and its aliases. Usually, whenever an aliased
1515 variable Vi is found to alias with a memory tag T, we add Vi
1516 to the may-aliases set for T. Meaning that after alias
1517 analysis, we will have:
1519 may-aliases(T) = { V1, V2, V3, ..., Vn }
1521 This means that every statement that references T, will get 'n'
1522 virtual operands for each of the Vi tags. But, when alias
1523 grouping is enabled, we make T an alias tag and add it to the
1524 alias set of all the Vi variables:
1526 may-aliases(V1) = { T }
1527 may-aliases(V2) = { T }
1529 may-aliases(Vn) = { T }
1531 This has two effects: (a) statements referencing T will only get
1532 a single virtual operand, and, (b) all the variables Vi will now
1533 appear to alias each other. So, we lose alias precision to
1534 improve compile time. But, in theory, a program with such a high
1535 level of aliasing should not be very optimizable in the first
1536 place.
1538 3- Since variables may be in the alias set of more than one
1539 memory tag, the grouping done in step (2) needs to be extended
1540 to all the memory tags that have a non-empty intersection with
1541 the may-aliases set of tag T. For instance, if we originally
1542 had these may-aliases sets:
1544 may-aliases(T) = { V1, V2, V3 }
1545 may-aliases(R) = { V2, V4 }
1547 In step (2) we would have reverted the aliases for T as:
1549 may-aliases(V1) = { T }
1550 may-aliases(V2) = { T }
1551 may-aliases(V3) = { T }
1553 But note that now V2 is no longer aliased with R. We could
1554 add R to may-aliases(V2), but we are in the process of
1555 grouping aliases to reduce virtual operands so what we do is
1556 add V4 to the grouping to obtain:
1558 may-aliases(V1) = { T }
1559 may-aliases(V2) = { T }
1560 may-aliases(V3) = { T }
1561 may-aliases(V4) = { T }
1563 4- If the total number of virtual operands due to aliasing is
1564 still above the threshold set by max-alias-vops, go back to (2). */
1566 static void
1567 group_aliases (struct alias_info *ai)
1569 size_t i;
1570 tree ptr;
1572 /* Sort the POINTERS array in descending order of contributed
1573 virtual operands. */
1574 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1575 total_alias_vops_cmp);
1577 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1578 and the tag's may-aliases set. */
1579 for (i = 0; i < ai->num_pointers; i++)
1581 size_t j;
1582 tree tag1 = var_ann (ai->pointers[i]->var)->symbol_mem_tag;
1583 bitmap tag1_aliases = ai->pointers[i]->may_aliases;
1585 /* Skip tags that have been grouped already. */
1586 if (ai->pointers[i]->grouped_p)
1587 continue;
1589 /* See if TAG1 had any aliases in common with other symbol tags.
1590 If we find a TAG2 with common aliases with TAG1, add TAG2's
1591 aliases into TAG1. */
1592 for (j = i + 1; j < ai->num_pointers; j++)
1594 bitmap tag2_aliases = ai->pointers[j]->may_aliases;
1596 if (bitmap_intersect_p (tag1_aliases, tag2_aliases))
1598 tree tag2 = var_ann (ai->pointers[j]->var)->symbol_mem_tag;
1600 bitmap_ior_into (tag1_aliases, tag2_aliases);
1602 /* TAG2 does not need its aliases anymore. */
1603 bitmap_clear (tag2_aliases);
1604 var_ann (tag2)->may_aliases = NULL;
1606 /* TAG1 is the unique alias of TAG2. */
1607 add_may_alias (tag2, tag1);
1609 ai->pointers[j]->grouped_p = true;
1613 /* Now group all the aliases we collected into TAG1. */
1614 group_aliases_into (tag1, tag1_aliases, ai);
1616 /* If we've reduced total number of virtual operands below the
1617 threshold, stop. */
1618 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1619 break;
1622 /* Finally, all the variables that have been grouped cannot be in
1623 the may-alias set of name memory tags. Suppose that we have
1624 grouped the aliases in this code so that may-aliases(a) = SMT.20
1626 p_5 = &a;
1628 # a_9 = V_MAY_DEF <a_8>
1629 p_5->field = 0
1630 ... Several modifications to SMT.20 ...
1631 # VUSE <a_9>
1632 x_30 = p_5->field
1634 Since p_5 points to 'a', the optimizers will try to propagate 0
1635 into p_5->field, but that is wrong because there have been
1636 modifications to 'SMT.20' in between. To prevent this we have to
1637 replace 'a' with 'SMT.20' in the name tag of p_5. */
1638 for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
1640 size_t j;
1641 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1642 VEC(tree,gc) *aliases;
1643 tree alias;
1645 if (name_tag == NULL_TREE)
1646 continue;
1648 aliases = var_ann (name_tag)->may_aliases;
1649 for (j = 0; VEC_iterate (tree, aliases, j, alias); j++)
1651 var_ann_t ann = var_ann (alias);
1653 if ((!MTAG_P (alias)
1654 || TREE_CODE (alias) == STRUCT_FIELD_TAG)
1655 && ann->may_aliases)
1657 tree new_alias;
1659 gcc_assert (VEC_length (tree, ann->may_aliases) == 1);
1661 new_alias = VEC_index (tree, ann->may_aliases, 0);
1662 replace_may_alias (name_tag, j, new_alias);
1667 compact_name_tags ();
1669 if (dump_file)
1670 fprintf (dump_file,
1671 "%s: Total number of aliased vops after grouping: %ld%s\n",
1672 get_name (current_function_decl),
1673 ai->total_alias_vops,
1674 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1678 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1680 static void
1681 create_alias_map_for (tree var, struct alias_info *ai)
1683 struct alias_map_d *alias_map;
1684 alias_map = XCNEW (struct alias_map_d);
1685 alias_map->var = var;
1686 alias_map->set = get_alias_set (var);
1687 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1691 /* Create memory tags for all the dereferenced pointers and build the
1692 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1693 sets. Based on the address escape and points-to information collected
1694 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1695 variables whose address is not needed anymore. */
1697 static void
1698 setup_pointers_and_addressables (struct alias_info *ai)
1700 size_t n_vars, num_addressable_vars, num_pointers;
1701 referenced_var_iterator rvi;
1702 tree var;
1703 VEC (tree, heap) *varvec = NULL;
1704 safe_referenced_var_iterator srvi;
1706 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1707 num_addressable_vars = num_pointers = 0;
1709 FOR_EACH_REFERENCED_VAR (var, rvi)
1711 if (may_be_aliased (var))
1712 num_addressable_vars++;
1714 if (POINTER_TYPE_P (TREE_TYPE (var)))
1716 /* Since we don't keep track of volatile variables, assume that
1717 these pointers are used in indirect store operations. */
1718 if (TREE_THIS_VOLATILE (var))
1719 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
1721 num_pointers++;
1725 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1726 always going to be slightly bigger than we actually need them
1727 because some TREE_ADDRESSABLE variables will be marked
1728 non-addressable below and only pointers with unique symbol tags are
1729 going to be added to POINTERS. */
1730 ai->addressable_vars = XCNEWVEC (struct alias_map_d *, num_addressable_vars);
1731 ai->pointers = XCNEWVEC (struct alias_map_d *, num_pointers);
1732 ai->num_addressable_vars = 0;
1733 ai->num_pointers = 0;
1735 /* Since we will be creating symbol memory tags within this loop,
1736 cache the value of NUM_REFERENCED_VARS to avoid processing the
1737 additional tags unnecessarily. */
1738 n_vars = num_referenced_vars;
1740 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
1742 var_ann_t v_ann = var_ann (var);
1743 subvar_t svars;
1745 /* Name memory tags already have flow-sensitive aliasing
1746 information, so they need not be processed by
1747 compute_flow_insensitive_aliasing. Similarly, symbol memory
1748 tags are already accounted for when we process their
1749 associated pointer.
1751 Structure fields, on the other hand, have to have some of this
1752 information processed for them, but it's pointless to mark them
1753 non-addressable (since they are fake variables anyway). */
1754 if (MTAG_P (var) && TREE_CODE (var) != STRUCT_FIELD_TAG)
1755 continue;
1757 /* Remove the ADDRESSABLE flag from every addressable variable whose
1758 address is not needed anymore. This is caused by the propagation
1759 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1760 removal of dead pointer assignments done by the early scalar
1761 cleanup passes. */
1762 if (TREE_ADDRESSABLE (var))
1764 if (!bitmap_bit_p (addressable_vars, DECL_UID (var))
1765 && TREE_CODE (var) != RESULT_DECL
1766 && !is_global_var (var))
1768 bool okay_to_mark = true;
1770 /* Since VAR is now a regular GIMPLE register, we will need
1771 to rename VAR into SSA afterwards. */
1772 mark_sym_for_renaming (var);
1774 /* If VAR can have sub-variables, and any of its
1775 sub-variables has its address taken, then we cannot
1776 remove the addressable flag from VAR. */
1777 if (var_can_have_subvars (var)
1778 && (svars = get_subvars_for_var (var)))
1780 subvar_t sv;
1782 for (sv = svars; sv; sv = sv->next)
1784 if (bitmap_bit_p (addressable_vars, DECL_UID (sv->var)))
1785 okay_to_mark = false;
1786 mark_sym_for_renaming (sv->var);
1790 /* The address of VAR is not needed, remove the
1791 addressable bit, so that it can be optimized as a
1792 regular variable. */
1793 if (okay_to_mark)
1794 mark_non_addressable (var);
1798 /* Global variables and addressable locals may be aliased. Create an
1799 entry in ADDRESSABLE_VARS for VAR. */
1800 if (may_be_aliased (var)
1801 && (!var_can_have_subvars (var)
1802 || get_subvars_for_var (var) == NULL))
1804 create_alias_map_for (var, ai);
1805 mark_sym_for_renaming (var);
1808 /* Add pointer variables that have been dereferenced to the POINTERS
1809 array and create a symbol memory tag for them. */
1810 if (POINTER_TYPE_P (TREE_TYPE (var)))
1812 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var))
1813 || bitmap_bit_p (ai->dereferenced_ptrs_load, DECL_UID (var))))
1815 tree tag;
1816 var_ann_t t_ann;
1818 /* If pointer VAR still doesn't have a memory tag
1819 associated with it, create it now or re-use an
1820 existing one. */
1821 tag = get_tmt_for (var, ai);
1822 t_ann = var_ann (tag);
1824 /* The symbol tag will need to be renamed into SSA
1825 afterwards. Note that we cannot do this inside
1826 get_tmt_for because aliasing may run multiple times
1827 and we only create symbol tags the first time. */
1828 mark_sym_for_renaming (tag);
1830 /* Similarly, if pointer VAR used to have another type
1831 tag, we will need to process it in the renamer to
1832 remove the stale virtual operands. */
1833 if (v_ann->symbol_mem_tag)
1834 mark_sym_for_renaming (v_ann->symbol_mem_tag);
1836 /* Associate the tag with pointer VAR. */
1837 v_ann->symbol_mem_tag = tag;
1839 /* If pointer VAR has been used in a store operation,
1840 then its memory tag must be marked as written-to. */
1841 if (bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var)))
1842 bitmap_set_bit (ai->written_vars, DECL_UID (tag));
1844 /* All the dereferences of pointer VAR count as
1845 references of TAG. Since TAG can be associated with
1846 several pointers, add the dereferences of VAR to the
1847 TAG. */
1848 NUM_REFERENCES_SET (t_ann,
1849 NUM_REFERENCES (t_ann)
1850 + NUM_REFERENCES (v_ann));
1852 else
1854 /* The pointer has not been dereferenced. If it had a
1855 symbol memory tag, remove it and mark the old tag for
1856 renaming to remove it out of the IL. */
1857 var_ann_t ann = var_ann (var);
1858 tree tag = ann->symbol_mem_tag;
1859 if (tag)
1861 mark_sym_for_renaming (tag);
1862 ann->symbol_mem_tag = NULL_TREE;
1867 VEC_free (tree, heap, varvec);
1871 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1872 every call site, we need to emit V_MAY_DEF expressions to represent the
1873 clobbering effects of the call for variables whose address escapes the
1874 current function.
1876 One approach is to group all call-clobbered variables into a single
1877 representative that is used as an alias of every call-clobbered variable
1878 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1879 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1881 The second approach is to emit a clobbering V_MAY_DEF for every
1882 call-clobbered variable at call sites. This is the preferred way in terms
1883 of optimization opportunities but it may create too many V_MAY_DEF operands
1884 if there are many call clobbered variables and function calls in the
1885 function.
1887 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1888 function calls found by the number of call-clobbered variables. If that
1889 product is beyond a certain threshold, as determined by the parameterized
1890 values shown below, we use .GLOBAL_VAR.
1892 FIXME. This heuristic should be improved. One idea is to use several
1893 .GLOBAL_VARs of different types instead of a single one. The thresholds
1894 have been derived from a typical bootstrap cycle, including all target
1895 libraries. Compile times were found increase by ~1% compared to using
1896 .GLOBAL_VAR. */
1898 static void
1899 maybe_create_global_var (struct alias_info *ai)
1901 unsigned i, n_clobbered;
1902 bitmap_iterator bi;
1904 /* No need to create it, if we have one already. */
1905 if (global_var == NULL_TREE)
1907 /* Count all the call-clobbered variables. */
1908 n_clobbered = 0;
1909 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1911 n_clobbered++;
1914 /* If the number of virtual operands that would be needed to
1915 model all the call-clobbered variables is larger than
1916 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1918 Also create .GLOBAL_VAR if there are no call-clobbered
1919 variables and the program contains a mixture of pure/const
1920 and regular function calls. This is to avoid the problem
1921 described in PR 20115:
1923 int X;
1924 int func_pure (void) { return X; }
1925 int func_non_pure (int a) { X += a; }
1926 int foo ()
1928 int a = func_pure ();
1929 func_non_pure (a);
1930 a = func_pure ();
1931 return a;
1934 Since foo() has no call-clobbered variables, there is
1935 no relationship between the calls to func_pure and
1936 func_non_pure. Since func_pure has no side-effects, value
1937 numbering optimizations elide the second call to func_pure.
1938 So, if we have some pure/const and some regular calls in the
1939 program we create .GLOBAL_VAR to avoid missing these
1940 relations. */
1941 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1942 || (n_clobbered == 0
1943 && ai->num_calls_found > 0
1944 && ai->num_pure_const_calls_found > 0
1945 && ai->num_calls_found > ai->num_pure_const_calls_found))
1946 create_global_var ();
1949 /* Mark all call-clobbered symbols for renaming. Since the initial
1950 rewrite into SSA ignored all call sites, we may need to rename
1951 .GLOBAL_VAR and the call-clobbered variables. */
1952 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1954 tree var = referenced_var (i);
1956 /* If the function has calls to clobbering functions and
1957 .GLOBAL_VAR has been created, make it an alias for all
1958 call-clobbered variables. */
1959 if (global_var && var != global_var)
1961 add_may_alias (var, global_var);
1962 gcc_assert (!get_subvars_for_var (var));
1965 mark_sym_for_renaming (var);
1970 /* Return TRUE if pointer PTR may point to variable VAR.
1972 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1973 This is needed because when checking for type conflicts we are
1974 interested in the alias set of the memory location pointed-to by
1975 PTR. The alias set of PTR itself is irrelevant.
1977 VAR_ALIAS_SET is the alias set for VAR. */
1979 static bool
1980 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1981 tree var, HOST_WIDE_INT var_alias_set,
1982 bool alias_set_only)
1984 tree mem;
1986 alias_stats.alias_queries++;
1987 alias_stats.simple_queries++;
1989 /* By convention, a variable cannot alias itself. */
1990 mem = var_ann (ptr)->symbol_mem_tag;
1991 if (mem == var)
1993 alias_stats.alias_noalias++;
1994 alias_stats.simple_resolved++;
1995 return false;
1998 /* If -fargument-noalias-global is > 2, pointer arguments may
1999 not point to anything else. */
2000 if (flag_argument_noalias > 2 && TREE_CODE (ptr) == PARM_DECL)
2002 alias_stats.alias_noalias++;
2003 alias_stats.simple_resolved++;
2004 return false;
2007 /* If -fargument-noalias-global is > 1, pointer arguments may
2008 not point to global variables. */
2009 if (flag_argument_noalias > 1 && is_global_var (var)
2010 && TREE_CODE (ptr) == PARM_DECL)
2012 alias_stats.alias_noalias++;
2013 alias_stats.simple_resolved++;
2014 return false;
2017 /* If either MEM or VAR is a read-only global and the other one
2018 isn't, then PTR cannot point to VAR. */
2019 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
2020 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
2022 alias_stats.alias_noalias++;
2023 alias_stats.simple_resolved++;
2024 return false;
2027 gcc_assert (TREE_CODE (mem) == SYMBOL_MEMORY_TAG);
2029 alias_stats.tbaa_queries++;
2031 /* If the alias sets don't conflict then MEM cannot alias VAR. */
2032 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
2034 alias_stats.alias_noalias++;
2035 alias_stats.tbaa_resolved++;
2036 return false;
2039 /* If var is a record or union type, ptr cannot point into var
2040 unless there is some operation explicit address operation in the
2041 program that can reference a field of the ptr's dereferenced
2042 type. This also assumes that the types of both var and ptr are
2043 contained within the compilation unit, and that there is no fancy
2044 addressing arithmetic associated with any of the types
2045 involved. */
2047 if ((mem_alias_set != 0) && (var_alias_set != 0))
2049 tree ptr_type = TREE_TYPE (ptr);
2050 tree var_type = TREE_TYPE (var);
2052 /* The star count is -1 if the type at the end of the pointer_to
2053 chain is not a record or union type. */
2054 if ((!alias_set_only) &&
2055 ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
2057 int ptr_star_count = 0;
2059 /* Ipa_type_escape_star_count_of_interesting_type is a little to
2060 restrictive for the pointer type, need to allow pointers to
2061 primitive types as long as those types cannot be pointers
2062 to everything. */
2063 while (POINTER_TYPE_P (ptr_type))
2064 /* Strip the *'s off. */
2066 ptr_type = TREE_TYPE (ptr_type);
2067 ptr_star_count++;
2070 /* There does not appear to be a better test to see if the
2071 pointer type was one of the pointer to everything
2072 types. */
2074 if (ptr_star_count > 0)
2076 alias_stats.structnoaddress_queries++;
2077 if (ipa_type_escape_field_does_not_clobber_p (var_type,
2078 TREE_TYPE (ptr)))
2080 alias_stats.structnoaddress_resolved++;
2081 alias_stats.alias_noalias++;
2082 return false;
2085 else if (ptr_star_count == 0)
2087 /* If ptr_type was not really a pointer to type, it cannot
2088 alias. */
2089 alias_stats.structnoaddress_queries++;
2090 alias_stats.structnoaddress_resolved++;
2091 alias_stats.alias_noalias++;
2092 return false;
2097 alias_stats.alias_mayalias++;
2098 return true;
2102 /* Add ALIAS to the set of variables that may alias VAR. */
2104 static void
2105 add_may_alias (tree var, tree alias)
2107 size_t i;
2108 var_ann_t v_ann = get_var_ann (var);
2109 var_ann_t a_ann = get_var_ann (alias);
2110 tree al;
2112 /* Don't allow self-referential aliases. */
2113 gcc_assert (var != alias);
2115 /* ALIAS must be addressable if it's being added to an alias set. */
2116 #if 1
2117 TREE_ADDRESSABLE (alias) = 1;
2118 #else
2119 gcc_assert (may_be_aliased (alias));
2120 #endif
2122 if (v_ann->may_aliases == NULL)
2123 v_ann->may_aliases = VEC_alloc (tree, gc, 2);
2125 /* Avoid adding duplicates. */
2126 for (i = 0; VEC_iterate (tree, v_ann->may_aliases, i, al); i++)
2127 if (alias == al)
2128 return;
2130 VEC_safe_push (tree, gc, v_ann->may_aliases, alias);
2131 a_ann->is_aliased = 1;
2135 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
2137 static void
2138 replace_may_alias (tree var, size_t i, tree new_alias)
2140 var_ann_t v_ann = var_ann (var);
2141 VEC_replace (tree, v_ann->may_aliases, i, new_alias);
2145 /* Mark pointer PTR as pointing to an arbitrary memory location. */
2147 static void
2148 set_pt_anything (tree ptr)
2150 struct ptr_info_def *pi = get_ptr_info (ptr);
2152 pi->pt_anything = 1;
2153 pi->pt_vars = NULL;
2155 /* The pointer used to have a name tag, but we now found it pointing
2156 to an arbitrary location. The name tag needs to be renamed and
2157 disassociated from PTR. */
2158 if (pi->name_mem_tag)
2160 mark_sym_for_renaming (pi->name_mem_tag);
2161 pi->name_mem_tag = NULL_TREE;
2166 /* Return true if STMT is an "escape" site from the current function. Escape
2167 sites those statements which might expose the address of a variable
2168 outside the current function. STMT is an escape site iff:
2170 1- STMT is a function call, or
2171 2- STMT is an __asm__ expression, or
2172 3- STMT is an assignment to a non-local variable, or
2173 4- STMT is a return statement.
2175 Return the type of escape site found, if we found one, or NO_ESCAPE
2176 if none. */
2178 enum escape_type
2179 is_escape_site (tree stmt)
2181 tree call = get_call_expr_in (stmt);
2182 if (call != NULL_TREE)
2184 if (!TREE_SIDE_EFFECTS (call))
2185 return ESCAPE_TO_PURE_CONST;
2187 return ESCAPE_TO_CALL;
2189 else if (TREE_CODE (stmt) == ASM_EXPR)
2190 return ESCAPE_TO_ASM;
2191 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2193 tree lhs = TREE_OPERAND (stmt, 0);
2195 /* Get to the base of _REF nodes. */
2196 if (TREE_CODE (lhs) != SSA_NAME)
2197 lhs = get_base_address (lhs);
2199 /* If we couldn't recognize the LHS of the assignment, assume that it
2200 is a non-local store. */
2201 if (lhs == NULL_TREE)
2202 return ESCAPE_UNKNOWN;
2204 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2205 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2206 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2208 tree from = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (stmt, 1), 0));
2209 tree to = TREE_TYPE (TREE_OPERAND (stmt, 1));
2211 /* If the RHS is a conversion between a pointer and an integer, the
2212 pointer escapes since we can't track the integer. */
2213 if (POINTER_TYPE_P (from) && !POINTER_TYPE_P (to))
2214 return ESCAPE_BAD_CAST;
2216 /* Same if the RHS is a conversion between a regular pointer and a
2217 ref-all pointer since we can't track the SMT of the former. */
2218 if (POINTER_TYPE_P (from) && !TYPE_REF_CAN_ALIAS_ALL (from)
2219 && POINTER_TYPE_P (to) && TYPE_REF_CAN_ALIAS_ALL (to))
2220 return ESCAPE_BAD_CAST;
2223 /* If the LHS is an SSA name, it can't possibly represent a non-local
2224 memory store. */
2225 if (TREE_CODE (lhs) == SSA_NAME)
2226 return NO_ESCAPE;
2228 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2229 local variables we cannot be sure if it will escape, because we
2230 don't have information about objects not in SSA form. Need to
2231 implement something along the lines of
2233 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2234 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2235 Conference on Object-Oriented Programming Systems, Languages, and
2236 Applications (OOPSLA), pp. 1-19, 1999. */
2237 return ESCAPE_STORED_IN_GLOBAL;
2239 else if (TREE_CODE (stmt) == RETURN_EXPR)
2240 return ESCAPE_TO_RETURN;
2242 return NO_ESCAPE;
2245 /* Create a new memory tag of type TYPE.
2246 Does NOT push it into the current binding. */
2248 static tree
2249 create_tag_raw (enum tree_code code, tree type, const char *prefix)
2251 tree tmp_var;
2252 tree new_type;
2254 /* Make the type of the variable writable. */
2255 new_type = build_type_variant (type, 0, 0);
2256 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
2258 tmp_var = build_decl (code, create_tmp_var_name (prefix),
2259 type);
2260 /* Make the variable writable. */
2261 TREE_READONLY (tmp_var) = 0;
2263 /* It doesn't start out global. */
2264 MTAG_GLOBAL (tmp_var) = 0;
2265 TREE_STATIC (tmp_var) = 0;
2266 TREE_USED (tmp_var) = 1;
2268 return tmp_var;
2271 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2272 is considered to represent all the pointers whose pointed-to types are
2273 in the same alias set class. Otherwise, the tag represents a single
2274 SSA_NAME pointer variable. */
2276 static tree
2277 create_memory_tag (tree type, bool is_type_tag)
2279 var_ann_t ann;
2280 tree tag = create_tag_raw (is_type_tag ? SYMBOL_MEMORY_TAG : NAME_MEMORY_TAG,
2281 type, (is_type_tag) ? "SMT" : "NMT");
2283 /* By default, memory tags are local variables. Alias analysis will
2284 determine whether they should be considered globals. */
2285 DECL_CONTEXT (tag) = current_function_decl;
2287 /* Memory tags are by definition addressable. */
2288 TREE_ADDRESSABLE (tag) = 1;
2290 ann = get_var_ann (tag);
2291 ann->symbol_mem_tag = NULL_TREE;
2293 /* Add the tag to the symbol table. */
2294 add_referenced_var (tag);
2296 return tag;
2300 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2301 This is used if P_i has been found to point to a specific set of
2302 variables or to a non-aliased memory location like the address returned
2303 by malloc functions. */
2305 static tree
2306 get_nmt_for (tree ptr)
2308 struct ptr_info_def *pi = get_ptr_info (ptr);
2309 tree tag = pi->name_mem_tag;
2311 if (tag == NULL_TREE)
2312 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2313 return tag;
2317 /* Return the symbol memory tag associated to pointer PTR. A memory
2318 tag is an artificial variable that represents the memory location
2319 pointed-to by PTR. It is used to model the effects of pointer
2320 de-references on addressable variables.
2322 AI points to the data gathered during alias analysis. This
2323 function populates the array AI->POINTERS. */
2325 static tree
2326 get_tmt_for (tree ptr, struct alias_info *ai)
2328 size_t i;
2329 tree tag;
2330 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2331 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2333 /* We use a unique memory tag for all the ref-all pointers. */
2334 if (PTR_IS_REF_ALL (ptr))
2336 if (!ai->ref_all_symbol_mem_tag)
2337 ai->ref_all_symbol_mem_tag = create_memory_tag (void_type_node, true);
2338 return ai->ref_all_symbol_mem_tag;
2341 /* To avoid creating unnecessary memory tags, only create one memory tag
2342 per alias set class. Note that it may be tempting to group
2343 memory tags based on conflicting alias sets instead of
2344 equivalence. That would be wrong because alias sets are not
2345 necessarily transitive (as demonstrated by the libstdc++ test
2346 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2347 such that conflicts (A, B) == true and conflicts (A, C) == true,
2348 it does not necessarily follow that conflicts (B, C) == true. */
2349 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2351 struct alias_map_d *curr = ai->pointers[i];
2352 tree curr_tag = var_ann (curr->var)->symbol_mem_tag;
2353 if (tag_set == curr->set)
2355 tag = curr_tag;
2356 break;
2360 /* If VAR cannot alias with any of the existing memory tags, create a new
2361 tag for PTR and add it to the POINTERS array. */
2362 if (tag == NULL_TREE)
2364 struct alias_map_d *alias_map;
2366 /* If PTR did not have a symbol tag already, create a new SMT.*
2367 artificial variable representing the memory location
2368 pointed-to by PTR. */
2369 if (var_ann (ptr)->symbol_mem_tag == NULL_TREE)
2370 tag = create_memory_tag (tag_type, true);
2371 else
2372 tag = var_ann (ptr)->symbol_mem_tag;
2374 /* Add PTR to the POINTERS array. Note that we are not interested in
2375 PTR's alias set. Instead, we cache the alias set for the memory that
2376 PTR points to. */
2377 alias_map = XCNEW (struct alias_map_d);
2378 alias_map->var = ptr;
2379 alias_map->set = tag_set;
2380 ai->pointers[ai->num_pointers++] = alias_map;
2383 /* If the pointed-to type is volatile, so is the tag. */
2384 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2386 /* Make sure that the symbol tag has the same alias set as the
2387 pointed-to type. */
2388 gcc_assert (tag_set == get_alias_set (tag));
2390 return tag;
2394 /* Create GLOBAL_VAR, an artificial global variable to act as a
2395 representative of all the variables that may be clobbered by function
2396 calls. */
2398 static void
2399 create_global_var (void)
2401 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2402 void_type_node);
2403 DECL_ARTIFICIAL (global_var) = 1;
2404 TREE_READONLY (global_var) = 0;
2405 DECL_EXTERNAL (global_var) = 1;
2406 TREE_STATIC (global_var) = 1;
2407 TREE_USED (global_var) = 1;
2408 DECL_CONTEXT (global_var) = NULL_TREE;
2409 TREE_THIS_VOLATILE (global_var) = 0;
2410 TREE_ADDRESSABLE (global_var) = 0;
2412 create_var_ann (global_var);
2413 mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
2414 add_referenced_var (global_var);
2415 mark_sym_for_renaming (global_var);
2419 /* Dump alias statistics on FILE. */
2421 static void
2422 dump_alias_stats (FILE *file)
2424 const char *funcname
2425 = lang_hooks.decl_printable_name (current_function_decl, 2);
2426 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2427 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2428 fprintf (file, "Total alias mayalias results:\t%u\n",
2429 alias_stats.alias_mayalias);
2430 fprintf (file, "Total alias noalias results:\t%u\n",
2431 alias_stats.alias_noalias);
2432 fprintf (file, "Total simple queries:\t%u\n",
2433 alias_stats.simple_queries);
2434 fprintf (file, "Total simple resolved:\t%u\n",
2435 alias_stats.simple_resolved);
2436 fprintf (file, "Total TBAA queries:\t%u\n",
2437 alias_stats.tbaa_queries);
2438 fprintf (file, "Total TBAA resolved:\t%u\n",
2439 alias_stats.tbaa_resolved);
2440 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
2441 alias_stats.structnoaddress_queries);
2442 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
2443 alias_stats.structnoaddress_resolved);
2447 /* Dump alias information on FILE. */
2449 void
2450 dump_alias_info (FILE *file)
2452 size_t i;
2453 const char *funcname
2454 = lang_hooks.decl_printable_name (current_function_decl, 2);
2455 referenced_var_iterator rvi;
2456 tree var;
2458 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2460 fprintf (file, "Aliased symbols\n\n");
2462 FOR_EACH_REFERENCED_VAR (var, rvi)
2464 if (may_be_aliased (var))
2465 dump_variable (file, var);
2468 fprintf (file, "\nDereferenced pointers\n\n");
2470 FOR_EACH_REFERENCED_VAR (var, rvi)
2472 var_ann_t ann = var_ann (var);
2473 if (ann->symbol_mem_tag)
2474 dump_variable (file, var);
2477 fprintf (file, "\nSymbol memory tags\n\n");
2479 FOR_EACH_REFERENCED_VAR (var, rvi)
2481 if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
2482 dump_variable (file, var);
2485 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2487 fprintf (file, "SSA_NAME pointers\n\n");
2488 for (i = 1; i < num_ssa_names; i++)
2490 tree ptr = ssa_name (i);
2491 struct ptr_info_def *pi;
2493 if (ptr == NULL_TREE)
2494 continue;
2496 pi = SSA_NAME_PTR_INFO (ptr);
2497 if (!SSA_NAME_IN_FREE_LIST (ptr)
2498 && pi
2499 && pi->name_mem_tag)
2500 dump_points_to_info_for (file, ptr);
2503 fprintf (file, "\nName memory tags\n\n");
2505 FOR_EACH_REFERENCED_VAR (var, rvi)
2507 if (TREE_CODE (var) == NAME_MEMORY_TAG)
2508 dump_variable (file, var);
2511 fprintf (file, "\n");
2515 /* Dump alias information on stderr. */
2517 void
2518 debug_alias_info (void)
2520 dump_alias_info (stderr);
2524 /* Return the alias information associated with pointer T. It creates a
2525 new instance if none existed. */
2527 struct ptr_info_def *
2528 get_ptr_info (tree t)
2530 struct ptr_info_def *pi;
2532 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2534 pi = SSA_NAME_PTR_INFO (t);
2535 if (pi == NULL)
2537 pi = GGC_CNEW (struct ptr_info_def);
2538 SSA_NAME_PTR_INFO (t) = pi;
2541 return pi;
2545 /* Dump points-to information for SSA_NAME PTR into FILE. */
2547 void
2548 dump_points_to_info_for (FILE *file, tree ptr)
2550 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2552 print_generic_expr (file, ptr, dump_flags);
2554 if (pi)
2556 if (pi->name_mem_tag)
2558 fprintf (file, ", name memory tag: ");
2559 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2562 if (pi->is_dereferenced)
2563 fprintf (file, ", is dereferenced");
2565 if (pi->value_escapes_p)
2566 fprintf (file, ", its value escapes");
2568 if (pi->pt_anything)
2569 fprintf (file, ", points-to anything");
2571 if (pi->pt_null)
2572 fprintf (file, ", points-to NULL");
2574 if (pi->pt_vars)
2576 unsigned ix;
2577 bitmap_iterator bi;
2579 fprintf (file, ", points-to vars: { ");
2580 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2582 print_generic_expr (file, referenced_var (ix), dump_flags);
2583 fprintf (file, " ");
2585 fprintf (file, "}");
2589 fprintf (file, "\n");
2593 /* Dump points-to information for VAR into stderr. */
2595 void
2596 debug_points_to_info_for (tree var)
2598 dump_points_to_info_for (stderr, var);
2602 /* Dump points-to information into FILE. NOTE: This function is slow, as
2603 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2605 void
2606 dump_points_to_info (FILE *file)
2608 basic_block bb;
2609 block_stmt_iterator si;
2610 ssa_op_iter iter;
2611 const char *fname =
2612 lang_hooks.decl_printable_name (current_function_decl, 2);
2613 referenced_var_iterator rvi;
2614 tree var;
2616 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2618 /* First dump points-to information for the default definitions of
2619 pointer variables. This is necessary because default definitions are
2620 not part of the code. */
2621 FOR_EACH_REFERENCED_VAR (var, rvi)
2623 if (POINTER_TYPE_P (TREE_TYPE (var)))
2625 tree def = default_def (var);
2626 if (def)
2627 dump_points_to_info_for (file, def);
2631 /* Dump points-to information for every pointer defined in the program. */
2632 FOR_EACH_BB (bb)
2634 tree phi;
2636 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2638 tree ptr = PHI_RESULT (phi);
2639 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2640 dump_points_to_info_for (file, ptr);
2643 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2645 tree stmt = bsi_stmt (si);
2646 tree def;
2647 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2648 if (POINTER_TYPE_P (TREE_TYPE (def)))
2649 dump_points_to_info_for (file, def);
2653 fprintf (file, "\n");
2657 /* Dump points-to info pointed to by PTO into STDERR. */
2659 void
2660 debug_points_to_info (void)
2662 dump_points_to_info (stderr);
2665 /* Dump to FILE the list of variables that may be aliasing VAR. */
2667 void
2668 dump_may_aliases_for (FILE *file, tree var)
2670 VEC(tree, gc) *aliases;
2672 if (TREE_CODE (var) == SSA_NAME)
2673 var = SSA_NAME_VAR (var);
2675 aliases = var_ann (var)->may_aliases;
2676 if (aliases)
2678 size_t i;
2679 tree al;
2680 fprintf (file, "{ ");
2681 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2683 print_generic_expr (file, al, dump_flags);
2684 fprintf (file, " ");
2686 fprintf (file, "}");
2691 /* Dump to stderr the list of variables that may be aliasing VAR. */
2693 void
2694 debug_may_aliases_for (tree var)
2696 dump_may_aliases_for (stderr, var);
2699 /* Return true if VAR may be aliased. */
2701 bool
2702 may_be_aliased (tree var)
2704 /* Obviously. */
2705 if (TREE_ADDRESSABLE (var))
2706 return true;
2708 /* Globally visible variables can have their addresses taken by other
2709 translation units. */
2711 if (MTAG_P (var)
2712 && (MTAG_GLOBAL (var) || TREE_PUBLIC (var)))
2713 return true;
2714 else if (!MTAG_P (var)
2715 && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
2716 return true;
2718 /* Automatic variables can't have their addresses escape any other way.
2719 This must be after the check for global variables, as extern declarations
2720 do not have TREE_STATIC set. */
2721 if (!TREE_STATIC (var))
2722 return false;
2724 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2725 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2726 we can only be sure the variable isn't addressable if it's local to the
2727 current function. */
2728 if (flag_unit_at_a_time)
2729 return false;
2730 if (decl_function_context (var) == current_function_decl)
2731 return false;
2733 return true;
2737 /* Given two symbols return TRUE if one is in the alias set of the other. */
2738 bool
2739 is_aliased_with (tree tag, tree sym)
2741 size_t i;
2742 VEC(tree,gc) *aliases;
2743 tree al;
2745 if (var_ann (sym)->is_aliased)
2747 aliases = var_ann (tag)->may_aliases;
2749 if (aliases == NULL)
2750 return false;
2752 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2753 if (al == sym)
2754 return true;
2756 else
2758 aliases = var_ann (sym)->may_aliases;
2760 if (aliases == NULL)
2761 return false;
2763 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2764 if (al == tag)
2765 return true;
2768 return false;
2771 /* The following is based on code in add_stmt_operand to ensure that the
2772 same defs/uses/vdefs/vuses will be found after replacing a reference
2773 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
2774 is the address of var. Return a memtag for the ptr, after adding the
2775 proper may_aliases to it (which are the aliases of var, if it has any,
2776 or var itself). */
2778 static tree
2779 add_may_alias_for_new_tag (tree tag, tree var)
2781 var_ann_t v_ann = var_ann (var);
2782 VEC(tree, gc) *aliases = v_ann->may_aliases;
2784 /* Case 1: |aliases| == 1 */
2785 if ((aliases != NULL)
2786 && (VEC_length (tree, aliases) == 1))
2788 tree ali = VEC_index (tree, aliases, 0);
2790 if (TREE_CODE (ali) == SYMBOL_MEMORY_TAG)
2791 return ali;
2794 /* Case 2: |aliases| == 0 */
2795 if (aliases == NULL)
2796 add_may_alias (tag, var);
2797 else
2799 /* Case 3: |aliases| > 1 */
2800 unsigned i;
2801 tree al;
2803 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2804 add_may_alias (tag, al);
2807 return tag;
2810 /* Create a new symbol tag for PTR. Construct the may-alias list of this type
2811 tag so that it has the aliasing of VAR, or of the relevant subvars of VAR
2812 according to the location accessed by EXPR.
2814 Note, the set of aliases represented by the new symbol tag are not marked
2815 for renaming. */
2817 void
2818 new_type_alias (tree ptr, tree var, tree expr)
2820 var_ann_t p_ann = var_ann (ptr);
2821 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2822 tree tag;
2823 subvar_t svars;
2824 tree ali = NULL_TREE;
2825 HOST_WIDE_INT offset, size, maxsize;
2826 tree ref;
2827 VEC (tree, heap) *overlaps = NULL;
2828 subvar_t sv;
2829 unsigned int len;
2831 gcc_assert (p_ann->symbol_mem_tag == NULL_TREE);
2832 gcc_assert (!MTAG_P (var));
2834 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
2835 gcc_assert (ref);
2837 tag = create_memory_tag (tag_type, true);
2838 p_ann->symbol_mem_tag = tag;
2840 /* Add VAR to the may-alias set of PTR's new symbol tag. If VAR has
2841 subvars, add the subvars to the tag instead of the actual var. */
2842 if (var_can_have_subvars (ref)
2843 && (svars = get_subvars_for_var (ref)))
2845 for (sv = svars; sv; sv = sv->next)
2847 bool exact;
2849 if (overlap_subvar (offset, maxsize, sv->var, &exact))
2850 VEC_safe_push (tree, heap, overlaps, sv->var);
2852 gcc_assert (overlaps != NULL);
2854 else if (var_can_have_subvars (var)
2855 && (svars = get_subvars_for_var (var)))
2857 /* If the REF is not a direct access to VAR (e.g., it is a dereference
2858 of a pointer), we should scan the virtual operands of REF the same
2859 way as tree-ssa-operands do. At the moment, this is somewhat
2860 difficult, so we just give up and add all the subvars of VAR.
2861 On mem-ssa branch, the scanning for virtual operands have been
2862 split from the rest of tree-ssa-operands, so it should be much
2863 easier to fix this problem correctly once mem-ssa is merged. */
2864 for (sv = svars; sv; sv = sv->next)
2865 VEC_safe_push (tree, heap, overlaps, sv->var);
2867 gcc_assert (overlaps != NULL);
2869 else
2870 ali = add_may_alias_for_new_tag (tag, var);
2872 len = VEC_length (tree, overlaps);
2873 if (len > 0)
2875 if (dump_file && (dump_flags & TDF_DETAILS))
2876 fprintf (dump_file, "\nnumber of overlapping subvars = %u\n", len);
2878 if (len == 1)
2879 ali = add_may_alias_for_new_tag (tag, VEC_index (tree, overlaps, 0));
2880 else if (len > 1)
2882 unsigned int k;
2883 tree sv_var;
2885 for (k = 0; VEC_iterate (tree, overlaps, k, sv_var); k++)
2887 ali = add_may_alias_for_new_tag (tag, sv_var);
2889 if (ali != tag)
2891 /* Can happen only if 'Case 1' of add_may_alias_for_new_tag
2892 took place. Since more than one svar was found, we add
2893 'ali' as one of the may_aliases of the new tag. */
2894 add_may_alias (tag, ali);
2895 ali = tag;
2899 VEC_free (tree, heap, overlaps);
2902 p_ann->symbol_mem_tag = ali;
2903 TREE_READONLY (tag) = TREE_READONLY (var);
2904 MTAG_GLOBAL (tag) = is_global_var (var);
2907 /* This represents the used range of a variable. */
2909 typedef struct used_part
2911 HOST_WIDE_INT minused;
2912 HOST_WIDE_INT maxused;
2913 /* True if we have an explicit use/def of some portion of this variable,
2914 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2915 bool explicit_uses;
2916 /* True if we have an implicit use/def of some portion of this
2917 variable. Implicit uses occur when we can't tell what part we
2918 are referencing, and have to make conservative assumptions. */
2919 bool implicit_uses;
2920 /* True if the structure is only written to or taken its address. */
2921 bool write_only;
2922 } *used_part_t;
2924 /* An array of used_part structures, indexed by variable uid. */
2926 static htab_t used_portions;
2928 struct used_part_map
2930 unsigned int uid;
2931 used_part_t to;
2934 /* Return true if the uid in the two used part maps are equal. */
2936 static int
2937 used_part_map_eq (const void *va, const void *vb)
2939 const struct used_part_map *a = (const struct used_part_map *) va;
2940 const struct used_part_map *b = (const struct used_part_map *) vb;
2941 return (a->uid == b->uid);
2944 /* Hash a from uid in a used_part_map. */
2946 static unsigned int
2947 used_part_map_hash (const void *item)
2949 return ((const struct used_part_map *)item)->uid;
2952 /* Free a used part map element. */
2954 static void
2955 free_used_part_map (void *item)
2957 free (((struct used_part_map *)item)->to);
2958 free (item);
2961 /* Lookup a used_part structure for a UID. */
2963 static used_part_t
2964 up_lookup (unsigned int uid)
2966 struct used_part_map *h, in;
2967 in.uid = uid;
2968 h = (struct used_part_map *) htab_find_with_hash (used_portions, &in, uid);
2969 if (!h)
2970 return NULL;
2971 return h->to;
2974 /* Insert the pair UID, TO into the used part hashtable. */
2976 static void
2977 up_insert (unsigned int uid, used_part_t to)
2979 struct used_part_map *h;
2980 void **loc;
2982 h = XNEW (struct used_part_map);
2983 h->uid = uid;
2984 h->to = to;
2985 loc = htab_find_slot_with_hash (used_portions, h,
2986 uid, INSERT);
2987 if (*loc != NULL)
2988 free (*loc);
2989 *(struct used_part_map **) loc = h;
2993 /* Given a variable uid, UID, get or create the entry in the used portions
2994 table for the variable. */
2996 static used_part_t
2997 get_or_create_used_part_for (size_t uid)
2999 used_part_t up;
3000 if ((up = up_lookup (uid)) == NULL)
3002 up = XCNEW (struct used_part);
3003 up->minused = INT_MAX;
3004 up->maxused = 0;
3005 up->explicit_uses = false;
3006 up->implicit_uses = false;
3007 up->write_only = true;
3010 return up;
3014 /* Create and return a structure sub-variable for field type FIELD at
3015 offset OFFSET, with size SIZE, of variable VAR. */
3017 static tree
3018 create_sft (tree var, tree field, unsigned HOST_WIDE_INT offset,
3019 unsigned HOST_WIDE_INT size)
3021 var_ann_t ann;
3022 tree subvar = create_tag_raw (STRUCT_FIELD_TAG, field, "SFT");
3024 /* We need to copy the various flags from VAR to SUBVAR, so that
3025 they are is_global_var iff the original variable was. */
3026 DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
3027 MTAG_GLOBAL (subvar) = DECL_EXTERNAL (var);
3028 TREE_PUBLIC (subvar) = TREE_PUBLIC (var);
3029 TREE_STATIC (subvar) = TREE_STATIC (var);
3030 TREE_READONLY (subvar) = TREE_READONLY (var);
3031 TREE_ADDRESSABLE (subvar) = TREE_ADDRESSABLE (var);
3033 /* Add the new variable to REFERENCED_VARS. */
3034 ann = get_var_ann (subvar);
3035 ann->symbol_mem_tag = NULL;
3036 add_referenced_var (subvar);
3037 SFT_PARENT_VAR (subvar) = var;
3038 SFT_OFFSET (subvar) = offset;
3039 SFT_SIZE (subvar) = size;
3040 return subvar;
3044 /* Given an aggregate VAR, create the subvariables that represent its
3045 fields. */
3047 static void
3048 create_overlap_variables_for (tree var)
3050 VEC(fieldoff_s,heap) *fieldstack = NULL;
3051 used_part_t up;
3052 size_t uid = DECL_UID (var);
3054 up = up_lookup (uid);
3055 if (!up
3056 || up->write_only)
3057 return;
3059 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL);
3060 if (VEC_length (fieldoff_s, fieldstack) != 0)
3062 subvar_t *subvars;
3063 fieldoff_s *fo;
3064 bool notokay = false;
3065 int fieldcount = 0;
3066 int i;
3067 HOST_WIDE_INT lastfooffset = -1;
3068 HOST_WIDE_INT lastfosize = -1;
3069 tree lastfotype = NULL_TREE;
3071 /* Not all fields have DECL_SIZE set, and those that don't, we don't
3072 know their size, and thus, can't handle.
3073 The same is true of fields with DECL_SIZE that is not an integer
3074 constant (such as variable sized fields).
3075 Fields with offsets which are not constant will have an offset < 0
3076 We *could* handle fields that are constant sized arrays, but
3077 currently don't. Doing so would require some extra changes to
3078 tree-ssa-operands.c. */
3080 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3082 if (!fo->size
3083 || TREE_CODE (fo->size) != INTEGER_CST
3084 || fo->offset < 0)
3086 notokay = true;
3087 break;
3089 fieldcount++;
3092 /* The current heuristic we use is as follows:
3093 If the variable has no used portions in this function, no
3094 structure vars are created for it.
3095 Otherwise,
3096 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
3097 we always create structure vars for them.
3098 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3099 some explicit uses, we create structure vars for them.
3100 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3101 no explicit uses, we do not create structure vars for them.
3104 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
3105 && !up->explicit_uses)
3107 if (dump_file && (dump_flags & TDF_DETAILS))
3109 fprintf (dump_file, "Variable ");
3110 print_generic_expr (dump_file, var, 0);
3111 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
3113 notokay = true;
3116 /* Bail out, if we can't create overlap variables. */
3117 if (notokay)
3119 VEC_free (fieldoff_s, heap, fieldstack);
3120 return;
3123 /* Otherwise, create the variables. */
3124 subvars = lookup_subvars_for_var (var);
3126 sort_fieldstack (fieldstack);
3128 for (i = VEC_length (fieldoff_s, fieldstack);
3129 VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
3131 subvar_t sv;
3132 HOST_WIDE_INT fosize;
3133 tree currfotype;
3135 fosize = TREE_INT_CST_LOW (fo->size);
3136 currfotype = fo->type;
3138 /* If this field isn't in the used portion,
3139 or it has the exact same offset and size as the last
3140 field, skip it. */
3142 if (((fo->offset <= up->minused
3143 && fo->offset + fosize <= up->minused)
3144 || fo->offset >= up->maxused)
3145 || (fo->offset == lastfooffset
3146 && fosize == lastfosize
3147 && currfotype == lastfotype))
3148 continue;
3149 sv = GGC_NEW (struct subvar);
3150 sv->next = *subvars;
3151 sv->var = create_sft (var, fo->type, fo->offset, fosize);
3153 if (dump_file)
3155 fprintf (dump_file, "structure field tag %s created for var %s",
3156 get_name (sv->var), get_name (var));
3157 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
3158 SFT_OFFSET (sv->var));
3159 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
3160 SFT_SIZE (sv->var));
3161 fprintf (dump_file, "\n");
3164 lastfotype = currfotype;
3165 lastfooffset = fo->offset;
3166 lastfosize = fosize;
3167 *subvars = sv;
3170 /* Once we have created subvars, the original is no longer call
3171 clobbered on its own. Its call clobbered status depends
3172 completely on the call clobbered status of the subvars.
3174 add_referenced_var in the above loop will take care of
3175 marking subvars of global variables as call clobbered for us
3176 to start, since they are global as well. */
3177 clear_call_clobbered (var);
3180 VEC_free (fieldoff_s, heap, fieldstack);
3184 /* Find the conservative answer to the question of what portions of what
3185 structures are used by this statement. We assume that if we have a
3186 component ref with a known size + offset, that we only need that part
3187 of the structure. For unknown cases, or cases where we do something
3188 to the whole structure, we assume we need to create fields for the
3189 entire structure. */
3191 static tree
3192 find_used_portions (tree *tp, int *walk_subtrees, void *lhs_p)
3194 switch (TREE_CODE (*tp))
3196 case MODIFY_EXPR:
3197 /* Recurse manually here to track whether the use is in the
3198 LHS of an assignment. */
3199 find_used_portions (&TREE_OPERAND (*tp, 0), walk_subtrees, tp);
3200 return find_used_portions (&TREE_OPERAND (*tp, 1), walk_subtrees, NULL);
3201 case REALPART_EXPR:
3202 case IMAGPART_EXPR:
3203 case COMPONENT_REF:
3204 case ARRAY_REF:
3206 HOST_WIDE_INT bitsize;
3207 HOST_WIDE_INT bitmaxsize;
3208 HOST_WIDE_INT bitpos;
3209 tree ref;
3210 ref = get_ref_base_and_extent (*tp, &bitpos, &bitsize, &bitmaxsize);
3211 if (DECL_P (ref)
3212 && var_can_have_subvars (ref)
3213 && bitmaxsize != -1)
3215 size_t uid = DECL_UID (ref);
3216 used_part_t up;
3218 up = get_or_create_used_part_for (uid);
3220 if (bitpos <= up->minused)
3221 up->minused = bitpos;
3222 if ((bitpos + bitmaxsize >= up->maxused))
3223 up->maxused = bitpos + bitmaxsize;
3225 if (bitsize == bitmaxsize)
3226 up->explicit_uses = true;
3227 else
3228 up->implicit_uses = true;
3229 if (!lhs_p)
3230 up->write_only = false;
3231 up_insert (uid, up);
3233 *walk_subtrees = 0;
3234 return NULL_TREE;
3237 break;
3238 /* This is here to make sure we mark the entire base variable as used
3239 when you take its address. Because our used portion analysis is
3240 simple, we aren't looking at casts or pointer arithmetic to see what
3241 happens when you take the address. */
3242 case ADDR_EXPR:
3244 tree var = get_base_address (TREE_OPERAND (*tp, 0));
3246 if (var
3247 && DECL_P (var)
3248 && DECL_SIZE (var)
3249 && var_can_have_subvars (var)
3250 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3252 used_part_t up;
3253 size_t uid = DECL_UID (var);
3255 up = get_or_create_used_part_for (uid);
3257 up->minused = 0;
3258 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3259 up->implicit_uses = true;
3260 if (!lhs_p)
3261 up->write_only = false;
3263 up_insert (uid, up);
3264 *walk_subtrees = 0;
3265 return NULL_TREE;
3268 break;
3269 case CALL_EXPR:
3271 tree *arg;
3272 for (arg = &TREE_OPERAND (*tp, 1); *arg; arg = &TREE_CHAIN (*arg))
3274 if (TREE_CODE (TREE_VALUE (*arg)) != ADDR_EXPR)
3275 find_used_portions (&TREE_VALUE (*arg), walk_subtrees, NULL);
3277 *walk_subtrees = 0;
3278 return NULL_TREE;
3280 case VAR_DECL:
3281 case PARM_DECL:
3282 case RESULT_DECL:
3284 tree var = *tp;
3285 if (DECL_SIZE (var)
3286 && var_can_have_subvars (var)
3287 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3289 used_part_t up;
3290 size_t uid = DECL_UID (var);
3292 up = get_or_create_used_part_for (uid);
3294 up->minused = 0;
3295 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3296 up->implicit_uses = true;
3298 up_insert (uid, up);
3299 *walk_subtrees = 0;
3300 return NULL_TREE;
3303 break;
3305 default:
3306 break;
3309 return NULL_TREE;
3312 /* Create structure field variables for structures used in this function. */
3314 static unsigned int
3315 create_structure_vars (void)
3317 basic_block bb;
3318 safe_referenced_var_iterator rvi;
3319 VEC (tree, heap) *varvec = NULL;
3320 tree var;
3322 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
3323 free_used_part_map);
3325 FOR_EACH_BB (bb)
3327 block_stmt_iterator bsi;
3328 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3330 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3331 find_used_portions,
3332 NULL);
3335 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, rvi)
3337 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3338 if (var
3339 && DECL_SIZE (var)
3340 && var_can_have_subvars (var)
3341 && !MTAG_P (var)
3342 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3343 create_overlap_variables_for (var);
3345 htab_delete (used_portions);
3346 VEC_free (tree, heap, varvec);
3347 return 0;
3350 static bool
3351 gate_structure_vars (void)
3353 return flag_tree_salias != 0;
3356 struct tree_opt_pass pass_create_structure_vars =
3358 "salias", /* name */
3359 gate_structure_vars, /* gate */
3360 create_structure_vars, /* execute */
3361 NULL, /* sub */
3362 NULL, /* next */
3363 0, /* static_pass_number */
3364 0, /* tv_id */
3365 PROP_cfg, /* properties_required */
3366 0, /* properties_provided */
3367 0, /* properties_destroyed */
3368 0, /* todo_flags_start */
3369 TODO_dump_func, /* todo_flags_finish */
3370 0 /* letter */
3373 /* Reset the DECL_CALL_CLOBBERED flags on our referenced vars. In
3374 theory, this only needs to be done for globals. */
3376 static unsigned int
3377 reset_cc_flags (void)
3379 tree var;
3380 referenced_var_iterator rvi;
3382 FOR_EACH_REFERENCED_VAR (var, rvi)
3383 DECL_CALL_CLOBBERED (var) = false;
3384 return 0;
3387 struct tree_opt_pass pass_reset_cc_flags =
3389 NULL, /* name */
3390 NULL, /* gate */
3391 reset_cc_flags, /* execute */
3392 NULL, /* sub */
3393 NULL, /* next */
3394 0, /* static_pass_number */
3395 0, /* tv_id */
3396 PROP_referenced_vars |PROP_cfg, /* properties_required */
3397 0, /* properties_provided */
3398 0, /* properties_destroyed */
3399 0, /* todo_flags_start */
3400 0, /* todo_flags_finish */
3401 0 /* letter */