OSDN Git Service

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