* arm.c (adjacent_mem_locations): Reject volatile memory refs.
[official-gcc.git] / gcc / tree-ssa-alias.c
blob0327e3441b297ba4d981f916c906a553fd2e76ec
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 struct tree_opt_pass pass_may_alias =
350 "alias", /* name */
351 NULL, /* gate */
352 compute_may_aliases, /* execute */
353 NULL, /* sub */
354 NULL, /* next */
355 0, /* static_pass_number */
356 TV_TREE_MAY_ALIAS, /* tv_id */
357 PROP_cfg | PROP_ssa, /* properties_required */
358 PROP_alias, /* properties_provided */
359 0, /* properties_destroyed */
360 0, /* todo_flags_start */
361 TODO_dump_func | TODO_rename_vars
362 | TODO_ggc_collect | TODO_verify_ssa
363 | TODO_verify_stmts, /* todo_flags_finish */
364 0 /* letter */
368 /* Data structure used to count the number of dereferences to PTR
369 inside an expression. */
370 struct count_ptr_d
372 tree ptr;
373 unsigned count;
377 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
378 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
380 static tree
381 count_ptr_derefs (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
383 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
385 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
386 count_p->count++;
388 return NULL_TREE;
392 /* Count the number of direct and indirect uses for pointer PTR in
393 statement STMT. The two counts are stored in *NUM_USES_P and
394 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
395 least one of those dereferences is a store operation. */
397 static void
398 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
399 unsigned *num_derefs_p, bool *is_store)
401 ssa_op_iter i;
402 tree use;
404 *num_uses_p = 0;
405 *num_derefs_p = 0;
406 *is_store = false;
408 /* Find out the total number of uses of PTR in STMT. */
409 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
410 if (use == ptr)
411 (*num_uses_p)++;
413 /* Now count the number of indirect references to PTR. This is
414 truly awful, but we don't have much choice. There are no parent
415 pointers inside INDIRECT_REFs, so an expression like
416 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
417 find all the indirect and direct uses of x_1 inside. The only
418 shortcut we can take is the fact that GIMPLE only allows
419 INDIRECT_REFs inside the expressions below. */
420 if (TREE_CODE (stmt) == MODIFY_EXPR
421 || (TREE_CODE (stmt) == RETURN_EXPR
422 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
423 || TREE_CODE (stmt) == ASM_EXPR
424 || TREE_CODE (stmt) == CALL_EXPR)
426 tree lhs, rhs;
428 if (TREE_CODE (stmt) == MODIFY_EXPR)
430 lhs = TREE_OPERAND (stmt, 0);
431 rhs = TREE_OPERAND (stmt, 1);
433 else if (TREE_CODE (stmt) == RETURN_EXPR)
435 tree e = TREE_OPERAND (stmt, 0);
436 lhs = TREE_OPERAND (e, 0);
437 rhs = TREE_OPERAND (e, 1);
439 else if (TREE_CODE (stmt) == ASM_EXPR)
441 lhs = ASM_OUTPUTS (stmt);
442 rhs = ASM_INPUTS (stmt);
444 else
446 lhs = NULL_TREE;
447 rhs = stmt;
450 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
452 struct count_ptr_d count;
453 count.ptr = ptr;
454 count.count = 0;
455 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
456 *is_store = true;
457 *num_derefs_p = count.count;
460 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
462 struct count_ptr_d count;
463 count.ptr = ptr;
464 count.count = 0;
465 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
466 *num_derefs_p += count.count;
470 gcc_assert (*num_uses_p >= *num_derefs_p);
474 /* Initialize the data structures used for alias analysis. */
476 static struct alias_info *
477 init_alias_info (void)
479 struct alias_info *ai;
481 ai = xcalloc (1, sizeof (struct alias_info));
482 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
483 sbitmap_zero (ai->ssa_names_visited);
484 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
485 ai->addresses_needed = BITMAP_ALLOC (NULL);
486 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
487 ai->written_vars = BITMAP_ALLOC (NULL);
488 ai->dereferenced_ptrs_store = BITMAP_ALLOC (NULL);
489 ai->dereferenced_ptrs_load = BITMAP_ALLOC (NULL);
491 /* If aliases have been computed before, clear existing information. */
492 if (aliases_computed_p)
494 unsigned i;
495 basic_block bb;
497 /* Make sure that every statement has a valid set of operands.
498 If a statement needs to be scanned for operands while we
499 compute aliases, it may get erroneous operands because all
500 the alias relations are not built at that point.
501 FIXME: This code will become obsolete when operands are not
502 lazily updated. */
503 FOR_EACH_BB (bb)
505 block_stmt_iterator si;
506 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
507 get_stmt_operands (bsi_stmt (si));
510 /* Similarly, clear the set of addressable variables. In this
511 case, we can just clear the set because addressability is
512 only computed here. */
513 bitmap_clear (addressable_vars);
515 /* Clear flow-insensitive alias information from each symbol. */
516 for (i = 0; i < num_referenced_vars; i++)
518 tree var = referenced_var (i);
519 var_ann_t ann = var_ann (var);
521 ann->is_alias_tag = 0;
522 ann->may_aliases = NULL;
524 /* Since we are about to re-discover call-clobbered
525 variables, clear the call-clobbered flag. Variables that
526 are intrinsically call-clobbered (globals, local statics,
527 etc) will not be marked by the aliasing code, so we can't
528 remove them from CALL_CLOBBERED_VARS.
530 NB: STRUCT_FIELDS are still call clobbered if they are for
531 a global variable, so we *don't* clear their call clobberedness
532 just because they are tags, though we will clear it if they
533 aren't for global variables. */
534 if (ann->mem_tag_kind == NAME_TAG
535 || ann->mem_tag_kind == TYPE_TAG
536 || !is_global_var (var))
537 clear_call_clobbered (var);
540 /* Clear flow-sensitive points-to information from each SSA name. */
541 for (i = 1; i < num_ssa_names; i++)
543 tree name = ssa_name (i);
545 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
546 continue;
548 if (SSA_NAME_PTR_INFO (name))
550 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
552 /* Clear all the flags but keep the name tag to
553 avoid creating new temporaries unnecessarily. If
554 this pointer is found to point to a subset or
555 superset of its former points-to set, then a new
556 tag will need to be created in create_name_tags. */
557 pi->pt_anything = 0;
558 pi->pt_malloc = 0;
559 pi->pt_null = 0;
560 pi->value_escapes_p = 0;
561 pi->is_dereferenced = 0;
562 if (pi->pt_vars)
563 bitmap_clear (pi->pt_vars);
568 /* Next time, we will need to reset alias information. */
569 aliases_computed_p = true;
571 return ai;
575 /* Deallocate memory used by alias analysis. */
577 static void
578 delete_alias_info (struct alias_info *ai)
580 size_t i;
582 sbitmap_free (ai->ssa_names_visited);
583 ai->processed_ptrs = NULL;
584 BITMAP_FREE (ai->addresses_needed);
586 for (i = 0; i < ai->num_addressable_vars; i++)
588 sbitmap_free (ai->addressable_vars[i]->may_aliases);
589 free (ai->addressable_vars[i]);
591 free (ai->addressable_vars);
593 for (i = 0; i < ai->num_pointers; i++)
595 sbitmap_free (ai->pointers[i]->may_aliases);
596 free (ai->pointers[i]);
598 free (ai->pointers);
600 ai->num_references = NULL;
601 BITMAP_FREE (ai->written_vars);
602 BITMAP_FREE (ai->dereferenced_ptrs_store);
603 BITMAP_FREE (ai->dereferenced_ptrs_load);
605 free (ai);
609 /* Walk use-def chains for pointer PTR to determine what variables is PTR
610 pointing to. */
612 static void
613 collect_points_to_info_for (struct alias_info *ai, tree ptr)
615 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
617 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
619 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
620 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
621 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
626 /* Traverse use-def links for all the pointers in the program to collect
627 address escape and points-to information.
629 This is loosely based on the same idea described in R. Hasti and S.
630 Horwitz, ``Using static single assignment form to improve
631 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
632 Programming Language Design and Implementation, pp. 97-105, 1998. */
634 static void
635 compute_points_to_and_addr_escape (struct alias_info *ai)
637 basic_block bb;
638 unsigned i;
639 tree op;
640 ssa_op_iter iter;
642 timevar_push (TV_TREE_PTA);
644 FOR_EACH_BB (bb)
646 bb_ann_t block_ann = bb_ann (bb);
647 block_stmt_iterator si;
649 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
651 bitmap addr_taken;
652 tree stmt = bsi_stmt (si);
653 bool stmt_escapes_p = is_escape_site (stmt, ai);
654 bitmap_iterator bi;
656 /* Mark all the variables whose address are taken by the
657 statement. Note that this will miss all the addresses taken
658 in PHI nodes (those are discovered while following the use-def
659 chains). */
660 get_stmt_operands (stmt);
661 addr_taken = addresses_taken (stmt);
662 if (addr_taken)
663 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
665 tree var = referenced_var (i);
666 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
667 if (stmt_escapes_p)
668 mark_call_clobbered (var);
671 if (stmt_escapes_p)
672 block_ann->has_escape_site = 1;
674 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
676 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
677 struct ptr_info_def *pi;
678 bool is_store;
679 unsigned num_uses, num_derefs;
681 /* If the operand's variable may be aliased, keep track
682 of how many times we've referenced it. This is used
683 for alias grouping in compute_flow_sensitive_aliasing.
684 Note that we don't need to grow AI->NUM_REFERENCES
685 because we are processing regular variables, not
686 memory tags (the array's initial size is set to
687 NUM_REFERENCED_VARS). */
688 if (may_be_aliased (SSA_NAME_VAR (op)))
689 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
691 if (!POINTER_TYPE_P (TREE_TYPE (op)))
692 continue;
694 collect_points_to_info_for (ai, op);
696 pi = SSA_NAME_PTR_INFO (op);
697 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs,
698 &is_store);
700 if (num_derefs > 0)
702 /* Mark OP as dereferenced. In a subsequent pass,
703 dereferenced pointers that point to a set of
704 variables will be assigned a name tag to alias
705 all the variables OP points to. */
706 pi->is_dereferenced = 1;
708 /* Keep track of how many time we've dereferenced each
709 pointer. Again, we don't need to grow
710 AI->NUM_REFERENCES because we're processing
711 existing program variables. */
712 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
714 /* If this is a store operation, mark OP as being
715 dereferenced to store, otherwise mark it as being
716 dereferenced to load. */
717 if (is_store)
718 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
719 else
720 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
723 if (stmt_escapes_p && num_derefs < num_uses)
725 /* If STMT is an escape point and STMT contains at
726 least one direct use of OP, then the value of OP
727 escapes and so the pointed-to variables need to
728 be marked call-clobbered. */
729 pi->value_escapes_p = 1;
731 /* If the statement makes a function call, assume
732 that pointer OP will be dereferenced in a store
733 operation inside the called function. */
734 if (get_call_expr_in (stmt))
736 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
737 pi->is_dereferenced = 1;
742 /* Update reference counter for definitions to any
743 potentially aliased variable. This is used in the alias
744 grouping heuristics. */
745 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
747 tree var = SSA_NAME_VAR (op);
748 var_ann_t ann = var_ann (var);
749 bitmap_set_bit (ai->written_vars, ann->uid);
750 if (may_be_aliased (var))
751 (VARRAY_UINT (ai->num_references, ann->uid))++;
753 if (POINTER_TYPE_P (TREE_TYPE (op)))
754 collect_points_to_info_for (ai, op);
757 /* Mark variables in V_MAY_DEF operands as being written to. */
758 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
760 tree var = SSA_NAME_VAR (op);
761 var_ann_t ann = var_ann (var);
762 bitmap_set_bit (ai->written_vars, ann->uid);
765 /* After promoting variables and computing aliasing we will
766 need to re-scan most statements. FIXME: Try to minimize the
767 number of statements re-scanned. It's not really necessary to
768 re-scan *all* statements. */
769 modify_stmt (stmt);
773 timevar_pop (TV_TREE_PTA);
777 /* Create name tags for all the pointers that have been dereferenced.
778 We only create a name tag for a pointer P if P is found to point to
779 a set of variables (so that we can alias them to *P) or if it is
780 the result of a call to malloc (which means that P cannot point to
781 anything else nor alias any other variable).
783 If two pointers P and Q point to the same set of variables, they
784 are assigned the same name tag. */
786 static void
787 create_name_tags (struct alias_info *ai)
789 size_t i;
791 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
793 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
794 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
796 if (pi->pt_anything || !pi->is_dereferenced)
798 /* No name tags for pointers that have not been
799 dereferenced or point to an arbitrary location. */
800 pi->name_mem_tag = NULL_TREE;
801 continue;
804 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
806 size_t j;
807 tree old_name_tag = pi->name_mem_tag;
809 /* If PTR points to a set of variables, check if we don't
810 have another pointer Q with the same points-to set before
811 creating a tag. If so, use Q's tag instead of creating a
812 new one.
814 This is important for not creating unnecessary symbols
815 and also for copy propagation. If we ever need to
816 propagate PTR into Q or vice-versa, we would run into
817 problems if they both had different name tags because
818 they would have different SSA version numbers (which
819 would force us to take the name tags in and out of SSA). */
820 for (j = 0; j < i; j++)
822 tree q = VARRAY_TREE (ai->processed_ptrs, j);
823 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
825 if (qi
826 && qi->pt_vars
827 && qi->name_mem_tag
828 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
830 pi->name_mem_tag = qi->name_mem_tag;
831 break;
835 /* If we didn't find a pointer with the same points-to set
836 as PTR, create a new name tag if needed. */
837 if (pi->name_mem_tag == NULL_TREE)
838 pi->name_mem_tag = get_nmt_for (ptr);
840 /* If the new name tag computed for PTR is different than
841 the old name tag that it used to have, then the old tag
842 needs to be removed from the IL, so we mark it for
843 renaming. */
844 if (old_name_tag && old_name_tag != pi->name_mem_tag)
845 bitmap_set_bit (vars_to_rename, var_ann (old_name_tag)->uid);
847 else if (pi->pt_malloc)
849 /* Otherwise, create a unique name tag for this pointer. */
850 pi->name_mem_tag = get_nmt_for (ptr);
852 else
854 /* Only pointers that may point to malloc or other variables
855 may receive a name tag. If the pointer does not point to
856 a known spot, we should use type tags. */
857 set_pt_anything (ptr);
858 continue;
861 TREE_THIS_VOLATILE (pi->name_mem_tag)
862 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
864 /* Mark the new name tag for renaming. */
865 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
871 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
872 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
873 name tag and the variables it points-to are call-clobbered. Finally, if
874 P_i escapes and we could not determine where it points to, then all the
875 variables in the same alias set as *P_i are marked call-clobbered. This
876 is necessary because we must assume that P_i may take the address of any
877 variable in the same alias set. */
879 static void
880 compute_flow_sensitive_aliasing (struct alias_info *ai)
882 size_t i;
884 create_name_tags (ai);
886 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
888 unsigned j;
889 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
890 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
891 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
892 bitmap_iterator bi;
894 if (pi->value_escapes_p || pi->pt_anything)
896 /* If PTR escapes or may point to anything, then its associated
897 memory tags and pointed-to variables are call-clobbered. */
898 if (pi->name_mem_tag)
899 mark_call_clobbered (pi->name_mem_tag);
901 if (v_ann->type_mem_tag)
902 mark_call_clobbered (v_ann->type_mem_tag);
904 if (pi->pt_vars)
905 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
907 mark_call_clobbered (referenced_var (j));
911 /* Set up aliasing information for PTR's name memory tag (if it has
912 one). Note that only pointers that have been dereferenced will
913 have a name memory tag. */
914 if (pi->name_mem_tag && pi->pt_vars)
915 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
917 add_may_alias (pi->name_mem_tag, referenced_var (j));
918 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
921 /* If the name tag is call clobbered, so is the type tag
922 associated with the base VAR_DECL. */
923 if (pi->name_mem_tag
924 && v_ann->type_mem_tag
925 && is_call_clobbered (pi->name_mem_tag))
926 mark_call_clobbered (v_ann->type_mem_tag);
931 /* Compute type-based alias sets. Traverse all the pointers and
932 addressable variables found in setup_pointers_and_addressables.
934 For every pointer P in AI->POINTERS and addressable variable V in
935 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
936 memory tag (TMT) if their alias sets conflict. V is then marked as
937 an alias tag so that the operand scanner knows that statements
938 containing V have aliased operands. */
940 static void
941 compute_flow_insensitive_aliasing (struct alias_info *ai)
943 size_t i;
945 /* Initialize counter for the total number of virtual operands that
946 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
947 threshold set by --params max-alias-vops, we enable alias
948 grouping. */
949 ai->total_alias_vops = 0;
951 /* For every pointer P, determine which addressable variables may alias
952 with P's type memory tag. */
953 for (i = 0; i < ai->num_pointers; i++)
955 size_t j;
956 struct alias_map_d *p_map = ai->pointers[i];
957 tree tag = var_ann (p_map->var)->type_mem_tag;
958 var_ann_t tag_ann = var_ann (tag);
960 p_map->total_alias_vops = 0;
961 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
962 sbitmap_zero (p_map->may_aliases);
964 for (j = 0; j < ai->num_addressable_vars; j++)
966 struct alias_map_d *v_map;
967 var_ann_t v_ann;
968 tree var;
969 bool tag_stored_p, var_stored_p;
971 v_map = ai->addressable_vars[j];
972 var = v_map->var;
973 v_ann = var_ann (var);
975 /* Skip memory tags and variables that have never been
976 written to. We also need to check if the variables are
977 call-clobbered because they may be overwritten by
978 function calls.
980 Note this is effectively random accessing elements in
981 the sparse bitset, which can be highly inefficient.
982 So we first check the call_clobbered status of the
983 tag and variable before querying the bitmap. */
984 tag_stored_p = is_call_clobbered (tag)
985 || bitmap_bit_p (ai->written_vars, tag_ann->uid);
986 var_stored_p = is_call_clobbered (var)
987 || bitmap_bit_p (ai->written_vars, v_ann->uid);
988 if (!tag_stored_p && !var_stored_p)
989 continue;
991 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
993 subvar_t svars;
994 size_t num_tag_refs, num_var_refs;
996 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
997 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
999 /* Add VAR to TAG's may-aliases set. */
1001 /* If this is an aggregate, we may have subvariables for it
1002 that need to be pointed to. */
1003 if (var_can_have_subvars (var)
1004 && (svars = get_subvars_for_var (var)))
1006 subvar_t sv;
1008 for (sv = svars; sv; sv = sv->next)
1010 add_may_alias (tag, sv->var);
1011 /* Update the bitmap used to represent TAG's alias set
1012 in case we need to group aliases. */
1013 SET_BIT (p_map->may_aliases, var_ann (sv->var)->uid);
1016 else
1018 add_may_alias (tag, var);
1019 /* Update the bitmap used to represent TAG's alias set
1020 in case we need to group aliases. */
1021 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
1024 /* Update the total number of virtual operands due to
1025 aliasing. Since we are adding one more alias to TAG's
1026 may-aliases set, the total number of virtual operands due
1027 to aliasing will be increased by the number of references
1028 made to VAR and TAG (every reference to TAG will also
1029 count as a reference to VAR). */
1030 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1031 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1038 /* Since this analysis is based exclusively on symbols, it fails to
1039 handle cases where two pointers P and Q have different memory
1040 tags with conflicting alias set numbers but no aliased symbols in
1041 common.
1043 For example, suppose that we have two memory tags TMT.1 and TMT.2
1044 such that
1046 may-aliases (TMT.1) = { a }
1047 may-aliases (TMT.2) = { b }
1049 and the alias set number of TMT.1 conflicts with that of TMT.2.
1050 Since they don't have symbols in common, loads and stores from
1051 TMT.1 and TMT.2 will seem independent of each other, which will
1052 lead to the optimizers making invalid transformations (see
1053 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1055 To avoid this problem, we do a final traversal of AI->POINTERS
1056 looking for pairs of pointers that have no aliased symbols in
1057 common and yet have conflicting alias set numbers. */
1058 for (i = 0; i < ai->num_pointers; i++)
1060 size_t j;
1061 struct alias_map_d *p_map1 = ai->pointers[i];
1062 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1063 sbitmap may_aliases1 = p_map1->may_aliases;
1065 for (j = i + 1; j < ai->num_pointers; j++)
1067 struct alias_map_d *p_map2 = ai->pointers[j];
1068 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1069 sbitmap may_aliases2 = p_map2->may_aliases;
1071 /* If the pointers may not point to each other, do nothing. */
1072 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set))
1073 continue;
1075 /* The two pointers may alias each other. If they already have
1076 symbols in common, do nothing. */
1077 if (sbitmap_any_common_bits (may_aliases1, may_aliases2))
1078 continue;
1080 if (sbitmap_first_set_bit (may_aliases2) >= 0)
1082 size_t k;
1084 /* Add all the aliases for TAG2 into TAG1's alias set.
1085 FIXME, update grouping heuristic counters. */
1086 EXECUTE_IF_SET_IN_SBITMAP (may_aliases2, 0, k,
1087 add_may_alias (tag1, referenced_var (k)));
1088 sbitmap_a_or_b (may_aliases1, may_aliases1, may_aliases2);
1090 else
1092 /* Since TAG2 does not have any aliases of its own, add
1093 TAG2 itself to the alias set of TAG1. */
1094 add_may_alias (tag1, tag2);
1095 SET_BIT (may_aliases1, var_ann (tag2)->uid);
1100 if (dump_file)
1101 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
1102 get_name (current_function_decl),
1103 ai->total_alias_vops);
1105 /* Determine if we need to enable alias grouping. */
1106 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1107 group_aliases (ai);
1111 /* Comparison function for qsort used in group_aliases. */
1113 static int
1114 total_alias_vops_cmp (const void *p, const void *q)
1116 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1117 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1118 long n1 = (*p1)->total_alias_vops;
1119 long n2 = (*p2)->total_alias_vops;
1121 /* We want to sort in descending order. */
1122 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1125 /* Group all the aliases for TAG to make TAG represent all the
1126 variables in its alias set. Update the total number
1127 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1128 function will make TAG be the unique alias tag for all the
1129 variables in its may-aliases. So, given:
1131 may-aliases(TAG) = { V1, V2, V3 }
1133 This function will group the variables into:
1135 may-aliases(V1) = { TAG }
1136 may-aliases(V2) = { TAG }
1137 may-aliases(V2) = { TAG } */
1139 static void
1140 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
1142 size_t i;
1143 var_ann_t tag_ann = var_ann (tag);
1144 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1146 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i,
1148 tree var = referenced_var (i);
1149 var_ann_t ann = var_ann (var);
1151 /* Make TAG the unique alias of VAR. */
1152 ann->is_alias_tag = 0;
1153 ann->may_aliases = NULL;
1155 /* Note that VAR and TAG may be the same if the function has no
1156 addressable variables (see the discussion at the end of
1157 setup_pointers_and_addressables). */
1158 if (var != tag)
1159 add_may_alias (var, tag);
1161 /* Reduce total number of virtual operands contributed
1162 by TAG on behalf of VAR. Notice that the references to VAR
1163 itself won't be removed. We will merely replace them with
1164 references to TAG. */
1165 ai->total_alias_vops -= num_tag_refs;
1168 /* We have reduced the number of virtual operands that TAG makes on
1169 behalf of all the variables formerly aliased with it. However,
1170 we have also "removed" all the virtual operands for TAG itself,
1171 so we add them back. */
1172 ai->total_alias_vops += num_tag_refs;
1174 /* TAG no longer has any aliases. */
1175 tag_ann->may_aliases = NULL;
1179 /* Group may-aliases sets to reduce the number of virtual operands due
1180 to aliasing.
1182 1- Sort the list of pointers in decreasing number of contributed
1183 virtual operands.
1185 2- Take the first entry in AI->POINTERS and revert the role of
1186 the memory tag and its aliases. Usually, whenever an aliased
1187 variable Vi is found to alias with a memory tag T, we add Vi
1188 to the may-aliases set for T. Meaning that after alias
1189 analysis, we will have:
1191 may-aliases(T) = { V1, V2, V3, ..., Vn }
1193 This means that every statement that references T, will get 'n'
1194 virtual operands for each of the Vi tags. But, when alias
1195 grouping is enabled, we make T an alias tag and add it to the
1196 alias set of all the Vi variables:
1198 may-aliases(V1) = { T }
1199 may-aliases(V2) = { T }
1201 may-aliases(Vn) = { T }
1203 This has two effects: (a) statements referencing T will only get
1204 a single virtual operand, and, (b) all the variables Vi will now
1205 appear to alias each other. So, we lose alias precision to
1206 improve compile time. But, in theory, a program with such a high
1207 level of aliasing should not be very optimizable in the first
1208 place.
1210 3- Since variables may be in the alias set of more than one
1211 memory tag, the grouping done in step (2) needs to be extended
1212 to all the memory tags that have a non-empty intersection with
1213 the may-aliases set of tag T. For instance, if we originally
1214 had these may-aliases sets:
1216 may-aliases(T) = { V1, V2, V3 }
1217 may-aliases(R) = { V2, V4 }
1219 In step (2) we would have reverted the aliases for T as:
1221 may-aliases(V1) = { T }
1222 may-aliases(V2) = { T }
1223 may-aliases(V3) = { T }
1225 But note that now V2 is no longer aliased with R. We could
1226 add R to may-aliases(V2), but we are in the process of
1227 grouping aliases to reduce virtual operands so what we do is
1228 add V4 to the grouping to obtain:
1230 may-aliases(V1) = { T }
1231 may-aliases(V2) = { T }
1232 may-aliases(V3) = { T }
1233 may-aliases(V4) = { T }
1235 4- If the total number of virtual operands due to aliasing is
1236 still above the threshold set by max-alias-vops, go back to (2). */
1238 static void
1239 group_aliases (struct alias_info *ai)
1241 size_t i;
1243 /* Sort the POINTERS array in descending order of contributed
1244 virtual operands. */
1245 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1246 total_alias_vops_cmp);
1248 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1249 and the tag's may-aliases set. */
1250 for (i = 0; i < ai->num_pointers; i++)
1252 size_t j;
1253 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1254 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1256 /* Skip tags that have been grouped already. */
1257 if (ai->pointers[i]->grouped_p)
1258 continue;
1260 /* See if TAG1 had any aliases in common with other type tags.
1261 If we find a TAG2 with common aliases with TAG1, add TAG2's
1262 aliases into TAG1. */
1263 for (j = i + 1; j < ai->num_pointers; j++)
1265 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1267 if (sbitmap_any_common_bits (tag1_aliases, tag2_aliases))
1269 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1271 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1273 /* TAG2 does not need its aliases anymore. */
1274 sbitmap_zero (tag2_aliases);
1275 var_ann (tag2)->may_aliases = NULL;
1277 /* TAG1 is the unique alias of TAG2. */
1278 add_may_alias (tag2, tag1);
1280 ai->pointers[j]->grouped_p = true;
1284 /* Now group all the aliases we collected into TAG1. */
1285 group_aliases_into (tag1, tag1_aliases, ai);
1287 /* If we've reduced total number of virtual operands below the
1288 threshold, stop. */
1289 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1290 break;
1293 /* Finally, all the variables that have been grouped cannot be in
1294 the may-alias set of name memory tags. Suppose that we have
1295 grouped the aliases in this code so that may-aliases(a) = TMT.20
1297 p_5 = &a;
1299 # a_9 = V_MAY_DEF <a_8>
1300 p_5->field = 0
1301 ... Several modifications to TMT.20 ...
1302 # VUSE <a_9>
1303 x_30 = p_5->field
1305 Since p_5 points to 'a', the optimizers will try to propagate 0
1306 into p_5->field, but that is wrong because there have been
1307 modifications to 'TMT.20' in between. To prevent this we have to
1308 replace 'a' with 'TMT.20' in the name tag of p_5. */
1309 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1311 size_t j;
1312 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1313 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1314 varray_type aliases;
1316 if (name_tag == NULL_TREE)
1317 continue;
1319 aliases = var_ann (name_tag)->may_aliases;
1320 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1322 tree alias = VARRAY_TREE (aliases, j);
1323 var_ann_t ann = var_ann (alias);
1325 if ((ann->mem_tag_kind == NOT_A_TAG
1326 || ann->mem_tag_kind == STRUCT_FIELD)
1327 && ann->may_aliases)
1329 tree new_alias;
1331 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1333 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1334 replace_may_alias (name_tag, j, new_alias);
1339 if (dump_file)
1340 fprintf (dump_file,
1341 "%s: Total number of aliased vops after grouping: %ld%s\n",
1342 get_name (current_function_decl),
1343 ai->total_alias_vops,
1344 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1348 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1350 static void
1351 create_alias_map_for (tree var, struct alias_info *ai)
1353 struct alias_map_d *alias_map;
1354 alias_map = xcalloc (1, sizeof (*alias_map));
1355 alias_map->var = var;
1356 alias_map->set = get_alias_set (var);
1357 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1361 /* Create memory tags for all the dereferenced pointers and build the
1362 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1363 sets. Based on the address escape and points-to information collected
1364 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1365 variables whose address is not needed anymore. */
1367 static void
1368 setup_pointers_and_addressables (struct alias_info *ai)
1370 size_t i, n_vars, num_addressable_vars, num_pointers;
1372 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1373 num_addressable_vars = num_pointers = 0;
1374 for (i = 0; i < num_referenced_vars; i++)
1376 tree var = referenced_var (i);
1378 if (may_be_aliased (var))
1379 num_addressable_vars++;
1381 if (POINTER_TYPE_P (TREE_TYPE (var)))
1383 /* Since we don't keep track of volatile variables, assume that
1384 these pointers are used in indirect store operations. */
1385 if (TREE_THIS_VOLATILE (var))
1386 bitmap_set_bit (ai->dereferenced_ptrs_store, var_ann (var)->uid);
1388 num_pointers++;
1392 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1393 always going to be slightly bigger than we actually need them
1394 because some TREE_ADDRESSABLE variables will be marked
1395 non-addressable below and only pointers with unique type tags are
1396 going to be added to POINTERS. */
1397 ai->addressable_vars = xcalloc (num_addressable_vars,
1398 sizeof (struct alias_map_d *));
1399 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1400 ai->num_addressable_vars = 0;
1401 ai->num_pointers = 0;
1403 /* Since we will be creating type memory tags within this loop, cache the
1404 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1405 unnecessarily. */
1406 n_vars = num_referenced_vars;
1408 for (i = 0; i < n_vars; i++)
1410 tree var = referenced_var (i);
1411 var_ann_t v_ann = var_ann (var);
1412 subvar_t svars;
1414 /* Name memory tags already have flow-sensitive aliasing
1415 information, so they need not be processed by
1416 compute_flow_insensitive_aliasing. Similarly, type memory
1417 tags are already accounted for when we process their
1418 associated pointer.
1420 Structure fields, on the other hand, have to have some of this
1421 information processed for them, but it's pointless to mark them
1422 non-addressable (since they are fake variables anyway). */
1423 if (v_ann->mem_tag_kind != NOT_A_TAG
1424 && v_ann->mem_tag_kind != STRUCT_FIELD)
1425 continue;
1427 /* Remove the ADDRESSABLE flag from every addressable variable whose
1428 address is not needed anymore. This is caused by the propagation
1429 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1430 removal of dead pointer assignments done by the early scalar
1431 cleanup passes. */
1432 if (TREE_ADDRESSABLE (var) && v_ann->mem_tag_kind != STRUCT_FIELD)
1434 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1435 && TREE_CODE (var) != RESULT_DECL
1436 && !is_global_var (var))
1438 bool okay_to_mark = true;
1439 /* Since VAR is now a regular GIMPLE register, we will need
1440 to rename VAR into SSA afterwards. */
1441 bitmap_set_bit (vars_to_rename, v_ann->uid);
1443 if (var_can_have_subvars (var)
1444 && (svars = get_subvars_for_var (var)))
1446 subvar_t sv;
1448 for (sv = svars; sv; sv = sv->next)
1450 var_ann_t svann = var_ann (sv->var);
1451 if (bitmap_bit_p (ai->addresses_needed, svann->uid))
1452 okay_to_mark = false;
1453 bitmap_set_bit (vars_to_rename, svann->uid);
1456 /* The address of VAR is not needed, remove the
1457 addressable bit, so that it can be optimized as a
1458 regular variable. */
1459 if (okay_to_mark)
1460 mark_non_addressable (var);
1463 else
1465 /* Add the variable to the set of addressables. Mostly
1466 used when scanning operands for ASM_EXPRs that
1467 clobber memory. In those cases, we need to clobber
1468 all call-clobbered variables and all addressables. */
1469 bitmap_set_bit (addressable_vars, v_ann->uid);
1470 if (var_can_have_subvars (var)
1471 && (svars = get_subvars_for_var (var)))
1473 subvar_t sv;
1474 for (sv = svars; sv; sv = sv->next)
1475 bitmap_set_bit (addressable_vars, var_ann (sv->var)->uid);
1481 /* Global variables and addressable locals may be aliased. Create an
1482 entry in ADDRESSABLE_VARS for VAR. */
1483 if (may_be_aliased (var))
1485 create_alias_map_for (var, ai);
1486 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1489 /* Add pointer variables that have been dereferenced to the POINTERS
1490 array and create a type memory tag for them. */
1491 if (POINTER_TYPE_P (TREE_TYPE (var)))
1493 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1494 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1496 tree tag;
1497 var_ann_t t_ann;
1499 /* If pointer VAR still doesn't have a memory tag
1500 associated with it, create it now or re-use an
1501 existing one. */
1502 tag = get_tmt_for (var, ai);
1503 t_ann = var_ann (tag);
1505 /* The type tag will need to be renamed into SSA
1506 afterwards. Note that we cannot do this inside
1507 get_tmt_for because aliasing may run multiple times
1508 and we only create type tags the first time. */
1509 bitmap_set_bit (vars_to_rename, t_ann->uid);
1511 /* Associate the tag with pointer VAR. */
1512 v_ann->type_mem_tag = tag;
1514 /* If pointer VAR has been used in a store operation,
1515 then its memory tag must be marked as written-to. */
1516 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1517 bitmap_set_bit (ai->written_vars, t_ann->uid);
1519 /* If pointer VAR is a global variable or a PARM_DECL,
1520 then its memory tag should be considered a global
1521 variable. */
1522 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1523 mark_call_clobbered (tag);
1525 /* All the dereferences of pointer VAR count as
1526 references of TAG. Since TAG can be associated with
1527 several pointers, add the dereferences of VAR to the
1528 TAG. We may need to grow AI->NUM_REFERENCES because
1529 we have been adding name and type tags. */
1530 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1531 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1533 VARRAY_UINT (ai->num_references, t_ann->uid)
1534 += VARRAY_UINT (ai->num_references, v_ann->uid);
1536 else
1538 /* The pointer has not been dereferenced. If it had a
1539 type memory tag, remove it and mark the old tag for
1540 renaming to remove it out of the IL. */
1541 var_ann_t ann = var_ann (var);
1542 tree tag = ann->type_mem_tag;
1543 if (tag)
1545 bitmap_set_bit (vars_to_rename, var_ann (tag)->uid);
1546 ann->type_mem_tag = NULL_TREE;
1554 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1555 every call site, we need to emit V_MAY_DEF expressions to represent the
1556 clobbering effects of the call for variables whose address escapes the
1557 current function.
1559 One approach is to group all call-clobbered variables into a single
1560 representative that is used as an alias of every call-clobbered variable
1561 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1562 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1564 The second approach is to emit a clobbering V_MAY_DEF for every
1565 call-clobbered variable at call sites. This is the preferred way in terms
1566 of optimization opportunities but it may create too many V_MAY_DEF operands
1567 if there are many call clobbered variables and function calls in the
1568 function.
1570 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1571 function calls found by the number of call-clobbered variables. If that
1572 product is beyond a certain threshold, as determined by the parameterized
1573 values shown below, we use .GLOBAL_VAR.
1575 FIXME. This heuristic should be improved. One idea is to use several
1576 .GLOBAL_VARs of different types instead of a single one. The thresholds
1577 have been derived from a typical bootstrap cycle, including all target
1578 libraries. Compile times were found increase by ~1% compared to using
1579 .GLOBAL_VAR. */
1581 static void
1582 maybe_create_global_var (struct alias_info *ai)
1584 unsigned i, n_clobbered;
1585 bitmap_iterator bi;
1587 /* No need to create it, if we have one already. */
1588 if (global_var == NULL_TREE)
1590 /* Count all the call-clobbered variables. */
1591 n_clobbered = 0;
1592 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1594 n_clobbered++;
1597 /* If the number of virtual operands that would be needed to
1598 model all the call-clobbered variables is larger than
1599 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1601 Also create .GLOBAL_VAR if there are no call-clobbered
1602 variables and the program contains a mixture of pure/const
1603 and regular function calls. This is to avoid the problem
1604 described in PR 20115:
1606 int X;
1607 int func_pure (void) { return X; }
1608 int func_non_pure (int a) { X += a; }
1609 int foo ()
1611 int a = func_pure ();
1612 func_non_pure (a);
1613 a = func_pure ();
1614 return a;
1617 Since foo() has no call-clobbered variables, there is
1618 no relationship between the calls to func_pure and
1619 func_non_pure. Since func_pure has no side-effects, value
1620 numbering optimizations elide the second call to func_pure.
1621 So, if we have some pure/const and some regular calls in the
1622 program we create .GLOBAL_VAR to avoid missing these
1623 relations. */
1624 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1625 || (n_clobbered == 0
1626 && ai->num_calls_found > 0
1627 && ai->num_pure_const_calls_found > 0
1628 && ai->num_calls_found > ai->num_pure_const_calls_found))
1629 create_global_var ();
1632 /* Mark all call-clobbered symbols for renaming. Since the initial
1633 rewrite into SSA ignored all call sites, we may need to rename
1634 .GLOBAL_VAR and the call-clobbered variables. */
1635 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1637 tree var = referenced_var (i);
1639 /* If the function has calls to clobbering functions and
1640 .GLOBAL_VAR has been created, make it an alias for all
1641 call-clobbered variables. */
1642 if (global_var && var != global_var)
1644 subvar_t svars;
1645 add_may_alias (var, global_var);
1646 if (var_can_have_subvars (var)
1647 && (svars = get_subvars_for_var (var)))
1649 subvar_t sv;
1650 for (sv = svars; sv; sv = sv->next)
1651 bitmap_set_bit (vars_to_rename, var_ann (sv->var)->uid);
1655 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1660 /* Return TRUE if pointer PTR may point to variable VAR.
1662 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1663 This is needed because when checking for type conflicts we are
1664 interested in the alias set of the memory location pointed-to by
1665 PTR. The alias set of PTR itself is irrelevant.
1667 VAR_ALIAS_SET is the alias set for VAR. */
1669 static bool
1670 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1671 tree var, HOST_WIDE_INT var_alias_set)
1673 tree mem;
1674 var_ann_t m_ann;
1676 alias_stats.alias_queries++;
1677 alias_stats.simple_queries++;
1679 /* By convention, a variable cannot alias itself. */
1680 mem = var_ann (ptr)->type_mem_tag;
1681 if (mem == var)
1683 alias_stats.alias_noalias++;
1684 alias_stats.simple_resolved++;
1685 return false;
1688 m_ann = var_ann (mem);
1690 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1692 alias_stats.tbaa_queries++;
1694 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1695 PTR can't possibly affect VAR. Note, that we are specifically testing
1696 for PTR's alias set here, not its pointed-to type. We also can't
1697 do this check with relaxed aliasing enabled. */
1698 if (POINTER_TYPE_P (TREE_TYPE (var))
1699 && var_alias_set != 0
1700 && mem_alias_set != 0)
1702 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1703 if (ptr_alias_set == var_alias_set)
1705 alias_stats.alias_noalias++;
1706 alias_stats.tbaa_resolved++;
1707 return false;
1711 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1712 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1714 alias_stats.alias_noalias++;
1715 alias_stats.tbaa_resolved++;
1716 return false;
1718 alias_stats.alias_mayalias++;
1719 return true;
1723 /* Add ALIAS to the set of variables that may alias VAR. */
1725 static void
1726 add_may_alias (tree var, tree alias)
1728 size_t i;
1729 var_ann_t v_ann = get_var_ann (var);
1730 var_ann_t a_ann = get_var_ann (alias);
1732 gcc_assert (var != alias);
1734 if (v_ann->may_aliases == NULL)
1735 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1737 /* Avoid adding duplicates. */
1738 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1739 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1740 return;
1742 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1743 FIXME, call-clobbering should only depend on whether an address
1744 escapes. It should be independent of aliasing. */
1745 if (is_call_clobbered (var))
1746 mark_call_clobbered (alias);
1748 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1749 else if (is_call_clobbered (alias))
1750 mark_call_clobbered (var);
1752 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1753 a_ann->is_alias_tag = 1;
1757 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1759 static void
1760 replace_may_alias (tree var, size_t i, tree new_alias)
1762 var_ann_t v_ann = var_ann (var);
1763 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1765 /* If VAR is a call-clobbered variable, so is 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 (new_alias);
1771 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1772 else if (is_call_clobbered (new_alias))
1773 mark_call_clobbered (var);
1777 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1779 static void
1780 set_pt_anything (tree ptr)
1782 struct ptr_info_def *pi = get_ptr_info (ptr);
1784 pi->pt_anything = 1;
1785 pi->pt_malloc = 0;
1787 /* The pointer used to have a name tag, but we now found it pointing
1788 to an arbitrary location. The name tag needs to be renamed and
1789 disassociated from PTR. */
1790 if (pi->name_mem_tag)
1792 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
1793 pi->name_mem_tag = NULL_TREE;
1798 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1800 static void
1801 set_pt_malloc (tree ptr)
1803 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1805 /* If the pointer has already been found to point to arbitrary
1806 memory locations, it is unsafe to mark it as pointing to malloc. */
1807 if (pi->pt_anything)
1808 return;
1810 pi->pt_malloc = 1;
1814 /* Given two different pointers DEST and ORIG. Merge the points-to
1815 information in ORIG into DEST. AI contains all the alias
1816 information collected up to this point. */
1818 static void
1819 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1821 struct ptr_info_def *dest_pi, *orig_pi;
1823 gcc_assert (dest != orig);
1825 /* Make sure we have points-to information for ORIG. */
1826 collect_points_to_info_for (ai, orig);
1828 dest_pi = get_ptr_info (dest);
1829 orig_pi = SSA_NAME_PTR_INFO (orig);
1831 if (orig_pi)
1833 gcc_assert (orig_pi != dest_pi);
1835 /* Notice that we never merge PT_MALLOC. This attribute is only
1836 true if the pointer is the result of a malloc() call.
1837 Otherwise, we can end up in this situation:
1839 P_i = malloc ();
1841 P_j = P_i + X;
1843 P_j would be marked as PT_MALLOC, however we currently do not
1844 handle cases of more than one pointer pointing to the same
1845 malloc'd area.
1847 FIXME: If the merging comes from an expression that preserves
1848 the PT_MALLOC attribute (copy assignment, address
1849 arithmetic), we ought to merge PT_MALLOC, but then both
1850 pointers would end up getting different name tags because
1851 create_name_tags is not smart enough to determine that the
1852 two come from the same malloc call. Copy propagation before
1853 aliasing should cure this. */
1854 dest_pi->pt_malloc = 0;
1855 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1856 set_pt_anything (dest);
1858 dest_pi->pt_null |= orig_pi->pt_null;
1860 if (!dest_pi->pt_anything
1861 && orig_pi->pt_vars
1862 && !bitmap_empty_p (orig_pi->pt_vars))
1864 if (dest_pi->pt_vars == NULL)
1866 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1867 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1869 else
1870 bitmap_ior_into (dest_pi->pt_vars, orig_pi->pt_vars);
1873 else
1874 set_pt_anything (dest);
1878 /* Add EXPR to the list of expressions pointed-to by PTR. */
1880 static void
1881 add_pointed_to_expr (struct alias_info *ai, tree ptr, tree expr)
1883 if (TREE_CODE (expr) == WITH_SIZE_EXPR)
1884 expr = TREE_OPERAND (expr, 0);
1886 get_ptr_info (ptr);
1888 if (TREE_CODE (expr) == CALL_EXPR
1889 && (call_expr_flags (expr) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1891 /* If EXPR is a malloc-like call, then the area pointed to PTR
1892 is guaranteed to not alias with anything else. */
1893 set_pt_malloc (ptr);
1895 else if (TREE_CODE (expr) == ADDR_EXPR)
1897 /* Found P_i = ADDR_EXPR */
1898 add_pointed_to_var (ai, ptr, expr);
1900 else if (TREE_CODE (expr) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (expr)))
1902 /* Found P_i = Q_j. */
1903 merge_pointed_to_info (ai, ptr, expr);
1905 else if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
1907 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1908 tree op0 = TREE_OPERAND (expr, 0);
1909 tree op1 = TREE_OPERAND (expr, 1);
1911 /* Both operands may be of pointer type. FIXME: Shouldn't
1912 we just expect PTR + OFFSET always? */
1913 if (POINTER_TYPE_P (TREE_TYPE (op0))
1914 && TREE_CODE (op0) != INTEGER_CST)
1916 if (TREE_CODE (op0) == SSA_NAME)
1917 merge_pointed_to_info (ai, ptr, op0);
1918 else if (TREE_CODE (op0) == ADDR_EXPR)
1919 add_pointed_to_var (ai, ptr, op0);
1920 else
1921 set_pt_anything (ptr);
1924 if (POINTER_TYPE_P (TREE_TYPE (op1))
1925 && TREE_CODE (op1) != INTEGER_CST)
1927 if (TREE_CODE (op1) == SSA_NAME)
1928 merge_pointed_to_info (ai, ptr, op1);
1929 else if (TREE_CODE (op1) == ADDR_EXPR)
1930 add_pointed_to_var (ai, ptr, op1);
1931 else
1932 set_pt_anything (ptr);
1935 /* Neither operand is a pointer? VAR can be pointing anywhere.
1936 FIXME: Shouldn't we abort here? If we get here, we found
1937 PTR = INT_CST + INT_CST, which should not be a valid pointer
1938 expression. */
1939 if (!(POINTER_TYPE_P (TREE_TYPE (op0))
1940 && TREE_CODE (op0) != INTEGER_CST)
1941 && !(POINTER_TYPE_P (TREE_TYPE (op1))
1942 && TREE_CODE (op1) != INTEGER_CST))
1943 set_pt_anything (ptr);
1945 else if (integer_zerop (expr))
1947 /* EXPR is the NULL pointer. Mark PTR as pointing to NULL. */
1948 SSA_NAME_PTR_INFO (ptr)->pt_null = 1;
1950 else
1952 /* If we can't recognize the expression, assume that PTR may
1953 point anywhere. */
1954 set_pt_anything (ptr);
1959 /* If VALUE is of the form &DECL, add DECL to the set of variables
1960 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1961 PTR. AI points to the collected alias information. */
1963 static void
1964 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1966 struct ptr_info_def *pi = get_ptr_info (ptr);
1967 tree pt_var = NULL_TREE;
1968 HOST_WIDE_INT offset, size;
1969 tree addrop;
1970 size_t uid;
1971 tree ref;
1972 subvar_t svars;
1974 gcc_assert (TREE_CODE (value) == ADDR_EXPR);
1976 addrop = TREE_OPERAND (value, 0);
1977 if (REFERENCE_CLASS_P (addrop))
1978 pt_var = get_base_address (addrop);
1979 else
1980 pt_var = addrop;
1982 /* If this is a component_ref, see if we can get a smaller number of
1983 variables to take the address of. */
1984 if (TREE_CODE (addrop) == COMPONENT_REF
1985 && (ref = okay_component_ref_for_subvars (addrop, &offset ,&size)))
1987 subvar_t sv;
1988 svars = get_subvars_for_var (ref);
1990 uid = var_ann (pt_var)->uid;
1992 if (pi->pt_vars == NULL)
1993 pi->pt_vars = BITMAP_GGC_ALLOC ();
1994 /* If the variable is a global, mark the pointer as pointing to
1995 global memory (which will make its tag a global variable). */
1996 if (is_global_var (pt_var))
1997 pi->pt_global_mem = 1;
1999 for (sv = svars; sv; sv = sv->next)
2001 if (overlap_subvar (offset, size, sv, NULL))
2003 bitmap_set_bit (pi->pt_vars, var_ann (sv->var)->uid);
2004 bitmap_set_bit (ai->addresses_needed, var_ann (sv->var)->uid);
2008 else if (pt_var && SSA_VAR_P (pt_var))
2011 uid = var_ann (pt_var)->uid;
2013 if (pi->pt_vars == NULL)
2014 pi->pt_vars = BITMAP_GGC_ALLOC ();
2016 /* If this is an aggregate, we may have subvariables for it that need
2017 to be pointed to. */
2018 if (var_can_have_subvars (pt_var)
2019 && (svars = get_subvars_for_var (pt_var)))
2021 subvar_t sv;
2022 for (sv = svars; sv; sv = sv->next)
2024 uid = var_ann (sv->var)->uid;
2025 bitmap_set_bit (ai->addresses_needed, uid);
2026 bitmap_set_bit (pi->pt_vars, uid);
2029 else
2031 bitmap_set_bit (ai->addresses_needed, uid);
2032 bitmap_set_bit (pi->pt_vars, uid);
2035 /* If the variable is a global, mark the pointer as pointing to
2036 global memory (which will make its tag a global variable). */
2037 if (is_global_var (pt_var))
2038 pi->pt_global_mem = 1;
2043 /* Callback for walk_use_def_chains to gather points-to information from the
2044 SSA web.
2046 VAR is an SSA variable or a GIMPLE expression.
2048 STMT is the statement that generates the SSA variable or, if STMT is a
2049 PHI_NODE, VAR is one of the PHI arguments.
2051 DATA is a pointer to a structure of type ALIAS_INFO. */
2053 static bool
2054 collect_points_to_info_r (tree var, tree stmt, void *data)
2056 struct alias_info *ai = (struct alias_info *) data;
2058 if (dump_file && (dump_flags & TDF_DETAILS))
2060 fprintf (dump_file, "Visiting use-def links for ");
2061 print_generic_expr (dump_file, var, dump_flags);
2062 fprintf (dump_file, "\n");
2065 switch (TREE_CODE (stmt))
2067 case RETURN_EXPR:
2068 if (TREE_CODE (TREE_OPERAND (stmt, 0)) != MODIFY_EXPR)
2069 abort ();
2070 stmt = TREE_OPERAND (stmt, 0);
2071 /* FALLTHRU */
2073 case MODIFY_EXPR:
2075 tree rhs = TREE_OPERAND (stmt, 1);
2076 STRIP_NOPS (rhs);
2077 add_pointed_to_expr (ai, var, rhs);
2078 break;
2081 case ASM_EXPR:
2082 /* Pointers defined by __asm__ statements can point anywhere. */
2083 set_pt_anything (var);
2084 break;
2086 case NOP_EXPR:
2087 if (IS_EMPTY_STMT (stmt))
2089 tree decl = SSA_NAME_VAR (var);
2091 if (TREE_CODE (decl) == PARM_DECL)
2092 add_pointed_to_expr (ai, var, decl);
2093 else if (DECL_INITIAL (decl))
2094 add_pointed_to_expr (ai, var, DECL_INITIAL (decl));
2095 else
2096 add_pointed_to_expr (ai, var, decl);
2098 break;
2100 case PHI_NODE:
2102 /* It STMT is a PHI node, then VAR is one of its arguments. The
2103 variable that we are analyzing is the LHS of the PHI node. */
2104 tree lhs = PHI_RESULT (stmt);
2106 switch (TREE_CODE (var))
2108 case ADDR_EXPR:
2109 add_pointed_to_var (ai, lhs, var);
2110 break;
2112 case SSA_NAME:
2113 /* Avoid unnecessary merges. */
2114 if (lhs != var)
2115 merge_pointed_to_info (ai, lhs, var);
2116 break;
2118 default:
2119 gcc_assert (is_gimple_min_invariant (var));
2120 add_pointed_to_expr (ai, lhs, var);
2121 break;
2123 break;
2126 default:
2127 gcc_unreachable ();
2130 return false;
2134 /* Return true if STMT is an "escape" site from the current function. Escape
2135 sites those statements which might expose the address of a variable
2136 outside the current function. STMT is an escape site iff:
2138 1- STMT is a function call, or
2139 2- STMT is an __asm__ expression, or
2140 3- STMT is an assignment to a non-local variable, or
2141 4- STMT is a return statement.
2143 AI points to the alias information collected so far. */
2145 static bool
2146 is_escape_site (tree stmt, struct alias_info *ai)
2148 tree call = get_call_expr_in (stmt);
2149 if (call != NULL_TREE)
2151 ai->num_calls_found++;
2153 if (!TREE_SIDE_EFFECTS (call))
2154 ai->num_pure_const_calls_found++;
2156 return true;
2158 else if (TREE_CODE (stmt) == ASM_EXPR)
2159 return true;
2160 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2162 tree lhs = TREE_OPERAND (stmt, 0);
2164 /* Get to the base of _REF nodes. */
2165 if (TREE_CODE (lhs) != SSA_NAME)
2166 lhs = get_base_address (lhs);
2168 /* If we couldn't recognize the LHS of the assignment, assume that it
2169 is a non-local store. */
2170 if (lhs == NULL_TREE)
2171 return true;
2173 /* If the RHS is a conversion between a pointer and an integer, the
2174 pointer escapes since we can't track the integer. */
2175 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2176 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2177 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2178 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
2179 (TREE_OPERAND (stmt, 1), 0)))
2180 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
2181 return true;
2183 /* If the LHS is an SSA name, it can't possibly represent a non-local
2184 memory store. */
2185 if (TREE_CODE (lhs) == SSA_NAME)
2186 return false;
2188 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2189 local variables we cannot be sure if it will escape, because we
2190 don't have information about objects not in SSA form. Need to
2191 implement something along the lines of
2193 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2194 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2195 Conference on Object-Oriented Programming Systems, Languages, and
2196 Applications (OOPSLA), pp. 1-19, 1999. */
2197 return true;
2199 else if (TREE_CODE (stmt) == RETURN_EXPR)
2200 return true;
2202 return false;
2206 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2207 is considered to represent all the pointers whose pointed-to types are
2208 in the same alias set class. Otherwise, the tag represents a single
2209 SSA_NAME pointer variable. */
2211 static tree
2212 create_memory_tag (tree type, bool is_type_tag)
2214 var_ann_t ann;
2215 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2217 /* By default, memory tags are local variables. Alias analysis will
2218 determine whether they should be considered globals. */
2219 DECL_CONTEXT (tag) = current_function_decl;
2221 /* Memory tags are by definition addressable. This also prevents
2222 is_gimple_ref frome confusing memory tags with optimizable
2223 variables. */
2224 TREE_ADDRESSABLE (tag) = 1;
2226 ann = get_var_ann (tag);
2227 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2228 ann->type_mem_tag = NULL_TREE;
2230 /* Add the tag to the symbol table. */
2231 add_referenced_tmp_var (tag);
2233 return tag;
2237 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2238 This is used if P_i has been found to point to a specific set of
2239 variables or to a non-aliased memory location like the address returned
2240 by malloc functions. */
2242 static tree
2243 get_nmt_for (tree ptr)
2245 struct ptr_info_def *pi = get_ptr_info (ptr);
2246 tree tag = pi->name_mem_tag;
2248 if (tag == NULL_TREE)
2249 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2251 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
2252 then its name tag should be considered a global variable. */
2253 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
2254 || pi->pt_malloc
2255 || pi->pt_global_mem)
2256 mark_call_clobbered (tag);
2258 return tag;
2262 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2263 artificial variable that represents the memory location pointed-to by
2264 PTR. It is used to model the effects of pointer de-references on
2265 addressable variables.
2267 AI points to the data gathered during alias analysis. This function
2268 populates the array AI->POINTERS. */
2270 static tree
2271 get_tmt_for (tree ptr, struct alias_info *ai)
2273 size_t i;
2274 tree tag;
2275 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2276 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2278 /* To avoid creating unnecessary memory tags, only create one memory tag
2279 per alias set class. Note that it may be tempting to group
2280 memory tags based on conflicting alias sets instead of
2281 equivalence. That would be wrong because alias sets are not
2282 necessarily transitive (as demonstrated by the libstdc++ test
2283 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2284 such that conflicts (A, B) == true and conflicts (A, C) == true,
2285 it does not necessarily follow that conflicts (B, C) == true. */
2286 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2288 struct alias_map_d *curr = ai->pointers[i];
2289 if (tag_set == curr->set)
2291 tag = var_ann (curr->var)->type_mem_tag;
2292 break;
2296 /* If VAR cannot alias with any of the existing memory tags, create a new
2297 tag for PTR and add it to the POINTERS array. */
2298 if (tag == NULL_TREE)
2300 struct alias_map_d *alias_map;
2302 /* If PTR did not have a type tag already, create a new TMT.*
2303 artificial variable representing the memory location
2304 pointed-to by PTR. */
2305 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2306 tag = create_memory_tag (tag_type, true);
2307 else
2308 tag = var_ann (ptr)->type_mem_tag;
2310 /* Add PTR to the POINTERS array. Note that we are not interested in
2311 PTR's alias set. Instead, we cache the alias set for the memory that
2312 PTR points to. */
2313 alias_map = xcalloc (1, sizeof (*alias_map));
2314 alias_map->var = ptr;
2315 alias_map->set = tag_set;
2316 ai->pointers[ai->num_pointers++] = alias_map;
2319 /* If the pointed-to type is volatile, so is the tag. */
2320 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2322 /* Make sure that the type tag has the same alias set as the
2323 pointed-to type. */
2324 gcc_assert (tag_set == get_alias_set (tag));
2326 return tag;
2330 /* Create GLOBAL_VAR, an artificial global variable to act as a
2331 representative of all the variables that may be clobbered by function
2332 calls. */
2334 static void
2335 create_global_var (void)
2337 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2338 void_type_node);
2339 DECL_ARTIFICIAL (global_var) = 1;
2340 TREE_READONLY (global_var) = 0;
2341 DECL_EXTERNAL (global_var) = 1;
2342 TREE_STATIC (global_var) = 1;
2343 TREE_USED (global_var) = 1;
2344 DECL_CONTEXT (global_var) = NULL_TREE;
2345 TREE_THIS_VOLATILE (global_var) = 0;
2346 TREE_ADDRESSABLE (global_var) = 0;
2348 add_referenced_tmp_var (global_var);
2349 bitmap_set_bit (vars_to_rename, var_ann (global_var)->uid);
2353 /* Dump alias statistics on FILE. */
2355 static void
2356 dump_alias_stats (FILE *file)
2358 const char *funcname
2359 = lang_hooks.decl_printable_name (current_function_decl, 2);
2360 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2361 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2362 fprintf (file, "Total alias mayalias results:\t%u\n",
2363 alias_stats.alias_mayalias);
2364 fprintf (file, "Total alias noalias results:\t%u\n",
2365 alias_stats.alias_noalias);
2366 fprintf (file, "Total simple queries:\t%u\n",
2367 alias_stats.simple_queries);
2368 fprintf (file, "Total simple resolved:\t%u\n",
2369 alias_stats.simple_resolved);
2370 fprintf (file, "Total TBAA queries:\t%u\n",
2371 alias_stats.tbaa_queries);
2372 fprintf (file, "Total TBAA resolved:\t%u\n",
2373 alias_stats.tbaa_resolved);
2377 /* Dump alias information on FILE. */
2379 void
2380 dump_alias_info (FILE *file)
2382 size_t i;
2383 const char *funcname
2384 = lang_hooks.decl_printable_name (current_function_decl, 2);
2386 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2388 fprintf (file, "Aliased symbols\n\n");
2389 for (i = 0; i < num_referenced_vars; i++)
2391 tree var = referenced_var (i);
2392 if (may_be_aliased (var))
2393 dump_variable (file, var);
2396 fprintf (file, "\nDereferenced pointers\n\n");
2397 for (i = 0; i < num_referenced_vars; i++)
2399 tree var = referenced_var (i);
2400 var_ann_t ann = var_ann (var);
2401 if (ann->type_mem_tag)
2402 dump_variable (file, var);
2405 fprintf (file, "\nType memory tags\n\n");
2406 for (i = 0; i < num_referenced_vars; i++)
2408 tree var = referenced_var (i);
2409 var_ann_t ann = var_ann (var);
2410 if (ann->mem_tag_kind == TYPE_TAG)
2411 dump_variable (file, var);
2414 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2416 fprintf (file, "SSA_NAME pointers\n\n");
2417 for (i = 1; i < num_ssa_names; i++)
2419 tree ptr = ssa_name (i);
2420 struct ptr_info_def *pi;
2422 if (ptr == NULL_TREE)
2423 continue;
2425 pi = SSA_NAME_PTR_INFO (ptr);
2426 if (!SSA_NAME_IN_FREE_LIST (ptr)
2427 && pi
2428 && pi->name_mem_tag)
2429 dump_points_to_info_for (file, ptr);
2432 fprintf (file, "\nName memory tags\n\n");
2433 for (i = 0; i < num_referenced_vars; i++)
2435 tree var = referenced_var (i);
2436 var_ann_t ann = var_ann (var);
2437 if (ann->mem_tag_kind == NAME_TAG)
2438 dump_variable (file, var);
2441 fprintf (file, "\n");
2445 /* Dump alias information on stderr. */
2447 void
2448 debug_alias_info (void)
2450 dump_alias_info (stderr);
2454 /* Return the alias information associated with pointer T. It creates a
2455 new instance if none existed. */
2457 struct ptr_info_def *
2458 get_ptr_info (tree t)
2460 struct ptr_info_def *pi;
2462 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2464 pi = SSA_NAME_PTR_INFO (t);
2465 if (pi == NULL)
2467 pi = ggc_alloc (sizeof (*pi));
2468 memset ((void *)pi, 0, sizeof (*pi));
2469 SSA_NAME_PTR_INFO (t) = pi;
2472 return pi;
2476 /* Dump points-to information for SSA_NAME PTR into FILE. */
2478 void
2479 dump_points_to_info_for (FILE *file, tree ptr)
2481 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2483 print_generic_expr (file, ptr, dump_flags);
2485 if (pi)
2487 if (pi->name_mem_tag)
2489 fprintf (file, ", name memory tag: ");
2490 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2493 if (pi->is_dereferenced)
2494 fprintf (file, ", is dereferenced");
2496 if (pi->value_escapes_p)
2497 fprintf (file, ", its value escapes");
2499 if (pi->pt_anything)
2500 fprintf (file, ", points-to anything");
2502 if (pi->pt_malloc)
2503 fprintf (file, ", points-to malloc");
2505 if (pi->pt_null)
2506 fprintf (file, ", points-to NULL");
2508 if (pi->pt_vars)
2510 unsigned ix;
2511 bitmap_iterator bi;
2513 fprintf (file, ", points-to vars: { ");
2514 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2516 print_generic_expr (file, referenced_var (ix), dump_flags);
2517 fprintf (file, " ");
2519 fprintf (file, "}");
2523 fprintf (file, "\n");
2527 /* Dump points-to information for VAR into stderr. */
2529 void
2530 debug_points_to_info_for (tree var)
2532 dump_points_to_info_for (stderr, var);
2536 /* Dump points-to information into FILE. NOTE: This function is slow, as
2537 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2539 void
2540 dump_points_to_info (FILE *file)
2542 basic_block bb;
2543 block_stmt_iterator si;
2544 size_t i;
2545 ssa_op_iter iter;
2546 const char *fname =
2547 lang_hooks.decl_printable_name (current_function_decl, 2);
2549 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2551 /* First dump points-to information for the default definitions of
2552 pointer variables. This is necessary because default definitions are
2553 not part of the code. */
2554 for (i = 0; i < num_referenced_vars; i++)
2556 tree var = referenced_var (i);
2557 if (POINTER_TYPE_P (TREE_TYPE (var)))
2559 var_ann_t ann = var_ann (var);
2560 if (ann->default_def)
2561 dump_points_to_info_for (file, ann->default_def);
2565 /* Dump points-to information for every pointer defined in the program. */
2566 FOR_EACH_BB (bb)
2568 tree phi;
2570 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2572 tree ptr = PHI_RESULT (phi);
2573 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2574 dump_points_to_info_for (file, ptr);
2577 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2579 tree stmt = bsi_stmt (si);
2580 tree def;
2581 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2582 if (POINTER_TYPE_P (TREE_TYPE (def)))
2583 dump_points_to_info_for (file, def);
2587 fprintf (file, "\n");
2591 /* Dump points-to info pointed by PTO into STDERR. */
2593 void
2594 debug_points_to_info (void)
2596 dump_points_to_info (stderr);
2599 /* Dump to FILE the list of variables that may be aliasing VAR. */
2601 void
2602 dump_may_aliases_for (FILE *file, tree var)
2604 varray_type aliases;
2606 if (TREE_CODE (var) == SSA_NAME)
2607 var = SSA_NAME_VAR (var);
2609 aliases = var_ann (var)->may_aliases;
2610 if (aliases)
2612 size_t i;
2613 fprintf (file, "{ ");
2614 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2616 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2617 fprintf (file, " ");
2619 fprintf (file, "}");
2624 /* Dump to stderr the list of variables that may be aliasing VAR. */
2626 void
2627 debug_may_aliases_for (tree var)
2629 dump_may_aliases_for (stderr, var);
2632 /* Return true if VAR may be aliased. */
2634 bool
2635 may_be_aliased (tree var)
2637 /* Obviously. */
2638 if (TREE_ADDRESSABLE (var))
2639 return true;
2641 /* Globally visible variables can have their addresses taken by other
2642 translation units. */
2643 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2644 return true;
2646 /* Automatic variables can't have their addresses escape any other way.
2647 This must be after the check for global variables, as extern declarations
2648 do not have TREE_STATIC set. */
2649 if (!TREE_STATIC (var))
2650 return false;
2652 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2653 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2654 we can only be sure the variable isn't addressable if it's local to the
2655 current function. */
2656 if (flag_unit_at_a_time)
2657 return false;
2658 if (decl_function_context (var) == current_function_decl)
2659 return false;
2661 return true;
2664 /* This structure is simply used during pushing fields onto the fieldstack
2665 to track the offset of the field, since bitpos_of_field gives it relative
2666 to its immediate containing type, and we want it relative to the ultimate
2667 containing object. */
2669 typedef struct fieldoff
2671 tree field;
2672 HOST_WIDE_INT offset;
2673 } *fieldoff_t;
2675 DEF_VEC_MALLOC_P(fieldoff_t);
2677 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2678 structure.
2679 Return -1 if the position is conditional or otherwise non-constant
2680 integer. */
2682 static HOST_WIDE_INT
2683 bitpos_of_field (const tree fdecl)
2686 if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2687 || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2688 return -1;
2690 return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2691 + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2694 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all the fields
2695 of TYPE onto fieldstack, recording their offsets along the way.
2696 OFFSET is used to keep track of the offset in this entire structure, rather
2697 than just the immediately containing structure. */
2699 static void
2700 push_fields_onto_fieldstack (tree type, VEC(fieldoff_t) **fieldstack,
2701 HOST_WIDE_INT offset)
2703 fieldoff_t pair;
2704 tree field = TYPE_FIELDS (type);
2705 if (!field)
2706 return;
2707 if (var_can_have_subvars (field)
2708 && TREE_CODE (field) == FIELD_DECL)
2710 size_t before = VEC_length (fieldoff_t, *fieldstack);
2711 /* Empty structures may have actual size, like in C++. So see if we
2712 actually end up pushing a field, and if not, if the size is nonzero,
2713 push the field onto the stack */
2714 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack, offset);
2715 if (before == VEC_length (fieldoff_t, *fieldstack)
2716 && DECL_SIZE (field)
2717 && !integer_zerop (DECL_SIZE (field)))
2719 pair = xmalloc (sizeof (struct fieldoff));
2720 pair->field = field;
2721 pair->offset = offset;
2722 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2725 else if (TREE_CODE (field) == FIELD_DECL)
2727 pair = xmalloc (sizeof (struct fieldoff));
2728 pair->field = field;
2729 pair->offset = offset + bitpos_of_field (field);
2730 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2732 for (field = TREE_CHAIN (field); field; field = TREE_CHAIN (field))
2734 if (TREE_CODE (field) != FIELD_DECL)
2735 continue;
2736 if (var_can_have_subvars (field))
2738 size_t before = VEC_length (fieldoff_t, *fieldstack);
2739 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack,
2740 offset + bitpos_of_field (field));
2741 /* Empty structures may have actual size, like in C++. So see if we
2742 actually end up pushing a field, and if not, if the size is nonzero,
2743 push the field onto the stack */
2744 if (before == VEC_length (fieldoff_t, *fieldstack)
2745 && DECL_SIZE (field)
2746 && !integer_zerop (DECL_SIZE (field)))
2748 pair = xmalloc (sizeof (struct fieldoff));
2749 pair->field = field;
2750 pair->offset = offset + bitpos_of_field (field);
2751 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2754 else
2756 pair = xmalloc (sizeof (struct fieldoff));
2757 pair->field = field;
2758 pair->offset = offset + bitpos_of_field (field);
2759 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2765 /* This represents the used range of a variable. */
2767 typedef struct used_part
2769 HOST_WIDE_INT minused;
2770 HOST_WIDE_INT maxused;
2771 } *used_part_t;
2773 /* An array of used_part structures, indexed by variable uid. */
2775 static used_part_t *used_portions;
2777 /* Given a variable uid, UID, get or create the entry in the used portions
2778 table for the variable. */
2780 static used_part_t
2781 get_or_create_used_part_for (size_t uid)
2783 used_part_t up;
2784 if (used_portions[uid] == NULL)
2786 up = xcalloc (1, sizeof (struct used_part));
2787 up->minused = INT_MAX;
2788 up->maxused = 0;
2790 else
2791 up = used_portions[uid];
2792 return up;
2797 /* Given an aggregate VAR, create the subvariables that represent its
2798 fields. */
2800 static void
2801 create_overlap_variables_for (tree var)
2803 VEC(fieldoff_t) *fieldstack = NULL;
2804 used_part_t up;
2805 size_t uid = var_ann (var)->uid;
2807 if (used_portions[uid] == NULL)
2808 return;
2810 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0);
2811 if (VEC_length (fieldoff_t, fieldstack) != 0)
2813 subvar_t *subvars;
2814 fieldoff_t fo;
2815 bool notokay = false;
2816 int i;
2818 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2819 know their size, and thus, can't handle.
2820 The same is true of fields with DECL_SIZE that is not an integer
2821 constant (such as variable sized fields).
2822 Fields with offsets which are not constant will have an offset < 0
2823 We *could* handle fields that are constant sized arrays, but
2824 currently don't. Doing so would require some extra changes to
2825 tree-ssa-operands.c. */
2827 for (i = 0; VEC_iterate (fieldoff_t, fieldstack, i, fo); i++)
2829 if (!DECL_SIZE (fo->field)
2830 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
2831 || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
2832 || fo->offset < 0)
2834 notokay = true;
2835 break;
2838 /* Cleanup after ourselves if we can't create overlap variables. */
2839 if (notokay)
2841 while (VEC_length (fieldoff_t, fieldstack) != 0)
2843 fo = VEC_pop (fieldoff_t, fieldstack);
2844 free (fo);
2846 VEC_free (fieldoff_t, fieldstack);
2847 return;
2849 /* Otherwise, create the variables. */
2850 subvars = lookup_subvars_for_var (var);
2851 up = used_portions[uid];
2853 while (VEC_length (fieldoff_t, fieldstack) != 0)
2855 subvar_t sv = ggc_alloc (sizeof (struct subvar));
2856 HOST_WIDE_INT fosize;
2857 var_ann_t ann;
2859 fo = VEC_pop (fieldoff_t, fieldstack);
2860 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
2862 if ((fo->offset <= up->minused
2863 && fo->offset + fosize <= up->minused)
2864 || fo->offset >= up->maxused)
2866 free (fo);
2867 continue;
2870 sv->offset = fo->offset;
2871 sv->size = fosize;
2872 sv->next = *subvars;
2873 sv->var = create_tmp_var_raw (TREE_TYPE (fo->field), "SFT");
2874 if (dump_file)
2876 fprintf (dump_file, "structure field tag %s created for var %s",
2877 get_name (sv->var), get_name (var));
2878 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
2879 sv->offset);
2880 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
2881 sv->size);
2882 fprintf (dump_file, "\n");
2886 /* We need to copy the various flags from var to sv->var, so that
2887 they are is_global_var iff the original variable was. */
2889 DECL_EXTERNAL (sv->var) = DECL_EXTERNAL (var);
2890 TREE_PUBLIC (sv->var) = TREE_PUBLIC (var);
2891 TREE_STATIC (sv->var) = TREE_STATIC (var);
2892 TREE_READONLY (sv->var) = TREE_READONLY (var);
2894 /* Like other memory tags, these need to be marked addressable to
2895 keep is_gimple_reg from thinking they are real. */
2896 TREE_ADDRESSABLE (sv->var) = 1;
2898 DECL_CONTEXT (sv->var) = DECL_CONTEXT (var);
2900 ann = get_var_ann (sv->var);
2901 ann->mem_tag_kind = STRUCT_FIELD;
2902 ann->type_mem_tag = NULL;
2903 add_referenced_tmp_var (sv->var);
2905 *subvars = sv;
2906 free (fo);
2909 /* Once we have created subvars, the original is no longer call
2910 clobbered on its own. Its call clobbered status depends
2911 completely on the call clobbered status of the subvars.
2913 add_referenced_var in the above loop will take care of
2914 marking subvars of global variables as call clobbered for us
2915 to start, since they are global as well. */
2916 clear_call_clobbered (var);
2920 VEC_free (fieldoff_t, fieldstack);
2924 /* Find the conservative answer to the question of what portions of what
2925 structures are used by this statement. We assume that if we have a
2926 component ref with a known size + offset, that we only need that part
2927 of the structure. For unknown cases, or cases where we do something
2928 to the whole structure, we assume we need to create fields for the
2929 entire structure. */
2931 static tree
2932 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2934 switch (TREE_CODE (*tp))
2936 case COMPONENT_REF:
2938 HOST_WIDE_INT bitsize;
2939 HOST_WIDE_INT bitpos;
2940 tree offset;
2941 enum machine_mode mode;
2942 int unsignedp;
2943 int volatilep;
2944 tree ref;
2945 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
2946 &unsignedp, &volatilep, false);
2947 if (DECL_P (ref) && offset == NULL && bitsize != -1)
2949 size_t uid = var_ann (ref)->uid;
2950 used_part_t up;
2952 up = get_or_create_used_part_for (uid);
2954 if (bitpos <= up->minused)
2955 up->minused = bitpos;
2956 if ((bitpos + bitsize >= up->maxused))
2957 up->maxused = bitpos + bitsize;
2959 used_portions[uid] = up;
2961 *walk_subtrees = 0;
2962 return NULL_TREE;
2964 else if (DECL_P (ref))
2966 if (DECL_SIZE (ref)
2967 && var_can_have_subvars (ref)
2968 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
2970 used_part_t up;
2971 size_t uid = var_ann (ref)->uid;
2973 up = get_or_create_used_part_for (uid);
2975 up->minused = 0;
2976 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
2978 used_portions[uid] = up;
2980 *walk_subtrees = 0;
2981 return NULL_TREE;
2985 break;
2986 case VAR_DECL:
2987 case PARM_DECL:
2989 tree var = *tp;
2990 if (DECL_SIZE (var)
2991 && var_can_have_subvars (var)
2992 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2994 used_part_t up;
2995 size_t uid = var_ann (var)->uid;
2997 up = get_or_create_used_part_for (uid);
2999 up->minused = 0;
3000 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3002 used_portions[uid] = up;
3003 *walk_subtrees = 0;
3004 return NULL_TREE;
3007 break;
3009 default:
3010 break;
3013 return NULL_TREE;
3016 /* We are about to create some new referenced variables, and we need the
3017 before size. */
3019 static size_t old_referenced_vars;
3022 /* Create structure field variables for structures used in this function. */
3024 static void
3025 create_structure_vars (void)
3027 basic_block bb;
3028 size_t i;
3030 old_referenced_vars = num_referenced_vars;
3031 used_portions = xcalloc (num_referenced_vars, sizeof (used_part_t));
3033 FOR_EACH_BB (bb)
3035 block_stmt_iterator bsi;
3036 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3038 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3039 find_used_portions,
3040 NULL);
3043 for (i = 0; i < old_referenced_vars; i++)
3045 tree var = referenced_var (i);
3046 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3047 if (var
3048 && DECL_SIZE (var)
3049 && var_can_have_subvars (var)
3050 && var_ann (var)->mem_tag_kind == NOT_A_TAG
3051 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3052 create_overlap_variables_for (var);
3054 for (i = 0; i < old_referenced_vars; i++)
3055 free (used_portions[i]);
3057 free (used_portions);
3060 static bool
3061 gate_structure_vars (void)
3063 return flag_tree_salias != 0;
3066 struct tree_opt_pass pass_create_structure_vars =
3068 "salias", /* name */
3069 gate_structure_vars, /* gate */
3070 create_structure_vars, /* execute */
3071 NULL, /* sub */
3072 NULL, /* next */
3073 0, /* static_pass_number */
3074 0, /* tv_id */
3075 PROP_cfg, /* properties_required */
3076 0, /* properties_provided */
3077 0, /* properties_destroyed */
3078 0, /* todo_flags_start */
3079 TODO_dump_func, /* todo_flags_finish */
3080 0 /* letter */