2004-08-18 David Daney <ddaney@avtrex.com>
[official-gcc.git] / gcc / tree-ssa-alias.c
bloba344384b42656a247c1b210723397130f15e14b8
1 /* Alias analysis for trees.
2 Copyright (C) 2004 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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-alias-common.h"
43 #include "tree-pass.h"
44 #include "convert.h"
45 #include "params.h"
48 /* Structure to map a variable to its alias set and keep track of the
49 virtual operands that will be needed to represent it. */
50 struct alias_map_d
52 /* Variable and its alias set. */
53 tree var;
54 HOST_WIDE_INT set;
56 /* Total number of virtual operands that will be needed to represent
57 all the aliases of VAR. */
58 long total_alias_vops;
60 /* Nonzero if the aliases for this memory tag have been grouped
61 already. Used in group_aliases. */
62 unsigned int grouped_p : 1;
64 /* Set of variables aliased with VAR. This is the exact same
65 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
66 bitmap form to speed up alias grouping. */
67 sbitmap may_aliases;
71 /* Alias information used by compute_may_aliases and its helpers. */
72 struct alias_info
74 /* SSA names visited while collecting points-to information. If bit I
75 is set, it means that SSA variable with version I has already been
76 visited. */
77 bitmap ssa_names_visited;
79 /* Array of SSA_NAME pointers processed by the points-to collector. */
80 varray_type processed_ptrs;
82 /* Variables whose address is still needed. */
83 bitmap addresses_needed;
85 /* ADDRESSABLE_VARS contains all the global variables and locals that
86 have had their address taken. */
87 struct alias_map_d **addressable_vars;
88 size_t num_addressable_vars;
90 /* POINTERS contains all the _DECL pointers with unique memory tags
91 that have been referenced in the program. */
92 struct alias_map_d **pointers;
93 size_t num_pointers;
95 /* Number of function calls found in the program. */
96 size_t num_calls_found;
98 /* Array of counters to keep track of how many times each pointer has
99 been dereferenced in the program. This is used by the alias grouping
100 heuristic in compute_flow_insensitive_aliasing. */
101 varray_type num_references;
103 /* Total number of virtual operands that will be needed to represent
104 all the aliases of all the pointers found in the program. */
105 long total_alias_vops;
107 /* Variables that have been written to. */
108 bitmap written_vars;
110 /* Pointers that have been used in an indirect store operation. */
111 bitmap dereferenced_ptrs_store;
113 /* Pointers that have been used in an indirect load operation. */
114 bitmap dereferenced_ptrs_load;
118 /* Counters used to display statistics on alias analysis. */
119 struct alias_stats_d
121 unsigned int alias_queries;
122 unsigned int alias_mayalias;
123 unsigned int alias_noalias;
124 unsigned int simple_queries;
125 unsigned int simple_resolved;
126 unsigned int tbaa_queries;
127 unsigned int tbaa_resolved;
128 unsigned int pta_queries;
129 unsigned int pta_resolved;
133 /* Local variables. */
134 static struct alias_stats_d alias_stats;
136 /* Local functions. */
137 static void compute_flow_insensitive_aliasing (struct alias_info *);
138 static void dump_alias_stats (FILE *);
139 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT);
140 static tree create_memory_tag (tree type, bool is_type_tag);
141 static tree get_tmt_for (tree, struct alias_info *);
142 static tree get_nmt_for (tree);
143 static void add_may_alias (tree, tree);
144 static void replace_may_alias (tree, size_t, tree);
145 static struct alias_info *init_alias_info (void);
146 static void delete_alias_info (struct alias_info *);
147 static void compute_points_to_and_addr_escape (struct alias_info *);
148 static void compute_flow_sensitive_aliasing (struct alias_info *);
149 static void setup_pointers_and_addressables (struct alias_info *);
150 static bool collect_points_to_info_r (tree, tree, void *);
151 static bool is_escape_site (tree, size_t *);
152 static void add_pointed_to_var (struct alias_info *, tree, tree);
153 static void add_pointed_to_expr (tree, tree);
154 static void create_global_var (void);
155 static void collect_points_to_info_for (struct alias_info *, tree);
156 static bool ptr_is_dereferenced_by (tree, tree, bool *);
157 static void maybe_create_global_var (struct alias_info *ai);
158 static void group_aliases (struct alias_info *);
159 static struct ptr_info_def *get_ptr_info (tree t);
160 static void set_pt_anything (tree ptr);
161 static void set_pt_malloc (tree ptr);
163 /* Global declarations. */
165 /* Call clobbered variables in the function. If bit I is set, then
166 REFERENCED_VARS (I) is call-clobbered. */
167 bitmap call_clobbered_vars;
169 /* Addressable variables in the function. If bit I is set, then
170 REFERENCED_VARS (I) has had its address taken. Note that
171 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
172 addressable variable is not necessarily call-clobbered (e.g., a
173 local addressable whose address does not escape) and not all
174 call-clobbered variables are addressable (e.g., a local static
175 variable). */
176 bitmap addressable_vars;
178 /* When the program has too many call-clobbered variables and call-sites,
179 this variable is used to represent the clobbering effects of function
180 calls. In these cases, all the call clobbered variables in the program
181 are forced to alias this variable. This reduces compile times by not
182 having to keep track of too many V_MAY_DEF expressions at call sites. */
183 tree global_var;
186 /* Compute may-alias information for every variable referenced in function
187 FNDECL.
189 Alias analysis proceeds in 3 main phases:
191 1- Points-to and escape analysis.
193 This phase walks the use-def chains in the SSA web looking for three
194 things:
196 * Assignments of the form P_i = &VAR
197 * Assignments of the form P_i = malloc()
198 * Pointers and ADDR_EXPR that escape the current function.
200 The concept of 'escaping' is the same one used in the Java world. When
201 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
202 outside of the current function. So, assignment to global variables,
203 function arguments and returning a pointer are all escape sites.
205 This is where we are currently limited. Since not everything is renamed
206 into SSA, we lose track of escape properties when a pointer is stashed
207 inside a field in a structure, for instance. In those cases, we are
208 assuming that the pointer does escape.
210 We use escape analysis to determine whether a variable is
211 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
212 is call-clobbered. If a pointer P_i escapes, then all the variables
213 pointed-to by P_i (and its memory tag) also escape.
215 2- Compute flow-sensitive aliases
217 We have two classes of memory tags. Memory tags associated with the
218 pointed-to data type of the pointers in the program. These tags are
219 called "type memory tag" (TMT). The other class are those associated
220 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
221 when adding operands for an INDIRECT_REF *P_i, we will first check
222 whether P_i has a name tag, if it does we use it, because that will have
223 more precise aliasing information. Otherwise, we use the standard type
224 tag.
226 In this phase, we go through all the pointers we found in points-to
227 analysis and create alias sets for the name memory tags associated with
228 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
229 it points to and its tag.
232 3- Compute flow-insensitive aliases
234 This pass will compare the alias set of every type memory tag and every
235 addressable variable found in the program. Given a type memory tag TMT
236 and an addressable variable V. If the alias sets of TMT and V conflict
237 (as computed by may_alias_p), then V is marked as an alias tag and added
238 to the alias set of TMT.
240 For instance, consider the following function:
242 foo (int i)
244 int *p, *q, a, b;
246 if (i > 10)
247 p = &a;
248 else
249 q = &b;
251 *p = 3;
252 *q = 5;
253 a = b + 2;
254 return *p;
257 After aliasing analysis has finished, the type memory tag for pointer
258 'p' will have two aliases, namely variables 'a' and 'b'. Every time
259 pointer 'p' is dereferenced, we want to mark the operation as a
260 potential reference to 'a' and 'b'.
262 foo (int i)
264 int *p, a, b;
266 if (i_2 > 10)
267 p_4 = &a;
268 else
269 p_6 = &b;
270 # p_1 = PHI <p_4(1), p_6(2)>;
272 # a_7 = V_MAY_DEF <a_3>;
273 # b_8 = V_MAY_DEF <b_5>;
274 *p_1 = 3;
276 # a_9 = V_MAY_DEF <a_7>
277 # VUSE <b_8>
278 a_9 = b_8 + 2;
280 # VUSE <a_9>;
281 # VUSE <b_8>;
282 return *p_1;
285 In certain cases, the list of may aliases for a pointer may grow too
286 large. This may cause an explosion in the number of virtual operands
287 inserted in the code. Resulting in increased memory consumption and
288 compilation time.
290 When the number of virtual operands needed to represent aliased
291 loads and stores grows too large (configurable with @option{--param
292 max-aliased-vops}), alias sets are grouped to avoid severe
293 compile-time slow downs and memory consumption. See group_aliases. */
295 static void
296 compute_may_aliases (void)
298 struct alias_info *ai;
300 memset (&alias_stats, 0, sizeof (alias_stats));
302 /* Initialize aliasing information. */
303 ai = init_alias_info ();
305 /* For each pointer P_i, determine the sets of variables that P_i may
306 point-to. For every addressable variable V, determine whether the
307 address of V escapes the current function, making V call-clobbered
308 (i.e., whether &V is stored in a global variable or if its passed as a
309 function call argument). */
310 compute_points_to_and_addr_escape (ai);
312 /* Collect all pointers and addressable variables, compute alias sets,
313 create memory tags for pointers and promote variables whose address is
314 not needed anymore. */
315 setup_pointers_and_addressables (ai);
317 /* Compute flow-sensitive, points-to based aliasing for all the name
318 memory tags. Note that this pass needs to be done before flow
319 insensitive analysis because it uses the points-to information
320 gathered before to mark call-clobbered type tags. */
321 compute_flow_sensitive_aliasing (ai);
323 /* Compute type-based flow-insensitive aliasing for all the type
324 memory tags. */
325 compute_flow_insensitive_aliasing (ai);
327 /* If the program has too many call-clobbered variables and/or function
328 calls, create .GLOBAL_VAR and use it to model call-clobbering
329 semantics at call sites. This reduces the number of virtual operands
330 considerably, improving compile times at the expense of lost
331 aliasing precision. */
332 maybe_create_global_var (ai);
334 /* Debugging dumps. */
335 if (dump_file)
337 dump_referenced_vars (dump_file);
338 if (dump_flags & TDF_STATS)
339 dump_alias_stats (dump_file);
340 dump_points_to_info (dump_file);
341 dump_alias_info (dump_file);
344 /* Deallocate memory used by aliasing data structures. */
345 delete_alias_info (ai);
348 struct tree_opt_pass pass_may_alias =
350 "alias", /* name */
351 NULL, /* gate */
352 compute_may_aliases, /* execute */
353 NULL, /* sub */
354 NULL, /* next */
355 0, /* static_pass_number */
356 TV_TREE_MAY_ALIAS, /* tv_id */
357 PROP_cfg | PROP_ssa | PROP_pta, /* properties_required */
358 PROP_alias, /* properties_provided */
359 0, /* properties_destroyed */
360 0, /* todo_flags_start */
361 TODO_dump_func | TODO_rename_vars
362 | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
366 /* Initialize the data structures used for alias analysis. */
368 static struct alias_info *
369 init_alias_info (void)
371 struct alias_info *ai;
372 static bool aliases_computed_p = false;
374 ai = xcalloc (1, sizeof (struct alias_info));
375 ai->ssa_names_visited = BITMAP_XMALLOC ();
376 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
377 ai->addresses_needed = BITMAP_XMALLOC ();
378 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
379 ai->written_vars = BITMAP_XMALLOC ();
380 ai->dereferenced_ptrs_store = BITMAP_XMALLOC ();
381 ai->dereferenced_ptrs_load = BITMAP_XMALLOC ();
383 /* If aliases have been computed before, clear existing information. */
384 if (aliases_computed_p)
386 size_t i;
388 /* Clear the call-clobbered set. We are going to re-discover
389 call-clobbered variables. */
390 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
392 tree var = referenced_var (i);
394 /* Variables that are intrinsically call-clobbered (globals,
395 local statics, etc) will not be marked by the aliasing
396 code, so we can't remove them from CALL_CLOBBERED_VARS. */
397 if (!is_call_clobbered (var))
398 bitmap_clear_bit (call_clobbered_vars, var_ann (var)->uid);
401 /* Similarly, clear the set of addressable variables. In this
402 case, we can just clear the set because addressability is
403 only computed here. */
404 bitmap_clear (addressable_vars);
406 /* Clear flow-insensitive alias information from each symbol. */
407 for (i = 0; i < num_referenced_vars; i++)
409 var_ann_t ann = var_ann (referenced_var (i));
410 ann->is_alias_tag = 0;
411 ann->may_aliases = NULL;
414 /* Clear flow-sensitive points-to information from each SSA name. */
415 for (i = 1; i < num_ssa_names; i++)
417 tree name = ssa_name (i);
419 if (!POINTER_TYPE_P (TREE_TYPE (name)))
420 continue;
422 if (SSA_NAME_PTR_INFO (name))
424 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
426 /* Clear all the flags but keep the name tag to
427 avoid creating new temporaries unnecessarily. If
428 this pointer is found to point to a subset or
429 superset of its former points-to set, then a new
430 tag will need to be created in create_name_tags. */
431 pi->pt_anything = 0;
432 pi->pt_malloc = 0;
433 pi->value_escapes_p = 0;
434 pi->is_dereferenced = 0;
435 if (pi->pt_vars)
436 bitmap_clear (pi->pt_vars);
441 /* Next time, we will need to reset alias information. */
442 aliases_computed_p = true;
444 return ai;
448 /* Deallocate memory used by alias analysis. */
450 static void
451 delete_alias_info (struct alias_info *ai)
453 size_t i;
455 BITMAP_XFREE (ai->ssa_names_visited);
456 ai->processed_ptrs = NULL;
457 BITMAP_XFREE (ai->addresses_needed);
459 for (i = 0; i < ai->num_addressable_vars; i++)
461 sbitmap_free (ai->addressable_vars[i]->may_aliases);
462 free (ai->addressable_vars[i]);
464 free (ai->addressable_vars);
466 for (i = 0; i < ai->num_pointers; i++)
468 sbitmap_free (ai->pointers[i]->may_aliases);
469 free (ai->pointers[i]);
471 free (ai->pointers);
473 ai->num_references = NULL;
474 BITMAP_XFREE (ai->written_vars);
475 BITMAP_XFREE (ai->dereferenced_ptrs_store);
476 BITMAP_XFREE (ai->dereferenced_ptrs_load);
478 free (ai);
482 /* Walk use-def chains for pointer PTR to determine what variables is PTR
483 pointing to. */
485 static void
486 collect_points_to_info_for (struct alias_info *ai, tree ptr)
488 #if defined ENABLE_CHECKING
489 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
490 abort ();
491 #endif
493 if (!bitmap_bit_p (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
495 bitmap_set_bit (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
496 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
497 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
502 /* Helper for ptr_is_dereferenced_by. Called by walk_tree to look for
503 INDIRECT_REF nodes for the pointer passed in DATA. */
505 static tree
506 find_ptr_dereference (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
508 tree ptr = (tree) data;
510 if (TREE_CODE (*tp) == INDIRECT_REF
511 && TREE_OPERAND (*tp, 0) == ptr)
512 return *tp;
514 return NULL_TREE;
518 /* Return true if STMT contains INDIRECT_REF <PTR>. *IS_STORE is set
519 to 'true' if the dereference is on the LHS of an assignment. */
521 static bool
522 ptr_is_dereferenced_by (tree ptr, tree stmt, bool *is_store)
524 *is_store = false;
526 if (TREE_CODE (stmt) == MODIFY_EXPR
527 || (TREE_CODE (stmt) == RETURN_EXPR
528 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR))
530 tree e, lhs, rhs;
532 e = (TREE_CODE (stmt) == RETURN_EXPR) ? TREE_OPERAND (stmt, 0) : stmt;
533 lhs = TREE_OPERAND (e, 0);
534 rhs = TREE_OPERAND (e, 1);
536 if (EXPR_P (lhs)
537 && walk_tree (&lhs, find_ptr_dereference, ptr, NULL))
539 *is_store = true;
540 return true;
542 else if (EXPR_P (rhs)
543 && walk_tree (&rhs, find_ptr_dereference, ptr, NULL))
545 return true;
548 else if (TREE_CODE (stmt) == ASM_EXPR)
550 if (walk_tree (&ASM_OUTPUTS (stmt), find_ptr_dereference, ptr, NULL)
551 || walk_tree (&ASM_CLOBBERS (stmt), find_ptr_dereference, ptr, NULL))
553 *is_store = true;
554 return true;
556 else if (walk_tree (&ASM_INPUTS (stmt), find_ptr_dereference, ptr, NULL))
558 return true;
562 return false;
566 /* Traverse use-def links for all the pointers in the program to collect
567 address escape and points-to information.
569 This is loosely based on the same idea described in R. Hasti and S.
570 Horwitz, ``Using static single assignment form to improve
571 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
572 Programming Language Design and Implementation, pp. 97-105, 1998. */
574 static void
575 compute_points_to_and_addr_escape (struct alias_info *ai)
577 basic_block bb;
578 size_t i;
580 timevar_push (TV_TREE_PTA);
582 FOR_EACH_BB (bb)
584 bb_ann_t block_ann = bb_ann (bb);
585 block_stmt_iterator si;
587 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
589 use_optype uses;
590 def_optype defs;
591 v_may_def_optype v_may_defs;
592 v_must_def_optype v_must_defs;
593 stmt_ann_t ann;
594 bitmap addr_taken;
595 tree stmt = bsi_stmt (si);
596 bool stmt_escapes_p = is_escape_site (stmt, &ai->num_calls_found);
598 /* Mark all the variables whose address are taken by the
599 statement. Note that this will miss all the addresses taken
600 in PHI nodes (those are discovered while following the use-def
601 chains). */
602 get_stmt_operands (stmt);
603 addr_taken = addresses_taken (stmt);
604 if (addr_taken)
605 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i,
607 tree var = referenced_var (i);
608 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
609 if (stmt_escapes_p)
610 mark_call_clobbered (var);
613 if (stmt_escapes_p)
614 block_ann->has_escape_site = 1;
616 /* Special case for silly ADDR_EXPR tricks
617 (gcc.c-torture/unsorted/pass.c). If this statement is an
618 assignment to a non-pointer variable and the RHS takes the
619 address of a variable, assume that the variable on the RHS is
620 call-clobbered. We could add the LHS to the list of
621 "pointers" and follow it to see if it really escapes, but it's
622 not worth the pain. */
623 if (addr_taken
624 && TREE_CODE (stmt) == MODIFY_EXPR
625 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 0))))
626 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i,
628 tree var = referenced_var (i);
629 mark_call_clobbered (var);
632 ann = stmt_ann (stmt);
633 uses = USE_OPS (ann);
634 for (i = 0; i < NUM_USES (uses); i++)
636 tree op = USE_OP (uses, i);
637 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
638 struct ptr_info_def *pi;
639 bool is_store;
641 /* If the operand's variable may be aliased, keep track
642 of how many times we've referenced it. This is used
643 for alias grouping in compute_flow_sensitive_aliasing.
644 Note that we don't need to grow AI->NUM_REFERENCES
645 because we are processing regular variables, not
646 memory tags (the array's initial size is set to
647 NUM_REFERENCED_VARS). */
648 if (may_be_aliased (SSA_NAME_VAR (op)))
649 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
651 if (!POINTER_TYPE_P (TREE_TYPE (op)))
652 continue;
654 collect_points_to_info_for (ai, op);
656 pi = SSA_NAME_PTR_INFO (op);
657 if (ptr_is_dereferenced_by (op, stmt, &is_store))
659 /* If we found OP to point to a set of variables or
660 malloc, then mark it as being dereferenced. In a
661 subsequent pass, dereferenced pointers that point
662 to a set of variables will be assigned a name tag
663 to alias all the variables OP points to. */
664 if (pi->pt_malloc || pi->pt_vars)
665 pi->is_dereferenced = 1;
667 /* Keep track of how many time we've dereferenced each
668 pointer. Again, we don't need to grow
669 AI->NUM_REFERENCES because we're processing
670 existing program variables. */
671 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
673 /* If this is a store operation, mark OP as being
674 dereferenced to store, otherwise mark it as being
675 dereferenced to load. */
676 if (is_store)
677 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
678 else
679 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
681 else if (stmt_escapes_p)
683 /* Note that even if STMT is an escape point, pointer OP
684 will not escape if it is being dereferenced. That's
685 why we only check for escape points if OP is not
686 dereferenced by STMT. */
687 pi->value_escapes_p = 1;
689 /* If the statement makes a function call, assume
690 that pointer OP will be dereferenced in a store
691 operation inside the called function. */
692 if (get_call_expr_in (stmt))
694 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
695 pi->is_dereferenced = 1;
700 /* Update reference counter for definitions to any
701 potentially aliased variable. This is used in the alias
702 grouping heuristics. */
703 defs = DEF_OPS (ann);
704 for (i = 0; i < NUM_DEFS (defs); i++)
706 tree op = DEF_OP (defs, i);
707 tree var = SSA_NAME_VAR (op);
708 var_ann_t ann = var_ann (var);
709 bitmap_set_bit (ai->written_vars, ann->uid);
710 if (may_be_aliased (var))
711 (VARRAY_UINT (ai->num_references, ann->uid))++;
714 /* Mark variables in V_MAY_DEF operands as being written to. */
715 v_may_defs = V_MAY_DEF_OPS (ann);
716 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
718 tree op = V_MAY_DEF_OP (v_may_defs, i);
719 tree var = SSA_NAME_VAR (op);
720 var_ann_t ann = var_ann (var);
721 bitmap_set_bit (ai->written_vars, ann->uid);
724 /* Mark variables in V_MUST_DEF operands as being written to. */
725 v_must_defs = V_MUST_DEF_OPS (ann);
726 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
728 tree op = V_MUST_DEF_OP (v_must_defs, i);
729 tree var = SSA_NAME_VAR (op);
730 var_ann_t ann = var_ann (var);
731 bitmap_set_bit (ai->written_vars, ann->uid);
734 /* After promoting variables and computing aliasing we will
735 need to re-scan most statements. FIXME: Try to minimize the
736 number of statements re-scanned. It's not really necessary to
737 re-scan *all* statements. */
738 modify_stmt (stmt);
742 timevar_pop (TV_TREE_PTA);
746 /* Create name tags for all the pointers that have been dereferenced.
747 We only create a name tag for a pointer P if P is found to point to
748 a set of variables (so that we can alias them to *P) or if it is
749 the result of a call to malloc (which means that P cannot point to
750 anything else nor alias any other variable).
752 If two pointers P and Q point to the same set of variables, they
753 are assigned the same name tag. */
755 static void
756 create_name_tags (struct alias_info *ai)
758 size_t i;
760 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
762 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
763 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
765 if (!pi->is_dereferenced)
767 /* No name tags for pointers that have not been
768 dereferenced. */
769 pi->name_mem_tag = NULL_TREE;
770 continue;
773 if (pi->pt_vars)
775 size_t j;
776 tree old_name_tag = pi->name_mem_tag;
778 /* If PTR points to a set of variables, check if we don't
779 have another pointer Q with the same points-to set before
780 creating a tag. If so, use Q's tag instead of creating a
781 new one.
783 This is important for not creating unnecessary symbols
784 and also for copy propagation. If we ever need to
785 propagate PTR into Q or vice-versa, we would run into
786 problems if they both had different name tags because
787 they would have different SSA version numbers (which
788 would force us to take the name tags in and out of SSA). */
789 for (j = 0; j < i; j++)
791 tree q = VARRAY_TREE (ai->processed_ptrs, j);
792 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
794 if (qi
795 && qi->pt_vars
796 && qi->name_mem_tag
797 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
799 pi->name_mem_tag = qi->name_mem_tag;
800 break;
804 /* If we didn't find a pointer with the same points-to set
805 as PTR, create a new name tag if needed. */
806 if (pi->name_mem_tag == NULL_TREE)
807 pi->name_mem_tag = get_nmt_for (ptr);
809 /* If the new name tag computed for PTR is different than
810 the old name tag that it used to have, then the old tag
811 needs to be removed from the IL, so we mark it for
812 renaming. */
813 if (old_name_tag && old_name_tag != pi->name_mem_tag)
814 bitmap_set_bit (vars_to_rename, var_ann (old_name_tag)->uid);
816 else if (pi->pt_malloc)
818 /* Otherwise, create a unique name tag for this pointer. */
819 pi->name_mem_tag = get_nmt_for (ptr);
821 else
823 /* Only pointers that may point to malloc or other variables
824 may receive a name tag. If the pointer does not point to
825 a known spot, we should use type tags. */
826 set_pt_anything (ptr);
827 continue;
830 /* Mark the new name tag for renaming. */
831 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
837 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
838 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
839 name tag and the variables it points-to are call-clobbered. Finally, if
840 P_i escapes and we could not determine where it points to, then all the
841 variables in the same alias set as *P_i are marked call-clobbered. This
842 is necessary because we must assume that P_i may take the address of any
843 variable in the same alias set. */
845 static void
846 compute_flow_sensitive_aliasing (struct alias_info *ai)
848 size_t i;
850 create_name_tags (ai);
852 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
854 size_t j;
855 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
856 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
857 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
859 if (pi->value_escapes_p || pi->pt_anything)
861 /* If PTR escapes or may point to anything, then its associated
862 memory tags are call-clobbered. */
863 if (pi->name_mem_tag)
864 mark_call_clobbered (pi->name_mem_tag);
866 if (v_ann->type_mem_tag)
867 mark_call_clobbered (v_ann->type_mem_tag);
869 /* If PTR may point to anything, mark call-clobbered all the
870 addressables with the same alias set as the type pointed-to by
871 PTR. */
872 if (pi->pt_anything)
874 HOST_WIDE_INT ptr_set;
875 ptr_set = get_alias_set (TREE_TYPE (TREE_TYPE (ptr)));
876 for (j = 0; j < ai->num_addressable_vars; j++)
878 struct alias_map_d *alias_map = ai->addressable_vars[j];
879 if (alias_map->set == ptr_set)
880 mark_call_clobbered (alias_map->var);
884 /* If PTR's value may escape and PTR is never dereferenced, we
885 need to mark all the variables PTR points-to as
886 call-clobbered. Note that we only need do this it PTR is
887 never dereferenced. If PTR is dereferenced, it will have a
888 name memory tag, which will have been marked call-clobbered.
889 This will in turn mark the pointed-to variables as
890 call-clobbered when we call add_may_alias below. */
891 if (pi->value_escapes_p
892 && pi->name_mem_tag == NULL_TREE
893 && pi->pt_vars)
894 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j,
895 mark_call_clobbered (referenced_var (j)));
898 /* Set up aliasing information for PTR's name memory tag (if it has
899 one). Note that only pointers that have been dereferenced will
900 have a name memory tag. */
901 if (pi->name_mem_tag && pi->pt_vars)
902 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j,
903 add_may_alias (pi->name_mem_tag, referenced_var (j)));
905 /* If the name tag is call clobbered, so is the type tag
906 associated with the base VAR_DECL. */
907 if (pi->name_mem_tag
908 && v_ann->type_mem_tag
909 && is_call_clobbered (pi->name_mem_tag))
910 mark_call_clobbered (v_ann->type_mem_tag);
915 /* Compute type-based alias sets. Traverse all the pointers and
916 addressable variables found in setup_pointers_and_addressables.
918 For every pointer P in AI->POINTERS and addressable variable V in
919 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
920 memory tag (TMT) if their alias sets conflict. V is then marked as
921 an alias tag so that the operand scanner knows that statements
922 containing V have aliased operands. */
924 static void
925 compute_flow_insensitive_aliasing (struct alias_info *ai)
927 size_t i;
929 /* Initialize counter for the total number of virtual operands that
930 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
931 threshold set by --params max-alias-vops, we enable alias
932 grouping. */
933 ai->total_alias_vops = 0;
935 /* For every pointer P, determine which addressable variables may alias
936 with P's type memory tag. */
937 for (i = 0; i < ai->num_pointers; i++)
939 size_t j;
940 struct alias_map_d *p_map = ai->pointers[i];
941 tree tag = var_ann (p_map->var)->type_mem_tag;
942 var_ann_t tag_ann = var_ann (tag);
944 p_map->total_alias_vops = 0;
945 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
946 sbitmap_zero (p_map->may_aliases);
948 for (j = 0; j < ai->num_addressable_vars; j++)
950 struct alias_map_d *v_map;
951 var_ann_t v_ann;
952 tree var;
953 bool tag_stored_p, var_stored_p;
955 v_map = ai->addressable_vars[j];
956 var = v_map->var;
957 v_ann = var_ann (var);
959 /* Skip memory tags and variables that have never been
960 written to. We also need to check if the variables are
961 call-clobbered because they may be overwritten by
962 function calls. */
963 tag_stored_p = bitmap_bit_p (ai->written_vars, tag_ann->uid)
964 || is_call_clobbered (tag);
965 var_stored_p = bitmap_bit_p (ai->written_vars, v_ann->uid)
966 || is_call_clobbered (var);
967 if (!tag_stored_p && !var_stored_p)
968 continue;
970 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
972 size_t num_tag_refs, num_var_refs;
974 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
975 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
977 /* Add VAR to TAG's may-aliases set. */
978 add_may_alias (tag, var);
980 /* Update the total number of virtual operands due to
981 aliasing. Since we are adding one more alias to TAG's
982 may-aliases set, the total number of virtual operands due
983 to aliasing will be increased by the number of references
984 made to VAR and TAG (every reference to TAG will also
985 count as a reference to VAR). */
986 ai->total_alias_vops += (num_var_refs + num_tag_refs);
987 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
989 /* Update the bitmap used to represent TAG's alias set
990 in case we need to group aliases. */
991 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
996 if (dump_file)
997 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
998 get_name (current_function_decl),
999 ai->total_alias_vops);
1001 /* Determine if we need to enable alias grouping. */
1002 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1003 group_aliases (ai);
1007 /* Comparison function for qsort used in group_aliases. */
1009 static int
1010 total_alias_vops_cmp (const void *p, const void *q)
1012 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1013 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1014 long n1 = (*p1)->total_alias_vops;
1015 long n2 = (*p2)->total_alias_vops;
1017 /* We want to sort in descending order. */
1018 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1021 /* Group all the aliases for TAG to make TAG represent all the
1022 variables in its alias set. Update the total number
1023 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1024 function will make TAG be the unique alias tag for all the
1025 variables in its may-aliases. So, given:
1027 may-aliases(TAG) = { V1, V2, V3 }
1029 This function will group the variables into:
1031 may-aliases(V1) = { TAG }
1032 may-aliases(V2) = { TAG }
1033 may-aliases(V2) = { TAG } */
1035 static void
1036 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
1038 size_t i;
1039 var_ann_t tag_ann = var_ann (tag);
1040 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1042 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i,
1044 tree var = referenced_var (i);
1045 var_ann_t ann = var_ann (var);
1047 /* Make TAG the unique alias of VAR. */
1048 ann->is_alias_tag = 0;
1049 ann->may_aliases = NULL;
1051 /* Note that VAR and TAG may be the same if the function has no
1052 addressable variables (see the discussion at the end of
1053 setup_pointers_and_addressables). */
1054 if (var != tag)
1055 add_may_alias (var, tag);
1057 /* Reduce total number of virtual operands contributed
1058 by TAG on behalf of VAR. Notice that the references to VAR
1059 itself won't be removed. We will merely replace them with
1060 references to TAG. */
1061 ai->total_alias_vops -= num_tag_refs;
1064 /* We have reduced the number of virtual operands that TAG makes on
1065 behalf of all the variables formerly aliased with it. However,
1066 we have also "removed" all the virtual operands for TAG itself,
1067 so we add them back. */
1068 ai->total_alias_vops += num_tag_refs;
1070 /* TAG no longer has any aliases. */
1071 tag_ann->may_aliases = NULL;
1075 /* Group may-aliases sets to reduce the number of virtual operands due
1076 to aliasing.
1078 1- Sort the list of pointers in decreasing number of contributed
1079 virtual operands.
1081 2- Take the first entry in AI->POINTERS and revert the role of
1082 the memory tag and its aliases. Usually, whenever an aliased
1083 variable Vi is found to alias with a memory tag T, we add Vi
1084 to the may-aliases set for T. Meaning that after alias
1085 analysis, we will have:
1087 may-aliases(T) = { V1, V2, V3, ..., Vn }
1089 This means that every statement that references T, will get 'n'
1090 virtual operands for each of the Vi tags. But, when alias
1091 grouping is enabled, we make T an alias tag and add it to the
1092 alias set of all the Vi variables:
1094 may-aliases(V1) = { T }
1095 may-aliases(V2) = { T }
1097 may-aliases(Vn) = { T }
1099 This has two effects: (a) statements referencing T will only get
1100 a single virtual operand, and, (b) all the variables Vi will now
1101 appear to alias each other. So, we lose alias precision to
1102 improve compile time. But, in theory, a program with such a high
1103 level of aliasing should not be very optimizable in the first
1104 place.
1106 3- Since variables may be in the alias set of more than one
1107 memory tag, the grouping done in step (2) needs to be extended
1108 to all the memory tags that have a non-empty intersection with
1109 the may-aliases set of tag T. For instance, if we originally
1110 had these may-aliases sets:
1112 may-aliases(T) = { V1, V2, V3 }
1113 may-aliases(R) = { V2, V4 }
1115 In step (2) we would have reverted the aliases for T as:
1117 may-aliases(V1) = { T }
1118 may-aliases(V2) = { T }
1119 may-aliases(V3) = { T }
1121 But note that now V2 is no longer aliased with R. We could
1122 add R to may-aliases(V2), but we are in the process of
1123 grouping aliases to reduce virtual operands so what we do is
1124 add V4 to the grouping to obtain:
1126 may-aliases(V1) = { T }
1127 may-aliases(V2) = { T }
1128 may-aliases(V3) = { T }
1129 may-aliases(V4) = { T }
1131 4- If the total number of virtual operands due to aliasing is
1132 still above the threshold set by max-alias-vops, go back to (2). */
1134 static void
1135 group_aliases (struct alias_info *ai)
1137 size_t i;
1138 sbitmap res;
1140 /* Sort the POINTERS array in descending order of contributed
1141 virtual operands. */
1142 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1143 total_alias_vops_cmp);
1145 res = sbitmap_alloc (num_referenced_vars);
1147 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1148 and the tag's may-aliases set. */
1149 for (i = 0; i < ai->num_pointers; i++)
1151 size_t j;
1152 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1153 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1155 /* Skip tags that have been grouped already. */
1156 if (ai->pointers[i]->grouped_p)
1157 continue;
1159 /* See if TAG1 had any aliases in common with other type tags.
1160 If we find a TAG2 with common aliases with TAG1, add TAG2's
1161 aliases into TAG1. */
1162 for (j = i + 1; j < ai->num_pointers; j++)
1164 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1166 sbitmap_a_and_b (res, tag1_aliases, tag2_aliases);
1167 if (sbitmap_first_set_bit (res) >= 0)
1169 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1171 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1173 /* TAG2 does not need its aliases anymore. */
1174 sbitmap_zero (tag2_aliases);
1175 var_ann (tag2)->may_aliases = NULL;
1177 /* TAG1 is the unique alias of TAG2. */
1178 add_may_alias (tag2, tag1);
1180 ai->pointers[j]->grouped_p = true;
1184 /* Now group all the aliases we collected into TAG1. */
1185 group_aliases_into (tag1, tag1_aliases, ai);
1187 /* If we've reduced total number of virtual operands below the
1188 threshold, stop. */
1189 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1190 break;
1193 /* Finally, all the variables that have been grouped cannot be in
1194 the may-alias set of name memory tags. Suppose that we have
1195 grouped the aliases in this code so that may-aliases(a) = TMT.20
1197 p_5 = &a;
1199 # a_9 = V_MAY_DEF <a_8>
1200 p_5->field = 0
1201 ... Several modifications to TMT.20 ...
1202 # VUSE <a_9>
1203 x_30 = p_5->field
1205 Since p_5 points to 'a', the optimizers will try to propagate 0
1206 into p_5->field, but that is wrong because there have been
1207 modifications to 'TMT.20' in between. To prevent this we have to
1208 replace 'a' with 'TMT.20' in the name tag of p_5. */
1209 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1211 size_t j;
1212 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1213 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1214 varray_type aliases;
1216 if (name_tag == NULL_TREE)
1217 continue;
1219 aliases = var_ann (name_tag)->may_aliases;
1220 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1222 tree alias = VARRAY_TREE (aliases, j);
1223 var_ann_t ann = var_ann (alias);
1225 if (ann->mem_tag_kind == NOT_A_TAG && ann->may_aliases)
1227 tree new_alias;
1229 #if defined ENABLE_CHECKING
1230 if (VARRAY_ACTIVE_SIZE (ann->may_aliases) != 1)
1231 abort ();
1232 #endif
1233 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1234 replace_may_alias (name_tag, j, new_alias);
1239 sbitmap_free (res);
1241 if (dump_file)
1242 fprintf (dump_file,
1243 "%s: Total number of aliased vops after grouping: %ld%s\n",
1244 get_name (current_function_decl),
1245 ai->total_alias_vops,
1246 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1250 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1252 static void
1253 create_alias_map_for (tree var, struct alias_info *ai)
1255 struct alias_map_d *alias_map;
1256 alias_map = xcalloc (1, sizeof (*alias_map));
1257 alias_map->var = var;
1258 alias_map->set = get_alias_set (var);
1259 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1263 /* Create memory tags for all the dereferenced pointers and build the
1264 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1265 sets. Based on the address escape and points-to information collected
1266 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1267 variables whose address is not needed anymore. */
1269 static void
1270 setup_pointers_and_addressables (struct alias_info *ai)
1272 size_t i, n_vars, num_addressable_vars, num_pointers;
1274 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1275 num_addressable_vars = num_pointers = 0;
1276 for (i = 0; i < num_referenced_vars; i++)
1278 tree var = referenced_var (i);
1280 if (may_be_aliased (var))
1281 num_addressable_vars++;
1283 if (POINTER_TYPE_P (TREE_TYPE (var)))
1285 /* Since we don't keep track of volatile variables, assume that
1286 these pointers are used in indirect store operations. */
1287 if (TREE_THIS_VOLATILE (var))
1288 bitmap_set_bit (ai->dereferenced_ptrs_store, var_ann (var)->uid);
1290 num_pointers++;
1294 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1295 always going to be slightly bigger than we actually need them
1296 because some TREE_ADDRESSABLE variables will be marked
1297 non-addressable below and only pointers with unique type tags are
1298 going to be added to POINTERS. */
1299 ai->addressable_vars = xcalloc (num_addressable_vars,
1300 sizeof (struct alias_map_d *));
1301 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1302 ai->num_addressable_vars = 0;
1303 ai->num_pointers = 0;
1305 /* Since we will be creating type memory tags within this loop, cache the
1306 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1307 unnecessarily. */
1308 n_vars = num_referenced_vars;
1310 for (i = 0; i < n_vars; i++)
1312 tree var = referenced_var (i);
1313 var_ann_t v_ann = var_ann (var);
1315 /* Name memory tags already have flow-sensitive aliasing
1316 information, so they need not be processed by
1317 compute_may_aliases. Similarly, type memory tags are already
1318 accounted for when we process their associated pointer. */
1319 if (v_ann->mem_tag_kind != NOT_A_TAG)
1320 continue;
1322 /* Remove the ADDRESSABLE flag from every addressable variable whose
1323 address is not needed anymore. This is caused by the propagation
1324 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1325 removal of dead pointer assignments done by the early scalar
1326 cleanup passes. */
1327 if (TREE_ADDRESSABLE (var))
1329 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1330 && v_ann->mem_tag_kind == NOT_A_TAG
1331 && !is_global_var (var))
1333 /* The address of VAR is not needed, remove the
1334 addressable bit, so that it can be optimized as a
1335 regular variable. */
1336 mark_non_addressable (var);
1338 /* Since VAR is now a regular GIMPLE register, we will need
1339 to rename VAR into SSA afterwards. */
1340 bitmap_set_bit (vars_to_rename, v_ann->uid);
1342 else
1344 /* Add the variable to the set of addressables. Mostly
1345 used when scanning operands for ASM_EXPRs that
1346 clobber memory. In those cases, we need to clobber
1347 all call-clobbered variables and all addressables. */
1348 bitmap_set_bit (addressable_vars, v_ann->uid);
1352 /* Global variables and addressable locals may be aliased. Create an
1353 entry in ADDRESSABLE_VARS for VAR. */
1354 if (may_be_aliased (var))
1356 create_alias_map_for (var, ai);
1357 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1360 /* Add pointer variables that have been dereferenced to the POINTERS
1361 array and create a type memory tag for them. */
1362 if (POINTER_TYPE_P (TREE_TYPE (var)))
1364 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1365 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1367 tree tag;
1368 var_ann_t t_ann;
1370 /* If pointer VAR still doesn't have a memory tag
1371 associated with it, create it now or re-use an
1372 existing one. */
1373 tag = get_tmt_for (var, ai);
1374 t_ann = var_ann (tag);
1376 /* The type tag will need to be renamed into SSA
1377 afterwards. Note that we cannot do this inside
1378 get_tmt_for because aliasing may run multiple times
1379 and we only create type tags the first time. */
1380 bitmap_set_bit (vars_to_rename, t_ann->uid);
1382 /* Associate the tag with pointer VAR. */
1383 v_ann->type_mem_tag = tag;
1385 /* If pointer VAR has been used in a store operation,
1386 then its memory tag must be marked as written-to. */
1387 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1388 bitmap_set_bit (ai->written_vars, t_ann->uid);
1390 /* If pointer VAR is a global variable or a PARM_DECL,
1391 then its memory tag should be considered a global
1392 variable. */
1393 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1394 mark_call_clobbered (tag);
1396 /* All the dereferences of pointer VAR count as
1397 references of TAG. Since TAG can be associated with
1398 several pointers, add the dereferences of VAR to the
1399 TAG. We may need to grow AI->NUM_REFERENCES because
1400 we have been adding name and type tags. */
1401 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1402 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1404 VARRAY_UINT (ai->num_references, t_ann->uid)
1405 += VARRAY_UINT (ai->num_references, v_ann->uid);
1407 else
1409 /* The pointer has not been dereferenced. If it had a
1410 type memory tag, remove it and mark the old tag for
1411 renaming to remove it out of the IL. */
1412 var_ann_t ann = var_ann (var);
1413 tree tag = ann->type_mem_tag;
1414 if (tag)
1416 bitmap_set_bit (vars_to_rename, var_ann (tag)->uid);
1417 ann->type_mem_tag = NULL_TREE;
1423 /* If we found no addressable variables, but we have more than one
1424 pointer, we will need to check for conflicts between the
1425 pointers. Otherwise, we would miss alias relations as in
1426 testsuite/gcc.dg/tree-ssa/20040319-1.c:
1428 struct bar { int count; int *arr;};
1430 void foo (struct bar *b)
1432 b->count = 0;
1433 *(b->arr) = 2;
1434 if (b->count == 0)
1435 abort ();
1438 b->count and *(b->arr) could be aliased if b->arr == &b->count.
1439 To do this, we add all the memory tags for the pointers in
1440 AI->POINTERS to AI->ADDRESSABLE_VARS, so that
1441 compute_flow_insensitive_aliasing will naturally compare every
1442 pointer to every type tag. */
1443 if (ai->num_addressable_vars == 0
1444 && ai->num_pointers > 1)
1446 free (ai->addressable_vars);
1447 ai->addressable_vars = xcalloc (ai->num_pointers,
1448 sizeof (struct alias_map_d *));
1449 ai->num_addressable_vars = 0;
1450 for (i = 0; i < ai->num_pointers; i++)
1452 struct alias_map_d *p = ai->pointers[i];
1453 tree tag = var_ann (p->var)->type_mem_tag;
1454 create_alias_map_for (tag, ai);
1460 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1461 every call site, we need to emit V_MAY_DEF expressions to represent the
1462 clobbering effects of the call for variables whose address escapes the
1463 current function.
1465 One approach is to group all call-clobbered variables into a single
1466 representative that is used as an alias of every call-clobbered variable
1467 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1468 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1470 The second approach is to emit a clobbering V_MAY_DEF for every
1471 call-clobbered variable at call sites. This is the preferred way in terms
1472 of optimization opportunities but it may create too many V_MAY_DEF operands
1473 if there are many call clobbered variables and function calls in the
1474 function.
1476 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1477 function calls found by the number of call-clobbered variables. If that
1478 product is beyond a certain threshold, as determined by the parameterized
1479 values shown below, we use .GLOBAL_VAR.
1481 FIXME. This heuristic should be improved. One idea is to use several
1482 .GLOBAL_VARs of different types instead of a single one. The thresholds
1483 have been derived from a typical bootstrap cycle, including all target
1484 libraries. Compile times were found increase by ~1% compared to using
1485 .GLOBAL_VAR. */
1487 static void
1488 maybe_create_global_var (struct alias_info *ai)
1490 size_t i, n_clobbered;
1492 /* No need to create it, if we have one already. */
1493 if (global_var == NULL_TREE)
1495 /* Count all the call-clobbered variables. */
1496 n_clobbered = 0;
1497 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, n_clobbered++);
1499 /* Create .GLOBAL_VAR if we have too many call-clobbered
1500 variables. We also create .GLOBAL_VAR when there no
1501 call-clobbered variables to prevent code motion
1502 transformations from re-arranging function calls that may
1503 have side effects. For instance,
1505 foo ()
1507 int a = f ();
1508 g ();
1509 h (a);
1512 There are no call-clobbered variables in foo(), so it would
1513 be entirely possible for a pass to want to move the call to
1514 f() after the call to g(). If f() has side effects, that
1515 would be wrong. Creating .GLOBAL_VAR in this case will
1516 insert VDEFs for it and prevent such transformations. */
1517 if (n_clobbered == 0
1518 || ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD)
1519 create_global_var ();
1522 /* If the function has calls to clobbering functions and .GLOBAL_VAR has
1523 been created, make it an alias for all call-clobbered variables. */
1524 if (global_var)
1525 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1527 tree var = referenced_var (i);
1528 if (var != global_var)
1530 add_may_alias (var, global_var);
1531 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1537 /* Return TRUE if pointer PTR may point to variable VAR.
1539 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1540 This is needed because when checking for type conflicts we are
1541 interested in the alias set of the memory location pointed-to by
1542 PTR. The alias set of PTR itself is irrelevant.
1544 VAR_ALIAS_SET is the alias set for VAR. */
1546 static bool
1547 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1548 tree var, HOST_WIDE_INT var_alias_set)
1550 tree mem;
1551 var_ann_t v_ann, m_ann;
1553 alias_stats.alias_queries++;
1554 alias_stats.simple_queries++;
1556 /* By convention, a variable cannot alias itself. */
1557 mem = var_ann (ptr)->type_mem_tag;
1558 if (mem == var)
1560 alias_stats.alias_noalias++;
1561 alias_stats.simple_resolved++;
1562 return false;
1565 v_ann = var_ann (var);
1566 m_ann = var_ann (mem);
1568 #if defined ENABLE_CHECKING
1569 if (m_ann->mem_tag_kind != TYPE_TAG)
1570 abort ();
1571 #endif
1573 alias_stats.tbaa_queries++;
1575 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1576 PTR can't possibly affect VAR. Note, that we are specifically testing
1577 for PTR's alias set here, not its pointed-to type. We also can't
1578 do this check with relaxed aliasing enabled. */
1579 if (POINTER_TYPE_P (TREE_TYPE (var))
1580 && var_alias_set != 0)
1582 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1583 if (ptr_alias_set == var_alias_set)
1585 alias_stats.alias_noalias++;
1586 alias_stats.tbaa_resolved++;
1587 return false;
1591 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1592 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1594 /* Handle aliases to structure fields. If either VAR or MEM are
1595 aggregate types, they may not have conflicting types, but one of
1596 the structures could contain a pointer to the other one.
1598 For instance, given
1600 MEM -> struct P *p;
1601 VAR -> struct Q *q;
1603 It may happen that '*p' and '*q' can't alias because 'struct P'
1604 and 'struct Q' have non-conflicting alias sets. However, it could
1605 happen that one of the fields in 'struct P' is a 'struct Q *' or
1606 vice-versa.
1608 Therefore, we also need to check if 'struct P' aliases 'struct Q *'
1609 or 'struct Q' aliases 'struct P *'. Notice, that since GIMPLE
1610 does not have more than one-level pointers, we don't need to
1611 recurse into the structures. */
1612 if (AGGREGATE_TYPE_P (TREE_TYPE (mem))
1613 || AGGREGATE_TYPE_P (TREE_TYPE (var)))
1615 tree ptr_to_var;
1617 if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
1618 ptr_to_var = TYPE_POINTER_TO (TREE_TYPE (TREE_TYPE (var)));
1619 else
1620 ptr_to_var = TYPE_POINTER_TO (TREE_TYPE (var));
1622 /* If no pointer-to VAR exists, then MEM can't alias VAR. */
1623 if (ptr_to_var == NULL_TREE)
1625 alias_stats.alias_noalias++;
1626 alias_stats.tbaa_resolved++;
1627 return false;
1630 /* If MEM doesn't alias a pointer to VAR and VAR doesn't alias
1631 PTR, then PTR can't alias VAR. */
1632 if (!alias_sets_conflict_p (mem_alias_set, get_alias_set (ptr_to_var))
1633 && !alias_sets_conflict_p (var_alias_set, get_alias_set (ptr)))
1635 alias_stats.alias_noalias++;
1636 alias_stats.tbaa_resolved++;
1637 return false;
1640 else
1642 alias_stats.alias_noalias++;
1643 alias_stats.tbaa_resolved++;
1644 return false;
1648 if (flag_tree_points_to != PTA_NONE)
1649 alias_stats.pta_queries++;
1651 /* If -ftree-points-to is given, check if PTR may point to VAR. */
1652 if (flag_tree_points_to == PTA_ANDERSEN
1653 && !ptr_may_alias_var (ptr, var))
1655 alias_stats.alias_noalias++;
1656 alias_stats.pta_resolved++;
1657 return false;
1660 alias_stats.alias_mayalias++;
1661 return true;
1665 /* Add ALIAS to the set of variables that may alias VAR. */
1667 static void
1668 add_may_alias (tree var, tree alias)
1670 size_t i;
1671 var_ann_t v_ann = get_var_ann (var);
1672 var_ann_t a_ann = get_var_ann (alias);
1674 #if defined ENABLE_CHECKING
1675 if (var == alias)
1676 abort ();
1677 #endif
1679 if (v_ann->may_aliases == NULL)
1680 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1682 /* Avoid adding duplicates. */
1683 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1684 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1685 return;
1687 /* If VAR is a call-clobbered variable, so is its new ALIAS. */
1688 if (is_call_clobbered (var))
1689 mark_call_clobbered (alias);
1691 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1692 else if (is_call_clobbered (alias))
1693 mark_call_clobbered (var);
1695 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1696 a_ann->is_alias_tag = 1;
1700 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1702 static void
1703 replace_may_alias (tree var, size_t i, tree new_alias)
1705 var_ann_t v_ann = var_ann (var);
1706 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1708 /* If VAR is a call-clobbered variable, so is NEW_ALIAS. */
1709 if (is_call_clobbered (var))
1710 mark_call_clobbered (new_alias);
1712 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1713 else if (is_call_clobbered (new_alias))
1714 mark_call_clobbered (var);
1718 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1720 static void
1721 set_pt_anything (tree ptr)
1723 struct ptr_info_def *pi = get_ptr_info (ptr);
1725 pi->pt_anything = 1;
1726 pi->pt_malloc = 0;
1727 pi->pt_vars = NULL;
1728 pi->is_dereferenced = 0;
1730 /* The pointer used to have a name tag, but we now found it pointing
1731 to an arbitrary location. The name tag needs to be renamed and
1732 disassociated from PTR. */
1733 if (pi->name_mem_tag)
1735 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
1736 pi->name_mem_tag = NULL_TREE;
1741 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1743 static void
1744 set_pt_malloc (tree ptr)
1746 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1748 /* If the pointer has already been found to point to arbitrary
1749 memory locations, it is unsafe to mark it as pointing to malloc. */
1750 if (pi->pt_anything)
1751 return;
1753 pi->pt_malloc = 1;
1757 /* Given two pointers DEST and ORIG. Merge the points-to information in
1758 ORIG into DEST. AI is as in collect_points_to_info. */
1760 static void
1761 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1763 struct ptr_info_def *dest_pi, *orig_pi;
1765 /* Make sure we have points-to information for ORIG. */
1766 collect_points_to_info_for (ai, orig);
1768 dest_pi = get_ptr_info (dest);
1769 orig_pi = SSA_NAME_PTR_INFO (orig);
1771 if (orig_pi)
1773 /* Notice that we never merge PT_MALLOC. This attribute is only
1774 true if the pointer is the result of a malloc() call.
1775 Otherwise, we can end up in this situation:
1777 P_i = malloc ();
1779 P_j = P_i + X;
1781 P_j would be marked as PT_MALLOC, which is wrong because
1782 PT_MALLOC implies that the pointer may not point to another
1783 variable.
1785 FIXME 1: Subsequent analysis may determine that P_j
1786 cannot alias anything else, but we are being conservative
1787 here.
1789 FIXME 2: If the merging comes from a copy assignment, we
1790 ought to merge PT_MALLOC, but then both pointers would end up
1791 getting different name tags because create_name_tags is not
1792 smart enough to determine that the two come from the same
1793 malloc call. Copy propagation before aliasing should cure
1794 this. */
1795 dest_pi->pt_malloc = 0;
1797 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1798 set_pt_anything (dest);
1800 if (!dest_pi->pt_anything
1801 && orig_pi->pt_vars
1802 && bitmap_first_set_bit (orig_pi->pt_vars) >= 0)
1804 if (dest_pi->pt_vars == NULL)
1806 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1807 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1809 else
1810 bitmap_a_or_b (dest_pi->pt_vars,
1811 dest_pi->pt_vars,
1812 orig_pi->pt_vars);
1818 /* Add VALUE to the list of expressions pointed-to by PTR. */
1820 static void
1821 add_pointed_to_expr (tree ptr, tree value)
1823 if (TREE_CODE (value) == WITH_SIZE_EXPR)
1824 value = TREE_OPERAND (value, 0);
1826 #if defined ENABLE_CHECKING
1827 /* Pointer variables should have been handled by merge_pointed_to_info. */
1828 if (TREE_CODE (value) == SSA_NAME
1829 && POINTER_TYPE_P (TREE_TYPE (value)))
1830 abort ();
1831 #endif
1833 get_ptr_info (ptr);
1835 /* If VALUE is the result of a malloc-like call, then the area pointed to
1836 PTR is guaranteed to not alias with anything else. */
1837 if (TREE_CODE (value) == CALL_EXPR
1838 && (call_expr_flags (value) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1839 set_pt_malloc (ptr);
1840 else
1841 set_pt_anything (ptr);
1843 if (dump_file)
1845 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1847 fprintf (dump_file, "Pointer ");
1848 print_generic_expr (dump_file, ptr, dump_flags);
1849 fprintf (dump_file, " points to ");
1850 if (pi->pt_malloc)
1851 fprintf (dump_file, "malloc space: ");
1852 else
1853 fprintf (dump_file, "an arbitrary address: ");
1854 print_generic_expr (dump_file, value, dump_flags);
1855 fprintf (dump_file, "\n");
1860 /* If VALUE is of the form &DECL, add DECL to the set of variables
1861 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1862 PTR. AI is as in collect_points_to_info. */
1864 static void
1865 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1867 struct ptr_info_def *pi = get_ptr_info (ptr);
1868 tree pt_var;
1869 size_t uid;
1871 #if defined ENABLE_CHECKING
1872 if (TREE_CODE (value) != ADDR_EXPR)
1873 abort ();
1874 #endif
1876 pt_var = TREE_OPERAND (value, 0);
1877 if (TREE_CODE_CLASS (TREE_CODE (pt_var)) == 'r')
1878 pt_var = get_base_address (pt_var);
1880 if (pt_var && SSA_VAR_P (pt_var))
1882 uid = var_ann (pt_var)->uid;
1883 bitmap_set_bit (ai->addresses_needed, uid);
1885 /* If PTR has already been found to point anywhere, don't
1886 add the variable to PTR's points-to set. */
1887 if (!pi->pt_anything)
1889 if (pi->pt_vars == NULL)
1890 pi->pt_vars = BITMAP_GGC_ALLOC ();
1891 bitmap_set_bit (pi->pt_vars, uid);
1897 /* Callback for walk_use_def_chains to gather points-to information from the
1898 SSA web.
1900 VAR is an SSA variable or a GIMPLE expression.
1902 STMT is the statement that generates the SSA variable or, if STMT is a
1903 PHI_NODE, VAR is one of the PHI arguments.
1905 DATA is a pointer to a structure of type ALIAS_INFO. */
1907 static bool
1908 collect_points_to_info_r (tree var, tree stmt, void *data)
1910 struct alias_info *ai = (struct alias_info *) data;
1912 if (dump_file && (dump_flags & TDF_DETAILS))
1914 fprintf (dump_file, "Visiting use-def links for ");
1915 print_generic_expr (dump_file, var, dump_flags);
1916 fprintf (dump_file, "\n");
1919 if (TREE_CODE (stmt) == MODIFY_EXPR)
1921 tree rhs = TREE_OPERAND (stmt, 1);
1922 STRIP_NOPS (rhs);
1924 /* Found P_i = ADDR_EXPR */
1925 if (TREE_CODE (rhs) == ADDR_EXPR)
1926 add_pointed_to_var (ai, var, rhs);
1928 /* Found P_i = Q_j. */
1929 else if (TREE_CODE (rhs) == SSA_NAME
1930 && POINTER_TYPE_P (TREE_TYPE (rhs)))
1931 merge_pointed_to_info (ai, var, rhs);
1933 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1934 else if (TREE_CODE (rhs) == PLUS_EXPR
1935 || TREE_CODE (rhs) == MINUS_EXPR)
1937 tree op0 = TREE_OPERAND (rhs, 0);
1938 tree op1 = TREE_OPERAND (rhs, 1);
1940 if (TREE_CODE (op0) == SSA_NAME
1941 && POINTER_TYPE_P (TREE_TYPE (op0)))
1942 merge_pointed_to_info (ai, var, op0);
1943 else if (TREE_CODE (op1) == SSA_NAME
1944 && POINTER_TYPE_P (TREE_TYPE (op1)))
1945 merge_pointed_to_info (ai, var, op1);
1946 else if (TREE_CODE (op0) == ADDR_EXPR)
1947 add_pointed_to_var (ai, var, op0);
1948 else if (TREE_CODE (op1) == ADDR_EXPR)
1949 add_pointed_to_var (ai, var, op1);
1950 else
1951 add_pointed_to_expr (var, rhs);
1954 /* Something else. */
1955 else
1956 add_pointed_to_expr (var, rhs);
1958 else if (TREE_CODE (stmt) == ASM_EXPR)
1960 /* Pointers defined by __asm__ statements can point anywhere. */
1961 set_pt_anything (var);
1963 else if (IS_EMPTY_STMT (stmt))
1965 tree decl = SSA_NAME_VAR (var);
1967 if (TREE_CODE (decl) == PARM_DECL)
1968 add_pointed_to_expr (var, decl);
1969 else if (DECL_INITIAL (decl))
1970 add_pointed_to_var (ai, var, DECL_INITIAL (decl));
1971 else
1972 add_pointed_to_expr (var, decl);
1974 else if (TREE_CODE (stmt) == PHI_NODE)
1976 /* It STMT is a PHI node, then VAR is one of its arguments. The
1977 variable that we are analyzing is the LHS of the PHI node. */
1978 tree lhs = PHI_RESULT (stmt);
1980 if (TREE_CODE (var) == ADDR_EXPR)
1981 add_pointed_to_var (ai, lhs, var);
1982 else if (TREE_CODE (var) == SSA_NAME)
1984 if (bitmap_bit_p (ai->ssa_names_visited, SSA_NAME_VERSION (var)))
1985 merge_pointed_to_info (ai, lhs, var);
1986 else
1987 set_pt_anything (lhs);
1989 else if (is_gimple_min_invariant (var))
1990 add_pointed_to_expr (lhs, var);
1991 else
1992 abort ();
1994 else
1995 abort ();
1997 return false;
2001 /* Return true if STMT is an "escape" site from the current function. Escape
2002 sites those statements which might expose the address of a variable
2003 outside the current function. STMT is an escape site iff:
2005 1- STMT is a function call, or
2006 2- STMT is an __asm__ expression, or
2007 3- STMT is an assignment to a non-local variable, or
2008 4- STMT is a return statement.
2010 If NUM_CALLS_P is not NULL, the counter is incremented if STMT contains
2011 a function call. */
2013 static bool
2014 is_escape_site (tree stmt, size_t *num_calls_p)
2016 if (get_call_expr_in (stmt) != NULL_TREE)
2018 if (num_calls_p)
2019 (*num_calls_p)++;
2021 return true;
2023 else if (TREE_CODE (stmt) == ASM_EXPR)
2024 return true;
2025 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2027 tree lhs = TREE_OPERAND (stmt, 0);
2029 /* Get to the base of _REF nodes. */
2030 if (TREE_CODE (lhs) != SSA_NAME)
2031 lhs = get_base_address (lhs);
2033 /* If we couldn't recognize the LHS of the assignment, assume that it
2034 is a non-local store. */
2035 if (lhs == NULL_TREE)
2036 return true;
2038 /* If the LHS is an SSA name, it can't possibly represent a non-local
2039 memory store. */
2040 if (TREE_CODE (lhs) == SSA_NAME)
2041 return false;
2043 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2044 local variables we cannot be sure if it will escape, because we
2045 don't have information about objects not in SSA form. Need to
2046 implement something along the lines of
2048 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2049 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2050 Conference on Object-Oriented Programming Systems, Languages, and
2051 Applications (OOPSLA), pp. 1-19, 1999. */
2052 return true;
2054 else if (TREE_CODE (stmt) == RETURN_EXPR)
2055 return true;
2057 return false;
2061 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2062 is considered to represent all the pointers whose pointed-to types are
2063 in the same alias set class. Otherwise, the tag represents a single
2064 SSA_NAME pointer variable. */
2066 static tree
2067 create_memory_tag (tree type, bool is_type_tag)
2069 var_ann_t ann;
2070 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2072 /* By default, memory tags are local variables. Alias analysis will
2073 determine whether they should be considered globals. */
2074 DECL_CONTEXT (tag) = current_function_decl;
2076 /* If the pointed-to type is volatile, so is the tag. */
2077 TREE_THIS_VOLATILE (tag) = TREE_THIS_VOLATILE (type);
2079 /* Memory tags are by definition addressable. This also prevents
2080 is_gimple_ref frome confusing memory tags with optimizable
2081 variables. */
2082 TREE_ADDRESSABLE (tag) = 1;
2084 ann = get_var_ann (tag);
2085 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2086 ann->type_mem_tag = NULL_TREE;
2088 /* Add the tag to the symbol table. */
2089 add_referenced_tmp_var (tag);
2091 return tag;
2095 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2096 This is used if P_i has been found to point to a specific set of
2097 variables or to a non-aliased memory location like the address returned
2098 by malloc functions. */
2100 static tree
2101 get_nmt_for (tree ptr)
2103 struct ptr_info_def *pi = get_ptr_info (ptr);
2104 tree tag = pi->name_mem_tag;
2106 if (tag == NULL_TREE)
2107 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2109 /* If PTR is a PARM_DECL, its memory tag should be considered a global
2110 variable. */
2111 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL)
2112 mark_call_clobbered (tag);
2114 /* Similarly, if PTR points to malloc, then TAG is a global. */
2115 if (pi->pt_malloc)
2116 mark_call_clobbered (tag);
2118 return tag;
2122 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2123 artificial variable that represents the memory location pointed-to by
2124 PTR. It is used to model the effects of pointer de-references on
2125 addressable variables.
2127 AI points to the data gathered during alias analysis. This function
2128 populates the array AI->POINTERS. */
2130 static tree
2131 get_tmt_for (tree ptr, struct alias_info *ai)
2133 size_t i;
2134 tree tag;
2135 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2136 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2138 /* To avoid creating unnecessary memory tags, only create one memory tag
2139 per alias set class. Note that it may be tempting to group
2140 memory tags based on conflicting alias sets instead of
2141 equivalence. That would be wrong because alias sets are not
2142 necessarily transitive (as demonstrated by the libstdc++ test
2143 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2144 such that conflicts (A, B) == true and conflicts (A, C) == true,
2145 it does not necessarily follow that conflicts (B, C) == true. */
2146 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2148 struct alias_map_d *curr = ai->pointers[i];
2149 if (tag_set == curr->set
2150 && (flag_tree_points_to == PTA_NONE
2151 || same_points_to_set (curr->var, ptr)))
2153 tag = var_ann (curr->var)->type_mem_tag;
2154 break;
2158 /* If VAR cannot alias with any of the existing memory tags, create a new
2159 tag for PTR and add it to the POINTERS array. */
2160 if (tag == NULL_TREE)
2162 struct alias_map_d *alias_map;
2164 /* If PTR did not have a type tag already, create a new TMT.*
2165 artificial variable representing the memory location
2166 pointed-to by PTR. */
2167 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2168 tag = create_memory_tag (tag_type, true);
2169 else
2170 tag = var_ann (ptr)->type_mem_tag;
2172 /* Add PTR to the POINTERS array. Note that we are not interested in
2173 PTR's alias set. Instead, we cache the alias set for the memory that
2174 PTR points to. */
2175 alias_map = xcalloc (1, sizeof (*alias_map));
2176 alias_map->var = ptr;
2177 alias_map->set = tag_set;
2178 ai->pointers[ai->num_pointers++] = alias_map;
2181 #if defined ENABLE_CHECKING
2182 /* Make sure that the type tag has the same alias set as the
2183 pointed-to type. */
2184 if (tag_set != get_alias_set (tag))
2185 abort ();
2186 #endif
2189 return tag;
2193 /* Create GLOBAL_VAR, an artificial global variable to act as a
2194 representative of all the variables that may be clobbered by function
2195 calls. */
2197 static void
2198 create_global_var (void)
2200 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2201 size_type_node);
2202 DECL_ARTIFICIAL (global_var) = 1;
2203 TREE_READONLY (global_var) = 0;
2204 DECL_EXTERNAL (global_var) = 1;
2205 TREE_STATIC (global_var) = 1;
2206 TREE_USED (global_var) = 1;
2207 DECL_CONTEXT (global_var) = NULL_TREE;
2208 TREE_THIS_VOLATILE (global_var) = 0;
2209 TREE_ADDRESSABLE (global_var) = 0;
2211 add_referenced_tmp_var (global_var);
2212 bitmap_set_bit (vars_to_rename, var_ann (global_var)->uid);
2216 /* Dump alias statistics on FILE. */
2218 static void
2219 dump_alias_stats (FILE *file)
2221 const char *funcname
2222 = lang_hooks.decl_printable_name (current_function_decl, 2);
2223 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2224 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2225 fprintf (file, "Total alias mayalias results:\t%u\n",
2226 alias_stats.alias_mayalias);
2227 fprintf (file, "Total alias noalias results:\t%u\n",
2228 alias_stats.alias_noalias);
2229 fprintf (file, "Total simple queries:\t%u\n",
2230 alias_stats.simple_queries);
2231 fprintf (file, "Total simple resolved:\t%u\n",
2232 alias_stats.simple_resolved);
2233 fprintf (file, "Total TBAA queries:\t%u\n",
2234 alias_stats.tbaa_queries);
2235 fprintf (file, "Total TBAA resolved:\t%u\n",
2236 alias_stats.tbaa_resolved);
2237 fprintf (file, "Total PTA queries:\t%u\n",
2238 alias_stats.pta_queries);
2239 fprintf (file, "Total PTA resolved:\t%u\n",
2240 alias_stats.pta_resolved);
2244 /* Dump alias information on FILE. */
2246 void
2247 dump_alias_info (FILE *file)
2249 size_t i;
2250 const char *funcname
2251 = lang_hooks.decl_printable_name (current_function_decl, 2);
2253 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2255 fprintf (file, "Aliased symbols\n\n");
2256 for (i = 0; i < num_referenced_vars; i++)
2258 tree var = referenced_var (i);
2259 if (may_be_aliased (var))
2260 dump_variable (file, var);
2263 fprintf (file, "\nDereferenced pointers\n\n");
2264 for (i = 0; i < num_referenced_vars; i++)
2266 tree var = referenced_var (i);
2267 var_ann_t ann = var_ann (var);
2268 if (ann->type_mem_tag)
2269 dump_variable (file, var);
2272 fprintf (file, "\nType memory tags\n\n");
2273 for (i = 0; i < num_referenced_vars; i++)
2275 tree var = referenced_var (i);
2276 var_ann_t ann = var_ann (var);
2277 if (ann->mem_tag_kind == TYPE_TAG)
2278 dump_variable (file, var);
2281 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2283 fprintf (file, "SSA_NAME pointers\n\n");
2284 for (i = 1; i < num_ssa_names; i++)
2286 tree ptr = ssa_name (i);
2287 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2288 if (!SSA_NAME_IN_FREE_LIST (ptr)
2289 && pi
2290 && pi->name_mem_tag)
2291 dump_points_to_info_for (file, ptr);
2294 fprintf (file, "\nName memory tags\n\n");
2295 for (i = 0; i < num_referenced_vars; i++)
2297 tree var = referenced_var (i);
2298 var_ann_t ann = var_ann (var);
2299 if (ann->mem_tag_kind == NAME_TAG)
2300 dump_variable (file, var);
2303 fprintf (file, "\n");
2307 /* Dump alias information on stderr. */
2309 void
2310 debug_alias_info (void)
2312 dump_alias_info (stderr);
2316 /* Return the alias information associated with pointer T. It creates a
2317 new instance if none existed. */
2319 static struct ptr_info_def *
2320 get_ptr_info (tree t)
2322 struct ptr_info_def *pi;
2324 #if defined ENABLE_CHECKING
2325 if (!POINTER_TYPE_P (TREE_TYPE (t)))
2326 abort ();
2327 #endif
2329 pi = SSA_NAME_PTR_INFO (t);
2330 if (pi == NULL)
2332 pi = ggc_alloc (sizeof (*pi));
2333 memset ((void *)pi, 0, sizeof (*pi));
2334 SSA_NAME_PTR_INFO (t) = pi;
2337 return pi;
2341 /* Dump points-to information for SSA_NAME PTR into FILE. */
2343 void
2344 dump_points_to_info_for (FILE *file, tree ptr)
2346 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2348 print_generic_expr (file, ptr, dump_flags);
2350 if (pi)
2352 if (pi->name_mem_tag)
2354 fprintf (file, ", name memory tag: ");
2355 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2358 if (pi->is_dereferenced)
2359 fprintf (file, ", is dereferenced");
2361 if (pi->value_escapes_p)
2362 fprintf (file, ", its value escapes");
2364 if (pi->pt_anything)
2365 fprintf (file, ", points-to anything");
2367 if (pi->pt_malloc)
2368 fprintf (file, ", points-to malloc");
2370 if (pi->pt_vars)
2372 unsigned ix;
2374 fprintf (file, ", points-to vars: { ");
2375 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix,
2377 print_generic_expr (file, referenced_var (ix), dump_flags);
2378 fprintf (file, " ");
2380 fprintf (file, "}");
2384 fprintf (file, "\n");
2388 /* Dump points-to information for VAR into stderr. */
2390 void
2391 debug_points_to_info_for (tree var)
2393 dump_points_to_info_for (stderr, var);
2397 /* Dump points-to information into FILE. NOTE: This function is slow, as
2398 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2400 void
2401 dump_points_to_info (FILE *file)
2403 basic_block bb;
2404 block_stmt_iterator si;
2405 size_t i;
2406 const char *fname =
2407 lang_hooks.decl_printable_name (current_function_decl, 2);
2409 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2411 /* First dump points-to information for the default definitions of
2412 pointer variables. This is necessary because default definitions are
2413 not part of the code. */
2414 for (i = 0; i < num_referenced_vars; i++)
2416 tree var = referenced_var (i);
2417 if (POINTER_TYPE_P (TREE_TYPE (var)))
2419 var_ann_t ann = var_ann (var);
2420 if (ann->default_def)
2421 dump_points_to_info_for (file, ann->default_def);
2425 /* Dump points-to information for every pointer defined in the program. */
2426 FOR_EACH_BB (bb)
2428 tree phi;
2430 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2432 tree ptr = PHI_RESULT (phi);
2433 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2434 dump_points_to_info_for (file, ptr);
2437 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2439 stmt_ann_t ann = stmt_ann (bsi_stmt (si));
2440 def_optype defs = DEF_OPS (ann);
2441 if (defs)
2442 for (i = 0; i < NUM_DEFS (defs); i++)
2443 if (POINTER_TYPE_P (TREE_TYPE (DEF_OP (defs, i))))
2444 dump_points_to_info_for (file, DEF_OP (defs, i));
2448 fprintf (file, "\n");
2452 /* Dump points-to info pointed by PTO into STDERR. */
2454 void
2455 debug_points_to_info (void)
2457 dump_points_to_info (stderr);
2460 /* Dump to FILE the list of variables that may be aliasing VAR. */
2462 void
2463 dump_may_aliases_for (FILE *file, tree var)
2465 varray_type aliases;
2467 if (TREE_CODE (var) == SSA_NAME)
2468 var = SSA_NAME_VAR (var);
2470 aliases = var_ann (var)->may_aliases;
2471 if (aliases)
2473 size_t i;
2474 fprintf (file, "{ ");
2475 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2477 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2478 fprintf (file, " ");
2480 fprintf (file, "}");
2485 /* Dump to stderr the list of variables that may be aliasing VAR. */
2487 void
2488 debug_may_aliases_for (tree var)
2490 dump_may_aliases_for (stderr, var);
2493 /* Return true if VAR may be aliased. */
2495 bool
2496 may_be_aliased (tree var)
2498 /* Obviously. */
2499 if (TREE_ADDRESSABLE (var))
2500 return true;
2502 /* Automatic variables can't have their addresses escape any other way. */
2503 if (!TREE_STATIC (var))
2504 return false;
2506 /* Globally visible variables can have their addresses taken by other
2507 translation units. */
2508 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2509 return true;
2511 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2512 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2513 we can only be sure the variable isn't addressable if it's local to the
2514 current function. */
2515 if (flag_unit_at_a_time)
2516 return false;
2517 if (decl_function_context (var) == current_function_decl)
2518 return false;
2520 return true;