OSDN Git Service

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