* Mainline merge as of 2006-02-16 (@111136).
[official-gcc.git] / gcc / tree-ssa-alias.c
blobb262fd0f0fd81a367ab7d9a45ecd6cf9d1cc2869
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "expr.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "tree-gimple.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "tree-ssa-structalias.h"
44 #include "convert.h"
45 #include "params.h"
46 #include "ipa-type-escape.h"
47 #include "vec.h"
48 #include "bitmap.h"
50 /* Obstack used to hold grouping bitmaps and other temporary bitmaps used by
51 aliasing */
52 static bitmap_obstack alias_obstack;
54 /* 'true' after aliases have been computed (see compute_may_aliases). */
55 bool aliases_computed_p;
57 /* Structure to map a variable to its alias set and keep track of the
58 virtual operands that will be needed to represent it. */
59 struct alias_map_d
61 /* Variable and its alias set. */
62 tree var;
63 HOST_WIDE_INT set;
65 /* Total number of virtual operands that will be needed to represent
66 all the aliases of VAR. */
67 long total_alias_vops;
69 /* Nonzero if the aliases for this memory tag have been grouped
70 already. Used in group_aliases. */
71 unsigned int grouped_p : 1;
73 /* Set of variables aliased with VAR. This is the exact same
74 information contained in VAR_ANN (VAR)->MAY_ALIASES, but in
75 bitmap form to speed up alias grouping. */
76 bitmap may_aliases;
80 /* Counters used to display statistics on alias analysis. */
81 struct alias_stats_d
83 unsigned int alias_queries;
84 unsigned int alias_mayalias;
85 unsigned int alias_noalias;
86 unsigned int simple_queries;
87 unsigned int simple_resolved;
88 unsigned int tbaa_queries;
89 unsigned int tbaa_resolved;
90 unsigned int structnoaddress_queries;
91 unsigned int structnoaddress_resolved;
95 /* Local variables. */
96 static struct alias_stats_d alias_stats;
98 /* Local functions. */
99 static void compute_flow_insensitive_aliasing (struct alias_info *);
100 static void dump_alias_stats (FILE *);
101 static bool may_alias_p (tree, HOST_WIDE_INT, tree, HOST_WIDE_INT, bool);
102 static tree create_memory_tag (tree type, bool is_type_tag);
103 static tree get_tmt_for (tree, struct alias_info *);
104 static tree get_nmt_for (tree);
105 static void add_may_alias (tree, tree);
106 static void replace_may_alias (tree, size_t, tree);
107 static struct alias_info *init_alias_info (void);
108 static void delete_alias_info (struct alias_info *);
109 static void compute_flow_sensitive_aliasing (struct alias_info *);
110 static void setup_pointers_and_addressables (struct alias_info *);
111 static void create_global_var (void);
112 static void maybe_create_global_var (struct alias_info *ai);
113 static void group_aliases (struct alias_info *);
114 static void set_pt_anything (tree ptr);
116 /* Global declarations. */
118 /* Call clobbered variables in the function. If bit I is set, then
119 REFERENCED_VARS (I) is call-clobbered. */
120 bitmap call_clobbered_vars;
122 /* Addressable variables in the function. If bit I is set, then
123 REFERENCED_VARS (I) has had its address taken. Note that
124 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
125 addressable variable is not necessarily call-clobbered (e.g., a
126 local addressable whose address does not escape) and not all
127 call-clobbered variables are addressable (e.g., a local static
128 variable). */
129 bitmap addressable_vars;
131 /* When the program has too many call-clobbered variables and call-sites,
132 this variable is used to represent the clobbering effects of function
133 calls. In these cases, all the call clobbered variables in the program
134 are forced to alias this variable. This reduces compile times by not
135 having to keep track of too many V_MAY_DEF expressions at call sites. */
136 tree global_var;
138 DEF_VEC_I(int);
139 DEF_VEC_ALLOC_I(int,heap);
141 /* qsort comparison function to sort type/name tags by DECL_UID. */
143 static int
144 sort_tags_by_id (const void *pa, const void *pb)
146 tree a = *(tree *)pa;
147 tree b = *(tree *)pb;
149 return DECL_UID (a) - DECL_UID (b);
152 /* Initialize WORKLIST to contain those memory tags that are marked call
153 clobbered. Initialized WORKLIST2 to contain the reasons these
154 memory tags escaped. */
156 static void
157 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
158 VEC (int, heap) **worklist2)
160 referenced_var_iterator rvi;
161 tree curr;
163 FOR_EACH_REFERENCED_VAR (curr, rvi)
165 if (MTAG_P (curr) && is_call_clobbered (curr))
167 VEC_safe_push (tree, heap, *worklist, curr);
168 VEC_safe_push (int, heap, *worklist2, var_ann (curr)->escape_mask);
173 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
174 ALIAS is not already marked call clobbered, and is a memory
175 tag. */
177 static void
178 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
179 VEC (int, heap) **worklist2,
180 int reason)
182 if (MTAG_P (alias) && !is_call_clobbered (alias))
184 VEC_safe_push (tree, heap, *worklist, alias);
185 VEC_safe_push (int, heap, *worklist2, reason);
189 /* Mark aliases of TAG as call clobbered, and place any tags on the
190 alias list that were not already call clobbered on WORKLIST. */
192 static void
193 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
194 VEC (int, heap) **worklist2)
196 unsigned int i;
197 VEC (tree, gc) *ma;
198 tree entry;
199 var_ann_t ta = var_ann (tag);
201 if (!MTAG_P (tag))
202 return;
203 ma = may_aliases (tag);
204 if (!ma)
205 return;
207 for (i = 0; VEC_iterate (tree, ma, i, entry); i++)
209 if (!unmodifiable_var_p (entry))
211 add_to_worklist (entry, worklist, worklist2, ta->escape_mask);
212 mark_call_clobbered (entry, ta->escape_mask);
217 /* Tags containing global vars need to be marked as global.
218 Tags containing call clobbered vars need to be marked as call
219 clobbered. */
221 static void
222 compute_tag_properties (void)
224 referenced_var_iterator rvi;
225 tree tag;
226 bool changed = true;
227 VEC (tree, heap) *taglist = NULL;
229 FOR_EACH_REFERENCED_VAR (tag, rvi)
231 if (!MTAG_P (tag) || TREE_CODE (tag) == STRUCT_FIELD_TAG)
232 continue;
233 VEC_safe_push (tree, heap, taglist, tag);
236 /* We sort the taglist by DECL_UID, for two reasons.
237 1. To get a sequential ordering to make the bitmap accesses
238 faster.
239 2. Because of the way we compute aliases, it's more likely that
240 an earlier tag is included in a later tag, and this will reduce
241 the number of iterations.
243 If we had a real tag graph, we would just topo-order it and be
244 done with it. */
245 qsort (VEC_address (tree, taglist),
246 VEC_length (tree, taglist),
247 sizeof (tree),
248 sort_tags_by_id);
250 /* Go through each tag not marked as global, and if it aliases
251 global vars, mark it global.
253 If the tag contains call clobbered vars, mark it call
254 clobbered.
256 This loop iterates because tags may appear in the may-aliases
257 list of other tags when we group. */
259 while (changed)
261 unsigned int k;
263 changed = false;
264 for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
266 VEC (tree, gc) *ma;
267 unsigned int i;
268 tree entry;
269 bool tagcc = is_call_clobbered (tag);
270 bool tagglobal = MTAG_GLOBAL (tag);
272 if (tagcc && tagglobal)
273 continue;
275 ma = may_aliases (tag);
276 if (!ma)
277 continue;
279 for (i = 0; VEC_iterate (tree, ma, i, entry); i++)
281 /* Call clobbered entries cause the tag to be marked
282 call clobbered. */
283 if (!tagcc && is_call_clobbered (entry))
285 mark_call_clobbered (tag, var_ann (entry)->escape_mask);
286 tagcc = true;
287 changed = true;
290 /* Global vars cause the tag to be marked global. */
291 if (!tagglobal && is_global_var (entry))
293 MTAG_GLOBAL (tag) = true;
294 changed = true;
295 tagglobal = true;
298 /* Early exit once both global and cc are set, since the
299 loop can't do any more than that. */
300 if (tagcc && tagglobal)
301 break;
305 VEC_free (tree, heap, taglist);
308 /* Set up the initial variable clobbers and globalness.
309 When this function completes, only tags whose aliases need to be
310 clobbered will be set clobbered. Tags clobbered because they
311 contain call clobbered vars are handled in compute_tag_properties. */
313 static void
314 set_initial_properties (struct alias_info *ai)
316 unsigned int i;
317 referenced_var_iterator rvi;
318 tree var;
320 FOR_EACH_REFERENCED_VAR (var, rvi)
322 if (is_global_var (var)
323 && (!var_can_have_subvars (var)
324 || get_subvars_for_var (var) == NULL))
326 if (!unmodifiable_var_p (var))
327 mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
329 else if (TREE_CODE (var) == PARM_DECL
330 && default_def (var)
331 && POINTER_TYPE_P (TREE_TYPE (var)))
333 tree def = default_def (var);
334 get_ptr_info (def)->value_escapes_p = 1;
335 get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;
339 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
341 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
342 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
343 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
345 if (pi->value_escapes_p)
347 /* If PTR escapes then its associated memory tags and
348 pointed-to variables are call-clobbered. */
349 if (pi->name_mem_tag)
350 mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
352 if (v_ann->type_mem_tag)
353 mark_call_clobbered (v_ann->type_mem_tag, pi->escape_mask);
355 if (pi->pt_vars)
357 bitmap_iterator bi;
358 unsigned int j;
359 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
360 if (!unmodifiable_var_p (referenced_var (j)))
361 mark_call_clobbered (referenced_var (j), pi->escape_mask);
364 /* If the name tag is call clobbered, so is the type tag
365 associated with the base VAR_DECL. */
366 if (pi->name_mem_tag
367 && v_ann->type_mem_tag
368 && is_call_clobbered (pi->name_mem_tag))
369 mark_call_clobbered (v_ann->type_mem_tag, pi->escape_mask);
371 /* Name tags and type tags that we don't know where they point
372 to, might point to global memory, and thus, are clobbered.
374 FIXME: This is not quite right. They should only be
375 clobbered if value_escapes_p is true, regardless of whether
376 they point to global memory or not.
377 So removing this code and fixing all the bugs would be nice.
378 It is the cause of a bunch of clobbering. */
379 if ((pi->pt_global_mem || pi->pt_anything)
380 && pi->is_dereferenced && pi->name_mem_tag)
382 mark_call_clobbered (pi->name_mem_tag, ESCAPE_IS_GLOBAL);
383 MTAG_GLOBAL (pi->name_mem_tag) = true;
386 if ((pi->pt_global_mem || pi->pt_anything)
387 && pi->is_dereferenced && v_ann->type_mem_tag)
389 mark_call_clobbered (v_ann->type_mem_tag, ESCAPE_IS_GLOBAL);
390 MTAG_GLOBAL (v_ann->type_mem_tag) = true;
395 /* Compute which variables need to be marked call clobbered because
396 their tag is call clobbered, and which tags need to be marked
397 global because they contain global variables. */
399 static void
400 compute_call_clobbered (struct alias_info *ai)
402 VEC (tree, heap) *worklist = NULL;
403 VEC(int,heap) *worklist2 = NULL;
405 set_initial_properties (ai);
406 init_transitive_clobber_worklist (&worklist, &worklist2);
407 while (VEC_length (tree, worklist) != 0)
409 tree curr = VEC_pop (tree, worklist);
410 int reason = VEC_pop (int, worklist2);
412 mark_call_clobbered (curr, reason);
413 mark_aliases_call_clobbered (curr, &worklist, &worklist2);
415 VEC_free (tree, heap, worklist);
416 VEC_free (int, heap, worklist2);
417 compute_tag_properties ();
420 /* Compute may-alias information for every variable referenced in function
421 FNDECL.
423 Alias analysis proceeds in 3 main phases:
425 1- Points-to and escape analysis.
427 This phase walks the use-def chains in the SSA web looking for three
428 things:
430 * Assignments of the form P_i = &VAR
431 * Assignments of the form P_i = malloc()
432 * Pointers and ADDR_EXPR that escape the current function.
434 The concept of 'escaping' is the same one used in the Java world. When
435 a pointer or an ADDR_EXPR escapes, it means that it has been exposed
436 outside of the current function. So, assignment to global variables,
437 function arguments and returning a pointer are all escape sites, as are
438 conversions between pointers and integers.
440 This is where we are currently limited. Since not everything is renamed
441 into SSA, we lose track of escape properties when a pointer is stashed
442 inside a field in a structure, for instance. In those cases, we are
443 assuming that the pointer does escape.
445 We use escape analysis to determine whether a variable is
446 call-clobbered. Simply put, if an ADDR_EXPR escapes, then the variable
447 is call-clobbered. If a pointer P_i escapes, then all the variables
448 pointed-to by P_i (and its memory tag) also escape.
450 2- Compute flow-sensitive aliases
452 We have two classes of memory tags. Memory tags associated with the
453 pointed-to data type of the pointers in the program. These tags are
454 called "type memory tag" (TMT). The other class are those associated
455 with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
456 when adding operands for an INDIRECT_REF *P_i, we will first check
457 whether P_i has a name tag, if it does we use it, because that will have
458 more precise aliasing information. Otherwise, we use the standard type
459 tag.
461 In this phase, we go through all the pointers we found in points-to
462 analysis and create alias sets for the name memory tags associated with
463 each pointer P_i. If P_i escapes, we mark call-clobbered the variables
464 it points to and its tag.
467 3- Compute flow-insensitive aliases
469 This pass will compare the alias set of every type memory tag and every
470 addressable variable found in the program. Given a type memory tag TMT
471 and an addressable variable V. If the alias sets of TMT and V conflict
472 (as computed by may_alias_p), then V is marked as an alias tag and added
473 to the alias set of TMT.
475 For instance, consider the following function:
477 foo (int i)
479 int *p, a, b;
481 if (i > 10)
482 p = &a;
483 else
484 p = &b;
486 *p = 3;
487 a = b + 2;
488 return *p;
491 After aliasing analysis has finished, the type memory tag for pointer
492 'p' will have two aliases, namely variables 'a' and 'b'. Every time
493 pointer 'p' is dereferenced, we want to mark the operation as a
494 potential reference to 'a' and 'b'.
496 foo (int i)
498 int *p, a, b;
500 if (i_2 > 10)
501 p_4 = &a;
502 else
503 p_6 = &b;
504 # p_1 = PHI <p_4(1), p_6(2)>;
506 # a_7 = V_MAY_DEF <a_3>;
507 # b_8 = V_MAY_DEF <b_5>;
508 *p_1 = 3;
510 # a_9 = V_MAY_DEF <a_7>
511 # VUSE <b_8>
512 a_9 = b_8 + 2;
514 # VUSE <a_9>;
515 # VUSE <b_8>;
516 return *p_1;
519 In certain cases, the list of may aliases for a pointer may grow too
520 large. This may cause an explosion in the number of virtual operands
521 inserted in the code. Resulting in increased memory consumption and
522 compilation time.
524 When the number of virtual operands needed to represent aliased
525 loads and stores grows too large (configurable with @option{--param
526 max-aliased-vops}), alias sets are grouped to avoid severe
527 compile-time slow downs and memory consumption. See group_aliases. */
529 static void
530 compute_may_aliases (void)
532 struct alias_info *ai;
534 memset (&alias_stats, 0, sizeof (alias_stats));
536 /* Initialize aliasing information. */
537 ai = init_alias_info ();
539 /* For each pointer P_i, determine the sets of variables that P_i may
540 point-to. For every addressable variable V, determine whether the
541 address of V escapes the current function, making V call-clobbered
542 (i.e., whether &V is stored in a global variable or if its passed as a
543 function call argument). */
544 compute_points_to_sets (ai);
546 /* Collect all pointers and addressable variables, compute alias sets,
547 create memory tags for pointers and promote variables whose address is
548 not needed anymore. */
549 setup_pointers_and_addressables (ai);
551 /* Compute flow-sensitive, points-to based aliasing for all the name
552 memory tags. Note that this pass needs to be done before flow
553 insensitive analysis because it uses the points-to information
554 gathered before to mark call-clobbered type tags. */
555 compute_flow_sensitive_aliasing (ai);
557 /* Compute type-based flow-insensitive aliasing for all the type
558 memory tags. */
559 compute_flow_insensitive_aliasing (ai);
561 /* Determine if we need to enable alias grouping. */
562 if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
563 group_aliases (ai);
565 /* Compute call clobbering information. */
566 compute_call_clobbered (ai);
568 /* If the program has too many call-clobbered variables and/or function
569 calls, create .GLOBAL_VAR and use it to model call-clobbering
570 semantics at call sites. This reduces the number of virtual operands
571 considerably, improving compile times at the expense of lost
572 aliasing precision. */
573 maybe_create_global_var (ai);
575 /* Debugging dumps. */
576 if (dump_file)
578 dump_referenced_vars (dump_file);
579 if (dump_flags & TDF_STATS)
580 dump_alias_stats (dump_file);
581 dump_points_to_info (dump_file);
582 dump_alias_info (dump_file);
585 /* Deallocate memory used by aliasing data structures. */
586 delete_alias_info (ai);
589 block_stmt_iterator bsi;
590 basic_block bb;
591 FOR_EACH_BB (bb)
593 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
595 update_stmt_if_modified (bsi_stmt (bsi));
602 struct tree_opt_pass pass_may_alias =
604 "alias", /* name */
605 NULL, /* gate */
606 compute_may_aliases, /* execute */
607 NULL, /* sub */
608 NULL, /* next */
609 0, /* static_pass_number */
610 TV_TREE_MAY_ALIAS, /* tv_id */
611 PROP_cfg | PROP_ssa, /* properties_required */
612 PROP_alias, /* properties_provided */
613 0, /* properties_destroyed */
614 0, /* todo_flags_start */
615 TODO_dump_func | TODO_update_ssa
616 | TODO_ggc_collect | TODO_verify_ssa
617 | TODO_verify_stmts, /* todo_flags_finish */
618 0 /* letter */
622 /* Data structure used to count the number of dereferences to PTR
623 inside an expression. */
624 struct count_ptr_d
626 tree ptr;
627 unsigned count;
631 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
632 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
634 static tree
635 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
637 struct count_ptr_d *count_p = (struct count_ptr_d *) data;
639 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
640 pointer 'ptr' is *not* dereferenced, it is simply used to compute
641 the address of 'fld' as 'ptr + offsetof(fld)'. */
642 if (TREE_CODE (*tp) == ADDR_EXPR)
644 *walk_subtrees = 0;
645 return NULL_TREE;
648 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
649 count_p->count++;
651 return NULL_TREE;
655 /* Count the number of direct and indirect uses for pointer PTR in
656 statement STMT. The two counts are stored in *NUM_USES_P and
657 *NUM_DEREFS_P respectively. *IS_STORE_P is set to 'true' if at
658 least one of those dereferences is a store operation. */
660 void
661 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
662 unsigned *num_derefs_p, bool *is_store)
664 ssa_op_iter i;
665 tree use;
667 *num_uses_p = 0;
668 *num_derefs_p = 0;
669 *is_store = false;
671 /* Find out the total number of uses of PTR in STMT. */
672 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
673 if (use == ptr)
674 (*num_uses_p)++;
676 /* Now count the number of indirect references to PTR. This is
677 truly awful, but we don't have much choice. There are no parent
678 pointers inside INDIRECT_REFs, so an expression like
679 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
680 find all the indirect and direct uses of x_1 inside. The only
681 shortcut we can take is the fact that GIMPLE only allows
682 INDIRECT_REFs inside the expressions below. */
683 if (TREE_CODE (stmt) == MODIFY_EXPR
684 || (TREE_CODE (stmt) == RETURN_EXPR
685 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
686 || TREE_CODE (stmt) == ASM_EXPR
687 || TREE_CODE (stmt) == CALL_EXPR)
689 tree lhs, rhs;
691 if (TREE_CODE (stmt) == MODIFY_EXPR)
693 lhs = TREE_OPERAND (stmt, 0);
694 rhs = TREE_OPERAND (stmt, 1);
696 else if (TREE_CODE (stmt) == RETURN_EXPR)
698 tree e = TREE_OPERAND (stmt, 0);
699 lhs = TREE_OPERAND (e, 0);
700 rhs = TREE_OPERAND (e, 1);
702 else if (TREE_CODE (stmt) == ASM_EXPR)
704 lhs = ASM_OUTPUTS (stmt);
705 rhs = ASM_INPUTS (stmt);
707 else
709 lhs = NULL_TREE;
710 rhs = stmt;
713 if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
715 struct count_ptr_d count;
716 count.ptr = ptr;
717 count.count = 0;
718 walk_tree (&lhs, count_ptr_derefs, &count, NULL);
719 *is_store = true;
720 *num_derefs_p = count.count;
723 if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
725 struct count_ptr_d count;
726 count.ptr = ptr;
727 count.count = 0;
728 walk_tree (&rhs, count_ptr_derefs, &count, NULL);
729 *num_derefs_p += count.count;
733 gcc_assert (*num_uses_p >= *num_derefs_p);
736 /* Initialize the data structures used for alias analysis. */
738 static struct alias_info *
739 init_alias_info (void)
741 struct alias_info *ai;
742 referenced_var_iterator rvi;
743 tree var;
745 bitmap_obstack_initialize (&alias_obstack);
746 ai = XCNEW (struct alias_info);
747 ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
748 sbitmap_zero (ai->ssa_names_visited);
749 VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
750 ai->written_vars = BITMAP_ALLOC (&alias_obstack);
751 ai->dereferenced_ptrs_store = BITMAP_ALLOC (&alias_obstack);
752 ai->dereferenced_ptrs_load = BITMAP_ALLOC (&alias_obstack);
754 /* If aliases have been computed before, clear existing information. */
755 if (aliases_computed_p)
757 unsigned i;
759 /* Similarly, clear the set of addressable variables. In this
760 case, we can just clear the set because addressability is
761 only computed here. */
762 bitmap_clear (addressable_vars);
764 /* Clear flow-insensitive alias information from each symbol. */
765 FOR_EACH_REFERENCED_VAR (var, rvi)
767 var_ann_t ann = var_ann (var);
769 ann->is_alias_tag = 0;
770 ann->may_aliases = NULL;
771 NUM_REFERENCES_CLEAR (ann);
773 /* Since we are about to re-discover call-clobbered
774 variables, clear the call-clobbered flag. Variables that
775 are intrinsically call-clobbered (globals, local statics,
776 etc) will not be marked by the aliasing code, so we can't
777 remove them from CALL_CLOBBERED_VARS.
779 NB: STRUCT_FIELDS are still call clobbered if they are for
780 a global variable, so we *don't* clear their call clobberedness
781 just because they are tags, though we will clear it if they
782 aren't for global variables. */
783 if (TREE_CODE (var) == NAME_MEMORY_TAG
784 || TREE_CODE (var) == TYPE_MEMORY_TAG
785 || !is_global_var (var))
786 clear_call_clobbered (var);
789 /* Clear flow-sensitive points-to information from each SSA name. */
790 for (i = 1; i < num_ssa_names; i++)
792 tree name = ssa_name (i);
794 if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
795 continue;
797 if (SSA_NAME_PTR_INFO (name))
799 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
801 /* Clear all the flags but keep the name tag to
802 avoid creating new temporaries unnecessarily. If
803 this pointer is found to point to a subset or
804 superset of its former points-to set, then a new
805 tag will need to be created in create_name_tags. */
806 pi->pt_anything = 0;
807 pi->pt_null = 0;
808 pi->value_escapes_p = 0;
809 pi->is_dereferenced = 0;
810 if (pi->pt_vars)
811 bitmap_clear (pi->pt_vars);
816 /* Next time, we will need to reset alias information. */
817 aliases_computed_p = true;
819 return ai;
823 /* Deallocate memory used by alias analysis. */
825 static void
826 delete_alias_info (struct alias_info *ai)
828 size_t i;
829 referenced_var_iterator rvi;
830 tree var;
832 sbitmap_free (ai->ssa_names_visited);
833 ai->processed_ptrs = NULL;
835 for (i = 0; i < ai->num_addressable_vars; i++)
836 free (ai->addressable_vars[i]);
838 FOR_EACH_REFERENCED_VAR(var, rvi)
840 var_ann_t ann = var_ann (var);
841 NUM_REFERENCES_CLEAR (ann);
844 free (ai->addressable_vars);
846 for (i = 0; i < ai->num_pointers; i++)
847 free (ai->pointers[i]);
848 free (ai->pointers);
850 BITMAP_FREE (ai->written_vars);
851 BITMAP_FREE (ai->dereferenced_ptrs_store);
852 BITMAP_FREE (ai->dereferenced_ptrs_load);
853 bitmap_obstack_release (&alias_obstack);
854 free (ai);
856 delete_points_to_sets ();
859 /* Create name tags for all the pointers that have been dereferenced.
860 We only create a name tag for a pointer P if P is found to point to
861 a set of variables (so that we can alias them to *P) or if it is
862 the result of a call to malloc (which means that P cannot point to
863 anything else nor alias any other variable).
865 If two pointers P and Q point to the same set of variables, they
866 are assigned the same name tag. */
868 static void
869 create_name_tags (void)
871 size_t i;
872 VEC (tree, heap) *with_ptvars = NULL;
873 tree ptr;
875 /* Collect the list of pointers with a non-empty points to set. */
876 for (i = 1; i < num_ssa_names; i++)
878 tree ptr = ssa_name (i);
879 struct ptr_info_def *pi;
881 if (!ptr
882 || !POINTER_TYPE_P (TREE_TYPE (ptr))
883 || !SSA_NAME_PTR_INFO (ptr))
884 continue;
886 pi = SSA_NAME_PTR_INFO (ptr);
888 if (pi->pt_anything || !pi->is_dereferenced)
890 /* No name tags for pointers that have not been
891 dereferenced or point to an arbitrary location. */
892 pi->name_mem_tag = NULL_TREE;
893 continue;
896 /* Set pt_anything on the pointers without pt_vars filled in so
897 that they are assigned a type tag. */
899 if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars))
900 VEC_safe_push (tree, heap, with_ptvars, ptr);
901 else
902 set_pt_anything (ptr);
905 /* If we didn't find any pointers with pt_vars set, we're done. */
906 if (!with_ptvars)
907 return;
909 /* Now go through the pointers with pt_vars, and find a name tag
910 with the same pt_vars as this pointer, or create one if one
911 doesn't exist. */
912 for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
914 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
915 size_t j;
916 tree ptr2;
917 tree old_name_tag = pi->name_mem_tag;
919 /* If PTR points to a set of variables, check if we don't
920 have another pointer Q with the same points-to set before
921 creating a tag. If so, use Q's tag instead of creating a
922 new one.
924 This is important for not creating unnecessary symbols
925 and also for copy propagation. If we ever need to
926 propagate PTR into Q or vice-versa, we would run into
927 problems if they both had different name tags because
928 they would have different SSA version numbers (which
929 would force us to take the name tags in and out of SSA). */
930 for (j = 0; j < i && VEC_iterate (tree, with_ptvars, j, ptr2); j++)
932 struct ptr_info_def *qi = SSA_NAME_PTR_INFO (ptr2);
934 if (bitmap_equal_p (pi->pt_vars, qi->pt_vars))
936 pi->name_mem_tag = qi->name_mem_tag;
937 break;
941 /* If we didn't find a pointer with the same points-to set
942 as PTR, create a new name tag if needed. */
943 if (pi->name_mem_tag == NULL_TREE)
944 pi->name_mem_tag = get_nmt_for (ptr);
946 /* If the new name tag computed for PTR is different than
947 the old name tag that it used to have, then the old tag
948 needs to be removed from the IL, so we mark it for
949 renaming. */
950 if (old_name_tag && old_name_tag != pi->name_mem_tag)
951 mark_sym_for_renaming (old_name_tag);
953 TREE_THIS_VOLATILE (pi->name_mem_tag)
954 |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
956 /* Mark the new name tag for renaming. */
957 mark_sym_for_renaming (pi->name_mem_tag);
960 VEC_free (tree, heap, with_ptvars);
964 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
965 the name memory tag (NMT) associated with P_i. If P_i escapes, then its
966 name tag and the variables it points-to are call-clobbered. Finally, if
967 P_i escapes and we could not determine where it points to, then all the
968 variables in the same alias set as *P_i are marked call-clobbered. This
969 is necessary because we must assume that P_i may take the address of any
970 variable in the same alias set. */
972 static void
973 compute_flow_sensitive_aliasing (struct alias_info *ai)
975 size_t i;
977 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
979 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
980 if (!find_what_p_points_to (ptr))
981 set_pt_anything (ptr);
984 create_name_tags ();
986 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
988 unsigned j;
989 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
990 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
991 var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
992 bitmap_iterator bi;
995 /* Set up aliasing information for PTR's name memory tag (if it has
996 one). Note that only pointers that have been dereferenced will
997 have a name memory tag. */
998 if (pi->name_mem_tag && pi->pt_vars)
999 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
1001 add_may_alias (pi->name_mem_tag, referenced_var (j));
1002 add_may_alias (v_ann->type_mem_tag, referenced_var (j));
1008 /* Compute type-based alias sets. Traverse all the pointers and
1009 addressable variables found in setup_pointers_and_addressables.
1011 For every pointer P in AI->POINTERS and addressable variable V in
1012 AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
1013 memory tag (TMT) if their alias sets conflict. V is then marked as
1014 an alias tag so that the operand scanner knows that statements
1015 containing V have aliased operands. */
1017 static void
1018 compute_flow_insensitive_aliasing (struct alias_info *ai)
1020 size_t i;
1022 /* Initialize counter for the total number of virtual operands that
1023 aliasing will introduce. When AI->TOTAL_ALIAS_VOPS goes beyond the
1024 threshold set by --params max-alias-vops, we enable alias
1025 grouping. */
1026 ai->total_alias_vops = 0;
1028 /* For every pointer P, determine which addressable variables may alias
1029 with P's type memory tag. */
1030 for (i = 0; i < ai->num_pointers; i++)
1032 size_t j;
1033 struct alias_map_d *p_map = ai->pointers[i];
1034 tree tag = var_ann (p_map->var)->type_mem_tag;
1035 var_ann_t tag_ann = var_ann (tag);
1037 p_map->total_alias_vops = 0;
1038 p_map->may_aliases = BITMAP_ALLOC (&alias_obstack);
1040 for (j = 0; j < ai->num_addressable_vars; j++)
1042 struct alias_map_d *v_map;
1043 var_ann_t v_ann;
1044 tree var;
1045 bool tag_stored_p, var_stored_p;
1047 v_map = ai->addressable_vars[j];
1048 var = v_map->var;
1049 v_ann = var_ann (var);
1051 /* Skip memory tags and variables that have never been
1052 written to. We also need to check if the variables are
1053 call-clobbered because they may be overwritten by
1054 function calls.
1056 Note this is effectively random accessing elements in
1057 the sparse bitset, which can be highly inefficient.
1058 So we first check the call_clobbered status of the
1059 tag and variable before querying the bitmap. */
1060 tag_stored_p = is_call_clobbered (tag)
1061 || bitmap_bit_p (ai->written_vars, DECL_UID (tag));
1062 var_stored_p = is_call_clobbered (var)
1063 || bitmap_bit_p (ai->written_vars, DECL_UID (var));
1064 if (!tag_stored_p && !var_stored_p)
1065 continue;
1067 if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
1069 size_t num_tag_refs, num_var_refs;
1071 num_tag_refs = NUM_REFERENCES (tag_ann);
1072 num_var_refs = NUM_REFERENCES (v_ann);
1074 /* Add VAR to TAG's may-aliases set. */
1076 /* We should never have a var with subvars here, because
1077 they shouldn't get into the set of addressable vars */
1078 gcc_assert (!var_can_have_subvars (var)
1079 || get_subvars_for_var (var) == NULL);
1081 add_may_alias (tag, var);
1082 /* Update the bitmap used to represent TAG's alias set
1083 in case we need to group aliases. */
1084 bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
1086 /* Update the total number of virtual operands due to
1087 aliasing. Since we are adding one more alias to TAG's
1088 may-aliases set, the total number of virtual operands due
1089 to aliasing will be increased by the number of references
1090 made to VAR and TAG (every reference to TAG will also
1091 count as a reference to VAR). */
1092 ai->total_alias_vops += (num_var_refs + num_tag_refs);
1093 p_map->total_alias_vops += (num_var_refs + num_tag_refs);
1100 /* Since this analysis is based exclusively on symbols, it fails to
1101 handle cases where two pointers P and Q have different memory
1102 tags with conflicting alias set numbers but no aliased symbols in
1103 common.
1105 For example, suppose that we have two memory tags TMT.1 and TMT.2
1106 such that
1108 may-aliases (TMT.1) = { a }
1109 may-aliases (TMT.2) = { b }
1111 and the alias set number of TMT.1 conflicts with that of TMT.2.
1112 Since they don't have symbols in common, loads and stores from
1113 TMT.1 and TMT.2 will seem independent of each other, which will
1114 lead to the optimizers making invalid transformations (see
1115 testsuite/gcc.c-torture/execute/pr15262-[12].c).
1117 To avoid this problem, we do a final traversal of AI->POINTERS
1118 looking for pairs of pointers that have no aliased symbols in
1119 common and yet have conflicting alias set numbers. */
1120 for (i = 0; i < ai->num_pointers; i++)
1122 size_t j;
1123 struct alias_map_d *p_map1 = ai->pointers[i];
1124 tree tag1 = var_ann (p_map1->var)->type_mem_tag;
1125 bitmap may_aliases1 = p_map1->may_aliases;
1127 for (j = i + 1; j < ai->num_pointers; j++)
1129 struct alias_map_d *p_map2 = ai->pointers[j];
1130 tree tag2 = var_ann (p_map2->var)->type_mem_tag;
1131 bitmap may_aliases2 = p_map2->may_aliases;
1133 /* If the pointers may not point to each other, do nothing. */
1134 if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
1135 continue;
1137 /* The two pointers may alias each other. If they already have
1138 symbols in common, do nothing. */
1139 if (bitmap_intersect_p (may_aliases1, may_aliases2))
1140 continue;
1142 if (!bitmap_empty_p (may_aliases2))
1144 unsigned int k;
1145 bitmap_iterator bi;
1147 /* Add all the aliases for TAG2 into TAG1's alias set.
1148 FIXME, update grouping heuristic counters. */
1149 EXECUTE_IF_SET_IN_BITMAP (may_aliases2, 0, k, bi)
1150 add_may_alias (tag1, referenced_var (k));
1151 bitmap_ior_into (may_aliases1, may_aliases2);
1153 else
1155 /* Since TAG2 does not have any aliases of its own, add
1156 TAG2 itself to the alias set of TAG1. */
1157 add_may_alias (tag1, tag2);
1158 bitmap_set_bit (may_aliases1, DECL_UID (tag2));
1163 if (dump_file)
1164 fprintf (dump_file, "\n%s: Total number of aliased vops: %ld\n",
1165 get_name (current_function_decl),
1166 ai->total_alias_vops);
1170 /* Comparison function for qsort used in group_aliases. */
1172 static int
1173 total_alias_vops_cmp (const void *p, const void *q)
1175 const struct alias_map_d **p1 = (const struct alias_map_d **)p;
1176 const struct alias_map_d **p2 = (const struct alias_map_d **)q;
1177 long n1 = (*p1)->total_alias_vops;
1178 long n2 = (*p2)->total_alias_vops;
1180 /* We want to sort in descending order. */
1181 return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
1184 /* Group all the aliases for TAG to make TAG represent all the
1185 variables in its alias set. Update the total number
1186 of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS). This
1187 function will make TAG be the unique alias tag for all the
1188 variables in its may-aliases. So, given:
1190 may-aliases(TAG) = { V1, V2, V3 }
1192 This function will group the variables into:
1194 may-aliases(V1) = { TAG }
1195 may-aliases(V2) = { TAG }
1196 may-aliases(V2) = { TAG } */
1198 static void
1199 group_aliases_into (tree tag, bitmap tag_aliases, struct alias_info *ai)
1201 unsigned int i;
1202 var_ann_t tag_ann = var_ann (tag);
1203 size_t num_tag_refs = NUM_REFERENCES (tag_ann);
1204 bitmap_iterator bi;
1206 EXECUTE_IF_SET_IN_BITMAP (tag_aliases, 0, i, bi)
1208 tree var = referenced_var (i);
1209 var_ann_t ann = var_ann (var);
1211 /* Make TAG the unique alias of VAR. */
1212 ann->is_alias_tag = 0;
1213 ann->may_aliases = NULL;
1215 /* Note that VAR and TAG may be the same if the function has no
1216 addressable variables (see the discussion at the end of
1217 setup_pointers_and_addressables). */
1218 if (var != tag)
1219 add_may_alias (var, tag);
1221 /* Reduce total number of virtual operands contributed
1222 by TAG on behalf of VAR. Notice that the references to VAR
1223 itself won't be removed. We will merely replace them with
1224 references to TAG. */
1225 ai->total_alias_vops -= num_tag_refs;
1228 /* We have reduced the number of virtual operands that TAG makes on
1229 behalf of all the variables formerly aliased with it. However,
1230 we have also "removed" all the virtual operands for TAG itself,
1231 so we add them back. */
1232 ai->total_alias_vops += num_tag_refs;
1234 /* TAG no longer has any aliases. */
1235 tag_ann->may_aliases = NULL;
1239 /* Group may-aliases sets to reduce the number of virtual operands due
1240 to aliasing.
1242 1- Sort the list of pointers in decreasing number of contributed
1243 virtual operands.
1245 2- Take the first entry in AI->POINTERS and revert the role of
1246 the memory tag and its aliases. Usually, whenever an aliased
1247 variable Vi is found to alias with a memory tag T, we add Vi
1248 to the may-aliases set for T. Meaning that after alias
1249 analysis, we will have:
1251 may-aliases(T) = { V1, V2, V3, ..., Vn }
1253 This means that every statement that references T, will get 'n'
1254 virtual operands for each of the Vi tags. But, when alias
1255 grouping is enabled, we make T an alias tag and add it to the
1256 alias set of all the Vi variables:
1258 may-aliases(V1) = { T }
1259 may-aliases(V2) = { T }
1261 may-aliases(Vn) = { T }
1263 This has two effects: (a) statements referencing T will only get
1264 a single virtual operand, and, (b) all the variables Vi will now
1265 appear to alias each other. So, we lose alias precision to
1266 improve compile time. But, in theory, a program with such a high
1267 level of aliasing should not be very optimizable in the first
1268 place.
1270 3- Since variables may be in the alias set of more than one
1271 memory tag, the grouping done in step (2) needs to be extended
1272 to all the memory tags that have a non-empty intersection with
1273 the may-aliases set of tag T. For instance, if we originally
1274 had these may-aliases sets:
1276 may-aliases(T) = { V1, V2, V3 }
1277 may-aliases(R) = { V2, V4 }
1279 In step (2) we would have reverted the aliases for T as:
1281 may-aliases(V1) = { T }
1282 may-aliases(V2) = { T }
1283 may-aliases(V3) = { T }
1285 But note that now V2 is no longer aliased with R. We could
1286 add R to may-aliases(V2), but we are in the process of
1287 grouping aliases to reduce virtual operands so what we do is
1288 add V4 to the grouping to obtain:
1290 may-aliases(V1) = { T }
1291 may-aliases(V2) = { T }
1292 may-aliases(V3) = { T }
1293 may-aliases(V4) = { T }
1295 4- If the total number of virtual operands due to aliasing is
1296 still above the threshold set by max-alias-vops, go back to (2). */
1298 static void
1299 group_aliases (struct alias_info *ai)
1301 size_t i;
1303 /* Sort the POINTERS array in descending order of contributed
1304 virtual operands. */
1305 qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1306 total_alias_vops_cmp);
1308 /* For every pointer in AI->POINTERS, reverse the roles of its tag
1309 and the tag's may-aliases set. */
1310 for (i = 0; i < ai->num_pointers; i++)
1312 size_t j;
1313 tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1314 bitmap tag1_aliases = ai->pointers[i]->may_aliases;
1316 /* Skip tags that have been grouped already. */
1317 if (ai->pointers[i]->grouped_p)
1318 continue;
1320 /* See if TAG1 had any aliases in common with other type tags.
1321 If we find a TAG2 with common aliases with TAG1, add TAG2's
1322 aliases into TAG1. */
1323 for (j = i + 1; j < ai->num_pointers; j++)
1325 bitmap tag2_aliases = ai->pointers[j]->may_aliases;
1327 if (bitmap_intersect_p (tag1_aliases, tag2_aliases))
1329 tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1331 bitmap_ior_into (tag1_aliases, tag2_aliases);
1333 /* TAG2 does not need its aliases anymore. */
1334 bitmap_clear (tag2_aliases);
1335 var_ann (tag2)->may_aliases = NULL;
1337 /* TAG1 is the unique alias of TAG2. */
1338 add_may_alias (tag2, tag1);
1340 ai->pointers[j]->grouped_p = true;
1344 /* Now group all the aliases we collected into TAG1. */
1345 group_aliases_into (tag1, tag1_aliases, ai);
1347 /* If we've reduced total number of virtual operands below the
1348 threshold, stop. */
1349 if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1350 break;
1353 /* Finally, all the variables that have been grouped cannot be in
1354 the may-alias set of name memory tags. Suppose that we have
1355 grouped the aliases in this code so that may-aliases(a) = TMT.20
1357 p_5 = &a;
1359 # a_9 = V_MAY_DEF <a_8>
1360 p_5->field = 0
1361 ... Several modifications to TMT.20 ...
1362 # VUSE <a_9>
1363 x_30 = p_5->field
1365 Since p_5 points to 'a', the optimizers will try to propagate 0
1366 into p_5->field, but that is wrong because there have been
1367 modifications to 'TMT.20' in between. To prevent this we have to
1368 replace 'a' with 'TMT.20' in the name tag of p_5. */
1369 for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1371 size_t j;
1372 tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1373 tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1374 VEC(tree,gc) *aliases;
1375 tree alias;
1377 if (name_tag == NULL_TREE)
1378 continue;
1380 aliases = var_ann (name_tag)->may_aliases;
1381 for (j = 0; VEC_iterate (tree, aliases, j, alias); j++)
1383 var_ann_t ann = var_ann (alias);
1385 if ((!MTAG_P (alias)
1386 || TREE_CODE (alias) == STRUCT_FIELD_TAG)
1387 && ann->may_aliases)
1389 tree new_alias;
1391 gcc_assert (VEC_length (tree, ann->may_aliases) == 1);
1393 new_alias = VEC_index (tree, ann->may_aliases, 0);
1394 replace_may_alias (name_tag, j, new_alias);
1399 if (dump_file)
1400 fprintf (dump_file,
1401 "%s: Total number of aliased vops after grouping: %ld%s\n",
1402 get_name (current_function_decl),
1403 ai->total_alias_vops,
1404 (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1408 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS. */
1410 static void
1411 create_alias_map_for (tree var, struct alias_info *ai)
1413 struct alias_map_d *alias_map;
1414 alias_map = XCNEW (struct alias_map_d);
1415 alias_map->var = var;
1416 alias_map->set = get_alias_set (var);
1417 ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1421 /* Create memory tags for all the dereferenced pointers and build the
1422 ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1423 sets. Based on the address escape and points-to information collected
1424 earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1425 variables whose address is not needed anymore. */
1427 static void
1428 setup_pointers_and_addressables (struct alias_info *ai)
1430 size_t n_vars, num_addressable_vars, num_pointers;
1431 referenced_var_iterator rvi;
1432 tree var;
1433 VEC (tree, heap) *varvec = NULL;
1434 safe_referenced_var_iterator srvi;
1436 /* Size up the arrays ADDRESSABLE_VARS and POINTERS. */
1437 num_addressable_vars = num_pointers = 0;
1439 FOR_EACH_REFERENCED_VAR (var, rvi)
1441 if (may_be_aliased (var))
1442 num_addressable_vars++;
1444 if (POINTER_TYPE_P (TREE_TYPE (var)))
1446 /* Since we don't keep track of volatile variables, assume that
1447 these pointers are used in indirect store operations. */
1448 if (TREE_THIS_VOLATILE (var))
1449 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
1451 num_pointers++;
1455 /* Create ADDRESSABLE_VARS and POINTERS. Note that these arrays are
1456 always going to be slightly bigger than we actually need them
1457 because some TREE_ADDRESSABLE variables will be marked
1458 non-addressable below and only pointers with unique type tags are
1459 going to be added to POINTERS. */
1460 ai->addressable_vars = XCNEWVEC (struct alias_map_d *, num_addressable_vars);
1461 ai->pointers = XCNEWVEC (struct alias_map_d *, num_pointers);
1462 ai->num_addressable_vars = 0;
1463 ai->num_pointers = 0;
1465 /* Since we will be creating type memory tags within this loop, cache the
1466 value of NUM_REFERENCED_VARS to avoid processing the additional tags
1467 unnecessarily. */
1468 n_vars = num_referenced_vars;
1470 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
1472 var_ann_t v_ann = var_ann (var);
1473 subvar_t svars;
1475 /* Name memory tags already have flow-sensitive aliasing
1476 information, so they need not be processed by
1477 compute_flow_insensitive_aliasing. Similarly, type memory
1478 tags are already accounted for when we process their
1479 associated pointer.
1481 Structure fields, on the other hand, have to have some of this
1482 information processed for them, but it's pointless to mark them
1483 non-addressable (since they are fake variables anyway). */
1484 if (MTAG_P (var) && TREE_CODE (var) != STRUCT_FIELD_TAG)
1485 continue;
1487 /* Remove the ADDRESSABLE flag from every addressable variable whose
1488 address is not needed anymore. This is caused by the propagation
1489 of ADDR_EXPR constants into INDIRECT_REF expressions and the
1490 removal of dead pointer assignments done by the early scalar
1491 cleanup passes. */
1492 if (TREE_ADDRESSABLE (var))
1494 if (!bitmap_bit_p (addressable_vars, DECL_UID (var))
1495 && TREE_CODE (var) != RESULT_DECL
1496 && !is_global_var (var))
1498 bool okay_to_mark = true;
1500 /* Since VAR is now a regular GIMPLE register, we will need
1501 to rename VAR into SSA afterwards. */
1502 mark_sym_for_renaming (var);
1504 /* If VAR can have sub-variables, and any of its
1505 sub-variables has its address taken, then we cannot
1506 remove the addressable flag from VAR. */
1507 if (var_can_have_subvars (var)
1508 && (svars = get_subvars_for_var (var)))
1510 subvar_t sv;
1512 for (sv = svars; sv; sv = sv->next)
1514 if (bitmap_bit_p (addressable_vars, DECL_UID (sv->var)))
1515 okay_to_mark = false;
1516 mark_sym_for_renaming (sv->var);
1520 /* The address of VAR is not needed, remove the
1521 addressable bit, so that it can be optimized as a
1522 regular variable. */
1523 if (okay_to_mark)
1524 mark_non_addressable (var);
1528 /* Global variables and addressable locals may be aliased. Create an
1529 entry in ADDRESSABLE_VARS for VAR. */
1530 if (may_be_aliased (var)
1531 && (!var_can_have_subvars (var)
1532 || get_subvars_for_var (var) == NULL))
1534 create_alias_map_for (var, ai);
1535 mark_sym_for_renaming (var);
1538 /* Add pointer variables that have been dereferenced to the POINTERS
1539 array and create a type memory tag for them. */
1540 if (POINTER_TYPE_P (TREE_TYPE (var)))
1542 if ((bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var))
1543 || bitmap_bit_p (ai->dereferenced_ptrs_load, DECL_UID (var))))
1545 tree tag;
1546 var_ann_t t_ann;
1548 /* If pointer VAR still doesn't have a memory tag
1549 associated with it, create it now or re-use an
1550 existing one. */
1551 tag = get_tmt_for (var, ai);
1552 t_ann = var_ann (tag);
1554 /* The type tag will need to be renamed into SSA
1555 afterwards. Note that we cannot do this inside
1556 get_tmt_for because aliasing may run multiple times
1557 and we only create type tags the first time. */
1558 mark_sym_for_renaming (tag);
1560 /* Similarly, if pointer VAR used to have another type
1561 tag, we will need to process it in the renamer to
1562 remove the stale virtual operands. */
1563 if (v_ann->type_mem_tag)
1564 mark_sym_for_renaming (v_ann->type_mem_tag);
1566 /* Associate the tag with pointer VAR. */
1567 v_ann->type_mem_tag = tag;
1569 /* If pointer VAR has been used in a store operation,
1570 then its memory tag must be marked as written-to. */
1571 if (bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var)))
1572 bitmap_set_bit (ai->written_vars, DECL_UID (tag));
1574 /* All the dereferences of pointer VAR count as
1575 references of TAG. Since TAG can be associated with
1576 several pointers, add the dereferences of VAR to the
1577 TAG. */
1578 NUM_REFERENCES_SET (t_ann,
1579 NUM_REFERENCES (t_ann)
1580 + NUM_REFERENCES (v_ann));
1582 else
1584 /* The pointer has not been dereferenced. If it had a
1585 type memory tag, remove it and mark the old tag for
1586 renaming to remove it out of the IL. */
1587 var_ann_t ann = var_ann (var);
1588 tree tag = ann->type_mem_tag;
1589 if (tag)
1591 mark_sym_for_renaming (tag);
1592 ann->type_mem_tag = NULL_TREE;
1597 VEC_free (tree, heap, varvec);
1601 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1602 every call site, we need to emit V_MAY_DEF expressions to represent the
1603 clobbering effects of the call for variables whose address escapes the
1604 current function.
1606 One approach is to group all call-clobbered variables into a single
1607 representative that is used as an alias of every call-clobbered variable
1608 (.GLOBAL_VAR). This works well, but it ties the optimizer hands because
1609 references to any call clobbered variable is a reference to .GLOBAL_VAR.
1611 The second approach is to emit a clobbering V_MAY_DEF for every
1612 call-clobbered variable at call sites. This is the preferred way in terms
1613 of optimization opportunities but it may create too many V_MAY_DEF operands
1614 if there are many call clobbered variables and function calls in the
1615 function.
1617 To decide whether or not to use .GLOBAL_VAR we multiply the number of
1618 function calls found by the number of call-clobbered variables. If that
1619 product is beyond a certain threshold, as determined by the parameterized
1620 values shown below, we use .GLOBAL_VAR.
1622 FIXME. This heuristic should be improved. One idea is to use several
1623 .GLOBAL_VARs of different types instead of a single one. The thresholds
1624 have been derived from a typical bootstrap cycle, including all target
1625 libraries. Compile times were found increase by ~1% compared to using
1626 .GLOBAL_VAR. */
1628 static void
1629 maybe_create_global_var (struct alias_info *ai)
1631 unsigned i, n_clobbered;
1632 bitmap_iterator bi;
1634 /* No need to create it, if we have one already. */
1635 if (global_var == NULL_TREE)
1637 /* Count all the call-clobbered variables. */
1638 n_clobbered = 0;
1639 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1641 n_clobbered++;
1644 /* If the number of virtual operands that would be needed to
1645 model all the call-clobbered variables is larger than
1646 GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1648 Also create .GLOBAL_VAR if there are no call-clobbered
1649 variables and the program contains a mixture of pure/const
1650 and regular function calls. This is to avoid the problem
1651 described in PR 20115:
1653 int X;
1654 int func_pure (void) { return X; }
1655 int func_non_pure (int a) { X += a; }
1656 int foo ()
1658 int a = func_pure ();
1659 func_non_pure (a);
1660 a = func_pure ();
1661 return a;
1664 Since foo() has no call-clobbered variables, there is
1665 no relationship between the calls to func_pure and
1666 func_non_pure. Since func_pure has no side-effects, value
1667 numbering optimizations elide the second call to func_pure.
1668 So, if we have some pure/const and some regular calls in the
1669 program we create .GLOBAL_VAR to avoid missing these
1670 relations. */
1671 if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1672 || (n_clobbered == 0
1673 && ai->num_calls_found > 0
1674 && ai->num_pure_const_calls_found > 0
1675 && ai->num_calls_found > ai->num_pure_const_calls_found))
1676 create_global_var ();
1679 /* Mark all call-clobbered symbols for renaming. Since the initial
1680 rewrite into SSA ignored all call sites, we may need to rename
1681 .GLOBAL_VAR and the call-clobbered variables. */
1682 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1684 tree var = referenced_var (i);
1686 /* If the function has calls to clobbering functions and
1687 .GLOBAL_VAR has been created, make it an alias for all
1688 call-clobbered variables. */
1689 if (global_var && var != global_var)
1691 add_may_alias (var, global_var);
1692 gcc_assert (!get_subvars_for_var (var));
1695 mark_sym_for_renaming (var);
1700 /* Return TRUE if pointer PTR may point to variable VAR.
1702 MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1703 This is needed because when checking for type conflicts we are
1704 interested in the alias set of the memory location pointed-to by
1705 PTR. The alias set of PTR itself is irrelevant.
1707 VAR_ALIAS_SET is the alias set for VAR. */
1709 static bool
1710 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1711 tree var, HOST_WIDE_INT var_alias_set,
1712 bool alias_set_only)
1714 tree mem;
1716 alias_stats.alias_queries++;
1717 alias_stats.simple_queries++;
1719 /* By convention, a variable cannot alias itself. */
1720 mem = var_ann (ptr)->type_mem_tag;
1721 if (mem == var)
1723 alias_stats.alias_noalias++;
1724 alias_stats.simple_resolved++;
1725 return false;
1728 /* If -fargument-noalias-global is >1, pointer arguments may
1729 not point to global variables. */
1730 if (flag_argument_noalias > 1 && is_global_var (var)
1731 && TREE_CODE (ptr) == PARM_DECL)
1733 alias_stats.alias_noalias++;
1734 alias_stats.simple_resolved++;
1735 return false;
1738 /* If either MEM or VAR is a read-only global and the other one
1739 isn't, then PTR cannot point to VAR. */
1740 if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1741 || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1743 alias_stats.alias_noalias++;
1744 alias_stats.simple_resolved++;
1745 return false;
1748 gcc_assert (TREE_CODE (mem) == TYPE_MEMORY_TAG);
1750 alias_stats.tbaa_queries++;
1752 /* If the alias sets don't conflict then MEM cannot alias VAR. */
1753 if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1755 alias_stats.alias_noalias++;
1756 alias_stats.tbaa_resolved++;
1757 return false;
1760 /* If var is a record or union type, ptr cannot point into var
1761 unless there is some operation explicit address operation in the
1762 program that can reference a field of the ptr's dereferenced
1763 type. This also assumes that the types of both var and ptr are
1764 contained within the compilation unit, and that there is no fancy
1765 addressing arithmetic associated with any of the types
1766 involved. */
1768 if ((mem_alias_set != 0) && (var_alias_set != 0))
1770 tree ptr_type = TREE_TYPE (ptr);
1771 tree var_type = TREE_TYPE (var);
1773 /* The star count is -1 if the type at the end of the pointer_to
1774 chain is not a record or union type. */
1775 if ((!alias_set_only) &&
1776 ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
1778 int ptr_star_count = 0;
1780 /* Ipa_type_escape_star_count_of_interesting_type is a little to
1781 restrictive for the pointer type, need to allow pointers to
1782 primitive types as long as those types cannot be pointers
1783 to everything. */
1784 while (POINTER_TYPE_P (ptr_type))
1785 /* Strip the *'s off. */
1787 ptr_type = TREE_TYPE (ptr_type);
1788 ptr_star_count++;
1791 /* There does not appear to be a better test to see if the
1792 pointer type was one of the pointer to everything
1793 types. */
1795 if (ptr_star_count > 0)
1797 alias_stats.structnoaddress_queries++;
1798 if (ipa_type_escape_field_does_not_clobber_p (var_type,
1799 TREE_TYPE (ptr)))
1801 alias_stats.structnoaddress_resolved++;
1802 alias_stats.alias_noalias++;
1803 return false;
1806 else if (ptr_star_count == 0)
1808 /* If ptr_type was not really a pointer to type, it cannot
1809 alias. */
1810 alias_stats.structnoaddress_queries++;
1811 alias_stats.structnoaddress_resolved++;
1812 alias_stats.alias_noalias++;
1813 return false;
1818 alias_stats.alias_mayalias++;
1819 return true;
1823 /* Add ALIAS to the set of variables that may alias VAR. */
1825 static void
1826 add_may_alias (tree var, tree alias)
1828 size_t i;
1829 var_ann_t v_ann = get_var_ann (var);
1830 var_ann_t a_ann = get_var_ann (alias);
1831 tree al;
1833 /* Don't allow self-referential aliases. */
1834 gcc_assert (var != alias);
1836 /* ALIAS must be addressable if it's being added to an alias set. */
1837 #if 1
1838 TREE_ADDRESSABLE (alias) = 1;
1839 #else
1840 gcc_assert (may_be_aliased (alias));
1841 #endif
1843 if (v_ann->may_aliases == NULL)
1844 v_ann->may_aliases = VEC_alloc (tree, gc, 2);
1846 /* Avoid adding duplicates. */
1847 for (i = 0; VEC_iterate (tree, v_ann->may_aliases, i, al); i++)
1848 if (alias == al)
1849 return;
1851 VEC_safe_push (tree, gc, v_ann->may_aliases, alias);
1852 a_ann->is_alias_tag = 1;
1856 /* Replace alias I in the alias sets of VAR with NEW_ALIAS. */
1858 static void
1859 replace_may_alias (tree var, size_t i, tree new_alias)
1861 var_ann_t v_ann = var_ann (var);
1862 VEC_replace (tree, v_ann->may_aliases, i, new_alias);
1866 /* Mark pointer PTR as pointing to an arbitrary memory location. */
1868 static void
1869 set_pt_anything (tree ptr)
1871 struct ptr_info_def *pi = get_ptr_info (ptr);
1873 pi->pt_anything = 1;
1874 pi->pt_vars = NULL;
1876 /* The pointer used to have a name tag, but we now found it pointing
1877 to an arbitrary location. The name tag needs to be renamed and
1878 disassociated from PTR. */
1879 if (pi->name_mem_tag)
1881 mark_sym_for_renaming (pi->name_mem_tag);
1882 pi->name_mem_tag = NULL_TREE;
1887 /* Return true if STMT is an "escape" site from the current function. Escape
1888 sites those statements which might expose the address of a variable
1889 outside the current function. STMT is an escape site iff:
1891 1- STMT is a function call, or
1892 2- STMT is an __asm__ expression, or
1893 3- STMT is an assignment to a non-local variable, or
1894 4- STMT is a return statement.
1896 AI points to the alias information collected so far.
1898 Return the type of escape site found, if we found one, or NO_ESCAPE
1899 if none. */
1901 enum escape_type
1902 is_escape_site (tree stmt, struct alias_info *ai)
1904 tree call = get_call_expr_in (stmt);
1905 if (call != NULL_TREE)
1907 ai->num_calls_found++;
1909 if (!TREE_SIDE_EFFECTS (call))
1911 ai->num_pure_const_calls_found++;
1912 return ESCAPE_TO_PURE_CONST;
1915 return ESCAPE_TO_CALL;
1917 else if (TREE_CODE (stmt) == ASM_EXPR)
1918 return ESCAPE_TO_ASM;
1919 else if (TREE_CODE (stmt) == MODIFY_EXPR)
1921 tree lhs = TREE_OPERAND (stmt, 0);
1923 /* Get to the base of _REF nodes. */
1924 if (TREE_CODE (lhs) != SSA_NAME)
1925 lhs = get_base_address (lhs);
1927 /* If we couldn't recognize the LHS of the assignment, assume that it
1928 is a non-local store. */
1929 if (lhs == NULL_TREE)
1930 return ESCAPE_UNKNOWN;
1932 /* If the RHS is a conversion between a pointer and an integer, the
1933 pointer escapes since we can't track the integer. */
1934 if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
1935 || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
1936 || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
1937 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
1938 (TREE_OPERAND (stmt, 1), 0)))
1939 && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1940 return ESCAPE_BAD_CAST;
1942 /* If the LHS is an SSA name, it can't possibly represent a non-local
1943 memory store. */
1944 if (TREE_CODE (lhs) == SSA_NAME)
1945 return NO_ESCAPE;
1947 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
1948 local variables we cannot be sure if it will escape, because we
1949 don't have information about objects not in SSA form. Need to
1950 implement something along the lines of
1952 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
1953 Midkiff, ``Escape analysis for java,'' in Proceedings of the
1954 Conference on Object-Oriented Programming Systems, Languages, and
1955 Applications (OOPSLA), pp. 1-19, 1999. */
1956 return ESCAPE_STORED_IN_GLOBAL;
1958 else if (TREE_CODE (stmt) == RETURN_EXPR)
1959 return ESCAPE_TO_RETURN;
1961 return NO_ESCAPE;
1964 /* Create a new memory tag of type TYPE.
1965 Does NOT push it into the current binding. */
1967 static tree
1968 create_tag_raw (enum tree_code code, tree type, const char *prefix)
1970 tree tmp_var;
1971 tree new_type;
1973 /* Make the type of the variable writable. */
1974 new_type = build_type_variant (type, 0, 0);
1975 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
1977 tmp_var = build_decl (code, create_tmp_var_name (prefix),
1978 type);
1979 /* Make the variable writable. */
1980 TREE_READONLY (tmp_var) = 0;
1982 /* It doesn't start out global. */
1983 MTAG_GLOBAL (tmp_var) = 0;
1984 TREE_STATIC (tmp_var) = 0;
1985 TREE_USED (tmp_var) = 1;
1987 return tmp_var;
1990 /* Create a new memory tag of type TYPE. If IS_TYPE_TAG is true, the tag
1991 is considered to represent all the pointers whose pointed-to types are
1992 in the same alias set class. Otherwise, the tag represents a single
1993 SSA_NAME pointer variable. */
1995 static tree
1996 create_memory_tag (tree type, bool is_type_tag)
1998 var_ann_t ann;
1999 tree tag = create_tag_raw (is_type_tag ? TYPE_MEMORY_TAG : NAME_MEMORY_TAG,
2000 type, (is_type_tag) ? "TMT" : "NMT");
2002 /* By default, memory tags are local variables. Alias analysis will
2003 determine whether they should be considered globals. */
2004 DECL_CONTEXT (tag) = current_function_decl;
2006 /* Memory tags are by definition addressable. */
2007 TREE_ADDRESSABLE (tag) = 1;
2009 ann = get_var_ann (tag);
2010 ann->type_mem_tag = NULL_TREE;
2012 /* Add the tag to the symbol table. */
2013 add_referenced_tmp_var (tag);
2015 return tag;
2019 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
2020 This is used if P_i has been found to point to a specific set of
2021 variables or to a non-aliased memory location like the address returned
2022 by malloc functions. */
2024 static tree
2025 get_nmt_for (tree ptr)
2027 struct ptr_info_def *pi = get_ptr_info (ptr);
2028 tree tag = pi->name_mem_tag;
2030 if (tag == NULL_TREE)
2031 tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
2032 return tag;
2036 /* Return the type memory tag associated to pointer PTR. A memory tag is an
2037 artificial variable that represents the memory location pointed-to by
2038 PTR. It is used to model the effects of pointer de-references on
2039 addressable variables.
2041 AI points to the data gathered during alias analysis. This function
2042 populates the array AI->POINTERS. */
2044 static tree
2045 get_tmt_for (tree ptr, struct alias_info *ai)
2047 size_t i;
2048 tree tag;
2049 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2050 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2052 /* To avoid creating unnecessary memory tags, only create one memory tag
2053 per alias set class. Note that it may be tempting to group
2054 memory tags based on conflicting alias sets instead of
2055 equivalence. That would be wrong because alias sets are not
2056 necessarily transitive (as demonstrated by the libstdc++ test
2057 23_containers/vector/cons/4.cc). Given three alias sets A, B, C
2058 such that conflicts (A, B) == true and conflicts (A, C) == true,
2059 it does not necessarily follow that conflicts (B, C) == true. */
2060 for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
2062 struct alias_map_d *curr = ai->pointers[i];
2063 tree curr_tag = var_ann (curr->var)->type_mem_tag;
2064 if (tag_set == curr->set)
2066 tag = curr_tag;
2067 break;
2071 /* If VAR cannot alias with any of the existing memory tags, create a new
2072 tag for PTR and add it to the POINTERS array. */
2073 if (tag == NULL_TREE)
2075 struct alias_map_d *alias_map;
2077 /* If PTR did not have a type tag already, create a new TMT.*
2078 artificial variable representing the memory location
2079 pointed-to by PTR. */
2080 if (var_ann (ptr)->type_mem_tag == NULL_TREE)
2081 tag = create_memory_tag (tag_type, true);
2082 else
2083 tag = var_ann (ptr)->type_mem_tag;
2085 /* Add PTR to the POINTERS array. Note that we are not interested in
2086 PTR's alias set. Instead, we cache the alias set for the memory that
2087 PTR points to. */
2088 alias_map = XCNEW (struct alias_map_d);
2089 alias_map->var = ptr;
2090 alias_map->set = tag_set;
2091 ai->pointers[ai->num_pointers++] = alias_map;
2094 /* If the pointed-to type is volatile, so is the tag. */
2095 TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
2097 /* Make sure that the type tag has the same alias set as the
2098 pointed-to type. */
2099 gcc_assert (tag_set == get_alias_set (tag));
2101 return tag;
2105 /* Create GLOBAL_VAR, an artificial global variable to act as a
2106 representative of all the variables that may be clobbered by function
2107 calls. */
2109 static void
2110 create_global_var (void)
2112 global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
2113 void_type_node);
2114 DECL_ARTIFICIAL (global_var) = 1;
2115 TREE_READONLY (global_var) = 0;
2116 DECL_EXTERNAL (global_var) = 1;
2117 TREE_STATIC (global_var) = 1;
2118 TREE_USED (global_var) = 1;
2119 DECL_CONTEXT (global_var) = NULL_TREE;
2120 TREE_THIS_VOLATILE (global_var) = 0;
2121 TREE_ADDRESSABLE (global_var) = 0;
2123 create_var_ann (global_var);
2124 mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
2125 add_referenced_tmp_var (global_var);
2126 mark_sym_for_renaming (global_var);
2130 /* Dump alias statistics on FILE. */
2132 static void
2133 dump_alias_stats (FILE *file)
2135 const char *funcname
2136 = lang_hooks.decl_printable_name (current_function_decl, 2);
2137 fprintf (file, "\nAlias statistics for %s\n\n", funcname);
2138 fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
2139 fprintf (file, "Total alias mayalias results:\t%u\n",
2140 alias_stats.alias_mayalias);
2141 fprintf (file, "Total alias noalias results:\t%u\n",
2142 alias_stats.alias_noalias);
2143 fprintf (file, "Total simple queries:\t%u\n",
2144 alias_stats.simple_queries);
2145 fprintf (file, "Total simple resolved:\t%u\n",
2146 alias_stats.simple_resolved);
2147 fprintf (file, "Total TBAA queries:\t%u\n",
2148 alias_stats.tbaa_queries);
2149 fprintf (file, "Total TBAA resolved:\t%u\n",
2150 alias_stats.tbaa_resolved);
2151 fprintf (file, "Total non-addressable structure type queries:\t%u\n",
2152 alias_stats.structnoaddress_queries);
2153 fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
2154 alias_stats.structnoaddress_resolved);
2158 /* Dump alias information on FILE. */
2160 void
2161 dump_alias_info (FILE *file)
2163 size_t i;
2164 const char *funcname
2165 = lang_hooks.decl_printable_name (current_function_decl, 2);
2166 referenced_var_iterator rvi;
2167 tree var;
2169 fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
2171 fprintf (file, "Aliased symbols\n\n");
2173 FOR_EACH_REFERENCED_VAR (var, rvi)
2175 if (may_be_aliased (var))
2176 dump_variable (file, var);
2179 fprintf (file, "\nDereferenced pointers\n\n");
2181 FOR_EACH_REFERENCED_VAR (var, rvi)
2183 var_ann_t ann = var_ann (var);
2184 if (ann->type_mem_tag)
2185 dump_variable (file, var);
2188 fprintf (file, "\nType memory tags\n\n");
2190 FOR_EACH_REFERENCED_VAR (var, rvi)
2192 if (TREE_CODE (var) == TYPE_MEMORY_TAG)
2193 dump_variable (file, var);
2196 fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
2198 fprintf (file, "SSA_NAME pointers\n\n");
2199 for (i = 1; i < num_ssa_names; i++)
2201 tree ptr = ssa_name (i);
2202 struct ptr_info_def *pi;
2204 if (ptr == NULL_TREE)
2205 continue;
2207 pi = SSA_NAME_PTR_INFO (ptr);
2208 if (!SSA_NAME_IN_FREE_LIST (ptr)
2209 && pi
2210 && pi->name_mem_tag)
2211 dump_points_to_info_for (file, ptr);
2214 fprintf (file, "\nName memory tags\n\n");
2216 FOR_EACH_REFERENCED_VAR (var, rvi)
2218 if (TREE_CODE (var) == NAME_MEMORY_TAG)
2219 dump_variable (file, var);
2222 fprintf (file, "\n");
2226 /* Dump alias information on stderr. */
2228 void
2229 debug_alias_info (void)
2231 dump_alias_info (stderr);
2235 /* Return the alias information associated with pointer T. It creates a
2236 new instance if none existed. */
2238 struct ptr_info_def *
2239 get_ptr_info (tree t)
2241 struct ptr_info_def *pi;
2243 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2245 pi = SSA_NAME_PTR_INFO (t);
2246 if (pi == NULL)
2248 pi = GGC_NEW (struct ptr_info_def);
2249 memset ((void *)pi, 0, sizeof (*pi));
2250 SSA_NAME_PTR_INFO (t) = pi;
2253 return pi;
2257 /* Dump points-to information for SSA_NAME PTR into FILE. */
2259 void
2260 dump_points_to_info_for (FILE *file, tree ptr)
2262 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2264 print_generic_expr (file, ptr, dump_flags);
2266 if (pi)
2268 if (pi->name_mem_tag)
2270 fprintf (file, ", name memory tag: ");
2271 print_generic_expr (file, pi->name_mem_tag, dump_flags);
2274 if (pi->is_dereferenced)
2275 fprintf (file, ", is dereferenced");
2277 if (pi->value_escapes_p)
2278 fprintf (file, ", its value escapes");
2280 if (pi->pt_anything)
2281 fprintf (file, ", points-to anything");
2283 if (pi->pt_null)
2284 fprintf (file, ", points-to NULL");
2286 if (pi->pt_vars)
2288 unsigned ix;
2289 bitmap_iterator bi;
2291 fprintf (file, ", points-to vars: { ");
2292 EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2294 print_generic_expr (file, referenced_var (ix), dump_flags);
2295 fprintf (file, " ");
2297 fprintf (file, "}");
2301 fprintf (file, "\n");
2305 /* Dump points-to information for VAR into stderr. */
2307 void
2308 debug_points_to_info_for (tree var)
2310 dump_points_to_info_for (stderr, var);
2314 /* Dump points-to information into FILE. NOTE: This function is slow, as
2315 it needs to traverse the whole CFG looking for pointer SSA_NAMEs. */
2317 void
2318 dump_points_to_info (FILE *file)
2320 basic_block bb;
2321 block_stmt_iterator si;
2322 ssa_op_iter iter;
2323 const char *fname =
2324 lang_hooks.decl_printable_name (current_function_decl, 2);
2325 referenced_var_iterator rvi;
2326 tree var;
2328 fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2330 /* First dump points-to information for the default definitions of
2331 pointer variables. This is necessary because default definitions are
2332 not part of the code. */
2333 FOR_EACH_REFERENCED_VAR (var, rvi)
2335 if (POINTER_TYPE_P (TREE_TYPE (var)))
2337 tree def = default_def (var);
2338 if (def)
2339 dump_points_to_info_for (file, def);
2343 /* Dump points-to information for every pointer defined in the program. */
2344 FOR_EACH_BB (bb)
2346 tree phi;
2348 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2350 tree ptr = PHI_RESULT (phi);
2351 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2352 dump_points_to_info_for (file, ptr);
2355 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2357 tree stmt = bsi_stmt (si);
2358 tree def;
2359 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2360 if (POINTER_TYPE_P (TREE_TYPE (def)))
2361 dump_points_to_info_for (file, def);
2365 fprintf (file, "\n");
2369 /* Dump points-to info pointed to by PTO into STDERR. */
2371 void
2372 debug_points_to_info (void)
2374 dump_points_to_info (stderr);
2377 /* Dump to FILE the list of variables that may be aliasing VAR. */
2379 void
2380 dump_may_aliases_for (FILE *file, tree var)
2382 VEC(tree, gc) *aliases;
2384 if (TREE_CODE (var) == SSA_NAME)
2385 var = SSA_NAME_VAR (var);
2387 aliases = var_ann (var)->may_aliases;
2388 if (aliases)
2390 size_t i;
2391 tree al;
2392 fprintf (file, "{ ");
2393 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2395 print_generic_expr (file, al, dump_flags);
2396 fprintf (file, " ");
2398 fprintf (file, "}");
2403 /* Dump to stderr the list of variables that may be aliasing VAR. */
2405 void
2406 debug_may_aliases_for (tree var)
2408 dump_may_aliases_for (stderr, var);
2411 /* Return true if VAR may be aliased. */
2413 bool
2414 may_be_aliased (tree var)
2416 /* Obviously. */
2417 if (TREE_ADDRESSABLE (var))
2418 return true;
2420 /* Globally visible variables can have their addresses taken by other
2421 translation units. */
2423 if (MTAG_P (var)
2424 && (MTAG_GLOBAL (var) || TREE_PUBLIC (var)))
2425 return true;
2426 else if (!MTAG_P (var)
2427 && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
2428 return true;
2430 /* Automatic variables can't have their addresses escape any other way.
2431 This must be after the check for global variables, as extern declarations
2432 do not have TREE_STATIC set. */
2433 if (!TREE_STATIC (var))
2434 return false;
2436 /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2437 of address-of operators, and so we can trust TREE_ADDRESSABLE. Otherwise
2438 we can only be sure the variable isn't addressable if it's local to the
2439 current function. */
2440 if (flag_unit_at_a_time)
2441 return false;
2442 if (decl_function_context (var) == current_function_decl)
2443 return false;
2445 return true;
2449 /* Given two symbols return TRUE if one is in the alias set of the other. */
2450 bool
2451 is_aliased_with (tree tag, tree sym)
2453 size_t i;
2454 VEC(tree,gc) *aliases;
2455 tree al;
2457 if (var_ann (sym)->is_alias_tag)
2459 aliases = var_ann (tag)->may_aliases;
2461 if (aliases == NULL)
2462 return false;
2464 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2465 if (al == sym)
2466 return true;
2468 else
2470 aliases = var_ann (sym)->may_aliases;
2472 if (aliases == NULL)
2473 return false;
2475 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2476 if (al == tag)
2477 return true;
2480 return false;
2484 /* Add VAR to the list of may-aliases of PTR's type tag. If PTR
2485 doesn't already have a type tag, create one. */
2487 void
2488 add_type_alias (tree ptr, tree var)
2490 VEC(tree, gc) *aliases;
2491 tree tag, al;
2492 var_ann_t ann = var_ann (ptr);
2493 subvar_t svars;
2494 VEC (tree, heap) *varvec = NULL;
2495 unsigned i;
2497 if (ann->type_mem_tag == NULL_TREE)
2499 tree q = NULL_TREE;
2500 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2501 HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2502 safe_referenced_var_iterator rvi;
2504 /* PTR doesn't have a type tag, create a new one and add VAR to
2505 the new tag's alias set.
2507 FIXME, This is slower than necessary. We need to determine
2508 whether there is another pointer Q with the same alias set as
2509 PTR. This could be sped up by having type tags associated
2510 with types. */
2511 FOR_EACH_REFERENCED_VAR_SAFE (q, varvec, rvi)
2513 if (POINTER_TYPE_P (TREE_TYPE (q))
2514 && tag_set == get_alias_set (TREE_TYPE (TREE_TYPE (q))))
2516 /* Found another pointer Q with the same alias set as
2517 the PTR's pointed-to type. If Q has a type tag, use
2518 it. Otherwise, create a new memory tag for PTR. */
2519 var_ann_t ann1 = var_ann (q);
2520 if (ann1->type_mem_tag)
2521 ann->type_mem_tag = ann1->type_mem_tag;
2522 else
2523 ann->type_mem_tag = create_memory_tag (tag_type, true);
2524 goto found_tag;
2528 /* Couldn't find any other pointer with a type tag we could use.
2529 Create a new memory tag for PTR. */
2530 ann->type_mem_tag = create_memory_tag (tag_type, true);
2533 found_tag:
2534 /* If VAR is not already PTR's type tag, add it to the may-alias set
2535 for PTR's type tag. */
2536 gcc_assert (!MTAG_P (var));
2537 tag = ann->type_mem_tag;
2539 /* If VAR has subvars, add the subvars to the tag instead of the
2540 actual var. */
2541 if (var_can_have_subvars (var)
2542 && (svars = get_subvars_for_var (var)))
2544 subvar_t sv;
2545 for (sv = svars; sv; sv = sv->next)
2546 add_may_alias (tag, sv->var);
2548 else
2549 add_may_alias (tag, var);
2551 /* TAG and its set of aliases need to be marked for renaming. */
2552 mark_sym_for_renaming (tag);
2553 if ((aliases = var_ann (tag)->may_aliases) != NULL)
2555 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2556 mark_sym_for_renaming (al);
2559 /* If we had grouped aliases, VAR may have aliases of its own. Mark
2560 them for renaming as well. Other statements referencing the
2561 aliases of VAR will need to be updated. */
2562 if ((aliases = var_ann (var)->may_aliases) != NULL)
2564 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2565 mark_sym_for_renaming (al);
2567 VEC_free (tree, heap, varvec);
2571 /* Create a new type tag for PTR. Construct the may-alias list of this type
2572 tag so that it has the aliasing of VAR.
2574 Note, the set of aliases represented by the new type tag are not marked
2575 for renaming. */
2577 void
2578 new_type_alias (tree ptr, tree var)
2580 var_ann_t p_ann = var_ann (ptr);
2581 tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2582 var_ann_t v_ann = var_ann (var);
2583 tree tag;
2584 subvar_t svars;
2586 gcc_assert (p_ann->type_mem_tag == NULL_TREE);
2587 gcc_assert (!MTAG_P (var));
2589 /* Add VAR to the may-alias set of PTR's new type tag. If VAR has
2590 subvars, add the subvars to the tag instead of the actual var. */
2591 if (var_can_have_subvars (var)
2592 && (svars = get_subvars_for_var (var)))
2594 subvar_t sv;
2596 tag = create_memory_tag (tag_type, true);
2597 p_ann->type_mem_tag = tag;
2599 for (sv = svars; sv; sv = sv->next)
2600 add_may_alias (tag, sv->var);
2602 else
2604 /* The following is based on code in add_stmt_operand to ensure that the
2605 same defs/uses/vdefs/vuses will be found after replacing a reference
2606 to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
2607 is the address of var. */
2608 VEC(tree, gc) *aliases = v_ann->may_aliases;
2610 if ((aliases != NULL)
2611 && (VEC_length (tree, aliases) == 1))
2613 tree ali = VEC_index (tree, aliases, 0);
2615 if (TREE_CODE (ali) == TYPE_MEMORY_TAG)
2617 p_ann->type_mem_tag = ali;
2618 return;
2622 tag = create_memory_tag (tag_type, true);
2623 p_ann->type_mem_tag = tag;
2625 if (aliases == NULL)
2626 add_may_alias (tag, var);
2627 else
2629 unsigned i;
2630 tree al;
2632 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
2633 add_may_alias (tag, al);
2640 /* This represents the used range of a variable. */
2642 typedef struct used_part
2644 HOST_WIDE_INT minused;
2645 HOST_WIDE_INT maxused;
2646 /* True if we have an explicit use/def of some portion of this variable,
2647 even if it is all of it. i.e. a.b = 5 or temp = a.b. */
2648 bool explicit_uses;
2649 /* True if we have an implicit use/def of some portion of this
2650 variable. Implicit uses occur when we can't tell what part we
2651 are referencing, and have to make conservative assumptions. */
2652 bool implicit_uses;
2653 /* True if the structure is only written to or taken its address. */
2654 bool write_only;
2655 } *used_part_t;
2657 /* An array of used_part structures, indexed by variable uid. */
2659 static htab_t used_portions;
2661 struct used_part_map
2663 unsigned int uid;
2664 used_part_t to;
2667 /* Return true if the uid in the two used part maps are equal. */
2669 static int
2670 used_part_map_eq (const void *va, const void *vb)
2672 const struct used_part_map *a = (const struct used_part_map *) va;
2673 const struct used_part_map *b = (const struct used_part_map *) vb;
2674 return (a->uid == b->uid);
2677 /* Hash a from uid in a used_part_map. */
2679 static unsigned int
2680 used_part_map_hash (const void *item)
2682 return ((const struct used_part_map *)item)->uid;
2685 /* Free a used part map element. */
2687 static void
2688 free_used_part_map (void *item)
2690 free (((struct used_part_map *)item)->to);
2691 free (item);
2694 /* Lookup a used_part structure for a UID. */
2696 static used_part_t
2697 up_lookup (unsigned int uid)
2699 struct used_part_map *h, in;
2700 in.uid = uid;
2701 h = (struct used_part_map *) htab_find_with_hash (used_portions, &in, uid);
2702 if (!h)
2703 return NULL;
2704 return h->to;
2707 /* Insert the pair UID, TO into the used part hashtable. */
2709 static void
2710 up_insert (unsigned int uid, used_part_t to)
2712 struct used_part_map *h;
2713 void **loc;
2715 h = XNEW (struct used_part_map);
2716 h->uid = uid;
2717 h->to = to;
2718 loc = htab_find_slot_with_hash (used_portions, h,
2719 uid, INSERT);
2720 if (*loc != NULL)
2721 free (*loc);
2722 *(struct used_part_map **) loc = h;
2726 /* Given a variable uid, UID, get or create the entry in the used portions
2727 table for the variable. */
2729 static used_part_t
2730 get_or_create_used_part_for (size_t uid)
2732 used_part_t up;
2733 if ((up = up_lookup (uid)) == NULL)
2735 up = XCNEW (struct used_part);
2736 up->minused = INT_MAX;
2737 up->maxused = 0;
2738 up->explicit_uses = false;
2739 up->implicit_uses = false;
2740 up->write_only = true;
2743 return up;
2747 /* Create and return a structure sub-variable for field type FIELD at
2748 offset OFFSET, with size SIZE, of variable VAR. */
2750 static tree
2751 create_sft (tree var, tree field, unsigned HOST_WIDE_INT offset,
2752 unsigned HOST_WIDE_INT size)
2754 var_ann_t ann;
2755 tree subvar = create_tag_raw (STRUCT_FIELD_TAG, field, "SFT");
2757 /* We need to copy the various flags from VAR to SUBVAR, so that
2758 they are is_global_var iff the original variable was. */
2759 DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
2760 MTAG_GLOBAL (subvar) = DECL_EXTERNAL (var);
2761 TREE_PUBLIC (subvar) = TREE_PUBLIC (var);
2762 TREE_STATIC (subvar) = TREE_STATIC (var);
2763 TREE_READONLY (subvar) = TREE_READONLY (var);
2765 /* Add the new variable to REFERENCED_VARS. */
2766 ann = get_var_ann (subvar);
2767 ann->type_mem_tag = NULL;
2768 add_referenced_tmp_var (subvar);
2769 SFT_PARENT_VAR (subvar) = var;
2770 SFT_OFFSET (subvar) = offset;
2771 SFT_SIZE (subvar) = size;
2772 return subvar;
2776 /* Given an aggregate VAR, create the subvariables that represent its
2777 fields. */
2779 static void
2780 create_overlap_variables_for (tree var)
2782 VEC(fieldoff_s,heap) *fieldstack = NULL;
2783 used_part_t up;
2784 size_t uid = DECL_UID (var);
2786 up = up_lookup (uid);
2787 if (!up
2788 || up->write_only)
2789 return;
2791 push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL);
2792 if (VEC_length (fieldoff_s, fieldstack) != 0)
2794 subvar_t *subvars;
2795 fieldoff_s *fo;
2796 bool notokay = false;
2797 int fieldcount = 0;
2798 int i;
2799 HOST_WIDE_INT lastfooffset = -1;
2800 HOST_WIDE_INT lastfosize = -1;
2801 tree lastfotype = NULL_TREE;
2803 /* Not all fields have DECL_SIZE set, and those that don't, we don't
2804 know their size, and thus, can't handle.
2805 The same is true of fields with DECL_SIZE that is not an integer
2806 constant (such as variable sized fields).
2807 Fields with offsets which are not constant will have an offset < 0
2808 We *could* handle fields that are constant sized arrays, but
2809 currently don't. Doing so would require some extra changes to
2810 tree-ssa-operands.c. */
2812 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
2814 if (!fo->size
2815 || TREE_CODE (fo->size) != INTEGER_CST
2816 || fo->offset < 0)
2818 notokay = true;
2819 break;
2821 fieldcount++;
2824 /* The current heuristic we use is as follows:
2825 If the variable has no used portions in this function, no
2826 structure vars are created for it.
2827 Otherwise,
2828 If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
2829 we always create structure vars for them.
2830 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2831 some explicit uses, we create structure vars for them.
2832 If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2833 no explicit uses, we do not create structure vars for them.
2836 if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
2837 && !up->explicit_uses)
2839 if (dump_file && (dump_flags & TDF_DETAILS))
2841 fprintf (dump_file, "Variable ");
2842 print_generic_expr (dump_file, var, 0);
2843 fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
2845 notokay = true;
2848 /* Bail out, if we can't create overlap variables. */
2849 if (notokay)
2851 VEC_free (fieldoff_s, heap, fieldstack);
2852 return;
2855 /* Otherwise, create the variables. */
2856 subvars = lookup_subvars_for_var (var);
2858 sort_fieldstack (fieldstack);
2860 for (i = VEC_length (fieldoff_s, fieldstack);
2861 VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
2863 subvar_t sv;
2864 HOST_WIDE_INT fosize;
2865 tree currfotype;
2867 fosize = TREE_INT_CST_LOW (fo->size);
2868 currfotype = fo->type;
2870 /* If this field isn't in the used portion,
2871 or it has the exact same offset and size as the last
2872 field, skip it. */
2874 if (((fo->offset <= up->minused
2875 && fo->offset + fosize <= up->minused)
2876 || fo->offset >= up->maxused)
2877 || (fo->offset == lastfooffset
2878 && fosize == lastfosize
2879 && currfotype == lastfotype))
2880 continue;
2881 sv = GGC_NEW (struct subvar);
2882 sv->next = *subvars;
2883 sv->var = create_sft (var, fo->type, fo->offset, fosize);
2885 if (dump_file)
2887 fprintf (dump_file, "structure field tag %s created for var %s",
2888 get_name (sv->var), get_name (var));
2889 fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
2890 SFT_OFFSET (sv->var));
2891 fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
2892 SFT_SIZE (sv->var));
2893 fprintf (dump_file, "\n");
2896 lastfotype = currfotype;
2897 lastfooffset = fo->offset;
2898 lastfosize = fosize;
2899 *subvars = sv;
2902 /* Once we have created subvars, the original is no longer call
2903 clobbered on its own. Its call clobbered status depends
2904 completely on the call clobbered status of the subvars.
2906 add_referenced_var in the above loop will take care of
2907 marking subvars of global variables as call clobbered for us
2908 to start, since they are global as well. */
2909 clear_call_clobbered (var);
2912 VEC_free (fieldoff_s, heap, fieldstack);
2916 /* Find the conservative answer to the question of what portions of what
2917 structures are used by this statement. We assume that if we have a
2918 component ref with a known size + offset, that we only need that part
2919 of the structure. For unknown cases, or cases where we do something
2920 to the whole structure, we assume we need to create fields for the
2921 entire structure. */
2923 static tree
2924 find_used_portions (tree *tp, int *walk_subtrees, void *lhs_p)
2926 switch (TREE_CODE (*tp))
2928 case MODIFY_EXPR:
2929 /* Recurse manually here to track whether the use is in the
2930 LHS of an assignment. */
2931 find_used_portions (&TREE_OPERAND (*tp, 0), walk_subtrees, tp);
2932 return find_used_portions (&TREE_OPERAND (*tp, 1), walk_subtrees, NULL);
2933 case REALPART_EXPR:
2934 case IMAGPART_EXPR:
2935 case COMPONENT_REF:
2936 case ARRAY_REF:
2938 HOST_WIDE_INT bitsize;
2939 HOST_WIDE_INT bitmaxsize;
2940 HOST_WIDE_INT bitpos;
2941 tree ref;
2942 ref = get_ref_base_and_extent (*tp, &bitpos, &bitsize, &bitmaxsize);
2943 if (DECL_P (ref)
2944 && var_can_have_subvars (ref)
2945 && bitmaxsize != -1)
2947 size_t uid = DECL_UID (ref);
2948 used_part_t up;
2950 up = get_or_create_used_part_for (uid);
2952 if (bitpos <= up->minused)
2953 up->minused = bitpos;
2954 if ((bitpos + bitmaxsize >= up->maxused))
2955 up->maxused = bitpos + bitmaxsize;
2957 if (bitsize == bitmaxsize)
2958 up->explicit_uses = true;
2959 else
2960 up->implicit_uses = true;
2961 if (!lhs_p)
2962 up->write_only = false;
2963 up_insert (uid, up);
2965 *walk_subtrees = 0;
2966 return NULL_TREE;
2969 break;
2970 /* This is here to make sure we mark the entire base variable as used
2971 when you take its address. Because our used portion analysis is
2972 simple, we aren't looking at casts or pointer arithmetic to see what
2973 happens when you take the address. */
2974 case ADDR_EXPR:
2976 tree var = get_base_address (TREE_OPERAND (*tp, 0));
2978 if (var
2979 && DECL_P (var)
2980 && DECL_SIZE (var)
2981 && var_can_have_subvars (var)
2982 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2984 used_part_t up;
2985 size_t uid = DECL_UID (var);
2987 up = get_or_create_used_part_for (uid);
2989 up->minused = 0;
2990 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
2991 up->implicit_uses = true;
2993 up_insert (uid, up);
2994 *walk_subtrees = 0;
2995 return NULL_TREE;
2998 break;
2999 case VAR_DECL:
3000 case PARM_DECL:
3001 case RESULT_DECL:
3003 tree var = *tp;
3004 if (DECL_SIZE (var)
3005 && var_can_have_subvars (var)
3006 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3008 used_part_t up;
3009 size_t uid = DECL_UID (var);
3011 up = get_or_create_used_part_for (uid);
3013 up->minused = 0;
3014 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
3015 up->implicit_uses = true;
3017 up_insert (uid, up);
3018 *walk_subtrees = 0;
3019 return NULL_TREE;
3022 break;
3024 default:
3025 break;
3028 return NULL_TREE;
3031 /* Create structure field variables for structures used in this function. */
3033 static void
3034 create_structure_vars (void)
3036 basic_block bb;
3037 safe_referenced_var_iterator rvi;
3038 VEC (tree, heap) *varvec = NULL;
3039 tree var;
3041 used_portions = htab_create (10, used_part_map_hash, used_part_map_eq,
3042 free_used_part_map);
3044 FOR_EACH_BB (bb)
3046 block_stmt_iterator bsi;
3047 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3049 walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
3050 find_used_portions,
3051 NULL);
3054 FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, rvi)
3056 /* The C++ FE creates vars without DECL_SIZE set, for some reason. */
3057 if (var
3058 && DECL_SIZE (var)
3059 && var_can_have_subvars (var)
3060 && !MTAG_P (var)
3061 && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
3062 create_overlap_variables_for (var);
3064 htab_delete (used_portions);
3065 VEC_free (tree, heap, varvec);
3069 static bool
3070 gate_structure_vars (void)
3072 return flag_tree_salias != 0;
3075 struct tree_opt_pass pass_create_structure_vars =
3077 "salias", /* name */
3078 gate_structure_vars, /* gate */
3079 create_structure_vars, /* execute */
3080 NULL, /* sub */
3081 NULL, /* next */
3082 0, /* static_pass_number */
3083 0, /* tv_id */
3084 PROP_cfg, /* properties_required */
3085 0, /* properties_provided */
3086 0, /* properties_destroyed */
3087 0, /* todo_flags_start */
3088 TODO_dump_func, /* todo_flags_finish */
3089 0 /* letter */