objc/
[official-gcc.git] / gcc / tree-ssa-alias.c
blob39d9fdecab88a33d556dba768244dad3ae5d37d8
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "expr.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "tree-gimple.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "convert.h"
44 #include "params.h"
45 #include "vec.h"
46 #include "bitmap.h"
48 /* Keep track of how many times each pointer has been dereferenced in
49 the program using the aux variable. This is used by the alias
50 grouping heuristic in compute_flow_insensitive_aliasing. */
51 #define NUM_REFERENCES(ANN) ((size_t)((ANN)->common.aux))
52 #define NUM_REFERENCES_CLEAR(ANN) ((ANN)->common.aux) = 0
53 #define NUM_REFERENCES_INC(ANN) (ANN)->common.aux = (void*) (((size_t)((ANN)->common.aux)) + 1)
54 #define NUM_REFERENCES_SET(ANN, VAL) (ANN)->common.aux = (void*) ((void *)(VAL))
56 /* Obstack used to hold grouping bitmaps and other temporary bitmaps used by
57 aliasing */
58 static bitmap_obstack alias_obstack;
60 /* 'true' after aliases have been computed (see compute_may_aliases). */
61 bool aliases_computed_p;
63 /* Structure to map a variable to its alias set and keep track of the
64 virtual operands that will be needed to represent it. */
65 struct alias_map_d
67 /* Variable and its alias set. */
68 tree var;
69 HOST_WIDE_INT set;
71 /* Total number of virtual operands that will be needed to represent
72 all the aliases of VAR. */
73 long total_alias_vops;
75 /* Nonzero if the aliases for this memory tag have been grouped
76 already. Used in group_aliases. */
77 unsigned int grouped_p : 1;
79 /* Set of variables aliased with VAR. This is the exact same
80 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
81 bitmap form to speed up alias grouping. */
82 bitmap may_aliases;
86 /* Alias information used by compute_may_aliases and its helpers. */
87 struct alias_info
89 /* SSA names visited while collecting points-to information. If bit I
90 is set, it means that SSA variable with version I has already been
91 visited. */
92 sbitmap ssa_names_visited;
94 /* Array of SSA_NAME pointers processed by the points-to collector. */
95 varray_type processed_ptrs;
97 /* Variables whose address is still needed. */
98 bitmap addresses_needed;
100 /* ADDRESSABLE_VARS contains all the global variables and locals that
101 have had their address taken. */
102 struct alias_map_d **addressable_vars;
103 size_t num_addressable_vars;
105 /* POINTERS contains all the _DECL pointers with unique memory tags
106 that have been referenced in the program. */
107 struct alias_map_d **pointers;
108 size_t num_pointers;
110 /* Number of function calls found in the program. */
111 size_t num_calls_found;
113 /* Number of const/pure function calls found in the program. */
114 size_t num_pure_const_calls_found;
116 /* Total number of virtual operands that will be needed to represent
117 all the aliases of all the pointers found in the program. */
118 long total_alias_vops;
120 /* Variables that have been written to. */
121 bitmap written_vars;
123 /* Pointers that have been used in an indirect store operation. */
124 bitmap dereferenced_ptrs_store;
126 /* Pointers that have been used in an indirect load operation. */
127 bitmap dereferenced_ptrs_load;
131 /* Counters used to display statistics on alias analysis. */
132 struct alias_stats_d
134 unsigned int alias_queries;
135 unsigned int alias_mayalias;
136 unsigned int alias_noalias;
137 unsigned int simple_queries;
138 unsigned int simple_resolved;
139 unsigned int tbaa_queries;
140 unsigned int tbaa_resolved;
144 /* Local variables. */
145 static struct alias_stats_d alias_stats;
147 /* Local functions. */
148 static void compute_flow_insensitive_aliasing (struct alias_info *);
149 static void dump_alias_stats (FILE *);
150 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT);
151 static tree create_memory_tag (tree type, bool is_type_tag);
152 static tree get_tmt_for (tree, struct alias_info *);
153 static tree get_nmt_for (tree);
154 static void add_may_alias (tree, tree);
155 static void replace_may_alias (tree, size_t, tree);
156 static struct alias_info *init_alias_info (void);
157 static void delete_alias_info (struct alias_info *);
158 static void compute_points_to_and_addr_escape (struct alias_info *);
159 static void compute_flow_sensitive_aliasing (struct alias_info *);
160 static void setup_pointers_and_addressables (struct alias_info *);
161 static bool collect_points_to_info_r (tree, tree, void *);
162 static bool is_escape_site (tree, struct alias_info *);
163 static void add_pointed_to_var (struct alias_info *, tree, tree);
164 static void create_global_var (void);
165 static void collect_points_to_info_for (struct alias_info *, tree);
166 static void maybe_create_global_var (struct alias_info *ai);
167 static void group_aliases (struct alias_info *);
168 static void set_pt_anything (tree ptr);
169 static void set_pt_malloc (tree ptr);
171 /* Global declarations. */
173 /* Call clobbered variables in the function. If bit I is set, then
174 REFERENCED_VARS (I) is call-clobbered. */
175 bitmap call_clobbered_vars;
177 /* Addressable variables in the function. If bit I is set, then
178 REFERENCED_VARS (I) has had its address taken. Note that
179 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
180 addressable variable is not necessarily call-clobbered (e.g., a
181 local addressable whose address does not escape) and not all
182 call-clobbered variables are addressable (e.g., a local static
183 variable). */
184 bitmap addressable_vars;
186 /* When the program has too many call-clobbered variables and call-sites,
187 this variable is used to represent the clobbering effects of function
188 calls. In these cases, all the call clobbered variables in the program
189 are forced to alias this variable. This reduces compile times by not
190 having to keep track of too many V_MAY_DEF expressions at call sites. */
191 tree global_var;
194 /* Compute may-alias information for every variable referenced in function
195 FNDECL.
197 Alias analysis proceeds in 3 main phases:
199 1- Points-to and escape analysis.
201 This phase walks the use-def chains in the SSA web looking for three
202 things:
204 * Assignments of the form P_i = &VAR
205 * Assignments of the form P_i = malloc()
206 * Pointers and ADDR_EXPR that escape the current function.
208 The concept of 'escaping' is the same one used in the Java world. When
209 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
210 outside of the current function. So, assignment to global variables,
211 function arguments and returning a pointer are all escape sites, as are
212 conversions between pointers and integers.
214 This is where we are currently limited. Since not everything is renamed
215 into SSA, we lose track of escape properties when a pointer is stashed
216 inside a field in a structure, for instance. In those cases, we are
217 assuming that the pointer does escape.
219 We use escape analysis to determine whether a variable is
220 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
221 is call-clobbered. If a pointer P_i escapes, then all the variables
222 pointed-to by P_i (and its memory tag) also escape.
224 2- Compute flow-sensitive aliases
226 We have two classes of memory tags. Memory tags associated with the
227 pointed-to data type of the pointers in the program. These tags are
228 called "type memory tag" (TMT). The other class are those associated
229 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
230 when adding operands for an INDIRECT_REF *P_i, we will first check
231 whether P_i has a name tag, if it does we use it, because that will have
232 more precise aliasing information. Otherwise, we use the standard type
233 tag.
235 In this phase, we go through all the pointers we found in points-to
236 analysis and create alias sets for the name memory tags associated with
237 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
238 it points to and its tag.
241 3- Compute flow-insensitive aliases
243 This pass will compare the alias set of every type memory tag and every
244 addressable variable found in the program. Given a type memory tag TMT
245 and an addressable variable V. If the alias sets of TMT and V conflict
246 (as computed by may_alias_p), then V is marked as an alias tag and added
247 to the alias set of TMT.
249 For instance, consider the following function:
251 foo (int i)
253 int *p, a, b;
255 if (i > 10)
256 p = &a;
257 else
258 p = &b;
260 *p = 3;
261 a = b + 2;
262 return *p;
265 After aliasing analysis has finished, the type memory tag for pointer
266 'p' will have two aliases, namely variables 'a' and 'b'. Every time
267 pointer 'p' is dereferenced, we want to mark the operation as a
268 potential reference to 'a' and 'b'.
270 foo (int i)
272 int *p, a, b;
274 if (i_2 > 10)
275 p_4 = &a;
276 else
277 p_6 = &b;
278 # p_1 = PHI <p_4(1), p_6(2)>;
280 # a_7 = V_MAY_DEF <a_3>;
281 # b_8 = V_MAY_DEF <b_5>;
282 *p_1 = 3;
284 # a_9 = V_MAY_DEF <a_7>
285 # VUSE <b_8>
286 a_9 = b_8 + 2;
288 # VUSE <a_9>;
289 # VUSE <b_8>;
290 return *p_1;
293 In certain cases, the list of may aliases for a pointer may grow too
294 large. This may cause an explosion in the number of virtual operands
295 inserted in the code. Resulting in increased memory consumption and
296 compilation time.
298 When the number of virtual operands needed to represent aliased
299 loads and stores grows too large (configurable with @option{--param
300 max-aliased-vops}), alias sets are grouped to avoid severe
301 compile-time slow downs and memory consumption. See group_aliases. */
303 static void
304 compute_may_aliases (void)
306 struct alias_info *ai;
308 memset (&alias_stats, 0, sizeof (alias_stats));
310 /* Initialize aliasing information. */
311 ai = init_alias_info ();
313 /* For each pointer P_i, determine the sets of variables that P_i may
314 point-to. For every addressable variable V, determine whether the
315 address of V escapes the current function, making V call-clobbered
316 (i.e., whether &V is stored in a global variable or if its passed as a
317 function call argument). */
318 compute_points_to_and_addr_escape (ai);
320 /* Collect all pointers and addressable variables, compute alias sets,
321 create memory tags for pointers and promote variables whose address is
322 not needed anymore. */
323 setup_pointers_and_addressables (ai);
325 /* Compute flow-sensitive, points-to based aliasing for all the name
326 memory tags. Note that this pass needs to be done before flow
327 insensitive analysis because it uses the points-to information
328 gathered before to mark call-clobbered type tags. */
329 compute_flow_sensitive_aliasing (ai);
331 /* Compute type-based flow-insensitive aliasing for all the type
332 memory tags. */
333 compute_flow_insensitive_aliasing (ai);
335 /* If the program has too many call-clobbered variables and/or function
336 calls, create .GLOBAL_VAR and use it to model call-clobbering
337 semantics at call sites. This reduces the number of virtual operands
338 considerably, improving compile times at the expense of lost
339 aliasing precision. */
340 maybe_create_global_var (ai);
342 /* Debugging dumps. */
343 if (dump_file)
345 dump_referenced_vars (dump_file);
346 if (dump_flags & TDF_STATS)
347 dump_alias_stats (dump_file);
348 dump_points_to_info (dump_file);
349 dump_alias_info (dump_file);
352 /* Deallocate memory used by aliasing data structures. */
353 delete_alias_info (ai);
356 block_stmt_iterator bsi;
357 basic_block bb;
358 FOR_EACH_BB (bb)
360 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
362 update_stmt_if_modified (bsi_stmt (bsi));
369 struct tree_opt_pass pass_may_alias =
371 "alias", /* name */
372 NULL, /* gate */
373 compute_may_aliases, /* execute */
374 NULL, /* sub */
375 NULL, /* next */
376 0, /* static_pass_number */
377 TV_TREE_MAY_ALIAS, /* tv_id */
378 PROP_cfg | PROP_ssa, /* properties_required */
379 PROP_alias, /* properties_provided */
380 0, /* properties_destroyed */
381 0, /* todo_flags_start */
382 TODO_dump_func | TODO_update_ssa
383 | TODO_ggc_collect | TODO_verify_ssa
384 | TODO_verify_stmts, /* todo_flags_finish */
385 0 /* letter */
389 /* Data structure used to count the number of dereferences to PTR
390 inside an expression. */
391 struct count_ptr_d
393 tree ptr;
394 unsigned count;
398 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
399 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
401 static tree
402 count_ptr_derefs (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
404 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
406 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
407 count_p->count++;
409 return NULL_TREE;
413 /* Count the number of direct and indirect uses for pointer PTR in
414 statement STMT. The two counts are stored in *NUM_USES_P and
415 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
416 least one of those dereferences is a store operation. */
418 void
419 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
420 unsigned *num_derefs_p, bool *is_store)
422 ssa_op_iter i;
423 tree use;
425 *num_uses_p = 0;
426 *num_derefs_p = 0;
427 *is_store = false;
429 /* Find out the total number of uses of PTR in STMT. */
430 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
431 if (use == ptr)
432 (*num_uses_p)++;
434 /* Now count the number of indirect references to PTR. This is
435 truly awful, but we don't have much choice. There are no parent
436 pointers inside INDIRECT_REFs, so an expression like
437 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
438 find all the indirect and direct uses of x_1 inside. The only
439 shortcut we can take is the fact that GIMPLE only allows
440 INDIRECT_REFs inside the expressions below. */
441 if (TREE_CODE (stmt) == MODIFY_EXPR
442 || (TREE_CODE (stmt) == RETURN_EXPR
443 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
444 || TREE_CODE (stmt) == ASM_EXPR
445 || TREE_CODE (stmt) == CALL_EXPR)
447 tree lhs, rhs;
449 if (TREE_CODE (stmt) == MODIFY_EXPR)
451 lhs = TREE_OPERAND (stmt, 0);
452 rhs = TREE_OPERAND (stmt, 1);
454 else if (TREE_CODE (stmt) == RETURN_EXPR)
456 tree e = TREE_OPERAND (stmt, 0);
457 lhs = TREE_OPERAND (e, 0);
458 rhs = TREE_OPERAND (e, 1);
460 else if (TREE_CODE (stmt) == ASM_EXPR)
462 lhs = ASM_OUTPUTS (stmt);
463 rhs = ASM_INPUTS (stmt);
465 else
467 lhs = NULL_TREE;
468 rhs = stmt;
471 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
473 struct count_ptr_d count;
474 count.ptr = ptr;
475 count.count = 0;
476 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
477 *is_store = true;
478 *num_derefs_p = count.count;
481 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
483 struct count_ptr_d count;
484 count.ptr = ptr;
485 count.count = 0;
486 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
487 *num_derefs_p += count.count;
491 gcc_assert (*num_uses_p >= *num_derefs_p);
495 /* Initialize the data structures used for alias analysis. */
497 static struct alias_info *
498 init_alias_info (void)
500 struct alias_info *ai;
501 referenced_var_iterator rvi;
502 tree var;
504 bitmap_obstack_initialize (&alias_obstack);
505 ai = xcalloc (1, sizeof (struct alias_info));
506 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
507 sbitmap_zero (ai->ssa_names_visited);
508 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
509 ai->addresses_needed = BITMAP_ALLOC (&alias_obstack);
510 ai->written_vars = BITMAP_ALLOC (&alias_obstack);
511 ai->dereferenced_ptrs_store = BITMAP_ALLOC (&alias_obstack);
512 ai->dereferenced_ptrs_load = BITMAP_ALLOC (&alias_obstack);
514 /* If aliases have been computed before, clear existing information. */
515 if (aliases_computed_p)
517 unsigned i;
519 /* Similarly, clear the set of addressable variables. In this
520 case, we can just clear the set because addressability is
521 only computed here. */
522 bitmap_clear (addressable_vars);
524 /* Clear flow-insensitive alias information from each symbol. */
525 FOR_EACH_REFERENCED_VAR (var, rvi)
527 var_ann_t ann = var_ann (var);
529 ann->is_alias_tag = 0;
530 ann->may_aliases = NULL;
531 NUM_REFERENCES_CLEAR (ann);
533 /* Since we are about to re-discover call-clobbered
534 variables, clear the call-clobbered flag. Variables that
535 are intrinsically call-clobbered (globals, local statics,
536 etc) will not be marked by the aliasing code, so we can't
537 remove them from CALL_CLOBBERED_VARS.
539 NB: STRUCT_FIELDS are still call clobbered if they are for
540 a global variable, so we *don't* clear their call clobberedness
541 just because they are tags, though we will clear it if they
542 aren't for global variables. */
543 if (ann->mem_tag_kind == NAME_TAG
544 || ann->mem_tag_kind == TYPE_TAG
545 || !is_global_var (var))
546 clear_call_clobbered (var);
549 /* Clear flow-sensitive points-to information from each SSA name. */
550 for (i = 1; i < num_ssa_names; i++)
552 tree name = ssa_name (i);
554 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
555 continue;
557 if (SSA_NAME_PTR_INFO (name))
559 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
561 /* Clear all the flags but keep the name tag to
562 avoid creating new temporaries unnecessarily. If
563 this pointer is found to point to a subset or
564 superset of its former points-to set, then a new
565 tag will need to be created in create_name_tags. */
566 pi->pt_anything = 0;
567 pi->pt_malloc = 0;
568 pi->pt_null = 0;
569 pi->value_escapes_p = 0;
570 pi->is_dereferenced = 0;
571 if (pi->pt_vars)
572 bitmap_clear (pi->pt_vars);
577 /* Next time, we will need to reset alias information. */
578 aliases_computed_p = true;
580 return ai;
584 /* Deallocate memory used by alias analysis. */
586 static void
587 delete_alias_info (struct alias_info *ai)
589 size_t i;
590 referenced_var_iterator rvi;
591 tree var;
593 sbitmap_free (ai->ssa_names_visited);
594 ai->processed_ptrs = NULL;
595 BITMAP_FREE (ai->addresses_needed);
597 for (i = 0; i < ai->num_addressable_vars; i++)
598 free (ai->addressable_vars[i]);
600 FOR_EACH_REFERENCED_VAR(var, rvi)
602 var_ann_t ann = var_ann (var);
603 NUM_REFERENCES_CLEAR (ann);
606 free (ai->addressable_vars);
608 for (i = 0; i < ai->num_pointers; i++)
609 free (ai->pointers[i]);
610 free (ai->pointers);
612 BITMAP_FREE (ai->written_vars);
613 BITMAP_FREE (ai->dereferenced_ptrs_store);
614 BITMAP_FREE (ai->dereferenced_ptrs_load);
615 bitmap_obstack_release (&alias_obstack);
617 free (ai);
621 /* Walk use-def chains for pointer PTR to determine what variables is PTR
622 pointing to. */
624 static void
625 collect_points_to_info_for (struct alias_info *ai, tree ptr)
627 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
629 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
631 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
632 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
633 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
637 /* Traverse use-def links for all the pointers in the program to collect
638 address escape and points-to information.
640 This is loosely based on the same idea described in R. Hasti and S.
641 Horwitz, ``Using static single assignment form to improve
642 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
643 Programming Language Design and Implementation, pp. 97-105, 1998. */
645 static void
646 compute_points_to_and_addr_escape (struct alias_info *ai)
648 basic_block bb;
649 unsigned i;
650 tree op;
651 ssa_op_iter iter;
653 timevar_push (TV_TREE_PTA);
655 FOR_EACH_BB (bb)
657 block_stmt_iterator si;
659 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
661 bitmap addr_taken;
662 tree stmt = bsi_stmt (si);
663 bool stmt_escapes_p = is_escape_site (stmt, ai);
664 bitmap_iterator bi;
666 /* Mark all the variables whose address are taken by the
667 statement. Note that this will miss all the addresses taken
668 in PHI nodes (those are discovered while following the use-def
669 chains). */
670 addr_taken = addresses_taken (stmt);
671 if (addr_taken)
672 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
674 tree var = referenced_var (i);
675 bitmap_set_bit (ai->addresses_needed, DECL_UID (var));
676 if (stmt_escapes_p)
677 mark_call_clobbered (var);
680 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
682 tree var = SSA_NAME_VAR (op);
683 var_ann_t v_ann = var_ann (var);
684 struct ptr_info_def *pi;
685 bool is_store;
686 unsigned num_uses, num_derefs;
688 /* If the operand's variable may be aliased, keep track
689 of how many times we've referenced it. This is used
690 for alias grouping in compute_flow_sensitive_aliasing.
691 Note that we don't need to grow AI->NUM_REFERENCES
692 because we are processing regular variables, not
693 memory tags (the array's initial size is set to
694 NUM_REFERENCED_VARS). */
695 if (may_be_aliased (var))
696 NUM_REFERENCES_INC (v_ann);
698 if (!POINTER_TYPE_P (TREE_TYPE (op)))
699 continue;
701 collect_points_to_info_for (ai, op);
703 pi = SSA_NAME_PTR_INFO (op);
704 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs,
705 &is_store);
707 if (num_derefs > 0)
709 /* Mark OP as dereferenced. In a subsequent pass,
710 dereferenced pointers that point to a set of
711 variables will be assigned a name tag to alias
712 all the variables OP points to. */
713 pi->is_dereferenced = 1;
715 /* Keep track of how many time we've dereferenced each
716 pointer. */
717 NUM_REFERENCES_INC (v_ann);
719 /* If this is a store operation, mark OP as being
720 dereferenced to store, otherwise mark it as being
721 dereferenced to load. */
722 if (is_store)
723 bitmap_set_bit (ai->dereferenced_ptrs_store,
724 DECL_UID (var));
725 else
726 bitmap_set_bit (ai->dereferenced_ptrs_load,
727 DECL_UID (var));
730 if (stmt_escapes_p && num_derefs < num_uses)
732 /* If STMT is an escape point and STMT contains at
733 least one direct use of OP, then the value of OP
734 escapes and so the pointed-to variables need to
735 be marked call-clobbered. */
736 pi->value_escapes_p = 1;
738 /* If the statement makes a function call, assume
739 that pointer OP will be dereferenced in a store
740 operation inside the called function. */
741 if (get_call_expr_in (stmt))
743 bitmap_set_bit (ai->dereferenced_ptrs_store,
744 DECL_UID (var));
745 pi->is_dereferenced = 1;
750 /* Update reference counter for definitions to any
751 potentially aliased variable. This is used in the alias
752 grouping heuristics. */
753 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
755 tree var = SSA_NAME_VAR (op);
756 var_ann_t ann = var_ann (var);
757 bitmap_set_bit (ai->written_vars, DECL_UID (var));
758 if (may_be_aliased (var))
759 NUM_REFERENCES_INC (ann);
761 if (POINTER_TYPE_P (TREE_TYPE (op)))
762 collect_points_to_info_for (ai, op);
765 /* Mark variables in V_MAY_DEF operands as being written to. */
766 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
768 tree var = DECL_P (op) ? op : SSA_NAME_VAR (op);
769 bitmap_set_bit (ai->written_vars, DECL_UID (var));
772 /* After promoting variables and computing aliasing we will
773 need to re-scan most statements. FIXME: Try to minimize the
774 number of statements re-scanned. It's not really necessary to
775 re-scan *all* statements. */
776 mark_stmt_modified (stmt);
780 timevar_pop (TV_TREE_PTA);
784 /* Create name tags for all the pointers that have been dereferenced.
785 We only create a name tag for a pointer P if P is found to point to
786 a set of variables (so that we can alias them to *P) or if it is
787 the result of a call to malloc (which means that P cannot point to
788 anything else nor alias any other variable).
790 If two pointers P and Q point to the same set of variables, they
791 are assigned the same name tag. */
793 static void
794 create_name_tags (void)
796 size_t i;
798 for (i = 1; i < num_ssa_names; i++)
800 tree ptr = ssa_name (i);
801 struct ptr_info_def *pi;
803 if (!ptr
804 || !POINTER_TYPE_P (TREE_TYPE (ptr))
805 || !SSA_NAME_PTR_INFO (ptr))
806 continue;
808 pi = SSA_NAME_PTR_INFO (ptr);
810 if (pi->pt_anything || !pi->is_dereferenced)
812 /* No name tags for pointers that have not been
813 dereferenced or point to an arbitrary location. */
814 pi->name_mem_tag = NULL_TREE;
815 continue;
818 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
820 size_t j;
821 tree old_name_tag = pi->name_mem_tag;
823 /* If PTR points to a set of variables, check if we don't
824 have another pointer Q with the same points-to set before
825 creating a tag. If so, use Q's tag instead of creating a
826 new one.
828 This is important for not creating unnecessary symbols
829 and also for copy propagation. If we ever need to
830 propagate PTR into Q or vice-versa, we would run into
831 problems if they both had different name tags because
832 they would have different SSA version numbers (which
833 would force us to take the name tags in and out of SSA). */
834 for (j = 1; j < i; j++)
836 tree q = ssa_name (j);
837 struct ptr_info_def *qi;
839 if (!q || !POINTER_TYPE_P (TREE_TYPE (q)))
840 continue;
842 qi = SSA_NAME_PTR_INFO (q);
844 if (qi
845 && qi->pt_vars
846 && qi->name_mem_tag
847 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
849 pi->name_mem_tag = qi->name_mem_tag;
850 break;
854 /* If we didn't find a pointer with the same points-to set
855 as PTR, create a new name tag if needed. */
856 if (pi->name_mem_tag == NULL_TREE)
857 pi->name_mem_tag = get_nmt_for (ptr);
859 /* If the new name tag computed for PTR is different than
860 the old name tag that it used to have, then the old tag
861 needs to be removed from the IL, so we mark it for
862 renaming. */
863 if (old_name_tag && old_name_tag != pi->name_mem_tag)
864 mark_sym_for_renaming (old_name_tag);
866 else if (pi->pt_malloc)
868 /* Otherwise, create a unique name tag for this pointer. */
869 pi->name_mem_tag = get_nmt_for (ptr);
871 else
873 /* Only pointers that may point to malloc or other variables
874 may receive a name tag. If the pointer does not point to
875 a known spot, we should use type tags. */
876 set_pt_anything (ptr);
877 continue;
880 TREE_THIS_VOLATILE (pi->name_mem_tag)
881 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
883 /* Mark the new name tag for renaming. */
884 mark_sym_for_renaming (pi->name_mem_tag);
889 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
890 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
891 name tag and the variables it points-to are call-clobbered. Finally, if
892 P_i escapes and we could not determine where it points to, then all the
893 variables in the same alias set as *P_i are marked call-clobbered. This
894 is necessary because we must assume that P_i may take the address of any
895 variable in the same alias set. */
897 static void
898 compute_flow_sensitive_aliasing (struct alias_info *ai)
900 size_t i;
902 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
904 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
905 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
906 if (pi->pt_anything || pi->pt_vars == NULL)
908 find_what_p_points_to (ptr);
912 create_name_tags ();
914 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
916 unsigned j;
917 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
918 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
919 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
920 bitmap_iterator bi;
922 if (pi->value_escapes_p || pi->pt_anything)
924 /* If PTR escapes or may point to anything, then its associated
925 memory tags and pointed-to variables are call-clobbered. */
926 if (pi->name_mem_tag)
927 mark_call_clobbered (pi->name_mem_tag);
929 if (v_ann->type_mem_tag)
930 mark_call_clobbered (v_ann->type_mem_tag);
932 if (pi->pt_vars)
933 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
935 mark_call_clobbered (referenced_var (j));
939 /* Set up aliasing information for PTR's name memory tag (if it has
940 one). Note that only pointers that have been dereferenced will
941 have a name memory tag. */
942 if (pi->name_mem_tag && pi->pt_vars)
943 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
945 add_may_alias (pi->name_mem_tag, referenced_var (j));
946 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
949 /* If the name tag is call clobbered, so is the type tag
950 associated with the base VAR_DECL. */
951 if (pi->name_mem_tag
952 && v_ann->type_mem_tag
953 && is_call_clobbered (pi->name_mem_tag))
954 mark_call_clobbered (v_ann->type_mem_tag);
959 /* Compute type-based alias sets. Traverse all the pointers and
960 addressable variables found in setup_pointers_and_addressables.
962 For every pointer P in AI->POINTERS and addressable variable V in
963 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
964 memory tag (TMT) if their alias sets conflict. V is then marked as
965 an alias tag so that the operand scanner knows that statements
966 containing V have aliased operands. */
968 static void
969 compute_flow_insensitive_aliasing (struct alias_info *ai)
971 size_t i;
973 /* Initialize counter for the total number of virtual operands that
974 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
975 threshold set by --params max-alias-vops, we enable alias
976 grouping. */
977 ai->total_alias_vops = 0;
979 /* For every pointer P, determine which addressable variables may alias
980 with P's type memory tag. */
981 for (i = 0; i < ai->num_pointers; i++)
983 size_t j;
984 struct alias_map_d *p_map = ai->pointers[i];
985 tree tag = var_ann (p_map->var)->type_mem_tag;
986 var_ann_t tag_ann = var_ann (tag);
988 p_map->total_alias_vops = 0;
989 p_map->may_aliases = BITMAP_ALLOC (&alias_obstack);
991 for (j = 0; j < ai->num_addressable_vars; j++)
993 struct alias_map_d *v_map;
994 var_ann_t v_ann;
995 tree var;
996 bool tag_stored_p, var_stored_p;
998 v_map = ai->addressable_vars[j];
999 var = v_map->var;
1000 v_ann = var_ann (var);
1002 /* Skip memory tags and variables that have never been
1003 written to. We also need to check if the variables are
1004 call-clobbered because they may be overwritten by
1005 function calls.
1007 Note this is effectively random accessing elements in
1008 the sparse bitset, which can be highly inefficient.
1009 So we first check the call_clobbered status of the
1010 tag and variable before querying the bitmap. */
1011 tag_stored_p = is_call_clobbered (tag)
1012 || bitmap_bit_p (ai->written_vars, DECL_UID (tag));
1013 var_stored_p = is_call_clobbered (var)
1014 || bitmap_bit_p (ai->written_vars, DECL_UID (var));
1015 if (!tag_stored_p && !var_stored_p)
1016 continue;
1018 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
1020 subvar_t svars;
1021 size_t num_tag_refs, num_var_refs;
1023 num_tag_refs = NUM_REFERENCES (tag_ann);
1024 num_var_refs = NUM_REFERENCES (v_ann);
1026 /* Add VAR to TAG's may-aliases set. */
1028 /* If this is an aggregate, we may have subvariables for it
1029 that need to be pointed to. */
1030 if (var_can_have_subvars (var)
1031 && (svars = get_subvars_for_var (var)))
1033 subvar_t sv;
1035 for (sv = svars; sv; sv = sv->next)
1037 add_may_alias (tag, sv->var);
1038 /* Update the bitmap used to represent TAG's alias set
1039 in case we need to group aliases. */
1040 bitmap_set_bit (p_map->may_aliases, DECL_UID (sv->var));
1043 else
1045 add_may_alias (tag, var);
1046 /* Update the bitmap used to represent TAG's alias set
1047 in case we need to group aliases. */
1048 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
1051 /* Update the total number of virtual operands due to
1052 aliasing. Since we are adding one more alias to TAG's
1053 may-aliases set, the total number of virtual operands due
1054 to aliasing will be increased by the number of references
1055 made to VAR and TAG (every reference to TAG will also
1056 count as a reference to VAR). */
1057 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1058 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1065 /* Since this analysis is based exclusively on symbols, it fails to
1066 handle cases where two pointers P and Q have different memory
1067 tags with conflicting alias set numbers but no aliased symbols in
1068 common.
1070 For example, suppose that we have two memory tags TMT.1 and TMT.2
1071 such that
1073 may-aliases (TMT.1) = { a }
1074 may-aliases (TMT.2) = { b }
1076 and the alias set number of TMT.1 conflicts with that of TMT.2.
1077 Since they don't have symbols in common, loads and stores from
1078 TMT.1 and TMT.2 will seem independent of each other, which will
1079 lead to the optimizers making invalid transformations (see
1080 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1082 To avoid this problem, we do a final traversal of AI->POINTERS
1083 looking for pairs of pointers that have no aliased symbols in
1084 common and yet have conflicting alias set numbers. */
1085 for (i = 0; i < ai->num_pointers; i++)
1087 size_t j;
1088 struct alias_map_d *p_map1 = ai->pointers[i];
1089 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1090 bitmap may_aliases1 = p_map1->may_aliases;
1092 for (j = i + 1; j < ai->num_pointers; j++)
1094 struct alias_map_d *p_map2 = ai->pointers[j];
1095 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1096 bitmap may_aliases2 = p_map2->may_aliases;
1098 /* If the pointers may not point to each other, do nothing. */
1099 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set))
1100 continue;
1102 /* The two pointers may alias each other. If they already have
1103 symbols in common, do nothing. */
1104 if (bitmap_intersect_p (may_aliases1, may_aliases2))
1105 continue;
1107 if (!bitmap_empty_p (may_aliases2))
1109 unsigned int k;
1110 bitmap_iterator bi;
1112 /* Add all the aliases for TAG2 into TAG1's alias set.
1113 FIXME, update grouping heuristic counters. */
1114 EXECUTE_IF_SET_IN_BITMAP (may_aliases2, 0, k, bi)
1115 add_may_alias (tag1, referenced_var (k));
1116 bitmap_ior_into (may_aliases1, may_aliases2);
1118 else
1120 /* Since TAG2 does not have any aliases of its own, add
1121 TAG2 itself to the alias set of TAG1. */
1122 add_may_alias (tag1, tag2);
1123 bitmap_set_bit (may_aliases1, DECL_UID (tag2));
1128 if (dump_file)
1129 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
1130 get_name (current_function_decl),
1131 ai->total_alias_vops);
1133 /* Determine if we need to enable alias grouping. */
1134 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1135 group_aliases (ai);
1139 /* Comparison function for qsort used in group_aliases. */
1141 static int
1142 total_alias_vops_cmp (const void *p, const void *q)
1144 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1145 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1146 long n1 = (*p1)->total_alias_vops;
1147 long n2 = (*p2)->total_alias_vops;
1149 /* We want to sort in descending order. */
1150 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1153 /* Group all the aliases for TAG to make TAG represent all the
1154 variables in its alias set. Update the total number
1155 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1156 function will make TAG be the unique alias tag for all the
1157 variables in its may-aliases. So, given:
1159 may-aliases(TAG) = { V1, V2, V3 }
1161 This function will group the variables into:
1163 may-aliases(V1) = { TAG }
1164 may-aliases(V2) = { TAG }
1165 may-aliases(V2) = { TAG } */
1167 static void
1168 group_aliases_into (tree tag, bitmap tag_aliases, struct alias_info *ai)
1170 unsigned int i;
1171 var_ann_t tag_ann = var_ann (tag);
1172 size_t num_tag_refs = NUM_REFERENCES (tag_ann);
1173 bitmap_iterator bi;
1175 EXECUTE_IF_SET_IN_BITMAP (tag_aliases, 0, i, bi)
1177 tree var = referenced_var (i);
1178 var_ann_t ann = var_ann (var);
1180 /* Make TAG the unique alias of VAR. */
1181 ann->is_alias_tag = 0;
1182 ann->may_aliases = NULL;
1184 /* Note that VAR and TAG may be the same if the function has no
1185 addressable variables (see the discussion at the end of
1186 setup_pointers_and_addressables). */
1187 if (var != tag)
1188 add_may_alias (var, tag);
1190 /* Reduce total number of virtual operands contributed
1191 by TAG on behalf of VAR. Notice that the references to VAR
1192 itself won't be removed. We will merely replace them with
1193 references to TAG. */
1194 ai->total_alias_vops -= num_tag_refs;
1197 /* We have reduced the number of virtual operands that TAG makes on
1198 behalf of all the variables formerly aliased with it. However,
1199 we have also "removed" all the virtual operands for TAG itself,
1200 so we add them back. */
1201 ai->total_alias_vops += num_tag_refs;
1203 /* TAG no longer has any aliases. */
1204 tag_ann->may_aliases = NULL;
1208 /* Group may-aliases sets to reduce the number of virtual operands due
1209 to aliasing.
1211 1- Sort the list of pointers in decreasing number of contributed
1212 virtual operands.
1214 2- Take the first entry in AI->POINTERS and revert the role of
1215 the memory tag and its aliases. Usually, whenever an aliased
1216 variable Vi is found to alias with a memory tag T, we add Vi
1217 to the may-aliases set for T. Meaning that after alias
1218 analysis, we will have:
1220 may-aliases(T) = { V1, V2, V3, ..., Vn }
1222 This means that every statement that references T, will get 'n'
1223 virtual operands for each of the Vi tags. But, when alias
1224 grouping is enabled, we make T an alias tag and add it to the
1225 alias set of all the Vi variables:
1227 may-aliases(V1) = { T }
1228 may-aliases(V2) = { T }
1230 may-aliases(Vn) = { T }
1232 This has two effects: (a) statements referencing T will only get
1233 a single virtual operand, and, (b) all the variables Vi will now
1234 appear to alias each other. So, we lose alias precision to
1235 improve compile time. But, in theory, a program with such a high
1236 level of aliasing should not be very optimizable in the first
1237 place.
1239 3- Since variables may be in the alias set of more than one
1240 memory tag, the grouping done in step (2) needs to be extended
1241 to all the memory tags that have a non-empty intersection with
1242 the may-aliases set of tag T. For instance, if we originally
1243 had these may-aliases sets:
1245 may-aliases(T) = { V1, V2, V3 }
1246 may-aliases(R) = { V2, V4 }
1248 In step (2) we would have reverted the aliases for T as:
1250 may-aliases(V1) = { T }
1251 may-aliases(V2) = { T }
1252 may-aliases(V3) = { T }
1254 But note that now V2 is no longer aliased with R. We could
1255 add R to may-aliases(V2), but we are in the process of
1256 grouping aliases to reduce virtual operands so what we do is
1257 add V4 to the grouping to obtain:
1259 may-aliases(V1) = { T }
1260 may-aliases(V2) = { T }
1261 may-aliases(V3) = { T }
1262 may-aliases(V4) = { T }
1264 4- If the total number of virtual operands due to aliasing is
1265 still above the threshold set by max-alias-vops, go back to (2). */
1267 static void
1268 group_aliases (struct alias_info *ai)
1270 size_t i;
1272 /* Sort the POINTERS array in descending order of contributed
1273 virtual operands. */
1274 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1275 total_alias_vops_cmp);
1277 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1278 and the tag's may-aliases set. */
1279 for (i = 0; i < ai->num_pointers; i++)
1281 size_t j;
1282 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1283 bitmap tag1_aliases = ai->pointers[i]->may_aliases;
1285 /* Skip tags that have been grouped already. */
1286 if (ai->pointers[i]->grouped_p)
1287 continue;
1289 /* See if TAG1 had any aliases in common with other type tags.
1290 If we find a TAG2 with common aliases with TAG1, add TAG2's
1291 aliases into TAG1. */
1292 for (j = i + 1; j < ai->num_pointers; j++)
1294 bitmap tag2_aliases = ai->pointers[j]->may_aliases;
1296 if (bitmap_intersect_p (tag1_aliases, tag2_aliases))
1298 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1300 bitmap_ior_into (tag1_aliases, tag2_aliases);
1302 /* TAG2 does not need its aliases anymore. */
1303 bitmap_clear (tag2_aliases);
1304 var_ann (tag2)->may_aliases = NULL;
1306 /* TAG1 is the unique alias of TAG2. */
1307 add_may_alias (tag2, tag1);
1309 ai->pointers[j]->grouped_p = true;
1313 /* Now group all the aliases we collected into TAG1. */
1314 group_aliases_into (tag1, tag1_aliases, ai);
1316 /* If we've reduced total number of virtual operands below the
1317 threshold, stop. */
1318 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1319 break;
1322 /* Finally, all the variables that have been grouped cannot be in
1323 the may-alias set of name memory tags. Suppose that we have
1324 grouped the aliases in this code so that may-aliases(a) = TMT.20
1326 p_5 = &a;
1328 # a_9 = V_MAY_DEF <a_8>
1329 p_5->field = 0
1330 ... Several modifications to TMT.20 ...
1331 # VUSE <a_9>
1332 x_30 = p_5->field
1334 Since p_5 points to 'a', the optimizers will try to propagate 0
1335 into p_5->field, but that is wrong because there have been
1336 modifications to 'TMT.20' in between. To prevent this we have to
1337 replace 'a' with 'TMT.20' in the name tag of p_5. */
1338 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1340 size_t j;
1341 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1342 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1343 varray_type aliases;
1345 if (name_tag == NULL_TREE)
1346 continue;
1348 aliases = var_ann (name_tag)->may_aliases;
1349 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1351 tree alias = VARRAY_TREE (aliases, j);
1352 var_ann_t ann = var_ann (alias);
1354 if ((ann->mem_tag_kind == NOT_A_TAG
1355 || ann->mem_tag_kind == STRUCT_FIELD)
1356 && ann->may_aliases)
1358 tree new_alias;
1360 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1362 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1363 replace_may_alias (name_tag, j, new_alias);
1368 if (dump_file)
1369 fprintf (dump_file,
1370 "%s: Total number of aliased vops after grouping: %ld%s\n",
1371 get_name (current_function_decl),
1372 ai->total_alias_vops,
1373 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1377 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1379 static void
1380 create_alias_map_for (tree var, struct alias_info *ai)
1382 struct alias_map_d *alias_map;
1383 alias_map = xcalloc (1, sizeof (*alias_map));
1384 alias_map->var = var;
1385 alias_map->set = get_alias_set (var);
1386 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1390 /* Create memory tags for all the dereferenced pointers and build the
1391 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1392 sets. Based on the address escape and points-to information collected
1393 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1394 variables whose address is not needed anymore. */
1396 static void
1397 setup_pointers_and_addressables (struct alias_info *ai)
1399 size_t n_vars, num_addressable_vars, num_pointers;
1400 referenced_var_iterator rvi;
1401 tree var;
1403 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1404 num_addressable_vars = num_pointers = 0;
1406 FOR_EACH_REFERENCED_VAR (var, rvi)
1408 if (may_be_aliased (var))
1409 num_addressable_vars++;
1411 if (POINTER_TYPE_P (TREE_TYPE (var)))
1413 /* Since we don't keep track of volatile variables, assume that
1414 these pointers are used in indirect store operations. */
1415 if (TREE_THIS_VOLATILE (var))
1416 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
1418 num_pointers++;
1422 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1423 always going to be slightly bigger than we actually need them
1424 because some TREE_ADDRESSABLE variables will be marked
1425 non-addressable below and only pointers with unique type tags are
1426 going to be added to POINTERS. */
1427 ai->addressable_vars = xcalloc (num_addressable_vars,
1428 sizeof (struct alias_map_d *));
1429 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1430 ai->num_addressable_vars = 0;
1431 ai->num_pointers = 0;
1433 /* Since we will be creating type memory tags within this loop, cache the
1434 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1435 unnecessarily. */
1436 n_vars = num_referenced_vars;
1438 FOR_EACH_REFERENCED_VAR (var, rvi)
1440 var_ann_t v_ann = var_ann (var);
1441 subvar_t svars;
1443 /* Name memory tags already have flow-sensitive aliasing
1444 information, so they need not be processed by
1445 compute_flow_insensitive_aliasing. Similarly, type memory
1446 tags are already accounted for when we process their
1447 associated pointer.
1449 Structure fields, on the other hand, have to have some of this
1450 information processed for them, but it's pointless to mark them
1451 non-addressable (since they are fake variables anyway). */
1452 if (v_ann->mem_tag_kind != NOT_A_TAG
1453 && v_ann->mem_tag_kind != STRUCT_FIELD)
1454 continue;
1456 /* Remove the ADDRESSABLE flag from every addressable variable whose
1457 address is not needed anymore. This is caused by the propagation
1458 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1459 removal of dead pointer assignments done by the early scalar
1460 cleanup passes. */
1461 if (TREE_ADDRESSABLE (var) && v_ann->mem_tag_kind != STRUCT_FIELD)
1463 if (!bitmap_bit_p (ai->addresses_needed, DECL_UID (var))
1464 && TREE_CODE (var) != RESULT_DECL
1465 && !is_global_var (var))
1467 bool okay_to_mark = true;
1469 /* Since VAR is now a regular GIMPLE register, we will need
1470 to rename VAR into SSA afterwards. */
1471 mark_sym_for_renaming (var);
1473 if (var_can_have_subvars (var)
1474 && (svars = get_subvars_for_var (var)))
1476 subvar_t sv;
1478 for (sv = svars; sv; sv = sv->next)
1480 if (bitmap_bit_p (ai->addresses_needed,
1481 DECL_UID (sv->var)))
1482 okay_to_mark = false;
1483 mark_sym_for_renaming (sv->var);
1487 /* The address of VAR is not needed, remove the
1488 addressable bit, so that it can be optimized as a
1489 regular variable. */
1490 if (okay_to_mark)
1491 mark_non_addressable (var);
1493 else
1495 /* Add the variable to the set of addressables. Mostly
1496 used when scanning operands for ASM_EXPRs that
1497 clobber memory. In those cases, we need to clobber
1498 all call-clobbered variables and all addressables. */
1499 bitmap_set_bit (addressable_vars, DECL_UID (var));
1500 if (var_can_have_subvars (var)
1501 && (svars = get_subvars_for_var (var)))
1503 subvar_t sv;
1504 for (sv = svars; sv; sv = sv->next)
1505 bitmap_set_bit (addressable_vars, DECL_UID (sv->var));
1511 /* Global variables and addressable locals may be aliased. Create an
1512 entry in ADDRESSABLE_VARS for VAR. */
1513 if (may_be_aliased (var))
1515 create_alias_map_for (var, ai);
1516 mark_sym_for_renaming (var);
1519 /* Add pointer variables that have been dereferenced to the POINTERS
1520 array and create a type memory tag for them. */
1521 if (POINTER_TYPE_P (TREE_TYPE (var)))
1523 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var))
1524 || bitmap_bit_p (ai->dereferenced_ptrs_load, DECL_UID (var))))
1526 tree tag;
1527 var_ann_t t_ann;
1529 /* If pointer VAR still doesn't have a memory tag
1530 associated with it, create it now or re-use an
1531 existing one. */
1532 tag = get_tmt_for (var, ai);
1533 t_ann = var_ann (tag);
1535 /* The type tag will need to be renamed into SSA
1536 afterwards. Note that we cannot do this inside
1537 get_tmt_for because aliasing may run multiple times
1538 and we only create type tags the first time. */
1539 mark_sym_for_renaming (tag);
1541 /* Similarly, if pointer VAR used to have another type
1542 tag, we will need to process it in the renamer to
1543 remove the stale virtual operands. */
1544 if (v_ann->type_mem_tag)
1545 mark_sym_for_renaming (v_ann->type_mem_tag);
1547 /* Associate the tag with pointer VAR. */
1548 v_ann->type_mem_tag = tag;
1550 /* If pointer VAR has been used in a store operation,
1551 then its memory tag must be marked as written-to. */
1552 if (bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var)))
1553 bitmap_set_bit (ai->written_vars, DECL_UID (tag));
1555 /* If pointer VAR is a global variable or a PARM_DECL,
1556 then its memory tag should be considered a global
1557 variable. */
1558 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1559 mark_call_clobbered (tag);
1561 /* All the dereferences of pointer VAR count as
1562 references of TAG. Since TAG can be associated with
1563 several pointers, add the dereferences of VAR to the
1564 TAG. */
1566 NUM_REFERENCES_SET (t_ann,
1567 NUM_REFERENCES (t_ann) +
1568 NUM_REFERENCES (v_ann));
1570 else
1572 /* The pointer has not been dereferenced. If it had a
1573 type memory tag, remove it and mark the old tag for
1574 renaming to remove it out of the IL. */
1575 var_ann_t ann = var_ann (var);
1576 tree tag = ann->type_mem_tag;
1577 if (tag)
1579 mark_sym_for_renaming (tag);
1580 ann->type_mem_tag = NULL_TREE;
1588 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1589 every call site, we need to emit V_MAY_DEF expressions to represent the
1590 clobbering effects of the call for variables whose address escapes the
1591 current function.
1593 One approach is to group all call-clobbered variables into a single
1594 representative that is used as an alias of every call-clobbered variable
1595 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1596 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1598 The second approach is to emit a clobbering V_MAY_DEF for every
1599 call-clobbered variable at call sites. This is the preferred way in terms
1600 of optimization opportunities but it may create too many V_MAY_DEF operands
1601 if there are many call clobbered variables and function calls in the
1602 function.
1604 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1605 function calls found by the number of call-clobbered variables. If that
1606 product is beyond a certain threshold, as determined by the parameterized
1607 values shown below, we use .GLOBAL_VAR.
1609 FIXME. This heuristic should be improved. One idea is to use several
1610 .GLOBAL_VARs of different types instead of a single one. The thresholds
1611 have been derived from a typical bootstrap cycle, including all target
1612 libraries. Compile times were found increase by ~1% compared to using
1613 .GLOBAL_VAR. */
1615 static void
1616 maybe_create_global_var (struct alias_info *ai)
1618 unsigned i, n_clobbered;
1619 bitmap_iterator bi;
1621 /* No need to create it, if we have one already. */
1622 if (global_var == NULL_TREE)
1624 /* Count all the call-clobbered variables. */
1625 n_clobbered = 0;
1626 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1628 n_clobbered++;
1631 /* If the number of virtual operands that would be needed to
1632 model all the call-clobbered variables is larger than
1633 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1635 Also create .GLOBAL_VAR if there are no call-clobbered
1636 variables and the program contains a mixture of pure/const
1637 and regular function calls. This is to avoid the problem
1638 described in PR 20115:
1640 int X;
1641 int func_pure (void) { return X; }
1642 int func_non_pure (int a) { X += a; }
1643 int foo ()
1645 int a = func_pure ();
1646 func_non_pure (a);
1647 a = func_pure ();
1648 return a;
1651 Since foo() has no call-clobbered variables, there is
1652 no relationship between the calls to func_pure and
1653 func_non_pure. Since func_pure has no side-effects, value
1654 numbering optimizations elide the second call to func_pure.
1655 So, if we have some pure/const and some regular calls in the
1656 program we create .GLOBAL_VAR to avoid missing these
1657 relations. */
1658 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1659 || (n_clobbered == 0
1660 && ai->num_calls_found > 0
1661 && ai->num_pure_const_calls_found > 0
1662 && ai->num_calls_found > ai->num_pure_const_calls_found))
1663 create_global_var ();
1666 /* Mark all call-clobbered symbols for renaming. Since the initial
1667 rewrite into SSA ignored all call sites, we may need to rename
1668 .GLOBAL_VAR and the call-clobbered variables. */
1669 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1671 tree var = referenced_var (i);
1673 /* If the function has calls to clobbering functions and
1674 .GLOBAL_VAR has been created, make it an alias for all
1675 call-clobbered variables. */
1676 if (global_var && var != global_var)
1678 subvar_t svars;
1679 add_may_alias (var, global_var);
1680 if (var_can_have_subvars (var)
1681 && (svars = get_subvars_for_var (var)))
1683 subvar_t sv;
1684 for (sv = svars; sv; sv = sv->next)
1685 mark_sym_for_renaming (sv->var);
1689 mark_sym_for_renaming (var);
1694 /* Return TRUE if pointer PTR may point to variable VAR.
1696 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1697 This is needed because when checking for type conflicts we are
1698 interested in the alias set of the memory location pointed-to by
1699 PTR. The alias set of PTR itself is irrelevant.
1701 VAR_ALIAS_SET is the alias set for VAR. */
1703 static bool
1704 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1705 tree var, HOST_WIDE_INT var_alias_set)
1707 tree mem;
1708 var_ann_t m_ann;
1710 alias_stats.alias_queries++;
1711 alias_stats.simple_queries++;
1713 /* By convention, a variable cannot alias itself. */
1714 mem = var_ann (ptr)->type_mem_tag;
1715 if (mem == var)
1717 alias_stats.alias_noalias++;
1718 alias_stats.simple_resolved++;
1719 return false;
1722 /* If -fargument-noalias-global is >1, pointer arguments may
1723 not point to global variables. */
1724 if (flag_argument_noalias > 1 && is_global_var (var)
1725 && TREE_CODE (ptr) == PARM_DECL)
1727 alias_stats.alias_noalias++;
1728 alias_stats.simple_resolved++;
1729 return false;
1732 /* If either MEM or VAR is a read-only global and the other one
1733 isn't, then PTR cannot point to VAR. */
1734 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1735 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1737 alias_stats.alias_noalias++;
1738 alias_stats.simple_resolved++;
1739 return false;
1742 m_ann = var_ann (mem);
1744 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1746 alias_stats.tbaa_queries++;
1748 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1749 PTR can't possibly affect VAR. Note, that we are specifically testing
1750 for PTR's alias set here, not its pointed-to type. We also can't
1751 do this check with relaxed aliasing enabled. */
1752 if (POINTER_TYPE_P (TREE_TYPE (var))
1753 && var_alias_set != 0
1754 && mem_alias_set != 0)
1756 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1757 if (ptr_alias_set == var_alias_set)
1759 alias_stats.alias_noalias++;
1760 alias_stats.tbaa_resolved++;
1761 return false;
1765 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1766 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1768 alias_stats.alias_noalias++;
1769 alias_stats.tbaa_resolved++;
1770 return false;
1772 alias_stats.alias_mayalias++;
1773 return true;
1777 /* Add ALIAS to the set of variables that may alias VAR. */
1779 static void
1780 add_may_alias (tree var, tree alias)
1782 size_t i;
1783 var_ann_t v_ann = get_var_ann (var);
1784 var_ann_t a_ann = get_var_ann (alias);
1786 gcc_assert (var != alias);
1788 if (v_ann->may_aliases == NULL)
1789 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1791 /* Avoid adding duplicates. */
1792 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1793 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1794 return;
1796 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1797 FIXME, call-clobbering should only depend on whether an address
1798 escapes. It should be independent of aliasing. */
1799 if (is_call_clobbered (var))
1800 mark_call_clobbered (alias);
1802 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1803 else if (is_call_clobbered (alias))
1804 mark_call_clobbered (var);
1806 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1807 a_ann->is_alias_tag = 1;
1811 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1813 static void
1814 replace_may_alias (tree var, size_t i, tree new_alias)
1816 var_ann_t v_ann = var_ann (var);
1817 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1819 /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1820 FIXME, call-clobbering should only depend on whether an address
1821 escapes. It should be independent of aliasing. */
1822 if (is_call_clobbered (var))
1823 mark_call_clobbered (new_alias);
1825 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1826 else if (is_call_clobbered (new_alias))
1827 mark_call_clobbered (var);
1831 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1833 static void
1834 set_pt_anything (tree ptr)
1836 struct ptr_info_def *pi = get_ptr_info (ptr);
1838 pi->pt_anything = 1;
1839 pi->pt_malloc = 0;
1841 /* The pointer used to have a name tag, but we now found it pointing
1842 to an arbitrary location. The name tag needs to be renamed and
1843 disassociated from PTR. */
1844 if (pi->name_mem_tag)
1846 mark_sym_for_renaming (pi->name_mem_tag);
1847 pi->name_mem_tag = NULL_TREE;
1852 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1854 static void
1855 set_pt_malloc (tree ptr)
1857 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1859 /* If the pointer has already been found to point to arbitrary
1860 memory locations, it is unsafe to mark it as pointing to malloc. */
1861 if (pi->pt_anything)
1862 return;
1864 pi->pt_malloc = 1;
1868 /* Given two different pointers DEST and ORIG. Merge the points-to
1869 information in ORIG into DEST. AI contains all the alias
1870 information collected up to this point. */
1872 static void
1873 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1875 struct ptr_info_def *dest_pi, *orig_pi;
1877 gcc_assert (dest != orig);
1879 /* Make sure we have points-to information for ORIG. */
1880 collect_points_to_info_for (ai, orig);
1882 dest_pi = get_ptr_info (dest);
1883 orig_pi = SSA_NAME_PTR_INFO (orig);
1885 if (orig_pi)
1887 gcc_assert (orig_pi != dest_pi);
1889 /* Notice that we never merge PT_MALLOC. This attribute is only
1890 true if the pointer is the result of a malloc() call.
1891 Otherwise, we can end up in this situation:
1893 P_i = malloc ();
1895 P_j = P_i + X;
1897 P_j would be marked as PT_MALLOC, however we currently do not
1898 handle cases of more than one pointer pointing to the same
1899 malloc'd area.
1901 FIXME: If the merging comes from an expression that preserves
1902 the PT_MALLOC attribute (copy assignment, address
1903 arithmetic), we ought to merge PT_MALLOC, but then both
1904 pointers would end up getting different name tags because
1905 create_name_tags is not smart enough to determine that the
1906 two come from the same malloc call. Copy propagation before
1907 aliasing should cure this. */
1908 dest_pi->pt_malloc = 0;
1909 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1910 set_pt_anything (dest);
1912 dest_pi->pt_null |= orig_pi->pt_null;
1914 if (!dest_pi->pt_anything
1915 && orig_pi->pt_vars
1916 && !bitmap_empty_p (orig_pi->pt_vars))
1918 if (dest_pi->pt_vars == NULL)
1920 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1921 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1923 else
1924 bitmap_ior_into (dest_pi->pt_vars, orig_pi->pt_vars);
1927 else
1928 set_pt_anything (dest);
1932 /* Add EXPR to the list of expressions pointed-to by PTR. */
1934 static void
1935 add_pointed_to_expr (struct alias_info *ai, tree ptr, tree expr)
1937 if (TREE_CODE (expr) == WITH_SIZE_EXPR)
1938 expr = TREE_OPERAND (expr, 0);
1940 get_ptr_info (ptr);
1942 if (TREE_CODE (expr) == CALL_EXPR
1943 && (call_expr_flags (expr) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1945 /* If EXPR is a malloc-like call, then the area pointed to PTR
1946 is guaranteed to not alias with anything else. */
1947 set_pt_malloc (ptr);
1949 else if (TREE_CODE (expr) == ADDR_EXPR)
1951 /* Found P_i = ADDR_EXPR */
1952 add_pointed_to_var (ai, ptr, expr);
1954 else if (TREE_CODE (expr) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (expr)))
1956 /* Found P_i = Q_j. */
1957 merge_pointed_to_info (ai, ptr, expr);
1959 else if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
1961 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1962 tree op0 = TREE_OPERAND (expr, 0);
1963 tree op1 = TREE_OPERAND (expr, 1);
1965 /* Both operands may be of pointer type. FIXME: Shouldn't
1966 we just expect PTR + OFFSET always? */
1967 if (POINTER_TYPE_P (TREE_TYPE (op0))
1968 && TREE_CODE (op0) != INTEGER_CST)
1970 if (TREE_CODE (op0) == SSA_NAME)
1971 merge_pointed_to_info (ai, ptr, op0);
1972 else if (TREE_CODE (op0) == ADDR_EXPR)
1973 add_pointed_to_var (ai, ptr, op0);
1974 else
1975 set_pt_anything (ptr);
1978 if (POINTER_TYPE_P (TREE_TYPE (op1))
1979 && TREE_CODE (op1) != INTEGER_CST)
1981 if (TREE_CODE (op1) == SSA_NAME)
1982 merge_pointed_to_info (ai, ptr, op1);
1983 else if (TREE_CODE (op1) == ADDR_EXPR)
1984 add_pointed_to_var (ai, ptr, op1);
1985 else
1986 set_pt_anything (ptr);
1989 /* Neither operand is a pointer? VAR can be pointing anywhere.
1990 FIXME: Shouldn't we asserting here? If we get here, we found
1991 PTR = INT_CST + INT_CST, which should not be a valid pointer
1992 expression. */
1993 if (!(POINTER_TYPE_P (TREE_TYPE (op0))
1994 && TREE_CODE (op0) != INTEGER_CST)
1995 && !(POINTER_TYPE_P (TREE_TYPE (op1))
1996 && TREE_CODE (op1) != INTEGER_CST))
1997 set_pt_anything (ptr);
1999 else if (integer_zerop (expr))
2001 /* EXPR is the NULL pointer. Mark PTR as pointing to NULL. */
2002 SSA_NAME_PTR_INFO (ptr)->pt_null = 1;
2004 else
2006 /* If we can't recognize the expression, assume that PTR may
2007 point anywhere. */
2008 set_pt_anything (ptr);
2013 /* If VALUE is of the form &DECL, add DECL to the set of variables
2014 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
2015 PTR. AI points to the collected alias information. */
2017 static void
2018 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
2020 struct ptr_info_def *pi = get_ptr_info (ptr);
2021 tree pt_var = NULL_TREE;
2022 HOST_WIDE_INT offset, size;
2023 tree addrop;
2024 size_t uid;
2025 tree ref;
2026 subvar_t svars;
2028 gcc_assert (TREE_CODE (value) == ADDR_EXPR);
2030 addrop = TREE_OPERAND (value, 0);
2031 if (REFERENCE_CLASS_P (addrop))
2032 pt_var = get_base_address (addrop);
2033 else
2034 pt_var = addrop;
2036 /* If this is a component_ref, see if we can get a smaller number of
2037 variables to take the address of. */
2038 if (TREE_CODE (addrop) == COMPONENT_REF
2039 && (ref = okay_component_ref_for_subvars (addrop, &offset ,&size)))
2041 subvar_t sv;
2042 svars = get_subvars_for_var (ref);
2044 uid = DECL_UID (pt_var);
2046 if (pi->pt_vars == NULL)
2047 pi->pt_vars = BITMAP_GGC_ALLOC ();
2048 /* If the variable is a global, mark the pointer as pointing to
2049 global memory (which will make its tag a global variable). */
2050 if (is_global_var (pt_var))
2051 pi->pt_global_mem = 1;
2053 for (sv = svars; sv; sv = sv->next)
2055 if (overlap_subvar (offset, size, sv, NULL))
2057 bitmap_set_bit (pi->pt_vars, DECL_UID (sv->var));
2058 bitmap_set_bit (ai->addresses_needed, DECL_UID (sv->var));
2062 else if (pt_var && SSA_VAR_P (pt_var))
2065 uid = DECL_UID (pt_var);
2067 if (pi->pt_vars == NULL)
2068 pi->pt_vars = BITMAP_GGC_ALLOC ();
2070 /* If this is an aggregate, we may have subvariables for it that need
2071 to be pointed to. */
2072 if (var_can_have_subvars (pt_var)
2073 && (svars = get_subvars_for_var (pt_var)))
2075 subvar_t sv;
2076 for (sv = svars; sv; sv = sv->next)
2078 uid = DECL_UID (sv->var);
2079 bitmap_set_bit (ai->addresses_needed, uid);
2080 bitmap_set_bit (pi->pt_vars, uid);
2083 else
2085 bitmap_set_bit (ai->addresses_needed, uid);
2086 bitmap_set_bit (pi->pt_vars, uid);
2089 /* If the variable is a global, mark the pointer as pointing to
2090 global memory (which will make its tag a global variable). */
2091 if (is_global_var (pt_var))
2092 pi->pt_global_mem = 1;
2097 /* Callback for walk_use_def_chains to gather points-to information from the
2098 SSA web.
2100 VAR is an SSA variable or a GIMPLE expression.
2102 STMT is the statement that generates the SSA variable or, if STMT is a
2103 PHI_NODE, VAR is one of the PHI arguments.
2105 DATA is a pointer to a structure of type ALIAS_INFO. */
2107 static bool
2108 collect_points_to_info_r (tree var, tree stmt, void *data)
2110 struct alias_info *ai = (struct alias_info *) data;
2112 if (dump_file && (dump_flags & TDF_DETAILS))
2114 fprintf (dump_file, "Visiting use-def links for ");
2115 print_generic_expr (dump_file, var, dump_flags);
2116 fprintf (dump_file, "\n");
2119 switch (TREE_CODE (stmt))
2121 case RETURN_EXPR:
2122 gcc_assert (TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR);
2123 stmt = TREE_OPERAND (stmt, 0);
2124 /* FALLTHRU */
2126 case MODIFY_EXPR:
2128 tree rhs = TREE_OPERAND (stmt, 1);
2129 STRIP_NOPS (rhs);
2130 add_pointed_to_expr (ai, var, rhs);
2131 break;
2134 case ASM_EXPR:
2135 /* Pointers defined by __asm__ statements can point anywhere. */
2136 set_pt_anything (var);
2137 break;
2139 case NOP_EXPR:
2140 if (IS_EMPTY_STMT (stmt))
2142 tree decl = SSA_NAME_VAR (var);
2144 if (TREE_CODE (decl) == PARM_DECL)
2145 add_pointed_to_expr (ai, var, decl);
2146 else if (DECL_INITIAL (decl))
2147 add_pointed_to_expr (ai, var, DECL_INITIAL (decl));
2148 else
2149 add_pointed_to_expr (ai, var, decl);
2151 break;
2153 case PHI_NODE:
2155 /* It STMT is a PHI node, then VAR is one of its arguments. The
2156 variable that we are analyzing is the LHS of the PHI node. */
2157 tree lhs = PHI_RESULT (stmt);
2159 switch (TREE_CODE (var))
2161 case ADDR_EXPR:
2162 add_pointed_to_var (ai, lhs, var);
2163 break;
2165 case SSA_NAME:
2166 /* Avoid unnecessary merges. */
2167 if (lhs != var)
2168 merge_pointed_to_info (ai, lhs, var);
2169 break;
2171 default:
2172 gcc_assert (is_gimple_min_invariant (var));
2173 add_pointed_to_expr (ai, lhs, var);
2174 break;
2176 break;
2179 default:
2180 gcc_unreachable ();
2183 return false;
2187 /* Return true if STMT is an "escape" site from the current function. Escape
2188 sites those statements which might expose the address of a variable
2189 outside the current function. STMT is an escape site iff:
2191 1- STMT is a function call, or
2192 2- STMT is an __asm__ expression, or
2193 3- STMT is an assignment to a non-local variable, or
2194 4- STMT is a return statement.
2196 AI points to the alias information collected so far. */
2198 static bool
2199 is_escape_site (tree stmt, struct alias_info *ai)
2201 tree call = get_call_expr_in (stmt);
2202 if (call != NULL_TREE)
2204 ai->num_calls_found++;
2206 if (!TREE_SIDE_EFFECTS (call))
2207 ai->num_pure_const_calls_found++;
2209 return true;
2211 else if (TREE_CODE (stmt) == ASM_EXPR)
2212 return true;
2213 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2215 tree lhs = TREE_OPERAND (stmt, 0);
2217 /* Get to the base of _REF nodes. */
2218 if (TREE_CODE (lhs) != SSA_NAME)
2219 lhs = get_base_address (lhs);
2221 /* If we couldn't recognize the LHS of the assignment, assume that it
2222 is a non-local store. */
2223 if (lhs == NULL_TREE)
2224 return true;
2226 /* If the RHS is a conversion between a pointer and an integer, the
2227 pointer escapes since we can't track the integer. */
2228 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2229 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2230 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2231 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
2232 (TREE_OPERAND (stmt, 1), 0)))
2233 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
2234 return true;
2236 /* If the LHS is an SSA name, it can't possibly represent a non-local
2237 memory store. */
2238 if (TREE_CODE (lhs) == SSA_NAME)
2239 return false;
2241 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2242 local variables we cannot be sure if it will escape, because we
2243 don't have information about objects not in SSA form. Need to
2244 implement something along the lines of
2246 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2247 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2248 Conference on Object-Oriented Programming Systems, Languages, and
2249 Applications (OOPSLA), pp. 1-19, 1999. */
2250 return true;
2252 else if (TREE_CODE (stmt) == RETURN_EXPR)
2253 return true;
2255 return false;
2259 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2260 is considered to represent all the pointers whose pointed-to types are
2261 in the same alias set class. Otherwise, the tag represents a single
2262 SSA_NAME pointer variable. */
2264 static tree
2265 create_memory_tag (tree type, bool is_type_tag)
2267 var_ann_t ann;
2268 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2270 /* By default, memory tags are local variables. Alias analysis will
2271 determine whether they should be considered globals. */
2272 DECL_CONTEXT (tag) = current_function_decl;
2274 /* Memory tags are by definition addressable. This also prevents
2275 is_gimple_ref frome confusing memory tags with optimizable
2276 variables. */
2277 TREE_ADDRESSABLE (tag) = 1;
2279 ann = get_var_ann (tag);
2280 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2281 ann->type_mem_tag = NULL_TREE;
2283 /* Add the tag to the symbol table. */
2284 add_referenced_tmp_var (tag);
2286 return tag;
2290 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2291 This is used if P_i has been found to point to a specific set of
2292 variables or to a non-aliased memory location like the address returned
2293 by malloc functions. */
2295 static tree
2296 get_nmt_for (tree ptr)
2298 struct ptr_info_def *pi = get_ptr_info (ptr);
2299 tree tag = pi->name_mem_tag;
2301 if (tag == NULL_TREE)
2302 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2304 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
2305 then its name tag should be considered a global variable. */
2306 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
2307 || pi->pt_malloc
2308 || pi->pt_global_mem)
2309 mark_call_clobbered (tag);
2311 return tag;
2315 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2316 artificial variable that represents the memory location pointed-to by
2317 PTR. It is used to model the effects of pointer de-references on
2318 addressable variables.
2320 AI points to the data gathered during alias analysis. This function
2321 populates the array AI->POINTERS. */
2323 static tree
2324 get_tmt_for (tree ptr, struct alias_info *ai)
2326 size_t i;
2327 tree tag;
2328 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2329 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2331 /* To avoid creating unnecessary memory tags, only create one memory tag
2332 per alias set class. Note that it may be tempting to group
2333 memory tags based on conflicting alias sets instead of
2334 equivalence. That would be wrong because alias sets are not
2335 necessarily transitive (as demonstrated by the libstdc++ test
2336 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2337 such that conflicts (A, B) == true and conflicts (A, C) == true,
2338 it does not necessarily follow that conflicts (B, C) == true. */
2339 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2341 struct alias_map_d *curr = ai->pointers[i];
2342 tree curr_tag = var_ann (curr->var)->type_mem_tag;
2343 if (tag_set == curr->set
2344 && TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (curr_tag)))
2346 tag = curr_tag;
2347 break;
2351 /* If VAR cannot alias with any of the existing memory tags, create a new
2352 tag for PTR and add it to the POINTERS array. */
2353 if (tag == NULL_TREE)
2355 struct alias_map_d *alias_map;
2357 /* If PTR did not have a type tag already, create a new TMT.*
2358 artificial variable representing the memory location
2359 pointed-to by PTR. */
2360 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2361 tag = create_memory_tag (tag_type, true);
2362 else
2363 tag = var_ann (ptr)->type_mem_tag;
2365 /* Add PTR to the POINTERS array. Note that we are not interested in
2366 PTR's alias set. Instead, we cache the alias set for the memory that
2367 PTR points to. */
2368 alias_map = xcalloc (1, sizeof (*alias_map));
2369 alias_map->var = ptr;
2370 alias_map->set = tag_set;
2371 ai->pointers[ai->num_pointers++] = alias_map;
2374 /* If the pointed-to type is volatile, so is the tag. */
2375 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2377 /* Make sure that the type tag has the same alias set as the
2378 pointed-to type. */
2379 gcc_assert (tag_set == get_alias_set (tag));
2381 /* If PTR's pointed-to type is read-only, then TAG's type must also
2382 be read-only. */
2383 gcc_assert (TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (tag)));
2385 return tag;
2389 /* Create GLOBAL_VAR, an artificial global variable to act as a
2390 representative of all the variables that may be clobbered by function
2391 calls. */
2393 static void
2394 create_global_var (void)
2396 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2397 void_type_node);
2398 DECL_ARTIFICIAL (global_var) = 1;
2399 TREE_READONLY (global_var) = 0;
2400 DECL_EXTERNAL (global_var) = 1;
2401 TREE_STATIC (global_var) = 1;
2402 TREE_USED (global_var) = 1;
2403 DECL_CONTEXT (global_var) = NULL_TREE;
2404 TREE_THIS_VOLATILE (global_var) = 0;
2405 TREE_ADDRESSABLE (global_var) = 0;
2407 add_referenced_tmp_var (global_var);
2408 mark_sym_for_renaming (global_var);
2412 /* Dump alias statistics on FILE. */
2414 static void
2415 dump_alias_stats (FILE *file)
2417 const char *funcname
2418 = lang_hooks.decl_printable_name (current_function_decl, 2);
2419 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2420 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2421 fprintf (file, "Total alias mayalias results:\t%u\n",
2422 alias_stats.alias_mayalias);
2423 fprintf (file, "Total alias noalias results:\t%u\n",
2424 alias_stats.alias_noalias);
2425 fprintf (file, "Total simple queries:\t%u\n",
2426 alias_stats.simple_queries);
2427 fprintf (file, "Total simple resolved:\t%u\n",
2428 alias_stats.simple_resolved);
2429 fprintf (file, "Total TBAA queries:\t%u\n",
2430 alias_stats.tbaa_queries);
2431 fprintf (file, "Total TBAA resolved:\t%u\n",
2432 alias_stats.tbaa_resolved);
2436 /* Dump alias information on FILE. */
2438 void
2439 dump_alias_info (FILE *file)
2441 size_t i;
2442 const char *funcname
2443 = lang_hooks.decl_printable_name (current_function_decl, 2);
2444 referenced_var_iterator rvi;
2445 tree var;
2447 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2449 fprintf (file, "Aliased symbols\n\n");
2451 FOR_EACH_REFERENCED_VAR (var, rvi)
2453 if (may_be_aliased (var))
2454 dump_variable (file, var);
2457 fprintf (file, "\nDereferenced pointers\n\n");
2459 FOR_EACH_REFERENCED_VAR (var, rvi)
2461 var_ann_t ann = var_ann (var);
2462 if (ann->type_mem_tag)
2463 dump_variable (file, var);
2466 fprintf (file, "\nType memory tags\n\n");
2468 FOR_EACH_REFERENCED_VAR (var, rvi)
2470 var_ann_t ann = var_ann (var);
2471 if (ann->mem_tag_kind == TYPE_TAG)
2472 dump_variable (file, var);
2475 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2477 fprintf (file, "SSA_NAME pointers\n\n");
2478 for (i = 1; i < num_ssa_names; i++)
2480 tree ptr = ssa_name (i);
2481 struct ptr_info_def *pi;
2483 if (ptr == NULL_TREE)
2484 continue;
2486 pi = SSA_NAME_PTR_INFO (ptr);
2487 if (!SSA_NAME_IN_FREE_LIST (ptr)
2488 && pi
2489 && pi->name_mem_tag)
2490 dump_points_to_info_for (file, ptr);
2493 fprintf (file, "\nName memory tags\n\n");
2495 FOR_EACH_REFERENCED_VAR (var, rvi)
2497 var_ann_t ann = var_ann (var);
2498 if (ann->mem_tag_kind == NAME_TAG)
2499 dump_variable (file, var);
2502 fprintf (file, "\n");
2506 /* Dump alias information on stderr. */
2508 void
2509 debug_alias_info (void)
2511 dump_alias_info (stderr);
2515 /* Return the alias information associated with pointer T. It creates a
2516 new instance if none existed. */
2518 struct ptr_info_def *
2519 get_ptr_info (tree t)
2521 struct ptr_info_def *pi;
2523 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2525 pi = SSA_NAME_PTR_INFO (t);
2526 if (pi == NULL)
2528 pi = ggc_alloc (sizeof (*pi));
2529 memset ((void *)pi, 0, sizeof (*pi));
2530 SSA_NAME_PTR_INFO (t) = pi;
2533 return pi;
2537 /* Dump points-to information for SSA_NAME PTR into FILE. */
2539 void
2540 dump_points_to_info_for (FILE *file, tree ptr)
2542 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2544 print_generic_expr (file, ptr, dump_flags);
2546 if (pi)
2548 if (pi->name_mem_tag)
2550 fprintf (file, ", name memory tag: ");
2551 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2554 if (pi->is_dereferenced)
2555 fprintf (file, ", is dereferenced");
2557 if (pi->value_escapes_p)
2558 fprintf (file, ", its value escapes");
2560 if (pi->pt_anything)
2561 fprintf (file, ", points-to anything");
2563 if (pi->pt_malloc)
2564 fprintf (file, ", points-to malloc");
2566 if (pi->pt_null)
2567 fprintf (file, ", points-to NULL");
2569 if (pi->pt_vars)
2571 unsigned ix;
2572 bitmap_iterator bi;
2574 fprintf (file, ", points-to vars: { ");
2575 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2577 print_generic_expr (file, referenced_var (ix), dump_flags);
2578 fprintf (file, " ");
2580 fprintf (file, "}");
2584 fprintf (file, "\n");
2588 /* Dump points-to information for VAR into stderr. */
2590 void
2591 debug_points_to_info_for (tree var)
2593 dump_points_to_info_for (stderr, var);
2597 /* Dump points-to information into FILE. NOTE: This function is slow, as
2598 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2600 void
2601 dump_points_to_info (FILE *file)
2603 basic_block bb;
2604 block_stmt_iterator si;
2605 ssa_op_iter iter;
2606 const char *fname =
2607 lang_hooks.decl_printable_name (current_function_decl, 2);
2608 referenced_var_iterator rvi;
2609 tree var;
2611 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2613 /* First dump points-to information for the default definitions of
2614 pointer variables. This is necessary because default definitions are
2615 not part of the code. */
2616 FOR_EACH_REFERENCED_VAR (var, rvi)
2618 if (POINTER_TYPE_P (TREE_TYPE (var)))
2620 var_ann_t ann = var_ann (var);
2621 if (ann->default_def)
2622 dump_points_to_info_for (file, ann->default_def);
2626 /* Dump points-to information for every pointer defined in the program. */
2627 FOR_EACH_BB (bb)
2629 tree phi;
2631 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2633 tree ptr = PHI_RESULT (phi);
2634 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2635 dump_points_to_info_for (file, ptr);
2638 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2640 tree stmt = bsi_stmt (si);
2641 tree def;
2642 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2643 if (POINTER_TYPE_P (TREE_TYPE (def)))
2644 dump_points_to_info_for (file, def);
2648 fprintf (file, "\n");
2652 /* Dump points-to info pointed by PTO into STDERR. */
2654 void
2655 debug_points_to_info (void)
2657 dump_points_to_info (stderr);
2660 /* Dump to FILE the list of variables that may be aliasing VAR. */
2662 void
2663 dump_may_aliases_for (FILE *file, tree var)
2665 varray_type aliases;
2667 if (TREE_CODE (var) == SSA_NAME)
2668 var = SSA_NAME_VAR (var);
2670 aliases = var_ann (var)->may_aliases;
2671 if (aliases)
2673 size_t i;
2674 fprintf (file, "{ ");
2675 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2677 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2678 fprintf (file, " ");
2680 fprintf (file, "}");
2685 /* Dump to stderr the list of variables that may be aliasing VAR. */
2687 void
2688 debug_may_aliases_for (tree var)
2690 dump_may_aliases_for (stderr, var);
2693 /* Return true if VAR may be aliased. */
2695 bool
2696 may_be_aliased (tree var)
2698 /* Obviously. */
2699 if (TREE_ADDRESSABLE (var))
2700 return true;
2702 /* Globally visible variables can have their addresses taken by other
2703 translation units. */
2704 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2705 return true;
2707 /* Automatic variables can't have their addresses escape any other way.
2708 This must be after the check for global variables, as extern declarations
2709 do not have TREE_STATIC set. */
2710 if (!TREE_STATIC (var))
2711 return false;
2713 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2714 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2715 we can only be sure the variable isn't addressable if it's local to the
2716 current function. */
2717 if (flag_unit_at_a_time)
2718 return false;
2719 if (decl_function_context (var) == current_function_decl)
2720 return false;
2722 return true;
2726 /* Add VAR to the list of may-aliases of PTR's type tag. If PTR
2727 doesn't already have a type tag, create one. */
2729 void
2730 add_type_alias (tree ptr, tree var)
2732 varray_type aliases;
2733 tree tag;
2734 var_ann_t ann = var_ann (ptr);
2735 subvar_t svars;
2738 if (ann->type_mem_tag == NULL_TREE)
2740 tree q = NULL_TREE;
2741 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2742 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2743 referenced_var_iterator rvi;
2745 /* PTR doesn't have a type tag, create a new one and add VAR to
2746 the new tag's alias set.
2748 FIXME, This is slower than necessary. We need to determine
2749 whether there is another pointer Q with the same alias set as
2750 PTR. This could be sped up by having type tags associated
2751 with types. */
2752 FOR_EACH_REFERENCED_VAR (q, rvi)
2754 if (POINTER_TYPE_P (TREE_TYPE (q))
2755 && tag_set == get_alias_set (TREE_TYPE (TREE_TYPE (q))))
2757 /* Found another pointer Q with the same alias set as
2758 the PTR's pointed-to type. If Q has a type tag, use
2759 it. Otherwise, create a new memory tag for PTR. */
2760 var_ann_t ann1 = var_ann (q);
2761 if (ann1->type_mem_tag)
2762 ann->type_mem_tag = ann1->type_mem_tag;
2763 else
2764 ann->type_mem_tag = create_memory_tag (tag_type, true);
2765 goto found_tag;
2769 /* Couldn't find any other pointer with a type tag we could use.
2770 Create a new memory tag for PTR. */
2771 ann->type_mem_tag = create_memory_tag (tag_type, true);
2774 found_tag:
2775 /* If VAR is not already PTR's type tag, add it to the may-alias set
2776 for PTR's type tag. */
2777 gcc_assert (var_ann (var)->type_mem_tag == NOT_A_TAG);
2778 tag = ann->type_mem_tag;
2780 /* If VAR has subvars, add the subvars to the tag instead of the
2781 actual var. */
2782 if (var_can_have_subvars (var)
2783 && (svars = get_subvars_for_var (var)))
2785 subvar_t sv;
2786 for (sv = svars; sv; sv = sv->next)
2787 add_may_alias (tag, sv->var);
2789 else
2790 add_may_alias (tag, var);
2792 /* TAG and its set of aliases need to be marked for renaming. */
2793 mark_sym_for_renaming (tag);
2794 if ((aliases = var_ann (tag)->may_aliases) != NULL)
2796 size_t i;
2797 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2798 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2801 /* If we had grouped aliases, VAR may have aliases of its own. Mark
2802 them for renaming as well. Other statements referencing the
2803 aliases of VAR will need to be updated. */
2804 if ((aliases = var_ann (var)->may_aliases) != NULL)
2806 size_t i;
2807 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2808 mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2813 /* Create a new type tag for PTR. Construct the may-alias list of this type
2814 tag so that it has the aliasing of VAR.
2816 Note, the set of aliases represented by the new type tag are not marked
2817 for renaming. */
2819 void
2820 new_type_alias (tree ptr, tree var)
2822 var_ann_t p_ann = var_ann (ptr);
2823 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2824 var_ann_t v_ann = var_ann (var);
2825 tree tag;
2826 subvar_t svars;
2828 gcc_assert (p_ann->type_mem_tag == NULL_TREE);
2829 gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG);
2831 /* Add VAR to the may-alias set of PTR's new type tag. If VAR has
2832 subvars, add the subvars to the tag instead of the actual var. */
2833 if (var_can_have_subvars (var)
2834 && (svars = get_subvars_for_var (var)))
2836 subvar_t sv;
2838 tag = create_memory_tag (tag_type, true);
2839 p_ann->type_mem_tag = tag;
2841 for (sv = svars; sv; sv = sv->next)
2842 add_may_alias (tag, sv->var);
2844 else
2846 /* The following is based on code in add_stmt_operand to ensure that the
2847 same defs/uses/vdefs/vuses will be found after replacing a reference
2848 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
2849 is the address of var. */
2850 varray_type aliases = v_ann->may_aliases;
2852 if ((aliases != NULL)
2853 && (VARRAY_ACTIVE_SIZE (aliases) == 1))
2855 tree ali = VARRAY_TREE (aliases, 0);
2857 if (get_var_ann (ali)->mem_tag_kind == TYPE_TAG)
2859 p_ann->type_mem_tag = ali;
2860 return;
2864 tag = create_memory_tag (tag_type, true);
2865 p_ann->type_mem_tag = tag;
2867 if (aliases == NULL)
2868 add_may_alias (tag, var);
2869 else
2871 size_t i;
2873 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2874 add_may_alias (tag, VARRAY_TREE (aliases, i));
2881 /* This represents the used range of a variable. */
2883 typedef struct used_part
2885 HOST_WIDE_INT minused;
2886 HOST_WIDE_INT maxused;
2887 /* True if we have an explicit use/def of some portion of this variable,
2888 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2889 bool explicit_uses;
2890 /* True if we have an implicit use/def of some portion of this
2891 variable. Implicit uses occur when we can't tell what part we
2892 are referencing, and have to make conservative assumptions. */
2893 bool implicit_uses;
2894 } *used_part_t;
2896 /* An array of used_part structures, indexed by variable uid. */
2898 static htab_t used_portions;
2900 struct used_part_map
2902 unsigned int uid;
2903 used_part_t to;
2906 /* Return true if the uid in the two used part maps are equal. */
2908 static int
2909 used_part_map_eq (const void *va, const void *vb)
2911 const struct used_part_map *a = va, *b = vb;
2912 return (a->uid == b->uid);
2915 /* Hash a from uid in a used_part_map. */
2917 static unsigned int
2918 used_part_map_hash (const void *item)
2920 return ((const struct used_part_map *)item)->uid;
2923 /* Free a used part map element. */
2925 static void
2926 free_used_part_map (void *item)
2928 free (((struct used_part_map *)item)->to);
2931 /* Lookup a used_part structure for a UID. */
2933 static used_part_t
2934 up_lookup (unsigned int uid)
2936 struct used_part_map *h, in;
2937 in.uid = uid;
2938 h = htab_find_with_hash (used_portions, &in, uid);
2939 if (!h)
2940 return NULL;
2941 return h->to;
2944 /* Insert the pair UID, TO into the used part hashtable. */
2946 static void
2947 up_insert (unsigned int uid, used_part_t to)
2949 struct used_part_map *h;
2950 void **loc;
2952 h = xmalloc (sizeof (struct used_part_map));
2953 h->uid = uid;
2954 h->to = to;
2955 loc = htab_find_slot_with_hash (used_portions, h,
2956 uid, INSERT);
2957 *(struct used_part_map **) loc = h;
2961 /* Given a variable uid, UID, get or create the entry in the used portions
2962 table for the variable. */
2964 static used_part_t
2965 get_or_create_used_part_for (size_t uid)
2967 used_part_t up;
2968 if ((up = up_lookup (uid)) == NULL)
2970 up = xcalloc (1, sizeof (struct used_part));
2971 up->minused = INT_MAX;
2972 up->maxused = 0;
2973 up->explicit_uses = false;
2974 up->implicit_uses = false;
2977 return up;
2981 /* Given an aggregate VAR, create the subvariables that represent its
2982 fields. */
2984 static void
2985 create_overlap_variables_for (tree var)
2987 VEC(fieldoff_s,heap) *fieldstack = NULL;
2988 used_part_t up;
2989 size_t uid = DECL_UID (var);
2991 if (!up_lookup (uid))
2992 return;
2994 up = up_lookup (uid);
2995 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL);
2996 if (VEC_length (fieldoff_s, fieldstack) != 0)
2998 subvar_t *subvars;
2999 fieldoff_s *fo;
3000 bool notokay = false;
3001 int fieldcount = 0;
3002 int i;
3003 HOST_WIDE_INT lastfooffset = -1;
3004 HOST_WIDE_INT lastfosize = -1;
3005 tree lastfotype = NULL_TREE;
3007 /* Not all fields have DECL_SIZE set, and those that don't, we don't
3008 know their size, and thus, can't handle.
3009 The same is true of fields with DECL_SIZE that is not an integer
3010 constant (such as variable sized fields).
3011 Fields with offsets which are not constant will have an offset < 0
3012 We *could* handle fields that are constant sized arrays, but
3013 currently don't. Doing so would require some extra changes to
3014 tree-ssa-operands.c. */
3016 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3018 if (!DECL_SIZE (fo->field)
3019 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
3020 || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
3021 || fo->offset < 0)
3023 notokay = true;
3024 break;
3026 fieldcount++;
3029 /* The current heuristic we use is as follows:
3030 If the variable has no used portions in this function, no
3031 structure vars are created for it.
3032 Otherwise,
3033 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
3034 we always create structure vars for them.
3035 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3036 some explicit uses, we create structure vars for them.
3037 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
3038 no explicit uses, we do not create structure vars for them.
3041 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
3042 && !up->explicit_uses)
3044 if (dump_file && (dump_flags & TDF_DETAILS))
3046 fprintf (dump_file, "Variable ");
3047 print_generic_expr (dump_file, var, 0);
3048 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
3050 notokay = true;
3053 /* Bail out, if we can't create overlap variables. */
3054 if (notokay)
3056 VEC_free (fieldoff_s, heap, fieldstack);
3057 return;
3060 /* Otherwise, create the variables. */
3061 subvars = lookup_subvars_for_var (var);
3063 sort_fieldstack (fieldstack);
3065 for (i = VEC_length (fieldoff_s, fieldstack);
3066 VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
3068 subvar_t sv;
3069 HOST_WIDE_INT fosize;
3070 var_ann_t ann;
3071 tree currfotype;
3073 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
3074 currfotype = TREE_TYPE (fo->field);
3076 /* If this field isn't in the used portion,
3077 or it has the exact same offset and size as the last
3078 field, skip it. */
3080 if (((fo->offset <= up->minused
3081 && fo->offset + fosize <= up->minused)
3082 || fo->offset >= up->maxused)
3083 || (fo->offset == lastfooffset
3084 && fosize == lastfosize
3085 && currfotype == lastfotype))
3086 continue;
3087 sv = ggc_alloc (sizeof (struct subvar));
3088 sv->offset = fo->offset;
3089 sv->size = fosize;
3090 sv->next = *subvars;
3091 sv->var = create_tmp_var_raw (TREE_TYPE (fo->field), "SFT");
3092 if (dump_file)
3094 fprintf (dump_file, "structure field tag %s created for var %s",
3095 get_name (sv->var), get_name (var));
3096 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
3097 sv->offset);
3098 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
3099 sv->size);
3100 fprintf (dump_file, "\n");
3103 /* We need to copy the various flags from var to sv->var, so that
3104 they are is_global_var iff the original variable was. */
3106 DECL_EXTERNAL (sv->var) = DECL_EXTERNAL (var);
3107 TREE_PUBLIC (sv->var) = TREE_PUBLIC (var);
3108 TREE_STATIC (sv->var) = TREE_STATIC (var);
3109 TREE_READONLY (sv->var) = TREE_READONLY (var);
3111 /* Like other memory tags, these need to be marked addressable to
3112 keep is_gimple_reg from thinking they are real. */
3113 TREE_ADDRESSABLE (sv->var) = 1;
3115 DECL_CONTEXT (sv->var) = DECL_CONTEXT (var);
3117 ann = get_var_ann (sv->var);
3118 ann->mem_tag_kind = STRUCT_FIELD;
3119 ann->type_mem_tag = NULL;
3120 add_referenced_tmp_var (sv->var);
3122 lastfotype = currfotype;
3123 lastfooffset = fo->offset;
3124 lastfosize = fosize;
3125 *subvars = sv;
3128 /* Once we have created subvars, the original is no longer call
3129 clobbered on its own. Its call clobbered status depends
3130 completely on the call clobbered status of the subvars.
3132 add_referenced_var in the above loop will take care of
3133 marking subvars of global variables as call clobbered for us
3134 to start, since they are global as well. */
3135 clear_call_clobbered (var);
3138 VEC_free (fieldoff_s, heap, fieldstack);
3142 /* Find the conservative answer to the question of what portions of what
3143 structures are used by this statement. We assume that if we have a
3144 component ref with a known size + offset, that we only need that part
3145 of the structure. For unknown cases, or cases where we do something
3146 to the whole structure, we assume we need to create fields for the
3147 entire structure. */
3149 static tree
3150 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3152 switch (TREE_CODE (*tp))
3154 case COMPONENT_REF:
3156 HOST_WIDE_INT bitsize;
3157 HOST_WIDE_INT bitpos;
3158 tree offset;
3159 enum machine_mode mode;
3160 int unsignedp;
3161 int volatilep;
3162 tree ref;
3163 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
3164 &unsignedp, &volatilep, false);
3165 if (DECL_P (ref) && offset == NULL && bitsize != -1)
3167 size_t uid = DECL_UID (ref);
3168 used_part_t up;
3170 up = get_or_create_used_part_for (uid);
3172 if (bitpos <= up->minused)
3173 up->minused = bitpos;
3174 if ((bitpos + bitsize >= up->maxused))
3175 up->maxused = bitpos + bitsize;
3177 up->explicit_uses = true;
3178 up_insert (uid, up);
3180 *walk_subtrees = 0;
3181 return NULL_TREE;
3183 else if (DECL_P (ref))
3185 if (DECL_SIZE (ref)
3186 && var_can_have_subvars (ref)
3187 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
3189 used_part_t up;
3190 size_t uid = DECL_UID (ref);
3192 up = get_or_create_used_part_for (uid);
3194 up->minused = 0;
3195 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
3197 up->implicit_uses = true;
3199 up_insert (uid, up);
3201 *walk_subtrees = 0;
3202 return NULL_TREE;
3206 break;
3207 /* This is here to make sure we mark the entire base variable as used
3208 when you take its address. Because our used portion analysis is
3209 simple, we aren't looking at casts or pointer arithmetic to see what
3210 happens when you take the address. */
3211 case ADDR_EXPR:
3213 tree var = get_base_address (TREE_OPERAND (*tp, 0));
3215 if (var
3216 && DECL_P (var)
3217 && DECL_SIZE (var)
3218 && var_can_have_subvars (var)
3219 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3221 used_part_t up;
3222 size_t uid = DECL_UID (var);
3224 up = get_or_create_used_part_for (uid);
3226 up->minused = 0;
3227 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3228 up->implicit_uses = true;
3230 up_insert (uid, up);
3231 *walk_subtrees = 0;
3232 return NULL_TREE;
3235 break;
3236 case VAR_DECL:
3237 case PARM_DECL:
3239 tree var = *tp;
3240 if (DECL_SIZE (var)
3241 && var_can_have_subvars (var)
3242 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3244 used_part_t up;
3245 size_t uid = DECL_UID (var);
3247 up = get_or_create_used_part_for (uid);
3249 up->minused = 0;
3250 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3251 up->implicit_uses = true;
3253 up_insert (uid, up);
3254 *walk_subtrees = 0;
3255 return NULL_TREE;
3258 break;
3260 default:
3261 break;
3264 return NULL_TREE;
3267 /* Create structure field variables for structures used in this function. */
3269 static void
3270 create_structure_vars (void)
3272 basic_block bb;
3273 referenced_var_iterator rvi;
3274 tree var;
3276 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
3277 free_used_part_map);
3279 FOR_EACH_BB (bb)
3281 block_stmt_iterator bsi;
3282 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3284 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3285 find_used_portions,
3286 NULL);
3289 FOR_EACH_REFERENCED_VAR (var, rvi)
3291 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3292 if (var
3293 && DECL_SIZE (var)
3294 && var_can_have_subvars (var)
3295 && var_ann (var)->mem_tag_kind == NOT_A_TAG
3296 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3297 create_overlap_variables_for (var);
3299 htab_delete (used_portions);
3303 static bool
3304 gate_structure_vars (void)
3306 return flag_tree_salias != 0;
3309 struct tree_opt_pass pass_create_structure_vars =
3311 "salias", /* name */
3312 gate_structure_vars, /* gate */
3313 create_structure_vars, /* execute */
3314 NULL, /* sub */
3315 NULL, /* next */
3316 0, /* static_pass_number */
3317 0, /* tv_id */
3318 PROP_cfg, /* properties_required */
3319 0, /* properties_provided */
3320 0, /* properties_destroyed */
3321 0, /* todo_flags_start */
3322 TODO_dump_func, /* todo_flags_finish */
3323 0 /* letter */