* doc/contrib.texi (Contributors): Add gfortran contributors and
[official-gcc.git] / gcc / tree-ssa-alias.c
blobcc0a00a559b568e66ac39e560a86bdbfd6a7e254
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 struct alias_info *init_alias_info (void);
145 static void delete_alias_info (struct alias_info *);
146 static void compute_points_to_and_addr_escape (struct alias_info *);
147 static void compute_flow_sensitive_aliasing (struct alias_info *);
148 static void setup_pointers_and_addressables (struct alias_info *);
149 static bool collect_points_to_info_r (tree, tree, void *);
150 static bool is_escape_site (tree, size_t *);
151 static void add_pointed_to_var (struct alias_info *, tree, tree);
152 static void add_pointed_to_expr (tree, tree);
153 static void create_global_var (void);
154 static void collect_points_to_info_for (struct alias_info *, tree);
155 static bool ptr_is_dereferenced_by (tree, tree, bool *);
156 static void maybe_create_global_var (struct alias_info *ai);
157 static void group_aliases (struct alias_info *);
158 static struct ptr_info_def *get_ptr_info (tree t);
160 /* Global declarations. */
162 /* Call clobbered variables in the function. If bit I is set, then
163 REFERENCED_VARS (I) is call-clobbered. */
164 bitmap call_clobbered_vars;
166 /* Addressable variables in the function. If bit I is set, then
167 REFERENCED_VARS (I) has had its address taken. */
168 bitmap addressable_vars;
170 /* 'true' after aliases have been computed (see compute_may_aliases). This
171 is used by get_stmt_operands and its helpers to determine what to do
172 when scanning an operand for a variable that may be aliased. If
173 may-alias information is still not available, the statement is marked as
174 having volatile operands. */
175 bool aliases_computed_p;
177 /* When the program has too many call-clobbered variables and call-sites,
178 this variable is used to represent the clobbering effects of function
179 calls. In these cases, all the call clobbered variables in the program
180 are forced to alias this variable. This reduces compile times by not
181 having to keep track of too many V_MAY_DEF expressions at call sites. */
182 tree global_var;
185 /* Compute may-alias information for every variable referenced in function
186 FNDECL.
188 Alias analysis proceeds in 3 main phases:
190 1- Points-to and escape analysis.
192 This phase walks the use-def chains in the SSA web looking for three
193 things:
195 * Assignments of the form P_i = &VAR
196 * Assignments of the form P_i = malloc()
197 * Pointers and ADDR_EXPR that escape the current function.
199 The concept of 'escaping' is the same one used in the Java world. When
200 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
201 outside of the current function. So, assignment to global variables,
202 function arguments and returning a pointer are all escape sites.
204 This is where we are currently limited. Since not everything is renamed
205 into SSA, we lose track of escape properties when a pointer is stashed
206 inside a field in a structure, for instance. In those cases, we are
207 assuming that the pointer does escape.
209 We use escape analysis to determine whether a variable is
210 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
211 is call-clobbered. If a pointer P_i escapes, then all the variables
212 pointed-to by P_i (and its memory tag) also escape.
214 2- Compute flow-sensitive aliases
216 We have two classes of memory tags. Memory tags associated with the
217 pointed-to data type of the pointers in the program. These tags are
218 called "type memory tag" (TMT). The other class are those associated
219 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
220 when adding operands for an INDIRECT_REF *P_i, we will first check
221 whether P_i has a name tag, if it does we use it, because that will have
222 more precise aliasing information. Otherwise, we use the standard type
223 tag.
225 In this phase, we go through all the pointers we found in points-to
226 analysis and create alias sets for the name memory tags associated with
227 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
228 it points to and its tag.
231 3- Compute flow-insensitive aliases
233 This pass will compare the alias set of every type memory tag and every
234 addressable variable found in the program. Given a type memory tag TMT
235 and an addressable variable V. If the alias sets of TMT and V conflict
236 (as computed by may_alias_p), then V is marked as an alias tag and added
237 to the alias set of TMT.
239 For instance, consider the following function:
241 foo (int i)
243 int *p, *q, a, b;
245 if (i > 10)
246 p = &a;
247 else
248 q = &b;
250 *p = 3;
251 *q = 5;
252 a = b + 2;
253 return *p;
256 After aliasing analysis has finished, the type memory tag for pointer
257 'p' will have two aliases, namely variables 'a' and 'b'. Every time
258 pointer 'p' is dereferenced, we want to mark the operation as a
259 potential reference to 'a' and 'b'.
261 foo (int i)
263 int *p, a, b;
265 if (i_2 > 10)
266 p_4 = &a;
267 else
268 p_6 = &b;
269 # p_1 = PHI <p_4(1), p_6(2)>;
271 # a_7 = V_MAY_DEF <a_3>;
272 # b_8 = V_MAY_DEF <b_5>;
273 *p_1 = 3;
275 # a_9 = V_MAY_DEF <a_7>
276 # VUSE <b_8>
277 a_9 = b_8 + 2;
279 # VUSE <a_9>;
280 # VUSE <b_8>;
281 return *p_1;
284 In certain cases, the list of may aliases for a pointer may grow too
285 large. This may cause an explosion in the number of virtual operands
286 inserted in the code. Resulting in increased memory consumption and
287 compilation time.
289 When the number of virtual operands needed to represent aliased
290 loads and stores grows too large (configurable with @option{--param
291 max-aliased-vops}), alias sets are grouped to avoid severe
292 compile-time slow downs and memory consumption. See group_aliases. */
294 static void
295 compute_may_aliases (void)
297 struct alias_info *ai;
299 memset (&alias_stats, 0, sizeof (alias_stats));
301 /* Initialize aliasing information. */
302 ai = init_alias_info ();
304 /* For each pointer P_i, determine the sets of variables that P_i may
305 point-to. For every addressable variable V, determine whether the
306 address of V escapes the current function, making V call-clobbered
307 (i.e., whether &V is stored in a global variable or if its passed as a
308 function call argument). */
309 compute_points_to_and_addr_escape (ai);
311 /* Collect all pointers and addressable variables, compute alias sets,
312 create memory tags for pointers and promote variables whose address is
313 not needed anymore. */
314 setup_pointers_and_addressables (ai);
316 /* Compute flow-sensitive, points-to based aliasing for all the name
317 memory tags. Note that this pass needs to be done before flow
318 insensitive analysis because it uses the points-to information
319 gathered before to mark call-clobbered type tags. */
320 compute_flow_sensitive_aliasing (ai);
322 /* Compute type-based flow-insensitive aliasing for all the type
323 memory tags. */
324 compute_flow_insensitive_aliasing (ai);
326 /* If the program has too many call-clobbered variables and/or function
327 calls, create .GLOBAL_VAR and use it to model call-clobbering
328 semantics at call sites. This reduces the number of virtual operands
329 considerably, improving compile times at the expense of lost
330 aliasing precision. */
331 maybe_create_global_var (ai);
333 /* Debugging dumps. */
334 if (dump_file)
336 dump_referenced_vars (dump_file);
337 if (dump_flags & TDF_STATS)
338 dump_alias_stats (dump_file);
339 dump_points_to_info (dump_file);
340 dump_alias_info (dump_file);
343 /* Deallocate memory used by aliasing data structures. */
344 delete_alias_info (ai);
346 /* Indicate that may-alias information is now available. */
347 aliases_computed_p = true;
350 struct tree_opt_pass pass_may_alias =
352 "alias", /* name */
353 NULL, /* gate */
354 compute_may_aliases, /* execute */
355 NULL, /* sub */
356 NULL, /* next */
357 0, /* static_pass_number */
358 TV_TREE_MAY_ALIAS, /* tv_id */
359 PROP_cfg | PROP_ssa | PROP_pta, /* properties_required */
360 0, /* properties_provided */
361 0, /* properties_destroyed */
362 0, /* todo_flags_start */
363 TODO_dump_func | TODO_rename_vars
364 | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
368 /* Initialize the data structures used for alias analysis. */
370 static struct alias_info *
371 init_alias_info (void)
373 struct alias_info *ai;
375 ai = xcalloc (1, sizeof (struct alias_info));
376 ai->ssa_names_visited = BITMAP_XMALLOC ();
377 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
378 ai->addresses_needed = BITMAP_XMALLOC ();
379 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
380 ai->written_vars = BITMAP_XMALLOC ();
381 ai->dereferenced_ptrs_store = BITMAP_XMALLOC ();
382 ai->dereferenced_ptrs_load = BITMAP_XMALLOC ();
384 return ai;
388 /* Deallocate memory used by alias analysis. */
390 static void
391 delete_alias_info (struct alias_info *ai)
393 size_t i;
395 BITMAP_XFREE (ai->ssa_names_visited);
396 ai->processed_ptrs = NULL;
397 BITMAP_XFREE (ai->addresses_needed);
399 for (i = 0; i < ai->num_addressable_vars; i++)
401 sbitmap_free (ai->addressable_vars[i]->may_aliases);
402 free (ai->addressable_vars[i]);
404 free (ai->addressable_vars);
406 for (i = 0; i < ai->num_pointers; i++)
408 sbitmap_free (ai->pointers[i]->may_aliases);
409 free (ai->pointers[i]);
411 free (ai->pointers);
413 ai->num_references = NULL;
414 BITMAP_XFREE (ai->written_vars);
415 BITMAP_XFREE (ai->dereferenced_ptrs_store);
416 BITMAP_XFREE (ai->dereferenced_ptrs_load);
418 free (ai);
422 /* Walk use-def chains for pointer PTR to determine what variables is PTR
423 pointing to. */
425 static void
426 collect_points_to_info_for (struct alias_info *ai, tree ptr)
428 #if defined ENABLE_CHECKING
429 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
430 abort ();
431 #endif
433 if (!bitmap_bit_p (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
435 bitmap_set_bit (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
436 walk_use_def_chains (ptr, collect_points_to_info_r, ai);
437 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
442 /* Helper for ptr_is_dereferenced_by. Called by walk_tree to look for
443 INDIRECT_REF nodes for the pointer passed in DATA. */
445 static tree
446 find_ptr_dereference (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
448 tree ptr = (tree) data;
450 if (TREE_CODE (*tp) == INDIRECT_REF
451 && TREE_OPERAND (*tp, 0) == ptr)
452 return *tp;
454 return NULL_TREE;
458 /* Return true if STMT contains INDIRECT_REF <PTR>. *IS_STORE is set
459 to 'true' if the dereference is on the LHS of an assignment. */
461 static bool
462 ptr_is_dereferenced_by (tree ptr, tree stmt, bool *is_store)
464 *is_store = false;
466 if (TREE_CODE (stmt) == MODIFY_EXPR
467 || (TREE_CODE (stmt) == RETURN_EXPR
468 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR))
470 tree e, lhs, rhs;
472 e = (TREE_CODE (stmt) == RETURN_EXPR) ? TREE_OPERAND (stmt, 0) : stmt;
473 lhs = TREE_OPERAND (e, 0);
474 rhs = TREE_OPERAND (e, 1);
476 if (EXPR_P (lhs)
477 && walk_tree (&lhs, find_ptr_dereference, ptr, NULL))
479 *is_store = true;
480 return true;
482 else if (EXPR_P (rhs)
483 && walk_tree (&rhs, find_ptr_dereference, ptr, NULL))
485 return true;
488 else if (TREE_CODE (stmt) == ASM_EXPR)
490 if (walk_tree (&ASM_OUTPUTS (stmt), find_ptr_dereference, ptr, NULL)
491 || walk_tree (&ASM_CLOBBERS (stmt), find_ptr_dereference, ptr, NULL))
493 *is_store = true;
494 return true;
496 else if (walk_tree (&ASM_INPUTS (stmt), find_ptr_dereference, ptr, NULL))
498 return true;
502 return false;
506 /* Traverse use-def links for all the pointers in the program to collect
507 address escape and points-to information.
509 This is loosely based on the same idea described in R. Hasti and S.
510 Horwitz, ``Using static single assignment form to improve
511 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
512 Programming Language Design and Implementation, pp. 97-105, 1998. */
514 static void
515 compute_points_to_and_addr_escape (struct alias_info *ai)
517 basic_block bb;
518 size_t i;
520 timevar_push (TV_TREE_PTA);
522 FOR_EACH_BB (bb)
524 bb_ann_t block_ann = bb_ann (bb);
525 block_stmt_iterator si;
527 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
529 use_optype uses;
530 def_optype defs;
531 v_may_def_optype v_may_defs;
532 v_must_def_optype v_must_defs;
533 stmt_ann_t ann;
534 bitmap addr_taken;
535 tree stmt = bsi_stmt (si);
536 bool stmt_escapes_p = is_escape_site (stmt, &ai->num_calls_found);
538 /* Mark all the variables whose address are taken by the
539 statement. Note that this will miss all the addresses taken
540 in PHI nodes (those are discovered while following the use-def
541 chains). */
542 get_stmt_operands (stmt);
543 addr_taken = addresses_taken (stmt);
544 if (addr_taken)
545 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i,
547 tree var = referenced_var (i);
548 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
549 if (stmt_escapes_p)
550 mark_call_clobbered (var);
553 if (stmt_escapes_p)
554 block_ann->has_escape_site = 1;
556 /* Special case for silly ADDR_EXPR tricks
557 (gcc.c-torture/unsorted/pass.c). If this statement is an
558 assignment to a non-pointer variable and the RHS takes the
559 address of a variable, assume that the variable on the RHS is
560 call-clobbered. We could add the LHS to the list of
561 "pointers" and follow it to see if it really escapes, but it's
562 not worth the pain. */
563 if (addr_taken
564 && TREE_CODE (stmt) == MODIFY_EXPR
565 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 0))))
566 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i,
568 tree var = referenced_var (i);
569 mark_call_clobbered (var);
572 ann = stmt_ann (stmt);
573 uses = USE_OPS (ann);
574 for (i = 0; i < NUM_USES (uses); i++)
576 tree op = USE_OP (uses, i);
577 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
578 struct ptr_info_def *pi;
579 bool is_store;
581 /* If the operand's variable may be aliased, keep track
582 of how many times we've referenced it. This is used
583 for alias grouping in compute_flow_sensitive_aliasing.
584 Note that we don't need to grow AI->NUM_REFERENCES
585 because we are processing regular variables, not
586 memory tags (the array's initial size is set to
587 NUM_REFERENCED_VARS). */
588 if (may_be_aliased (SSA_NAME_VAR (op)))
589 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
591 if (!POINTER_TYPE_P (TREE_TYPE (op)))
592 continue;
594 collect_points_to_info_for (ai, op);
596 pi = SSA_NAME_PTR_INFO (op);
597 if (ptr_is_dereferenced_by (op, stmt, &is_store))
599 /* If we found OP to point to a set of variables or
600 malloc, then mark it as being dereferenced. In a
601 subsequent pass, dereferenced pointers that point
602 to a set of variables will be assigned a name tag
603 to alias all the variables OP points to. */
604 if (pi->pt_malloc || pi->pt_vars)
605 pi->is_dereferenced = 1;
607 /* Keep track of how many time we've dereferenced each
608 pointer. Again, we don't need to grow
609 AI->NUM_REFERENCES because we're processing
610 existing program variables. */
611 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
613 /* If this is a store operation, mark OP as being
614 dereferenced to store, otherwise mark it as being
615 dereferenced to load. */
616 if (is_store)
617 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
618 else
619 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
621 else if (stmt_escapes_p)
623 /* Note that even if STMT is an escape point, pointer OP
624 will not escape if it is being dereferenced. That's
625 why we only check for escape points if OP is not
626 dereferenced by STMT. */
627 pi->value_escapes_p = 1;
629 /* If the statement makes a function call, assume
630 that pointer OP will be dereferenced in a store
631 operation inside the called function. */
632 if (get_call_expr_in (stmt))
633 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
637 /* Update reference counter for definitions to any
638 potentially aliased variable. This is used in the alias
639 grouping heuristics. */
640 defs = DEF_OPS (ann);
641 for (i = 0; i < NUM_DEFS (defs); i++)
643 tree op = DEF_OP (defs, i);
644 tree var = SSA_NAME_VAR (op);
645 var_ann_t ann = var_ann (var);
646 bitmap_set_bit (ai->written_vars, ann->uid);
647 if (may_be_aliased (var))
648 (VARRAY_UINT (ai->num_references, ann->uid))++;
651 /* Mark variables in V_MAY_DEF operands as being written to. */
652 v_may_defs = V_MAY_DEF_OPS (ann);
653 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
655 tree op = V_MAY_DEF_OP (v_may_defs, i);
656 tree var = SSA_NAME_VAR (op);
657 var_ann_t ann = var_ann (var);
658 bitmap_set_bit (ai->written_vars, ann->uid);
661 /* Mark variables in V_MUST_DEF operands as being written to. */
662 v_must_defs = V_MUST_DEF_OPS (ann);
663 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
665 tree op = V_MUST_DEF_OP (v_must_defs, i);
666 tree var = SSA_NAME_VAR (op);
667 var_ann_t ann = var_ann (var);
668 bitmap_set_bit (ai->written_vars, ann->uid);
671 /* After promoting variables and computing aliasing we will
672 need to re-scan most statements. FIXME: Try to minimize the
673 number of statements re-scanned. It's not really necessary to
674 re-scan *all* statements. */
675 modify_stmt (stmt);
679 timevar_pop (TV_TREE_PTA);
683 /* Create name tags for all the pointers that have been dereferenced.
684 We only create a name tag for a pointer P if P is found to point to
685 a set of variables (so that we can alias them to *P) or if it is
686 the result of a call to malloc (which means that P cannot point to
687 anything else nor alias any other variable).
689 If two pointers P and Q point to the same set of variables, they
690 are assigned the same name tag. */
692 static void
693 create_name_tags (struct alias_info *ai)
695 size_t i;
697 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
699 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
700 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
702 /* If we could not determine where PTR was pointing to, clear
703 all the other points-to flags. */
704 pi = SSA_NAME_PTR_INFO (ptr);
705 if (pi->pt_anything)
707 pi->pt_malloc = 0;
708 pi->pt_vars = NULL;
709 pi->is_dereferenced = 0;
712 if (!pi->is_dereferenced)
713 continue;
715 if (pi->pt_vars)
717 size_t j;
719 /* If PTR points to a set of variables, check if we don't
720 have another pointer Q with the same points-to set before
721 creating a tag. If so, use Q's tag instead of creating a
722 new one.
724 This is important for not creating unnecessary symbols
725 and also for copy propagation. If we ever need to
726 propagate PTR into Q or vice-versa, we would run into
727 problems if they both had different name tags because
728 they would have different SSA version numbers (which
729 would force us to take the name tags in and out of SSA). */
730 pi->name_mem_tag = NULL_TREE;
731 for (j = 0; j < i; j++)
733 tree q = VARRAY_TREE (ai->processed_ptrs, j);
734 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
736 if (qi
737 && qi->pt_vars
738 && qi->name_mem_tag
739 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
741 pi->name_mem_tag = qi->name_mem_tag;
742 break;
746 if (pi->name_mem_tag == NULL_TREE)
747 pi->name_mem_tag = get_nmt_for (ptr);
749 else if (pi->pt_malloc)
751 /* Otherwise, create a unique name tag for this pointer. */
752 pi->name_mem_tag = get_nmt_for (ptr);
754 else
756 /* Only pointers that may point to malloc or other variables
757 may receive a name tag. If the pointer does not point to
758 a known spot, we should use type tags. */
759 abort ();
762 /* Mark the new name tag for renaming. */
763 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
769 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
770 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
771 name tag and the variables it points-to are call-clobbered. Finally, if
772 P_i escapes and we could not determine where it points to, then all the
773 variables in the same alias set as *P_i are marked call-clobbered. This
774 is necessary because we must assume that P_i may take the address of any
775 variable in the same alias set. */
777 static void
778 compute_flow_sensitive_aliasing (struct alias_info *ai)
780 size_t i;
782 create_name_tags (ai);
784 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
786 size_t j;
787 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
788 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
789 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
791 if (pi->value_escapes_p || pi->pt_anything)
793 /* If PTR escapes or may point to anything, then its associated
794 memory tags are call-clobbered. */
795 if (pi->name_mem_tag)
796 mark_call_clobbered (pi->name_mem_tag);
798 if (v_ann->type_mem_tag)
799 mark_call_clobbered (v_ann->type_mem_tag);
801 /* If PTR may point to anything, mark call-clobbered all the
802 addressables with the same alias set as the type pointed-to by
803 PTR. */
804 if (pi->pt_anything)
806 HOST_WIDE_INT ptr_set;
807 ptr_set = get_alias_set (TREE_TYPE (TREE_TYPE (ptr)));
808 for (j = 0; j < ai->num_addressable_vars; j++)
810 struct alias_map_d *alias_map = ai->addressable_vars[j];
811 if (alias_map->set == ptr_set)
812 mark_call_clobbered (alias_map->var);
816 /* If PTR's value may escape and PTR is never dereferenced, we
817 need to mark all the variables PTR points-to as
818 call-clobbered. Note that we only need do this it PTR is
819 never dereferenced. If PTR is dereferenced, it will have a
820 name memory tag, which will have been marked call-clobbered.
821 This will in turn mark the pointed-to variables as
822 call-clobbered when we call add_may_alias below. */
823 if (pi->value_escapes_p
824 && pi->name_mem_tag == NULL_TREE
825 && pi->pt_vars)
826 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j,
827 mark_call_clobbered (referenced_var (j)));
830 /* Set up aliasing information for PTR's name memory tag (if it has
831 one). Note that only pointers that have been dereferenced will
832 have a name memory tag. */
833 if (pi->name_mem_tag && pi->pt_vars)
834 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j,
835 add_may_alias (pi->name_mem_tag, referenced_var (j)));
837 /* If the name tag is call clobbered, so is the type tag
838 associated with the base VAR_DECL. */
839 if (pi->name_mem_tag
840 && v_ann->type_mem_tag
841 && is_call_clobbered (pi->name_mem_tag))
842 mark_call_clobbered (v_ann->type_mem_tag);
847 /* Compute type-based alias sets. Traverse all the pointers and
848 addressable variables found in setup_pointers_and_addressables.
850 For every pointer P in AI->POINTERS and addressable variable V in
851 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
852 memory tag (TMT) if their alias sets conflict. V is then marked as
853 an alias tag so that the operand scanner knows that statements
854 containing V have aliased operands. */
856 static void
857 compute_flow_insensitive_aliasing (struct alias_info *ai)
859 size_t i;
861 /* Initialize counter for the total number of virtual operands that
862 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
863 threshold set by --params max-alias-vops, we enable alias
864 grouping. */
865 ai->total_alias_vops = 0;
867 /* For every pointer P, determine which addressable variables may alias
868 with P's type memory tag. */
869 for (i = 0; i < ai->num_pointers; i++)
871 size_t j;
872 struct alias_map_d *p_map = ai->pointers[i];
873 tree tag = var_ann (p_map->var)->type_mem_tag;
874 var_ann_t tag_ann = var_ann (tag);
876 p_map->total_alias_vops = 0;
877 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
878 sbitmap_zero (p_map->may_aliases);
880 for (j = 0; j < ai->num_addressable_vars; j++)
882 struct alias_map_d *v_map;
883 var_ann_t v_ann;
884 tree var;
885 bool tag_stored_p, var_stored_p;
887 v_map = ai->addressable_vars[j];
888 var = v_map->var;
889 v_ann = var_ann (var);
891 /* Skip memory tags and variables that have never been
892 written to. We also need to check if the variables are
893 call-clobbered because they may be overwritten by
894 function calls. */
895 tag_stored_p = bitmap_bit_p (ai->written_vars, tag_ann->uid)
896 || is_call_clobbered (tag);
897 var_stored_p = bitmap_bit_p (ai->written_vars, v_ann->uid)
898 || is_call_clobbered (var);
899 if (!tag_stored_p && !var_stored_p)
900 continue;
902 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
904 size_t num_tag_refs, num_var_refs;
906 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
907 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
909 /* Add VAR to TAG's may-aliases set. */
910 add_may_alias (tag, var);
912 /* Update the total number of virtual operands due to
913 aliasing. Since we are adding one more alias to TAG's
914 may-aliases set, the total number of virtual operands due
915 to aliasing will be increased by the number of references
916 made to VAR and TAG (every reference to TAG will also
917 count as a reference to VAR). */
918 ai->total_alias_vops += (num_var_refs + num_tag_refs);
919 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
921 /* Update the bitmap used to represent TAG's alias set
922 in case we need to group aliases. */
923 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
928 if (dump_file)
929 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
930 get_name (current_function_decl),
931 ai->total_alias_vops);
933 /* Determine if we need to enable alias grouping. */
934 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
935 group_aliases (ai);
939 /* Comparison function for qsort used in group_aliases. */
941 static int
942 total_alias_vops_cmp (const void *p, const void *q)
944 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
945 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
946 long n1 = (*p1)->total_alias_vops;
947 long n2 = (*p2)->total_alias_vops;
949 /* We want to sort in descending order. */
950 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
953 /* Group all the aliases for TAG to make TAG represent all the
954 variables in its alias set. Update the total number
955 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
956 function will make TAG be the unique alias tag for all the
957 variables in its may-aliases. So, given:
959 may-aliases(TAG) = { V1, V2, V3 }
961 This function will group the variables into:
963 may-aliases(V1) = { TAG }
964 may-aliases(V2) = { TAG }
965 may-aliases(V2) = { TAG } */
967 static void
968 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
970 size_t i;
971 var_ann_t tag_ann = var_ann (tag);
972 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
974 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i,
976 tree var = referenced_var (i);
977 var_ann_t ann = var_ann (var);
979 /* Make TAG the unique alias of VAR. */
980 ann->is_alias_tag = 0;
981 ann->may_aliases = NULL;
983 /* Note that VAR and TAG may be the same if the function has no
984 addressable variables (see the discussion at the end of
985 setup_pointers_and_addressables). */
986 if (var != tag)
987 add_may_alias (var, tag);
989 /* Reduce total number of virtual operands contributed
990 by TAG on behalf of VAR. Notice that the references to VAR
991 itself won't be removed. We will merely replace them with
992 references to TAG. */
993 ai->total_alias_vops -= num_tag_refs;
996 /* We have reduced the number of virtual operands that TAG makes on
997 behalf of all the variables formerly aliased with it. However,
998 we have also "removed" all the virtual operands for TAG itself,
999 so we add them back. */
1000 ai->total_alias_vops += num_tag_refs;
1002 /* TAG no longer has any aliases. */
1003 tag_ann->may_aliases = NULL;
1007 /* Group may-aliases sets to reduce the number of virtual operands due
1008 to aliasing.
1010 1- Sort the list of pointers in decreasing number of contributed
1011 virtual operands.
1013 2- Take the first entry in AI->POINTERS and revert the role of
1014 the memory tag and its aliases. Usually, whenever an aliased
1015 variable Vi is found to alias with a memory tag T, we add Vi
1016 to the may-aliases set for T. Meaning that after alias
1017 analysis, we will have:
1019 may-aliases(T) = { V1, V2, V3, ..., Vn }
1021 This means that every statement that references T, will get 'n'
1022 virtual operands for each of the Vi tags. But, when alias
1023 grouping is enabled, we make T an alias tag and add it to the
1024 alias set of all the Vi variables:
1026 may-aliases(V1) = { T }
1027 may-aliases(V2) = { T }
1029 may-aliases(Vn) = { T }
1031 This has two effects: (a) statements referencing T will only get
1032 a single virtual operand, and, (b) all the variables Vi will now
1033 appear to alias each other. So, we lose alias precision to
1034 improve compile time. But, in theory, a program with such a high
1035 level of aliasing should not be very optimizable in the first
1036 place.
1038 3- Since variables may be in the alias set of more than one
1039 memory tag, the grouping done in step (2) needs to be extended
1040 to all the memory tags that have a non-empty intersection with
1041 the may-aliases set of tag T. For instance, if we originally
1042 had these may-aliases sets:
1044 may-aliases(T) = { V1, V2, V3 }
1045 may-aliases(R) = { V2, V4 }
1047 In step (2) we would have reverted the aliases for T as:
1049 may-aliases(V1) = { T }
1050 may-aliases(V2) = { T }
1051 may-aliases(V3) = { T }
1053 But note that now V2 is no longer aliased with R. We could
1054 add R to may-aliases(V2), but we are in the process of
1055 grouping aliases to reduce virtual operands so what we do is
1056 add V4 to the grouping to obtain:
1058 may-aliases(V1) = { T }
1059 may-aliases(V2) = { T }
1060 may-aliases(V3) = { T }
1061 may-aliases(V4) = { T }
1063 4- If the total number of virtual operands due to aliasing is
1064 still above the threshold set by max-alias-vops, go back to (2). */
1066 static void
1067 group_aliases (struct alias_info *ai)
1069 size_t i;
1070 sbitmap res;
1072 /* Sort the POINTERS array in descending order of contributed
1073 virtual operands. */
1074 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1075 total_alias_vops_cmp);
1077 res = sbitmap_alloc (num_referenced_vars);
1079 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1080 and the tag's may-aliases set. */
1081 for (i = 0; i < ai->num_pointers; i++)
1083 size_t j;
1084 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1085 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1087 /* Skip tags that have been grouped already. */
1088 if (ai->pointers[i]->grouped_p)
1089 continue;
1091 /* See if TAG1 had any aliases in common with other type tags.
1092 If we find a TAG2 with common aliases with TAG1, add TAG2's
1093 aliases into TAG1. */
1094 for (j = i + 1; j < ai->num_pointers; j++)
1096 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1098 sbitmap_a_and_b (res, tag1_aliases, tag2_aliases);
1099 if (sbitmap_first_set_bit (res) >= 0)
1101 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1103 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1105 /* TAG2 does not need its aliases anymore. */
1106 sbitmap_zero (tag2_aliases);
1107 var_ann (tag2)->may_aliases = NULL;
1109 /* TAG1 is the unique alias of TAG2. */
1110 add_may_alias (tag2, tag1);
1112 ai->pointers[j]->grouped_p = true;
1116 /* Now group all the aliases we collected into TAG1. */
1117 group_aliases_into (tag1, tag1_aliases, ai);
1119 /* If we've reduced total number of virtual operands below the
1120 threshold, stop. */
1121 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1122 break;
1125 /* Finally, all the variables that have been grouped cannot be in
1126 the may-alias set of name memory tags. Suppose that we have
1127 grouped the aliases in this code so that may-aliases(a) = TMT.20
1129 p_5 = &a;
1131 # a_9 = V_MAY_DEF <a_8>
1132 p_5->field = 0
1133 ... Several modifications to TMT.20 ...
1134 # VUSE <a_9>
1135 x_30 = p_5->field
1137 Since p_5 points to 'a', the optimizers will try to propagate 0
1138 into p_5->field, but that is wrong because there have been
1139 modifications to 'TMT.20' in between. To prevent this we have to
1140 replace 'a' with 'TMT.20' in the name tag of p_5. */
1141 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1143 size_t j;
1144 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1145 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1146 varray_type aliases;
1148 if (name_tag == NULL_TREE)
1149 continue;
1151 aliases = var_ann (name_tag)->may_aliases;
1152 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1154 tree alias = VARRAY_TREE (aliases, j);
1155 var_ann_t ann = var_ann (alias);
1156 if (ann->may_aliases)
1158 #if defined ENABLE_CHECKING
1159 if (VARRAY_ACTIVE_SIZE (ann->may_aliases) != 1)
1160 abort ();
1161 #endif
1162 VARRAY_TREE (aliases, j) = VARRAY_TREE (ann->may_aliases, 0);
1167 sbitmap_free (res);
1169 if (dump_file)
1170 fprintf (dump_file,
1171 "%s: Total number of aliased vops after grouping: %ld%s\n",
1172 get_name (current_function_decl),
1173 ai->total_alias_vops,
1174 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1178 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1180 static void
1181 create_alias_map_for (tree var, struct alias_info *ai)
1183 struct alias_map_d *alias_map;
1184 alias_map = xcalloc (1, sizeof (*alias_map));
1185 alias_map->var = var;
1187 if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
1188 alias_map->set = get_alias_set (TREE_TYPE (TREE_TYPE (var)));
1189 else
1190 alias_map->set = get_alias_set (var);
1191 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1195 /* Create memory tags for all the dereferenced pointers and build the
1196 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1197 sets. Based on the address escape and points-to information collected
1198 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1199 variables whose address is not needed anymore. */
1201 static void
1202 setup_pointers_and_addressables (struct alias_info *ai)
1204 size_t i, n_vars, num_addressable_vars, num_pointers;
1206 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1207 num_addressable_vars = num_pointers = 0;
1208 for (i = 0; i < num_referenced_vars; i++)
1210 tree var = referenced_var (i);
1212 if (may_be_aliased (var))
1213 num_addressable_vars++;
1215 if (POINTER_TYPE_P (TREE_TYPE (var)))
1217 /* Since we don't keep track of volatile variables nor
1218 variables with hidden uses, assume that these pointers
1219 are used in indirect store operations. */
1220 var_ann_t ann = var_ann (var);
1221 if (TREE_THIS_VOLATILE (var) || ann->has_hidden_use)
1222 bitmap_set_bit (ai->dereferenced_ptrs_store, ann->uid);
1224 num_pointers++;
1228 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1229 always going to be slightly bigger than we actually need them
1230 because some TREE_ADDRESSABLE variables will be marked
1231 non-addressable below and only pointers with unique type tags are
1232 going to be added to POINTERS. */
1233 ai->addressable_vars = xcalloc (num_addressable_vars,
1234 sizeof (struct alias_map_d *));
1235 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1236 ai->num_addressable_vars = 0;
1237 ai->num_pointers = 0;
1239 /* Since we will be creating type memory tags within this loop, cache the
1240 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1241 unnecessarily. */
1242 n_vars = num_referenced_vars;
1244 for (i = 0; i < n_vars; i++)
1246 tree var = referenced_var (i);
1247 var_ann_t v_ann = var_ann (var);
1249 /* Name memory tags already have flow-sensitive aliasing
1250 information, so they need not be processed by
1251 compute_may_aliases. Similarly, type memory tags are already
1252 accounted for when we process their associated pointer. */
1253 if (v_ann->mem_tag_kind != NOT_A_TAG)
1254 continue;
1256 /* Remove the ADDRESSABLE flag from every addressable variable whose
1257 address is not needed anymore. This is caused by the propagation
1258 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1259 removal of dead pointer assignments done by the early scalar
1260 cleanup passes. */
1261 if (TREE_ADDRESSABLE (var))
1263 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1264 && !v_ann->has_hidden_use
1265 && v_ann->mem_tag_kind == NOT_A_TAG
1266 && !needs_to_live_in_memory (var))
1268 /* The address of VAR is not needed, remove the
1269 addressable bit, so that it can be optimized as a
1270 regular variable. */
1271 mark_non_addressable (var);
1273 /* Since VAR is now a regular GIMPLE register, we will need
1274 to rename VAR into SSA afterwards. */
1275 bitmap_set_bit (vars_to_rename, v_ann->uid);
1277 else
1279 /* Add the variable to the set of addressables. Mostly
1280 used when scanning operands for ASM_EXPRs that
1281 clobber memory. In those cases, we need to clobber
1282 all call-clobbered variables and all addressables. */
1283 bitmap_set_bit (addressable_vars, v_ann->uid);
1287 /* Global variables and addressable locals may be aliased. Create an
1288 entry in ADDRESSABLE_VARS for VAR. */
1289 if (may_be_aliased (var))
1291 create_alias_map_for (var, ai);
1292 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1295 /* Add pointer variables that have been dereferenced to the POINTERS
1296 array and create a type memory tag for them. */
1297 if (POINTER_TYPE_P (TREE_TYPE (var))
1298 && (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1299 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1301 tree tag = v_ann->type_mem_tag;
1302 var_ann_t t_ann;
1304 /* If pointer VAR still doesn't have a memory tag associated
1305 with it, create it now or re-use an existing one. */
1306 if (tag == NULL_TREE)
1307 tag = get_tmt_for (var, ai);
1308 t_ann = var_ann (tag);
1310 /* The type tag will need to be renamed into SSA afterwards.
1311 Note that we cannot do this inside get_tmt_for because
1312 aliasing may run multiple times and we only create type
1313 tags the first time. */
1314 bitmap_set_bit (vars_to_rename, t_ann->uid);
1316 /* Associate the tag with pointer VAR. */
1317 v_ann->type_mem_tag = tag;
1319 /* If pointer VAR has been used in a store operation, then its
1320 memory tag must be marked as written-to. */
1321 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1322 bitmap_set_bit (ai->written_vars, t_ann->uid);
1324 /* If pointer VAR is a global variable or a PARM_DECL, then its
1325 memory tag should be considered a global variable. */
1326 if (TREE_CODE (var) == PARM_DECL || needs_to_live_in_memory (var))
1327 mark_call_clobbered (tag);
1329 /* All the dereferences of pointer VAR count as references of
1330 TAG. Since TAG can be associated with several pointers, add
1331 the dereferences of VAR to the TAG. We may need to grow
1332 AI->NUM_REFERENCES because we have been adding name and
1333 type tags. */
1334 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1335 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1337 VARRAY_UINT (ai->num_references, t_ann->uid)
1338 += VARRAY_UINT (ai->num_references, v_ann->uid);
1342 /* If we found no addressable variables, but we have more than one
1343 pointer, we will need to check for conflicts between the
1344 pointers. Otherwise, we would miss alias relations as in
1345 testsuite/gcc.dg/tree-ssa/20040319-1.c:
1347 struct bar { int count; int *arr;};
1349 void foo (struct bar *b)
1351 b->count = 0;
1352 *(b->arr) = 2;
1353 if (b->count == 0)
1354 abort ();
1357 b->count and *(b->arr) could be aliased if b->arr == &b->count.
1358 To do this, we add all the memory tags for the pointers in
1359 AI->POINTERS to AI->ADDRESSABLE_VARS, so that
1360 compute_flow_insensitive_aliasing will naturally compare every
1361 pointer to every type tag. */
1362 if (ai->num_addressable_vars == 0
1363 && ai->num_pointers > 1)
1365 free (ai->addressable_vars);
1366 ai->addressable_vars = xcalloc (ai->num_pointers,
1367 sizeof (struct alias_map_d *));
1368 ai->num_addressable_vars = 0;
1369 for (i = 0; i < ai->num_pointers; i++)
1371 struct alias_map_d *p = ai->pointers[i];
1372 tree tag = var_ann (p->var)->type_mem_tag;
1373 create_alias_map_for (tag, ai);
1379 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1380 every call site, we need to emit V_MAY_DEF expressions to represent the
1381 clobbering effects of the call for variables whose address escapes the
1382 current function.
1384 One approach is to group all call-clobbered variables into a single
1385 representative that is used as an alias of every call-clobbered variable
1386 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1387 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1389 The second approach is to emit a clobbering V_MAY_DEF for every
1390 call-clobbered variable at call sites. This is the preferred way in terms
1391 of optimization opportunities but it may create too many V_MAY_DEF operands
1392 if there are many call clobbered variables and function calls in the
1393 function.
1395 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1396 function calls found by the number of call-clobbered variables. If that
1397 product is beyond a certain threshold, as determined by the parameterized
1398 values shown below, we use .GLOBAL_VAR.
1400 FIXME. This heuristic should be improved. One idea is to use several
1401 .GLOBAL_VARs of different types instead of a single one. The thresholds
1402 have been derived from a typical bootstrap cycle, including all target
1403 libraries. Compile times were found increase by ~1% compared to using
1404 .GLOBAL_VAR. */
1406 static void
1407 maybe_create_global_var (struct alias_info *ai)
1409 size_t i, n_clobbered;
1411 /* Count all the call-clobbered variables. */
1412 n_clobbered = 0;
1413 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, n_clobbered++);
1415 /* Create .GLOBAL_VAR if we have too many call-clobbered variables.
1416 We also create .GLOBAL_VAR when there no call-clobbered variables
1417 to prevent code motion transformations from re-arranging function
1418 calls that may have side effects. For instance,
1420 foo ()
1422 int a = f ();
1423 g ();
1424 h (a);
1427 There are no call-clobbered variables in foo(), so it would be
1428 entirely possible for a pass to want to move the call to f()
1429 after the call to g(). If f() has side effects, that would be
1430 wrong. Creating .GLOBAL_VAR in this case will insert VDEFs for
1431 it and prevent such transformations. */
1432 if (n_clobbered == 0
1433 || ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD)
1434 create_global_var ();
1436 /* If the function has calls to clobbering functions and .GLOBAL_VAR has
1437 been created, make it an alias for all call-clobbered variables. */
1438 if (global_var)
1439 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1441 tree var = referenced_var (i);
1442 if (var != global_var)
1444 add_may_alias (var, global_var);
1445 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1451 /* Return TRUE if pointer PTR may point to variable VAR.
1453 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1454 This is needed because when checking for type conflicts we are
1455 interested in the alias set of the memory location pointed-to by
1456 PTR. The alias set of PTR itself is irrelevant.
1458 VAR_ALIAS_SET is the alias set for VAR. */
1460 static bool
1461 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1462 tree var, HOST_WIDE_INT var_alias_set)
1464 tree mem;
1465 var_ann_t v_ann, m_ann;
1467 alias_stats.alias_queries++;
1468 alias_stats.simple_queries++;
1470 /* By convention, a variable cannot alias itself. */
1471 mem = var_ann (ptr)->type_mem_tag;
1472 if (mem == var)
1474 alias_stats.alias_noalias++;
1475 alias_stats.simple_resolved++;
1476 return false;
1479 v_ann = var_ann (var);
1480 m_ann = var_ann (mem);
1482 #if defined ENABLE_CHECKING
1483 if (m_ann->mem_tag_kind != TYPE_TAG)
1484 abort ();
1485 #endif
1487 alias_stats.tbaa_queries++;
1489 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1490 PTR can't possibly affect VAR. Note, that we are specifically testing
1491 for PTR's alias set here, not its pointed-to type. We also can't
1492 do this check with relaxed aliasing enabled. */
1493 if (POINTER_TYPE_P (TREE_TYPE (var))
1494 && var_alias_set != 0)
1496 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1497 if (ptr_alias_set == var_alias_set)
1499 alias_stats.alias_noalias++;
1500 alias_stats.tbaa_resolved++;
1501 return false;
1505 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1506 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1508 /* Handle aliases to structure fields. If either VAR or MEM are
1509 aggregate types, they may not have conflicting types, but one of
1510 the structures could contain a pointer to the other one.
1512 For instance, given
1514 MEM -> struct P *p;
1515 VAR -> struct Q *q;
1517 It may happen that '*p' and '*q' can't alias because 'struct P'
1518 and 'struct Q' have non-conflicting alias sets. However, it could
1519 happen that one of the fields in 'struct P' is a 'struct Q *' or
1520 vice-versa.
1522 Therefore, we also need to check if 'struct P' aliases 'struct Q *'
1523 or 'struct Q' aliases 'struct P *'. Notice, that since GIMPLE
1524 does not have more than one-level pointers, we don't need to
1525 recurse into the structures. */
1526 if (AGGREGATE_TYPE_P (TREE_TYPE (mem))
1527 || AGGREGATE_TYPE_P (TREE_TYPE (var)))
1529 tree ptr_to_var;
1531 if (TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE)
1532 ptr_to_var = TYPE_POINTER_TO (TREE_TYPE (TREE_TYPE (var)));
1533 else
1534 ptr_to_var = TYPE_POINTER_TO (TREE_TYPE (var));
1536 /* If no pointer-to VAR exists, then MEM can't alias VAR. */
1537 if (ptr_to_var == NULL_TREE)
1539 alias_stats.alias_noalias++;
1540 alias_stats.tbaa_resolved++;
1541 return false;
1544 /* If MEM doesn't alias a pointer to VAR and VAR doesn't alias
1545 PTR, then PTR can't alias VAR. */
1546 if (!alias_sets_conflict_p (mem_alias_set, get_alias_set (ptr_to_var))
1547 && !alias_sets_conflict_p (var_alias_set, get_alias_set (ptr)))
1549 alias_stats.alias_noalias++;
1550 alias_stats.tbaa_resolved++;
1551 return false;
1554 else
1556 alias_stats.alias_noalias++;
1557 alias_stats.tbaa_resolved++;
1558 return false;
1562 if (flag_tree_points_to != PTA_NONE)
1563 alias_stats.pta_queries++;
1565 /* If -ftree-points-to is given, check if PTR may point to VAR. */
1566 if (flag_tree_points_to == PTA_ANDERSEN
1567 && !ptr_may_alias_var (ptr, var))
1569 alias_stats.alias_noalias++;
1570 alias_stats.pta_resolved++;
1571 return false;
1574 alias_stats.alias_mayalias++;
1575 return true;
1579 /* Add ALIAS to the set of variables that may alias VAR. */
1581 static void
1582 add_may_alias (tree var, tree alias)
1584 size_t i;
1585 var_ann_t v_ann = get_var_ann (var);
1586 var_ann_t a_ann = get_var_ann (alias);
1588 #if defined ENABLE_CHECKING
1589 if (var == alias)
1590 abort ();
1591 #endif
1593 if (v_ann->may_aliases == NULL)
1594 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1596 /* Avoid adding duplicates. */
1597 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1598 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1599 return;
1601 /* If VAR is a call-clobbered variable, so is its new ALIAS. */
1602 if (is_call_clobbered (var))
1603 mark_call_clobbered (alias);
1605 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1606 else if (is_call_clobbered (alias))
1607 mark_call_clobbered (var);
1609 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1610 a_ann->is_alias_tag = 1;
1614 /* Given two pointers DEST and ORIG. Merge the points-to information in
1615 ORIG into DEST. AI is as in collect_points_to_info. */
1617 static void
1618 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1620 struct ptr_info_def *dest_pi, *orig_pi;
1622 /* Make sure we have points-to information for ORIG. */
1623 collect_points_to_info_for (ai, orig);
1625 dest_pi = get_ptr_info (dest);
1626 orig_pi = SSA_NAME_PTR_INFO (orig);
1628 if (orig_pi)
1630 dest_pi->pt_anything |= orig_pi->pt_anything;
1632 /* Notice that we never merge PT_MALLOC. This attribute is only
1633 true if the pointer is the result of a malloc() call.
1634 Otherwise, we can end up in this situation:
1636 P_i = malloc ();
1638 P_j = P_i + X;
1640 P_j would be marked as PT_MALLOC, which is wrong because
1641 PT_MALLOC implies that the pointer may not point to another
1642 variable.
1644 FIXME 1: Subsequent analysis may determine that P_j
1645 cannot alias anything else, but we are being conservative
1646 here.
1648 FIXME 2: If the merging comes from a copy assignment, we
1649 ought to merge PT_MALLOC, but then both pointers would end up
1650 getting different name tags because create_name_tags is not
1651 smart enough to determine that the two come from the same
1652 malloc call. Copy propagation before aliasing should cure
1653 this. */
1654 dest_pi->pt_malloc = 0;
1655 if (orig_pi->pt_malloc)
1656 dest_pi->pt_anything = 1;
1658 if (orig_pi->pt_vars)
1660 if (dest_pi->pt_vars == NULL)
1662 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1663 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1665 else
1666 bitmap_a_or_b (dest_pi->pt_vars,
1667 dest_pi->pt_vars,
1668 orig_pi->pt_vars);
1674 /* Add VALUE to the list of expressions pointed-to by PTR. */
1676 static void
1677 add_pointed_to_expr (tree ptr, tree value)
1679 struct ptr_info_def *pi;
1681 #if defined ENABLE_CHECKING
1682 /* Pointer variables should have been handled by merge_pointed_to_info. */
1683 if (TREE_CODE (value) == SSA_NAME
1684 && POINTER_TYPE_P (TREE_TYPE (value)))
1685 abort ();
1686 #endif
1688 pi = get_ptr_info (ptr);
1690 /* If VALUE is the result of a malloc-like call, then the area pointed to
1691 PTR is guaranteed to not alias with anything else. */
1692 if (TREE_CODE (value) == CALL_EXPR
1693 && (call_expr_flags (value) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1694 pi->pt_malloc = 1;
1695 else
1696 pi->pt_anything = 1;
1698 if (dump_file)
1700 fprintf (dump_file, "Pointer ");
1701 print_generic_expr (dump_file, ptr, dump_flags);
1702 fprintf (dump_file, " points to ");
1703 if (pi->pt_malloc)
1704 fprintf (dump_file, "malloc space: ");
1705 else
1706 fprintf (dump_file, "an arbitrary address: ");
1707 print_generic_expr (dump_file, value, dump_flags);
1708 fprintf (dump_file, "\n");
1713 /* If VALUE is of the form &DECL, add DECL to the set of variables
1714 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1715 PTR. AI is as in collect_points_to_info. */
1717 static void
1718 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1720 if (TREE_CODE (value) == ADDR_EXPR)
1722 tree pt_var;
1723 struct ptr_info_def *pi;
1724 size_t uid;
1726 pt_var = TREE_OPERAND (value, 0);
1727 if (TREE_CODE_CLASS (TREE_CODE (pt_var)) == 'r')
1728 pt_var = get_base_address (pt_var);
1730 if (pt_var && SSA_VAR_P (pt_var))
1732 pi = get_ptr_info (ptr);
1733 uid = var_ann (pt_var)->uid;
1734 if (pi->pt_vars == NULL)
1735 pi->pt_vars = BITMAP_GGC_ALLOC ();
1736 bitmap_set_bit (pi->pt_vars, uid);
1737 bitmap_set_bit (ai->addresses_needed, uid);
1739 else
1740 add_pointed_to_expr (ptr, value);
1742 else
1743 add_pointed_to_expr (ptr, value);
1747 /* Callback for walk_use_def_chains to gather points-to information from the
1748 SSA web.
1750 VAR is an SSA variable or a GIMPLE expression.
1752 STMT is the statement that generates the SSA variable or, if STMT is a
1753 PHI_NODE, VAR is one of the PHI arguments.
1755 DATA is a pointer to a structure of type ALIAS_INFO. */
1757 static bool
1758 collect_points_to_info_r (tree var, tree stmt, void *data)
1760 struct alias_info *ai = (struct alias_info *) data;
1762 if (dump_file && (dump_flags & TDF_DETAILS))
1764 fprintf (dump_file, "Visiting use-def links for ");
1765 print_generic_expr (dump_file, var, dump_flags);
1766 fprintf (dump_file, "\n");
1769 if (TREE_CODE (stmt) == MODIFY_EXPR)
1771 tree rhs = TREE_OPERAND (stmt, 1);
1772 STRIP_NOPS (rhs);
1774 /* Found P_i = CONST. */
1775 if (is_gimple_min_invariant (rhs))
1776 add_pointed_to_var (ai, var, rhs);
1778 /* Found P_i = Q_j. */
1779 else if (TREE_CODE (rhs) == SSA_NAME
1780 && POINTER_TYPE_P (TREE_TYPE (rhs)))
1781 merge_pointed_to_info (ai, var, rhs);
1783 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1784 else if (TREE_CODE (rhs) == PLUS_EXPR
1785 || TREE_CODE (rhs) == MINUS_EXPR)
1787 tree op0 = TREE_OPERAND (rhs, 0);
1788 tree op1 = TREE_OPERAND (rhs, 1);
1790 if (TREE_CODE (op0) == SSA_NAME
1791 && POINTER_TYPE_P (TREE_TYPE (op0)))
1792 merge_pointed_to_info (ai, var, op0);
1793 else if (TREE_CODE (op1) == SSA_NAME
1794 && POINTER_TYPE_P (TREE_TYPE (op1)))
1795 merge_pointed_to_info (ai, var, op1);
1796 else if (is_gimple_min_invariant (op0))
1797 add_pointed_to_var (ai, var, op0);
1798 else if (is_gimple_min_invariant (op1))
1799 add_pointed_to_var (ai, var, op1);
1800 else
1801 add_pointed_to_expr (var, rhs);
1804 /* Something else. */
1805 else
1806 add_pointed_to_expr (var, rhs);
1808 else if (TREE_CODE (stmt) == ASM_EXPR)
1810 /* Pointers defined by __asm__ statements can point anywhere. */
1811 get_ptr_info (var)->pt_anything = 1;
1813 else if (IS_EMPTY_STMT (stmt))
1815 tree decl = SSA_NAME_VAR (var);
1817 if (TREE_CODE (decl) == PARM_DECL)
1818 add_pointed_to_expr (var, decl);
1819 else if (DECL_INITIAL (decl))
1820 add_pointed_to_var (ai, var, DECL_INITIAL (decl));
1821 else
1822 add_pointed_to_expr (var, decl);
1824 else if (TREE_CODE (stmt) == PHI_NODE)
1826 tree lhs = PHI_RESULT (stmt);
1828 if (is_gimple_min_invariant (var))
1829 add_pointed_to_var (ai, lhs, var);
1830 else if (TREE_CODE (var) == SSA_NAME)
1831 merge_pointed_to_info (ai, lhs, var);
1832 else
1833 abort ();
1835 else
1836 abort ();
1838 return false;
1842 /* Return true if STMT is an "escape" site from the current function. Escape
1843 sites those statements which might expose the address of a variable
1844 outside the current function. STMT is an escape site iff:
1846 1- STMT is a function call, or
1847 2- STMT is an __asm__ expression, or
1848 3- STMT is an assignment to a non-local variable, or
1849 4- STMT is a return statement.
1851 If NUM_CALLS_P is not NULL, the counter is incremented if STMT contains
1852 a function call. */
1854 static bool
1855 is_escape_site (tree stmt, size_t *num_calls_p)
1857 if (get_call_expr_in (stmt) != NULL_TREE)
1859 if (num_calls_p)
1860 (*num_calls_p)++;
1862 return true;
1864 else if (TREE_CODE (stmt) == ASM_EXPR)
1865 return true;
1866 else if (TREE_CODE (stmt) == MODIFY_EXPR)
1868 tree lhs = TREE_OPERAND (stmt, 0);
1870 /* Get to the base of _REF nodes. */
1871 if (TREE_CODE (lhs) != SSA_NAME)
1872 lhs = get_base_address (lhs);
1874 /* If we couldn't recognize the LHS of the assignment, assume that it
1875 is a non-local store. */
1876 if (lhs == NULL_TREE)
1877 return true;
1879 /* If the LHS is an SSA name, it can't possibly represent a non-local
1880 memory store. */
1881 if (TREE_CODE (lhs) == SSA_NAME)
1882 return false;
1884 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
1885 local variables we cannot be sure if it will escape, because we
1886 don't have information about objects not in SSA form. Need to
1887 implement something along the lines of
1889 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
1890 Midkiff, ``Escape analysis for java,'' in Proceedings of the
1891 Conference on Object-Oriented Programming Systems, Languages, and
1892 Applications (OOPSLA), pp. 1-19, 1999. */
1893 return true;
1895 else if (TREE_CODE (stmt) == RETURN_EXPR)
1896 return true;
1898 return false;
1902 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
1903 is considered to represent all the pointers whose pointed-to types are
1904 in the same alias set class. Otherwise, the tag represents a single
1905 SSA_NAME pointer variable. */
1907 static tree
1908 create_memory_tag (tree type, bool is_type_tag)
1910 var_ann_t ann;
1911 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
1913 /* By default, memory tags are local variables. Alias analysis will
1914 determine whether they should be considered globals. */
1915 DECL_CONTEXT (tag) = current_function_decl;
1917 /* If the pointed-to type is volatile, so is the tag. */
1918 TREE_THIS_VOLATILE (tag) = TREE_THIS_VOLATILE (type);
1920 /* Memory tags are by definition addressable. This also prevents
1921 is_gimple_ref frome confusing memory tags with optimizable
1922 variables. */
1923 TREE_ADDRESSABLE (tag) = 1;
1925 ann = get_var_ann (tag);
1926 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
1927 ann->type_mem_tag = NULL_TREE;
1929 /* Add the tag to the symbol table. */
1930 add_referenced_tmp_var (tag);
1932 return tag;
1936 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
1937 This is used if P_i has been found to point to a specific set of
1938 variables or to a non-aliased memory location like the address returned
1939 by malloc functions. */
1941 static tree
1942 get_nmt_for (tree ptr)
1944 struct ptr_info_def *pi = get_ptr_info (ptr);
1945 tree tag = pi->name_mem_tag;
1947 if (tag == NULL_TREE)
1949 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
1951 /* If PTR is a PARM_DECL, its memory tag should be considered a
1952 global variable. */
1953 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL)
1954 mark_call_clobbered (tag);
1956 /* Similarly, if PTR points to malloc, then TAG is a global. */
1957 if (pi->pt_malloc)
1958 mark_call_clobbered (tag);
1961 return tag;
1965 /* Return the type memory tag associated to pointer PTR. A memory tag is an
1966 artificial variable that represents the memory location pointed-to by
1967 PTR. It is used to model the effects of pointer de-references on
1968 addressable variables.
1970 AI points to the data gathered during alias analysis. This function
1971 populates the array AI->POINTERS. */
1973 static tree
1974 get_tmt_for (tree ptr, struct alias_info *ai)
1976 size_t i;
1977 tree tag;
1978 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
1979 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
1981 /* To avoid creating unnecessary memory tags, only create one memory tag
1982 per alias set class. Note that it may be tempting to group
1983 memory tags based on conflicting alias sets instead of
1984 equivalence. That would be wrong because alias sets are not
1985 necessarily transitive (as demonstrated by the libstdc++ test
1986 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
1987 such that conflicts (A, B) == true and conflicts (A, C) == true,
1988 it does not necessarily follow that conflicts (B, C) == true. */
1989 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
1991 struct alias_map_d *curr = ai->pointers[i];
1992 if (tag_set == curr->set
1993 && (flag_tree_points_to == PTA_NONE
1994 || same_points_to_set (curr->var, ptr)))
1996 tag = var_ann (curr->var)->type_mem_tag;
1997 break;
2001 /* If VAR cannot alias with any of the existing memory tags, create a new
2002 tag for PTR and add it to the POINTERS array. */
2003 if (tag == NULL_TREE)
2005 struct alias_map_d *alias_map;
2007 /* Create a new MT.* artificial variable representing the memory
2008 location pointed-to by PTR. */
2009 tag = create_memory_tag (tag_type, true);
2011 /* Add PTR to the POINTERS array. Note that we are not interested in
2012 PTR's alias set. Instead, we cache the alias set for the memory that
2013 PTR points to. */
2014 alias_map = xcalloc (1, sizeof (*alias_map));
2015 alias_map->var = ptr;
2016 alias_map->set = tag_set;
2017 ai->pointers[ai->num_pointers++] = alias_map;
2020 return tag;
2024 /* Create GLOBAL_VAR, an artificial global variable to act as a
2025 representative of all the variables that may be clobbered by function
2026 calls. */
2028 static void
2029 create_global_var (void)
2031 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2032 size_type_node);
2033 DECL_ARTIFICIAL (global_var) = 1;
2034 TREE_READONLY (global_var) = 0;
2035 DECL_EXTERNAL (global_var) = 0;
2036 TREE_STATIC (global_var) = 1;
2037 TREE_USED (global_var) = 1;
2038 DECL_CONTEXT (global_var) = NULL_TREE;
2039 TREE_THIS_VOLATILE (global_var) = 0;
2040 TREE_ADDRESSABLE (global_var) = 0;
2042 add_referenced_tmp_var (global_var);
2043 bitmap_set_bit (vars_to_rename, var_ann (global_var)->uid);
2047 /* Dump alias statistics on FILE. */
2049 static void
2050 dump_alias_stats (FILE *file)
2052 const char *funcname
2053 = lang_hooks.decl_printable_name (current_function_decl, 2);
2054 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2055 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2056 fprintf (file, "Total alias mayalias results:\t%u\n",
2057 alias_stats.alias_mayalias);
2058 fprintf (file, "Total alias noalias results:\t%u\n",
2059 alias_stats.alias_noalias);
2060 fprintf (file, "Total simple queries:\t%u\n",
2061 alias_stats.simple_queries);
2062 fprintf (file, "Total simple resolved:\t%u\n",
2063 alias_stats.simple_resolved);
2064 fprintf (file, "Total TBAA queries:\t%u\n",
2065 alias_stats.tbaa_queries);
2066 fprintf (file, "Total TBAA resolved:\t%u\n",
2067 alias_stats.tbaa_resolved);
2068 fprintf (file, "Total PTA queries:\t%u\n",
2069 alias_stats.pta_queries);
2070 fprintf (file, "Total PTA resolved:\t%u\n",
2071 alias_stats.pta_resolved);
2075 /* Dump alias information on FILE. */
2077 void
2078 dump_alias_info (FILE *file)
2080 size_t i;
2081 const char *funcname
2082 = lang_hooks.decl_printable_name (current_function_decl, 2);
2084 fprintf (file, "\nAlias information for %s\n\n", funcname);
2086 for (i = 0; i < num_referenced_vars; i++)
2088 tree var = referenced_var (i);
2089 var_ann_t ann = var_ann (var);
2090 if (ann->may_aliases
2091 || ann->type_mem_tag
2092 || ann->is_alias_tag
2093 || ann->mem_tag_kind != NOT_A_TAG)
2094 dump_variable (file, var);
2097 fprintf (file, "\n");
2101 /* Dump alias information on stderr. */
2103 void
2104 debug_alias_info (void)
2106 dump_alias_info (stderr);
2110 /* Return the alias information associated with pointer T. It creates a
2111 new instance if none existed. */
2113 static struct ptr_info_def *
2114 get_ptr_info (tree t)
2116 struct ptr_info_def *pi;
2118 #if defined ENABLE_CHECKING
2119 if (!POINTER_TYPE_P (TREE_TYPE (t)))
2120 abort ();
2121 #endif
2123 pi = SSA_NAME_PTR_INFO (t);
2124 if (pi == NULL)
2126 pi = ggc_alloc (sizeof (*pi));
2127 memset ((void *)pi, 0, sizeof (*pi));
2128 SSA_NAME_PTR_INFO (t) = pi;
2131 return pi;
2135 /* Dump points-to information for SSA_NAME PTR into FILE. */
2137 void
2138 dump_points_to_info_for (FILE *file, tree ptr)
2140 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2142 fprintf (file, "Pointer ");
2143 print_generic_expr (file, ptr, dump_flags);
2145 if (pi)
2147 if (pi->name_mem_tag)
2149 fprintf (file, ", name memory tag: ");
2150 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2153 if (pi->is_dereferenced)
2154 fprintf (file, ", is dereferenced");
2156 if (pi->value_escapes_p)
2157 fprintf (file, ", its value escapes");
2159 if (pi->pt_anything)
2160 fprintf (file, ", points-to anything");
2162 if (pi->pt_malloc)
2163 fprintf (file, ", points-to malloc");
2165 if (pi->pt_vars)
2167 unsigned ix;
2169 fprintf (file, ", points-to vars: { ");
2170 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix,
2172 print_generic_expr (file, referenced_var (ix), dump_flags);
2173 fprintf (file, " ");
2175 fprintf (file, "}");
2179 fprintf (file, "\n");
2183 /* Dump points-to information for VAR into stderr. */
2185 void
2186 debug_points_to_info_for (tree var)
2188 dump_points_to_info_for (stderr, var);
2192 /* Dump points-to information into FILE. NOTE: This function is slow, as
2193 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2195 void
2196 dump_points_to_info (FILE *file)
2198 basic_block bb;
2199 block_stmt_iterator si;
2200 size_t i;
2201 const char *fname =
2202 lang_hooks.decl_printable_name (current_function_decl, 2);
2204 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2206 /* First dump points-to information for the default definitions of
2207 pointer variables. This is necessary because default definitions are
2208 not part of the code. */
2209 for (i = 0; i < num_referenced_vars; i++)
2211 tree var = referenced_var (i);
2212 if (POINTER_TYPE_P (TREE_TYPE (var)))
2214 var_ann_t ann = var_ann (var);
2215 if (ann->default_def)
2216 dump_points_to_info_for (file, ann->default_def);
2220 /* Dump points-to information for every pointer defined in the program. */
2221 FOR_EACH_BB (bb)
2223 tree phi;
2225 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2227 tree ptr = PHI_RESULT (phi);
2228 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2229 dump_points_to_info_for (file, ptr);
2232 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2234 stmt_ann_t ann = stmt_ann (bsi_stmt (si));
2235 def_optype defs = DEF_OPS (ann);
2236 if (defs)
2237 for (i = 0; i < NUM_DEFS (defs); i++)
2238 if (POINTER_TYPE_P (TREE_TYPE (DEF_OP (defs, i))))
2239 dump_points_to_info_for (file, DEF_OP (defs, i));
2243 fprintf (file, "\n");
2247 /* Dump points-to info pointed by PTO into STDERR. */
2249 void
2250 debug_points_to_info (void)
2252 dump_points_to_info (stderr);
2255 /* Dump to FILE the list of variables that may be aliasing VAR. */
2257 void
2258 dump_may_aliases_for (FILE *file, tree var)
2260 varray_type aliases;
2262 if (TREE_CODE (var) == SSA_NAME)
2263 var = SSA_NAME_VAR (var);
2265 aliases = var_ann (var)->may_aliases;
2266 if (aliases)
2268 size_t i;
2269 fprintf (file, "{ ");
2270 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2272 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2273 fprintf (file, " ");
2275 fprintf (file, "}");
2280 /* Dump to stderr the list of variables that may be aliasing VAR. */
2282 void
2283 debug_may_aliases_for (tree var)
2285 dump_may_aliases_for (stderr, var);