OSDN Git Service

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