This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / gcc / tree-ssa-alias.c
blob18e88e3db58a85afffb683891de9dc5b11dfe6d2
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 /* Structure to map a variable to its alias set and keep track of the
48 virtual operands that will be needed to represent it. */
49 struct alias_map_d
51 /* Variable and its alias set. */
52 tree var;
53 HOST_WIDE_INT set;
55 /* Total number of virtual operands that will be needed to represent
56 all the aliases of VAR. */
57 long total_alias_vops;
59 /* Nonzero if the aliases for this memory tag have been grouped
60 already. Used in group_aliases. */
61 unsigned int grouped_p : 1;
63 /* Set of variables aliased with VAR. This is the exact same
64 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
65 bitmap form to speed up alias grouping. */
66 sbitmap may_aliases;
70 /* Alias information used by compute_may_aliases and its helpers. */
71 struct alias_info
73 /* SSA names visited while collecting points-to information. If bit I
74 is set, it means that SSA variable with version I has already been
75 visited. */
76 sbitmap ssa_names_visited;
78 /* Array of SSA_NAME pointers processed by the points-to collector. */
79 varray_type processed_ptrs;
81 /* Variables whose address is still needed. */
82 bitmap addresses_needed;
84 /* ADDRESSABLE_VARS contains all the global variables and locals that
85 have had their address taken. */
86 struct alias_map_d **addressable_vars;
87 size_t num_addressable_vars;
89 /* POINTERS contains all the _DECL pointers with unique memory tags
90 that have been referenced in the program. */
91 struct alias_map_d **pointers;
92 size_t num_pointers;
94 /* Number of function calls found in the program. */
95 size_t num_calls_found;
97 /* Array of counters to keep track of how many times each pointer has
98 been dereferenced in the program. This is used by the alias grouping
99 heuristic in compute_flow_insensitive_aliasing. */
100 varray_type num_references;
102 /* Total number of virtual operands that will be needed to represent
103 all the aliases of all the pointers found in the program. */
104 long total_alias_vops;
106 /* Variables that have been written to. */
107 bitmap written_vars;
109 /* Pointers that have been used in an indirect store operation. */
110 bitmap dereferenced_ptrs_store;
112 /* Pointers that have been used in an indirect load operation. */
113 bitmap dereferenced_ptrs_load;
117 /* Counters used to display statistics on alias analysis. */
118 struct alias_stats_d
120 unsigned int alias_queries;
121 unsigned int alias_mayalias;
122 unsigned int alias_noalias;
123 unsigned int simple_queries;
124 unsigned int simple_resolved;
125 unsigned int tbaa_queries;
126 unsigned int tbaa_resolved;
130 /* Local variables. */
131 static struct alias_stats_d alias_stats;
133 /* Local functions. */
134 static void compute_flow_insensitive_aliasing (struct alias_info *);
135 static void dump_alias_stats (FILE *);
136 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT);
137 static tree create_memory_tag (tree type, bool is_type_tag);
138 static tree get_tmt_for (tree, struct alias_info *);
139 static tree get_nmt_for (tree);
140 static void add_may_alias (tree, tree);
141 static void replace_may_alias (tree, size_t, tree);
142 static struct alias_info *init_alias_info (void);
143 static void delete_alias_info (struct alias_info *);
144 static void compute_points_to_and_addr_escape (struct alias_info *);
145 static void compute_flow_sensitive_aliasing (struct alias_info *);
146 static void setup_pointers_and_addressables (struct alias_info *);
147 static bool collect_points_to_info_r (tree, tree, void *);
148 static bool is_escape_site (tree, size_t *);
149 static void add_pointed_to_var (struct alias_info *, tree, tree);
150 static void create_global_var (void);
151 static void collect_points_to_info_for (struct alias_info *, tree);
152 static void maybe_create_global_var (struct alias_info *ai);
153 static void group_aliases (struct alias_info *);
154 static void set_pt_anything (tree ptr);
155 static void set_pt_malloc (tree ptr);
157 /* Global declarations. */
159 /* Call clobbered variables in the function. If bit I is set, then
160 REFERENCED_VARS (I) is call-clobbered. */
161 bitmap call_clobbered_vars;
163 /* Addressable variables in the function. If bit I is set, then
164 REFERENCED_VARS (I) has had its address taken. Note that
165 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
166 addressable variable is not necessarily call-clobbered (e.g., a
167 local addressable whose address does not escape) and not all
168 call-clobbered variables are addressable (e.g., a local static
169 variable). */
170 bitmap addressable_vars;
172 /* When the program has too many call-clobbered variables and call-sites,
173 this variable is used to represent the clobbering effects of function
174 calls. In these cases, all the call clobbered variables in the program
175 are forced to alias this variable. This reduces compile times by not
176 having to keep track of too many V_MAY_DEF expressions at call sites. */
177 tree global_var;
180 /* Compute may-alias information for every variable referenced in function
181 FNDECL.
183 Alias analysis proceeds in 3 main phases:
185 1- Points-to and escape analysis.
187 This phase walks the use-def chains in the SSA web looking for three
188 things:
190 * Assignments of the form P_i = &VAR
191 * Assignments of the form P_i = malloc()
192 * Pointers and ADDR_EXPR that escape the current function.
194 The concept of 'escaping' is the same one used in the Java world. When
195 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
196 outside of the current function. So, assignment to global variables,
197 function arguments and returning a pointer are all escape sites, as are
198 conversions between pointers and integers.
200 This is where we are currently limited. Since not everything is renamed
201 into SSA, we lose track of escape properties when a pointer is stashed
202 inside a field in a structure, for instance. In those cases, we are
203 assuming that the pointer does escape.
205 We use escape analysis to determine whether a variable is
206 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
207 is call-clobbered. If a pointer P_i escapes, then all the variables
208 pointed-to by P_i (and its memory tag) also escape.
210 2- Compute flow-sensitive aliases
212 We have two classes of memory tags. Memory tags associated with the
213 pointed-to data type of the pointers in the program. These tags are
214 called "type memory tag" (TMT). The other class are those associated
215 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
216 when adding operands for an INDIRECT_REF *P_i, we will first check
217 whether P_i has a name tag, if it does we use it, because that will have
218 more precise aliasing information. Otherwise, we use the standard type
219 tag.
221 In this phase, we go through all the pointers we found in points-to
222 analysis and create alias sets for the name memory tags associated with
223 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
224 it points to and its tag.
227 3- Compute flow-insensitive aliases
229 This pass will compare the alias set of every type memory tag and every
230 addressable variable found in the program. Given a type memory tag TMT
231 and an addressable variable V. If the alias sets of TMT and V conflict
232 (as computed by may_alias_p), then V is marked as an alias tag and added
233 to the alias set of TMT.
235 For instance, consider the following function:
237 foo (int i)
239 int *p, a, b;
241 if (i > 10)
242 p = &a;
243 else
244 p = &b;
246 *p = 3;
247 a = b + 2;
248 return *p;
251 After aliasing analysis has finished, the type memory tag for pointer
252 'p' will have two aliases, namely variables 'a' and 'b'. Every time
253 pointer 'p' is dereferenced, we want to mark the operation as a
254 potential reference to 'a' and 'b'.
256 foo (int i)
258 int *p, a, b;
260 if (i_2 > 10)
261 p_4 = &a;
262 else
263 p_6 = &b;
264 # p_1 = PHI <p_4(1), p_6(2)>;
266 # a_7 = V_MAY_DEF <a_3>;
267 # b_8 = V_MAY_DEF <b_5>;
268 *p_1 = 3;
270 # a_9 = V_MAY_DEF <a_7>
271 # VUSE <b_8>
272 a_9 = b_8 + 2;
274 # VUSE <a_9>;
275 # VUSE <b_8>;
276 return *p_1;
279 In certain cases, the list of may aliases for a pointer may grow too
280 large. This may cause an explosion in the number of virtual operands
281 inserted in the code. Resulting in increased memory consumption and
282 compilation time.
284 When the number of virtual operands needed to represent aliased
285 loads and stores grows too large (configurable with @option{--param
286 max-aliased-vops}), alias sets are grouped to avoid severe
287 compile-time slow downs and memory consumption. See group_aliases. */
289 static void
290 compute_may_aliases (void)
292 struct alias_info *ai;
294 memset (&alias_stats, 0, sizeof (alias_stats));
296 /* Initialize aliasing information. */
297 ai = init_alias_info ();
299 /* For each pointer P_i, determine the sets of variables that P_i may
300 point-to. For every addressable variable V, determine whether the
301 address of V escapes the current function, making V call-clobbered
302 (i.e., whether &V is stored in a global variable or if its passed as a
303 function call argument). */
304 compute_points_to_and_addr_escape (ai);
306 /* Collect all pointers and addressable variables, compute alias sets,
307 create memory tags for pointers and promote variables whose address is
308 not needed anymore. */
309 setup_pointers_and_addressables (ai);
311 /* Compute flow-sensitive, points-to based aliasing for all the name
312 memory tags. Note that this pass needs to be done before flow
313 insensitive analysis because it uses the points-to information
314 gathered before to mark call-clobbered type tags. */
315 compute_flow_sensitive_aliasing (ai);
317 /* Compute type-based flow-insensitive aliasing for all the type
318 memory tags. */
319 compute_flow_insensitive_aliasing (ai);
321 /* If the program has too many call-clobbered variables and/or function
322 calls, create .GLOBAL_VAR and use it to model call-clobbering
323 semantics at call sites. This reduces the number of virtual operands
324 considerably, improving compile times at the expense of lost
325 aliasing precision. */
326 maybe_create_global_var (ai);
328 /* Debugging dumps. */
329 if (dump_file)
331 dump_referenced_vars (dump_file);
332 if (dump_flags & TDF_STATS)
333 dump_alias_stats (dump_file);
334 dump_points_to_info (dump_file);
335 dump_alias_info (dump_file);
338 /* Deallocate memory used by aliasing data structures. */
339 delete_alias_info (ai);
342 struct tree_opt_pass pass_may_alias =
344 "alias", /* name */
345 NULL, /* gate */
346 compute_may_aliases, /* execute */
347 NULL, /* sub */
348 NULL, /* next */
349 0, /* static_pass_number */
350 TV_TREE_MAY_ALIAS, /* tv_id */
351 PROP_cfg | PROP_ssa, /* properties_required */
352 PROP_alias, /* properties_provided */
353 0, /* properties_destroyed */
354 0, /* todo_flags_start */
355 TODO_dump_func | TODO_rename_vars
356 | TODO_ggc_collect | TODO_verify_ssa
357 | TODO_verify_stmts, /* todo_flags_finish */
358 0 /* letter */
362 /* Data structure used to count the number of dereferences to PTR
363 inside an expression. */
364 struct count_ptr_d
366 tree ptr;
367 unsigned count;
371 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
372 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
374 static tree
375 count_ptr_derefs (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
377 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
379 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
380 count_p->count++;
382 return NULL_TREE;
386 /* Count the number of direct and indirect uses for pointer PTR in
387 statement STMT. The two counts are stored in *NUM_USES_P and
388 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
389 least one of those dereferences is a store operation. */
391 static void
392 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
393 unsigned *num_derefs_p, bool *is_store)
395 ssa_op_iter i;
396 tree use;
398 *num_uses_p = 0;
399 *num_derefs_p = 0;
400 *is_store = false;
402 /* Find out the total number of uses of PTR in STMT. */
403 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
404 if (use == ptr)
405 (*num_uses_p)++;
407 /* Now count the number of indirect references to PTR. This is
408 truly awful, but we don't have much choice. There are no parent
409 pointers inside INDIRECT_REFs, so an expression like
410 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
411 find all the indirect and direct uses of x_1 inside. The only
412 shortcut we can take is the fact that GIMPLE only allows
413 INDIRECT_REFs inside the expressions below. */
414 if (TREE_CODE (stmt) == MODIFY_EXPR
415 || (TREE_CODE (stmt) == RETURN_EXPR
416 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
417 || TREE_CODE (stmt) == ASM_EXPR
418 || TREE_CODE (stmt) == CALL_EXPR)
420 tree lhs, rhs;
422 if (TREE_CODE (stmt) == MODIFY_EXPR)
424 lhs = TREE_OPERAND (stmt, 0);
425 rhs = TREE_OPERAND (stmt, 1);
427 else if (TREE_CODE (stmt) == RETURN_EXPR)
429 tree e = TREE_OPERAND (stmt, 0);
430 lhs = TREE_OPERAND (e, 0);
431 rhs = TREE_OPERAND (e, 1);
433 else if (TREE_CODE (stmt) == ASM_EXPR)
435 lhs = ASM_OUTPUTS (stmt);
436 rhs = ASM_INPUTS (stmt);
438 else
440 lhs = NULL_TREE;
441 rhs = stmt;
444 if (lhs && EXPR_P (lhs))
446 struct count_ptr_d count;
447 count.ptr = ptr;
448 count.count = 0;
449 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
450 *is_store = true;
451 *num_derefs_p = count.count;
454 if (rhs && EXPR_P (rhs))
456 struct count_ptr_d count;
457 count.ptr = ptr;
458 count.count = 0;
459 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
460 *num_derefs_p += count.count;
464 gcc_assert (*num_uses_p >= *num_derefs_p);
468 /* Count the number of calls in the function and conditionally
469 create GLOBAL_VAR. This is performed before translation
470 into SSA (and thus before alias analysis) to avoid compile time
471 and memory utilization explosions in functions with many
472 of calls and call clobbered variables. */
474 static void
475 count_calls_and_maybe_create_global_var (void)
477 struct alias_info ai;
478 basic_block bb;
479 bool temp;
481 memset (&ai, 0, sizeof (struct alias_info));
483 /* First count the number of calls in the IL. */
484 FOR_EACH_BB (bb)
486 block_stmt_iterator si;
488 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
490 tree stmt = bsi_stmt (si);
492 if (get_call_expr_in (stmt) != NULL_TREE)
493 ai.num_calls_found++;
497 /* If there are no call clobbered variables, then maybe_create_global_var
498 will always create a GLOBAL_VAR. At this point we do not want that
499 behavior. So we turn on one bit in CALL_CLOBBERED_VARs, call
500 maybe_create_global_var, then reset the bit to its original state. */
501 temp = bitmap_bit_p (call_clobbered_vars, 0);
502 bitmap_set_bit (call_clobbered_vars, 0);
503 maybe_create_global_var (&ai);
504 if (!temp)
505 bitmap_clear_bit (call_clobbered_vars, 0);
508 struct tree_opt_pass pass_maybe_create_global_var =
510 "maybe_create_global_var", /* name */
511 NULL, /* gate */
512 count_calls_and_maybe_create_global_var, /* execute */
513 NULL, /* sub */
514 NULL, /* next */
515 0, /* static_pass_number */
516 TV_TREE_MAY_ALIAS, /* tv_id */
517 PROP_cfg, /* properties_required */
518 0, /* properties_provided */
519 0, /* properties_destroyed */
520 0, /* todo_flags_start */
521 0, /* todo_flags_finish */
522 0 /* letter */
525 /* Initialize the data structures used for alias analysis. */
527 static struct alias_info *
528 init_alias_info (void)
530 struct alias_info *ai;
531 static bool aliases_computed_p = false;
533 ai = xcalloc (1, sizeof (struct alias_info));
534 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
535 sbitmap_zero (ai->ssa_names_visited);
536 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
537 ai->addresses_needed = BITMAP_XMALLOC ();
538 VARRAY_UINT_INIT (ai->num_references, num_referenced_vars, "num_references");
539 ai->written_vars = BITMAP_XMALLOC ();
540 ai->dereferenced_ptrs_store = BITMAP_XMALLOC ();
541 ai->dereferenced_ptrs_load = BITMAP_XMALLOC ();
543 /* If aliases have been computed before, clear existing information. */
544 if (aliases_computed_p)
546 unsigned i;
547 basic_block bb;
549 /* Make sure that every statement has a valid set of operands.
550 If a statement needs to be scanned for operands while we
551 compute aliases, it may get erroneous operands because all
552 the alias relations are not built at that point.
553 FIXME: This code will become obsolete when operands are not
554 lazily updated. */
555 FOR_EACH_BB (bb)
557 block_stmt_iterator si;
558 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
559 get_stmt_operands (bsi_stmt (si));
562 /* Similarly, clear the set of addressable variables. In this
563 case, we can just clear the set because addressability is
564 only computed here. */
565 bitmap_clear (addressable_vars);
567 /* Clear flow-insensitive alias information from each symbol. */
568 for (i = 0; i < num_referenced_vars; i++)
570 tree var = referenced_var (i);
571 var_ann_t ann = var_ann (var);
573 ann->is_alias_tag = 0;
574 ann->may_aliases = NULL;
576 /* Since we are about to re-discover call-clobbered
577 variables, clear the call-clobbered flag. Variables that
578 are intrinsically call-clobbered (globals, local statics,
579 etc) will not be marked by the aliasing code, so we can't
580 remove them from CALL_CLOBBERED_VARS.
582 NB: STRUCT_FIELDS are still call clobbered if they are for
583 a global variable, so we *don't* clear their call clobberedness
584 just because they are tags, though we will clear it if they
585 aren't for global variables. */
586 if (ann->mem_tag_kind == NAME_TAG
587 || ann->mem_tag_kind == TYPE_TAG
588 || !is_global_var (var))
589 clear_call_clobbered (var);
592 /* Clear flow-sensitive points-to information from each SSA name. */
593 for (i = 1; i < num_ssa_names; i++)
595 tree name = ssa_name (i);
597 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
598 continue;
600 if (SSA_NAME_PTR_INFO (name))
602 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
604 /* Clear all the flags but keep the name tag to
605 avoid creating new temporaries unnecessarily. If
606 this pointer is found to point to a subset or
607 superset of its former points-to set, then a new
608 tag will need to be created in create_name_tags. */
609 pi->pt_anything = 0;
610 pi->pt_malloc = 0;
611 pi->pt_null = 0;
612 pi->value_escapes_p = 0;
613 pi->is_dereferenced = 0;
614 if (pi->pt_vars)
615 bitmap_clear (pi->pt_vars);
620 /* Next time, we will need to reset alias information. */
621 aliases_computed_p = true;
623 return ai;
627 /* Deallocate memory used by alias analysis. */
629 static void
630 delete_alias_info (struct alias_info *ai)
632 size_t i;
634 sbitmap_free (ai->ssa_names_visited);
635 ai->processed_ptrs = NULL;
636 BITMAP_XFREE (ai->addresses_needed);
638 for (i = 0; i < ai->num_addressable_vars; i++)
640 sbitmap_free (ai->addressable_vars[i]->may_aliases);
641 free (ai->addressable_vars[i]);
643 free (ai->addressable_vars);
645 for (i = 0; i < ai->num_pointers; i++)
647 sbitmap_free (ai->pointers[i]->may_aliases);
648 free (ai->pointers[i]);
650 free (ai->pointers);
652 ai->num_references = NULL;
653 BITMAP_XFREE (ai->written_vars);
654 BITMAP_XFREE (ai->dereferenced_ptrs_store);
655 BITMAP_XFREE (ai->dereferenced_ptrs_load);
657 free (ai);
661 /* Walk use-def chains for pointer PTR to determine what variables is PTR
662 pointing to. */
664 static void
665 collect_points_to_info_for (struct alias_info *ai, tree ptr)
667 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
669 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr)))
671 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (ptr));
672 walk_use_def_chains (ptr, collect_points_to_info_r, ai, true);
673 VARRAY_PUSH_TREE (ai->processed_ptrs, ptr);
678 /* Traverse use-def links for all the pointers in the program to collect
679 address escape and points-to information.
681 This is loosely based on the same idea described in R. Hasti and S.
682 Horwitz, ``Using static single assignment form to improve
683 flow-insensitive pointer analysis,'' in SIGPLAN Conference on
684 Programming Language Design and Implementation, pp. 97-105, 1998. */
686 static void
687 compute_points_to_and_addr_escape (struct alias_info *ai)
689 basic_block bb;
690 unsigned i;
691 tree op;
692 ssa_op_iter iter;
694 timevar_push (TV_TREE_PTA);
696 FOR_EACH_BB (bb)
698 bb_ann_t block_ann = bb_ann (bb);
699 block_stmt_iterator si;
701 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
703 bitmap addr_taken;
704 tree stmt = bsi_stmt (si);
705 bool stmt_escapes_p = is_escape_site (stmt, &ai->num_calls_found);
706 bitmap_iterator bi;
708 /* Mark all the variables whose address are taken by the
709 statement. Note that this will miss all the addresses taken
710 in PHI nodes (those are discovered while following the use-def
711 chains). */
712 get_stmt_operands (stmt);
713 addr_taken = addresses_taken (stmt);
714 if (addr_taken)
715 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
717 tree var = referenced_var (i);
718 bitmap_set_bit (ai->addresses_needed, var_ann (var)->uid);
719 if (stmt_escapes_p)
720 mark_call_clobbered (var);
723 if (stmt_escapes_p)
724 block_ann->has_escape_site = 1;
726 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
728 var_ann_t v_ann = var_ann (SSA_NAME_VAR (op));
729 struct ptr_info_def *pi;
730 bool is_store;
731 unsigned num_uses, num_derefs;
733 /* If the operand's variable may be aliased, keep track
734 of how many times we've referenced it. This is used
735 for alias grouping in compute_flow_sensitive_aliasing.
736 Note that we don't need to grow AI->NUM_REFERENCES
737 because we are processing regular variables, not
738 memory tags (the array's initial size is set to
739 NUM_REFERENCED_VARS). */
740 if (may_be_aliased (SSA_NAME_VAR (op)))
741 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
743 if (!POINTER_TYPE_P (TREE_TYPE (op)))
744 continue;
746 collect_points_to_info_for (ai, op);
748 pi = SSA_NAME_PTR_INFO (op);
749 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs,
750 &is_store);
752 if (num_derefs > 0)
754 /* Mark OP as dereferenced. In a subsequent pass,
755 dereferenced pointers that point to a set of
756 variables will be assigned a name tag to alias
757 all the variables OP points to. */
758 pi->is_dereferenced = 1;
760 /* Keep track of how many time we've dereferenced each
761 pointer. Again, we don't need to grow
762 AI->NUM_REFERENCES because we're processing
763 existing program variables. */
764 (VARRAY_UINT (ai->num_references, v_ann->uid))++;
766 /* If this is a store operation, mark OP as being
767 dereferenced to store, otherwise mark it as being
768 dereferenced to load. */
769 if (is_store)
770 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
771 else
772 bitmap_set_bit (ai->dereferenced_ptrs_load, v_ann->uid);
775 if (stmt_escapes_p && num_derefs < num_uses)
777 /* If STMT is an escape point and STMT contains at
778 least one direct use of OP, then the value of OP
779 escapes and so the pointed-to variables need to
780 be marked call-clobbered. */
781 pi->value_escapes_p = 1;
783 /* If the statement makes a function call, assume
784 that pointer OP will be dereferenced in a store
785 operation inside the called function. */
786 if (get_call_expr_in (stmt))
788 bitmap_set_bit (ai->dereferenced_ptrs_store, v_ann->uid);
789 pi->is_dereferenced = 1;
794 /* Update reference counter for definitions to any
795 potentially aliased variable. This is used in the alias
796 grouping heuristics. */
797 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
799 tree var = SSA_NAME_VAR (op);
800 var_ann_t ann = var_ann (var);
801 bitmap_set_bit (ai->written_vars, ann->uid);
802 if (may_be_aliased (var))
803 (VARRAY_UINT (ai->num_references, ann->uid))++;
805 if (POINTER_TYPE_P (TREE_TYPE (op)))
806 collect_points_to_info_for (ai, op);
809 /* Mark variables in V_MAY_DEF operands as being written to. */
810 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
812 tree var = SSA_NAME_VAR (op);
813 var_ann_t ann = var_ann (var);
814 bitmap_set_bit (ai->written_vars, ann->uid);
817 /* After promoting variables and computing aliasing we will
818 need to re-scan most statements. FIXME: Try to minimize the
819 number of statements re-scanned. It's not really necessary to
820 re-scan *all* statements. */
821 modify_stmt (stmt);
825 timevar_pop (TV_TREE_PTA);
829 /* Create name tags for all the pointers that have been dereferenced.
830 We only create a name tag for a pointer P if P is found to point to
831 a set of variables (so that we can alias them to *P) or if it is
832 the result of a call to malloc (which means that P cannot point to
833 anything else nor alias any other variable).
835 If two pointers P and Q point to the same set of variables, they
836 are assigned the same name tag. */
838 static void
839 create_name_tags (struct alias_info *ai)
841 size_t i;
843 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
845 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
846 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
848 if (pi->pt_anything || !pi->is_dereferenced)
850 /* No name tags for pointers that have not been
851 dereferenced or point to an arbitrary location. */
852 pi->name_mem_tag = NULL_TREE;
853 continue;
856 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
858 size_t j;
859 tree old_name_tag = pi->name_mem_tag;
861 /* If PTR points to a set of variables, check if we don't
862 have another pointer Q with the same points-to set before
863 creating a tag. If so, use Q's tag instead of creating a
864 new one.
866 This is important for not creating unnecessary symbols
867 and also for copy propagation. If we ever need to
868 propagate PTR into Q or vice-versa, we would run into
869 problems if they both had different name tags because
870 they would have different SSA version numbers (which
871 would force us to take the name tags in and out of SSA). */
872 for (j = 0; j < i; j++)
874 tree q = VARRAY_TREE (ai->processed_ptrs, j);
875 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (q);
877 if (qi
878 && qi->pt_vars
879 && qi->name_mem_tag
880 && bitmap_equal_p (pi->pt_vars, qi->pt_vars))
882 pi->name_mem_tag = qi->name_mem_tag;
883 break;
887 /* If we didn't find a pointer with the same points-to set
888 as PTR, create a new name tag if needed. */
889 if (pi->name_mem_tag == NULL_TREE)
890 pi->name_mem_tag = get_nmt_for (ptr);
892 /* If the new name tag computed for PTR is different than
893 the old name tag that it used to have, then the old tag
894 needs to be removed from the IL, so we mark it for
895 renaming. */
896 if (old_name_tag && old_name_tag != pi->name_mem_tag)
897 bitmap_set_bit (vars_to_rename, var_ann (old_name_tag)->uid);
899 else if (pi->pt_malloc)
901 /* Otherwise, create a unique name tag for this pointer. */
902 pi->name_mem_tag = get_nmt_for (ptr);
904 else
906 /* Only pointers that may point to malloc or other variables
907 may receive a name tag. If the pointer does not point to
908 a known spot, we should use type tags. */
909 set_pt_anything (ptr);
910 continue;
913 TREE_THIS_VOLATILE (pi->name_mem_tag)
914 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
916 /* Mark the new name tag for renaming. */
917 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
923 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
924 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
925 name tag and the variables it points-to are call-clobbered. Finally, if
926 P_i escapes and we could not determine where it points to, then all the
927 variables in the same alias set as *P_i are marked call-clobbered. This
928 is necessary because we must assume that P_i may take the address of any
929 variable in the same alias set. */
931 static void
932 compute_flow_sensitive_aliasing (struct alias_info *ai)
934 size_t i;
936 create_name_tags (ai);
938 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
940 unsigned j;
941 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
942 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
943 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
944 bitmap_iterator bi;
946 if (pi->value_escapes_p || pi->pt_anything)
948 /* If PTR escapes or may point to anything, then its associated
949 memory tags and pointed-to variables are call-clobbered. */
950 if (pi->name_mem_tag)
951 mark_call_clobbered (pi->name_mem_tag);
953 if (v_ann->type_mem_tag)
954 mark_call_clobbered (v_ann->type_mem_tag);
956 if (pi->pt_vars)
957 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
959 mark_call_clobbered (referenced_var (j));
963 /* Set up aliasing information for PTR's name memory tag (if it has
964 one). Note that only pointers that have been dereferenced will
965 have a name memory tag. */
966 if (pi->name_mem_tag && pi->pt_vars)
967 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
969 add_may_alias (pi->name_mem_tag, referenced_var (j));
970 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
973 /* If the name tag is call clobbered, so is the type tag
974 associated with the base VAR_DECL. */
975 if (pi->name_mem_tag
976 && v_ann->type_mem_tag
977 && is_call_clobbered (pi->name_mem_tag))
978 mark_call_clobbered (v_ann->type_mem_tag);
983 /* Compute type-based alias sets. Traverse all the pointers and
984 addressable variables found in setup_pointers_and_addressables.
986 For every pointer P in AI->POINTERS and addressable variable V in
987 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
988 memory tag (TMT) if their alias sets conflict. V is then marked as
989 an alias tag so that the operand scanner knows that statements
990 containing V have aliased operands. */
992 static void
993 compute_flow_insensitive_aliasing (struct alias_info *ai)
995 size_t i;
997 /* Initialize counter for the total number of virtual operands that
998 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
999 threshold set by --params max-alias-vops, we enable alias
1000 grouping. */
1001 ai->total_alias_vops = 0;
1003 /* For every pointer P, determine which addressable variables may alias
1004 with P's type memory tag. */
1005 for (i = 0; i < ai->num_pointers; i++)
1007 size_t j;
1008 struct alias_map_d *p_map = ai->pointers[i];
1009 tree tag = var_ann (p_map->var)->type_mem_tag;
1010 var_ann_t tag_ann = var_ann (tag);
1012 p_map->total_alias_vops = 0;
1013 p_map->may_aliases = sbitmap_alloc (num_referenced_vars);
1014 sbitmap_zero (p_map->may_aliases);
1016 for (j = 0; j < ai->num_addressable_vars; j++)
1018 struct alias_map_d *v_map;
1019 var_ann_t v_ann;
1020 tree var;
1021 bool tag_stored_p, var_stored_p;
1023 v_map = ai->addressable_vars[j];
1024 var = v_map->var;
1025 v_ann = var_ann (var);
1027 /* Skip memory tags and variables that have never been
1028 written to. We also need to check if the variables are
1029 call-clobbered because they may be overwritten by
1030 function calls.
1032 Note this is effectively random accessing elements in
1033 the sparse bitset, which can be highly inefficient.
1034 So we first check the call_clobbered status of the
1035 tag and variable before querying the bitmap. */
1036 tag_stored_p = is_call_clobbered (tag)
1037 || bitmap_bit_p (ai->written_vars, tag_ann->uid);
1038 var_stored_p = is_call_clobbered (var)
1039 || bitmap_bit_p (ai->written_vars, v_ann->uid);
1040 if (!tag_stored_p && !var_stored_p)
1041 continue;
1043 if (may_alias_p (p_map->var, p_map->set, var, v_map->set))
1045 subvar_t svars;
1046 size_t num_tag_refs, num_var_refs;
1048 num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1049 num_var_refs = VARRAY_UINT (ai->num_references, v_ann->uid);
1051 /* Add VAR to TAG's may-aliases set. */
1053 /* If this is an aggregate, we may have fake variables for it
1054 that need to be pointed to. */
1055 if (AGGREGATE_TYPE_P (TREE_TYPE (var))
1056 && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE
1057 && (svars = get_subvars_for_var (var)))
1059 subvar_t sv;
1061 for (sv = svars; sv; sv = sv->next)
1062 add_may_alias (tag, sv->var);
1064 else
1066 add_may_alias (tag, var);
1069 /* Update the total number of virtual operands due to
1070 aliasing. Since we are adding one more alias to TAG's
1071 may-aliases set, the total number of virtual operands due
1072 to aliasing will be increased by the number of references
1073 made to VAR and TAG (every reference to TAG will also
1074 count as a reference to VAR). */
1075 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1076 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1078 /* Update the bitmap used to represent TAG's alias set
1079 in case we need to group aliases. */
1080 SET_BIT (p_map->may_aliases, var_ann (var)->uid);
1085 /* Since this analysis is based exclusively on symbols, it fails to
1086 handle cases where two pointers P and Q have different memory
1087 tags with conflicting alias set numbers but no aliased symbols in
1088 common.
1090 For example, suppose that we have two memory tags TMT.1 and TMT.2
1091 such that
1093 may-aliases (TMT.1) = { a }
1094 may-aliases (TMT.2) = { b }
1096 and the alias set number of TMT.1 conflicts with that of TMT.2.
1097 Since they don't have symbols in common, loads and stores from
1098 TMT.1 and TMT.2 will seem independent of each other, which will
1099 lead to the optimizers making invalid transformations (see
1100 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1102 To avoid this problem, we do a final traversal of AI->POINTERS
1103 looking for pairs of pointers that have no aliased symbols in
1104 common and yet have conflicting alias set numbers. */
1105 for (i = 0; i < ai->num_pointers; i++)
1107 size_t j;
1108 struct alias_map_d *p_map1 = ai->pointers[i];
1109 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1110 sbitmap may_aliases1 = p_map1->may_aliases;
1112 for (j = i + 1; j < ai->num_pointers; j++)
1114 struct alias_map_d *p_map2 = ai->pointers[j];
1115 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1116 sbitmap may_aliases2 = p_map2->may_aliases;
1118 /* If the pointers may not point to each other, do nothing. */
1119 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set))
1120 continue;
1122 /* The two pointers may alias each other. If they already have
1123 symbols in common, do nothing. */
1124 if (sbitmap_any_common_bits (may_aliases1, may_aliases2))
1125 continue;
1127 if (sbitmap_first_set_bit (may_aliases2) >= 0)
1129 size_t k;
1131 /* Add all the aliases for TAG2 into TAG1's alias set.
1132 FIXME, update grouping heuristic counters. */
1133 EXECUTE_IF_SET_IN_SBITMAP (may_aliases2, 0, k,
1134 add_may_alias (tag1, referenced_var (k)));
1135 sbitmap_a_or_b (may_aliases1, may_aliases1, may_aliases2);
1137 else
1139 /* Since TAG2 does not have any aliases of its own, add
1140 TAG2 itself to the alias set of TAG1. */
1141 add_may_alias (tag1, tag2);
1146 if (dump_file)
1147 fprintf (dump_file, "%s: Total number of aliased vops: %ld\n",
1148 get_name (current_function_decl),
1149 ai->total_alias_vops);
1151 /* Determine if we need to enable alias grouping. */
1152 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
1153 group_aliases (ai);
1157 /* Comparison function for qsort used in group_aliases. */
1159 static int
1160 total_alias_vops_cmp (const void *p, const void *q)
1162 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1163 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1164 long n1 = (*p1)->total_alias_vops;
1165 long n2 = (*p2)->total_alias_vops;
1167 /* We want to sort in descending order. */
1168 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1171 /* Group all the aliases for TAG to make TAG represent all the
1172 variables in its alias set. Update the total number
1173 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1174 function will make TAG be the unique alias tag for all the
1175 variables in its may-aliases. So, given:
1177 may-aliases(TAG) = { V1, V2, V3 }
1179 This function will group the variables into:
1181 may-aliases(V1) = { TAG }
1182 may-aliases(V2) = { TAG }
1183 may-aliases(V2) = { TAG } */
1185 static void
1186 group_aliases_into (tree tag, sbitmap tag_aliases, struct alias_info *ai)
1188 size_t i;
1189 var_ann_t tag_ann = var_ann (tag);
1190 size_t num_tag_refs = VARRAY_UINT (ai->num_references, tag_ann->uid);
1192 EXECUTE_IF_SET_IN_SBITMAP (tag_aliases, 0, i,
1194 tree var = referenced_var (i);
1195 var_ann_t ann = var_ann (var);
1197 /* Make TAG the unique alias of VAR. */
1198 ann->is_alias_tag = 0;
1199 ann->may_aliases = NULL;
1201 /* Note that VAR and TAG may be the same if the function has no
1202 addressable variables (see the discussion at the end of
1203 setup_pointers_and_addressables). */
1204 if (var != tag)
1205 add_may_alias (var, tag);
1207 /* Reduce total number of virtual operands contributed
1208 by TAG on behalf of VAR. Notice that the references to VAR
1209 itself won't be removed. We will merely replace them with
1210 references to TAG. */
1211 ai->total_alias_vops -= num_tag_refs;
1214 /* We have reduced the number of virtual operands that TAG makes on
1215 behalf of all the variables formerly aliased with it. However,
1216 we have also "removed" all the virtual operands for TAG itself,
1217 so we add them back. */
1218 ai->total_alias_vops += num_tag_refs;
1220 /* TAG no longer has any aliases. */
1221 tag_ann->may_aliases = NULL;
1225 /* Group may-aliases sets to reduce the number of virtual operands due
1226 to aliasing.
1228 1- Sort the list of pointers in decreasing number of contributed
1229 virtual operands.
1231 2- Take the first entry in AI->POINTERS and revert the role of
1232 the memory tag and its aliases. Usually, whenever an aliased
1233 variable Vi is found to alias with a memory tag T, we add Vi
1234 to the may-aliases set for T. Meaning that after alias
1235 analysis, we will have:
1237 may-aliases(T) = { V1, V2, V3, ..., Vn }
1239 This means that every statement that references T, will get 'n'
1240 virtual operands for each of the Vi tags. But, when alias
1241 grouping is enabled, we make T an alias tag and add it to the
1242 alias set of all the Vi variables:
1244 may-aliases(V1) = { T }
1245 may-aliases(V2) = { T }
1247 may-aliases(Vn) = { T }
1249 This has two effects: (a) statements referencing T will only get
1250 a single virtual operand, and, (b) all the variables Vi will now
1251 appear to alias each other. So, we lose alias precision to
1252 improve compile time. But, in theory, a program with such a high
1253 level of aliasing should not be very optimizable in the first
1254 place.
1256 3- Since variables may be in the alias set of more than one
1257 memory tag, the grouping done in step (2) needs to be extended
1258 to all the memory tags that have a non-empty intersection with
1259 the may-aliases set of tag T. For instance, if we originally
1260 had these may-aliases sets:
1262 may-aliases(T) = { V1, V2, V3 }
1263 may-aliases(R) = { V2, V4 }
1265 In step (2) we would have reverted the aliases for T as:
1267 may-aliases(V1) = { T }
1268 may-aliases(V2) = { T }
1269 may-aliases(V3) = { T }
1271 But note that now V2 is no longer aliased with R. We could
1272 add R to may-aliases(V2), but we are in the process of
1273 grouping aliases to reduce virtual operands so what we do is
1274 add V4 to the grouping to obtain:
1276 may-aliases(V1) = { T }
1277 may-aliases(V2) = { T }
1278 may-aliases(V3) = { T }
1279 may-aliases(V4) = { T }
1281 4- If the total number of virtual operands due to aliasing is
1282 still above the threshold set by max-alias-vops, go back to (2). */
1284 static void
1285 group_aliases (struct alias_info *ai)
1287 size_t i;
1289 /* Sort the POINTERS array in descending order of contributed
1290 virtual operands. */
1291 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1292 total_alias_vops_cmp);
1294 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1295 and the tag's may-aliases set. */
1296 for (i = 0; i < ai->num_pointers; i++)
1298 size_t j;
1299 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1300 sbitmap tag1_aliases = ai->pointers[i]->may_aliases;
1302 /* Skip tags that have been grouped already. */
1303 if (ai->pointers[i]->grouped_p)
1304 continue;
1306 /* See if TAG1 had any aliases in common with other type tags.
1307 If we find a TAG2 with common aliases with TAG1, add TAG2's
1308 aliases into TAG1. */
1309 for (j = i + 1; j < ai->num_pointers; j++)
1311 sbitmap tag2_aliases = ai->pointers[j]->may_aliases;
1313 if (sbitmap_any_common_bits (tag1_aliases, tag2_aliases))
1315 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1317 sbitmap_a_or_b (tag1_aliases, tag1_aliases, tag2_aliases);
1319 /* TAG2 does not need its aliases anymore. */
1320 sbitmap_zero (tag2_aliases);
1321 var_ann (tag2)->may_aliases = NULL;
1323 /* TAG1 is the unique alias of TAG2. */
1324 add_may_alias (tag2, tag1);
1326 ai->pointers[j]->grouped_p = true;
1330 /* Now group all the aliases we collected into TAG1. */
1331 group_aliases_into (tag1, tag1_aliases, ai);
1333 /* If we've reduced total number of virtual operands below the
1334 threshold, stop. */
1335 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1336 break;
1339 /* Finally, all the variables that have been grouped cannot be in
1340 the may-alias set of name memory tags. Suppose that we have
1341 grouped the aliases in this code so that may-aliases(a) = TMT.20
1343 p_5 = &a;
1345 # a_9 = V_MAY_DEF <a_8>
1346 p_5->field = 0
1347 ... Several modifications to TMT.20 ...
1348 # VUSE <a_9>
1349 x_30 = p_5->field
1351 Since p_5 points to 'a', the optimizers will try to propagate 0
1352 into p_5->field, but that is wrong because there have been
1353 modifications to 'TMT.20' in between. To prevent this we have to
1354 replace 'a' with 'TMT.20' in the name tag of p_5. */
1355 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1357 size_t j;
1358 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1359 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1360 varray_type aliases;
1362 if (name_tag == NULL_TREE)
1363 continue;
1365 aliases = var_ann (name_tag)->may_aliases;
1366 for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1368 tree alias = VARRAY_TREE (aliases, j);
1369 var_ann_t ann = var_ann (alias);
1371 if ((ann->mem_tag_kind == NOT_A_TAG
1372 || ann->mem_tag_kind == STRUCT_FIELD)
1373 && ann->may_aliases)
1375 tree new_alias;
1377 gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1379 new_alias = VARRAY_TREE (ann->may_aliases, 0);
1380 replace_may_alias (name_tag, j, new_alias);
1385 if (dump_file)
1386 fprintf (dump_file,
1387 "%s: Total number of aliased vops after grouping: %ld%s\n",
1388 get_name (current_function_decl),
1389 ai->total_alias_vops,
1390 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1394 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1396 static void
1397 create_alias_map_for (tree var, struct alias_info *ai)
1399 struct alias_map_d *alias_map;
1400 alias_map = xcalloc (1, sizeof (*alias_map));
1401 alias_map->var = var;
1402 alias_map->set = get_alias_set (var);
1403 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1407 /* Create memory tags for all the dereferenced pointers and build the
1408 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1409 sets. Based on the address escape and points-to information collected
1410 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1411 variables whose address is not needed anymore. */
1413 static void
1414 setup_pointers_and_addressables (struct alias_info *ai)
1416 size_t i, n_vars, num_addressable_vars, num_pointers;
1418 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1419 num_addressable_vars = num_pointers = 0;
1420 for (i = 0; i < num_referenced_vars; i++)
1422 tree var = referenced_var (i);
1424 if (may_be_aliased (var))
1425 num_addressable_vars++;
1427 if (POINTER_TYPE_P (TREE_TYPE (var)))
1429 /* Since we don't keep track of volatile variables, assume that
1430 these pointers are used in indirect store operations. */
1431 if (TREE_THIS_VOLATILE (var))
1432 bitmap_set_bit (ai->dereferenced_ptrs_store, var_ann (var)->uid);
1434 num_pointers++;
1438 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1439 always going to be slightly bigger than we actually need them
1440 because some TREE_ADDRESSABLE variables will be marked
1441 non-addressable below and only pointers with unique type tags are
1442 going to be added to POINTERS. */
1443 ai->addressable_vars = xcalloc (num_addressable_vars,
1444 sizeof (struct alias_map_d *));
1445 ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1446 ai->num_addressable_vars = 0;
1447 ai->num_pointers = 0;
1449 /* Since we will be creating type memory tags within this loop, cache the
1450 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1451 unnecessarily. */
1452 n_vars = num_referenced_vars;
1454 for (i = 0; i < n_vars; i++)
1456 tree var = referenced_var (i);
1457 var_ann_t v_ann = var_ann (var);
1458 subvar_t svars;
1460 /* Name memory tags already have flow-sensitive aliasing
1461 information, so they need not be processed by
1462 compute_flow_insensitive_aliasing. Similarly, type memory
1463 tags are already accounted for when we process their
1464 associated pointer.
1466 Structure fields, on the other hand, have to have some of this
1467 information processed for them, but it's pointless to mark them
1468 non-addressable (since they are fake variables anyway). */
1469 if (v_ann->mem_tag_kind != NOT_A_TAG
1470 && v_ann->mem_tag_kind != STRUCT_FIELD)
1471 continue;
1473 /* Remove the ADDRESSABLE flag from every addressable variable whose
1474 address is not needed anymore. This is caused by the propagation
1475 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1476 removal of dead pointer assignments done by the early scalar
1477 cleanup passes. */
1478 if (TREE_ADDRESSABLE (var) && v_ann->mem_tag_kind != STRUCT_FIELD)
1480 if (!bitmap_bit_p (ai->addresses_needed, v_ann->uid)
1481 && TREE_CODE (var) != RESULT_DECL
1482 && !is_global_var (var))
1484 bool okay_to_mark = true;
1485 /* Since VAR is now a regular GIMPLE register, we will need
1486 to rename VAR into SSA afterwards. */
1487 bitmap_set_bit (vars_to_rename, v_ann->uid);
1489 if (AGGREGATE_TYPE_P (TREE_TYPE (var))
1490 && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE
1491 && (svars = get_subvars_for_var (var)))
1493 subvar_t sv;
1495 for (sv = svars; sv; sv = sv->next)
1497 var_ann_t svann = var_ann (sv->var);
1498 if (bitmap_bit_p (ai->addresses_needed, svann->uid))
1499 okay_to_mark = false;
1500 bitmap_set_bit (vars_to_rename, svann->uid);
1503 /* The address of VAR is not needed, remove the
1504 addressable bit, so that it can be optimized as a
1505 regular variable. */
1506 if (okay_to_mark)
1507 mark_non_addressable (var);
1510 else
1512 /* Add the variable to the set of addressables. Mostly
1513 used when scanning operands for ASM_EXPRs that
1514 clobber memory. In those cases, we need to clobber
1515 all call-clobbered variables and all addressables. */
1516 bitmap_set_bit (addressable_vars, v_ann->uid);
1517 if (AGGREGATE_TYPE_P (TREE_TYPE (var))
1518 && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE
1519 && (svars = get_subvars_for_var (var)))
1521 subvar_t sv;
1522 for (sv = svars; sv; sv = sv->next)
1523 bitmap_set_bit (addressable_vars, var_ann (sv->var)->uid);
1529 /* Global variables and addressable locals may be aliased. Create an
1530 entry in ADDRESSABLE_VARS for VAR. */
1531 if (may_be_aliased (var))
1533 create_alias_map_for (var, ai);
1534 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1537 /* Add pointer variables that have been dereferenced to the POINTERS
1538 array and create a type memory tag for them. */
1539 if (POINTER_TYPE_P (TREE_TYPE (var)))
1541 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid)
1542 || bitmap_bit_p (ai->dereferenced_ptrs_load, v_ann->uid)))
1544 tree tag;
1545 var_ann_t t_ann;
1547 /* If pointer VAR still doesn't have a memory tag
1548 associated with it, create it now or re-use an
1549 existing one. */
1550 tag = get_tmt_for (var, ai);
1551 t_ann = var_ann (tag);
1553 /* The type tag will need to be renamed into SSA
1554 afterwards. Note that we cannot do this inside
1555 get_tmt_for because aliasing may run multiple times
1556 and we only create type tags the first time. */
1557 bitmap_set_bit (vars_to_rename, t_ann->uid);
1559 /* Associate the tag with pointer VAR. */
1560 v_ann->type_mem_tag = tag;
1562 /* If pointer VAR has been used in a store operation,
1563 then its memory tag must be marked as written-to. */
1564 if (bitmap_bit_p (ai->dereferenced_ptrs_store, v_ann->uid))
1565 bitmap_set_bit (ai->written_vars, t_ann->uid);
1567 /* If pointer VAR is a global variable or a PARM_DECL,
1568 then its memory tag should be considered a global
1569 variable. */
1570 if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1571 mark_call_clobbered (tag);
1573 /* All the dereferences of pointer VAR count as
1574 references of TAG. Since TAG can be associated with
1575 several pointers, add the dereferences of VAR to the
1576 TAG. We may need to grow AI->NUM_REFERENCES because
1577 we have been adding name and type tags. */
1578 if (t_ann->uid >= VARRAY_SIZE (ai->num_references))
1579 VARRAY_GROW (ai->num_references, t_ann->uid + 10);
1581 VARRAY_UINT (ai->num_references, t_ann->uid)
1582 += VARRAY_UINT (ai->num_references, v_ann->uid);
1584 else
1586 /* The pointer has not been dereferenced. If it had a
1587 type memory tag, remove it and mark the old tag for
1588 renaming to remove it out of the IL. */
1589 var_ann_t ann = var_ann (var);
1590 tree tag = ann->type_mem_tag;
1591 if (tag)
1593 bitmap_set_bit (vars_to_rename, var_ann (tag)->uid);
1594 ann->type_mem_tag = NULL_TREE;
1602 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1603 every call site, we need to emit V_MAY_DEF expressions to represent the
1604 clobbering effects of the call for variables whose address escapes the
1605 current function.
1607 One approach is to group all call-clobbered variables into a single
1608 representative that is used as an alias of every call-clobbered variable
1609 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1610 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1612 The second approach is to emit a clobbering V_MAY_DEF for every
1613 call-clobbered variable at call sites. This is the preferred way in terms
1614 of optimization opportunities but it may create too many V_MAY_DEF operands
1615 if there are many call clobbered variables and function calls in the
1616 function.
1618 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1619 function calls found by the number of call-clobbered variables. If that
1620 product is beyond a certain threshold, as determined by the parameterized
1621 values shown below, we use .GLOBAL_VAR.
1623 FIXME. This heuristic should be improved. One idea is to use several
1624 .GLOBAL_VARs of different types instead of a single one. The thresholds
1625 have been derived from a typical bootstrap cycle, including all target
1626 libraries. Compile times were found increase by ~1% compared to using
1627 .GLOBAL_VAR. */
1629 static void
1630 maybe_create_global_var (struct alias_info *ai)
1632 unsigned i, n_clobbered;
1633 bitmap_iterator bi;
1635 /* No need to create it, if we have one already. */
1636 if (global_var == NULL_TREE)
1638 /* Count all the call-clobbered variables. */
1639 n_clobbered = 0;
1640 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1642 n_clobbered++;
1645 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD)
1646 create_global_var ();
1649 /* If the function has calls to clobbering functions and .GLOBAL_VAR has
1650 been created, make it an alias for all call-clobbered variables. */
1651 if (global_var)
1652 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1654 tree var = referenced_var (i);
1655 if (var != global_var)
1657 subvar_t svars;
1658 add_may_alias (var, global_var);
1659 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1660 if (AGGREGATE_TYPE_P (TREE_TYPE (var))
1661 && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE
1662 && (svars = get_subvars_for_var (var)))
1664 subvar_t sv;
1665 for (sv = svars; sv; sv = sv->next)
1666 bitmap_set_bit (vars_to_rename, var_ann (sv->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 v_ann, 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 v_ann = var_ann (var);
1702 m_ann = var_ann (mem);
1704 gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1706 alias_stats.tbaa_queries++;
1708 /* If VAR is a pointer with the same alias set as PTR, then dereferencing
1709 PTR can't possibly affect VAR. Note, that we are specifically testing
1710 for PTR's alias set here, not its pointed-to type. We also can't
1711 do this check with relaxed aliasing enabled. */
1712 if (POINTER_TYPE_P (TREE_TYPE (var))
1713 && var_alias_set != 0
1714 && mem_alias_set != 0)
1716 HOST_WIDE_INT ptr_alias_set = get_alias_set (ptr);
1717 if (ptr_alias_set == var_alias_set)
1719 alias_stats.alias_noalias++;
1720 alias_stats.tbaa_resolved++;
1721 return false;
1725 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1726 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1728 alias_stats.alias_noalias++;
1729 alias_stats.tbaa_resolved++;
1730 return false;
1732 alias_stats.alias_mayalias++;
1733 return true;
1737 /* Add ALIAS to the set of variables that may alias VAR. */
1739 static void
1740 add_may_alias (tree var, tree alias)
1742 size_t i;
1743 var_ann_t v_ann = get_var_ann (var);
1744 var_ann_t a_ann = get_var_ann (alias);
1746 gcc_assert (var != alias);
1748 if (v_ann->may_aliases == NULL)
1749 VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1751 /* Avoid adding duplicates. */
1752 for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1753 if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1754 return;
1756 /* If VAR is a call-clobbered variable, so is its new ALIAS.
1757 FIXME, call-clobbering should only depend on whether an address
1758 escapes. It should be independent of aliasing. */
1759 if (is_call_clobbered (var))
1760 mark_call_clobbered (alias);
1762 /* Likewise. If ALIAS is call-clobbered, so is VAR. */
1763 else if (is_call_clobbered (alias))
1764 mark_call_clobbered (var);
1766 VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1767 a_ann->is_alias_tag = 1;
1771 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1773 static void
1774 replace_may_alias (tree var, size_t i, tree new_alias)
1776 var_ann_t v_ann = var_ann (var);
1777 VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1779 /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1780 FIXME, call-clobbering should only depend on whether an address
1781 escapes. It should be independent of aliasing. */
1782 if (is_call_clobbered (var))
1783 mark_call_clobbered (new_alias);
1785 /* Likewise. If NEW_ALIAS is call-clobbered, so is VAR. */
1786 else if (is_call_clobbered (new_alias))
1787 mark_call_clobbered (var);
1791 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1793 static void
1794 set_pt_anything (tree ptr)
1796 struct ptr_info_def *pi = get_ptr_info (ptr);
1798 pi->pt_anything = 1;
1799 pi->pt_malloc = 0;
1801 /* The pointer used to have a name tag, but we now found it pointing
1802 to an arbitrary location. The name tag needs to be renamed and
1803 disassociated from PTR. */
1804 if (pi->name_mem_tag)
1806 bitmap_set_bit (vars_to_rename, var_ann (pi->name_mem_tag)->uid);
1807 pi->name_mem_tag = NULL_TREE;
1812 /* Mark pointer PTR as pointing to a malloc'd memory area. */
1814 static void
1815 set_pt_malloc (tree ptr)
1817 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
1819 /* If the pointer has already been found to point to arbitrary
1820 memory locations, it is unsafe to mark it as pointing to malloc. */
1821 if (pi->pt_anything)
1822 return;
1824 pi->pt_malloc = 1;
1828 /* Given two different pointers DEST and ORIG. Merge the points-to
1829 information in ORIG into DEST. AI is as in
1830 collect_points_to_info. */
1832 static void
1833 merge_pointed_to_info (struct alias_info *ai, tree dest, tree orig)
1835 struct ptr_info_def *dest_pi, *orig_pi;
1837 gcc_assert (dest != orig);
1839 /* Make sure we have points-to information for ORIG. */
1840 collect_points_to_info_for (ai, orig);
1842 dest_pi = get_ptr_info (dest);
1843 orig_pi = SSA_NAME_PTR_INFO (orig);
1845 if (orig_pi)
1847 gcc_assert (orig_pi != dest_pi);
1849 /* Notice that we never merge PT_MALLOC. This attribute is only
1850 true if the pointer is the result of a malloc() call.
1851 Otherwise, we can end up in this situation:
1853 P_i = malloc ();
1855 P_j = P_i + X;
1857 P_j would be marked as PT_MALLOC, however we currently do not
1858 handle cases of more than one pointer pointing to the same
1859 malloc'd area.
1861 FIXME: If the merging comes from an expression that preserves
1862 the PT_MALLOC attribute (copy assignment, address
1863 arithmetic), we ought to merge PT_MALLOC, but then both
1864 pointers would end up getting different name tags because
1865 create_name_tags is not smart enough to determine that the
1866 two come from the same malloc call. Copy propagation before
1867 aliasing should cure this. */
1868 dest_pi->pt_malloc = 0;
1869 if (orig_pi->pt_malloc || orig_pi->pt_anything)
1870 set_pt_anything (dest);
1872 dest_pi->pt_null |= orig_pi->pt_null;
1874 if (!dest_pi->pt_anything
1875 && orig_pi->pt_vars
1876 && !bitmap_empty_p (orig_pi->pt_vars))
1878 if (dest_pi->pt_vars == NULL)
1880 dest_pi->pt_vars = BITMAP_GGC_ALLOC ();
1881 bitmap_copy (dest_pi->pt_vars, orig_pi->pt_vars);
1883 else
1884 bitmap_ior_into (dest_pi->pt_vars, orig_pi->pt_vars);
1887 else
1888 set_pt_anything (dest);
1892 /* Add EXPR to the list of expressions pointed-to by PTR. */
1894 static void
1895 add_pointed_to_expr (struct alias_info *ai, tree ptr, tree expr)
1897 if (TREE_CODE (expr) == WITH_SIZE_EXPR)
1898 expr = TREE_OPERAND (expr, 0);
1900 get_ptr_info (ptr);
1902 if (TREE_CODE (expr) == CALL_EXPR
1903 && (call_expr_flags (expr) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
1905 /* If EXPR is a malloc-like call, then the area pointed to PTR
1906 is guaranteed to not alias with anything else. */
1907 set_pt_malloc (ptr);
1909 else if (TREE_CODE (expr) == ADDR_EXPR)
1911 /* Found P_i = ADDR_EXPR */
1912 add_pointed_to_var (ai, ptr, expr);
1914 else if (TREE_CODE (expr) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (expr)))
1916 /* Found P_i = Q_j. */
1917 merge_pointed_to_info (ai, ptr, expr);
1919 else if (TREE_CODE (expr) == PLUS_EXPR || TREE_CODE (expr) == MINUS_EXPR)
1921 /* Found P_i = PLUS_EXPR or P_i = MINUS_EXPR */
1922 tree op0 = TREE_OPERAND (expr, 0);
1923 tree op1 = TREE_OPERAND (expr, 1);
1925 /* Both operands may be of pointer type. FIXME: Shouldn't
1926 we just expect PTR + OFFSET always? */
1927 if (POINTER_TYPE_P (TREE_TYPE (op0))
1928 && TREE_CODE (op0) != INTEGER_CST)
1930 if (TREE_CODE (op0) == SSA_NAME)
1931 merge_pointed_to_info (ai, ptr, op0);
1932 else if (TREE_CODE (op0) == ADDR_EXPR)
1933 add_pointed_to_var (ai, ptr, op0);
1934 else
1935 set_pt_anything (ptr);
1938 if (POINTER_TYPE_P (TREE_TYPE (op1))
1939 && TREE_CODE (op1) != INTEGER_CST)
1941 if (TREE_CODE (op1) == SSA_NAME)
1942 merge_pointed_to_info (ai, ptr, op1);
1943 else if (TREE_CODE (op1) == ADDR_EXPR)
1944 add_pointed_to_var (ai, ptr, op1);
1945 else
1946 set_pt_anything (ptr);
1949 /* Neither operand is a pointer? VAR can be pointing anywhere.
1950 FIXME: Shouldn't we abort here? If we get here, we found
1951 PTR = INT_CST + INT_CST, which should not be a valid pointer
1952 expression. */
1953 if (!(POINTER_TYPE_P (TREE_TYPE (op0))
1954 && TREE_CODE (op0) != INTEGER_CST)
1955 && !(POINTER_TYPE_P (TREE_TYPE (op1))
1956 && TREE_CODE (op1) != INTEGER_CST))
1957 set_pt_anything (ptr);
1959 else if (integer_zerop (expr))
1961 /* EXPR is the NULL pointer. Mark PTR as pointing to NULL. */
1962 SSA_NAME_PTR_INFO (ptr)->pt_null = 1;
1964 else
1966 /* If we can't recognize the expression, assume that PTR may
1967 point anywhere. */
1968 set_pt_anything (ptr);
1973 /* If VALUE is of the form &DECL, add DECL to the set of variables
1974 pointed-to by PTR. Otherwise, add VALUE as a pointed-to expression by
1975 PTR. AI is as in collect_points_to_info. */
1977 static void
1978 add_pointed_to_var (struct alias_info *ai, tree ptr, tree value)
1980 struct ptr_info_def *pi = get_ptr_info (ptr);
1981 tree pt_var = NULL_TREE;
1982 HOST_WIDE_INT offset, size;
1983 tree addrop;
1984 size_t uid;
1985 tree ref;
1986 subvar_t svars;
1988 gcc_assert (TREE_CODE (value) == ADDR_EXPR);
1990 addrop = TREE_OPERAND (value, 0);
1991 if (REFERENCE_CLASS_P (addrop))
1992 pt_var = get_base_address (addrop);
1993 else
1994 pt_var = addrop;
1996 /* If this is a component_ref, see if we can get a smaller number of
1997 variables to take the address of. */
1998 if (TREE_CODE (addrop) == COMPONENT_REF
1999 && (ref = okay_component_ref_for_subvars (addrop, &offset ,&size)))
2001 subvar_t sv;
2002 svars = get_subvars_for_var (ref);
2004 uid = var_ann (pt_var)->uid;
2005 bitmap_set_bit (ai->addresses_needed, uid);
2006 if (pi->pt_vars == NULL)
2007 pi->pt_vars = BITMAP_GGC_ALLOC ();
2008 /* If the variable is a global, mark the pointer as pointing to
2009 global memory (which will make its tag a global variable). */
2010 if (is_global_var (pt_var))
2011 pi->pt_global_mem = 1;
2013 for (sv = svars; sv; sv = sv->next)
2015 if (offset == sv->offset && size == sv->size)
2016 bitmap_set_bit (pi->pt_vars, var_ann (sv->var)->uid);
2017 else if (offset >= sv->offset && offset < (sv->offset + sv->size))
2018 bitmap_set_bit (pi->pt_vars, var_ann (sv->var)->uid);
2019 else if (offset < sv->offset
2020 && (offset + size > sv->offset))
2021 bitmap_set_bit (pi->pt_vars, var_ann (sv->var)->uid);
2024 else if (pt_var && SSA_VAR_P (pt_var))
2027 uid = var_ann (pt_var)->uid;
2028 bitmap_set_bit (ai->addresses_needed, uid);
2030 if (pi->pt_vars == NULL)
2031 pi->pt_vars = BITMAP_GGC_ALLOC ();
2033 /* If this is an aggregate, we may have fake variables for it that need
2034 to be pointed to. */
2035 if (AGGREGATE_TYPE_P (TREE_TYPE (pt_var))
2036 && TREE_CODE (TREE_TYPE (pt_var)) != ARRAY_TYPE
2037 && (svars = get_subvars_for_var (pt_var)))
2039 subvar_t sv;
2040 for (sv = svars; sv; sv = sv->next)
2042 uid = var_ann (sv->var)->uid;
2043 bitmap_set_bit (ai->addresses_needed, uid);
2044 bitmap_set_bit (pi->pt_vars, uid);
2047 else
2048 bitmap_set_bit (pi->pt_vars, uid);
2050 /* If the variable is a global, mark the pointer as pointing to
2051 global memory (which will make its tag a global variable). */
2052 if (is_global_var (pt_var))
2053 pi->pt_global_mem = 1;
2058 /* Callback for walk_use_def_chains to gather points-to information from the
2059 SSA web.
2061 VAR is an SSA variable or a GIMPLE expression.
2063 STMT is the statement that generates the SSA variable or, if STMT is a
2064 PHI_NODE, VAR is one of the PHI arguments.
2066 DATA is a pointer to a structure of type ALIAS_INFO. */
2068 static bool
2069 collect_points_to_info_r (tree var, tree stmt, void *data)
2071 struct alias_info *ai = (struct alias_info *) data;
2073 if (dump_file && (dump_flags & TDF_DETAILS))
2075 fprintf (dump_file, "Visiting use-def links for ");
2076 print_generic_expr (dump_file, var, dump_flags);
2077 fprintf (dump_file, "\n");
2080 switch (TREE_CODE (stmt))
2082 case RETURN_EXPR:
2083 if (TREE_CODE (TREE_OPERAND (stmt, 0)) != MODIFY_EXPR)
2084 abort ();
2085 stmt = TREE_OPERAND (stmt, 0);
2086 /* FALLTHRU */
2088 case MODIFY_EXPR:
2090 tree rhs = TREE_OPERAND (stmt, 1);
2091 STRIP_NOPS (rhs);
2092 add_pointed_to_expr (ai, var, rhs);
2093 break;
2096 case ASM_EXPR:
2097 /* Pointers defined by __asm__ statements can point anywhere. */
2098 set_pt_anything (var);
2099 break;
2101 case NOP_EXPR:
2102 if (IS_EMPTY_STMT (stmt))
2104 tree decl = SSA_NAME_VAR (var);
2106 if (TREE_CODE (decl) == PARM_DECL)
2107 add_pointed_to_expr (ai, var, decl);
2108 else if (DECL_INITIAL (decl))
2109 add_pointed_to_expr (ai, var, DECL_INITIAL (decl));
2110 else
2111 add_pointed_to_expr (ai, var, decl);
2113 break;
2115 case PHI_NODE:
2117 /* It STMT is a PHI node, then VAR is one of its arguments. The
2118 variable that we are analyzing is the LHS of the PHI node. */
2119 tree lhs = PHI_RESULT (stmt);
2121 switch (TREE_CODE (var))
2123 case ADDR_EXPR:
2124 add_pointed_to_var (ai, lhs, var);
2125 break;
2127 case SSA_NAME:
2128 /* Avoid unnecessary merges. */
2129 if (lhs != var)
2130 merge_pointed_to_info (ai, lhs, var);
2131 break;
2133 default:
2134 gcc_assert (is_gimple_min_invariant (var));
2135 add_pointed_to_expr (ai, lhs, var);
2136 break;
2138 break;
2141 default:
2142 gcc_unreachable ();
2145 return false;
2149 /* Return true if STMT is an "escape" site from the current function. Escape
2150 sites those statements which might expose the address of a variable
2151 outside the current function. STMT is an escape site iff:
2153 1- STMT is a function call, or
2154 2- STMT is an __asm__ expression, or
2155 3- STMT is an assignment to a non-local variable, or
2156 4- STMT is a return statement.
2158 If NUM_CALLS_P is not NULL, the counter is incremented if STMT contains
2159 a function call. */
2161 static bool
2162 is_escape_site (tree stmt, size_t *num_calls_p)
2164 if (get_call_expr_in (stmt) != NULL_TREE)
2166 if (num_calls_p)
2167 (*num_calls_p)++;
2169 return true;
2171 else if (TREE_CODE (stmt) == ASM_EXPR)
2172 return true;
2173 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2175 tree lhs = TREE_OPERAND (stmt, 0);
2177 /* Get to the base of _REF nodes. */
2178 if (TREE_CODE (lhs) != SSA_NAME)
2179 lhs = get_base_address (lhs);
2181 /* If we couldn't recognize the LHS of the assignment, assume that it
2182 is a non-local store. */
2183 if (lhs == NULL_TREE)
2184 return true;
2186 /* If the RHS is a conversion between a pointer and an integer, the
2187 pointer escapes since we can't track the integer. */
2188 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
2189 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
2190 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
2191 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
2192 (TREE_OPERAND (stmt, 1), 0)))
2193 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
2194 return true;
2196 /* If the LHS is an SSA name, it can't possibly represent a non-local
2197 memory store. */
2198 if (TREE_CODE (lhs) == SSA_NAME)
2199 return false;
2201 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
2202 local variables we cannot be sure if it will escape, because we
2203 don't have information about objects not in SSA form. Need to
2204 implement something along the lines of
2206 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
2207 Midkiff, ``Escape analysis for java,'' in Proceedings of the
2208 Conference on Object-Oriented Programming Systems, Languages, and
2209 Applications (OOPSLA), pp. 1-19, 1999. */
2210 return true;
2212 else if (TREE_CODE (stmt) == RETURN_EXPR)
2213 return true;
2215 return false;
2219 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
2220 is considered to represent all the pointers whose pointed-to types are
2221 in the same alias set class. Otherwise, the tag represents a single
2222 SSA_NAME pointer variable. */
2224 static tree
2225 create_memory_tag (tree type, bool is_type_tag)
2227 var_ann_t ann;
2228 tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
2230 /* By default, memory tags are local variables. Alias analysis will
2231 determine whether they should be considered globals. */
2232 DECL_CONTEXT (tag) = current_function_decl;
2234 /* Memory tags are by definition addressable. This also prevents
2235 is_gimple_ref frome confusing memory tags with optimizable
2236 variables. */
2237 TREE_ADDRESSABLE (tag) = 1;
2239 ann = get_var_ann (tag);
2240 ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
2241 ann->type_mem_tag = NULL_TREE;
2243 /* Add the tag to the symbol table. */
2244 add_referenced_tmp_var (tag);
2246 return tag;
2250 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2251 This is used if P_i has been found to point to a specific set of
2252 variables or to a non-aliased memory location like the address returned
2253 by malloc functions. */
2255 static tree
2256 get_nmt_for (tree ptr)
2258 struct ptr_info_def *pi = get_ptr_info (ptr);
2259 tree tag = pi->name_mem_tag;
2261 if (tag == NULL_TREE)
2262 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2264 /* If PTR is a PARM_DECL, it points to a global variable or malloc,
2265 then its name tag should be considered a global variable. */
2266 if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
2267 || pi->pt_malloc
2268 || pi->pt_global_mem)
2269 mark_call_clobbered (tag);
2271 return tag;
2275 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2276 artificial variable that represents the memory location pointed-to by
2277 PTR. It is used to model the effects of pointer de-references on
2278 addressable variables.
2280 AI points to the data gathered during alias analysis. This function
2281 populates the array AI->POINTERS. */
2283 static tree
2284 get_tmt_for (tree ptr, struct alias_info *ai)
2286 size_t i;
2287 tree tag;
2288 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2289 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2291 /* To avoid creating unnecessary memory tags, only create one memory tag
2292 per alias set class. Note that it may be tempting to group
2293 memory tags based on conflicting alias sets instead of
2294 equivalence. That would be wrong because alias sets are not
2295 necessarily transitive (as demonstrated by the libstdc++ test
2296 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2297 such that conflicts (A, B) == true and conflicts (A, C) == true,
2298 it does not necessarily follow that conflicts (B, C) == true. */
2299 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2301 struct alias_map_d *curr = ai->pointers[i];
2302 if (tag_set == curr->set)
2304 tag = var_ann (curr->var)->type_mem_tag;
2305 break;
2309 /* If VAR cannot alias with any of the existing memory tags, create a new
2310 tag for PTR and add it to the POINTERS array. */
2311 if (tag == NULL_TREE)
2313 struct alias_map_d *alias_map;
2315 /* If PTR did not have a type tag already, create a new TMT.*
2316 artificial variable representing the memory location
2317 pointed-to by PTR. */
2318 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2319 tag = create_memory_tag (tag_type, true);
2320 else
2321 tag = var_ann (ptr)->type_mem_tag;
2323 /* Add PTR to the POINTERS array. Note that we are not interested in
2324 PTR's alias set. Instead, we cache the alias set for the memory that
2325 PTR points to. */
2326 alias_map = xcalloc (1, sizeof (*alias_map));
2327 alias_map->var = ptr;
2328 alias_map->set = tag_set;
2329 ai->pointers[ai->num_pointers++] = alias_map;
2332 /* If the pointed-to type is volatile, so is the tag. */
2333 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2335 /* Make sure that the type tag has the same alias set as the
2336 pointed-to type. */
2337 gcc_assert (tag_set == get_alias_set (tag));
2339 return tag;
2343 /* Create GLOBAL_VAR, an artificial global variable to act as a
2344 representative of all the variables that may be clobbered by function
2345 calls. */
2347 static void
2348 create_global_var (void)
2350 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2351 void_type_node);
2352 DECL_ARTIFICIAL (global_var) = 1;
2353 TREE_READONLY (global_var) = 0;
2354 DECL_EXTERNAL (global_var) = 1;
2355 TREE_STATIC (global_var) = 1;
2356 TREE_USED (global_var) = 1;
2357 DECL_CONTEXT (global_var) = NULL_TREE;
2358 TREE_THIS_VOLATILE (global_var) = 0;
2359 TREE_ADDRESSABLE (global_var) = 0;
2361 add_referenced_tmp_var (global_var);
2362 bitmap_set_bit (vars_to_rename, var_ann (global_var)->uid);
2366 /* Dump alias statistics on FILE. */
2368 static void
2369 dump_alias_stats (FILE *file)
2371 const char *funcname
2372 = lang_hooks.decl_printable_name (current_function_decl, 2);
2373 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2374 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2375 fprintf (file, "Total alias mayalias results:\t%u\n",
2376 alias_stats.alias_mayalias);
2377 fprintf (file, "Total alias noalias results:\t%u\n",
2378 alias_stats.alias_noalias);
2379 fprintf (file, "Total simple queries:\t%u\n",
2380 alias_stats.simple_queries);
2381 fprintf (file, "Total simple resolved:\t%u\n",
2382 alias_stats.simple_resolved);
2383 fprintf (file, "Total TBAA queries:\t%u\n",
2384 alias_stats.tbaa_queries);
2385 fprintf (file, "Total TBAA resolved:\t%u\n",
2386 alias_stats.tbaa_resolved);
2390 /* Dump alias information on FILE. */
2392 void
2393 dump_alias_info (FILE *file)
2395 size_t i;
2396 const char *funcname
2397 = lang_hooks.decl_printable_name (current_function_decl, 2);
2399 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2401 fprintf (file, "Aliased symbols\n\n");
2402 for (i = 0; i < num_referenced_vars; i++)
2404 tree var = referenced_var (i);
2405 if (may_be_aliased (var))
2406 dump_variable (file, var);
2409 fprintf (file, "\nDereferenced pointers\n\n");
2410 for (i = 0; i < num_referenced_vars; i++)
2412 tree var = referenced_var (i);
2413 var_ann_t ann = var_ann (var);
2414 if (ann->type_mem_tag)
2415 dump_variable (file, var);
2418 fprintf (file, "\nType memory tags\n\n");
2419 for (i = 0; i < num_referenced_vars; i++)
2421 tree var = referenced_var (i);
2422 var_ann_t ann = var_ann (var);
2423 if (ann->mem_tag_kind == TYPE_TAG)
2424 dump_variable (file, var);
2427 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2429 fprintf (file, "SSA_NAME pointers\n\n");
2430 for (i = 1; i < num_ssa_names; i++)
2432 tree ptr = ssa_name (i);
2433 struct ptr_info_def *pi;
2435 if (ptr == NULL_TREE)
2436 continue;
2438 pi = SSA_NAME_PTR_INFO (ptr);
2439 if (!SSA_NAME_IN_FREE_LIST (ptr)
2440 && pi
2441 && pi->name_mem_tag)
2442 dump_points_to_info_for (file, ptr);
2445 fprintf (file, "\nName memory tags\n\n");
2446 for (i = 0; i < num_referenced_vars; i++)
2448 tree var = referenced_var (i);
2449 var_ann_t ann = var_ann (var);
2450 if (ann->mem_tag_kind == NAME_TAG)
2451 dump_variable (file, var);
2454 fprintf (file, "\n");
2458 /* Dump alias information on stderr. */
2460 void
2461 debug_alias_info (void)
2463 dump_alias_info (stderr);
2467 /* Return the alias information associated with pointer T. It creates a
2468 new instance if none existed. */
2470 struct ptr_info_def *
2471 get_ptr_info (tree t)
2473 struct ptr_info_def *pi;
2475 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2477 pi = SSA_NAME_PTR_INFO (t);
2478 if (pi == NULL)
2480 pi = ggc_alloc (sizeof (*pi));
2481 memset ((void *)pi, 0, sizeof (*pi));
2482 SSA_NAME_PTR_INFO (t) = pi;
2485 return pi;
2489 /* Dump points-to information for SSA_NAME PTR into FILE. */
2491 void
2492 dump_points_to_info_for (FILE *file, tree ptr)
2494 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2496 print_generic_expr (file, ptr, dump_flags);
2498 if (pi)
2500 if (pi->name_mem_tag)
2502 fprintf (file, ", name memory tag: ");
2503 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2506 if (pi->is_dereferenced)
2507 fprintf (file, ", is dereferenced");
2509 if (pi->value_escapes_p)
2510 fprintf (file, ", its value escapes");
2512 if (pi->pt_anything)
2513 fprintf (file, ", points-to anything");
2515 if (pi->pt_malloc)
2516 fprintf (file, ", points-to malloc");
2518 if (pi->pt_null)
2519 fprintf (file, ", points-to NULL");
2521 if (pi->pt_vars)
2523 unsigned ix;
2524 bitmap_iterator bi;
2526 fprintf (file, ", points-to vars: { ");
2527 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2529 print_generic_expr (file, referenced_var (ix), dump_flags);
2530 fprintf (file, " ");
2532 fprintf (file, "}");
2536 fprintf (file, "\n");
2540 /* Dump points-to information for VAR into stderr. */
2542 void
2543 debug_points_to_info_for (tree var)
2545 dump_points_to_info_for (stderr, var);
2549 /* Dump points-to information into FILE. NOTE: This function is slow, as
2550 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2552 void
2553 dump_points_to_info (FILE *file)
2555 basic_block bb;
2556 block_stmt_iterator si;
2557 size_t i;
2558 ssa_op_iter iter;
2559 const char *fname =
2560 lang_hooks.decl_printable_name (current_function_decl, 2);
2562 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2564 /* First dump points-to information for the default definitions of
2565 pointer variables. This is necessary because default definitions are
2566 not part of the code. */
2567 for (i = 0; i < num_referenced_vars; i++)
2569 tree var = referenced_var (i);
2570 if (POINTER_TYPE_P (TREE_TYPE (var)))
2572 var_ann_t ann = var_ann (var);
2573 if (ann->default_def)
2574 dump_points_to_info_for (file, ann->default_def);
2578 /* Dump points-to information for every pointer defined in the program. */
2579 FOR_EACH_BB (bb)
2581 tree phi;
2583 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2585 tree ptr = PHI_RESULT (phi);
2586 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2587 dump_points_to_info_for (file, ptr);
2590 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2592 tree stmt = bsi_stmt (si);
2593 tree def;
2594 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2595 if (POINTER_TYPE_P (TREE_TYPE (def)))
2596 dump_points_to_info_for (file, def);
2600 fprintf (file, "\n");
2604 /* Dump points-to info pointed by PTO into STDERR. */
2606 void
2607 debug_points_to_info (void)
2609 dump_points_to_info (stderr);
2612 /* Dump to FILE the list of variables that may be aliasing VAR. */
2614 void
2615 dump_may_aliases_for (FILE *file, tree var)
2617 varray_type aliases;
2619 if (TREE_CODE (var) == SSA_NAME)
2620 var = SSA_NAME_VAR (var);
2622 aliases = var_ann (var)->may_aliases;
2623 if (aliases)
2625 size_t i;
2626 fprintf (file, "{ ");
2627 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2629 print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2630 fprintf (file, " ");
2632 fprintf (file, "}");
2637 /* Dump to stderr the list of variables that may be aliasing VAR. */
2639 void
2640 debug_may_aliases_for (tree var)
2642 dump_may_aliases_for (stderr, var);
2645 /* Return true if VAR may be aliased. */
2647 bool
2648 may_be_aliased (tree var)
2650 /* Obviously. */
2651 if (TREE_ADDRESSABLE (var))
2652 return true;
2654 /* Globally visible variables can have their addresses taken by other
2655 translation units. */
2656 if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2657 return true;
2659 /* Automatic variables can't have their addresses escape any other way.
2660 This must be after the check for global variables, as extern declarations
2661 do not have TREE_STATIC set. */
2662 if (!TREE_STATIC (var))
2663 return false;
2665 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2666 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2667 we can only be sure the variable isn't addressable if it's local to the
2668 current function. */
2669 if (flag_unit_at_a_time)
2670 return false;
2671 if (decl_function_context (var) == current_function_decl)
2672 return false;
2674 return true;
2677 /* This structure is simply used during pushing fields onto the fieldstack
2678 to track the offset of the field, since bitpos_of_field gives it relative
2679 to it's immediate containing type, and we want it relative to the ultimate
2680 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 it's
2690 structure. */
2692 static unsigned HOST_WIDE_INT
2693 bitpos_of_field (const tree fdecl)
2695 return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2696 + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2699 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all the fields
2700 of TYPE onto fieldstack, recording their offsets along the way.
2701 OFFSET is used to keep track of the offset in this entire structure, rather
2702 than just the immediately containing structure. */
2704 static void
2705 push_fields_onto_fieldstack (tree type, VEC(fieldoff_t) **fieldstack,
2706 unsigned HOST_WIDE_INT offset)
2708 fieldoff_t pair;
2709 tree field = TYPE_FIELDS (type);
2710 if (!field)
2711 return;
2712 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
2713 && TREE_CODE (TREE_TYPE (field)) != ARRAY_TYPE
2714 && TREE_CODE (field) == FIELD_DECL)
2716 size_t before = VEC_length (fieldoff_t, *fieldstack);
2717 /* Empty structures may have actual size, like in C++. So see if we
2718 actually end up pushing a field, and if not, if the size is non-zero,
2719 push the field onto the stack */
2720 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack, offset);
2721 if (before == VEC_length (fieldoff_t, *fieldstack)
2722 && DECL_SIZE (field)
2723 && !integer_zerop (DECL_SIZE (field)))
2725 pair = xmalloc (sizeof (struct fieldoff));
2726 pair->field = field;
2727 pair->offset = offset;
2728 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2731 else if (TREE_CODE (field) == FIELD_DECL)
2733 pair = xmalloc (sizeof (struct fieldoff));
2734 pair->field = field;
2735 pair->offset = offset + bitpos_of_field (field);
2736 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2738 for (field = TREE_CHAIN (field); field; field = TREE_CHAIN (field))
2740 if (TREE_CODE (field) != FIELD_DECL)
2741 continue;
2742 if (AGGREGATE_TYPE_P (TREE_TYPE (field))
2743 && TREE_CODE (TREE_TYPE (field)) != ARRAY_TYPE)
2745 push_fields_onto_fieldstack (TREE_TYPE (field), fieldstack,
2746 offset + bitpos_of_field (field));
2748 else
2750 pair = xmalloc (sizeof (struct fieldoff));
2751 pair->field = field;
2752 pair->offset = offset + bitpos_of_field (field);
2753 VEC_safe_push (fieldoff_t, *fieldstack, pair);
2759 typedef struct used_part
2761 HOST_WIDE_INT minused;
2762 HOST_WIDE_INT maxused;
2763 } *used_part_t;
2765 static used_part_t *used_portions;
2767 /* Given a variable uid, UID, get or create the entry in the used portions
2768 table for the variable. */
2769 static used_part_t
2770 get_or_create_used_part_for (size_t uid)
2772 used_part_t up;
2773 if (used_portions[uid] == NULL)
2775 up = xcalloc (1, sizeof (struct used_part));
2776 up->minused = INT_MAX;
2777 up->maxused = 0;
2779 else
2780 up = used_portions[uid];
2781 return up;
2786 /* Given an aggregate VAR, create the fake variables that represent it's
2787 fields. */
2789 static void
2790 create_overlap_variables_for (tree var)
2792 VEC(fieldoff_t) *fieldstack = NULL;
2793 used_part_t up;
2794 size_t uid = var_ann (var)->uid;
2796 if (used_portions[uid] == NULL)
2797 return;
2799 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0);
2800 if (VEC_length (fieldoff_t, fieldstack) != 0)
2802 subvar_t *subvars;
2803 fieldoff_t fo;
2804 bool notokay = false;
2805 int i;
2806 int count = 0;
2807 /* Not all fields have DECL_SIZE set for some reason. Also, we can't
2808 handle variable sized fields. */
2809 for (i = 0; VEC_iterate (fieldoff_t, fieldstack, i, fo); i++)
2811 if (!DECL_SIZE (fo->field)
2812 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST)
2814 #if 0
2815 HOST_WIDE_INT fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
2817 if (!((fo->offset <= up->minused
2818 && fo->offset + fosize <= up->minused)
2819 || fo->offset >= up->maxused))
2821 count++;
2823 #endif
2824 notokay = true;
2825 break;
2828 /* Cleanup after ourselves if we can't create overlap variables. */
2829 if (notokay || count > 8)
2831 while (VEC_length (fieldoff_t, fieldstack) != 0)
2833 fo = VEC_pop (fieldoff_t, fieldstack);
2834 free (fo);
2836 VEC_free (fieldoff_t, fieldstack);
2837 return;
2839 /* Otherwise, create the variables. */
2840 subvars = lookup_subvars_for_var (var);
2841 up = used_portions[uid];
2843 while (VEC_length (fieldoff_t, fieldstack) != 0)
2845 subvar_t sv = ggc_alloc (sizeof (struct subvar));
2846 char *name;
2847 HOST_WIDE_INT fosize;
2848 var_ann_t ann;
2850 fo = VEC_pop (fieldoff_t, fieldstack);
2851 fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
2853 if ((fo->offset <= up->minused
2854 && fo->offset + fosize <= up->minused)
2855 || fo->offset >= up->maxused)
2857 free (fo);
2858 continue;
2861 name = alloca (512);
2862 sv->offset = fo->offset;
2863 sv->size = fosize;
2864 sv->next = *subvars;
2865 sprintf (name, "fo:%s#" HOST_WIDE_INT_PRINT_DEC "#" HOST_WIDE_INT_PRINT_DEC, get_name (var), sv->offset, sv->size);
2866 sv->var = create_tmp_var_raw (TREE_TYPE (fo->field), name);
2868 /* We need to copy the external and static flags from var to
2869 sv->var, so that they are is_global_var iff the original variable
2870 was. */
2871 if (DECL_EXTERNAL (var))
2872 DECL_EXTERNAL (sv->var) = 1;
2873 if (TREE_PUBLIC (var))
2874 TREE_PUBLIC (sv->var) = 1;
2875 if (TREE_STATIC (var))
2876 TREE_STATIC (sv->var) = 1;
2877 if (TREE_READONLY (fo->field))
2878 TREE_READONLY (sv->var) = 1;
2879 TREE_ADDRESSABLE (sv->var) = 1;
2880 DECL_CONTEXT (sv->var) = DECL_CONTEXT (var);
2881 ann = get_var_ann (sv->var);
2882 ann->mem_tag_kind = STRUCT_FIELD;
2883 ann->type_mem_tag = NULL;
2884 add_referenced_tmp_var (sv->var);
2886 *subvars = sv;
2887 free (fo);
2891 VEC_free (fieldoff_t, fieldstack);
2895 /* Find the conservative answer to the question of what portions of what
2896 structures are used by this statement. We assume that if we have a
2897 component ref with a known size + offset, that we only need that part
2898 of the structure. For unknown cases, or cases where we do something
2899 to the whole structure, we assume we need to create fields for the
2900 entire structure. */
2901 static tree
2902 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2904 switch (TREE_CODE (*tp))
2906 case COMPONENT_REF:
2908 HOST_WIDE_INT bitsize;
2909 HOST_WIDE_INT bitpos;
2910 tree offset;
2911 enum machine_mode mode;
2912 int unsignedp;
2913 int volatilep;
2914 tree ref;
2915 ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
2916 &unsignedp, &volatilep, false);
2917 if (DECL_P (ref) && offset == NULL && bitsize != -1)
2919 size_t uid = var_ann (ref)->uid;
2920 used_part_t up;
2922 up = get_or_create_used_part_for (uid);
2924 if (bitpos <= up->minused)
2925 up->minused = bitpos;
2926 if ((bitpos + bitsize >= up->maxused))
2927 up->maxused = bitpos + bitsize;
2929 used_portions[uid] = up;
2931 *walk_subtrees = 0;
2932 return NULL_TREE;
2934 else if (DECL_P (ref))
2936 if (DECL_SIZE (ref)
2937 && AGGREGATE_TYPE_P (TREE_TYPE (ref))
2938 && TREE_CODE (TREE_TYPE (ref)) != ARRAY_TYPE
2939 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
2941 used_part_t up;
2942 size_t uid = var_ann (ref)->uid;
2944 up = get_or_create_used_part_for (uid);
2946 up->minused = 0;
2947 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
2949 used_portions[uid] = up;
2951 *walk_subtrees = 0;
2952 return NULL_TREE;
2956 break;
2957 case VAR_DECL:
2958 case PARM_DECL:
2960 tree var = *tp;
2961 if (DECL_SIZE (var)
2962 && AGGREGATE_TYPE_P (TREE_TYPE (var))
2963 && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE
2964 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2966 used_part_t up;
2967 size_t uid = var_ann (var)->uid;
2969 up = get_or_create_used_part_for (uid);
2971 up->minused = 0;
2972 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
2974 used_portions[uid] = up;
2975 *walk_subtrees = 0;
2976 return NULL_TREE;
2979 break;
2981 default:
2982 break;
2985 return NULL_TREE;
2987 /* We are about to create some new referenced variables, and we need the
2988 before size. */
2990 static size_t old_referenced_vars;
2993 /* Create structure field variables for structures used in this function. */
2995 static void
2996 create_structure_vars (void)
2998 basic_block bb;
2999 size_t i;
3001 old_referenced_vars = num_referenced_vars;
3002 used_portions = xcalloc (num_referenced_vars, sizeof (used_part_t));
3004 FOR_EACH_BB (bb)
3006 block_stmt_iterator bsi;
3007 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3009 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3010 find_used_portions,
3011 NULL);
3014 for (i = 0; i < old_referenced_vars; i++)
3016 tree var = referenced_var (i);
3017 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3018 if (var
3019 && DECL_SIZE (var)
3020 && AGGREGATE_TYPE_P (TREE_TYPE (var))
3021 && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE
3022 && var_ann (var)->mem_tag_kind == NOT_A_TAG
3023 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3024 create_overlap_variables_for (var);
3026 for (i = 0; i < old_referenced_vars; i++)
3027 free (used_portions[i]);
3029 free (used_portions);
3031 struct tree_opt_pass pass_create_structure_vars =
3033 "svars", /* name */
3034 NULL, /* gate */
3035 create_structure_vars, /* execute */
3036 NULL, /* sub */
3037 NULL, /* next */
3038 0, /* static_pass_number */
3039 0, /* tv_id */
3040 PROP_cfg, /* properties_required */
3041 0, /* properties_provided */
3042 0, /* properties_destroyed */
3043 0, /* todo_flags_start */
3044 TODO_dump_func, /* todo_flags_finish */
3045 0 /* letter */