OSDN Git Service

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