OSDN Git Service

2009-01-26 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2    Copyright (C) 2004, 2005, 2006, 2007, 2008 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "expr.h"
32 #include "ggc.h"
33 #include "langhooks.h"
34 #include "flags.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "tree-dump.h"
38 #include "gimple.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "tree-pass.h"
42 #include "tree-ssa-structalias.h"
43 #include "convert.h"
44 #include "params.h"
45 #include "ipa-type-escape.h"
46 #include "vec.h"
47 #include "bitmap.h"
48 #include "vecprim.h"
49 #include "pointer-set.h"
50 #include "alloc-pool.h"
51
52 /* Broad overview of how aliasing works:
53    
54    First we compute points-to sets, which is done in
55    tree-ssa-structalias.c
56       
57    During points-to set constraint finding, a bunch of little bits of
58    information is collected.
59    This is not done because it is necessary for points-to, but because
60    points-to has to walk every statement anyway.  The function performing
61    this collecting is update_alias_info.
62
63    Bits update_alias_info collects include:
64    1. Directly escaping variables and variables whose value escapes
65    (using is_escape_site).  This is the set of variables and values that
66    escape prior to transitive closure of the clobbers.
67    2.  The set of variables dereferenced on the LHS (into
68    dereferenced_ptr_stores) 
69    3. The set of variables dereferenced on the RHS (into
70    dereferenced_ptr_loads) 
71    4. The set of all pointers we saw.
72    5. The number of loads and stores for each variable
73    6. The number of statements touching memory
74    7. The set of address taken variables.
75    
76    
77    #1 is computed by a combination of is_escape_site, and counting the
78    number of uses/deref operators.  This function properly accounts for
79    situations like &ptr->field, which is *not* a dereference.
80    
81    After points-to sets are computed, the sets themselves still
82    contain points-to specific variables, such as a variable that says
83    the pointer points to anything, a variable that says the pointer
84    points to readonly memory, etc.
85
86    These are eliminated in a later phase, as we will see.
87
88    The rest of the phases are located in tree-ssa-alias.c
89
90    The next phase after points-to set computation is called
91    "setup_pointers_and_addressables"
92
93    This pass does 3 main things:
94    
95    1. All variables that can have TREE_ADDRESSABLE removed safely (IE
96    non-globals whose address is not taken), have TREE_ADDRESSABLE
97    removed.
98    2. All variables that may be aliased (which is the set of addressable
99    variables and globals) at all, are marked for renaming, and have
100    symbol memory tags created for them.
101    3. All variables which are stored into have their SMT's added to
102    written vars. 
103
104
105    After this function is run, all variables that will ever have an
106    SMT, have one, though its aliases are not filled in.
107
108    The next phase is to compute flow-insensitive aliasing, which in
109    our case, is a misnomer.  it is really computing aliasing that
110    requires no transitive closure to be correct.  In particular, it
111    uses stack vs non-stack, TBAA, etc, to determine whether two
112    symbols could *ever* alias .  This phase works by going through all
113    the pointers we collected during update_alias_info, and for every
114    addressable variable in the program, seeing if they alias.  If so,
115    the addressable variable is added to the symbol memory tag for the
116    pointer.
117
118    As part of this, we handle symbol memory tags that conflict but
119    have no aliases in common, by forcing them to have a symbol in
120    common (through unioning alias sets or adding one as an alias of
121    the other), or by adding one as an alias of another.  The case of
122    conflicts with no aliases in common occurs mainly due to aliasing
123    we cannot see.  In particular, it generally means we have a load
124    through a pointer whose value came from outside the function.
125    Without an addressable symbol to point to, they would get the wrong
126    answer.
127
128    After flow insensitive aliasing is computed, we compute name tags
129    (called compute_flow_sensitive_info).  We walk each pointer we
130    collected and see if it has a usable points-to set.  If so, we
131    generate a name tag using that pointer, and make an alias bitmap for
132    it.  Name tags are shared between all things with the same alias
133    bitmap.  The alias bitmap will be translated from what points-to
134    computed.  In particular, the "anything" variable in points-to will be
135    transformed into a pruned set of SMT's and their aliases that
136    compute_flow_insensitive_aliasing computed.
137    Note that since 4.3, every pointer that points-to computed a solution for
138    will get a name tag (whereas before 4.3, only those whose set did
139    *not* include the anything variable would).  At the point where name
140    tags are all assigned, symbol memory tags are dead, and could be
141    deleted, *except* on global variables.  Global variables still use
142    symbol memory tags as of right now.
143
144    After name tags are computed, the set of clobbered variables is
145    transitively closed.  In particular, we compute the set of clobbered
146    variables based on the initial set of clobbers, plus the aliases of
147    pointers which either escape, or have their value escape.
148
149    After this, maybe_create_global_var is run, which handles a corner
150    case where we have no call clobbered variables, but have pure and
151    non-pure functions.
152    
153    Staring at this function, I now remember it is a hack for the fact
154    that we do not mark all globals in the program as call clobbered for a
155    function unless they are actually used in that function.  Instead,  we
156    only mark the set that is actually clobbered.  As a result, you can
157    end up with situations where you have no call clobbered vars set.
158    
159    After maybe_create_global_var, we set pointers with the REF_ALL flag
160    to have alias sets that include all clobbered
161    memory tags and variables.
162    
163    After this, memory partitioning is computed (by the function
164    compute_memory_partitions) and alias sets are reworked accordingly.
165
166    Lastly, we delete partitions with no symbols, and clean up after
167    ourselves.  */
168
169
170 /* Alias information used by compute_may_aliases and its helpers.  */
171 struct alias_info
172 {
173   /* SSA names visited while collecting points-to information.  If bit I
174      is set, it means that SSA variable with version I has already been
175      visited.  */
176   sbitmap ssa_names_visited;
177
178   /* Array of SSA_NAME pointers processed by the points-to collector.  */
179   VEC(tree,heap) *processed_ptrs;
180
181   /* ADDRESSABLE_VARS contains all the global variables and locals that
182      have had their address taken.  */
183   struct alias_map_d **addressable_vars;
184   size_t num_addressable_vars;
185
186   /* POINTERS contains all the _DECL pointers with unique memory tags
187      that have been referenced in the program.  */
188   struct alias_map_d **pointers;
189   size_t num_pointers;
190
191   /* Pointers that have been used in an indirect load/store operation.  */
192   struct pointer_set_t *dereferenced_ptrs;
193 };
194
195
196 /* Structure to map a variable to its alias set.  */
197 struct alias_map_d
198 {
199   /* Variable and its alias set.  */
200   tree var;
201   alias_set_type set;
202 };
203
204
205 /* Counters used to display statistics on alias analysis.  */
206 struct alias_stats_d
207 {
208   unsigned int alias_queries;
209   unsigned int alias_mayalias;
210   unsigned int alias_noalias;
211   unsigned int simple_queries;
212   unsigned int simple_resolved;
213   unsigned int tbaa_queries;
214   unsigned int tbaa_resolved;
215   unsigned int structnoaddress_queries;
216   unsigned int structnoaddress_resolved;
217 };
218
219
220 /* Local variables.  */
221 static struct alias_stats_d alias_stats;
222 static bitmap_obstack alias_bitmap_obstack;
223
224 /* Local functions.  */
225 static void compute_flow_insensitive_aliasing (struct alias_info *);
226 static void dump_alias_stats (FILE *);
227 static tree create_memory_tag (tree type, bool is_type_tag);
228 static tree get_smt_for (tree, struct alias_info *);
229 static tree get_nmt_for (tree);
230 static void add_may_alias (tree, tree);
231 static struct alias_info *init_alias_info (void);
232 static void delete_alias_info (struct alias_info *);
233 static void compute_flow_sensitive_aliasing (struct alias_info *);
234 static void setup_pointers_and_addressables (struct alias_info *);
235 static void update_alias_info (struct alias_info *);
236 static void create_global_var (void);
237 static void maybe_create_global_var (void);
238 static void set_pt_anything (tree);
239
240 void debug_mp_info (VEC(mem_sym_stats_t,heap) *);
241
242 static alloc_pool mem_sym_stats_pool;
243
244 /* Return memory reference stats for symbol VAR.  Create a new slot in
245    cfun->gimple_df->mem_sym_stats if needed.  */
246
247 static struct mem_sym_stats_d *
248 get_mem_sym_stats_for (tree var)
249 {
250   void **slot;
251   struct mem_sym_stats_d *stats;
252   struct pointer_map_t *map = gimple_mem_ref_stats (cfun)->mem_sym_stats;
253   
254   gcc_assert (map);
255
256   slot = pointer_map_insert (map, var);
257   if (*slot == NULL)
258     {
259       stats = (struct mem_sym_stats_d *) pool_alloc (mem_sym_stats_pool);
260       memset (stats, 0, sizeof (*stats));
261       stats->var = var;
262       *slot = (void *) stats;
263     }
264   else
265     stats = (struct mem_sym_stats_d *) *slot;
266
267   return stats;
268 }
269
270
271 /* Return memory reference statistics for variable VAR in function FN.
272    This is computed by alias analysis, but it is not kept
273    incrementally up-to-date.  So, these stats are only accurate if
274    pass_may_alias has been run recently.  If no alias information
275    exists, this function returns NULL.  */
276
277 static mem_sym_stats_t
278 mem_sym_stats (struct function *fn, tree var)
279 {
280   void **slot;
281   struct pointer_map_t *stats_map = gimple_mem_ref_stats (fn)->mem_sym_stats;
282
283   if (stats_map == NULL)
284     return NULL;
285
286   slot = pointer_map_contains (stats_map, var);
287   if (slot == NULL)
288     return NULL;
289
290   return (mem_sym_stats_t) *slot;
291 }
292
293
294 /* Set MPT to be the memory partition associated with symbol SYM.  */
295
296 static inline void
297 set_memory_partition (tree sym, tree mpt)
298 {
299 #if defined ENABLE_CHECKING
300   if (mpt)
301     gcc_assert (TREE_CODE (mpt) == MEMORY_PARTITION_TAG
302                 && !is_gimple_reg (sym));
303 #endif
304
305   var_ann (sym)->mpt = mpt;
306   if (mpt)
307     {
308       if (MPT_SYMBOLS (mpt) == NULL)
309         MPT_SYMBOLS (mpt) = BITMAP_ALLOC (&alias_bitmap_obstack);
310
311       bitmap_set_bit (MPT_SYMBOLS (mpt), DECL_UID (sym));
312
313       /* MPT inherits the call-clobbering attributes from SYM.  */
314       if (is_call_clobbered (sym))
315         {
316           MTAG_GLOBAL (mpt) = 1;
317           mark_call_clobbered (mpt, ESCAPE_IS_GLOBAL);
318         }
319     }
320 }
321
322
323 /* Mark variable VAR as being non-addressable.  */
324
325 static void
326 mark_non_addressable (tree var)
327 {
328   tree mpt;
329
330   if (!TREE_ADDRESSABLE (var))
331     return;
332
333   mpt = memory_partition (var);
334
335   clear_call_clobbered (var);
336   TREE_ADDRESSABLE (var) = 0;
337
338   if (mpt)
339     {
340       /* Note that it's possible for a symbol to have an associated
341          MPT and the MPT have a NULL empty set.  During
342          init_alias_info, all MPTs get their sets cleared out, but the
343          symbols still point to the old MPTs that used to hold them.
344          This is done so that compute_memory_partitions can now which
345          symbols are losing or changing partitions and mark them for
346          renaming.  */
347       if (MPT_SYMBOLS (mpt))
348         bitmap_clear_bit (MPT_SYMBOLS (mpt), DECL_UID (var));
349       set_memory_partition (var, NULL_TREE);
350     }
351 }
352
353
354 /* qsort comparison function to sort type/name tags by DECL_UID.  */
355
356 static int
357 sort_tags_by_id (const void *pa, const void *pb)
358 {
359   const_tree const a = *(const_tree const *)pa;
360   const_tree const b = *(const_tree const *)pb;
361  
362   return DECL_UID (a) - DECL_UID (b);
363 }
364
365 /* Initialize WORKLIST to contain those memory tags that are marked call
366    clobbered.  Initialized WORKLIST2 to contain the reasons these
367    memory tags escaped.  */
368
369 static void
370 init_transitive_clobber_worklist (VEC (tree, heap) **worklist,
371                                   VEC (int, heap) **worklist2,
372                                   bitmap on_worklist)
373 {
374   referenced_var_iterator rvi;
375   tree curr;
376
377   FOR_EACH_REFERENCED_VAR (curr, rvi)
378     {
379       if (MTAG_P (curr) && is_call_clobbered (curr))
380         {
381           VEC_safe_push (tree, heap, *worklist, curr);
382           VEC_safe_push (int, heap, *worklist2,
383                          var_ann (curr)->escape_mask);
384           bitmap_set_bit (on_worklist, DECL_UID (curr));
385         }
386     }
387 }
388
389 /* Add ALIAS to WORKLIST (and the reason for escaping REASON to WORKLIST2) if
390    ALIAS is not already marked call clobbered, and is a memory
391    tag.  */
392
393 static void
394 add_to_worklist (tree alias, VEC (tree, heap) **worklist,
395                  VEC (int, heap) **worklist2, int reason,
396                  bitmap on_worklist)
397 {
398   if (MTAG_P (alias) && !is_call_clobbered (alias)
399       && !bitmap_bit_p (on_worklist, DECL_UID (alias)))
400     {
401       VEC_safe_push (tree, heap, *worklist, alias);
402       VEC_safe_push (int, heap, *worklist2, reason);
403       bitmap_set_bit (on_worklist, DECL_UID (alias));
404     }
405 }
406
407 /* Mark aliases of TAG as call clobbered, and place any tags on the
408    alias list that were not already call clobbered on WORKLIST.  */
409
410 static void
411 mark_aliases_call_clobbered (tree tag, VEC (tree, heap) **worklist,
412                              VEC (int, heap) **worklist2, bitmap on_worklist)
413 {
414   bitmap aliases;
415   bitmap_iterator bi;
416   unsigned int i;
417   tree entry;
418   var_ann_t ta = var_ann (tag);
419
420   if (!MTAG_P (tag))
421     return;
422   aliases = may_aliases (tag);
423   if (!aliases)
424     return;
425
426   EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
427     {
428       entry = referenced_var (i);
429       /* If you clobber one part of a structure, you
430          clobber the entire thing.  While this does not make
431          the world a particularly nice place, it is necessary
432          in order to allow C/C++ tricks that involve
433          pointer arithmetic to work.  */
434       if (!unmodifiable_var_p (entry))
435         {
436           add_to_worklist (entry, worklist, worklist2, ta->escape_mask,
437                            on_worklist);
438           mark_call_clobbered (entry, ta->escape_mask);
439         }
440     }
441 }
442
443 /* Tags containing global vars need to be marked as global.
444    Tags containing call clobbered vars need to be marked as call
445    clobbered. */
446
447 static void
448 compute_tag_properties (void)
449 {
450   referenced_var_iterator rvi;
451   tree tag;
452   bool changed = true;
453   VEC (tree, heap) *taglist = NULL;
454
455   FOR_EACH_REFERENCED_VAR (tag, rvi)
456     {
457       if (!MTAG_P (tag))
458         continue;
459       VEC_safe_push (tree, heap, taglist, tag);
460     }
461
462   /* We sort the taglist by DECL_UID, for two reasons.
463      1. To get a sequential ordering to make the bitmap accesses
464      faster.
465      2. Because of the way we compute aliases, it's more likely that
466      an earlier tag is included in a later tag, and this will reduce
467      the number of iterations.
468
469      If we had a real tag graph, we would just topo-order it and be
470      done with it.  */
471   qsort (VEC_address (tree, taglist),
472          VEC_length (tree, taglist),
473          sizeof (tree),
474          sort_tags_by_id);
475
476   /* Go through each tag not marked as global, and if it aliases
477      global vars, mark it global. 
478      
479      If the tag contains call clobbered vars, mark it call
480      clobbered.  
481
482      This loop iterates because tags may appear in the may-aliases
483      list of other tags when we group.  */
484
485   while (changed)
486     {
487       unsigned int k;
488
489       changed = false;      
490       for (k = 0; VEC_iterate (tree, taglist, k, tag); k++)
491         {
492           bitmap ma;
493           bitmap_iterator bi;
494           unsigned int i;
495           tree entry;
496           bool tagcc = is_call_clobbered (tag);
497           bool tagglobal = MTAG_GLOBAL (tag);
498           
499           if (tagcc && tagglobal)
500             continue;
501           
502           ma = may_aliases (tag);
503           if (!ma)
504             continue;
505
506           EXECUTE_IF_SET_IN_BITMAP (ma, 0, i, bi)
507             {
508               entry = referenced_var (i);
509               /* Call clobbered entries cause the tag to be marked
510                  call clobbered.  */
511               if (!tagcc && is_call_clobbered (entry))
512                 {
513                   mark_call_clobbered (tag, var_ann (entry)->escape_mask);
514                   tagcc = true;
515                   changed = true;
516                 }
517
518               /* Global vars cause the tag to be marked global.  */
519               if (!tagglobal && is_global_var (entry))
520                 {
521                   MTAG_GLOBAL (tag) = true;
522                   changed = true;
523                   tagglobal = true;
524                 }
525
526               /* Early exit once both global and cc are set, since the
527                  loop can't do any more than that.  */
528               if (tagcc && tagglobal)
529                 break;
530             }
531         }
532     }
533   VEC_free (tree, heap, taglist);
534 }
535
536 /* Set up the initial variable clobbers, call-uses and globalness.
537    When this function completes, only tags whose aliases need to be
538    clobbered will be set clobbered.  Tags clobbered because they   
539    contain call clobbered vars are handled in compute_tag_properties.  */
540
541 static void
542 set_initial_properties (struct alias_info *ai)
543 {
544   unsigned int i;
545   referenced_var_iterator rvi;
546   tree var;
547   tree ptr;
548   bool any_pt_anything = false;
549   enum escape_type pt_anything_mask = 0;
550
551   FOR_EACH_REFERENCED_VAR (var, rvi)
552     {
553       if (is_global_var (var))
554         {
555           if (!unmodifiable_var_p (var))
556             mark_call_clobbered (var, ESCAPE_IS_GLOBAL);
557         }
558       else if (TREE_CODE (var) == PARM_DECL
559                && gimple_default_def (cfun, var)
560                && POINTER_TYPE_P (TREE_TYPE (var)))
561         {
562           tree def = gimple_default_def (cfun, var);
563           get_ptr_info (def)->value_escapes_p = 1;
564           get_ptr_info (def)->escape_mask |= ESCAPE_IS_PARM;      
565         }
566     }
567
568   if (!clobber_what_escaped ())
569     {
570       any_pt_anything = true;
571       pt_anything_mask |= ESCAPE_TO_CALL;
572     }
573
574   compute_call_used_vars ();
575
576   for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
577     {
578       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
579       tree tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
580
581       /* A pointer that only escapes via a function return does not
582          add to the call clobber or call used solution.
583          To exclude ESCAPE_TO_PURE_CONST we would need to track
584          call used variables separately or compute those properly
585          in the operand scanner.  */
586       if (pi->value_escapes_p
587           && pi->escape_mask & ~ESCAPE_TO_RETURN)
588         {
589           /* If PTR escapes then its associated memory tags and
590              pointed-to variables are call-clobbered.  */
591           if (pi->name_mem_tag)
592             mark_call_clobbered (pi->name_mem_tag, pi->escape_mask);
593
594           if (tag)
595             mark_call_clobbered (tag, pi->escape_mask);
596         }
597
598       /* If the name tag is call clobbered, so is the symbol tag
599          associated with the base VAR_DECL.  */
600       if (pi->name_mem_tag
601           && tag
602           && is_call_clobbered (pi->name_mem_tag))
603         mark_call_clobbered (tag, pi->escape_mask);
604
605       /* Name tags and symbol tags that we don't know where they point
606          to, might point to global memory, and thus, are clobbered.
607
608          FIXME:  This is not quite right.  They should only be
609          clobbered if value_escapes_p is true, regardless of whether
610          they point to global memory or not.
611          So removing this code and fixing all the bugs would be nice.
612          It is the cause of a bunch of clobbering.  */
613       if ((pi->pt_global_mem || pi->pt_anything) 
614           && pi->memory_tag_needed && pi->name_mem_tag)
615         {
616           mark_call_clobbered (pi->name_mem_tag, ESCAPE_IS_GLOBAL);
617           MTAG_GLOBAL (pi->name_mem_tag) = true;
618         }
619       
620       if ((pi->pt_global_mem || pi->pt_anything) 
621           && pi->memory_tag_needed
622           && tag)
623         {
624           mark_call_clobbered (tag, ESCAPE_IS_GLOBAL);
625           MTAG_GLOBAL (tag) = true;
626         }
627     }
628
629   /* If a pt_anything pointer escaped we need to mark all addressable
630      variables call clobbered.  */
631   if (any_pt_anything)
632     {
633       bitmap_iterator bi;
634       unsigned int j;
635
636       EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, j, bi)
637         {
638           tree var = referenced_var (j);
639           if (!unmodifiable_var_p (var))
640             mark_call_clobbered (var, pt_anything_mask);
641         }
642     }
643 }
644
645 /* Compute which variables need to be marked call clobbered because
646    their tag is call clobbered, and which tags need to be marked
647    global because they contain global variables.  */
648
649 static void
650 compute_call_clobbered (struct alias_info *ai)
651 {
652   VEC (tree, heap) *worklist = NULL;
653   VEC (int,heap) *worklist2 = NULL;
654   bitmap on_worklist;
655
656   timevar_push (TV_CALL_CLOBBER);
657   on_worklist = BITMAP_ALLOC (NULL);
658     
659   set_initial_properties (ai);
660   init_transitive_clobber_worklist (&worklist, &worklist2, on_worklist);
661   while (VEC_length (tree, worklist) != 0)
662     {
663       tree curr = VEC_pop (tree, worklist);
664       int reason = VEC_pop (int, worklist2);
665
666       bitmap_clear_bit (on_worklist, DECL_UID (curr));
667       mark_call_clobbered (curr, reason);
668       mark_aliases_call_clobbered (curr, &worklist, &worklist2, on_worklist);
669     }
670   VEC_free (tree, heap, worklist);
671   VEC_free (int, heap, worklist2);
672   BITMAP_FREE (on_worklist);
673   compute_tag_properties ();
674   timevar_pop (TV_CALL_CLOBBER);
675 }
676
677
678 /* Dump memory partition information to FILE.  */
679
680 static void
681 dump_memory_partitions (FILE *file)
682 {
683   unsigned i, npart;
684   unsigned long nsyms;
685   tree mpt;
686
687   fprintf (file, "\nMemory partitions\n\n");
688   for (i = 0, npart = 0, nsyms = 0;
689        VEC_iterate (tree, gimple_ssa_operands (cfun)->mpt_table, i, mpt);
690        i++)
691     {
692       if (mpt)
693         {
694           bitmap syms = MPT_SYMBOLS (mpt);
695           unsigned long n = (syms) ? bitmap_count_bits (syms) : 0;
696
697           fprintf (file, "#%u: ", i);
698           print_generic_expr (file, mpt, 0);
699           fprintf (file, ": %lu elements: ", n);
700           dump_decl_set (file, syms);
701           npart++;
702           nsyms += n;
703         }
704     }
705
706   fprintf (file, "\n%u memory partitions holding %lu symbols\n", npart, nsyms);
707 }
708
709
710 /* Dump memory partition information to stderr.  */
711
712 void
713 debug_memory_partitions (void)
714 {
715   dump_memory_partitions (stderr);
716 }
717
718
719 /* Return true if memory partitioning is required given the memory
720    reference estimates in STATS.  */
721
722 static inline bool
723 need_to_partition_p (struct mem_ref_stats_d *stats)
724 {
725   long num_vops = stats->num_vuses + stats->num_vdefs;
726   long avg_vops = CEIL (num_vops, stats->num_mem_stmts);
727   return (num_vops > (long) MAX_ALIASED_VOPS
728           && avg_vops > (long) AVG_ALIASED_VOPS);
729 }
730
731
732 /* Count the actual number of virtual operators in CFUN.  Note that
733    this is only meaningful after virtual operands have been populated,
734    so it should be invoked at the end of compute_may_aliases.
735
736    The number of virtual operators are stored in *NUM_VDEFS_P and
737    *NUM_VUSES_P, the number of partitioned symbols in
738    *NUM_PARTITIONED_P and the number of unpartitioned symbols in
739    *NUM_UNPARTITIONED_P.
740
741    If any of these pointers is NULL the corresponding count is not
742    computed.  */
743
744 static void
745 count_mem_refs (long *num_vuses_p, long *num_vdefs_p,
746                 long *num_partitioned_p, long *num_unpartitioned_p)
747 {
748   gimple_stmt_iterator gsi;
749   basic_block bb;
750   long num_vdefs, num_vuses, num_partitioned, num_unpartitioned;
751   referenced_var_iterator rvi;
752   tree sym;
753
754   num_vuses = num_vdefs = num_partitioned = num_unpartitioned = 0;
755
756   if (num_vuses_p || num_vdefs_p)
757     FOR_EACH_BB (bb)
758       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
759         {
760           gimple stmt = gsi_stmt (gsi);
761           if (gimple_references_memory_p (stmt))
762             {
763               num_vuses += NUM_SSA_OPERANDS (stmt, SSA_OP_VUSE);
764               num_vdefs += NUM_SSA_OPERANDS (stmt, SSA_OP_VDEF);
765             }
766         }
767
768   if (num_partitioned_p || num_unpartitioned_p)
769     FOR_EACH_REFERENCED_VAR (sym, rvi)
770       {
771         if (is_gimple_reg (sym))
772           continue;
773
774         if (memory_partition (sym))
775           num_partitioned++;
776         else
777           num_unpartitioned++;
778       }
779
780   if (num_vdefs_p)
781     *num_vdefs_p = num_vdefs;
782
783   if (num_vuses_p)
784     *num_vuses_p = num_vuses;
785
786   if (num_partitioned_p)
787     *num_partitioned_p = num_partitioned;
788
789   if (num_unpartitioned_p)
790     *num_unpartitioned_p = num_unpartitioned;
791 }
792
793
794 /* The list is sorted by increasing partitioning score (PSCORE).
795    This score is computed such that symbols with high scores are
796    those that are least likely to be partitioned.  Given a symbol
797    MP->VAR, PSCORE(S) is the result of the following weighted sum
798
799    PSCORE(S) =   FW * 64 + FR * 32
800                + DW * 16 + DR *  8 
801                + IW *  4 + IR *  2
802                + NO_ALIAS
803
804    where
805
806    FW           Execution frequency of writes to S
807    FR           Execution frequency of reads from S
808    DW           Number of direct writes to S
809    DR           Number of direct reads from S
810    IW           Number of indirect writes to S
811    IR           Number of indirect reads from S
812    NO_ALIAS     State of the NO_ALIAS* flags
813
814    The basic idea here is that symbols that are frequently
815    written-to in hot paths of the code are the last to be considered
816    for partitioning.  */
817
818 static inline long
819 mem_sym_score (mem_sym_stats_t mp)
820 {
821   return mp->frequency_writes * 64 + mp->frequency_reads * 32
822          + mp->num_direct_writes * 16 + mp->num_direct_reads * 8
823          + mp->num_indirect_writes * 4 + mp->num_indirect_reads * 2
824          + var_ann (mp->var)->noalias_state;
825 }
826
827
828 /* Dump memory reference stats for function CFUN to FILE.  */
829
830 void
831 dump_mem_ref_stats (FILE *file)
832 {
833   long actual_num_vuses, actual_num_vdefs;
834   long num_partitioned, num_unpartitioned;
835   struct mem_ref_stats_d *stats;
836   
837   stats = gimple_mem_ref_stats (cfun);
838
839   count_mem_refs (&actual_num_vuses, &actual_num_vdefs, &num_partitioned,
840                   &num_unpartitioned);
841
842   fprintf (file, "\nMemory reference statistics for %s\n\n", 
843            lang_hooks.decl_printable_name (current_function_decl, 2));
844
845   fprintf (file, "Number of memory statements:     %ld\n",
846            stats->num_mem_stmts);
847   fprintf (file, "Number of call sites:            %ld\n",
848            stats->num_call_sites);
849   fprintf (file, "Number of pure/const call sites: %ld\n",
850            stats->num_pure_const_call_sites);
851   fprintf (file, "Number of asm sites:             %ld\n",
852            stats->num_asm_sites);
853   fprintf (file, "Estimated number of loads:       %ld (%ld/stmt)\n",
854            stats->num_vuses,
855            (stats->num_mem_stmts)
856            ? CEIL (stats->num_vuses, stats->num_mem_stmts)
857            : 0);
858   fprintf (file, "Actual number of loads:          %ld (%ld/stmt)\n",
859            actual_num_vuses, 
860            (stats->num_mem_stmts)
861            ? CEIL (actual_num_vuses, stats->num_mem_stmts)
862            : 0);
863
864   if (actual_num_vuses > stats->num_vuses + (stats->num_vuses / 25))
865     fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
866
867   fprintf (file, "Estimated number of stores:      %ld (%ld/stmt)\n",
868            stats->num_vdefs,
869            (stats->num_mem_stmts)
870            ? CEIL (stats->num_vdefs, stats->num_mem_stmts)
871            : 0);
872   fprintf (file, "Actual number of stores:         %ld (%ld/stmt)\n",
873            actual_num_vdefs, 
874            (stats->num_mem_stmts)
875            ? CEIL (actual_num_vdefs, stats->num_mem_stmts)
876            : 0);
877
878   if (actual_num_vdefs > stats->num_vdefs + (stats->num_vdefs / 25))
879     fprintf (file, "\t(warning: estimation is lower by more than 25%%)\n");
880
881   fprintf (file, "Partitioning thresholds:         MAX = %d   AVG = %d "
882            "(%sNEED TO PARTITION)\n", MAX_ALIASED_VOPS, AVG_ALIASED_VOPS,
883            stats->num_mem_stmts && need_to_partition_p (stats) ? "" : "NO ");
884   fprintf (file, "Number of partitioned symbols:   %ld\n", num_partitioned);
885   fprintf (file, "Number of unpartitioned symbols: %ld\n", num_unpartitioned);
886 }
887
888
889 /* Dump memory reference stats for function FN to stderr.  */
890
891 void
892 debug_mem_ref_stats (void)
893 {
894   dump_mem_ref_stats (stderr);
895 }
896
897
898 /* Dump memory reference stats for variable VAR to FILE.  */
899
900 static void
901 dump_mem_sym_stats (FILE *file, tree var)
902 {
903   mem_sym_stats_t stats = mem_sym_stats (cfun, var);
904
905   if (stats == NULL)
906     return;
907
908   fprintf (file, "read frequency: %6ld, write frequency: %6ld, "
909            "direct reads: %3ld, direct writes: %3ld, "
910            "indirect reads: %4ld, indirect writes: %4ld, symbol: ",
911            stats->frequency_reads, stats->frequency_writes,
912            stats->num_direct_reads, stats->num_direct_writes,
913            stats->num_indirect_reads, stats->num_indirect_writes);
914   print_generic_expr (file, stats->var, 0);
915   fprintf (file, ", tags: ");
916   dump_decl_set (file, stats->parent_tags);
917 }
918
919
920 /* Dump memory reference stats for variable VAR to stderr.  */
921
922 void
923 debug_mem_sym_stats (tree var)
924 {
925   dump_mem_sym_stats (stderr, var);
926 }
927
928 /* Dump memory reference stats for variable VAR to FILE.  For use
929    of tree-dfa.c:dump_variable.  */
930
931 void
932 dump_mem_sym_stats_for_var (FILE *file, tree var)
933 {
934   mem_sym_stats_t stats = mem_sym_stats (cfun, var);
935
936   if (stats == NULL)
937     return;
938
939   fprintf (file, ", score: %ld", mem_sym_score (stats));
940   fprintf (file, ", direct reads: %ld", stats->num_direct_reads);
941   fprintf (file, ", direct writes: %ld", stats->num_direct_writes);
942   fprintf (file, ", indirect reads: %ld", stats->num_indirect_reads);
943   fprintf (file, ", indirect writes: %ld", stats->num_indirect_writes);
944 }
945
946 /* Dump memory reference stats for all memory symbols to FILE.  */
947
948 static void
949 dump_all_mem_sym_stats (FILE *file)
950 {
951   referenced_var_iterator rvi;
952   tree sym;
953
954   FOR_EACH_REFERENCED_VAR (sym, rvi)
955     {
956       if (is_gimple_reg (sym))
957         continue;
958
959       dump_mem_sym_stats (file, sym);
960     }
961 }
962
963
964 /* Dump memory reference stats for all memory symbols to stderr.  */
965
966 void
967 debug_all_mem_sym_stats (void)
968 {
969   dump_all_mem_sym_stats (stderr);
970 }
971
972
973 /* Dump the MP_INFO array to FILE.  */
974
975 static void
976 dump_mp_info (FILE *file, VEC(mem_sym_stats_t,heap) *mp_info)
977 {
978   unsigned i;
979   mem_sym_stats_t mp_p;
980
981   for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
982     if (!mp_p->partitioned_p)
983       dump_mem_sym_stats (file, mp_p->var);
984 }
985
986
987 /* Dump the MP_INFO array to stderr.  */
988
989 void
990 debug_mp_info (VEC(mem_sym_stats_t,heap) *mp_info)
991 {
992   dump_mp_info (stderr, mp_info);
993 }
994
995
996 /* Update memory reference stats for symbol VAR in statement STMT.
997    NUM_DIRECT_READS and NUM_DIRECT_WRITES specify the number of times
998    that VAR is read/written in STMT (indirect reads/writes are not
999    recorded by this function, see compute_memory_partitions).  */
1000
1001 void
1002 update_mem_sym_stats_from_stmt (tree var, gimple stmt, long num_direct_reads,
1003                                 long num_direct_writes)
1004 {
1005   mem_sym_stats_t stats;
1006
1007   gcc_assert (num_direct_reads >= 0 && num_direct_writes >= 0);
1008
1009   stats = get_mem_sym_stats_for (var);
1010
1011   stats->num_direct_reads += num_direct_reads;
1012   stats->frequency_reads += ((long) gimple_bb (stmt)->frequency
1013                              * num_direct_reads);
1014
1015   stats->num_direct_writes += num_direct_writes;
1016   stats->frequency_writes += ((long) gimple_bb (stmt)->frequency
1017                               * num_direct_writes);
1018 }
1019
1020
1021 /* Given two MP_INFO entries MP1 and MP2, return -1 if MP1->VAR should
1022    be partitioned before MP2->VAR, 0 if they are the same or 1 if
1023    MP1->VAR should be partitioned after MP2->VAR.  */
1024
1025 static inline int
1026 compare_mp_info_entries (mem_sym_stats_t mp1, mem_sym_stats_t mp2)
1027 {
1028   long pscore1 = mem_sym_score (mp1);
1029   long pscore2 = mem_sym_score (mp2);
1030
1031   if (pscore1 < pscore2)
1032     return -1;
1033   else if (pscore1 > pscore2)
1034     return 1;
1035   else
1036     return DECL_UID (mp1->var) - DECL_UID (mp2->var);
1037 }
1038
1039
1040 /* Comparison routine for qsort.  The list is sorted by increasing
1041    partitioning score (PSCORE).  This score is computed such that
1042    symbols with high scores are those that are least likely to be
1043    partitioned.  */
1044
1045 static int
1046 mp_info_cmp (const void *p, const void *q)
1047 {
1048   mem_sym_stats_t e1 = *((const mem_sym_stats_t *) p);
1049   mem_sym_stats_t e2 = *((const mem_sym_stats_t *) q);
1050   return compare_mp_info_entries (e1, e2);
1051 }
1052
1053
1054 /* Sort the array of reference counts used to compute memory partitions.
1055    Elements are sorted in ascending order of execution frequency and 
1056    descending order of virtual operators needed.  */
1057
1058 static inline void
1059 sort_mp_info (VEC(mem_sym_stats_t,heap) *list)
1060 {
1061   unsigned num = VEC_length (mem_sym_stats_t, list);
1062
1063   if (num < 2)
1064     return;
1065
1066   if (num == 2)
1067     {
1068       if (compare_mp_info_entries (VEC_index (mem_sym_stats_t, list, 0),
1069                                    VEC_index (mem_sym_stats_t, list, 1)) > 0)
1070         {  
1071           /* Swap elements if they are in the wrong order.  */
1072           mem_sym_stats_t tmp = VEC_index (mem_sym_stats_t, list, 0);
1073           VEC_replace (mem_sym_stats_t, list, 0,
1074                        VEC_index (mem_sym_stats_t, list, 1));
1075           VEC_replace (mem_sym_stats_t, list, 1, tmp);
1076         }
1077
1078       return;
1079     }
1080
1081   /* There are 3 or more elements, call qsort.  */
1082   qsort (VEC_address (mem_sym_stats_t, list),
1083          VEC_length (mem_sym_stats_t, list), 
1084          sizeof (mem_sym_stats_t),
1085          mp_info_cmp);
1086 }
1087
1088
1089 /* Return the memory partition tag (MPT) associated with memory
1090    symbol SYM.  */
1091
1092 static tree
1093 get_mpt_for (tree sym)
1094 {
1095   tree mpt;
1096
1097   /* Don't create a new tag unnecessarily.  */
1098   mpt = memory_partition (sym);
1099   if (mpt == NULL_TREE)
1100     {
1101       mpt = create_tag_raw (MEMORY_PARTITION_TAG, TREE_TYPE (sym), "MPT");
1102       TREE_ADDRESSABLE (mpt) = 0;
1103       add_referenced_var (mpt);
1104       VEC_safe_push (tree, heap, gimple_ssa_operands (cfun)->mpt_table, mpt);
1105       gcc_assert (MPT_SYMBOLS (mpt) == NULL);
1106       set_memory_partition (sym, mpt);
1107     }
1108
1109   return mpt;
1110 }
1111
1112
1113 /* Add MP_P->VAR to a memory partition and return the partition.  */
1114
1115 static tree
1116 find_partition_for (mem_sym_stats_t mp_p)
1117 {
1118   unsigned i;
1119   VEC(tree,heap) *mpt_table;
1120   tree mpt;
1121
1122   mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1123   mpt = NULL_TREE;
1124
1125   /* Find an existing partition for MP_P->VAR.  */
1126   for (i = 0; VEC_iterate (tree, mpt_table, i, mpt); i++)
1127     {
1128       mem_sym_stats_t mpt_stats;
1129
1130       /* If MPT does not have any symbols yet, use it.  */
1131       if (MPT_SYMBOLS (mpt) == NULL)
1132         break;
1133
1134       /* Otherwise, see if MPT has common parent tags with MP_P->VAR,
1135          but avoid grouping clobbered variables with non-clobbered
1136          variables (otherwise, this tends to creates a single memory
1137          partition because other call-clobbered variables may have
1138          common parent tags with non-clobbered ones).  */
1139       mpt_stats = get_mem_sym_stats_for (mpt);
1140       if (mp_p->parent_tags
1141           && mpt_stats->parent_tags
1142           && is_call_clobbered (mpt) == is_call_clobbered (mp_p->var)
1143           && bitmap_intersect_p (mpt_stats->parent_tags, mp_p->parent_tags))
1144         break;
1145
1146       /* If no common parent tags are found, see if both MPT and
1147          MP_P->VAR are call-clobbered.  */
1148       if (is_call_clobbered (mpt) && is_call_clobbered (mp_p->var))
1149         break;
1150     }
1151
1152   if (mpt == NULL_TREE)
1153     mpt = get_mpt_for (mp_p->var);
1154   else
1155     set_memory_partition (mp_p->var, mpt);
1156
1157   mp_p->partitioned_p = true;
1158
1159   mark_sym_for_renaming (mp_p->var);
1160   mark_sym_for_renaming (mpt);
1161
1162   return mpt;
1163 }
1164
1165
1166 /* Rewrite the alias set for TAG to use the newly created partitions.
1167    If TAG is NULL, rewrite the set of call-clobbered variables.
1168    NEW_ALIASES is a scratch bitmap to build the new set of aliases for
1169    TAG.  */
1170
1171 static void
1172 rewrite_alias_set_for (tree tag, bitmap new_aliases)
1173 {
1174   bitmap_iterator bi;
1175   unsigned i;
1176   tree mpt, sym;
1177
1178   EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, i, bi)
1179     {
1180       sym = referenced_var (i);
1181       mpt = memory_partition (sym);
1182       if (mpt)
1183         bitmap_set_bit (new_aliases, DECL_UID (mpt));
1184       else
1185         bitmap_set_bit (new_aliases, DECL_UID (sym));
1186     }
1187
1188   /* Rebuild the may-alias array for TAG.  */
1189   bitmap_copy (MTAG_ALIASES (tag), new_aliases);
1190 }
1191
1192
1193 /* Determine how many virtual operands can be saved by partitioning
1194    MP_P->VAR into MPT.  When a symbol S is thrown inside a partition
1195    P, every virtual operand that used to reference S will now
1196    reference P.  Whether it reduces the number of virtual operands
1197    depends on:
1198
1199    1- Direct references to S are never saved.  Instead of the virtual
1200       operand to S, we will now have a virtual operand to P.
1201
1202    2- Indirect references to S are reduced only for those memory tags
1203       holding S that already had other symbols partitioned into P.
1204       For instance, if a memory tag T has the alias set { a b S c },
1205       the first time we partition S into P, the alias set will become
1206       { a b P c }, so no virtual operands will be saved. However, if
1207       we now partition symbol 'c' into P, then the alias set for T
1208       will become { a b P }, so we will be saving one virtual operand
1209       for every indirect reference to 'c'.
1210
1211    3- Is S is call-clobbered, we save as many virtual operands as
1212       call/asm sites exist in the code, but only if other
1213       call-clobbered symbols have been grouped into P.  The first
1214       call-clobbered symbol that we group does not produce any
1215       savings.
1216
1217    MEM_REF_STATS points to CFUN's memory reference information.  */
1218
1219 static void
1220 estimate_vop_reduction (struct mem_ref_stats_d *mem_ref_stats,
1221                         mem_sym_stats_t mp_p, tree mpt)
1222 {
1223   unsigned i;
1224   bitmap_iterator bi;
1225   mem_sym_stats_t mpt_stats;
1226
1227   /* We should only get symbols with indirect references here.  */
1228   gcc_assert (mp_p->num_indirect_reads > 0 || mp_p->num_indirect_writes > 0);
1229
1230   /* Note that the only statistics we keep for MPT is the set of
1231      parent tags to know which memory tags have had alias members
1232      partitioned, and the indicator has_call_clobbered_vars.
1233      Reference counts are not important for MPT.  */
1234   mpt_stats = get_mem_sym_stats_for (mpt);
1235
1236   /* Traverse all the parent tags for MP_P->VAR.  For every tag T, if
1237      partition P is already grouping aliases of T, then reduce the
1238      number of virtual operands by the number of direct references
1239      to T.  */
1240   if (mp_p->parent_tags)
1241     {
1242       if (mpt_stats->parent_tags == NULL)
1243         mpt_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1244
1245       EXECUTE_IF_SET_IN_BITMAP (mp_p->parent_tags, 0, i, bi)
1246         {
1247           if (bitmap_bit_p (mpt_stats->parent_tags, i))
1248             {
1249               /* Partition MPT is already partitioning symbols in the
1250                  alias set for TAG.  This means that we are now saving
1251                  1 virtual operand for every direct reference to TAG.  */
1252               tree tag = referenced_var (i);
1253               mem_sym_stats_t tag_stats = mem_sym_stats (cfun, tag);
1254               mem_ref_stats->num_vuses -= tag_stats->num_direct_reads;
1255               mem_ref_stats->num_vdefs -= tag_stats->num_direct_writes;
1256             }
1257           else
1258             {
1259               /* This is the first symbol in tag I's alias set that is
1260                  being grouped under MPT.  We will not save any
1261                  virtual operands this time, but record that MPT is
1262                  grouping a symbol from TAG's alias set so that the
1263                  next time we get the savings.  */
1264               bitmap_set_bit (mpt_stats->parent_tags, i);
1265             }
1266         }
1267     }
1268
1269   /* If MP_P->VAR is call-clobbered, and MPT is already grouping
1270      call-clobbered symbols, then we will save as many virtual
1271      operands as asm/call sites there are.  */
1272   if (is_call_clobbered (mp_p->var))
1273     {
1274       if (mpt_stats->has_call_clobbered_vars)
1275         mem_ref_stats->num_vdefs -= mem_ref_stats->num_call_sites
1276                                     + mem_ref_stats->num_asm_sites;
1277       else
1278         mpt_stats->has_call_clobbered_vars = true;
1279     }
1280 }
1281
1282
1283 /* Helper for compute_memory_partitions.  Transfer reference counts
1284    from pointers to their pointed-to sets.  Counters for pointers were
1285    computed by update_alias_info.  MEM_REF_STATS points to CFUN's
1286    memory reference information.  */
1287
1288 static void
1289 update_reference_counts (struct mem_ref_stats_d *mem_ref_stats)
1290 {
1291   unsigned i;
1292   bitmap_iterator bi;
1293   mem_sym_stats_t sym_stats;
1294
1295   for (i = 1; i < num_ssa_names; i++)
1296     {
1297       tree ptr;
1298       struct ptr_info_def *pi;
1299
1300       ptr = ssa_name (i);
1301       if (ptr
1302           && POINTER_TYPE_P (TREE_TYPE (ptr))
1303           && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1304           && pi->memory_tag_needed)
1305         {
1306           unsigned j;
1307           bitmap_iterator bj;
1308           tree tag;
1309           mem_sym_stats_t ptr_stats, tag_stats;
1310
1311           /* If PTR has flow-sensitive points-to information, use
1312              PTR's name tag, otherwise use the symbol tag associated
1313              with PTR's symbol.  */
1314           if (pi->name_mem_tag)
1315             tag = pi->name_mem_tag;
1316           else
1317             tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
1318
1319           ptr_stats = get_mem_sym_stats_for (ptr);
1320           tag_stats = get_mem_sym_stats_for (tag);
1321
1322           /* TAG has as many direct references as dereferences we
1323              found for its parent pointer.  */
1324           tag_stats->num_direct_reads += ptr_stats->num_direct_reads;
1325           tag_stats->num_direct_writes += ptr_stats->num_direct_writes;
1326
1327           /* All the dereferences of pointer PTR are considered direct
1328              references to PTR's memory tag (TAG).  In turn,
1329              references to TAG will become virtual operands for every
1330              symbol in TAG's alias set.  So, for every symbol ALIAS in
1331              TAG's alias set, add as many indirect references to ALIAS
1332              as direct references there are for TAG.  */
1333           if (MTAG_ALIASES (tag))
1334             EXECUTE_IF_SET_IN_BITMAP (MTAG_ALIASES (tag), 0, j, bj)
1335               {
1336                 tree alias = referenced_var (j);
1337                 sym_stats = get_mem_sym_stats_for (alias);
1338
1339                 /* All the direct references to TAG are indirect references
1340                    to ALIAS.  */
1341                 sym_stats->num_indirect_reads += ptr_stats->num_direct_reads;
1342                 sym_stats->num_indirect_writes += ptr_stats->num_direct_writes;
1343                 sym_stats->frequency_reads += ptr_stats->frequency_reads;
1344                 sym_stats->frequency_writes += ptr_stats->frequency_writes;
1345
1346                 /* Indicate that TAG is one of ALIAS's parent tags.  */
1347                 if (sym_stats->parent_tags == NULL)
1348                   sym_stats->parent_tags = BITMAP_ALLOC (&alias_bitmap_obstack);
1349                 bitmap_set_bit (sym_stats->parent_tags, DECL_UID (tag));
1350               }
1351         }
1352     }
1353
1354   /* Call-clobbered symbols are indirectly written at every
1355      call/asm site.  */
1356   EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1357     {
1358       tree sym = referenced_var (i);
1359       sym_stats = get_mem_sym_stats_for (sym);
1360       sym_stats->num_indirect_writes += mem_ref_stats->num_call_sites
1361                                         + mem_ref_stats->num_asm_sites;
1362     }
1363
1364   /* Addressable symbols are indirectly written at some ASM sites.
1365      Since only ASM sites that clobber memory actually affect
1366      addressable symbols, this is an over-estimation.  */
1367   EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, i, bi)
1368     {
1369       tree sym = referenced_var (i);
1370       sym_stats = get_mem_sym_stats_for (sym);
1371       sym_stats->num_indirect_writes += mem_ref_stats->num_asm_sites;
1372     }
1373 }
1374
1375
1376 /* Helper for compute_memory_partitions.  Add all memory symbols to
1377    *MP_INFO_P and compute the initial estimate for the total number of
1378    virtual operands needed.  MEM_REF_STATS points to CFUN's memory
1379    reference information.  On exit, *TAGS_P will contain the list of
1380    memory tags whose alias set need to be rewritten after
1381    partitioning.  */
1382
1383 static void
1384 build_mp_info (struct mem_ref_stats_d *mem_ref_stats,
1385                VEC(mem_sym_stats_t,heap) **mp_info_p,
1386                VEC(tree,heap) **tags_p)
1387 {
1388   tree var;
1389   referenced_var_iterator rvi;
1390
1391   FOR_EACH_REFERENCED_VAR (var, rvi)
1392     {
1393       mem_sym_stats_t sym_stats;
1394       tree old_mpt;
1395
1396       /* We are only interested in memory symbols other than MPTs.  */
1397       if (is_gimple_reg (var) || TREE_CODE (var) == MEMORY_PARTITION_TAG)
1398         continue;
1399
1400       /* Collect memory tags into the TAGS array so that we can
1401          rewrite their alias sets after partitioning.  */
1402       if (MTAG_P (var) && MTAG_ALIASES (var))
1403         VEC_safe_push (tree, heap, *tags_p, var);
1404
1405       /* Since we are going to re-compute partitions, any symbols that
1406          used to belong to a partition must be detached from it and
1407          marked for renaming.  */
1408       if ((old_mpt = memory_partition (var)) != NULL)
1409         {
1410           mark_sym_for_renaming (old_mpt);
1411           set_memory_partition (var, NULL_TREE);
1412           mark_sym_for_renaming (var);
1413         }
1414
1415       sym_stats = get_mem_sym_stats_for (var);
1416
1417       /* Add VAR's reference info to MP_INFO.  Note that the only
1418          symbols that make sense to partition are those that have
1419          indirect references.  If a symbol S is always directly
1420          referenced, partitioning it will not reduce the number of
1421          virtual operators.  The only symbols that are profitable to
1422          partition are those that belong to alias sets and/or are
1423          call-clobbered.  */
1424       if (sym_stats->num_indirect_reads > 0
1425           || sym_stats->num_indirect_writes > 0)
1426         VEC_safe_push (mem_sym_stats_t, heap, *mp_info_p, sym_stats);
1427
1428       /* Update the number of estimated VOPS.  Note that direct
1429          references to memory tags are always counted as indirect
1430          references to their alias set members, so if a memory tag has
1431          aliases, do not count its direct references to avoid double
1432          accounting.  */
1433       if (!MTAG_P (var) || !MTAG_ALIASES (var))
1434         {
1435           mem_ref_stats->num_vuses += sym_stats->num_direct_reads;
1436           mem_ref_stats->num_vdefs += sym_stats->num_direct_writes;
1437         }
1438
1439       mem_ref_stats->num_vuses += sym_stats->num_indirect_reads;
1440       mem_ref_stats->num_vdefs += sym_stats->num_indirect_writes;
1441     }
1442 }
1443
1444
1445 /* Compute memory partitions.  A memory partition (MPT) is an
1446    arbitrary grouping of memory symbols, such that references to one
1447    member of the group is considered a reference to all the members of
1448    the group.
1449    
1450    As opposed to alias sets in memory tags, the grouping into
1451    partitions is completely arbitrary and only done to reduce the
1452    number of virtual operands.  The only rule that needs to be
1453    observed when creating memory partitions is that given two memory
1454    partitions MPT.i and MPT.j, they must not contain symbols in
1455    common.
1456
1457    Memory partitions are used when putting the program into Memory-SSA
1458    form.  In particular, in Memory-SSA PHI nodes are not computed for
1459    individual memory symbols.  They are computed for memory
1460    partitions.  This reduces the amount of PHI nodes in the SSA graph
1461    at the expense of precision (i.e., it makes unrelated stores affect
1462    each other).
1463    
1464    However, it is possible to increase precision by changing this
1465    partitioning scheme.  For instance, if the partitioning scheme is
1466    such that get_mpt_for is the identity function (that is,
1467    get_mpt_for (s) = s), this will result in ultimate precision at the
1468    expense of huge SSA webs.
1469
1470    At the other extreme, a partitioning scheme that groups all the
1471    symbols in the same set results in minimal SSA webs and almost
1472    total loss of precision.
1473
1474    There partitioning heuristic uses three parameters to decide the
1475    order in which symbols are processed.  The list of symbols is
1476    sorted so that symbols that are more likely to be partitioned are
1477    near the top of the list:
1478
1479    - Execution frequency.  If a memory references is in a frequently
1480      executed code path, grouping it into a partition may block useful
1481      transformations and cause sub-optimal code generation.  So, the
1482      partition heuristic tries to avoid grouping symbols with high
1483      execution frequency scores.  Execution frequency is taken
1484      directly from the basic blocks where every reference is made (see
1485      update_mem_sym_stats_from_stmt), which in turn uses the
1486      profile guided machinery, so if the program is compiled with PGO
1487      enabled, more accurate partitioning decisions will be made.
1488
1489    - Number of references.  Symbols with few references in the code,
1490      are partitioned before symbols with many references.
1491
1492    - NO_ALIAS attributes.  Symbols with any of the NO_ALIAS*
1493      attributes are partitioned after symbols marked MAY_ALIAS.
1494
1495    Once the list is sorted, the partitioning proceeds as follows:
1496
1497    1- For every symbol S in MP_INFO, create a new memory partition MP,
1498       if necessary.  To avoid memory partitions that contain symbols
1499       from non-conflicting alias sets, memory partitions are
1500       associated to the memory tag that holds S in its alias set.  So,
1501       when looking for a memory partition for S, the memory partition
1502       associated with one of the memory tags holding S is chosen.  If
1503       none exists, a new one is created.
1504
1505    2- Add S to memory partition MP.
1506
1507    3- Reduce by 1 the number of VOPS for every memory tag holding S.
1508
1509    4- If the total number of VOPS is less than MAX_ALIASED_VOPS or the
1510       average number of VOPS per statement is less than
1511       AVG_ALIASED_VOPS, stop.  Otherwise, go to the next symbol in the
1512       list.  */
1513
1514 static void
1515 compute_memory_partitions (void)
1516 {
1517   tree tag;
1518   unsigned i;
1519   mem_sym_stats_t mp_p;
1520   VEC(mem_sym_stats_t,heap) *mp_info;
1521   bitmap new_aliases;
1522   VEC(tree,heap) *tags;
1523   struct mem_ref_stats_d *mem_ref_stats;
1524   int prev_max_aliased_vops;
1525
1526   mem_ref_stats = gimple_mem_ref_stats (cfun);
1527   gcc_assert (mem_ref_stats->num_vuses == 0 && mem_ref_stats->num_vdefs == 0);
1528
1529   if (mem_ref_stats->num_mem_stmts == 0)
1530     return;
1531
1532   timevar_push (TV_MEMORY_PARTITIONING);
1533
1534   mp_info = NULL;
1535   tags = NULL;
1536   prev_max_aliased_vops = MAX_ALIASED_VOPS;
1537
1538   /* Since we clearly cannot lower the number of virtual operators
1539      below the total number of memory statements in the function, we
1540      may need to adjust MAX_ALIASED_VOPS beforehand.  */
1541   if (MAX_ALIASED_VOPS < mem_ref_stats->num_mem_stmts)
1542     MAX_ALIASED_VOPS = mem_ref_stats->num_mem_stmts;
1543
1544   /* Update reference stats for all the pointed-to variables and
1545      memory tags.  */
1546   update_reference_counts (mem_ref_stats);
1547
1548   /* Add all the memory symbols to MP_INFO.  */
1549   build_mp_info (mem_ref_stats, &mp_info, &tags);
1550
1551   /* No partitions required if we are below the threshold.  */
1552   if (!need_to_partition_p (mem_ref_stats))
1553     {
1554       if (dump_file)
1555         fprintf (dump_file, "\nMemory partitioning NOT NEEDED for %s\n",
1556                  get_name (current_function_decl));
1557       goto done;
1558     }
1559
1560   /* Sort the MP_INFO array so that symbols that should be partitioned
1561      first are near the top of the list.  */
1562   sort_mp_info (mp_info);
1563
1564   if (dump_file)
1565     {
1566       fprintf (dump_file, "\nMemory partitioning NEEDED for %s\n\n",
1567                get_name (current_function_decl));
1568       fprintf (dump_file, "Memory symbol references before partitioning:\n");
1569       dump_mp_info (dump_file, mp_info);
1570     }
1571
1572   /* Create partitions for variables in MP_INFO until we have enough
1573      to lower the total number of VOPS below MAX_ALIASED_VOPS or if
1574      the average number of VOPS per statement is below
1575      AVG_ALIASED_VOPS.  */
1576   for (i = 0; VEC_iterate (mem_sym_stats_t, mp_info, i, mp_p); i++)
1577     {
1578       tree mpt;
1579
1580       /* If we are below the threshold, stop.  */
1581       if (!need_to_partition_p (mem_ref_stats))
1582         break;
1583
1584       mpt = find_partition_for (mp_p);
1585       estimate_vop_reduction (mem_ref_stats, mp_p, mpt);
1586     }
1587
1588   /* After partitions have been created, rewrite alias sets to use
1589      them instead of the original symbols.  This way, if the alias set
1590      was computed as { a b c d e f }, and the subset { b e f } was
1591      grouped into partition MPT.3, then the new alias set for the tag
1592      will be  { a c d MPT.3 }.
1593      
1594      Note that this is not strictly necessary.  The operand scanner
1595      will always check if a symbol belongs to a partition when adding
1596      virtual operands.  However, by reducing the size of the alias
1597      sets to be scanned, the work needed inside the operand scanner is
1598      significantly reduced.  */
1599   new_aliases = BITMAP_ALLOC (&alias_bitmap_obstack);
1600
1601   for (i = 0; VEC_iterate (tree, tags, i, tag); i++)
1602     {
1603       rewrite_alias_set_for (tag, new_aliases);
1604       bitmap_clear (new_aliases);
1605     }
1606
1607   BITMAP_FREE (new_aliases);
1608
1609   if (dump_file)
1610     {
1611       fprintf (dump_file, "\nMemory symbol references after partitioning:\n");
1612       dump_mp_info (dump_file, mp_info);
1613     }
1614
1615 done:
1616   /* Free allocated memory.  */
1617   VEC_free (mem_sym_stats_t, heap, mp_info);
1618   VEC_free (tree, heap, tags);
1619
1620   MAX_ALIASED_VOPS = prev_max_aliased_vops;
1621
1622   timevar_pop (TV_MEMORY_PARTITIONING);
1623 }
1624
1625 /* Compute may-alias information for every variable referenced in function
1626    FNDECL.
1627
1628    Alias analysis proceeds in 3 main phases:
1629
1630    1- Points-to and escape analysis.
1631
1632    This phase walks the use-def chains in the SSA web looking for three
1633    things:
1634
1635         * Assignments of the form P_i = &VAR
1636         * Assignments of the form P_i = malloc()
1637         * Pointers and ADDR_EXPR that escape the current function.
1638
1639    The concept of 'escaping' is the same one used in the Java world.  When
1640    a pointer or an ADDR_EXPR escapes, it means that it has been exposed
1641    outside of the current function.  So, assignment to global variables,
1642    function arguments and returning a pointer are all escape sites, as are
1643    conversions between pointers and integers.
1644
1645    This is where we are currently limited.  Since not everything is renamed
1646    into SSA, we lose track of escape properties when a pointer is stashed
1647    inside a field in a structure, for instance.  In those cases, we are
1648    assuming that the pointer does escape.
1649
1650    We use escape analysis to determine whether a variable is
1651    call-clobbered.  Simply put, if an ADDR_EXPR escapes, then the variable
1652    is call-clobbered.  If a pointer P_i escapes, then all the variables
1653    pointed-to by P_i (and its memory tag) also escape.
1654
1655    2- Compute flow-sensitive aliases
1656
1657    We have two classes of memory tags.  Memory tags associated with the
1658    pointed-to data type of the pointers in the program.  These tags are
1659    called "symbol memory tag" (SMT).  The other class are those associated
1660    with SSA_NAMEs, called "name memory tag" (NMT). The basic idea is that
1661    when adding operands for an INDIRECT_REF *P_i, we will first check
1662    whether P_i has a name tag, if it does we use it, because that will have
1663    more precise aliasing information.  Otherwise, we use the standard symbol
1664    tag.
1665
1666    In this phase, we go through all the pointers we found in points-to
1667    analysis and create alias sets for the name memory tags associated with
1668    each pointer P_i.  If P_i escapes, we mark call-clobbered the variables
1669    it points to and its tag.
1670
1671
1672    3- Compute flow-insensitive aliases
1673
1674    This pass will compare the alias set of every symbol memory tag and
1675    every addressable variable found in the program.  Given a symbol
1676    memory tag SMT and an addressable variable V.  If the alias sets of
1677    SMT and V conflict (as computed by may_alias_p), then V is marked
1678    as an alias tag and added to the alias set of SMT.
1679
1680    For instance, consider the following function:
1681
1682             foo (int i)
1683             {
1684               int *p, a, b;
1685             
1686               if (i > 10)
1687                 p = &a;
1688               else
1689                 p = &b;
1690             
1691               *p = 3;
1692               a = b + 2;
1693               return *p;
1694             }
1695
1696    After aliasing analysis has finished, the symbol memory tag for pointer
1697    'p' will have two aliases, namely variables 'a' and 'b'.  Every time
1698    pointer 'p' is dereferenced, we want to mark the operation as a
1699    potential reference to 'a' and 'b'.
1700
1701             foo (int i)
1702             {
1703               int *p, a, b;
1704
1705               if (i_2 > 10)
1706                 p_4 = &a;
1707               else
1708                 p_6 = &b;
1709               # p_1 = PHI <p_4(1), p_6(2)>;
1710
1711               # a_7 = VDEF <a_3>;
1712               # b_8 = VDEF <b_5>;
1713               *p_1 = 3;
1714
1715               # a_9 = VDEF <a_7>
1716               # VUSE <b_8>
1717               a_9 = b_8 + 2;
1718
1719               # VUSE <a_9>;
1720               # VUSE <b_8>;
1721               return *p_1;
1722             }
1723
1724    In certain cases, the list of may aliases for a pointer may grow too
1725    large.  This may cause an explosion in the number of virtual operands
1726    inserted in the code.  Resulting in increased memory consumption and
1727    compilation time.
1728
1729    When the number of virtual operands needed to represent aliased
1730    loads and stores grows too large (configurable with option --param
1731    max-aliased-vops and --param avg-aliased-vops), alias sets are
1732    grouped to avoid severe compile-time slow downs and memory
1733    consumption. See compute_memory_partitions.  */
1734
1735 unsigned int
1736 compute_may_aliases (void)
1737 {
1738   struct alias_info *ai;
1739
1740   timevar_push (TV_TREE_MAY_ALIAS);
1741   
1742   memset (&alias_stats, 0, sizeof (alias_stats));
1743
1744   /* Initialize aliasing information.  */
1745   ai = init_alias_info ();
1746
1747   /* For each pointer P_i, determine the sets of variables that P_i may
1748      point-to.  For every addressable variable V, determine whether the
1749      address of V escapes the current function, making V call-clobbered
1750      (i.e., whether &V is stored in a global variable or if its passed as a
1751      function call argument).  */
1752   compute_points_to_sets ();
1753
1754   /* Update various related attributes like escaped addresses,
1755      pointer dereferences for loads and stores.  This is used
1756      when creating name tags and alias sets.  */
1757   update_alias_info (ai);
1758
1759   /* Collect all pointers and addressable variables, compute alias sets,
1760      create memory tags for pointers and promote variables whose address is
1761      not needed anymore.  */
1762   setup_pointers_and_addressables (ai);
1763
1764   /* Compute type-based flow-insensitive aliasing for all the type
1765      memory tags.  */
1766   compute_flow_insensitive_aliasing (ai);
1767
1768   /* Compute flow-sensitive, points-to based aliasing for all the name
1769      memory tags.  */
1770   compute_flow_sensitive_aliasing (ai);
1771   
1772   /* Compute call clobbering information.  */
1773   compute_call_clobbered (ai);
1774
1775   /* If the program makes no reference to global variables, but it
1776      contains a mixture of pure and non-pure functions, then we need
1777      to create use-def and def-def links between these functions to
1778      avoid invalid transformations on them.  */
1779   maybe_create_global_var ();
1780
1781   /* Compute memory partitions for every memory variable.  */
1782   compute_memory_partitions ();
1783
1784   /* Remove partitions with no symbols.  Partitions may end up with an
1785      empty MPT_SYMBOLS set if a previous round of alias analysis
1786      needed to partition more symbols.  Since we don't need those
1787      partitions anymore, remove them to free up the space.  */
1788   {
1789     tree mpt;
1790     unsigned i;
1791     VEC(tree,heap) *mpt_table;
1792
1793     mpt_table = gimple_ssa_operands (cfun)->mpt_table;
1794     i = 0;
1795     while (i < VEC_length (tree, mpt_table))
1796       {
1797         mpt = VEC_index (tree, mpt_table, i);
1798         if (MPT_SYMBOLS (mpt) == NULL)
1799           VEC_unordered_remove (tree, mpt_table, i);
1800         else
1801           i++;
1802       }
1803   }
1804
1805   /* Populate all virtual operands and newly promoted register operands.  */
1806   {
1807     gimple_stmt_iterator gsi;
1808     basic_block bb;
1809     FOR_EACH_BB (bb)
1810       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1811         update_stmt_if_modified (gsi_stmt (gsi));
1812   }
1813
1814   /* Debugging dumps.  */
1815   if (dump_file)
1816     {
1817       dump_mem_ref_stats (dump_file);
1818       dump_alias_info (dump_file);
1819       dump_points_to_info (dump_file);
1820
1821       if (dump_flags & TDF_STATS)
1822         dump_alias_stats (dump_file);
1823
1824       if (dump_flags & TDF_DETAILS)
1825         dump_referenced_vars (dump_file);
1826     }
1827
1828   /* Deallocate memory used by aliasing data structures.  */
1829   delete_alias_info (ai);
1830
1831   if (need_ssa_update_p ())
1832     update_ssa (TODO_update_ssa);
1833
1834   timevar_pop (TV_TREE_MAY_ALIAS);
1835   
1836   return 0;
1837 }
1838
1839 /* Data structure used to count the number of dereferences to PTR
1840    inside an expression.  */
1841 struct count_ptr_d
1842 {
1843   tree ptr;
1844   unsigned num_stores;
1845   unsigned num_loads;
1846 };
1847
1848
1849 /* Helper for count_uses_and_derefs.  Called by walk_tree to look for
1850    (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA.  */
1851
1852 static tree
1853 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
1854 {
1855   struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
1856   struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
1857
1858   /* Do not walk inside ADDR_EXPR nodes.  In the expression &ptr->fld,
1859      pointer 'ptr' is *not* dereferenced, it is simply used to compute
1860      the address of 'fld' as 'ptr + offsetof(fld)'.  */
1861   if (TREE_CODE (*tp) == ADDR_EXPR)
1862     {
1863       *walk_subtrees = 0;
1864       return NULL_TREE;
1865     }
1866
1867   if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
1868     {
1869       if (wi_p->is_lhs)
1870         count_p->num_stores++;
1871       else
1872         count_p->num_loads++;
1873     }
1874
1875   return NULL_TREE;
1876 }
1877
1878
1879 /* Count the number of direct and indirect uses for pointer PTR in
1880    statement STMT.  The number of direct uses is stored in
1881    *NUM_USES_P.  Indirect references are counted separately depending
1882    on whether they are store or load operations.  The counts are
1883    stored in *NUM_STORES_P and *NUM_LOADS_P.  */
1884
1885 void
1886 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
1887                        unsigned *num_loads_p, unsigned *num_stores_p)
1888 {
1889   ssa_op_iter i;
1890   tree use;
1891
1892   *num_uses_p = 0;
1893   *num_loads_p = 0;
1894   *num_stores_p = 0;
1895
1896   /* Find out the total number of uses of PTR in STMT.  */
1897   FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
1898     if (use == ptr)
1899       (*num_uses_p)++;
1900
1901   /* Now count the number of indirect references to PTR.  This is
1902      truly awful, but we don't have much choice.  There are no parent
1903      pointers inside INDIRECT_REFs, so an expression like
1904      '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
1905      find all the indirect and direct uses of x_1 inside.  The only
1906      shortcut we can take is the fact that GIMPLE only allows
1907      INDIRECT_REFs inside the expressions below.  */
1908   if (is_gimple_assign (stmt)
1909       || gimple_code (stmt) == GIMPLE_RETURN
1910       || gimple_code (stmt) == GIMPLE_ASM
1911       || is_gimple_call (stmt))
1912     {
1913       struct walk_stmt_info wi;
1914       struct count_ptr_d count;
1915
1916       count.ptr = ptr;
1917       count.num_stores = 0;
1918       count.num_loads = 0;
1919
1920       memset (&wi, 0, sizeof (wi));
1921       wi.info = &count;
1922       walk_gimple_op (stmt, count_ptr_derefs, &wi);
1923
1924       *num_stores_p = count.num_stores;
1925       *num_loads_p = count.num_loads;
1926     }
1927
1928   gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
1929 }
1930
1931 /* Remove memory references stats for function FN.  */
1932
1933 void
1934 delete_mem_ref_stats (struct function *fn)
1935 {
1936   if (gimple_mem_ref_stats (fn)->mem_sym_stats)
1937     {
1938       free_alloc_pool (mem_sym_stats_pool);
1939       pointer_map_destroy (gimple_mem_ref_stats (fn)->mem_sym_stats);
1940     }
1941   gimple_mem_ref_stats (fn)->mem_sym_stats = NULL;
1942 }
1943
1944
1945 /* Initialize memory reference stats.  */
1946
1947 static void
1948 init_mem_ref_stats (void)
1949 {
1950   struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
1951
1952   mem_sym_stats_pool = create_alloc_pool ("Mem sym stats",
1953                                           sizeof (struct mem_sym_stats_d),
1954                                           100);
1955   memset (mem_ref_stats, 0, sizeof (struct mem_ref_stats_d));
1956   mem_ref_stats->mem_sym_stats = pointer_map_create ();
1957 }
1958
1959
1960 /* Helper for init_alias_info.  Reset existing aliasing information.  */
1961
1962 static void
1963 reset_alias_info (void)
1964 {
1965   referenced_var_iterator rvi;
1966   tree var;
1967   unsigned i;
1968   bitmap active_nmts, all_nmts;
1969
1970   /* Clear the set of addressable variables.  We do not need to clear
1971      the TREE_ADDRESSABLE bit on every symbol because we are going to
1972      re-compute addressability here.  */
1973   bitmap_clear (gimple_addressable_vars (cfun));
1974
1975   active_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
1976   all_nmts = BITMAP_ALLOC (&alias_bitmap_obstack);
1977
1978   /* Clear flow-insensitive alias information from each symbol.  */
1979   FOR_EACH_REFERENCED_VAR (var, rvi)
1980     {
1981       if (is_gimple_reg (var))
1982         continue;
1983
1984       if (MTAG_P (var))
1985         MTAG_ALIASES (var) = NULL;
1986
1987       /* Memory partition information will be computed from scratch.  */
1988       if (TREE_CODE (var) == MEMORY_PARTITION_TAG)
1989         MPT_SYMBOLS (var) = NULL;
1990
1991       /* Collect all the name tags to determine if we have any
1992          orphaned that need to be removed from the IL.  A name tag
1993          will be orphaned if it is not associated with any active SSA
1994          name.  */
1995       if (TREE_CODE (var) == NAME_MEMORY_TAG)
1996         bitmap_set_bit (all_nmts, DECL_UID (var));
1997
1998       /* Since we are about to re-discover call-clobbered
1999          variables, clear the call-clobbered flag.  */
2000       clear_call_clobbered (var);
2001     }
2002
2003   /* There should be no call-clobbered variable left.  */
2004   gcc_assert (bitmap_empty_p (gimple_call_clobbered_vars (cfun)));
2005
2006   /* Clear the call-used variables.  */
2007   bitmap_clear (gimple_call_used_vars (cfun));
2008
2009   /* Clear flow-sensitive points-to information from each SSA name.  */
2010   for (i = 1; i < num_ssa_names; i++)
2011     {
2012       tree name = ssa_name (i);
2013
2014       if (!name || !POINTER_TYPE_P (TREE_TYPE (name)))
2015         continue;
2016
2017       if (SSA_NAME_PTR_INFO (name))
2018         {
2019           struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
2020
2021           /* Clear all the flags but keep the name tag to
2022              avoid creating new temporaries unnecessarily.  If
2023              this pointer is found to point to a subset or
2024              superset of its former points-to set, then a new
2025              tag will need to be created in create_name_tags.  */
2026           pi->pt_anything = 0;
2027           pi->pt_null = 0;
2028           pi->value_escapes_p = 0;
2029           pi->memory_tag_needed = 0;
2030           pi->is_dereferenced = 0;
2031           if (pi->pt_vars)
2032             bitmap_clear (pi->pt_vars);
2033
2034           /* Add NAME's name tag to the set of active tags.  */
2035           if (pi->name_mem_tag)
2036             bitmap_set_bit (active_nmts, DECL_UID (pi->name_mem_tag));
2037         }
2038     }
2039
2040   /* Name memory tags that are no longer associated with an SSA name
2041      are considered stale and should be removed from the IL.  All the
2042      name tags that are in the set ALL_NMTS but not in ACTIVE_NMTS are
2043      considered stale and marked for renaming.  */
2044   bitmap_and_compl_into (all_nmts, active_nmts);
2045   mark_set_for_renaming (all_nmts);
2046
2047   BITMAP_FREE (all_nmts);
2048   BITMAP_FREE (active_nmts);
2049 }
2050
2051
2052 /* Initialize the data structures used for alias analysis.  */
2053
2054 static struct alias_info *
2055 init_alias_info (void)
2056 {
2057   struct alias_info *ai;
2058   referenced_var_iterator rvi;
2059   tree var;
2060   static bool alias_bitmap_obstack_initialized;
2061
2062   ai = XCNEW (struct alias_info);
2063   ai->ssa_names_visited = sbitmap_alloc (num_ssa_names);
2064   sbitmap_zero (ai->ssa_names_visited);
2065   ai->processed_ptrs = VEC_alloc (tree, heap, 50);
2066   ai->dereferenced_ptrs = pointer_set_create ();
2067
2068   /* Clear out all memory reference stats.  */
2069   init_mem_ref_stats ();
2070
2071   /* If aliases have been computed before, clear existing information.  */
2072   if (gimple_aliases_computed_p (cfun))
2073     reset_alias_info ();
2074   else
2075     {
2076       /* If this is the first time we compute aliasing information,
2077          every non-register symbol will need to be put into SSA form
2078          (the initial SSA form only operates on GIMPLE registers).  */
2079       FOR_EACH_REFERENCED_VAR (var, rvi)
2080         if (!is_gimple_reg (var))
2081           mark_sym_for_renaming (var);
2082     }
2083
2084   /* Next time, we will need to reset alias information.  */
2085   cfun->gimple_df->aliases_computed_p = true;
2086   if (alias_bitmap_obstack_initialized)
2087     bitmap_obstack_release (&alias_bitmap_obstack);    
2088   bitmap_obstack_initialize (&alias_bitmap_obstack);
2089   alias_bitmap_obstack_initialized = true;
2090
2091   return ai;
2092 }
2093
2094
2095 /* Deallocate memory used by alias analysis.  */
2096
2097 static void
2098 delete_alias_info (struct alias_info *ai)
2099 {
2100   size_t i;
2101
2102   sbitmap_free (ai->ssa_names_visited);
2103
2104   VEC_free (tree, heap, ai->processed_ptrs);
2105
2106   for (i = 0; i < ai->num_addressable_vars; i++)
2107     free (ai->addressable_vars[i]);
2108   free (ai->addressable_vars);
2109   
2110   for (i = 0; i < ai->num_pointers; i++)
2111     free (ai->pointers[i]);
2112   free (ai->pointers);
2113
2114   pointer_set_destroy (ai->dereferenced_ptrs);
2115   free (ai);
2116
2117   delete_mem_ref_stats (cfun);
2118   delete_points_to_sets ();
2119 }
2120
2121
2122 /* Used for hashing to identify pointer infos with identical
2123    pt_vars bitmaps.  */
2124
2125 static int
2126 eq_ptr_info (const void *p1, const void *p2)
2127 {
2128   const struct ptr_info_def *n1 = (const struct ptr_info_def *) p1;
2129   const struct ptr_info_def *n2 = (const struct ptr_info_def *) p2;
2130   return bitmap_equal_p (n1->pt_vars, n2->pt_vars);
2131 }
2132
2133 static hashval_t
2134 ptr_info_hash (const void *p)
2135 {
2136   const struct ptr_info_def *n = (const struct ptr_info_def *) p;
2137   return bitmap_hash (n->pt_vars);
2138 }
2139
2140
2141 /* Create name tags for all the pointers that have been dereferenced.
2142    We only create a name tag for a pointer P if P is found to point to
2143    a set of variables (so that we can alias them to *P) or if it is
2144    the result of a call to malloc (which means that P cannot point to
2145    anything else nor alias any other variable).
2146
2147    If two pointers P and Q point to the same set of variables, they
2148    are assigned the same name tag.  */
2149
2150 static void
2151 create_name_tags (void)
2152 {
2153   size_t i;
2154   VEC (tree, heap) *with_ptvars = NULL;
2155   tree ptr;
2156   htab_t ptr_hash;
2157
2158   /* Collect the list of pointers with a non-empty points to set.  */
2159   for (i = 1; i < num_ssa_names; i++)
2160     {
2161       tree ptr = ssa_name (i);
2162       struct ptr_info_def *pi;
2163
2164       if (!ptr
2165           || !POINTER_TYPE_P (TREE_TYPE (ptr))
2166           || !SSA_NAME_PTR_INFO (ptr))
2167         continue;
2168
2169       pi = SSA_NAME_PTR_INFO (ptr);
2170
2171       if (pi->pt_anything || !pi->memory_tag_needed)
2172         {
2173           /* No name tags for pointers that have not been
2174              dereferenced or point to an arbitrary location.  */
2175           pi->name_mem_tag = NULL_TREE;
2176           continue;
2177         }
2178
2179       /* Set pt_anything on the pointers without pt_vars filled in so
2180          that they are assigned a symbol tag.  */
2181       if (pi->pt_vars && !bitmap_empty_p (pi->pt_vars)) 
2182         VEC_safe_push (tree, heap, with_ptvars, ptr);
2183       else
2184         set_pt_anything (ptr);
2185     }
2186   
2187   /* If we didn't find any pointers with pt_vars set, we're done.  */
2188   if (!with_ptvars)
2189     return;
2190
2191   ptr_hash = htab_create (10, ptr_info_hash, eq_ptr_info, NULL);
2192
2193   /* Now go through the pointers with pt_vars, and find a name tag
2194      with the same pt_vars as this pointer, or create one if one
2195      doesn't exist.  */
2196   for (i = 0; VEC_iterate (tree, with_ptvars, i, ptr); i++)
2197     {
2198       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2199       tree old_name_tag = pi->name_mem_tag;
2200       struct ptr_info_def **slot;
2201       
2202       /* If PTR points to a set of variables, check if we don't
2203          have another pointer Q with the same points-to set before
2204          creating a tag.  If so, use Q's tag instead of creating a
2205          new one.
2206          
2207          This is important for not creating unnecessary symbols
2208          and also for copy propagation.  If we ever need to
2209          propagate PTR into Q or vice-versa, we would run into
2210          problems if they both had different name tags because
2211          they would have different SSA version numbers (which
2212          would force us to take the name tags in and out of SSA).  */
2213       slot = (struct ptr_info_def **) htab_find_slot (ptr_hash, pi, INSERT);
2214       if (*slot)
2215         pi->name_mem_tag = (*slot)->name_mem_tag;
2216       else
2217         {
2218           *slot = pi;
2219
2220           /* If we didn't find a pointer with the same points-to set
2221              as PTR, create a new name tag if needed.  */
2222           if (pi->name_mem_tag == NULL_TREE)
2223             pi->name_mem_tag = get_nmt_for (ptr);
2224         }
2225       
2226       /* If the new name tag computed for PTR is different than
2227          the old name tag that it used to have, then the old tag
2228          needs to be removed from the IL, so we mark it for
2229          renaming.  */
2230       if (old_name_tag && old_name_tag != pi->name_mem_tag)
2231         mark_sym_for_renaming (old_name_tag);
2232
2233       /* Inherit volatility from the pointed-to type.  */
2234       TREE_THIS_VOLATILE (pi->name_mem_tag)
2235         |= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (ptr)));
2236       
2237       /* Mark the new name tag for renaming.  */
2238       mark_sym_for_renaming (pi->name_mem_tag);
2239     }
2240
2241   htab_delete (ptr_hash);
2242
2243   VEC_free (tree, heap, with_ptvars);
2244 }
2245
2246
2247 /* Union the alias set SET into the may-aliases for TAG.  */
2248
2249 static void
2250 union_alias_set_into (tree tag, bitmap set)
2251 {
2252   bitmap ma = MTAG_ALIASES (tag);
2253   
2254   if (bitmap_empty_p (set))
2255     return;
2256   
2257   if (!ma)
2258     ma = MTAG_ALIASES (tag) = BITMAP_ALLOC (&alias_bitmap_obstack);
2259   bitmap_ior_into (ma, set);
2260 }
2261
2262
2263 /* For every pointer P_i in AI->PROCESSED_PTRS, create may-alias sets for
2264    the name memory tag (NMT) associated with P_i.  If P_i escapes, then its
2265    name tag and the variables it points-to are call-clobbered.  Finally, if
2266    P_i escapes and we could not determine where it points to, then all the
2267    variables in the same alias set as *P_i are marked call-clobbered.  This
2268    is necessary because we must assume that P_i may take the address of any
2269    variable in the same alias set.  */
2270
2271 static void
2272 compute_flow_sensitive_aliasing (struct alias_info *ai)
2273 {
2274   size_t i;
2275   tree ptr;
2276   
2277   timevar_push (TV_FLOW_SENSITIVE);
2278   
2279   for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2280     {
2281       if (!find_what_p_points_to (ptr))
2282         set_pt_anything (ptr);
2283     }
2284
2285   create_name_tags ();
2286
2287   for (i = 0; VEC_iterate (tree, ai->processed_ptrs, i, ptr); i++)
2288     {
2289       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2290
2291       /* Set up aliasing information for PTR's name memory tag (if it has
2292          one).  Note that only pointers that have been dereferenced will
2293          have a name memory tag.  */
2294       if (pi->name_mem_tag && pi->pt_vars)
2295         {
2296           if (!bitmap_empty_p (pi->pt_vars))
2297             union_alias_set_into (pi->name_mem_tag, pi->pt_vars);
2298         }
2299     }
2300   timevar_pop (TV_FLOW_SENSITIVE);
2301 }
2302
2303
2304 /* Return TRUE if at least one symbol in TAG2's alias set is also
2305    present in TAG1's alias set.  */
2306
2307 static bool
2308 have_common_aliases_p (bitmap tag1aliases, bitmap tag2aliases)
2309 {
2310
2311   /* This is the old behavior of have_common_aliases_p, which is to
2312      return false if both sets are empty, or one set is and the other
2313      isn't.  */
2314   if (tag1aliases == NULL || tag2aliases == NULL)
2315     return false;
2316
2317   return bitmap_intersect_p (tag1aliases, tag2aliases);
2318 }
2319
2320 /* Compute type-based alias sets.  Traverse all the pointers and
2321    addressable variables found in setup_pointers_and_addressables.
2322    
2323    For every pointer P in AI->POINTERS and addressable variable V in
2324    AI->ADDRESSABLE_VARS, add V to the may-alias sets of P's symbol
2325    memory tag (SMT) if their alias sets conflict.  V is then marked as
2326    an aliased symbol so that the operand scanner knows that statements
2327    containing V have aliased operands.  */
2328
2329 static void
2330 compute_flow_insensitive_aliasing (struct alias_info *ai)
2331 {
2332   referenced_var_iterator rvi;
2333   tree var;
2334   size_t i;
2335
2336   timevar_push (TV_FLOW_INSENSITIVE);
2337   /* For every pointer P, determine which addressable variables may alias
2338      with P's symbol memory tag.  */
2339   for (i = 0; i < ai->num_pointers; i++)
2340     {
2341       size_t j;
2342       struct alias_map_d *p_map = ai->pointers[i];
2343       tree tag = symbol_mem_tag (p_map->var);
2344       tree var;
2345
2346       for (j = 0; j < ai->num_addressable_vars; j++)
2347         {
2348           struct alias_map_d *v_map;
2349           var_ann_t v_ann;
2350           
2351           v_map = ai->addressable_vars[j];
2352           var = v_map->var;
2353           v_ann = var_ann (var);
2354
2355           /* We used to skip variables that have never been written to
2356              if the memory tag has been never written to directly (or
2357              either of them were call clobbered).  This is not enough
2358              though, as this misses writes through the tags aliases.
2359              So, for correctness we need to include any aliased
2360              variable here.  */
2361
2362           if (may_alias_p (p_map->var, p_map->set, var, v_map->set, false))
2363             {
2364               /* Add VAR to TAG's may-aliases set.  */
2365               add_may_alias (tag, var);
2366             }
2367         }
2368     }
2369
2370   /* Since this analysis is based exclusively on symbols, it fails to
2371      handle cases where two pointers P and Q have different memory
2372      tags with conflicting alias set numbers but no aliased symbols in
2373      common.
2374
2375      For example, suppose that we have two memory tags SMT.1 and SMT.2
2376      such that
2377      
2378                 may-aliases (SMT.1) = { a }
2379                 may-aliases (SMT.2) = { b }
2380
2381      and the alias set number of SMT.1 conflicts with that of SMT.2.
2382      Since they don't have symbols in common, loads and stores from
2383      SMT.1 and SMT.2 will seem independent of each other, which will
2384      lead to the optimizers making invalid transformations (see
2385      testsuite/gcc.c-torture/execute/pr15262-[12].c).
2386
2387      To avoid this problem, we do a final traversal of AI->POINTERS
2388      looking for pairs of pointers that have no aliased symbols in
2389      common and yet have conflicting alias set numbers.  */
2390   for (i = 0; i < ai->num_pointers; i++)
2391     {
2392       size_t j;
2393       struct alias_map_d *p_map1 = ai->pointers[i];
2394       tree tag1 = symbol_mem_tag (p_map1->var);
2395       bitmap may_aliases1 = MTAG_ALIASES (tag1);
2396
2397       for (j = 0; j < ai->num_pointers; j++)
2398         {
2399           struct alias_map_d *p_map2 = ai->pointers[j];
2400           tree tag2 = symbol_mem_tag (p_map2->var);
2401           bitmap may_aliases2 = may_aliases (tag2);
2402
2403           /* By convention tags don't alias themselves.  */
2404           if (tag1 == tag2)
2405             continue;
2406
2407           /* If the pointers may not point to each other, do nothing.  */
2408           if (!may_alias_p (p_map1->var, p_map1->set, tag2, p_map2->set, true))
2409             continue;
2410
2411           /* The two pointers may alias each other.  If they already have
2412              symbols in common, do nothing.  */
2413           if (have_common_aliases_p (may_aliases1, may_aliases2))
2414             continue;
2415
2416           add_may_alias (tag1, tag2);
2417         }
2418     }
2419
2420   /* We have to add all HEAP variables to all SMTs aliases bitmaps.
2421      As we don't know which effective type the HEAP will have we cannot
2422      do better here and we need the conflicts with obfuscated pointers
2423      (a simple (*(int[n] *)ptr)[i] will do, with ptr from a VLA array
2424      allocation).  */
2425   for (i = 0; i < ai->num_pointers; i++)
2426     {
2427       struct alias_map_d *p_map = ai->pointers[i];
2428       tree tag = symbol_mem_tag (p_map->var);
2429
2430       FOR_EACH_REFERENCED_VAR (var, rvi)
2431         {
2432           if (var_ann (var)->is_heapvar)
2433             add_may_alias (tag, var);
2434         }
2435     }
2436
2437   timevar_pop (TV_FLOW_INSENSITIVE);
2438 }
2439
2440
2441 /* Create a new alias set entry for VAR in AI->ADDRESSABLE_VARS.  */
2442
2443 static void
2444 create_alias_map_for (tree var, struct alias_info *ai)
2445 {
2446   struct alias_map_d *alias_map;
2447   alias_map = XCNEW (struct alias_map_d);
2448   alias_map->var = var;
2449   alias_map->set = get_alias_set (var);
2450   ai->addressable_vars[ai->num_addressable_vars++] = alias_map;
2451 }
2452
2453
2454 /* Update related alias information kept in AI.  This is used when
2455    building name tags, alias sets and deciding grouping heuristics.
2456    STMT is the statement to process.  This function also updates
2457    ADDRESSABLE_VARS.  */
2458
2459 static void
2460 update_alias_info_1 (gimple stmt, struct alias_info *ai)
2461 {
2462   bitmap addr_taken;
2463   use_operand_p use_p;
2464   ssa_op_iter iter;
2465   bool stmt_dereferences_ptr_p;
2466   enum escape_type stmt_escape_type = is_escape_site (stmt);
2467   struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
2468
2469   stmt_dereferences_ptr_p = false;
2470
2471   if (stmt_escape_type == ESCAPE_TO_CALL
2472       || stmt_escape_type == ESCAPE_TO_PURE_CONST)
2473     {
2474       mem_ref_stats->num_call_sites++;
2475       if (stmt_escape_type == ESCAPE_TO_PURE_CONST)
2476         mem_ref_stats->num_pure_const_call_sites++;
2477     }
2478   else if (stmt_escape_type == ESCAPE_TO_ASM)
2479     mem_ref_stats->num_asm_sites++;
2480
2481   /* Mark all the variables whose address are taken by the statement.  */
2482   addr_taken = gimple_addresses_taken (stmt);
2483   if (addr_taken)
2484     bitmap_ior_into (gimple_addressable_vars (cfun), addr_taken);
2485
2486   /* If we have a call or an assignment, see if the lhs contains
2487      a local decl that requires not to be a gimple register.  */
2488   if (gimple_code (stmt) == GIMPLE_ASSIGN
2489       || gimple_code (stmt) == GIMPLE_CALL)
2490     {
2491       tree lhs = gimple_get_lhs (stmt);
2492       /* A plain decl does not need it set.  */
2493       if (lhs && handled_component_p (lhs))
2494         {
2495           tree var = get_base_address (lhs);
2496           if (DECL_P (var)
2497               /* We are not going to mess with RESULT_DECL anyway.  */
2498               && TREE_CODE (var) != RESULT_DECL
2499               && is_gimple_reg_type (TREE_TYPE (var)))
2500             bitmap_set_bit (gimple_addressable_vars (cfun), DECL_UID (var));
2501         }
2502     }
2503
2504   /* Process each operand use.  For pointers, determine whether they
2505      are dereferenced by the statement, or whether their value
2506      escapes, etc.  */
2507   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
2508     {
2509       tree op, var;
2510       var_ann_t v_ann;
2511       struct ptr_info_def *pi;
2512       unsigned num_uses, num_loads, num_stores;
2513
2514       op = USE_FROM_PTR (use_p);
2515
2516       /* If STMT is a PHI node, OP may be an ADDR_EXPR.  If so, add it
2517          to the set of addressable variables.  */
2518       if (TREE_CODE (op) == ADDR_EXPR)
2519         {
2520           bitmap addressable_vars = gimple_addressable_vars (cfun);
2521
2522           gcc_assert (gimple_code (stmt) == GIMPLE_PHI);
2523           gcc_assert (addressable_vars);
2524
2525           /* PHI nodes don't have annotations for pinning the set
2526              of addresses taken, so we collect them here.
2527
2528              FIXME, should we allow PHI nodes to have annotations
2529              so that they can be treated like regular statements?
2530              Currently, they are treated as second-class
2531              statements.  */
2532           add_to_addressable_set (TREE_OPERAND (op, 0), &addressable_vars);
2533           continue;
2534         }
2535
2536       /* Ignore constants (they may occur in PHI node arguments).  */
2537       if (TREE_CODE (op) != SSA_NAME)
2538         continue;
2539
2540       var = SSA_NAME_VAR (op);
2541       v_ann = var_ann (var);
2542
2543       /* The base variable of an SSA name must be a GIMPLE register, and thus
2544          it cannot be aliased.  */
2545       gcc_assert (!may_be_aliased (var));
2546
2547       /* We are only interested in pointers.  */
2548       if (!POINTER_TYPE_P (TREE_TYPE (op)))
2549         continue;
2550
2551       pi = get_ptr_info (op);
2552
2553       /* Add OP to AI->PROCESSED_PTRS, if it's not there already.  */
2554       if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op)))
2555         {
2556           SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op));
2557           VEC_safe_push (tree, heap, ai->processed_ptrs, op);
2558         }
2559
2560       /* If STMT is a PHI node, then it will not have pointer
2561          dereferences and it will not be an escape point.  */
2562       if (gimple_code (stmt) == GIMPLE_PHI)
2563         continue;
2564
2565       /* Determine whether OP is a dereferenced pointer, and if STMT
2566          is an escape point, whether OP escapes.  */
2567       count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
2568
2569       /* For directly dereferenced pointers we can apply
2570          TBAA-pruning to their points-to set.  We may not count the
2571          implicit dereferences &PTR->FLD here.  */
2572       if (num_loads + num_stores > 0)
2573         pi->is_dereferenced = 1;
2574
2575       /* Handle a corner case involving address expressions of the
2576          form '&PTR->FLD'.  The problem with these expressions is that
2577          they do not represent a dereference of PTR.  However, if some
2578          other transformation propagates them into an INDIRECT_REF
2579          expression, we end up with '*(&PTR->FLD)' which is folded
2580          into 'PTR->FLD'.
2581
2582          So, if the original code had no other dereferences of PTR,
2583          the aliaser will not create memory tags for it, and when
2584          &PTR->FLD gets propagated to INDIRECT_REF expressions, the
2585          memory operations will receive no VDEF/VUSE operands.
2586
2587          One solution would be to have count_uses_and_derefs consider
2588          &PTR->FLD a dereference of PTR.  But that is wrong, since it
2589          is not really a dereference but an offset calculation.
2590
2591          What we do here is to recognize these special ADDR_EXPR
2592          nodes.  Since these expressions are never GIMPLE values (they
2593          are not GIMPLE invariants), they can only appear on the RHS
2594          of an assignment and their base address is always an
2595          INDIRECT_REF expression.  */
2596       if (is_gimple_assign (stmt)
2597           && gimple_assign_rhs_code (stmt) == ADDR_EXPR
2598           && !is_gimple_val (gimple_assign_rhs1 (stmt)))
2599         {
2600           /* If the RHS if of the form &PTR->FLD and PTR == OP, then
2601              this represents a potential dereference of PTR.  */
2602           tree rhs = gimple_assign_rhs1 (stmt);
2603           tree base = get_base_address (TREE_OPERAND (rhs, 0));
2604           if (TREE_CODE (base) == INDIRECT_REF
2605               && TREE_OPERAND (base, 0) == op)
2606             num_loads++;
2607         }
2608
2609       if (num_loads + num_stores > 0)
2610         {
2611           /* Mark OP as dereferenced.  In a subsequent pass,
2612              dereferenced pointers that point to a set of
2613              variables will be assigned a name tag to alias
2614              all the variables OP points to.  */
2615           pi->memory_tag_needed = 1;
2616
2617           /* ???  For always executed direct dereferences we can
2618              apply TBAA-pruning to their escape set.  */
2619
2620           /* Mark OP as being dereferenced.  */
2621           pointer_set_insert (ai->dereferenced_ptrs, var);
2622
2623           /* Update the frequency estimate for all the dereferences of
2624              pointer OP.  */
2625           update_mem_sym_stats_from_stmt (op, stmt, num_loads, num_stores);
2626
2627           /* Indicate that STMT contains pointer dereferences.  */
2628           stmt_dereferences_ptr_p = true;
2629         }
2630
2631       if (stmt_escape_type != NO_ESCAPE && num_loads + num_stores < num_uses)
2632         {
2633           /* If STMT is an escape point and STMT contains at
2634              least one direct use of OP, then the value of OP
2635              escapes and so the pointed-to variables need to
2636              be marked call-clobbered.  */
2637           pi->value_escapes_p = 1;
2638           pi->escape_mask |= stmt_escape_type;
2639
2640           /* If the statement makes a function call, assume
2641              that pointer OP will be dereferenced in a store
2642              operation inside the called function.  */
2643           if (is_gimple_call (stmt)
2644               || stmt_escape_type == ESCAPE_STORED_IN_GLOBAL)
2645             {
2646               pointer_set_insert (ai->dereferenced_ptrs, var);
2647               pi->memory_tag_needed = 1;
2648             }
2649         }
2650     }
2651
2652   if (gimple_code (stmt) == GIMPLE_PHI)
2653     return;
2654
2655   /* Mark stored variables in STMT as being written to and update the
2656      memory reference stats for all memory symbols referenced by STMT.  */
2657   if (gimple_references_memory_p (stmt))
2658     {
2659       unsigned i;
2660       bitmap_iterator bi;
2661
2662       mem_ref_stats->num_mem_stmts++;
2663
2664       /* Notice that we only update memory reference stats for symbols
2665          loaded and stored by the statement if the statement does not
2666          contain pointer dereferences and it is not a call/asm site.
2667          This is to avoid double accounting problems when creating
2668          memory partitions.  After computing points-to information,
2669          pointer dereference statistics are used to update the
2670          reference stats of the pointed-to variables, so here we
2671          should only update direct references to symbols.
2672
2673          Indirect references are not updated here for two reasons: (1)
2674          The first time we compute alias information, the sets
2675          LOADED/STORED are empty for pointer dereferences, (2) After
2676          partitioning, LOADED/STORED may have references to
2677          partitions, not the original pointed-to variables.  So, if we
2678          always counted LOADED/STORED here and during partitioning, we
2679          would count many symbols more than once.
2680
2681          This does cause some imprecision when a statement has a
2682          combination of direct symbol references and pointer
2683          dereferences (e.g., MEMORY_VAR = *PTR) or if a call site has
2684          memory symbols in its argument list, but these cases do not
2685          occur so frequently as to constitute a serious problem.  */
2686       if (!stmt_dereferences_ptr_p
2687           && stmt_escape_type != ESCAPE_TO_CALL
2688           && stmt_escape_type != ESCAPE_TO_PURE_CONST
2689           && stmt_escape_type != ESCAPE_TO_ASM)
2690         {
2691           if (gimple_stored_syms (stmt))
2692             EXECUTE_IF_SET_IN_BITMAP (gimple_stored_syms (stmt), 0, i, bi)
2693               update_mem_sym_stats_from_stmt (referenced_var (i), stmt, 0, 1);
2694
2695           if (gimple_loaded_syms (stmt))
2696             EXECUTE_IF_SET_IN_BITMAP (gimple_loaded_syms (stmt), 0, i, bi)
2697               update_mem_sym_stats_from_stmt (referenced_var (i), stmt, 1, 0);
2698         }
2699     }
2700 }
2701
2702 /* Update various related attributes like escaped addresses,
2703    pointer dereferences for loads and stores.  This is used
2704    when creating name tags and alias sets.  */
2705
2706 static void
2707 update_alias_info (struct alias_info *ai)
2708 {
2709   basic_block bb;
2710
2711   FOR_EACH_BB (bb)
2712     {
2713       gimple_stmt_iterator gsi;
2714       gimple phi;
2715
2716       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2717         {
2718           phi = gsi_stmt (gsi);
2719           if (is_gimple_reg (PHI_RESULT (phi)))
2720             update_alias_info_1 (phi, ai);
2721         }
2722
2723       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2724         update_alias_info_1 (gsi_stmt (gsi), ai);
2725     }
2726 }
2727
2728 /* Create memory tags for all the dereferenced pointers and build the
2729    ADDRESSABLE_VARS and POINTERS arrays used for building the may-alias
2730    sets.  Based on the address escape and points-to information collected
2731    earlier, this pass will also clear the TREE_ADDRESSABLE flag from those
2732    variables whose address is not needed anymore.  */
2733
2734 static void
2735 setup_pointers_and_addressables (struct alias_info *ai)
2736 {
2737   size_t num_addressable_vars, num_pointers;
2738   referenced_var_iterator rvi;
2739   tree var;
2740   VEC (tree, heap) *varvec = NULL;
2741   safe_referenced_var_iterator srvi;
2742
2743   /* Size up the arrays ADDRESSABLE_VARS and POINTERS.  */
2744   num_addressable_vars = num_pointers = 0;
2745   
2746   FOR_EACH_REFERENCED_VAR (var, rvi)
2747     {
2748       if (may_be_aliased (var))
2749         num_addressable_vars++;
2750
2751       if (POINTER_TYPE_P (TREE_TYPE (var)))
2752         {
2753           /* Since we don't keep track of volatile variables, assume that
2754              these pointers are used in indirect store operations.  */
2755           if (TREE_THIS_VOLATILE (var))
2756             pointer_set_insert (ai->dereferenced_ptrs, var);
2757
2758           num_pointers++;
2759         }
2760     }
2761
2762   /* Create ADDRESSABLE_VARS and POINTERS.  Note that these arrays are
2763      always going to be slightly bigger than we actually need them
2764      because some TREE_ADDRESSABLE variables will be marked
2765      non-addressable below and only pointers with unique symbol tags are
2766      going to be added to POINTERS.  */
2767   ai->addressable_vars = XCNEWVEC (struct alias_map_d *, num_addressable_vars);
2768   ai->pointers = XCNEWVEC (struct alias_map_d *, num_pointers);
2769   ai->num_addressable_vars = 0;
2770   ai->num_pointers = 0;
2771
2772   FOR_EACH_REFERENCED_VAR_SAFE (var, varvec, srvi)
2773     {
2774       /* Name memory tags already have flow-sensitive aliasing
2775          information, so they need not be processed by
2776          compute_flow_insensitive_aliasing.  Similarly, symbol memory
2777          tags are already accounted for when we process their
2778          associated pointer. 
2779       
2780          Structure fields, on the other hand, have to have some of this
2781          information processed for them, but it's pointless to mark them
2782          non-addressable (since they are fake variables anyway).  */
2783       if (MTAG_P (var))
2784         continue;
2785
2786       /* Remove the ADDRESSABLE flag from every addressable variable whose
2787          address is not needed anymore.  This is caused by the propagation
2788          of ADDR_EXPR constants into INDIRECT_REF expressions and the
2789          removal of dead pointer assignments done by the early scalar
2790          cleanup passes.  */
2791       if (TREE_ADDRESSABLE (var))
2792         {
2793           if (!bitmap_bit_p (gimple_addressable_vars (cfun), DECL_UID (var))
2794               && TREE_CODE (var) != RESULT_DECL
2795               && !is_global_var (var))
2796             {
2797               bool okay_to_mark = true;
2798
2799               /* Since VAR is now a regular GIMPLE register, we will need
2800                  to rename VAR into SSA afterwards.  */
2801               mark_sym_for_renaming (var);
2802
2803               /* The address of VAR is not needed, remove the
2804                  addressable bit, so that it can be optimized as a
2805                  regular variable.  */
2806               if (okay_to_mark)
2807                 {
2808                   /* The memory partition holding VAR will no longer
2809                      contain VAR, and statements referencing it will need
2810                      to be updated.  */
2811                   if (memory_partition (var))
2812                     mark_sym_for_renaming (memory_partition (var));
2813
2814                   mark_non_addressable (var);
2815                 }
2816             }
2817         }
2818
2819       /* Global variables and addressable locals may be aliased.  Create an
2820          entry in ADDRESSABLE_VARS for VAR.  */
2821       if (may_be_aliased (var))
2822         {
2823           create_alias_map_for (var, ai);
2824           mark_sym_for_renaming (var);
2825         }
2826
2827       /* Add pointer variables that have been dereferenced to the POINTERS
2828          array and create a symbol memory tag for them.  */
2829       if (POINTER_TYPE_P (TREE_TYPE (var)))
2830         {
2831           if (pointer_set_contains (ai->dereferenced_ptrs, var))
2832             {
2833               tree tag, old_tag;
2834               var_ann_t t_ann;
2835
2836               /* If pointer VAR still doesn't have a memory tag
2837                  associated with it, create it now or re-use an
2838                  existing one.  */
2839               tag = get_smt_for (var, ai);
2840               t_ann = var_ann (tag);
2841
2842               /* The symbol tag will need to be renamed into SSA
2843                  afterwards. Note that we cannot do this inside
2844                  get_smt_for because aliasing may run multiple times
2845                  and we only create symbol tags the first time.  */
2846               mark_sym_for_renaming (tag);
2847
2848               /* Similarly, if pointer VAR used to have another type
2849                  tag, we will need to process it in the renamer to
2850                  remove the stale virtual operands.  */
2851               old_tag = symbol_mem_tag (var);
2852               if (old_tag)
2853                 mark_sym_for_renaming (old_tag);
2854
2855               /* Associate the tag with pointer VAR.  */
2856               set_symbol_mem_tag (var, tag);
2857             }
2858           else
2859             {
2860               /* The pointer has not been dereferenced.  If it had a
2861                  symbol memory tag, remove it and mark the old tag for
2862                  renaming to remove it out of the IL.  */
2863               tree tag = symbol_mem_tag (var);
2864               if (tag)
2865                 {
2866                   mark_sym_for_renaming (tag);
2867                   set_symbol_mem_tag (var, NULL_TREE);
2868                 }
2869             }
2870         }
2871     }
2872
2873   VEC_free (tree, heap, varvec);
2874 }
2875
2876
2877 /* Determine whether to use .GLOBAL_VAR to model call clobbering
2878    semantics.  If the function makes no references to global
2879    variables and contains at least one call to a non-pure function,
2880    then we need to mark the side-effects of the call using .GLOBAL_VAR
2881    to represent all possible global memory referenced by the callee.  */
2882
2883 static void
2884 maybe_create_global_var (void)
2885 {
2886   /* No need to create it, if we have one already.  */
2887   if (gimple_global_var (cfun) == NULL_TREE)
2888     {
2889       struct mem_ref_stats_d *stats = gimple_mem_ref_stats (cfun);
2890
2891       /* Create .GLOBAL_VAR if there are no call-clobbered
2892          variables and the program contains a mixture of pure/const
2893          and regular function calls.  This is to avoid the problem
2894          described in PR 20115:
2895
2896               int X;
2897               int func_pure (void) { return X; }
2898               int func_non_pure (int a) { X += a; }
2899               int foo ()
2900               {
2901                 int a = func_pure ();
2902                 func_non_pure (a);
2903                 a = func_pure ();
2904                 return a;
2905               }
2906
2907          Since foo() has no call-clobbered variables, there is
2908          no relationship between the calls to func_pure and
2909          func_non_pure.  Since func_pure has no side-effects, value
2910          numbering optimizations elide the second call to func_pure.
2911          So, if we have some pure/const and some regular calls in the
2912          program we create .GLOBAL_VAR to avoid missing these
2913          relations.  */
2914       if (bitmap_empty_p (gimple_call_clobbered_vars (cfun))
2915           && stats->num_call_sites > 0
2916           && stats->num_pure_const_call_sites > 0
2917           && stats->num_call_sites > stats->num_pure_const_call_sites)
2918         create_global_var ();
2919     }
2920 }
2921
2922
2923 /* Return TRUE if pointer PTR may point to variable VAR.
2924    
2925    MEM_ALIAS_SET is the alias set for the memory location pointed-to by PTR
2926         This is needed because when checking for type conflicts we are
2927         interested in the alias set of the memory location pointed-to by
2928         PTR.  The alias set of PTR itself is irrelevant.
2929    
2930    VAR_ALIAS_SET is the alias set for VAR.  */
2931
2932 bool
2933 may_alias_p (tree ptr, alias_set_type mem_alias_set,
2934              tree var, alias_set_type var_alias_set,
2935              bool alias_set_only)
2936 {
2937   tree mem;
2938
2939   alias_stats.alias_queries++;
2940   alias_stats.simple_queries++;
2941
2942   /* By convention, a variable cannot alias itself.  */
2943   mem = symbol_mem_tag (ptr);
2944   if (mem == var)
2945     {
2946       alias_stats.alias_noalias++;
2947       alias_stats.simple_resolved++;
2948       return false;
2949     }
2950
2951   /* If -fargument-noalias-global is > 2, pointer arguments may
2952      not point to anything else.  */
2953   if (flag_argument_noalias > 2 && TREE_CODE (ptr) == PARM_DECL)
2954     {
2955       alias_stats.alias_noalias++;
2956       alias_stats.simple_resolved++;
2957       return false;
2958     }
2959
2960   /* If -fargument-noalias-global is > 1, pointer arguments may
2961      not point to global variables.  */
2962   if (flag_argument_noalias > 1 && is_global_var (var)
2963       && TREE_CODE (ptr) == PARM_DECL)
2964     {
2965       alias_stats.alias_noalias++;
2966       alias_stats.simple_resolved++;
2967       return false;
2968     }
2969
2970   /* If the pointed to memory has alias set zero, or the pointer
2971      is ref-all, or the pointer decl is marked that no TBAA is to
2972      be applied, the MEM can alias VAR.  */
2973   if (mem_alias_set == 0
2974       || DECL_POINTER_ALIAS_SET (ptr) == 0
2975       || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (ptr))
2976       || DECL_NO_TBAA_P (ptr))
2977     {
2978       alias_stats.alias_mayalias++;
2979       alias_stats.simple_resolved++;
2980       return true;
2981     }
2982
2983   gcc_assert (TREE_CODE (mem) == SYMBOL_MEMORY_TAG);
2984
2985   alias_stats.tbaa_queries++;
2986
2987   /* If the alias sets don't conflict then MEM cannot alias VAR.  */
2988   if (mem_alias_set != var_alias_set
2989       && !alias_set_subset_of (mem_alias_set, var_alias_set))
2990     {
2991       alias_stats.alias_noalias++;
2992       alias_stats.tbaa_resolved++;
2993       return false;
2994     }
2995
2996   /* If VAR is a record or union type, PTR cannot point into VAR
2997      unless there is some explicit address operation in the
2998      program that can reference a field of the type pointed-to by
2999      PTR.  This also assumes that the types of both VAR and PTR
3000      are contained within the compilation unit, and that there is
3001      no fancy addressing arithmetic associated with any of the
3002      types involved.  */
3003   if (mem_alias_set != 0 && var_alias_set != 0)
3004     {
3005       tree ptr_type = TREE_TYPE (ptr);
3006       tree var_type = TREE_TYPE (var);
3007       
3008       /* The star count is -1 if the type at the end of the
3009          pointer_to chain is not a record or union type. */ 
3010       if (!alias_set_only && 
3011           0 /* FIXME tuples ipa_type_escape_star_count_of_interesting_type (var_type) >= 0*/)
3012         {
3013           int ptr_star_count = 0;
3014
3015           /* ipa_type_escape_star_count_of_interesting_type is a
3016              little too restrictive for the pointer type, need to
3017              allow pointers to primitive types as long as those
3018              types cannot be pointers to everything.  */
3019           while (POINTER_TYPE_P (ptr_type))
3020             {
3021               /* Strip the *s off.  */ 
3022               ptr_type = TREE_TYPE (ptr_type);
3023               ptr_star_count++;
3024             }
3025           
3026           /* There does not appear to be a better test to see if
3027              the pointer type was one of the pointer to everything
3028              types.  */
3029           if (ptr_star_count > 0)
3030             {
3031               alias_stats.structnoaddress_queries++;
3032               if (ipa_type_escape_field_does_not_clobber_p (var_type, 
3033                                                             TREE_TYPE (ptr)))
3034                 {
3035                   alias_stats.structnoaddress_resolved++;
3036                   alias_stats.alias_noalias++;
3037                   return false;
3038                 }
3039             }
3040           else if (ptr_star_count == 0)
3041             {
3042               /* If PTR_TYPE was not really a pointer to type, it cannot 
3043                  alias.  */ 
3044               alias_stats.structnoaddress_queries++;
3045               alias_stats.structnoaddress_resolved++;
3046               alias_stats.alias_noalias++;
3047               return false;
3048             }
3049         }
3050     }
3051
3052   alias_stats.alias_mayalias++;
3053   return true;
3054 }
3055
3056 /* Return true, if PTR may point to a global variable.  */
3057
3058 bool
3059 may_point_to_global_var (tree ptr)
3060 {
3061   struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3062
3063   /* If we do not have points-to information for this variable,
3064      we have to punt.  */
3065   if (!pi
3066       || !pi->name_mem_tag)
3067     return true;
3068
3069   /* The name memory tag is marked as global variable if the points-to
3070      set contains a global variable.  */
3071   return is_global_var (pi->name_mem_tag);
3072 }
3073
3074 /* Add ALIAS to the set of variables that may alias VAR.  */
3075
3076 static void
3077 add_may_alias (tree var, tree alias)
3078 {
3079   /* Don't allow self-referential aliases.  */
3080   gcc_assert (var != alias);
3081
3082   /* ALIAS must be addressable if it's being added to an alias set.  */
3083 #if 1
3084   TREE_ADDRESSABLE (alias) = 1;
3085 #else
3086   gcc_assert (may_be_aliased (alias));
3087 #endif
3088
3089   /* VAR must be a symbol or a name tag.  */
3090   gcc_assert (TREE_CODE (var) == SYMBOL_MEMORY_TAG
3091               || TREE_CODE (var) == NAME_MEMORY_TAG);
3092
3093   if (MTAG_ALIASES (var) == NULL)
3094     MTAG_ALIASES (var) = BITMAP_ALLOC (&alias_bitmap_obstack);
3095   
3096   bitmap_set_bit (MTAG_ALIASES (var), DECL_UID (alias));
3097 }
3098
3099
3100 /* Mark pointer PTR as pointing to an arbitrary memory location.  */
3101
3102 static void
3103 set_pt_anything (tree ptr)
3104 {
3105   struct ptr_info_def *pi = get_ptr_info (ptr);
3106
3107   pi->pt_anything = 1;
3108   /* Anything includes global memory.  */
3109   pi->pt_global_mem = 1;
3110   pi->pt_vars = NULL;
3111
3112   /* The pointer used to have a name tag, but we now found it pointing
3113      to an arbitrary location.  The name tag needs to be renamed and
3114      disassociated from PTR.  */
3115   if (pi->name_mem_tag)
3116     {
3117       mark_sym_for_renaming (pi->name_mem_tag);
3118       pi->name_mem_tag = NULL_TREE;
3119     }
3120 }
3121
3122
3123 /* Return true if STMT is an "escape" site from the current function.  Escape
3124    sites those statements which might expose the address of a variable
3125    outside the current function.  STMT is an escape site iff:
3126
3127         1- STMT is a function call, or
3128         2- STMT is an __asm__ expression, or
3129         3- STMT is an assignment to a non-local variable, or
3130         4- STMT is a return statement.
3131
3132    Return the type of escape site found, if we found one, or NO_ESCAPE
3133    if none.  */
3134
3135 enum escape_type
3136 is_escape_site (gimple stmt)
3137 {
3138   if (is_gimple_call (stmt))
3139     {
3140       if (gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST))
3141         return ESCAPE_TO_PURE_CONST;
3142
3143       return ESCAPE_TO_CALL;
3144     }
3145   else if (gimple_code (stmt) == GIMPLE_ASM)
3146     return ESCAPE_TO_ASM;
3147   else if (is_gimple_assign (stmt))
3148     {
3149       tree lhs = gimple_assign_lhs (stmt);
3150
3151       /* Get to the base of _REF nodes.  */
3152       if (TREE_CODE (lhs) != SSA_NAME)
3153         lhs = get_base_address (lhs);
3154
3155       /* If we couldn't recognize the LHS of the assignment, assume that it
3156          is a non-local store.  */
3157       if (lhs == NULL_TREE)
3158         return ESCAPE_UNKNOWN;
3159
3160       if (gimple_assign_cast_p (stmt))
3161         {
3162           tree from = TREE_TYPE (gimple_assign_rhs1 (stmt));
3163           tree to = TREE_TYPE (lhs);
3164
3165           /* If the RHS is a conversion between a pointer and an integer, the
3166              pointer escapes since we can't track the integer.  */
3167           if (POINTER_TYPE_P (from) && !POINTER_TYPE_P (to))
3168             return ESCAPE_BAD_CAST;
3169         }
3170
3171       /* If the LHS is an SSA name, it can't possibly represent a non-local
3172          memory store.  */
3173       if (TREE_CODE (lhs) == SSA_NAME)
3174         return NO_ESCAPE;
3175
3176       /* If the LHS is a non-global decl, it isn't a non-local memory store.
3177          If the LHS escapes, the RHS escape is dealt with in the PTA solver.  */
3178       if (DECL_P (lhs)
3179           && !is_global_var (lhs))
3180         return NO_ESCAPE;
3181
3182       /* FIXME: LHS is not an SSA_NAME.  Even if it's an assignment to a
3183          local variables we cannot be sure if it will escape, because we
3184          don't have information about objects not in SSA form.  Need to
3185          implement something along the lines of
3186
3187          J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
3188          Midkiff, ``Escape analysis for java,'' in Proceedings of the
3189          Conference on Object-Oriented Programming Systems, Languages, and
3190          Applications (OOPSLA), pp. 1-19, 1999.  */
3191       return ESCAPE_STORED_IN_GLOBAL;
3192     }
3193   else if (gimple_code (stmt) == GIMPLE_RETURN)
3194     return ESCAPE_TO_RETURN;
3195
3196   return NO_ESCAPE;
3197 }
3198
3199 /* Create a new memory tag of type TYPE.
3200    Does NOT push it into the current binding.  */
3201
3202 tree
3203 create_tag_raw (enum tree_code code, tree type, const char *prefix)
3204 {
3205   tree tmp_var;
3206
3207   tmp_var = build_decl (code, create_tmp_var_name (prefix), type);
3208
3209   /* Memory tags are always writable and non-static.  */
3210   TREE_READONLY (tmp_var) = 0;
3211   TREE_STATIC (tmp_var) = 0;
3212
3213   /* It doesn't start out global.  */
3214   MTAG_GLOBAL (tmp_var) = 0;
3215   TREE_USED (tmp_var) = 1;
3216
3217   return tmp_var;
3218 }
3219
3220 /* Create a new memory tag of type TYPE.  If IS_TYPE_TAG is true, the tag
3221    is considered to represent all the pointers whose pointed-to types are
3222    in the same alias set class.  Otherwise, the tag represents a single
3223    SSA_NAME pointer variable.  */
3224
3225 static tree
3226 create_memory_tag (tree type, bool is_type_tag)
3227 {
3228   tree tag = create_tag_raw (is_type_tag ? SYMBOL_MEMORY_TAG : NAME_MEMORY_TAG,
3229                              type, (is_type_tag) ? "SMT" : "NMT");
3230
3231   /* By default, memory tags are local variables.  Alias analysis will
3232      determine whether they should be considered globals.  */
3233   DECL_CONTEXT (tag) = current_function_decl;
3234
3235   /* Memory tags are by definition addressable.  */
3236   TREE_ADDRESSABLE (tag) = 1;
3237
3238   set_symbol_mem_tag (tag, NULL_TREE);
3239
3240   /* Add the tag to the symbol table.  */
3241   add_referenced_var (tag);
3242
3243   return tag;
3244 }
3245
3246
3247 /* Create a name memory tag to represent a specific SSA_NAME pointer P_i.
3248    This is used if P_i has been found to point to a specific set of
3249    variables or to a non-aliased memory location like the address returned
3250    by malloc functions.  */
3251
3252 static tree
3253 get_nmt_for (tree ptr)
3254 {
3255   struct ptr_info_def *pi = get_ptr_info (ptr);
3256   tree tag = pi->name_mem_tag;
3257
3258   if (tag == NULL_TREE)
3259     tag = create_memory_tag (TREE_TYPE (TREE_TYPE (ptr)), false);
3260   return tag;
3261 }
3262
3263
3264 /* Return the symbol memory tag associated to pointer PTR.  A memory
3265    tag is an artificial variable that represents the memory location
3266    pointed-to by PTR.  It is used to model the effects of pointer
3267    de-references on addressable variables.
3268    
3269    AI points to the data gathered during alias analysis.  This
3270    function populates the array AI->POINTERS.  */
3271
3272 static tree
3273 get_smt_for (tree ptr, struct alias_info *ai)
3274 {
3275   size_t i;
3276   tree tag;
3277   tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3278   alias_set_type tag_set;
3279
3280   /* Get the alias set to be used for the pointed-to memory.  If that
3281      differs from what we would get from looking at the type adjust
3282      the tag_type to void to make sure we get a proper alias set from
3283      just looking at the SMT we create.  */
3284   tag_set = get_alias_set (tag_type);
3285   if (TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (ptr))
3286       /* This is overly conservative but we do not want to assign
3287          restrict alias sets here (which if they are not assigned
3288          are -2 but still "known").  */
3289       || DECL_POINTER_ALIAS_SET_KNOWN_P (ptr))
3290     {
3291       tag_set = 0;
3292       tag_type = void_type_node;
3293     }
3294
3295   /* To avoid creating unnecessary memory tags, only create one memory tag
3296      per alias set class.  Note that it may be tempting to group
3297      memory tags based on conflicting alias sets instead of
3298      equivalence.  That would be wrong because alias sets are not
3299      necessarily transitive (as demonstrated by the libstdc++ test
3300      23_containers/vector/cons/4.cc).  Given three alias sets A, B, C
3301      such that conflicts (A, B) == true and conflicts (A, C) == true,
3302      it does not necessarily follow that conflicts (B, C) == true.  */
3303   for (i = 0, tag = NULL_TREE; i < ai->num_pointers; i++)
3304     {
3305       struct alias_map_d *curr = ai->pointers[i];
3306       tree curr_tag = symbol_mem_tag (curr->var);
3307       if (tag_set == curr->set)
3308         {
3309           tag = curr_tag;
3310           break;
3311         }
3312     }
3313
3314   /* If VAR cannot alias with any of the existing memory tags, create a new
3315      tag for PTR and add it to the POINTERS array.  */
3316   if (tag == NULL_TREE)
3317     {
3318       struct alias_map_d *alias_map;
3319
3320       /* If PTR did not have a symbol tag already, create a new SMT.*
3321          artificial variable representing the memory location
3322          pointed-to by PTR.  */
3323       tag = symbol_mem_tag (ptr);
3324       if (tag == NULL_TREE
3325           || tag_set != get_alias_set (tag))
3326         tag = create_memory_tag (tag_type, true);
3327
3328       /* Add PTR to the POINTERS array.  Note that we are not interested in
3329          PTR's alias set.  Instead, we cache the alias set for the memory that
3330          PTR points to.  */
3331       alias_map = XCNEW (struct alias_map_d);
3332       alias_map->var = ptr;
3333       alias_map->set = tag_set;
3334       ai->pointers[ai->num_pointers++] = alias_map;
3335     }
3336
3337   /* If the pointed-to type is volatile, so is the tag.  */
3338   TREE_THIS_VOLATILE (tag) |= TREE_THIS_VOLATILE (tag_type);
3339
3340   /* Make sure that the symbol tag has the same alias set as the
3341      pointed-to type or at least accesses through the pointer will
3342      alias that set.  The latter can happen after the vectorizer
3343      created pointers of vector type.  */
3344   gcc_assert (tag_set == get_alias_set (tag)
3345               || alias_set_subset_of (tag_set, get_alias_set (tag)));
3346
3347   return tag;
3348 }
3349
3350
3351 /* Create GLOBAL_VAR, an artificial global variable to act as a
3352    representative of all the variables that may be clobbered by function
3353    calls.  */
3354
3355 static void
3356 create_global_var (void)
3357 {
3358   tree global_var = build_decl (VAR_DECL, get_identifier (".GLOBAL_VAR"),
3359                                 void_type_node);
3360   DECL_ARTIFICIAL (global_var) = 1;
3361   TREE_READONLY (global_var) = 0;
3362   DECL_EXTERNAL (global_var) = 1;
3363   TREE_STATIC (global_var) = 1;
3364   TREE_USED (global_var) = 1;
3365   DECL_CONTEXT (global_var) = NULL_TREE;
3366   TREE_THIS_VOLATILE (global_var) = 0;
3367   TREE_ADDRESSABLE (global_var) = 0;
3368
3369   create_var_ann (global_var);
3370   mark_call_clobbered (global_var, ESCAPE_UNKNOWN);
3371   add_referenced_var (global_var);
3372   mark_sym_for_renaming (global_var);
3373   cfun->gimple_df->global_var = global_var;
3374 }
3375
3376
3377 /* Dump alias statistics on FILE.  */
3378
3379 static void 
3380 dump_alias_stats (FILE *file)
3381 {
3382   const char *funcname
3383     = lang_hooks.decl_printable_name (current_function_decl, 2);
3384   fprintf (file, "\nAlias statistics for %s\n\n", funcname);
3385   fprintf (file, "Total alias queries:\t%u\n", alias_stats.alias_queries);
3386   fprintf (file, "Total alias mayalias results:\t%u\n", 
3387            alias_stats.alias_mayalias);
3388   fprintf (file, "Total alias noalias results:\t%u\n",
3389            alias_stats.alias_noalias);
3390   fprintf (file, "Total simple queries:\t%u\n",
3391            alias_stats.simple_queries);
3392   fprintf (file, "Total simple resolved:\t%u\n",
3393            alias_stats.simple_resolved);
3394   fprintf (file, "Total TBAA queries:\t%u\n",
3395            alias_stats.tbaa_queries);
3396   fprintf (file, "Total TBAA resolved:\t%u\n",
3397            alias_stats.tbaa_resolved);
3398   fprintf (file, "Total non-addressable structure type queries:\t%u\n",
3399            alias_stats.structnoaddress_queries);
3400   fprintf (file, "Total non-addressable structure type resolved:\t%u\n",
3401            alias_stats.structnoaddress_resolved);
3402 }
3403   
3404
3405 /* Dump alias information on FILE.  */
3406
3407 void
3408 dump_alias_info (FILE *file)
3409 {
3410   size_t i;
3411   const char *funcname
3412     = lang_hooks.decl_printable_name (current_function_decl, 2);
3413   referenced_var_iterator rvi;
3414   tree var;
3415
3416   fprintf (file, "\nAlias information for %s\n\n", funcname);
3417
3418   dump_memory_partitions (file);
3419
3420   fprintf (file, "\nFlow-insensitive alias information for %s\n\n", funcname);
3421
3422   fprintf (file, "Aliased symbols\n\n");
3423   
3424   FOR_EACH_REFERENCED_VAR (var, rvi)
3425     {
3426       if (may_be_aliased (var))
3427         dump_variable (file, var);
3428     }
3429
3430   fprintf (file, "\nDereferenced pointers\n\n");
3431
3432   FOR_EACH_REFERENCED_VAR (var, rvi)
3433     if (symbol_mem_tag (var))
3434       dump_variable (file, var);
3435
3436   fprintf (file, "\nSymbol memory tags\n\n");
3437   
3438   FOR_EACH_REFERENCED_VAR (var, rvi)
3439     {
3440       if (TREE_CODE (var) == SYMBOL_MEMORY_TAG)
3441         dump_variable (file, var);
3442     }
3443
3444   fprintf (file, "\n\nFlow-sensitive alias information for %s\n\n", funcname);
3445
3446   fprintf (file, "SSA_NAME pointers\n\n");
3447   for (i = 1; i < num_ssa_names; i++)
3448     {
3449       tree ptr = ssa_name (i);
3450       struct ptr_info_def *pi;
3451       
3452       if (ptr == NULL_TREE)
3453         continue;
3454
3455       pi = SSA_NAME_PTR_INFO (ptr);
3456       if (!SSA_NAME_IN_FREE_LIST (ptr)
3457           && pi
3458           && pi->name_mem_tag)
3459         dump_points_to_info_for (file, ptr);
3460     }
3461
3462   fprintf (file, "\nName memory tags\n\n");
3463   
3464   FOR_EACH_REFERENCED_VAR (var, rvi)
3465     {
3466       if (TREE_CODE (var) == NAME_MEMORY_TAG)
3467         dump_variable (file, var);
3468     }
3469
3470   fprintf (file, "\n");
3471 }
3472
3473
3474 /* Dump alias information on stderr.  */
3475
3476 void
3477 debug_alias_info (void)
3478 {
3479   dump_alias_info (stderr);
3480 }
3481
3482
3483 /* Return the alias information associated with pointer T.  It creates a
3484    new instance if none existed.  */
3485
3486 struct ptr_info_def *
3487 get_ptr_info (tree t)
3488 {
3489   struct ptr_info_def *pi;
3490
3491   gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
3492
3493   pi = SSA_NAME_PTR_INFO (t);
3494   if (pi == NULL)
3495     {
3496       pi = GGC_CNEW (struct ptr_info_def);
3497       SSA_NAME_PTR_INFO (t) = pi;
3498     }
3499
3500   return pi;
3501 }
3502
3503 /* Dump points-to information for SSA_NAME PTR into FILE.  */
3504
3505 void
3506 dump_points_to_info_for (FILE *file, tree ptr)
3507 {
3508   struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3509
3510   print_generic_expr (file, ptr, dump_flags);
3511
3512   if (pi)
3513     {
3514       if (pi->name_mem_tag)
3515         {
3516           fprintf (file, ", name memory tag: ");
3517           print_generic_expr (file, pi->name_mem_tag, dump_flags);
3518         }
3519
3520       if (pi->is_dereferenced)
3521         fprintf (file, ", is dereferenced");
3522       else if (pi->memory_tag_needed)
3523         fprintf (file, ", is dereferenced in call");
3524
3525       if (pi->value_escapes_p)
3526         fprintf (file, ", its value escapes");
3527
3528       if (pi->pt_anything)
3529         fprintf (file, ", points-to anything");
3530
3531       if (pi->pt_null)
3532         fprintf (file, ", points-to NULL");
3533
3534       if (pi->pt_vars)
3535         {
3536           fprintf (file, ", points-to vars: ");
3537           dump_decl_set (file, pi->pt_vars);
3538         }
3539     }
3540
3541   fprintf (file, "\n");
3542 }
3543
3544
3545 /* Dump points-to information for VAR into stderr.  */
3546
3547 void
3548 debug_points_to_info_for (tree var)
3549 {
3550   dump_points_to_info_for (stderr, var);
3551 }
3552
3553
3554 /* Dump points-to information into FILE.  NOTE: This function is slow, as
3555    it needs to traverse the whole CFG looking for pointer SSA_NAMEs.  */
3556
3557 void
3558 dump_points_to_info (FILE *file ATTRIBUTE_UNUSED)
3559 {
3560   basic_block bb;
3561   gimple_stmt_iterator si;
3562   ssa_op_iter iter;
3563   const char *fname =
3564     lang_hooks.decl_printable_name (current_function_decl, 2);
3565   referenced_var_iterator rvi;
3566   tree var;
3567
3568   fprintf (file, "\n\nPointed-to sets for pointers in %s\n\n", fname);
3569
3570   /* First dump points-to information for the default definitions of
3571      pointer variables.  This is necessary because default definitions are
3572      not part of the code.  */
3573   FOR_EACH_REFERENCED_VAR (var, rvi)
3574     {
3575       if (POINTER_TYPE_P (TREE_TYPE (var)))
3576         {
3577           tree def = gimple_default_def (cfun, var);
3578           if (def)
3579             dump_points_to_info_for (file, def);
3580         }
3581     }
3582
3583   /* Dump points-to information for every pointer defined in the program.  */
3584   FOR_EACH_BB (bb)
3585     {
3586       for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
3587         {
3588           gimple phi = gsi_stmt (si);
3589           tree ptr = PHI_RESULT (phi);
3590           if (POINTER_TYPE_P (TREE_TYPE (ptr)))
3591             dump_points_to_info_for (file, ptr);
3592         }
3593
3594         for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
3595           {
3596             gimple stmt = gsi_stmt (si);
3597             tree def;
3598             FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
3599               if (TREE_CODE (def) == SSA_NAME
3600                   && POINTER_TYPE_P (TREE_TYPE (def)))
3601                 dump_points_to_info_for (file, def);
3602           }
3603     }
3604
3605   fprintf (file, "\n");
3606 }
3607
3608
3609 /* Dump points-to info pointed to by PTO into STDERR.  */
3610
3611 void
3612 debug_points_to_info (void)
3613 {
3614   dump_points_to_info (stderr);
3615 }
3616
3617 /* Dump to FILE the list of variables that may be aliasing VAR.  */
3618
3619 void
3620 dump_may_aliases_for (FILE *file, tree var)
3621 {
3622   bitmap aliases;
3623   
3624   aliases = MTAG_ALIASES (var);
3625   if (aliases)
3626     {
3627       bitmap_iterator bi;
3628       unsigned int i;
3629       tree al;
3630
3631       fprintf (file, "{ ");
3632       EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
3633         {
3634           al = referenced_var (i);
3635           print_generic_expr (file, al, dump_flags);
3636           fprintf (file, " ");
3637         }
3638       fprintf (file, "}");
3639     }
3640 }
3641
3642
3643 /* Dump to stderr the list of variables that may be aliasing VAR.  */
3644
3645 void
3646 debug_may_aliases_for (tree var)
3647 {
3648   dump_may_aliases_for (stderr, var);
3649 }
3650
3651 /* Return true if VAR may be aliased.  */
3652
3653 bool
3654 may_be_aliased (tree var)
3655 {
3656   /* Obviously.  */
3657   if (TREE_ADDRESSABLE (var))
3658     return true;
3659
3660   /* Globally visible variables can have their addresses taken by other
3661      translation units.  */
3662   if (MTAG_P (var)
3663       && MTAG_GLOBAL (var))
3664     return true;
3665   else if (!MTAG_P (var)
3666            && (DECL_EXTERNAL (var) || TREE_PUBLIC (var)))
3667     return true;
3668
3669   /* Automatic variables can't have their addresses escape any other
3670      way.  This must be after the check for global variables, as
3671      extern declarations do not have TREE_STATIC set.  */
3672   if (!TREE_STATIC (var))
3673     return false;
3674
3675   return false;
3676 }
3677
3678 /* The following is based on code in add_stmt_operand to ensure that the
3679    same defs/uses/vdefs/vuses will be found after replacing a reference
3680    to var (or ARRAY_REF to var) with an INDIRECT_REF to ptr whose value
3681    is the address of var.  Return a memtag for the ptr, after adding the 
3682    proper may_aliases to it (which are the aliases of var, if it has any,
3683    or var itself).  */
3684
3685 static tree
3686 add_may_alias_for_new_tag (tree tag, tree var)
3687 {
3688   bitmap aliases = NULL;
3689   
3690   if (MTAG_P (var))
3691     aliases = may_aliases (var);
3692
3693   /* Case 1: |aliases| == 1  */
3694   if (aliases
3695       && bitmap_single_bit_set_p (aliases))
3696     {
3697       tree ali = referenced_var (bitmap_first_set_bit (aliases));
3698       if (TREE_CODE (ali) == SYMBOL_MEMORY_TAG)
3699         return ali;
3700     }
3701
3702   /* Case 2: |aliases| == 0  */
3703   if (aliases == NULL)
3704     add_may_alias (tag, var);
3705   else
3706     {
3707       /* Case 3: |aliases| > 1  */
3708       union_alias_set_into (tag, aliases);
3709     }
3710   return tag;
3711 }
3712
3713 /* Create a new symbol tag for PTR.  Construct the may-alias list of
3714    this type tag so that it has the aliasing of VAR according to the
3715    location accessed by EXPR.
3716
3717    Note, the set of aliases represented by the new symbol tag are not
3718    marked for renaming.  */
3719
3720 void
3721 new_type_alias (tree ptr, tree var, tree expr)
3722 {
3723   tree tag_type = TREE_TYPE (TREE_TYPE (ptr));
3724   tree tag;
3725   tree ali = NULL_TREE;
3726   HOST_WIDE_INT offset, size, maxsize;
3727   tree ref;
3728
3729   gcc_assert (symbol_mem_tag (ptr) == NULL_TREE);
3730   gcc_assert (!MTAG_P (var));
3731
3732   ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
3733   gcc_assert (ref);
3734
3735   tag = create_memory_tag (tag_type, true);
3736   set_symbol_mem_tag (ptr, tag);
3737
3738   ali = add_may_alias_for_new_tag (tag, var);
3739
3740   set_symbol_mem_tag (ptr, ali);
3741   MTAG_GLOBAL (tag) = is_global_var (var);
3742 }
3743
3744
3745 /* Reset the call_clobbered flags on our referenced vars.  In
3746    theory, this only needs to be done for globals.  */
3747
3748 static unsigned int
3749 reset_cc_flags (void)
3750 {
3751   tree var;
3752   referenced_var_iterator rvi;
3753
3754   FOR_EACH_REFERENCED_VAR (var, rvi)
3755     var_ann (var)->call_clobbered = false;
3756   return 0;
3757 }
3758
3759 struct gimple_opt_pass pass_reset_cc_flags =
3760 {
3761  {
3762   GIMPLE_PASS,
3763   NULL,          /* name */
3764   NULL,          /* gate */
3765   reset_cc_flags, /* execute */
3766   NULL,                  /* sub */
3767   NULL,                  /* next */
3768   0,                     /* static_pass_number */
3769   0,                     /* tv_id */
3770   PROP_referenced_vars |PROP_cfg, /* properties_required */
3771   0,                     /* properties_provided */
3772   0,                     /* properties_destroyed */
3773   0,                     /* todo_flags_start */
3774   0                      /* todo_flags_finish */
3775  }
3776 };
3777
3778
3779 /* A dummy pass to cause aliases to be computed via TODO_rebuild_alias.  */
3780
3781 struct gimple_opt_pass pass_build_alias =
3782 {
3783  {
3784   GIMPLE_PASS,
3785   "alias",                  /* name */
3786   NULL,                     /* gate */
3787   NULL,                     /* execute */
3788   NULL,                     /* sub */
3789   NULL,                     /* next */
3790   0,                        /* static_pass_number */
3791   0,                        /* tv_id */
3792   PROP_cfg | PROP_ssa,      /* properties_required */
3793   PROP_alias,               /* properties_provided */
3794   0,                        /* properties_destroyed */
3795   0,                        /* todo_flags_start */
3796   TODO_rebuild_alias | TODO_dump_func  /* todo_flags_finish */
3797  }
3798 };