2005-06-07 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / tree-ssa-alias.c
blobab23cd0bab8aef216416ca06eff299dbe3c9b2db
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 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-pass.h"
43 #include "convert.h"
44 #include "params.h"
45 #include "vec.h"
47 /* 'true' after aliases have been computed (see compute_may_aliases). */
48 bool aliases_computed_p;
50 /* Structure to map a variable to its alias set and keep track of the
51 virtual operands that will be needed to represent it. */
52 struct alias_map_d
54 /* Variable and its alias set. */
55 tree var;
56 HOST_WIDE_INT set;
58 /* Total number of virtual operands that will be needed to represent
59 all the aliases of VAR. */
60 long total_alias_vops;
62 /* Nonzero if the aliases for this memory tag have been grouped
63 already. Used in group_aliases. */
64 unsigned int grouped_p : 1;
66 /* Set of variables aliased with VAR. This is the exact same
67 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
68 bitmap form to speed up alias grouping. */
69 sbitmap may_aliases;
73 /* Alias information used by compute_may_aliases and its helpers. */
74 struct alias_info
76 /* SSA names visited while collecting points-to information. If bit I
77 is set, it means that SSA variable with version I has already been
78 visited. */
79 sbitmap ssa_names_visited;
81 /* Array of SSA_NAME pointers processed by the points-to collector. */
82 varray_type processed_ptrs;
84 /* Variables whose address is still needed. */
85 bitmap addresses_needed;
87 /* ADDRESSABLE_VARS contains all the global variables and locals that
88 have had their address taken. */
89 struct alias_map_d **addressable_vars;
90 size_t num_addressable_vars;
92 /* POINTERS contains all the _DECL pointers with unique memory tags
93 that have been referenced in the program. */
94 struct alias_map_d **pointers;
95 size_t num_pointers;
97 /* Number of function calls found in the program. */
98 size_t num_calls_found;
100 /* Number of const/pure function calls found in the program. */
101 size_t num_pure_const_calls_found;
103 /* Array of counters to keep track of how many times each pointer has
104 been dereferenced in the program. This is used by the alias grouping
105 heuristic in compute_flow_insensitive_aliasing. */
106 varray_type num_references;
108 /* Total number of virtual operands that will be needed to represent
109 all the aliases of all the pointers found in the program. */
110 long total_alias_vops;
112 /* Variables that have been written to. */
113 bitmap written_vars;
115 /* Pointers that have been used in an indirect store operation. */
116 bitmap dereferenced_ptrs_store;
118 /* Pointers that have been used in an indirect load operation. */
119 bitmap dereferenced_ptrs_load;
123 /* Counters used to display statistics on alias analysis. */
124 struct alias_stats_d
126 unsigned int alias_queries;
127 unsigned int alias_mayalias;
128 unsigned int alias_noalias;
129 unsigned int simple_queries;
130 unsigned int simple_resolved;
131 unsigned int tbaa_queries;
132 unsigned int tbaa_resolved;
136 /* Local variables. */
137 static struct alias_stats_d alias_stats;
139 /* Local functions. */
140 static void compute_flow_insensitive_aliasing (struct alias_info *);
141 static void dump_alias_stats (FILE *);
142 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT);
143 static tree create_memory_tag (tree type, bool is_type_tag);
144 static tree get_tmt_for (tree, struct alias_info *);
145 static tree get_nmt_for (tree);
146 static void add_may_alias (tree, tree);
147 static void replace_may_alias (tree, size_t, tree);
148 static struct alias_info *init_alias_info (void);
149 static void delete_alias_info (struct alias_info *);
150 static void compute_points_to_and_addr_escape (struct alias_info *);
151 static void compute_flow_sensitive_aliasing (struct alias_info *);
152 static void setup_pointers_and_addressables (struct alias_info *);
153 static bool collect_points_to_info_r (tree, tree, void *);
154 static bool is_escape_site (tree, struct alias_info *);
155 static void add_pointed_to_var (struct alias_info *, tree, tree);
156 static void create_global_var (void);
157 static void collect_points_to_info_for (struct alias_info *, tree);
158 static void maybe_create_global_var (struct alias_info *ai);
159 static void group_aliases (struct alias_info *);
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, as are
204 conversions between pointers and integers.
206 This is where we are currently limited. Since not everything is renamed
207 into SSA, we lose track of escape properties when a pointer is stashed
208 inside a field in a structure, for instance. In those cases, we are
209 assuming that the pointer does escape.
211 We use escape analysis to determine whether a variable is
212 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
213 is call-clobbered. If a pointer P_i escapes, then all the variables
214 pointed-to by P_i (and its memory tag) also escape.
216 2- Compute flow-sensitive aliases
218 We have two classes of memory tags. Memory tags associated with the
219 pointed-to data type of the pointers in the program. These tags are
220 called "type memory tag" (TMT). The other class are those associated
221 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
222 when adding operands for an INDIRECT_REF *P_i, we will first check
223 whether P_i has a name tag, if it does we use it, because that will have
224 more precise aliasing information. Otherwise, we use the standard type
225 tag.
227 In this phase, we go through all the pointers we found in points-to
228 analysis and create alias sets for the name memory tags associated with
229 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
230 it points to and its tag.
233 3- Compute flow-insensitive aliases
235 This pass will compare the alias set of every type memory tag and every
236 addressable variable found in the program. Given a type memory tag TMT
237 and an addressable variable V. If the alias sets of TMT and V conflict
238 (as computed by may_alias_p), then V is marked as an alias tag and added
239 to the alias set of TMT.
241 For instance, consider the following function:
243 foo (int i)
245 int *p, a, b;
247 if (i > 10)
248 p = &a;
249 else
250 p = &b;
252 *p = 3;
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 block_stmt_iterator bsi;
349 basic_block bb;
350 FOR_EACH_BB (bb)
352 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
354 update_stmt_if_modified (bsi_stmt (bsi));
361 struct tree_opt_pass pass_may_alias =
363 "alias", /* name */
364 NULL, /* gate */
365 compute_may_aliases, /* execute */
366 NULL, /* sub */
367 NULL, /* next */
368 0, /* static_pass_number */
369 TV_TREE_MAY_ALIAS, /* tv_id */
370 PROP_cfg | PROP_ssa, /* properties_required */
371 PROP_alias, /* properties_provided */
372 0, /* properties_destroyed */
373 0, /* todo_flags_start */
374 TODO_dump_func | TODO_update_ssa
375 | TODO_ggc_collect | TODO_verify_ssa
376 | TODO_verify_stmts, /* todo_flags_finish */
377 0 /* letter */
381 /* Data structure used to count the number of dereferences to PTR
382 inside an expression. */
383 struct count_ptr_d
385 tree ptr;
386 unsigned count;
390 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
391 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
393 static tree
394 count_ptr_derefs (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
396 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
398 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
399 count_p->count++;
401 return NULL_TREE;
405 /* Count the number of direct and indirect uses for pointer PTR in
406 statement STMT. The two counts are stored in *NUM_USES_P and
407 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
408 least one of those dereferences is a store operation. */
410 void
411 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
412 unsigned *num_derefs_p, bool *is_store)
414 ssa_op_iter i;
415 tree use;
417 *num_uses_p = 0;
418 *num_derefs_p = 0;
419 *is_store = false;
421 /* Find out the total number of uses of PTR in STMT. */
422 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
423 if (use == ptr)
424 (*num_uses_p)++;
426 /* Now count the number of indirect references to PTR. This is
427 truly awful, but we don't have much choice. There are no parent
428 pointers inside INDIRECT_REFs, so an expression like
429 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
430 find all the indirect and direct uses of x_1 inside. The only
431 shortcut we can take is the fact that GIMPLE only allows
432 INDIRECT_REFs inside the expressions below. */
433 if (TREE_CODE (stmt) == MODIFY_EXPR
434 || (TREE_CODE (stmt) == RETURN_EXPR
435 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
436 || TREE_CODE (stmt) == ASM_EXPR
437 || TREE_CODE (stmt) == CALL_EXPR)
439 tree lhs, rhs;
441 if (TREE_CODE (stmt) == MODIFY_EXPR)
443 lhs = TREE_OPERAND (stmt, 0);
444 rhs = TREE_OPERAND (stmt, 1);
446 else if (TREE_CODE (stmt) == RETURN_EXPR)
448 tree e = TREE_OPERAND (stmt, 0);
449 lhs = TREE_OPERAND (e, 0);
450 rhs = TREE_OPERAND (e, 1);
452 else if (TREE_CODE (stmt) == ASM_EXPR)
454 lhs = ASM_OUTPUTS (stmt);
455 rhs = ASM_INPUTS (stmt);
457 else
459 lhs = NULL_TREE;
460 rhs = stmt;
463 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
465 struct count_ptr_d count;
466 count.ptr = ptr;
467 count.count = 0;
468 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
469 *is_store = true;
470 *num_derefs_p = count.count;
473 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
475 struct count_ptr_d count;
476 count.ptr = ptr;
477 count.count = 0;
478 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
479 *num_derefs_p += count.count;
483 gcc_assert (*num_uses_p >= *num_derefs_p);
487 /* Initialize the data structures used for alias analysis. */
489 static struct alias_info *
490 init_alias_info (void)
492 struct alias_info *ai;
494 ai = xcalloc (1, sizeof (struct alias_info));
495 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
496 sbitmap_zero (ai->ssa_names_visited);
497 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
498 ai->addresses_needed = BITMAP_ALLOC (NULL);
499 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
500 ai->written_vars = BITMAP_ALLOC (NULL);
501 ai->dereferenced_ptrs_store = BITMAP_ALLOC (NULL);
502 ai->dereferenced_ptrs_load = BITMAP_ALLOC (NULL);
504 /* If aliases have been computed before, clear existing information. */
505 if (aliases_computed_p)
507 unsigned i;
509 /* Similarly, clear the set of addressable variables. In this
510 case, we can just clear the set because addressability is
511 only computed here. */
512 bitmap_clear (addressable_vars);
514 /* Clear flow-insensitive alias information from each symbol. */
515 for (i = 0; i < num_referenced_vars; i++)
517 tree var = referenced_var (i);
518 var_ann_t ann = var_ann (var);
520 ann->is_alias_tag = 0;
521 ann->may_aliases = NULL;
523 /* Since we are about to re-discover call-clobbered
524 variables, clear the call-clobbered flag. Variables that
525 are intrinsically call-clobbered (globals, local statics,
526 etc) will not be marked by the aliasing code, so we can't
527 remove them from CALL_CLOBBERED_VARS.
529 NB: STRUCT_FIELDS are still call clobbered if they are for
530 a global variable, so we *don't* clear their call clobberedness
531 just because they are tags, though we will clear it if they
532 aren't for global variables. */
533 if (ann->mem_tag_kind == NAME_TAG
534 || ann->mem_tag_kind == TYPE_TAG
535 || !is_global_var (var))
536 clear_call_clobbered (var);
539 /* Clear flow-sensitive points-to information from each SSA name. */
540 for (i = 1; i < num_ssa_names; i++)
542 tree name = ssa_name (i);
544 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
545 continue;
547 if (SSA_NAME_PTR_INFO (name))
549 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
551 /* Clear all the flags but keep the name tag to
552 avoid creating new temporaries unnecessarily. If
553 this pointer is found to point to a subset or
554 superset of its former points-to set, then a new
555 tag will need to be created in create_name_tags. */
556 pi->pt_anything = 0;
557 pi->pt_malloc = 0;
558 pi->pt_null = 0;
559 pi->value_escapes_p = 0;
560 pi->is_dereferenced = 0;
561 if (pi->pt_vars)
562 bitmap_clear (pi->pt_vars);
567 /* Next time, we will need to reset alias information. */
568 aliases_computed_p = true;
570 return ai;
574 /* Deallocate memory used by alias analysis. */
576 static void
577 delete_alias_info (struct alias_info *ai)
579 size_t i;
581 sbitmap_free (ai->ssa_names_visited);
582 ai->processed_ptrs = NULL;
583 BITMAP_FREE (ai->addresses_needed);
585 for (i = 0; i < ai->num_addressable_vars; i++)
587 sbitmap_free (ai->addressable_vars[i]->may_aliases);
588 free (ai->addressable_vars[i]);
590 free (ai->addressable_vars);
592 for (i = 0; i < ai->num_pointers; i++)
594 sbitmap_free (ai->pointers[i]->may_aliases);
595 free (ai->pointers[i]);
597 free (ai->pointers);
599 ai->num_references = NULL;
600 BITMAP_FREE (ai->written_vars);
601 BITMAP_FREE (ai->dereferenced_ptrs_store);
602 BITMAP_FREE (ai->dereferenced_ptrs_load);
604 free (ai);
608 /* Walk use-def chains for pointer PTR to determine what variables is PTR
609 pointing to. */
611 static void
612 collect_points_to_info_for (struct alias_info *ai, tree ptr)
614 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
616 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
618 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
619 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
620 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
625 /* Traverse use-def links for all the pointers in the program to collect
626 address escape and points-to information.
628 This is loosely based on the same idea described in R. Hasti and S.
629 Horwitz, ``Using static single assignment form to improve
630 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
631 Programming Language Design and Implementation, pp. 97-105, 1998. */
633 static void
634 compute_points_to_and_addr_escape (struct alias_info *ai)
636 basic_block bb;
637 unsigned i;
638 tree op;
639 ssa_op_iter iter;
641 timevar_push (TV_TREE_PTA);
643 FOR_EACH_BB (bb)
645 block_stmt_iterator si;
647 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
649 bitmap addr_taken;
650 tree stmt = bsi_stmt (si);
651 bool stmt_escapes_p = is_escape_site (stmt, ai);
652 bitmap_iterator bi;
654 /* Mark all the variables whose address are taken by the
655 statement. Note that this will miss all the addresses taken
656 in PHI nodes (those are discovered while following the use-def
657 chains). */
658 addr_taken = addresses_taken (stmt);
659 if (addr_taken)
660 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
662 tree var = referenced_var (i);
663 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
664 if (stmt_escapes_p)
665 mark_call_clobbered (var);
668 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
670 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
671 struct ptr_info_def *pi;
672 bool is_store;
673 unsigned num_uses, num_derefs;
675 /* If the operand's variable may be aliased, keep track
676 of how many times we've referenced it. This is used
677 for alias grouping in compute_flow_sensitive_aliasing.
678 Note that we don't need to grow AI->NUM_REFERENCES
679 because we are processing regular variables, not
680 memory tags (the array's initial size is set to
681 NUM_REFERENCED_VARS). */
682 if (may_be_aliased (SSA_NAME_VAR (op)))
683 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
685 if (!POINTER_TYPE_P (TREE_TYPE (op)))
686 continue;
688 collect_points_to_info_for (ai, op);
690 pi = SSA_NAME_PTR_INFO (op);
691 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs,
692 &is_store);
694 if (num_derefs > 0)
696 /* Mark OP as dereferenced. In a subsequent pass,
697 dereferenced pointers that point to a set of
698 variables will be assigned a name tag to alias
699 all the variables OP points to. */
700 pi->is_dereferenced = 1;
702 /* Keep track of how many time we've dereferenced each
703 pointer. Again, we don't need to grow
704 AI->NUM_REFERENCES because we're processing
705 existing program variables. */
706 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
708 /* If this is a store operation, mark OP as being
709 dereferenced to store, otherwise mark it as being
710 dereferenced to load. */
711 if (is_store)
712 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
713 else
714 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
717 if (stmt_escapes_p && num_derefs < num_uses)
719 /* If STMT is an escape point and STMT contains at
720 least one direct use of OP, then the value of OP
721 escapes and so the pointed-to variables need to
722 be marked call-clobbered. */
723 pi->value_escapes_p = 1;
725 /* If the statement makes a function call, assume
726 that pointer OP will be dereferenced in a store
727 operation inside the called function. */
728 if (get_call_expr_in (stmt))
730 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
731 pi->is_dereferenced = 1;
736 /* Update reference counter for definitions to any
737 potentially aliased variable. This is used in the alias
738 grouping heuristics. */
739 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
741 tree var = SSA_NAME_VAR (op);
742 var_ann_t ann = var_ann (var);
743 bitmap_set_bit (ai->written_vars, ann->uid);
744 if (may_be_aliased (var))
745 (VARRAY_UINT (ai->num_references, ann->uid))++;
747 if (POINTER_TYPE_P (TREE_TYPE (op)))
748 collect_points_to_info_for (ai, op);
751 /* Mark variables in V_MAY_DEF operands as being written to. */
752 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
754 tree var = DECL_P (op) ? op : SSA_NAME_VAR (op);
755 var_ann_t ann = var_ann (var);
756 bitmap_set_bit (ai->written_vars, ann->uid);
759 /* After promoting variables and computing aliasing we will
760 need to re-scan most statements. FIXME: Try to minimize the
761 number of statements re-scanned. It's not really necessary to
762 re-scan *all* statements. */
763 mark_stmt_modified (stmt);
767 timevar_pop (TV_TREE_PTA);
771 /* Create name tags for all the pointers that have been dereferenced.
772 We only create a name tag for a pointer P if P is found to point to
773 a set of variables (so that we can alias them to *P) or if it is
774 the result of a call to malloc (which means that P cannot point to
775 anything else nor alias any other variable).
777 If two pointers P and Q point to the same set of variables, they
778 are assigned the same name tag. */
780 static void
781 create_name_tags (struct alias_info *ai)
783 size_t i;
785 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
787 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
788 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
790 if (pi->pt_anything || !pi->is_dereferenced)
792 /* No name tags for pointers that have not been
793 dereferenced or point to an arbitrary location. */
794 pi->name_mem_tag = NULL_TREE;
795 continue;
798 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
800 size_t j;
801 tree old_name_tag = pi->name_mem_tag;
803 /* If PTR points to a set of variables, check if we don't
804 have another pointer Q with the same points-to set before
805 creating a tag. If so, use Q's tag instead of creating a
806 new one.
808 This is important for not creating unnecessary symbols
809 and also for copy propagation. If we ever need to
810 propagate PTR into Q or vice-versa, we would run into
811 problems if they both had different name tags because
812 they would have different SSA version numbers (which
813 would force us to take the name tags in and out of SSA). */
814 for (j = 0; j < i; j++)
816 tree q = VARRAY_TREE (ai->processed_ptrs, j);
817 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
819 if (qi
820 && qi->pt_vars
821 && qi->name_mem_tag
822 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
824 pi->name_mem_tag = qi->name_mem_tag;
825 break;
829 /* If we didn't find a pointer with the same points-to set
830 as PTR, create a new name tag if needed. */
831 if (pi->name_mem_tag == NULL_TREE)
832 pi->name_mem_tag = get_nmt_for (ptr);
834 /* If the new name tag computed for PTR is different than
835 the old name tag that it used to have, then the old tag
836 needs to be removed from the IL, so we mark it for
837 renaming. */
838 if (old_name_tag && old_name_tag != pi->name_mem_tag)
839 mark_sym_for_renaming (old_name_tag);
841 else if (pi->pt_malloc)
843 /* Otherwise, create a unique name tag for this pointer. */
844 pi->name_mem_tag = get_nmt_for (ptr);
846 else
848 /* Only pointers that may point to malloc or other variables
849 may receive a name tag. If the pointer does not point to
850 a known spot, we should use type tags. */
851 set_pt_anything (ptr);
852 continue;
855 TREE_THIS_VOLATILE (pi->name_mem_tag)
856 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
858 /* Mark the new name tag for renaming. */
859 mark_sym_for_renaming (pi->name_mem_tag);
865 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
866 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
867 name tag and the variables it points-to are call-clobbered. Finally, if
868 P_i escapes and we could not determine where it points to, then all the
869 variables in the same alias set as *P_i are marked call-clobbered. This
870 is necessary because we must assume that P_i may take the address of any
871 variable in the same alias set. */
873 static void
874 compute_flow_sensitive_aliasing (struct alias_info *ai)
876 size_t i;
878 create_name_tags (ai);
880 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
882 unsigned j;
883 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
884 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
885 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
886 bitmap_iterator bi;
888 if (pi->value_escapes_p || pi->pt_anything)
890 /* If PTR escapes or may point to anything, then its associated
891 memory tags and pointed-to variables are call-clobbered. */
892 if (pi->name_mem_tag)
893 mark_call_clobbered (pi->name_mem_tag);
895 if (v_ann->type_mem_tag)
896 mark_call_clobbered (v_ann->type_mem_tag);
898 if (pi->pt_vars)
899 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
901 mark_call_clobbered (referenced_var (j));
905 /* Set up aliasing information for PTR's name memory tag (if it has
906 one). Note that only pointers that have been dereferenced will
907 have a name memory tag. */
908 if (pi->name_mem_tag && pi->pt_vars)
909 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
911 add_may_alias (pi->name_mem_tag, referenced_var (j));
912 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
915 /* If the name tag is call clobbered, so is the type tag
916 associated with the base VAR_DECL. */
917 if (pi->name_mem_tag
918 && v_ann->type_mem_tag
919 && is_call_clobbered (pi->name_mem_tag))
920 mark_call_clobbered (v_ann->type_mem_tag);
925 /* Compute type-based alias sets. Traverse all the pointers and
926 addressable variables found in setup_pointers_and_addressables.
928 For every pointer P in AI->POINTERS and addressable variable V in
929 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
930 memory tag (TMT) if their alias sets conflict. V is then marked as
931 an alias tag so that the operand scanner knows that statements
932 containing V have aliased operands. */
934 static void
935 compute_flow_insensitive_aliasing (struct alias_info *ai)
937 size_t i;
939 /* Initialize counter for the total number of virtual operands that
940 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
941 threshold set by --params max-alias-vops, we enable alias
942 grouping. */
943 ai->total_alias_vops = 0;
945 /* For every pointer P, determine which addressable variables may alias
946 with P's type memory tag. */
947 for (i = 0; i < ai->num_pointers; i++)
949 size_t j;
950 struct alias_map_d *p_map = ai->pointers[i];
951 tree tag = var_ann (p_map->var)->type_mem_tag;
952 var_ann_t tag_ann = var_ann (tag);
954 p_map->total_alias_vops = 0;
955 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
956 sbitmap_zero (p_map->may_aliases);
958 for (j = 0; j < ai->num_addressable_vars; j++)
960 struct alias_map_d *v_map;
961 var_ann_t v_ann;
962 tree var;
963 bool tag_stored_p, var_stored_p;
965 v_map = ai->addressable_vars[j];
966 var = v_map->var;
967 v_ann = var_ann (var);
969 /* Skip memory tags and variables that have never been
970 written to. We also need to check if the variables are
971 call-clobbered because they may be overwritten by
972 function calls.
974 Note this is effectively random accessing elements in
975 the sparse bitset, which can be highly inefficient.
976 So we first check the call_clobbered status of the
977 tag and variable before querying the bitmap. */
978 tag_stored_p = is_call_clobbered (tag)
979 || bitmap_bit_p (ai->written_vars, tag_ann->uid);
980 var_stored_p = is_call_clobbered (var)
981 || bitmap_bit_p (ai->written_vars, v_ann->uid);
982 if (!tag_stored_p && !var_stored_p)
983 continue;
985 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
987 subvar_t svars;
988 size_t num_tag_refs, num_var_refs;
990 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
991 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
993 /* Add VAR to TAG's may-aliases set. */
995 /* If this is an aggregate, we may have subvariables for it
996 that need to be pointed to. */
997 if (var_can_have_subvars (var)
998 && (svars = get_subvars_for_var (var)))
1000 subvar_t sv;
1002 for (sv = svars; sv; sv = sv->next)
1004 add_may_alias (tag, sv->var);
1005 /* Update the bitmap used to represent TAG's alias set
1006 in case we need to group aliases. */
1007 SET_BIT (p_map->may_aliases, var_ann (sv->var)->uid);
1010 else
1012 add_may_alias (tag, var);
1013 /* Update the bitmap used to represent TAG's alias set
1014 in case we need to group aliases. */
1015 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
1018 /* Update the total number of virtual operands due to
1019 aliasing. Since we are adding one more alias to TAG's
1020 may-aliases set, the total number of virtual operands due
1021 to aliasing will be increased by the number of references
1022 made to VAR and TAG (every reference to TAG will also
1023 count as a reference to VAR). */
1024 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1025 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1032 /* Since this analysis is based exclusively on symbols, it fails to
1033 handle cases where two pointers P and Q have different memory
1034 tags with conflicting alias set numbers but no aliased symbols in
1035 common.
1037 For example, suppose that we have two memory tags TMT.1 and TMT.2
1038 such that
1040 may-aliases (TMT.1) = { a }
1041 may-aliases (TMT.2) = { b }
1043 and the alias set number of TMT.1 conflicts with that of TMT.2.
1044 Since they don't have symbols in common, loads and stores from
1045 TMT.1 and TMT.2 will seem independent of each other, which will
1046 lead to the optimizers making invalid transformations (see
1047 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1049 To avoid this problem, we do a final traversal of AI->POINTERS
1050 looking for pairs of pointers that have no aliased symbols in
1051 common and yet have conflicting alias set numbers. */
1052 for (i = 0; i < ai->num_pointers; i++)
1054 size_t j;
1055 struct alias_map_d *p_map1 = ai->pointers[i];
1056 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1057 sbitmap may_aliases1 = p_map1->may_aliases;
1059 for (j = i + 1; j < ai->num_pointers; j++)
1061 struct alias_map_d *p_map2 = ai->pointers[j];
1062 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1063 sbitmap may_aliases2 = p_map2->may_aliases;
1065 /* If the pointers may not point to each other, do nothing. */
1066 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set))
1067 continue;
1069 /* The two pointers may alias each other. If they already have
1070 symbols in common, do nothing. */
1071 if (sbitmap_any_common_bits (may_aliases1, may_aliases2))
1072 continue;
1074 if (sbitmap_first_set_bit (may_aliases2) >= 0)
1076 unsigned int k;
1077 sbitmap_iterator sbi;
1079 /* Add all the aliases for TAG2 into TAG1's alias set.
1080 FIXME, update grouping heuristic counters. */
1081 EXECUTE_IF_SET_IN_SBITMAP (may_aliases2, 0, k, sbi)
1082 add_may_alias (tag1, referenced_var (k));
1083 sbitmap_a_or_b (may_aliases1, may_aliases1, may_aliases2);
1085 else
1087 /* Since TAG2 does not have any aliases of its own, add
1088 TAG2 itself to the alias set of TAG1. */
1089 add_may_alias (tag1, tag2);
1090 SET_BIT (may_aliases1, var_ann (tag2)->uid);
1095 if (dump_file)
1096 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
1097 get_name (current_function_decl),
1098 ai->total_alias_vops);
1100 /* Determine if we need to enable alias grouping. */
1101 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1102 group_aliases (ai);
1106 /* Comparison function for qsort used in group_aliases. */
1108 static int
1109 total_alias_vops_cmp (const void *p, const void *q)
1111 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1112 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1113 long n1 = (*p1)->total_alias_vops;
1114 long n2 = (*p2)->total_alias_vops;
1116 /* We want to sort in descending order. */
1117 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1120 /* Group all the aliases for TAG to make TAG represent all the
1121 variables in its alias set. Update the total number
1122 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1123 function will make TAG be the unique alias tag for all the
1124 variables in its may-aliases. So, given:
1126 may-aliases(TAG) = { V1, V2, V3 }
1128 This function will group the variables into:
1130 may-aliases(V1) = { TAG }
1131 may-aliases(V2) = { TAG }
1132 may-aliases(V2) = { TAG } */
1134 static void
1135 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
1137 unsigned int i;
1138 var_ann_t tag_ann = var_ann (tag);
1139 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1140 sbitmap_iterator sbi;
1142 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i, sbi)
1144 tree var = referenced_var (i);
1145 var_ann_t ann = var_ann (var);
1147 /* Make TAG the unique alias of VAR. */
1148 ann->is_alias_tag = 0;
1149 ann->may_aliases = NULL;
1151 /* Note that VAR and TAG may be the same if the function has no
1152 addressable variables (see the discussion at the end of
1153 setup_pointers_and_addressables). */
1154 if (var != tag)
1155 add_may_alias (var, tag);
1157 /* Reduce total number of virtual operands contributed
1158 by TAG on behalf of VAR. Notice that the references to VAR
1159 itself won't be removed. We will merely replace them with
1160 references to TAG. */
1161 ai->total_alias_vops -= num_tag_refs;
1164 /* We have reduced the number of virtual operands that TAG makes on
1165 behalf of all the variables formerly aliased with it. However,
1166 we have also "removed" all the virtual operands for TAG itself,
1167 so we add them back. */
1168 ai->total_alias_vops += num_tag_refs;
1170 /* TAG no longer has any aliases. */
1171 tag_ann->may_aliases = NULL;
1175 /* Group may-aliases sets to reduce the number of virtual operands due
1176 to aliasing.
1178 1- Sort the list of pointers in decreasing number of contributed
1179 virtual operands.
1181 2- Take the first entry in AI->POINTERS and revert the role of
1182 the memory tag and its aliases. Usually, whenever an aliased
1183 variable Vi is found to alias with a memory tag T, we add Vi
1184 to the may-aliases set for T. Meaning that after alias
1185 analysis, we will have:
1187 may-aliases(T) = { V1, V2, V3, ..., Vn }
1189 This means that every statement that references T, will get 'n'
1190 virtual operands for each of the Vi tags. But, when alias
1191 grouping is enabled, we make T an alias tag and add it to the
1192 alias set of all the Vi variables:
1194 may-aliases(V1) = { T }
1195 may-aliases(V2) = { T }
1197 may-aliases(Vn) = { T }
1199 This has two effects: (a) statements referencing T will only get
1200 a single virtual operand, and, (b) all the variables Vi will now
1201 appear to alias each other. So, we lose alias precision to
1202 improve compile time. But, in theory, a program with such a high
1203 level of aliasing should not be very optimizable in the first
1204 place.
1206 3- Since variables may be in the alias set of more than one
1207 memory tag, the grouping done in step (2) needs to be extended
1208 to all the memory tags that have a non-empty intersection with
1209 the may-aliases set of tag T. For instance, if we originally
1210 had these may-aliases sets:
1212 may-aliases(T) = { V1, V2, V3 }
1213 may-aliases(R) = { V2, V4 }
1215 In step (2) we would have reverted the aliases for T as:
1217 may-aliases(V1) = { T }
1218 may-aliases(V2) = { T }
1219 may-aliases(V3) = { T }
1221 But note that now V2 is no longer aliased with R. We could
1222 add R to may-aliases(V2), but we are in the process of
1223 grouping aliases to reduce virtual operands so what we do is
1224 add V4 to the grouping to obtain:
1226 may-aliases(V1) = { T }
1227 may-aliases(V2) = { T }
1228 may-aliases(V3) = { T }
1229 may-aliases(V4) = { T }
1231 4- If the total number of virtual operands due to aliasing is
1232 still above the threshold set by max-alias-vops, go back to (2). */
1234 static void
1235 group_aliases (struct alias_info *ai)
1237 size_t i;
1239 /* Sort the POINTERS array in descending order of contributed
1240 virtual operands. */
1241 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1242 total_alias_vops_cmp);
1244 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1245 and the tag's may-aliases set. */
1246 for (i = 0; i < ai->num_pointers; i++)
1248 size_t j;
1249 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1250 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1252 /* Skip tags that have been grouped already. */
1253 if (ai->pointers[i]->grouped_p)
1254 continue;
1256 /* See if TAG1 had any aliases in common with other type tags.
1257 If we find a TAG2 with common aliases with TAG1, add TAG2's
1258 aliases into TAG1. */
1259 for (j = i + 1; j < ai->num_pointers; j++)
1261 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1263 if (sbitmap_any_common_bits (tag1_aliases, tag2_aliases))
1265 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1267 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1269 /* TAG2 does not need its aliases anymore. */
1270 sbitmap_zero (tag2_aliases);
1271 var_ann (tag2)->may_aliases = NULL;
1273 /* TAG1 is the unique alias of TAG2. */
1274 add_may_alias (tag2, tag1);
1276 ai->pointers[j]->grouped_p = true;
1280 /* Now group all the aliases we collected into TAG1. */
1281 group_aliases_into (tag1, tag1_aliases, ai);
1283 /* If we've reduced total number of virtual operands below the
1284 threshold, stop. */
1285 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1286 break;
1289 /* Finally, all the variables that have been grouped cannot be in
1290 the may-alias set of name memory tags. Suppose that we have
1291 grouped the aliases in this code so that may-aliases(a) = TMT.20
1293 p_5 = &a;
1295 # a_9 = V_MAY_DEF <a_8>
1296 p_5->field = 0
1297 ... Several modifications to TMT.20 ...
1298 # VUSE <a_9>
1299 x_30 = p_5->field
1301 Since p_5 points to 'a', the optimizers will try to propagate 0
1302 into p_5->field, but that is wrong because there have been
1303 modifications to 'TMT.20' in between. To prevent this we have to
1304 replace 'a' with 'TMT.20' in the name tag of p_5. */
1305 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1307 size_t j;
1308 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1309 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1310 varray_type aliases;
1312 if (name_tag == NULL_TREE)
1313 continue;
1315 aliases = var_ann (name_tag)->may_aliases;
1316 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1318 tree alias = VARRAY_TREE (aliases, j);
1319 var_ann_t ann = var_ann (alias);
1321 if ((ann->mem_tag_kind == NOT_A_TAG
1322 || ann->mem_tag_kind == STRUCT_FIELD)
1323 && ann->may_aliases)
1325 tree new_alias;
1327 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1329 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1330 replace_may_alias (name_tag, j, new_alias);
1335 if (dump_file)
1336 fprintf (dump_file,
1337 "%s: Total number of aliased vops after grouping: %ld%s\n",
1338 get_name (current_function_decl),
1339 ai->total_alias_vops,
1340 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1344 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1346 static void
1347 create_alias_map_for (tree var, struct alias_info *ai)
1349 struct alias_map_d *alias_map;
1350 alias_map = xcalloc (1, sizeof (*alias_map));
1351 alias_map->var = var;
1352 alias_map->set = get_alias_set (var);
1353 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1357 /* Create memory tags for all the dereferenced pointers and build the
1358 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1359 sets. Based on the address escape and points-to information collected
1360 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1361 variables whose address is not needed anymore. */
1363 static void
1364 setup_pointers_and_addressables (struct alias_info *ai)
1366 size_t i, n_vars, num_addressable_vars, num_pointers;
1368 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1369 num_addressable_vars = num_pointers = 0;
1370 for (i = 0; i < num_referenced_vars; i++)
1372 tree var = referenced_var (i);
1374 if (may_be_aliased (var))
1375 num_addressable_vars++;
1377 if (POINTER_TYPE_P (TREE_TYPE (var)))
1379 /* Since we don't keep track of volatile variables, assume that
1380 these pointers are used in indirect store operations. */
1381 if (TREE_THIS_VOLATILE (var))
1382 bitmap_set_bit (ai->dereferenced_ptrs_store, var_ann (var)->uid);
1384 num_pointers++;
1388 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1389 always going to be slightly bigger than we actually need them
1390 because some TREE_ADDRESSABLE variables will be marked
1391 non-addressable below and only pointers with unique type tags are
1392 going to be added to POINTERS. */
1393 ai->addressable_vars = xcalloc (num_addressable_vars,
1394 sizeof (struct alias_map_d *));
1395 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1396 ai->num_addressable_vars = 0;
1397 ai->num_pointers = 0;
1399 /* Since we will be creating type memory tags within this loop, cache the
1400 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1401 unnecessarily. */
1402 n_vars = num_referenced_vars;
1404 for (i = 0; i < n_vars; i++)
1406 tree var = referenced_var (i);
1407 var_ann_t v_ann = var_ann (var);
1408 subvar_t svars;
1410 /* Name memory tags already have flow-sensitive aliasing
1411 information, so they need not be processed by
1412 compute_flow_insensitive_aliasing. Similarly, type memory
1413 tags are already accounted for when we process their
1414 associated pointer.
1416 Structure fields, on the other hand, have to have some of this
1417 information processed for them, but it's pointless to mark them
1418 non-addressable (since they are fake variables anyway). */
1419 if (v_ann->mem_tag_kind != NOT_A_TAG
1420 && v_ann->mem_tag_kind != STRUCT_FIELD)
1421 continue;
1423 /* Remove the ADDRESSABLE flag from every addressable variable whose
1424 address is not needed anymore. This is caused by the propagation
1425 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1426 removal of dead pointer assignments done by the early scalar
1427 cleanup passes. */
1428 if (TREE_ADDRESSABLE (var) && v_ann->mem_tag_kind != STRUCT_FIELD)
1430 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1431 && TREE_CODE (var) != RESULT_DECL
1432 && !is_global_var (var))
1434 bool okay_to_mark = true;
1436 /* Since VAR is now a regular GIMPLE register, we will need
1437 to rename VAR into SSA afterwards. */
1438 mark_sym_for_renaming (var);
1440 if (var_can_have_subvars (var)
1441 && (svars = get_subvars_for_var (var)))
1443 subvar_t sv;
1445 for (sv = svars; sv; sv = sv->next)
1447 var_ann_t svann = var_ann (sv->var);
1448 if (bitmap_bit_p (ai->addresses_needed, svann->uid))
1449 okay_to_mark = false;
1450 mark_sym_for_renaming (sv->var);
1454 /* The address of VAR is not needed, remove the
1455 addressable bit, so that it can be optimized as a
1456 regular variable. */
1457 if (okay_to_mark)
1458 mark_non_addressable (var);
1460 else
1462 /* Add the variable to the set of addressables. Mostly
1463 used when scanning operands for ASM_EXPRs that
1464 clobber memory. In those cases, we need to clobber
1465 all call-clobbered variables and all addressables. */
1466 bitmap_set_bit (addressable_vars, v_ann->uid);
1467 if (var_can_have_subvars (var)
1468 && (svars = get_subvars_for_var (var)))
1470 subvar_t sv;
1471 for (sv = svars; sv; sv = sv->next)
1472 bitmap_set_bit (addressable_vars, var_ann (sv->var)->uid);
1478 /* Global variables and addressable locals may be aliased. Create an
1479 entry in ADDRESSABLE_VARS for VAR. */
1480 if (may_be_aliased (var))
1482 create_alias_map_for (var, ai);
1483 mark_sym_for_renaming (var);
1486 /* Add pointer variables that have been dereferenced to the POINTERS
1487 array and create a type memory tag for them. */
1488 if (POINTER_TYPE_P (TREE_TYPE (var)))
1490 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1491 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1493 tree tag;
1494 var_ann_t t_ann;
1496 /* If pointer VAR still doesn't have a memory tag
1497 associated with it, create it now or re-use an
1498 existing one. */
1499 tag = get_tmt_for (var, ai);
1500 t_ann = var_ann (tag);
1502 /* The type tag will need to be renamed into SSA
1503 afterwards. Note that we cannot do this inside
1504 get_tmt_for because aliasing may run multiple times
1505 and we only create type tags the first time. */
1506 mark_sym_for_renaming (tag);
1508 /* Similarly, if pointer VAR used to have another type
1509 tag, we will need to process it in the renamer to
1510 remove the stale virtual operands. */
1511 if (v_ann->type_mem_tag)
1512 mark_sym_for_renaming (v_ann->type_mem_tag);
1514 /* Associate the tag with pointer VAR. */
1515 v_ann->type_mem_tag = tag;
1517 /* If pointer VAR has been used in a store operation,
1518 then its memory tag must be marked as written-to. */
1519 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1520 bitmap_set_bit (ai->written_vars, t_ann->uid);
1522 /* If pointer VAR is a global variable or a PARM_DECL,
1523 then its memory tag should be considered a global
1524 variable. */
1525 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1526 mark_call_clobbered (tag);
1528 /* All the dereferences of pointer VAR count as
1529 references of TAG. Since TAG can be associated with
1530 several pointers, add the dereferences of VAR to the
1531 TAG. We may need to grow AI->NUM_REFERENCES because
1532 we have been adding name and type tags. */
1533 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1534 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1536 VARRAY_UINT (ai->num_references, t_ann->uid)
1537 += VARRAY_UINT (ai->num_references, v_ann->uid);
1539 else
1541 /* The pointer has not been dereferenced. If it had a
1542 type memory tag, remove it and mark the old tag for
1543 renaming to remove it out of the IL. */
1544 var_ann_t ann = var_ann (var);
1545 tree tag = ann->type_mem_tag;
1546 if (tag)
1548 mark_sym_for_renaming (tag);
1549 ann->type_mem_tag = NULL_TREE;
1557 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1558 every call site, we need to emit V_MAY_DEF expressions to represent the
1559 clobbering effects of the call for variables whose address escapes the
1560 current function.
1562 One approach is to group all call-clobbered variables into a single
1563 representative that is used as an alias of every call-clobbered variable
1564 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1565 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1567 The second approach is to emit a clobbering V_MAY_DEF for every
1568 call-clobbered variable at call sites. This is the preferred way in terms
1569 of optimization opportunities but it may create too many V_MAY_DEF operands
1570 if there are many call clobbered variables and function calls in the
1571 function.
1573 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1574 function calls found by the number of call-clobbered variables. If that
1575 product is beyond a certain threshold, as determined by the parameterized
1576 values shown below, we use .GLOBAL_VAR.
1578 FIXME. This heuristic should be improved. One idea is to use several
1579 .GLOBAL_VARs of different types instead of a single one. The thresholds
1580 have been derived from a typical bootstrap cycle, including all target
1581 libraries. Compile times were found increase by ~1% compared to using
1582 .GLOBAL_VAR. */
1584 static void
1585 maybe_create_global_var (struct alias_info *ai)
1587 unsigned i, n_clobbered;
1588 bitmap_iterator bi;
1590 /* No need to create it, if we have one already. */
1591 if (global_var == NULL_TREE)
1593 /* Count all the call-clobbered variables. */
1594 n_clobbered = 0;
1595 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1597 n_clobbered++;
1600 /* If the number of virtual operands that would be needed to
1601 model all the call-clobbered variables is larger than
1602 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1604 Also create .GLOBAL_VAR if there are no call-clobbered
1605 variables and the program contains a mixture of pure/const
1606 and regular function calls. This is to avoid the problem
1607 described in PR 20115:
1609 int X;
1610 int func_pure (void) { return X; }
1611 int func_non_pure (int a) { X += a; }
1612 int foo ()
1614 int a = func_pure ();
1615 func_non_pure (a);
1616 a = func_pure ();
1617 return a;
1620 Since foo() has no call-clobbered variables, there is
1621 no relationship between the calls to func_pure and
1622 func_non_pure. Since func_pure has no side-effects, value
1623 numbering optimizations elide the second call to func_pure.
1624 So, if we have some pure/const and some regular calls in the
1625 program we create .GLOBAL_VAR to avoid missing these
1626 relations. */
1627 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1628 || (n_clobbered == 0
1629 && ai->num_calls_found > 0
1630 && ai->num_pure_const_calls_found > 0
1631 && ai->num_calls_found > ai->num_pure_const_calls_found))
1632 create_global_var ();
1635 /* Mark all call-clobbered symbols for renaming. Since the initial
1636 rewrite into SSA ignored all call sites, we may need to rename
1637 .GLOBAL_VAR and the call-clobbered variables. */
1638 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1640 tree var = referenced_var (i);
1642 /* If the function has calls to clobbering functions and
1643 .GLOBAL_VAR has been created, make it an alias for all
1644 call-clobbered variables. */
1645 if (global_var && var != global_var)
1647 subvar_t svars;
1648 add_may_alias (var, global_var);
1649 if (var_can_have_subvars (var)
1650 && (svars = get_subvars_for_var (var)))
1652 subvar_t sv;
1653 for (sv = svars; sv; sv = sv->next)
1654 mark_sym_for_renaming (sv->var);
1658 mark_sym_for_renaming (var);
1663 /* Return TRUE if pointer PTR may point to variable VAR.
1665 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1666 This is needed because when checking for type conflicts we are
1667 interested in the alias set of the memory location pointed-to by
1668 PTR. The alias set of PTR itself is irrelevant.
1670 VAR_ALIAS_SET is the alias set for VAR. */
1672 static bool
1673 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1674 tree var, HOST_WIDE_INT var_alias_set)
1676 tree mem;
1677 var_ann_t m_ann;
1679 alias_stats.alias_queries++;
1680 alias_stats.simple_queries++;
1682 /* By convention, a variable cannot alias itself. */
1683 mem = var_ann (ptr)->type_mem_tag;
1684 if (mem == var)
1686 alias_stats.alias_noalias++;
1687 alias_stats.simple_resolved++;
1688 return false;
1691 /* If -fargument-noalias-global is >1, pointer arguments may
1692 not point to global variables. */
1693 if (flag_argument_noalias > 1 && is_global_var (var)
1694 && TREE_CODE (ptr) == PARM_DECL)
1696 alias_stats.alias_noalias++;
1697 alias_stats.simple_resolved++;
1698 return false;
1701 /* If either MEM or VAR is a read-only global and the other one
1702 isn't, then PTR cannot point to VAR. */
1703 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1704 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1706 alias_stats.alias_noalias++;
1707 alias_stats.simple_resolved++;
1708 return false;
1711 m_ann = var_ann (mem);
1713 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1715 alias_stats.tbaa_queries++;
1717 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1718 PTR can't possibly affect VAR. Note, that we are specifically testing
1719 for PTR's alias set here, not its pointed-to type. We also can't
1720 do this check with relaxed aliasing enabled. */
1721 if (POINTER_TYPE_P (TREE_TYPE (var))
1722 && var_alias_set != 0
1723 && mem_alias_set != 0)
1725 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1726 if (ptr_alias_set == var_alias_set)
1728 alias_stats.alias_noalias++;
1729 alias_stats.tbaa_resolved++;
1730 return false;
1734 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1735 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1737 alias_stats.alias_noalias++;
1738 alias_stats.tbaa_resolved++;
1739 return false;
1741 alias_stats.alias_mayalias++;
1742 return true;
1746 /* Add ALIAS to the set of variables that may alias VAR. */
1748 static void
1749 add_may_alias (tree var, tree alias)
1751 size_t i;
1752 var_ann_t v_ann = get_var_ann (var);
1753 var_ann_t a_ann = get_var_ann (alias);
1755 gcc_assert (var != alias);
1757 if (v_ann->may_aliases == NULL)
1758 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1760 /* Avoid adding duplicates. */
1761 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1762 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1763 return;
1765 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1766 FIXME, call-clobbering should only depend on whether an address
1767 escapes. It should be independent of aliasing. */
1768 if (is_call_clobbered (var))
1769 mark_call_clobbered (alias);
1771 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1772 else if (is_call_clobbered (alias))
1773 mark_call_clobbered (var);
1775 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1776 a_ann->is_alias_tag = 1;
1780 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1782 static void
1783 replace_may_alias (tree var, size_t i, tree new_alias)
1785 var_ann_t v_ann = var_ann (var);
1786 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1788 /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1789 FIXME, call-clobbering should only depend on whether an address
1790 escapes. It should be independent of aliasing. */
1791 if (is_call_clobbered (var))
1792 mark_call_clobbered (new_alias);
1794 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1795 else if (is_call_clobbered (new_alias))
1796 mark_call_clobbered (var);
1800 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1802 static void
1803 set_pt_anything (tree ptr)
1805 struct ptr_info_def *pi = get_ptr_info (ptr);
1807 pi->pt_anything = 1;
1808 pi->pt_malloc = 0;
1810 /* The pointer used to have a name tag, but we now found it pointing
1811 to an arbitrary location. The name tag needs to be renamed and
1812 disassociated from PTR. */
1813 if (pi->name_mem_tag)
1815 mark_sym_for_renaming (pi->name_mem_tag);
1816 pi->name_mem_tag = NULL_TREE;
1821 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1823 static void
1824 set_pt_malloc (tree ptr)
1826 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1828 /* If the pointer has already been found to point to arbitrary
1829 memory locations, it is unsafe to mark it as pointing to malloc. */
1830 if (pi->pt_anything)
1831 return;
1833 pi->pt_malloc = 1;
1837 /* Given two different pointers DEST and ORIG. Merge the points-to
1838 information in ORIG into DEST. AI contains all the alias
1839 information collected up to this point. */
1841 static void
1842 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1844 struct ptr_info_def *dest_pi, *orig_pi;
1846 gcc_assert (dest != orig);
1848 /* Make sure we have points-to information for ORIG. */
1849 collect_points_to_info_for (ai, orig);
1851 dest_pi = get_ptr_info (dest);
1852 orig_pi = SSA_NAME_PTR_INFO (orig);
1854 if (orig_pi)
1856 gcc_assert (orig_pi != dest_pi);
1858 /* Notice that we never merge PT_MALLOC. This attribute is only
1859 true if the pointer is the result of a malloc() call.
1860 Otherwise, we can end up in this situation:
1862 P_i = malloc ();
1864 P_j = P_i + X;
1866 P_j would be marked as PT_MALLOC, however we currently do not
1867 handle cases of more than one pointer pointing to the same
1868 malloc'd area.
1870 FIXME: If the merging comes from an expression that preserves
1871 the PT_MALLOC attribute (copy assignment, address
1872 arithmetic), we ought to merge PT_MALLOC, but then both
1873 pointers would end up getting different name tags because
1874 create_name_tags is not smart enough to determine that the
1875 two come from the same malloc call. Copy propagation before
1876 aliasing should cure this. */
1877 dest_pi->pt_malloc = 0;
1878 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1879 set_pt_anything (dest);
1881 dest_pi->pt_null |= orig_pi->pt_null;
1883 if (!dest_pi->pt_anything
1884 && orig_pi->pt_vars
1885 && !bitmap_empty_p (orig_pi->pt_vars))
1887 if (dest_pi->pt_vars == NULL)
1889 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1890 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1892 else
1893 bitmap_ior_into (dest_pi->pt_vars, orig_pi->pt_vars);
1896 else
1897 set_pt_anything (dest);
1901 /* Add EXPR to the list of expressions pointed-to by PTR. */
1903 static void
1904 add_pointed_to_expr (struct alias_info *ai, tree ptr, tree expr)
1906 if (TREE_CODE (expr) == WITH_SIZE_EXPR)
1907 expr = TREE_OPERAND (expr, 0);
1909 get_ptr_info (ptr);
1911 if (TREE_CODE (expr) == CALL_EXPR
1912 && (call_expr_flags (expr) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1914 /* If EXPR is a malloc-like call, then the area pointed to PTR
1915 is guaranteed to not alias with anything else. */
1916 set_pt_malloc (ptr);
1918 else if (TREE_CODE (expr) == ADDR_EXPR)
1920 /* Found P_i = ADDR_EXPR */
1921 add_pointed_to_var (ai, ptr, expr);
1923 else if (TREE_CODE (expr) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (expr)))
1925 /* Found P_i = Q_j. */
1926 merge_pointed_to_info (ai, ptr, expr);
1928 else if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
1930 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1931 tree op0 = TREE_OPERAND (expr, 0);
1932 tree op1 = TREE_OPERAND (expr, 1);
1934 /* Both operands may be of pointer type. FIXME: Shouldn't
1935 we just expect PTR + OFFSET always? */
1936 if (POINTER_TYPE_P (TREE_TYPE (op0))
1937 && TREE_CODE (op0) != INTEGER_CST)
1939 if (TREE_CODE (op0) == SSA_NAME)
1940 merge_pointed_to_info (ai, ptr, op0);
1941 else if (TREE_CODE (op0) == ADDR_EXPR)
1942 add_pointed_to_var (ai, ptr, op0);
1943 else
1944 set_pt_anything (ptr);
1947 if (POINTER_TYPE_P (TREE_TYPE (op1))
1948 && TREE_CODE (op1) != INTEGER_CST)
1950 if (TREE_CODE (op1) == SSA_NAME)
1951 merge_pointed_to_info (ai, ptr, op1);
1952 else if (TREE_CODE (op1) == ADDR_EXPR)
1953 add_pointed_to_var (ai, ptr, op1);
1954 else
1955 set_pt_anything (ptr);
1958 /* Neither operand is a pointer? VAR can be pointing anywhere.
1959 FIXME: Shouldn't we asserting here? If we get here, we found
1960 PTR = INT_CST + INT_CST, which should not be a valid pointer
1961 expression. */
1962 if (!(POINTER_TYPE_P (TREE_TYPE (op0))
1963 && TREE_CODE (op0) != INTEGER_CST)
1964 && !(POINTER_TYPE_P (TREE_TYPE (op1))
1965 && TREE_CODE (op1) != INTEGER_CST))
1966 set_pt_anything (ptr);
1968 else if (integer_zerop (expr))
1970 /* EXPR is the NULL pointer. Mark PTR as pointing to NULL. */
1971 SSA_NAME_PTR_INFO (ptr)->pt_null = 1;
1973 else
1975 /* If we can't recognize the expression, assume that PTR may
1976 point anywhere. */
1977 set_pt_anything (ptr);
1982 /* If VALUE is of the form &DECL, add DECL to the set of variables
1983 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1984 PTR. AI points to the collected alias information. */
1986 static void
1987 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1989 struct ptr_info_def *pi = get_ptr_info (ptr);
1990 tree pt_var = NULL_TREE;
1991 HOST_WIDE_INT offset, size;
1992 tree addrop;
1993 size_t uid;
1994 tree ref;
1995 subvar_t svars;
1997 gcc_assert (TREE_CODE (value) == ADDR_EXPR);
1999 addrop = TREE_OPERAND (value, 0);
2000 if (REFERENCE_CLASS_P (addrop))
2001 pt_var = get_base_address (addrop);
2002 else
2003 pt_var = addrop;
2005 /* If this is a component_ref, see if we can get a smaller number of
2006 variables to take the address of. */
2007 if (TREE_CODE (addrop) == COMPONENT_REF
2008 && (ref = okay_component_ref_for_subvars (addrop, &offset ,&size)))
2010 subvar_t sv;
2011 svars = get_subvars_for_var (ref);
2013 uid = var_ann (pt_var)->uid;
2015 if (pi->pt_vars == NULL)
2016 pi->pt_vars = BITMAP_GGC_ALLOC ();
2017 /* If the variable is a global, mark the pointer as pointing to
2018 global memory (which will make its tag a global variable). */
2019 if (is_global_var (pt_var))
2020 pi->pt_global_mem = 1;
2022 for (sv = svars; sv; sv = sv->next)
2024 if (overlap_subvar (offset, size, sv, NULL))
2026 bitmap_set_bit (pi->pt_vars, var_ann (sv->var)->uid);
2027 bitmap_set_bit (ai->addresses_needed, var_ann (sv->var)->uid);
2031 else if (pt_var && SSA_VAR_P (pt_var))
2034 uid = var_ann (pt_var)->uid;
2036 if (pi->pt_vars == NULL)
2037 pi->pt_vars = BITMAP_GGC_ALLOC ();
2039 /* If this is an aggregate, we may have subvariables for it that need
2040 to be pointed to. */
2041 if (var_can_have_subvars (pt_var)
2042 && (svars = get_subvars_for_var (pt_var)))
2044 subvar_t sv;
2045 for (sv = svars; sv; sv = sv->next)
2047 uid = var_ann (sv->var)->uid;
2048 bitmap_set_bit (ai->addresses_needed, uid);
2049 bitmap_set_bit (pi->pt_vars, uid);
2052 else
2054 bitmap_set_bit (ai->addresses_needed, uid);
2055 bitmap_set_bit (pi->pt_vars, uid);
2058 /* If the variable is a global, mark the pointer as pointing to
2059 global memory (which will make its tag a global variable). */
2060 if (is_global_var (pt_var))
2061 pi->pt_global_mem = 1;
2066 /* Callback for walk_use_def_chains to gather points-to information from the
2067 SSA web.
2069 VAR is an SSA variable or a GIMPLE expression.
2071 STMT is the statement that generates the SSA variable or, if STMT is a
2072 PHI_NODE, VAR is one of the PHI arguments.
2074 DATA is a pointer to a structure of type ALIAS_INFO. */
2076 static bool
2077 collect_points_to_info_r (tree var, tree stmt, void *data)
2079 struct alias_info *ai = (struct alias_info *) data;
2081 if (dump_file && (dump_flags & TDF_DETAILS))
2083 fprintf (dump_file, "Visiting use-def links for ");
2084 print_generic_expr (dump_file, var, dump_flags);
2085 fprintf (dump_file, "\n");
2088 switch (TREE_CODE (stmt))
2090 case RETURN_EXPR:
2091 gcc_assert (TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR);
2092 stmt = TREE_OPERAND (stmt, 0);
2093 /* FALLTHRU */
2095 case MODIFY_EXPR:
2097 tree rhs = TREE_OPERAND (stmt, 1);
2098 STRIP_NOPS (rhs);
2099 add_pointed_to_expr (ai, var, rhs);
2100 break;
2103 case ASM_EXPR:
2104 /* Pointers defined by __asm__ statements can point anywhere. */
2105 set_pt_anything (var);
2106 break;
2108 case NOP_EXPR:
2109 if (IS_EMPTY_STMT (stmt))
2111 tree decl = SSA_NAME_VAR (var);
2113 if (TREE_CODE (decl) == PARM_DECL)
2114 add_pointed_to_expr (ai, var, decl);
2115 else if (DECL_INITIAL (decl))
2116 add_pointed_to_expr (ai, var, DECL_INITIAL (decl));
2117 else
2118 add_pointed_to_expr (ai, var, decl);
2120 break;
2122 case PHI_NODE:
2124 /* It STMT is a PHI node, then VAR is one of its arguments. The
2125 variable that we are analyzing is the LHS of the PHI node. */
2126 tree lhs = PHI_RESULT (stmt);
2128 switch (TREE_CODE (var))
2130 case ADDR_EXPR:
2131 add_pointed_to_var (ai, lhs, var);
2132 break;
2134 case SSA_NAME:
2135 /* Avoid unnecessary merges. */
2136 if (lhs != var)
2137 merge_pointed_to_info (ai, lhs, var);
2138 break;
2140 default:
2141 gcc_assert (is_gimple_min_invariant (var));
2142 add_pointed_to_expr (ai, lhs, var);
2143 break;
2145 break;
2148 default:
2149 gcc_unreachable ();
2152 return false;
2156 /* Return true if STMT is an "escape" site from the current function. Escape
2157 sites those statements which might expose the address of a variable
2158 outside the current function. STMT is an escape site iff:
2160 1- STMT is a function call, or
2161 2- STMT is an __asm__ expression, or
2162 3- STMT is an assignment to a non-local variable, or
2163 4- STMT is a return statement.
2165 AI points to the alias information collected so far. */
2167 static bool
2168 is_escape_site (tree stmt, struct alias_info *ai)
2170 tree call = get_call_expr_in (stmt);
2171 if (call != NULL_TREE)
2173 ai->num_calls_found++;
2175 if (!TREE_SIDE_EFFECTS (call))
2176 ai->num_pure_const_calls_found++;
2178 return true;
2180 else if (TREE_CODE (stmt) == ASM_EXPR)
2181 return true;
2182 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2184 tree lhs = TREE_OPERAND (stmt, 0);
2186 /* Get to the base of _REF nodes. */
2187 if (TREE_CODE (lhs) != SSA_NAME)
2188 lhs = get_base_address (lhs);
2190 /* If we couldn't recognize the LHS of the assignment, assume that it
2191 is a non-local store. */
2192 if (lhs == NULL_TREE)
2193 return true;
2195 /* If the RHS is a conversion between a pointer and an integer, the
2196 pointer escapes since we can't track the integer. */
2197 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2198 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2199 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2200 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
2201 (TREE_OPERAND (stmt, 1), 0)))
2202 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
2203 return true;
2205 /* If the LHS is an SSA name, it can't possibly represent a non-local
2206 memory store. */
2207 if (TREE_CODE (lhs) == SSA_NAME)
2208 return false;
2210 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2211 local variables we cannot be sure if it will escape, because we
2212 don't have information about objects not in SSA form. Need to
2213 implement something along the lines of
2215 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2216 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2217 Conference on Object-Oriented Programming Systems, Languages, and
2218 Applications (OOPSLA), pp. 1-19, 1999. */
2219 return true;
2221 else if (TREE_CODE (stmt) == RETURN_EXPR)
2222 return true;
2224 return false;
2228 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2229 is considered to represent all the pointers whose pointed-to types are
2230 in the same alias set class. Otherwise, the tag represents a single
2231 SSA_NAME pointer variable. */
2233 static tree
2234 create_memory_tag (tree type, bool is_type_tag)
2236 var_ann_t ann;
2237 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2239 /* By default, memory tags are local variables. Alias analysis will
2240 determine whether they should be considered globals. */
2241 DECL_CONTEXT (tag) = current_function_decl;
2243 /* Memory tags are by definition addressable. This also prevents
2244 is_gimple_ref frome confusing memory tags with optimizable
2245 variables. */
2246 TREE_ADDRESSABLE (tag) = 1;
2248 ann = get_var_ann (tag);
2249 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2250 ann->type_mem_tag = NULL_TREE;
2252 /* Add the tag to the symbol table. */
2253 add_referenced_tmp_var (tag);
2255 return tag;
2259 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2260 This is used if P_i has been found to point to a specific set of
2261 variables or to a non-aliased memory location like the address returned
2262 by malloc functions. */
2264 static tree
2265 get_nmt_for (tree ptr)
2267 struct ptr_info_def *pi = get_ptr_info (ptr);
2268 tree tag = pi->name_mem_tag;
2270 if (tag == NULL_TREE)
2271 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2273 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
2274 then its name tag should be considered a global variable. */
2275 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
2276 || pi->pt_malloc
2277 || pi->pt_global_mem)
2278 mark_call_clobbered (tag);
2280 return tag;
2284 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2285 artificial variable that represents the memory location pointed-to by
2286 PTR. It is used to model the effects of pointer de-references on
2287 addressable variables.
2289 AI points to the data gathered during alias analysis. This function
2290 populates the array AI->POINTERS. */
2292 static tree
2293 get_tmt_for (tree ptr, struct alias_info *ai)
2295 size_t i;
2296 tree tag;
2297 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2298 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2300 /* To avoid creating unnecessary memory tags, only create one memory tag
2301 per alias set class. Note that it may be tempting to group
2302 memory tags based on conflicting alias sets instead of
2303 equivalence. That would be wrong because alias sets are not
2304 necessarily transitive (as demonstrated by the libstdc++ test
2305 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2306 such that conflicts (A, B) == true and conflicts (A, C) == true,
2307 it does not necessarily follow that conflicts (B, C) == true. */
2308 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2310 struct alias_map_d *curr = ai->pointers[i];
2311 tree curr_tag = var_ann (curr->var)->type_mem_tag;
2312 if (tag_set == curr->set
2313 && TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (curr_tag)))
2315 tag = curr_tag;
2316 break;
2320 /* If VAR cannot alias with any of the existing memory tags, create a new
2321 tag for PTR and add it to the POINTERS array. */
2322 if (tag == NULL_TREE)
2324 struct alias_map_d *alias_map;
2326 /* If PTR did not have a type tag already, create a new TMT.*
2327 artificial variable representing the memory location
2328 pointed-to by PTR. */
2329 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2330 tag = create_memory_tag (tag_type, true);
2331 else
2332 tag = var_ann (ptr)->type_mem_tag;
2334 /* Add PTR to the POINTERS array. Note that we are not interested in
2335 PTR's alias set. Instead, we cache the alias set for the memory that
2336 PTR points to. */
2337 alias_map = xcalloc (1, sizeof (*alias_map));
2338 alias_map->var = ptr;
2339 alias_map->set = tag_set;
2340 ai->pointers[ai->num_pointers++] = alias_map;
2343 /* If the pointed-to type is volatile, so is the tag. */
2344 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2346 /* Make sure that the type tag has the same alias set as the
2347 pointed-to type. */
2348 gcc_assert (tag_set == get_alias_set (tag));
2350 /* If PTR's pointed-to type is read-only, then TAG's type must also
2351 be read-only. */
2352 gcc_assert (TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (tag)));
2354 return tag;
2358 /* Create GLOBAL_VAR, an artificial global variable to act as a
2359 representative of all the variables that may be clobbered by function
2360 calls. */
2362 static void
2363 create_global_var (void)
2365 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2366 void_type_node);
2367 DECL_ARTIFICIAL (global_var) = 1;
2368 TREE_READONLY (global_var) = 0;
2369 DECL_EXTERNAL (global_var) = 1;
2370 TREE_STATIC (global_var) = 1;
2371 TREE_USED (global_var) = 1;
2372 DECL_CONTEXT (global_var) = NULL_TREE;
2373 TREE_THIS_VOLATILE (global_var) = 0;
2374 TREE_ADDRESSABLE (global_var) = 0;
2376 add_referenced_tmp_var (global_var);
2377 mark_sym_for_renaming (global_var);
2381 /* Dump alias statistics on FILE. */
2383 static void
2384 dump_alias_stats (FILE *file)
2386 const char *funcname
2387 = lang_hooks.decl_printable_name (current_function_decl, 2);
2388 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2389 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2390 fprintf (file, "Total alias mayalias results:\t%u\n",
2391 alias_stats.alias_mayalias);
2392 fprintf (file, "Total alias noalias results:\t%u\n",
2393 alias_stats.alias_noalias);
2394 fprintf (file, "Total simple queries:\t%u\n",
2395 alias_stats.simple_queries);
2396 fprintf (file, "Total simple resolved:\t%u\n",
2397 alias_stats.simple_resolved);
2398 fprintf (file, "Total TBAA queries:\t%u\n",
2399 alias_stats.tbaa_queries);
2400 fprintf (file, "Total TBAA resolved:\t%u\n",
2401 alias_stats.tbaa_resolved);
2405 /* Dump alias information on FILE. */
2407 void
2408 dump_alias_info (FILE *file)
2410 size_t i;
2411 const char *funcname
2412 = lang_hooks.decl_printable_name (current_function_decl, 2);
2414 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2416 fprintf (file, "Aliased symbols\n\n");
2417 for (i = 0; i < num_referenced_vars; i++)
2419 tree var = referenced_var (i);
2420 if (may_be_aliased (var))
2421 dump_variable (file, var);
2424 fprintf (file, "\nDereferenced pointers\n\n");
2425 for (i = 0; i < num_referenced_vars; i++)
2427 tree var = referenced_var (i);
2428 var_ann_t ann = var_ann (var);
2429 if (ann->type_mem_tag)
2430 dump_variable (file, var);
2433 fprintf (file, "\nType memory tags\n\n");
2434 for (i = 0; i < num_referenced_vars; i++)
2436 tree var = referenced_var (i);
2437 var_ann_t ann = var_ann (var);
2438 if (ann->mem_tag_kind == TYPE_TAG)
2439 dump_variable (file, var);
2442 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2444 fprintf (file, "SSA_NAME pointers\n\n");
2445 for (i = 1; i < num_ssa_names; i++)
2447 tree ptr = ssa_name (i);
2448 struct ptr_info_def *pi;
2450 if (ptr == NULL_TREE)
2451 continue;
2453 pi = SSA_NAME_PTR_INFO (ptr);
2454 if (!SSA_NAME_IN_FREE_LIST (ptr)
2455 && pi
2456 && pi->name_mem_tag)
2457 dump_points_to_info_for (file, ptr);
2460 fprintf (file, "\nName memory tags\n\n");
2461 for (i = 0; i < num_referenced_vars; i++)
2463 tree var = referenced_var (i);
2464 var_ann_t ann = var_ann (var);
2465 if (ann->mem_tag_kind == NAME_TAG)
2466 dump_variable (file, var);
2469 fprintf (file, "\n");
2473 /* Dump alias information on stderr. */
2475 void
2476 debug_alias_info (void)
2478 dump_alias_info (stderr);
2482 /* Return the alias information associated with pointer T. It creates a
2483 new instance if none existed. */
2485 struct ptr_info_def *
2486 get_ptr_info (tree t)
2488 struct ptr_info_def *pi;
2490 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2492 pi = SSA_NAME_PTR_INFO (t);
2493 if (pi == NULL)
2495 pi = ggc_alloc (sizeof (*pi));
2496 memset ((void *)pi, 0, sizeof (*pi));
2497 SSA_NAME_PTR_INFO (t) = pi;
2500 return pi;
2504 /* Dump points-to information for SSA_NAME PTR into FILE. */
2506 void
2507 dump_points_to_info_for (FILE *file, tree ptr)
2509 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2511 print_generic_expr (file, ptr, dump_flags);
2513 if (pi)
2515 if (pi->name_mem_tag)
2517 fprintf (file, ", name memory tag: ");
2518 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2521 if (pi->is_dereferenced)
2522 fprintf (file, ", is dereferenced");
2524 if (pi->value_escapes_p)
2525 fprintf (file, ", its value escapes");
2527 if (pi->pt_anything)
2528 fprintf (file, ", points-to anything");
2530 if (pi->pt_malloc)
2531 fprintf (file, ", points-to malloc");
2533 if (pi->pt_null)
2534 fprintf (file, ", points-to NULL");
2536 if (pi->pt_vars)
2538 unsigned ix;
2539 bitmap_iterator bi;
2541 fprintf (file, ", points-to vars: { ");
2542 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2544 print_generic_expr (file, referenced_var (ix), dump_flags);
2545 fprintf (file, " ");
2547 fprintf (file, "}");
2551 fprintf (file, "\n");
2555 /* Dump points-to information for VAR into stderr. */
2557 void
2558 debug_points_to_info_for (tree var)
2560 dump_points_to_info_for (stderr, var);
2564 /* Dump points-to information into FILE. NOTE: This function is slow, as
2565 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2567 void
2568 dump_points_to_info (FILE *file)
2570 basic_block bb;
2571 block_stmt_iterator si;
2572 size_t i;
2573 ssa_op_iter iter;
2574 const char *fname =
2575 lang_hooks.decl_printable_name (current_function_decl, 2);
2577 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2579 /* First dump points-to information for the default definitions of
2580 pointer variables. This is necessary because default definitions are
2581 not part of the code. */
2582 for (i = 0; i < num_referenced_vars; i++)
2584 tree var = referenced_var (i);
2585 if (POINTER_TYPE_P (TREE_TYPE (var)))
2587 var_ann_t ann = var_ann (var);
2588 if (ann->default_def)
2589 dump_points_to_info_for (file, ann->default_def);
2593 /* Dump points-to information for every pointer defined in the program. */
2594 FOR_EACH_BB (bb)
2596 tree phi;
2598 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2600 tree ptr = PHI_RESULT (phi);
2601 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2602 dump_points_to_info_for (file, ptr);
2605 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2607 tree stmt = bsi_stmt (si);
2608 tree def;
2609 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2610 if (POINTER_TYPE_P (TREE_TYPE (def)))
2611 dump_points_to_info_for (file, def);
2615 fprintf (file, "\n");
2619 /* Dump points-to info pointed by PTO into STDERR. */
2621 void
2622 debug_points_to_info (void)
2624 dump_points_to_info (stderr);
2627 /* Dump to FILE the list of variables that may be aliasing VAR. */
2629 void
2630 dump_may_aliases_for (FILE *file, tree var)
2632 varray_type aliases;
2634 if (TREE_CODE (var) == SSA_NAME)
2635 var = SSA_NAME_VAR (var);
2637 aliases = var_ann (var)->may_aliases;
2638 if (aliases)
2640 size_t i;
2641 fprintf (file, "{ ");
2642 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2644 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2645 fprintf (file, " ");
2647 fprintf (file, "}");
2652 /* Dump to stderr the list of variables that may be aliasing VAR. */
2654 void
2655 debug_may_aliases_for (tree var)
2657 dump_may_aliases_for (stderr, var);
2660 /* Return true if VAR may be aliased. */
2662 bool
2663 may_be_aliased (tree var)
2665 /* Obviously. */
2666 if (TREE_ADDRESSABLE (var))
2667 return true;
2669 /* Globally visible variables can have their addresses taken by other
2670 translation units. */
2671 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2672 return true;
2674 /* Automatic variables can't have their addresses escape any other way.
2675 This must be after the check for global variables, as extern declarations
2676 do not have TREE_STATIC set. */
2677 if (!TREE_STATIC (var))
2678 return false;
2680 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2681 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2682 we can only be sure the variable isn't addressable if it's local to the
2683 current function. */
2684 if (flag_unit_at_a_time)
2685 return false;
2686 if (decl_function_context (var) == current_function_decl)
2687 return false;
2689 return true;
2693 /* Add VAR to the list of may-aliases of PTR's type tag. If PTR
2694 doesn't already have a type tag, create one. */
2696 void
2697 add_type_alias (tree ptr, tree var)
2699 varray_type aliases;
2700 tree tag;
2701 var_ann_t ann = var_ann (ptr);
2702 subvar_t svars;
2704 if (ann->type_mem_tag == NULL_TREE)
2706 size_t i;
2707 tree q = NULL_TREE;
2708 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2709 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2711 /* PTR doesn't have a type tag, create a new one and add VAR to
2712 the new tag's alias set.
2714 FIXME, This is slower than necessary. We need to determine
2715 whether there is another pointer Q with the same alias set as
2716 PTR. This could be sped up by having type tags associated
2717 with types. */
2718 for (i = 0; i < num_referenced_vars; i++)
2720 q = referenced_var (i);
2722 if (POINTER_TYPE_P (TREE_TYPE (q))
2723 && tag_set == get_alias_set (TREE_TYPE (TREE_TYPE (q))))
2725 /* Found another pointer Q with the same alias set as
2726 the PTR's pointed-to type. If Q has a type tag, use
2727 it. Otherwise, create a new memory tag for PTR. */
2728 var_ann_t ann1 = var_ann (q);
2729 if (ann1->type_mem_tag)
2730 ann->type_mem_tag = ann1->type_mem_tag;
2731 else
2732 ann->type_mem_tag = create_memory_tag (tag_type, true);
2733 goto found_tag;
2737 /* Couldn't find any other pointer with a type tag we could use.
2738 Create a new memory tag for PTR. */
2739 ann->type_mem_tag = create_memory_tag (tag_type, true);
2742 found_tag:
2743 /* If VAR is not already PTR's type tag, add it to the may-alias set
2744 for PTR's type tag. */
2745 gcc_assert (var_ann (var)->type_mem_tag == NOT_A_TAG);
2746 tag = ann->type_mem_tag;
2748 /* If VAR has subvars, add the subvars to the tag instead of the
2749 actual var. */
2750 if (var_can_have_subvars (var)
2751 && (svars = get_subvars_for_var (var)))
2753 subvar_t sv;
2754 for (sv = svars; sv; sv = sv->next)
2755 add_may_alias (tag, sv->var);
2757 else
2758 add_may_alias (tag, var);
2760 /* TAG and its set of aliases need to be marked for renaming. */
2761 mark_sym_for_renaming (tag);
2762 if ((aliases = var_ann (tag)->may_aliases) != NULL)
2764 size_t i;
2765 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2766 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2769 /* If we had grouped aliases, VAR may have aliases of its own. Mark
2770 them for renaming as well. Other statements referencing the
2771 aliases of VAR will need to be updated. */
2772 if ((aliases = var_ann (var)->may_aliases) != NULL)
2774 size_t i;
2775 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2776 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2781 /* Create a type tag for PTR. Construct the may-alias list of this type tag
2782 so that it has the aliasing of VAR. */
2784 void
2785 new_type_alias (tree ptr, tree var)
2787 var_ann_t p_ann = var_ann (ptr);
2788 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2789 var_ann_t v_ann = var_ann (var);
2790 tree tag;
2791 subvar_t svars;
2793 gcc_assert (p_ann->type_mem_tag == NULL_TREE);
2794 gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG);
2795 tag = create_memory_tag (tag_type, true);
2796 p_ann->type_mem_tag = tag;
2798 /* Add VAR to the may-alias set of PTR's new type tag. If VAR has
2799 subvars, add the subvars to the tag instead of the actual var. */
2800 if (var_can_have_subvars (var)
2801 && (svars = get_subvars_for_var (var)))
2803 subvar_t sv;
2804 for (sv = svars; sv; sv = sv->next)
2805 add_may_alias (tag, sv->var);
2807 else
2808 add_may_alias (tag, var);
2810 /* Note, TAG and its set of aliases are not marked for renaming. */
2814 /* This structure is simply used during pushing fields onto the fieldstack
2815 to track the offset of the field, since bitpos_of_field gives it relative
2816 to its immediate containing type, and we want it relative to the ultimate
2817 containing object. */
2819 typedef struct fieldoff
2821 tree field;
2822 HOST_WIDE_INT offset;
2823 } fieldoff_s;
2825 DEF_VEC_O (fieldoff_s);
2826 DEF_VEC_ALLOC_O(fieldoff_s,heap);
2828 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2829 structure.
2830 Return -1 if the position is conditional or otherwise non-constant
2831 integer. */
2833 static HOST_WIDE_INT
2834 bitpos_of_field (const tree fdecl)
2837 if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2838 || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2839 return -1;
2841 return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2842 + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2845 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all the fields
2846 of TYPE onto fieldstack, recording their offsets along the way.
2847 OFFSET is used to keep track of the offset in this entire structure, rather
2848 than just the immediately containing structure. Returns the number
2849 of fields pushed. */
2851 static int
2852 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
2853 HOST_WIDE_INT offset)
2855 tree field;
2856 int count = 0;
2858 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2859 if (TREE_CODE (field) == FIELD_DECL)
2861 bool push = false;
2863 if (!var_can_have_subvars (field))
2864 push = true;
2865 else if (!(push_fields_onto_fieldstack
2866 (TREE_TYPE (field), fieldstack,
2867 offset + bitpos_of_field (field)))
2868 && DECL_SIZE (field)
2869 && !integer_zerop (DECL_SIZE (field)))
2870 /* Empty structures may have actual size, like in C++. So
2871 see if we didn't push any subfields and the size is
2872 nonzero, push the field onto the stack */
2873 push = true;
2875 if (push)
2877 fieldoff_s *pair;
2879 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
2880 pair->field = field;
2881 pair->offset = offset + bitpos_of_field (field);
2882 count++;
2885 return count;
2889 /* This represents the used range of a variable. */
2891 typedef struct used_part
2893 HOST_WIDE_INT minused;
2894 HOST_WIDE_INT maxused;
2895 /* True if we have an explicit use/def of some portion of this variable,
2896 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2897 bool explicit_uses;
2898 /* True if we have an implicit use/def of some portion of this
2899 variable. Implicit uses occur when we can't tell what part we
2900 are referencing, and have to make conservative assumptions. */
2901 bool implicit_uses;
2902 } *used_part_t;
2904 /* An array of used_part structures, indexed by variable uid. */
2906 static used_part_t *used_portions;
2908 /* Given a variable uid, UID, get or create the entry in the used portions
2909 table for the variable. */
2911 static used_part_t
2912 get_or_create_used_part_for (size_t uid)
2914 used_part_t up;
2915 if (used_portions[uid] == NULL)
2917 up = xcalloc (1, sizeof (struct used_part));
2918 up->minused = INT_MAX;
2919 up->maxused = 0;
2920 up->explicit_uses = false;
2921 up->implicit_uses = false;
2923 else
2924 up = used_portions[uid];
2925 return up;
2928 /* qsort comparison function for two fieldoff's PA and PB */
2930 static int
2931 fieldoff_compare (const void *pa, const void *pb)
2933 const fieldoff_s *foa = (const fieldoff_s *)pa;
2934 const fieldoff_s *fob = (const fieldoff_s *)pb;
2935 HOST_WIDE_INT foasize, fobsize;
2937 if (foa->offset != fob->offset)
2938 return foa->offset - fob->offset;
2940 foasize = TREE_INT_CST_LOW (DECL_SIZE (foa->field));
2941 fobsize = TREE_INT_CST_LOW (DECL_SIZE (fob->field));
2942 return foasize - fobsize;
2945 /* Given an aggregate VAR, create the subvariables that represent its
2946 fields. */
2948 static void
2949 create_overlap_variables_for (tree var)
2951 VEC(fieldoff_s,heap) *fieldstack = NULL;
2952 used_part_t up;
2953 size_t uid = var_ann (var)->uid;
2955 if (used_portions[uid] == NULL)
2956 return;
2958 up = used_portions[uid];
2959 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0);
2960 if (VEC_length (fieldoff_s, fieldstack) != 0)
2962 subvar_t *subvars;
2963 fieldoff_s *fo;
2964 bool notokay = false;
2965 int fieldcount = 0;
2966 int i;
2967 HOST_WIDE_INT lastfooffset = -1;
2968 HOST_WIDE_INT lastfosize = -1;
2969 tree lastfotype = NULL_TREE;
2971 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2972 know their size, and thus, can't handle.
2973 The same is true of fields with DECL_SIZE that is not an integer
2974 constant (such as variable sized fields).
2975 Fields with offsets which are not constant will have an offset < 0
2976 We *could* handle fields that are constant sized arrays, but
2977 currently don't. Doing so would require some extra changes to
2978 tree-ssa-operands.c. */
2980 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
2982 if (!DECL_SIZE (fo->field)
2983 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
2984 || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
2985 || fo->offset < 0)
2987 notokay = true;
2988 break;
2990 fieldcount++;
2993 /* The current heuristic we use is as follows:
2994 If the variable has no used portions in this function, no
2995 structure vars are created for it.
2996 Otherwise,
2997 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
2998 we always create structure vars for them.
2999 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3000 some explicit uses, we create structure vars for them.
3001 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3002 no explicit uses, we do not create structure vars for them.
3005 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
3006 && !up->explicit_uses)
3008 if (dump_file && (dump_flags & TDF_DETAILS))
3010 fprintf (dump_file, "Variable ");
3011 print_generic_expr (dump_file, var, 0);
3012 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
3014 notokay = true;
3017 /* Bail out, if we can't create overlap variables. */
3018 if (notokay)
3020 VEC_free (fieldoff_s, heap, fieldstack);
3021 return;
3024 /* Otherwise, create the variables. */
3025 subvars = lookup_subvars_for_var (var);
3027 qsort (VEC_address (fieldoff_s, fieldstack),
3028 VEC_length (fieldoff_s, fieldstack),
3029 sizeof (fieldoff_s),
3030 fieldoff_compare);
3032 for (i = VEC_length (fieldoff_s, fieldstack);
3033 VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
3035 subvar_t sv;
3036 HOST_WIDE_INT fosize;
3037 var_ann_t ann;
3038 tree currfotype;
3040 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
3041 currfotype = TREE_TYPE (fo->field);
3043 /* If this field isn't in the used portion,
3044 or it has the exact same offset and size as the last
3045 field, skip it. */
3047 if (((fo->offset <= up->minused
3048 && fo->offset + fosize <= up->minused)
3049 || fo->offset >= up->maxused)
3050 || (fo->offset == lastfooffset
3051 && fosize == lastfosize
3052 && currfotype == lastfotype))
3053 continue;
3054 sv = ggc_alloc (sizeof (struct subvar));
3055 sv->offset = fo->offset;
3056 sv->size = fosize;
3057 sv->next = *subvars;
3058 sv->var = create_tmp_var_raw (TREE_TYPE (fo->field), "SFT");
3059 if (dump_file)
3061 fprintf (dump_file, "structure field tag %s created for var %s",
3062 get_name (sv->var), get_name (var));
3063 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
3064 sv->offset);
3065 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
3066 sv->size);
3067 fprintf (dump_file, "\n");
3070 /* We need to copy the various flags from var to sv->var, so that
3071 they are is_global_var iff the original variable was. */
3073 DECL_EXTERNAL (sv->var) = DECL_EXTERNAL (var);
3074 TREE_PUBLIC (sv->var) = TREE_PUBLIC (var);
3075 TREE_STATIC (sv->var) = TREE_STATIC (var);
3076 TREE_READONLY (sv->var) = TREE_READONLY (var);
3078 /* Like other memory tags, these need to be marked addressable to
3079 keep is_gimple_reg from thinking they are real. */
3080 TREE_ADDRESSABLE (sv->var) = 1;
3082 DECL_CONTEXT (sv->var) = DECL_CONTEXT (var);
3084 ann = get_var_ann (sv->var);
3085 ann->mem_tag_kind = STRUCT_FIELD;
3086 ann->type_mem_tag = NULL;
3087 add_referenced_tmp_var (sv->var);
3089 lastfotype = currfotype;
3090 lastfooffset = fo->offset;
3091 lastfosize = fosize;
3092 *subvars = sv;
3095 /* Once we have created subvars, the original is no longer call
3096 clobbered on its own. Its call clobbered status depends
3097 completely on the call clobbered status of the subvars.
3099 add_referenced_var in the above loop will take care of
3100 marking subvars of global variables as call clobbered for us
3101 to start, since they are global as well. */
3102 clear_call_clobbered (var);
3105 VEC_free (fieldoff_s, heap, fieldstack);
3109 /* Find the conservative answer to the question of what portions of what
3110 structures are used by this statement. We assume that if we have a
3111 component ref with a known size + offset, that we only need that part
3112 of the structure. For unknown cases, or cases where we do something
3113 to the whole structure, we assume we need to create fields for the
3114 entire structure. */
3116 static tree
3117 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3119 switch (TREE_CODE (*tp))
3121 case COMPONENT_REF:
3123 HOST_WIDE_INT bitsize;
3124 HOST_WIDE_INT bitpos;
3125 tree offset;
3126 enum machine_mode mode;
3127 int unsignedp;
3128 int volatilep;
3129 tree ref;
3130 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
3131 &unsignedp, &volatilep, false);
3132 if (DECL_P (ref) && offset == NULL && bitsize != -1)
3134 size_t uid = var_ann (ref)->uid;
3135 used_part_t up;
3137 up = get_or_create_used_part_for (uid);
3139 if (bitpos <= up->minused)
3140 up->minused = bitpos;
3141 if ((bitpos + bitsize >= up->maxused))
3142 up->maxused = bitpos + bitsize;
3144 up->explicit_uses = true;
3145 used_portions[uid] = up;
3147 *walk_subtrees = 0;
3148 return NULL_TREE;
3150 else if (DECL_P (ref))
3152 if (DECL_SIZE (ref)
3153 && var_can_have_subvars (ref)
3154 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
3156 used_part_t up;
3157 size_t uid = var_ann (ref)->uid;
3159 up = get_or_create_used_part_for (uid);
3161 up->minused = 0;
3162 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
3164 up->implicit_uses = true;
3166 used_portions[uid] = up;
3168 *walk_subtrees = 0;
3169 return NULL_TREE;
3173 break;
3174 case VAR_DECL:
3175 case PARM_DECL:
3177 tree var = *tp;
3178 if (DECL_SIZE (var)
3179 && var_can_have_subvars (var)
3180 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3182 used_part_t up;
3183 size_t uid = var_ann (var)->uid;
3185 up = get_or_create_used_part_for (uid);
3187 up->minused = 0;
3188 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3189 up->implicit_uses = true;
3191 used_portions[uid] = up;
3192 *walk_subtrees = 0;
3193 return NULL_TREE;
3196 break;
3198 default:
3199 break;
3202 return NULL_TREE;
3205 /* We are about to create some new referenced variables, and we need the
3206 before size. */
3208 static size_t old_referenced_vars;
3211 /* Create structure field variables for structures used in this function. */
3213 static void
3214 create_structure_vars (void)
3216 basic_block bb;
3217 size_t i;
3219 old_referenced_vars = num_referenced_vars;
3220 used_portions = xcalloc (num_referenced_vars, sizeof (used_part_t));
3222 FOR_EACH_BB (bb)
3224 block_stmt_iterator bsi;
3225 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3227 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3228 find_used_portions,
3229 NULL);
3232 for (i = 0; i < old_referenced_vars; i++)
3234 tree var = referenced_var (i);
3235 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3236 if (var
3237 && DECL_SIZE (var)
3238 && var_can_have_subvars (var)
3239 && var_ann (var)->mem_tag_kind == NOT_A_TAG
3240 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3241 create_overlap_variables_for (var);
3243 for (i = 0; i < old_referenced_vars; i++)
3244 free (used_portions[i]);
3246 free (used_portions);
3249 static bool
3250 gate_structure_vars (void)
3252 return flag_tree_salias != 0;
3255 struct tree_opt_pass pass_create_structure_vars =
3257 "salias", /* name */
3258 gate_structure_vars, /* gate */
3259 create_structure_vars, /* execute */
3260 NULL, /* sub */
3261 NULL, /* next */
3262 0, /* static_pass_number */
3263 0, /* tv_id */
3264 PROP_cfg, /* properties_required */
3265 0, /* properties_provided */
3266 0, /* properties_destroyed */
3267 0, /* todo_flags_start */
3268 TODO_dump_func, /* todo_flags_finish */
3269 0 /* letter */