OSDN Git Service

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