OSDN Git Service

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