OSDN Git Service

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