OSDN Git Service

gcc/ChangeLog:
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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"
49
50 /* Obstack used to hold grouping bitmaps and other temporary bitmaps used by
51    aliasing  */
52 static bitmap_obstack alias_obstack;
53
54 /* 'true' after aliases have been computed (see compute_may_aliases).  */
55 bool aliases_computed_p;
56
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
60 {
61   /* Variable and its alias set.  */
62   tree var;
63   HOST_WIDE_INT set;
64
65   /* Total number of virtual operands that will be needed to represent
66      all the aliases of VAR.  */
67   long total_alias_vops;
68
69   /* Nonzero if the aliases for this memory tag have been grouped
70      already.  Used in group_aliases.  */
71   unsigned int grouped_p : 1;
72
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;
77 };
78
79
80 /* Counters used to display statistics on alias analysis.  */
81 struct alias_stats_d
82 {
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;
92 };
93
94
95 /* Local variables.  */
96 static struct alias_stats_d alias_stats;
97
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);
115
116 /* Global declarations.  */
117
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;
121
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;
130
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;
137
138
139 /* Compute may-alias information for every variable referenced in function
140    FNDECL.
141
142    Alias analysis proceeds in 3 main phases:
143
144    1- Points-to and escape analysis.
145
146    This phase walks the use-def chains in the SSA web looking for three
147    things:
148
149         * Assignments of the form P_i = &VAR
150         * Assignments of the form P_i = malloc()
151         * Pointers and ADDR_EXPR that escape the current function.
152
153    The concept of 'escaping' is the same one used in the Java world.  When
154    a pointer or an ADDR_EXPR escapes, it means that it has been exposed
155    outside of the current function.  So, assignment to global variables,
156    function arguments and returning a pointer are all escape sites, as are
157    conversions between pointers and integers.
158
159    This is where we are currently limited.  Since not everything is renamed
160    into SSA, we lose track of escape properties when a pointer is stashed
161    inside a field in a structure, for instance.  In those cases, we are
162    assuming that the pointer does escape.
163
164    We use escape analysis to determine whether a variable is
165    call-clobbered.  Simply put, if an ADDR_EXPR escapes, then the variable
166    is call-clobbered.  If a pointer P_i escapes, then all the variables
167    pointed-to by P_i (and its memory tag) also escape.
168
169    2- Compute flow-sensitive aliases
170
171    We have two classes of memory tags.  Memory tags associated with the
172    pointed-to data type of the pointers in the program.  These tags are
173    called "type memory tag" (TMT).  The other class are those associated
174    with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
175    when adding operands for an INDIRECT_REF *P_i, we will first check
176    whether P_i has a name tag, if it does we use it, because that will have
177    more precise aliasing information.  Otherwise, we use the standard type
178    tag.
179
180    In this phase, we go through all the pointers we found in points-to
181    analysis and create alias sets for the name memory tags associated with
182    each pointer P_i.  If P_i escapes, we mark call-clobbered the variables
183    it points to and its tag.
184
185
186    3- Compute flow-insensitive aliases
187
188    This pass will compare the alias set of every type memory tag and every
189    addressable variable found in the program.  Given a type memory tag TMT
190    and an addressable variable V.  If the alias sets of TMT and V conflict
191    (as computed by may_alias_p), then V is marked as an alias tag and added
192    to the alias set of TMT.
193
194    For instance, consider the following function:
195
196             foo (int i)
197             {
198               int *p, a, b;
199             
200               if (i > 10)
201                 p = &a;
202               else
203                 p = &b;
204             
205               *p = 3;
206               a = b + 2;
207               return *p;
208             }
209
210    After aliasing analysis has finished, the type memory tag for pointer
211    'p' will have two aliases, namely variables 'a' and 'b'.  Every time
212    pointer 'p' is dereferenced, we want to mark the operation as a
213    potential reference to 'a' and 'b'.
214
215             foo (int i)
216             {
217               int *p, a, b;
218
219               if (i_2 > 10)
220                 p_4 = &a;
221               else
222                 p_6 = &b;
223               # p_1 = PHI <p_4(1), p_6(2)>;
224
225               # a_7 = V_MAY_DEF <a_3>;
226               # b_8 = V_MAY_DEF <b_5>;
227               *p_1 = 3;
228
229               # a_9 = V_MAY_DEF <a_7>
230               # VUSE <b_8>
231               a_9 = b_8 + 2;
232
233               # VUSE <a_9>;
234               # VUSE <b_8>;
235               return *p_1;
236             }
237
238    In certain cases, the list of may aliases for a pointer may grow too
239    large.  This may cause an explosion in the number of virtual operands
240    inserted in the code.  Resulting in increased memory consumption and
241    compilation time.
242
243    When the number of virtual operands needed to represent aliased
244    loads and stores grows too large (configurable with @option{--param
245    max-aliased-vops}), alias sets are grouped to avoid severe
246    compile-time slow downs and memory consumption.  See group_aliases.  */
247
248 static void
249 compute_may_aliases (void)
250 {
251   struct alias_info *ai;
252   
253   memset (&alias_stats, 0, sizeof (alias_stats));
254
255   delete_old_heap_vars ();
256
257   /* Initialize aliasing information.  */
258   ai = init_alias_info ();
259
260   /* For each pointer P_i, determine the sets of variables that P_i may
261      point-to.  For every addressable variable V, determine whether the
262      address of V escapes the current function, making V call-clobbered
263      (i.e., whether &V is stored in a global variable or if its passed as a
264      function call argument).  */
265   compute_points_to_sets (ai);
266
267   /* Collect all pointers and addressable variables, compute alias sets,
268      create memory tags for pointers and promote variables whose address is
269      not needed anymore.  */
270   setup_pointers_and_addressables (ai);
271
272   /* Compute flow-sensitive, points-to based aliasing for all the name
273      memory tags.  Note that this pass needs to be done before flow
274      insensitive analysis because it uses the points-to information
275      gathered before to mark call-clobbered type tags.  */
276   compute_flow_sensitive_aliasing (ai);
277
278   /* Compute type-based flow-insensitive aliasing for all the type
279      memory tags.  */
280   compute_flow_insensitive_aliasing (ai);
281
282   /* If the program has too many call-clobbered variables and/or function
283      calls, create .GLOBAL_VAR and use it to model call-clobbering
284      semantics at call sites.  This reduces the number of virtual operands
285      considerably, improving compile times at the expense of lost
286      aliasing precision.  */
287   maybe_create_global_var (ai);
288
289   /* Debugging dumps.  */
290   if (dump_file)
291     {
292       dump_referenced_vars (dump_file);
293       if (dump_flags & TDF_STATS)
294         dump_alias_stats (dump_file);
295       dump_points_to_info (dump_file);
296       dump_alias_info (dump_file);
297     }
298
299   /* Deallocate memory used by aliasing data structures.  */
300   delete_alias_info (ai);
301
302   {
303     block_stmt_iterator bsi;
304     basic_block bb;
305     FOR_EACH_BB (bb)
306       {
307         for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
308           {
309             update_stmt_if_modified (bsi_stmt (bsi));
310           }
311       }
312   }
313
314 }
315
316 struct tree_opt_pass pass_may_alias = 
317 {
318   "alias",                              /* name */
319   NULL,                                 /* gate */
320   compute_may_aliases,                  /* execute */
321   NULL,                                 /* sub */
322   NULL,                                 /* next */
323   0,                                    /* static_pass_number */
324   TV_TREE_MAY_ALIAS,                    /* tv_id */
325   PROP_cfg | PROP_ssa,                  /* properties_required */
326   PROP_alias,                           /* properties_provided */
327   0,                                    /* properties_destroyed */
328   0,                                    /* todo_flags_start */
329   TODO_dump_func | TODO_update_ssa
330     | TODO_ggc_collect | TODO_verify_ssa
331     | TODO_verify_stmts,                /* todo_flags_finish */
332   0                                     /* letter */
333 };
334
335
336 /* Data structure used to count the number of dereferences to PTR
337    inside an expression.  */
338 struct count_ptr_d
339 {
340   tree ptr;
341   unsigned count;
342 };
343
344
345 /* Helper for count_uses_and_derefs.  Called by walk_tree to look for
346    (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA.  */
347
348 static tree
349 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
350 {
351   struct count_ptr_d *count_p = (struct count_ptr_d *) data;
352
353   /* Do not walk inside ADDR_EXPR nodes.  In the expression &ptr->fld,
354      pointer 'ptr' is *not* dereferenced, it is simply used to compute
355      the address of 'fld' as 'ptr + offsetof(fld)'.  */
356   if (TREE_CODE (*tp) == ADDR_EXPR)
357     {
358       *walk_subtrees = 0;
359       return NULL_TREE;
360     }
361
362   if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
363     count_p->count++;
364
365   return NULL_TREE;
366 }
367
368
369 /* Count the number of direct and indirect uses for pointer PTR in
370    statement STMT.  The two counts are stored in *NUM_USES_P and
371    *NUM_DEREFS_P respectively.  *IS_STORE_P is set to 'true' if at
372    least one of those dereferences is a store operation.  */
373
374 void
375 count_uses_and_derefs (tree ptr, tree stmt, unsigned *num_uses_p,
376                        unsigned *num_derefs_p, bool *is_store)
377 {
378   ssa_op_iter i;
379   tree use;
380
381   *num_uses_p = 0;
382   *num_derefs_p = 0;
383   *is_store = false;
384
385   /* Find out the total number of uses of PTR in STMT.  */
386   FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
387     if (use == ptr)
388       (*num_uses_p)++;
389
390   /* Now count the number of indirect references to PTR.  This is
391      truly awful, but we don't have much choice.  There are no parent
392      pointers inside INDIRECT_REFs, so an expression like
393      '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
394      find all the indirect and direct uses of x_1 inside.  The only
395      shortcut we can take is the fact that GIMPLE only allows
396      INDIRECT_REFs inside the expressions below.  */
397   if (TREE_CODE (stmt) == MODIFY_EXPR
398       || (TREE_CODE (stmt) == RETURN_EXPR
399           && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
400       || TREE_CODE (stmt) == ASM_EXPR
401       || TREE_CODE (stmt) == CALL_EXPR)
402     {
403       tree lhs, rhs;
404
405       if (TREE_CODE (stmt) == MODIFY_EXPR)
406         {
407           lhs = TREE_OPERAND (stmt, 0);
408           rhs = TREE_OPERAND (stmt, 1);
409         }
410       else if (TREE_CODE (stmt) == RETURN_EXPR)
411         {
412           tree e = TREE_OPERAND (stmt, 0);
413           lhs = TREE_OPERAND (e, 0);
414           rhs = TREE_OPERAND (e, 1);
415         }
416       else if (TREE_CODE (stmt) == ASM_EXPR)
417         {
418           lhs = ASM_OUTPUTS (stmt);
419           rhs = ASM_INPUTS (stmt);
420         }
421       else
422         {
423           lhs = NULL_TREE;
424           rhs = stmt;
425         }
426
427       if (lhs && (TREE_CODE (lhs) == TREE_LIST || EXPR_P (lhs)))
428         {
429           struct count_ptr_d count;
430           count.ptr = ptr;
431           count.count = 0;
432           walk_tree (&lhs, count_ptr_derefs, &count, NULL);
433           *is_store = true;
434           *num_derefs_p = count.count;
435         }
436
437       if (rhs && (TREE_CODE (rhs) == TREE_LIST || EXPR_P (rhs)))
438         {
439           struct count_ptr_d count;
440           count.ptr = ptr;
441           count.count = 0;
442           walk_tree (&rhs, count_ptr_derefs, &count, NULL);
443           *num_derefs_p += count.count;
444         }
445     }
446
447   gcc_assert (*num_uses_p >= *num_derefs_p);
448 }
449
450 /* Initialize the data structures used for alias analysis.  */
451
452 static struct alias_info *
453 init_alias_info (void)
454 {
455   struct alias_info *ai;
456   referenced_var_iterator rvi;
457   tree var;
458
459   bitmap_obstack_initialize (&alias_obstack);
460   ai = xcalloc (1, sizeof (struct alias_info));
461   ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
462   sbitmap_zero (ai->ssa_names_visited);
463   VARRAY_TREE_INIT (ai->processed_ptrs, 50, "processed_ptrs");
464   ai->written_vars = BITMAP_ALLOC (&alias_obstack);
465   ai->dereferenced_ptrs_store = BITMAP_ALLOC (&alias_obstack);
466   ai->dereferenced_ptrs_load = BITMAP_ALLOC (&alias_obstack);
467
468   /* If aliases have been computed before, clear existing information.  */
469   if (aliases_computed_p)
470     {
471       unsigned i;
472   
473       /* Similarly, clear the set of addressable variables.  In this
474          case, we can just clear the set because addressability is
475          only computed here.  */
476       bitmap_clear (addressable_vars);
477
478       /* Clear flow-insensitive alias information from each symbol.  */
479       FOR_EACH_REFERENCED_VAR (var, rvi)
480         {
481           var_ann_t ann = var_ann (var);
482           
483           ann->is_alias_tag = 0;
484           ann->may_aliases = NULL;
485           NUM_REFERENCES_CLEAR (ann);
486
487           /* Since we are about to re-discover call-clobbered
488              variables, clear the call-clobbered flag.  Variables that
489              are intrinsically call-clobbered (globals, local statics,
490              etc) will not be marked by the aliasing code, so we can't
491              remove them from CALL_CLOBBERED_VARS.  
492
493              NB: STRUCT_FIELDS are still call clobbered if they are for
494              a global variable, so we *don't* clear their call clobberedness
495              just because they are tags, though we will clear it if they
496              aren't for global variables.  */
497           if (ann->mem_tag_kind == NAME_TAG 
498               || ann->mem_tag_kind == TYPE_TAG 
499               || !is_global_var (var))
500             clear_call_clobbered (var);
501         }
502
503       /* Clear flow-sensitive points-to information from each SSA name.  */
504       for (i = 1; i < num_ssa_names; i++)
505         {
506           tree name = ssa_name (i);
507
508           if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
509             continue;
510
511           if (SSA_NAME_PTR_INFO (name))
512             {
513               struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
514
515               /* Clear all the flags but keep the name tag to
516                  avoid creating new temporaries unnecessarily.  If
517                  this pointer is found to point to a subset or
518                  superset of its former points-to set, then a new
519                  tag will need to be created in create_name_tags.  */
520               pi->pt_anything = 0;
521               pi->pt_null = 0;
522               pi->value_escapes_p = 0;
523               pi->is_dereferenced = 0;
524               if (pi->pt_vars)
525                 bitmap_clear (pi->pt_vars);
526             }
527         }
528     }
529
530   /* Next time, we will need to reset alias information.  */
531   aliases_computed_p = true;
532
533   return ai;
534 }
535
536
537 /* Deallocate memory used by alias analysis.  */
538
539 static void
540 delete_alias_info (struct alias_info *ai)
541 {
542   size_t i;
543   referenced_var_iterator rvi;
544   tree var;
545
546   sbitmap_free (ai->ssa_names_visited);
547   ai->processed_ptrs = NULL;
548
549   for (i = 0; i < ai->num_addressable_vars; i++)
550     free (ai->addressable_vars[i]);
551   
552   FOR_EACH_REFERENCED_VAR(var, rvi)
553     {
554       var_ann_t ann = var_ann (var);
555       NUM_REFERENCES_CLEAR (ann);
556     }
557
558   free (ai->addressable_vars);
559
560   for (i = 0; i < ai->num_pointers; i++)
561     free (ai->pointers[i]);
562   free (ai->pointers);
563
564   BITMAP_FREE (ai->written_vars);
565   BITMAP_FREE (ai->dereferenced_ptrs_store);
566   BITMAP_FREE (ai->dereferenced_ptrs_load);
567   bitmap_obstack_release (&alias_obstack);
568   free (ai);
569
570   delete_points_to_sets ();
571 }
572
573 /* Create name tags for all the pointers that have been dereferenced.
574    We only create a name tag for a pointer P if P is found to point to
575    a set of variables (so that we can alias them to *P) or if it is
576    the result of a call to malloc (which means that P cannot point to
577    anything else nor alias any other variable).
578
579    If two pointers P and Q point to the same set of variables, they
580    are assigned the same name tag.  */
581
582 static void
583 create_name_tags (void)
584 {
585   size_t i;
586   VEC (tree, heap) *with_ptvars = NULL;
587   tree ptr;
588
589   /* Collect the list of pointers with a non-empty points to set.  */
590   for (i = 1; i < num_ssa_names; i++)
591     {
592       tree ptr = ssa_name (i);
593       struct ptr_info_def *pi;
594
595       if (!ptr
596           || !POINTER_TYPE_P (TREE_TYPE (ptr))
597           || !SSA_NAME_PTR_INFO (ptr))
598         continue;
599
600       pi = SSA_NAME_PTR_INFO (ptr);
601
602       if (pi->pt_anything || !pi->is_dereferenced)
603         {
604           /* No name tags for pointers that have not been
605              dereferenced or point to an arbitrary location.  */
606           pi->name_mem_tag = NULL_TREE;
607           continue;
608         }
609
610       /* Set pt_anything on the pointers without pt_vars filled in so
611          that they are assigned a type tag.  */
612       
613       if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars)) 
614         VEC_safe_push (tree, heap, with_ptvars, ptr);
615       else
616         set_pt_anything (ptr);
617     }
618   
619   /* If we didn't find any pointers with pt_vars set, we're done.  */
620   if (!with_ptvars)
621     return;
622
623   /* Now go through the pointers with pt_vars, and find a name tag
624      with the same pt_vars as this pointer, or create one if one
625      doesn't exist.  */
626   for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
627     {
628       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
629       size_t j;
630       tree ptr2;
631       tree old_name_tag = pi->name_mem_tag;
632       
633       /* If PTR points to a set of variables, check if we don't
634          have another pointer Q with the same points-to set before
635          creating a tag.  If so, use Q's tag instead of creating a
636          new one.
637          
638          This is important for not creating unnecessary symbols
639          and also for copy propagation.  If we ever need to
640          propagate PTR into Q or vice-versa, we would run into
641          problems if they both had different name tags because
642          they would have different SSA version numbers (which
643          would force us to take the name tags in and out of SSA).  */
644       for (j = 0; j < i && VEC_iterate (tree, with_ptvars, j, ptr2); j++)
645         {
646           struct ptr_info_def *qi = SSA_NAME_PTR_INFO (ptr2);
647           
648           if (bitmap_equal_p (pi->pt_vars, qi->pt_vars))
649             {
650               pi->name_mem_tag = qi->name_mem_tag;
651               break;
652             }
653         }
654       
655       /* If we didn't find a pointer with the same points-to set
656          as PTR, create a new name tag if needed.  */
657       if (pi->name_mem_tag == NULL_TREE)
658         pi->name_mem_tag = get_nmt_for (ptr);
659       
660       /* If the new name tag computed for PTR is different than
661          the old name tag that it used to have, then the old tag
662          needs to be removed from the IL, so we mark it for
663          renaming.  */
664       if (old_name_tag && old_name_tag != pi->name_mem_tag)
665         mark_sym_for_renaming (old_name_tag);
666       
667       TREE_THIS_VOLATILE (pi->name_mem_tag)
668         |= TREE_THIS_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
669       
670       /* Mark the new name tag for renaming.  */
671       mark_sym_for_renaming (pi->name_mem_tag);
672     }
673
674   VEC_free (tree, heap, with_ptvars);
675 }
676
677
678 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
679    the name memory tag (NMT) associated with P_i.  If P_i escapes, then its
680    name tag and the variables it points-to are call-clobbered.  Finally, if
681    P_i escapes and we could not determine where it points to, then all the
682    variables in the same alias set as *P_i are marked call-clobbered.  This
683    is necessary because we must assume that P_i may take the address of any
684    variable in the same alias set.  */
685
686 static void
687 compute_flow_sensitive_aliasing (struct alias_info *ai)
688 {
689   size_t i;
690   
691   for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
692     {
693       tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
694       if (!find_what_p_points_to (ptr))
695         set_pt_anything (ptr);
696     }
697
698   create_name_tags ();
699
700   for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
701     {
702       unsigned j;
703       tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
704       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
705       var_ann_t v_ann = var_ann (SSA_NAME_VAR (ptr));
706       bitmap_iterator bi;
707
708       if (pi->value_escapes_p || pi->pt_anything)
709         {
710           /* If PTR escapes or may point to anything, then its associated
711              memory tags and pointed-to variables are call-clobbered.  */
712           if (pi->name_mem_tag)
713             mark_call_clobbered (pi->name_mem_tag);
714
715           if (v_ann->type_mem_tag)
716             mark_call_clobbered (v_ann->type_mem_tag);
717
718           if (pi->pt_vars)
719             EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
720               mark_call_clobbered (referenced_var (j));
721         }
722
723       /* Set up aliasing information for PTR's name memory tag (if it has
724          one).  Note that only pointers that have been dereferenced will
725          have a name memory tag.  */
726       if (pi->name_mem_tag && pi->pt_vars)
727         EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, j, bi)
728           {
729             add_may_alias (pi->name_mem_tag, referenced_var (j));
730             add_may_alias (v_ann->type_mem_tag, referenced_var (j));
731           }
732
733       /* If the name tag is call clobbered, so is the type tag
734          associated with the base VAR_DECL.  */
735       if (pi->name_mem_tag
736           && v_ann->type_mem_tag
737           && is_call_clobbered (pi->name_mem_tag))
738         mark_call_clobbered (v_ann->type_mem_tag);
739     }
740 }
741
742
743 /* Compute type-based alias sets.  Traverse all the pointers and
744    addressable variables found in setup_pointers_and_addressables.
745    
746    For every pointer P in AI->POINTERS and addressable variable V in
747    AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's type
748    memory tag (TMT) if their alias sets conflict.  V is then marked as
749    an alias tag so that the operand scanner knows that statements
750    containing V have aliased operands.  */
751
752 static void
753 compute_flow_insensitive_aliasing (struct alias_info *ai)
754 {
755   size_t i;
756
757   /* Initialize counter for the total number of virtual operands that
758      aliasing will introduce.  When AI->TOTAL_ALIAS_VOPS goes beyond the
759      threshold set by --params max-alias-vops, we enable alias
760      grouping.  */
761   ai->total_alias_vops = 0;
762
763   /* For every pointer P, determine which addressable variables may alias
764      with P's type memory tag.  */
765   for (i = 0; i < ai->num_pointers; i++)
766     {
767       size_t j;
768       struct alias_map_d *p_map = ai->pointers[i];
769       tree tag = var_ann (p_map->var)->type_mem_tag;
770       var_ann_t tag_ann = var_ann (tag);
771
772       p_map->total_alias_vops = 0;
773       p_map->may_aliases = BITMAP_ALLOC (&alias_obstack);
774
775       for (j = 0; j < ai->num_addressable_vars; j++)
776         {
777           struct alias_map_d *v_map;
778           var_ann_t v_ann;
779           tree var;
780           bool tag_stored_p, var_stored_p;
781           
782           v_map = ai->addressable_vars[j];
783           var = v_map->var;
784           v_ann = var_ann (var);
785
786           /* Skip memory tags and variables that have never been
787              written to.  We also need to check if the variables are
788              call-clobbered because they may be overwritten by
789              function calls.
790
791              Note this is effectively random accessing elements in
792              the sparse bitset, which can be highly inefficient.
793              So we first check the call_clobbered status of the
794              tag and variable before querying the bitmap.  */
795           tag_stored_p = is_call_clobbered (tag)
796                          || bitmap_bit_p (ai->written_vars, DECL_UID (tag));
797           var_stored_p = is_call_clobbered (var)
798                          || bitmap_bit_p (ai->written_vars, DECL_UID (var));
799           if (!tag_stored_p && !var_stored_p)
800             continue;
801              
802           if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
803             {
804               size_t num_tag_refs, num_var_refs;
805
806               num_tag_refs = NUM_REFERENCES (tag_ann);
807               num_var_refs = NUM_REFERENCES (v_ann);
808
809               /* Add VAR to TAG's may-aliases set.  */
810
811               /* We should never have a var with subvars here, because
812                  they shouldn't get into the set of addressable vars */
813               gcc_assert (!var_can_have_subvars (var)
814                           || get_subvars_for_var (var) == NULL);
815
816               add_may_alias (tag, var);
817               /* Update the bitmap used to represent TAG's alias set
818                  in case we need to group aliases.  */
819               bitmap_set_bit (p_map->may_aliases, DECL_UID (var));
820
821               /* Update the total number of virtual operands due to
822                  aliasing.  Since we are adding one more alias to TAG's
823                  may-aliases set, the total number of virtual operands due
824                  to aliasing will be increased by the number of references
825                  made to VAR and TAG (every reference to TAG will also
826                  count as a reference to VAR).  */
827               ai->total_alias_vops += (num_var_refs + num_tag_refs);
828               p_map->total_alias_vops += (num_var_refs + num_tag_refs);
829
830
831             }
832         }
833     }
834
835   /* Since this analysis is based exclusively on symbols, it fails to
836      handle cases where two pointers P and Q have different memory
837      tags with conflicting alias set numbers but no aliased symbols in
838      common.
839
840      For example, suppose that we have two memory tags TMT.1 and TMT.2
841      such that
842      
843                 may-aliases (TMT.1) = { a }
844                 may-aliases (TMT.2) = { b }
845
846      and the alias set number of TMT.1 conflicts with that of TMT.2.
847      Since they don't have symbols in common, loads and stores from
848      TMT.1 and TMT.2 will seem independent of each other, which will
849      lead to the optimizers making invalid transformations (see
850      testsuite/gcc.c-torture/execute/pr15262-[12].c).
851
852      To avoid this problem, we do a final traversal of AI->POINTERS
853      looking for pairs of pointers that have no aliased symbols in
854      common and yet have conflicting alias set numbers.  */
855   for (i = 0; i < ai->num_pointers; i++)
856     {
857       size_t j;
858       struct alias_map_d *p_map1 = ai->pointers[i];
859       tree tag1 = var_ann (p_map1->var)->type_mem_tag;
860       bitmap may_aliases1 = p_map1->may_aliases;
861
862       for (j = i + 1; j < ai->num_pointers; j++)
863         {
864           struct alias_map_d *p_map2 = ai->pointers[j];
865           tree tag2 = var_ann (p_map2->var)->type_mem_tag;
866           bitmap may_aliases2 = p_map2->may_aliases;
867
868           /* If the pointers may not point to each other, do nothing.  */
869           if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
870             continue;
871
872           /* The two pointers may alias each other.  If they already have
873              symbols in common, do nothing.  */
874           if (bitmap_intersect_p (may_aliases1, may_aliases2))
875             continue;
876
877           if (!bitmap_empty_p (may_aliases2))
878             {
879               unsigned int k;
880               bitmap_iterator bi;
881
882               /* Add all the aliases for TAG2 into TAG1's alias set.
883                  FIXME, update grouping heuristic counters.  */
884               EXECUTE_IF_SET_IN_BITMAP (may_aliases2, 0, k, bi)
885                 add_may_alias (tag1, referenced_var (k));
886               bitmap_ior_into (may_aliases1, may_aliases2);
887             }
888           else
889             {
890               /* Since TAG2 does not have any aliases of its own, add
891                  TAG2 itself to the alias set of TAG1.  */
892               add_may_alias (tag1, tag2);
893               bitmap_set_bit (may_aliases1, DECL_UID (tag2));
894             }
895         }
896     }
897   
898   if (dump_file)
899     fprintf (dump_file, "\n%s: Total number of aliased vops: %ld\n",
900              get_name (current_function_decl),
901              ai->total_alias_vops);
902
903   /* Determine if we need to enable alias grouping.  */
904   if (ai->total_alias_vops >= MAX_ALIASED_VOPS)
905     group_aliases (ai);
906 }
907
908
909 /* Comparison function for qsort used in group_aliases.  */
910
911 static int
912 total_alias_vops_cmp (const void *p, const void *q)
913 {
914   const struct alias_map_d **p1 = (const struct alias_map_d **)p;
915   const struct alias_map_d **p2 = (const struct alias_map_d **)q;
916   long n1 = (*p1)->total_alias_vops;
917   long n2 = (*p2)->total_alias_vops;
918
919   /* We want to sort in descending order.  */
920   return (n1 > n2 ? -1 : (n1 == n2) ? 0 : 1);
921 }
922
923 /* Group all the aliases for TAG to make TAG represent all the
924    variables in its alias set.  Update the total number
925    of virtual operands due to aliasing (AI->TOTAL_ALIAS_VOPS).  This
926    function will make TAG be the unique alias tag for all the
927    variables in its may-aliases.  So, given:
928
929         may-aliases(TAG) = { V1, V2, V3 }
930
931    This function will group the variables into:
932
933         may-aliases(V1) = { TAG }
934         may-aliases(V2) = { TAG }
935         may-aliases(V2) = { TAG }  */
936
937 static void
938 group_aliases_into (tree tag, bitmap tag_aliases, struct alias_info *ai)
939 {
940   unsigned int i;
941   var_ann_t tag_ann = var_ann (tag);
942   size_t num_tag_refs = NUM_REFERENCES (tag_ann);
943   bitmap_iterator bi;
944
945   EXECUTE_IF_SET_IN_BITMAP (tag_aliases, 0, i, bi)
946     {
947       tree var = referenced_var (i);
948       var_ann_t ann = var_ann (var);
949
950       /* Make TAG the unique alias of VAR.  */
951       ann->is_alias_tag = 0;
952       ann->may_aliases = NULL;
953
954       /* Note that VAR and TAG may be the same if the function has no
955          addressable variables (see the discussion at the end of
956          setup_pointers_and_addressables).  */
957       if (var != tag)
958         add_may_alias (var, tag);
959
960       /* Reduce total number of virtual operands contributed
961          by TAG on behalf of VAR.  Notice that the references to VAR
962          itself won't be removed.  We will merely replace them with
963          references to TAG.  */
964       ai->total_alias_vops -= num_tag_refs;
965     }
966
967   /* We have reduced the number of virtual operands that TAG makes on
968      behalf of all the variables formerly aliased with it.  However,
969      we have also "removed" all the virtual operands for TAG itself,
970      so we add them back.  */
971   ai->total_alias_vops += num_tag_refs;
972
973   /* TAG no longer has any aliases.  */
974   tag_ann->may_aliases = NULL;
975 }
976
977
978 /* Group may-aliases sets to reduce the number of virtual operands due
979    to aliasing.
980
981      1- Sort the list of pointers in decreasing number of contributed
982         virtual operands.
983
984      2- Take the first entry in AI->POINTERS and revert the role of
985         the memory tag and its aliases.  Usually, whenever an aliased
986         variable Vi is found to alias with a memory tag T, we add Vi
987         to the may-aliases set for T.  Meaning that after alias
988         analysis, we will have:
989
990                 may-aliases(T) = { V1, V2, V3, ..., Vn }
991
992         This means that every statement that references T, will get 'n'
993         virtual operands for each of the Vi tags.  But, when alias
994         grouping is enabled, we make T an alias tag and add it to the
995         alias set of all the Vi variables:
996
997                 may-aliases(V1) = { T }
998                 may-aliases(V2) = { T }
999                 ...
1000                 may-aliases(Vn) = { T }
1001
1002         This has two effects: (a) statements referencing T will only get
1003         a single virtual operand, and, (b) all the variables Vi will now
1004         appear to alias each other.  So, we lose alias precision to
1005         improve compile time.  But, in theory, a program with such a high
1006         level of aliasing should not be very optimizable in the first
1007         place.
1008
1009      3- Since variables may be in the alias set of more than one
1010         memory tag, the grouping done in step (2) needs to be extended
1011         to all the memory tags that have a non-empty intersection with
1012         the may-aliases set of tag T.  For instance, if we originally
1013         had these may-aliases sets:
1014
1015                 may-aliases(T) = { V1, V2, V3 }
1016                 may-aliases(R) = { V2, V4 }
1017
1018         In step (2) we would have reverted the aliases for T as:
1019
1020                 may-aliases(V1) = { T }
1021                 may-aliases(V2) = { T }
1022                 may-aliases(V3) = { T }
1023
1024         But note that now V2 is no longer aliased with R.  We could
1025         add R to may-aliases(V2), but we are in the process of
1026         grouping aliases to reduce virtual operands so what we do is
1027         add V4 to the grouping to obtain:
1028
1029                 may-aliases(V1) = { T }
1030                 may-aliases(V2) = { T }
1031                 may-aliases(V3) = { T }
1032                 may-aliases(V4) = { T }
1033
1034      4- If the total number of virtual operands due to aliasing is
1035         still above the threshold set by max-alias-vops, go back to (2).  */
1036
1037 static void
1038 group_aliases (struct alias_info *ai)
1039 {
1040   size_t i;
1041
1042   /* Sort the POINTERS array in descending order of contributed
1043      virtual operands.  */
1044   qsort (ai->pointers, ai->num_pointers, sizeof (struct alias_map_d *),
1045          total_alias_vops_cmp);
1046
1047   /* For every pointer in AI->POINTERS, reverse the roles of its tag
1048      and the tag's may-aliases set.  */
1049   for (i = 0; i < ai->num_pointers; i++)
1050     {
1051       size_t j;
1052       tree tag1 = var_ann (ai->pointers[i]->var)->type_mem_tag;
1053       bitmap tag1_aliases = ai->pointers[i]->may_aliases;
1054
1055       /* Skip tags that have been grouped already.  */
1056       if (ai->pointers[i]->grouped_p)
1057         continue;
1058
1059       /* See if TAG1 had any aliases in common with other type tags.
1060          If we find a TAG2 with common aliases with TAG1, add TAG2's
1061          aliases into TAG1.  */
1062       for (j = i + 1; j < ai->num_pointers; j++)
1063         {
1064           bitmap tag2_aliases = ai->pointers[j]->may_aliases;
1065
1066           if (bitmap_intersect_p (tag1_aliases, tag2_aliases))
1067             {
1068               tree tag2 = var_ann (ai->pointers[j]->var)->type_mem_tag;
1069
1070               bitmap_ior_into (tag1_aliases, tag2_aliases);
1071
1072               /* TAG2 does not need its aliases anymore.  */
1073               bitmap_clear (tag2_aliases);
1074               var_ann (tag2)->may_aliases = NULL;
1075
1076               /* TAG1 is the unique alias of TAG2.  */
1077               add_may_alias (tag2, tag1);
1078
1079               ai->pointers[j]->grouped_p = true;
1080             }
1081         }
1082
1083       /* Now group all the aliases we collected into TAG1.  */
1084       group_aliases_into (tag1, tag1_aliases, ai);
1085
1086       /* If we've reduced total number of virtual operands below the
1087          threshold, stop.  */
1088       if (ai->total_alias_vops < MAX_ALIASED_VOPS)
1089         break;
1090     }
1091
1092   /* Finally, all the variables that have been grouped cannot be in
1093      the may-alias set of name memory tags.  Suppose that we have
1094      grouped the aliases in this code so that may-aliases(a) = TMT.20
1095
1096         p_5 = &a;
1097         ...
1098         # a_9 = V_MAY_DEF <a_8>
1099         p_5->field = 0
1100         ... Several modifications to TMT.20 ... 
1101         # VUSE <a_9>
1102         x_30 = p_5->field
1103
1104      Since p_5 points to 'a', the optimizers will try to propagate 0
1105      into p_5->field, but that is wrong because there have been
1106      modifications to 'TMT.20' in between.  To prevent this we have to
1107      replace 'a' with 'TMT.20' in the name tag of p_5.  */
1108   for (i = 0; i < VARRAY_ACTIVE_SIZE (ai->processed_ptrs); i++)
1109     {
1110       size_t j;
1111       tree ptr = VARRAY_TREE (ai->processed_ptrs, i);
1112       tree name_tag = SSA_NAME_PTR_INFO (ptr)->name_mem_tag;
1113       varray_type aliases;
1114       
1115       if (name_tag == NULL_TREE)
1116         continue;
1117
1118       aliases = var_ann (name_tag)->may_aliases;
1119       for (j = 0; aliases && j < VARRAY_ACTIVE_SIZE (aliases); j++)
1120         {
1121           tree alias = VARRAY_TREE (aliases, j);
1122           var_ann_t ann = var_ann (alias);
1123
1124           if ((ann->mem_tag_kind == NOT_A_TAG 
1125                || ann->mem_tag_kind == STRUCT_FIELD)
1126               && ann->may_aliases)
1127             {
1128               tree new_alias;
1129
1130               gcc_assert (VARRAY_ACTIVE_SIZE (ann->may_aliases) == 1);
1131
1132               new_alias = VARRAY_TREE (ann->may_aliases, 0);
1133               replace_may_alias (name_tag, j, new_alias);
1134             }
1135         }
1136     }
1137
1138   if (dump_file)
1139     fprintf (dump_file,
1140              "%s: Total number of aliased vops after grouping: %ld%s\n",
1141              get_name (current_function_decl),
1142              ai->total_alias_vops,
1143              (ai->total_alias_vops < 0) ? " (negative values are OK)" : "");
1144 }
1145
1146
1147 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS.  */
1148
1149 static void
1150 create_alias_map_for (tree var, struct alias_info *ai)
1151 {
1152   struct alias_map_d *alias_map;
1153   alias_map = xcalloc (1, sizeof (*alias_map));
1154   alias_map->var = var;
1155   alias_map->set = get_alias_set (var);
1156   ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
1157 }
1158
1159
1160 /* Create memory tags for all the dereferenced pointers and build the
1161    ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
1162    sets.  Based on the address escape and points-to information collected
1163    earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
1164    variables whose address is not needed anymore.  */
1165
1166 static void
1167 setup_pointers_and_addressables (struct alias_info *ai)
1168 {
1169   size_t n_vars, num_addressable_vars, num_pointers;
1170   referenced_var_iterator rvi;
1171   tree var;
1172   VEC (tree, heap) *varvec = NULL;
1173   safe_referenced_var_iterator srvi;
1174
1175   /* Size up the arrays ADDRESSABLE_VARS and POINTERS.  */
1176   num_addressable_vars = num_pointers = 0;
1177   
1178   FOR_EACH_REFERENCED_VAR (var, rvi)
1179     {
1180       if (may_be_aliased (var))
1181         num_addressable_vars++;
1182
1183       if (POINTER_TYPE_P (TREE_TYPE (var)))
1184         {
1185           /* Since we don't keep track of volatile variables, assume that
1186              these pointers are used in indirect store operations.  */
1187           if (TREE_THIS_VOLATILE (var))
1188             bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
1189
1190           num_pointers++;
1191         }
1192     }
1193
1194   /* Create ADDRESSABLE_VARS and POINTERS.  Note that these arrays are
1195      always going to be slightly bigger than we actually need them
1196      because some TREE_ADDRESSABLE variables will be marked
1197      non-addressable below and only pointers with unique type tags are
1198      going to be added to POINTERS.  */
1199   ai->addressable_vars = xcalloc (num_addressable_vars,
1200                                   sizeof (struct alias_map_d *));
1201   ai->pointers = xcalloc (num_pointers, sizeof (struct alias_map_d *));
1202   ai->num_addressable_vars = 0;
1203   ai->num_pointers = 0;
1204
1205   /* Since we will be creating type memory tags within this loop, cache the
1206      value of NUM_REFERENCED_VARS to avoid processing the additional tags
1207      unnecessarily.  */
1208   n_vars = num_referenced_vars;
1209
1210   FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
1211     {
1212       var_ann_t v_ann = var_ann (var);
1213       subvar_t svars;
1214
1215       /* Name memory tags already have flow-sensitive aliasing
1216          information, so they need not be processed by
1217          compute_flow_insensitive_aliasing.  Similarly, type memory
1218          tags are already accounted for when we process their
1219          associated pointer. 
1220       
1221          Structure fields, on the other hand, have to have some of this
1222          information processed for them, but it's pointless to mark them
1223          non-addressable (since they are fake variables anyway).  */
1224       if (v_ann->mem_tag_kind != NOT_A_TAG
1225           && v_ann->mem_tag_kind != STRUCT_FIELD) 
1226         continue;
1227
1228       /* Remove the ADDRESSABLE flag from every addressable variable whose
1229          address is not needed anymore.  This is caused by the propagation
1230          of ADDR_EXPR constants into INDIRECT_REF expressions and the
1231          removal of dead pointer assignments done by the early scalar
1232          cleanup passes.  */
1233       if (TREE_ADDRESSABLE (var))
1234         {
1235           if (!bitmap_bit_p (addressable_vars, DECL_UID (var))
1236               && TREE_CODE (var) != RESULT_DECL
1237               && !is_global_var (var))
1238             {
1239               bool okay_to_mark = true;
1240
1241               /* Since VAR is now a regular GIMPLE register, we will need
1242                  to rename VAR into SSA afterwards.  */
1243               mark_sym_for_renaming (var);
1244
1245               /* If VAR can have sub-variables, and any of its
1246                  sub-variables has its address taken, then we cannot
1247                  remove the addressable flag from VAR.  */
1248               if (var_can_have_subvars (var)
1249                   && (svars = get_subvars_for_var (var)))
1250                 {
1251                   subvar_t sv;
1252
1253                   for (sv = svars; sv; sv = sv->next)
1254                     {         
1255                       if (bitmap_bit_p (addressable_vars, DECL_UID (sv->var)))
1256                         okay_to_mark = false;
1257                       mark_sym_for_renaming (sv->var);
1258                     }
1259                 }
1260
1261               /* The address of VAR is not needed, remove the
1262                  addressable bit, so that it can be optimized as a
1263                  regular variable.  */
1264               if (okay_to_mark)
1265                 mark_non_addressable (var);
1266             }
1267         }
1268
1269       /* Global variables and addressable locals may be aliased.  Create an
1270          entry in ADDRESSABLE_VARS for VAR.  */
1271       if (may_be_aliased (var)    
1272           && (!var_can_have_subvars (var) 
1273               || get_subvars_for_var (var) == NULL))
1274         {
1275           create_alias_map_for (var, ai);
1276           mark_sym_for_renaming (var);
1277         }
1278
1279       /* Add pointer variables that have been dereferenced to the POINTERS
1280          array and create a type memory tag for them.  */
1281       if (POINTER_TYPE_P (TREE_TYPE (var)))
1282         {
1283           if ((bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var))
1284                || bitmap_bit_p (ai->dereferenced_ptrs_load, DECL_UID (var))))
1285             {
1286               tree tag;
1287               var_ann_t t_ann;
1288
1289               /* If pointer VAR still doesn't have a memory tag
1290                  associated with it, create it now or re-use an
1291                  existing one.  */
1292               tag = get_tmt_for (var, ai);
1293               t_ann = var_ann (tag);
1294
1295               /* The type tag will need to be renamed into SSA
1296                  afterwards. Note that we cannot do this inside
1297                  get_tmt_for because aliasing may run multiple times
1298                  and we only create type tags the first time.  */
1299               mark_sym_for_renaming (tag);
1300
1301               /* Similarly, if pointer VAR used to have another type
1302                  tag, we will need to process it in the renamer to
1303                  remove the stale virtual operands.  */
1304               if (v_ann->type_mem_tag)
1305                 mark_sym_for_renaming (v_ann->type_mem_tag);
1306
1307               /* Associate the tag with pointer VAR.  */
1308               v_ann->type_mem_tag = tag;
1309
1310               /* If pointer VAR has been used in a store operation,
1311                  then its memory tag must be marked as written-to.  */
1312               if (bitmap_bit_p (ai->dereferenced_ptrs_store, DECL_UID (var)))
1313                 bitmap_set_bit (ai->written_vars, DECL_UID (tag));
1314
1315               /* If pointer VAR is a global variable or a PARM_DECL,
1316                  then its memory tag should be considered a global
1317                  variable.  */
1318               if (TREE_CODE (var) == PARM_DECL || is_global_var (var))
1319                 mark_call_clobbered (tag);
1320
1321               /* All the dereferences of pointer VAR count as
1322                  references of TAG.  Since TAG can be associated with
1323                  several pointers, add the dereferences of VAR to the
1324                  TAG.  */
1325               NUM_REFERENCES_SET (t_ann, 
1326                                   NUM_REFERENCES (t_ann)
1327                                   + NUM_REFERENCES (v_ann));
1328             }
1329           else
1330             {
1331               /* The pointer has not been dereferenced.  If it had a
1332                  type memory tag, remove it and mark the old tag for
1333                  renaming to remove it out of the IL.  */
1334               var_ann_t ann = var_ann (var);
1335               tree tag = ann->type_mem_tag;
1336               if (tag)
1337                 {
1338                   mark_sym_for_renaming (tag);
1339                   ann->type_mem_tag = NULL_TREE;
1340                 }
1341             }
1342         }
1343     }
1344   VEC_free (tree, heap, varvec);
1345 }
1346
1347
1348 /* Determine whether to use .GLOBAL_VAR to model call clobbering semantics. At
1349    every call site, we need to emit V_MAY_DEF expressions to represent the
1350    clobbering effects of the call for variables whose address escapes the
1351    current function.
1352
1353    One approach is to group all call-clobbered variables into a single
1354    representative that is used as an alias of every call-clobbered variable
1355    (.GLOBAL_VAR).  This works well, but it ties the optimizer hands because
1356    references to any call clobbered variable is a reference to .GLOBAL_VAR.
1357
1358    The second approach is to emit a clobbering V_MAY_DEF for every 
1359    call-clobbered variable at call sites.  This is the preferred way in terms 
1360    of optimization opportunities but it may create too many V_MAY_DEF operands
1361    if there are many call clobbered variables and function calls in the 
1362    function.
1363
1364    To decide whether or not to use .GLOBAL_VAR we multiply the number of
1365    function calls found by the number of call-clobbered variables.  If that
1366    product is beyond a certain threshold, as determined by the parameterized
1367    values shown below, we use .GLOBAL_VAR.
1368
1369    FIXME.  This heuristic should be improved.  One idea is to use several
1370    .GLOBAL_VARs of different types instead of a single one.  The thresholds
1371    have been derived from a typical bootstrap cycle, including all target
1372    libraries. Compile times were found increase by ~1% compared to using
1373    .GLOBAL_VAR.  */
1374
1375 static void
1376 maybe_create_global_var (struct alias_info *ai)
1377 {
1378   unsigned i, n_clobbered;
1379   bitmap_iterator bi;
1380   
1381   /* No need to create it, if we have one already.  */
1382   if (global_var == NULL_TREE)
1383     {
1384       /* Count all the call-clobbered variables.  */
1385       n_clobbered = 0;
1386       EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1387         {
1388           n_clobbered++;
1389         }
1390
1391       /* If the number of virtual operands that would be needed to
1392          model all the call-clobbered variables is larger than
1393          GLOBAL_VAR_THRESHOLD, create .GLOBAL_VAR.
1394
1395          Also create .GLOBAL_VAR if there are no call-clobbered
1396          variables and the program contains a mixture of pure/const
1397          and regular function calls.  This is to avoid the problem
1398          described in PR 20115:
1399
1400               int X;
1401               int func_pure (void) { return X; }
1402               int func_non_pure (int a) { X += a; }
1403               int foo ()
1404               {
1405                 int a = func_pure ();
1406                 func_non_pure (a);
1407                 a = func_pure ();
1408                 return a;
1409               }
1410
1411          Since foo() has no call-clobbered variables, there is
1412          no relationship between the calls to func_pure and
1413          func_non_pure.  Since func_pure has no side-effects, value
1414          numbering optimizations elide the second call to func_pure.
1415          So, if we have some pure/const and some regular calls in the
1416          program we create .GLOBAL_VAR to avoid missing these
1417          relations.  */
1418       if (ai->num_calls_found * n_clobbered >= (size_t) GLOBAL_VAR_THRESHOLD
1419           || (n_clobbered == 0
1420               && ai->num_calls_found > 0
1421               && ai->num_pure_const_calls_found > 0
1422               && ai->num_calls_found > ai->num_pure_const_calls_found))
1423         create_global_var ();
1424     }
1425
1426   /* Mark all call-clobbered symbols for renaming.  Since the initial
1427      rewrite into SSA ignored all call sites, we may need to rename
1428      .GLOBAL_VAR and the call-clobbered variables.   */
1429   EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1430     {
1431       tree var = referenced_var (i);
1432
1433       /* If the function has calls to clobbering functions and
1434          .GLOBAL_VAR has been created, make it an alias for all
1435          call-clobbered variables.  */
1436       if (global_var && var != global_var)
1437         {
1438           subvar_t svars;
1439           add_may_alias (var, global_var);
1440           if (var_can_have_subvars (var)
1441               && (svars = get_subvars_for_var (var)))
1442             {
1443               subvar_t sv;
1444               for (sv = svars; sv; sv = sv->next)
1445                 mark_sym_for_renaming (sv->var);
1446             }
1447         }
1448       
1449       mark_sym_for_renaming (var);
1450     }
1451 }
1452
1453
1454 /* Return TRUE if pointer PTR may point to variable VAR.
1455    
1456    MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
1457         This is needed because when checking for type conflicts we are
1458         interested in the alias set of the memory location pointed-to by
1459         PTR.  The alias set of PTR itself is irrelevant.
1460    
1461    VAR_ALIAS_SET is the alias set for VAR.  */
1462
1463 static bool
1464 may_alias_p (tree ptr, HOST_WIDE_INT mem_alias_set,
1465              tree var, HOST_WIDE_INT var_alias_set,
1466              bool alias_set_only)
1467 {
1468   tree mem;
1469   var_ann_t m_ann;
1470
1471   alias_stats.alias_queries++;
1472   alias_stats.simple_queries++;
1473
1474   /* By convention, a variable cannot alias itself.  */
1475   mem = var_ann (ptr)->type_mem_tag;
1476   if (mem == var)
1477     {
1478       alias_stats.alias_noalias++;
1479       alias_stats.simple_resolved++;
1480       return false;
1481     }
1482   
1483   /* If -fargument-noalias-global is >1, pointer arguments may
1484      not point to global variables.  */
1485   if (flag_argument_noalias > 1 && is_global_var (var)
1486       && TREE_CODE (ptr) == PARM_DECL)
1487     {
1488       alias_stats.alias_noalias++;
1489       alias_stats.simple_resolved++;
1490       return false;
1491     }
1492
1493   /* If either MEM or VAR is a read-only global and the other one
1494      isn't, then PTR cannot point to VAR.  */
1495   if ((unmodifiable_var_p (mem) && !unmodifiable_var_p (var))
1496       || (unmodifiable_var_p (var) && !unmodifiable_var_p (mem)))
1497     {
1498       alias_stats.alias_noalias++;
1499       alias_stats.simple_resolved++;
1500       return false;
1501     }
1502
1503   m_ann = var_ann (mem);
1504
1505   gcc_assert (m_ann->mem_tag_kind == TYPE_TAG);
1506
1507   alias_stats.tbaa_queries++;
1508
1509   /* If the alias sets don't conflict then MEM cannot alias VAR.  */
1510   if (!alias_sets_conflict_p (mem_alias_set, var_alias_set))
1511     {
1512       alias_stats.alias_noalias++;
1513       alias_stats.tbaa_resolved++;
1514       return false;
1515     }
1516
1517   /* If var is a record or union type, ptr cannot point into var
1518      unless there is some operation explicit address operation in the
1519      program that can reference a field of the ptr's dereferenced
1520      type.  This also assumes that the types of both var and ptr are
1521      contained within the compilation unit, and that there is no fancy
1522      addressing arithmetic associated with any of the types
1523      involved.  */
1524
1525   if ((mem_alias_set != 0) && (var_alias_set != 0))
1526     {
1527       tree ptr_type = TREE_TYPE (ptr);
1528       tree var_type = TREE_TYPE (var);
1529       
1530       /* The star count is -1 if the type at the end of the pointer_to 
1531          chain is not a record or union type. */ 
1532       if ((!alias_set_only) && 
1533           ipa_type_escape_star_count_of_interesting_type (var_type) >= 0)
1534         {
1535           int ptr_star_count = 0;
1536           
1537           /* Ipa_type_escape_star_count_of_interesting_type is a little to
1538              restrictive for the pointer type, need to allow pointers to
1539              primitive types as long as those types cannot be pointers
1540              to everything.  */
1541           while (POINTER_TYPE_P (ptr_type))
1542             /* Strip the *'s off.  */ 
1543             {
1544               ptr_type = TREE_TYPE (ptr_type);
1545               ptr_star_count++;
1546             }
1547           
1548           /* There does not appear to be a better test to see if the 
1549              pointer type was one of the pointer to everything 
1550              types.  */
1551           
1552           if (ptr_star_count > 0)
1553             {
1554               alias_stats.structnoaddress_queries++;
1555               if (ipa_type_escape_field_does_not_clobber_p (var_type, 
1556                                                             TREE_TYPE (ptr))) 
1557                 {
1558                   alias_stats.structnoaddress_resolved++;
1559                   alias_stats.alias_noalias++;
1560                   return false;
1561                 }
1562             }
1563           else if (ptr_star_count == 0)
1564             {
1565               /* If ptr_type was not really a pointer to type, it cannot 
1566                  alias.  */ 
1567               alias_stats.structnoaddress_queries++;
1568               alias_stats.structnoaddress_resolved++;
1569               alias_stats.alias_noalias++;
1570               return false;
1571             }
1572         }
1573     }
1574
1575   alias_stats.alias_mayalias++;
1576   return true;
1577 }
1578
1579
1580 /* Add ALIAS to the set of variables that may alias VAR.  */
1581
1582 static void
1583 add_may_alias (tree var, tree alias)
1584 {
1585   size_t i;
1586   var_ann_t v_ann = get_var_ann (var);
1587   var_ann_t a_ann = get_var_ann (alias);
1588
1589   /* Don't allow self-referential aliases.  */
1590   gcc_assert (var != alias);
1591
1592   /* ALIAS must be addressable if it's being added to an alias set.  */
1593 #if 1
1594   TREE_ADDRESSABLE (alias) = 1;
1595 #else
1596   gcc_assert (may_be_aliased (alias));
1597 #endif
1598
1599   if (v_ann->may_aliases == NULL)
1600     VARRAY_TREE_INIT (v_ann->may_aliases, 2, "aliases");
1601
1602   /* Avoid adding duplicates.  */
1603   for (i = 0; i < VARRAY_ACTIVE_SIZE (v_ann->may_aliases); i++)
1604     if (alias == VARRAY_TREE (v_ann->may_aliases, i))
1605       return;
1606
1607   /* If VAR is a call-clobbered variable, so is its new ALIAS.
1608      FIXME, call-clobbering should only depend on whether an address
1609      escapes.  It should be independent of aliasing.  */
1610   if (is_call_clobbered (var))
1611     mark_call_clobbered (alias);
1612
1613   /* Likewise.  If ALIAS is call-clobbered, so is VAR.  */
1614   else if (is_call_clobbered (alias))
1615     mark_call_clobbered (var);
1616
1617   VARRAY_PUSH_TREE (v_ann->may_aliases, alias);
1618   a_ann->is_alias_tag = 1;
1619 }
1620
1621
1622 /* Replace alias I in the alias sets of VAR with NEW_ALIAS.  */
1623
1624 static void
1625 replace_may_alias (tree var, size_t i, tree new_alias)
1626 {
1627   var_ann_t v_ann = var_ann (var);
1628   VARRAY_TREE (v_ann->may_aliases, i) = new_alias;
1629
1630   /* If VAR is a call-clobbered variable, so is NEW_ALIAS.
1631      FIXME, call-clobbering should only depend on whether an address
1632      escapes.  It should be independent of aliasing.  */
1633   if (is_call_clobbered (var))
1634     mark_call_clobbered (new_alias);
1635
1636   /* Likewise.  If NEW_ALIAS is call-clobbered, so is VAR.  */
1637   else if (is_call_clobbered (new_alias))
1638     mark_call_clobbered (var);
1639 }
1640
1641
1642 /* Mark pointer PTR as pointing to an arbitrary memory location.  */
1643
1644 static void
1645 set_pt_anything (tree ptr)
1646 {
1647   struct ptr_info_def *pi = get_ptr_info (ptr);
1648
1649   pi->pt_anything = 1;
1650   pi->pt_vars = NULL;
1651
1652   /* The pointer used to have a name tag, but we now found it pointing
1653      to an arbitrary location.  The name tag needs to be renamed and
1654      disassociated from PTR.  */
1655   if (pi->name_mem_tag)
1656     {
1657       mark_sym_for_renaming (pi->name_mem_tag);
1658       pi->name_mem_tag = NULL_TREE;
1659     }
1660 }
1661
1662
1663 /* Return true if STMT is an "escape" site from the current function.  Escape
1664    sites those statements which might expose the address of a variable
1665    outside the current function.  STMT is an escape site iff:
1666
1667         1- STMT is a function call, or
1668         2- STMT is an __asm__ expression, or
1669         3- STMT is an assignment to a non-local variable, or
1670         4- STMT is a return statement.
1671
1672    AI points to the alias information collected so far.  */
1673
1674 bool
1675 is_escape_site (tree stmt, struct alias_info *ai)
1676 {
1677   tree call = get_call_expr_in (stmt);
1678   if (call != NULL_TREE)
1679     {
1680       ai->num_calls_found++;
1681
1682       if (!TREE_SIDE_EFFECTS (call))
1683         ai->num_pure_const_calls_found++;
1684
1685       return true;
1686     }
1687   else if (TREE_CODE (stmt) == ASM_EXPR)
1688     return true;
1689   else if (TREE_CODE (stmt) == MODIFY_EXPR)
1690     {
1691       tree lhs = TREE_OPERAND (stmt, 0);
1692
1693       /* Get to the base of _REF nodes.  */
1694       if (TREE_CODE (lhs) != SSA_NAME)
1695         lhs = get_base_address (lhs);
1696
1697       /* If we couldn't recognize the LHS of the assignment, assume that it
1698          is a non-local store.  */
1699       if (lhs == NULL_TREE)
1700         return true;
1701
1702       /* If the RHS is a conversion between a pointer and an integer, the
1703          pointer escapes since we can't track the integer.  */
1704       if ((TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
1705            || TREE_CODE (TREE_OPERAND (stmt, 1)) == CONVERT_EXPR
1706            || TREE_CODE (TREE_OPERAND (stmt, 1)) == VIEW_CONVERT_EXPR)
1707           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND
1708                                         (TREE_OPERAND (stmt, 1), 0)))
1709           && !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1710         return true;
1711
1712       /* If the LHS is an SSA name, it can't possibly represent a non-local
1713          memory store.  */
1714       if (TREE_CODE (lhs) == SSA_NAME)
1715         return false;
1716
1717       /* FIXME: LHS is not an SSA_NAME.  Even if it's an assignment to a
1718          local variables we cannot be sure if it will escape, because we
1719          don't have information about objects not in SSA form.  Need to
1720          implement something along the lines of
1721
1722          J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
1723          Midkiff, ``Escape analysis for java,'' in Proceedings of the
1724          Conference on Object-Oriented Programming Systems, Languages, and
1725          Applications (OOPSLA), pp. 1-19, 1999.  */
1726       return true;
1727     }
1728   else if (TREE_CODE (stmt) == RETURN_EXPR)
1729     return true;
1730
1731   return false;
1732 }
1733
1734
1735 /* Create a new memory tag of type TYPE.  If IS_TYPE_TAG is true, the tag
1736    is considered to represent all the pointers whose pointed-to types are
1737    in the same alias set class.  Otherwise, the tag represents a single
1738    SSA_NAME pointer variable.  */
1739
1740 static tree
1741 create_memory_tag (tree type, bool is_type_tag)
1742 {
1743   var_ann_t ann;
1744   tree tag = create_tmp_var_raw (type, (is_type_tag) ? "TMT" : "NMT");
1745
1746   /* By default, memory tags are local variables.  Alias analysis will
1747      determine whether they should be considered globals.  */
1748   DECL_CONTEXT (tag) = current_function_decl;
1749
1750   /* Memory tags are by definition addressable.  */
1751   TREE_ADDRESSABLE (tag) = 1;
1752
1753   ann = get_var_ann (tag);
1754   ann->mem_tag_kind = (is_type_tag) ? TYPE_TAG : NAME_TAG;
1755   ann->type_mem_tag = NULL_TREE;
1756
1757   /* Add the tag to the symbol table.  */
1758   add_referenced_tmp_var (tag);
1759
1760   return tag;
1761 }
1762
1763
1764 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
1765    This is used if P_i has been found to point to a specific set of
1766    variables or to a non-aliased memory location like the address returned
1767    by malloc functions.  */
1768
1769 static tree
1770 get_nmt_for (tree ptr)
1771 {
1772   struct ptr_info_def *pi = get_ptr_info (ptr);
1773   tree tag = pi->name_mem_tag;
1774
1775   if (tag == NULL_TREE)
1776     tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
1777
1778   /* If PTR is a PARM_DECL, it points to a global variable or malloc,
1779      then its name tag should be considered a global variable.  */
1780   if (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
1781       || pi->pt_global_mem)
1782     mark_call_clobbered (tag);
1783
1784   return tag;
1785 }
1786
1787
1788 /* Return the type memory tag associated to pointer PTR.  A memory tag is an
1789    artificial variable that represents the memory location pointed-to by
1790    PTR.  It is used to model the effects of pointer de-references on
1791    addressable variables.
1792    
1793    AI points to the data gathered during alias analysis.  This function
1794    populates the array AI->POINTERS.  */
1795
1796 static tree
1797 get_tmt_for (tree ptr, struct alias_info *ai)
1798 {
1799   size_t i;
1800   tree tag;
1801   tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
1802   HOST_WIDE_INT tag_set = get_alias_set (tag_type);
1803
1804   /* To avoid creating unnecessary memory tags, only create one memory tag
1805      per alias set class.  Note that it may be tempting to group
1806      memory tags based on conflicting alias sets instead of
1807      equivalence.  That would be wrong because alias sets are not
1808      necessarily transitive (as demonstrated by the libstdc++ test
1809      23_containers/vector/cons/4.cc).  Given three alias sets A, B, C
1810      such that conflicts (A, B) == true and conflicts (A, C) == true,
1811      it does not necessarily follow that conflicts (B, C) == true.  */
1812   for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
1813     {
1814       struct alias_map_d *curr = ai->pointers[i];
1815       tree curr_tag = var_ann (curr->var)->type_mem_tag;
1816       if (tag_set == curr->set
1817           && TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (curr_tag)))
1818         {
1819           tag = curr_tag;
1820           break;
1821         }
1822     }
1823
1824   /* If VAR cannot alias with any of the existing memory tags, create a new
1825      tag for PTR and add it to the POINTERS array.  */
1826   if (tag == NULL_TREE)
1827     {
1828       struct alias_map_d *alias_map;
1829
1830       /* If PTR did not have a type tag already, create a new TMT.*
1831          artificial variable representing the memory location
1832          pointed-to by PTR.  */
1833       if (var_ann (ptr)->type_mem_tag == NULL_TREE)
1834         tag = create_memory_tag (tag_type, true);
1835       else
1836         tag = var_ann (ptr)->type_mem_tag;
1837
1838       /* Add PTR to the POINTERS array.  Note that we are not interested in
1839          PTR's alias set.  Instead, we cache the alias set for the memory that
1840          PTR points to.  */
1841       alias_map = xcalloc (1, sizeof (*alias_map));
1842       alias_map->var = ptr;
1843       alias_map->set = tag_set;
1844       ai->pointers[ai->num_pointers++] = alias_map;
1845     }
1846
1847   /* If the pointed-to type is volatile, so is the tag.  */
1848   TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
1849
1850   /* Make sure that the type tag has the same alias set as the
1851      pointed-to type.  */
1852   gcc_assert (tag_set == get_alias_set (tag));
1853
1854   /* If PTR's pointed-to type is read-only, then TAG's type must also
1855      be read-only.  */
1856   gcc_assert (TYPE_READONLY (tag_type) == TYPE_READONLY (TREE_TYPE (tag)));
1857
1858   return tag;
1859 }
1860
1861
1862 /* Create GLOBAL_VAR, an artificial global variable to act as a
1863    representative of all the variables that may be clobbered by function
1864    calls.  */
1865
1866 static void
1867 create_global_var (void)
1868 {
1869   global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
1870                            void_type_node);
1871   DECL_ARTIFICIAL (global_var) = 1;
1872   TREE_READONLY (global_var) = 0;
1873   DECL_EXTERNAL (global_var) = 1;
1874   TREE_STATIC (global_var) = 1;
1875   TREE_USED (global_var) = 1;
1876   DECL_CONTEXT (global_var) = NULL_TREE;
1877   TREE_THIS_VOLATILE (global_var) = 0;
1878   TREE_ADDRESSABLE (global_var) = 0;
1879
1880   add_referenced_tmp_var (global_var);
1881   mark_sym_for_renaming (global_var);
1882 }
1883
1884
1885 /* Dump alias statistics on FILE.  */
1886
1887 static void 
1888 dump_alias_stats (FILE *file)
1889 {
1890   const char *funcname
1891     = lang_hooks.decl_printable_name (current_function_decl, 2);
1892   fprintf (file, "\nAlias statistics for %s\n\n", funcname);
1893   fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
1894   fprintf (file, "Total alias mayalias results:\t%u\n", 
1895            alias_stats.alias_mayalias);
1896   fprintf (file, "Total alias noalias results:\t%u\n",
1897            alias_stats.alias_noalias);
1898   fprintf (file, "Total simple queries:\t%u\n",
1899            alias_stats.simple_queries);
1900   fprintf (file, "Total simple resolved:\t%u\n",
1901            alias_stats.simple_resolved);
1902   fprintf (file, "Total TBAA queries:\t%u\n",
1903            alias_stats.tbaa_queries);
1904   fprintf (file, "Total TBAA resolved:\t%u\n",
1905            alias_stats.tbaa_resolved);
1906   fprintf (file, "Total non-addressable structure type queries:\t%u\n",
1907            alias_stats.structnoaddress_queries);
1908   fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
1909            alias_stats.structnoaddress_resolved);
1910 }
1911   
1912
1913 /* Dump alias information on FILE.  */
1914
1915 void
1916 dump_alias_info (FILE *file)
1917 {
1918   size_t i;
1919   const char *funcname
1920     = lang_hooks.decl_printable_name (current_function_decl, 2);
1921   referenced_var_iterator rvi;
1922   tree var;
1923
1924   fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
1925
1926   fprintf (file, "Aliased symbols\n\n");
1927   
1928   FOR_EACH_REFERENCED_VAR (var, rvi)
1929     {
1930       if (may_be_aliased (var))
1931         dump_variable (file, var);
1932     }
1933
1934   fprintf (file, "\nDereferenced pointers\n\n");
1935
1936   FOR_EACH_REFERENCED_VAR (var, rvi)
1937     {
1938       var_ann_t ann = var_ann (var);
1939       if (ann->type_mem_tag)
1940         dump_variable (file, var);
1941     }
1942
1943   fprintf (file, "\nType memory tags\n\n");
1944   
1945   FOR_EACH_REFERENCED_VAR (var, rvi)
1946     {
1947       var_ann_t ann = var_ann (var);
1948       if (ann->mem_tag_kind == TYPE_TAG)
1949         dump_variable (file, var);
1950     }
1951
1952   fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
1953
1954   fprintf (file, "SSA_NAME pointers\n\n");
1955   for (i = 1; i < num_ssa_names; i++)
1956     {
1957       tree ptr = ssa_name (i);
1958       struct ptr_info_def *pi;
1959       
1960       if (ptr == NULL_TREE)
1961         continue;
1962
1963       pi = SSA_NAME_PTR_INFO (ptr);
1964       if (!SSA_NAME_IN_FREE_LIST (ptr)
1965           && pi
1966           && pi->name_mem_tag)
1967         dump_points_to_info_for (file, ptr);
1968     }
1969
1970   fprintf (file, "\nName memory tags\n\n");
1971   
1972   FOR_EACH_REFERENCED_VAR (var, rvi)
1973     {
1974       var_ann_t ann = var_ann (var);
1975       if (ann->mem_tag_kind == NAME_TAG)
1976         dump_variable (file, var);
1977     }
1978
1979   fprintf (file, "\n");
1980 }
1981
1982
1983 /* Dump alias information on stderr.  */
1984
1985 void
1986 debug_alias_info (void)
1987 {
1988   dump_alias_info (stderr);
1989 }
1990
1991
1992 /* Return the alias information associated with pointer T.  It creates a
1993    new instance if none existed.  */
1994
1995 struct ptr_info_def *
1996 get_ptr_info (tree t)
1997 {
1998   struct ptr_info_def *pi;
1999
2000   gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2001
2002   pi = SSA_NAME_PTR_INFO (t);
2003   if (pi == NULL)
2004     {
2005       pi = ggc_alloc (sizeof (*pi));
2006       memset ((void *)pi, 0, sizeof (*pi));
2007       SSA_NAME_PTR_INFO (t) = pi;
2008     }
2009
2010   return pi;
2011 }
2012
2013
2014 /* Dump points-to information for SSA_NAME PTR into FILE.  */
2015
2016 void
2017 dump_points_to_info_for (FILE *file, tree ptr)
2018 {
2019   struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2020
2021   print_generic_expr (file, ptr, dump_flags);
2022
2023   if (pi)
2024     {
2025       if (pi->name_mem_tag)
2026         {
2027           fprintf (file, ", name memory tag: ");
2028           print_generic_expr (file, pi->name_mem_tag, dump_flags);
2029         }
2030
2031       if (pi->is_dereferenced)
2032         fprintf (file, ", is dereferenced");
2033
2034       if (pi->value_escapes_p)
2035         fprintf (file, ", its value escapes");
2036
2037       if (pi->pt_anything)
2038         fprintf (file, ", points-to anything");
2039
2040       if (pi->pt_null)
2041         fprintf (file, ", points-to NULL");
2042
2043       if (pi->pt_vars)
2044         {
2045           unsigned ix;
2046           bitmap_iterator bi;
2047
2048           fprintf (file, ", points-to vars: { ");
2049           EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi)
2050             {
2051               print_generic_expr (file, referenced_var (ix), dump_flags);
2052               fprintf (file, " ");
2053             }
2054           fprintf (file, "}");
2055         }
2056     }
2057
2058   fprintf (file, "\n");
2059 }
2060
2061
2062 /* Dump points-to information for VAR into stderr.  */
2063
2064 void
2065 debug_points_to_info_for (tree var)
2066 {
2067   dump_points_to_info_for (stderr, var);
2068 }
2069
2070
2071 /* Dump points-to information into FILE.  NOTE: This function is slow, as
2072    it needs to traverse the whole CFG looking for pointer SSA_NAMEs.  */
2073
2074 void
2075 dump_points_to_info (FILE *file)
2076 {
2077   basic_block bb;
2078   block_stmt_iterator si;
2079   ssa_op_iter iter;
2080   const char *fname =
2081     lang_hooks.decl_printable_name (current_function_decl, 2);
2082   referenced_var_iterator rvi;
2083   tree var;
2084
2085   fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
2086
2087   /* First dump points-to information for the default definitions of
2088      pointer variables.  This is necessary because default definitions are
2089      not part of the code.  */
2090   FOR_EACH_REFERENCED_VAR (var, rvi)
2091     {
2092       if (POINTER_TYPE_P (TREE_TYPE (var)))
2093         {
2094           tree def = default_def (var);
2095           if (def)
2096             dump_points_to_info_for (file, def);
2097         }
2098     }
2099
2100   /* Dump points-to information for every pointer defined in the program.  */
2101   FOR_EACH_BB (bb)
2102     {
2103       tree phi;
2104
2105       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2106         {
2107           tree ptr = PHI_RESULT (phi);
2108           if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2109             dump_points_to_info_for (file, ptr);
2110         }
2111
2112         for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2113           {
2114             tree stmt = bsi_stmt (si);
2115             tree def;
2116             FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2117               if (POINTER_TYPE_P (TREE_TYPE (def)))
2118                 dump_points_to_info_for (file, def);
2119           }
2120     }
2121
2122   fprintf (file, "\n");
2123 }
2124
2125
2126 /* Dump points-to info pointed to by PTO into STDERR.  */
2127
2128 void
2129 debug_points_to_info (void)
2130 {
2131   dump_points_to_info (stderr);
2132 }
2133
2134 /* Dump to FILE the list of variables that may be aliasing VAR.  */
2135
2136 void
2137 dump_may_aliases_for (FILE *file, tree var)
2138 {
2139   varray_type aliases;
2140   
2141   if (TREE_CODE (var) == SSA_NAME)
2142     var = SSA_NAME_VAR (var);
2143
2144   aliases = var_ann (var)->may_aliases;
2145   if (aliases)
2146     {
2147       size_t i;
2148       fprintf (file, "{ ");
2149       for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2150         {
2151           print_generic_expr (file, VARRAY_TREE (aliases, i), dump_flags);
2152           fprintf (file, " ");
2153         }
2154       fprintf (file, "}");
2155     }
2156 }
2157
2158
2159 /* Dump to stderr the list of variables that may be aliasing VAR.  */
2160
2161 void
2162 debug_may_aliases_for (tree var)
2163 {
2164   dump_may_aliases_for (stderr, var);
2165 }
2166
2167 /* Return true if VAR may be aliased.  */
2168
2169 bool
2170 may_be_aliased (tree var)
2171 {
2172   /* Obviously.  */
2173   if (TREE_ADDRESSABLE (var))
2174     return true;
2175
2176   /* Globally visible variables can have their addresses taken by other
2177      translation units.  */
2178   if (DECL_EXTERNAL (var) || TREE_PUBLIC (var))
2179     return true;
2180
2181   /* Automatic variables can't have their addresses escape any other way.
2182      This must be after the check for global variables, as extern declarations
2183      do not have TREE_STATIC set.  */
2184   if (!TREE_STATIC (var))
2185     return false;
2186
2187   /* If we're in unit-at-a-time mode, then we must have seen all occurrences
2188      of address-of operators, and so we can trust TREE_ADDRESSABLE.  Otherwise
2189      we can only be sure the variable isn't addressable if it's local to the
2190      current function.  */
2191   if (flag_unit_at_a_time)
2192     return false;
2193   if (decl_function_context (var) == current_function_decl)
2194     return false;
2195
2196   return true;
2197 }
2198
2199
2200 /* Given two symbols return TRUE if one is in the alias set of the other.  */
2201 bool
2202 is_aliased_with (tree tag, tree sym)
2203 {
2204   size_t i;
2205   varray_type aliases;
2206
2207   if (var_ann (sym)->is_alias_tag)
2208     {
2209       aliases = var_ann (tag)->may_aliases;
2210
2211       if (aliases == NULL)
2212         return false;
2213
2214       for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2215         if (VARRAY_TREE (aliases, i) == sym)
2216           return true;
2217     }
2218   else
2219     {
2220       aliases = var_ann (sym)->may_aliases;
2221
2222       if (aliases == NULL)
2223         return false;
2224
2225       for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2226         if (VARRAY_TREE (aliases, i) == tag)
2227           return true;
2228     }
2229
2230   return false;
2231 }
2232
2233
2234 /* Add VAR to the list of may-aliases of PTR's type tag.  If PTR
2235    doesn't already have a type tag, create one.  */
2236
2237 void
2238 add_type_alias (tree ptr, tree var)
2239 {
2240   varray_type aliases;
2241   tree tag;
2242   var_ann_t ann = var_ann (ptr);
2243   subvar_t svars;
2244   VEC (tree, heap) *varvec = NULL;  
2245
2246   if (ann->type_mem_tag == NULL_TREE)
2247     {
2248       tree q = NULL_TREE;
2249       tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2250       HOST_WIDE_INT tag_set = get_alias_set (tag_type);
2251       safe_referenced_var_iterator rvi;
2252
2253       /* PTR doesn't have a type tag, create a new one and add VAR to
2254          the new tag's alias set.
2255
2256          FIXME, This is slower than necessary.  We need to determine
2257          whether there is another pointer Q with the same alias set as
2258          PTR.  This could be sped up by having type tags associated
2259          with types.  */
2260       FOR_EACH_REFERENCED_VAR_SAFE (q, varvec, rvi)
2261         {
2262           if (POINTER_TYPE_P (TREE_TYPE (q))
2263               && tag_set == get_alias_set (TREE_TYPE (TREE_TYPE (q))))
2264             {
2265               /* Found another pointer Q with the same alias set as
2266                  the PTR's pointed-to type.  If Q has a type tag, use
2267                  it.  Otherwise, create a new memory tag for PTR.  */
2268               var_ann_t ann1 = var_ann (q);
2269               if (ann1->type_mem_tag)
2270                 ann->type_mem_tag = ann1->type_mem_tag;
2271               else
2272                 ann->type_mem_tag = create_memory_tag (tag_type, true);
2273               goto found_tag;
2274             }
2275         }
2276
2277       /* Couldn't find any other pointer with a type tag we could use.
2278          Create a new memory tag for PTR.  */
2279       ann->type_mem_tag = create_memory_tag (tag_type, true);
2280     }
2281
2282 found_tag:
2283   /* If VAR is not already PTR's type tag, add it to the may-alias set
2284      for PTR's type tag.  */
2285   gcc_assert (var_ann (var)->type_mem_tag == NOT_A_TAG);
2286   tag = ann->type_mem_tag;
2287
2288   /* If VAR has subvars, add the subvars to the tag instead of the
2289      actual var.  */
2290   if (var_can_have_subvars (var)
2291       && (svars = get_subvars_for_var (var)))
2292     {
2293       subvar_t sv;      
2294       for (sv = svars; sv; sv = sv->next)
2295         add_may_alias (tag, sv->var);
2296     }
2297   else
2298     add_may_alias (tag, var);
2299
2300   /* TAG and its set of aliases need to be marked for renaming.  */
2301   mark_sym_for_renaming (tag);
2302   if ((aliases = var_ann (tag)->may_aliases) != NULL)
2303     {
2304       size_t i;
2305       for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2306         mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2307     }
2308
2309   /* If we had grouped aliases, VAR may have aliases of its own.  Mark
2310      them for renaming as well.  Other statements referencing the
2311      aliases of VAR will need to be updated.  */
2312   if ((aliases = var_ann (var)->may_aliases) != NULL)
2313     {
2314       size_t i;
2315       for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2316         mark_sym_for_renaming (VARRAY_TREE (aliases, i));
2317     }
2318   VEC_free (tree, heap, varvec);
2319 }
2320
2321
2322 /* Create a new type tag for PTR.  Construct the may-alias list of this type
2323    tag so that it has the aliasing of VAR. 
2324
2325    Note, the set of aliases represented by the new type tag are not marked
2326    for renaming.  */
2327
2328 void
2329 new_type_alias (tree ptr, tree var)
2330 {
2331   var_ann_t p_ann = var_ann (ptr);
2332   tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
2333   var_ann_t v_ann = var_ann (var);
2334   tree tag;
2335   subvar_t svars;
2336
2337   gcc_assert (p_ann->type_mem_tag == NULL_TREE);
2338   gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG);
2339
2340   /* Add VAR to the may-alias set of PTR's new type tag.  If VAR has
2341      subvars, add the subvars to the tag instead of the actual var.  */
2342   if (var_can_have_subvars (var)
2343       && (svars = get_subvars_for_var (var)))
2344     {
2345       subvar_t sv;
2346
2347       tag = create_memory_tag (tag_type, true);
2348       p_ann->type_mem_tag = tag;
2349
2350       for (sv = svars; sv; sv = sv->next)
2351         add_may_alias (tag, sv->var);
2352     }
2353   else
2354     {
2355       /* The following is based on code in add_stmt_operand to ensure that the
2356          same defs/uses/vdefs/vuses will be found after replacing a reference
2357          to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
2358          is the address of var.  */
2359       varray_type aliases = v_ann->may_aliases;
2360
2361       if ((aliases != NULL)
2362           && (VARRAY_ACTIVE_SIZE (aliases) == 1))
2363         {
2364           tree ali = VARRAY_TREE (aliases, 0);
2365
2366           if (get_var_ann (ali)->mem_tag_kind == TYPE_TAG)
2367             {
2368               p_ann->type_mem_tag = ali;
2369               return;
2370             }
2371         }
2372
2373       tag = create_memory_tag (tag_type, true);
2374       p_ann->type_mem_tag = tag;
2375
2376       if (aliases == NULL)
2377         add_may_alias (tag, var);
2378       else
2379         {
2380           size_t i;
2381
2382           for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
2383             add_may_alias (tag, VARRAY_TREE (aliases, i));
2384         }
2385     }    
2386 }
2387
2388
2389
2390 /* This represents the used range of a variable.  */
2391
2392 typedef struct used_part
2393 {
2394   HOST_WIDE_INT minused;
2395   HOST_WIDE_INT maxused;
2396   /* True if we have an explicit use/def of some portion of this variable,
2397      even if it is all of it. i.e. a.b = 5 or temp = a.b.  */
2398   bool explicit_uses;
2399   /* True if we have an implicit use/def of some portion of this
2400      variable.  Implicit uses occur when we can't tell what part we
2401      are referencing, and have to make conservative assumptions.  */
2402   bool implicit_uses;
2403 } *used_part_t;
2404
2405 /* An array of used_part structures, indexed by variable uid.  */
2406
2407 static htab_t used_portions;
2408
2409 struct used_part_map
2410 {
2411   unsigned int uid;
2412   used_part_t to;
2413 };
2414
2415 /* Return true if the uid in the two used part maps are equal.  */
2416
2417 static int
2418 used_part_map_eq (const void *va, const void *vb)
2419 {
2420   const struct used_part_map  *a = va, *b = vb;
2421   return (a->uid == b->uid);
2422 }
2423
2424 /* Hash a from uid in a used_part_map.  */
2425
2426 static unsigned int
2427 used_part_map_hash (const void *item)
2428 {
2429   return ((const struct used_part_map *)item)->uid;
2430 }
2431
2432 /* Free a used part map element.  */
2433
2434 static void 
2435 free_used_part_map (void *item)
2436 {
2437   free (((struct used_part_map *)item)->to);
2438   free (item);
2439 }
2440
2441 /* Lookup a used_part structure for a UID.  */
2442
2443 static used_part_t
2444 up_lookup (unsigned int uid)
2445 {
2446   struct used_part_map *h, in;
2447   in.uid = uid;
2448   h = htab_find_with_hash (used_portions, &in, uid);
2449   if (!h)
2450     return NULL;
2451   return h->to;
2452 }
2453
2454 /* Insert the pair UID, TO into the used part hashtable.  */
2455  
2456 static void 
2457 up_insert (unsigned int uid, used_part_t to)
2458
2459   struct used_part_map *h;
2460   void **loc;
2461
2462   h = xmalloc (sizeof (struct used_part_map));
2463   h->uid = uid;
2464   h->to = to;
2465   loc = htab_find_slot_with_hash (used_portions, h,
2466                                   uid, INSERT);
2467   if (*loc != NULL)
2468     free (*loc);
2469   *(struct used_part_map **)  loc = h;
2470 }
2471
2472
2473 /* Given a variable uid, UID, get or create the entry in the used portions
2474    table for the variable.  */
2475
2476 static used_part_t
2477 get_or_create_used_part_for (size_t uid)
2478 {
2479   used_part_t up;
2480   if ((up = up_lookup (uid)) == NULL)
2481     {
2482       up = xcalloc (1, sizeof (struct used_part));
2483       up->minused = INT_MAX;
2484       up->maxused = 0;
2485       up->explicit_uses = false;
2486       up->implicit_uses = false;
2487     }
2488
2489   return up;
2490 }
2491
2492
2493 /* Create and return a structure sub-variable for field FIELD of
2494    variable VAR.  */
2495
2496 static tree
2497 create_sft (tree var, tree field)
2498 {
2499   var_ann_t ann;
2500   tree subvar = create_tmp_var_raw (TREE_TYPE (field), "SFT");
2501
2502   /* We need to copy the various flags from VAR to SUBVAR, so that
2503      they are is_global_var iff the original variable was.  */
2504   DECL_CONTEXT (subvar) = DECL_CONTEXT (var);
2505   DECL_EXTERNAL (subvar) = DECL_EXTERNAL (var);
2506   TREE_PUBLIC  (subvar) = TREE_PUBLIC (var);
2507   TREE_STATIC (subvar) = TREE_STATIC (var);
2508   TREE_READONLY (subvar) = TREE_READONLY (var);
2509
2510   /* Add the new variable to REFERENCED_VARS.  */
2511   ann = get_var_ann (subvar);
2512   ann->mem_tag_kind = STRUCT_FIELD; 
2513   ann->type_mem_tag = NULL;     
2514   add_referenced_tmp_var (subvar);
2515
2516   return subvar;
2517 }
2518
2519
2520 /* Given an aggregate VAR, create the subvariables that represent its
2521    fields.  */
2522
2523 static void
2524 create_overlap_variables_for (tree var)
2525 {
2526   VEC(fieldoff_s,heap) *fieldstack = NULL;
2527   used_part_t up;
2528   size_t uid = DECL_UID (var);
2529
2530   if (!up_lookup (uid))
2531     return;
2532
2533   up = up_lookup (uid);
2534   push_fields_onto_fieldstack (TREE_TYPE (var), &fieldstack, 0, NULL);
2535   if (VEC_length (fieldoff_s, fieldstack) != 0)
2536     {
2537       subvar_t *subvars;
2538       fieldoff_s *fo;
2539       bool notokay = false;
2540       int fieldcount = 0;
2541       int i;
2542       HOST_WIDE_INT lastfooffset = -1;
2543       HOST_WIDE_INT lastfosize = -1;
2544       tree lastfotype = NULL_TREE;
2545
2546       /* Not all fields have DECL_SIZE set, and those that don't, we don't
2547          know their size, and thus, can't handle.
2548          The same is true of fields with DECL_SIZE that is not an integer
2549          constant (such as variable sized fields).
2550          Fields with offsets which are not constant will have an offset < 0 
2551          We *could* handle fields that are constant sized arrays, but
2552          currently don't.  Doing so would require some extra changes to
2553          tree-ssa-operands.c.  */
2554
2555       for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
2556         {
2557           if (!DECL_SIZE (fo->field) 
2558               || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
2559               || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
2560               || fo->offset < 0)
2561             {
2562               notokay = true;
2563               break;
2564             }
2565           fieldcount++;
2566         }
2567
2568       /* The current heuristic we use is as follows:
2569          If the variable has no used portions in this function, no
2570          structure vars are created for it.
2571          Otherwise,
2572          If the variable has less than SALIAS_MAX_IMPLICIT_FIELDS,
2573          we always create structure vars for them.
2574          If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2575          some explicit uses, we create structure vars for them.
2576          If the variable has more than SALIAS_MAX_IMPLICIT_FIELDS, and
2577          no explicit uses, we do not create structure vars for them.
2578       */
2579       
2580       if (fieldcount >= SALIAS_MAX_IMPLICIT_FIELDS
2581           && !up->explicit_uses)
2582         {
2583           if (dump_file && (dump_flags & TDF_DETAILS))
2584             {
2585               fprintf (dump_file, "Variable ");
2586               print_generic_expr (dump_file, var, 0);
2587               fprintf (dump_file, " has no explicit uses in this function, and is > SALIAS_MAX_IMPLICIT_FIELDS, so skipping\n");
2588             }
2589           notokay = true;
2590         }
2591       
2592       /* Bail out, if we can't create overlap variables.  */
2593       if (notokay)
2594         {
2595           VEC_free (fieldoff_s, heap, fieldstack);
2596           return;
2597         }
2598       
2599       /* Otherwise, create the variables.  */
2600       subvars = lookup_subvars_for_var (var);
2601       
2602       sort_fieldstack (fieldstack);
2603
2604       for (i = VEC_length (fieldoff_s, fieldstack);
2605            VEC_iterate (fieldoff_s, fieldstack, --i, fo);)
2606         {
2607           subvar_t sv;
2608           HOST_WIDE_INT fosize;
2609           tree currfotype;
2610
2611           fosize = TREE_INT_CST_LOW (DECL_SIZE (fo->field));
2612           currfotype = TREE_TYPE (fo->field);
2613
2614           /* If this field isn't in the used portion,
2615              or it has the exact same offset and size as the last
2616              field, skip it.  */
2617
2618           if (((fo->offset <= up->minused
2619                 && fo->offset + fosize <= up->minused)
2620                || fo->offset >= up->maxused)
2621               || (fo->offset == lastfooffset
2622                   && fosize == lastfosize
2623                   && currfotype == lastfotype))
2624             continue;
2625           sv = ggc_alloc (sizeof (struct subvar));
2626           sv->offset = fo->offset;
2627           sv->size = fosize;
2628           sv->next = *subvars;
2629           sv->var = create_sft (var, fo->field);
2630
2631           if (dump_file)
2632             {
2633               fprintf (dump_file, "structure field tag %s created for var %s",
2634                        get_name (sv->var), get_name (var));
2635               fprintf (dump_file, " offset " HOST_WIDE_INT_PRINT_DEC,
2636                        sv->offset);
2637               fprintf (dump_file, " size " HOST_WIDE_INT_PRINT_DEC,
2638                        sv->size);
2639               fprintf (dump_file, "\n");
2640             }
2641           
2642           lastfotype = currfotype;
2643           lastfooffset = fo->offset;
2644           lastfosize = fosize;
2645           *subvars = sv;
2646         }
2647
2648       /* Once we have created subvars, the original is no longer call
2649          clobbered on its own.  Its call clobbered status depends
2650          completely on the call clobbered status of the subvars.
2651
2652          add_referenced_var in the above loop will take care of
2653          marking subvars of global variables as call clobbered for us
2654          to start, since they are global as well.  */
2655       clear_call_clobbered (var);
2656     }
2657
2658   VEC_free (fieldoff_s, heap, fieldstack);
2659 }
2660
2661
2662 /* Find the conservative answer to the question of what portions of what 
2663    structures are used by this statement.  We assume that if we have a
2664    component ref with a known size + offset, that we only need that part
2665    of the structure.  For unknown cases, or cases where we do something
2666    to the whole structure, we assume we need to create fields for the 
2667    entire structure.  */
2668
2669 static tree
2670 find_used_portions (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2671 {
2672   switch (TREE_CODE (*tp))
2673     {
2674     case COMPONENT_REF:
2675       {
2676         HOST_WIDE_INT bitsize;
2677         HOST_WIDE_INT bitpos;
2678         tree offset;
2679         enum machine_mode mode;
2680         int unsignedp;
2681         int volatilep;  
2682         tree ref;
2683         ref = get_inner_reference (*tp, &bitsize, &bitpos, &offset, &mode,
2684                                    &unsignedp, &volatilep, false);
2685         if (DECL_P (ref) && offset == NULL && bitsize != -1)
2686           {         
2687             size_t uid = DECL_UID (ref);
2688             used_part_t up;
2689
2690             up = get_or_create_used_part_for (uid);         
2691
2692             if (bitpos <= up->minused)
2693               up->minused = bitpos;
2694             if ((bitpos + bitsize >= up->maxused))
2695               up->maxused = bitpos + bitsize;       
2696
2697             up->explicit_uses = true;
2698             up_insert (uid, up);
2699
2700             *walk_subtrees = 0;
2701             return NULL_TREE;
2702           }
2703         else if (DECL_P (ref))
2704           {
2705             if (DECL_SIZE (ref)
2706                 && var_can_have_subvars (ref)
2707                 && TREE_CODE (DECL_SIZE (ref)) == INTEGER_CST)
2708               {
2709                 used_part_t up;
2710                 size_t uid = DECL_UID (ref);
2711
2712                 up = get_or_create_used_part_for (uid);
2713
2714                 up->minused = 0;
2715                 up->maxused = TREE_INT_CST_LOW (DECL_SIZE (ref));
2716
2717                 up->implicit_uses = true;
2718
2719                 up_insert (uid, up);
2720
2721                 *walk_subtrees = 0;
2722                 return NULL_TREE;
2723               }
2724           }
2725       }
2726       break;
2727       /* This is here to make sure we mark the entire base variable as used
2728          when you take its address.  Because our used portion analysis is
2729          simple, we aren't looking at casts or pointer arithmetic to see what
2730          happens when you take the address.  */
2731     case ADDR_EXPR:
2732       {
2733         tree var = get_base_address (TREE_OPERAND (*tp, 0));
2734
2735         if (var 
2736             && DECL_P (var)
2737             && DECL_SIZE (var)
2738             && var_can_have_subvars (var)
2739             && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2740           {
2741             used_part_t up;
2742             size_t uid = DECL_UID (var);
2743             
2744             up = get_or_create_used_part_for (uid);
2745  
2746             up->minused = 0;
2747             up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
2748             up->implicit_uses = true;
2749
2750             up_insert (uid, up);
2751             *walk_subtrees = 0;
2752             return NULL_TREE;
2753           }
2754       }
2755       break;
2756     case VAR_DECL:
2757     case PARM_DECL:
2758     case RESULT_DECL:
2759       {
2760         tree var = *tp;
2761         if (DECL_SIZE (var)
2762             && var_can_have_subvars (var)
2763             && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2764           {
2765             used_part_t up;
2766             size_t uid = DECL_UID (var);
2767             
2768             up = get_or_create_used_part_for (uid);
2769  
2770             up->minused = 0;
2771             up->maxused = TREE_INT_CST_LOW (DECL_SIZE (var));
2772             up->implicit_uses = true;
2773
2774             up_insert (uid, up);
2775             *walk_subtrees = 0;
2776             return NULL_TREE;
2777           }
2778       }
2779       break;
2780       
2781     default:
2782       break;
2783       
2784     }
2785   return NULL_TREE;
2786 }
2787
2788 /* Create structure field variables for structures used in this function.  */
2789
2790 static void
2791 create_structure_vars (void)
2792 {
2793   basic_block bb;
2794   safe_referenced_var_iterator rvi;
2795   VEC (tree, heap) *varvec = NULL;
2796   tree var;
2797
2798   used_portions = htab_create (10, used_part_map_hash, used_part_map_eq, 
2799                                free_used_part_map);
2800   
2801   FOR_EACH_BB (bb)
2802     {
2803       block_stmt_iterator bsi;
2804       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2805         {
2806           walk_tree_without_duplicates (bsi_stmt_ptr (bsi), 
2807                                         find_used_portions,
2808                                         NULL);
2809         }
2810     }
2811   FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, rvi)
2812     {
2813       /* The C++ FE creates vars without DECL_SIZE set, for some reason.  */
2814       if (var     
2815           && DECL_SIZE (var)
2816           && var_can_have_subvars (var)
2817           && var_ann (var)->mem_tag_kind == NOT_A_TAG
2818           && TREE_CODE (DECL_SIZE (var)) == INTEGER_CST)
2819         create_overlap_variables_for (var);
2820     }
2821   htab_delete (used_portions);
2822   VEC_free (tree, heap, varvec);
2823
2824 }
2825
2826 static bool
2827 gate_structure_vars (void)
2828 {
2829   return flag_tree_salias != 0;
2830 }
2831
2832 struct tree_opt_pass pass_create_structure_vars = 
2833 {
2834   "salias",              /* name */
2835   gate_structure_vars,   /* gate */
2836   create_structure_vars, /* execute */
2837   NULL,                  /* sub */
2838   NULL,                  /* next */
2839   0,                     /* static_pass_number */
2840   0,                     /* tv_id */
2841   PROP_cfg,              /* properties_required */
2842   0,                     /* properties_provided */
2843   0,                     /* properties_destroyed */
2844   0,                     /* todo_flags_start */
2845   TODO_dump_func,        /* todo_flags_finish */
2846   0                      /* letter */
2847 };