PR target/17245
[official-gcc.git] / gcc / tree-ssa-alias.c
blob8e1b63265f8505b73bf7f5eef4eef72565a20e74
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "expr.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "tree-gimple.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "convert.h"
44 #include "params.h"
45 #include "vec.h"
47 /* 'true' after aliases have been computed (see compute_may_aliases). */
48 bool aliases_computed_p;
50 /* Structure to map a variable to its alias set and keep track of the
51 virtual operands that will be needed to represent it. */
52 struct alias_map_d
54 /* Variable and its alias set. */
55 tree var;
56 HOST_WIDE_INT set;
58 /* Total number of virtual operands that will be needed to represent
59 all the aliases of VAR. */
60 long total_alias_vops;
62 /* Nonzero if the aliases for this memory tag have been grouped
63 already. Used in group_aliases. */
64 unsigned int grouped_p : 1;
66 /* Set of variables aliased with VAR. This is the exact same
67 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
68 bitmap form to speed up alias grouping. */
69 sbitmap may_aliases;
73 /* Alias information used by compute_may_aliases and its helpers. */
74 struct alias_info
76 /* SSA names visited while collecting points-to information. If bit I
77 is set, it means that SSA variable with version I has already been
78 visited. */
79 sbitmap ssa_names_visited;
81 /* Array of SSA_NAME pointers processed by the points-to collector. */
82 varray_type processed_ptrs;
84 /* Variables whose address is still needed. */
85 bitmap addresses_needed;
87 /* ADDRESSABLE_VARS contains all the global variables and locals that
88 have had their address taken. */
89 struct alias_map_d **addressable_vars;
90 size_t num_addressable_vars;
92 /* POINTERS contains all the _DECL pointers with unique memory tags
93 that have been referenced in the program. */
94 struct alias_map_d **pointers;
95 size_t num_pointers;
97 /* Number of function calls found in the program. */
98 size_t num_calls_found;
100 /* Number of const/pure function calls found in the program. */
101 size_t num_pure_const_calls_found;
103 /* Array of counters to keep track of how many times each pointer has
104 been dereferenced in the program. This is used by the alias grouping
105 heuristic in compute_flow_insensitive_aliasing. */
106 varray_type num_references;
108 /* Total number of virtual operands that will be needed to represent
109 all the aliases of all the pointers found in the program. */
110 long total_alias_vops;
112 /* Variables that have been written to. */
113 bitmap written_vars;
115 /* Pointers that have been used in an indirect store operation. */
116 bitmap dereferenced_ptrs_store;
118 /* Pointers that have been used in an indirect load operation. */
119 bitmap dereferenced_ptrs_load;
123 /* Counters used to display statistics on alias analysis. */
124 struct alias_stats_d
126 unsigned int alias_queries;
127 unsigned int alias_mayalias;
128 unsigned int alias_noalias;
129 unsigned int simple_queries;
130 unsigned int simple_resolved;
131 unsigned int tbaa_queries;
132 unsigned int tbaa_resolved;
136 /* Local variables. */
137 static struct alias_stats_d alias_stats;
139 /* Local functions. */
140 static void compute_flow_insensitive_aliasing (struct alias_info *);
141 static void dump_alias_stats (FILE *);
142 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT);
143 static tree create_memory_tag (tree type, bool is_type_tag);
144 static tree get_tmt_for (tree, struct alias_info *);
145 static tree get_nmt_for (tree);
146 static void add_may_alias (tree, tree);
147 static void replace_may_alias (tree, size_t, tree);
148 static struct alias_info *init_alias_info (void);
149 static void delete_alias_info (struct alias_info *);
150 static void compute_points_to_and_addr_escape (struct alias_info *);
151 static void compute_flow_sensitive_aliasing (struct alias_info *);
152 static void setup_pointers_and_addressables (struct alias_info *);
153 static bool collect_points_to_info_r (tree, tree, void *);
154 static bool is_escape_site (tree, struct alias_info *);
155 static void add_pointed_to_var (struct alias_info *, tree, tree);
156 static void create_global_var (void);
157 static void collect_points_to_info_for (struct alias_info *, tree);
158 static void maybe_create_global_var (struct alias_info *ai);
159 static void group_aliases (struct alias_info *);
160 static void set_pt_anything (tree ptr);
161 static void set_pt_malloc (tree ptr);
163 /* Global declarations. */
165 /* Call clobbered variables in the function. If bit I is set, then
166 REFERENCED_VARS (I) is call-clobbered. */
167 bitmap call_clobbered_vars;
169 /* Addressable variables in the function. If bit I is set, then
170 REFERENCED_VARS (I) has had its address taken. Note that
171 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
172 addressable variable is not necessarily call-clobbered (e.g., a
173 local addressable whose address does not escape) and not all
174 call-clobbered variables are addressable (e.g., a local static
175 variable). */
176 bitmap addressable_vars;
178 /* When the program has too many call-clobbered variables and call-sites,
179 this variable is used to represent the clobbering effects of function
180 calls. In these cases, all the call clobbered variables in the program
181 are forced to alias this variable. This reduces compile times by not
182 having to keep track of too many V_MAY_DEF expressions at call sites. */
183 tree global_var;
186 /* Compute may-alias information for every variable referenced in function
187 FNDECL.
189 Alias analysis proceeds in 3 main phases:
191 1- Points-to and escape analysis.
193 This phase walks the use-def chains in the SSA web looking for three
194 things:
196 * Assignments of the form P_i = &VAR
197 * Assignments of the form P_i = malloc()
198 * Pointers and ADDR_EXPR that escape the current function.
200 The concept of 'escaping' is the same one used in the Java world. When
201 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
202 outside of the current function. So, assignment to global variables,
203 function arguments and returning a pointer are all escape sites, as are
204 conversions between pointers and integers.
206 This is where we are currently limited. Since not everything is renamed
207 into SSA, we lose track of escape properties when a pointer is stashed
208 inside a field in a structure, for instance. In those cases, we are
209 assuming that the pointer does escape.
211 We use escape analysis to determine whether a variable is
212 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
213 is call-clobbered. If a pointer P_i escapes, then all the variables
214 pointed-to by P_i (and its memory tag) also escape.
216 2- Compute flow-sensitive aliases
218 We have two classes of memory tags. Memory tags associated with the
219 pointed-to data type of the pointers in the program. These tags are
220 called "type memory tag" (TMT). The other class are those associated
221 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
222 when adding operands for an INDIRECT_REF *P_i, we will first check
223 whether P_i has a name tag, if it does we use it, because that will have
224 more precise aliasing information. Otherwise, we use the standard type
225 tag.
227 In this phase, we go through all the pointers we found in points-to
228 analysis and create alias sets for the name memory tags associated with
229 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
230 it points to and its tag.
233 3- Compute flow-insensitive aliases
235 This pass will compare the alias set of every type memory tag and every
236 addressable variable found in the program. Given a type memory tag TMT
237 and an addressable variable V. If the alias sets of TMT and V conflict
238 (as computed by may_alias_p), then V is marked as an alias tag and added
239 to the alias set of TMT.
241 For instance, consider the following function:
243 foo (int i)
245 int *p, a, b;
247 if (i > 10)
248 p = &a;
249 else
250 p = &b;
252 *p = 3;
253 a = b + 2;
254 return *p;
257 After aliasing analysis has finished, the type memory tag for pointer
258 'p' will have two aliases, namely variables 'a' and 'b'. Every time
259 pointer 'p' is dereferenced, we want to mark the operation as a
260 potential reference to 'a' and 'b'.
262 foo (int i)
264 int *p, a, b;
266 if (i_2 > 10)
267 p_4 = &a;
268 else
269 p_6 = &b;
270 # p_1 = PHI <p_4(1), p_6(2)>;
272 # a_7 = V_MAY_DEF <a_3>;
273 # b_8 = V_MAY_DEF <b_5>;
274 *p_1 = 3;
276 # a_9 = V_MAY_DEF <a_7>
277 # VUSE <b_8>
278 a_9 = b_8 + 2;
280 # VUSE <a_9>;
281 # VUSE <b_8>;
282 return *p_1;
285 In certain cases, the list of may aliases for a pointer may grow too
286 large. This may cause an explosion in the number of virtual operands
287 inserted in the code. Resulting in increased memory consumption and
288 compilation time.
290 When the number of virtual operands needed to represent aliased
291 loads and stores grows too large (configurable with @option{--param
292 max-aliased-vops}), alias sets are grouped to avoid severe
293 compile-time slow downs and memory consumption. See group_aliases. */
295 static void
296 compute_may_aliases (void)
298 struct alias_info *ai;
300 memset (&alias_stats, 0, sizeof (alias_stats));
302 /* Initialize aliasing information. */
303 ai = init_alias_info ();
305 /* For each pointer P_i, determine the sets of variables that P_i may
306 point-to. For every addressable variable V, determine whether the
307 address of V escapes the current function, making V call-clobbered
308 (i.e., whether &V is stored in a global variable or if its passed as a
309 function call argument). */
310 compute_points_to_and_addr_escape (ai);
312 /* Collect all pointers and addressable variables, compute alias sets,
313 create memory tags for pointers and promote variables whose address is
314 not needed anymore. */
315 setup_pointers_and_addressables (ai);
317 /* Compute flow-sensitive, points-to based aliasing for all the name
318 memory tags. Note that this pass needs to be done before flow
319 insensitive analysis because it uses the points-to information
320 gathered before to mark call-clobbered type tags. */
321 compute_flow_sensitive_aliasing (ai);
323 /* Compute type-based flow-insensitive aliasing for all the type
324 memory tags. */
325 compute_flow_insensitive_aliasing (ai);
327 /* If the program has too many call-clobbered variables and/or function
328 calls, create .GLOBAL_VAR and use it to model call-clobbering
329 semantics at call sites. This reduces the number of virtual operands
330 considerably, improving compile times at the expense of lost
331 aliasing precision. */
332 maybe_create_global_var (ai);
334 /* Debugging dumps. */
335 if (dump_file)
337 dump_referenced_vars (dump_file);
338 if (dump_flags & TDF_STATS)
339 dump_alias_stats (dump_file);
340 dump_points_to_info (dump_file);
341 dump_alias_info (dump_file);
344 /* Deallocate memory used by aliasing data structures. */
345 delete_alias_info (ai);
348 block_stmt_iterator bsi;
349 basic_block bb;
350 FOR_EACH_BB (bb)
352 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
354 update_stmt_if_modified (bsi_stmt (bsi));
361 struct tree_opt_pass pass_may_alias =
363 "alias", /* name */
364 NULL, /* gate */
365 compute_may_aliases, /* execute */
366 NULL, /* sub */
367 NULL, /* next */
368 0, /* static_pass_number */
369 TV_TREE_MAY_ALIAS, /* tv_id */
370 PROP_cfg | PROP_ssa, /* properties_required */
371 PROP_alias, /* properties_provided */
372 0, /* properties_destroyed */
373 0, /* todo_flags_start */
374 TODO_dump_func | TODO_rename_vars
375 | TODO_ggc_collect | TODO_verify_ssa
376 | TODO_verify_stmts, /* todo_flags_finish */
377 0 /* letter */
381 /* Data structure used to count the number of dereferences to PTR
382 inside an expression. */
383 struct count_ptr_d
385 tree ptr;
386 unsigned count;
390 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
391 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
393 static tree
394 count_ptr_derefs (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
396 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
398 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
399 count_p->count++;
401 return NULL_TREE;
405 /* Count the number of direct and indirect uses for pointer PTR in
406 statement STMT. The two counts are stored in *NUM_USES_P and
407 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
408 least one of those dereferences is a store operation. */
410 static void
411 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
412 unsigned *num_derefs_p, bool *is_store)
414 ssa_op_iter i;
415 tree use;
417 *num_uses_p = 0;
418 *num_derefs_p = 0;
419 *is_store = false;
421 /* Find out the total number of uses of PTR in STMT. */
422 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
423 if (use == ptr)
424 (*num_uses_p)++;
426 /* Now count the number of indirect references to PTR. This is
427 truly awful, but we don't have much choice. There are no parent
428 pointers inside INDIRECT_REFs, so an expression like
429 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
430 find all the indirect and direct uses of x_1 inside. The only
431 shortcut we can take is the fact that GIMPLE only allows
432 INDIRECT_REFs inside the expressions below. */
433 if (TREE_CODE (stmt) == MODIFY_EXPR
434 || (TREE_CODE (stmt) == RETURN_EXPR
435 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
436 || TREE_CODE (stmt) == ASM_EXPR
437 || TREE_CODE (stmt) == CALL_EXPR)
439 tree lhs, rhs;
441 if (TREE_CODE (stmt) == MODIFY_EXPR)
443 lhs = TREE_OPERAND (stmt, 0);
444 rhs = TREE_OPERAND (stmt, 1);
446 else if (TREE_CODE (stmt) == RETURN_EXPR)
448 tree e = TREE_OPERAND (stmt, 0);
449 lhs = TREE_OPERAND (e, 0);
450 rhs = TREE_OPERAND (e, 1);
452 else if (TREE_CODE (stmt) == ASM_EXPR)
454 lhs = ASM_OUTPUTS (stmt);
455 rhs = ASM_INPUTS (stmt);
457 else
459 lhs = NULL_TREE;
460 rhs = stmt;
463 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
465 struct count_ptr_d count;
466 count.ptr = ptr;
467 count.count = 0;
468 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
469 *is_store = true;
470 *num_derefs_p = count.count;
473 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
475 struct count_ptr_d count;
476 count.ptr = ptr;
477 count.count = 0;
478 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
479 *num_derefs_p += count.count;
483 gcc_assert (*num_uses_p >= *num_derefs_p);
487 /* Initialize the data structures used for alias analysis. */
489 static struct alias_info *
490 init_alias_info (void)
492 struct alias_info *ai;
494 ai = xcalloc (1, sizeof (struct alias_info));
495 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
496 sbitmap_zero (ai->ssa_names_visited);
497 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
498 ai->addresses_needed = BITMAP_ALLOC (NULL);
499 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
500 ai->written_vars = BITMAP_ALLOC (NULL);
501 ai->dereferenced_ptrs_store = BITMAP_ALLOC (NULL);
502 ai->dereferenced_ptrs_load = BITMAP_ALLOC (NULL);
504 /* If aliases have been computed before, clear existing information. */
505 if (aliases_computed_p)
507 unsigned i;
508 basic_block bb;
510 /* Make sure that every statement has a valid set of operands.
511 If a statement needs to be scanned for operands while we
512 compute aliases, it may get erroneous operands because all
513 the alias relations are not built at that point.
514 FIXME: This code will become obsolete when operands are not
515 lazily updated. */
516 FOR_EACH_BB (bb)
518 block_stmt_iterator si;
519 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
520 get_stmt_operands (bsi_stmt (si));
523 /* Similarly, clear the set of addressable variables. In this
524 case, we can just clear the set because addressability is
525 only computed here. */
526 bitmap_clear (addressable_vars);
528 /* Clear flow-insensitive alias information from each symbol. */
529 for (i = 0; i < num_referenced_vars; i++)
531 tree var = referenced_var (i);
532 var_ann_t ann = var_ann (var);
534 ann->is_alias_tag = 0;
535 ann->may_aliases = NULL;
537 /* Since we are about to re-discover call-clobbered
538 variables, clear the call-clobbered flag. Variables that
539 are intrinsically call-clobbered (globals, local statics,
540 etc) will not be marked by the aliasing code, so we can't
541 remove them from CALL_CLOBBERED_VARS.
543 NB: STRUCT_FIELDS are still call clobbered if they are for
544 a global variable, so we *don't* clear their call clobberedness
545 just because they are tags, though we will clear it if they
546 aren't for global variables. */
547 if (ann->mem_tag_kind == NAME_TAG
548 || ann->mem_tag_kind == TYPE_TAG
549 || !is_global_var (var))
550 clear_call_clobbered (var);
553 /* Clear flow-sensitive points-to information from each SSA name. */
554 for (i = 1; i < num_ssa_names; i++)
556 tree name = ssa_name (i);
558 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
559 continue;
561 if (SSA_NAME_PTR_INFO (name))
563 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
565 /* Clear all the flags but keep the name tag to
566 avoid creating new temporaries unnecessarily. If
567 this pointer is found to point to a subset or
568 superset of its former points-to set, then a new
569 tag will need to be created in create_name_tags. */
570 pi->pt_anything = 0;
571 pi->pt_malloc = 0;
572 pi->pt_null = 0;
573 pi->value_escapes_p = 0;
574 pi->is_dereferenced = 0;
575 if (pi->pt_vars)
576 bitmap_clear (pi->pt_vars);
581 /* Next time, we will need to reset alias information. */
582 aliases_computed_p = true;
584 return ai;
588 /* Deallocate memory used by alias analysis. */
590 static void
591 delete_alias_info (struct alias_info *ai)
593 size_t i;
595 sbitmap_free (ai->ssa_names_visited);
596 ai->processed_ptrs = NULL;
597 BITMAP_FREE (ai->addresses_needed);
599 for (i = 0; i < ai->num_addressable_vars; i++)
601 sbitmap_free (ai->addressable_vars[i]->may_aliases);
602 free (ai->addressable_vars[i]);
604 free (ai->addressable_vars);
606 for (i = 0; i < ai->num_pointers; i++)
608 sbitmap_free (ai->pointers[i]->may_aliases);
609 free (ai->pointers[i]);
611 free (ai->pointers);
613 ai->num_references = NULL;
614 BITMAP_FREE (ai->written_vars);
615 BITMAP_FREE (ai->dereferenced_ptrs_store);
616 BITMAP_FREE (ai->dereferenced_ptrs_load);
618 free (ai);
622 /* Walk use-def chains for pointer PTR to determine what variables is PTR
623 pointing to. */
625 static void
626 collect_points_to_info_for (struct alias_info *ai, tree ptr)
628 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
630 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
632 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
633 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
634 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
639 /* Traverse use-def links for all the pointers in the program to collect
640 address escape and points-to information.
642 This is loosely based on the same idea described in R. Hasti and S.
643 Horwitz, ``Using static single assignment form to improve
644 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
645 Programming Language Design and Implementation, pp. 97-105, 1998. */
647 static void
648 compute_points_to_and_addr_escape (struct alias_info *ai)
650 basic_block bb;
651 unsigned i;
652 tree op;
653 ssa_op_iter iter;
655 timevar_push (TV_TREE_PTA);
657 FOR_EACH_BB (bb)
659 bb_ann_t block_ann = bb_ann (bb);
660 block_stmt_iterator si;
662 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
664 bitmap addr_taken;
665 tree stmt = bsi_stmt (si);
666 bool stmt_escapes_p = is_escape_site (stmt, ai);
667 bitmap_iterator bi;
669 /* Mark all the variables whose address are taken by the
670 statement. Note that this will miss all the addresses taken
671 in PHI nodes (those are discovered while following the use-def
672 chains). */
673 get_stmt_operands (stmt);
674 addr_taken = addresses_taken (stmt);
675 if (addr_taken)
676 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
678 tree var = referenced_var (i);
679 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
680 if (stmt_escapes_p)
681 mark_call_clobbered (var);
684 if (stmt_escapes_p)
685 block_ann->has_escape_site = 1;
687 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
689 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
690 struct ptr_info_def *pi;
691 bool is_store;
692 unsigned num_uses, num_derefs;
694 /* If the operand's variable may be aliased, keep track
695 of how many times we've referenced it. This is used
696 for alias grouping in compute_flow_sensitive_aliasing.
697 Note that we don't need to grow AI->NUM_REFERENCES
698 because we are processing regular variables, not
699 memory tags (the array's initial size is set to
700 NUM_REFERENCED_VARS). */
701 if (may_be_aliased (SSA_NAME_VAR (op)))
702 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
704 if (!POINTER_TYPE_P (TREE_TYPE (op)))
705 continue;
707 collect_points_to_info_for (ai, op);
709 pi = SSA_NAME_PTR_INFO (op);
710 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs,
711 &is_store);
713 if (num_derefs > 0)
715 /* Mark OP as dereferenced. In a subsequent pass,
716 dereferenced pointers that point to a set of
717 variables will be assigned a name tag to alias
718 all the variables OP points to. */
719 pi->is_dereferenced = 1;
721 /* Keep track of how many time we've dereferenced each
722 pointer. Again, we don't need to grow
723 AI->NUM_REFERENCES because we're processing
724 existing program variables. */
725 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
727 /* If this is a store operation, mark OP as being
728 dereferenced to store, otherwise mark it as being
729 dereferenced to load. */
730 if (is_store)
731 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
732 else
733 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
736 if (stmt_escapes_p && num_derefs < num_uses)
738 /* If STMT is an escape point and STMT contains at
739 least one direct use of OP, then the value of OP
740 escapes and so the pointed-to variables need to
741 be marked call-clobbered. */
742 pi->value_escapes_p = 1;
744 /* If the statement makes a function call, assume
745 that pointer OP will be dereferenced in a store
746 operation inside the called function. */
747 if (get_call_expr_in (stmt))
749 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
750 pi->is_dereferenced = 1;
755 /* Update reference counter for definitions to any
756 potentially aliased variable. This is used in the alias
757 grouping heuristics. */
758 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
760 tree var = SSA_NAME_VAR (op);
761 var_ann_t ann = var_ann (var);
762 bitmap_set_bit (ai->written_vars, ann->uid);
763 if (may_be_aliased (var))
764 (VARRAY_UINT (ai->num_references, ann->uid))++;
766 if (POINTER_TYPE_P (TREE_TYPE (op)))
767 collect_points_to_info_for (ai, op);
770 /* Mark variables in V_MAY_DEF operands as being written to. */
771 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
773 tree var = SSA_NAME_VAR (op);
774 var_ann_t ann = var_ann (var);
775 bitmap_set_bit (ai->written_vars, ann->uid);
778 /* After promoting variables and computing aliasing we will
779 need to re-scan most statements. FIXME: Try to minimize the
780 number of statements re-scanned. It's not really necessary to
781 re-scan *all* statements. */
782 mark_stmt_modified (stmt);
786 timevar_pop (TV_TREE_PTA);
790 /* Create name tags for all the pointers that have been dereferenced.
791 We only create a name tag for a pointer P if P is found to point to
792 a set of variables (so that we can alias them to *P) or if it is
793 the result of a call to malloc (which means that P cannot point to
794 anything else nor alias any other variable).
796 If two pointers P and Q point to the same set of variables, they
797 are assigned the same name tag. */
799 static void
800 create_name_tags (struct alias_info *ai)
802 size_t i;
804 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
806 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
807 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
809 if (pi->pt_anything || !pi->is_dereferenced)
811 /* No name tags for pointers that have not been
812 dereferenced or point to an arbitrary location. */
813 pi->name_mem_tag = NULL_TREE;
814 continue;
817 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
819 size_t j;
820 tree old_name_tag = pi->name_mem_tag;
822 /* If PTR points to a set of variables, check if we don't
823 have another pointer Q with the same points-to set before
824 creating a tag. If so, use Q's tag instead of creating a
825 new one.
827 This is important for not creating unnecessary symbols
828 and also for copy propagation. If we ever need to
829 propagate PTR into Q or vice-versa, we would run into
830 problems if they both had different name tags because
831 they would have different SSA version numbers (which
832 would force us to take the name tags in and out of SSA). */
833 for (j = 0; j < i; j++)
835 tree q = VARRAY_TREE (ai->processed_ptrs, j);
836 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
838 if (qi
839 && qi->pt_vars
840 && qi->name_mem_tag
841 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
843 pi->name_mem_tag = qi->name_mem_tag;
844 break;
848 /* If we didn't find a pointer with the same points-to set
849 as PTR, create a new name tag if needed. */
850 if (pi->name_mem_tag == NULL_TREE)
851 pi->name_mem_tag = get_nmt_for (ptr);
853 /* If the new name tag computed for PTR is different than
854 the old name tag that it used to have, then the old tag
855 needs to be removed from the IL, so we mark it for
856 renaming. */
857 if (old_name_tag && old_name_tag != pi->name_mem_tag)
858 bitmap_set_bit (vars_to_rename, var_ann (old_name_tag)->uid);
860 else if (pi->pt_malloc)
862 /* Otherwise, create a unique name tag for this pointer. */
863 pi->name_mem_tag = get_nmt_for (ptr);
865 else
867 /* Only pointers that may point to malloc or other variables
868 may receive a name tag. If the pointer does not point to
869 a known spot, we should use type tags. */
870 set_pt_anything (ptr);
871 continue;
874 TREE_THIS_VOLATILE (pi->name_mem_tag)
875 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
877 /* Mark the new name tag for renaming. */
878 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
884 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
885 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
886 name tag and the variables it points-to are call-clobbered. Finally, if
887 P_i escapes and we could not determine where it points to, then all the
888 variables in the same alias set as *P_i are marked call-clobbered. This
889 is necessary because we must assume that P_i may take the address of any
890 variable in the same alias set. */
892 static void
893 compute_flow_sensitive_aliasing (struct alias_info *ai)
895 size_t i;
897 create_name_tags (ai);
899 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
901 unsigned j;
902 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
903 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
904 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
905 bitmap_iterator bi;
907 if (pi->value_escapes_p || pi->pt_anything)
909 /* If PTR escapes or may point to anything, then its associated
910 memory tags and pointed-to variables are call-clobbered. */
911 if (pi->name_mem_tag)
912 mark_call_clobbered (pi->name_mem_tag);
914 if (v_ann->type_mem_tag)
915 mark_call_clobbered (v_ann->type_mem_tag);
917 if (pi->pt_vars)
918 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
920 mark_call_clobbered (referenced_var (j));
924 /* Set up aliasing information for PTR's name memory tag (if it has
925 one). Note that only pointers that have been dereferenced will
926 have a name memory tag. */
927 if (pi->name_mem_tag && pi->pt_vars)
928 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
930 add_may_alias (pi->name_mem_tag, referenced_var (j));
931 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
934 /* If the name tag is call clobbered, so is the type tag
935 associated with the base VAR_DECL. */
936 if (pi->name_mem_tag
937 && v_ann->type_mem_tag
938 && is_call_clobbered (pi->name_mem_tag))
939 mark_call_clobbered (v_ann->type_mem_tag);
944 /* Compute type-based alias sets. Traverse all the pointers and
945 addressable variables found in setup_pointers_and_addressables.
947 For every pointer P in AI->POINTERS and addressable variable V in
948 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
949 memory tag (TMT) if their alias sets conflict. V is then marked as
950 an alias tag so that the operand scanner knows that statements
951 containing V have aliased operands. */
953 static void
954 compute_flow_insensitive_aliasing (struct alias_info *ai)
956 size_t i;
958 /* Initialize counter for the total number of virtual operands that
959 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
960 threshold set by --params max-alias-vops, we enable alias
961 grouping. */
962 ai->total_alias_vops = 0;
964 /* For every pointer P, determine which addressable variables may alias
965 with P's type memory tag. */
966 for (i = 0; i < ai->num_pointers; i++)
968 size_t j;
969 struct alias_map_d *p_map = ai->pointers[i];
970 tree tag = var_ann (p_map->var)->type_mem_tag;
971 var_ann_t tag_ann = var_ann (tag);
973 p_map->total_alias_vops = 0;
974 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
975 sbitmap_zero (p_map->may_aliases);
977 for (j = 0; j < ai->num_addressable_vars; j++)
979 struct alias_map_d *v_map;
980 var_ann_t v_ann;
981 tree var;
982 bool tag_stored_p, var_stored_p;
984 v_map = ai->addressable_vars[j];
985 var = v_map->var;
986 v_ann = var_ann (var);
988 /* Skip memory tags and variables that have never been
989 written to. We also need to check if the variables are
990 call-clobbered because they may be overwritten by
991 function calls.
993 Note this is effectively random accessing elements in
994 the sparse bitset, which can be highly inefficient.
995 So we first check the call_clobbered status of the
996 tag and variable before querying the bitmap. */
997 tag_stored_p = is_call_clobbered (tag)
998 || bitmap_bit_p (ai->written_vars, tag_ann->uid);
999 var_stored_p = is_call_clobbered (var)
1000 || bitmap_bit_p (ai->written_vars, v_ann->uid);
1001 if (!tag_stored_p && !var_stored_p)
1002 continue;
1004 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
1006 subvar_t svars;
1007 size_t num_tag_refs, num_var_refs;
1009 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1010 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
1012 /* Add VAR to TAG's may-aliases set. */
1014 /* If this is an aggregate, we may have subvariables for it
1015 that need to be pointed to. */
1016 if (var_can_have_subvars (var)
1017 && (svars = get_subvars_for_var (var)))
1019 subvar_t sv;
1021 for (sv = svars; sv; sv = sv->next)
1023 add_may_alias (tag, sv->var);
1024 /* Update the bitmap used to represent TAG's alias set
1025 in case we need to group aliases. */
1026 SET_BIT (p_map->may_aliases, var_ann (sv->var)->uid);
1029 else
1031 add_may_alias (tag, var);
1032 /* Update the bitmap used to represent TAG's alias set
1033 in case we need to group aliases. */
1034 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
1037 /* Update the total number of virtual operands due to
1038 aliasing. Since we are adding one more alias to TAG's
1039 may-aliases set, the total number of virtual operands due
1040 to aliasing will be increased by the number of references
1041 made to VAR and TAG (every reference to TAG will also
1042 count as a reference to VAR). */
1043 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1044 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1051 /* Since this analysis is based exclusively on symbols, it fails to
1052 handle cases where two pointers P and Q have different memory
1053 tags with conflicting alias set numbers but no aliased symbols in
1054 common.
1056 For example, suppose that we have two memory tags TMT.1 and TMT.2
1057 such that
1059 may-aliases (TMT.1) = { a }
1060 may-aliases (TMT.2) = { b }
1062 and the alias set number of TMT.1 conflicts with that of TMT.2.
1063 Since they don't have symbols in common, loads and stores from
1064 TMT.1 and TMT.2 will seem independent of each other, which will
1065 lead to the optimizers making invalid transformations (see
1066 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1068 To avoid this problem, we do a final traversal of AI->POINTERS
1069 looking for pairs of pointers that have no aliased symbols in
1070 common and yet have conflicting alias set numbers. */
1071 for (i = 0; i < ai->num_pointers; i++)
1073 size_t j;
1074 struct alias_map_d *p_map1 = ai->pointers[i];
1075 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1076 sbitmap may_aliases1 = p_map1->may_aliases;
1078 for (j = i + 1; j < ai->num_pointers; j++)
1080 struct alias_map_d *p_map2 = ai->pointers[j];
1081 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1082 sbitmap may_aliases2 = p_map2->may_aliases;
1084 /* If the pointers may not point to each other, do nothing. */
1085 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set))
1086 continue;
1088 /* The two pointers may alias each other. If they already have
1089 symbols in common, do nothing. */
1090 if (sbitmap_any_common_bits (may_aliases1, may_aliases2))
1091 continue;
1093 if (sbitmap_first_set_bit (may_aliases2) >= 0)
1095 size_t k;
1097 /* Add all the aliases for TAG2 into TAG1's alias set.
1098 FIXME, update grouping heuristic counters. */
1099 EXECUTE_IF_SET_IN_SBITMAP (may_aliases2, 0, k,
1100 add_may_alias (tag1, referenced_var (k)));
1101 sbitmap_a_or_b (may_aliases1, may_aliases1, may_aliases2);
1103 else
1105 /* Since TAG2 does not have any aliases of its own, add
1106 TAG2 itself to the alias set of TAG1. */
1107 add_may_alias (tag1, tag2);
1108 SET_BIT (may_aliases1, var_ann (tag2)->uid);
1113 if (dump_file)
1114 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
1115 get_name (current_function_decl),
1116 ai->total_alias_vops);
1118 /* Determine if we need to enable alias grouping. */
1119 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1120 group_aliases (ai);
1124 /* Comparison function for qsort used in group_aliases. */
1126 static int
1127 total_alias_vops_cmp (const void *p, const void *q)
1129 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1130 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1131 long n1 = (*p1)->total_alias_vops;
1132 long n2 = (*p2)->total_alias_vops;
1134 /* We want to sort in descending order. */
1135 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1138 /* Group all the aliases for TAG to make TAG represent all the
1139 variables in its alias set. Update the total number
1140 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1141 function will make TAG be the unique alias tag for all the
1142 variables in its may-aliases. So, given:
1144 may-aliases(TAG) = { V1, V2, V3 }
1146 This function will group the variables into:
1148 may-aliases(V1) = { TAG }
1149 may-aliases(V2) = { TAG }
1150 may-aliases(V2) = { TAG } */
1152 static void
1153 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
1155 size_t i;
1156 var_ann_t tag_ann = var_ann (tag);
1157 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1159 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i,
1161 tree var = referenced_var (i);
1162 var_ann_t ann = var_ann (var);
1164 /* Make TAG the unique alias of VAR. */
1165 ann->is_alias_tag = 0;
1166 ann->may_aliases = NULL;
1168 /* Note that VAR and TAG may be the same if the function has no
1169 addressable variables (see the discussion at the end of
1170 setup_pointers_and_addressables). */
1171 if (var != tag)
1172 add_may_alias (var, tag);
1174 /* Reduce total number of virtual operands contributed
1175 by TAG on behalf of VAR. Notice that the references to VAR
1176 itself won't be removed. We will merely replace them with
1177 references to TAG. */
1178 ai->total_alias_vops -= num_tag_refs;
1181 /* We have reduced the number of virtual operands that TAG makes on
1182 behalf of all the variables formerly aliased with it. However,
1183 we have also "removed" all the virtual operands for TAG itself,
1184 so we add them back. */
1185 ai->total_alias_vops += num_tag_refs;
1187 /* TAG no longer has any aliases. */
1188 tag_ann->may_aliases = NULL;
1192 /* Group may-aliases sets to reduce the number of virtual operands due
1193 to aliasing.
1195 1- Sort the list of pointers in decreasing number of contributed
1196 virtual operands.
1198 2- Take the first entry in AI->POINTERS and revert the role of
1199 the memory tag and its aliases. Usually, whenever an aliased
1200 variable Vi is found to alias with a memory tag T, we add Vi
1201 to the may-aliases set for T. Meaning that after alias
1202 analysis, we will have:
1204 may-aliases(T) = { V1, V2, V3, ..., Vn }
1206 This means that every statement that references T, will get 'n'
1207 virtual operands for each of the Vi tags. But, when alias
1208 grouping is enabled, we make T an alias tag and add it to the
1209 alias set of all the Vi variables:
1211 may-aliases(V1) = { T }
1212 may-aliases(V2) = { T }
1214 may-aliases(Vn) = { T }
1216 This has two effects: (a) statements referencing T will only get
1217 a single virtual operand, and, (b) all the variables Vi will now
1218 appear to alias each other. So, we lose alias precision to
1219 improve compile time. But, in theory, a program with such a high
1220 level of aliasing should not be very optimizable in the first
1221 place.
1223 3- Since variables may be in the alias set of more than one
1224 memory tag, the grouping done in step (2) needs to be extended
1225 to all the memory tags that have a non-empty intersection with
1226 the may-aliases set of tag T. For instance, if we originally
1227 had these may-aliases sets:
1229 may-aliases(T) = { V1, V2, V3 }
1230 may-aliases(R) = { V2, V4 }
1232 In step (2) we would have reverted the aliases for T as:
1234 may-aliases(V1) = { T }
1235 may-aliases(V2) = { T }
1236 may-aliases(V3) = { T }
1238 But note that now V2 is no longer aliased with R. We could
1239 add R to may-aliases(V2), but we are in the process of
1240 grouping aliases to reduce virtual operands so what we do is
1241 add V4 to the grouping to obtain:
1243 may-aliases(V1) = { T }
1244 may-aliases(V2) = { T }
1245 may-aliases(V3) = { T }
1246 may-aliases(V4) = { T }
1248 4- If the total number of virtual operands due to aliasing is
1249 still above the threshold set by max-alias-vops, go back to (2). */
1251 static void
1252 group_aliases (struct alias_info *ai)
1254 size_t i;
1256 /* Sort the POINTERS array in descending order of contributed
1257 virtual operands. */
1258 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1259 total_alias_vops_cmp);
1261 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1262 and the tag's may-aliases set. */
1263 for (i = 0; i < ai->num_pointers; i++)
1265 size_t j;
1266 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1267 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1269 /* Skip tags that have been grouped already. */
1270 if (ai->pointers[i]->grouped_p)
1271 continue;
1273 /* See if TAG1 had any aliases in common with other type tags.
1274 If we find a TAG2 with common aliases with TAG1, add TAG2's
1275 aliases into TAG1. */
1276 for (j = i + 1; j < ai->num_pointers; j++)
1278 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1280 if (sbitmap_any_common_bits (tag1_aliases, tag2_aliases))
1282 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1284 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1286 /* TAG2 does not need its aliases anymore. */
1287 sbitmap_zero (tag2_aliases);
1288 var_ann (tag2)->may_aliases = NULL;
1290 /* TAG1 is the unique alias of TAG2. */
1291 add_may_alias (tag2, tag1);
1293 ai->pointers[j]->grouped_p = true;
1297 /* Now group all the aliases we collected into TAG1. */
1298 group_aliases_into (tag1, tag1_aliases, ai);
1300 /* If we've reduced total number of virtual operands below the
1301 threshold, stop. */
1302 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1303 break;
1306 /* Finally, all the variables that have been grouped cannot be in
1307 the may-alias set of name memory tags. Suppose that we have
1308 grouped the aliases in this code so that may-aliases(a) = TMT.20
1310 p_5 = &a;
1312 # a_9 = V_MAY_DEF <a_8>
1313 p_5->field = 0
1314 ... Several modifications to TMT.20 ...
1315 # VUSE <a_9>
1316 x_30 = p_5->field
1318 Since p_5 points to 'a', the optimizers will try to propagate 0
1319 into p_5->field, but that is wrong because there have been
1320 modifications to 'TMT.20' in between. To prevent this we have to
1321 replace 'a' with 'TMT.20' in the name tag of p_5. */
1322 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1324 size_t j;
1325 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1326 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1327 varray_type aliases;
1329 if (name_tag == NULL_TREE)
1330 continue;
1332 aliases = var_ann (name_tag)->may_aliases;
1333 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1335 tree alias = VARRAY_TREE (aliases, j);
1336 var_ann_t ann = var_ann (alias);
1338 if ((ann->mem_tag_kind == NOT_A_TAG
1339 || ann->mem_tag_kind == STRUCT_FIELD)
1340 && ann->may_aliases)
1342 tree new_alias;
1344 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1346 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1347 replace_may_alias (name_tag, j, new_alias);
1352 if (dump_file)
1353 fprintf (dump_file,
1354 "%s: Total number of aliased vops after grouping: %ld%s\n",
1355 get_name (current_function_decl),
1356 ai->total_alias_vops,
1357 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1361 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1363 static void
1364 create_alias_map_for (tree var, struct alias_info *ai)
1366 struct alias_map_d *alias_map;
1367 alias_map = xcalloc (1, sizeof (*alias_map));
1368 alias_map->var = var;
1369 alias_map->set = get_alias_set (var);
1370 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1374 /* Create memory tags for all the dereferenced pointers and build the
1375 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1376 sets. Based on the address escape and points-to information collected
1377 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1378 variables whose address is not needed anymore. */
1380 static void
1381 setup_pointers_and_addressables (struct alias_info *ai)
1383 size_t i, n_vars, num_addressable_vars, num_pointers;
1385 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1386 num_addressable_vars = num_pointers = 0;
1387 for (i = 0; i < num_referenced_vars; i++)
1389 tree var = referenced_var (i);
1391 if (may_be_aliased (var))
1392 num_addressable_vars++;
1394 if (POINTER_TYPE_P (TREE_TYPE (var)))
1396 /* Since we don't keep track of volatile variables, assume that
1397 these pointers are used in indirect store operations. */
1398 if (TREE_THIS_VOLATILE (var))
1399 bitmap_set_bit (ai->dereferenced_ptrs_store, var_ann (var)->uid);
1401 num_pointers++;
1405 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1406 always going to be slightly bigger than we actually need them
1407 because some TREE_ADDRESSABLE variables will be marked
1408 non-addressable below and only pointers with unique type tags are
1409 going to be added to POINTERS. */
1410 ai->addressable_vars = xcalloc (num_addressable_vars,
1411 sizeof (struct alias_map_d *));
1412 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1413 ai->num_addressable_vars = 0;
1414 ai->num_pointers = 0;
1416 /* Since we will be creating type memory tags within this loop, cache the
1417 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1418 unnecessarily. */
1419 n_vars = num_referenced_vars;
1421 for (i = 0; i < n_vars; i++)
1423 tree var = referenced_var (i);
1424 var_ann_t v_ann = var_ann (var);
1425 subvar_t svars;
1427 /* Name memory tags already have flow-sensitive aliasing
1428 information, so they need not be processed by
1429 compute_flow_insensitive_aliasing. Similarly, type memory
1430 tags are already accounted for when we process their
1431 associated pointer.
1433 Structure fields, on the other hand, have to have some of this
1434 information processed for them, but it's pointless to mark them
1435 non-addressable (since they are fake variables anyway). */
1436 if (v_ann->mem_tag_kind != NOT_A_TAG
1437 && v_ann->mem_tag_kind != STRUCT_FIELD)
1438 continue;
1440 /* Remove the ADDRESSABLE flag from every addressable variable whose
1441 address is not needed anymore. This is caused by the propagation
1442 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1443 removal of dead pointer assignments done by the early scalar
1444 cleanup passes. */
1445 if (TREE_ADDRESSABLE (var) && v_ann->mem_tag_kind != STRUCT_FIELD)
1447 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1448 && TREE_CODE (var) != RESULT_DECL
1449 && !is_global_var (var))
1451 bool okay_to_mark = true;
1452 /* Since VAR is now a regular GIMPLE register, we will need
1453 to rename VAR into SSA afterwards. */
1454 bitmap_set_bit (vars_to_rename, v_ann->uid);
1456 if (var_can_have_subvars (var)
1457 && (svars = get_subvars_for_var (var)))
1459 subvar_t sv;
1461 for (sv = svars; sv; sv = sv->next)
1463 var_ann_t svann = var_ann (sv->var);
1464 if (bitmap_bit_p (ai->addresses_needed, svann->uid))
1465 okay_to_mark = false;
1466 bitmap_set_bit (vars_to_rename, svann->uid);
1469 /* The address of VAR is not needed, remove the
1470 addressable bit, so that it can be optimized as a
1471 regular variable. */
1472 if (okay_to_mark)
1473 mark_non_addressable (var);
1476 else
1478 /* Add the variable to the set of addressables. Mostly
1479 used when scanning operands for ASM_EXPRs that
1480 clobber memory. In those cases, we need to clobber
1481 all call-clobbered variables and all addressables. */
1482 bitmap_set_bit (addressable_vars, v_ann->uid);
1483 if (var_can_have_subvars (var)
1484 && (svars = get_subvars_for_var (var)))
1486 subvar_t sv;
1487 for (sv = svars; sv; sv = sv->next)
1488 bitmap_set_bit (addressable_vars, var_ann (sv->var)->uid);
1494 /* Global variables and addressable locals may be aliased. Create an
1495 entry in ADDRESSABLE_VARS for VAR. */
1496 if (may_be_aliased (var))
1498 create_alias_map_for (var, ai);
1499 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1502 /* Add pointer variables that have been dereferenced to the POINTERS
1503 array and create a type memory tag for them. */
1504 if (POINTER_TYPE_P (TREE_TYPE (var)))
1506 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1507 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1509 tree tag;
1510 var_ann_t t_ann;
1512 /* If pointer VAR still doesn't have a memory tag
1513 associated with it, create it now or re-use an
1514 existing one. */
1515 tag = get_tmt_for (var, ai);
1516 t_ann = var_ann (tag);
1518 /* The type tag will need to be renamed into SSA
1519 afterwards. Note that we cannot do this inside
1520 get_tmt_for because aliasing may run multiple times
1521 and we only create type tags the first time. */
1522 bitmap_set_bit (vars_to_rename, t_ann->uid);
1524 /* Associate the tag with pointer VAR. */
1525 v_ann->type_mem_tag = tag;
1527 /* If pointer VAR has been used in a store operation,
1528 then its memory tag must be marked as written-to. */
1529 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1530 bitmap_set_bit (ai->written_vars, t_ann->uid);
1532 /* If pointer VAR is a global variable or a PARM_DECL,
1533 then its memory tag should be considered a global
1534 variable. */
1535 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1536 mark_call_clobbered (tag);
1538 /* All the dereferences of pointer VAR count as
1539 references of TAG. Since TAG can be associated with
1540 several pointers, add the dereferences of VAR to the
1541 TAG. We may need to grow AI->NUM_REFERENCES because
1542 we have been adding name and type tags. */
1543 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1544 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1546 VARRAY_UINT (ai->num_references, t_ann->uid)
1547 += VARRAY_UINT (ai->num_references, v_ann->uid);
1549 else
1551 /* The pointer has not been dereferenced. If it had a
1552 type memory tag, remove it and mark the old tag for
1553 renaming to remove it out of the IL. */
1554 var_ann_t ann = var_ann (var);
1555 tree tag = ann->type_mem_tag;
1556 if (tag)
1558 bitmap_set_bit (vars_to_rename, var_ann (tag)->uid);
1559 ann->type_mem_tag = NULL_TREE;
1567 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1568 every call site, we need to emit V_MAY_DEF expressions to represent the
1569 clobbering effects of the call for variables whose address escapes the
1570 current function.
1572 One approach is to group all call-clobbered variables into a single
1573 representative that is used as an alias of every call-clobbered variable
1574 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1575 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1577 The second approach is to emit a clobbering V_MAY_DEF for every
1578 call-clobbered variable at call sites. This is the preferred way in terms
1579 of optimization opportunities but it may create too many V_MAY_DEF operands
1580 if there are many call clobbered variables and function calls in the
1581 function.
1583 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1584 function calls found by the number of call-clobbered variables. If that
1585 product is beyond a certain threshold, as determined by the parameterized
1586 values shown below, we use .GLOBAL_VAR.
1588 FIXME. This heuristic should be improved. One idea is to use several
1589 .GLOBAL_VARs of different types instead of a single one. The thresholds
1590 have been derived from a typical bootstrap cycle, including all target
1591 libraries. Compile times were found increase by ~1% compared to using
1592 .GLOBAL_VAR. */
1594 static void
1595 maybe_create_global_var (struct alias_info *ai)
1597 unsigned i, n_clobbered;
1598 bitmap_iterator bi;
1600 /* No need to create it, if we have one already. */
1601 if (global_var == NULL_TREE)
1603 /* Count all the call-clobbered variables. */
1604 n_clobbered = 0;
1605 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1607 n_clobbered++;
1610 /* If the number of virtual operands that would be needed to
1611 model all the call-clobbered variables is larger than
1612 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1614 Also create .GLOBAL_VAR if there are no call-clobbered
1615 variables and the program contains a mixture of pure/const
1616 and regular function calls. This is to avoid the problem
1617 described in PR 20115:
1619 int X;
1620 int func_pure (void) { return X; }
1621 int func_non_pure (int a) { X += a; }
1622 int foo ()
1624 int a = func_pure ();
1625 func_non_pure (a);
1626 a = func_pure ();
1627 return a;
1630 Since foo() has no call-clobbered variables, there is
1631 no relationship between the calls to func_pure and
1632 func_non_pure. Since func_pure has no side-effects, value
1633 numbering optimizations elide the second call to func_pure.
1634 So, if we have some pure/const and some regular calls in the
1635 program we create .GLOBAL_VAR to avoid missing these
1636 relations. */
1637 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1638 || (n_clobbered == 0
1639 && ai->num_calls_found > 0
1640 && ai->num_pure_const_calls_found > 0
1641 && ai->num_calls_found > ai->num_pure_const_calls_found))
1642 create_global_var ();
1645 /* Mark all call-clobbered symbols for renaming. Since the initial
1646 rewrite into SSA ignored all call sites, we may need to rename
1647 .GLOBAL_VAR and the call-clobbered variables. */
1648 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1650 tree var = referenced_var (i);
1652 /* If the function has calls to clobbering functions and
1653 .GLOBAL_VAR has been created, make it an alias for all
1654 call-clobbered variables. */
1655 if (global_var && var != global_var)
1657 subvar_t svars;
1658 add_may_alias (var, global_var);
1659 if (var_can_have_subvars (var)
1660 && (svars = get_subvars_for_var (var)))
1662 subvar_t sv;
1663 for (sv = svars; sv; sv = sv->next)
1664 bitmap_set_bit (vars_to_rename, var_ann (sv->var)->uid);
1668 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1673 /* Return TRUE if pointer PTR may point to variable VAR.
1675 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1676 This is needed because when checking for type conflicts we are
1677 interested in the alias set of the memory location pointed-to by
1678 PTR. The alias set of PTR itself is irrelevant.
1680 VAR_ALIAS_SET is the alias set for VAR. */
1682 static bool
1683 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1684 tree var, HOST_WIDE_INT var_alias_set)
1686 tree mem;
1687 var_ann_t m_ann;
1689 alias_stats.alias_queries++;
1690 alias_stats.simple_queries++;
1692 /* By convention, a variable cannot alias itself. */
1693 mem = var_ann (ptr)->type_mem_tag;
1694 if (mem == var)
1696 alias_stats.alias_noalias++;
1697 alias_stats.simple_resolved++;
1698 return false;
1701 m_ann = var_ann (mem);
1703 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1705 alias_stats.tbaa_queries++;
1707 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1708 PTR can't possibly affect VAR. Note, that we are specifically testing
1709 for PTR's alias set here, not its pointed-to type. We also can't
1710 do this check with relaxed aliasing enabled. */
1711 if (POINTER_TYPE_P (TREE_TYPE (var))
1712 && var_alias_set != 0
1713 && mem_alias_set != 0)
1715 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1716 if (ptr_alias_set == var_alias_set)
1718 alias_stats.alias_noalias++;
1719 alias_stats.tbaa_resolved++;
1720 return false;
1724 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1725 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1727 alias_stats.alias_noalias++;
1728 alias_stats.tbaa_resolved++;
1729 return false;
1731 alias_stats.alias_mayalias++;
1732 return true;
1736 /* Add ALIAS to the set of variables that may alias VAR. */
1738 static void
1739 add_may_alias (tree var, tree alias)
1741 size_t i;
1742 var_ann_t v_ann = get_var_ann (var);
1743 var_ann_t a_ann = get_var_ann (alias);
1745 gcc_assert (var != alias);
1747 if (v_ann->may_aliases == NULL)
1748 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1750 /* Avoid adding duplicates. */
1751 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1752 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1753 return;
1755 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1756 FIXME, call-clobbering should only depend on whether an address
1757 escapes. It should be independent of aliasing. */
1758 if (is_call_clobbered (var))
1759 mark_call_clobbered (alias);
1761 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1762 else if (is_call_clobbered (alias))
1763 mark_call_clobbered (var);
1765 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1766 a_ann->is_alias_tag = 1;
1770 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1772 static void
1773 replace_may_alias (tree var, size_t i, tree new_alias)
1775 var_ann_t v_ann = var_ann (var);
1776 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1778 /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1779 FIXME, call-clobbering should only depend on whether an address
1780 escapes. It should be independent of aliasing. */
1781 if (is_call_clobbered (var))
1782 mark_call_clobbered (new_alias);
1784 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1785 else if (is_call_clobbered (new_alias))
1786 mark_call_clobbered (var);
1790 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1792 static void
1793 set_pt_anything (tree ptr)
1795 struct ptr_info_def *pi = get_ptr_info (ptr);
1797 pi->pt_anything = 1;
1798 pi->pt_malloc = 0;
1800 /* The pointer used to have a name tag, but we now found it pointing
1801 to an arbitrary location. The name tag needs to be renamed and
1802 disassociated from PTR. */
1803 if (pi->name_mem_tag)
1805 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
1806 pi->name_mem_tag = NULL_TREE;
1811 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1813 static void
1814 set_pt_malloc (tree ptr)
1816 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1818 /* If the pointer has already been found to point to arbitrary
1819 memory locations, it is unsafe to mark it as pointing to malloc. */
1820 if (pi->pt_anything)
1821 return;
1823 pi->pt_malloc = 1;
1827 /* Given two different pointers DEST and ORIG. Merge the points-to
1828 information in ORIG into DEST. AI contains all the alias
1829 information collected up to this point. */
1831 static void
1832 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1834 struct ptr_info_def *dest_pi, *orig_pi;
1836 gcc_assert (dest != orig);
1838 /* Make sure we have points-to information for ORIG. */
1839 collect_points_to_info_for (ai, orig);
1841 dest_pi = get_ptr_info (dest);
1842 orig_pi = SSA_NAME_PTR_INFO (orig);
1844 if (orig_pi)
1846 gcc_assert (orig_pi != dest_pi);
1848 /* Notice that we never merge PT_MALLOC. This attribute is only
1849 true if the pointer is the result of a malloc() call.
1850 Otherwise, we can end up in this situation:
1852 P_i = malloc ();
1854 P_j = P_i + X;
1856 P_j would be marked as PT_MALLOC, however we currently do not
1857 handle cases of more than one pointer pointing to the same
1858 malloc'd area.
1860 FIXME: If the merging comes from an expression that preserves
1861 the PT_MALLOC attribute (copy assignment, address
1862 arithmetic), we ought to merge PT_MALLOC, but then both
1863 pointers would end up getting different name tags because
1864 create_name_tags is not smart enough to determine that the
1865 two come from the same malloc call. Copy propagation before
1866 aliasing should cure this. */
1867 dest_pi->pt_malloc = 0;
1868 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1869 set_pt_anything (dest);
1871 dest_pi->pt_null |= orig_pi->pt_null;
1873 if (!dest_pi->pt_anything
1874 && orig_pi->pt_vars
1875 && !bitmap_empty_p (orig_pi->pt_vars))
1877 if (dest_pi->pt_vars == NULL)
1879 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1880 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1882 else
1883 bitmap_ior_into (dest_pi->pt_vars, orig_pi->pt_vars);
1886 else
1887 set_pt_anything (dest);
1891 /* Add EXPR to the list of expressions pointed-to by PTR. */
1893 static void
1894 add_pointed_to_expr (struct alias_info *ai, tree ptr, tree expr)
1896 if (TREE_CODE (expr) == WITH_SIZE_EXPR)
1897 expr = TREE_OPERAND (expr, 0);
1899 get_ptr_info (ptr);
1901 if (TREE_CODE (expr) == CALL_EXPR
1902 && (call_expr_flags (expr) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1904 /* If EXPR is a malloc-like call, then the area pointed to PTR
1905 is guaranteed to not alias with anything else. */
1906 set_pt_malloc (ptr);
1908 else if (TREE_CODE (expr) == ADDR_EXPR)
1910 /* Found P_i = ADDR_EXPR */
1911 add_pointed_to_var (ai, ptr, expr);
1913 else if (TREE_CODE (expr) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (expr)))
1915 /* Found P_i = Q_j. */
1916 merge_pointed_to_info (ai, ptr, expr);
1918 else if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
1920 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1921 tree op0 = TREE_OPERAND (expr, 0);
1922 tree op1 = TREE_OPERAND (expr, 1);
1924 /* Both operands may be of pointer type. FIXME: Shouldn't
1925 we just expect PTR + OFFSET always? */
1926 if (POINTER_TYPE_P (TREE_TYPE (op0))
1927 && TREE_CODE (op0) != INTEGER_CST)
1929 if (TREE_CODE (op0) == SSA_NAME)
1930 merge_pointed_to_info (ai, ptr, op0);
1931 else if (TREE_CODE (op0) == ADDR_EXPR)
1932 add_pointed_to_var (ai, ptr, op0);
1933 else
1934 set_pt_anything (ptr);
1937 if (POINTER_TYPE_P (TREE_TYPE (op1))
1938 && TREE_CODE (op1) != INTEGER_CST)
1940 if (TREE_CODE (op1) == SSA_NAME)
1941 merge_pointed_to_info (ai, ptr, op1);
1942 else if (TREE_CODE (op1) == ADDR_EXPR)
1943 add_pointed_to_var (ai, ptr, op1);
1944 else
1945 set_pt_anything (ptr);
1948 /* Neither operand is a pointer? VAR can be pointing anywhere.
1949 FIXME: Shouldn't we abort here? If we get here, we found
1950 PTR = INT_CST + INT_CST, which should not be a valid pointer
1951 expression. */
1952 if (!(POINTER_TYPE_P (TREE_TYPE (op0))
1953 && TREE_CODE (op0) != INTEGER_CST)
1954 && !(POINTER_TYPE_P (TREE_TYPE (op1))
1955 && TREE_CODE (op1) != INTEGER_CST))
1956 set_pt_anything (ptr);
1958 else if (integer_zerop (expr))
1960 /* EXPR is the NULL pointer. Mark PTR as pointing to NULL. */
1961 SSA_NAME_PTR_INFO (ptr)->pt_null = 1;
1963 else
1965 /* If we can't recognize the expression, assume that PTR may
1966 point anywhere. */
1967 set_pt_anything (ptr);
1972 /* If VALUE is of the form &DECL, add DECL to the set of variables
1973 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1974 PTR. AI points to the collected alias information. */
1976 static void
1977 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1979 struct ptr_info_def *pi = get_ptr_info (ptr);
1980 tree pt_var = NULL_TREE;
1981 HOST_WIDE_INT offset, size;
1982 tree addrop;
1983 size_t uid;
1984 tree ref;
1985 subvar_t svars;
1987 gcc_assert (TREE_CODE (value) == ADDR_EXPR);
1989 addrop = TREE_OPERAND (value, 0);
1990 if (REFERENCE_CLASS_P (addrop))
1991 pt_var = get_base_address (addrop);
1992 else
1993 pt_var = addrop;
1995 /* If this is a component_ref, see if we can get a smaller number of
1996 variables to take the address of. */
1997 if (TREE_CODE (addrop) == COMPONENT_REF
1998 && (ref = okay_component_ref_for_subvars (addrop, &offset ,&size)))
2000 subvar_t sv;
2001 svars = get_subvars_for_var (ref);
2003 uid = var_ann (pt_var)->uid;
2005 if (pi->pt_vars == NULL)
2006 pi->pt_vars = BITMAP_GGC_ALLOC ();
2007 /* If the variable is a global, mark the pointer as pointing to
2008 global memory (which will make its tag a global variable). */
2009 if (is_global_var (pt_var))
2010 pi->pt_global_mem = 1;
2012 for (sv = svars; sv; sv = sv->next)
2014 if (overlap_subvar (offset, size, sv, NULL))
2016 bitmap_set_bit (pi->pt_vars, var_ann (sv->var)->uid);
2017 bitmap_set_bit (ai->addresses_needed, var_ann (sv->var)->uid);
2021 else if (pt_var && SSA_VAR_P (pt_var))
2024 uid = var_ann (pt_var)->uid;
2026 if (pi->pt_vars == NULL)
2027 pi->pt_vars = BITMAP_GGC_ALLOC ();
2029 /* If this is an aggregate, we may have subvariables for it that need
2030 to be pointed to. */
2031 if (var_can_have_subvars (pt_var)
2032 && (svars = get_subvars_for_var (pt_var)))
2034 subvar_t sv;
2035 for (sv = svars; sv; sv = sv->next)
2037 uid = var_ann (sv->var)->uid;
2038 bitmap_set_bit (ai->addresses_needed, uid);
2039 bitmap_set_bit (pi->pt_vars, uid);
2042 else
2044 bitmap_set_bit (ai->addresses_needed, uid);
2045 bitmap_set_bit (pi->pt_vars, uid);
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;
2056 /* Callback for walk_use_def_chains to gather points-to information from the
2057 SSA web.
2059 VAR is an SSA variable or a GIMPLE expression.
2061 STMT is the statement that generates the SSA variable or, if STMT is a
2062 PHI_NODE, VAR is one of the PHI arguments.
2064 DATA is a pointer to a structure of type ALIAS_INFO. */
2066 static bool
2067 collect_points_to_info_r (tree var, tree stmt, void *data)
2069 struct alias_info *ai = (struct alias_info *) data;
2071 if (dump_file && (dump_flags & TDF_DETAILS))
2073 fprintf (dump_file, "Visiting use-def links for ");
2074 print_generic_expr (dump_file, var, dump_flags);
2075 fprintf (dump_file, "\n");
2078 switch (TREE_CODE (stmt))
2080 case RETURN_EXPR:
2081 gcc_assert (TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR);
2082 stmt = TREE_OPERAND (stmt, 0);
2083 /* FALLTHRU */
2085 case MODIFY_EXPR:
2087 tree rhs = TREE_OPERAND (stmt, 1);
2088 STRIP_NOPS (rhs);
2089 add_pointed_to_expr (ai, var, rhs);
2090 break;
2093 case ASM_EXPR:
2094 /* Pointers defined by __asm__ statements can point anywhere. */
2095 set_pt_anything (var);
2096 break;
2098 case NOP_EXPR:
2099 if (IS_EMPTY_STMT (stmt))
2101 tree decl = SSA_NAME_VAR (var);
2103 if (TREE_CODE (decl) == PARM_DECL)
2104 add_pointed_to_expr (ai, var, decl);
2105 else if (DECL_INITIAL (decl))
2106 add_pointed_to_expr (ai, var, DECL_INITIAL (decl));
2107 else
2108 add_pointed_to_expr (ai, var, decl);
2110 break;
2112 case PHI_NODE:
2114 /* It STMT is a PHI node, then VAR is one of its arguments. The
2115 variable that we are analyzing is the LHS of the PHI node. */
2116 tree lhs = PHI_RESULT (stmt);
2118 switch (TREE_CODE (var))
2120 case ADDR_EXPR:
2121 add_pointed_to_var (ai, lhs, var);
2122 break;
2124 case SSA_NAME:
2125 /* Avoid unnecessary merges. */
2126 if (lhs != var)
2127 merge_pointed_to_info (ai, lhs, var);
2128 break;
2130 default:
2131 gcc_assert (is_gimple_min_invariant (var));
2132 add_pointed_to_expr (ai, lhs, var);
2133 break;
2135 break;
2138 default:
2139 gcc_unreachable ();
2142 return false;
2146 /* Return true if STMT is an "escape" site from the current function. Escape
2147 sites those statements which might expose the address of a variable
2148 outside the current function. STMT is an escape site iff:
2150 1- STMT is a function call, or
2151 2- STMT is an __asm__ expression, or
2152 3- STMT is an assignment to a non-local variable, or
2153 4- STMT is a return statement.
2155 AI points to the alias information collected so far. */
2157 static bool
2158 is_escape_site (tree stmt, struct alias_info *ai)
2160 tree call = get_call_expr_in (stmt);
2161 if (call != NULL_TREE)
2163 ai->num_calls_found++;
2165 if (!TREE_SIDE_EFFECTS (call))
2166 ai->num_pure_const_calls_found++;
2168 return true;
2170 else if (TREE_CODE (stmt) == ASM_EXPR)
2171 return true;
2172 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2174 tree lhs = TREE_OPERAND (stmt, 0);
2176 /* Get to the base of _REF nodes. */
2177 if (TREE_CODE (lhs) != SSA_NAME)
2178 lhs = get_base_address (lhs);
2180 /* If we couldn't recognize the LHS of the assignment, assume that it
2181 is a non-local store. */
2182 if (lhs == NULL_TREE)
2183 return true;
2185 /* If the RHS is a conversion between a pointer and an integer, the
2186 pointer escapes since we can't track the integer. */
2187 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2188 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2189 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2190 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
2191 (TREE_OPERAND (stmt, 1), 0)))
2192 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
2193 return true;
2195 /* If the LHS is an SSA name, it can't possibly represent a non-local
2196 memory store. */
2197 if (TREE_CODE (lhs) == SSA_NAME)
2198 return false;
2200 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2201 local variables we cannot be sure if it will escape, because we
2202 don't have information about objects not in SSA form. Need to
2203 implement something along the lines of
2205 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2206 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2207 Conference on Object-Oriented Programming Systems, Languages, and
2208 Applications (OOPSLA), pp. 1-19, 1999. */
2209 return true;
2211 else if (TREE_CODE (stmt) == RETURN_EXPR)
2212 return true;
2214 return false;
2218 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2219 is considered to represent all the pointers whose pointed-to types are
2220 in the same alias set class. Otherwise, the tag represents a single
2221 SSA_NAME pointer variable. */
2223 static tree
2224 create_memory_tag (tree type, bool is_type_tag)
2226 var_ann_t ann;
2227 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2229 /* By default, memory tags are local variables. Alias analysis will
2230 determine whether they should be considered globals. */
2231 DECL_CONTEXT (tag) = current_function_decl;
2233 /* Memory tags are by definition addressable. This also prevents
2234 is_gimple_ref frome confusing memory tags with optimizable
2235 variables. */
2236 TREE_ADDRESSABLE (tag) = 1;
2238 ann = get_var_ann (tag);
2239 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2240 ann->type_mem_tag = NULL_TREE;
2242 /* Add the tag to the symbol table. */
2243 add_referenced_tmp_var (tag);
2245 return tag;
2249 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2250 This is used if P_i has been found to point to a specific set of
2251 variables or to a non-aliased memory location like the address returned
2252 by malloc functions. */
2254 static tree
2255 get_nmt_for (tree ptr)
2257 struct ptr_info_def *pi = get_ptr_info (ptr);
2258 tree tag = pi->name_mem_tag;
2260 if (tag == NULL_TREE)
2261 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2263 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
2264 then its name tag should be considered a global variable. */
2265 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
2266 || pi->pt_malloc
2267 || pi->pt_global_mem)
2268 mark_call_clobbered (tag);
2270 return tag;
2274 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2275 artificial variable that represents the memory location pointed-to by
2276 PTR. It is used to model the effects of pointer de-references on
2277 addressable variables.
2279 AI points to the data gathered during alias analysis. This function
2280 populates the array AI->POINTERS. */
2282 static tree
2283 get_tmt_for (tree ptr, struct alias_info *ai)
2285 size_t i;
2286 tree tag;
2287 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2288 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2290 /* To avoid creating unnecessary memory tags, only create one memory tag
2291 per alias set class. Note that it may be tempting to group
2292 memory tags based on conflicting alias sets instead of
2293 equivalence. That would be wrong because alias sets are not
2294 necessarily transitive (as demonstrated by the libstdc++ test
2295 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2296 such that conflicts (A, B) == true and conflicts (A, C) == true,
2297 it does not necessarily follow that conflicts (B, C) == true. */
2298 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2300 struct alias_map_d *curr = ai->pointers[i];
2301 if (tag_set == curr->set)
2303 tag = var_ann (curr->var)->type_mem_tag;
2304 break;
2308 /* If VAR cannot alias with any of the existing memory tags, create a new
2309 tag for PTR and add it to the POINTERS array. */
2310 if (tag == NULL_TREE)
2312 struct alias_map_d *alias_map;
2314 /* If PTR did not have a type tag already, create a new TMT.*
2315 artificial variable representing the memory location
2316 pointed-to by PTR. */
2317 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2318 tag = create_memory_tag (tag_type, true);
2319 else
2320 tag = var_ann (ptr)->type_mem_tag;
2322 /* Add PTR to the POINTERS array. Note that we are not interested in
2323 PTR's alias set. Instead, we cache the alias set for the memory that
2324 PTR points to. */
2325 alias_map = xcalloc (1, sizeof (*alias_map));
2326 alias_map->var = ptr;
2327 alias_map->set = tag_set;
2328 ai->pointers[ai->num_pointers++] = alias_map;
2331 /* If the pointed-to type is volatile, so is the tag. */
2332 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2334 /* Make sure that the type tag has the same alias set as the
2335 pointed-to type. */
2336 gcc_assert (tag_set == get_alias_set (tag));
2338 return tag;
2342 /* Create GLOBAL_VAR, an artificial global variable to act as a
2343 representative of all the variables that may be clobbered by function
2344 calls. */
2346 static void
2347 create_global_var (void)
2349 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2350 void_type_node);
2351 DECL_ARTIFICIAL (global_var) = 1;
2352 TREE_READONLY (global_var) = 0;
2353 DECL_EXTERNAL (global_var) = 1;
2354 TREE_STATIC (global_var) = 1;
2355 TREE_USED (global_var) = 1;
2356 DECL_CONTEXT (global_var) = NULL_TREE;
2357 TREE_THIS_VOLATILE (global_var) = 0;
2358 TREE_ADDRESSABLE (global_var) = 0;
2360 add_referenced_tmp_var (global_var);
2361 bitmap_set_bit (vars_to_rename, var_ann (global_var)->uid);
2365 /* Dump alias statistics on FILE. */
2367 static void
2368 dump_alias_stats (FILE *file)
2370 const char *funcname
2371 = lang_hooks.decl_printable_name (current_function_decl, 2);
2372 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2373 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2374 fprintf (file, "Total alias mayalias results:\t%u\n",
2375 alias_stats.alias_mayalias);
2376 fprintf (file, "Total alias noalias results:\t%u\n",
2377 alias_stats.alias_noalias);
2378 fprintf (file, "Total simple queries:\t%u\n",
2379 alias_stats.simple_queries);
2380 fprintf (file, "Total simple resolved:\t%u\n",
2381 alias_stats.simple_resolved);
2382 fprintf (file, "Total TBAA queries:\t%u\n",
2383 alias_stats.tbaa_queries);
2384 fprintf (file, "Total TBAA resolved:\t%u\n",
2385 alias_stats.tbaa_resolved);
2389 /* Dump alias information on FILE. */
2391 void
2392 dump_alias_info (FILE *file)
2394 size_t i;
2395 const char *funcname
2396 = lang_hooks.decl_printable_name (current_function_decl, 2);
2398 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2400 fprintf (file, "Aliased symbols\n\n");
2401 for (i = 0; i < num_referenced_vars; i++)
2403 tree var = referenced_var (i);
2404 if (may_be_aliased (var))
2405 dump_variable (file, var);
2408 fprintf (file, "\nDereferenced pointers\n\n");
2409 for (i = 0; i < num_referenced_vars; i++)
2411 tree var = referenced_var (i);
2412 var_ann_t ann = var_ann (var);
2413 if (ann->type_mem_tag)
2414 dump_variable (file, var);
2417 fprintf (file, "\nType memory tags\n\n");
2418 for (i = 0; i < num_referenced_vars; i++)
2420 tree var = referenced_var (i);
2421 var_ann_t ann = var_ann (var);
2422 if (ann->mem_tag_kind == TYPE_TAG)
2423 dump_variable (file, var);
2426 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2428 fprintf (file, "SSA_NAME pointers\n\n");
2429 for (i = 1; i < num_ssa_names; i++)
2431 tree ptr = ssa_name (i);
2432 struct ptr_info_def *pi;
2434 if (ptr == NULL_TREE)
2435 continue;
2437 pi = SSA_NAME_PTR_INFO (ptr);
2438 if (!SSA_NAME_IN_FREE_LIST (ptr)
2439 && pi
2440 && pi->name_mem_tag)
2441 dump_points_to_info_for (file, ptr);
2444 fprintf (file, "\nName memory tags\n\n");
2445 for (i = 0; i < num_referenced_vars; i++)
2447 tree var = referenced_var (i);
2448 var_ann_t ann = var_ann (var);
2449 if (ann->mem_tag_kind == NAME_TAG)
2450 dump_variable (file, var);
2453 fprintf (file, "\n");
2457 /* Dump alias information on stderr. */
2459 void
2460 debug_alias_info (void)
2462 dump_alias_info (stderr);
2466 /* Return the alias information associated with pointer T. It creates a
2467 new instance if none existed. */
2469 struct ptr_info_def *
2470 get_ptr_info (tree t)
2472 struct ptr_info_def *pi;
2474 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2476 pi = SSA_NAME_PTR_INFO (t);
2477 if (pi == NULL)
2479 pi = ggc_alloc (sizeof (*pi));
2480 memset ((void *)pi, 0, sizeof (*pi));
2481 SSA_NAME_PTR_INFO (t) = pi;
2484 return pi;
2488 /* Dump points-to information for SSA_NAME PTR into FILE. */
2490 void
2491 dump_points_to_info_for (FILE *file, tree ptr)
2493 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2495 print_generic_expr (file, ptr, dump_flags);
2497 if (pi)
2499 if (pi->name_mem_tag)
2501 fprintf (file, ", name memory tag: ");
2502 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2505 if (pi->is_dereferenced)
2506 fprintf (file, ", is dereferenced");
2508 if (pi->value_escapes_p)
2509 fprintf (file, ", its value escapes");
2511 if (pi->pt_anything)
2512 fprintf (file, ", points-to anything");
2514 if (pi->pt_malloc)
2515 fprintf (file, ", points-to malloc");
2517 if (pi->pt_null)
2518 fprintf (file, ", points-to NULL");
2520 if (pi->pt_vars)
2522 unsigned ix;
2523 bitmap_iterator bi;
2525 fprintf (file, ", points-to vars: { ");
2526 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2528 print_generic_expr (file, referenced_var (ix), dump_flags);
2529 fprintf (file, " ");
2531 fprintf (file, "}");
2535 fprintf (file, "\n");
2539 /* Dump points-to information for VAR into stderr. */
2541 void
2542 debug_points_to_info_for (tree var)
2544 dump_points_to_info_for (stderr, var);
2548 /* Dump points-to information into FILE. NOTE: This function is slow, as
2549 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2551 void
2552 dump_points_to_info (FILE *file)
2554 basic_block bb;
2555 block_stmt_iterator si;
2556 size_t i;
2557 ssa_op_iter iter;
2558 const char *fname =
2559 lang_hooks.decl_printable_name (current_function_decl, 2);
2561 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2563 /* First dump points-to information for the default definitions of
2564 pointer variables. This is necessary because default definitions are
2565 not part of the code. */
2566 for (i = 0; i < num_referenced_vars; i++)
2568 tree var = referenced_var (i);
2569 if (POINTER_TYPE_P (TREE_TYPE (var)))
2571 var_ann_t ann = var_ann (var);
2572 if (ann->default_def)
2573 dump_points_to_info_for (file, ann->default_def);
2577 /* Dump points-to information for every pointer defined in the program. */
2578 FOR_EACH_BB (bb)
2580 tree phi;
2582 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2584 tree ptr = PHI_RESULT (phi);
2585 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2586 dump_points_to_info_for (file, ptr);
2589 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2591 tree stmt = bsi_stmt (si);
2592 tree def;
2593 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2594 if (POINTER_TYPE_P (TREE_TYPE (def)))
2595 dump_points_to_info_for (file, def);
2599 fprintf (file, "\n");
2603 /* Dump points-to info pointed by PTO into STDERR. */
2605 void
2606 debug_points_to_info (void)
2608 dump_points_to_info (stderr);
2611 /* Dump to FILE the list of variables that may be aliasing VAR. */
2613 void
2614 dump_may_aliases_for (FILE *file, tree var)
2616 varray_type aliases;
2618 if (TREE_CODE (var) == SSA_NAME)
2619 var = SSA_NAME_VAR (var);
2621 aliases = var_ann (var)->may_aliases;
2622 if (aliases)
2624 size_t i;
2625 fprintf (file, "{ ");
2626 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2628 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2629 fprintf (file, " ");
2631 fprintf (file, "}");
2636 /* Dump to stderr the list of variables that may be aliasing VAR. */
2638 void
2639 debug_may_aliases_for (tree var)
2641 dump_may_aliases_for (stderr, var);
2644 /* Return true if VAR may be aliased. */
2646 bool
2647 may_be_aliased (tree var)
2649 /* Obviously. */
2650 if (TREE_ADDRESSABLE (var))
2651 return true;
2653 /* Globally visible variables can have their addresses taken by other
2654 translation units. */
2655 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2656 return true;
2658 /* Automatic variables can't have their addresses escape any other way.
2659 This must be after the check for global variables, as extern declarations
2660 do not have TREE_STATIC set. */
2661 if (!TREE_STATIC (var))
2662 return false;
2664 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2665 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2666 we can only be sure the variable isn't addressable if it's local to the
2667 current function. */
2668 if (flag_unit_at_a_time)
2669 return false;
2670 if (decl_function_context (var) == current_function_decl)
2671 return false;
2673 return true;
2676 /* This structure is simply used during pushing fields onto the fieldstack
2677 to track the offset of the field, since bitpos_of_field gives it relative
2678 to its immediate containing type, and we want it relative to the ultimate
2679 containing object. */
2681 typedef struct fieldoff
2683 tree field;
2684 HOST_WIDE_INT offset;
2685 } *fieldoff_t;
2687 DEF_VEC_MALLOC_P(fieldoff_t);
2689 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2690 structure.
2691 Return -1 if the position is conditional or otherwise non-constant
2692 integer. */
2694 static HOST_WIDE_INT
2695 bitpos_of_field (const tree fdecl)
2698 if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2699 || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2700 return -1;
2702 return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2703 + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2706 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all the fields
2707 of TYPE onto fieldstack, recording their offsets along the way.
2708 OFFSET is used to keep track of the offset in this entire structure, rather
2709 than just the immediately containing structure. */
2711 static void
2712 push_fields_onto_fieldstack (tree type, VEC(fieldoff_t) **fieldstack,
2713 HOST_WIDE_INT offset)
2715 fieldoff_t pair;
2716 tree field = TYPE_FIELDS (type);
2717 if (!field)
2718 return;
2719 if (var_can_have_subvars (field)
2720 && TREE_CODE (field) == FIELD_DECL)
2722 size_t before = VEC_length (fieldoff_t, *fieldstack);
2723 /* Empty structures may have actual size, like in C++. So see if we
2724 actually end up pushing a field, and if not, if the size is nonzero,
2725 push the field onto the stack */
2726 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack, offset);
2727 if (before == VEC_length (fieldoff_t, *fieldstack)
2728 && DECL_SIZE (field)
2729 && !integer_zerop (DECL_SIZE (field)))
2731 pair = xmalloc (sizeof (struct fieldoff));
2732 pair->field = field;
2733 pair->offset = offset;
2734 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2737 else if (TREE_CODE (field) == FIELD_DECL)
2739 pair = xmalloc (sizeof (struct fieldoff));
2740 pair->field = field;
2741 pair->offset = offset + bitpos_of_field (field);
2742 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2744 for (field = TREE_CHAIN (field); field; field = TREE_CHAIN (field))
2746 if (TREE_CODE (field) != FIELD_DECL)
2747 continue;
2748 if (var_can_have_subvars (field))
2750 size_t before = VEC_length (fieldoff_t, *fieldstack);
2751 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack,
2752 offset + bitpos_of_field (field));
2753 /* Empty structures may have actual size, like in C++. So see if we
2754 actually end up pushing a field, and if not, if the size is nonzero,
2755 push the field onto the stack */
2756 if (before == VEC_length (fieldoff_t, *fieldstack)
2757 && DECL_SIZE (field)
2758 && !integer_zerop (DECL_SIZE (field)))
2760 pair = xmalloc (sizeof (struct fieldoff));
2761 pair->field = field;
2762 pair->offset = offset + bitpos_of_field (field);
2763 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2766 else
2768 pair = xmalloc (sizeof (struct fieldoff));
2769 pair->field = field;
2770 pair->offset = offset + bitpos_of_field (field);
2771 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2777 /* This represents the used range of a variable. */
2779 typedef struct used_part
2781 HOST_WIDE_INT minused;
2782 HOST_WIDE_INT maxused;
2783 } *used_part_t;
2785 /* An array of used_part structures, indexed by variable uid. */
2787 static used_part_t *used_portions;
2789 /* Given a variable uid, UID, get or create the entry in the used portions
2790 table for the variable. */
2792 static used_part_t
2793 get_or_create_used_part_for (size_t uid)
2795 used_part_t up;
2796 if (used_portions[uid] == NULL)
2798 up = xcalloc (1, sizeof (struct used_part));
2799 up->minused = INT_MAX;
2800 up->maxused = 0;
2802 else
2803 up = used_portions[uid];
2804 return up;
2809 /* Given an aggregate VAR, create the subvariables that represent its
2810 fields. */
2812 static void
2813 create_overlap_variables_for (tree var)
2815 VEC(fieldoff_t) *fieldstack = NULL;
2816 used_part_t up;
2817 size_t uid = var_ann (var)->uid;
2819 if (used_portions[uid] == NULL)
2820 return;
2822 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0);
2823 if (VEC_length (fieldoff_t, fieldstack) != 0)
2825 subvar_t *subvars;
2826 fieldoff_t fo;
2827 bool notokay = false;
2828 int i;
2830 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2831 know their size, and thus, can't handle.
2832 The same is true of fields with DECL_SIZE that is not an integer
2833 constant (such as variable sized fields).
2834 Fields with offsets which are not constant will have an offset < 0
2835 We *could* handle fields that are constant sized arrays, but
2836 currently don't. Doing so would require some extra changes to
2837 tree-ssa-operands.c. */
2839 for (i = 0; VEC_iterate (fieldoff_t, fieldstack, i, fo); i++)
2841 if (!DECL_SIZE (fo->field)
2842 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
2843 || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
2844 || fo->offset < 0)
2846 notokay = true;
2847 break;
2850 /* Cleanup after ourselves if we can't create overlap variables. */
2851 if (notokay)
2853 while (VEC_length (fieldoff_t, fieldstack) != 0)
2855 fo = VEC_pop (fieldoff_t, fieldstack);
2856 free (fo);
2858 VEC_free (fieldoff_t, fieldstack);
2859 return;
2861 /* Otherwise, create the variables. */
2862 subvars = lookup_subvars_for_var (var);
2863 up = used_portions[uid];
2865 while (VEC_length (fieldoff_t, fieldstack) != 0)
2867 subvar_t sv = ggc_alloc (sizeof (struct subvar));
2868 HOST_WIDE_INT fosize;
2869 var_ann_t ann;
2871 fo = VEC_pop (fieldoff_t, fieldstack);
2872 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
2874 if ((fo->offset <= up->minused
2875 && fo->offset + fosize <= up->minused)
2876 || fo->offset >= up->maxused)
2878 free (fo);
2879 continue;
2882 sv->offset = fo->offset;
2883 sv->size = fosize;
2884 sv->next = *subvars;
2885 sv->var = create_tmp_var_raw (TREE_TYPE (fo->field), "SFT");
2886 if (dump_file)
2888 fprintf (dump_file, "structure field tag %s created for var %s",
2889 get_name (sv->var), get_name (var));
2890 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
2891 sv->offset);
2892 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
2893 sv->size);
2894 fprintf (dump_file, "\n");
2898 /* We need to copy the various flags from var to sv->var, so that
2899 they are is_global_var iff the original variable was. */
2901 DECL_EXTERNAL (sv->var) = DECL_EXTERNAL (var);
2902 TREE_PUBLIC (sv->var) = TREE_PUBLIC (var);
2903 TREE_STATIC (sv->var) = TREE_STATIC (var);
2904 TREE_READONLY (sv->var) = TREE_READONLY (var);
2906 /* Like other memory tags, these need to be marked addressable to
2907 keep is_gimple_reg from thinking they are real. */
2908 TREE_ADDRESSABLE (sv->var) = 1;
2910 DECL_CONTEXT (sv->var) = DECL_CONTEXT (var);
2912 ann = get_var_ann (sv->var);
2913 ann->mem_tag_kind = STRUCT_FIELD;
2914 ann->type_mem_tag = NULL;
2915 add_referenced_tmp_var (sv->var);
2917 *subvars = sv;
2918 free (fo);
2921 /* Once we have created subvars, the original is no longer call
2922 clobbered on its own. Its call clobbered status depends
2923 completely on the call clobbered status of the subvars.
2925 add_referenced_var in the above loop will take care of
2926 marking subvars of global variables as call clobbered for us
2927 to start, since they are global as well. */
2928 clear_call_clobbered (var);
2932 VEC_free (fieldoff_t, fieldstack);
2936 /* Find the conservative answer to the question of what portions of what
2937 structures are used by this statement. We assume that if we have a
2938 component ref with a known size + offset, that we only need that part
2939 of the structure. For unknown cases, or cases where we do something
2940 to the whole structure, we assume we need to create fields for the
2941 entire structure. */
2943 static tree
2944 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2946 switch (TREE_CODE (*tp))
2948 case COMPONENT_REF:
2950 HOST_WIDE_INT bitsize;
2951 HOST_WIDE_INT bitpos;
2952 tree offset;
2953 enum machine_mode mode;
2954 int unsignedp;
2955 int volatilep;
2956 tree ref;
2957 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
2958 &unsignedp, &volatilep, false);
2959 if (DECL_P (ref) && offset == NULL && bitsize != -1)
2961 size_t uid = var_ann (ref)->uid;
2962 used_part_t up;
2964 up = get_or_create_used_part_for (uid);
2966 if (bitpos <= up->minused)
2967 up->minused = bitpos;
2968 if ((bitpos + bitsize >= up->maxused))
2969 up->maxused = bitpos + bitsize;
2971 used_portions[uid] = up;
2973 *walk_subtrees = 0;
2974 return NULL_TREE;
2976 else if (DECL_P (ref))
2978 if (DECL_SIZE (ref)
2979 && var_can_have_subvars (ref)
2980 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
2982 used_part_t up;
2983 size_t uid = var_ann (ref)->uid;
2985 up = get_or_create_used_part_for (uid);
2987 up->minused = 0;
2988 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
2990 used_portions[uid] = up;
2992 *walk_subtrees = 0;
2993 return NULL_TREE;
2997 break;
2998 case VAR_DECL:
2999 case PARM_DECL:
3001 tree var = *tp;
3002 if (DECL_SIZE (var)
3003 && var_can_have_subvars (var)
3004 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3006 used_part_t up;
3007 size_t uid = var_ann (var)->uid;
3009 up = get_or_create_used_part_for (uid);
3011 up->minused = 0;
3012 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3014 used_portions[uid] = up;
3015 *walk_subtrees = 0;
3016 return NULL_TREE;
3019 break;
3021 default:
3022 break;
3025 return NULL_TREE;
3028 /* We are about to create some new referenced variables, and we need the
3029 before size. */
3031 static size_t old_referenced_vars;
3034 /* Create structure field variables for structures used in this function. */
3036 static void
3037 create_structure_vars (void)
3039 basic_block bb;
3040 size_t i;
3042 old_referenced_vars = num_referenced_vars;
3043 used_portions = xcalloc (num_referenced_vars, sizeof (used_part_t));
3045 FOR_EACH_BB (bb)
3047 block_stmt_iterator bsi;
3048 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3050 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3051 find_used_portions,
3052 NULL);
3055 for (i = 0; i < old_referenced_vars; i++)
3057 tree var = referenced_var (i);
3058 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3059 if (var
3060 && DECL_SIZE (var)
3061 && var_can_have_subvars (var)
3062 && var_ann (var)->mem_tag_kind == NOT_A_TAG
3063 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3064 create_overlap_variables_for (var);
3066 for (i = 0; i < old_referenced_vars; i++)
3067 free (used_portions[i]);
3069 free (used_portions);
3072 static bool
3073 gate_structure_vars (void)
3075 return flag_tree_salias != 0;
3078 struct tree_opt_pass pass_create_structure_vars =
3080 "salias", /* name */
3081 gate_structure_vars, /* gate */
3082 create_structure_vars, /* execute */
3083 NULL, /* sub */
3084 NULL, /* next */
3085 0, /* static_pass_number */
3086 0, /* tv_id */
3087 PROP_cfg, /* properties_required */
3088 0, /* properties_provided */
3089 0, /* properties_destroyed */
3090 0, /* todo_flags_start */
3091 TODO_dump_func, /* todo_flags_finish */
3092 0 /* letter */