OSDN Git Service

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